When a write-intent bitmap is used together with --write-behind on a --write-mostly member, the on-disk bitmap dirty bit (and Events Cleared) can be cleared before the deferred write to the write-mostly member has actually reached the disk. If a crash or I/O error happens in that window, a later bitmap-based resync (e.g. --re-add) skips chunks that were never written to the write-mostly device, silently corrupting data while the array reports itself clean and in sync. Commit cd5fc653381811 ("md/md-bitmap: move bitmap_{start, end}write to md upper layer") moved the bitmap chunk start/end accounting into the md upper layer: md_clone_bio() starts it and md_end_clone_io() ends it when the master bio completes. For write-behind, raid1_end_write_request() ends the master bio early via call_bio_endio() as soon as the non-write-mostly mirrors are done (behind_remaining >= remaining - 1), while the write-mostly bio is still in flight. That early completion runs md_end_clone_io() -> md_bitmap_end() and clears the chunk too soon. Before that commit, raid1 ended the bitmap chunk in close_write(), which runs only once r1_bio->remaining == 0, i.e. after every member including the write-behind copy has completed. Restore that ordering for the write-behind case. Add md_io_clone->no_bitmap_end so a personality can tell the md upper layer not to end the bitmap chunk in md_end_clone_io(). raid1 sets it when arming behind I/O and ends the chunk itself in close_write(), using r1_bio->sector/sectors (raid1 has no ->bitmap_sector hook and md_account_bio() runs after the request is split, so they match the clone's offset/sectors). Fixes: cd5fc653381811 ("md/md-bitmap: move bitmap_{start, end}write to md upper layer") Reported-by: Yuki Inoguchi Signed-off-by: Nigel Croxon ---  drivers/md/md.c    |  3 ++-  drivers/md/md.h    |  6 ++++++  drivers/md/raid1.c | 31 ++++++++++++++++++++++++++++---  3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/drivers/md/md.c b/drivers/md/md.c index 03e35386a802..61ecb21c2781 100644 --- a/drivers/md/md.c +++ b/drivers/md/md.c @@ -9495,7 +9495,7 @@ static void md_end_clone_io(struct bio *bio)      struct completion *reshape_completion = bio->bi_private;      if (bio_data_dir(orig_bio) == WRITE && md_io_clone->sectors && -        md_bitmap_enabled(mddev, false)) +        !md_io_clone->no_bitmap_end && md_bitmap_enabled(mddev, false))          md_bitmap_end(mddev, md_io_clone);      if (bio->bi_status && !orig_bio->bi_status) @@ -9523,6 +9523,7 @@ static void md_clone_bio(struct mddev *mddev, struct bio **bio)      md_io_clone->orig_bio = *bio;      md_io_clone->mddev = mddev;      md_io_clone->sectors = 0; +    md_io_clone->no_bitmap_end = false;      if (blk_queue_io_stat(bdev->bd_disk->queue))          md_io_clone->start_time = bio_start_io_acct(*bio);      else diff --git a/drivers/md/md.h b/drivers/md/md.h index 6440da292105..b338368c0307 100644 --- a/drivers/md/md.h +++ b/drivers/md/md.h @@ -885,6 +885,12 @@ struct md_io_clone {      sector_t    offset;      unsigned long    sectors;      enum stat_group    rw; +    /* +     * When set, md_end_clone_io() will NOT end the write-intent bitmap +     * chunk for this clone; the personality (e.g. raid1 write-behind) ends +     * it itself once all member writes have completed. +     */ +    bool        no_bitmap_end;      struct bio    bio_clone;  }; diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c index 894efb50c6d5..0cfb9c91aa31 100644 --- a/drivers/md/raid1.c +++ b/drivers/md/raid1.c @@ -445,8 +445,21 @@ static void close_write(struct r1bio *r1_bio)          r1_bio->behind_master_bio = NULL;      } -    if (test_bit(R1BIO_BehindIO, &r1_bio->state)) +    if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { +        /* +         * The write-intent bitmap chunk endwrite was deferred for this +         * write-behind request (md_io_clone->no_bitmap_end) so the dirty +         * bit stays set until the slow write-mostly copy is on disk.  All +         * members have now completed (r1_bio->remaining == 0), so end the +         * chunk here -- the pre-regression end point.  Otherwise the early +         * behind-write ACK would clear the bit while the write-mostly +         * write was still in flight, silently losing data on --re-add. +         */ +        if (md_bitmap_enabled(mddev, false)) +            mddev->bitmap_ops->end_write(mddev, r1_bio->sector, +                             r1_bio->sectors);          mddev->bitmap_ops->end_behind_write(mddev); +    }      md_write_end(mddev);  } @@ -1453,9 +1466,21 @@ static void raid1_start_write_behind(struct mddev *mddev, struct r1bio *r1_bio,      if (!stats.behind_wait && stats.behind_writes < max_write_behind)          alloc_behind_master_bio(r1_bio, bio); -    if (test_bit(R1BIO_BehindIO, &r1_bio->state)) -        mddev->bitmap_ops->start_behind_write(mddev); +    if (test_bit(R1BIO_BehindIO, &r1_bio->state)) { +        /* +         * Defer the bitmap chunk endwrite to close_write() for this +         * write-behind request.  Otherwise the md upper layer would end +         * it from md_end_clone_io() on the early behind-write ACK, while +         * the write-mostly copy is still in flight.  See close_write() +         * and md_io_clone->no_bitmap_end. +         */ +        struct md_io_clone *md_io_clone = +            container_of(r1_bio->master_bio, struct md_io_clone, +                     bio_clone); +        md_io_clone->no_bitmap_end = true; +        mddev->bitmap_ops->start_behind_write(mddev); +    }  }  static bool raid1_write_request(struct mddev *mddev, struct bio *bio, -- 2.47.3