]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/kthread.h
rfkill: allocate static minor
[mirror_ubuntu-bionic-kernel.git] / include / linux / kthread.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4
LT
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>
05e3db95 7#include <linux/cgroup.h>
1da177e4 8
b9075fa9 9__printf(4, 5)
207205a2
ED
10struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
11 void *data,
12 int node,
b9075fa9 13 const char namefmt[], ...);
207205a2 14
e154ccc8
JC
15/**
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
d16977f3 20 * @arg...: arguments for @namefmt.
e154ccc8
JC
21 *
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 */
207205a2 26#define kthread_create(threadfn, data, namefmt, arg...) \
e9f06986 27 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
207205a2 28
1da177e4 29
2a1d4460
TG
30struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
31 void *data,
32 unsigned int cpu,
33 const char *namefmt);
34
1da177e4 35/**
9e37bd30 36 * kthread_run - create and wake a thread.
1da177e4
LT
37 * @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
9e37bd30
RD
42 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
43 */
1da177e4
LT
44#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
1da5c46f 53void free_kthread_struct(struct task_struct *k);
1da177e4 54void kthread_bind(struct task_struct *k, unsigned int cpu);
25834c73 55void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
1da177e4 56int kthread_stop(struct task_struct *k);
2a1d4460
TG
57bool kthread_should_stop(void);
58bool kthread_should_park(void);
8a32c441 59bool kthread_freezable_should_stop(bool *was_frozen);
82805ab7 60void *kthread_data(struct task_struct *k);
e700591a 61void *kthread_probe_data(struct task_struct *k);
2a1d4460
TG
62int kthread_park(struct task_struct *k);
63void kthread_unpark(struct task_struct *k);
64void kthread_parkme(void);
605478e2 65void kthread_park_complete(struct task_struct *k);
1da177e4 66
73c27992
EB
67int kthreadd(void *unused);
68extern struct task_struct *kthreadd_task;
207205a2 69extern int tsk_fork_get_node(struct task_struct *tsk);
73c27992 70
b56c0d89
TH
71/*
72 * Simple work processor based on kthread.
73 *
74 * This provides easier way to make use of kthreads. A kthread_work
3989144f 75 * can be queued and flushed using queue/kthread_flush_work()
b56c0d89
TH
76 * respectively. Queued kthread_works are processed by a kthread
77 * running kthread_worker_fn().
b56c0d89
TH
78 */
79struct kthread_work;
80typedef void (*kthread_work_func_t)(struct kthread_work *work);
fe5c3b69 81void kthread_delayed_work_timer_fn(struct timer_list *t);
b56c0d89 82
dbf52682
PM
83enum {
84 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
85};
86
b56c0d89 87struct kthread_worker {
dbf52682 88 unsigned int flags;
b56c0d89
TH
89 spinlock_t lock;
90 struct list_head work_list;
22597dc3 91 struct list_head delayed_work_list;
b56c0d89 92 struct task_struct *task;
46f3d976 93 struct kthread_work *current_work;
b56c0d89
TH
94};
95
96struct kthread_work {
97 struct list_head node;
98 kthread_work_func_t func;
46f3d976 99 struct kthread_worker *worker;
37be45d4
PM
100 /* Number of canceling calls that are running at the moment. */
101 int canceling;
b56c0d89
TH
102};
103
22597dc3
PM
104struct kthread_delayed_work {
105 struct kthread_work work;
106 struct timer_list timer;
107};
108
b56c0d89 109#define KTHREAD_WORKER_INIT(worker) { \
92578c0b 110 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
b56c0d89 111 .work_list = LIST_HEAD_INIT((worker).work_list), \
22597dc3 112 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
b56c0d89
TH
113 }
114
115#define KTHREAD_WORK_INIT(work, fn) { \
116 .node = LIST_HEAD_INIT((work).node), \
117 .func = (fn), \
b56c0d89
TH
118 }
119
22597dc3
PM
120#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
121 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
841b86f3 122 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
22597dc3
PM
123 TIMER_IRQSAFE), \
124 }
125
b56c0d89
TH
126#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
22597dc3
PM
132#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
133 struct kthread_delayed_work dwork = \
134 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
135
4f32e9b1 136/*
95847e1b
LJ
137 * kthread_worker.lock needs its own lockdep class key when defined on
138 * stack with lockdep enabled. Use the following macros in such cases.
4f32e9b1
YZ
139 */
140#ifdef CONFIG_LOCKDEP
141# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
3989144f 142 ({ kthread_init_worker(&worker); worker; })
4f32e9b1
YZ
143# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
144 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
4f32e9b1
YZ
145#else
146# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
4f32e9b1
YZ
147#endif
148
3989144f 149extern void __kthread_init_worker(struct kthread_worker *worker,
4f32e9b1
YZ
150 const char *name, struct lock_class_key *key);
151
3989144f 152#define kthread_init_worker(worker) \
4f32e9b1
YZ
153 do { \
154 static struct lock_class_key __key; \
3989144f 155 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
4f32e9b1
YZ
156 } while (0)
157
3989144f 158#define kthread_init_work(work, fn) \
4f32e9b1
YZ
159 do { \
160 memset((work), 0, sizeof(struct kthread_work)); \
161 INIT_LIST_HEAD(&(work)->node); \
162 (work)->func = (fn); \
4f32e9b1 163 } while (0)
b56c0d89 164
22597dc3
PM
165#define kthread_init_delayed_work(dwork, fn) \
166 do { \
167 kthread_init_work(&(dwork)->work, (fn)); \
919b250f
KC
168 __init_timer(&(dwork)->timer, \
169 kthread_delayed_work_timer_fn, \
170 TIMER_IRQSAFE); \
22597dc3
PM
171 } while (0)
172
b56c0d89
TH
173int kthread_worker_fn(void *worker_ptr);
174
dbf52682 175__printf(2, 3)
fbae2d44 176struct kthread_worker *
dbf52682 177kthread_create_worker(unsigned int flags, const char namefmt[], ...);
fbae2d44 178
c0b942a7 179__printf(3, 4) struct kthread_worker *
dbf52682
PM
180kthread_create_worker_on_cpu(int cpu, unsigned int flags,
181 const char namefmt[], ...);
fbae2d44 182
3989144f 183bool kthread_queue_work(struct kthread_worker *worker,
b56c0d89 184 struct kthread_work *work);
22597dc3
PM
185
186bool kthread_queue_delayed_work(struct kthread_worker *worker,
187 struct kthread_delayed_work *dwork,
188 unsigned long delay);
189
9a6b06c8
PM
190bool kthread_mod_delayed_work(struct kthread_worker *worker,
191 struct kthread_delayed_work *dwork,
192 unsigned long delay);
193
3989144f
PM
194void kthread_flush_work(struct kthread_work *work);
195void kthread_flush_worker(struct kthread_worker *worker);
b56c0d89 196
37be45d4
PM
197bool kthread_cancel_work_sync(struct kthread_work *work);
198bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
199
35033fe9
PM
200void kthread_destroy_worker(struct kthread_worker *worker);
201
0b508bc9 202#ifdef CONFIG_BLK_CGROUP
05e3db95
SL
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
1da177e4 212#endif /* _LINUX_KTHREAD_H */