License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[sfrench/cifs-2.6.git] / include / linux / kthread.h
1 /* 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>
7
8 __printf(4, 5)
9 struct 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
29 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
30                                           void *data,
31                                           unsigned int cpu,
32                                           const char *namefmt);
33
34 /**
35  * kthread_run - create and wake a thread.
36  * @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
41  * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
42  */
43 #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
52 void free_kthread_struct(struct task_struct *k);
53 void kthread_bind(struct task_struct *k, unsigned int cpu);
54 void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
55 int kthread_stop(struct task_struct *k);
56 bool kthread_should_stop(void);
57 bool kthread_should_park(void);
58 bool kthread_freezable_should_stop(bool *was_frozen);
59 void *kthread_data(struct task_struct *k);
60 void *kthread_probe_data(struct task_struct *k);
61 int kthread_park(struct task_struct *k);
62 void kthread_unpark(struct task_struct *k);
63 void kthread_parkme(void);
64
65 int kthreadd(void *unused);
66 extern struct task_struct *kthreadd_task;
67 extern int tsk_fork_get_node(struct task_struct *tsk);
68
69 /*
70  * Simple work processor based on kthread.
71  *
72  * This provides easier way to make use of kthreads.  A kthread_work
73  * can be queued and flushed using queue/kthread_flush_work()
74  * respectively.  Queued kthread_works are processed by a kthread
75  * running kthread_worker_fn().
76  */
77 struct kthread_work;
78 typedef void (*kthread_work_func_t)(struct kthread_work *work);
79 void kthread_delayed_work_timer_fn(unsigned long __data);
80
81 enum {
82         KTW_FREEZABLE           = 1 << 0,       /* freeze during suspend */
83 };
84
85 struct kthread_worker {
86         unsigned int            flags;
87         spinlock_t              lock;
88         struct list_head        work_list;
89         struct list_head        delayed_work_list;
90         struct task_struct      *task;
91         struct kthread_work     *current_work;
92 };
93
94 struct kthread_work {
95         struct list_head        node;
96         kthread_work_func_t     func;
97         struct kthread_worker   *worker;
98         /* Number of canceling calls that are running at the moment. */
99         int                     canceling;
100 };
101
102 struct kthread_delayed_work {
103         struct kthread_work work;
104         struct timer_list timer;
105 };
106
107 #define KTHREAD_WORKER_INIT(worker)     {                               \
108         .lock = __SPIN_LOCK_UNLOCKED((worker).lock),                    \
109         .work_list = LIST_HEAD_INIT((worker).work_list),                \
110         .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
111         }
112
113 #define KTHREAD_WORK_INIT(work, fn)     {                               \
114         .node = LIST_HEAD_INIT((work).node),                            \
115         .func = (fn),                                                   \
116         }
117
118 #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
125 #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
131 #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn)                          \
132         struct kthread_delayed_work dwork =                             \
133                 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
134
135 /*
136  * kthread_worker.lock needs its own lockdep class key when defined on
137  * stack with lockdep enabled.  Use the following macros in such cases.
138  */
139 #ifdef CONFIG_LOCKDEP
140 # define KTHREAD_WORKER_INIT_ONSTACK(worker)                            \
141         ({ kthread_init_worker(&worker); worker; })
142 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker)                          \
143         struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
144 #else
145 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
146 #endif
147
148 extern void __kthread_init_worker(struct kthread_worker *worker,
149                         const char *name, struct lock_class_key *key);
150
151 #define kthread_init_worker(worker)                                     \
152         do {                                                            \
153                 static struct lock_class_key __key;                     \
154                 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
155         } while (0)
156
157 #define kthread_init_work(work, fn)                                     \
158         do {                                                            \
159                 memset((work), 0, sizeof(struct kthread_work));         \
160                 INIT_LIST_HEAD(&(work)->node);                          \
161                 (work)->func = (fn);                                    \
162         } while (0)
163
164 #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
173 int kthread_worker_fn(void *worker_ptr);
174
175 __printf(2, 3)
176 struct kthread_worker *
177 kthread_create_worker(unsigned int flags, const char namefmt[], ...);
178
179 __printf(3, 4) struct kthread_worker *
180 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
181                              const char namefmt[], ...);
182
183 bool kthread_queue_work(struct kthread_worker *worker,
184                         struct kthread_work *work);
185
186 bool kthread_queue_delayed_work(struct kthread_worker *worker,
187                                 struct kthread_delayed_work *dwork,
188                                 unsigned long delay);
189
190 bool kthread_mod_delayed_work(struct kthread_worker *worker,
191                               struct kthread_delayed_work *dwork,
192                               unsigned long delay);
193
194 void kthread_flush_work(struct kthread_work *work);
195 void kthread_flush_worker(struct kthread_worker *worker);
196
197 bool kthread_cancel_work_sync(struct kthread_work *work);
198 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
199
200 void kthread_destroy_worker(struct kthread_worker *worker);
201
202 #endif /* _LINUX_KTHREAD_H */