Merge tag 'slab-for-5.17-part2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-block.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 struct mm_struct;
9
10 __printf(4, 5)
11 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
12                                            void *data,
13                                            int node,
14                                            const char namefmt[], ...);
15
16 /**
17  * kthread_create - create a kthread on the current node
18  * @threadfn: the function to run in the thread
19  * @data: data pointer for @threadfn()
20  * @namefmt: printf-style format string for the thread name
21  * @arg: arguments for @namefmt.
22  *
23  * This macro will create a kthread on the current node, leaving it in
24  * the stopped state.  This is just a helper for kthread_create_on_node();
25  * see the documentation there for more details.
26  */
27 #define kthread_create(threadfn, data, namefmt, arg...) \
28         kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
29
30
31 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
32                                           void *data,
33                                           unsigned int cpu,
34                                           const char *namefmt);
35
36 bool set_kthread_struct(struct task_struct *p);
37
38 void kthread_set_per_cpu(struct task_struct *k, int cpu);
39 bool kthread_is_per_cpu(struct task_struct *k);
40
41 /**
42  * kthread_run - create and wake a thread.
43  * @threadfn: the function to run until signal_pending(current).
44  * @data: data ptr for @threadfn.
45  * @namefmt: printf-style name for the thread.
46  *
47  * Description: Convenient wrapper for kthread_create() followed by
48  * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
49  */
50 #define kthread_run(threadfn, data, namefmt, ...)                          \
51 ({                                                                         \
52         struct task_struct *__k                                            \
53                 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
54         if (!IS_ERR(__k))                                                  \
55                 wake_up_process(__k);                                      \
56         __k;                                                               \
57 })
58
59 /**
60  * kthread_run_on_cpu - create and wake a cpu bound thread.
61  * @threadfn: the function to run until signal_pending(current).
62  * @data: data ptr for @threadfn.
63  * @cpu: The cpu on which the thread should be bound,
64  * @namefmt: printf-style name for the thread. Format is restricted
65  *           to "name.*%u". Code fills in cpu number.
66  *
67  * Description: Convenient wrapper for kthread_create_on_cpu()
68  * followed by wake_up_process().  Returns the kthread or
69  * ERR_PTR(-ENOMEM).
70  */
71 static inline struct task_struct *
72 kthread_run_on_cpu(int (*threadfn)(void *data), void *data,
73                         unsigned int cpu, const char *namefmt)
74 {
75         struct task_struct *p;
76
77         p = kthread_create_on_cpu(threadfn, data, cpu, namefmt);
78         if (!IS_ERR(p))
79                 wake_up_process(p);
80
81         return p;
82 }
83
84 void free_kthread_struct(struct task_struct *k);
85 void kthread_bind(struct task_struct *k, unsigned int cpu);
86 void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
87 int kthread_stop(struct task_struct *k);
88 bool kthread_should_stop(void);
89 bool kthread_should_park(void);
90 bool __kthread_should_park(struct task_struct *k);
91 bool kthread_freezable_should_stop(bool *was_frozen);
92 void *kthread_func(struct task_struct *k);
93 void *kthread_data(struct task_struct *k);
94 void *kthread_probe_data(struct task_struct *k);
95 int kthread_park(struct task_struct *k);
96 void kthread_unpark(struct task_struct *k);
97 void kthread_parkme(void);
98 void kthread_exit(long result) __noreturn;
99 void kthread_complete_and_exit(struct completion *, long) __noreturn;
100
101 int kthreadd(void *unused);
102 extern struct task_struct *kthreadd_task;
103 extern int tsk_fork_get_node(struct task_struct *tsk);
104
105 /*
106  * Simple work processor based on kthread.
107  *
108  * This provides easier way to make use of kthreads.  A kthread_work
109  * can be queued and flushed using queue/kthread_flush_work()
110  * respectively.  Queued kthread_works are processed by a kthread
111  * running kthread_worker_fn().
112  */
113 struct kthread_work;
114 typedef void (*kthread_work_func_t)(struct kthread_work *work);
115 void kthread_delayed_work_timer_fn(struct timer_list *t);
116
117 enum {
118         KTW_FREEZABLE           = 1 << 0,       /* freeze during suspend */
119 };
120
121 struct kthread_worker {
122         unsigned int            flags;
123         raw_spinlock_t          lock;
124         struct list_head        work_list;
125         struct list_head        delayed_work_list;
126         struct task_struct      *task;
127         struct kthread_work     *current_work;
128 };
129
130 struct kthread_work {
131         struct list_head        node;
132         kthread_work_func_t     func;
133         struct kthread_worker   *worker;
134         /* Number of canceling calls that are running at the moment. */
135         int                     canceling;
136 };
137
138 struct kthread_delayed_work {
139         struct kthread_work work;
140         struct timer_list timer;
141 };
142
143 #define KTHREAD_WORKER_INIT(worker)     {                               \
144         .lock = __RAW_SPIN_LOCK_UNLOCKED((worker).lock),                \
145         .work_list = LIST_HEAD_INIT((worker).work_list),                \
146         .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
147         }
148
149 #define KTHREAD_WORK_INIT(work, fn)     {                               \
150         .node = LIST_HEAD_INIT((work).node),                            \
151         .func = (fn),                                                   \
152         }
153
154 #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) {                          \
155         .work = KTHREAD_WORK_INIT((dwork).work, (fn)),                  \
156         .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
157                                      TIMER_IRQSAFE),                    \
158         }
159
160 #define DEFINE_KTHREAD_WORKER(worker)                                   \
161         struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
162
163 #define DEFINE_KTHREAD_WORK(work, fn)                                   \
164         struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
165
166 #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn)                          \
167         struct kthread_delayed_work dwork =                             \
168                 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
169
170 /*
171  * kthread_worker.lock needs its own lockdep class key when defined on
172  * stack with lockdep enabled.  Use the following macros in such cases.
173  */
174 #ifdef CONFIG_LOCKDEP
175 # define KTHREAD_WORKER_INIT_ONSTACK(worker)                            \
176         ({ kthread_init_worker(&worker); worker; })
177 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker)                          \
178         struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
179 #else
180 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
181 #endif
182
183 extern void __kthread_init_worker(struct kthread_worker *worker,
184                         const char *name, struct lock_class_key *key);
185
186 #define kthread_init_worker(worker)                                     \
187         do {                                                            \
188                 static struct lock_class_key __key;                     \
189                 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
190         } while (0)
191
192 #define kthread_init_work(work, fn)                                     \
193         do {                                                            \
194                 memset((work), 0, sizeof(struct kthread_work));         \
195                 INIT_LIST_HEAD(&(work)->node);                          \
196                 (work)->func = (fn);                                    \
197         } while (0)
198
199 #define kthread_init_delayed_work(dwork, fn)                            \
200         do {                                                            \
201                 kthread_init_work(&(dwork)->work, (fn));                \
202                 timer_setup(&(dwork)->timer,                            \
203                              kthread_delayed_work_timer_fn,             \
204                              TIMER_IRQSAFE);                            \
205         } while (0)
206
207 int kthread_worker_fn(void *worker_ptr);
208
209 __printf(2, 3)
210 struct kthread_worker *
211 kthread_create_worker(unsigned int flags, const char namefmt[], ...);
212
213 __printf(3, 4) struct kthread_worker *
214 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
215                              const char namefmt[], ...);
216
217 bool kthread_queue_work(struct kthread_worker *worker,
218                         struct kthread_work *work);
219
220 bool kthread_queue_delayed_work(struct kthread_worker *worker,
221                                 struct kthread_delayed_work *dwork,
222                                 unsigned long delay);
223
224 bool kthread_mod_delayed_work(struct kthread_worker *worker,
225                               struct kthread_delayed_work *dwork,
226                               unsigned long delay);
227
228 void kthread_flush_work(struct kthread_work *work);
229 void kthread_flush_worker(struct kthread_worker *worker);
230
231 bool kthread_cancel_work_sync(struct kthread_work *work);
232 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
233
234 void kthread_destroy_worker(struct kthread_worker *worker);
235
236 void kthread_use_mm(struct mm_struct *mm);
237 void kthread_unuse_mm(struct mm_struct *mm);
238
239 struct cgroup_subsys_state;
240
241 #ifdef CONFIG_BLK_CGROUP
242 void kthread_associate_blkcg(struct cgroup_subsys_state *css);
243 struct cgroup_subsys_state *kthread_blkcg(void);
244 #else
245 static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
246 static inline struct cgroup_subsys_state *kthread_blkcg(void)
247 {
248         return NULL;
249 }
250 #endif
251 #endif /* _LINUX_KTHREAD_H */