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