Skip to content
Permalink
Browse files
KVM: Move VM's worker kthreads back to the original cgroups before ex…
…iting.

VM worker kthreads can linger in the VM process's cgroup for sometime
after KVM temrinates the VM process.

KVM terminates the worker kthreads by calling kthread_stop() which waits
on the signal generated by exit_mm() in do_exit() during kthread's exit.
However, these kthreads are removed from the cgroup using cgroup_exit()
call which happens after exit_mm() in do_exit(). A VM process can
terminate between the time window of exit_mm() to cgroup_exit(), leaving
only worker kthreads in the cgroup.

Moving worker kthreads back to the original cgroup (kthreadd_task's
cgroup) makes sure that cgroup is empty as soon as the main VM process
is terminated.

Signed-off-by: Vipin Sharma <vipinsh@google.com>
  • Loading branch information
shvipin authored and intel-lab-lkp committed Dec 14, 2021
1 parent d8f6ef4 commit fd29d23507ef3f06b61d9de1b7ecd1a0d70136f3
Showing 1 changed file with 10 additions and 1 deletion.
@@ -5785,14 +5785,23 @@ static int kvm_vm_worker_thread(void *context)
init_context = NULL;

if (err)
return err;
goto out;

/* Wait to be woken up by the spawner before proceeding. */
kthread_parkme();

if (!kthread_should_stop())
err = thread_fn(kvm, data);

out:
/*
* We need to move the kthread back to its original cgroups, so that it
* doesn't linger in the cgroups of the user process after that has
* already terminated. exit_mm() in do_exit() signals kthread_stop() to
* return, whereas, removal of the task from the cgroups happens in
* cgroup_exit() which happens after exit_mm().
*/
WARN_ON(cgroup_attach_task_all(kthreadd_task, current));
return err;
}

0 comments on commit fd29d23

Please sign in to comment.