kthread_worker: Initialize dynamically allocated spinlock properly for lockdep
PATCH 1/1 kthread_worker: Initialize dynamically allocated spinlock properly for lockdep
init_kthread_worker(), via KTHREAD_WORKER_INIT(), used an initializer for static spin_lock objects, SPIN_LOCK_UNLOCKED, on a dynamically allocated kthread_worker object's internal spinlock_t. This causes lockdep to gripe:
INFO: trying to register non-static key. the code is fine but needs lockdep annotation. turning off the locking correctness validator.
To keep lockdep happy, use spin_lock_init() for dynamically allocated kthread_worker objects' internal spinlock_t.
include/linux/kthread.h +7 -1
@@ -83,7 +83,13 @@ struct kthread_work {static inline void init_kthread_worker (struct kthread_worker *worker) { - *worker = (struct kthread_worker )KTHREAD_WORKER_INIT (*worker); +/* +* Lockdep complains if a dynamically allocated worker's spinlock_t +* is initialzed using SPIN_LOCK_UNLOCKED. +*/ +spin_lock_init (&worker->lock); +INIT_LIST_HEAD (&worker->work_list); + worker->task =NULL ; } static inline void init_kthread_work(struct kthread_work *work,
Quoted 26 lines · Andy Walls
init_kthread_worker(), viaKTHREAD_WORKER_INIT(), used an initializer for staticspin_lockobjects,SPIN_LOCK_UNLOCKED, on a dynamically allocatedkthread_workerobject's internalspinlock_t. This causes lockdep to gripe:INFO: trying to register non-static key. the code is fine but needs lockdep annotation. turning off the locking correctness validator.
To keep lockdep happy, use
spin_lock_init()for dynamically allocatedkthread_workerobjects' internalspinlock_t.Reported-by Nicolas <[email protected]>Signed-off-by Andy Walls <[email protected]>Cc Tejun Heo <[email protected]>Cc Jarod Wilson <[email protected]>Cc Ingo Molnar <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Cc Hans Verkuil <[email protected]>include/linux/kthread.h +0
-0
include/linux/kthread.h struct kthread_work { · line 90
Quoted 9 lines
include/linux/kthread.h +5
-1@@ -83,7 +83,13 @@ struct kthread_work { static inline void init_kthread_worker(struct kthread_worker *worker) { - *worker = (struct kthread_worker)KTHREAD_WORKER_INIT(*worker); + /* + * Lockdep complains if a dynamically allocated worker's spinlock_t + * is initialzed using SPIN_LOCK_UNLOCKED. + */ + spin_lock_init(&worker->lock);
This will make different kthead_worker->lock initialized with one same key. You know spin_lock_init() will be like below:
# define raw_spin_lock_init(lock) \
do { \
static struct lock_class_key __key; \
\
__raw_spin_lock_init((lock), #lock, &__key); \
} while (0)So we should put the real initializer to kernel/kthread.c and make init_kthread_worker() to be a MACRO.
BTW, init_kthread_work() should also be changed like above because member done is a wait_queue_head.
Thanks,
Yong
Signature · 1 line
Only stand for myself.
Quoted 46 lines · Yong Zhang
Quoted 26 lines · Andy Walls
init_kthread_worker(), viaKTHREAD_WORKER_INIT(), used an initializer for staticspin_lockobjects,SPIN_LOCK_UNLOCKED, on a dynamically allocatedkthread_workerobject's internalspinlock_t. This causes lockdep to gripe:INFO: trying to register non-static key. the code is fine but needs lockdep annotation. turning off the locking correctness validator.
To keep lockdep happy, use
spin_lock_init()for dynamically allocatedkthread_workerobjects' internalspinlock_t.Reported-by Nicolas <[email protected]>Signed-off-by Andy Walls <[email protected]>Cc Tejun Heo <[email protected]>Cc Jarod Wilson <[email protected]>Cc Ingo Molnar <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Cc Hans Verkuil <[email protected]>include/linux/kthread.h +0
-0include/linux/kthread.h struct kthread_work { · line 90
Quoted 9 lines
include/linux/kthread.h +5
-1@@ -83,7 +83,13 @@ struct kthread_work { static inline void init_kthread_worker(struct kthread_worker *worker) { - *worker = (struct kthread_worker)KTHREAD_WORKER_INIT(*worker); + /* + * Lockdep complains if a dynamically allocated worker's spinlock_t + * is initialzed using SPIN_LOCK_UNLOCKED. + */ + spin_lock_init(&worker->lock);This will make different
kthead_worker->lockinitialized with one same key. You knowspin_lock_init()will be like below:# define raw_spin_lock_init(lock) \ do { \ static struct lock_class_key __key; \ \ __raw_spin_lock_init((lock), #lock, &__key); \ } while (0)
Quoted 5 lines
So we should put the real initializer to
kernel/kthread.cand makeinit_kthread_worker()to be a MACRO.BTW,
init_kthread_work()should also be changed like above because member done is await_queue_head.
untested patch is here. Andy/Nicolas, is it ok for you?
---
Subject: [PATCH] kthread_work: Make lockdep happy
spinlock in kthread_worker and wait_queue_head in kthread_work both should be lockdep annotated.
So change the interface to make it suiltable for CONFIG_LOCKDEP.
---
I'm not sure if it's possible to define a worker on stack? So I left DEFINE_KTHREAD_WORKER() untouched.
include/linux/kthread.h | 37 +++++++++++++++++++++++++++---------- kernel/kthread.c | 9 +++++++++ 2 files changed, 36 insertions(+), 10 deletions(-)
include/linux/kthread.h +27 -10
@@ -75,22 +75,39 @@ struct kthread_work { .flushing = ATOMIC_INIT(0), \ } +/* Is it possible to define a worker on stack? */ #define DEFINE_KTHREAD_WORKER (worker) \ struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)#define DEFINE_KTHREAD_WORK (work, fn) \ struct kthread_work work = KTHREAD_WORK_INIT(work, fn) -static inline void init_kthread_worker (struct kthread_worker *worker) -{ - *worker = (struct kthread_worker )KTHREAD_WORKER_INIT (*worker); -} - -static inline void init_kthread_work (struct kthread_work *work, -kthread_work_func_t fn) -{ - *work = (struct kthread_work )KTHREAD_WORK_INIT (*work, fn); -} +#ifdef CONFIG_LOCKDEP +# define KTHREAD_WORK_INIT_ONSTACK (work, fn) \ + ({init_kthread_work((&work), fn); work}) +# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) \ + struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn) +#else +# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) DEFINE_KTHREAD_WORK(work, fn) +#endif + +extern void __init_kthread_worker (struct kthread_worker *worker, +struct lock_class_key *key); + +#define init_kthread_worker (worker) \ + do { \ + static struct lock_class_key __key; \ + __init_kthread_worker((worker), &__key); \ + } while (0) + +#define init_kthread_work (work, fn) \ + do { \ + memset((work), 0, sizeof(struct kthread_work)); \ + INIT_LIST_HEAD(&(work)->node); \ + (work)->func = (fn); \ + init_waitqueue_head(&(work)->done); \ + (work)->flushing = ATOMIC_INIT(0); \ + } while (0)int kthread_worker_fn (void *worker_ptr);
kernel/kthread.c +9 -0
@@ -265,6 +265,15 @@ int kthreadd(void *unused)return 0 ; } +void __init_kthread_worker (struct kthread_worker *worker, +struct lock_class_key *key) +{ +spin_lock_init (&worker->lock); +lockdep_set_class (&worker->lock, key); +INIT_LIST_HEAD (&worker->work_list); + worker->task ==NULL ; +} +/** * kthread_worker_fn - kthread function to process kthread_worker * @worker_ptr: pointer to initialized kthread_worker
Signature · 3 lines
1.7.0.4 -- Only stand for myself.
Hello,
Quoted 10 lines · Yong Zhang
Subject: [PATCH]
kthread_work: Make lockdep happyspinlock in
kthread_workerandwait_queue_headinkthread_workboth should be lockdep annotated.
So change the interface to make it suiltable forCONFIG_LOCKDEP.Signed-off-by Yong Zhang <[email protected]>---
I'm not sure if it's possible to define a worker on stack? So I leftDEFINE_KTHREAD_WORKER()untouched.
Yes, it can, I think. BTW, where are you using kthread_worker? I'm planning to update its flush semantics similar to that of proper workqueue so that it's less confusing and switching between the two is easy, so its usage may change slightly soon, although conversion shouldn't be difficult.
Thanks.
Signature · 1 line
tejun
Quoted 17 lines · Yong Zhang
Quoted 16 lines · Yong Zhang
Quoted 15 lines · Andy Walls
init_kthread_worker(), viaKTHREAD_WORKER_INIT(), used an initializer for staticspin_lockobjects,SPIN_LOCK_UNLOCKED, on a dynamically allocatedkthread_workerobject's internalspinlock_t. This causes lockdep to gripe:INFO: trying to register non-static key. the code is fine but needs lockdep annotation. turning off the locking correctness validator.
To keep lockdep happy, use
spin_lock_init()for dynamically allocatedkthread_workerobjects' internalspinlock_t.Reported-by Nicolas <[email protected]>Signed-off-by Andy Walls <[email protected]>
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Cc Tejun Heo <[email protected]>Cc Jarod Wilson <[email protected]>Cc Ingo Molnar <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Cc Hans Verkuil <[email protected]>This will make different
kthead_worker->lockinitialized with one same key.
Well, that wouldn't be very useful. :P
Quoted 2 lines
Quoted 2 lines
So we should put the real initializer to
kernel/kthread.cand makeinit_kthread_worker()to be a MACRO.
Sounds OK to me. I'm not a lockdep expert and I made my initial patch with the sole intention of making this bugzilla report go away:
https://bugzilla.redhat.com/show_bug.cgi?id=662384
Quoted 1 line
untested patch is here. Andy/Nicolas, is it ok for you?
No, see my comments below.
Quoted 21 lines
---
Subject: [PATCH]kthread_work: Make lockdep happyspinlock in
kthread_workerandwait_queue_headinkthread_workboth should be lockdep annotated.
So change the interface to make it suiltable forCONFIG_LOCKDEP.Signed-off-by Yong Zhang <[email protected]>---
I'm not sure if it's possible to define a worker on stack? So I leftDEFINE_KTHREAD_WORKER()untouched.include/linux/kthread.h | 37 +++++++++++++++++++++++++++---------- kernel/kthread.c | 9 +++++++++ 2 files changed, 36 insertions(+), 10 deletions(-)include/linux/kthread.h +0
-0
include/linux/kthread.h struct kthread_work { · line 78
Quoted 4 lines
include/linux/kthread.h +1
-0@@ -75,22 +75,39 @@ struct kthread_work { .flushing = ATOMIC_INIT(0), \ } +/* Is it possible to define a worker on stack? */
This comment doesn't help a developer decide if this interface is OK to use.
If there is an alternate preferred API for instantiating 1 (or more) thread(s) to handle work objects off of the stack, then the comment should point the reader to that API (e.g. singlethread_workqueue).
To answer the question in the comment:
It is possible to allocate a kthread worker off of the stack, but IMO it has little advantage over a singlethread_workqueue allocated off of the stack.
ivtv only needed the kthread_worker API, because it has some deferred work with tight timing constraints. ivtv sets the kthread_worker to SCHED_FIFO scheduling for ivtv work, which couldn't be done on a workqueue thread with the updated singlethread_workqueue implementation. Note that ivtv does *not* allocate its kthread worker off of the stack.
Quoted 50 lines
include/linux/kthread.h +26
-10#define DEFINE_KTHREAD_WORKER (worker) \ struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)#define DEFINE_KTHREAD_WORK (work, fn) \ struct kthread_work work = KTHREAD_WORK_INIT(work, fn) -static inline void init_kthread_worker (struct kthread_worker *worker) -{ - *worker = (struct kthread_worker )KTHREAD_WORKER_INIT (*worker); -} - -static inline void init_kthread_work (struct kthread_work *work, -kthread_work_func_t fn) -{ - *work = (struct kthread_work )KTHREAD_WORK_INIT (*work, fn); -} +#ifdef CONFIG_LOCKDEP +# define KTHREAD_WORK_INIT_ONSTACK (work, fn) \ + ({init_kthread_work((&work), fn); work}) +# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) \ + struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn) +#else +# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) DEFINE_KTHREAD_WORK(work, fn) +#endif + +extern void __init_kthread_worker (struct kthread_worker *worker, +struct lock_class_key *key); + +#define init_kthread_worker (worker) \ + do { \ + static struct lock_class_key __key; \ + __init_kthread_worker((worker), &__key); \ + } while (0) + +#define init_kthread_work (work, fn) \ + do { \ + memset((work), 0, sizeof(struct kthread_work)); \ + INIT_LIST_HEAD(&(work)->node); \ + (work)->func = (fn); \ + init_waitqueue_head(&(work)->done); \ + (work)->flushing = ATOMIC_INIT(0); \ + } while (0)int kthread_worker_fn (void *worker_ptr);kernel/kthread.c +0
-0
kernel/kthread.c int kthreadd(void *unused) · line 274
Quoted 10 lines
kernel/kthread.c +7
-0@@ -265,6 +265,15 @@ int kthreadd(void *unused)return 0 ; } +void __init_kthread_worker (struct kthread_worker *worker, +struct lock_class_key *key) +{ +spin_lock_init (&worker->lock); +lockdep_set_class (&worker->lock, key); +INIT_LIST_HEAD (&worker->work_list); + worker->task ==NULL ;
^^
| GCC should have griped, "Statement with no effect," or something similar. (Did it?)
Regards,
Andy
Quoted 7 lines
kernel/kthread.c +2
-0+} +/** * kthread_worker_fn - kthread function to process kthread_worker * @worker_ptr: pointer to initialized kthread_worker Signature · 1 line
1.7.0.4
Quoted 9 lines · Tejun Heo
Hello,
Quoted 6 lines · Yong Zhang
Subject: [PATCH]
kthread_work: Make lockdep happyspinlock in
kthread_workerandwait_queue_headinkthread_workboth should be lockdep annotated.
So change the interface to make it suiltable forCONFIG_LOCKDEP.
Quoted 6 lines
Quoted 4 lines
Signed-off-by Yong Zhang <[email protected]>---
I'm not sure if it's possible to define a worker on stack? So I leftDEFINE_KTHREAD_WORKER()untouched.Yes, it can, I think.
Thanks for your confirmation, I will update the patch in V2.
Quoted 1 line
BTW, where are you using
kthread_worker?
I don't have anything using kthread_worker, but go through the kernel and find ivtv is the only one.
This is why I sent the untested patch :)
Quoted 5 lines
I'm
planning to update its flush semantics similar to that of proper workqueue so that it's less confusing and switching between the two is easy, so its usage may change slightly soon, although conversion shouldn't be difficult.
No problem. Now I just make the patch based on the current kthread_work*.
Thanks,
Yong
Signature · 1 line
Only stand for myself.
Quoted 82 lines · Andy Walls
Quoted 17 lines · Yong Zhang
Quoted 16 lines · Yong Zhang
Quoted 15 lines · Andy Walls
init_kthread_worker(), viaKTHREAD_WORKER_INIT(), used an initializer for staticspin_lockobjects,SPIN_LOCK_UNLOCKED, on a dynamically allocatedkthread_workerobject's internalspinlock_t. This causes lockdep to gripe:INFO: trying to register non-static key. the code is fine but needs lockdep annotation. turning off the locking correctness validator.
To keep lockdep happy, use
spin_lock_init()for dynamically allocatedkthread_workerobjects' internalspinlock_t.Reported-by Nicolas <[email protected]>Signed-off-by Andy Walls <[email protected]>Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Cc Tejun Heo <[email protected]>Cc Jarod Wilson <[email protected]>Cc Ingo Molnar <[email protected]>Cc Mauro Carvalho Chehab <[email protected]>Cc Hans Verkuil <[email protected]>This will make different
kthead_worker->lockinitialized with one same key.Well, that wouldn't be very useful. :P
Quoted 2 lines
Quoted 2 lines
So we should put the real initializer to
kernel/kthread.cand makeinit_kthread_worker()to be a MACRO.Sounds OK to me. I'm not a lockdep expert and I made my initial patch with the sole intention of making this bugzilla report go away:
https://bugzilla.redhat.com/show_bug.cgi?id=662384
Quoted 1 line
untested patch is here. Andy/Nicolas, is it ok for you?
No, see my comments below.
Quoted 21 lines
---
Subject: [PATCH]kthread_work: Make lockdep happyspinlock in
kthread_workerandwait_queue_headinkthread_workboth should be lockdep annotated.
So change the interface to make it suiltable forCONFIG_LOCKDEP.Signed-off-by Yong Zhang <[email protected]>---
I'm not sure if it's possible to define a worker on stack? So I leftDEFINE_KTHREAD_WORKER()untouched.include/linux/kthread.h | 37 +++++++++++++++++++++++++++----------
kernel/kthread.c| 9 +++++++++2 files changed, 36 insertions(+), 10 deletions(-)include/linux/kthread.h +0
-0include/linux/kthread.h struct kthread_work { · line 78
Quoted 4 lines
include/linux/kthread.h +1
-0@@ -75,22 +75,39 @@ struct kthread_work { .flushing = ATOMIC_INIT(0), \ } +/* Is it possible to define a worker on stack? */ This comment doesn't help a developer decide if this interface is OK to use.
If there is an alternate preferred API for instantiating 1 (or more) thread(s) to handle work objects off of the stack, then the comment should point the reader to that API (e.g.
singlethread_workqueue).To answer the question in the comment:
It is possible to allocate a kthread worker off of the stack, but IMO it has little advantage over a
singlethread_workqueueallocated off of the stack.
Quoted 5 lines
ivtv only needed the
kthread_workerAPI, because it has some deferred work with tight timing constraints. ivtv sets thekthread_workertoSCHED_FIFOscheduling for ivtv work, which couldn't be done on a workqueue thread with the updatedsinglethread_workqueueimplementation. Note that ivtv does *not* allocate its kthread worker off of the stack.
Just like Tejun has said, It is possible to allocate a kthread worker on stack, so I can just delete that unhelpful comment and also touch kthread_worker in V2.
Quoted 55 lines
Quoted 55 lines
include/linux/kthread.h +26
-10#define DEFINE_KTHREAD_WORKER(worker) \ struct kthread_worker worker = KTHREAD_WORKER_INIT(worker) #define DEFINE_KTHREAD_WORK(work, fn) \ struct kthread_work work = KTHREAD_WORK_INIT(work, fn) -static inline void init_kthread_worker (struct kthread_worker *worker) -{ - *worker = (struct kthread_worker)KTHREAD_WORKER_INIT(*worker); -} - -static inline void init_kthread_work (struct kthread_work *work, - kthread_work_func_t fn) -{ - *work = (struct kthread_work)KTHREAD_WORK_INIT(*work, fn); -} +#ifdef CONFIG_LOCKDEP +# define KTHREAD_WORK_INIT_ONSTACK (work, fn) \ + ({init_kthread_work((&work), fn); work}) +# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) \ + struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn) +#else +# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) DEFINE_KTHREAD_WORK(work, fn) +#endif + +extern void __init_kthread_worker (struct kthread_worker *worker, + struct lock_class_key *key); + +#define init_kthread_worker (worker) \ + do { \ + static struct lock_class_key __key; \ + __init_kthread_worker((worker), &__key); \ + } while (0) + +#define init_kthread_work (work, fn) \ + do { \ + memset((work), 0, sizeof(struct kthread_work)); \ + INIT_LIST_HEAD(&(work)->node); \ + (work)->func = (fn); \ + init_waitqueue_head(&(work)->done); \ + (work)->flushing = ATOMIC_INIT(0); \ + } while (0) int kthread_worker_fn(void *worker_ptr);kernel/kthread.c +0
-0@@ -265,6 +265,15 @@ int kthreadd(void *unused) return 0; }
Quoted 11 lines
kernel/kthread.c int kthreadd(void *unused) · line 274
Quoted 7 lines
kernel/kthread.c +7
-0+void __init_kthread_worker (struct kthread_worker *worker, + struct lock_class_key *key) +{ + spin_lock_init(&worker->lock); + lockdep_set_class(&worker->lock, key); + INIT_LIST_HEAD(&worker->work_list); + worker->task == NULL;^^
|
GCC should have griped, "Statement with no effect," or something similar. (Did it?)
Good catch.
Will change it.
Thanks,
Yong
Signature · 1 line
Only stand for myself.
PATCH v2 1/1 kthread_work: Make lockdep happy
In reply to Tejun HeoFrom: Yong Zhang <[email protected]>
Subject: [V2 PATCH] kthread_work: Make lockdep happy
spinlock in kthread_worker and wait_queue_head in kthread_work both should be lockdep sensible.
So change the interface to make it suiltable for CONFIG_LOCKDEP.
---
Changes from V1:
*According to Tejun, kthread_worker could be defined on stack,
So introduce DEFINE_KTHREAD_WORKER_ONSTACK.
*Change wrong setting to kthread_work->task. Thanks Adny for
pointing it.
*including some minor issue.
BTW, only passed build.
include/linux/kthread.h | 45 +++++++++++++++++++++++++++++++++++---------- kernel/kthread.c | 9 +++++++++ 2 files changed, 44 insertions(+), 10 deletions(-)
include/linux/kthread.h +35 -10
@@ -81,16 +81,41 @@ struct kthread_work {#define DEFINE_KTHREAD_WORK (work, fn) \ struct kthread_work work = KTHREAD_WORK_INIT(work, fn) -static inline void init_kthread_worker (struct kthread_worker *worker) -{ - *worker = (struct kthread_worker )KTHREAD_WORKER_INIT (*worker); -} - -static inline void init_kthread_work (struct kthread_work *work, -kthread_work_func_t fn) -{ - *work = (struct kthread_work )KTHREAD_WORK_INIT (*work, fn); -} +/* +* kthread_worker.lock and kthread_work.done need +* special work if they are defined on stack with +* lockdep enabled. +*/ +#ifdef CONFIG_LOCKDEP +# define KTHREAD_WORKER_INIT_ONSTACK (worker) \ + ({ init_kthread_worker(&worker); worker; }) +# define DEFINE_KTHREAD_WORKER_ONSTACK (worker) \ + struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker) +# define KTHREAD_WORK_INIT_ONSTACK (work, fn) \ + ({ init_kthread_work((&work), fn); work; }) +# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) \ + struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn) +#else +# define DEFINE_KTHREAD_WORKER_ONSTACK (worker) DEFINE_KTHREAD_WORKER(worker) +# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) DEFINE_KTHREAD_WORK(work, fn) +#endif + +extern void __init_kthread_worker (struct kthread_worker *worker, +struct lock_class_key *key); + +#define init_kthread_worker (worker) \ + do { \ + static struct lock_class_key __key; \ + __init_kthread_worker((worker), &__key); \ + } while (0) + +#define init_kthread_work (work, fn) \ + do { \ + memset((work), 0, sizeof(struct kthread_work)); \ + INIT_LIST_HEAD(&(work)->node); \ + (work)->func = (fn); \ + init_waitqueue_head(&(work)->done); \ + } while (0)int kthread_worker_fn (void *worker_ptr);
kernel/kthread.c +9 -0
@@ -265,6 +265,15 @@ int kthreadd(void *unused)return 0 ; } +void __init_kthread_worker (struct kthread_worker *worker, +struct lock_class_key *key) +{ +spin_lock_init (&worker->lock); +lockdep_set_class (&worker->lock, key); +INIT_LIST_HEAD (&worker->work_list); + worker->task =NULL ; +} +/** * kthread_worker_fn - kthread function to process kthread_worker * @worker_ptr: pointer to initialized kthread_worker
Signature · 1 line
1.7.0.4
Re: [V2 PATCH] kthread_work: Make lockdep happy
Hello,
Quoted 20 lines · Yong Zhang
From: Yong Zhang <[email protected]>
Subject: [V2 PATCH]kthread_work: Make lockdep happyspinlock in
kthread_workerandwait_queue_headinkthread_workboth should be lockdep sensible.
So change the interface to make it suiltable forCONFIG_LOCKDEP.Reported-by Nicolas <[email protected]>Signed-off-by Yong Zhang <[email protected]>Cc Tejun Heo <[email protected]>Cc Andrew Morton <[email protected]>Cc Andy Walls <[email protected]>---
Changes from V1:
*According to Tejun,kthread_workercould be defined on stack,
So introduceDEFINE_KTHREAD_WORKER_ONSTACK.
*Change wrong setting tokthread_work->task. Thanks Adny for
pointing it.
*including some minor issue.
Quoted 1 line
BTW, only passed build.
If somebody can confirm this makes lockdep behave correctly, I'll route it through the wq tree.
Thanks.
Signature · 1 line
tejun
Re: [V2 PATCH] kthread_work: Make lockdep happy
Quoted 2 lines · Tejun Heo
If somebody can confirm this makes lockdep behave correctly, I'll route it through the wq tree.
I can boot-test it if someone adds it to the rawhide kernel or a kernel that can be downloaded from koji (don't really have the time to build kernels right now)
Signature · 1 line
Nicolas Mailhot
Re: [V2 PATCH] kthread_work: Make lockdep happy
In reply to Tejun HeoQuoted 23 lines · Tejun Heo
Hello,
Quoted 20 lines · Yong Zhang
From: Yong Zhang <[email protected]>
Subject: [V2 PATCH]kthread_work: Make lockdep happyspinlock in
kthread_workerandwait_queue_headinkthread_workboth should be lockdep sensible.
So change the interface to make it suiltable forCONFIG_LOCKDEP.Reported-by Nicolas <[email protected]>Signed-off-by Yong Zhang <[email protected]>Cc Tejun Heo <[email protected]>Cc Andrew Morton <[email protected]>Cc Andy Walls <[email protected]>---
Changes from V1:
*According to Tejun,kthread_workercould be defined on stack,
So introduceDEFINE_KTHREAD_WORKER_ONSTACK.
*Change wrong setting tokthread_work->task. Thanks Adny for
pointing it.
*including some minor issue.
Quoted 4 lines
Quoted 1 line
BTW, only passed build.
If somebody can confirm this makes lockdep behave correctly, I'll route it through the wq tree.
I will attempt to test later tonight with at least one ivtv (PVR-350) card installed. If I have time, I'll test with two cards (PVR-350 and PVR-150) installed in the machine, since that will create two different kthread_workers, each with their own lock.
How can I dump information on lockdep spinlock tracking to verify that the two distinct locks are tracked separately by lockdep?
Regards,
Andy
Re: [V2 PATCH] kthread_work: Make lockdep happy
In reply to Yong ZhangQuoted 20 lines · Yong Zhang
From: Yong Zhang <[email protected]>
Subject: [V2 PATCH]kthread_work: Make lockdep happyspinlock in
kthread_workerandwait_queue_headinkthread_workboth should be lockdep sensible.
So change the interface to make it suiltable forCONFIG_LOCKDEP.Reported-by Nicolas <[email protected]>Signed-off-by Yong Zhang <[email protected]>Cc Tejun Heo <[email protected]>Cc Andrew Morton <[email protected]>Cc Andy Walls <[email protected]>---
Changes from V1:
*According to Tejun,kthread_workercould be defined on stack,
So introduceDEFINE_KTHREAD_WORKER_ONSTACK.
*Change wrong setting tokthread_work->task. Thanks Adny for
pointing it.
*including some minor issue.
Quoted 5 lines
BTW, only passed build.
include/linux/kthread.h | 45 +++++++++++++++++++++++++++++++++++---------- kernel/kthread.c | 9 +++++++++ 2 files changed, 44 insertions(+), 10 deletions(-)
Yong,
I have tested your patch with the following patch on top of yours. I find the two patches together acceptable in testing in a machine with both a PVR-350 card and a PVR-500 card installed.
Could you please author a [V3 PATCH] adding my changes? Since the majority of the change is your work, it should be attributed to you as the author.
About my additional changes:
I needed the explicit EXPORT_SYMBOL_GPL(__init_kthread_worker), otherwise the build would fail, because the ivtv module had an unresolved symbol.
I added the lockdep class name to make it easier to identify the ivtv module's kthread worker's lock class. Now one can actually recognize the lock class in lockdep output strings:
# cat lockdep | grep itv ffffffffa0483a60 FD: 1 BD: 1 ......: (&itv->irq_worker)->lock
Thanks again.
Regards,
Andy
include/linux/kthread.h +5 -3
@@ -100,13 +100,15 @@ struct kthread_work {# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) DEFINE_KTHREAD_WORK(work, fn)#endif -extern void __init_kthread_worker (struct kthread_worker *worker, -struct lock_class_key *key); +void __init_kthread_worker (struct kthread_worker *worker, +struct lock_class_key *key, +const char *lock_class_name);#define init_kthread_worker (worker) \ do { \ static struct lock_class_key __key; \ - __init_kthread_worker((worker), &__key); \ + __init_kthread_worker((worker), &__key, \ + "(" #worker ")->lock"); \ } while (0)#define init_kthread_work (work, fn) \
kernel/kthread.c +4 -2
@@ -266,13 +266,15 @@ int kthreadd(void *unused) }void __init_kthread_worker (struct kthread_worker *worker, -struct lock_class_key *key) +struct lock_class_key *key, +const char *lock_class_name) {spin_lock_init (&worker->lock); -lockdep_set_class (&worker->lock, key); +lockdep_set_class_and_name (&worker->lock, key, lock_class_name);INIT_LIST_HEAD (&worker->work_list); worker->task =NULL ; } +EXPORT_SYMBOL_GPL (__init_kthread_worker);/** * kthread_worker_fn - kthread function to process kthread_worker
Re: [V2 PATCH] kthread_work: Make lockdep happy
Quoted 10 lines · Andy Walls
Yong,
I have tested your patch with the following patch on top of yours. I find the two patches together acceptable in testing in a machine with both a PVR-350 card and a PVR-500 card installed.
Tested-by Andy Walls <[email protected]>Signed-off-by Andy Walls <[email protected]>Could you please author a [V3 PATCH] adding my changes?
Sure. Will do it.
Quoted 10 lines
Since the
majority of the change is your work, it should be attributed to you as the author.About my additional changes:
I needed the explicit
EXPORT_SYMBOL_GPL(__init_kthread_worker), otherwise the build would fail, because the ivtv module had an unresolved symbol.
Will update.
Quoted 7 lines
I added the lockdep class name to make it easier to identify the ivtv module's kthread worker's lock class. Now one can actually recognize the lock class in lockdep output strings:
# cat lockdep | grep itv ffffffffa0483a60 FD: 1 BD: 1 ......: (&itv->irq_worker)->lock
Yeah, I like your idea.
V3 coming soon :)
Thanks,
Yong
Signature · 1 line
Only stand for myself.
PATCH v3 1/1 kthread_work: Make lockdep happy
In reply to Andy WallsFrom: Yong Zhang <[email protected]>
Subject: [V3 PATCH] kthread_work: Make lockdep happy
spinlock in kthread_worker and wait_queue_head in kthread_work both should be lockdep sensible.
So change the interface to make it suiltable for CONFIG_LOCKDEP.
---
Cahgnes form V2:
*export __init_kthread_worker, fix module build issue. *explicitly name worker->lock, which will make debug more easy.
idea from Andy Walls.
Changes from V1:
*According to Tejun, kthread_worker could be defined on stack,
So introduce DEFINE_KTHREAD_WORKER_ONSTACK.
*Change wrong setting to kthread_work->task. Thanks Adny for
pointing it.
*including some minor issue.
include/linux/kthread.h | 45 +++++++++++++++++++++++++++++++++++---------- kernel/kthread.c | 11 +++++++++++ 2 files changed, 46 insertions(+), 10 deletions(-)
include/linux/kthread.h +35 -10
@@ -81,16 +81,41 @@ struct kthread_work {#define DEFINE_KTHREAD_WORK (work, fn) \ struct kthread_work work = KTHREAD_WORK_INIT(work, fn) -static inline void init_kthread_worker (struct kthread_worker *worker) -{ - *worker = (struct kthread_worker )KTHREAD_WORKER_INIT (*worker); -} - -static inline void init_kthread_work (struct kthread_work *work, -kthread_work_func_t fn) -{ - *work = (struct kthread_work )KTHREAD_WORK_INIT (*work, fn); -} +/* +* kthread_worker.lock and kthread_work.done need +* special work if they are defined on stack with +* lockdep enabled. +*/ +#ifdef CONFIG_LOCKDEP +# define KTHREAD_WORKER_INIT_ONSTACK (worker) \ + ({ init_kthread_worker(&worker); worker; }) +# define DEFINE_KTHREAD_WORKER_ONSTACK (worker) \ + struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker) +# define KTHREAD_WORK_INIT_ONSTACK (work, fn) \ + ({ init_kthread_work((&work), fn); work; }) +# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) \ + struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn) +#else +# define DEFINE_KTHREAD_WORKER_ONSTACK (worker) DEFINE_KTHREAD_WORKER(worker) +# define DEFINE_KTHREAD_WORK_ONSTACK (work, fn) DEFINE_KTHREAD_WORK(work, fn) +#endif + +extern void __init_kthread_worker (struct kthread_worker *worker, +const char *name,struct lock_class_key *key); + +#define init_kthread_worker (worker) \ + do { \ + static struct lock_class_key __key; \ + __init_kthread_worker((worker), "("#worker")->lock", &__key); \ + } while (0) + +#define init_kthread_work (work, fn) \ + do { \ + memset((work), 0, sizeof(struct kthread_work)); \ + INIT_LIST_HEAD(&(work)->node); \ + (work)->func = (fn); \ + init_waitqueue_head(&(work)->done); \ + } while (0)int kthread_worker_fn (void *worker_ptr);
kernel/kthread.c +11 -0
@@ -265,6 +265,17 @@ int kthreadd(void *unused)return 0 ; } +void __init_kthread_worker (struct kthread_worker *worker, +const char *name, +struct lock_class_key *key) +{ +spin_lock_init (&worker->lock); +lockdep_set_class_and_name (&worker->lock, key, name); +INIT_LIST_HEAD (&worker->work_list); + worker->task =NULL ; +} +EXPORT_SYMBOL_GPL (__init_kthread_worker); +/** * kthread_worker_fn - kthread function to process kthread_worker * @worker_ptr: pointer to initialized kthread_worker
Signature · 1 line
1.7.0.4
Re: [V3 PATCH] kthread_work: Make lockdep happy
Quoted 7 lines · Yong Zhang
From: Yong Zhang <[email protected]>
Subject: [V3 PATCH]kthread_work: Make lockdep happyspinlock in
kthread_workerandwait_queue_headinkthread_workboth should be lockdep sensible.
So change the interface to make it suiltable forCONFIG_LOCKDEP.
Quoted 6 lines
Reported-by Nicolas <[email protected]>Signed-off-by Yong Zhang <[email protected]>Signed-off-by Andy Walls <[email protected]>Tested-by Andy Walls <[email protected]>Cc Tejun Heo <[email protected]>Cc Andrew Morton <[email protected]>
Applied with the following comment update (because the term "special work" was a bit too confusing given the context) to wq#for-linus. Will push out to Linus in several days.
+/*
+ * kthread_worker.lock and kthread_work.done need their own lockdep class
+ * keys if they are defined on stack with lockdep enabled. Use the
+ * following macros when defining them on stack.
+ */
Thank you.
Signature · 1 line
tejun