Re: [PATCH] md/raid5: protect bitmap batch counters aka seq_flush/seq_write
Chen Cheng <[email protected]>
| Newsgroups | gmane.linux.raid |
|---|---|
| Message-ID | <[email protected]> |
在 2026/6/16 21:30, [email protected] 写道: > Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: > > Pre-existing issues: > - [Critical] Missing full memory barriers between the bitmap update/scan and `seq_flush` access allows torn writes. > -- > > commit 45ef15a300eccd14518bc90e254146a73a49bf51 > Author: Chen Cheng <[email protected]> > > md/raid5: protect bitmap batch counters aka seq_flush/seq_write > > This commit attempts to fix a KCSAN data-race report on conf->seq_flush > in the raid5 code by introducing READ_ONCE() and WRITE_ONCE(). > >> diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c >> index d0d7f96fd7cd9..ffc89ebbc4857 100644 >> --- a/drivers/md/raid5.c >> +++ b/drivers/md/raid5.c >> @@ -3538,7 +3538,7 @@ static void __add_stripe_bio(struct stripe_head *sh, struct bio *bi, >> sh->dev[dd_idx].sector); >> >> if (conf->mddev->bitmap && firstwrite && !sh->batch_head) { >> - sh->bm_seq = conf->seq_flush+1; >> + sh->bm_seq = READ_ONCE(conf->seq_flush) + 1; > [Severity: Critical] > This is a pre-existing issue, but does this lockless synchronization require > a full memory barrier (smp_mb()) before reading seq_flush? > > The producer sequence updates the in-memory bitmap (which drops a lock, > providing only a Store-Release barrier) and then reads seq_flush. Because > there is no full memory barrier between the store to the bitmap and the load > of seq_flush, weakly-ordered CPUs (and even x86, which allows Store-Load > reordering) can reorder the read of seq_flush before the store to the > bitmap becomes globally visible. > > Could this lead to silent data corruption on a crash if the consumer sees > the old bitmap state but the producer assigns the stripe to the current > batch? barrier provide a tool to promise "the things before this statement all done", and I don't think I need such strong promise. What I need is, "all cpu cache coherence for accesses variable seq_flush", so READ_ONCE/WRITE_ONCE is suitable for this scenario. >> set_bit(STRIPE_BIT_DELAY, &sh->state); >> } >> } > [ ... ] > >> @@ -5755,7 +5755,7 @@ static void make_discard_request(struct mddev *mddev, struct bio *bi) >> } >> spin_unlock_irq(&sh->stripe_lock); >> if (conf->mddev->bitmap) { >> - sh->bm_seq = conf->seq_flush + 1; >> + sh->bm_seq = READ_ONCE(conf->seq_flush) + 1; > [Severity: Critical] > This is a pre-existing issue, but the same missing full memory barrier > pattern appears here in the discard path as well. > >> set_bit(STRIPE_BIT_DELAY, &sh->state); >> } >> > [ ... ] > >> @@ -6805,12 +6805,13 @@ static void raid5d(struct md_thread *thread) >> if ( >> !list_empty(&conf->bitmap_list)) { >> /* Now is a good time to flush some bitmap updates */ >> - conf->seq_flush++; >> + int seq = READ_ONCE(conf->seq_flush) + 1; >> + WRITE_ONCE(conf->seq_flush, seq); > [Severity: Critical] > This isn't a bug introduced by this patch, but does the consumer side > also rely dangerously on implicit barriers? > > The consumer (raid5d) writes seq_flush and relies on an implicit full > barrier deep inside unplug() (via test_and_clear_bit) to order the > subsequent bitmap scan. > > If the producer's missing barrier allows the read of seq_flush to be > reordered, the consumer might flush the old batch and advance seq_write, > unblocking the producer's stripe to be written to disk without its > write-intent bitmap bit being flushed. > >> spin_unlock_irq(&conf->device_lock); >> if (md_bitmap_enabled(mddev, true)) >> mddev->bitmap_ops->unplug(mddev, true); >> spin_lock_irq(&conf->device_lock); >> - conf->seq_write = conf->seq_flush; >> + WRITE_ONCE(conf->seq_write, seq); >> activate_bit_delay(conf, conf->temp_inactive_list); >> } >> raid5_activate_delayed(conf);