blob: 1577a2d56e9d4f1a70e80f7916650afe9b9a37bc [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);
Matthias Kaehlcke01218052019-01-28 15:46:24 -080059bool __kthread_should_park(struct task_struct *k);
Tejun Heo8a32c442011-11-21 12:32:23 -080060bool kthread_freezable_should_stop(bool *was_frozen);
Tejun Heo82805ab2010-06-29 10:07:09 +020061void *kthread_data(struct task_struct *k);
Petr Mladeke7005912016-10-11 13:55:17 -070062void *kthread_probe_data(struct task_struct *k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000063int kthread_park(struct task_struct *k);
64void kthread_unpark(struct task_struct *k);
65void kthread_parkme(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Eric W. Biederman73c27992007-05-09 02:34:32 -070067int kthreadd(void *unused);
68extern struct task_struct *kthreadd_task;
Eric Dumazet207205a2011-03-22 16:30:44 -070069extern int tsk_fork_get_node(struct task_struct *tsk);
Eric W. Biederman73c27992007-05-09 02:34:32 -070070
Tejun Heob56c0d82010-06-29 10:07:09 +020071/*
72 * Simple work processor based on kthread.
73 *
74 * This provides easier way to make use of kthreads. A kthread_work
Petr Mladek39891442016-10-11 13:55:20 -070075 * can be queued and flushed using queue/kthread_flush_work()
Tejun Heob56c0d82010-06-29 10:07:09 +020076 * respectively. Queued kthread_works are processed by a kthread
77 * running kthread_worker_fn().
Tejun Heob56c0d82010-06-29 10:07:09 +020078 */
79struct kthread_work;
80typedef void (*kthread_work_func_t)(struct kthread_work *work);
Kees Cookfe5c3b62017-10-04 16:27:06 -070081void kthread_delayed_work_timer_fn(struct timer_list *t);
Tejun Heob56c0d82010-06-29 10:07:09 +020082
Petr Mladekdbf52682016-10-11 13:55:50 -070083enum {
84 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
85};
86
Tejun Heob56c0d82010-06-29 10:07:09 +020087struct kthread_worker {
Petr Mladekdbf52682016-10-11 13:55:50 -070088 unsigned int flags;
Tejun Heob56c0d82010-06-29 10:07:09 +020089 spinlock_t lock;
90 struct list_head work_list;
Petr Mladek22597dc2016-10-11 13:55:40 -070091 struct list_head delayed_work_list;
Tejun Heob56c0d82010-06-29 10:07:09 +020092 struct task_struct *task;
Tejun Heo46f3d972012-07-19 13:52:53 -070093 struct kthread_work *current_work;
Tejun Heob56c0d82010-06-29 10:07:09 +020094};
95
96struct kthread_work {
97 struct list_head node;
98 kthread_work_func_t func;
Tejun Heo46f3d972012-07-19 13:52:53 -070099 struct kthread_worker *worker;
Petr Mladek37be45d2016-10-11 13:55:43 -0700100 /* Number of canceling calls that are running at the moment. */
101 int canceling;
Tejun Heob56c0d82010-06-29 10:07:09 +0200102};
103
Petr Mladek22597dc2016-10-11 13:55:40 -0700104struct kthread_delayed_work {
105 struct kthread_work work;
106 struct timer_list timer;
107};
108
Tejun Heob56c0d82010-06-29 10:07:09 +0200109#define KTHREAD_WORKER_INIT(worker) { \
Thomas Gleixner92578c0b2011-01-23 15:24:55 +0100110 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
Tejun Heob56c0d82010-06-29 10:07:09 +0200111 .work_list = LIST_HEAD_INIT((worker).work_list), \
Petr Mladek22597dc2016-10-11 13:55:40 -0700112 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
Tejun Heob56c0d82010-06-29 10:07:09 +0200113 }
114
115#define KTHREAD_WORK_INIT(work, fn) { \
116 .node = LIST_HEAD_INIT((work).node), \
117 .func = (fn), \
Tejun Heob56c0d82010-06-29 10:07:09 +0200118 }
119
Petr Mladek22597dc2016-10-11 13:55:40 -0700120#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
121 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
Kees Cook841b86f2017-10-23 09:40:42 +0200122 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
Petr Mladek22597dc2016-10-11 13:55:40 -0700123 TIMER_IRQSAFE), \
124 }
125
Tejun Heob56c0d82010-06-29 10:07:09 +0200126#define DEFINE_KTHREAD_WORKER(worker) \
127 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
128
129#define DEFINE_KTHREAD_WORK(work, fn) \
130 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
131
Petr Mladek22597dc2016-10-11 13:55:40 -0700132#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
133 struct kthread_delayed_work dwork = \
134 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
135
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100136/*
Lai Jiangshan95847e12014-07-26 12:04:00 +0800137 * kthread_worker.lock needs its own lockdep class key when defined on
138 * stack with lockdep enabled. Use the following macros in such cases.
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100139 */
140#ifdef CONFIG_LOCKDEP
141# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
Petr Mladek39891442016-10-11 13:55:20 -0700142 ({ kthread_init_worker(&worker); worker; })
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100143# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
144 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100145#else
146# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100147#endif
Tejun Heob56c0d82010-06-29 10:07:09 +0200148
Petr Mladek39891442016-10-11 13:55:20 -0700149extern void __kthread_init_worker(struct kthread_worker *worker,
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100150 const char *name, struct lock_class_key *key);
151
Petr Mladek39891442016-10-11 13:55:20 -0700152#define kthread_init_worker(worker) \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100153 do { \
154 static struct lock_class_key __key; \
Petr Mladek39891442016-10-11 13:55:20 -0700155 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100156 } while (0)
157
Petr Mladek39891442016-10-11 13:55:20 -0700158#define kthread_init_work(work, fn) \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100159 do { \
160 memset((work), 0, sizeof(struct kthread_work)); \
161 INIT_LIST_HEAD(&(work)->node); \
162 (work)->func = (fn); \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100163 } while (0)
Tejun Heob56c0d82010-06-29 10:07:09 +0200164
Petr Mladek22597dc2016-10-11 13:55:40 -0700165#define kthread_init_delayed_work(dwork, fn) \
166 do { \
167 kthread_init_work(&(dwork)->work, (fn)); \
Kees Cook919b2502017-10-22 18:48:43 -0700168 __init_timer(&(dwork)->timer, \
169 kthread_delayed_work_timer_fn, \
170 TIMER_IRQSAFE); \
Petr Mladek22597dc2016-10-11 13:55:40 -0700171 } 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 Li0b508bc2017-09-26 11:02:12 -0700202#ifdef CONFIG_BLK_CGROUP
Shaohua Li05e3db92017-09-14 14:02:04 -0700203void 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 */