From patchwork Tue Aug 22 19:17:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bart Van Assche X-Patchwork-Id: 716402 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6734FEE49AE for ; Tue, 22 Aug 2023 19:19:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230079AbjHVTTE (ORCPT ); Tue, 22 Aug 2023 15:19:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36564 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230078AbjHVTTD (ORCPT ); Tue, 22 Aug 2023 15:19:03 -0400 Received: from mail-pg1-f169.google.com (mail-pg1-f169.google.com [209.85.215.169]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D00C5E4B; Tue, 22 Aug 2023 12:18:57 -0700 (PDT) Received: by mail-pg1-f169.google.com with SMTP id 41be03b00d2f7-5656a5c6721so2770479a12.1; Tue, 22 Aug 2023 12:18:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692731937; x=1693336737; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+kT0lODXnQs5o0Wfuh374uESQu7wFS2RjdWOYRnRcYk=; b=O10P9rITFhzR/IDw8rcB5cyDfKEMwXNBCPOx4+s4bgr5TEO6brKKxez+lqxAN5OZ9Z 3AcunZ9RakIf5dSxhnLp8jz8pTYN/naBiToXekpVHhjFLDVq0o5K41L/1Z6qWeTbi7u5 pLHH4d8o+H2eMls0EwLiYAI49+SV/Gw+HKFWJD9xQ5IPMgFCz5s4UHkQ0MaC10FHJ0rO NHxHSaVmSB4CIuMqXP+7tQbtoMrtnMUxk78NU9epBN21XobmZH5htudGgIrgJ79I0foH prkYSLApBvCj72BVUTN9c60gDFmTTcwyaemTbu4oK6hvYW43YjSucXBH+eXjcuPSGA2u MI+w== X-Gm-Message-State: AOJu0YxoRqK8Ey+dpoFHHRQzx/sSskPw2ZdYVNQAp8v3K3JZ6vUmAF4O ej/OXdxHIZclcUxTfF99nJ4= X-Google-Smtp-Source: AGHT+IGr6l0caQkI8ZEkUdDCB7rvs2JFMQrdCELV38EHMPouevX7/TpkMGh5dFYtWHJyk3y5qnq3Mg== X-Received: by 2002:a17:90a:694d:b0:263:f435:ef2d with SMTP id j13-20020a17090a694d00b00263f435ef2dmr6973173pjm.10.1692731937236; Tue, 22 Aug 2023 12:18:57 -0700 (PDT) Received: from bvanassche-linux.mtv.corp.google.com ([2620:15c:211:201:88be:bf57:de29:7cc]) by smtp.gmail.com with ESMTPSA id m11-20020a17090a414b00b002696bd123e4sm8081632pjg.46.2023.08.22.12.18.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 12:18:56 -0700 (PDT) From: Bart Van Assche To: Jens Axboe Cc: linux-block@vger.kernel.org, linux-scsi@vger.kernel.org, "Martin K . Petersen" , Christoph Hellwig , Bart Van Assche , Damien Le Moal , Ming Lei , "James E.J. Bottomley" Subject: [PATCH v11 06/16] scsi: sd: Sort commands by LBA before resubmitting Date: Tue, 22 Aug 2023 12:17:01 -0700 Message-ID: <20230822191822.337080-7-bvanassche@acm.org> X-Mailer: git-send-email 2.42.0.rc1.204.g551eb34607-goog In-Reply-To: <20230822191822.337080-1-bvanassche@acm.org> References: <20230822191822.337080-1-bvanassche@acm.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org Sort SCSI commands by LBA before the SCSI error handler resubmits these commands. This is necessary when resubmitting zoned writes (REQ_OP_WRITE) if multiple writes have been queued for a single zone. Cc: Martin K. Petersen Cc: Damien Le Moal Cc: Christoph Hellwig Cc: Ming Lei Signed-off-by: Bart Van Assche --- drivers/scsi/sd.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index 3c668cfb146d..d4feac5de17a 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include @@ -117,6 +118,8 @@ static void sd_uninit_command(struct scsi_cmnd *SCpnt); static int sd_done(struct scsi_cmnd *); static void sd_eh_reset(struct scsi_cmnd *); static int sd_eh_action(struct scsi_cmnd *, int); +static bool sd_needs_prepare_resubmit(struct scsi_cmnd *cmd); +static void sd_prepare_resubmit(struct list_head *cmd_list); static void sd_read_capacity(struct scsi_disk *sdkp, unsigned char *buffer); static void scsi_disk_release(struct device *cdev); @@ -617,6 +620,8 @@ static struct scsi_driver sd_template = { .done = sd_done, .eh_action = sd_eh_action, .eh_reset = sd_eh_reset, + .eh_needs_prepare_resubmit = sd_needs_prepare_resubmit, + .eh_prepare_resubmit = sd_prepare_resubmit, }; /* @@ -2018,6 +2023,46 @@ static int sd_eh_action(struct scsi_cmnd *scmd, int eh_disp) return eh_disp; } +static bool sd_needs_prepare_resubmit(struct scsi_cmnd *cmd) +{ + struct request *rq = scsi_cmd_to_rq(cmd); + + return !rq->q->limits.use_zone_write_lock && + blk_rq_is_seq_zoned_write(rq); +} + +static int sd_cmp_sector(void *priv, const struct list_head *_a, + const struct list_head *_b) +{ + struct scsi_cmnd *a = list_entry(_a, typeof(*a), eh_entry); + struct scsi_cmnd *b = list_entry(_b, typeof(*b), eh_entry); + struct request *rq_a = scsi_cmd_to_rq(a); + struct request *rq_b = scsi_cmd_to_rq(b); + bool use_zwl_a = rq_a->q->limits.use_zone_write_lock; + bool use_zwl_b = rq_b->q->limits.use_zone_write_lock; + + /* + * Order the commands that need zone write locking after the commands + * that do not need zone write locking. Order the commands that do not + * need zone write locking by LBA. Do not reorder the commands that + * need zone write locking. See also the comment above the list_sort() + * definition. + */ + if (use_zwl_a || use_zwl_b) + return use_zwl_a > use_zwl_b; + return blk_rq_pos(rq_a) > blk_rq_pos(rq_b); +} + +static void sd_prepare_resubmit(struct list_head *cmd_list) +{ + /* + * Sort pending SCSI commands in starting sector order. This is + * important if one of the SCSI devices associated with @shost is a + * zoned block device for which zone write locking is disabled. + */ + list_sort(NULL, cmd_list, sd_cmp_sector); +} + static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd) { struct request *req = scsi_cmd_to_rq(scmd);