Skip to content

Commit 3e148a3

Browse files
GuoqingJiangliu-song-6
authored andcommitted
md/raid1: fix potential data inconsistency issue with write behind device
For write-behind mode, we think write IO is complete once it has reached all the non-writemostly devices. It works fine for single queue devices. But for multiqueue device, if there are lots of IOs come from upper layer, then the write-behind device could issue those IOs to different queues, depends on the each queue's delay, so there is no guarantee that those IOs can arrive in order. To address the issue, we need to check the collision among write behind IOs, we can only continue without collision, otherwise wait for the completion of previous collisioned IO. And WBCollision is introduced for multiqueue device which is worked under write-behind mode. But this patch doesn't handle below cases which could have the data inconsistency issue as well, these cases will be handled in later patches. 1. modify max_write_behind by write backlog node. 2. add or remove array's bitmap dynamically. 3. the change of member disk. Reviewed-by: NeilBrown <neilb@suse.com> Signed-off-by: Guoqing Jiang <gqjiang@suse.com> Signed-off-by: Song Liu <songliubraving@fb.com>
1 parent 0ce3537 commit 3e148a3

3 files changed

Lines changed: 129 additions & 1 deletion

File tree

‎drivers/md/md.c‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,19 @@ static inline int speed_max(struct mddev *mddev)
124124
mddev->sync_speed_max : sysctl_speed_limit_max;
125125
}
126126

127+
static int rdev_init_wb(struct md_rdev *rdev)
128+
{
129+
if (rdev->bdev->bd_queue->nr_hw_queues == 1)
130+
return 0;
131+
132+
spin_lock_init(&rdev->wb_list_lock);
133+
INIT_LIST_HEAD(&rdev->wb_list);
134+
init_waitqueue_head(&rdev->wb_io_wait);
135+
set_bit(WBCollisionCheck, &rdev->flags);
136+
137+
return 1;
138+
}
139+
127140
static struct ctl_table_header *raid_table_header;
128141

129142
static struct ctl_table raid_table[] = {
@@ -5597,6 +5610,32 @@ int md_run(struct mddev *mddev)
55975610
md_bitmap_destroy(mddev);
55985611
goto abort;
55995612
}
5613+
5614+
if (mddev->bitmap_info.max_write_behind > 0) {
5615+
bool creat_pool = false;
5616+
5617+
rdev_for_each(rdev, mddev) {
5618+
if (test_bit(WriteMostly, &rdev->flags) &&
5619+
rdev_init_wb(rdev))
5620+
creat_pool = true;
5621+
}
5622+
if (creat_pool && mddev->wb_info_pool == NULL) {
5623+
mddev->wb_info_pool =
5624+
mempool_create_kmalloc_pool(NR_WB_INFOS,
5625+
sizeof(struct wb_info));
5626+
if (!mddev->wb_info_pool) {
5627+
err = -ENOMEM;
5628+
mddev_detach(mddev);
5629+
if (mddev->private)
5630+
pers->free(mddev, mddev->private);
5631+
mddev->private = NULL;
5632+
module_put(pers->owner);
5633+
md_bitmap_destroy(mddev);
5634+
goto abort;
5635+
}
5636+
}
5637+
}
5638+
56005639
if (mddev->queue) {
56015640
bool nonrot = true;
56025641

@@ -5825,6 +5864,8 @@ static void __md_stop_writes(struct mddev *mddev)
58255864
mddev->in_sync = 1;
58265865
md_update_sb(mddev, 1);
58275866
}
5867+
mempool_destroy(mddev->wb_info_pool);
5868+
mddev->wb_info_pool = NULL;
58285869
}
58295870

58305871
void md_stop_writes(struct mddev *mddev)

‎drivers/md/md.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ struct md_rdev {
109109
* for reporting to userspace and storing
110110
* in superblock.
111111
*/
112+
113+
/*
114+
* The members for check collision of write behind IOs.
115+
*/
116+
struct list_head wb_list;
117+
spinlock_t wb_list_lock;
118+
wait_queue_head_t wb_io_wait;
119+
112120
struct work_struct del_work; /* used for delayed sysfs removal */
113121

114122
struct kernfs_node *sysfs_state; /* handle for 'state'
@@ -193,6 +201,10 @@ enum flag_bits {
193201
* it didn't fail, so don't use FailFast
194202
* any more for metadata
195203
*/
204+
WBCollisionCheck, /*
205+
* multiqueue device should check if there
206+
* is collision between write behind bios.
207+
*/
196208
};
197209

198210
static inline int is_badblock(struct md_rdev *rdev, sector_t s, int sectors,
@@ -245,6 +257,14 @@ enum mddev_sb_flags {
245257
MD_SB_NEED_REWRITE, /* metadata write needs to be repeated */
246258
};
247259

260+
#define NR_WB_INFOS 8
261+
/* record current range of write behind IOs */
262+
struct wb_info {
263+
sector_t lo;
264+
sector_t hi;
265+
struct list_head list;
266+
};
267+
248268
struct mddev {
249269
void *private;
250270
struct md_personality *pers;
@@ -461,6 +481,7 @@ struct mddev {
461481
*/
462482
struct work_struct flush_work;
463483
struct work_struct event_work; /* used by dm to report failure event */
484+
mempool_t *wb_info_pool;
464485
void (*sync_super)(struct mddev *mddev, struct md_rdev *rdev);
465486
struct md_cluster_info *cluster_info;
466487
unsigned int good_device_nr; /* good device num within cluster raid */

‎drivers/md/raid1.c‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,57 @@ static void lower_barrier(struct r1conf *conf, sector_t sector_nr);
5050

5151
#include "raid1-10.c"
5252

53+
static int check_and_add_wb(struct md_rdev *rdev, sector_t lo, sector_t hi)
54+
{
55+
struct wb_info *wi, *temp_wi;
56+
unsigned long flags;
57+
int ret = 0;
58+
struct mddev *mddev = rdev->mddev;
59+
60+
wi = mempool_alloc(mddev->wb_info_pool, GFP_NOIO);
61+
62+
spin_lock_irqsave(&rdev->wb_list_lock, flags);
63+
list_for_each_entry(temp_wi, &rdev->wb_list, list) {
64+
/* collision happened */
65+
if (hi > temp_wi->lo && lo < temp_wi->hi) {
66+
ret = -EBUSY;
67+
break;
68+
}
69+
}
70+
71+
if (!ret) {
72+
wi->lo = lo;
73+
wi->hi = hi;
74+
list_add(&wi->list, &rdev->wb_list);
75+
} else
76+
mempool_free(wi, mddev->wb_info_pool);
77+
spin_unlock_irqrestore(&rdev->wb_list_lock, flags);
78+
79+
return ret;
80+
}
81+
82+
static void remove_wb(struct md_rdev *rdev, sector_t lo, sector_t hi)
83+
{
84+
struct wb_info *wi;
85+
unsigned long flags;
86+
int found = 0;
87+
struct mddev *mddev = rdev->mddev;
88+
89+
spin_lock_irqsave(&rdev->wb_list_lock, flags);
90+
list_for_each_entry(wi, &rdev->wb_list, list)
91+
if (hi == wi->hi && lo == wi->lo) {
92+
list_del(&wi->list);
93+
mempool_free(wi, mddev->wb_info_pool);
94+
found = 1;
95+
break;
96+
}
97+
98+
if (!found)
99+
WARN_ON("The write behind IO is not recorded\n");
100+
spin_unlock_irqrestore(&rdev->wb_list_lock, flags);
101+
wake_up(&rdev->wb_io_wait);
102+
}
103+
53104
/*
54105
* for resync bio, r1bio pointer can be retrieved from the per-bio
55106
* 'struct resync_pages'.
@@ -446,6 +497,12 @@ static void raid1_end_write_request(struct bio *bio)
446497
}
447498

448499
if (behind) {
500+
if (test_bit(WBCollisionCheck, &rdev->flags)) {
501+
sector_t lo = r1_bio->sector;
502+
sector_t hi = r1_bio->sector + r1_bio->sectors;
503+
504+
remove_wb(rdev, lo, hi);
505+
}
449506
if (test_bit(WriteMostly, &rdev->flags))
450507
atomic_dec(&r1_bio->behind_remaining);
451508

@@ -1443,7 +1500,16 @@ static void raid1_write_request(struct mddev *mddev, struct bio *bio,
14431500
mbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
14441501

14451502
if (r1_bio->behind_master_bio) {
1446-
if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1503+
struct md_rdev *rdev = conf->mirrors[i].rdev;
1504+
1505+
if (test_bit(WBCollisionCheck, &rdev->flags)) {
1506+
sector_t lo = r1_bio->sector;
1507+
sector_t hi = r1_bio->sector + r1_bio->sectors;
1508+
1509+
wait_event(rdev->wb_io_wait,
1510+
check_and_add_wb(rdev, lo, hi) == 0);
1511+
}
1512+
if (test_bit(WriteMostly, &rdev->flags))
14471513
atomic_inc(&r1_bio->behind_remaining);
14481514
}
14491515

0 commit comments

Comments
 (0)