diff options
author | Stanislaw Gruszka <sgruszka@redhat.com> | 2012-03-02 04:43:28 -0500 |
---|---|---|
committer | Jens Axboe <axboe@kernel.dk> | 2012-03-02 04:44:17 -0500 |
commit | 9f53d2fe815b4011ff930a7b6db98385d45faa68 (patch) | |
tree | a46926f5cf0874102dcb81447894423994a09701 /block/genhd.c | |
parent | 12ebffd146768556ab7c415d0ff9ab78e3d16b7a (diff) |
block: fix __blkdev_get and add_disk race condition
The following situation might occur:
__blkdev_get: add_disk:
register_disk()
get_gendisk()
disk_block_events()
disk->ev == NULL
disk_add_events()
__disk_unblock_events()
disk->ev != NULL
--ev->block
Then we unblock events, when they are suppose to be blocked. This can
trigger events related block/genhd.c warnings, but also can crash in
sd_check_events() or other places.
I'm able to reproduce crashes with the following scripts (with
connected usb dongle as sdb disk).
<snip>
DEV=/dev/sdb
ENABLE=/sys/bus/usb/devices/1-2/bConfigurationValue
function stop_me()
{
for i in `jobs -p` ; do kill $i 2> /dev/null ; done
exit
}
trap stop_me SIGHUP SIGINT SIGTERM
for ((i = 0; i < 10; i++)) ; do
while true; do fdisk -l $DEV 2>&1 > /dev/null ; done &
done
while true ; do
echo 1 > $ENABLE
sleep 1
echo 0 > $ENABLE
done
</snip>
I use the script to verify patch fixing oops in sd_revalidate_disk
http://marc.info/?l=linux-scsi&m=132935572512352&w=2
Without Jun'ichi Nomura patch titled "Fix NULL pointer dereference in
sd_revalidate_disk" or this one, script easily crash kernel within
a few seconds. With both patches applied I do not observe crash.
Unfortunately after some time (dozen of minutes), script will hung in:
[ 1563.906432] [<c08354f5>] schedule_timeout_uninterruptible+0x15/0x20
[ 1563.906437] [<c04532d5>] msleep+0x15/0x20
[ 1563.906443] [<c05d60b2>] blk_drain_queue+0x32/0xd0
[ 1563.906447] [<c05d6e00>] blk_cleanup_queue+0xd0/0x170
[ 1563.906454] [<c06d278f>] scsi_free_queue+0x3f/0x60
[ 1563.906459] [<c06d7e6e>] __scsi_remove_device+0x6e/0xb0
[ 1563.906463] [<c06d4aff>] scsi_forget_host+0x4f/0x60
[ 1563.906468] [<c06cd84a>] scsi_remove_host+0x5a/0xf0
[ 1563.906482] [<f7f030fb>] quiesce_and_remove_host+0x5b/0xa0 [usb_storage]
[ 1563.906490] [<f7f03203>] usb_stor_disconnect+0x13/0x20 [usb_storage]
Anyway I think this patch is some step forward.
As drawback, I do not teardown on sysfs file create error, because I do
not know how to nullify disk->ev (since it can be used). However add_disk
error handling practically does not exist too, and things will work
without this sysfs file, except events will not be exported to user
space.
Signed-off-by: Stanislaw Gruszka <sgruszka@redhat.com>
Acked-by: Tejun Heo <tj@kernel.org>
Cc: stable@kernel.org
Signed-off-by: Jens Axboe <axboe@kernel.dk>
Diffstat (limited to 'block/genhd.c')
-rw-r--r-- | block/genhd.c | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/block/genhd.c b/block/genhd.c index 23b4f7063322..b26c4085590d 100644 --- a/block/genhd.c +++ b/block/genhd.c | |||
@@ -35,6 +35,7 @@ static DEFINE_IDR(ext_devt_idr); | |||
35 | 35 | ||
36 | static struct device_type disk_type; | 36 | static struct device_type disk_type; |
37 | 37 | ||
38 | static void disk_alloc_events(struct gendisk *disk); | ||
38 | static void disk_add_events(struct gendisk *disk); | 39 | static void disk_add_events(struct gendisk *disk); |
39 | static void disk_del_events(struct gendisk *disk); | 40 | static void disk_del_events(struct gendisk *disk); |
40 | static void disk_release_events(struct gendisk *disk); | 41 | static void disk_release_events(struct gendisk *disk); |
@@ -601,6 +602,8 @@ void add_disk(struct gendisk *disk) | |||
601 | disk->major = MAJOR(devt); | 602 | disk->major = MAJOR(devt); |
602 | disk->first_minor = MINOR(devt); | 603 | disk->first_minor = MINOR(devt); |
603 | 604 | ||
605 | disk_alloc_events(disk); | ||
606 | |||
604 | /* Register BDI before referencing it from bdev */ | 607 | /* Register BDI before referencing it from bdev */ |
605 | bdi = &disk->queue->backing_dev_info; | 608 | bdi = &disk->queue->backing_dev_info; |
606 | bdi_register_dev(bdi, disk_devt(disk)); | 609 | bdi_register_dev(bdi, disk_devt(disk)); |
@@ -1733,9 +1736,9 @@ module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops, | |||
1733 | &disk_events_dfl_poll_msecs, 0644); | 1736 | &disk_events_dfl_poll_msecs, 0644); |
1734 | 1737 | ||
1735 | /* | 1738 | /* |
1736 | * disk_{add|del|release}_events - initialize and destroy disk_events. | 1739 | * disk_{alloc|add|del|release}_events - initialize and destroy disk_events. |
1737 | */ | 1740 | */ |
1738 | static void disk_add_events(struct gendisk *disk) | 1741 | static void disk_alloc_events(struct gendisk *disk) |
1739 | { | 1742 | { |
1740 | struct disk_events *ev; | 1743 | struct disk_events *ev; |
1741 | 1744 | ||
@@ -1748,16 +1751,6 @@ static void disk_add_events(struct gendisk *disk) | |||
1748 | return; | 1751 | return; |
1749 | } | 1752 | } |
1750 | 1753 | ||
1751 | if (sysfs_create_files(&disk_to_dev(disk)->kobj, | ||
1752 | disk_events_attrs) < 0) { | ||
1753 | pr_warn("%s: failed to create sysfs files for events\n", | ||
1754 | disk->disk_name); | ||
1755 | kfree(ev); | ||
1756 | return; | ||
1757 | } | ||
1758 | |||
1759 | disk->ev = ev; | ||
1760 | |||
1761 | INIT_LIST_HEAD(&ev->node); | 1754 | INIT_LIST_HEAD(&ev->node); |
1762 | ev->disk = disk; | 1755 | ev->disk = disk; |
1763 | spin_lock_init(&ev->lock); | 1756 | spin_lock_init(&ev->lock); |
@@ -1766,8 +1759,21 @@ static void disk_add_events(struct gendisk *disk) | |||
1766 | ev->poll_msecs = -1; | 1759 | ev->poll_msecs = -1; |
1767 | INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn); | 1760 | INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn); |
1768 | 1761 | ||
1762 | disk->ev = ev; | ||
1763 | } | ||
1764 | |||
1765 | static void disk_add_events(struct gendisk *disk) | ||
1766 | { | ||
1767 | if (!disk->ev) | ||
1768 | return; | ||
1769 | |||
1770 | /* FIXME: error handling */ | ||
1771 | if (sysfs_create_files(&disk_to_dev(disk)->kobj, disk_events_attrs) < 0) | ||
1772 | pr_warn("%s: failed to create sysfs files for events\n", | ||
1773 | disk->disk_name); | ||
1774 | |||
1769 | mutex_lock(&disk_events_mutex); | 1775 | mutex_lock(&disk_events_mutex); |
1770 | list_add_tail(&ev->node, &disk_events); | 1776 | list_add_tail(&disk->ev->node, &disk_events); |
1771 | mutex_unlock(&disk_events_mutex); | 1777 | mutex_unlock(&disk_events_mutex); |
1772 | 1778 | ||
1773 | /* | 1779 | /* |