From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 272A44D179E; Thu, 17 Sep 2026 17:04:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789664658; cv=none; b=R7zNyuznm2G1UFAolDOvxxqB5ZzZoV6606qS/YRbKA/s+smMmlP1VFb57qycVtfuxf7bG0LvvASJmrHuZcuttZbA0uvHz2ujkl36iNhJlP9eivZVOWAjHbfQ2oW6HkBsjI77SyUKilFc/aOYtBOcPk+8nbojaEP+PR5L8NJW2mc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789664658; c=relaxed/simple; bh=I9fTwN7ALVJoiEIuQjE9KlDOZjfBWe8ytlMLzBGozPU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=MEFOqRIpWu6UOwcnlYU0f9e2XfuxcXtxRpVtLuR6ki2BCzKFQU6b1vUfHC0mSWbDr+i/1pt4sa3CFroQp8nPwdXpHSvNjKZEGtcqYiC3rxeGedGCe2Pygtr/jUh5aKwHFDfPO1N3v6T2A+ATBsEOZuBt1YJ4yjx8DA8UDrP4h4w= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=NyvmmcGE; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="NyvmmcGE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7C1D11F000FF; Thu, 17 Sep 2026 17:04:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1789664657; bh=olq62mTnec27Ic3zesQcIK/bTbAWbJoJ+enn8YiCEFw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=NyvmmcGEO300G22w6BDGPTjF1qyWtXENnbFDnuhVTNs8cM15Blh4j3wozg5XyCOvh T3Sb/j/V0OY3vIz7voxonW/wlUpVpEZ4+1Nm7/nflZhM3Wok5iARkZPNrab9RP9ctv y/XVW8I22veNcT7v1xysOK6MAi3IFuG/dMVwq+Lk= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Yu Kuai , Sasha Levin Subject: [PATCH 6.18 0446/1250] md/raid5: account discard IO Date: Thu, 17 Sep 2026 16:04:01 +0100 Message-ID: <20260917151604.088316442@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260917151551.901433442@linuxfoundation.org> References: <20260917151551.901433442@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.18-stable review patch. If anyone has any objections, please let me know. ------------------ From: Yu Kuai [ Upstream commit 74ddbf98e2db646ec58f7e7731c936b7a4a470fe ] 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. Link: https://patch.msgid.link/20260605072639.2434847-2-yukuai@kernel.org Signed-off-by: Yu Kuai Signed-off-by: Sasha Levin --- drivers/md/raid5.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c index 84168ce7a0ff6..bf30ad2a9e177 100644 --- a/drivers/md/raid5.c +++ b/drivers/md/raid5.c @@ -5700,7 +5700,10 @@ 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 */ @@ -5711,19 +5714,29 @@ static void make_discard_request(struct mddev *mddev, struct bio *bi) /* 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)) { -- 2.53.0