Commit f4efdd65 authored by Linus Torvalds's avatar Linus Torvalds
Browse files
* git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus:
  work_on_cpu(): rewrite it to create a kernel thread on demand
  kthread: move sched-realeted initialization from kthreadd context
  kthread: Don't looking for a task in create_kthread() #2
parents d8312204 6b44003e
Loading
Loading
Loading
Loading
+12 −14
Original line number Original line Diff line number Diff line
@@ -76,6 +76,7 @@ static int kthread(void *_create)


	/* OK, tell user we're spawned, wait for stop or wakeup */
	/* OK, tell user we're spawned, wait for stop or wakeup */
	__set_current_state(TASK_UNINTERRUPTIBLE);
	__set_current_state(TASK_UNINTERRUPTIBLE);
	create->result = current;
	complete(&create->started);
	complete(&create->started);
	schedule();
	schedule();


@@ -96,22 +97,10 @@ static void create_kthread(struct kthread_create_info *create)


	/* We want our own signal handler (we take no signals by default). */
	/* We want our own signal handler (we take no signals by default). */
	pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
	pid = kernel_thread(kthread, create, CLONE_FS | CLONE_FILES | SIGCHLD);
	if (pid < 0) {
	if (pid < 0)
		create->result = ERR_PTR(pid);
		create->result = ERR_PTR(pid);
	} else {
	else
		struct sched_param param = { .sched_priority = 0 };
		wait_for_completion(&create->started);
		wait_for_completion(&create->started);
		read_lock(&tasklist_lock);
		create->result = find_task_by_pid_ns(pid, &init_pid_ns);
		read_unlock(&tasklist_lock);
		/*
		 * root may have changed our (kthreadd's) priority or CPU mask.
		 * The kernel thread should not inherit these properties.
		 */
		sched_setscheduler(create->result, SCHED_NORMAL, &param);
		set_user_nice(create->result, KTHREAD_NICE_LEVEL);
		set_cpus_allowed_ptr(create->result, cpu_all_mask);
	}
	complete(&create->done);
	complete(&create->done);
}
}


@@ -154,11 +143,20 @@ struct task_struct *kthread_create(int (*threadfn)(void *data),
	wait_for_completion(&create.done);
	wait_for_completion(&create.done);


	if (!IS_ERR(create.result)) {
	if (!IS_ERR(create.result)) {
		struct sched_param param = { .sched_priority = 0 };
		va_list args;
		va_list args;

		va_start(args, namefmt);
		va_start(args, namefmt);
		vsnprintf(create.result->comm, sizeof(create.result->comm),
		vsnprintf(create.result->comm, sizeof(create.result->comm),
			  namefmt, args);
			  namefmt, args);
		va_end(args);
		va_end(args);
		/*
		 * root may have changed our (kthreadd's) priority or CPU mask.
		 * The kernel thread should not inherit these properties.
		 */
		sched_setscheduler_nocheck(create.result, SCHED_NORMAL, &param);
		set_user_nice(create.result, KTHREAD_NICE_LEVEL);
		set_cpus_allowed_ptr(create.result, cpu_all_mask);
	}
	}
	return create.result;
	return create.result;
}
}
+19 −17
Original line number Original line Diff line number Diff line
@@ -966,20 +966,20 @@ undo:
}
}


#ifdef CONFIG_SMP
#ifdef CONFIG_SMP
static struct workqueue_struct *work_on_cpu_wq __read_mostly;


struct work_for_cpu {
struct work_for_cpu {
	struct work_struct work;
	struct completion completion;
	long (*fn)(void *);
	long (*fn)(void *);
	void *arg;
	void *arg;
	long ret;
	long ret;
};
};


static void do_work_for_cpu(struct work_struct *w)
static int do_work_for_cpu(void *_wfc)
{
{
	struct work_for_cpu *wfc = container_of(w, struct work_for_cpu, work);
	struct work_for_cpu *wfc = _wfc;

	wfc->ret = wfc->fn(wfc->arg);
	wfc->ret = wfc->fn(wfc->arg);
	complete(&wfc->completion);
	return 0;
}
}


/**
/**
@@ -990,17 +990,23 @@ static void do_work_for_cpu(struct work_struct *w)
 *
 *
 * This will return the value @fn returns.
 * This will return the value @fn returns.
 * It is up to the caller to ensure that the cpu doesn't go offline.
 * It is up to the caller to ensure that the cpu doesn't go offline.
 * The caller must not hold any locks which would prevent @fn from completing.
 */
 */
long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
long work_on_cpu(unsigned int cpu, long (*fn)(void *), void *arg)
{
{
	struct work_for_cpu wfc;
	struct task_struct *sub_thread;

	struct work_for_cpu wfc = {
	INIT_WORK(&wfc.work, do_work_for_cpu);
		.completion = COMPLETION_INITIALIZER_ONSTACK(wfc.completion),
	wfc.fn = fn;
		.fn = fn,
	wfc.arg = arg;
		.arg = arg,
	queue_work_on(cpu, work_on_cpu_wq, &wfc.work);
	};
	flush_work(&wfc.work);


	sub_thread = kthread_create(do_work_for_cpu, &wfc, "work_for_cpu");
	if (IS_ERR(sub_thread))
		return PTR_ERR(sub_thread);
	kthread_bind(sub_thread, cpu);
	wake_up_process(sub_thread);
	wait_for_completion(&wfc.completion);
	return wfc.ret;
	return wfc.ret;
}
}
EXPORT_SYMBOL_GPL(work_on_cpu);
EXPORT_SYMBOL_GPL(work_on_cpu);
@@ -1016,8 +1022,4 @@ void __init init_workqueues(void)
	hotcpu_notifier(workqueue_cpu_callback, 0);
	hotcpu_notifier(workqueue_cpu_callback, 0);
	keventd_wq = create_workqueue("events");
	keventd_wq = create_workqueue("events");
	BUG_ON(!keventd_wq);
	BUG_ON(!keventd_wq);
#ifdef CONFIG_SMP
	work_on_cpu_wq = create_workqueue("work_on_cpu");
	BUG_ON(!work_on_cpu_wq);
#endif
}
}