From nobody Mon Feb 9 07:07:46 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F0581BC40 for ; Mon, 9 Sep 2024 01:36:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725845804; cv=none; b=k6bHnWGWUy0qMy4wiT8e2Gh0zFwbWsQV5vRgtm9QbzdXJV0uxcq2s87zqVIIWNaf/hh3i4/baCLP2SC+nBSFyIX9eFklwqRMA/0TtTK5Ql0hKIocsaXS6ORjo+zdMKeQxHn1aVSEvDWMXjcLGjZGSwPIOrdzYk6FSQkOZW4xKus= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725845804; c=relaxed/simple; bh=q60zYy1/Faf6jVVm6utx8YjJpmwhDSnklipNz1Xc/VQ=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=op3itm8SFiKvgRI2qlRrUppkJ4OCBPs5B2NszszBXYFitbZfknOnmQWCqI5Lic+P2FYfDa+gufTfpTuaGWZF8wFoz8IrBdQ047RYz/s/2+fZ1TRbZ7/CgfxRl1+cWLfvEDKU5xXnf9comfvrVMkCX4DqiAXsplNftaSFNBBOc5U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZMBEF+WA; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZMBEF+WA" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1725845801; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4kaz4nYOPB8CgQNt7ZV4Ttw3NVtlnR/6nzex43AvYrI=; b=ZMBEF+WAsP+US/RqxII87iuqkOQr+61voqFJbw/QBaVODb/kB6OOdWHQYPF9bAFB7S0SLG jyBeA2/MFm36YGVpQG706CuqMIhjoEbSm9oRJW77PZeDUfzzpbx2GqQmPj/Ijb09Ptl3dX jd9zGsmVQuIQW3vH2ttEXDgR0tiq+es= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-637-1JVi5RVQNTOLgcDQ6raXIA-1; Sun, 08 Sep 2024 21:36:40 -0400 X-MC-Unique: 1JVi5RVQNTOLgcDQ6raXIA-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 1C8BE1953963; Mon, 9 Sep 2024 01:36:39 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.25]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id F085619560AF; Mon, 9 Sep 2024 01:36:34 +0000 (UTC) From: Cindy Lu To: lulu@redhat.com, jasowang@redhat.com, mst@redhat.com, michael.christie@oracle.com, linux-kernel@vger.kernel.org, virtualization@lists.linux-foundation.org Subject: [PATCH v1 6/7] vhost: Add kthread support in function vhost_worker_create Date: Mon, 9 Sep 2024 09:33:45 +0800 Message-ID: <20240909013531.1243525-7-lulu@redhat.com> In-Reply-To: <20240909013531.1243525-1-lulu@redhat.com> References: <20240909013531.1243525-1-lulu@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Split the function vhost_worker_create to support both task and kthread =C2=A0 =C2=A0 =C2=A0 =C2=A0 Added back the previous old function vhost_worker_create and rename it to vhost_worker_create_khtread to support the khtread. The new vhost_worker_create will be selected which to use based on the value of the parameter. the old function vhost_worker_create was change to support task in commit 6e890c5d5021 ('vhost: use vhost_tasks for worker threads') also changed in commit 1cdaafa1b8b ('vhost: replace single worker pointer with xarray') commit c011bb669dd ('vhost: dynamically allocate vhost_worker') Signed-off-by: Cindy Lu --- drivers/vhost/vhost.c | 55 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index f05545b125f0..bf1e971cb06f 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -807,7 +807,8 @@ static void vhost_workers_free(struct vhost_dev *dev) else vhost_workers_free_task(dev); } -static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) + +static struct vhost_worker *vhost_worker_create_task(struct vhost_dev *dev) { struct vhost_worker *worker; struct vhost_task *vtsk; @@ -848,6 +849,50 @@ static struct vhost_worker *vhost_worker_create(struct= vhost_dev *dev) return NULL; } =20 +static struct vhost_worker *vhost_worker_create_kthread(struct vhost_dev *= dev) +{ + struct vhost_worker *worker; + struct task_struct *task; + int ret; + u32 id; + + worker =3D kzalloc(sizeof(*worker), GFP_KERNEL_ACCOUNT); + if (!worker) + return NULL; + + worker->dev =3D dev; + worker->kcov_handle =3D kcov_common_handle(); + + mutex_init(&worker->mutex); + init_llist_head(&worker->work_list); + + task =3D kthread_create(vhost_run_work_kthread_list, worker, "vhost-%d", + current->pid); + if (IS_ERR(task)) { + ret =3D PTR_ERR(task); + goto free_worker; + } + + worker->task =3D task; + wake_up_process(task); /* avoid contributing to loadavg */ + ret =3D xa_alloc(&dev->worker_xa, &id, worker, xa_limit_32b, GFP_KERNEL); + if (ret < 0) + goto stop_worker; + worker->id =3D id; + + ret =3D vhost_attach_cgroups(dev); + if (ret) + goto stop_worker; + + return worker; + +stop_worker: + kthread_stop(worker->task); +free_worker: + kfree(worker); + return NULL; +} + /* Caller must have device mutex */ static void __vhost_vq_attach_worker(struct vhost_virtqueue *vq, struct vhost_worker *worker) @@ -936,6 +981,14 @@ static int vhost_vq_attach_worker(struct vhost_virtque= ue *vq, return 0; } =20 +static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) +{ + if (use_kthread) + return vhost_worker_create_kthread(dev); + else + return vhost_worker_create_task(dev); +} + /* Caller must have device mutex */ static int vhost_new_worker(struct vhost_dev *dev, struct vhost_worker_state *info) --=20 2.45.0