ia64/linux-2.6.18-xen.hg

annotate kernel/kthread.c @ 562:66faefe721eb

pvSCSI backend driver

Signed-off-by: Tomonari Horikoshi <t.horikoshi@jp.fujitsu.com>
Signed-off-by: Jun Kamada <kama@jp.fujitsu.com>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Jun 02 09:58:27 2008 +0100 (2008-06-02)
parents 831230e53067
children
rev   line source
ian@0 1 /* Kernel thread helper functions.
ian@0 2 * Copyright (C) 2004 IBM Corporation, Rusty Russell.
ian@0 3 *
ian@0 4 * Creation is done via keventd, so that we get a clean environment
ian@0 5 * even if we're invoked from userspace (think modprobe, hotplug cpu,
ian@0 6 * etc.).
ian@0 7 */
ian@0 8 #include <linux/sched.h>
ian@0 9 #include <linux/kthread.h>
ian@0 10 #include <linux/completion.h>
ian@0 11 #include <linux/err.h>
ian@0 12 #include <linux/unistd.h>
ian@0 13 #include <linux/file.h>
ian@0 14 #include <linux/module.h>
ian@0 15 #include <linux/mutex.h>
ian@0 16 #include <asm/semaphore.h>
ian@0 17
ian@0 18 /*
ian@0 19 * We dont want to execute off keventd since it might
ian@0 20 * hold a semaphore our callers hold too:
ian@0 21 */
ian@0 22 static struct workqueue_struct *helper_wq;
ian@0 23
ian@0 24 struct kthread_create_info
ian@0 25 {
ian@0 26 /* Information passed to kthread() from keventd. */
ian@0 27 int (*threadfn)(void *data);
ian@0 28 void *data;
ian@0 29 struct completion started;
ian@0 30
ian@0 31 /* Result passed back to kthread_create() from keventd. */
ian@0 32 struct task_struct *result;
ian@0 33 struct completion done;
ian@0 34 };
ian@0 35
ian@0 36 struct kthread_stop_info
ian@0 37 {
ian@0 38 struct task_struct *k;
ian@0 39 int err;
ian@0 40 struct completion done;
ian@0 41 };
ian@0 42
ian@0 43 /* Thread stopping is done by setthing this var: lock serializes
ian@0 44 * multiple kthread_stop calls. */
ian@0 45 static DEFINE_MUTEX(kthread_stop_lock);
ian@0 46 static struct kthread_stop_info kthread_stop_info;
ian@0 47
ian@0 48 /**
ian@0 49 * kthread_should_stop - should this kthread return now?
ian@0 50 *
ian@0 51 * When someone calls kthread_stop on your kthread, it will be woken
ian@0 52 * and this will return true. You should then return, and your return
ian@0 53 * value will be passed through to kthread_stop().
ian@0 54 */
ian@0 55 int kthread_should_stop(void)
ian@0 56 {
ian@0 57 return (kthread_stop_info.k == current);
ian@0 58 }
ian@0 59 EXPORT_SYMBOL(kthread_should_stop);
ian@0 60
ian@0 61 static void kthread_exit_files(void)
ian@0 62 {
ian@0 63 struct fs_struct *fs;
ian@0 64 struct task_struct *tsk = current;
ian@0 65
ian@0 66 exit_fs(tsk); /* current->fs->count--; */
ian@0 67 fs = init_task.fs;
ian@0 68 tsk->fs = fs;
ian@0 69 atomic_inc(&fs->count);
ian@0 70 exit_files(tsk);
ian@0 71 current->files = init_task.files;
ian@0 72 atomic_inc(&tsk->files->count);
ian@0 73 }
ian@0 74
ian@0 75 static int kthread(void *_create)
ian@0 76 {
ian@0 77 struct kthread_create_info *create = _create;
ian@0 78 int (*threadfn)(void *data);
ian@0 79 void *data;
ian@0 80 sigset_t blocked;
ian@0 81 int ret = -EINTR;
ian@0 82
ian@0 83 kthread_exit_files();
ian@0 84
ian@0 85 /* Copy data: it's on keventd's stack */
ian@0 86 threadfn = create->threadfn;
ian@0 87 data = create->data;
ian@0 88
ian@0 89 /* Block and flush all signals (in case we're not from keventd). */
ian@0 90 sigfillset(&blocked);
ian@0 91 sigprocmask(SIG_BLOCK, &blocked, NULL);
ian@0 92 flush_signals(current);
ian@0 93
ian@0 94 /* By default we can run anywhere, unlike keventd. */
ian@0 95 set_cpus_allowed(current, CPU_MASK_ALL);
ian@0 96
ian@0 97 /* OK, tell user we're spawned, wait for stop or wakeup */
ian@0 98 __set_current_state(TASK_INTERRUPTIBLE);
ian@0 99 complete(&create->started);
ian@0 100 schedule();
ian@0 101
ian@0 102 if (!kthread_should_stop())
ian@0 103 ret = threadfn(data);
ian@0 104
ian@0 105 /* It might have exited on its own, w/o kthread_stop. Check. */
ian@0 106 if (kthread_should_stop()) {
ian@0 107 kthread_stop_info.err = ret;
ian@0 108 complete(&kthread_stop_info.done);
ian@0 109 }
ian@0 110 return 0;
ian@0 111 }
ian@0 112
ian@0 113 /* We are keventd: create a thread. */
ian@0 114 static void keventd_create_kthread(void *_create)
ian@0 115 {
ian@0 116 struct kthread_create_info *create = _create;
ian@0 117 int pid;
ian@0 118
ian@0 119 /* We want our own signal handler (we take no signals by default). */
ian@0 120 pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
ian@0 121 if (pid < 0) {
ian@0 122 create->result = ERR_PTR(pid);
ian@0 123 } else {
ian@0 124 wait_for_completion(&create->started);
ian@0 125 read_lock(&tasklist_lock);
ian@0 126 create->result = find_task_by_pid(pid);
ian@0 127 read_unlock(&tasklist_lock);
ian@0 128 }
ian@0 129 complete(&create->done);
ian@0 130 }
ian@0 131
ian@0 132 /**
ian@0 133 * kthread_create - create a kthread.
ian@0 134 * @threadfn: the function to run until signal_pending(current).
ian@0 135 * @data: data ptr for @threadfn.
ian@0 136 * @namefmt: printf-style name for the thread.
ian@0 137 *
ian@0 138 * Description: This helper function creates and names a kernel
ian@0 139 * thread. The thread will be stopped: use wake_up_process() to start
ian@0 140 * it. See also kthread_run(), kthread_create_on_cpu().
ian@0 141 *
ian@0 142 * When woken, the thread will run @threadfn() with @data as its
ian@0 143 * argument. @threadfn can either call do_exit() directly if it is a
ian@0 144 * standalone thread for which noone will call kthread_stop(), or
ian@0 145 * return when 'kthread_should_stop()' is true (which means
ian@0 146 * kthread_stop() has been called). The return value should be zero
ian@0 147 * or a negative error number; it will be passed to kthread_stop().
ian@0 148 *
ian@0 149 * Returns a task_struct or ERR_PTR(-ENOMEM).
ian@0 150 */
ian@0 151 struct task_struct *kthread_create(int (*threadfn)(void *data),
ian@0 152 void *data,
ian@0 153 const char namefmt[],
ian@0 154 ...)
ian@0 155 {
ian@0 156 struct kthread_create_info create;
ian@0 157 DECLARE_WORK(work, keventd_create_kthread, &create);
ian@0 158
ian@0 159 create.threadfn = threadfn;
ian@0 160 create.data = data;
ian@0 161 init_completion(&create.started);
ian@0 162 init_completion(&create.done);
ian@0 163
ian@0 164 /*
ian@0 165 * The workqueue needs to start up first:
ian@0 166 */
ian@0 167 if (!helper_wq)
ian@0 168 work.func(work.data);
ian@0 169 else {
ian@0 170 queue_work(helper_wq, &work);
ian@0 171 wait_for_completion(&create.done);
ian@0 172 }
ian@0 173 if (!IS_ERR(create.result)) {
ian@0 174 va_list args;
ian@0 175 va_start(args, namefmt);
ian@0 176 vsnprintf(create.result->comm, sizeof(create.result->comm),
ian@0 177 namefmt, args);
ian@0 178 va_end(args);
ian@0 179 }
ian@0 180
ian@0 181 return create.result;
ian@0 182 }
ian@0 183 EXPORT_SYMBOL(kthread_create);
ian@0 184
ian@0 185 /**
ian@0 186 * kthread_bind - bind a just-created kthread to a cpu.
ian@0 187 * @k: thread created by kthread_create().
ian@0 188 * @cpu: cpu (might not be online, must be possible) for @k to run on.
ian@0 189 *
ian@0 190 * Description: This function is equivalent to set_cpus_allowed(),
ian@0 191 * except that @cpu doesn't need to be online, and the thread must be
ian@0 192 * stopped (i.e., just returned from kthread_create().
ian@0 193 */
ian@0 194 void kthread_bind(struct task_struct *k, unsigned int cpu)
ian@0 195 {
ian@0 196 BUG_ON(k->state != TASK_INTERRUPTIBLE);
ian@0 197 /* Must have done schedule() in kthread() before we set_task_cpu */
ian@0 198 wait_task_inactive(k);
ian@0 199 set_task_cpu(k, cpu);
ian@0 200 k->cpus_allowed = cpumask_of_cpu(cpu);
ian@0 201 }
ian@0 202 EXPORT_SYMBOL(kthread_bind);
ian@0 203
ian@0 204 /**
ian@0 205 * kthread_stop - stop a thread created by kthread_create().
ian@0 206 * @k: thread created by kthread_create().
ian@0 207 *
ian@0 208 * Sets kthread_should_stop() for @k to return true, wakes it, and
ian@0 209 * waits for it to exit. Your threadfn() must not call do_exit()
ian@0 210 * itself if you use this function! This can also be called after
ian@0 211 * kthread_create() instead of calling wake_up_process(): the thread
ian@0 212 * will exit without calling threadfn().
ian@0 213 *
ian@0 214 * Returns the result of threadfn(), or %-EINTR if wake_up_process()
ian@0 215 * was never called.
ian@0 216 */
ian@0 217 int kthread_stop(struct task_struct *k)
ian@0 218 {
ian@0 219 int ret;
ian@0 220
ian@0 221 mutex_lock(&kthread_stop_lock);
ian@0 222
ian@0 223 /* It could exit after stop_info.k set, but before wake_up_process. */
ian@0 224 get_task_struct(k);
ian@0 225
ian@0 226 /* Must init completion *before* thread sees kthread_stop_info.k */
ian@0 227 init_completion(&kthread_stop_info.done);
ian@0 228 smp_wmb();
ian@0 229
ian@0 230 /* Now set kthread_should_stop() to true, and wake it up. */
ian@0 231 kthread_stop_info.k = k;
ian@0 232 wake_up_process(k);
ian@0 233 put_task_struct(k);
ian@0 234
ian@0 235 /* Once it dies, reset stop ptr, gather result and we're done. */
ian@0 236 wait_for_completion(&kthread_stop_info.done);
ian@0 237 kthread_stop_info.k = NULL;
ian@0 238 ret = kthread_stop_info.err;
ian@0 239 mutex_unlock(&kthread_stop_lock);
ian@0 240
ian@0 241 return ret;
ian@0 242 }
ian@0 243 EXPORT_SYMBOL(kthread_stop);
ian@0 244
ian@0 245 static __init int helper_init(void)
ian@0 246 {
ian@0 247 helper_wq = create_singlethread_workqueue("kthread");
ian@0 248 BUG_ON(!helper_wq);
ian@0 249
ian@0 250 return 0;
ian@0 251 }
ian@0 252
ian@0 253 core_initcall(helper_init);