Skip to content

Commit e52a293

Browse files
ecashintorvalds
authored andcommitted
aoe: avoid races between device destruction and discovery
This change avoids a race that could result in a NULL pointer derference following a WARNing from kobject_add_internal, "don't try to register things with the same name in the same directory." The problem was found with a test that forgets and discovers an aoe device in a loop: while test ! -r /tmp/stop; do aoe-flush -a aoe-discover done The race was between aoedev_flush taking aoedevs out of the devlist, allowing a new discovery of the same AoE target to take place before the driver gets around to calling sysfs_remove_group. Fixing that one revealed another race between do_open and add_disk, and this patch avoids that, too. The fix required some care, because for flushing (forgetting) an aoedev, some of the steps must be performed under lock and some must be able to sleep. Also, for discovering a new aoedev, some steps might sleep. The check for a bad aoedev pointer remains from a time when about half of this patch was done, and it was possible for the bdev->bd_disk->private_data to become corrupted. The check should be removed eventually, but it is not expected to add significant overhead, occurring in the aoeblk_open routine. Signed-off-by: Ed Cashin <ecashin@coraid.com> Cc: Jens Axboe <axboe@kernel.dk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent bbb44e3 commit e52a293

3 files changed

Lines changed: 146 additions & 63 deletions

File tree

‎drivers/block/aoe/aoe.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ enum {
7474
DEVFL_TKILL = (1<<1), /* flag for timer to know when to kill self */
7575
DEVFL_EXT = (1<<2), /* device accepts lba48 commands */
7676
DEVFL_GDALLOC = (1<<3), /* need to alloc gendisk */
77-
DEVFL_KICKME = (1<<4), /* slow polling network card catch */
78-
DEVFL_NEWSIZE = (1<<5), /* need to update dev size in block layer */
77+
DEVFL_GD_NOW = (1<<4), /* allocating gendisk */
78+
DEVFL_KICKME = (1<<5), /* slow polling network card catch */
79+
DEVFL_NEWSIZE = (1<<6), /* need to update dev size in block layer */
80+
DEVFL_FREEING = (1<<7), /* set when device is being cleaned up */
81+
DEVFL_FREED = (1<<8), /* device has been cleaned up */
7982
};
8083

8184
enum {

‎drivers/block/aoe/aoeblk.c‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,18 @@ aoeblk_open(struct block_device *bdev, fmode_t mode)
147147
struct aoedev *d = bdev->bd_disk->private_data;
148148
ulong flags;
149149

150+
if (!virt_addr_valid(d)) {
151+
pr_crit("aoe: invalid device pointer in %s\n",
152+
__func__);
153+
WARN_ON(1);
154+
return -ENODEV;
155+
}
156+
if (!(d->flags & DEVFL_UP) || d->flags & DEVFL_TKILL)
157+
return -ENODEV;
158+
150159
mutex_lock(&aoeblk_mutex);
151160
spin_lock_irqsave(&d->lock, flags);
152-
if (d->flags & DEVFL_UP) {
161+
if (d->flags & DEVFL_UP && !(d->flags & DEVFL_TKILL)) {
153162
d->nopen++;
154163
spin_unlock_irqrestore(&d->lock, flags);
155164
mutex_unlock(&aoeblk_mutex);
@@ -259,6 +268,18 @@ aoeblk_gdalloc(void *vp)
259268
struct request_queue *q;
260269
enum { KB = 1024, MB = KB * KB, READ_AHEAD = 2 * MB, };
261270
ulong flags;
271+
int late = 0;
272+
273+
spin_lock_irqsave(&d->lock, flags);
274+
if (d->flags & DEVFL_GDALLOC
275+
&& !(d->flags & DEVFL_TKILL)
276+
&& !(d->flags & DEVFL_GD_NOW))
277+
d->flags |= DEVFL_GD_NOW;
278+
else
279+
late = 1;
280+
spin_unlock_irqrestore(&d->lock, flags);
281+
if (late)
282+
return;
262283

263284
gd = alloc_disk(AOE_PARTITIONS);
264285
if (gd == NULL) {
@@ -282,6 +303,11 @@ aoeblk_gdalloc(void *vp)
282303
}
283304

284305
spin_lock_irqsave(&d->lock, flags);
306+
WARN_ON(!(d->flags & DEVFL_GD_NOW));
307+
WARN_ON(!(d->flags & DEVFL_GDALLOC));
308+
WARN_ON(d->flags & DEVFL_TKILL);
309+
WARN_ON(d->gd);
310+
WARN_ON(d->flags & DEVFL_UP);
285311
blk_queue_max_hw_sectors(q, BLK_DEF_MAX_SECTORS);
286312
q->backing_dev_info.name = "aoe";
287313
q->backing_dev_info.ra_pages = READ_AHEAD / PAGE_CACHE_SIZE;
@@ -306,6 +332,11 @@ aoeblk_gdalloc(void *vp)
306332

307333
add_disk(gd);
308334
aoedisk_add_sysfs(d);
335+
336+
spin_lock_irqsave(&d->lock, flags);
337+
WARN_ON(!(d->flags & DEVFL_GD_NOW));
338+
d->flags &= ~DEVFL_GD_NOW;
339+
spin_unlock_irqrestore(&d->lock, flags);
309340
return;
310341

311342
err_mempool:
@@ -314,7 +345,8 @@ aoeblk_gdalloc(void *vp)
314345
put_disk(gd);
315346
err:
316347
spin_lock_irqsave(&d->lock, flags);
317-
d->flags &= ~DEVFL_GDALLOC;
348+
d->flags &= ~DEVFL_GD_NOW;
349+
schedule_work(&d->work);
318350
spin_unlock_irqrestore(&d->lock, flags);
319351
}
320352

‎drivers/block/aoe/aoedev.c‎

Lines changed: 107 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "aoe.h"
1616

1717
static void dummy_timer(ulong);
18-
static void aoedev_freedev(struct aoedev *);
1918
static void freetgt(struct aoedev *d, struct aoetgt *t);
2019
static void skbpoolfree(struct aoedev *d);
2120

@@ -236,29 +235,6 @@ aoedev_downdev(struct aoedev *d)
236235
set_capacity(d->gd, 0);
237236
}
238237

239-
static void
240-
aoedev_freedev(struct aoedev *d)
241-
{
242-
struct aoetgt **t, **e;
243-
244-
cancel_work_sync(&d->work);
245-
if (d->gd) {
246-
aoedisk_rm_sysfs(d);
247-
del_gendisk(d->gd);
248-
put_disk(d->gd);
249-
blk_cleanup_queue(d->blkq);
250-
}
251-
t = d->targets;
252-
e = t + NTARGETS;
253-
for (; t < e && *t; t++)
254-
freetgt(d, *t);
255-
if (d->bufpool)
256-
mempool_destroy(d->bufpool);
257-
skbpoolfree(d);
258-
minor_free(d->sysminor);
259-
kfree(d);
260-
}
261-
262238
/* return whether the user asked for this particular
263239
* device to be flushed
264240
*/
@@ -283,17 +259,62 @@ user_req(char *s, size_t slen, struct aoedev *d)
283259
return !strncmp(s, p, lim);
284260
}
285261

286-
int
287-
aoedev_flush(const char __user *str, size_t cnt)
262+
static void
263+
freedev(struct aoedev *d)
264+
{
265+
struct aoetgt **t, **e;
266+
int freeing = 0;
267+
unsigned long flags;
268+
269+
spin_lock_irqsave(&d->lock, flags);
270+
if (d->flags & DEVFL_TKILL
271+
&& !(d->flags & DEVFL_FREEING)) {
272+
d->flags |= DEVFL_FREEING;
273+
freeing = 1;
274+
}
275+
spin_unlock_irqrestore(&d->lock, flags);
276+
if (!freeing)
277+
return;
278+
279+
del_timer_sync(&d->timer);
280+
if (d->gd) {
281+
aoedisk_rm_sysfs(d);
282+
del_gendisk(d->gd);
283+
put_disk(d->gd);
284+
blk_cleanup_queue(d->blkq);
285+
}
286+
t = d->targets;
287+
e = t + NTARGETS;
288+
for (; t < e && *t; t++)
289+
freetgt(d, *t);
290+
if (d->bufpool)
291+
mempool_destroy(d->bufpool);
292+
skbpoolfree(d);
293+
minor_free(d->sysminor);
294+
295+
spin_lock_irqsave(&d->lock, flags);
296+
d->flags |= DEVFL_FREED;
297+
spin_unlock_irqrestore(&d->lock, flags);
298+
}
299+
300+
enum flush_parms {
301+
NOT_EXITING = 0,
302+
EXITING = 1,
303+
};
304+
305+
static int
306+
flush(const char __user *str, size_t cnt, int exiting)
288307
{
289308
ulong flags;
290309
struct aoedev *d, **dd;
291-
struct aoedev *rmd = NULL;
292310
char buf[16];
293311
int all = 0;
294312
int specified = 0; /* flush a specific device */
313+
unsigned int skipflags;
314+
315+
skipflags = DEVFL_GDALLOC | DEVFL_NEWSIZE | DEVFL_TKILL;
295316

296-
if (cnt >= 3) {
317+
if (!exiting && cnt >= 3) {
297318
if (cnt > sizeof buf)
298319
cnt = sizeof buf;
299320
if (copy_from_user(buf, str, cnt))
@@ -303,39 +324,71 @@ aoedev_flush(const char __user *str, size_t cnt)
303324
specified = 1;
304325
}
305326

327+
flush_scheduled_work();
328+
/* pass one: without sleeping, do aoedev_downdev */
306329
spin_lock_irqsave(&devlist_lock, flags);
307-
dd = &devlist;
308-
while ((d = *dd)) {
330+
for (d = devlist; d; d = d->next) {
309331
spin_lock(&d->lock);
310-
if (specified) {
332+
if (exiting) {
333+
/* unconditionally take each device down */
334+
} else if (specified) {
311335
if (!user_req(buf, cnt, d))
312-
goto skip;
336+
goto cont;
313337
} else if ((!all && (d->flags & DEVFL_UP))
314-
|| (d->flags & (DEVFL_GDALLOC|DEVFL_NEWSIZE))
338+
|| d->flags & skipflags
315339
|| d->nopen
316340
|| d->ref)
317-
goto skip;
341+
goto cont;
318342

319-
*dd = d->next;
320343
aoedev_downdev(d);
321344
d->flags |= DEVFL_TKILL;
345+
cont:
322346
spin_unlock(&d->lock);
323-
d->next = rmd;
324-
rmd = d;
325-
continue;
326-
skip:
327-
spin_unlock(&d->lock);
328-
dd = &d->next;
329347
}
330348
spin_unlock_irqrestore(&devlist_lock, flags);
331-
while ((d = rmd)) {
332-
rmd = d->next;
333-
del_timer_sync(&d->timer);
334-
aoedev_freedev(d); /* must be able to sleep */
349+
350+
/* pass two: call freedev, which might sleep,
351+
* for aoedevs marked with DEVFL_TKILL
352+
*/
353+
restart:
354+
spin_lock_irqsave(&devlist_lock, flags);
355+
for (d = devlist; d; d = d->next) {
356+
spin_lock(&d->lock);
357+
if (d->flags & DEVFL_TKILL
358+
&& !(d->flags & DEVFL_FREEING)) {
359+
spin_unlock(&d->lock);
360+
spin_unlock_irqrestore(&devlist_lock, flags);
361+
freedev(d);
362+
goto restart;
363+
}
364+
spin_unlock(&d->lock);
335365
}
366+
367+
/* pass three: remove aoedevs marked with DEVFL_FREED */
368+
for (dd = &devlist, d = *dd; d; d = *dd) {
369+
struct aoedev *doomed = NULL;
370+
371+
spin_lock(&d->lock);
372+
if (d->flags & DEVFL_FREED) {
373+
*dd = d->next;
374+
doomed = d;
375+
} else {
376+
dd = &d->next;
377+
}
378+
spin_unlock(&d->lock);
379+
kfree(doomed);
380+
}
381+
spin_unlock_irqrestore(&devlist_lock, flags);
382+
336383
return 0;
337384
}
338385

386+
int
387+
aoedev_flush(const char __user *str, size_t cnt)
388+
{
389+
return flush(str, cnt, NOT_EXITING);
390+
}
391+
339392
/* This has been confirmed to occur once with Tms=3*1000 due to the
340393
* driver changing link and not processing its transmit ring. The
341394
* problem is hard enough to solve by returning an error that I'm
@@ -388,7 +441,14 @@ aoedev_by_aoeaddr(ulong maj, int min, int do_alloc)
388441

389442
for (d=devlist; d; d=d->next)
390443
if (d->aoemajor == maj && d->aoeminor == min) {
444+
spin_lock(&d->lock);
445+
if (d->flags & DEVFL_TKILL) {
446+
spin_unlock(&d->lock);
447+
d = NULL;
448+
goto out;
449+
}
391450
d->ref++;
451+
spin_unlock(&d->lock);
392452
break;
393453
}
394454
if (d || !do_alloc || minor_get(&sysminor, maj, min) < 0)
@@ -448,21 +508,9 @@ freetgt(struct aoedev *d, struct aoetgt *t)
448508
void
449509
aoedev_exit(void)
450510
{
451-
struct aoedev *d;
452-
ulong flags;
453-
511+
flush_scheduled_work();
454512
aoe_flush_iocq();
455-
while ((d = devlist)) {
456-
devlist = d->next;
457-
458-
spin_lock_irqsave(&d->lock, flags);
459-
aoedev_downdev(d);
460-
d->flags |= DEVFL_TKILL;
461-
spin_unlock_irqrestore(&d->lock, flags);
462-
463-
del_timer_sync(&d->timer);
464-
aoedev_freedev(d);
465-
}
513+
flush(NULL, 0, EXITING);
466514
}
467515

468516
int __init

0 commit comments

Comments
 (0)