Skip to content

[Deepin-Kernel-SIG] [linux 6.6.y] [Upstream] provide a sane discard_granularity default - #2047

Open
opsiff wants to merge 9 commits into
deepin-community:linux-6.6.yfrom
opsiff:linux-6.6.y-2026-08-04-update-blk-p2
Open

[Deepin-Kernel-SIG] [linux 6.6.y] [Upstream] provide a sane discard_granularity default#2047
opsiff wants to merge 9 commits into
deepin-community:linux-6.6.yfrom
opsiff:linux-6.6.y-2026-08-04-update-blk-p2

Conversation

@opsiff

@opsiff opsiff commented Aug 4, 2026

Copy link
Copy Markdown
Member

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:

  • Default the global block queue discard_granularity to 512 bytes and keep it in sync with the logical block size.

Enhancements:

  • Ensure discard_granularity is automatically adjusted when the logical block size is increased.
  • Remove redundant discard_granularity settings from various block drivers to centralize discard behavior in core block code.

Christoph Hellwig added 9 commits August 4, 2026 18:41
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>
@sourcery-ai

sourcery-ai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Set 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

Change Details Files
Make discard granularity default to a sector and auto-align it with logical block size in core block queue limits.
  • Initialize queue_limits.discard_granularity to 512 in blk_set_default_limits instead of 0.
  • Ensure discard_granularity is at least logical_block_size when blk_queue_logical_block_size is set.
  • Simplify bio_split_discard by treating zero max_discard_sectors as a normal failure case without an extra comment or warning placeholder.
block/blk-settings.c
block/blk-merge.c
Stop block drivers from directly setting discard_granularity and rely on block core defaults instead.
  • Remove explicit discard_granularity assignments in nbd setup, config teardown, and device add paths.
  • Remove discard_granularity overrides in mtd block translation, UML ubd, null_blk, zram, and bcache device initialization when trim/discard is enabled.
  • Retain and/or adjust max_discard_sectors configuration where appropriate so discard capability is preserved without driver-specific granularity values.
drivers/block/nbd.c
drivers/mtd/mtd_blkdevs.c
arch/um/drivers/ubd_kern.c
drivers/block/null_blk/main.c
drivers/block/zram/zram_drv.c
drivers/md/bcache/super.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@dongert
dongert requested a lite review from Copilot August 4, 2026 10:42
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from opsiff. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • 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.
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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_granularity default to 512 in blk_set_default_limits() and bump it when blk_queue_logical_block_size() increases the logical block size.
  • Remove driver-specific discard_granularity assignments 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.

Comment on lines 2266 to 2268
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);
Comment thread drivers/md/bcache/super.c
Comment on lines 956 to 959
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;
Comment thread block/blk-settings.c
lim->max_hw_discard_sectors = 0;
lim->max_secure_erase_sectors = 0;
lim->discard_granularity = 0;
lim->discard_granularity = 512;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants