Linux RAID subsystem development
 help / color / mirror / Atom feed
From: "Chen Cheng" <chencheng@fnnas.com>
To: <linux-raid@vger.kernel.org>, <yukuai@fygo.io>, <yukuai@fnnas.com>
Cc: <chencheng@fnnas.com>, <chenchneg33@gmail.com>,
	 <linux-kernel@vger.kernel.org>
Subject: [PATCH] md/raid10: protect head_position accesses consistency
Date: Thu, 18 Jun 2026 10:52:06 +0800	[thread overview]
Message-ID: <20260618025206.906035-1-chencheng@fnnas.com> (raw)

From: Chen Cheng <chencheng@fnnas.com>

the completion paths update conf->mirrors[x].head_position
without a lock, while read_balance() reads the same field
locklessly.

protect conf->mirror[x].head_position consistency for
multi-CPUs by READ_ONCE()/WRITE_ONCE().

the report #1:
=====================
 BUG: KCSAN: data-race in find_bio_disk.isra.0 / read_balance

 write to 0xffff8f0309679fd0 of 8 bytes by interrupt on cpu 9:
  find_bio_disk.isra.0+0x108/0x180
  raid10_end_write_request+0xd2/0x530
  bio_endio+0x3c9/0x3e0
 [...]

 read to 0xffff8f0309679fd0 of 8 bytes by task 338766 on cpu 7:
  read_balance+0x2ec/0x700
  raid10_read_request+0x17b/0x550
  raid10_make_request+0x27e/0x1080
  md_handle_request+0x2c5/0x700
  [...]

 value changed: 0x000000000032f440 -> 0x000000000014f8e0

the report #2:
=======================
 BUG: KCSAN: data-race in raid10_end_read_request / read_balance

 write to 0xffff8f03291091b0 of 8 bytes by interrupt on cpu 9:
  raid10_end_read_request+0xf0/0x400
  bio_endio+0x3c9/0x3e0
  [...]

 read to 0xffff8f03291091b0 of 8 bytes by task 336943 on cpu 8:
  read_balance+0x2ec/0x700
  raid10_read_request+0x17b/0x550
  raid10_make_request+0x27e/0x1080
  md_handle_request+0x2c5/0x700
 [...]

 value changed: 0x000000000041c188 -> 0x000000000041c1b8

Signed-off-by: Chen Cheng <chencheng@fnnas.com>
---
 drivers/md/raid10.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
index caf1da15aa87..55aa052a957b 100644
--- a/drivers/md/raid10.c
+++ b/drivers/md/raid10.c
@@ -343,12 +343,12 @@ static void raid_end_bio_io(struct r10bio *r10_bio)
  */
 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
 {
 	struct r10conf *conf = r10_bio->mddev->private;
 
-	conf->mirrors[r10_bio->devs[slot].devnum].head_position =
-		r10_bio->devs[slot].addr + (r10_bio->sectors);
+	WRITE_ONCE(conf->mirrors[r10_bio->devs[slot].devnum].head_position,
+		   r10_bio->devs[slot].addr + (r10_bio->sectors));
 }
 
 /*
  * Find the disk number which triggered given bio
  */
@@ -841,11 +841,11 @@ static struct md_rdev *read_balance(struct r10conf *conf,
 		/* for far > 1 always use the lowest address */
 		else if (geo->far_copies > 1)
 			new_distance = r10_bio->devs[slot].addr;
 		else
 			new_distance = abs(r10_bio->devs[slot].addr -
-					   conf->mirrors[disk].head_position);
+					   READ_ONCE(conf->mirrors[disk].head_position));
 
 		if (new_distance < best_dist) {
 			best_dist = new_distance;
 			best_dist_slot = slot;
 			best_dist_rdev = rdev;
-- 
2.54.0

                 reply	other threads:[~2026-06-18  2:52 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260618025206.906035-1-chencheng@fnnas.com \
    --to=chencheng@fnnas.com \
    --cc=chenchneg33@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=yukuai@fnnas.com \
    --cc=yukuai@fygo.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox