1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 21da177e4SLinus Torvalds #ifndef _LINUX_KTHREAD_H 31da177e4SLinus Torvalds #define _LINUX_KTHREAD_H 41da177e4SLinus Torvalds /* Simple interface for creating and stopping kernel threads without mess. */ 51da177e4SLinus Torvalds #include <linux/err.h> 61da177e4SLinus Torvalds #include <linux/sched.h> 71da177e4SLinus Torvalds 89bf5b9ebSChristoph Hellwig struct mm_struct; 99bf5b9ebSChristoph Hellwig 10b9075fa9SJoe Perches __printf(4, 5) 11207205a2SEric Dumazet struct task_struct *kthread_create_on_node(int (*threadfn)(void *data), 121da177e4SLinus Torvalds void *data, 13207205a2SEric Dumazet int node, 14b9075fa9SJoe Perches const char namefmt[], ...); 15207205a2SEric Dumazet 16e154ccc8SJonathan Corbet /** 17e154ccc8SJonathan Corbet * kthread_create - create a kthread on the current node 18e154ccc8SJonathan Corbet * @threadfn: the function to run in the thread 19e154ccc8SJonathan Corbet * @data: data pointer for @threadfn() 20e154ccc8SJonathan Corbet * @namefmt: printf-style format string for the thread name 2120ce0c2dSJonathan Neuschäfer * @arg: arguments for @namefmt. 22e154ccc8SJonathan Corbet * 23e154ccc8SJonathan Corbet * This macro will create a kthread on the current node, leaving it in 24e154ccc8SJonathan Corbet * the stopped state. This is just a helper for kthread_create_on_node(); 25e154ccc8SJonathan Corbet * see the documentation there for more details. 26e154ccc8SJonathan Corbet */ 27207205a2SEric Dumazet #define kthread_create(threadfn, data, namefmt, arg...) \ 28e9f06986SAndrew Morton kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg) 29207205a2SEric Dumazet 301da177e4SLinus Torvalds 312a1d4460SThomas Gleixner struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data), 322a1d4460SThomas Gleixner void *data, 332a1d4460SThomas Gleixner unsigned int cpu, 342a1d4460SThomas Gleixner const char *namefmt); 352a1d4460SThomas Gleixner 3600b89fe0SValentin Schneider void set_kthread_struct(struct task_struct *p); 3700b89fe0SValentin Schneider 38ac687e6eSPeter Zijlstra void kthread_set_per_cpu(struct task_struct *k, int cpu); 39ac687e6eSPeter Zijlstra bool kthread_is_per_cpu(struct task_struct *k); 40ac687e6eSPeter Zijlstra 411da177e4SLinus Torvalds /** 429e37bd30SRandy Dunlap * kthread_run - create and wake a thread. 431da177e4SLinus Torvalds * @threadfn: the function to run until signal_pending(current). 441da177e4SLinus Torvalds * @data: data ptr for @threadfn. 451da177e4SLinus Torvalds * @namefmt: printf-style name for the thread. 461da177e4SLinus Torvalds * 471da177e4SLinus Torvalds * Description: Convenient wrapper for kthread_create() followed by 489e37bd30SRandy Dunlap * wake_up_process(). Returns the kthread or ERR_PTR(-ENOMEM). 499e37bd30SRandy Dunlap */ 501da177e4SLinus Torvalds #define kthread_run(threadfn, data, namefmt, ...) \ 511da177e4SLinus Torvalds ({ \ 521da177e4SLinus Torvalds struct task_struct *__k \ 531da177e4SLinus Torvalds = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \ 541da177e4SLinus Torvalds if (!IS_ERR(__k)) \ 551da177e4SLinus Torvalds wake_up_process(__k); \ 561da177e4SLinus Torvalds __k; \ 571da177e4SLinus Torvalds }) 581da177e4SLinus Torvalds 591da5c46fSOleg Nesterov void free_kthread_struct(struct task_struct *k); 601da177e4SLinus Torvalds void kthread_bind(struct task_struct *k, unsigned int cpu); 6125834c73SPeter Zijlstra void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask); 621da177e4SLinus Torvalds int kthread_stop(struct task_struct *k); 632a1d4460SThomas Gleixner bool kthread_should_stop(void); 642a1d4460SThomas Gleixner bool kthread_should_park(void); 650121805dSMatthias Kaehlcke bool __kthread_should_park(struct task_struct *k); 668a32c441STejun Heo bool kthread_freezable_should_stop(bool *was_frozen); 6752782c92SJ. Bruce Fields void *kthread_func(struct task_struct *k); 6882805ab7STejun Heo void *kthread_data(struct task_struct *k); 69e700591aSPetr Mladek void *kthread_probe_data(struct task_struct *k); 702a1d4460SThomas Gleixner int kthread_park(struct task_struct *k); 712a1d4460SThomas Gleixner void kthread_unpark(struct task_struct *k); 722a1d4460SThomas Gleixner void kthread_parkme(void); 73*bbda86e9SEric W. Biederman void kthread_exit(long result) __noreturn; 741da177e4SLinus Torvalds 7573c27992SEric W. Biederman int kthreadd(void *unused); 7673c27992SEric W. Biederman extern struct task_struct *kthreadd_task; 77207205a2SEric Dumazet extern int tsk_fork_get_node(struct task_struct *tsk); 7873c27992SEric W. Biederman 79b56c0d89STejun Heo /* 80b56c0d89STejun Heo * Simple work processor based on kthread. 81b56c0d89STejun Heo * 82b56c0d89STejun Heo * This provides easier way to make use of kthreads. A kthread_work 833989144fSPetr Mladek * can be queued and flushed using queue/kthread_flush_work() 84b56c0d89STejun Heo * respectively. Queued kthread_works are processed by a kthread 85b56c0d89STejun Heo * running kthread_worker_fn(). 86b56c0d89STejun Heo */ 87b56c0d89STejun Heo struct kthread_work; 88b56c0d89STejun Heo typedef void (*kthread_work_func_t)(struct kthread_work *work); 89fe5c3b69SKees Cook void kthread_delayed_work_timer_fn(struct timer_list *t); 90b56c0d89STejun Heo 91dbf52682SPetr Mladek enum { 92dbf52682SPetr Mladek KTW_FREEZABLE = 1 << 0, /* freeze during suspend */ 93dbf52682SPetr Mladek }; 94dbf52682SPetr Mladek 95b56c0d89STejun Heo struct kthread_worker { 96dbf52682SPetr Mladek unsigned int flags; 97fe99a4f4SJulia Cartwright raw_spinlock_t lock; 98b56c0d89STejun Heo struct list_head work_list; 9922597dc3SPetr Mladek struct list_head delayed_work_list; 100b56c0d89STejun Heo struct task_struct *task; 10146f3d976STejun Heo struct kthread_work *current_work; 102b56c0d89STejun Heo }; 103b56c0d89STejun Heo 104b56c0d89STejun Heo struct kthread_work { 105b56c0d89STejun Heo struct list_head node; 106b56c0d89STejun Heo kthread_work_func_t func; 10746f3d976STejun Heo struct kthread_worker *worker; 10837be45d4SPetr Mladek /* Number of canceling calls that are running at the moment. */ 10937be45d4SPetr Mladek int canceling; 110b56c0d89STejun Heo }; 111b56c0d89STejun Heo 11222597dc3SPetr Mladek struct kthread_delayed_work { 11322597dc3SPetr Mladek struct kthread_work work; 11422597dc3SPetr Mladek struct timer_list timer; 11522597dc3SPetr Mladek }; 11622597dc3SPetr Mladek 117b56c0d89STejun Heo #define KTHREAD_WORKER_INIT(worker) { \ 118fe99a4f4SJulia Cartwright .lock = __RAW_SPIN_LOCK_UNLOCKED((worker).lock), \ 119b56c0d89STejun Heo .work_list = LIST_HEAD_INIT((worker).work_list), \ 12022597dc3SPetr Mladek .delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\ 121b56c0d89STejun Heo } 122b56c0d89STejun Heo 123b56c0d89STejun Heo #define KTHREAD_WORK_INIT(work, fn) { \ 124b56c0d89STejun Heo .node = LIST_HEAD_INIT((work).node), \ 125b56c0d89STejun Heo .func = (fn), \ 126b56c0d89STejun Heo } 127b56c0d89STejun Heo 12822597dc3SPetr Mladek #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) { \ 12922597dc3SPetr Mladek .work = KTHREAD_WORK_INIT((dwork).work, (fn)), \ 130841b86f3SKees Cook .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\ 13122597dc3SPetr Mladek TIMER_IRQSAFE), \ 13222597dc3SPetr Mladek } 13322597dc3SPetr Mladek 134b56c0d89STejun Heo #define DEFINE_KTHREAD_WORKER(worker) \ 135b56c0d89STejun Heo struct kthread_worker worker = KTHREAD_WORKER_INIT(worker) 136b56c0d89STejun Heo 137b56c0d89STejun Heo #define DEFINE_KTHREAD_WORK(work, fn) \ 138b56c0d89STejun Heo struct kthread_work work = KTHREAD_WORK_INIT(work, fn) 139b56c0d89STejun Heo 14022597dc3SPetr Mladek #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn) \ 14122597dc3SPetr Mladek struct kthread_delayed_work dwork = \ 14222597dc3SPetr Mladek KTHREAD_DELAYED_WORK_INIT(dwork, fn) 14322597dc3SPetr Mladek 1444f32e9b1SYong Zhang /* 14595847e1bSLai Jiangshan * kthread_worker.lock needs its own lockdep class key when defined on 14695847e1bSLai Jiangshan * stack with lockdep enabled. Use the following macros in such cases. 1474f32e9b1SYong Zhang */ 1484f32e9b1SYong Zhang #ifdef CONFIG_LOCKDEP 1494f32e9b1SYong Zhang # define KTHREAD_WORKER_INIT_ONSTACK(worker) \ 1503989144fSPetr Mladek ({ kthread_init_worker(&worker); worker; }) 1514f32e9b1SYong Zhang # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) \ 1524f32e9b1SYong Zhang struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker) 1534f32e9b1SYong Zhang #else 1544f32e9b1SYong Zhang # define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker) 1554f32e9b1SYong Zhang #endif 156b56c0d89STejun Heo 1573989144fSPetr Mladek extern void __kthread_init_worker(struct kthread_worker *worker, 1584f32e9b1SYong Zhang const char *name, struct lock_class_key *key); 1594f32e9b1SYong Zhang 1603989144fSPetr Mladek #define kthread_init_worker(worker) \ 1614f32e9b1SYong Zhang do { \ 1624f32e9b1SYong Zhang static struct lock_class_key __key; \ 1633989144fSPetr Mladek __kthread_init_worker((worker), "("#worker")->lock", &__key); \ 1644f32e9b1SYong Zhang } while (0) 1654f32e9b1SYong Zhang 1663989144fSPetr Mladek #define kthread_init_work(work, fn) \ 1674f32e9b1SYong Zhang do { \ 1684f32e9b1SYong Zhang memset((work), 0, sizeof(struct kthread_work)); \ 1694f32e9b1SYong Zhang INIT_LIST_HEAD(&(work)->node); \ 1704f32e9b1SYong Zhang (work)->func = (fn); \ 1714f32e9b1SYong Zhang } while (0) 172b56c0d89STejun Heo 17322597dc3SPetr Mladek #define kthread_init_delayed_work(dwork, fn) \ 17422597dc3SPetr Mladek do { \ 17522597dc3SPetr Mladek kthread_init_work(&(dwork)->work, (fn)); \ 176ad01423aSSebastian Andrzej Siewior timer_setup(&(dwork)->timer, \ 17798c985d7SPetr Mladek kthread_delayed_work_timer_fn, \ 17898c985d7SPetr Mladek TIMER_IRQSAFE); \ 17922597dc3SPetr Mladek } while (0) 18022597dc3SPetr Mladek 181b56c0d89STejun Heo int kthread_worker_fn(void *worker_ptr); 182b56c0d89STejun Heo 183dbf52682SPetr Mladek __printf(2, 3) 184fbae2d44SPetr Mladek struct kthread_worker * 185dbf52682SPetr Mladek kthread_create_worker(unsigned int flags, const char namefmt[], ...); 186fbae2d44SPetr Mladek 187c0b942a7SNicolas Iooss __printf(3, 4) struct kthread_worker * 188dbf52682SPetr Mladek kthread_create_worker_on_cpu(int cpu, unsigned int flags, 189dbf52682SPetr Mladek const char namefmt[], ...); 190fbae2d44SPetr Mladek 1913989144fSPetr Mladek bool kthread_queue_work(struct kthread_worker *worker, 192b56c0d89STejun Heo struct kthread_work *work); 19322597dc3SPetr Mladek 19422597dc3SPetr Mladek bool kthread_queue_delayed_work(struct kthread_worker *worker, 19522597dc3SPetr Mladek struct kthread_delayed_work *dwork, 19622597dc3SPetr Mladek unsigned long delay); 19722597dc3SPetr Mladek 1989a6b06c8SPetr Mladek bool kthread_mod_delayed_work(struct kthread_worker *worker, 1999a6b06c8SPetr Mladek struct kthread_delayed_work *dwork, 2009a6b06c8SPetr Mladek unsigned long delay); 2019a6b06c8SPetr Mladek 2023989144fSPetr Mladek void kthread_flush_work(struct kthread_work *work); 2033989144fSPetr Mladek void kthread_flush_worker(struct kthread_worker *worker); 204b56c0d89STejun Heo 20537be45d4SPetr Mladek bool kthread_cancel_work_sync(struct kthread_work *work); 20637be45d4SPetr Mladek bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work); 20737be45d4SPetr Mladek 20835033fe9SPetr Mladek void kthread_destroy_worker(struct kthread_worker *worker); 20935033fe9SPetr Mladek 210f5678e7fSChristoph Hellwig void kthread_use_mm(struct mm_struct *mm); 211f5678e7fSChristoph Hellwig void kthread_unuse_mm(struct mm_struct *mm); 2129bf5b9ebSChristoph Hellwig 2138af0c18aSSuren Baghdasaryan struct cgroup_subsys_state; 2148af0c18aSSuren Baghdasaryan 2150b508bc9SShaohua Li #ifdef CONFIG_BLK_CGROUP 21605e3db95SShaohua Li void kthread_associate_blkcg(struct cgroup_subsys_state *css); 21705e3db95SShaohua Li struct cgroup_subsys_state *kthread_blkcg(void); 21805e3db95SShaohua Li #else 21905e3db95SShaohua Li static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { } 22005e3db95SShaohua Li static inline struct cgroup_subsys_state *kthread_blkcg(void) 22105e3db95SShaohua Li { 22205e3db95SShaohua Li return NULL; 22305e3db95SShaohua Li } 22405e3db95SShaohua Li #endif 2251da177e4SLinus Torvalds #endif /* _LINUX_KTHREAD_H */ 226