From: Mike Christie <michael.christie@oracle.com>
To: Cindy Lu <lulu@redhat.com>,
jasowang@redhat.com, mst@redhat.com,
linux-kernel@vger.kernel.org,
virtualization@lists.linux-foundation.org
Subject: Re: [PATCH v2 4/7] vhost: Add the vhost_worker to support kthread
Date: Mon, 14 Oct 2024 17:56:45 -0500 [thread overview]
Message-ID: <dd03e6d2-54e6-487c-8aa8-8a760213a42a@oracle.com> (raw)
In-Reply-To: <20241004015937.2286459-5-lulu@redhat.com>
On 10/3/24 8:58 PM, Cindy Lu wrote:
> Add back the previously removed vhost_worker function to support the kthread
> and rename it vhost_run_work_kthread_list.
>
> The old function vhost_worker was change to support task in
> commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads")
> change to xarray in
> commit 1cdaafa1b8b4 ("vhost: replace single worker pointer with xarray")
>
> Signed-off-by: Cindy Lu <lulu@redhat.com>
> ---
> drivers/vhost/vhost.c | 38 ++++++++++++++++++++++++++++++++++++++
> 1 file changed, 38 insertions(+)
>
> diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
> index fed6671c1ffb..349499139f4f 100644
> --- a/drivers/vhost/vhost.c
> +++ b/drivers/vhost/vhost.c
> @@ -418,6 +418,44 @@ static void vhost_vq_reset(struct vhost_dev *dev,
> __vhost_vq_meta_reset(vq);
> }
>
> +static int vhost_run_work_kthread_list(void *data)
> +{
> + struct vhost_worker *worker = data;
> + struct vhost_work *work, *work_next;
> + struct vhost_dev *dev = worker->dev;
> + struct llist_node *node;
> +
> + kthread_use_mm(dev->mm);
> +
> + for (;;) {
> + /* mb paired w/ kthread_stop */
> + set_current_state(TASK_INTERRUPTIBLE);
> +
> + if (kthread_should_stop()) {
> + __set_current_state(TASK_RUNNING);
> + break;
> + }
> + node = llist_del_all(&worker->work_list);
> + if (!node)
> + schedule();
> +
> + node = llist_reverse_order(node);
> + /* make sure flag is seen after deletion */
> + smp_wmb();
> + llist_for_each_entry_safe(work, work_next, node, node) {
> + clear_bit(VHOST_WORK_QUEUED, &work->flags);
> + __set_current_state(TASK_RUNNING);
> + kcov_remote_start_common(worker->kcov_handle);
> + work->fn(work);
> + kcov_remote_stop();
> + cond_resched();
> + }
> + }
> + kthread_unuse_mm(dev->mm);
> +
> + return 0;
> +}
I think there is a lot of unneeded code duplication where in the functions
you are adding there's only 1-3 lines different. To fix this I think we
could:
1. Go really invasive and modify copy_process and its helpers so they take
a task_struct instead of using "current". We can then just pass in "current"
or kthreadd. We can then use most of the existing vhost code. We just
need a per vhost_worker check/field for the mm and cgroup use like:
vhost_task_fn():
{
...
/* The mm would be passed in during creation for the kthread case */
if (vtsk->mm)
kthread_use_mm(vtsk->mm);
Or
2. Go hacky and in the vhost code, when we get a VHOST_SET_OWNER call create
a tmp kthread. The tmp kthread would then call the existing vhost_worker_create
function. The resulting vhost_task would inherit the kthreadd settings like we
want. We then just need a per vhost_worker check/field for the mm and cgroup use
like above.
Or
3. There doesn't seem to be a lot of differences in the functions you are
adding. In the function above the only differences are the mm calls and kthread
should stop. In the destroy functions it's kthread_stop. In the queue function its
wake_up_process. In create its kthread_create, stop and the cgroup functions.
I think we could add just some callouts on the vhost_task or vhost_worker for stop,
wakeup and use mm. For create we would do something like
vhost_worker_create()
....
worker = kzalloc();
if (inherit from caller) {
worker->stop = vhost_task_stop;
worker->wakeup = vhost_task_wakeup;
worker->vtsk = vhost_task_create();
} else {
worker->stop = vhost_kthread_stop;
worker->wakeup = vhost_kthread_wakeup;
worker->tsk = kthread->create();
vhost_kthread_setup_cgroups();
}
...
}
vhost_worker_destroy()
{
if (!worker)
return;
WARN_ON(!llist_empty(&worker->work_list));
xa_erase(&dev->worker_xa, worker->id);
worker->stop(worker);
kfree(worker);
}
next prev parent reply other threads:[~2024-10-14 22:56 UTC|newest]
Thread overview: 30+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-04 1:58 [PATCH v2 0/7] vhost: Add support of kthread API Cindy Lu
2024-10-04 1:58 ` [PATCH v2 1/7] vhost: Add a new modparam to allow userspace select vhost_task Cindy Lu
2024-10-05 15:42 ` kernel test robot
2024-10-07 13:31 ` Stefano Garzarella
2024-10-09 7:28 ` Jason Wang
2024-10-09 7:57 ` Stefano Garzarella
2024-10-09 8:20 ` Jason Wang
2024-10-14 6:47 ` Cindy Lu
2024-10-14 6:46 ` Cindy Lu
2024-10-04 1:58 ` [PATCH v2 2/7] vhost: Add kthread support in function vhost_worker_queue() Cindy Lu
2024-10-04 1:58 ` [PATCH v2 3/7] vhost: Add kthread support in function vhost_workers_free() Cindy Lu
2024-10-07 13:32 ` Stefano Garzarella
2024-10-15 5:54 ` Cindy Lu
2024-10-14 21:05 ` Mike Christie
2024-10-15 6:05 ` Cindy Lu
2024-10-15 6:52 ` Stefano Garzarella
2024-10-15 7:19 ` Cindy Lu
2024-10-04 1:58 ` [PATCH v2 4/7] vhost: Add the vhost_worker to support kthread Cindy Lu
2024-10-14 22:56 ` Mike Christie [this message]
2024-10-15 9:03 ` Cindy Lu
2024-10-04 1:58 ` [PATCH v2 5/7] vhost: Add the cgroup related function Cindy Lu
2024-10-04 1:58 ` [PATCH v2 6/7] vhost: Add kthread support in function vhost_worker_create Cindy Lu
2024-10-14 21:02 ` Mike Christie
2024-10-15 6:30 ` Cindy Lu
2024-10-04 1:58 ` [PATCH v2 7/7] vhost: Add new UAPI to support change to task mode Cindy Lu
2024-10-07 13:37 ` Stefano Garzarella
2024-10-14 20:56 ` Mike Christie
2024-10-15 2:35 ` Cindy Lu
2024-10-15 10:19 ` Michael S. Tsirkin
2024-10-17 6:53 ` Jason Wang
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=dd03e6d2-54e6-487c-8aa8-8a760213a42a@oracle.com \
--to=michael.christie@oracle.com \
--cc=jasowang@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lulu@redhat.com \
--cc=mst@redhat.com \
--cc=virtualization@lists.linux-foundation.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
all inboxes | Powered by JetHome®