[Deepin-Kernel-SIG] [linux 6.6.y] [Upstream] provide a sane discard_granularity default - #2047
Conversation
mainline inclusion from mainline-v6.8-rc1 category: performance 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. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20231228075545.362768-2-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk> (cherry picked from commit 928a5dd) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v6.8-rc1 category: performance 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. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20231228075545.362768-3-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk> (cherry picked from commit 5e7169e) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v6.8-rc1 category: performance 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. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20231228075545.362768-4-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk> (cherry picked from commit 3c407dc) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v6.8-rc1 category: performance The discard granularity now defaults to a single sector, so don't set that value explicitly. Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Richard Weinberger <richard@nod.at> Link: https://lore.kernel.org/r/20231228075545.362768-5-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk> (cherry picked from commit 599d9d4) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v6.8-rc1 category: performance 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. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20231228075545.362768-6-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk> (cherry picked from commit 1e2ab2e) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v6.8-rc1 category: performance The discard granularity now defaults to a single sector, so don't set that value explicitly. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20231228075545.362768-7-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk> (cherry picked from commit 7243254) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v6.8-rc1 category: performance The discard granularity now defaults to a single sector, so don't set that value explicitly. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20231228075545.362768-8-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk> (cherry picked from commit 3753039) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v6.8-rc1 category: performance The discard granularity now defaults to a single sector, so don't set that value explicitly. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20231228075545.362768-9-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk> (cherry picked from commit 105c1a5) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
mainline inclusion from mainline-v6.8-rc1 category: performance The discard granularity now defaults to a single sector, so don't set that value explicitly. Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Richard Weinberger <richard@nod.at> Link: https://lore.kernel.org/r/20231228075545.362768-10-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk> (cherry picked from commit 31e4fac) Signed-off-by: Wentao Guan <guanwentao@uniontech.com>
Reviewer's guide (collapsed on small PRs)Reviewer's GuideSet a sane default discard granularity in the block layer and stop individual drivers from manually poking queue->limits.discard_granularity, relying instead on core logic tied to the logical block size and default limits. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider replacing the hardcoded
512inblk_set_default_limits()with a symbolic constant (e.g.,SECTOR_SIZEorQUEUE_DEFAULT_SECTOR_SIZE) so the default discard granularity stays aligned with the canonical sector size definition and is easier to update globally if needed. - Now that
discard_granularityis managed centrally, it may be worth adding a brief comment nearblk_queue_logical_block_size()explaining the intended invariants (e.g.,discard_granularity >= logical_block_size) to help future maintainers understand why drivers should not override it directly.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider replacing the hardcoded `512` in `blk_set_default_limits()` with a symbolic constant (e.g., `SECTOR_SIZE` or `QUEUE_DEFAULT_SECTOR_SIZE`) so the default discard granularity stays aligned with the canonical sector size definition and is easier to update globally if needed.
- Now that `discard_granularity` is managed centrally, it may be worth adding a brief comment near `blk_queue_logical_block_size()` explaining the intended invariants (e.g., `discard_granularity >= logical_block_size`) to help future maintainers understand why drivers should not override it directly.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Pull request overview
This PR aligns discard handling with upstream by centralizing a sane discard_granularity default in core block code (defaulting to 512 and ensuring it’s not smaller than the logical block size), and removes a set of per-driver overrides.
Changes:
- Set
queue_limits.discard_granularitydefault to 512 inblk_set_default_limits()and bump it whenblk_queue_logical_block_size()increases the logical block size. - Remove driver-specific
discard_granularityassignments in several block-ish drivers (mtd blktrans, bcache, zram, null_blk, nbd, ubd). - Simplify discard splitting logic in
bio_split_discard().
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| drivers/mtd/mtd_blkdevs.c | Drops per-transport discard granularity override in favor of core defaults. |
| drivers/md/bcache/super.c | Removes hardcoded discard granularity, relying on queue defaults. |
| drivers/block/zram/zram_drv.c | Removes PAGE_SIZE discard granularity override. |
| drivers/block/null_blk/main.c | Removes redundant discard granularity assignment (now relies on core logic). |
| drivers/block/nbd.c | Removes explicit discard granularity set/reset, relies on core behavior. |
| block/blk-settings.c | Introduces default discard granularity and keeps it in sync with logical block size increases. |
| block/blk-merge.c | Minor simplification of discard bio splitting logic. |
| arch/um/drivers/ubd_kern.c | Removes explicit discard granularity assignment. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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); |
| blk_queue_max_discard_sectors(q, UINT_MAX); | ||
| q->limits.discard_granularity = 512; | ||
| q->limits.io_min = block_size; | ||
| q->limits.logical_block_size = block_size; | ||
| q->limits.physical_block_size = block_size; |
| lim->max_hw_discard_sectors = 0; | ||
| lim->max_secure_erase_sectors = 0; | ||
| lim->discard_granularity = 0; | ||
| lim->discard_granularity = 512; |
Link: https://lore.kernel.org/all/20231228075545.362768-1-hch@lst.de/
Summary by Sourcery
Set a sane default discard_granularity in the block layer and rely on it instead of per-driver overrides.
New Features:
Enhancements: