md/raid5: discard accounting and llbitmap support
v2 cover md/raid5: discard accounting and llbitmap support
From: Yu Kuai <[email protected]>
Hi,
This series fixes raid5 discard IO accounting and lets llbitmap arrays use discard to convert llbitmap bits even when lower member devices do not support discard.
Patch 1 accounts the full-stripe range that raid5 handles before discard is split.
Patch 2 advertises a UINT_MAX discard limit from raid5. Lower discard support is cached in r5conf when raid5 sets queue limits, and unsupported discard bios are rejected before stripe work is queued.
Patch 3 lets llbitmap discard bypass the lower-device support check. If lower discard is not supported, raid5 completes the accounted clone after md_account_bio(), so llbitmap conversion callbacks run without issuing member discard bios.
Changes since v1:
- Account the exact full-stripe range handled by raid5 before converting llbitmap bits.
- Always advertise raid5 discard limits and move lower-device support validation to the request path.
- Cache unsupported lower discard state in r5conf as raid5_discard_unsupported.
- Keep llbitmap discard useful even when lower member devices cannot safely handle discard.
Validation:
- QEMU RAID5 discard test with devices_handle_discard_safely=N:
llbitmap bit conversion matched full-stripe aligned and unaligned discard ranges, md discard iostat advanced for handled llbitmap discards, lower member discard counters stayed at zero, and normal RAID5 discarded no IO when lower discard was unsupported.
Thanks,
Kuai
Yu Kuai (3):
md/raid5: account discard IO
md/raid5: validate discard support at request time md/raid5: always convert llbitmap bits for discard
drivers/md/raid5.c | 74 +++++++++++++++++++++++++++++++--------------- drivers/md/raid5.h | 1 + 2 files changed, 51 insertions(+), 24 deletions(-)
base-commit: 717359a168bb66ac95f6161715d17e491ee86ca7
Signature · 1 line
2.51.0
PATCH v2 1/3 md/raid5: account discard IO
From: Yu Kuai <[email protected]>
Raid5 handles discard bios internally through make_discard_request() and never passes them through md_account_bio(). As a result, discard IO is missing the md-device iostat accounting that normal raid5 IO and discard IO in other raid levels get from md_account_bio().
Before accounting the bio, trim the request to the full data stripes that raid5 will actually discard. The first full stripe is the ceiling of the bio start divided by data-stripe sectors, and the last full stripe is the floor of the bio end divided by data-stripe sectors. Account that exact MD logical full-stripe range, then restore the original iterator so bio completion and iostat still cover the original request.
--- drivers/md/raid5.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-)
drivers/md/raid5.c +23 -10
@@ -5688,34 +5688,47 @@ static void release_stripe_plug(struct mddev *mddev,static void make_discard_request (struct mddev *mddev,struct bio *bi) {struct r5conf *conf = mddev->private;sector_t logical_sector, last_sector; +sector_t first_stripe, last_stripe;struct stripe_head *sh; +struct bvec_iter bi_iter; +struct bio *orig_bi = bi;int stripe_sectors;/* We need to handle this when io_uring supports discard/trim */ if (WARN_ON_ONCE (bi->bi_opf &REQ_NOWAIT ))return ;if (mddev->reshape_position != MaxSector)/* Skip discard while reshape is happening */ return ; - logical_sector = bi->bi_iter.bi_sector & ~((sector_t )RAID5_STRIPE_SECTORS (conf)-1 ); - last_sector =bio_end_sector (bi); - - bi->bi_next =NULL ; - stripe_sectors = conf->chunk_sectors * (conf->raid_disks - conf->max_degraded); - logical_sector =DIV_ROUND_UP_SECTOR_T (logical_sector, - stripe_sectors); -sector_div (last_sector, stripe_sectors); + first_stripe =DIV_ROUND_UP_SECTOR_T (bi->bi_iter.bi_sector, + stripe_sectors); + last_stripe =bio_end_sector (bi); +sector_div (last_stripe, stripe_sectors); + +if (first_stripe >= last_stripe) { +bio_endio (bi); +return ; + } + + bi_iter = bi->bi_iter; + bi->bi_iter.bi_sector = first_stripe * stripe_sectors; + bi->bi_iter.bi_size = ((last_stripe - first_stripe) * + stripe_sectors) <<9 ; +md_account_bio (mddev, &bi); + orig_bi->bi_iter = bi_iter; + bi->bi_iter = bi_iter; + bi->bi_next =NULL ; - logical_sector *= conf->chunk_sectors; - last_sector *= conf->chunk_sectors; + logical_sector = first_stripe * conf->chunk_sectors; + last_sector = last_stripe * conf->chunk_sectors;for (; logical_sector < last_sector; logical_sector +=RAID5_STRIPE_SECTORS (conf)) {DEFINE_WAIT (w);int d;
Signature · 1 line
2.51.0
PATCH v2 2/3 md/raid5: validate discard support at request time
In reply to Yu KuaiFrom: Yu Kuai <[email protected]>
Raid5 used to disable discard limits when devices_handle_discard_safely was not set or when stacked member limits could not support a full-stripe discard. That hides discard from userspace before raid5 can decide whether a request can be handled safely.
Follow other virtual drivers and advertise a UINT_MAX discard limit for the md device. Cache lower discard support in r5conf when setting queue limits, and reject unsupported discard bios before queuing stripe work.
--- drivers/md/raid5.c | 34 +++++++++++++++++++--------------- drivers/md/raid5.h | 1 + 2 files changed, 20 insertions(+), 15 deletions(-)
drivers/md/raid5.c +19 -15
@@ -1132,10 +1132,22 @@ static void defer_issue_bios(struct r5conf *conf, sector_t sector,spin_unlock (&conf->pending_bios_lock);dispatch_bio_list (&tmp); } +static bool raid5_discard_limits (struct mddev *mddev,struct bio *bi) +{ +struct r5conf *conf = mddev->private; + +if (!conf->raid5_discard_unsupported) +return true; + + bi->bi_status =BLK_STS_NOTSUPP ; +bio_endio (bi); +return false; +} +static void raid5_end_read_request (struct bio *bi);static void raid5_end_write_request (struct bio *bi);
@@ -5702,10 +5714,13 @@ static void make_discard_request(struct mddev *mddev, struct bio *bi)if (mddev->reshape_position != MaxSector)/* Skip discard while reshape is happening */ return ; +if (!raid5_discard_limits (mddev, bi)) +return ; + stripe_sectors = conf->chunk_sectors * (conf->raid_disks - conf->max_degraded); first_stripe =DIV_ROUND_UP_SECTOR_T (bi->bi_iter.bi_sector, stripe_sectors); last_stripe =bio_end_sector (bi);
@@ -7815,36 +7830,25 @@ static int raid5_set_limits(struct mddev *mddev)mddev_stack_rdev_limits (mddev, &lim,0 );rdev_for_each (rdev, mddev)queue_limits_stack_bdev (&lim, rdev->bdev, rdev->new_data_offset, mddev->gendisk->disk_name); -/* -* Zeroing is required for discard, otherwise data could be lost. -* -* Consider a scenario: discard a stripe (the stripe could be -* inconsistent if discard_zeroes_data is 0); write one disk of the -* stripe (the stripe could be inconsistent again depending on which -* disks are used to calculate parity); the disk is broken; The stripe -* data of this disk is lost. -* -* We only allow DISCARD if the sysadmin has confirmed that only safe -* devices are in use by setting a module parameter. A better idea -* might be to turn DISCARD into WRITE_ZEROES requests, as that is -* required to be safe. -*/ if (!devices_handle_discard_safely || lim.max_discard_sectors < (stripe >>9 ) || lim.discard_granularity < stripe) - lim.max_hw_discard_sectors =0 ; + conf->raid5_discard_unsupported = true; +else + conf->raid5_discard_unsupported = false;/* * Requests require having a bitmap for each stripe. * Limit the max sectors based on this. */ lim.max_hw_sectors =RAID5_MAX_REQ_STRIPES <<RAID5_STRIPE_SHIFT (conf);if ((lim.max_hw_sectors <<9 ) < lim.io_opt) lim.max_hw_sectors = lim.io_opt >>9 ; + lim.max_hw_discard_sectors =UINT_MAX ;/* No restrictions on the number of segments in the request */ lim.max_segments =USHRT_MAX ;return queue_limits_set (mddev->gendisk->queue, &lim);
drivers/md/raid5.h +1 -0
@@ -687,10 +687,11 @@ struct r5conf {struct r5pending_data *pending_data;struct list_head free_list;struct list_head pending_list;int pending_data_cnt;struct r5pending_data *next_pending_data; +bool raid5_discard_unsupported;mempool_t *ctx_pool;int ctx_size; };
Signature · 1 line
2.51.0
PATCH v2 3/3 md/raid5: always convert llbitmap bits for discard
In reply to Yu KuaiFrom: Yu Kuai <[email protected]>
llbitmap discard is useful even when no underlying member device supports it. The discard still converts the llbitmap range to unwritten, so later reads and recovery do not rely on stale parity for that range.
Let llbitmap discard bypass the raid5 lower discard support check. If lower discard is not safe or not supported, complete the accounted clone after md_account_bio() so the llbitmap conversion callbacks run without member discard bios.
--- drivers/md/raid5.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/md/raid5.c +9 -0
@@ -1136,10 +1136,13 @@ static void defer_issue_bios(struct r5conf *conf, sector_t sector,static bool raid5_discard_limits (struct mddev *mddev,struct bio *bi) {struct r5conf *conf = mddev->private; +if (mddev->bitmap_id ==ID_LLBITMAP ) +return true; +if (!conf->raid5_discard_unsupported)return true; bi->bi_status =BLK_STS_NOTSUPP ;bio_endio (bi);
@@ -5738,10 +5741,16 @@ static void make_discard_request(struct mddev *mddev, struct bio *bi)md_account_bio (mddev, &bi); orig_bi->bi_iter = bi_iter; bi->bi_iter = bi_iter; bi->bi_next =NULL ; +if (mddev->bitmap_id ==ID_LLBITMAP && + conf->raid5_discard_unsupported) { +bio_endio (bi); +return ; + } + logical_sector = first_stripe * conf->chunk_sectors; last_sector = last_stripe * conf->chunk_sectors;for (; logical_sector < last_sector; logical_sector +=RAID5_STRIPE_SECTORS (conf)) {
Signature · 1 line
2.51.0