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