blob: 77435dcde707827fcf8863d5443ca982d7f0f244 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#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
Joe Perchesb9075fa2011-10-31 17:11:33 -07007__printf(4, 5)
Eric Dumazet207205a2011-03-22 16:30:44 -07008struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
9 void *data,
10 int node,
Joe Perchesb9075fa2011-10-31 17:11:33 -070011 const char namefmt[], ...);
Eric Dumazet207205a2011-03-22 16:30:44 -070012
13#define kthread_create(threadfn, data, namefmt, arg...) \
Andrew Mortone9f06982015-09-04 15:42:42 -070014 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
Eric Dumazet207205a2011-03-22 16:30:44 -070015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000017struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
18 void *data,
19 unsigned int cpu,
20 const char *namefmt);
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022/**
Randy Dunlap9e37bd32006-06-25 05:49:19 -070023 * kthread_run - create and wake a thread.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * @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
Randy Dunlap9e37bd32006-06-25 05:49:19 -070029 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM).
30 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070040void kthread_bind(struct task_struct *k, unsigned int cpu);
Peter Zijlstra25834c72015-05-15 17:43:34 +020041void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042int kthread_stop(struct task_struct *k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000043bool kthread_should_stop(void);
44bool kthread_should_park(void);
Tejun Heo8a32c442011-11-21 12:32:23 -080045bool kthread_freezable_should_stop(bool *was_frozen);
Tejun Heo82805ab2010-06-29 10:07:09 +020046void *kthread_data(struct task_struct *k);
Petr Mladeke7005912016-10-11 13:55:17 -070047void *kthread_probe_data(struct task_struct *k);
Thomas Gleixner2a1d4462012-07-16 10:42:36 +000048int kthread_park(struct task_struct *k);
49void kthread_unpark(struct task_struct *k);
50void kthread_parkme(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Eric W. Biederman73c27992007-05-09 02:34:32 -070052int kthreadd(void *unused);
53extern struct task_struct *kthreadd_task;
Eric Dumazet207205a2011-03-22 16:30:44 -070054extern int tsk_fork_get_node(struct task_struct *tsk);
Eric W. Biederman73c27992007-05-09 02:34:32 -070055
Tejun Heob56c0d82010-06-29 10:07:09 +020056/*
57 * Simple work processor based on kthread.
58 *
59 * This provides easier way to make use of kthreads. A kthread_work
Petr Mladek39891442016-10-11 13:55:20 -070060 * can be queued and flushed using queue/kthread_flush_work()
Tejun Heob56c0d82010-06-29 10:07:09 +020061 * respectively. Queued kthread_works are processed by a kthread
62 * running kthread_worker_fn().
Tejun Heob56c0d82010-06-29 10:07:09 +020063 */
64struct kthread_work;
65typedef void (*kthread_work_func_t)(struct kthread_work *work);
Petr Mladek22597dc2016-10-11 13:55:40 -070066void kthread_delayed_work_timer_fn(unsigned long __data);
Tejun Heob56c0d82010-06-29 10:07:09 +020067
68struct kthread_worker {
69 spinlock_t lock;
70 struct list_head work_list;
Petr Mladek22597dc2016-10-11 13:55:40 -070071 struct list_head delayed_work_list;
Tejun Heob56c0d82010-06-29 10:07:09 +020072 struct task_struct *task;
Tejun Heo46f3d972012-07-19 13:52:53 -070073 struct kthread_work *current_work;
Tejun Heob56c0d82010-06-29 10:07:09 +020074};
75
76struct kthread_work {
77 struct list_head node;
78 kthread_work_func_t func;
Tejun Heo46f3d972012-07-19 13:52:53 -070079 struct kthread_worker *worker;
Petr Mladek37be45d2016-10-11 13:55:43 -070080 /* Number of canceling calls that are running at the moment. */
81 int canceling;
Tejun Heob56c0d82010-06-29 10:07:09 +020082};
83
Petr Mladek22597dc2016-10-11 13:55:40 -070084struct kthread_delayed_work {
85 struct kthread_work work;
86 struct timer_list timer;
87};
88
Tejun Heob56c0d82010-06-29 10:07:09 +020089#define KTHREAD_WORKER_INIT(worker) { \
Thomas Gleixner92578c0b2011-01-23 15:24:55 +010090 .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \
Tejun Heob56c0d82010-06-29 10:07:09 +020091 .work_list = LIST_HEAD_INIT((worker).work_list), \
Petr Mladek22597dc2016-10-11 13:55:40 -070092 .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
Tejun Heob56c0d82010-06-29 10:07:09 +020093 }
94
95#define KTHREAD_WORK_INIT(work, fn) { \
96 .node = LIST_HEAD_INIT((work).node), \
97 .func = (fn), \
Tejun Heob56c0d82010-06-29 10:07:09 +020098 }
99
Petr Mladek22597dc2016-10-11 13:55:40 -0700100#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \
101 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \
102 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn, \
103 0, (unsigned long)&(dwork), \
104 TIMER_IRQSAFE), \
105 }
106
Tejun Heob56c0d82010-06-29 10:07:09 +0200107#define DEFINE_KTHREAD_WORKER(worker) \
108 struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)
109
110#define DEFINE_KTHREAD_WORK(work, fn) \
111 struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
112
Petr Mladek22597dc2016-10-11 13:55:40 -0700113#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \
114 struct kthread_delayed_work dwork = \
115 KTHREAD_DELAYED_WORK_INIT(dwork, fn)
116
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100117/*
Lai Jiangshan95847e12014-07-26 12:04:00 +0800118 * kthread_worker.lock needs its own lockdep class key when defined on
119 * stack with lockdep enabled. Use the following macros in such cases.
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100120 */
121#ifdef CONFIG_LOCKDEP
122# define KTHREAD_WORKER_INIT_ONSTACK(worker) \
Petr Mladek39891442016-10-11 13:55:20 -0700123 ({ kthread_init_worker(&worker); worker; })
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100124# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \
125 struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100126#else
127# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100128#endif
Tejun Heob56c0d82010-06-29 10:07:09 +0200129
Petr Mladek39891442016-10-11 13:55:20 -0700130extern void __kthread_init_worker(struct kthread_worker *worker,
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100131 const char *name, struct lock_class_key *key);
132
Petr Mladek39891442016-10-11 13:55:20 -0700133#define kthread_init_worker(worker) \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100134 do { \
135 static struct lock_class_key __key; \
Petr Mladek39891442016-10-11 13:55:20 -0700136 __kthread_init_worker((worker), "("#worker")->lock", &__key); \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100137 } while (0)
138
Petr Mladek39891442016-10-11 13:55:20 -0700139#define kthread_init_work(work, fn) \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100140 do { \
141 memset((work), 0, sizeof(struct kthread_work)); \
142 INIT_LIST_HEAD(&(work)->node); \
143 (work)->func = (fn); \
Yong Zhang4f32e9b2010-12-22 10:27:53 +0100144 } while (0)
Tejun Heob56c0d82010-06-29 10:07:09 +0200145
Petr Mladek22597dc2016-10-11 13:55:40 -0700146#define kthread_init_delayed_work(dwork, fn) \
147 do { \
148 kthread_init_work(&(dwork)->work, (fn)); \
149 __setup_timer(&(dwork)->timer, \
150 kthread_delayed_work_timer_fn, \
151 (unsigned long)(dwork), \
152 TIMER_IRQSAFE); \
153 } while (0)
154
Tejun Heob56c0d82010-06-29 10:07:09 +0200155int kthread_worker_fn(void *worker_ptr);
156
Petr Mladekfbae2d42016-10-11 13:55:30 -0700157__printf(1, 2)
158struct kthread_worker *
159kthread_create_worker(const char namefmt[], ...);
160
161struct kthread_worker *
162kthread_create_worker_on_cpu(int cpu, const char namefmt[], ...);
163
Petr Mladek39891442016-10-11 13:55:20 -0700164bool kthread_queue_work(struct kthread_worker *worker,
Tejun Heob56c0d82010-06-29 10:07:09 +0200165 struct kthread_work *work);
Petr Mladek22597dc2016-10-11 13:55:40 -0700166
167bool kthread_queue_delayed_work(struct kthread_worker *worker,
168 struct kthread_delayed_work *dwork,
169 unsigned long delay);
170
Petr Mladek39891442016-10-11 13:55:20 -0700171void kthread_flush_work(struct kthread_work *work);
172void kthread_flush_worker(struct kthread_worker *worker);
Tejun Heob56c0d82010-06-29 10:07:09 +0200173
Petr Mladek37be45d2016-10-11 13:55:43 -0700174bool kthread_cancel_work_sync(struct kthread_work *work);
175bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
176
Petr Mladek35033fe2016-10-11 13:55:33 -0700177void kthread_destroy_worker(struct kthread_worker *worker);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#endif /* _LINUX_KTHREAD_H */