I'm wondering if there's a way to block a userspace task from kernel space? Is there a function already in the kernel for this? I have tried to look but found nothing obvious so far.
In UP, this is quite simple: set the state of the task to TASK_INTERRUPTIBLE
and call schedule()
. You can "resume" it later by setting its state to TASK_RUNNING
.
In SMP, you have to make sure that the task is not running on another CPU.
See this:
http://lxr.linux.no/linux+v3.0.4/include/linux/sched.h#L242
250/*
251 * This serializes "schedule()" and also protects
252 * the run-queue from deletions/modifications (but
253 * _adding_ to the beginning of the run-queue has
254 * a separate lock).
255 */
256extern rwlock_t tasklist_lock;
257extern spinlock_t mmlist_lock;
258
So we we know this lock is for synchronizing access to update the scheduling structure. To change the tasking running status, look for an example:
http://lxr.linux.no/linux+v3.0.4/kernel/signal.c#L1812
1769 read_lock(&tasklist_lock);
1809 __set_current_state(TASK_RUNNING);
1810 if (clear_code)
1811 current->exit_code = 0;
1812 read_unlock(&tasklist_lock
U just need to lock/unlock the tasklist_lock, and set the status.
来源:https://stackoverflow.com/questions/7695124/is-it-possible-to-block-a-task-from-kernel-space