From nobody Thu Nov 28 06:48:39 2024 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 E39B41B7E9 for ; Fri, 4 Oct 2024 02:00:14 +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=1728007216; cv=none; b=NU00xOKveTra4IS+qYKqDvTGCUtFQow+9nOrPMsXW8K8sPdZTpL4gXp/BV/yW5sCRnmMiWXdkb20Dcm2OhUQNGmciUT/+vP1+eat+nANSfhFNN5f276XaNXNFY5wl4CK49Zf7F5h60B2hVplyoaCKj4DsCzBg9E4umDwuyzrMlo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728007216; c=relaxed/simple; bh=BjJr59mp9uuv2YmTKWyzhnhDDsNGKnGb+2hQDETXr2M=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=p3y0FfwhNWp6x0vL7SxrL0pED/7/h8SEceIYyGjDa5xCm8Ln09EaDWS4ENeHOJ6O6zm69beniYh3BkXnS+G6ymAR/V94KH3cvNkCz3BsePzsE/ax/h2s+Ito3TPUB2JGSh5edasXku8+HUeya4QeDKY4ZGxRlU4Z2JBI6nBSlBo= 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=WacstYPt; 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="WacstYPt" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728007213; 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=ocgoI1lOofS6GZhyj4LGKPzfRZFIHFLkb07SAt9nUao=; b=WacstYPte6ixydlaBOTXIlkPYPwtjoMAYqGTxa40WPD9MCZSreTQlIIFpRmhTNiNIbh/VY Q6ix1gvG0pAmiKitNh12QEOkpPjRR4O3fDOzuT5Yo/RfsYa+M2C0Tigt1hCrUvkNrfHFZF Yw7Rl3RKW4/5HqM+qXzr+73dMG7y6hk= Received: from mx-prod-mc-01.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-629-311buRSkN1GuMCITReGcrQ-1; Thu, 03 Oct 2024 21:59:59 -0400 X-MC-Unique: 311buRSkN1GuMCITReGcrQ-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 44F0719560B2; Fri, 4 Oct 2024 01:59:58 +0000 (UTC) Received: from server.redhat.com (unknown [10.72.112.45]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id D2BF219560A3; Fri, 4 Oct 2024 01:59:54 +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 3/7] vhost: Add kthread support in function vhost_workers_free() Date: Fri, 4 Oct 2024 09:58:17 +0800 Message-ID: <20241004015937.2286459-4-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.17 Content-Type: text/plain; charset="utf-8" Added back the previously removed function vhost_workers_free() and renamed it to vhost_workers_free_khtread(). The new vhost_workers_free() will select the different mode based on the value of the parameter. The old function vhost_workers_free was change to support task in commit 6e890c5d5021 ("vhost: use vhost_tasks for worker threads") also changed in commit a284f09effea ("vhost: Fix crash during early vhost_transport_send_pk= t calls") change to xarray in commit 1cdaafa1b8b4 ("vhost: replace single worker pointer with xarray") Signed-off-by: Cindy Lu --- drivers/vhost/vhost.c | 52 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c index ad359d4b725f..fed6671c1ffb 100644 --- a/drivers/vhost/vhost.c +++ b/drivers/vhost/vhost.c @@ -648,8 +648,21 @@ static void vhost_detach_mm(struct vhost_dev *dev) dev->mm =3D NULL; } =20 -static void vhost_worker_destroy(struct vhost_dev *dev, - struct vhost_worker *worker) +static void vhost_worker_destroy_kthread(struct vhost_dev *dev, + struct vhost_worker *worker) +{ + if (!worker) + return; + + WARN_ON(!llist_empty(&worker->work_list)); + + xa_erase(&dev->worker_xa, worker->id); + kthread_stop(worker->task); + kfree(worker); +} + +static void vhost_worker_destroy_task(struct vhost_dev *dev, + struct vhost_worker *worker) { if (!worker) return; @@ -660,7 +673,7 @@ static void vhost_worker_destroy(struct vhost_dev *dev, kfree(worker); } =20 -static void vhost_workers_free(struct vhost_dev *dev) +static void vhost_workers_free_task(struct vhost_dev *dev) { struct vhost_worker *worker; unsigned long i; @@ -675,10 +688,36 @@ static void vhost_workers_free(struct vhost_dev *dev) * created but couldn't clean up (it forgot or crashed). */ xa_for_each(&dev->worker_xa, i, worker) - vhost_worker_destroy(dev, worker); + vhost_worker_destroy_task(dev, worker); xa_destroy(&dev->worker_xa); } =20 +static void vhost_workers_free_kthread(struct vhost_dev *dev) +{ + struct vhost_worker *worker; + unsigned long i; + + if (!dev->use_worker) + return; + + for (i =3D 0; i < dev->nvqs; i++) + rcu_assign_pointer(dev->vqs[i]->worker, NULL); + /* + * Free the default worker we created and cleanup workers userspace + * created but couldn't clean up (it forgot or crashed). + */ + xa_for_each(&dev->worker_xa, i, worker) + vhost_worker_destroy_kthread(dev, worker); + xa_destroy(&dev->worker_xa); +} + +static void vhost_workers_free(struct vhost_dev *dev) +{ + if (enforce_inherit_owner) + vhost_workers_free_task(dev); + else + vhost_workers_free_kthread(dev); +} static struct vhost_worker *vhost_worker_create(struct vhost_dev *dev) { struct vhost_worker *worker; @@ -846,7 +885,10 @@ static int vhost_free_worker(struct vhost_dev *dev, __vhost_worker_flush(worker); mutex_unlock(&worker->mutex); =20 - vhost_worker_destroy(dev, worker); + if (enforce_inherit_owner) + vhost_worker_destroy_task(dev, worker); + else + vhost_worker_destroy_kthread(dev, worker); return 0; } =20 --=20 2.45.0