blktrace: Fix potentail deadlock between delete & sysfs ops
Subsystems (3)
PATCH v2 1/1 blktrace: Fix potentail deadlock between delete & sysfs ops
The lockdep code had reported the following unsafe locking scenario:
CPU0 CPU1
---- ----
lock(s_active#228);
lock(&bdev->bd_mutex/1);
lock(s_active#228); lock(&bdev->bd_mutex);
*** DEADLOCK ***
The deadlock may happen when one task (CPU1) is trying to delete a partition in a block device and another task (CPU0) is accessing tracing sysfs file in that partition.
To avoid that, accessing tracing sysfs file will now use a mutex trylock loop and the operation will fail if a delete operation is in progress.
---
v2:
- Use READ_ONCE() and smp_store_mb() to read and write bd_deleting.
- Check for signal in the mutex_trylock loops.
- Use usleep() instead of schedule() for RT tasks.
block/ioctl.c | 3 +++ include/linux/fs.h | 1 + kernel/trace/blktrace.c | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 2 deletions(-)
block/ioctl.c +3 -0
@@ -86,12 +86,15 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __userreturn -EBUSY ; }/* all seems OK */ +smp_store_mb (bdev->bd_deleting,1 );fsync_bdev (bdevp);invalidate_bdev (bdevp);mutex_lock_nested (&bdev->bd_mutex,1 );delete_partition (disk, partno);mutex_unlock (&bdev->bd_mutex); +smp_store_mb (bdev->bd_deleting,0 ); +mutex_unlock (&bdevp->bd_mutex);bdput (bdevp);
include/linux/fs.h +1 -0
@@ -427,6 +427,7 @@ struct block_device {#endif struct block_device * bd_contains;unsigned bd_block_size; +int bd_deleting;struct hd_struct * bd_part;/* number of times partitions within this device have been opened. */ unsigned bd_part_count;
kernel/trace/blktrace.c +37 -2
@@ -27,6 +27,8 @@#include <linux/time.h> #include <linux/uaccess.h> #include <linux/list.h> +#include <linux/delay.h> +#include <linux/sched/rt.h> #include "../../block/blk.h"
@@ -1605,6 +1607,23 @@ static struct request_queue *blk_trace_get_queue(struct block_device *bdev)return bdev_get_queue (bdev); } +/* +* Read/write to the tracing sysfs file requires taking references to the +* sysfs file and then acquiring the bd_mutex. Deleting a block device +* requires acquiring the bd_mutex and then waiting for all the sysfs +* references to be gone. This can lead to deadlock if both operations +* happen simultaneously. To avoid this problem, read/write to the +* the tracing sysfs files can now fail if the bd_mutex cannot be +* acquired while a deletion operation is in progress. +* +* A mutex trylock loop is used assuming that tracing sysfs operations +* aren't frequently enough to cause any contention problem. +* +* For RT tasks, a running high priority task will prevent any lower +* priority RT tasks from being run. So they do need to actually sleep +* when the trylock fails to allow lower priority tasks to make forward +* progress. +*/ static ssize_t sysfs_blk_trace_attr_show (struct device *dev,struct device_attribute *attr,char *buf)
@@ -1622,7 +1641,15 @@ static ssize_t sysfs_blk_trace_attr_show(struct device *dev,if (q ==NULL ) goto out_bdput; -mutex_lock (&bdev->bd_mutex); +while (!mutex_trylock (&bdev->bd_mutex)) { +if (READ_ONCE (bdev->bd_deleting)) + goto out_bdput; +if (signal_pending (current)) { + ret = -EINTR ; + goto out_bdput; + } +rt_task (current) ?usleep_range (10 ,10 ) :schedule (); + }if (attr == &dev_attr_enable) { ret =sprintf (buf,"%u\n" , !!q->blk_trace);
@@ -1683,7 +1710,15 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,if (q ==NULL ) goto out_bdput; -mutex_lock (&bdev->bd_mutex); +while (!mutex_trylock (&bdev->bd_mutex)) { +if (READ_ONCE (bdev->bd_deleting)) + goto out_bdput; +if (signal_pending (current)) { + ret = -EINTR ; + goto out_bdput; + } +rt_task (current) ?usleep_range (10 ,10 ) :schedule (); + }if (attr == &dev_attr_enable) {if (value)
Signature · 1 line
1.8.3.1
Steven Rostedt
maintainerQuoted 8 lines · On Wed, 16 Aug 2017 16:40:40 -0400 Waiman Long <[email protected]> wrote:
The lockdep code had reported the following unsafe locking scenario:
CPU0 CPU1 ---- ---- lock(s_active#228); lock(&bdev->bd_mutex/1); lock(s_active#228);
lock(&bdev->bd_mutex);
Can you show the exact locations of these locks. I have no idea where this "s_active" is.
Quoted 11 lines
*** DEADLOCK ***
The deadlock may happen when one task (CPU1) is trying to delete a partition in a block device and another task (CPU0) is accessing tracing sysfs file in that partition.
To avoid that, accessing tracing sysfs file will now use a mutex trylock loop and the operation will fail if a delete operation is in progress.
Quoted 7 lines
Signed-off-by Waiman Long <[email protected]>---
v2:
- UseREAD_ONCE()andsmp_store_mb()to read and writebd_deleting.
- Check for signal in themutex_trylockloops.
- Useusleep()instead ofschedule()for RT tasks.
I'm sorry but I really do hate this patch.
Quoted 11 lines
block/ioctl.c | 3 +++ include/linux/fs.h | 1 + kernel/trace/blktrace.c | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 2 deletions(-)block/ioctl.c +0
-0
block/ioctl.c static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user · line 89
Quoted 4 lines
block/ioctl.c +1
-0@@ -86,12 +86,15 @@ static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __userreturn -EBUSY ; }/* all seems OK */ +smp_store_mb (bdev->bd_deleting,1 );
No comment to explain what is happening here, and why.
block/ioctl.c static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user · line 97
Quoted 8 lines
block/ioctl.c +2
-0fsync_bdev (bdevp);invalidate_bdev (bdevp);mutex_lock_nested (&bdev->bd_mutex,1 );delete_partition (disk, partno);mutex_unlock (&bdev->bd_mutex); +smp_store_mb (bdev->bd_deleting,0 ); +
ditto.
Quoted 29 lines
block/ioctl.c +0
-0mutex_unlock (&bdevp->bd_mutex);bdput (bdevp);include/linux/fs.h +1
-0@@ -427,6 +427,7 @@ struct block_device {#endif struct block_device * bd_contains;unsigned bd_block_size; +int bd_deleting;struct hd_struct * bd_part;/* number of times partitions within this device have been opened. */ unsigned bd_part_count;kernel/trace/blktrace.c +2
-0@@ -27,6 +27,8 @@#include <linux/time.h> #include <linux/uaccess.h> #include <linux/list.h> +#include <linux/delay.h> +#include <linux/sched/rt.h> #include "../../block/blk.h"
kernel/trace/blktrace.c static struct request_queue *blk_trace_get_queue(struct block_device *bdev) · line 1611
Quoted 5 lines
kernel/trace/blktrace.c +2
-0@@ -1605,6 +1607,23 @@ static struct request_queue *blk_trace_get_queue(struct block_device *bdev)return bdev_get_queue (bdev); } +/* +* Read/write to the tracing sysfs file requires taking references to the
What's the "tracing sysfs" file? tracefs?
kernel/trace/blktrace.c static struct request_queue *blk_trace_get_queue(struct block_device *bdev) · line 1619
Quoted 8 lines
kernel/trace/blktrace.c +8
-0+* sysfs file and then acquiring the bd_mutex. Deleting a block device +* requires acquiring the bd_mutex and then waiting for all the sysfs +* references to be gone. This can lead to deadlock if both operations +* happen simultaneously. To avoid this problem, read/write to the +* the tracing sysfs files can now fail if the bd_mutex cannot be +* acquired while a deletion operation is in progress. +* +* A mutex trylock loop is used assuming that tracing sysfs operations
A mutex trylock loop is not enough to stop a deadlock. But I'm guessing the undocumented bd_deleting may prevent that.
Quoted 13 lines
kernel/trace/blktrace.c +7
-0+* aren't frequently enough to cause any contention problem. +* +* For RT tasks, a running high priority task will prevent any lower +* priority RT tasks from being run. So they do need to actually sleep +* when the trylock fails to allow lower priority tasks to make forward +* progress. +*/ static ssize_t sysfs_blk_trace_attr_show (struct device *dev,struct device_attribute *attr,char *buf)@@ -1622,7 +1641,15 @@ static ssize_t sysfs_blk_trace_attr_show(struct device *dev,if (q ==NULL ) goto out_bdput;
kernel/trace/blktrace.c static ssize_t sysfs_blk_trace_attr_show(struct device *dev, · line 1651
Quoted 10 lines
kernel/trace/blktrace.c +8
-1-mutex_lock (&bdev->bd_mutex); +while (!mutex_trylock (&bdev->bd_mutex)) { +if (READ_ONCE (bdev->bd_deleting)) + goto out_bdput; +if (signal_pending (current)) { + ret = -EINTR ; + goto out_bdput; + } +rt_task (current) ?usleep_range (10 ,10 ) :schedule ();
We need to come up with a better solution. This is just a hack that circumvents a lot of the lockdep infrastructure.
-- Steve
Quoted 21 lines
kernel/trace/blktrace.c +10
-1+ }if (attr == &dev_attr_enable) { ret =sprintf (buf,"%u\n" , !!q->blk_trace);@@ -1683,7 +1710,15 @@ static ssize_t sysfs_blk_trace_attr_store(struct device *dev,if (q ==NULL ) goto out_bdput; -mutex_lock (&bdev->bd_mutex); +while (!mutex_trylock (&bdev->bd_mutex)) { +if (READ_ONCE (bdev->bd_deleting)) + goto out_bdput; +if (signal_pending (current)) { + ret = -EINTR ; + goto out_bdput; + } +rt_task (current) ?usleep_range (10 ,10 ) :schedule (); + }if (attr == &dev_attr_enable) {if (value)
Quoted 5 lines · Steven Rostedt
Quoted 2 lines · On Wed, 16 Aug 2017 16:40:40 -0400 Waiman Long <[email protected]> wrote:
The lockdep code had reported the following unsafe locking scenario:
Quoted 8 lines
Quoted 6 lines
CPU0 CPU1 ---- ---- lock(s_active#228); lock(&bdev->bd_mutex/1); lock(s_active#228);
lock(&bdev->bd_mutex);Can you show the exact locations of these locks. I have no idea where this "
s_active" is.
The s_active isn't an actual lock. It is a reference count (kn->count) on the sysfs (kernfs) file. Removal of a sysfs file, however, require a wait until all the references are gone. The reference count is treated like a rwsem using lockdep instrumentation code.
Quoted 13 lines
Quoted 13 lines
*** DEADLOCK ***
The deadlock may happen when one task (CPU1) is trying to delete a partition in a block device and another task (CPU0) is accessing tracing sysfs file in that partition.
To avoid that, accessing tracing sysfs file will now use a mutex trylock loop and the operation will fail if a delete operation is in progress.
Signed-off-by Waiman Long <[email protected]>---
Quoted 6 lines
Quoted 5 lines
v2:
- UseREAD_ONCE()andsmp_store_mb()to read and write bd_deleting.=- Check for signal in the
mutex_trylockloops.
- Useusleep()instead ofschedule()for RT tasks.I'm sorry but I really do hate this patch.
Any suggestion on how to make it better?
Quoted 10 lines
block/ioctl.c static int blkpg_ioctl(struct block_device *bdev, =
Quoted 10 lines
block/ioctl.c | 3 +++ include/linux/fs.h | 1 + kernel/trace/blktrace.c | 39 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 41 insertions(+), 2 deletions(-)block/ioctl.c +0
-0@@ -86,12 +86,15 @@ static int blkpg_ioctl(struct block_device *bdev, =
struct blkpg_ioctl_arg __user
Quoted 5 lines
block/ioctl.c static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user · line 89
Quoted 4 lines
block/ioctl.c +1
-0return -EBUSY ; }/* all seems OK */ +smp_store_mb (bdev->bd_deleting,1 );No comment to explain what is happening here, and why.
I am going to add a comment block here.
Quoted 10 lines
block/ioctl.c static int blkpg_ioctl(struct block_device *bdev, struct blkpg_ioctl_arg __user · line 97
Quoted 8 lines
block/ioctl.c +0
-0fsync_bdev (bdevp);invalidate_bdev (bdevp);=20
block/ioctl.c +2
-0mutex_lock_nested (&bdev->bd_mutex,1 );delete_partition (disk, partno);mutex_unlock (&bdev->bd_mutex); +smp_store_mb (bdev->bd_deleting,0 ); +ditto.
Quoted 14 lines
Quoted 8 lines
block/ioctl.c +0
-0mutex_unlock (&bdevp->bd_mutex);bdput (bdevp);=20
include/linux/fs.h +0
-0include/linux/fs.h struct block_device { · line 432
Quoted 6 lines
include/linux/fs.h +1
-0@@ -427,6 +427,7 @@ struct block_device {#endif struct block_device * bd_contains;unsigned bd_block_size; +int bd_deleting;struct hd_struct * bd_part;/* number of times partitions within this device have been opened. *=
/
Quoted 7 lines
Quoted 7 lines
include/linux/fs.h +0
-0unsigned bd_part_count;kernel/trace/blktrace.c +0
-0@@ -27,6 +27,8 @@#include <linux/time.h>
Quoted 8 lines
kernel/trace/blktrace.c static struct request_queue *blk_trace_get_queu=
Quoted 8 lines
kernel/trace/blktrace.c +2
-0#include <linux/uaccess.h> #include <linux/list.h> +#include <linux/delay.h> +#include <linux/sched/rt.h> =20
#include "../../block/blk.h"
=20kernel/trace/blktrace.c +0
-0@@ -1605,6 +1607,23 @@ static struct request_queue *blk_trace_get_queu=
e(struct block_device *bdev)
Quoted 5 lines
Quoted 5 lines
kernel/trace/blktrace.c +0
-0return bdev_get_queue (bdev); }=20
+/*
+ * Read/write to the tracing sysfs file requires taking references to=
the
Quoted 1 line
What's the "tracing sysfs" file? tracefs?
I am referring to blk_trace sysfs files which are used for enable tracing of block operations.
Quoted 1 line
Quoted 1 line
+ * sysfs file and then acquiring the
bd_mutex. Deleting a block devic=
e
Quoted 3 lines
Quoted 3 lines
+ * requires acquiring the
bd_mutexand then waiting for all the sysfs=+ * references to be gone. This can lead to deadlock if both operation=
s
Quoted 5 lines
Quoted 5 lines
+ * happen simultaneously. To avoid this problem, read/write to the
+ * the tracing sysfs files can now fail if thebd_mutexcannot be
+ * acquired while a deletion operation is in progress.
+ *
+ * A mutex trylock loop is used assuming that tracing sysfs operation=
s
Quoted 3 lines
A mutex trylock loop is not enough to stop a deadlock. But I'm guessing=
the undocumented
bd_deletingmay prevent that.
Yes, that is what the bd_deleting flag is for.
I was thinking about failing the sysfs operation after a certain number of trylock attempts, but then it will require changes to user space code to handle the occasional failures. Finally I decided to fail it only when a delete operation is in progress as all hopes are lost in this case= =2E
Quoted 4 lines
Quoted 4 lines
+ * aren't frequently enough to cause any contention problem.
+ *
+ * For RT tasks, a running high priority task will prevent any lower
+ * priority RT tasks from being run. So they do need to actually slee=
p
Quoted 1 line
Quoted 1 line
+ * when the trylock fails to allow lower priority tasks to make forwa=
rd
Quoted 6 lines
kernel/trace/blktrace.c static ssize_t sysfs_blk_trace_attr_show(struct=
Quoted 6 lines
+ * progress.
+ */
staticssize_tsysfs_blk_trace_attr_show(struct device *dev,kernel/trace/blktrace.c +0
-0struct device_attribute *attr, char *buf)@@ -1622,7 +1641,15 @@ static ssize_t sysfs_blk_trace_attr_show(struct=
device *dev,
Quoted 15 lines
kernel/trace/blktrace.c static ssize_t sysfs_blk_trace_attr_show(struct device *dev, · line 1651
Quoted 12 lines
kernel/trace/blktrace.c +0
-0if (q =3D=3D NULL) goto out_bdput;=20
kernel/trace/blktrace.c +8
-1-mutex_lock (&bdev->bd_mutex); +while (!mutex_trylock (&bdev->bd_mutex)) { +if (READ_ONCE (bdev->bd_deleting)) + goto out_bdput; +if (signal_pending (current)) { + ret =3D -EINTR ; + goto out_bdput; + } +rt_task (current) ?usleep_range (10 ,10 ) :schedule ();We need to come up with a better solution. This is just a hack that circumvents a lot of the lockdep infrastructure.
Quoted 1 line
-- Steve
The root cause is the lock inversion under this circumstance. I think modifying the blk_trace code has the least impact overall. I agree that the code is ugly. If you have a better suggestion, I will certainly like to hear it.
Cheers,
Longman
Steven Rostedt
maintainerQuoted 12 lines · On Thu, 17 Aug 2017 12:24:39 -0400 Waiman Long <[email protected]> wrote:
Quoted 11 lines · Steven Rostedt
Quoted 8 lines · On Wed, 16 Aug 2017 16:40:40 -0400 Waiman Long <[email protected]> wrote:
The lockdep code had reported the following unsafe locking scenario:
CPU0 CPU1 ---- ---- lock(s_active#228); lock(&bdev->bd_mutex/1); lock(s_active#228);
lock(&bdev->bd_mutex);
Quoted 6 lines
Quoted 2 lines
Can you show the exact locations of these locks. I have no idea where this "
s_active" is.The
s_activeisn't an actual lock. It is a reference count (kn->count) on the sysfs (kernfs) file. Removal of a sysfs file, however, require a wait until all the references are gone. The reference count is treated like a rwsem using lockdep instrumentation code.
Which kernel is this? I don't see any lockdep annotation around kn->count (doing a git grep, I find it referenced in fs/kernfs/dir.c)
Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
*** DEADLOCK ***
The deadlock may happen when one task (CPU1) is trying to delete a partition in a block device and another task (CPU0) is accessing tracing sysfs file in that partition.
To avoid that, accessing tracing sysfs file will now use a mutex trylock loop and the operation will fail if a delete operation is in progress.
Signed-off-by Waiman Long <[email protected]>---
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
v2:
- UseREAD_ONCE()andsmp_store_mb()to read and writebd_deleting.
- Check for signal in themutex_trylockloops.
- Useusleep()instead ofschedule()for RT tasks.I'm sorry but I really do hate this patch.
Any suggestion on how to make it better?
I'd like to be able to at least trigger the warning. And see the lock issues. I wont be able to recommend anything until I understand what is happening.
Quoted 4 lines
The root cause is the lock inversion under this circumstance. I think modifying the
blk_tracecode has the least impact overall. I agree that the code is ugly. If you have a better suggestion, I will certainly like to hear it.
Again, I need to see where the issue lies before recommending something else. I would hope there is a more elegant solution to this.
-- Steve
T24gVGh1LCAyMDE3LTA4LTE3IGF0IDE2OjMwIC0wNDAwLCBTdGV2ZW4gUm9zdGVkdCB3cm90ZToN Cj4gT24gVGh1LCAxNyBBdWcgMjAxNyAxMjoyNDozOSAtMDQwMA0KPiBXYWltYW4gTG9uZyA8bG9u Z21hbkByZWRoYXQuY29tPiB3cm90ZToNCj4gDQo+ID4gT24gMDgvMTcvMjAxNyAwOTozNCBBTSwg U3RldmVuIFJvc3RlZHQgd3JvdGU6DQo+ID4gPiBPbiBXZWQsIDE2IEF1ZyAyMDE3IDE2OjQwOjQw IC0wNDAwDQo+ID4gPiBXYWltYW4gTG9uZyA8bG9uZ21hbkByZWRoYXQuY29tPiB3cm90ZToNCj4g PiA+ICANCj4gPiA+ID4gVGhlIGxvY2tkZXAgY29kZSBoYWQgcmVwb3J0ZWQgdGhlIGZvbGxvd2lu ZyB1bnNhZmUgbG9ja2luZyBzY2VuYXJpbzoNCj4gPiA+ID4gDQo+ID4gPiA+ICAgICAgICBDUFUw ICAgICAgICAgICAgICAgICAgICBDUFUxDQo+ID4gPiA+ICAgICAgICAtLS0tICAgICAgICAgICAg ICAgICAgICAtLS0tDQo+ID4gPiA+ICAgbG9jayhzX2FjdGl2ZSMyMjgpOw0KPiA+ID4gPiAgICAg ICAgICAgICAgICAgICAgICAgICAgICAgICAgbG9jaygmYmRldi0+YmRfbXV0ZXgvMSk7DQo+ID4g PiA+ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBsb2NrKHNfYWN0aXZlIzIyOCk7DQo+ ID4gPiA+ICAgbG9jaygmYmRldi0+YmRfbXV0ZXgpOyAgDQo+ID4gPiANCj4gPiA+IENhbiB5b3Ug c2hvdyB0aGUgZXhhY3QgbG9jYXRpb25zIG9mIHRoZXNlIGxvY2tzLiBJIGhhdmUgbm8gaWRlYSB3 aGVyZQ0KPiA+ID4gdGhpcyAic19hY3RpdmUiIGlzLiAgDQo+ID4gDQo+ID4gVGh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GVhc2UoJmtuLT5kZXBfbWFwLCAxLCBf UkVUX0lQXyk7DQpmcy9rZXJuZnMvZGlyLmM6NDY4OgkJcndzZW1fYWNxdWlyZSgma24tPmRlcF9t YXAsIDAsIDAsIF9SRVRfSVBfKTsNCmZzL2tlcm5mcy9kaXIuYzo0NzA6CQkJbG9ja19jb250ZW5k ZWQoJmtuLT5kZXBfbWFwLCBfUkVUX0lQXyk7DQpmcy9rZXJuZnMvZGlyLmM6NDc4OgkJbG9ja19h Y3F1aXJlZCgma24tPmRlcF9tYXAsIF9SRVRfSVBfKTsNCmZzL2tlcm5mcy9kaXIuYzo0Nzk6CQly d3NlbV9yZWxlYXNlKCZrbi0+ZGVwX21hcCwgMSwgX1JFVF9JUF8pOw0KZnMva2VybmZzL2Rpci5j OjEzODU6CQlyd3NlbV9hY3F1aXJlKCZrbi0+ZGVwX21hcCwgMCwgMSwgX1JFVF9JUF8pOw0KZnMv a2VybmZzL2ZpbGUuYzoxMDAwOgkJbG9ja2RlcF9pbml0X21hcCgma24tPmRlcF9tYXAsICJzX2Fj dGl2ZSIsIGtleSwgMCk7DQoNCkJhcnQu
Steven Rostedt
maintainerQuoted 31 lines · On Thu, 17 Aug 2017 20:41:10 +0000 Bart Van Assche <[email protected]>…
Quoted 19 lines · Steven Rostedt
Quoted 16 lines · On Thu, 17 Aug 2017 12:24:39 -0400 Waiman Long <[email protected]> wrote:
Quoted 14 lines · Steven Rostedt
Quoted 8 lines · On Wed, 16 Aug 2017 16:40:40 -0400 Waiman Long <[email protected]> wrote:
The lockdep code had reported the following unsafe locking scenario:
CPU0 CPU1 ---- ---- lock(s_active#228); lock(&bdev->bd_mutex/1); lock(s_active#228);
lock(&bdev->bd_mutex);Can you show the exact locations of these locks. I have no idea where this "
s_active" is.Quoted 7 lines
Quoted 4 lines
The
s_activeisn't an actual lock. It is a reference count (kn->count) on the sysfs (kernfs) file. Removal of a sysfs file, however, require a wait until all the references are gone. The reference count is treated like a rwsem using lockdep instrumentation code.Which kernel is this? I don't see any lockdep annotation around
kn->count(doing a git grep, I find it referenced infs/kernfs/dir.c)As far as I know the
s_activelockdep annotations were introduced in 2007 through commit0ab66088c855("sysfs: implementsysfs_direntactive reference and immediate disconnect"). Today these annotations exist in kernfs:
Quoted 10 lines
$ git grep -nHw dep_map fs/kernfs fs/kernfs/dir.c:421: rwsem_acquire_read(&kn->dep_map, 0, 1, _RET_IP_); fs/kernfs/dir.c:441: rwsem_release(&kn->dep_map, 1, _RET_IP_); fs/kernfs/dir.c:468: rwsem_acquire(&kn->dep_map, 0, 0, _RET_IP_); fs/kernfs/dir.c:470: lock_contended(&kn->dep_map, _RET_IP_); fs/kernfs/dir.c:478: lock_acquired(&kn->dep_map, _RET_IP_); fs/kernfs/dir.c:479: rwsem_release(&kn->dep_map, 1, _RET_IP_); fs/kernfs/dir.c:1385: rwsem_acquire(&kn->dep_map, 0, 1, _RET_IP_); fs/kernfs/file.c:1000: lockdep_init_map(&kn->dep_map, "s_active", key, 0);
Ah thanks. I was searching for the wrong thing. :-/
-- Steve
Steven Rostedt
maintainerQuoted 11 lines · On Thu, 17 Aug 2017 12:24:39 -0400 Waiman Long <[email protected]> wrote:
Quoted 10 lines
Quoted 8 lines
+ * sysfs file and then acquiring the
bd_mutex. Deleting a block device
+ * requires acquiring thebd_mutexand then waiting for all the sysfs
+ * references to be gone. This can lead to deadlock if both operations
+ * happen simultaneously. To avoid this problem, read/write to the
+ * the tracing sysfs files can now fail if thebd_mutexcannot be
+ * acquired while a deletion operation is in progress.
+ *
+ * A mutex trylock loop is used assuming that tracing sysfs operationsA mutex trylock loop is not enough to stop a deadlock. But I'm guessing the undocumented
bd_deletingmay prevent that.
Quoted 6 lines
Yes, that is what the
bd_deletingflag is for.I was thinking about failing the sysfs operation after a certain number of trylock attempts, but then it will require changes to user space code to handle the occasional failures. Finally I decided to fail it only when a delete operation is in progress as all hopes are lost in this case.
Actually, why not just fail the attr read on deletion? If it is being deleted, and one is reading the attribute, perhaps -ENODEV is the proper return value?
Quoted 5 lines
The root cause is the lock inversion under this circumstance. I think modifying the
blk_tracecode has the least impact overall. I agree that the code is ugly. If you have a better suggestion, I will certainly like to hear it.
Instead of playing games with taking the lock, the only way this race is hit, is if the partition is being deleted and the sysfs attribute is being read at the same time, correct? In that case, just return -ENODEV, and be done with it.
-- Steve
Quoted 4 lines · Steven Rostedt
Quoted 1 line · On Thu, 17 Aug 2017 12:24:39 -0400 Waiman Long <[email protected]> wrote:
Quoted 1 line
Quoted 1 line
+ * sysfs file and then acquiring the
bd_mutex. Deleting a block dev=
ice
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
+ * requires acquiring the
bd_mutexand then waiting for all the sys=
fs
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
+ * references to be gone. This can lead to deadlock if both operati=
ons
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
+ * happen simultaneously. To avoid this problem, read/write to the
+ * the tracing sysfs files can now fail if thebd_mutexcannot be
+ * acquired while a deletion operation is in progress.
+ *
+ * A mutex trylock loop is used assuming that tracing sysfs operati=
ons =20
Quoted 1 line
Quoted 1 line
Quoted 1 line
A mutex trylock loop is not enough to stop a deadlock. But I'm guessi=
ng
Quoted 4 lines
Quoted 4 lines
Quoted 1 line
the undocumented
bd_deletingmay prevent that. =20Yes, that is what the
bd_deletingflag is for.I was thinking about failing the sysfs operation after a certain numbe=
r
Quoted 1 line
Quoted 1 line
of trylock attempts, but then it will require changes to user space co=
de
Quoted 2 lines
Quoted 2 lines
to handle the occasional failures. Finally I decided to fail it only when a delete operation is in progress as all hopes are lost in this c=
ase.
Quoted 6 lines
Actually, why not just fail the attr read on deletion? If it is being deleted, and one is reading the attribute, perhaps
-ENODEVis the proper return value?Quoted 2 lines
The root cause is the lock inversion under this circumstance. I think modifying the
blk_tracecode has the least impact overall. I agree tha=
t
Quoted 1 line
Quoted 1 line
the code is ugly. If you have a better suggestion, I will certainly li=
ke
Quoted 8 lines
Quoted 1 line
to hear it.
Instead of playing games with taking the lock, the only way this race is hit, is if the partition is being deleted and the sysfs attribute is=
being read at the same time, correct? In that case, just return
-ENODEV, and be done with it.-- Steve
It is actually what the patch is trying to do by checking for the deletion flag in the mutex_trylock loop. Please note that mutex does not guarantee FIFO ordering of lock acquisition. As a result, cpu1 may call mutex_lock() and wait for it while cpu2 can set the deletion flag later and get the mutex first before cpu1. So checking for the deletion flag before taking the mutex isn't enough.
Cheers,
Longman
Steven Rostedt
maintainerQuoted 6 lines · On Thu, 17 Aug 2017 17:27:22 -0400 Waiman Long <[email protected]> wrote:
It is actually what the patch is trying to do by checking for the deletion flag in the
mutex_trylockloop. Please note that mutex does not guarantee FIFO ordering of lock acquisition. As a result, cpu1 may callmutex_lock()and wait for it while cpu2 can set the deletion flag later and get the mutex first before cpu1. So checking for the deletion flag before taking the mutex isn't enough.
Yeah, I figured that out already (crossed emails). BTW, how did you trigger this warning. I'm playing around with adding loop devices, volume groups, and logical volumes, and reading the trace files created in the sysfs directory, then removing those items, but it's not triggering the "delete" path. What operation deletes the partition?
Thanks
-- Steve
Steven Rostedt
maintainerQuoted 11 lines · On Thu, 17 Aug 2017 18:13:20 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Thu, 17 Aug 2017 17:27:22 -0400 Waiman Long <[email protected]> wrote:
It is actually what the patch is trying to do by checking for the deletion flag in the
mutex_trylockloop. Please note that mutex does not guarantee FIFO ordering of lock acquisition. As a result, cpu1 may callmutex_lock()and wait for it while cpu2 can set the deletion flag later and get the mutex first before cpu1. So checking for the deletion flag before taking the mutex isn't enough.
Quoted 5 lines
Yeah, I figured that out already (crossed emails). BTW, how did you trigger this warning. I'm playing around with adding loop devices, volume groups, and logical volumes, and reading the trace files created in the sysfs directory, then removing those items, but it's not triggering the "delete" path. What operation deletes the partition?
I'm guessing that deleting an actual partition may work (unfortunately, my test box has no partition to delete ;-) I'll find another box to test on.
-- Steve
Steven Rostedt
maintainerQuoted 20 lines · On Thu, 17 Aug 2017 18:18:18 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 11 lines · On Thu, 17 Aug 2017 18:13:20 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Thu, 17 Aug 2017 17:27:22 -0400 Waiman Long <[email protected]> wrote:
It is actually what the patch is trying to do by checking for the deletion flag in the
mutex_trylockloop. Please note that mutex does not guarantee FIFO ordering of lock acquisition. As a result, cpu1 may callmutex_lock()and wait for it while cpu2 can set the deletion flag later and get the mutex first before cpu1. So checking for the deletion flag before taking the mutex isn't enough.Quoted 5 lines
Yeah, I figured that out already (crossed emails). BTW, how did you trigger this warning. I'm playing around with adding loop devices, volume groups, and logical volumes, and reading the trace files created in the sysfs directory, then removing those items, but it's not triggering the "delete" path. What operation deletes the partition?
Quoted 4 lines
I'm guessing that deleting an actual partition may work (unfortunately, my test box has no partition to delete ;-) I'll find another box to test on.
OK, deleting a partition doesn't trigger the lockdep splat. But I also added a printk in the BLKPG_DEL_PARTITION case switch, which also doesn't print. What command do I need to do to trigger that path?
Thanks,
-- Steve
Quoted 25 lines · Steven Rostedt
Quoted 13 lines · On Thu, 17 Aug 2017 18:18:18 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 10 lines · On Thu, 17 Aug 2017 18:13:20 -0400 Steven Rostedt <[email protected]> wrote:
Quoted 6 lines · On Thu, 17 Aug 2017 17:27:22 -0400 Waiman Long <[email protected]> wrote:
It is actually what the patch is trying to do by checking for the deletion flag in the
mutex_trylockloop. Please note that mutex does not guarantee FIFO ordering of lock acquisition. As a result, cpu1 may callmutex_lock()and wait for it while cpu2 can set the deletion flag later and get the mutex first before cpu1. So checking for the deletion flag before taking the mutex isn't enough.Quoted 9 lines
Quoted 5 lines
Yeah, I figured that out already (crossed emails). BTW, how did you trigger this warning. I'm playing around with adding loop devices, volume groups, and logical volumes, and reading the trace files created in the sysfs directory, then removing those items, but it's not triggering the "delete" path. What operation deletes the partition?
I'm guessing that deleting an actual partition may work (unfortunately, my test box has no partition to delete ;-) I'll find another box to test on.
Quoted 7 lines
OK, deleting a partition doesn't trigger the lockdep splat. But I also added a printk in the
BLKPG_DEL_PARTITIONcase switch, which also doesn't print. What command do I need to do to trigger that path?Thanks,
-- Steve
Attached is a reproducer that was used to trigger the warning. Some tuning may be needed depend on the actual configuration of the test machine.
Cheers,
Longman
Steven Rostedt
maintainerQuoted 4 lines · On Thu, 17 Aug 2017 17:10:07 -0400 Steven Rostedt <[email protected]> wrote:
Instead of playing games with taking the lock, the only way this race is hit, is if the partition is being deleted and the sysfs attribute is being read at the same time, correct? In that case, just return
-ENODEV, and be done with it.
Nevermind that wont work. Too bad there's not a mutex_lock_timeout() that we could use in a loop. It would solve the issue of forward progress with RT tasks, and will break after a timeout in case of deadlock.
-- Steve
Quoted 6 lines · Steven Rostedt
Quoted 2 lines · On Thu, 17 Aug 2017 17:10:07 -0400 Steven Rostedt <[email protected]> wrote:
Instead of playing games with taking the lock, the only way this race is hit, is if the partition is being deleted and the sysfs attribute i=
s
Quoted 8 lines
Quoted 2 lines
being read at the same time, correct? In that case, just return
-ENODEV, and be done with it.Nevermind that wont work. Too bad there's not a
mutex_lock_timeout()that we could use in a loop. It would solve the issue of forward progress with RT tasks, and will break after a timeout in case of deadlock.-- Steve
I think it will be useful to have mutex_timed_lock(). RT-mutex does have a timed version, so I guess it shouldn't be hard to implement one for mutex. I can take a shot at trying to do that.
Thanks,
Longman
T24gRnJpLCAyMDE3LTA4LTE4IGF0IDA5OjU1IC0wNDAwLCBXYWltYW4gTG9uZyB3cm90ZToNCj4g T24gMDgvMTcvMjAxNyAwNTozMCBQTSwgU3RldmVuIFJvc3RlZHQgd3JvdGU6DQo+ID4gT24gVGh1 LCAxNyBBdWcgMjAxNyAxNzoxMDowNyAtMDQwMA0KPiA+IFN0ZXZlbiBSb3N0ZWR0IDxyb3N0ZWR0 QGdvb2RtaXMub3JnPiB3cm90ZToNCj4gPiA+IEluc3RlYWQgb2YgcGxheWluZyBnYW1lcyB3aXRo IHRha2luZy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20gaW5zaWRlIGEgYmxvY2sg ZGV2aWNlIHN5c2ZzIGNhbGxiYWNrDQpmdW5jdGlvbiBpcyBub3Qgc2FmZSBidXQgb2J0YWluaW5n IHRoZSBwZXItYmxvY2stZGV2aWNlIG11dGV4IGZyb20gaW5zaWRlIGEgc3lzZnMNCmNhbGxiYWNr IGZ1bmN0aW9uIGlzIHNhZmUuDQoNCkJhcnQu
Quoted 5 lines · Bart Van Assche
Quoted 4 lines · Waiman Long
Quoted 3 lines · Steven Rostedt
Quoted 1 line · On Thu, 17 Aug 2017 17:10:07 -0400 Steven Rostedt <[email protected]> wrote:
Instead of playing games with taking the lock, the only way this rac=
e
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
is hit, is if the partition is being deleted and the sysfs attribute=
is
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 2 lines
being read at the same time, correct? In that case, just return
-ENODEV, and be done with it.Nevermind that wont work. Too bad there's not a
mutex_lock_timeout()that we could use in a loop. It would solve the issue of forward progress with RT tasks, and will break after a timeout in case of deadlock.I think it will be useful to have
mutex_timed_lock(). RT-mutex does ha=
ve
Quoted 5 lines
Quoted 2 lines
a timed version, so I guess it shouldn't be hard to implement one for mutex. I can take a shot at trying to do that.
(just caught up with the entire e-mail thread)
Sorry Waiman but personally I thoroughly detest loops around mutex_tryl=
ock() or
Quoted 1 line
mutex_timed_lock()because such loops are usually used to paper over a =
problem
Quoted 1 line
instead of fixing the root cause. What I understood from the comment in=
v1 of your
Quoted 1 line
patch is that
bd_mutexis not only held during block device creation an=
d removal
Quoted 1 line
but additionally that
bd_mutexis obtained inside sysfs attribute callb=
ack methods?
Quoted 1 line
That pattern is guaranteed to lead to deadlocks. Since the block device=
removal
Quoted 1 line
code waits until all sysfs callback methods have finished there is no n=
eed to
Quoted 1 line
protect against block device removal inside the sysfs callback methods.=
My proposal
You are right. We don't really need to take the bd_mutex as the fact that inside the sysfs callback method will guarantee the block device won't go away.
Quoted 1 line
is to split
bd_mutex: one global mutex that serializes block device cre=
ation and
Quoted 1 line
removal and one mutex per block device that serializes changes to a sin=
gle block
Quoted 1 line
device. Obtaining the global mutex from inside a block device sysfs cal=
lback
Quoted 1 line
function is not safe but obtaining the per-block-device mutex from insi=
de a sysfs
Quoted 3 lines
callback function is safe.
Bart.
The bd_mutex we are talking here is already per block device. I am thinking about having a global blktrace mutex that is used to serialize the read and write of blktrace attributes. Since blktrace sysfs files are not supposed to be frequently accessed, having a global lock shouldn't cause any problem.
Thanks,
Longman
Steven Rostedt
maintainerQuoted 3 lines · On Fri, 18 Aug 2017 16:21:46 +0000 Bart Van Assche <[email protected]>…
Sorry Waiman but personally I thoroughly detest loops around
mutex_trylock()ormutex_timed_lock()because such loops are usually used to paper over a problem instead of fixing the root cause.
100% agree.
-- Steve