2.6.24-rc7 lockdep warning when poweroff
Hi,
When I "halt -p", lockdep warnings triggered as following (hand copy):
WARNING : at kernel/lockdep.c: 700 lookup_lock_class()
<snip>lock_acquirecleanup_workqueue_threadworkqueue_cpu_callbacknotifier_call_chain__raw_notifier_call_chainraw_notifier_call_chain__cpu_downdisable_nonboot_cpuskernel_power_offsys_reboot
<snip>
Regards
dave
Quoted 6 lines · Dave Young
Hi,
When I "halt -p", lockdep warnings triggered as following (hand copy):
WARNING : at
kernel/lockdep.c: 700lookup_lock_class()
Quoted 12 lines
<snip>
lock_acquirecleanup_workqueue_threadworkqueue_cpu_callbacknotifier_call_chain__raw_notifier_call_chainraw_notifier_call_chain__cpu_downdisable_nonboot_cpuskernel_power_offsys_reboot
<snip>
My first guess would be a __create_workqueue_key() where a key is re-used with a different workqueue name.
Quoted 18 lines
Quoted 5 lines
When I "halt -p", lockdep warnings triggered as following (hand copy):
WARNING : at
kernel/lockdep.c: 700lookup_lock_class()Quoted 12 lines
<snip>
lock_acquirecleanup_workqueue_threadworkqueue_cpu_callbacknotifier_call_chain__raw_notifier_call_chainraw_notifier_call_chain__cpu_downdisable_nonboot_cpuskernel_power_offsys_reboot
<snip>
Quoted 2 lines
My first guess would be a
__create_workqueue_key()where a key is re-used with a different workqueue name.
Hm, can you elaborate how you got there from the trace above? I don't think I'm following.
johannes
Quoted 17 lines · Johannes Berg
Quoted 17 lines
Quoted 16 lines
When I "halt -p", lockdep warnings triggered as following (hand copy):
WARNING : at
kernel/lockdep.c: 700lookup_lock_class()<snip>
lock_acquirecleanup_workqueue_threadworkqueue_cpu_callbacknotifier_call_chain__raw_notifier_call_chainraw_notifier_call_chain__cpu_downdisable_nonboot_cpuskernel_power_offsys_reboot
<snip>
Quoted 5 lines
Quoted 2 lines
My first guess would be a
__create_workqueue_key()where a key is re-used with a different workqueue name.Hm, can you elaborate how you got there from the trace above? I don't think I'm following.
The warning that triggered (lockdep.c:700) means that one class (key) was used with more than one name.
Looking at cleanup_workqueue_thread(), the lock_acquire() there works on wq->lockdep_map, and that is only initialized at one spot:__create_workqueue_key(), thus it stands to reason that that was mis-used.
Quoted 3 lines
The warning that triggered (lockdep.c:700) means that one class (key) was used with more than one name.
Right.
Quoted 4 lines
Looking at
cleanup_workqueue_thread(), thelock_acquire()there works onwq->lockdep_map, and that is only initialized at one spot:__create_workqueue_key(), thus it stands to reason that that was mis-used.
Oh ok, yes, makes sense. Maybe something is generating a workqueue with a name that's passed in but the key is statically from that place. I'll try to find it.
johannes
Quoted 6 lines · Johannes Berg
Quoted 3 lines
The warning that triggered (lockdep.c:700) means that one class (key) was used with more than one name.
Right.
Quoted 8 lines
Quoted 4 lines
Looking at
cleanup_workqueue_thread(), thelock_acquire()there works onwq->lockdep_map, and that is only initialized at one spot:__create_workqueue_key(), thus it stands to reason that that was mis-used.Oh ok, yes, makes sense. Maybe something is generating a workqueue with a name that's passed in but the key is statically from that place. I'll try to find it.
I add some debug printk and found the names :
block_osm/exec_osm
in drivers/message/i2o
maybe this helps.
Regards
dave
Quoted 3 lines
johannes
Quoted 16 lines · Dave Young
Quoted 6 lines · Johannes Berg
Quoted 3 lines
The warning that triggered (lockdep.c:700) means that one class (key) was used with more than one name.
Right.
Quoted 8 lines
Quoted 4 lines
Looking at
cleanup_workqueue_thread(), thelock_acquire()there works onwq->lockdep_map, and that is only initialized at one spot:__create_workqueue_key(), thus it stands to reason that that was mis-used.Oh ok, yes, makes sense. Maybe something is generating a workqueue with a name that's passed in but the key is statically from that place. I'll try to find it.
Quoted 7 lines
I add some debug printk and found the names :
block_osm/exec_osm
in
drivers/message/i2omaybe this helps.
Not sure right or not, the following patch fixed the problem:
include/linux/workqueue.h +1 -1
@@ -154,7 +154,7 @@ __create_workqueue_key(const char *name,#ifdef CONFIG_LOCKDEP #define __create_workqueue (name, singlethread, freezeable) \ ({ \ - static struct lock_class_key __key; \ + struct lock_class_key __key; \ \ __create_workqueue_key((name), (singlethread), \ (freezeable), &__key); \
Quoted 7 lines
Regards
daveQuoted 3 lines
johannes
Quoted 31 lines · Dave Young
Quoted 16 lines · Dave Young
Quoted 6 lines · Johannes Berg
Quoted 3 lines
The warning that triggered (lockdep.c:700) means that one class (key) was used with more than one name.
Right.
Quoted 8 lines
Quoted 4 lines
Looking at
cleanup_workqueue_thread(), thelock_acquire()there works onwq->lockdep_map, and that is only initialized at one spot:__create_workqueue_key(), thus it stands to reason that that was mis-used.Oh ok, yes, makes sense. Maybe something is generating a workqueue with a name that's passed in but the key is statically from that place. I'll try to find it.
Quoted 7 lines
I add some debug printk and found the names :
block_osm/exec_osm
in
drivers/message/i2omaybe this helps.
Not sure right or not, the following patch fixed the problem:
diff -upr
linux/include/linux/workqueue.hlinux.new/include/linux/workqueue.h
include/linux/workqueue.h __create_workqueue_key(const char *name, · line 160
Quoted 8 lines
include/linux/workqueue.h +1
-1@@ -154,7 +154,7 @@ __create_workqueue_key(const char *name,#ifdef CONFIG_LOCKDEP #define __create_workqueue (name, singlethread, freezeable) \ ({ \ - static struct lock_class_key __key; \ + struct lock_class_key __key; \ \ __create_workqueue_key((name), (singlethread), \ (freezeable), &__key); \
That didn't get you:
INFO: trying to register non-static key.
Msgs?
But it sure looks like __create_workqueue() is asking for trouble, if there is a __create_workqueue() instance that takes a non constant name we're in trouble.
Ok, I checked all users of the create*workqueue() API now.
Turns out that there are many users that give a dynamic string as the workqueue name (only the first three are relevant for the problem at hand because the others are single-threaded):
drivers/connector/cn_queue.cdrivers/media/video/ivtv/ivtv-driver.cdrivers/message/i2o/driver.c
drivers/i2c/chips/m41t00.c drivers/infiniband/core/mad.c drivers/message/fusion/mptfc.c drivers/net/qla3xxx.c drivers/scsi/hosts.c drivers/scsi/qla4xxx/ql4_os.c drivers/scsi/scsi_transport_fc.c drivers/spi/mpc52xx_psc_spi.c drivers/spi/omap2_mcspi.c drivers/spi/spi_bitbang.c drivers/spi/spi_txx9.c drivers/spi/spi_imx.c drivers/spi/pxa2xx_spi.c drivers/spi/spi_bfin5xx.c drivers/power/ds2760_battery.c net/mac80211/ieee80211.c
That's not really the problem though.
TBH, when writing the workqueue deadlock detection code I wasn't aware that it is not allowed to use the same key with different names.
To make sure now:
same key - different name - BAD
same key - same name - OK
different key - same name - OK
different key - different name - OK
Correct?
The root problem here seems to be that I use the same name as for the workqueue for the lockdep_map and other code uses a non-static workqueue name. Using the workqueue name for the lock is good for knowing which workqueue ran into trouble though.
mac80211 for example wants to allocate a (single-threaded) workqueue for each hardware that is plugged in and wants to call it by the hardware name.
Anyway, the patch below should help. I hope the patch compiles, I don't have a lockdep-enabled system at hand right now (irqtrace is still not supported on powerpc and my 64-bit powerpc isn't running a kernel with my irqtrace support patch at the moment).
If you think the patch is a correct way to solve the problem I'll submit it formally and it should then be included in 2.6.24 to avoid regressions with the workqueue API (the workqueue lockup detection was merged early in 2.6.24.) Who should I send it to in that case?
Dave, do you know if you had connector, ivtv or i2o in the kernel (just to make sure my analysis was correct)? And can you reproduce the problem, and if so, can you try if this patch helps?
johannes
--- include/linux/workqueue.h | 14 +++++++++++--- kernel/workqueue.c | 5 +++-- 2 files changed, 14 insertions(+), 5 deletions(-)
include/linux/workqueue.h +11 -3
@@ -149,19 +149,27 @@ struct execute_work {extern struct workqueue_struct *__create_workqueue_key (const char *name,int singlethread, -int freezeable,struct lock_class_key *key); +int freezeable,struct lock_class_key *key, +const char *lock_name);#ifdef CONFIG_LOCKDEP #define __create_workqueue (name, singlethread, freezeable) \ ({ \ static struct lock_class_key __key; \ + const char *__lock_name; \ + \ + if (__builtin_constant_p(name)) \ + __lock_name = (name); \ + else \ + __lock_name = #name; \ \ __create_workqueue_key((name), (singlethread), \ - (freezeable), &__key); \ + (freezeable), &__key, \ + __lock_name); \ })#else #define __create_workqueue (name, singlethread, freezeable) \ - __create_workqueue_key((name), (singlethread), (freezeable), NULL) + __create_workqueue_key((name), (singlethread), (freezeable), NULL, NULL)#endif #define create_workqueue (name) __create_workqueue((name), 0, 0)
kernel/workqueue.c +3 -2
@@ -722,7 +722,8 @@ static void start_workqueue_thread(strucstruct workqueue_struct *__create_workqueue_key (const char *name,int singlethread,int freezeable, -struct lock_class_key *key) +struct lock_class_key *key, +const char *lock_name) {struct workqueue_struct *wq;struct cpu_workqueue_struct *cwq;
@@ -739,7 +740,7 @@ struct workqueue_struct *__create_workqu } wq->name = name; -lockdep_init_map (&wq->lockdep_map, name, key,0 ); +lockdep_init_map (&wq->lockdep_map, lock_name, key,0 ); wq->singlethread = singlethread; wq->freezeable = freezeable;INIT_LIST_HEAD (&wq->list);
Quoted 5 lines · Johannes Berg
Ok, I checked all users of the create*workqueue() API now.
Turns out that there are many users that give a dynamic string as the workqueue name (only the first three are relevant for the problem at hand because the others are single-threaded):
I'm not sure the single threadedness of a workqueue matters here.
Quoted 16 lines
drivers/connector/cn_queue.cdrivers/media/video/ivtv/ivtv-driver.cdrivers/message/i2o/driver.c
drivers/i2c/chips/m41t00.cdrivers/infiniband/core/mad.cdrivers/message/fusion/mptfc.cdrivers/net/qla3xxx.cdrivers/scsi/hosts.cdrivers/scsi/qla4xxx/ql4_os.cdrivers/scsi/scsi_transport_fc.cdrivers/spi/mpc52xx_psc_spi.cdrivers/spi/omap2_mcspi.cdrivers/spi/spi_bitbang.cdrivers/spi/spi_txx9.cdrivers/spi/spi_imx.cdrivers/spi/pxa2xx_spi.cdrivers/spi/spi_bfin5xx.cdrivers/power/ds2760_battery.cnet/mac80211/ieee80211.cThat's not really the problem though.
Quoted 7 lines
TBH, when writing the workqueue deadlock detection code I wasn't aware that it is not allowed to use the same key with different names.
To make sure now:
same key - different name - BAD
same key - same name - OK
different key - same name - OK
Strictly speaking one can do that, although I would recommend against it
- it leads to confusion as to which lock got into trouble when looking at lockdep/stat output.
Quoted 3 lines
different key - different name - OK
Correct?
Yeah.
Quoted 4 lines
The root problem here seems to be that I use the same name as for the workqueue for the
lockdep_mapand other code uses a non-static workqueue name. Using the workqueue name for the lock is good for knowing which workqueue ran into trouble though.
Indeed, and also using a different key allows the workqueue to have different lock dependencies as well. The trouble is, lockdep works at the class level, a class with multiple names just doesn't make sense, and reporting will get it wrong (although it may appear to work correctly in the trivial cases).
Quoted 3 lines
mac80211 for example wants to allocate a (single-threaded) workqueue for each hardware that is plugged in and wants to call it by the hardware name.
Right, that would require a new key for each instance.
Quoted 4 lines
Anyway, the patch below should help. I hope the patch compiles, I don't have a lockdep-enabled system at hand right now (irqtrace is still not supported on powerpc and my 64-bit powerpc isn't running a kernel with my irqtrace support patch at the moment).
Tssk :-)
Quoted 4 lines
If you think the patch is a correct way to solve the problem I'll submit it formally and it should then be included in 2.6.24 to avoid regressions with the workqueue API (the workqueue lockup detection was merged early in 2.6.24.)
The patch looks ok, one important thing to note is that it means that all workqueues instantiated by the same __create_workqueue() call-site share lock dependency chains - I'm unsure if that might get us into trouble or not.
Quoted 1 line
Who should I send it to in that case?
Me and Ingo :-)
Quoted 68 lines
Dave, do you know if you had connector, ivtv or i2o in the kernel (just to make sure my analysis was correct)? And can you reproduce the problem, and if so, can you try if this patch helps?
johannes
--- include/linux/workqueue.h | 14 +++++++++++--- kernel/workqueue.c | 5 +++-- 2 files changed, 14 insertions(+), 5 deletions(-)include/linux/workqueue.h +11
-3@@ -149,19 +149,27 @@ struct execute_work {extern struct workqueue_struct *__create_workqueue_key (const char *name,int singlethread, -int freezeable,struct lock_class_key *key); +int freezeable,struct lock_class_key *key, +const char *lock_name);#ifdef CONFIG_LOCKDEP #define __create_workqueue (name, singlethread, freezeable) \ ({ \ static struct lock_class_key __key; \ + const char *__lock_name; \ + \ + if (__builtin_constant_p(name)) \ + __lock_name = (name); \ + else \ + __lock_name = #name; \ \ __create_workqueue_key((name), (singlethread), \ - (freezeable), &__key); \ + (freezeable), &__key, \ + __lock_name); \ })#else #define __create_workqueue (name, singlethread, freezeable) \ - __create_workqueue_key((name), (singlethread), (freezeable), NULL) + __create_workqueue_key((name), (singlethread), (freezeable), NULL, NULL)#endif #define create_workqueue (name) __create_workqueue((name), 0, 0)kernel/workqueue.c +3
-2@@ -722,7 +722,8 @@ static void start_workqueue_thread(strucstruct workqueue_struct *__create_workqueue_key (const char *name,int singlethread,int freezeable, -struct lock_class_key *key) +struct lock_class_key *key, +const char *lock_name) {struct workqueue_struct *wq;struct cpu_workqueue_struct *cwq;@@ -739,7 +740,7 @@ struct workqueue_struct *__create_workqu } wq->name = name; -lockdep_init_map (&wq->lockdep_map, name, key,0 ); +lockdep_init_map (&wq->lockdep_map, lock_name, key,0 ); wq->singlethread = singlethread; wq->freezeable = freezeable;INIT_LIST_HEAD (&wq->list);
Quoted 9 lines
Quoted 5 lines
To make sure now:
same key - different name - BAD
same key - same name - OK
different key - same name - OKStrictly speaking one can do that, although I would recommend against it
- it leads to confusion as to which lock got into trouble when looking at lockdep/stat output.
True, but I don't see a good way to avoid that. Similar things also happen with
mutex_init(&priv->mtx);
for example, no?
Quoted 10 lines
Quoted 4 lines
The root problem here seems to be that I use the same name as for the workqueue for the
lockdep_mapand other code uses a non-static workqueue name. Using the workqueue name for the lock is good for knowing which workqueue ran into trouble though.Indeed, and also using a different key allows the workqueue to have different lock dependencies as well. The trouble is, lockdep works at the class level, a class with multiple names just doesn't make sense, and reporting will get it wrong (although it may appear to work correctly in the trivial cases).
Right.
Quoted 5 lines
Quoted 3 lines
mac80211 for example wants to allocate a (single-threaded) workqueue for each hardware that is plugged in and wants to call it by the hardware name.
Right, that would require a new key for each instance.
Except, how could I do that though? Keys are required to be static, so I can't have the object as the key. In any case, I don't think it matters much because the workqueues are per-hardware but all have similar users, I think that the other users here probably behave similarly.
Quoted 9 lines
Quoted 4 lines
If you think the patch is a correct way to solve the problem I'll submit it formally and it should then be included in 2.6.24 to avoid regressions with the workqueue API (the workqueue lockup detection was merged early in 2.6.24.)
The patch looks ok, one important thing to note is that it means that all workqueues instantiated by the same
__create_workqueue()call-site share lock dependency chains - I'm unsure if that might get us into trouble or not.
It doesn't seem to have so far ;) I don't think it should. If some code allocates a per-instance workqueue that's much like having an inode lock or so.
The scenario to get into trouble with this would require having a per-instance lock and a per-instance workqueue and flushing the workqueue (that can contain functions taking the lock of instance A) of instance A under the lock of instance B, but unless that is nested in a way that it cannot happen in order BA as well it's actually a possible ABBA deadlock.
Quoted 3 lines
Quoted 1 line
Who should I send it to in that case?
Me and Ingo :-)
Alright, I'll write a patch description and send it in a minute.
johannes
Quoted 9 lines · Johannes Berg
Quoted 8 lines
Quoted 4 lines
To make sure now:
same key - different name - BAD
same key - same name - OK
different key - same name - OKStrictly speaking one can do that, although I would recommend against it
- it leads to confusion as to which lock got into trouble when looking at lockdep/stat output.
Quoted 6 lines
True, but I don't see a good way to avoid that. Similar things also happen with
mutex_init(&priv->mtx);for example, no?
Yeah, it happens, I tend to 'fix' them when I encounter it though, sometimes by just slightly altering the expression. It helps when grepping the tree.
Quoted 10 lines
Quoted 5 lines
Quoted 3 lines
mac80211 for example wants to allocate a (single-threaded) workqueue for each hardware that is plugged in and wants to call it by the hardware name.
Right, that would require a new key for each instance.
Except, how could I do that though? Keys are required to be static, so I can't have the object as the key. In any case, I don't think it matters much because the workqueues are per-hardware but all have similar users, I think that the other users here probably behave similarly.
Yeah, I think so too, but never underestimate the creativity of driver authors:-)
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
If you think the patch is a correct way to solve the problem I'll submit it formally and it should then be included in 2.6.24 to avoid regressions with the workqueue API (the workqueue lockup detection was merged early in 2.6.24.)
Quoted 8 lines
Quoted 4 lines
The patch looks ok, one important thing to note is that it means that all workqueues instantiated by the same
__create_workqueue()call-site share lock dependency chains - I'm unsure if that might get us into trouble or not.It doesn't seem to have so far ;) I don't think it should. If some code allocates a per-instance workqueue that's much like having an inode lock or so.
We had to split up the inode lock to per filesystem classes, just because the lock chains were conflicting between them...
Quoted 3 lines
Quoted 1 line
Me and Ingo :-)
Alright, I'll write a patch description and send it in a minute.
Great, thanks for the effort.
PATCH 1/1 fix workqueue creation API lockdep interaction
Dave Young reported warnings from lockdep that the workqueue API can sometimes try to register lockdep classes with the same key but different names. This is not permitted in lockdep.
Unfortunately, I was unaware of that restriction when I wrote the code to debug workqueue problems with lockdep and used the workqueue name as the lockdep class name. This can obviously lead to the problem if the workqueue name is dynamic.
This patch solves the problem by always using a constant name for the workqueue's lockdep class, namely either the constant name that was passed in or a string consisting of the variable name.
---
Please be careful with this patch, I haven't been able to test it so far because my powerbook doesn't have lockdep.
include/linux/workqueue.h | 14 +++++++++++--- kernel/workqueue.c | 5 +++-- 2 files changed, 14 insertions(+), 5 deletions(-)
include/linux/workqueue.h +11 -3
@@ -149,19 +149,27 @@ struct execute_work {extern struct workqueue_struct *__create_workqueue_key (const char *name,int singlethread, -int freezeable,struct lock_class_key *key); +int freezeable,struct lock_class_key *key, +const char *lock_name);#ifdef CONFIG_LOCKDEP #define __create_workqueue (name, singlethread, freezeable) \ ({ \ static struct lock_class_key __key; \ + const char *__lock_name; \ + \ + if (__builtin_constant_p(name)) \ + __lock_name = (name); \ + else \ + __lock_name = #name; \ \ __create_workqueue_key((name), (singlethread), \ - (freezeable), &__key); \ + (freezeable), &__key, \ + __lock_name); \ })#else #define __create_workqueue (name, singlethread, freezeable) \ - __create_workqueue_key((name), (singlethread), (freezeable), NULL) + __create_workqueue_key((name), (singlethread), (freezeable), NULL, NULL)#endif #define create_workqueue (name) __create_workqueue((name), 0, 0)
kernel/workqueue.c +3 -2
@@ -722,7 +722,8 @@ static void start_workqueue_thread(strucstruct workqueue_struct *__create_workqueue_key (const char *name,int singlethread,int freezeable, -struct lock_class_key *key) +struct lock_class_key *key, +const char *lock_name) {struct workqueue_struct *wq;struct cpu_workqueue_struct *cwq;
@@ -739,7 +740,7 @@ struct workqueue_struct *__create_workqu } wq->name = name; -lockdep_init_map (&wq->lockdep_map, name, key,0 ); +lockdep_init_map (&wq->lockdep_map, lock_name, key,0 ); wq->singlethread = singlethread; wq->freezeable = freezeable;INIT_LIST_HEAD (&wq->list);
Re: [PATCH for 2.6.24] fix workqueue creation API lockdep interaction
Quoted 14 lines · Johannes Berg
Dave Young reported warnings from lockdep that the workqueue API can sometimes try to register lockdep classes with the same key but different names. This is not permitted in lockdep.
Unfortunately, I was unaware of that restriction when I wrote the code to debug workqueue problems with lockdep and used the workqueue name as the lockdep class name. This can obviously lead to the problem if the workqueue name is dynamic.
This patch solves the problem by always using a constant name for the workqueue's lockdep class, namely either the constant name that was passed in or a string consisting of the variable name.
Quoted 4 lines
Signed-off-by Johannes Berg <[email protected]>---
Please be careful with this patch, I haven't been able to test it so far because my powerbook doesn't have lockdep.
Hi,
Just for confirm, the warnings didn't trigger after applied your patch, thanks.
Regards
dave
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines
Quoted 4 lines
The patch looks ok, one important thing to note is that it means that all workqueues instantiated by the same
__create_workqueue()call-site share lock dependency chains - I'm unsure if that might get us into trouble or not.It doesn't seem to have so far ;) I don't think it should. If some code allocates a per-instance workqueue that's much like having an inode lock or so.
We had to split up the inode lock to per filesystem classes, just because the lock chains were conflicting between them...
i.e. filesystems can legally have different locking rules wrt. i_lock. I dont really like it (we should have as simple locking rules as possible) but it is the VFS status quo :)
Ingo
Quoted 6 lines
Quoted 4 lines
Turns out that there are many users that give a dynamic string as the workqueue name (only the first three are relevant for the problem at hand because the others are single-threaded):
I'm not sure the single threadedness of a workqueue matters here.
Oh I should have explained this, missed it earlier. No, the single-threadedness doesn't really matter for the problem, but the warning Dave got couldn't possibly have triggered for a single-threaded workqueue because it was from the CPU up/down code that spawns/stops the new workqueue thread on a new/dying CPU.
johannes