aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorcolyli@suse.de <colyli@suse.de>2017-01-28 08:11:49 -0500
committerShaohua Li <shli@fb.com>2017-02-13 12:17:50 -0500
commit03a9e24ef2aaa5f1f9837356aed79c860521407a (patch)
treeb825397bb7a395ee0a134deb78d104e1a0d641eb
parent7089db84e356562f8ba737c29e472cc42d530dbc (diff)
md linear: fix a race between linear_add() and linear_congested()
Recently I receive a bug report that on Linux v3.0 based kerenl, hot add disk to a md linear device causes kernel crash at linear_congested(). From the crash image analysis, I find in linear_congested(), mddev->raid_disks contains value N, but conf->disks[] only has N-1 pointers available. Then a NULL pointer deference crashes the kernel. There is a race between linear_add() and linear_congested(), RCU stuffs used in these two functions cannot avoid the race. Since Linuv v4.0 RCU code is replaced by introducing mddev_suspend(). After checking the upstream code, it seems linear_congested() is not called in generic_make_request() code patch, so mddev_suspend() cannot provent it from being called. The possible race still exists. Here I explain how the race still exists in current code. For a machine has many CPUs, on one CPU, linear_add() is called to add a hard disk to a md linear device; at the same time on other CPU, linear_congested() is called to detect whether this md linear device is congested before issuing an I/O request onto it. Now I use a possible code execution time sequence to demo how the possible race happens, seq linear_add() linear_congested() 0 conf=mddev->private 1 oldconf=mddev->private 2 mddev->raid_disks++ 3 for (i=0; i<mddev->raid_disks;i++) 4 bdev_get_queue(conf->disks[i].rdev->bdev) 5 mddev->private=newconf In linear_add() mddev->raid_disks is increased in time seq 2, and on another CPU in linear_congested() the for-loop iterates conf->disks[i] by the increased mddev->raid_disks in time seq 3,4. But conf with one more element (which is a pointer to struct dev_info type) to conf->disks[] is not updated yet, accessing its structure member in time seq 4 will cause a NULL pointer deference fault. To fix this race, there are 2 parts of modification in the patch, 1) Add 'int raid_disks' in struct linear_conf, as a copy of mddev->raid_disks. It is initialized in linear_conf(), always being consistent with pointers number of 'struct dev_info disks[]'. When iterating conf->disks[] in linear_congested(), use conf->raid_disks to replace mddev->raid_disks in the for-loop, then NULL pointer deference will not happen again. 2) RCU stuffs are back again, and use kfree_rcu() in linear_add() to free oldconf memory. Because oldconf may be referenced as mddev->private in linear_congested(), kfree_rcu() makes sure that its memory will not be released until no one uses it any more. Also some code comments are added in this patch, to make this modification to be easier understandable. This patch can be applied for kernels since v4.0 after commit: 3be260cc18f8 ("md/linear: remove rcu protections in favour of suspend/resume"). But this bug is reported on Linux v3.0 based kernel, for people who maintain kernels before Linux v4.0, they need to do some back back port to this patch. Changelog: - V3: add 'int raid_disks' in struct linear_conf, and use kfree_rcu() to replace rcu_call() in linear_add(). - v2: add RCU stuffs by suggestion from Shaohua and Neil. - v1: initial effort. Signed-off-by: Coly Li <colyli@suse.de> Cc: Shaohua Li <shli@fb.com> Cc: Neil Brown <neilb@suse.com> Cc: stable@vger.kernel.org Signed-off-by: Shaohua Li <shli@fb.com>
-rw-r--r--drivers/md/linear.c39
-rw-r--r--drivers/md/linear.h1
2 files changed, 35 insertions, 5 deletions
diff --git a/drivers/md/linear.c b/drivers/md/linear.c
index 5975c9915684..26a73b2002cf 100644
--- a/drivers/md/linear.c
+++ b/drivers/md/linear.c
@@ -53,18 +53,26 @@ static inline struct dev_info *which_dev(struct mddev *mddev, sector_t sector)
53 return conf->disks + lo; 53 return conf->disks + lo;
54} 54}
55 55
56/*
57 * In linear_congested() conf->raid_disks is used as a copy of
58 * mddev->raid_disks to iterate conf->disks[], because conf->raid_disks
59 * and conf->disks[] are created in linear_conf(), they are always
60 * consitent with each other, but mddev->raid_disks does not.
61 */
56static int linear_congested(struct mddev *mddev, int bits) 62static int linear_congested(struct mddev *mddev, int bits)
57{ 63{
58 struct linear_conf *conf; 64 struct linear_conf *conf;
59 int i, ret = 0; 65 int i, ret = 0;
60 66
61 conf = mddev->private; 67 rcu_read_lock();
68 conf = rcu_dereference(mddev->private);
62 69
63 for (i = 0; i < mddev->raid_disks && !ret ; i++) { 70 for (i = 0; i < conf->raid_disks && !ret ; i++) {
64 struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev); 71 struct request_queue *q = bdev_get_queue(conf->disks[i].rdev->bdev);
65 ret |= bdi_congested(&q->backing_dev_info, bits); 72 ret |= bdi_congested(&q->backing_dev_info, bits);
66 } 73 }
67 74
75 rcu_read_unlock();
68 return ret; 76 return ret;
69} 77}
70 78
@@ -144,6 +152,19 @@ static struct linear_conf *linear_conf(struct mddev *mddev, int raid_disks)
144 conf->disks[i-1].end_sector + 152 conf->disks[i-1].end_sector +
145 conf->disks[i].rdev->sectors; 153 conf->disks[i].rdev->sectors;
146 154
155 /*
156 * conf->raid_disks is copy of mddev->raid_disks. The reason to
157 * keep a copy of mddev->raid_disks in struct linear_conf is,
158 * mddev->raid_disks may not be consistent with pointers number of
159 * conf->disks[] when it is updated in linear_add() and used to
160 * iterate old conf->disks[] earray in linear_congested().
161 * Here conf->raid_disks is always consitent with number of
162 * pointers in conf->disks[] array, and mddev->private is updated
163 * with rcu_assign_pointer() in linear_addr(), such race can be
164 * avoided.
165 */
166 conf->raid_disks = raid_disks;
167
147 return conf; 168 return conf;
148 169
149out: 170out:
@@ -196,15 +217,23 @@ static int linear_add(struct mddev *mddev, struct md_rdev *rdev)
196 if (!newconf) 217 if (!newconf)
197 return -ENOMEM; 218 return -ENOMEM;
198 219
220 /* newconf->raid_disks already keeps a copy of * the increased
221 * value of mddev->raid_disks, WARN_ONCE() is just used to make
222 * sure of this. It is possible that oldconf is still referenced
223 * in linear_congested(), therefore kfree_rcu() is used to free
224 * oldconf until no one uses it anymore.
225 */
199 mddev_suspend(mddev); 226 mddev_suspend(mddev);
200 oldconf = mddev->private; 227 oldconf = rcu_dereference(mddev->private);
201 mddev->raid_disks++; 228 mddev->raid_disks++;
202 mddev->private = newconf; 229 WARN_ONCE(mddev->raid_disks != newconf->raid_disks,
230 "copied raid_disks doesn't match mddev->raid_disks");
231 rcu_assign_pointer(mddev->private, newconf);
203 md_set_array_sectors(mddev, linear_size(mddev, 0, 0)); 232 md_set_array_sectors(mddev, linear_size(mddev, 0, 0));
204 set_capacity(mddev->gendisk, mddev->array_sectors); 233 set_capacity(mddev->gendisk, mddev->array_sectors);
205 mddev_resume(mddev); 234 mddev_resume(mddev);
206 revalidate_disk(mddev->gendisk); 235 revalidate_disk(mddev->gendisk);
207 kfree(oldconf); 236 kfree_rcu(oldconf, rcu);
208 return 0; 237 return 0;
209} 238}
210 239
diff --git a/drivers/md/linear.h b/drivers/md/linear.h
index b685ddd7d7f7..8d392e6098b3 100644
--- a/drivers/md/linear.h
+++ b/drivers/md/linear.h
@@ -10,6 +10,7 @@ struct linear_conf
10{ 10{
11 struct rcu_head rcu; 11 struct rcu_head rcu;
12 sector_t array_sectors; 12 sector_t array_sectors;
13 int raid_disks; /* a copy of mddev->raid_disks */
13 struct dev_info disks[0]; 14 struct dev_info disks[0];
14}; 15};
15#endif 16#endif