5 #include <linux/bitops.h>
6 #include <linux/kthread.h>
7 #include <linux/rcupdate.h>
8 #include <linux/sched.h>
10 #include "tools-util.h"
13 KTHREAD_IS_PER_CPU = 0,
19 static void *kthread_start_fn(void *data)
21 rcu_register_thread();
25 current->thread_fn(current->thread_data);
27 complete(¤t->exited);
28 put_task_struct(current);
29 rcu_unregister_thread();
34 * kthread_create_on_node - create a kthread.
35 * @threadfn: the function to run until signal_pending(current).
36 * @data: data ptr for @threadfn.
37 * @node: task and thread structures for the thread are allocated on this node
38 * @namefmt: printf-style name for the thread.
40 * Description: This helper function creates and names a kernel
41 * thread. The thread will be stopped: use wake_up_process() to start
42 * it. See also kthread_run(). The new thread has SCHED_NORMAL policy and
43 * is affine to all CPUs.
45 * If thread is going to be bound on a particular cpu, give its node
46 * in @node, to get NUMA affinity for kthread stack, or else give NUMA_NO_NODE.
47 * When woken, the thread will run @threadfn() with @data as its
48 * argument. @threadfn() can either call do_exit() directly if it is a
49 * standalone thread for which no one will call kthread_stop(), or
50 * return when 'kthread_should_stop()' is true (which means
51 * kthread_stop() has been called). The return value should be zero
52 * or a negative error number; it will be passed to kthread_stop().
54 * Returns a task_struct or ERR_PTR(-ENOMEM) or ERR_PTR(-EINTR).
56 struct task_struct *kthread_create(int (*thread_fn)(void *data),
58 const char namefmt[], ...)
61 struct task_struct *p = malloc(sizeof(*p));
64 memset(p, 0, sizeof(*p));
66 va_start(args, namefmt);
67 vsnprintf(p->comm, sizeof(p->comm), namefmt, args);
70 p->flags |= PF_KTHREAD;
71 p->thread_fn = thread_fn;
72 p->thread_data = thread_data;
73 p->state = TASK_UNINTERRUPTIBLE;
74 p->signal = &p->_signal;
75 atomic_set(&p->usage, 1);
76 init_completion(&p->exited);
77 init_rwsem(&p->_signal.exec_update_lock);
80 pthread_attr_init(&attr);
81 pthread_attr_setstacksize(&attr, 32 << 10);
83 for (unsigned i = 0; i < 10; i++) {
84 ret = pthread_create(&p->thread, &attr, kthread_start_fn, p);
88 run_shrinkers(GFP_KERNEL, true);
92 pthread_setname_np(p->thread, p->comm);
97 * kthread_should_stop - should this kthread return now?
99 * When someone calls kthread_stop() on your kthread, it will be woken
100 * and this will return true. You should then return, and your return
101 * value will be passed through to kthread_stop().
103 bool kthread_should_stop(void)
105 return test_bit(KTHREAD_SHOULD_STOP, ¤t->kthread_flags);
108 bool kthread_freezable_should_stop(bool *was_frozen)
110 return test_bit(KTHREAD_SHOULD_STOP, ¤t->kthread_flags);
114 * kthread_stop - stop a thread created by kthread_create().
115 * @k: thread created by kthread_create().
117 * Sets kthread_should_stop() for @k to return true, wakes it, and
118 * waits for it to exit. This can also be called after kthread_create()
119 * instead of calling wake_up_process(): the thread will exit without
120 * calling threadfn().
122 * If threadfn() may call do_exit() itself, the caller must ensure
123 * task_struct can't go away.
125 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
128 int kthread_stop(struct task_struct *p)
132 set_bit(KTHREAD_SHOULD_STOP, &p->kthread_flags);
134 wait_for_completion(&p->exited);