From nobody Tue Sep 9 21:30:23 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C51B7C6FD1D for ; Thu, 23 Mar 2023 05:33:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230445AbjCWFdo (ORCPT ); Thu, 23 Mar 2023 01:33:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60306 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230380AbjCWFdC (ORCPT ); Thu, 23 Mar 2023 01:33:02 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B43B2F05E for ; Wed, 22 Mar 2023 22:31:46 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id lr16-20020a17090b4b9000b0023f187954acso906796pjb.2 for ; Wed, 22 Mar 2023 22:31:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1679549504; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=zPmWWrpSgaPOGhUBOkIMLmxdo25WMNikMv3Jin+/Fz0=; b=btCCImtEbx8h8b5MD0xvsC/Msz/cOhLuDrbLg0UodbNPkO2mHUq11NP+bQKtb0oenJ ijTDX4qMzrOxRN8Q6m6QdHKsRDBa76hJsly6iCT01NSBqpkAQDe92CfLIZiuWEoBVMth aPzzyKYC1lrr0yhkDkhOBJCgUVryj7L3C+/W3RgS/IjjsoznZU7iTadcsrnVzciP+mpU wibTJbNT2kcHf4OTU7b2/kApuwKfm2BqzuBvmpHRrKqg+RIDVrZSc9iubeSdvWMR1rRR E50p9m2g+chyR2K+FXF/HExk9tuZNlFHaAzgDs8Hns1KSqmj9gv9dK6mg1K4OW6F83RR yhEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679549504; 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=zPmWWrpSgaPOGhUBOkIMLmxdo25WMNikMv3Jin+/Fz0=; b=FdYPAAIVtjFSJzGMrKTSSyjwg7LJo6zfoduR9ocoGaZbNeP2tw6UcpwbO/YV+89ZTa hcdvCeVqGkK30O5PdkeZw++9r2j5RxKO4gw0VCUAFo1+iw92ewmqKHH0VkTJR53U6IKN iH3SovaxaMAFFMZW3WkW38i4g61Q2pu7GfjevHoVEY4BB3gOmSClE9mo3mO73m5kM8Ga fLCak4KVSHQdFp5ouWbjAor+c76yyg7XbD2OU5bD7w3y5kHXHos4ZjExX0cZ54ktpWAb A2j4X5y5AjkixjQ/JzkyrlH5OtgVWkLVaVfJLyF8fKIwmbRxsWfKm3NNGFlaATXgv/R2 AzPA== X-Gm-Message-State: AAQBX9eyxy66AJQgx5SRgqyxzP1UQTZK8HJENFchHv/ESH4clS1VL1KC tx9tSuKQgN7p58f6sUdE1bVB X-Google-Smtp-Source: AKy350aYQXtp/fxWTfa15HOXnombEQ5hcdOFqTe4R06IwntYd5TeLT8X9GUayCtlAZZ6G7O2XGmANQ== X-Received: by 2002:a17:903:32d1:b0:1a1:abc9:198f with SMTP id i17-20020a17090332d100b001a1abc9198fmr4732456plr.4.1679549504645; Wed, 22 Mar 2023 22:31:44 -0700 (PDT) Received: from localhost ([139.177.225.255]) by smtp.gmail.com with ESMTPSA id p9-20020a63f449000000b0050bf1d1cdc8sm10687768pgk.21.2023.03.22.22.31.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Mar 2023 22:31:44 -0700 (PDT) From: Xie Yongji To: mst@redhat.com, jasowang@redhat.com, tglx@linutronix.de, hch@lst.de Cc: virtualization@lists.linux-foundation.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 10/11] vduse: Delay iova domain creation Date: Thu, 23 Mar 2023 13:30:42 +0800 Message-Id: <20230323053043.35-11-xieyongji@bytedance.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230323053043.35-1-xieyongji@bytedance.com> References: <20230323053043.35-1-xieyongji@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Delay creating iova domain until the vduse device is registered to vdpa bus. This is a preparation for adding sysfs interface to support specifying bounce buffer size for the iova domain. Signed-off-by: Xie Yongji Acked-by: Jason Wang --- drivers/vdpa/vdpa_user/vduse_dev.c | 75 +++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/drivers/vdpa/vdpa_user/vduse_dev.c b/drivers/vdpa/vdpa_user/vd= use_dev.c index 8c06f6ab960b..ccca84d51a28 100644 --- a/drivers/vdpa/vdpa_user/vduse_dev.c +++ b/drivers/vdpa/vdpa_user/vduse_dev.c @@ -111,6 +111,8 @@ struct vduse_dev { u32 vq_align; struct vduse_umem *umem; struct mutex mem_lock; + unsigned int bounce_size; + struct mutex domain_lock; }; =20 struct vduse_dev_msg { @@ -425,7 +427,7 @@ static void vduse_dev_reset(struct vduse_dev *dev) struct vduse_iova_domain *domain =3D dev->domain; =20 /* The coherent mappings are handled in vduse_dev_free_coherent() */ - if (domain->bounce_map) + if (domain && domain->bounce_map) vduse_domain_reset_bounce_map(domain); =20 down_write(&dev->rwsem); @@ -993,6 +995,9 @@ static int vduse_dev_dereg_umem(struct vduse_dev *dev, goto unlock; =20 ret =3D -EINVAL; + if (!dev->domain) + goto unlock; + if (dev->umem->iova !=3D iova || size !=3D dev->domain->bounce_size) goto unlock; =20 @@ -1019,7 +1024,7 @@ static int vduse_dev_reg_umem(struct vduse_dev *dev, unsigned long npages, lock_limit; int ret; =20 - if (!dev->domain->bounce_map || + if (!dev->domain || !dev->domain->bounce_map || size !=3D dev->domain->bounce_size || iova !=3D 0 || uaddr & ~PAGE_MASK) return -EINVAL; @@ -1109,7 +1114,6 @@ static long vduse_dev_ioctl(struct file *file, unsign= ed int cmd, struct vduse_iotlb_entry entry; struct vhost_iotlb_map *map; struct vdpa_map_file *map_file; - struct vduse_iova_domain *domain =3D dev->domain; struct file *f =3D NULL; =20 ret =3D -EFAULT; @@ -1120,8 +1124,13 @@ static long vduse_dev_ioctl(struct file *file, unsig= ned int cmd, if (entry.start > entry.last) break; =20 - spin_lock(&domain->iotlb_lock); - map =3D vhost_iotlb_itree_first(domain->iotlb, + mutex_lock(&dev->domain_lock); + if (!dev->domain) { + mutex_unlock(&dev->domain_lock); + break; + } + spin_lock(&dev->domain->iotlb_lock); + map =3D vhost_iotlb_itree_first(dev->domain->iotlb, entry.start, entry.last); if (map) { map_file =3D (struct vdpa_map_file *)map->opaque; @@ -1131,7 +1140,8 @@ static long vduse_dev_ioctl(struct file *file, unsign= ed int cmd, entry.last =3D map->last; entry.perm =3D map->perm; } - spin_unlock(&domain->iotlb_lock); + spin_unlock(&dev->domain->iotlb_lock); + mutex_unlock(&dev->domain_lock); ret =3D -EINVAL; if (!f) break; @@ -1284,8 +1294,10 @@ static long vduse_dev_ioctl(struct file *file, unsig= ned int cmd, sizeof(umem.reserved))) break; =20 + mutex_lock(&dev->domain_lock); ret =3D vduse_dev_reg_umem(dev, umem.iova, umem.uaddr, umem.size); + mutex_unlock(&dev->domain_lock); break; } case VDUSE_IOTLB_DEREG_UMEM: { @@ -1299,15 +1311,15 @@ static long vduse_dev_ioctl(struct file *file, unsi= gned int cmd, if (!is_mem_zero((const char *)umem.reserved, sizeof(umem.reserved))) break; - + mutex_lock(&dev->domain_lock); ret =3D vduse_dev_dereg_umem(dev, umem.iova, umem.size); + mutex_unlock(&dev->domain_lock); break; } case VDUSE_IOTLB_GET_INFO: { struct vduse_iova_info info; struct vhost_iotlb_map *map; - struct vduse_iova_domain *domain =3D dev->domain; =20 ret =3D -EFAULT; if (copy_from_user(&info, argp, sizeof(info))) @@ -1321,18 +1333,24 @@ static long vduse_dev_ioctl(struct file *file, unsi= gned int cmd, sizeof(info.reserved))) break; =20 - spin_lock(&domain->iotlb_lock); - map =3D vhost_iotlb_itree_first(domain->iotlb, + mutex_lock(&dev->domain_lock); + if (!dev->domain) { + mutex_unlock(&dev->domain_lock); + break; + } + spin_lock(&dev->domain->iotlb_lock); + map =3D vhost_iotlb_itree_first(dev->domain->iotlb, info.start, info.last); if (map) { info.start =3D map->start; info.last =3D map->last; info.capability =3D 0; - if (domain->bounce_map && map->start =3D=3D 0 && - map->last =3D=3D domain->bounce_size - 1) + if (dev->domain->bounce_map && map->start =3D=3D 0 && + map->last =3D=3D dev->domain->bounce_size - 1) info.capability |=3D VDUSE_IOVA_CAP_UMEM; } - spin_unlock(&domain->iotlb_lock); + spin_unlock(&dev->domain->iotlb_lock); + mutex_unlock(&dev->domain_lock); if (!map) break; =20 @@ -1355,7 +1373,10 @@ static int vduse_dev_release(struct inode *inode, st= ruct file *file) { struct vduse_dev *dev =3D file->private_data; =20 - vduse_dev_dereg_umem(dev, 0, dev->domain->bounce_size); + mutex_lock(&dev->domain_lock); + if (dev->domain) + vduse_dev_dereg_umem(dev, 0, dev->domain->bounce_size); + mutex_unlock(&dev->domain_lock); spin_lock(&dev->msg_lock); /* Make sure the inflight messages can processed after reconncection */ list_splice_init(&dev->recv_list, &dev->send_list); @@ -1564,6 +1585,7 @@ static struct vduse_dev *vduse_dev_create(void) =20 mutex_init(&dev->lock); mutex_init(&dev->mem_lock); + mutex_init(&dev->domain_lock); spin_lock_init(&dev->msg_lock); INIT_LIST_HEAD(&dev->send_list); INIT_LIST_HEAD(&dev->recv_list); @@ -1613,7 +1635,8 @@ static int vduse_destroy_dev(char *name) idr_remove(&vduse_idr, dev->minor); kvfree(dev->config); vduse_dev_deinit_vqs(dev); - vduse_domain_destroy(dev->domain); + if (dev->domain) + vduse_domain_destroy(dev->domain); kfree(dev->name); vduse_dev_destroy(dev); module_put(THIS_MODULE); @@ -1722,11 +1745,7 @@ static int vduse_create_dev(struct vduse_dev_config = *config, if (!dev->name) goto err_str; =20 - dev->domain =3D vduse_domain_create(VDUSE_IOVA_SIZE - 1, - VDUSE_BOUNCE_SIZE); - if (!dev->domain) - goto err_domain; - + dev->bounce_size =3D VDUSE_BOUNCE_SIZE; dev->config =3D config_buf; dev->config_size =3D config->config_size; =20 @@ -1756,8 +1775,6 @@ static int vduse_create_dev(struct vduse_dev_config *= config, err_dev: idr_remove(&vduse_idr, dev->minor); err_idr: - vduse_domain_destroy(dev->domain); -err_domain: kfree(dev->name); err_str: vduse_dev_destroy(dev); @@ -1924,9 +1941,23 @@ static int vdpa_dev_add(struct vdpa_mgmt_dev *mdev, = const char *name, if (ret) return ret; =20 + mutex_lock(&dev->domain_lock); + if (!dev->domain) + dev->domain =3D vduse_domain_create(VDUSE_IOVA_SIZE - 1, + dev->bounce_size); + mutex_unlock(&dev->domain_lock); + if (!dev->domain) { + put_device(&dev->vdev->vdpa.dev); + return -ENOMEM; + } + ret =3D _vdpa_register_device(&dev->vdev->vdpa, dev->vq_num); if (ret) { put_device(&dev->vdev->vdpa.dev); + mutex_lock(&dev->domain_lock); + vduse_domain_destroy(dev->domain); + dev->domain =3D NULL; + mutex_unlock(&dev->domain_lock); return ret; } =20 --=20 2.20.1