From nobody Thu Nov 28 06:51:38 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.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 A71F345957 for ; Fri, 4 Oct 2024 02:00:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728007238; cv=none; b=nbXfY8kUAe0+G9+KHFvPcZOv7CEv8ooCYnuLIPMC0nkke7FG0YwE2iVWUE9XxqF2QXl1xIplumhMnnmQpXABxNRyQ7a7BsxJZWOpod8e2xId/S4rSpn9v6W/bylFW4GQuOYTVWy4rzKCMg2G/gYdJ4lACHLrYfyhj0G0evk7QmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728007238; c=relaxed/simple; bh=ibksF1L5VO/D2Z/M7Fh0x+Uf1O1C2b7zoljoOkEL24Y=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Wt01DENCYX2fpDkE7hkW/03bv8mn121o+5QWgrII64/xN6AEXpkoJ33T6MWmgM1JJ+7KxRoFOghUesQkl9UBgjS9N8FtTJ/8FsW08Pebr5KEfiLZMKmqimYTK59bMiRHTvDc1gZTzJMxx4ShYAQ4TWRD9U9uqJiiiPt6tB4w/SA= 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=feKVUXzD; arc=none smtp.client-ip=170.10.133.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="feKVUXzD" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728007235; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QVlMkQ4oKYM9gBEztNWU+WNNZQwcLrOt4Z9NJVDjOHE=; b=feKVUXzD8QSm3KHgvBWH+DnUl1rn43zHwp+PU/vMgZZsBHIVTLz+CH0x0tRuqW/NSMdOlH BTRg8eluOrWA3Rs7JHlH9yijK2oqUw3AvKtFWn7ar5GqTC0YcBIRzV0EVXmne6VbmumIOw rItdAXK5dyXZnZpPFzMx9om0RjkrNW8= Received: from mx-prod-mc-05.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-104-90JBbasfNli1cM-4pjlN_w-1; Thu, 03 Oct 2024 22:00:34 -0400 X-MC-Unique: 90JBbasfNli1cM-4pjlN_w-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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6CF7619560A3; Fri, 4 Oct 2024 02:00:33 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.45]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 10A5A19560A2; Fri, 4 Oct 2024 02:00:29 +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 v2 6/7] vhost: Add kthread support in function vhost_worker_create Date: Fri, 4 Oct 2024 09:58:20 +0800 Message-ID: <20241004015937.2286459-7-lulu@redhat.com> In-Reply-To: <20241004015937.2286459-1-lulu@redhat.com> References: <20241004015937.2286459-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-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" Split the function vhost_worker_create to support both task and kthread 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 1cdaafa1b8b4 ("vhost: replace single worker pointer with xarray") commit c011bb669ddc ("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 eb30da658bfe..08c9e77916ca 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -808,7 +808,8 @@ static void vhost_workers_free(struct vhost_dev *dev) else vhost_workers_free_kthread(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; @@ -849,6 +850,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) @@ -937,6 +982,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 (enforce_inherit_owner) + return vhost_worker_create_task(dev); + else + return vhost_worker_create_kthread(dev); +} + /* Caller must have device mutex */ static int vhost_new_worker(struct vhost_dev *dev, struct vhost_worker_state *info) --=20 2.45.0