Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef _LINUX_KTHREAD_H
0003 #define _LINUX_KTHREAD_H
0004 /* Simple interface for creating and stopping kernel threads without mess. */
0005 #include <linux/err.h>
0006 #include <linux/sched.h>
0007 
0008 struct mm_struct;
0009 
0010 __printf(4, 5)
0011 struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
0012                        void *data,
0013                        int node,
0014                        const char namefmt[], ...);
0015 
0016 /**
0017  * kthread_create - create a kthread on the current node
0018  * @threadfn: the function to run in the thread
0019  * @data: data pointer for @threadfn()
0020  * @namefmt: printf-style format string for the thread name
0021  * @arg: arguments for @namefmt.
0022  *
0023  * This macro will create a kthread on the current node, leaving it in
0024  * the stopped state.  This is just a helper for kthread_create_on_node();
0025  * see the documentation there for more details.
0026  */
0027 #define kthread_create(threadfn, data, namefmt, arg...) \
0028     kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
0029 
0030 
0031 struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
0032                       void *data,
0033                       unsigned int cpu,
0034                       const char *namefmt);
0035 
0036 void get_kthread_comm(char *buf, size_t buf_size, struct task_struct *tsk);
0037 bool set_kthread_struct(struct task_struct *p);
0038 
0039 void kthread_set_per_cpu(struct task_struct *k, int cpu);
0040 bool kthread_is_per_cpu(struct task_struct *k);
0041 
0042 /**
0043  * kthread_run - create and wake a thread.
0044  * @threadfn: the function to run until signal_pending(current).
0045  * @data: data ptr for @threadfn.
0046  * @namefmt: printf-style name for the thread.
0047  *
0048  * Description: Convenient wrapper for kthread_create() followed by
0049  * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
0050  */
0051 #define kthread_run(threadfn, data, namefmt, ...)              \
0052 ({                                     \
0053     struct task_struct *__k                        \
0054         = kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
0055     if (!IS_ERR(__k))                          \
0056         wake_up_process(__k);                      \
0057     __k;                                   \
0058 })
0059 
0060 /**
0061  * kthread_run_on_cpu - create and wake a cpu bound thread.
0062  * @threadfn: the function to run until signal_pending(current).
0063  * @data: data ptr for @threadfn.
0064  * @cpu: The cpu on which the thread should be bound,
0065  * @namefmt: printf-style name for the thread. Format is restricted
0066  *       to "name.*%u". Code fills in cpu number.
0067  *
0068  * Description: Convenient wrapper for kthread_create_on_cpu()
0069  * followed by wake_up_process().  Returns the kthread or
0070  * ERR_PTR(-ENOMEM).
0071  */
0072 static inline struct task_struct *
0073 kthread_run_on_cpu(int (*threadfn)(void *data), void *data,
0074             unsigned int cpu, const char *namefmt)
0075 {
0076     struct task_struct *p;
0077 
0078     p = kthread_create_on_cpu(threadfn, data, cpu, namefmt);
0079     if (!IS_ERR(p))
0080         wake_up_process(p);
0081 
0082     return p;
0083 }
0084 
0085 void free_kthread_struct(struct task_struct *k);
0086 void kthread_bind(struct task_struct *k, unsigned int cpu);
0087 void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
0088 int kthread_stop(struct task_struct *k);
0089 bool kthread_should_stop(void);
0090 bool kthread_should_park(void);
0091 bool __kthread_should_park(struct task_struct *k);
0092 bool kthread_freezable_should_stop(bool *was_frozen);
0093 void *kthread_func(struct task_struct *k);
0094 void *kthread_data(struct task_struct *k);
0095 void *kthread_probe_data(struct task_struct *k);
0096 int kthread_park(struct task_struct *k);
0097 void kthread_unpark(struct task_struct *k);
0098 void kthread_parkme(void);
0099 void kthread_exit(long result) __noreturn;
0100 void kthread_complete_and_exit(struct completion *, long) __noreturn;
0101 
0102 int kthreadd(void *unused);
0103 extern struct task_struct *kthreadd_task;
0104 extern int tsk_fork_get_node(struct task_struct *tsk);
0105 
0106 /*
0107  * Simple work processor based on kthread.
0108  *
0109  * This provides easier way to make use of kthreads.  A kthread_work
0110  * can be queued and flushed using queue/kthread_flush_work()
0111  * respectively.  Queued kthread_works are processed by a kthread
0112  * running kthread_worker_fn().
0113  */
0114 struct kthread_work;
0115 typedef void (*kthread_work_func_t)(struct kthread_work *work);
0116 void kthread_delayed_work_timer_fn(struct timer_list *t);
0117 
0118 enum {
0119     KTW_FREEZABLE       = 1 << 0,   /* freeze during suspend */
0120 };
0121 
0122 struct kthread_worker {
0123     unsigned int        flags;
0124     raw_spinlock_t      lock;
0125     struct list_head    work_list;
0126     struct list_head    delayed_work_list;
0127     struct task_struct  *task;
0128     struct kthread_work *current_work;
0129 };
0130 
0131 struct kthread_work {
0132     struct list_head    node;
0133     kthread_work_func_t func;
0134     struct kthread_worker   *worker;
0135     /* Number of canceling calls that are running at the moment. */
0136     int         canceling;
0137 };
0138 
0139 struct kthread_delayed_work {
0140     struct kthread_work work;
0141     struct timer_list timer;
0142 };
0143 
0144 #define KTHREAD_WORK_INIT(work, fn) {               \
0145     .node = LIST_HEAD_INIT((work).node),                \
0146     .func = (fn),                           \
0147     }
0148 
0149 #define KTHREAD_DELAYED_WORK_INIT(dwork, fn) {              \
0150     .work = KTHREAD_WORK_INIT((dwork).work, (fn)),          \
0151     .timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
0152                      TIMER_IRQSAFE),            \
0153     }
0154 
0155 #define DEFINE_KTHREAD_WORK(work, fn)                   \
0156     struct kthread_work work = KTHREAD_WORK_INIT(work, fn)
0157 
0158 #define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn)              \
0159     struct kthread_delayed_work dwork =             \
0160         KTHREAD_DELAYED_WORK_INIT(dwork, fn)
0161 
0162 extern void __kthread_init_worker(struct kthread_worker *worker,
0163             const char *name, struct lock_class_key *key);
0164 
0165 #define kthread_init_worker(worker)                 \
0166     do {                                \
0167         static struct lock_class_key __key;         \
0168         __kthread_init_worker((worker), "("#worker")->lock", &__key); \
0169     } while (0)
0170 
0171 #define kthread_init_work(work, fn)                 \
0172     do {                                \
0173         memset((work), 0, sizeof(struct kthread_work));     \
0174         INIT_LIST_HEAD(&(work)->node);              \
0175         (work)->func = (fn);                    \
0176     } while (0)
0177 
0178 #define kthread_init_delayed_work(dwork, fn)                \
0179     do {                                \
0180         kthread_init_work(&(dwork)->work, (fn));        \
0181         timer_setup(&(dwork)->timer,                \
0182                  kthread_delayed_work_timer_fn,     \
0183                  TIMER_IRQSAFE);                \
0184     } while (0)
0185 
0186 int kthread_worker_fn(void *worker_ptr);
0187 
0188 __printf(2, 3)
0189 struct kthread_worker *
0190 kthread_create_worker(unsigned int flags, const char namefmt[], ...);
0191 
0192 __printf(3, 4) struct kthread_worker *
0193 kthread_create_worker_on_cpu(int cpu, unsigned int flags,
0194                  const char namefmt[], ...);
0195 
0196 bool kthread_queue_work(struct kthread_worker *worker,
0197             struct kthread_work *work);
0198 
0199 bool kthread_queue_delayed_work(struct kthread_worker *worker,
0200                 struct kthread_delayed_work *dwork,
0201                 unsigned long delay);
0202 
0203 bool kthread_mod_delayed_work(struct kthread_worker *worker,
0204                   struct kthread_delayed_work *dwork,
0205                   unsigned long delay);
0206 
0207 void kthread_flush_work(struct kthread_work *work);
0208 void kthread_flush_worker(struct kthread_worker *worker);
0209 
0210 bool kthread_cancel_work_sync(struct kthread_work *work);
0211 bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);
0212 
0213 void kthread_destroy_worker(struct kthread_worker *worker);
0214 
0215 void kthread_use_mm(struct mm_struct *mm);
0216 void kthread_unuse_mm(struct mm_struct *mm);
0217 
0218 struct cgroup_subsys_state;
0219 
0220 #ifdef CONFIG_BLK_CGROUP
0221 void kthread_associate_blkcg(struct cgroup_subsys_state *css);
0222 struct cgroup_subsys_state *kthread_blkcg(void);
0223 #else
0224 static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
0225 #endif
0226 #endif /* _LINUX_KTHREAD_H */