block: fix RO partition with RW disk
PATCH 1/1 block: fix RO partition with RW disk
When md raid1 was used with imsm metadata, during the boot stage, the raid device will first be set to readonly, then mdmon will set it read-write later. When there were some partitions in this device, the following race would make some partition left ro and fail to mount.
CPU 1: CPU 2:add_partition() set_disk_ro() //set disk RW
//disk was RO, so partition set to RO
p->policy = get_disk_ro(disk);
if (disk->part0.policy != flag) {
set_disk_ro_uevent(disk, flag);
// disk set to RW
disk->part0.policy = flag;
}
// set all exit partition to RW
while ((part = disk_part_iter_next(&piter)))
part->policy = flag; // this part was not yet added, so it was still RO rcu_assign_pointer(ptbl->part[partno], p);
Move RO status setting of partitions after they were added into partition table and introduce a mutex to sync RO status between disk and partitions.
--- block/genhd.c | 3 +++ block/partition-generic.c | 5 ++++- include/linux/genhd.h | 1 + 3 files changed, 8 insertions(+), 1 deletion(-)
block/genhd.c +3 -0
@@ -1479,6 +1479,7 @@ struct gendisk *__alloc_disk_node(int minors, int node_id) } ptbl =rcu_dereference_protected (disk->part_tbl,1 );rcu_assign_pointer (ptbl->part[0 ], &disk->part0); +mutex_init (&disk->part_lock);/* * set_capacity() and get_capacity() currently don't use
@@ -1570,6 +1571,7 @@ void set_disk_ro(struct gendisk *disk, int flag)struct disk_part_iter piter;struct hd_struct *part; +mutex_lock (&disk->part_lock);if (disk->part0.policy != flag) {set_disk_ro_uevent (disk, flag); disk->part0.policy = flag;
@@ -1579,6 +1581,7 @@ void set_disk_ro(struct gendisk *disk, int flag)while ((part =disk_part_iter_next (&piter))) part->policy = flag;disk_part_iter_exit (&piter); +mutex_unlock (&disk->part_lock); }EXPORT_SYMBOL (set_disk_ro);
block/partition-generic.c +4 -1
@@ -345,7 +345,6 @@ struct hd_struct *add_partition(struct gendisk *disk, int partno,queue_limit_discard_alignment (&disk->queue->limits, start); p->nr_sects = len; p->partno = partno; - p->policy =get_disk_ro (disk);if (info) {struct partition_meta_info *pinfo =alloc_part_info (disk);
@@ -401,6 +400,10 @@ struct hd_struct *add_partition(struct gendisk *disk, int partno,/* everything is up and running, commence */ rcu_assign_pointer (ptbl->part[partno], p); +mutex_lock (&disk->part_lock); + p->policy =get_disk_ro (disk); +mutex_unlock (&disk->part_lock); +/* suppress uevent if the disk suppresses it */ if (!dev_get_uevent_suppress (ddev))kobject_uevent (&pdev->kobj,KOBJ_ADD );
include/linux/genhd.h +1 -0
@@ -201,6 +201,7 @@ struct gendisk {*/ struct disk_part_tbl __rcu *part_tbl;struct hd_struct part0; +struct mutex part_lock;const struct block_device_operations *fops;struct request_queue *queue;
Signature · 1 line
2.17.1
Hi Junxiao,
While this is reported by md, is it possible to reproduce the error on purpose with other device (e.g., loop) and add a test to blktests?
Dongli Zhang
Quoted 94 lines · Junxiao Bi
When md raid1 was used with imsm metadata, during the boot stage, the raid device will first be set to readonly, then mdmon will set it read-write later. When there were some partitions in this device, the following race would make some partition left ro and fail to mount.
CPU 1: CPU 2:
add_partition()set_disk_ro()//set disk RW
//disk was RO, so partition set to ROp->policy = get_disk_ro(disk); if (disk->part0.policy != flag) { set_disk_ro_uevent(disk, flag); // disk set to RW disk->part0.policy = flag; } // set all exit partition to RW while ((part = disk_part_iter_next(&piter))) part->policy = flag;// this part was not yet added, so it was still RO
rcu_assign_pointer(ptbl->part[partno], p);Move RO status setting of partitions after they were added into partition table and introduce a mutex to sync RO status between disk and partitions.
Signed-off-by Junxiao Bi <[email protected]>--- block/genhd.c | 3 +++ block/partition-generic.c | 5 ++++- include/linux/genhd.h | 1 + 3 files changed, 8 insertions(+), 1 deletion(-)block/genhd.c +3
-0@@ -1479,6 +1479,7 @@ struct gendisk *__alloc_disk_node(int minors, int node_id) } ptbl =rcu_dereference_protected (disk->part_tbl,1 );rcu_assign_pointer (ptbl->part[0 ], &disk->part0); +mutex_init (&disk->part_lock);/* * set_capacity() and get_capacity() currently don't use @@ -1570,6 +1571,7 @@ void set_disk_ro(struct gendisk *disk, int flag)struct disk_part_iter piter;struct hd_struct *part; +mutex_lock (&disk->part_lock);if (disk->part0.policy != flag) {set_disk_ro_uevent (disk, flag); disk->part0.policy = flag;@@ -1579,6 +1581,7 @@ void set_disk_ro(struct gendisk *disk, int flag)while ((part =disk_part_iter_next (&piter))) part->policy = flag;disk_part_iter_exit (&piter); +mutex_unlock (&disk->part_lock); }EXPORT_SYMBOL (set_disk_ro);block/partition-generic.c +4
-1@@ -345,7 +345,6 @@ struct hd_struct *add_partition(struct gendisk *disk, int partno,queue_limit_discard_alignment (&disk->queue->limits, start); p->nr_sects = len; p->partno = partno; - p->policy =get_disk_ro (disk);if (info) {struct partition_meta_info *pinfo =alloc_part_info (disk);@@ -401,6 +400,10 @@ struct hd_struct *add_partition(struct gendisk *disk, int partno,/* everything is up and running, commence */ rcu_assign_pointer (ptbl->part[partno], p); +mutex_lock (&disk->part_lock); + p->policy =get_disk_ro (disk); +mutex_unlock (&disk->part_lock); +/* suppress uevent if the disk suppresses it */ if (!dev_get_uevent_suppress (ddev))kobject_uevent (&pdev->kobj,KOBJ_ADD );include/linux/genhd.h +1
-0@@ -201,6 +201,7 @@ struct gendisk {*/ struct disk_part_tbl __rcu *part_tbl;struct hd_struct part0; +struct mutex part_lock;const struct block_device_operations *fops;struct request_queue *queue;
Hi Dongli,
I think it's possible. A rough test case i can think is to create one loop device, one process issued BLKRRPART ioctl to re-read partition table(this will drop all partitions and recreate again) and another process set disk ro then rw. Let them race and check whether ro partition was left.
Thanks,
Junxiao.
Quoted 102 lines · Dongli Zhang
Hi Junxiao,
While this is reported by md, is it possible to reproduce the error on purpose with other device (e.g., loop) and add a test to blktests?
Dongli Zhang
Quoted 94 lines · Junxiao Bi
When md raid1 was used with imsm metadata, during the boot stage, the raid device will first be set to readonly, then mdmon will set it read-write later. When there were some partitions in this device, the following race would make some partition left ro and fail to mount.
CPU 1: CPU 2:
add_partition()set_disk_ro()//set disk RW
//disk was RO, so partition set to ROp->policy = get_disk_ro(disk); if (disk->part0.policy != flag) { set_disk_ro_uevent(disk, flag); // disk set to RW disk->part0.policy = flag; } // set all exit partition to RW while ((part = disk_part_iter_next(&piter))) part->policy = flag;// this part was not yet added, so it was still RO
rcu_assign_pointer(ptbl->part[partno], p);Move RO status setting of partitions after they were added into partition table and introduce a mutex to sync RO status between disk and partitions.
Signed-off-by Junxiao Bi <[email protected]>--- block/genhd.c | 3 +++ block/partition-generic.c | 5 ++++- include/linux/genhd.h | 1 + 3 files changed, 8 insertions(+), 1 deletion(-)block/genhd.c +3
-0@@ -1479,6 +1479,7 @@ struct gendisk *__alloc_disk_node(int minors, int node_id) } ptbl =rcu_dereference_protected (disk->part_tbl,1 );rcu_assign_pointer (ptbl->part[0 ], &disk->part0); +mutex_init (&disk->part_lock);/* * set_capacity() and get_capacity() currently don't use @@ -1570,6 +1571,7 @@ void set_disk_ro(struct gendisk *disk, int flag)struct disk_part_iter piter;struct hd_struct *part; +mutex_lock (&disk->part_lock);if (disk->part0.policy != flag) {set_disk_ro_uevent (disk, flag); disk->part0.policy = flag;@@ -1579,6 +1581,7 @@ void set_disk_ro(struct gendisk *disk, int flag)while ((part =disk_part_iter_next (&piter))) part->policy = flag;disk_part_iter_exit (&piter); +mutex_unlock (&disk->part_lock); }EXPORT_SYMBOL (set_disk_ro);block/partition-generic.c +4
-1@@ -345,7 +345,6 @@ struct hd_struct *add_partition(struct gendisk *disk, int partno,queue_limit_discard_alignment (&disk->queue->limits, start); p->nr_sects = len; p->partno = partno; - p->policy =get_disk_ro (disk);if (info) {struct partition_meta_info *pinfo =alloc_part_info (disk);@@ -401,6 +400,10 @@ struct hd_struct *add_partition(struct gendisk *disk, int partno,/* everything is up and running, commence */ rcu_assign_pointer (ptbl->part[partno], p); +mutex_lock (&disk->part_lock); + p->policy =get_disk_ro (disk); +mutex_unlock (&disk->part_lock); +/* suppress uevent if the disk suppresses it */ if (!dev_get_uevent_suppress (ddev))kobject_uevent (&pdev->kobj,KOBJ_ADD );include/linux/genhd.h +1
-0@@ -201,6 +201,7 @@ struct gendisk {*/ struct disk_part_tbl __rcu *part_tbl;struct hd_struct part0; +struct mutex part_lock;const struct block_device_operations *fops;struct request_queue *queue;
Anybody could help review this bug?
thanks,
Junxiao.
Quoted 93 lines · Junxiao Bi
When md raid1 was used with imsm metadata, during the boot stage, the raid device will first be set to readonly, then mdmon will set it read-write later. When there were some partitions in this device, the following race would make some partition left ro and fail to mount.
CPU 1: CPU 2:
add_partition()set_disk_ro()//set disk RW
//disk was RO, so partition set to ROp->policy = get_disk_ro(disk); if (disk->part0.policy != flag) { set_disk_ro_uevent(disk, flag); // disk set to RW disk->part0.policy = flag; } // set all exit partition to RW while ((part = disk_part_iter_next(&piter))) part->policy = flag;// this part was not yet added, so it was still RO
rcu_assign_pointer(ptbl->part[partno], p);Move RO status setting of partitions after they were added into partition table and introduce a mutex to sync RO status between disk and partitions.
Signed-off-by Junxiao Bi <[email protected]>--- block/genhd.c | 3 +++ block/partition-generic.c | 5 ++++- include/linux/genhd.h | 1 + 3 files changed, 8 insertions(+), 1 deletion(-)block/genhd.c +3
-0@@ -1479,6 +1479,7 @@ struct gendisk *__alloc_disk_node(int minors, int node_id) } ptbl =rcu_dereference_protected (disk->part_tbl,1 );rcu_assign_pointer (ptbl->part[0 ], &disk->part0); +mutex_init (&disk->part_lock);/* * set_capacity() and get_capacity() currently don't use @@ -1570,6 +1571,7 @@ void set_disk_ro(struct gendisk *disk, int flag)struct disk_part_iter piter;struct hd_struct *part; +mutex_lock (&disk->part_lock);if (disk->part0.policy != flag) {set_disk_ro_uevent (disk, flag); disk->part0.policy = flag;@@ -1579,6 +1581,7 @@ void set_disk_ro(struct gendisk *disk, int flag)while ((part =disk_part_iter_next (&piter))) part->policy = flag;disk_part_iter_exit (&piter); +mutex_unlock (&disk->part_lock); }EXPORT_SYMBOL (set_disk_ro);block/partition-generic.c +4
-1@@ -345,7 +345,6 @@ struct hd_struct *add_partition(struct gendisk *disk, int partno,queue_limit_discard_alignment (&disk->queue->limits, start); p->nr_sects = len; p->partno = partno; - p->policy =get_disk_ro (disk);if (info) {struct partition_meta_info *pinfo =alloc_part_info (disk);@@ -401,6 +400,10 @@ struct hd_struct *add_partition(struct gendisk *disk, int partno,/* everything is up and running, commence */ rcu_assign_pointer (ptbl->part[partno], p); +mutex_lock (&disk->part_lock); + p->policy =get_disk_ro (disk); +mutex_unlock (&disk->part_lock); +/* suppress uevent if the disk suppresses it */ if (!dev_get_uevent_suppress (ddev))kobject_uevent (&pdev->kobj,KOBJ_ADD );include/linux/genhd.h +1
-0@@ -201,6 +201,7 @@ struct gendisk {*/ struct disk_part_tbl __rcu *part_tbl;struct hd_struct part0; +struct mutex part_lock;const struct block_device_operations *fops;struct request_queue *queue;
Junxiao,
Quoted 1 line
Anybody could help review this bug?
It's on my list. However, your patch is clashing with my general read-only handling changes so I'll probably need to roll your changes into mine.
I'll try to look at this today.
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering