Message ID | 20230302113421.174582-7-sgarzare@redhat.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp4185068wrd; Thu, 2 Mar 2023 03:38:47 -0800 (PST) X-Google-Smtp-Source: AK7set9mikggTdCRSxrbGKK6EprG3+xpIBcebieKOEfuiAJByYZNsGwPryPfzKDB5EGBVsXmeTKo X-Received: by 2002:aa7:c713:0:b0:4af:70cf:d142 with SMTP id i19-20020aa7c713000000b004af70cfd142mr1526577edq.17.1677757127760; Thu, 02 Mar 2023 03:38:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677757127; cv=none; d=google.com; s=arc-20160816; b=FGd7ryeUN1PrGoy69toAyUlZvhdsLC4TkAndb/8SdXUBIlS0BAsO98lS79i2Q1devQ HSa1heDM3q62H3qLCsm93erkISTFduclqURblwQ1HgAbEm2ZipoWR4D+22xR6wIG5JuZ vRFaXv7gTZRo66Jpmd5Oiek6eGrLQ5JBd8cW0JuRsN/9z7+tg5ea/XLhMA3ufGfvI2Lu ZUK8eZLOFNOiBzRU1zuyfeMoH4uaYxQ9J0axZTyHjCTnIsyCHOHVIEE+VW7G6JH0VAcF J+4U0c4a3SaLJ+OTzcTJjYbR9bVAYHVSulNa1pQpHyF9xnFgycPXTZUYabtO0aUzsvWN WNGw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=garxf7u2PYyPkA/AdwQgG8IfzxLSGUACcilXDazWE4o=; b=XZlAY/EjFNHqdK7u93Y1u2zUZMaWiGTIHR/EyVWfWtY/WLL6/RURWpH6HFLNFFE2KJ j7U5z2kRtnROwQfhX62Hefb5Xf1lr5ezakWA6ljvF92G7dFdHkasoeKbDsGv68POYfAO sjoiBBDThBmlIlSi0WgxEX8B3qqC+NCsgrsBNlaJUyRxjgGz+7hTBIvpRzm7Z7YpPzqO 0XPoLGappUkTqyvBc7TJuiH6zwmLcN5vOAvq0FBqXpCSTWTdaJA+wK7BYt5HYKHgvFi3 LsIzx9PI+SclTwEfgfMY+CspN3O2EdLB5a3i/ZTNupuvOKg9L6iWjTjpkH69Ik7B92WL N+5Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eYfmIggr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k11-20020a05640212cb00b004ad71106d26si17144217edx.60.2023.03.02.03.38.24; Thu, 02 Mar 2023 03:38:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eYfmIggr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229793AbjCBLgo (ORCPT <rfc822;davidbtadokoro@gmail.com> + 99 others); Thu, 2 Mar 2023 06:36:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55558 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229913AbjCBLgE (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 2 Mar 2023 06:36:04 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A3C07136F2 for <linux-kernel@vger.kernel.org>; Thu, 2 Mar 2023 03:35:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677756914; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=garxf7u2PYyPkA/AdwQgG8IfzxLSGUACcilXDazWE4o=; b=eYfmIggrFzs+QlJskU3P1wY5oLW8cnrT8NCG7jz8PNEMfCDXvtrKKLTBkqD5HXUNTdbB/N 1594+iYYehYrAJGWUffQ+yIXPAnIwgD3KkiRDPx9Ov6wIytgpWrkr8+TwIaPmwDi3O4RKq rUK1tysPzyQmgQR9189E3Y4G1g29Tvc= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-550-4ce3jHClP6C2VQ-cDkbndw-1; Thu, 02 Mar 2023 06:35:13 -0500 X-MC-Unique: 4ce3jHClP6C2VQ-cDkbndw-1 Received: by mail-qv1-f70.google.com with SMTP id pz4-20020ad45504000000b0056f060452adso8571212qvb.6 for <linux-kernel@vger.kernel.org>; Thu, 02 Mar 2023 03:35:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677756913; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=garxf7u2PYyPkA/AdwQgG8IfzxLSGUACcilXDazWE4o=; b=lRq97CYN9pb7TXkjxK8SkfNF4u3cmd/YnNnm+0tYzmbQkNfplbXooLny4BRbYu99BF E49t1sbHSpgzv8xt4uRsdEcDClcsR0D9QW4/bnZnMUWmZUJTFq2bp9GLllG5OXmZv25f cxYdCUizCoRiVAbTvavB7Qrm3Y0g9xcGWbiifDQadNnvEUje9PM1NMRWSe31dqiNv7IC dl3zjiGcWc6SIEDtJ5N7sFAN6r+oTQKVVq2BvYRGbobKazC/Ti0c+R6GiPsfnkBPTqOF HgQAG+Co8I+z3MeOyRq19b3uAhG2szOgEDBXAc6y9iZA21VLepyO6weAX/G9NJs+8J/O ucvA== X-Gm-Message-State: AO0yUKVgm8e74Ndn4+RRg3A2zoV/EInImFVdGVGrxZ5uqNEYFGXMFBtq ssbj4Kf66z9qZxR7PrfncI6SlZzQbcsi0d/c1IQ3oXvu2zHsqAkx8e/949Ozlhl1xlxDaSBibmB pZBaOEBymYAVnwJiD2HxV86X54AVZww== X-Received: by 2002:a05:6214:ca2:b0:56e:a620:7b39 with SMTP id s2-20020a0562140ca200b0056ea6207b39mr2884830qvs.4.1677756913180; Thu, 02 Mar 2023 03:35:13 -0800 (PST) X-Received: by 2002:a05:6214:ca2:b0:56e:a620:7b39 with SMTP id s2-20020a0562140ca200b0056ea6207b39mr2884814qvs.4.1677756912950; Thu, 02 Mar 2023 03:35:12 -0800 (PST) Received: from step1.redhat.com (c-115-213.cust-q.wadsl.it. [212.43.115.213]) by smtp.gmail.com with ESMTPSA id p12-20020a37420c000000b007426ec97253sm8383058qka.111.2023.03.02.03.35.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Mar 2023 03:35:12 -0800 (PST) From: Stefano Garzarella <sgarzare@redhat.com> To: virtualization@lists.linux-foundation.org Cc: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>, eperezma@redhat.com, netdev@vger.kernel.org, stefanha@redhat.com, linux-kernel@vger.kernel.org, Jason Wang <jasowang@redhat.com>, "Michael S. Tsirkin" <mst@redhat.com>, kvm@vger.kernel.org, Stefano Garzarella <sgarzare@redhat.com> Subject: [PATCH v2 6/8] vdpa_sim: use kthread worker Date: Thu, 2 Mar 2023 12:34:19 +0100 Message-Id: <20230302113421.174582-7-sgarzare@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230302113421.174582-1-sgarzare@redhat.com> References: <20230302113421.174582-1-sgarzare@redhat.com> MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759255857931791328?= X-GMAIL-MSGID: =?utf-8?q?1759255857931791328?= |
Series |
vdpa_sim: add support for user VA
|
|
Commit Message
Stefano Garzarella
March 2, 2023, 11:34 a.m. UTC
Let's use our own kthread to run device jobs.
This allows us more flexibility, especially we can attach the kthread
to the user address space when vDPA uses user's VA.
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
drivers/vdpa/vdpa_sim/vdpa_sim.h | 3 ++-
drivers/vdpa/vdpa_sim/vdpa_sim.c | 17 ++++++++++++-----
2 files changed, 14 insertions(+), 6 deletions(-)
Comments
On Thu, Mar 02, 2023 at 12:34:19PM +0100, Stefano Garzarella wrote: > Let's use our own kthread to run device jobs. > This allows us more flexibility, especially we can attach the kthread > to the user address space when vDPA uses user's VA. > > Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> > --- > drivers/vdpa/vdpa_sim/vdpa_sim.h | 3 ++- > drivers/vdpa/vdpa_sim/vdpa_sim.c | 17 ++++++++++++----- > 2 files changed, 14 insertions(+), 6 deletions(-) > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h > index acee20faaf6a..ce83f9130a5d 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h > @@ -57,7 +57,8 @@ struct vdpasim_dev_attr { > struct vdpasim { > struct vdpa_device vdpa; > struct vdpasim_virtqueue *vqs; > - struct work_struct work; > + struct kthread_worker *worker; > + struct kthread_work work; > struct vdpasim_dev_attr dev_attr; > /* spinlock to synchronize virtqueue state */ > spinlock_t lock; > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c > index a6ee830efc38..6feb29726c2a 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c > @@ -11,8 +11,8 @@ > #include <linux/module.h> > #include <linux/device.h> > #include <linux/kernel.h> > +#include <linux/kthread.h> > #include <linux/slab.h> > -#include <linux/sched.h> > #include <linux/dma-map-ops.h> > #include <linux/vringh.h> > #include <linux/vdpa.h> > @@ -116,7 +116,7 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim) > static const struct vdpa_config_ops vdpasim_config_ops; > static const struct vdpa_config_ops vdpasim_batch_config_ops; > > -static void vdpasim_work_fn(struct work_struct *work) > +static void vdpasim_work_fn(struct kthread_work *work) > { > struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); > > @@ -159,7 +159,13 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, > > vdpasim = vdpa_to_sim(vdpa); > vdpasim->dev_attr = *dev_attr; > - INIT_WORK(&vdpasim->work, vdpasim_work_fn); > + > + kthread_init_work(&vdpasim->work, vdpasim_work_fn); > + vdpasim->worker = kthread_create_worker(0, "vDPA sim worker: %s", > + dev_attr->name); > + if (IS_ERR(vdpasim->worker)) > + goto err_iommu; Branching to err_iommu will result in a call to put_device(dev)... > + > spin_lock_init(&vdpasim->lock); > spin_lock_init(&vdpasim->iommu_lock); ... but dev is not initialised until the line following this hunk, which is: dev = &vdpasim->vdpa.dev; In order to avoid leaking dev I _think_ the correct approach is to move the initialisation of dev above the branch to err_iommu, perhaps above the call to kthread_init_work() is a good place. This does move the assignment outside the locks above. But I _think_ that is ok. > @@ -212,7 +218,7 @@ EXPORT_SYMBOL_GPL(vdpasim_create); > > void vdpasim_schedule_work(struct vdpasim *vdpasim) > { > - schedule_work(&vdpasim->work); > + kthread_queue_work(vdpasim->worker, &vdpasim->work); > } > EXPORT_SYMBOL_GPL(vdpasim_schedule_work); > > @@ -612,7 +618,8 @@ static void vdpasim_free(struct vdpa_device *vdpa) > struct vdpasim *vdpasim = vdpa_to_sim(vdpa); > int i; > > - cancel_work_sync(&vdpasim->work); > + kthread_cancel_work_sync(&vdpasim->work); > + kthread_destroy_worker(vdpasim->worker); > > for (i = 0; i < vdpasim->dev_attr.nvqs; i++) { > vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov); > -- > 2.39.2 >
On Thu, Mar 02, 2023 at 04:30:04PM +0100, Simon Horman wrote: >On Thu, Mar 02, 2023 at 12:34:19PM +0100, Stefano Garzarella wrote: >> Let's use our own kthread to run device jobs. >> This allows us more flexibility, especially we can attach the kthread >> to the user address space when vDPA uses user's VA. >> >> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> >> --- >> drivers/vdpa/vdpa_sim/vdpa_sim.h | 3 ++- >> drivers/vdpa/vdpa_sim/vdpa_sim.c | 17 ++++++++++++----- >> 2 files changed, 14 insertions(+), 6 deletions(-) >> >> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h >> index acee20faaf6a..ce83f9130a5d 100644 >> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h >> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h >> @@ -57,7 +57,8 @@ struct vdpasim_dev_attr { >> struct vdpasim { >> struct vdpa_device vdpa; >> struct vdpasim_virtqueue *vqs; >> - struct work_struct work; >> + struct kthread_worker *worker; >> + struct kthread_work work; >> struct vdpasim_dev_attr dev_attr; >> /* spinlock to synchronize virtqueue state */ >> spinlock_t lock; >> diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c >> index a6ee830efc38..6feb29726c2a 100644 >> --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c >> +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c >> @@ -11,8 +11,8 @@ >> #include <linux/module.h> >> #include <linux/device.h> >> #include <linux/kernel.h> >> +#include <linux/kthread.h> >> #include <linux/slab.h> >> -#include <linux/sched.h> >> #include <linux/dma-map-ops.h> >> #include <linux/vringh.h> >> #include <linux/vdpa.h> >> @@ -116,7 +116,7 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim) >> static const struct vdpa_config_ops vdpasim_config_ops; >> static const struct vdpa_config_ops vdpasim_batch_config_ops; >> >> -static void vdpasim_work_fn(struct work_struct *work) >> +static void vdpasim_work_fn(struct kthread_work *work) >> { >> struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); >> >> @@ -159,7 +159,13 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, >> >> vdpasim = vdpa_to_sim(vdpa); >> vdpasim->dev_attr = *dev_attr; >> - INIT_WORK(&vdpasim->work, vdpasim_work_fn); >> + >> + kthread_init_work(&vdpasim->work, vdpasim_work_fn); >> + vdpasim->worker = kthread_create_worker(0, "vDPA sim worker: %s", >> + dev_attr->name); >> + if (IS_ERR(vdpasim->worker)) >> + goto err_iommu; > >Branching to err_iommu will result in a call to put_device(dev)... Good catch! > >> + >> spin_lock_init(&vdpasim->lock); >> spin_lock_init(&vdpasim->iommu_lock); > >... but dev is not initialised until the line following this hunk, >which is: > > dev = &vdpasim->vdpa.dev; > >In order to avoid leaking dev I _think_ the correct approach >is to move the initialisation of dev above the branch to >err_iommu, perhaps above the call to kthread_init_work() >is a good place. Yep, I agree. I'll fix in v3. Thanks, Stefano > >This does move the assignment outside the locks above. >But I _think_ that is ok. > >> @@ -212,7 +218,7 @@ EXPORT_SYMBOL_GPL(vdpasim_create); >> >> void vdpasim_schedule_work(struct vdpasim *vdpasim) >> { >> - schedule_work(&vdpasim->work); >> + kthread_queue_work(vdpasim->worker, &vdpasim->work); >> } >> EXPORT_SYMBOL_GPL(vdpasim_schedule_work); >> >> @@ -612,7 +618,8 @@ static void vdpasim_free(struct vdpa_device *vdpa) >> struct vdpasim *vdpasim = vdpa_to_sim(vdpa); >> int i; >> >> - cancel_work_sync(&vdpasim->work); >> + kthread_cancel_work_sync(&vdpasim->work); >> + kthread_destroy_worker(vdpasim->worker); >> >> for (i = 0; i < vdpasim->dev_attr.nvqs; i++) { >> vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov); >> -- >> 2.39.2 >> >
Hi Stefano, I love your patch! Perhaps something to improve: [auto build test WARNING on mst-vhost/linux-next] [also build test WARNING on linus/master next-20230303] [cannot apply to v6.2] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in https://git-scm.com/docs/git-format-patch#_base_tree_information] url: https://github.com/intel-lab-lkp/linux/commits/Stefano-Garzarella/vdpa-add-bind_mm-unbind_mm-callbacks/20230302-193850 base: https://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost.git linux-next patch link: https://lore.kernel.org/r/20230302113421.174582-7-sgarzare%40redhat.com patch subject: [PATCH v2 6/8] vdpa_sim: use kthread worker config: i386-allyesconfig (https://download.01.org/0day-ci/archive/20230305/202303051841.bPAIzJRy-lkp@intel.com/config) compiler: clang version 14.0.6 (https://github.com/llvm/llvm-project f28c006a5895fc0e329fe15fead81e37457cb1d1) reproduce (this is a W=1 build): wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # https://github.com/intel-lab-lkp/linux/commit/5b2107457ac0e7b1bb0aa3635ebf13b02e82bb78 git remote add linux-review https://github.com/intel-lab-lkp/linux git fetch --no-tags linux-review Stefano-Garzarella/vdpa-add-bind_mm-unbind_mm-callbacks/20230302-193850 git checkout 5b2107457ac0e7b1bb0aa3635ebf13b02e82bb78 # save the config file mkdir build_dir && cp config build_dir/.config COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=i386 SHELL=/bin/bash drivers/net/wireless/ath/ath10k/ drivers/vdpa/vdpa_sim/ fs/erofs/ If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <lkp@intel.com> | Link: https://lore.kernel.org/oe-kbuild-all/202303051841.bPAIzJRy-lkp@intel.com/ All warnings (new ones prefixed by >>): >> drivers/vdpa/vdpa_sim/vdpa_sim.c:166:6: warning: variable 'dev' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized] if (IS_ERR(vdpasim->worker)) ^~~~~~~~~~~~~~~~~~~~~~~ drivers/vdpa/vdpa_sim/vdpa_sim.c:213:13: note: uninitialized use occurs here put_device(dev); ^~~ drivers/vdpa/vdpa_sim/vdpa_sim.c:166:2: note: remove the 'if' if its condition is always false if (IS_ERR(vdpasim->worker)) ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ drivers/vdpa/vdpa_sim/vdpa_sim.c:132:20: note: initialize the variable 'dev' to silence this warning struct device *dev; ^ = NULL 1 warning generated. vim +166 drivers/vdpa/vdpa_sim/vdpa_sim.c 125 126 struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, 127 const struct vdpa_dev_set_config *config) 128 { 129 const struct vdpa_config_ops *ops; 130 struct vdpa_device *vdpa; 131 struct vdpasim *vdpasim; 132 struct device *dev; 133 int i, ret = -ENOMEM; 134 135 if (!dev_attr->alloc_size) 136 return ERR_PTR(-EINVAL); 137 138 if (config->mask & BIT_ULL(VDPA_ATTR_DEV_FEATURES)) { 139 if (config->device_features & 140 ~dev_attr->supported_features) 141 return ERR_PTR(-EINVAL); 142 dev_attr->supported_features = 143 config->device_features; 144 } 145 146 if (batch_mapping) 147 ops = &vdpasim_batch_config_ops; 148 else 149 ops = &vdpasim_config_ops; 150 151 vdpa = __vdpa_alloc_device(NULL, ops, 152 dev_attr->ngroups, dev_attr->nas, 153 dev_attr->alloc_size, 154 dev_attr->name, false); 155 if (IS_ERR(vdpa)) { 156 ret = PTR_ERR(vdpa); 157 goto err_alloc; 158 } 159 160 vdpasim = vdpa_to_sim(vdpa); 161 vdpasim->dev_attr = *dev_attr; 162 163 kthread_init_work(&vdpasim->work, vdpasim_work_fn); 164 vdpasim->worker = kthread_create_worker(0, "vDPA sim worker: %s", 165 dev_attr->name); > 166 if (IS_ERR(vdpasim->worker)) 167 goto err_iommu; 168 169 spin_lock_init(&vdpasim->lock); 170 spin_lock_init(&vdpasim->iommu_lock); 171 172 dev = &vdpasim->vdpa.dev; 173 dev->dma_mask = &dev->coherent_dma_mask; 174 if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) 175 goto err_iommu; 176 vdpasim->vdpa.mdev = dev_attr->mgmt_dev; 177 178 vdpasim->config = kzalloc(dev_attr->config_size, GFP_KERNEL); 179 if (!vdpasim->config) 180 goto err_iommu; 181 182 vdpasim->vqs = kcalloc(dev_attr->nvqs, sizeof(struct vdpasim_virtqueue), 183 GFP_KERNEL); 184 if (!vdpasim->vqs) 185 goto err_iommu; 186 187 vdpasim->iommu = kmalloc_array(vdpasim->dev_attr.nas, 188 sizeof(*vdpasim->iommu), GFP_KERNEL); 189 if (!vdpasim->iommu) 190 goto err_iommu; 191 192 vdpasim->iommu_pt = kmalloc_array(vdpasim->dev_attr.nas, 193 sizeof(*vdpasim->iommu_pt), GFP_KERNEL); 194 if (!vdpasim->iommu_pt) 195 goto err_iommu; 196 197 for (i = 0; i < vdpasim->dev_attr.nas; i++) 198 vhost_iotlb_init(&vdpasim->iommu[i], max_iotlb_entries, 0); 199 200 vdpasim->buffer = kvmalloc(dev_attr->buffer_size, GFP_KERNEL); 201 if (!vdpasim->buffer) 202 goto err_iommu; 203 204 for (i = 0; i < dev_attr->nvqs; i++) 205 vringh_set_iotlb(&vdpasim->vqs[i].vring, &vdpasim->iommu[0], 206 &vdpasim->iommu_lock); 207 208 vdpasim->vdpa.dma_dev = dev; 209 210 return vdpasim; 211 212 err_iommu: 213 put_device(dev); 214 err_alloc: 215 return ERR_PTR(ret); 216 } 217 EXPORT_SYMBOL_GPL(vdpasim_create); 218
On Thu, Mar 2, 2023 at 7:35 PM Stefano Garzarella <sgarzare@redhat.com> wrote: > > Let's use our own kthread to run device jobs. > This allows us more flexibility, especially we can attach the kthread > to the user address space when vDPA uses user's VA. > > Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Acked-by: Jason Wang <jasowang@redhat.com> Thanks > --- > drivers/vdpa/vdpa_sim/vdpa_sim.h | 3 ++- > drivers/vdpa/vdpa_sim/vdpa_sim.c | 17 ++++++++++++----- > 2 files changed, 14 insertions(+), 6 deletions(-) > > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h > index acee20faaf6a..ce83f9130a5d 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h > @@ -57,7 +57,8 @@ struct vdpasim_dev_attr { > struct vdpasim { > struct vdpa_device vdpa; > struct vdpasim_virtqueue *vqs; > - struct work_struct work; > + struct kthread_worker *worker; > + struct kthread_work work; > struct vdpasim_dev_attr dev_attr; > /* spinlock to synchronize virtqueue state */ > spinlock_t lock; > diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c > index a6ee830efc38..6feb29726c2a 100644 > --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c > +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c > @@ -11,8 +11,8 @@ > #include <linux/module.h> > #include <linux/device.h> > #include <linux/kernel.h> > +#include <linux/kthread.h> > #include <linux/slab.h> > -#include <linux/sched.h> > #include <linux/dma-map-ops.h> > #include <linux/vringh.h> > #include <linux/vdpa.h> > @@ -116,7 +116,7 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim) > static const struct vdpa_config_ops vdpasim_config_ops; > static const struct vdpa_config_ops vdpasim_batch_config_ops; > > -static void vdpasim_work_fn(struct work_struct *work) > +static void vdpasim_work_fn(struct kthread_work *work) > { > struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); > > @@ -159,7 +159,13 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, > > vdpasim = vdpa_to_sim(vdpa); > vdpasim->dev_attr = *dev_attr; > - INIT_WORK(&vdpasim->work, vdpasim_work_fn); > + > + kthread_init_work(&vdpasim->work, vdpasim_work_fn); > + vdpasim->worker = kthread_create_worker(0, "vDPA sim worker: %s", > + dev_attr->name); > + if (IS_ERR(vdpasim->worker)) > + goto err_iommu; > + > spin_lock_init(&vdpasim->lock); > spin_lock_init(&vdpasim->iommu_lock); > > @@ -212,7 +218,7 @@ EXPORT_SYMBOL_GPL(vdpasim_create); > > void vdpasim_schedule_work(struct vdpasim *vdpasim) > { > - schedule_work(&vdpasim->work); > + kthread_queue_work(vdpasim->worker, &vdpasim->work); > } > EXPORT_SYMBOL_GPL(vdpasim_schedule_work); > > @@ -612,7 +618,8 @@ static void vdpasim_free(struct vdpa_device *vdpa) > struct vdpasim *vdpasim = vdpa_to_sim(vdpa); > int i; > > - cancel_work_sync(&vdpasim->work); > + kthread_cancel_work_sync(&vdpasim->work); > + kthread_destroy_worker(vdpasim->worker); > > for (i = 0; i < vdpasim->dev_attr.nvqs; i++) { > vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov); > -- > 2.39.2 >
diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.h b/drivers/vdpa/vdpa_sim/vdpa_sim.h index acee20faaf6a..ce83f9130a5d 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.h +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.h @@ -57,7 +57,8 @@ struct vdpasim_dev_attr { struct vdpasim { struct vdpa_device vdpa; struct vdpasim_virtqueue *vqs; - struct work_struct work; + struct kthread_worker *worker; + struct kthread_work work; struct vdpasim_dev_attr dev_attr; /* spinlock to synchronize virtqueue state */ spinlock_t lock; diff --git a/drivers/vdpa/vdpa_sim/vdpa_sim.c b/drivers/vdpa/vdpa_sim/vdpa_sim.c index a6ee830efc38..6feb29726c2a 100644 --- a/drivers/vdpa/vdpa_sim/vdpa_sim.c +++ b/drivers/vdpa/vdpa_sim/vdpa_sim.c @@ -11,8 +11,8 @@ #include <linux/module.h> #include <linux/device.h> #include <linux/kernel.h> +#include <linux/kthread.h> #include <linux/slab.h> -#include <linux/sched.h> #include <linux/dma-map-ops.h> #include <linux/vringh.h> #include <linux/vdpa.h> @@ -116,7 +116,7 @@ static void vdpasim_do_reset(struct vdpasim *vdpasim) static const struct vdpa_config_ops vdpasim_config_ops; static const struct vdpa_config_ops vdpasim_batch_config_ops; -static void vdpasim_work_fn(struct work_struct *work) +static void vdpasim_work_fn(struct kthread_work *work) { struct vdpasim *vdpasim = container_of(work, struct vdpasim, work); @@ -159,7 +159,13 @@ struct vdpasim *vdpasim_create(struct vdpasim_dev_attr *dev_attr, vdpasim = vdpa_to_sim(vdpa); vdpasim->dev_attr = *dev_attr; - INIT_WORK(&vdpasim->work, vdpasim_work_fn); + + kthread_init_work(&vdpasim->work, vdpasim_work_fn); + vdpasim->worker = kthread_create_worker(0, "vDPA sim worker: %s", + dev_attr->name); + if (IS_ERR(vdpasim->worker)) + goto err_iommu; + spin_lock_init(&vdpasim->lock); spin_lock_init(&vdpasim->iommu_lock); @@ -212,7 +218,7 @@ EXPORT_SYMBOL_GPL(vdpasim_create); void vdpasim_schedule_work(struct vdpasim *vdpasim) { - schedule_work(&vdpasim->work); + kthread_queue_work(vdpasim->worker, &vdpasim->work); } EXPORT_SYMBOL_GPL(vdpasim_schedule_work); @@ -612,7 +618,8 @@ static void vdpasim_free(struct vdpa_device *vdpa) struct vdpasim *vdpasim = vdpa_to_sim(vdpa); int i; - cancel_work_sync(&vdpasim->work); + kthread_cancel_work_sync(&vdpasim->work); + kthread_destroy_worker(vdpasim->worker); for (i = 0; i < vdpasim->dev_attr.nvqs; i++) { vringh_kiov_cleanup(&vdpasim->vqs[i].out_iov);