From patchwork Thu Jun 12 06:02:10 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Damien Le Moal X-Patchwork-Id: 895894 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26E0E153598 for ; Thu, 12 Jun 2025 06:03:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749708239; cv=none; b=YSldp6V4Ctbfp5B29PEmdWPVaPlzJunUw36TRuSdPV+X8/T7Q+yttd+tKEwfVmm2H3CZHw74EKVcw3/sBBruyAUAoHQ2P7YGw9hCb9Bs1f8qRNso4Fum1/rZXT61Kn1ukwbtZ9ZWkcro+yvoQCF4TAgBa//GwOEXNqhRg7g0Og0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749708239; c=relaxed/simple; bh=Qkk9fJUpwJdHjR+9Wipu+d2XRSMaJTKkFESl97m/QX8=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=FVgXMEtX2y+y7hbckx/Xm3EzOxMdbh/g8t2cs/p+6kJGAQChm6EA1jqbPwyt4yrqPX0CW9w9koV5qRklNLJqzD37LKx1Pv4TXlERMRXG3iYYB1I6Mw9hb70iy19CPzbmgI4SkivDsi7WbGY+r2841nl+o4LGnuiREgcSxJNsK4s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FRTstZNS; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FRTstZNS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44860C4CEEB; Thu, 12 Jun 2025 06:03:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749708238; bh=Qkk9fJUpwJdHjR+9Wipu+d2XRSMaJTKkFESl97m/QX8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=FRTstZNS1+cjzJlNWRMBTe1nbNHiUocNUOB7igeVkqJ+6BJR+lLhVpub1nL66E4z1 KirinuY/3G3cL6MErDDM6r0+xCAZL7oBcS3/uk2vLLJyjesqKtEtXyYe6eix5cPJmy nR4f9uFu7FF5Nodix4FGkBRYFpbXsEE20dc8IQnBOipv9sUzmSTTHeNnPPPNnmXdV9 0kqvlmEbcf/o0S4i78frCvCEo7o1HQJjPUHOk5v5SzWCeQaoJ+yY96YT9OKQPT8Hh0 HdsmOeWqsPhQ5fDcma+n2VU4Ij8nE+MT0MEeGOb0piLONGxedmkFQBdVHrbGP8KbHZ rJzGUZJSqehyg== From: Damien Le Moal To: "Martin K . Petersen" , linux-scsi@vger.kernel.org Subject: [PATCH v3 1/2] scsi: sd: Prevent logical_to_bytes() from returning overflowed values Date: Thu, 12 Jun 2025 15:02:10 +0900 Message-ID: <20250612060211.1970933-2-dlemoal@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250612060211.1970933-1-dlemoal@kernel.org> References: <20250612060211.1970933-1-dlemoal@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Make sure that logical_to_bytes() does not return an overflowed value by changing its return type from unsigned int (32-bits) to size_t (64-bits). Signed-off-by: Damien Le Moal --- drivers/scsi/sd.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h index 36382eca941c..3803eb8cb532 100644 --- a/drivers/scsi/sd.h +++ b/drivers/scsi/sd.h @@ -213,7 +213,7 @@ static inline sector_t logical_to_sectors(struct scsi_device *sdev, sector_t blo return blocks << (ilog2(sdev->sector_size) - 9); } -static inline unsigned int logical_to_bytes(struct scsi_device *sdev, sector_t blocks) +static inline size_t logical_to_bytes(struct scsi_device *sdev, sector_t blocks) { return blocks * sdev->sector_size; } From patchwork Thu Jun 12 06:02:11 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Damien Le Moal X-Patchwork-Id: 897290 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EDAB01EEA28 for ; Thu, 12 Jun 2025 06:03:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749708240; cv=none; b=ZOWgwltJZMQwkm0i9JTXjsekMAlY7QoJ4txPIPiFwkKdlQuFBI8DmzmkwGt9paBAk+lmZdwEkJ105ERs541cFmjokA6Oe1DBp9b//ifNcwaB+L2vuDZFlffOCn3XWzvbGY8krqWjXVABjTyVDVJMc5OU0nekGIna/NvcongX0ZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749708240; c=relaxed/simple; bh=WtGXBAqnnP89EcvfhqMvn4YNXCU8xT6+2QPx+55dDrs=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AkJ08J+eZNukK4p/SyYrhB0WU8hfuImxfK9uVOokwjfE7uYepRP5wi78101BRlTSNZ7fCx7+HY3kWAREWeXrfkI3T1G/whogWXLFMI2HN0DCpiw2GgHNsPbqE44tIxY21VrWyriIX20dJ5xd/PW2oQKU610LuTR6pj9/nMWZqQM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kW0ZVSQg; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kW0ZVSQg" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1577BC4CEEA; Thu, 12 Jun 2025 06:03:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749708239; bh=WtGXBAqnnP89EcvfhqMvn4YNXCU8xT6+2QPx+55dDrs=; h=From:To:Subject:Date:In-Reply-To:References:From; b=kW0ZVSQgujU1rtMS6vfFURiM/DG0M/wn1siyvG5mq0sJMuoF9Keb9tCOVqCQtlQ3H TnD/bGMud0awD6lre5Ly3pawMBpb85A1nOu6q+6dnSYLcpiLMsgF28E7n9WAvyRkP8 D/W9dxFjDLKoq4uCuprzC5z6d2S31YN603Bj6wO8WNuG8TYy/elJs5yZ99LrWOge9p q19kbxHt6Tt57JiuWAfFz51uZ1rw2EOjO0PNg+orv17n4fgzUAiPe3RvZrqLyr6UDD LD2WR3XZx821DIKHvIhE5pKXzVkpxxW+5A2WYaWnFr8LO6zeW4j57EOWlcdRE8gWvz lmpINuLDILnDw== From: Damien Le Moal To: "Martin K . Petersen" , linux-scsi@vger.kernel.org Subject: [PATCH v3 2/2] scsi: sd: Set a default optimal IO size if one is not defined Date: Thu, 12 Jun 2025 15:02:11 +0900 Message-ID: <20250612060211.1970933-3-dlemoal@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250612060211.1970933-1-dlemoal@kernel.org> References: <20250612060211.1970933-1-dlemoal@kernel.org> Precedence: bulk X-Mailing-List: linux-scsi@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Introduce the helper function sd_set_io_opt() to set a disk io_opt limit. This new way of setting this limit falls back to using the max_sectors limit if the host does not define an optimal sector limit and the device did not indicate an optimal transfer size (e.g. as is the case for ATA devices). io_opt calculation is done using a local 64-bits variable to avoid overflows. The final value is clamped to UINT_MAX aligned down to the device physical block size. This fallback io_opt limit avoids setting up the disk with a zero io_opt limit, which result in the rather small 128 KB read_ahead_kb attribute. The larger read_ahead_kb value set with the default non-zero io_opt limit significantly improves buffered read performance with file systems without any intervention from the user. Signed-off-by: Damien Le Moal Reviewed-by: Johannes Thumshirn --- drivers/scsi/sd.c | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index daddef2e9e87..8070356285a7 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -3681,6 +3681,40 @@ static void sd_read_block_zero(struct scsi_disk *sdkp) kfree(buffer); } +/* + * Set the optimal I/O size: limit the default to the SCSI host optimal sector + * limit if it is set. There may be an impact on performance when the size of + * a request exceeds this host limit. If the host did not set any optimal + * sector limit and the device did not indicate an optimal transfer size + * (e.g. ATA devices), default to using the device max_sectors limit. + */ +static void sd_set_io_opt(struct scsi_disk *sdkp, unsigned int dev_max, + struct queue_limits *lim) +{ + struct scsi_device *sdp = sdkp->device; + struct Scsi_Host *shost = sdp->host; + u64 io_opt; + + io_opt = (u64)shost->opt_sectors << SECTOR_SHIFT; + if (sd_validate_opt_xfer_size(sdkp, dev_max)) + io_opt = min_not_zero(io_opt, + logical_to_bytes(sdp, sdkp->opt_xfer_blocks)); + if (io_opt) { + lim->io_opt = ALIGN_DOWN(min_t(u64, io_opt, UINT_MAX), + sdkp->physical_block_size - 1); + return; + } + + /* Set default */ + io_opt = (u64)lim->max_sectors << SECTOR_SHIFT; + lim->io_opt = ALIGN_DOWN(min_t(u64, io_opt, UINT_MAX), + sdkp->physical_block_size - 1); + + sd_first_printk(KERN_INFO, sdkp, + "Using default optimal transfer size of %u bytes\n", + lim->io_opt); +} + /** * sd_revalidate_disk - called the first time a new disk is seen, * performs disk spin up, read_capacity, etc. @@ -3777,16 +3811,7 @@ static int sd_revalidate_disk(struct gendisk *disk) else lim.io_min = 0; - /* - * Limit default to SCSI host optimal sector limit if set. There may be - * an impact on performance for when the size of a request exceeds this - * host limit. - */ - lim.io_opt = sdp->host->opt_sectors << SECTOR_SHIFT; - if (sd_validate_opt_xfer_size(sdkp, dev_max)) { - lim.io_opt = min_not_zero(lim.io_opt, - logical_to_bytes(sdp, sdkp->opt_xfer_blocks)); - } + sd_set_io_opt(sdkp, dev_max, &lim); sdkp->first_scan = 0;