Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 7 | __printf(4, 5) |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 8 | struct task_struct *kthread_create_on_node(int (*threadfn)(void *data), |
| 9 | void *data, |
| 10 | int node, |
Joe Perches | b9075fa | 2011-10-31 17:11:33 -0700 | [diff] [blame] | 11 | const char namefmt[], ...); |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 12 | |
Jonathan Corbet | e154ccc | 2016-10-11 13:55:53 -0700 | [diff] [blame] | 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 |
| 18 | * @...: arguments for @namefmt. |
| 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 | */ |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 24 | #define kthread_create(threadfn, data, namefmt, arg...) \ |
Andrew Morton | e9f0698 | 2015-09-04 15:42:42 -0700 | [diff] [blame] | 25 | kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg) |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 28 | struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), |
| 29 | void *data, |
| 30 | unsigned int cpu, |
| 31 | const char *namefmt); |
| 32 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | /** |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 34 | * kthread_run - create and wake a thread. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Randy Dunlap | 9e37bd3 | 2006-06-25 05:49:19 -0700 | [diff] [blame] | 40 | * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM). |
| 41 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Oleg Nesterov | 1da5c46 | 2016-11-29 18:50:57 +0100 | [diff] [blame] | 51 | void free_kthread_struct(struct task_struct *k); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | void kthread_bind(struct task_struct *k, unsigned int cpu); |
Peter Zijlstra | 25834c7 | 2015-05-15 17:43:34 +0200 | [diff] [blame] | 53 | void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | int kthread_stop(struct task_struct *k); |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 55 | bool kthread_should_stop(void); |
| 56 | bool kthread_should_park(void); |
Tejun Heo | 8a32c44 | 2011-11-21 12:32:23 -0800 | [diff] [blame] | 57 | bool kthread_freezable_should_stop(bool *was_frozen); |
Tejun Heo | 82805ab | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 58 | void *kthread_data(struct task_struct *k); |
Petr Mladek | e700591 | 2016-10-11 13:55:17 -0700 | [diff] [blame] | 59 | void *kthread_probe_data(struct task_struct *k); |
Thomas Gleixner | 2a1d446 | 2012-07-16 10:42:36 +0000 | [diff] [blame] | 60 | int kthread_park(struct task_struct *k); |
| 61 | void kthread_unpark(struct task_struct *k); |
| 62 | void kthread_parkme(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 64 | int kthreadd(void *unused); |
| 65 | extern struct task_struct *kthreadd_task; |
Eric Dumazet | 207205a | 2011-03-22 16:30:44 -0700 | [diff] [blame] | 66 | extern int tsk_fork_get_node(struct task_struct *tsk); |
Eric W. Biederman | 73c2799 | 2007-05-09 02:34:32 -0700 | [diff] [blame] | 67 | |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 68 | /* |
| 69 | * Simple work processor based on kthread. |
| 70 | * |
| 71 | * This provides easier way to make use of kthreads. A kthread_work |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 72 | * can be queued and flushed using queue/kthread_flush_work() |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 73 | * respectively. Queued kthread_works are processed by a kthread |
| 74 | * running kthread_worker_fn(). |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 75 | */ |
| 76 | struct kthread_work; |
| 77 | typedef void (*kthread_work_func_t)(struct kthread_work *work); |
Petr Mladek | 22597dc | 2016-10-11 13:55:40 -0700 | [diff] [blame] | 78 | void kthread_delayed_work_timer_fn(unsigned long __data); |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 79 | |
Petr Mladek | dbf5268 | 2016-10-11 13:55:50 -0700 | [diff] [blame] | 80 | enum { |
| 81 | KTW_FREEZABLE = 1 << 0, /* freeze during suspend */ |
| 82 | }; |
| 83 | |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 84 | struct kthread_worker { |
Petr Mladek | dbf5268 | 2016-10-11 13:55:50 -0700 | [diff] [blame] | 85 | unsigned int flags; |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 86 | spinlock_t lock; |
| 87 | struct list_head work_list; |
Petr Mladek | 22597dc | 2016-10-11 13:55:40 -0700 | [diff] [blame] | 88 | struct list_head delayed_work_list; |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 89 | struct task_struct *task; |
Tejun Heo | 46f3d97 | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 90 | struct kthread_work *current_work; |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | struct kthread_work { |
| 94 | struct list_head node; |
| 95 | kthread_work_func_t func; |
Tejun Heo | 46f3d97 | 2012-07-19 13:52:53 -0700 | [diff] [blame] | 96 | struct kthread_worker *worker; |
Petr Mladek | 37be45d | 2016-10-11 13:55:43 -0700 | [diff] [blame] | 97 | /* Number of canceling calls that are running at the moment. */ |
| 98 | int canceling; |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Petr Mladek | 22597dc | 2016-10-11 13:55:40 -0700 | [diff] [blame] | 101 | struct kthread_delayed_work { |
| 102 | struct kthread_work work; |
| 103 | struct timer_list timer; |
| 104 | }; |
| 105 | |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 106 | #define KTHREAD_WORKER_INIT(worker) { \ |
Thomas Gleixner | 92578c0b | 2011-01-23 15:24:55 +0100 | [diff] [blame] | 107 | .lock = __SPIN_LOCK_UNLOCKED((worker).lock), \ |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 108 | .work_list = LIST_HEAD_INIT((worker).work_list), \ |
Petr Mladek | 22597dc | 2016-10-11 13:55:40 -0700 | [diff] [blame] | 109 | .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\ |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | #define KTHREAD_WORK_INIT(work, fn) { \ |
| 113 | .node = LIST_HEAD_INIT((work).node), \ |
| 114 | .func = (fn), \ |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Petr Mladek | 22597dc | 2016-10-11 13:55:40 -0700 | [diff] [blame] | 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 | |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 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 | |
Petr Mladek | 22597dc | 2016-10-11 13:55:40 -0700 | [diff] [blame] | 130 | #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \ |
| 131 | struct kthread_delayed_work dwork = \ |
| 132 | KTHREAD_DELAYED_WORK_INIT(dwork, fn) |
| 133 | |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 134 | /* |
Lai Jiangshan | 95847e1 | 2014-07-26 12:04:00 +0800 | [diff] [blame] | 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. |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 137 | */ |
| 138 | #ifdef CONFIG_LOCKDEP |
| 139 | # define KTHREAD_WORKER_INIT_ONSTACK(worker) \ |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 140 | ({ kthread_init_worker(&worker); worker; }) |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 141 | # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \ |
| 142 | struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker) |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 143 | #else |
| 144 | # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker) |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 145 | #endif |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 146 | |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 147 | extern void __kthread_init_worker(struct kthread_worker *worker, |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 148 | const char *name, struct lock_class_key *key); |
| 149 | |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 150 | #define kthread_init_worker(worker) \ |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 151 | do { \ |
| 152 | static struct lock_class_key __key; \ |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 153 | __kthread_init_worker((worker), "("#worker")->lock", &__key); \ |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 154 | } while (0) |
| 155 | |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 156 | #define kthread_init_work(work, fn) \ |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 157 | do { \ |
| 158 | memset((work), 0, sizeof(struct kthread_work)); \ |
| 159 | INIT_LIST_HEAD(&(work)->node); \ |
| 160 | (work)->func = (fn); \ |
Yong Zhang | 4f32e9b | 2010-12-22 10:27:53 +0100 | [diff] [blame] | 161 | } while (0) |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 162 | |
Petr Mladek | 22597dc | 2016-10-11 13:55:40 -0700 | [diff] [blame] | 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 | |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 172 | int kthread_worker_fn(void *worker_ptr); |
| 173 | |
Petr Mladek | dbf5268 | 2016-10-11 13:55:50 -0700 | [diff] [blame] | 174 | __printf(2, 3) |
Petr Mladek | fbae2d4 | 2016-10-11 13:55:30 -0700 | [diff] [blame] | 175 | struct kthread_worker * |
Petr Mladek | dbf5268 | 2016-10-11 13:55:50 -0700 | [diff] [blame] | 176 | kthread_create_worker(unsigned int flags, const char namefmt[], ...); |
Petr Mladek | fbae2d4 | 2016-10-11 13:55:30 -0700 | [diff] [blame] | 177 | |
Nicolas Iooss | c0b942a | 2016-12-12 16:40:39 -0800 | [diff] [blame] | 178 | __printf(3, 4) struct kthread_worker * |
Petr Mladek | dbf5268 | 2016-10-11 13:55:50 -0700 | [diff] [blame] | 179 | kthread_create_worker_on_cpu(int cpu, unsigned int flags, |
| 180 | const char namefmt[], ...); |
Petr Mladek | fbae2d4 | 2016-10-11 13:55:30 -0700 | [diff] [blame] | 181 | |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 182 | bool kthread_queue_work(struct kthread_worker *worker, |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 183 | struct kthread_work *work); |
Petr Mladek | 22597dc | 2016-10-11 13:55:40 -0700 | [diff] [blame] | 184 | |
| 185 | bool kthread_queue_delayed_work(struct kthread_worker *worker, |
| 186 | struct kthread_delayed_work *dwork, |
| 187 | unsigned long delay); |
| 188 | |
Petr Mladek | 9a6b06c | 2016-10-11 13:55:46 -0700 | [diff] [blame] | 189 | bool kthread_mod_delayed_work(struct kthread_worker *worker, |
| 190 | struct kthread_delayed_work *dwork, |
| 191 | unsigned long delay); |
| 192 | |
Petr Mladek | 3989144 | 2016-10-11 13:55:20 -0700 | [diff] [blame] | 193 | void kthread_flush_work(struct kthread_work *work); |
| 194 | void kthread_flush_worker(struct kthread_worker *worker); |
Tejun Heo | b56c0d8 | 2010-06-29 10:07:09 +0200 | [diff] [blame] | 195 | |
Petr Mladek | 37be45d | 2016-10-11 13:55:43 -0700 | [diff] [blame] | 196 | bool kthread_cancel_work_sync(struct kthread_work *work); |
| 197 | bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work); |
| 198 | |
Petr Mladek | 35033fe | 2016-10-11 13:55:33 -0700 | [diff] [blame] | 199 | void kthread_destroy_worker(struct kthread_worker *worker); |
| 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | #endif /* _LINUX_KTHREAD_H */ |