From: Sean Christopherson <seanjc-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
To: Vipin Sharma <vipinsh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
Cc: pbonzini-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org,
tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
lizefan.x-EC8Uxl6Npydl57MIdRCFDg@public.gmane.org,
hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org,
dmatlack-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org,
jiangshanlai-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org,
kvm-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Subject: Re: [PATCH v2] KVM: Move VM's worker kthreads back to the original cgroups before exiting.
Date: Tue, 28 Dec 2021 17:17:41 +0000 [thread overview]
Message-ID: <YctGtWzYcNP2iTaN@google.com> (raw)
In-Reply-To: <20211222225350.1912249-1-vipinsh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
On Wed, Dec 22, 2021, Vipin Sharma wrote:
> kthreadd_task is not an exported symbol which causes build errors if KVM
> is built as a loadable module. Both users (kvm_main & vhost) of
> cgroup_attach_task_all(), have the same issue, therefore, using
> kthreadd_task as a default option is chosen when the API is called with
> NULL argument.
...
> diff --git a/kernel/cgroup/cgroup-v1.c b/kernel/cgroup/cgroup-v1.c
> index 81c9e0685948..81d4b2f2acf0 100644
> --- a/kernel/cgroup/cgroup-v1.c
> +++ b/kernel/cgroup/cgroup-v1.c
> @@ -51,6 +51,8 @@ bool cgroup1_ssid_disabled(int ssid)
> * @from: attach to all cgroups of a given task
> * @tsk: the task to be attached
> *
> + * If @from is NULL then use kthreadd_task for finding the destination cgroups.
> + *
> * Return: %0 on success or a negative errno code on failure
> */
> int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
> @@ -58,6 +60,9 @@ int cgroup_attach_task_all(struct task_struct *from, struct task_struct *tsk)
> struct cgroup_root *root;
> int retval = 0;
>
> + if (!from)
> + from = kthreadd_task;
Rather than sully cgroup_attach_task_all() with this behavior, can't KVM do
cgroup_attach_task_all(current->real_parent, current)
since AFAICT real_parent is guaranteed to point at kthreadd_task.
> +
> mutex_lock(&cgroup_mutex);
> percpu_down_write(&cgroup_threadgroup_rwsem);
> for_each_root(root) {
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index b0f7e6eb00ff..f7504578c374 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -5785,7 +5785,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();
> @@ -5793,6 +5793,19 @@ static int kvm_vm_worker_thread(void *context)
> if (!kthread_should_stop())
> err = thread_fn(kvm, data);
>
> +out:
> + /*
> + * We need to move the kthread back to its original cgroups, so that it
Please state what is being done, not what "needs" to be done. The need to do
something is implicit, otherwise we wouldn't be doing it.
> + * doesn't linger in the cgroups of the user process after the user
> + * process has already terminated.
> + *
> + * kthread_stop() waits on 'exited' completion condition which is set
> + * in exit_mm(), via mm_release(), in do_exit(). However, kthread
> + * is removed from cgroups in the cgroup_exit() which is called after
> + * exit_mm(). This causes lingering of kthreads in cgroups after main
> + * VM process has finished.
> + */
> + WARN_ON(cgroup_attach_task_all(NULL, current));
This should not WARN, cgroup_attach_task_all() needs to perform allocations and
will fail with -ENOMEM even in the absense of kernel bugs.
> return err;
> }
>
>
> base-commit: 5e4e84f1124aa02643833b7ea40abd5a8e964388
> --
> 2.34.1.307.g9b7440fafd-goog
>
next prev parent reply other threads:[~2021-12-28 17:17 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-12-22 22:53 [PATCH v2] KVM: Move VM's worker kthreads back to the original cgroups before exiting Vipin Sharma
[not found] ` <20211222225350.1912249-1-vipinsh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2021-12-28 17:17 ` Sean Christopherson [this message]
[not found] ` <YctGtWzYcNP2iTaN-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org>
2022-01-18 19:53 ` Vipin Sharma
2022-01-05 18:04 ` Michal Koutný
2022-01-18 20:25 ` Vipin Sharma
[not found] ` <CAHVum0e84nUcGtdPYQaJDQszKj-QVP5gM+nteBpSTaQ2sWYpmQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2022-01-18 20:39 ` Tejun Heo
2022-01-19 18:02 ` Paolo Bonzini
[not found] ` <7a0bc562-9f25-392d-5c05-9dbcd350d002-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org>
2022-01-19 18:30 ` Tejun Heo
[not found] ` <YehY0z2vHYVZk52J-NiLfg/pYEd1N0TnZuCh8vA@public.gmane.org>
2022-01-19 18:49 ` Vipin Sharma
2022-01-19 19:05 ` Tejun Heo
2022-01-20 15:05 ` Michal Koutný
[not found] ` <20220120150502.GC27269-9OudH3eul5jcvrawFnH+a6VXKuFTiq87@public.gmane.org>
2022-02-16 17:37 ` Vipin Sharma
[not found] ` <CAHVum0fOP-2XcUcG3PqW08DY7CmpDroG6Fcv9KoD1FqLmGpB8w-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2022-02-16 19:48 ` Paolo Bonzini
2022-02-25 17:37 ` Michal Koutný
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YctGtWzYcNP2iTaN@google.com \
--to=seanjc-hpiqsd4aklfqt0dzr+alfa@public.gmane.org \
--cc=cgroups-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=dmatlack-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org \
--cc=hannes-druUgvl0LCNAfugRpC6u6w@public.gmane.org \
--cc=jiangshanlai-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org \
--cc=kvm-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=lizefan.x-EC8Uxl6Npydl57MIdRCFDg@public.gmane.org \
--cc=pbonzini-H+wXaHxf7aLQT0dZR+AlfA@public.gmane.org \
--cc=tj-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
--cc=vipinsh-hpIqsD4AKlfQT0dZR+AlfA@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).