kthread: better support freezable kthread workers
[cascardo/linux.git] / include / linux / kthread.h
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
7 __printf(4, 5)
8 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
9                                            void *data,
10                                            int node,
11                                            const char namefmt[], ...);
12
13 #define kthread_create(threadfn, data, namefmt, arg...) \
14         kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
15
16
17 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
18                                           void *data,
19                                           unsigned int cpu,
20                                           const char *namefmt);
21
22 /**
23  * kthread_run - create and wake a thread.
24  * @threadfn: the function to run until signal_pending(current).
25  * @data: data ptr for @threadfn.
26  * @namefmt: printf-style name for the thread.
27  *
28  * Description: Convenient wrapper for kthread_create() followed by
29  * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
30  */
31 #define kthread_run(threadfn, data, namefmt, ...)                          \
32 ({                                                                         \
33         struct task_struct *__k                                            \
34                 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
35         if (!IS_ERR(__k))                                                  \
36                 wake_up_process(__k);                                      \
37         __k;                                                               \
38 })
39
40 void kthread_bind(struct task_struct *k, unsigned int cpu);
41 void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
42 int kthread_stop(struct task_struct *k);
43 bool kthread_should_stop(void);
44 bool kthread_should_park(void);
45 bool kthread_freezable_should_stop(bool *was_frozen);
46 void *kthread_data(struct task_struct *k);
47 void *kthread_probe_data(struct task_struct *k);
48 int kthread_park(struct task_struct *k);
49 void kthread_unpark(struct task_struct *k);
50 void kthread_parkme(void);
51
52 int kthreadd(void *unused);
53 extern struct task_struct *kthreadd_task;
54 extern int tsk_fork_get_node(struct task_struct *tsk);
55
56 /*
57  * Simple work processor based on kthread.
58  *
59  * This provides easier way to make use of kthreads.  A kthread_work
60  * can be queued and flushed using queue/kthread_flush_work()
61  * respectively.  Queued kthread_works are processed by a kthread
62  * running kthread_worker_fn().
63  */
64 struct kthread_work;
65 typedef void (*kthread_work_func_t)(struct kthread_work *work);
66 void kthread_delayed_work_timer_fn(unsigned long __data);
67
68 enum {
69         KTW_FREEZABLE           = 1 << 0,       /* freeze during suspend */
70 };
71
72 struct kthread_worker {
73         unsigned int            flags;
74         spinlock_t              lock;
75         struct list_head        work_list;
76         struct list_head        delayed_work_list;
77         struct task_struct      *task;
78         struct kthread_work     *current_work;
79 };
80
81 struct kthread_work {
82         struct list_head        node;
83         kthread_work_func_t     func;
84         struct kthread_worker   *worker;
85         /* Number of canceling calls that are running at the moment. */
86         int                     canceling;
87 };
88
89 struct kthread_delayed_work {
90         struct kthread_work work;
91         struct timer_list timer;
92 };
93
94 #define KTHREAD_WORKER_INIT(worker)     {                               \
95         .lock = __SPIN_LOCK_UNLOCKED((worker).lock),                    \
96         .work_list = LIST_HEAD_INIT((worker).work_list),                \
97         .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
98         }
99
100 #define KTHREAD_WORK_INIT(work, fn)     {                               \
101         .node = LIST_HEAD_INIT((work).node),                            \
102         .func = (fn),                                                   \
103         }
104
105 #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) {                          \
106         .work = KTHREAD_WORK_INIT((dwork).work, (fn)),                  \
107         .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,     \
108                                      0, (unsigned long)&(dwork),        \
109                                      TIMER_IRQSAFE),                    \
110         }
111
112 #define DEFINE_KTHREAD_WORKER(worker)                                   \
113         struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
114
115 #define DEFINE_KTHREAD_WORK(work, fn)                                   \
116         struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
117
118 #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn)                          \
119         struct kthread_delayed_work dwork =                             \
120                 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
121
122 /*
123  * kthread_worker.lock needs its own lockdep class key when defined on
124  * stack with lockdep enabled.  Use the following macros in such cases.
125  */
126 #ifdef CONFIG_LOCKDEP
127 # define KTHREAD_WORKER_INIT_ONSTACK(worker)                            \
128         ({ kthread_init_worker(&worker); worker; })
129 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker)                          \
130         struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
131 #else
132 # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
133 #endif
134
135 extern void __kthread_init_worker(struct kthread_worker *worker,
136                         const char *name, struct lock_class_key *key);
137
138 #define kthread_init_worker(worker)                                     \
139         do {                                                            \
140                 static struct lock_class_key __key;                     \
141                 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
142         } while (0)
143
144 #define kthread_init_work(work, fn)                                     \
145         do {                                                            \
146                 memset((work), 0, sizeof(struct kthread_work));         \
147                 INIT_LIST_HEAD(&(work)->node);                          \
148                 (work)->func = (fn);                                    \
149         } while (0)
150
151 #define kthread_init_delayed_work(dwork, fn)                            \
152         do {                                                            \
153                 kthread_init_work(&(dwork)->work, (fn));                \
154                 __setup_timer(&(dwork)->timer,                          \
155                               kthread_delayed_work_timer_fn,            \
156                               (unsigned long)(dwork),                   \
157                               TIMER_IRQSAFE);                           \
158         } while (0)
159
160 int kthread_worker_fn(void *worker_ptr);
161
162 __printf(2, 3)
163 struct kthread_worker *
164 kthread_create_worker(unsigned int flags, const char namefmt[], ...);
165
166 struct kthread_worker *
167 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
168                              const char namefmt[], ...);
169
170 bool kthread_queue_work(struct kthread_worker *worker,
171                         struct kthread_work *work);
172
173 bool kthread_queue_delayed_work(struct kthread_worker *worker,
174                                 struct kthread_delayed_work *dwork,
175                                 unsigned long delay);
176
177 bool kthread_mod_delayed_work(struct kthread_worker *worker,
178                               struct kthread_delayed_work *dwork,
179                               unsigned long delay);
180
181 void kthread_flush_work(struct kthread_work *work);
182 void kthread_flush_worker(struct kthread_worker *worker);
183
184 bool kthread_cancel_work_sync(struct kthread_work *work);
185 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
186
187 void kthread_destroy_worker(struct kthread_worker *worker);
188
189 #endif /* _LINUX_KTHREAD_H */