blob: c1961761311dbfd5968d6ed64ea91ca3c7d25b0e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef _LINUX_KTHREAD_H
3#define _LINUX_KTHREAD_H
4/* Simple interface for creating and stopping kernel threads without mess. */
5#include <linux/err.h>
6#include <linux/sched.h>
Shaohua Li05e3db92017-09-14 14:02:04 -07007#include <linux/cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
Joe Perchesb9075fa2011-10-31 17:11:33 -07009__printf(4, 5)
Eric Dumazet207205a2011-03-22 16:30:44 -070010struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
11 void *data,
12 int node,
Joe Perchesb9075fa2011-10-31 17:11:33 -070013 const char namefmt[], ...);
Eric Dumazet207205a2011-03-22 16:30:44 -070014
Jonathan Corbete154ccc2016-10-11 13:55:53 -070015/**
16 * kthread_create - create a kthread on the current node
17 * @threadfn: the function to run in the thread
18 * @data: data pointer for @threadfn()
19 * @namefmt: printf-style format string for the thread name
Jonathan Corbetd16977f2017-08-02 13:32:01 -070020 * @arg...: arguments for @namefmt.
Jonathan Corbete154ccc2016-10-11 13:55:53 -070021 *
22 * This macro will create a kthread on the current node, leaving it in
23 * the stopped state. This is just a helper for kthread_create_on_node();
24 * see the documentation there for more details.
25 */
Eric Dumazet207205a2011-03-22 16:30:44 -070026#define kthread_create(threadfn, data, namefmt, arg...) \
Andrew Mortone9f06982015-09-04 15:42:42 -070027 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
Eric Dumazet207205a2011-03-22 16:30:44 -070028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000030struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
31 void *data,
32 unsigned int cpu,
33 const char *namefmt);
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -070036 * kthread_run - create and wake a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * @threadfn: the function to run until signal_pending(current).
38 * @data: data ptr for @threadfn.
39 * @namefmt: printf-style name for the thread.
40 *
41 * Description: Convenient wrapper for kthread_create() followed by
Randy Dunlap9e37bd32006-06-25 05:49:19 -070042 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
43 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define kthread_run(threadfn, data, namefmt, ...) \
45({ \
46 struct task_struct *__k \
47 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
48 if (!IS_ERR(__k)) \
49 wake_up_process(__k); \
50 __k; \
51})
52
Oleg Nesterov1da5c462016-11-29 18:50:57 +010053void free_kthread_struct(struct task_struct *k);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054void kthread_bind(struct task_struct *k, unsigned int cpu);
Peter Zijlstra25834c72015-05-15 17:43:34 +020055void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056int kthread_stop(struct task_struct *k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000057bool kthread_should_stop(void);
58bool kthread_should_park(void);
Tejun Heo8a32c442011-11-21 12:32:23 -080059bool kthread_freezable_should_stop(bool *was_frozen);
Tejun Heo82805ab2010-06-29 10:07:09 +020060void *kthread_data(struct task_struct *k);
Petr Mladeke7005912016-10-11 13:55:17 -070061void *kthread_probe_data(struct task_struct *k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000062int kthread_park(struct task_struct *k);
63void kthread_unpark(struct task_struct *k);
64void kthread_parkme(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Eric W. Biederman73c27992007-05-09 02:34:32 -070066int kthreadd(void *unused);
67extern struct task_struct *kthreadd_task;
Eric Dumazet207205a2011-03-22 16:30:44 -070068extern int tsk_fork_get_node(struct task_struct *tsk);
Eric W. Biederman73c27992007-05-09 02:34:32 -070069
Tejun Heob56c0d82010-06-29 10:07:09 +020070/*
71 * Simple work processor based on kthread.
72 *
73 * This provides easier way to make use of kthreads. A kthread_work
Petr Mladek39891442016-10-11 13:55:20 -070074 * can be queued and flushed using queue/kthread_flush_work()
Tejun Heob56c0d82010-06-29 10:07:09 +020075 * respectively. Queued kthread_works are processed by a kthread
76 * running kthread_worker_fn().
Tejun Heob56c0d82010-06-29 10:07:09 +020077 */
78struct kthread_work;
79typedef void (*kthread_work_func_t)(struct kthread_work *work);
Kees Cookfe5c3b62017-10-04 16:27:06 -070080void kthread_delayed_work_timer_fn(struct timer_list *t);
Tejun Heob56c0d82010-06-29 10:07:09 +020081
Petr Mladekdbf52682016-10-11 13:55:50 -070082enum {
83 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
84};
85
Tejun Heob56c0d82010-06-29 10:07:09 +020086struct kthread_worker {
Petr Mladekdbf52682016-10-11 13:55:50 -070087 unsigned int flags;
Tejun Heob56c0d82010-06-29 10:07:09 +020088 spinlock_t lock;
89 struct list_head work_list;
Petr Mladek22597dc2016-10-11 13:55:40 -070090 struct list_head delayed_work_list;
Tejun Heob56c0d82010-06-29 10:07:09 +020091 struct task_struct *task;
Tejun Heo46f3d972012-07-19 13:52:53 -070092 struct kthread_work *current_work;
Tejun Heob56c0d82010-06-29 10:07:09 +020093};
94
95struct kthread_work {
96 struct list_head node;
97 kthread_work_func_t func;
Tejun Heo46f3d972012-07-19 13:52:53 -070098 struct kthread_worker *worker;
Petr Mladek37be45d2016-10-11 13:55:43 -070099 /* Number of canceling calls that are running at the moment. */
100 int canceling;
Tejun Heob56c0d82010-06-29 10:07:09 +0200101};
102
Petr Mladek22597dc2016-10-11 13:55:40 -0700103struct kthread_delayed_work {
104 struct kthread_work work;
105 struct timer_list timer;
106};
107
Tejun Heob56c0d82010-06-29 10:07:09 +0200108#define KTHREAD_WORKER_INIT(worker) { \
Thomas Gleixner92578c0b2011-01-23 15:24:55 +0100109 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
Tejun Heob56c0d82010-06-29 10:07:09 +0200110 .work_list = LIST_HEAD_INIT((worker).work_list), \
Petr Mladek22597dc2016-10-11 13:55:40 -0700111 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
Tejun Heob56c0d82010-06-29 10:07:09 +0200112 }
113
114#define KTHREAD_WORK_INIT(work, fn) { \
115 .node = LIST_HEAD_INIT((work).node), \
116 .func = (fn), \
Tejun Heob56c0d82010-06-29 10:07:09 +0200117 }
118
Petr Mladek22597dc2016-10-11 13:55:40 -0700119#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
120 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
Kees Cook841b86f2017-10-23 09:40:42 +0200121 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
Petr Mladek22597dc2016-10-11 13:55:40 -0700122 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 Zhang4f32e9b2010-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 Zhang4f32e9b2010-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 Zhang4f32e9b2010-12-22 10:27:53 +0100142# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
143 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100144#else
145# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
Yong Zhang4f32e9b2010-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 Zhang4f32e9b2010-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 Zhang4f32e9b2010-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 Zhang4f32e9b2010-12-22 10:27:53 +0100155 } while (0)
156
Petr Mladek39891442016-10-11 13:55:20 -0700157#define kthread_init_work(work, fn) \
Yong Zhang4f32e9b2010-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 Zhang4f32e9b2010-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)); \
Kees Cook919b2502017-10-22 18:48:43 -0700167 __init_timer(&(dwork)->timer, \
168 kthread_delayed_work_timer_fn, \
169 TIMER_IRQSAFE); \
Petr Mladek22597dc2016-10-11 13:55:40 -0700170 } while (0)
171
Tejun Heob56c0d82010-06-29 10:07:09 +0200172int kthread_worker_fn(void *worker_ptr);
173
Petr Mladekdbf52682016-10-11 13:55:50 -0700174__printf(2, 3)
Petr Mladekfbae2d42016-10-11 13:55:30 -0700175struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700176kthread_create_worker(unsigned int flags, const char namefmt[], ...);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700177
Nicolas Ioossc0b942a2016-12-12 16:40:39 -0800178__printf(3, 4) struct kthread_worker *
Petr Mladekdbf52682016-10-11 13:55:50 -0700179kthread_create_worker_on_cpu(int cpu, unsigned int flags,
180 const char namefmt[], ...);
Petr Mladekfbae2d42016-10-11 13:55:30 -0700181
Petr Mladek39891442016-10-11 13:55:20 -0700182bool kthread_queue_work(struct kthread_worker *worker,
Tejun Heob56c0d82010-06-29 10:07:09 +0200183 struct kthread_work *work);
Petr Mladek22597dc2016-10-11 13:55:40 -0700184
185bool kthread_queue_delayed_work(struct kthread_worker *worker,
186 struct kthread_delayed_work *dwork,
187 unsigned long delay);
188
Petr Mladek9a6b06c2016-10-11 13:55:46 -0700189bool kthread_mod_delayed_work(struct kthread_worker *worker,
190 struct kthread_delayed_work *dwork,
191 unsigned long delay);
192
Petr Mladek39891442016-10-11 13:55:20 -0700193void kthread_flush_work(struct kthread_work *work);
194void kthread_flush_worker(struct kthread_worker *worker);
Tejun Heob56c0d82010-06-29 10:07:09 +0200195
Petr Mladek37be45d2016-10-11 13:55:43 -0700196bool kthread_cancel_work_sync(struct kthread_work *work);
197bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
198
Petr Mladek35033fe2016-10-11 13:55:33 -0700199void kthread_destroy_worker(struct kthread_worker *worker);
200
Shaohua Li0b508bc2017-09-26 11:02:12 -0700201#ifdef CONFIG_BLK_CGROUP
Shaohua Li05e3db92017-09-14 14:02:04 -0700202void kthread_associate_blkcg(struct cgroup_subsys_state *css);
203struct cgroup_subsys_state *kthread_blkcg(void);
204#else
205static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
206static inline struct cgroup_subsys_state *kthread_blkcg(void)
207{
208 return NULL;
209}
210#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#endif /* _LINUX_KTHREAD_H */