Skip to contentSkip to conversation

provide a sane discard_granularity default

PATCH In review Christoph Hellwig 6 participants 21 messages Dec 28, 2023 – Jan 6, 2024

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

Hi Jens,

this series defaults the discard_granularity to the sector size as that is a very logical default for devices that have no further restrictions. This removes the need for trivial drivers to set a discard granularity and cleans up the interface.

Diffstat:

 arch/um/drivers/ubd_kern.c    |    1 -
 block/blk-merge.c             |    6 +-----
 block/blk-settings.c          |    5 ++++-
 drivers/block/nbd.c           |    6 +-----
 drivers/block/null_blk/main.c |    1 -
 drivers/block/zram/zram_drv.c |    1 -
 drivers/md/bcache/super.c     |    1 -
 drivers/mtd/mtd_blkdevs.c     |    4 +---
 8 files changed, 7 insertions(+), 18 deletions(-)
#1 lore raw

PATCH 1/9 block: remove two comments in bio_split_discard

A zero discard_granularity is not treated the same as a single-block one, and not having any segments after taking alignment is perfectly fine and does not need a warning.

---
 block/blk-merge.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
block/blk-merge.c +1 -5
index 65e75efa9bd366..2d470cf2173e29 100644
@@ -115,17 +115,13 @@ static struct bio *bio_split_discard(struct bio *bio,
 
 	*nsegs = 1;
 
-	/* Zero-sector (unknown) and one-sector granularities are the same.  */
 	granularity = max(lim->discard_granularity >> 9, 1U);
 
 	max_discard_sectors =
 		min(lim->max_discard_sectors, bio_allowed_max_sectors(lim));
 	max_discard_sectors -= max_discard_sectors % granularity;
-
-	if (unlikely(!max_discard_sectors)) {
-		/* XXX: warn */
+	if (unlikely(!max_discard_sectors))
 		return NULL;
-	}
 
 	if (bio_sectors(bio) <= max_discard_sectors)
 		return NULL;
Signature · 1 line
2.39.2
#2 lore raw

PATCH 2/9 bcache: discard_granularity should not be smaller than a sector

In reply to Christoph Hellwig

Just like all block I/O, discards are in units of sectors. Thus setting a smaller than sector size discard limit in case of > 512 byte sectors in bcache doesn't make sense. Always set the discard granularity to 512 bytes instead.

---
 drivers/md/bcache/super.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/md/bcache/super.c +1 -1
index bfe1685dbae574..ecc1447f202a42 100644
@@ -954,7 +954,7 @@ static int bcache_device_init(struct bcache_device *d, unsigned int block_size,
 	q->limits.max_segment_size	= UINT_MAX;
 	q->limits.max_segments		= BIO_MAX_VECS;
 	blk_queue_max_discard_sectors(q, UINT_MAX);
-	q->limits.discard_granularity	= 512;
+	q->limits.discard_granularity	= block_size;
 	q->limits.io_min		= block_size;
 	q->limits.logical_block_size	= block_size;
 	q->limits.physical_block_size	= block_size;
Signature · 1 line
2.39.2
#3 lore raw

PATCH 3/9 block: default the discard granularity to sector size

In reply to Christoph Hellwig

Current the discard granularity defaults to 0 and must be initialized by any driver that wants to support discard. Default to the sector size instead, which is the smallest possible value, and a very useful default.

---
 block/blk-settings.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/blk-settings.c +4 -1
index ba6e0e97118c08..d993d20dab3c6d 100644
@@ -48,7 +48,7 @@ void blk_set_default_limits(struct queue_limits *lim)
 	lim->max_discard_sectors = 0;
 	lim->max_hw_discard_sectors = 0;
 	lim->max_secure_erase_sectors = 0;
-	lim->discard_granularity = 0;
+	lim->discard_granularity = 512;
 	lim->discard_alignment = 0;
 	lim->discard_misaligned = 0;
 	lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
@@ -309,6 +309,9 @@ void blk_queue_logical_block_size(struct request_queue *q, unsigned int size)
 
 	limits->logical_block_size = size;
 
+	if (limits->discard_granularity < limits->logical_block_size)
+		limits->discard_granularity = limits->logical_block_size;
+
 	if (limits->physical_block_size < size)
 		limits->physical_block_size = size;
 
Signature · 1 line
2.39.2
#4 lore raw

Re: [PATCH 3/9] block: default the discard granularity to sector size

Hi Christoph, is there a reason you used 512 instead of SECTOR_SIZE from include/linux/blk_types.h? Thanks!

Quoted 37 lines · Christoph Hellwig

Current the discard granularity defaults to 0 and must be initialized by any driver that wants to support discard. Default to the sector size instead, which is the smallest possible value, and a very useful default.

---
 block/blk-settings.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
block/blk-settings.c +4 -2
index ba6e0e97118c08..d993d20dab3c6d 100644
@@ -48,7 +48,7 @@ void blk_set_default_limits(struct queue_limits *lim)
        lim->max_discard_sectors = 0;
        lim->max_hw_discard_sectors = 0;
        lim->max_secure_erase_sectors = 0;
-       lim->discard_granularity = 0;
+       lim->discard_granularity = 512;
        lim->discard_alignment = 0;
        lim->discard_misaligned = 0;
        lim->logical_block_size = lim->physical_block_size = lim->io_min = 512;
@@ -309,6 +309,9 @@ void blk_queue_logical_block_size(struct request_queue *q, unsigned int size)
 
        limits->logical_block_size = size;
 
+       if (limits->discard_granularity < limits->logical_block_size)
+               limits->discard_granularity = limits->logical_block_size;
+
        if (limits->physical_block_size < size)
                limits->physical_block_size = size;
 
--

2.39.2

#5 lore raw

Re: [PATCH 3/9] block: default the discard granularity to sector size

Quoted 2 lines · John Pittman

Hi Christoph, is there a reason you used 512 instead of SECTOR_SIZE from include/linux/blk_types.h? Thanks!

To match the logical_block_size/physical_block_size/io_min assignments just below.

#6 lore raw

PATCH 4/9 ubd: use the default discard granularity

In reply to Christoph Hellwig

The discard granularity now defaults to a single sector, so don't set that value explicitly.

---
 arch/um/drivers/ubd_kern.c | 1 -
 1 file changed, 1 deletion(-)
arch/um/drivers/ubd_kern.c +0 -1
index 50206feac577d5..92ee2697ff3984 100644
@@ -798,7 +798,6 @@ static int ubd_open_dev(struct ubd *ubd_dev)
 		ubd_dev->cow.fd = err;
 	}
 	if (ubd_dev->no_trim == 0) {
-		ubd_dev->queue->limits.discard_granularity = SECTOR_SIZE;
 		blk_queue_max_discard_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
 		blk_queue_max_write_zeroes_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
 	}
Signature · 1 line
2.39.2
maintainer Acked-by on v1 4/9
#7 lore raw

Re: [PATCH 4/9] ubd: use the default discard granularity

----- Ursprüngliche Mail -----

Quoted 23 lines

Von: "hch" <[email protected]>
An: "Jens Axboe" <[email protected]>
CC: "richard" <[email protected]>, "anton ivanov" <[email protected]>, "Josef Bacik" <[email protected]>, "Minchan Kim" <[email protected]>, "senozhatsky" <[email protected]>, "Coly Li" <[email protected]>, "Miquel Raynal" <[email protected]>, "Vignesh Raghavendra" <[email protected]>, "linux-um" <[email protected]>, "linux-block" <[email protected]>, [email protected], "linux-bcache" <[email protected]>, "linux-mtd" <[email protected]>
Gesendet: Donnerstag, 28. Dezember 2023 08:55:40
Betreff: [PATCH 4/9] ubd: use the default discard granularity

The discard granularity now defaults to a single sector, so don't set that value explicitly.

---
arch/um/drivers/ubd_kern.c | 1 -

1 file changed, 1 deletion(-)
arch/um/drivers/ubd_kern.c +0 -0
index 50206feac577d5..92ee2697ff3984 100644

arch/um/drivers/ubd_kern.c static int ubd_open_dev(struct ubd *ubd_dev) · line 803

Quoted 7 lines
arch/um/drivers/ubd_kern.c +0 -1
@@ -798,7 +798,6 @@ static int ubd_open_dev(struct ubd *ubd_dev)
 		ubd_dev->cow.fd = err;
 	}
 	if (ubd_dev->no_trim == 0) {
-		ubd_dev->queue->limits.discard_granularity = SECTOR_SIZE;
 		blk_queue_max_discard_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
 		blk_queue_max_write_zeroes_sectors(ubd_dev->queue, UBD_MAX_REQUEST);
 	}

Thanks,
//richard

#8 lore raw

PATCH 5/9 nbd: use the default discard granularity

In reply to Christoph Hellwig

The discard granularity now defaults to a single sector, so don't set that value explicitly. Also don't bother clearing it as a discard granularity without discard_sectors doesn't mean anything.

---
 drivers/block/nbd.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/block/nbd.c +1 -5
index b6414e1e645b76..4e72ec4e25ac5a 100644
@@ -334,10 +334,8 @@ static int nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
 	if (!nbd->pid)
 		return 0;
 
-	if (nbd->config->flags & NBD_FLAG_SEND_TRIM) {
-		nbd->disk->queue->limits.discard_granularity = blksize;
+	if (nbd->config->flags & NBD_FLAG_SEND_TRIM)
 		blk_queue_max_discard_sectors(nbd->disk->queue, UINT_MAX);
-	}
 	blk_queue_logical_block_size(nbd->disk->queue, blksize);
 	blk_queue_physical_block_size(nbd->disk->queue, blksize);
 
@@ -1357,7 +1355,6 @@ static void nbd_config_put(struct nbd_device *nbd)
 		nbd->config = NULL;
 
 		nbd->tag_set.timeout = 0;
-		nbd->disk->queue->limits.discard_granularity = 0;
 		blk_queue_max_discard_sectors(nbd->disk->queue, 0);
 
 		mutex_unlock(&nbd->config_lock);
@@ -1850,7 +1847,6 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
 	 * Tell the block layer that we are not a rotational device
 	 */
 	blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
-	disk->queue->limits.discard_granularity = 0;
 	blk_queue_max_discard_sectors(disk->queue, 0);
 	blk_queue_max_segment_size(disk->queue, UINT_MAX);
 	blk_queue_max_segments(disk->queue, USHRT_MAX);
Signature · 1 line
2.39.2
#9 lore raw

PATCH 6/9 null_blk: use the default discard granularity

In reply to Christoph Hellwig

The discard granularity now defaults to a single sector, so don't set that value explicitly.

---
 drivers/block/null_blk/main.c | 1 -
 1 file changed, 1 deletion(-)
drivers/block/null_blk/main.c +0 -1
index 13ed446b5e198e..9f7695f00c2db8 100644
@@ -1880,7 +1880,6 @@ static void null_config_discard(struct nullb *nullb)
 		return;
 	}
 
-	nullb->q->limits.discard_granularity = nullb->dev->blocksize;
 	blk_queue_max_discard_sectors(nullb->q, UINT_MAX >> 9);
 }
 
Signature · 1 line
2.39.2
#10 lore raw

PATCH 7/9 zram: use the default discard granularity

In reply to Christoph Hellwig

The discard granularity now defaults to a single sector, so don't set that value explicitly.

---
 drivers/block/zram/zram_drv.c | 1 -
 1 file changed, 1 deletion(-)
drivers/block/zram/zram_drv.c +0 -1
index d77d3664ca0805..e1dec0483a012b 100644
@@ -2227,7 +2227,6 @@ static int zram_add(void)
 					ZRAM_LOGICAL_BLOCK_SIZE);
 	blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
 	blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
-	zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
 	blk_queue_max_discard_sectors(zram->disk->queue, UINT_MAX);
 
 	/*
Signature · 1 line
2.39.2
#11 lore raw

Re: [PATCH 7/9] zram: use the default discard granularity

Quoted 3 lines · Christoph Hellwig

The discard granularity now defaults to a single sector, so don't set that value explicitly

Hmm, but sector size != PAGE_SIZE

[..]

Quoted 5 lines
drivers/block/zram/zram_drv.c +0 -1
@@ -2227,7 +2227,6 @@ static int zram_add(void)
 					ZRAM_LOGICAL_BLOCK_SIZE);
 	blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
 	blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
-	zram->disk->queue->limits.discard_granularity = PAGE_SIZE;
maintainer
#12 lore raw

Re: [PATCH 7/9] zram: use the default discard granularity

Quoted 7 lines · Sergey Senozhatsky
Quoted 3 lines · Christoph Hellwig

The discard granularity now defaults to a single sector, so don't set that value explicitly

Hmm, but sector size != PAGE_SIZE

Quoted 7 lines

[..]

drivers/block/zram/zram_drv.c static int zram_add(void) · line 2230

Quoted 5 lines
drivers/block/zram/zram_drv.c +0 -1
@@ -2227,7 +2227,6 @@ static int zram_add(void)
 					ZRAM_LOGICAL_BLOCK_SIZE);
 	blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
 	blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
-	zram->disk->queue->limits.discard_granularity = PAGE_SIZE;

Yep, that does indeed look buggy.

Signature · 1 line
Jens Axboe
maintainer
#13 lore raw

Re: [PATCH 7/9] zram: use the default discard granularity

Quoted 10 lines · Jens Axboe
Quoted 9 lines · Sergey Senozhatsky
Quoted 3 lines · Christoph Hellwig

The discard granularity now defaults to a single sector, so don't set that value explicitly

Hmm, but sector size != PAGE_SIZE

[..]

Quoted 7 lines
Quoted 5 lines

drivers/block/zram/zram_drv.c static int zram_add(void) · line 2230

Quoted 5 lines
drivers/block/zram/zram_drv.c +0 -1
@@ -2227,7 +2227,6 @@ static int zram_add(void)
 					ZRAM_LOGICAL_BLOCK_SIZE);
 	blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
 	blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
-	zram->disk->queue->limits.discard_granularity = PAGE_SIZE;

Yep, that does indeed look buggy.

Ah no it's fine, it'll default to the sector size of the device, which is set before the discard limit/granularity. So should work fine as-is.

Signature · 1 line
Jens Axboe
maintainer
#14 lore raw

Re: [PATCH 7/9] zram: use the default discard granularity

Quoted 19 lines · Jens Axboe
Quoted 10 lines · Jens Axboe
Quoted 9 lines · Sergey Senozhatsky
Quoted 3 lines · Christoph Hellwig

The discard granularity now defaults to a single sector, so don't set that value explicitly

Hmm, but sector size != PAGE_SIZE

[..]

Quoted 7 lines
Quoted 5 lines

drivers/block/zram/zram_drv.c static int zram_add(void) · line 2230

Quoted 5 lines
drivers/block/zram/zram_drv.c +0 -1
@@ -2227,7 +2227,6 @@ static int zram_add(void)
 					ZRAM_LOGICAL_BLOCK_SIZE);
 	blk_queue_io_min(zram->disk->queue, PAGE_SIZE);
 	blk_queue_io_opt(zram->disk->queue, PAGE_SIZE);
-	zram->disk->queue->limits.discard_granularity = PAGE_SIZE;

Yep, that does indeed look buggy.

Quoted 2 lines

Ah no it's fine, it'll default to the sector size of the device, which is set before the discard limit/granularity. So should work fine as-is.

Replying to myself again... It does then default it to the logical block size, not the physical one. Which does look odd, seems like that should be the physical size?

In any case, this does change behavior for zram.

Christoph?

Signature · 1 line
Jens Axboe
#16 lore raw

PATCH 8/9 bcache: use the default discard granularity

In reply to Christoph Hellwig

The discard granularity now defaults to a single sector, so don't set that value explicitly.

---
 drivers/md/bcache/super.c | 1 -
 1 file changed, 1 deletion(-)
drivers/md/bcache/super.c +0 -1
index ecc1447f202a42..39ec95b8613f1f 100644
@@ -954,7 +954,6 @@ static int bcache_device_init(struct bcache_device *d, unsigned int block_size,
 	q->limits.max_segment_size	= UINT_MAX;
 	q->limits.max_segments		= BIO_MAX_VECS;
 	blk_queue_max_discard_sectors(q, UINT_MAX);
-	q->limits.discard_granularity	= block_size;
 	q->limits.io_min		= block_size;
 	q->limits.logical_block_size	= block_size;
 	q->limits.physical_block_size	= block_size;
Signature · 1 line
2.39.2
Acked-by on v1 8/9
#17 lore raw

Re: [PATCH 8/9] bcache: use the default discard granularity

Quoted 4 lines · Christoph Hellwig

The discard granularity now defaults to a single sector, so don't set that value explicitly.

Thanks.

Quoted 20 lines
---
 drivers/md/bcache/super.c | 1 -
 1 file changed, 1 deletion(-)
drivers/md/bcache/super.c +0 -1
index ecc1447f202a42..39ec95b8613f1f 100644
@@ -954,7 +954,6 @@ static int bcache_device_init(struct bcache_device *d, unsigned int block_size,
 	q->limits.max_segment_size	= UINT_MAX;
 	q->limits.max_segments		= BIO_MAX_VECS;
 	blk_queue_max_discard_sectors(q, UINT_MAX);
-	q->limits.discard_granularity	= block_size;
 	q->limits.io_min		= block_size;
 	q->limits.logical_block_size	= block_size;
 	q->limits.physical_block_size	= block_size;
Signature · 1 line
2.39.2
Signature · 1 line
Coly Li
#18 lore raw

PATCH 9/9 mtd_blkdevs: use the default discard granularity

In reply to Christoph Hellwig

The discard granularity now defaults to a single sector, so don't set that value explicitly.

---
 drivers/mtd/mtd_blkdevs.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/mtd/mtd_blkdevs.c +1 -3
index ff18636e088973..0da7b33849471a 100644
@@ -376,10 +376,8 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
 	blk_queue_flag_set(QUEUE_FLAG_NONROT, new->rq);
 	blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, new->rq);
 
-	if (tr->discard) {
+	if (tr->discard)
 		blk_queue_max_discard_sectors(new->rq, UINT_MAX);
-		new->rq->limits.discard_granularity = tr->blksize;
-	}
 
 	gd->queue = new->rq;
 
Signature · 1 line
2.39.2
maintainer Acked-by on v1 9/9
#19 lore raw

Re: [PATCH 9/9] mtd_blkdevs: use the default discard granularity

----- Ursprüngliche Mail -----

Quoted 23 lines

Von: "hch" <[email protected]>
An: "Jens Axboe" <[email protected]>
CC: "richard" <[email protected]>, "anton ivanov" <[email protected]>, "Josef Bacik" <[email protected]>, "Minchan Kim" <[email protected]>, "senozhatsky" <[email protected]>, "Coly Li" <[email protected]>, "Miquel Raynal" <[email protected]>, "Vignesh Raghavendra" <[email protected]>, "linux-um" <[email protected]>, "linux-block" <[email protected]>, [email protected], "linux-bcache" <[email protected]>, "linux-mtd" <[email protected]>
Gesendet: Donnerstag, 28. Dezember 2023 08:55:45
Betreff: [PATCH 9/9] mtd_blkdevs: use the default discard granularity

The discard granularity now defaults to a single sector, so don't set that value explicitly.

---
drivers/mtd/mtd_blkdevs.c | 4 +---

1 file changed, 1 insertion(+), 3 deletions(-)
drivers/mtd/mtd_blkdevs.c +0 -0
index ff18636e088973..0da7b33849471a 100644

drivers/mtd/mtd_blkdevs.c int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new) · line 382

Quoted 8 lines
drivers/mtd/mtd_blkdevs.c +1 -3
@@ -376,10 +376,8 @@ int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
 	blk_queue_flag_set(QUEUE_FLAG_NONROT, new->rq);
 	blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, new->rq);
 
-	if (tr->discard) {
+	if (tr->discard)
 		blk_queue_max_discard_sectors(new->rq, UINT_MAX);
-		new->rq->limits.discard_granularity = tr->blksize;
-	}

Thanks,
//richard

maintainer Applied
#20 lore raw
In reply to Christoph Hellwig
Quoted 16 lines · Christoph Hellwig

this series defaults the discard_granularity to the sector size as that is a very logical default for devices that have no further restrictions. This removes the need for trivial drivers to set a discard granularity and cleans up the interface.

Diffstat:

 arch/um/drivers/ubd_kern.c    |    1 -
 block/blk-merge.c             |    6 +-----
 block/blk-settings.c          |    5 ++++-
 drivers/block/nbd.c           |    6 +-----
 drivers/block/null_blk/main.c |    1 -
 drivers/block/zram/zram_drv.c |    1 -
 drivers/md/bcache/super.c     |    1 -
 drivers/mtd/mtd_blkdevs.c     |    4 +---
 8 files changed, 7 insertions(+), 18 deletions(-)
Quoted 1 line

[...]

Applied, thanks!

[1/9] block: remove two comments in bio_split_discard

      commit: 928a5dd3a849dc6d8298835bdcb25c360d41bccb
[2/9] bcache: discard_granularity should not be smaller than a sector
      commit: 5e7169e7f7c0989304dbe8467a1d703d614c64db
[3/9] block: default the discard granularity to sector size
      commit: 3c407dc723bbf914f3744b0c2bb82265b411a50c
[4/9] ubd: use the default discard granularity
      commit: 599d9d4eab7c3d5dc6f1e0f8f052fee9eaa54e50
[5/9] nbd: use the default discard granularity
      commit: 1e2ab2e8a98c9e0629b5b8bff8ee6f2cb3e8daac
[6/9] null_blk: use the default discard granularity
      commit: 724325477f8a48ce1defc2a49998bbc19fe85c88
[7/9] zram: use the default discard granularity
      commit: 3753039def5d0d1c43af847b507ba9b782db183a
[8/9] bcache: use the default discard granularity
      commit: 105c1a5f6ccef7f52f9e76664407ef96218272eb
[9/9] mtd_blkdevs: use the default discard granularity
      commit: 31e4fac930814f2f92eb6ebac9c4d4e3b09f7aaf

Best regards,

Signature · 1 line
Jens Axboe