Skip to content

Commit 4f32e9b

Browse files
yongzhang0htejun
authored andcommitted
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. tj: comment update Reported-by: Nicolas <nicolas.mailhot@laposte.net> Signed-off-by: Yong Zhang <yong.zhang0@gmail.com> Signed-off-by: Andy Walls <awalls@md.metrocast.net> Tested-by: Andy Walls <awalls@md.metrocast.net> Cc: Tejun Heo <tj@kernel.org> Cc: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Tejun Heo <tj@kernel.org>
1 parent 90a8a73 commit 4f32e9b

2 files changed

Lines changed: 46 additions & 10 deletions

File tree

‎include/linux/kthread.h‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,41 @@ struct kthread_work {
8181
#define DEFINE_KTHREAD_WORK(work, fn) \
8282
struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
8383

84-
static inline void init_kthread_worker(struct kthread_worker *worker)
85-
{
86-
*worker = (struct kthread_worker)KTHREAD_WORKER_INIT(*worker);
87-
}
88-
89-
static inline void init_kthread_work(struct kthread_work *work,
90-
kthread_work_func_t fn)
91-
{
92-
*work = (struct kthread_work)KTHREAD_WORK_INIT(*work, fn);
93-
}
84+
/*
85+
* kthread_worker.lock and kthread_work.done need their own lockdep class
86+
* keys if they are defined on stack with lockdep enabled. Use the
87+
* following macros when defining them on stack.
88+
*/
89+
#ifdef CONFIG_LOCKDEP
90+
# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
91+
({ init_kthread_worker(&worker); worker; })
92+
# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
93+
struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
94+
# define KTHREAD_WORK_INIT_ONSTACK(work, fn) \
95+
({ init_kthread_work((&work), fn); work; })
96+
# define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) \
97+
struct kthread_work work = KTHREAD_WORK_INIT_ONSTACK(work, fn)
98+
#else
99+
# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
100+
# define DEFINE_KTHREAD_WORK_ONSTACK(work, fn) DEFINE_KTHREAD_WORK(work, fn)
101+
#endif
102+
103+
extern void __init_kthread_worker(struct kthread_worker *worker,
104+
const char *name, struct lock_class_key *key);
105+
106+
#define init_kthread_worker(worker) \
107+
do { \
108+
static struct lock_class_key __key; \
109+
__init_kthread_worker((worker), "("#worker")->lock", &__key); \
110+
} while (0)
111+
112+
#define init_kthread_work(work, fn) \
113+
do { \
114+
memset((work), 0, sizeof(struct kthread_work)); \
115+
INIT_LIST_HEAD(&(work)->node); \
116+
(work)->func = (fn); \
117+
init_waitqueue_head(&(work)->done); \
118+
} while (0)
94119

95120
int kthread_worker_fn(void *worker_ptr);
96121

‎kernel/kthread.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,17 @@ int kthreadd(void *unused)
265265
return 0;
266266
}
267267

268+
void __init_kthread_worker(struct kthread_worker *worker,
269+
const char *name,
270+
struct lock_class_key *key)
271+
{
272+
spin_lock_init(&worker->lock);
273+
lockdep_set_class_and_name(&worker->lock, key, name);
274+
INIT_LIST_HEAD(&worker->work_list);
275+
worker->task = NULL;
276+
}
277+
EXPORT_SYMBOL_GPL(__init_kthread_worker);
278+
268279
/**
269280
* kthread_worker_fn - kthread function to process kthread_worker
270281
* @worker_ptr: pointer to initialized kthread_worker

0 commit comments

Comments
 (0)