FILE
kthread.h
File preview with syntax highlighting.
linux-6.19.14/include/linux/kthread.h
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 8struct mm_struct; 9 10/* opaque kthread data */ 11struct kthread; 12 13/* 14 * When "(p->flags & PF_KTHREAD)" is set the task is a kthread and will 15 * always remain a kthread. For kthreads p->worker_private always 16 * points to a struct kthread. For tasks that are not kthreads 17 * p->worker_private is used to point to other things. 18 * 19 * Return NULL for any task that is not a kthread. 20 */ 21static inline struct kthread *tsk_is_kthread(struct task_struct *p) 22{ 23 if (p->flags & PF_KTHREAD) 24 return p->worker_private; 25 return NULL; 26} 27 28__printf(4, 5) 29struct task_struct *kthread_create_on_node(int (*threadfn)(void *data), 30 void *data, 31 int node, 32 const char namefmt[], ...); 33 34/** 35 * kthread_create - create a kthread on the current node 36 * @threadfn: the function to run in the thread 37 * @data: data pointer for @threadfn() 38 * @namefmt: printf-style format string for the thread name 39 * @arg: arguments for @namefmt. 40 * 41 * This macro will create a kthread on the current node, leaving it in 42 * the stopped state. This is just a helper for kthread_create_on_node(); 43 * see the documentation there for more details. 44 */ 45#define kthread_create(threadfn, data, namefmt, arg...) \ 46 kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg) 47 48 49struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), 50 void *data, 51 unsigned int cpu, 52 const char *namefmt); 53 54void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk); 55bool set_kthread_struct(struct task_struct *p); 56 57void kthread_set_per_cpu(struct task_struct *k, int cpu); 58bool kthread_is_per_cpu(struct task_struct *k); 59 60/** 61 * kthread_run - create and wake a thread. 62 * @threadfn: the function to run until signal_pending(current). 63 * @data: data ptr for @threadfn. 64 * @namefmt: printf-style name for the thread. 65 * 66 * Description: Convenient wrapper for kthread_create() followed by 67 * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM). 68 */ 69#define kthread_run(threadfn, data, namefmt, ...) \ 70({ \ 71 struct task_struct *__k \ 72 = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \ 73 if (!IS_ERR(__k)) \ 74 wake_up_process(__k); \ 75 __k; \ 76}) 77 78/** 79 * kthread_run_on_cpu - create and wake a cpu bound thread. 80 * @threadfn: the function to run until signal_pending(current). 81 * @data: data ptr for @threadfn. 82 * @cpu: The cpu on which the thread should be bound, 83 * @namefmt: printf-style name for the thread. Format is restricted 84 * to "name.*%u". Code fills in cpu number. 85 * 86 * Description: Convenient wrapper for kthread_create_on_cpu() 87 * followed by wake_up_process(). Returns the kthread or 88 * ERR_PTR(-ENOMEM). 89 */ 90static inline struct task_struct * 91kthread_run_on_cpu(int (*threadfn)(void *data), void *data, 92 unsigned int cpu, const char *namefmt) 93{ 94 struct task_struct *p; 95 96 p = kthread_create_on_cpu(threadfn, data, cpu, namefmt); 97 if (!IS_ERR(p)) 98 wake_up_process(p); 99 100 return p; 101} 102 103void free_kthread_struct(struct task_struct *k); 104void kthread_bind(struct task_struct *k, unsigned int cpu); 105void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask); 106int kthread_affine_preferred(struct task_struct *p, const struct cpumask *mask); 107int kthread_stop(struct task_struct *k); 108int kthread_stop_put(struct task_struct *k); 109bool kthread_should_stop(void); 110bool kthread_should_park(void); 111bool kthread_should_stop_or_park(void); 112bool kthread_freezable_should_stop(bool *was_frozen); 113void *kthread_func(struct task_struct *k); 114void *kthread_data(struct task_struct *k); 115void *kthread_probe_data(struct task_struct *k); 116int kthread_park(struct task_struct *k); 117void kthread_unpark(struct task_struct *k); 118void kthread_parkme(void); 119#define kthread_exit(result) do_exit(result) 120void kthread_complete_and_exit(struct completion *, long) __noreturn; 121void kthread_do_exit(struct kthread *, long); 122 123int kthreadd(void *unused); 124extern struct task_struct *kthreadd_task; 125extern int tsk_fork_get_node(struct task_struct *tsk); 126 127/* 128 * Simple work processor based on kthread. 129 * 130 * This provides easier way to make use of kthreads. A kthread_work 131 * can be queued and flushed using queue/kthread_flush_work() 132 * respectively. Queued kthread_works are processed by a kthread 133 * running kthread_worker_fn(). 134 */ 135struct kthread_work; 136typedef void (*kthread_work_func_t)(struct kthread_work *work); 137void kthread_delayed_work_timer_fn(struct timer_list *t); 138 139enum { 140 KTW_FREEZABLE = 1 << 0, /* freeze during suspend */ 141}; 142 143struct kthread_worker { 144 unsigned int flags; 145 raw_spinlock_t lock; 146 struct list_head work_list; 147 struct list_head delayed_work_list; 148 struct task_struct *task; 149 struct kthread_work *current_work; 150}; 151 152struct kthread_work { 153 struct list_head node; 154 kthread_work_func_t func; 155 struct kthread_worker *worker; 156 /* Number of canceling calls that are running at the moment. */ 157 int canceling; 158}; 159 160struct kthread_delayed_work { 161 struct kthread_work work; 162 struct timer_list timer; 163}; 164 165#define KTHREAD_WORK_INIT(work, fn) { \ 166 .node = LIST_HEAD_INIT((work).node), \ 167 .func = (fn), \ 168 } 169 170#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \ 171 .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \ 172 .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\ 173 TIMER_IRQSAFE), \ 174 } 175 176#define DEFINE_KTHREAD_WORK(work, fn) \ 177 struct kthread_work work = KTHREAD_WORK_INIT(work, fn) 178 179#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \ 180 struct kthread_delayed_work dwork = \ 181 KTHREAD_DELAYED_WORK_INIT(dwork, fn) 182 183extern void __kthread_init_worker(struct kthread_worker *worker, 184 const char *name, struct lock_class_key *key); 185 186#define kthread_init_worker(worker) \ 187 do { \ 188 static struct lock_class_key __key; \ 189 __kthread_init_worker((worker), "("#worker")->lock", &__key); \ 190 } while (0) 191 192#define kthread_init_work(work, fn) \ 193 do { \ 194 memset((work), 0, sizeof(struct kthread_work)); \ 195 INIT_LIST_HEAD(&(work)->node); \ 196 (work)->func = (fn); \ 197 } while (0) 198 199#define kthread_init_delayed_work(dwork, fn) \ 200 do { \ 201 kthread_init_work(&(dwork)->work, (fn)); \ 202 timer_setup(&(dwork)->timer, \ 203 kthread_delayed_work_timer_fn, \ 204 TIMER_IRQSAFE); \ 205 } while (0) 206 207int kthread_worker_fn(void *worker_ptr); 208 209__printf(3, 4) 210struct kthread_worker *kthread_create_worker_on_node(unsigned int flags, 211 int node, 212 const char namefmt[], ...); 213 214#define kthread_create_worker(flags, namefmt, ...) \ 215 kthread_create_worker_on_node(flags, NUMA_NO_NODE, namefmt, ## __VA_ARGS__); 216 217/** 218 * kthread_run_worker - create and wake a kthread worker. 219 * @flags: flags modifying the default behavior of the worker 220 * @namefmt: printf-style name for the thread. 221 * 222 * Description: Convenient wrapper for kthread_create_worker() followed by 223 * wake_up_process(). Returns the kthread_worker or ERR_PTR(-ENOMEM). 224 */ 225#define kthread_run_worker(flags, namefmt, ...) \ 226({ \ 227 struct kthread_worker *__kw \ 228 = kthread_create_worker(flags, namefmt, ## __VA_ARGS__); \ 229 if (!IS_ERR(__kw)) \ 230 wake_up_process(__kw->task); \ 231 __kw; \ 232}) 233 234struct kthread_worker * 235kthread_create_worker_on_cpu(int cpu, unsigned int flags, 236 const char namefmt[]); 237 238/** 239 * kthread_run_worker_on_cpu - create and wake a cpu bound kthread worker. 240 * @cpu: CPU number 241 * @flags: flags modifying the default behavior of the worker 242 * @namefmt: printf-style name for the thread. Format is restricted 243 * to "name.*%u". Code fills in cpu number. 244 * 245 * Description: Convenient wrapper for kthread_create_worker_on_cpu() 246 * followed by wake_up_process(). Returns the kthread_worker or 247 * ERR_PTR(-ENOMEM). 248 */ 249static inline struct kthread_worker * 250kthread_run_worker_on_cpu(int cpu, unsigned int flags, 251 const char namefmt[]) 252{ 253 struct kthread_worker *kw; 254 255 kw = kthread_create_worker_on_cpu(cpu, flags, namefmt); 256 if (!IS_ERR(kw)) 257 wake_up_process(kw->task); 258 259 return kw; 260} 261 262bool kthread_queue_work(struct kthread_worker *worker, 263 struct kthread_work *work); 264 265bool kthread_queue_delayed_work(struct kthread_worker *worker, 266 struct kthread_delayed_work *dwork, 267 unsigned long delay); 268 269bool kthread_mod_delayed_work(struct kthread_worker *worker, 270 struct kthread_delayed_work *dwork, 271 unsigned long delay); 272 273void kthread_flush_work(struct kthread_work *work); 274void kthread_flush_worker(struct kthread_worker *worker); 275 276bool kthread_cancel_work_sync(struct kthread_work *work); 277bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work); 278 279void kthread_destroy_worker(struct kthread_worker *worker); 280 281void kthread_use_mm(struct mm_struct *mm); 282void kthread_unuse_mm(struct mm_struct *mm); 283 284struct cgroup_subsys_state; 285 286#ifdef CONFIG_BLK_CGROUP 287void kthread_associate_blkcg(struct cgroup_subsys_state *css); 288struct cgroup_subsys_state *kthread_blkcg(void); 289#else 290static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { } 291#endif 292#endif /* _LINUX_KTHREAD_H */
~/
Read-only file preview.