Skip to content

Commit 963c555

Browse files
GuoqingJiangliu-song-6
authored andcommitted
md: introduce mddev_create/destroy_wb_pool for the change of member device
Previously, we called rdev_init_wb to avoid potential data inconsistency when array is created. Now, we need to call the function and create mempool if a device is added or just be flaged as "writemostly". So mddev_create_wb_pool is introduced and called accordingly. And for safety reason, we mark implicit GFP_NOIO allocation scope for create mempool during mddev_suspend/mddev_resume. And mempool should be removed conversely after remove a member device or its's "writemostly" flag, which is done by call mddev_destroy_wb_pool. Reviewed-by: NeilBrown <neilb@suse.com> Signed-off-by: Guoqing Jiang <gqjiang@suse.com> Signed-off-by: Song Liu <songliubraving@fb.com>
1 parent 3e148a3 commit 963c555

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

‎drivers/md/md.c‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
3838
*/
3939

40+
#include <linux/sched/mm.h>
4041
#include <linux/sched/signal.h>
4142
#include <linux/kthread.h>
4243
#include <linux/blkdev.h>
@@ -137,6 +138,64 @@ static int rdev_init_wb(struct md_rdev *rdev)
137138
return 1;
138139
}
139140

141+
/*
142+
* Create wb_info_pool if rdev is the first multi-queue device flaged
143+
* with writemostly, also write-behind mode is enabled.
144+
*/
145+
void mddev_create_wb_pool(struct mddev *mddev, struct md_rdev *rdev,
146+
bool is_suspend)
147+
{
148+
if (mddev->bitmap_info.max_write_behind == 0)
149+
return;
150+
151+
if (!test_bit(WriteMostly, &rdev->flags) || !rdev_init_wb(rdev))
152+
return;
153+
154+
if (mddev->wb_info_pool == NULL) {
155+
unsigned int noio_flag;
156+
157+
if (!is_suspend)
158+
mddev_suspend(mddev);
159+
noio_flag = memalloc_noio_save();
160+
mddev->wb_info_pool = mempool_create_kmalloc_pool(NR_WB_INFOS,
161+
sizeof(struct wb_info));
162+
memalloc_noio_restore(noio_flag);
163+
if (!mddev->wb_info_pool)
164+
pr_err("can't alloc memory pool for writemostly\n");
165+
if (!is_suspend)
166+
mddev_resume(mddev);
167+
}
168+
}
169+
EXPORT_SYMBOL_GPL(mddev_create_wb_pool);
170+
171+
/*
172+
* destroy wb_info_pool if rdev is the last device flaged with WBCollisionCheck.
173+
*/
174+
static void mddev_destroy_wb_pool(struct mddev *mddev, struct md_rdev *rdev)
175+
{
176+
if (!test_and_clear_bit(WBCollisionCheck, &rdev->flags))
177+
return;
178+
179+
if (mddev->wb_info_pool) {
180+
struct md_rdev *temp;
181+
int num = 0;
182+
183+
/*
184+
* Check if other rdevs need wb_info_pool.
185+
*/
186+
rdev_for_each(temp, mddev)
187+
if (temp != rdev &&
188+
test_bit(WBCollisionCheck, &temp->flags))
189+
num++;
190+
if (!num) {
191+
mddev_suspend(rdev->mddev);
192+
mempool_destroy(mddev->wb_info_pool);
193+
mddev->wb_info_pool = NULL;
194+
mddev_resume(rdev->mddev);
195+
}
196+
}
197+
}
198+
140199
static struct ctl_table_header *raid_table_header;
141200

142201
static struct ctl_table raid_table[] = {
@@ -2223,6 +2282,9 @@ static int bind_rdev_to_array(struct md_rdev *rdev, struct mddev *mddev)
22232282
rdev->mddev = mddev;
22242283
pr_debug("md: bind<%s>\n", b);
22252284

2285+
if (mddev->raid_disks)
2286+
mddev_create_wb_pool(mddev, rdev, false);
2287+
22262288
if ((err = kobject_add(&rdev->kobj, &mddev->kobj, "dev-%s", b)))
22272289
goto fail;
22282290

@@ -2259,6 +2321,7 @@ static void unbind_rdev_from_array(struct md_rdev *rdev)
22592321
bd_unlink_disk_holder(rdev->bdev, rdev->mddev->gendisk);
22602322
list_del_rcu(&rdev->same_set);
22612323
pr_debug("md: unbind<%s>\n", bdevname(rdev->bdev,b));
2324+
mddev_destroy_wb_pool(rdev->mddev, rdev);
22622325
rdev->mddev = NULL;
22632326
sysfs_remove_link(&rdev->kobj, "block");
22642327
sysfs_put(rdev->sysfs_state);
@@ -2771,8 +2834,10 @@ state_store(struct md_rdev *rdev, const char *buf, size_t len)
27712834
}
27722835
} else if (cmd_match(buf, "writemostly")) {
27732836
set_bit(WriteMostly, &rdev->flags);
2837+
mddev_create_wb_pool(rdev->mddev, rdev, false);
27742838
err = 0;
27752839
} else if (cmd_match(buf, "-writemostly")) {
2840+
mddev_destroy_wb_pool(rdev->mddev, rdev);
27762841
clear_bit(WriteMostly, &rdev->flags);
27772842
err = 0;
27782843
} else if (cmd_match(buf, "blocked")) {

‎drivers/md/md.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,8 @@ extern struct bio *bio_alloc_mddev(gfp_t gfp_mask, int nr_iovecs,
730730
extern void md_reload_sb(struct mddev *mddev, int raid_disk);
731731
extern void md_update_sb(struct mddev *mddev, int force);
732732
extern void md_kick_rdev_from_array(struct md_rdev * rdev);
733+
extern void mddev_create_wb_pool(struct mddev *mddev, struct md_rdev *rdev,
734+
bool is_suspend);
733735
struct md_rdev *md_find_rdev_nr_rcu(struct mddev *mddev, int nr);
734736
struct md_rdev *md_find_rdev_rcu(struct mddev *mddev, dev_t dev);
735737

0 commit comments

Comments
 (0)