Skip to content

Commit 1d4c8ba

Browse files
YuKuai-huaweigregkh
authored andcommitted
null_blk: fix null-ptr-dereference while configuring 'power' and 'submit_queues'
commit a2db328b0839312c169eb42746ec46fc1ab53ed2 upstream. Writing 'power' and 'submit_queues' concurrently will trigger kernel panic: Test script: modprobe null_blk nr_devices=0 mkdir -p /sys/kernel/config/nullb/nullb0 while true; do echo 1 > submit_queues; echo 4 > submit_queues; done & while true; do echo 1 > power; echo 0 > power; done Test result: BUG: kernel NULL pointer dereference, address: 0000000000000148 Oops: 0000 [#1] PREEMPT SMP RIP: 0010:__lock_acquire+0x41d/0x28f0 Call Trace: <TASK> lock_acquire+0x121/0x450 down_write+0x5f/0x1d0 simple_recursive_removal+0x12f/0x5c0 blk_mq_debugfs_unregister_hctxs+0x7c/0x100 blk_mq_update_nr_hw_queues+0x4a3/0x720 nullb_update_nr_hw_queues+0x71/0xf0 [null_blk] nullb_device_submit_queues_store+0x79/0xf0 [null_blk] configfs_write_iter+0x119/0x1e0 vfs_write+0x326/0x730 ksys_write+0x74/0x150 This is because del_gendisk() can concurrent with blk_mq_update_nr_hw_queues(): nullb_device_power_store nullb_apply_submit_queues null_del_dev del_gendisk nullb_update_nr_hw_queues if (!dev->nullb) // still set while gendisk is deleted return 0 blk_mq_update_nr_hw_queues dev->nullb = NULL Fix this problem by resuing the global mutex to protect nullb_device_power_store() and nullb_update_nr_hw_queues() from configfs. Fixes: 45919fb ("null_blk: Enable modifying 'submit_queues' after an instance has been configured") Reported-and-tested-by: Yi Zhang <[email protected]> Closes: https://lore.kernel.org/all/CAHj4cs9LgsHLnjg8z06LQ3Pr5cax-+Ps+xT7AP7TPnEjStuwZA@mail.gmail.com/ Signed-off-by: Yu Kuai <[email protected]> Reviewed-by: Zhu Yanjun <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Xiangyu Chen <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent af40404 commit 1d4c8ba

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

drivers/block/null_blk/main.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -392,13 +392,25 @@ static int nullb_update_nr_hw_queues(struct nullb_device *dev,
392392
static int nullb_apply_submit_queues(struct nullb_device *dev,
393393
unsigned int submit_queues)
394394
{
395-
return nullb_update_nr_hw_queues(dev, submit_queues, dev->poll_queues);
395+
int ret;
396+
397+
mutex_lock(&lock);
398+
ret = nullb_update_nr_hw_queues(dev, submit_queues, dev->poll_queues);
399+
mutex_unlock(&lock);
400+
401+
return ret;
396402
}
397403

398404
static int nullb_apply_poll_queues(struct nullb_device *dev,
399405
unsigned int poll_queues)
400406
{
401-
return nullb_update_nr_hw_queues(dev, dev->submit_queues, poll_queues);
407+
int ret;
408+
409+
mutex_lock(&lock);
410+
ret = nullb_update_nr_hw_queues(dev, dev->submit_queues, poll_queues);
411+
mutex_unlock(&lock);
412+
413+
return ret;
402414
}
403415

404416
NULLB_DEVICE_ATTR(size, ulong, NULL);
@@ -444,28 +456,31 @@ static ssize_t nullb_device_power_store(struct config_item *item,
444456
if (ret < 0)
445457
return ret;
446458

459+
ret = count;
460+
mutex_lock(&lock);
447461
if (!dev->power && newp) {
448462
if (test_and_set_bit(NULLB_DEV_FL_UP, &dev->flags))
449-
return count;
463+
goto out;
464+
450465
ret = null_add_dev(dev);
451466
if (ret) {
452467
clear_bit(NULLB_DEV_FL_UP, &dev->flags);
453-
return ret;
468+
goto out;
454469
}
455470

456471
set_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
457472
dev->power = newp;
458473
} else if (dev->power && !newp) {
459474
if (test_and_clear_bit(NULLB_DEV_FL_UP, &dev->flags)) {
460-
mutex_lock(&lock);
461475
dev->power = newp;
462476
null_del_dev(dev->nullb);
463-
mutex_unlock(&lock);
464477
}
465478
clear_bit(NULLB_DEV_FL_CONFIGURED, &dev->flags);
466479
}
467480

468-
return count;
481+
out:
482+
mutex_unlock(&lock);
483+
return ret;
469484
}
470485

471486
CONFIGFS_ATTR(nullb_device_, power);
@@ -2102,15 +2117,12 @@ static int null_add_dev(struct nullb_device *dev)
21022117
blk_queue_flag_set(QUEUE_FLAG_NONROT, nullb->q);
21032118
blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, nullb->q);
21042119

2105-
mutex_lock(&lock);
21062120
rv = ida_alloc(&nullb_indexes, GFP_KERNEL);
2107-
if (rv < 0) {
2108-
mutex_unlock(&lock);
2121+
if (rv < 0)
21092122
goto out_cleanup_zone;
2110-
}
2123+
21112124
nullb->index = rv;
21122125
dev->index = rv;
2113-
mutex_unlock(&lock);
21142126

21152127
blk_queue_logical_block_size(nullb->q, dev->blocksize);
21162128
blk_queue_physical_block_size(nullb->q, dev->blocksize);
@@ -2134,9 +2146,7 @@ static int null_add_dev(struct nullb_device *dev)
21342146
if (rv)
21352147
goto out_ida_free;
21362148

2137-
mutex_lock(&lock);
21382149
list_add_tail(&nullb->list, &nullb_list);
2139-
mutex_unlock(&lock);
21402150

21412151
pr_info("disk %s created\n", nullb->disk_name);
21422152

@@ -2185,7 +2195,9 @@ static int null_create_dev(void)
21852195
if (!dev)
21862196
return -ENOMEM;
21872197

2198+
mutex_lock(&lock);
21882199
ret = null_add_dev(dev);
2200+
mutex_unlock(&lock);
21892201
if (ret) {
21902202
null_free_dev(dev);
21912203
return ret;

0 commit comments

Comments
 (0)