XenevaOS
Loading...
Searching...
No Matches
kthread.h
Go to the documentation of this file.
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
8struct mm_struct;
9
10/* opaque kthread data */
11struct kthread;
12
13/*
14 * When "(p->flags & PF_KTHREAD)" is set the task is a kthread and will
15 * always remain a kthread. For kthreads p->worker_private always
16 * points to a struct kthread. For tasks that are not kthreads
17 * p->worker_private is used to point to other things.
18 *
19 * Return NULL for any task that is not a kthread.
20 */
21static inline struct kthread *tsk_is_kthread(struct task_struct *p)
22{
23 if (p->flags & PF_KTHREAD)
24 return p->worker_private;
25 return NULL;
26}
27
29struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
30 void *data,
31 int node,
32 const char namefmt[], ...);
33
45#define kthread_create(threadfn, data, namefmt, arg...) \
46 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
47
48
49struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
50 void *data,
51 unsigned int cpu,
52 const char *namefmt);
53
54void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk);
56
57void kthread_set_per_cpu(struct task_struct *k, int cpu);
59
69#define kthread_run(threadfn, data, namefmt, ...) \
70({ \
71 struct task_struct *__k \
72 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
73 if (!IS_ERR(__k)) \
74 wake_up_process(__k); \
75 __k; \
76})
77
90static inline struct task_struct *
91kthread_run_on_cpu(int (*threadfn)(void *data), void *data,
92 unsigned int cpu, const char *namefmt)
93{
94 struct task_struct *p;
95
96 p = kthread_create_on_cpu(threadfn, data, cpu, namefmt);
97 if (!IS_ERR(p))
98 wake_up_process(p);
99
100 return p;
101}
102
104void kthread_bind(struct task_struct *k, unsigned int cpu);
105void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
106int kthread_affine_preferred(struct task_struct *p, const struct cpumask *mask);
112bool kthread_freezable_should_stop(bool *was_frozen);
113void *kthread_func(struct task_struct *k);
114void *kthread_data(struct task_struct *k);
118void kthread_parkme(void);
119#define kthread_exit(result) do_exit(result)
122void kthread_do_exit(struct kthread *, long);
123
124int kthreadd(void *unused);
125extern struct task_struct *kthreadd_task;
126extern int tsk_fork_get_node(struct task_struct *tsk);
127
128/*
129 * Simple work processor based on kthread.
130 *
131 * This provides easier way to make use of kthreads. A kthread_work
132 * can be queued and flushed using queue/kthread_flush_work()
133 * respectively. Queued kthread_works are processed by a kthread
134 * running kthread_worker_fn().
135 */
136struct kthread_work;
137typedef void (*kthread_work_func_t)(struct kthread_work *work);
139
140enum {
141 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
142};
143
145 unsigned int flags;
146 raw_spinlock_t lock;
151};
152
157 /* Number of canceling calls that are running at the moment. */
159};
160
165
166#define KTHREAD_WORK_INIT(work, fn) { \
167 .node = LIST_HEAD_INIT((work).node), \
168 .func = (fn), \
169 }
170
171#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
172 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
173 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
174 TIMER_IRQSAFE), \
175 }
176
177#define DEFINE_KTHREAD_WORK(work, fn) \
178 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
179
180#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
181 struct kthread_delayed_work dwork = \
182 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
183
184extern void __kthread_init_worker(struct kthread_worker *worker,
185 const char *name, struct lock_class_key *key);
186
187#define kthread_init_worker(worker) \
188 do { \
189 static struct lock_class_key __key; \
190 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
191 } while (0)
192
193#define kthread_init_work(work, fn) \
194 do { \
195 memset((work), 0, sizeof(struct kthread_work)); \
196 INIT_LIST_HEAD(&(work)->node); \
197 (work)->func = (fn); \
198 } while (0)
199
200#define kthread_init_delayed_work(dwork, fn) \
201 do { \
202 kthread_init_work(&(dwork)->work, (fn)); \
203 timer_setup(&(dwork)->timer, \
204 kthread_delayed_work_timer_fn, \
205 TIMER_IRQSAFE); \
206 } while (0)
207
208int kthread_worker_fn(void *worker_ptr);
209
211struct kthread_worker *kthread_create_worker_on_node(unsigned int flags,
212 int node,
213 const char namefmt[], ...);
214
215#define kthread_create_worker(flags, namefmt, ...) \
216 kthread_create_worker_on_node(flags, NUMA_NO_NODE, namefmt, ## __VA_ARGS__);
217
226#define kthread_run_worker(flags, namefmt, ...) \
227({ \
228 struct kthread_worker *__kw \
229 = kthread_create_worker(flags, namefmt, ## __VA_ARGS__); \
230 if (!IS_ERR(__kw)) \
231 wake_up_process(__kw->task); \
232 __kw; \
233})
234
235struct kthread_worker *
237 const char namefmt[]);
238
250static inline struct kthread_worker *
251kthread_run_worker_on_cpu(int cpu, unsigned int flags,
252 const char namefmt[])
253{
254 struct kthread_worker *kw;
255
257 if (!IS_ERR(kw))
258 wake_up_process(kw->task);
259
260 return kw;
261}
262
264 struct kthread_work *work);
265
267 struct kthread_delayed_work *dwork,
268 unsigned long delay);
269
271 struct kthread_delayed_work *dwork,
272 unsigned long delay);
273
276
279
281
282void kthread_use_mm(struct mm_struct *mm);
283void kthread_unuse_mm(struct mm_struct *mm);
284
285struct cgroup_subsys_state;
286
287#ifdef CONFIG_BLK_CGROUP
288void kthread_associate_blkcg(struct cgroup_subsys_state *css);
289struct cgroup_subsys_state *kthread_blkcg(void);
290#else
291static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
292#endif
293#endif /* _LINUX_KTHREAD_H */
uint8_t flags
Definition virtio.h:0
XETime t
Definition main.cpp:53
#define NULL
Definition actypes.h:561
#define __printf(fmt_idx, arg_idx)
Definition compiler.h:144
#define __noreturn
Definition compiler_attributes.h:271
struct drm_crtc struct drm_plane struct drm_plane const struct drm_crtc_funcs const char * name
Definition drm_crtc.h:1242
#define IS_ERR(ptr)
Definition err.h:59
bool kthread_freezable_should_stop(bool *was_frozen)
void kthread_bind(struct task_struct *k, unsigned int cpu)
void * kthread_probe_data(struct task_struct *k)
bool kthread_is_per_cpu(struct task_struct *k)
int kthreadd(void *unused)
void free_kthread_struct(struct task_struct *k)
struct task_struct * kthreadd_task
bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work)
void kthread_do_exit(struct kthread *, long)
@ KTW_FREEZABLE
Definition kthread.h:141
void kthread_set_per_cpu(struct task_struct *k, int cpu)
bool kthread_cancel_work_sync(struct kthread_work *work)
int kthread_park(struct task_struct *k)
void kthread_flush_worker(struct kthread_worker *worker)
bool kthread_should_park(void)
void int const char namefmt[]
Definition kthread.h:32
void(* kthread_work_func_t)(struct kthread_work *work)
Definition kthread.h:137
int kthreads_update_housekeeping(void)
void * data
Definition kthread.h:30
void kthread_parkme(void)
bool kthread_queue_work(struct kthread_worker *worker, struct kthread_work *work)
bool kthread_should_stop_or_park(void)
void kthread_destroy_worker(struct kthread_worker *worker)
void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask)
bool set_kthread_struct(struct task_struct *p)
int kthread_stop_put(struct task_struct *k)
void kthread_flush_work(struct kthread_work *work)
void * kthread_data(struct task_struct *k)
void * kthread_func(struct task_struct *k)
void kthread_unpark(struct task_struct *k)
int kthread_stop(struct task_struct *k)
bool kthread_queue_delayed_work(struct kthread_worker *worker, struct kthread_delayed_work *dwork, unsigned long delay)
void __kthread_init_worker(struct kthread_worker *worker, const char *name, struct lock_class_key *key)
int tsk_fork_get_node(struct task_struct *tsk)
struct task_struct * kthread_create_on_cpu(int(*threadfn)(void *data), void *data, unsigned int cpu, const char *namefmt)
void kthread_use_mm(struct mm_struct *mm)
int kthread_affine_preferred(struct task_struct *p, const struct cpumask *mask)
void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk)
bool kthread_mod_delayed_work(struct kthread_worker *worker, struct kthread_delayed_work *dwork, unsigned long delay)
int kthread_worker_fn(void *worker_ptr)
void kthread_delayed_work_timer_fn(struct timer_list *t)
bool kthread_should_stop(void)
struct kthread_worker * kthread_create_worker_on_cpu(int cpu, unsigned int flags, const char namefmt[])
void int node
Definition kthread.h:31
void kthread_unuse_mm(struct mm_struct *mm)
void kthread_complete_and_exit(struct completion *, long) __noreturn
#define PF_KTHREAD
Definition sched.h:102
Definition completion.h:55
Definition kthread.h:161
struct kthread_work work
Definition kthread.h:162
struct timer_list timer
Definition kthread.h:163
Definition kthread.h:153
kthread_work_func_t func
Definition kthread.h:155
int canceling
Definition kthread.h:158
struct kthread_worker * worker
Definition kthread.h:156
struct list_head node
Definition kthread.h:154
Definition kthread.h:144
unsigned int flags
Definition kthread.h:145
raw_spinlock_t lock
Definition kthread.h:146
struct task_struct * task
Definition kthread.h:149
struct list_head work_list
Definition kthread.h:147
struct list_head delayed_work_list
Definition kthread.h:148
struct kthread_work * current_work
Definition kthread.h:150
Definition list.h:11
Definition lockdep.h:160
Definition sched.h:121
unsigned int flags
Definition sched.h:125
void * worker_private
Definition sched.h:126
Definition timer.h:6