All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] KVM: Move VM's worker kthreads back to the original cgroup before exiting.
@ 2022-02-17  6:16 ` Vipin Sharma
  0 siblings, 0 replies; 19+ messages in thread
From: Vipin Sharma @ 2022-02-17  6:16 UTC (permalink / raw)
  To: pbonzini-H+wXaHxf7aLQT0dZR+AlfA, seanjc-hpIqsD4AKlfQT0dZR+AlfA
  Cc: mkoutny-IBi9RG/b67k, tj-DgEjT+Ai2ygdnm+yROfE0A,
	lizefan.x-EC8Uxl6Npydl57MIdRCFDg, hannes-druUgvl0LCNAfugRpC6u6w,
	dmatlack-hpIqsD4AKlfQT0dZR+AlfA,
	jiangshanlai-Re5JQEeQqe8AvxtiuMwx3w, kvm-u79uwXL29TY76Z2rM5mHXA,
	cgroups-u79uwXL29TY76Z2rM5mHXA,
	linux-kernel-u79uwXL29TY76Z2rM5mHXA, Vipin Sharma

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

KVM terminates the worker kthreads by calling kthread_stop() which waits
on the 'exited' completion, triggered by exit_mm(), via mm_release(), in
do_exit() during the kthread's exit.  However, these kthreads are
removed from the cgroup using the cgroup_exit() which happens after the
exit_mm(). Therefore, a VM process can terminate in between the
exit_mm() and cgroup_exit() calls, leaving only worker kthreads in the
cgroup.

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

Signed-off-by: Vipin Sharma <vipinsh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
---

v3:
- Use 'current->real_parent' (kthreadd_task) in the
  cgroup_attach_task_all() call.
- Revert cgroup APIs changes in v2. Now, patch does not touch cgroup
  APIs.
- Update commit and comment message

v2: https://lore.kernel.org/lkml/20211222225350.1912249-1-vipinsh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org/
- Use kthreadd_task in the cgroup API to avoid build issue.

v1: https://lore.kernel.org/lkml/20211214050708.4040200-1-vipinsh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org/

 virt/kvm/kvm_main.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 83c57bcc6eb6..2c9dcfffb606 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -5813,7 +5813,7 @@ static int kvm_vm_worker_thread(void *context)
 	struct kvm *kvm = init_context->kvm;
 	kvm_vm_thread_fn_t thread_fn = init_context->thread_fn;
 	uintptr_t data = init_context->data;
-	int err;
+	int err, reattach_err;
 
 	err = kthread_park(current);
 	/* kthread_park(current) is never supposed to return an error */
@@ -5836,7 +5836,7 @@ 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();
@@ -5844,6 +5844,23 @@ static int kvm_vm_worker_thread(void *context)
 	if (!kthread_should_stop())
 		err = thread_fn(kvm, data);
 
+out:
+	/*
+	 * Move kthread back to its original cgroup to prevent it lingering in
+	 * the cgroup of the VM process, after the latter finishes its
+	 * execution.
+	 *
+	 * kthread_stop() waits on the 'exited' completion condition which is
+	 * set in exit_mm(), via mm_release(), in do_exit(). However, the
+	 * kthread is removed from the cgroup in the cgroup_exit() which is
+	 * called after the exit_mm(). This causes the kthread_stop() to return
+	 * before the kthread actually quits the cgroup.
+	 */
+	reattach_err = cgroup_attach_task_all(current->real_parent, current);
+	if (reattach_err) {
+		kvm_err("%s: cgroup_attach_task_all failed on reattach with err %d\n",
+			__func__, reattach_err);
+	}
 	return err;
 }
 

base-commit: db6e7adf8de9b3b99a9856acb73870cc3a70e3ca
-- 
2.35.1.265.g69c8d7142f-goog


^ permalink raw reply related	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2022-02-19  7:55 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-17  6:16 [PATCH v3] KVM: Move VM's worker kthreads back to the original cgroup before exiting Vipin Sharma
2022-02-17  6:16 ` Vipin Sharma
     [not found] ` <20220217061616.3303271-1-vipinsh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2022-02-17 12:34   ` kernel test robot
2022-02-17 12:34     ` kernel test robot
2022-02-17 12:34     ` kernel test robot
     [not found]     ` <202202172046.GuW8pHQc-lkp-ral2JQCrhuEAvxtiuMwx3w@public.gmane.org>
2022-02-17 16:05       ` Paolo Bonzini
2022-02-17 16:05         ` Paolo Bonzini
2022-02-17 16:05         ` Paolo Bonzini
     [not found]         ` <3113f00a-e910-2dfb-479f-268566445630-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2022-02-19  0:30           ` Sean Christopherson
2022-02-19  0:30             ` Sean Christopherson
2022-02-19  0:30             ` Sean Christopherson
2022-02-19  7:55             ` Paolo Bonzini
2022-02-19  7:55               ` Paolo Bonzini
2022-02-17 19:47   ` kernel test robot
2022-02-17 19:47     ` kernel test robot
2022-02-17 19:47     ` kernel test robot
2022-02-17 23:22   ` kernel test robot
2022-02-17 23:22     ` kernel test robot
2022-02-17 23:22     ` kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.