blob: c7108ce5a051c4f8294795a3ad4f32d4650563ab [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001/* SPDX-License-Identifier: GPL-2.0 */
2#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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007
8__printf(4, 5)
9struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
10 void *data,
11 int node,
12 const char namefmt[], ...);
13
14/**
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
19 * @arg...: arguments for @namefmt.
20 *
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 */
25#define kthread_create(threadfn, data, namefmt, arg...) \
26 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
27
28
29struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
30 void *data,
31 unsigned int cpu,
32 const char *namefmt);
33
Olivier Deprez0e641232021-09-23 10:07:05 +020034void kthread_set_per_cpu(struct task_struct *k, int cpu);
35bool kthread_is_per_cpu(struct task_struct *k);
36
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037/**
38 * kthread_run - create and wake a thread.
39 * @threadfn: the function to run until signal_pending(current).
40 * @data: data ptr for @threadfn.
41 * @namefmt: printf-style name for the thread.
42 *
43 * Description: Convenient wrapper for kthread_create() followed by
44 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
45 */
46#define kthread_run(threadfn, data, namefmt, ...) \
47({ \
48 struct task_struct *__k \
49 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
50 if (!IS_ERR(__k)) \
51 wake_up_process(__k); \
52 __k; \
53})
54
55void free_kthread_struct(struct task_struct *k);
56void kthread_bind(struct task_struct *k, unsigned int cpu);
57void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
58int kthread_stop(struct task_struct *k);
59bool kthread_should_stop(void);
60bool kthread_should_park(void);
David Brazdil0f672f62019-12-10 10:32:29 +000061bool __kthread_should_park(struct task_struct *k);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000062bool kthread_freezable_should_stop(bool *was_frozen);
63void *kthread_data(struct task_struct *k);
64void *kthread_probe_data(struct task_struct *k);
65int kthread_park(struct task_struct *k);
66void kthread_unpark(struct task_struct *k);
67void kthread_parkme(void);
68
69int kthreadd(void *unused);
70extern struct task_struct *kthreadd_task;
71extern int tsk_fork_get_node(struct task_struct *tsk);
72
73/*
74 * Simple work processor based on kthread.
75 *
76 * This provides easier way to make use of kthreads. A kthread_work
77 * can be queued and flushed using queue/kthread_flush_work()
78 * respectively. Queued kthread_works are processed by a kthread
79 * running kthread_worker_fn().
80 */
81struct kthread_work;
82typedef void (*kthread_work_func_t)(struct kthread_work *work);
83void kthread_delayed_work_timer_fn(struct timer_list *t);
84
85enum {
86 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
87};
88
89struct kthread_worker {
90 unsigned int flags;
David Brazdil0f672f62019-12-10 10:32:29 +000091 raw_spinlock_t lock;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092 struct list_head work_list;
93 struct list_head delayed_work_list;
94 struct task_struct *task;
95 struct kthread_work *current_work;
96};
97
98struct kthread_work {
99 struct list_head node;
100 kthread_work_func_t func;
101 struct kthread_worker *worker;
102 /* Number of canceling calls that are running at the moment. */
103 int canceling;
104};
105
106struct kthread_delayed_work {
107 struct kthread_work work;
108 struct timer_list timer;
109};
110
111#define KTHREAD_WORKER_INIT(worker) { \
David Brazdil0f672f62019-12-10 10:32:29 +0000112 .lock = __RAW_SPIN_LOCK_UNLOCKED((worker).lock), \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000113 .work_list = LIST_HEAD_INIT((worker).work_list), \
114 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
115 }
116
117#define KTHREAD_WORK_INIT(work, fn) { \
118 .node = LIST_HEAD_INIT((work).node), \
119 .func = (fn), \
120 }
121
122#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
123 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
124 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
125 TIMER_IRQSAFE), \
126 }
127
128#define DEFINE_KTHREAD_WORKER(worker) \
129 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
130
131#define DEFINE_KTHREAD_WORK(work, fn) \
132 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
133
134#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
135 struct kthread_delayed_work dwork = \
136 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
137
138/*
139 * kthread_worker.lock needs its own lockdep class key when defined on
140 * stack with lockdep enabled. Use the following macros in such cases.
141 */
142#ifdef CONFIG_LOCKDEP
143# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
144 ({ kthread_init_worker(&worker); worker; })
145# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
146 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
147#else
148# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
149#endif
150
151extern void __kthread_init_worker(struct kthread_worker *worker,
152 const char *name, struct lock_class_key *key);
153
154#define kthread_init_worker(worker) \
155 do { \
156 static struct lock_class_key __key; \
157 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
158 } while (0)
159
160#define kthread_init_work(work, fn) \
161 do { \
162 memset((work), 0, sizeof(struct kthread_work)); \
163 INIT_LIST_HEAD(&(work)->node); \
164 (work)->func = (fn); \
165 } while (0)
166
167#define kthread_init_delayed_work(dwork, fn) \
168 do { \
169 kthread_init_work(&(dwork)->work, (fn)); \
David Brazdil0f672f62019-12-10 10:32:29 +0000170 timer_setup(&(dwork)->timer, \
171 kthread_delayed_work_timer_fn, 0); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172 } while (0)
173
174int kthread_worker_fn(void *worker_ptr);
175
176__printf(2, 3)
177struct kthread_worker *
178kthread_create_worker(unsigned int flags, const char namefmt[], ...);
179
180__printf(3, 4) struct kthread_worker *
181kthread_create_worker_on_cpu(int cpu, unsigned int flags,
182 const char namefmt[], ...);
183
184bool kthread_queue_work(struct kthread_worker *worker,
185 struct kthread_work *work);
186
187bool kthread_queue_delayed_work(struct kthread_worker *worker,
188 struct kthread_delayed_work *dwork,
189 unsigned long delay);
190
191bool kthread_mod_delayed_work(struct kthread_worker *worker,
192 struct kthread_delayed_work *dwork,
193 unsigned long delay);
194
195void kthread_flush_work(struct kthread_work *work);
196void kthread_flush_worker(struct kthread_worker *worker);
197
198bool kthread_cancel_work_sync(struct kthread_work *work);
199bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
200
201void kthread_destroy_worker(struct kthread_worker *worker);
202
David Brazdil0f672f62019-12-10 10:32:29 +0000203struct cgroup_subsys_state;
204
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000205#ifdef CONFIG_BLK_CGROUP
206void kthread_associate_blkcg(struct cgroup_subsys_state *css);
207struct cgroup_subsys_state *kthread_blkcg(void);
208#else
209static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
210static inline struct cgroup_subsys_state *kthread_blkcg(void)
211{
212 return NULL;
213}
214#endif
215#endif /* _LINUX_KTHREAD_H */