Linux RAID subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Miroslav Cimerman" <mc@raidlab.org>
Cc: linux-raid@vger.kernel.org, yukuai@fygo.io
Subject: Re: [PATCH 7/8] md/raid1,raid10: hold writes that skip a newly failed device
Date: Fri, 27 Nov 2026 01:01:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-236387@kernel.org> (raw)
In-Reply-To: <20261127003231.1180197-8-mc@raidlab.org>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] md/raid1,raid10: ignored errors on faulty device skip hold and
  underflow nr_pending
- [Medium] md/raid10: newly failed replacement devices skipped in
  raid10_write_request()
- [Medium] md/raid1: write-behind bypasses hold for faulty rdev in
  raid1_write_request()
- [Medium] md/raid1,raid10: race with slot_store() when selecting faulty
  rdev
--

commit 1e300613f1afedbc7152ab6fe06cab49a896efac
Author: Miroslav Cimerman <mc@raidlab.org>

md/raid1,raid10: hold writes that skip a newly failed device

This commit assigns writes to a newly Faulty device while it is still
Blocked or after MD_BROKEN is set, so that raid1_submit_write() fails the
copy and holds the write until the superblock records the failure.

[ ... ]

> diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
> index f596b36ec6e10..508673e8c02e8 100644
> --- a/drivers/md/raid1.c
> +++ b/drivers/md/raid1.c
> @@ -1524,8 +1524,15 @@ static bool raid1_write_request(struct mddev *mddev, struct bio *bio,
>  			write_behind = false;
>  
>  		r1_bio->bios[i] = NULL;
> -		if (!rdev || test_bit(Faulty, &rdev->flags))
> +		if (!rdev)
> +			continue;
> +		if (test_bit(Faulty, &rdev->flags)) {
> +			if (raid1_write_to_faulty(mddev, rdev)) {
> +				atomic_inc(&rdev->nr_pending);
> +				r1_bio->bios[i] = bio;
> +			}
>  			continue;
> +		}
>  
>  		if (test_bit(WriteErrorSeen, &rdev->flags)) {
>  			sector_t first_bad;

[Severity: Medium]
Can a write-behind bio still complete early before the superblock update when
a WriteMostly device becomes Faulty after wait_blocked_rdev()?

raid1_write_request() sets write_behind = true for a WriteMostly rdev before
checking Faulty, and later increments r1_bio->behind_remaining when cloning
for that Faulty rdev.  Because write-behind requires an active bitmap,
raid1_add_bio_to_plug() queues the clones instead of calling
raid1_submit_write() immediately, so R1BIO_WriteError is not set yet.  If the
working non-WriteMostly copy completes before raid1_submit_write() fails the
queued Faulty WriteMostly clone, raid1_end_write_request() sees
behind_remaining >= remaining - 1 with R1BIO_Uptodate set and
R1BIO_WriteError still clear, and completes master_bio early.

[Severity: Medium]
Can selecting an already-Faulty rdev here (and in raid10_write_request())
race with slot_store() and cause a NULL pointer dereference?

Unlike state_store(), slot_store() does not suspend the array via
rdev_attr_store().  Because an already-Faulty rdev has In_sync cleared,
slot_store() can remove it as soon as Blocked is clear and nr_pending is 0:

CPU0                                  CPU1
raid1_write_request()
  test_bit(Faulty, &rdev->flags)
  raid1_write_to_faulty(mddev, rdev)
                                      slot_store()
                                        clear_bit(Blocked, &rdev->flags)
                                        remove_and_add_spares()
                                          ...
                                          raid1_remove_disk()
                                            /* nr_pending == 0 */
                                            p->rdev = NULL
  atomic_inc(&rdev->nr_pending)
  r1_bio->bios[i] = bio
  ...
  rdev = conf->mirrors[i].rdev
  bio_alloc_clone(rdev->bdev, ...)    <-- NULL pointer dereference

> diff --git a/drivers/md/raid10.c b/drivers/md/raid10.c
> index 519b6ac0accd0..6ea84eb8bf874 100644
> --- a/drivers/md/raid10.c
> +++ b/drivers/md/raid10.c
> @@ -1387,14 +1387,19 @@ static bool raid10_write_request(struct mddev *mddev, struct bio *bio,
>  
>  		rdev = conf->mirrors[d].rdev;
>  		rrdev = conf->mirrors[d].replacement;
> -		if (rdev && (test_bit(Faulty, &rdev->flags)))
> +		r10_bio->devs[i].bio = NULL;
> +		r10_bio->devs[i].repl_bio = NULL;
> +
> +		if (rdev && test_bit(Faulty, &rdev->flags)) {
> +			if (raid1_write_to_faulty(mddev, rdev)) {
> +				r10_bio->devs[i].bio = bio;
> +				atomic_inc(&rdev->nr_pending);
> +			}
>  			rdev = NULL;
> +		}

[Severity: High]
What happens when bio is a REQ_OP_DISCARD or REQ_RAHEAD request and
raid1_write_to_faulty() gives it to a Faulty rdev (both here and in
raid1_write_request())?

raid1_submit_write() fails the clone with bio_io_error(), but
raid1_end_write_request() and raid10_end_write_request() compute
ignore_error = true for REQ_OP_DISCARD and REQ_RAHEAD bios:

drivers/md/raid10.c:raid10_end_write_request() {
	bool ignore_error = !raid1_should_handle_error(bio) ||
		(bio->bi_status && bio_op(bio) == REQ_OP_DISCARD);
	...
	if (bio->bi_status && !ignore_error) {
		...
	} else {
		...
	}

	one_write_done(r10_bio);
	if (dec_rdev)
		rdev_dec_pending(rdev, conf->mddev);
}

Because ignore_error is true, R1BIO_WriteError / R10BIO_WriteError is never
set, so the request is not held for the superblock update (or failed on
MD_BROKEN).  In raid10_end_write_request(), taking the else branch also
leaves dec_rdev = 1 and decrements rdev->nr_pending while leaving
r10_bio->devs[slot].bio non-NULL with bi_status != 0.  If another copy in the
same r10_bio sets R10BIO_MadeGood and schedules handle_write_completed(),
handle_write_completed() sees bio != NULL && bio->bi_status on the Faulty
slot and calls rdev_dec_pending() a second time, underflowing
rdev->nr_pending (or dereferencing NULL if rdev was already removed).

>  		if (rrdev && (test_bit(Faulty, &rrdev->flags)))
>  			rrdev = NULL;

[Severity: Medium]
Should a newly Faulty replacement device (rrdev) also hold the write while it
is Blocked or after MD_BROKEN is set?

wait_blocked_dev() waits for blocked replacement devices just as it did
before commit f2e7e269a752 ("md/raid10: pull the code that wait for blocked
dev into one function"), and after raid10_spare_active() marks a finished
replacement In_sync and marks the old rdev Faulty (without Blocked), the
active mirror stays in conf->mirrors[d].replacement until
raid10_remove_disk() moves it.  If rrdev fails between wait_blocked_dev() and
this loop (or after MD_BROKEN is set), setting rrdev = NULL skips it without
holding the write, allowing the write to complete before the superblock
records rrdev's failure.  (Note that raid10_end_write_request() and
handle_write_completed() also currently do not set R10BIO_WriteError or hold
writes when a repl_bio fails.)

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261127003231.1180197-1-mc@raidlab.org?part=7

  reply	other threads:[~2026-11-27  1:01 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  0:32 [PATCH 0/8] md/raid1,raid10: fix writes acknowledged before a failure is recorded Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 1/8] md/raid1,raid10: fail held writes when the array fails Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 2/8] md/raid10: wait for the devices of the write in wait_blocked_dev() Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 3/8] md/raid1,raid10: fail writes that waited while the array failed Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 4/8] md/raid1,raid10: fail held writes that no working device has Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 5/8] md/raid1,raid10: hold writes that fail on a faulty device Miroslav Cimerman
2026-11-27  0:57   ` sashiko-bot
2026-11-27  0:32 ` [PATCH 6/8] md/raid1: set MD_SB_CHANGE_PENDING under device_lock in raid1_error() Miroslav Cimerman
2026-11-27  0:32 ` [PATCH 7/8] md/raid1,raid10: hold writes that skip a newly failed device Miroslav Cimerman
2026-11-27  1:01   ` sashiko-bot [this message]
2026-11-27  0:32 ` [PATCH 8/8] md/raid1: hold write-behind writes that failed on a device Miroslav Cimerman

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=sashiko-outbox-236387@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=mc@raidlab.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --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