kthread.h 6.6 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
L
Linus Torvalds 已提交
2 3 4 5 6
#ifndef _LINUX_KTHREAD_H
#define _LINUX_KTHREAD_H
/* Simple interface for creating and stopping kernel threads without mess. */
#include <linux/err.h>
#include <linux/sched.h>
7
#include <linux/cgroup.h>
L
Linus Torvalds 已提交
8

9
__printf(4, 5)
10 11 12
struct task_struct *kthread_create_on_node(int (*threadfn)(void *data),
					   void *data,
					   int node,
13
					   const char namefmt[], ...);
14

15 16 17 18 19
/**
 * kthread_create - create a kthread on the current node
 * @threadfn: the function to run in the thread
 * @data: data pointer for @threadfn()
 * @namefmt: printf-style format string for the thread name
20
 * @arg...: arguments for @namefmt.
21 22 23 24 25
 *
 * This macro will create a kthread on the current node, leaving it in
 * the stopped state.  This is just a helper for kthread_create_on_node();
 * see the documentation there for more details.
 */
26
#define kthread_create(threadfn, data, namefmt, arg...) \
27
	kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
28

L
Linus Torvalds 已提交
29

30 31 32 33 34
struct task_struct *kthread_create_on_cpu(int (*threadfn)(void *data),
					  void *data,
					  unsigned int cpu,
					  const char *namefmt);

L
Linus Torvalds 已提交
35
/**
36
 * kthread_run - create and wake a thread.
L
Linus Torvalds 已提交
37 38 39 40 41
 * @threadfn: the function to run until signal_pending(current).
 * @data: data ptr for @threadfn.
 * @namefmt: printf-style name for the thread.
 *
 * Description: Convenient wrapper for kthread_create() followed by
42 43
 * wake_up_process().  Returns the kthread or ERR_PTR(-ENOMEM).
 */
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52
#define kthread_run(threadfn, data, namefmt, ...)			   \
({									   \
	struct task_struct *__k						   \
		= kthread_create(threadfn, data, namefmt, ## __VA_ARGS__); \
	if (!IS_ERR(__k))						   \
		wake_up_process(__k);					   \
	__k;								   \
})

53
void free_kthread_struct(struct task_struct *k);
L
Linus Torvalds 已提交
54
void kthread_bind(struct task_struct *k, unsigned int cpu);
55
void kthread_bind_mask(struct task_struct *k, const struct cpumask *mask);
L
Linus Torvalds 已提交
56
int kthread_stop(struct task_struct *k);
57 58
bool kthread_should_stop(void);
bool kthread_should_park(void);
59
bool __kthread_should_park(struct task_struct *k);
60
bool kthread_freezable_should_stop(bool *was_frozen);
T
Tejun Heo 已提交
61
void *kthread_data(struct task_struct *k);
62
void *kthread_probe_data(struct task_struct *k);
63 64 65
int kthread_park(struct task_struct *k);
void kthread_unpark(struct task_struct *k);
void kthread_parkme(void);
L
Linus Torvalds 已提交
66

67 68
int kthreadd(void *unused);
extern struct task_struct *kthreadd_task;
69
extern int tsk_fork_get_node(struct task_struct *tsk);
70

T
Tejun Heo 已提交
71 72 73 74
/*
 * Simple work processor based on kthread.
 *
 * This provides easier way to make use of kthreads.  A kthread_work
P
Petr Mladek 已提交
75
 * can be queued and flushed using queue/kthread_flush_work()
T
Tejun Heo 已提交
76 77 78 79 80
 * respectively.  Queued kthread_works are processed by a kthread
 * running kthread_worker_fn().
 */
struct kthread_work;
typedef void (*kthread_work_func_t)(struct kthread_work *work);
81
void kthread_delayed_work_timer_fn(struct timer_list *t);
T
Tejun Heo 已提交
82

83 84 85 86
enum {
	KTW_FREEZABLE		= 1 << 0,	/* freeze during suspend */
};

T
Tejun Heo 已提交
87
struct kthread_worker {
88
	unsigned int		flags;
89
	raw_spinlock_t		lock;
T
Tejun Heo 已提交
90
	struct list_head	work_list;
91
	struct list_head	delayed_work_list;
T
Tejun Heo 已提交
92
	struct task_struct	*task;
93
	struct kthread_work	*current_work;
T
Tejun Heo 已提交
94 95 96 97 98
};

struct kthread_work {
	struct list_head	node;
	kthread_work_func_t	func;
99
	struct kthread_worker	*worker;
100 101
	/* Number of canceling calls that are running at the moment. */
	int			canceling;
T
Tejun Heo 已提交
102 103
};

104 105 106 107 108
struct kthread_delayed_work {
	struct kthread_work work;
	struct timer_list timer;
};

T
Tejun Heo 已提交
109
#define KTHREAD_WORKER_INIT(worker)	{				\
110
	.lock = __RAW_SPIN_LOCK_UNLOCKED((worker).lock),		\
T
Tejun Heo 已提交
111
	.work_list = LIST_HEAD_INIT((worker).work_list),		\
112
	.delayed_work_list = LIST_HEAD_INIT((worker).delayed_work_list),\
T
Tejun Heo 已提交
113 114 115 116 117 118 119
	}

#define KTHREAD_WORK_INIT(work, fn)	{				\
	.node = LIST_HEAD_INIT((work).node),				\
	.func = (fn),							\
	}

120 121
#define KTHREAD_DELAYED_WORK_INIT(dwork, fn) {				\
	.work = KTHREAD_WORK_INIT((dwork).work, (fn)),			\
122
	.timer = __TIMER_INITIALIZER(kthread_delayed_work_timer_fn,\
123 124 125
				     TIMER_IRQSAFE),			\
	}

T
Tejun Heo 已提交
126 127 128 129 130 131
#define DEFINE_KTHREAD_WORKER(worker)					\
	struct kthread_worker worker = KTHREAD_WORKER_INIT(worker)

#define DEFINE_KTHREAD_WORK(work, fn)					\
	struct kthread_work work = KTHREAD_WORK_INIT(work, fn)

132 133 134 135
#define DEFINE_KTHREAD_DELAYED_WORK(dwork, fn)				\
	struct kthread_delayed_work dwork =				\
		KTHREAD_DELAYED_WORK_INIT(dwork, fn)

Y
Yong Zhang 已提交
136
/*
137 138
 * kthread_worker.lock needs its own lockdep class key when defined on
 * stack with lockdep enabled.  Use the following macros in such cases.
Y
Yong Zhang 已提交
139 140 141
 */
#ifdef CONFIG_LOCKDEP
# define KTHREAD_WORKER_INIT_ONSTACK(worker)				\
P
Petr Mladek 已提交
142
	({ kthread_init_worker(&worker); worker; })
Y
Yong Zhang 已提交
143 144 145 146 147 148
# define DEFINE_KTHREAD_WORKER_ONSTACK(worker)				\
	struct kthread_worker worker = KTHREAD_WORKER_INIT_ONSTACK(worker)
#else
# define DEFINE_KTHREAD_WORKER_ONSTACK(worker) DEFINE_KTHREAD_WORKER(worker)
#endif

P
Petr Mladek 已提交
149
extern void __kthread_init_worker(struct kthread_worker *worker,
Y
Yong Zhang 已提交
150 151
			const char *name, struct lock_class_key *key);

P
Petr Mladek 已提交
152
#define kthread_init_worker(worker)					\
Y
Yong Zhang 已提交
153 154
	do {								\
		static struct lock_class_key __key;			\
P
Petr Mladek 已提交
155
		__kthread_init_worker((worker), "("#worker")->lock", &__key); \
Y
Yong Zhang 已提交
156 157
	} while (0)

P
Petr Mladek 已提交
158
#define kthread_init_work(work, fn)					\
Y
Yong Zhang 已提交
159 160 161 162 163
	do {								\
		memset((work), 0, sizeof(struct kthread_work));		\
		INIT_LIST_HEAD(&(work)->node);				\
		(work)->func = (fn);					\
	} while (0)
T
Tejun Heo 已提交
164

165 166 167
#define kthread_init_delayed_work(dwork, fn)				\
	do {								\
		kthread_init_work(&(dwork)->work, (fn));		\
168 169
		timer_setup(&(dwork)->timer,				\
			     kthread_delayed_work_timer_fn, 0);		\
170 171
	} while (0)

T
Tejun Heo 已提交
172 173
int kthread_worker_fn(void *worker_ptr);

174
__printf(2, 3)
175
struct kthread_worker *
176
kthread_create_worker(unsigned int flags, const char namefmt[], ...);
177

N
Nicolas Iooss 已提交
178
__printf(3, 4) struct kthread_worker *
179 180
kthread_create_worker_on_cpu(int cpu, unsigned int flags,
			     const char namefmt[], ...);
181

P
Petr Mladek 已提交
182
bool kthread_queue_work(struct kthread_worker *worker,
T
Tejun Heo 已提交
183
			struct kthread_work *work);
184 185 186 187 188

bool kthread_queue_delayed_work(struct kthread_worker *worker,
				struct kthread_delayed_work *dwork,
				unsigned long delay);

189 190 191 192
bool kthread_mod_delayed_work(struct kthread_worker *worker,
			      struct kthread_delayed_work *dwork,
			      unsigned long delay);

P
Petr Mladek 已提交
193 194
void kthread_flush_work(struct kthread_work *work);
void kthread_flush_worker(struct kthread_worker *worker);
T
Tejun Heo 已提交
195

196 197 198
bool kthread_cancel_work_sync(struct kthread_work *work);
bool kthread_cancel_delayed_work_sync(struct kthread_delayed_work *work);

199 200
void kthread_destroy_worker(struct kthread_worker *worker);

S
Shaohua Li 已提交
201
#ifdef CONFIG_BLK_CGROUP
202 203 204 205 206 207 208 209 210
void kthread_associate_blkcg(struct cgroup_subsys_state *css);
struct cgroup_subsys_state *kthread_blkcg(void);
#else
static inline void kthread_associate_blkcg(struct cgroup_subsys_state *css) { }
static inline struct cgroup_subsys_state *kthread_blkcg(void)
{
	return NULL;
}
#endif
L
Linus Torvalds 已提交
211
#endif /* _LINUX_KTHREAD_H */