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