Skip to contentSkip to conversation

enable sector size > PAGE_SIZE for scsi

PATCH Superseded [email protected] 2 participants 4 messages Feb 11

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover enable sector size > PAGE_SIZE for scsi

From: Swarna Prabhu <[email protected]>

Hi All,

This is v2 series sent based on the feedback received on v1 [1] and RFC series. This patchset enables sector sizes > PAGE_SIZE for sd driver and scsi_debug driver since block layer can support block size > PAGE_SIZE. There was one issue with write_same16 and write_same10 command, which is fixed as a part of the series.

Changes since v1:
- Retain the single page mempool for the regular devices.
- Initialize a large page mempool at 'sd_probe' when the first device with sector size > 4k is detected for sd driver with ensuring atomicity.
- Safe destruction of the large page mempool in 'sd_probe' if the device fails at probe after the mempool is successfully created.
- Safe destruction of the large page mempool in 'sd_remove' when the last device with sector size > PAGE_SIZE is detached from the system.
- Added a check in 'sd_set_special_bvec' to use the correct mempool for allocation based on the sector size of the device.
- Added a check in 'sd_uninit_command' to use the correct mempool for freeing based on the sector size of the device.
- Added check to destroy large page mempool if it exists while exiting sd driver.
- Slightly modified the git commit message to update the above changes for scsi sd driver fix patch.

Thanks to Damien for feedback on the v1 series.

Testing:
-Test suite: xfs and generic from fstest + QEMU emulated block
device(scsi and nvme)
- fstest Config for patched xfs 16k block size [xfs_reflink_16k_scsi]

    TEST_DEV=/dev/sda
    SCRATCH_DEV_POOL="/dev/sdb"
    MKFS_OPTIONS='-f -m reflink=1,rmapbt=1, -i sparse=1, -b size=16384,
    -s size=16384'

- Generic test results
Baseline: 6.19-rc8 kernel + nvme 16k logical block size
Patched: 6.19-rc8 kernel + scsi 16k logical block size No regressions introduced by the patch.
- XFS tests results
Baseline: 6.19-rc8 kernel + nvme 16k logical block size
Patched: 6.19-rc8 kernel + scsi 16k logical block size No regressions introduced by the patch
- Blktests results
scsi and block layer tests with 16k logical block size.
Baseline: vanilla kernel + scsi 4k
No regressions seen by the patch.

Link to v1: https://lore.kernel.org/all/[email protected]/ [1]

Swarna Prabhu (2):
scsi: sd: fix write_same(16/10) to enable sector size > PAGE_SIZE
scsi: scsi_debug: enable sdebug_sector_size > PAGE_SIZE

 drivers/scsi/scsi_debug.c |  8 +---
 drivers/scsi/sd.c         | 79 +++++++++++++++++++++++++++++++++------
 2 files changed, 68 insertions(+), 19 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH v2 1/2 scsi: sd: fix write_same(16/10) to enable sector size > PAGE_SIZE

From: Swarna Prabhu <[email protected]>

The WRITE SAME(16) and WRITE SAME(10) scsi commands uses a page from a dedicated mempool('sd_page_pool') for its payload. This pool was initialized to allocate single pages, which was sufficient as long as the device sector size did not exceed the PAGE_SIZE.

Given that block layer now supports block size upto 64K ie beyond PAGE_SIZE, initialize large page pool in 'sd_probe()' if a higher sector device is attached ensuring atomicity. Adapt 'sd_set_special_bvec()' to use large page pool when a higher sector size device is attached.

With the above fix, enable sector sizes > PAGE_SIZE in scsi sd driver.

---
 drivers/scsi/sd.c | 79 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 67 insertions(+), 12 deletions(-)
drivers/scsi/sd.c +67 -12
index f50b92e63201..0e0c5dd1c668 100644
@@ -112,8 +112,11 @@ static void sd_shutdown(struct device *);
 static void scsi_disk_release(struct device *cdev);
 
 static DEFINE_IDA(sd_index_ida);
+static DEFINE_MUTEX(sd_mutex_lock);
 
 static mempool_t *sd_page_pool;
+static mempool_t *sd_large_page_pool;
+static atomic_t sd_large_page_pool_users = ATOMIC_INIT(0);
 static struct lock_class_key sd_bio_compl_lkclass;
 
 static const char *sd_cache_types[] = {
@@ -922,14 +925,27 @@ static void sd_config_discard(struct scsi_disk *sdkp, struct queue_limits *lim,
 		(logical_block_size >> SECTOR_SHIFT);
 }
 
-static void *sd_set_special_bvec(struct request *rq, unsigned int data_len)
+static void *sd_set_special_bvec(struct scsi_cmnd *cmd, unsigned int data_len)
 {
 	struct page *page;
+	struct request *rq = scsi_cmd_to_rq(cmd);
+	struct scsi_device *sdp = cmd->device;
+	unsigned sector_size = sdp->sector_size;
+	unsigned int nr_pages = DIV_ROUND_UP(sector_size, PAGE_SIZE);
+	int n = 0;
 
-	page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
+	if (sector_size > PAGE_SIZE)
+		page = mempool_alloc(sd_large_page_pool, GFP_ATOMIC);
+	else
+		page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
 	if (!page)
 		return NULL;
-	clear_highpage(page);
+
+	do {
+		clear_highpage(page + n);
+		n++;
+	} while (n < nr_pages);
+
 	bvec_set_page(&rq->special_vec, page, data_len, 0);
 	rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
 	return bvec_virt(&rq->special_vec);
@@ -945,7 +961,7 @@ static blk_status_t sd_setup_unmap_cmnd(struct scsi_cmnd *cmd)
 	unsigned int data_len = 24;
 	char *buf;
 
-	buf = sd_set_special_bvec(rq, data_len);
+	buf = sd_set_special_bvec(cmd, data_len);
 	if (!buf)
 		return BLK_STS_RESOURCE;
 
@@ -1034,7 +1050,7 @@ static blk_status_t sd_setup_write_same16_cmnd(struct scsi_cmnd *cmd,
 	u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
 	u32 data_len = sdp->sector_size;
 
-	if (!sd_set_special_bvec(rq, data_len))
+	if (!sd_set_special_bvec(cmd, data_len))
 		return BLK_STS_RESOURCE;
 
 	cmd->cmd_len = 16;
@@ -1061,7 +1077,7 @@ static blk_status_t sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd,
 	u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
 	u32 data_len = sdp->sector_size;
 
-	if (!sd_set_special_bvec(rq, data_len))
+	if (!sd_set_special_bvec(cmd, data_len))
 		return BLK_STS_RESOURCE;
 
 	cmd->cmd_len = 10;
@@ -1507,9 +1523,15 @@ static blk_status_t sd_init_command(struct scsi_cmnd *cmd)
 static void sd_uninit_command(struct scsi_cmnd *SCpnt)
 {
 	struct request *rq = scsi_cmd_to_rq(SCpnt);
+	struct scsi_device *sdp = SCpnt->device;
+	unsigned sector_size = sdp->sector_size;
 
-	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
-		mempool_free(rq->special_vec.bv_page, sd_page_pool);
+	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
+		if (sector_size > PAGE_SIZE)
+			mempool_free(rq->special_vec.bv_page, sd_large_page_pool);
+		else
+			mempool_free(rq->special_vec.bv_page, sd_page_pool);
+	}
 }
 
 static bool sd_need_revalidate(struct gendisk *disk, struct scsi_disk *sdkp)
@@ -2920,10 +2942,7 @@ sd_read_capacity(struct scsi_disk *sdkp, struct queue_limits *lim,
 			  "assuming 512.\n");
 	}
 
-	if (sector_size != 512 &&
-	    sector_size != 1024 &&
-	    sector_size != 2048 &&
-	    sector_size != 4096) {
+	if (blk_validate_block_size(sector_size)) {
 		sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
 			  sector_size);
 		/*
@@ -4044,6 +4063,21 @@ static int sd_probe(struct device *dev)
 	sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS;
 
 	sd_revalidate_disk(gd);
+	if (sdp->sector_size > PAGE_SIZE) {
+		mutex_lock(&sd_mutex_lock);
+		if (!sd_large_page_pool) {
+			sd_large_page_pool = mempool_create_page_pool(
+					SD_MEMPOOL_SIZE, get_order(BLK_MAX_BLOCK_SIZE));
+			if (!sd_large_page_pool) {
+				printk(KERN_ERR "sd: can't create large page mempool\n");
+				error = -ENOMEM;
+				mutex_unlock(&sd_mutex_lock);
+				goto out_free_index;
+			}
+		}
+		atomic_inc(&sd_large_page_pool_users);
+		mutex_unlock(&sd_mutex_lock);
+	}
 
 	if (sdp->removable) {
 		gd->flags |= GENHD_FL_REMOVABLE;
@@ -4061,6 +4095,14 @@ static int sd_probe(struct device *dev)
 	if (error) {
 		device_unregister(&sdkp->disk_dev);
 		put_disk(gd);
+		if (sdp->sector_size > PAGE_SIZE) {
+			mutex_lock(&sd_mutex_lock);
+			if (atomic_dec_and_test(&sd_large_page_pool_users)) {
+				mempool_destroy(sd_large_page_pool);
+				sd_large_page_pool = NULL;
+			}
+			mutex_unlock(&sd_mutex_lock);
+		}
 		goto out;
 	}
 
@@ -4101,6 +4143,7 @@ static int sd_probe(struct device *dev)
 static int sd_remove(struct device *dev)
 {
 	struct scsi_disk *sdkp = dev_get_drvdata(dev);
+	struct scsi_device *sdp = sdkp->device;
 
 	scsi_autopm_get_device(sdkp->device);
 
@@ -4110,6 +4153,16 @@ static int sd_remove(struct device *dev)
 		sd_shutdown(dev);
 
 	put_disk(sdkp->disk);
+
+	if (sdp->sector_size > PAGE_SIZE) {
+		mutex_lock(&sd_mutex_lock);
+		if (atomic_dec_and_test(&sd_large_page_pool_users)) {
+			mempool_destroy(sd_large_page_pool);
+			sd_large_page_pool = NULL;
+		}
+		mutex_unlock(&sd_mutex_lock);
+	}
+
 	return 0;
 }
 
@@ -4446,6 +4499,8 @@ static void __exit exit_sd(void)
 
 	scsi_unregister_driver(&sd_template.gendrv);
 	mempool_destroy(sd_page_pool);
+	if (sd_large_page_pool)
+		mempool_destroy(sd_large_page_pool);
 
 	class_unregister(&sd_disk_class);
 
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 1/2] scsi: sd: fix write_same(16/10) to enable sector size > PAGE_SIZE

Quoted 8 lines · On 2/11/26 10:50, [email protected] wrote:

From: Swarna Prabhu <[email protected]>

The WRITE SAME(16) and WRITE SAME(10) scsi commands uses a page from a dedicated mempool('sd_page_pool') for its payload. This pool was initialized to allocate single pages, which was sufficient as long as the device sector size did not exceed the PAGE_SIZE.

Quoted 8 lines

Given that block layer now supports block size upto 64K ie beyond PAGE_SIZE, initialize large page pool in 'sd_probe()' if a higher sector device is attached ensuring atomicity. Adapt 'sd_set_special_bvec()' to use large page pool when a higher sector size device is attached.

With the above fix, enable sector sizes > PAGE_SIZE in scsi sd driver.

This is not a fix (as in a bug fix) but rather a new feature.

Quoted 1 line

Why ? Before this patch, scsi allows only up to 4K sector size, which is not > PAGE_SIZE.

Quoted 40 lines
---
 drivers/scsi/sd.c | 79 ++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 67 insertions(+), 12 deletions(-)
drivers/scsi/sd.c +11 -2
index f50b92e63201..0e0c5dd1c668 100644
@@ -112,8 +112,11 @@ static void sd_shutdown(struct device *);
 static void scsi_disk_release(struct device *cdev);
 
 static DEFINE_IDA(sd_index_ida);
+static DEFINE_MUTEX(sd_mutex_lock);
 
 static mempool_t *sd_page_pool;
+static mempool_t *sd_large_page_pool;
+static atomic_t sd_large_page_pool_users = ATOMIC_INIT(0);
 static struct lock_class_key sd_bio_compl_lkclass;
 
 static const char *sd_cache_types[] = {
@@ -922,14 +925,27 @@ static void sd_config_discard(struct scsi_disk *sdkp, struct queue_limits *lim,
 		(logical_block_size >> SECTOR_SHIFT);
 }
 
-static void *sd_set_special_bvec(struct request *rq, unsigned int data_len)
+static void *sd_set_special_bvec(struct scsi_cmnd *cmd, unsigned int data_len)
 {
 	struct page *page;
+	struct request *rq = scsi_cmd_to_rq(cmd);
+	struct scsi_device *sdp = cmd->device;
+	unsigned sector_size = sdp->sector_size;
+	unsigned int nr_pages = DIV_ROUND_UP(sector_size, PAGE_SIZE);
+	int n = 0;
 
-	page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
+	if (sector_size > PAGE_SIZE)
+		page = mempool_alloc(sd_large_page_pool, GFP_ATOMIC);

drivers/scsi/sd.c static void sd_config_discard(struct scsi_disk *sdkp, struct queue_limits *lim, · line 947

Quoted 10 lines
drivers/scsi/sd.c +7 -1
+	else
+		page = mempool_alloc(sd_page_pool, GFP_ATOMIC);
 	if (!page)
 		return NULL;
-	clear_highpage(page);
+
+	do {
+		clear_highpage(page + n);
+		n++;
+	} while (n < nr_pages);

A for loop would be a lot cleaner and simpler.

Quoted 70 lines
drivers/scsi/sd.c +18 -9
+
 	bvec_set_page(&rq->special_vec, page, data_len, 0);
 	rq->rq_flags |= RQF_SPECIAL_PAYLOAD;
 	return bvec_virt(&rq->special_vec);
@@ -945,7 +961,7 @@ static blk_status_t sd_setup_unmap_cmnd(struct scsi_cmnd *cmd)
 	unsigned int data_len = 24;
 	char *buf;
 
-	buf = sd_set_special_bvec(rq, data_len);
+	buf = sd_set_special_bvec(cmd, data_len);
 	if (!buf)
 		return BLK_STS_RESOURCE;
 
@@ -1034,7 +1050,7 @@ static blk_status_t sd_setup_write_same16_cmnd(struct scsi_cmnd *cmd,
 	u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
 	u32 data_len = sdp->sector_size;
 
-	if (!sd_set_special_bvec(rq, data_len))
+	if (!sd_set_special_bvec(cmd, data_len))
 		return BLK_STS_RESOURCE;
 
 	cmd->cmd_len = 16;
@@ -1061,7 +1077,7 @@ static blk_status_t sd_setup_write_same10_cmnd(struct scsi_cmnd *cmd,
 	u32 nr_blocks = sectors_to_logical(sdp, blk_rq_sectors(rq));
 	u32 data_len = sdp->sector_size;
 
-	if (!sd_set_special_bvec(rq, data_len))
+	if (!sd_set_special_bvec(cmd, data_len))
 		return BLK_STS_RESOURCE;
 
 	cmd->cmd_len = 10;
@@ -1507,9 +1523,15 @@ static blk_status_t sd_init_command(struct scsi_cmnd *cmd)
 static void sd_uninit_command(struct scsi_cmnd *SCpnt)
 {
 	struct request *rq = scsi_cmd_to_rq(SCpnt);
+	struct scsi_device *sdp = SCpnt->device;
+	unsigned sector_size = sdp->sector_size;
 
-	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD)
-		mempool_free(rq->special_vec.bv_page, sd_page_pool);
+	if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
+		if (sector_size > PAGE_SIZE)
+			mempool_free(rq->special_vec.bv_page, sd_large_page_pool);
+		else
+			mempool_free(rq->special_vec.bv_page, sd_page_pool);
+	}
 }
 
 static bool sd_need_revalidate(struct gendisk *disk, struct scsi_disk *sdkp)
@@ -2920,10 +2942,7 @@ sd_read_capacity(struct scsi_disk *sdkp, struct queue_limits *lim,
 			  "assuming 512.\n");
 	}
 
-	if (sector_size != 512 &&
-	    sector_size != 1024 &&
-	    sector_size != 2048 &&
-	    sector_size != 4096) {
+	if (blk_validate_block_size(sector_size)) {
 		sd_printk(KERN_NOTICE, sdkp, "Unsupported sector size %d.\n",
 			  sector_size);
 		/*
@@ -4044,6 +4063,21 @@ static int sd_probe(struct device *dev)
 	sdkp->max_medium_access_timeouts = SD_MAX_MEDIUM_TIMEOUTS;
 
 	sd_revalidate_disk(gd);
+	if (sdp->sector_size > PAGE_SIZE) {
+		mutex_lock(&sd_mutex_lock);
+		if (!sd_large_page_pool) {
+			sd_large_page_pool = mempool_create_page_pool(
+					SD_MEMPOOL_SIZE, get_order(BLK_MAX_BLOCK_SIZE));

drivers/scsi/sd.c static int sd_probe(struct device *dev) · line 4080

Quoted 10 lines
drivers/scsi/sd.c +10 -0
+			if (!sd_large_page_pool) {
+				printk(KERN_ERR "sd: can't create large page mempool\n");
+				error = -ENOMEM;
+				mutex_unlock(&sd_mutex_lock);
+				goto out_free_index;
+			}
+		}
+		atomic_inc(&sd_large_page_pool_users);
+		mutex_unlock(&sd_mutex_lock);
+	}

It would be a lot nicer to have this defined as a helper function that goes together with a pool destroy function (see below).

Quoted 5 lines
drivers/scsi/sd.c +0 -0
 	if (sdp->removable) {
 		gd->flags |= GENHD_FL_REMOVABLE;
@@ -4061,6 +4095,14 @@ static int sd_probe(struct device *dev)
 	if (error) {

drivers/scsi/sd.c static int sd_probe(struct device *dev) · line 4105

Quoted 10 lines
drivers/scsi/sd.c +8 -0
 		device_unregister(&sdkp->disk_dev);
 		put_disk(gd);
+		if (sdp->sector_size > PAGE_SIZE) {
+			mutex_lock(&sd_mutex_lock);
+			if (atomic_dec_and_test(&sd_large_page_pool_users)) {
+				mempool_destroy(sd_large_page_pool);
+				sd_large_page_pool = NULL;
+			}
+			mutex_unlock(&sd_mutex_lock);
+		}

This hunk is repeated twice. Make this a helper please.

Quoted 37 lines
drivers/scsi/sd.c +13 -0
 		goto out;
 	}
 
@@ -4101,6 +4143,7 @@ static int sd_probe(struct device *dev)
 static int sd_remove(struct device *dev)
 {
 	struct scsi_disk *sdkp = dev_get_drvdata(dev);
+	struct scsi_device *sdp = sdkp->device;
 
 	scsi_autopm_get_device(sdkp->device);
 
@@ -4110,6 +4153,16 @@ static int sd_remove(struct device *dev)
 		sd_shutdown(dev);
 
 	put_disk(sdkp->disk);
+
+	if (sdp->sector_size > PAGE_SIZE) {
+		mutex_lock(&sd_mutex_lock);
+		if (atomic_dec_and_test(&sd_large_page_pool_users)) {
+			mempool_destroy(sd_large_page_pool);
+			sd_large_page_pool = NULL;
+		}
+		mutex_unlock(&sd_mutex_lock);
+	}
+
 	return 0;
 }
 
@@ -4446,6 +4499,8 @@ static void __exit exit_sd(void)
 
 	scsi_unregister_driver(&sd_template.gendrv);
 	mempool_destroy(sd_page_pool);
+	if (sd_large_page_pool)
+		mempool_destroy(sd_large_page_pool);
 
 	class_unregister(&sd_disk_class);
 
Signature · 2 lines
Damien Le Moal
Western Digital Research
#3 lore raw

PATCH v2 2/2 scsi: scsi_debug: enable sdebug_sector_size > PAGE_SIZE

In reply to [email protected]

From: Swarna Prabhu <[email protected]>

Now that block layer can support block size > PAGE_SIZE and the issue with WRITE_SAME(16) and WRITE_SAME(10) are fixed for sector sizes > PAGE_SIZE, enable sdebug_sector_size

Quoted 1 line

PAGE_SIZE in scsi_debug.

---
 drivers/scsi/scsi_debug.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
drivers/scsi/scsi_debug.c +1 -7
index c5085e6d2e75..91f42b7e68aa 100644
@@ -8508,13 +8508,7 @@ static int __init scsi_debug_init(void)
 	} else if (sdebug_ndelay > 0)
 		sdebug_jdelay = JDELAY_OVERRIDDEN;
 
-	switch (sdebug_sector_size) {
-	case  512:
-	case 1024:
-	case 2048:
-	case 4096:
-		break;
-	default:
+	if (blk_validate_block_size(sdebug_sector_size)) {
 		pr_err("invalid sector_size %d\n", sdebug_sector_size);
 		return -EINVAL;
 	}
Signature · 1 line
2.39.5