]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - include/linux/kthread.h
<linux/uaccess.h>: Fix copy_in_user() declaration
[mirror_ubuntu-artful-kernel.git] / include / linux / kthread.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_KTHREAD_H
2#define _LINUX_KTHREAD_H
3/* Simple interface for creating and stopping kernel threads without mess. */
4#include <linux/err.h>
5#include <linux/sched.h>
6
b9075fa9 7__printf(4, 5)
207205a2
ED
8struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
9 void *data,
10 int node,
b9075fa9 11 const char namefmt[], ...);
207205a2 12
e154ccc8
JC
13/**
14 * kthread_create - create a kthread on the current node
15 * @threadfn: the function to run in the thread
16 * @data: data pointer for @threadfn()
17 * @namefmt: printf-style format string for the thread name
d16977f3 18 * @arg...: arguments for @namefmt.
e154ccc8
JC
19 *
20 * This macro will create a kthread on the current node, leaving it in
21 * the stopped state. This is just a helper for kthread_create_on_node();
22 * see the documentation there for more details.
23 */
207205a2 24#define kthread_create(threadfn, data, namefmt, arg...) \
e9f06986 25 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
207205a2 26
1da177e4 27
2a1d4460
TG
28struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
29 void *data,
30 unsigned int cpu,
31 const char *namefmt);
32
1da177e4 33/**
9e37bd30 34 * kthread_run - create and wake a thread.
1da177e4
LT
35 * @threadfn: the function to run until signal_pending(current).
36 * @data: data ptr for @threadfn.
37 * @namefmt: printf-style name for the thread.
38 *
39 * Description: Convenient wrapper for kthread_create() followed by
9e37bd30
RD
40 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
41 */
1da177e4
LT
42#define kthread_run(threadfn, data, namefmt, ...) \
43({ \
44 struct task_struct *__k \
45 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
46 if (!IS_ERR(__k)) \
47 wake_up_process(__k); \
48 __k; \
49})
50
1da5c46f 51void free_kthread_struct(struct task_struct *k);
1da177e4 52void kthread_bind(struct task_struct *k, unsigned int cpu);
25834c73 53void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
1da177e4 54int kthread_stop(struct task_struct *k);
2a1d4460
TG
55bool kthread_should_stop(void);
56bool kthread_should_park(void);
8a32c441 57bool kthread_freezable_should_stop(bool *was_frozen);
82805ab7 58void *kthread_data(struct task_struct *k);
e700591a 59void *kthread_probe_data(struct task_struct *k);
2a1d4460
TG
60int kthread_park(struct task_struct *k);
61void kthread_unpark(struct task_struct *k);
62void kthread_parkme(void);
1da177e4 63
73c27992
EB
64int kthreadd(void *unused);
65extern struct task_struct *kthreadd_task;
207205a2 66extern int tsk_fork_get_node(struct task_struct *tsk);
73c27992 67
b56c0d89
TH
68/*
69 * Simple work processor based on kthread.
70 *
71 * This provides easier way to make use of kthreads. A kthread_work
3989144f 72 * can be queued and flushed using queue/kthread_flush_work()
b56c0d89
TH
73 * respectively. Queued kthread_works are processed by a kthread
74 * running kthread_worker_fn().
b56c0d89
TH
75 */
76struct kthread_work;
77typedef void (*kthread_work_func_t)(struct kthread_work *work);
22597dc3 78void kthread_delayed_work_timer_fn(unsigned long __data);
b56c0d89 79
dbf52682
PM
80enum {
81 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */
82};
83
b56c0d89 84struct kthread_worker {
dbf52682 85 unsigned int flags;
b56c0d89
TH
86 spinlock_t lock;
87 struct list_head work_list;
22597dc3 88 struct list_head delayed_work_list;
b56c0d89 89 struct task_struct *task;
46f3d976 90 struct kthread_work *current_work;
b56c0d89
TH
91};
92
93struct kthread_work {
94 struct list_head node;
95 kthread_work_func_t func;
46f3d976 96 struct kthread_worker *worker;
37be45d4
PM
97 /* Number of canceling calls that are running at the moment. */
98 int canceling;
b56c0d89
TH
99};
100
22597dc3
PM
101struct kthread_delayed_work {
102 struct kthread_work work;
103 struct timer_list timer;
104};
105
b56c0d89 106#define KTHREAD_WORKER_INIT(worker) { \
92578c0b 107 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
b56c0d89 108 .work_list = LIST_HEAD_INIT((worker).work_list), \
22597dc3 109 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
b56c0d89
TH
110 }
111
112#define KTHREAD_WORK_INIT(work, fn) { \
113 .node = LIST_HEAD_INIT((work).node), \
114 .func = (fn), \
b56c0d89
TH
115 }
116
22597dc3
PM
117#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
118 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
119 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn, \
120 0, (unsigned long)&(dwork), \
121 TIMER_IRQSAFE), \
122 }
123
b56c0d89
TH
124#define DEFINE_KTHREAD_WORKER(worker) \
125 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
126
127#define DEFINE_KTHREAD_WORK(work, fn) \
128 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
129
22597dc3
PM
130#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
131 struct kthread_delayed_work dwork = \
132 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
133
4f32e9b1 134/*
95847e1b
LJ
135 * kthread_worker.lock needs its own lockdep class key when defined on
136 * stack with lockdep enabled. Use the following macros in such cases.
4f32e9b1
YZ
137 */
138#ifdef CONFIG_LOCKDEP
139# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
3989144f 140 ({ kthread_init_worker(&worker); worker; })
4f32e9b1
YZ
141# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
142 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
4f32e9b1
YZ
143#else
144# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
4f32e9b1
YZ
145#endif
146
3989144f 147extern void __kthread_init_worker(struct kthread_worker *worker,
4f32e9b1
YZ
148 const char *name, struct lock_class_key *key);
149
3989144f 150#define kthread_init_worker(worker) \
4f32e9b1
YZ
151 do { \
152 static struct lock_class_key __key; \
3989144f 153 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
4f32e9b1
YZ
154 } while (0)
155
3989144f 156#define kthread_init_work(work, fn) \
4f32e9b1
YZ
157 do { \
158 memset((work), 0, sizeof(struct kthread_work)); \
159 INIT_LIST_HEAD(&(work)->node); \
160 (work)->func = (fn); \
4f32e9b1 161 } while (0)
b56c0d89 162
22597dc3
PM
163#define kthread_init_delayed_work(dwork, fn) \
164 do { \
165 kthread_init_work(&(dwork)->work, (fn)); \
166 __setup_timer(&(dwork)->timer, \
167 kthread_delayed_work_timer_fn, \
168 (unsigned long)(dwork), \
169 TIMER_IRQSAFE); \
170 } while (0)
171
b56c0d89
TH
172int kthread_worker_fn(void *worker_ptr);
173
dbf52682 174__printf(2, 3)
fbae2d44 175struct kthread_worker *
dbf52682 176kthread_create_worker(unsigned int flags, const char namefmt[], ...);
fbae2d44 177
c0b942a7 178__printf(3, 4) struct kthread_worker *
dbf52682
PM
179kthread_create_worker_on_cpu(int cpu, unsigned int flags,
180 const char namefmt[], ...);
fbae2d44 181
3989144f 182bool kthread_queue_work(struct kthread_worker *worker,
b56c0d89 183 struct kthread_work *work);
22597dc3
PM
184
185bool kthread_queue_delayed_work(struct kthread_worker *worker,
186 struct kthread_delayed_work *dwork,
187 unsigned long delay);
188
9a6b06c8
PM
189bool kthread_mod_delayed_work(struct kthread_worker *worker,
190 struct kthread_delayed_work *dwork,
191 unsigned long delay);
192
3989144f
PM
193void kthread_flush_work(struct kthread_work *work);
194void kthread_flush_worker(struct kthread_worker *worker);
b56c0d89 195
37be45d4
PM
196bool kthread_cancel_work_sync(struct kthread_work *work);
197bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
198
35033fe9
PM
199void kthread_destroy_worker(struct kthread_worker *worker);
200
1da177e4 201#endif /* _LINUX_KTHREAD_H */