blob: bd4369c83dfba7221d23dc25a37332d9efb961f0 [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>
Shaohua Li05e3db92017-09-14 14:02:04 -07006#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007
Joe Perchesb9075fa2011-10-31 17:11:33 -07008__printf(4, 5)
Eric Dumazet207205a2011-03-22 16:30:44 -07009struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
10 void *data,
11 int node,
Joe Perchesb9075fa2011-10-31 17:11:33 -070012 const char namefmt[], ...);
Eric Dumazet207205a2011-03-22 16:30:44 -070013
Jonathan Corbete154ccc2016-10-11 13:55:53 -070014/**
15 * kthread_create - create a kthread on the current node
16 * @threadfn: the function to run in the thread
17 * @data: data pointer for @threadfn()
18 * @namefmt: printf-style format string for the thread name
Jonathan Corbetd16977f2017-08-02 13:32:01 -070019 * @arg...: arguments for @namefmt.
Jonathan Corbete154ccc2016-10-11 13:55:53 -070020 *
21 * This macro will create a kthread on the current node, leaving it in
22 * the stopped state. This is just a helper for kthread_create_on_node();
23 * see the documentation there for more details.
24 */
Eric Dumazet207205a2011-03-22 16:30:44 -070025#define kthread_create(threadfn, data, namefmt, arg...) \
Andrew Mortone9f06982015-09-04 15:42:42 -070026 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
Eric Dumazet207205a2011-03-22 16:30:44 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000029struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
30 void *data,
31 unsigned int cpu,
32 const char *namefmt);
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -070035 * kthread_run - create and wake a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * @threadfn: the function to run until signal_pending(current).
37 * @data: data ptr for @threadfn.
38 * @namefmt: printf-style name for the thread.
39 *
40 * Description: Convenient wrapper for kthread_create() followed by
Randy Dunlap9e37bd32006-06-25 05:49:19 -070041 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
42 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#define kthread_run(threadfn, data, namefmt, ...) \
44({ \
45 struct task_struct *__k \
46 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
47 if (!IS_ERR(__k)) \
48 wake_up_process(__k); \
49 __k; \
50})
51
Oleg Nesterov1da5c462016-11-29 18:50:57 +010052void free_kthread_struct(struct task_struct *k);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053void kthread_bind(struct task_struct *k, unsigned int cpu);
Peter Zijlstra25834c72015-05-15 17:43:34 +020054void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055int kthread_stop(struct task_struct *k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000056bool kthread_should_stop(void);
57bool kthread_should_park(void);
Tejun Heo8a32c442011-11-21 12:32:23 -080058bool kthread_freezable_should_stop(bool *was_frozen);
Tejun Heo82805ab2010-06-29 10:07:09 +020059void *kthread_data(struct task_struct *k);
Petr Mladeke7005912016-10-11 13:55:17 -070060void *kthread_probe_data(struct task_struct *k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000061int kthread_park(struct task_struct *k);
62void kthread_unpark(struct task_struct *k);
63void kthread_parkme(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Eric W. Biederman73c27992007-05-09 02:34:32 -070065int kthreadd(void *unused);
66extern struct task_struct *kthreadd_task;
Eric Dumazet207205a2011-03-22 16:30:44 -070067extern int tsk_fork_get_node(struct task_struct *tsk);
Eric W. Biederman73c27992007-05-09 02:34:32 -070068
Tejun Heob56c0d82010-06-29 10:07:09 +020069/*
70 * Simple work processor based on kthread.
71 *
72 * This provides easier way to make use of kthreads. A kthread_work
Petr Mladek39891442016-10-11 13:55:20 -070073 * can be queued and flushed using queue/kthread_flush_work()
Tejun Heob56c0d82010-06-29 10:07:09 +020074 * respectively. Queued kthread_works are processed by a kthread
75 * running kthread_worker_fn().
Tejun Heob56c0d82010-06-29 10:07:09 +020076 */
77struct kthread_work;
78typedef void (*kthread_work_func_t)(struct kthread_work *work);
Petr Mladek22597dc2016-10-11 13:55:40 -070079void kthread_delayed_work_timer_fn(unsigned long __data);
Tejun Heob56c0d82010-06-29 10:07:09 +020080
Petr Mladekdbf52682016-10-11 13:55:50 -070081enum {
82 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
83};
84
Tejun Heob56c0d82010-06-29 10:07:09 +020085struct kthread_worker {
Petr Mladekdbf52682016-10-11 13:55:50 -070086 unsigned int flags;
Tejun Heob56c0d82010-06-29 10:07:09 +020087 spinlock_t lock;
88 struct list_head work_list;
Petr Mladek22597dc2016-10-11 13:55:40 -070089 struct list_head delayed_work_list;
Tejun Heob56c0d82010-06-29 10:07:09 +020090 struct task_struct *task;
Tejun Heo46f3d972012-07-19 13:52:53 -070091 struct kthread_work *current_work;
Tejun Heob56c0d82010-06-29 10:07:09 +020092};
93
94struct kthread_work {
95 struct list_head node;
96 kthread_work_func_t func;
Tejun Heo46f3d972012-07-19 13:52:53 -070097 struct kthread_worker *worker;
Petr Mladek37be45d2016-10-11 13:55:43 -070098 /* Number of canceling calls that are running at the moment. */
99 int canceling;
Tejun Heob56c0d82010-06-29 10:07:09 +0200100};
101
Petr Mladek22597dc2016-10-11 13:55:40 -0700102struct kthread_delayed_work {
103 struct kthread_work work;
104 struct timer_list timer;
105};
106
Tejun Heob56c0d82010-06-29 10:07:09 +0200107#define KTHREAD_WORKER_INIT(worker) { \
Thomas Gleixner92578c0b2011-01-23 15:24:55 +0100108 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
Tejun Heob56c0d82010-06-29 10:07:09 +0200109 .work_list = LIST_HEAD_INIT((worker).work_list), \
Petr Mladek22597dc2016-10-11 13:55:40 -0700110 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
Tejun Heob56c0d82010-06-29 10:07:09 +0200111 }
112
113#define KTHREAD_WORK_INIT(work, fn) { \
114 .node = LIST_HEAD_INIT((work).node), \
115 .func = (fn), \
Tejun Heob56c0d82010-06-29 10:07:09 +0200116 }
117
Petr Mladek22597dc2016-10-11 13:55:40 -0700118#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
119 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
120 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn, \
121 0, (unsigned long)&(dwork), \
122 TIMER_IRQSAFE), \
123 }
124
Tejun Heob56c0d82010-06-29 10:07:09 +0200125#define DEFINE_KTHREAD_WORKER(worker) \
126 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
127
128#define DEFINE_KTHREAD_WORK(work, fn) \
129 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
130
Petr Mladek22597dc2016-10-11 13:55:40 -0700131#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
132 struct kthread_delayed_work dwork = \
133 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
134
Yong Zhang4f32e9b12010-12-22 10:27:53 +0100135/*
Lai Jiangshan95847e12014-07-26 12:04:00 +0800136 * kthread_worker.lock needs its own lockdep class key when defined on
137 * stack with lockdep enabled. Use the following macros in such cases.
Yong Zhang4f32e9b12010-12-22 10:27:53 +0100138 */
139#ifdef CONFIG_LOCKDEP
140# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
Petr Mladek39891442016-10-11 13:55:20 -0700141 ({ kthread_init_worker(&worker); worker; })
Yong Zhang4f32e9b12010-12-22 10:27:53 +0100142# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
143 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
Yong Zhang4f32e9b12010-12-22 10:27:53 +0100144#else
145# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
Yong Zhang4f32e9b12010-12-22 10:27:53 +0100146#endif
Tejun Heob56c0d82010-06-29 10:07:09 +0200147
Petr Mladek39891442016-10-11 13:55:20 -0700148extern void __kthread_init_worker(struct kthread_worker *worker,
Yong Zhang4f32e9b12010-12-22 10:27:53 +0100149 const char *name, struct lock_class_key *key);
150
Petr Mladek39891442016-10-11 13:55:20 -0700151#define kthread_init_worker(worker) \
Yong Zhang4f32e9b12010-12-22 10:27:53 +0100152 do { \
153 static struct lock_class_key __key; \
Petr Mladek39891442016-10-11 13:55:20 -0700154 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
Yong Zhang4f32e9b12010-12-22 10:27:53 +0100155 } while (0)
156
Petr Mladek39891442016-10-11 13:55:20 -0700157#define kthread_init_work(work, fn) \
Yong Zhang4f32e9b12010-12-22 10:27:53 +0100158 do { \
159 memset((work), 0, sizeof(struct kthread_work)); \
160 INIT_LIST_HEAD(&(work)->node); \
161 (work)->func = (fn); \
Yong Zhang4f32e9b12010-12-22 10:27:53 +0100162 } while (0)
Tejun Heob56c0d82010-06-29 10:07:09 +0200163
Petr Mladek22597dc2016-10-11 13:55:40 -0700164#define kthread_init_delayed_work(dwork, fn) \
165 do { \
166 kthread_init_work(&(dwork)->work, (fn)); \
167 __setup_timer(&(dwork)->timer, \
168 kthread_delayed_work_timer_fn, \
169 (unsigned long)(dwork), \
170 TIMER_IRQSAFE); \
171 } while (0)
172
Tejun Heob56c0d82010-06-29 10:07:09 +0200173int kthread_worker_fn(void *worker_ptr);
174
Petr Mladekdbf52682016-10-11 13:55:50 -0700175__printf(2, 3)
Petr Mladekfbae2d42016-10-11 13:55:30 -0700176struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700177kthread_create_worker(unsigned int flags, const char namefmt[], ...);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700178
Nicolas Ioossc0b942a2016-12-12 16:40:39 -0800179__printf(3, 4) struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700180kthread_create_worker_on_cpu(int cpu, unsigned int flags,
181 const char namefmt[], ...);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700182
Petr Mladek39891442016-10-11 13:55:20 -0700183bool kthread_queue_work(struct kthread_worker *worker,
Tejun Heob56c0d82010-06-29 10:07:09 +0200184 struct kthread_work *work);
Petr Mladek22597dc2016-10-11 13:55:40 -0700185
186bool kthread_queue_delayed_work(struct kthread_worker *worker,
187 struct kthread_delayed_work *dwork,
188 unsigned long delay);
189
Petr Mladek9a6b06c2016-10-11 13:55:46 -0700190bool kthread_mod_delayed_work(struct kthread_worker *worker,
191 struct kthread_delayed_work *dwork,
192 unsigned long delay);
193
Petr Mladek39891442016-10-11 13:55:20 -0700194void kthread_flush_work(struct kthread_work *work);
195void kthread_flush_worker(struct kthread_worker *worker);
Tejun Heob56c0d82010-06-29 10:07:09 +0200196
Petr Mladek37be45d2016-10-11 13:55:43 -0700197bool kthread_cancel_work_sync(struct kthread_work *work);
198bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
199
Petr Mladek35033fe2016-10-11 13:55:33 -0700200void kthread_destroy_worker(struct kthread_worker *worker);
201
Shaohua Li05e3db92017-09-14 14:02:04 -0700202#ifdef CONFIG_CGROUPS
203void kthread_associate_blkcg(struct cgroup_subsys_state *css);
204struct cgroup_subsys_state *kthread_blkcg(void);
205#else
206static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
207static inline struct cgroup_subsys_state *kthread_blkcg(void)
208{
209 return NULL;
210}
211#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#endif /* _LINUX_KTHREAD_H */