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