blob: a6e82a69c363bb3d7af21e358aaef84bc2f265aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _LINUX_KTHREAD_H
2#define _LINUX_KTHREAD_H
3/* Simple interface for creating and stopping kernel threads without mess. */
4#include <linux/err.h>
5#include <linux/sched.h>
6
Joe Perchesb9075fa2011-10-31 17:11:33 -07007__printf(4, 5)
Eric Dumazet207205a2011-03-22 16:30:44 -07008struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
9 void *data,
10 int node,
Joe Perchesb9075fa2011-10-31 17:11:33 -070011 const char namefmt[], ...);
Eric Dumazet207205a2011-03-22 16:30:44 -070012
Jonathan Corbete154ccc2016-10-11 13:55:53 -070013/**
14 * kthread_create - create a kthread on the current node
15 * @threadfn: the function to run in the thread
16 * @data: data pointer for @threadfn()
17 * @namefmt: printf-style format string for the thread name
18 * @...: arguments for @namefmt.
19 *
20 * This macro will create a kthread on the current node, leaving it in
21 * the stopped state. This is just a helper for kthread_create_on_node();
22 * see the documentation there for more details.
23 */
Eric Dumazet207205a2011-03-22 16:30:44 -070024#define kthread_create(threadfn, data, namefmt, arg...) \
Andrew Mortone9f06982015-09-04 15:42:42 -070025 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
Eric Dumazet207205a2011-03-22 16:30:44 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000028struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
29 void *data,
30 unsigned int cpu,
31 const char *namefmt);
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -070034 * kthread_run - create and wake a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * @threadfn: the function to run until signal_pending(current).
36 * @data: data ptr for @threadfn.
37 * @namefmt: printf-style name for the thread.
38 *
39 * Description: Convenient wrapper for kthread_create() followed by
Randy Dunlap9e37bd32006-06-25 05:49:19 -070040 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
41 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#define kthread_run(threadfn, data, namefmt, ...) \
43({ \
44 struct task_struct *__k \
45 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
46 if (!IS_ERR(__k)) \
47 wake_up_process(__k); \
48 __k; \
49})
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051void kthread_bind(struct task_struct *k, unsigned int cpu);
Peter Zijlstra25834c72015-05-15 17:43:34 +020052void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053int kthread_stop(struct task_struct *k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000054bool kthread_should_stop(void);
55bool kthread_should_park(void);
Tejun Heo8a32c442011-11-21 12:32:23 -080056bool kthread_freezable_should_stop(bool *was_frozen);
Tejun Heo82805ab2010-06-29 10:07:09 +020057void *kthread_data(struct task_struct *k);
Petr Mladeke7005912016-10-11 13:55:17 -070058void *kthread_probe_data(struct task_struct *k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000059int kthread_park(struct task_struct *k);
60void kthread_unpark(struct task_struct *k);
61void kthread_parkme(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Eric W. Biederman73c27992007-05-09 02:34:32 -070063int kthreadd(void *unused);
64extern struct task_struct *kthreadd_task;
Eric Dumazet207205a2011-03-22 16:30:44 -070065extern int tsk_fork_get_node(struct task_struct *tsk);
Eric W. Biederman73c27992007-05-09 02:34:32 -070066
Tejun Heob56c0d82010-06-29 10:07:09 +020067/*
68 * Simple work processor based on kthread.
69 *
70 * This provides easier way to make use of kthreads. A kthread_work
Petr Mladek39891442016-10-11 13:55:20 -070071 * can be queued and flushed using queue/kthread_flush_work()
Tejun Heob56c0d82010-06-29 10:07:09 +020072 * respectively. Queued kthread_works are processed by a kthread
73 * running kthread_worker_fn().
Tejun Heob56c0d82010-06-29 10:07:09 +020074 */
75struct kthread_work;
76typedef void (*kthread_work_func_t)(struct kthread_work *work);
Petr Mladek22597dc2016-10-11 13:55:40 -070077void kthread_delayed_work_timer_fn(unsigned long __data);
Tejun Heob56c0d82010-06-29 10:07:09 +020078
Petr Mladekdbf52682016-10-11 13:55:50 -070079enum {
80 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
81};
82
Tejun Heob56c0d82010-06-29 10:07:09 +020083struct kthread_worker {
Petr Mladekdbf52682016-10-11 13:55:50 -070084 unsigned int flags;
Tejun Heob56c0d82010-06-29 10:07:09 +020085 spinlock_t lock;
86 struct list_head work_list;
Petr Mladek22597dc2016-10-11 13:55:40 -070087 struct list_head delayed_work_list;
Tejun Heob56c0d82010-06-29 10:07:09 +020088 struct task_struct *task;
Tejun Heo46f3d972012-07-19 13:52:53 -070089 struct kthread_work *current_work;
Tejun Heob56c0d82010-06-29 10:07:09 +020090};
91
92struct kthread_work {
93 struct list_head node;
94 kthread_work_func_t func;
Tejun Heo46f3d972012-07-19 13:52:53 -070095 struct kthread_worker *worker;
Petr Mladek37be45d2016-10-11 13:55:43 -070096 /* Number of canceling calls that are running at the moment. */
97 int canceling;
Tejun Heob56c0d82010-06-29 10:07:09 +020098};
99
Petr Mladek22597dc2016-10-11 13:55:40 -0700100struct kthread_delayed_work {
101 struct kthread_work work;
102 struct timer_list timer;
103};
104
Tejun Heob56c0d82010-06-29 10:07:09 +0200105#define KTHREAD_WORKER_INIT(worker) { \
Thomas Gleixner92578c02011-01-23 15:24:55 +0100106 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
Tejun Heob56c0d82010-06-29 10:07:09 +0200107 .work_list = LIST_HEAD_INIT((worker).work_list), \
Petr Mladek22597dc2016-10-11 13:55:40 -0700108 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
Tejun Heob56c0d82010-06-29 10:07:09 +0200109 }
110
111#define KTHREAD_WORK_INIT(work, fn) { \
112 .node = LIST_HEAD_INIT((work).node), \
113 .func = (fn), \
Tejun Heob56c0d82010-06-29 10:07:09 +0200114 }
115
Petr Mladek22597dc2016-10-11 13:55:40 -0700116#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
117 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
118 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn, \
119 0, (unsigned long)&(dwork), \
120 TIMER_IRQSAFE), \
121 }
122
Tejun Heob56c0d82010-06-29 10:07:09 +0200123#define DEFINE_KTHREAD_WORKER(worker) \
124 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
125
126#define DEFINE_KTHREAD_WORK(work, fn) \
127 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
128
Petr Mladek22597dc2016-10-11 13:55:40 -0700129#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
130 struct kthread_delayed_work dwork = \
131 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
132
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100133/*
Lai Jiangshan95847e12014-07-26 12:04:00 +0800134 * kthread_worker.lock needs its own lockdep class key when defined on
135 * stack with lockdep enabled. Use the following macros in such cases.
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100136 */
137#ifdef CONFIG_LOCKDEP
138# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
Petr Mladek39891442016-10-11 13:55:20 -0700139 ({ kthread_init_worker(&worker); worker; })
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100140# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
141 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100142#else
143# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100144#endif
Tejun Heob56c0d82010-06-29 10:07:09 +0200145
Petr Mladek39891442016-10-11 13:55:20 -0700146extern void __kthread_init_worker(struct kthread_worker *worker,
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100147 const char *name, struct lock_class_key *key);
148
Petr Mladek39891442016-10-11 13:55:20 -0700149#define kthread_init_worker(worker) \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100150 do { \
151 static struct lock_class_key __key; \
Petr Mladek39891442016-10-11 13:55:20 -0700152 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100153 } while (0)
154
Petr Mladek39891442016-10-11 13:55:20 -0700155#define kthread_init_work(work, fn) \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100156 do { \
157 memset((work), 0, sizeof(struct kthread_work)); \
158 INIT_LIST_HEAD(&(work)->node); \
159 (work)->func = (fn); \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100160 } while (0)
Tejun Heob56c0d82010-06-29 10:07:09 +0200161
Petr Mladek22597dc2016-10-11 13:55:40 -0700162#define kthread_init_delayed_work(dwork, fn) \
163 do { \
164 kthread_init_work(&(dwork)->work, (fn)); \
165 __setup_timer(&(dwork)->timer, \
166 kthread_delayed_work_timer_fn, \
167 (unsigned long)(dwork), \
168 TIMER_IRQSAFE); \
169 } while (0)
170
Tejun Heob56c0d82010-06-29 10:07:09 +0200171int kthread_worker_fn(void *worker_ptr);
172
Petr Mladekdbf52682016-10-11 13:55:50 -0700173__printf(2, 3)
Petr Mladekfbae2d42016-10-11 13:55:30 -0700174struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700175kthread_create_worker(unsigned int flags, const char namefmt[], ...);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700176
177struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700178kthread_create_worker_on_cpu(int cpu, unsigned int flags,
179 const char namefmt[], ...);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700180
Petr Mladek39891442016-10-11 13:55:20 -0700181bool kthread_queue_work(struct kthread_worker *worker,
Tejun Heob56c0d82010-06-29 10:07:09 +0200182 struct kthread_work *work);
Petr Mladek22597dc2016-10-11 13:55:40 -0700183
184bool kthread_queue_delayed_work(struct kthread_worker *worker,
185 struct kthread_delayed_work *dwork,
186 unsigned long delay);
187
Petr Mladek9a6b06c2016-10-11 13:55:46 -0700188bool kthread_mod_delayed_work(struct kthread_worker *worker,
189 struct kthread_delayed_work *dwork,
190 unsigned long delay);
191
Petr Mladek39891442016-10-11 13:55:20 -0700192void kthread_flush_work(struct kthread_work *work);
193void kthread_flush_worker(struct kthread_worker *worker);
Tejun Heob56c0d82010-06-29 10:07:09 +0200194
Petr Mladek37be45d2016-10-11 13:55:43 -0700195bool kthread_cancel_work_sync(struct kthread_work *work);
196bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
197
Petr Mladek35033fe2016-10-11 13:55:33 -0700198void kthread_destroy_worker(struct kthread_worker *worker);
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#endif /* _LINUX_KTHREAD_H */