From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl0-f71.google.com (mail-pl0-f71.google.com [209.85.160.71]) by kanga.kvack.org (Postfix) with ESMTP id F32376B000D for ; Thu, 24 May 2018 08:35:49 -0400 (EDT) Received: by mail-pl0-f71.google.com with SMTP id a6-v6so913094pll.22 for ; Thu, 24 May 2018 05:35:49 -0700 (PDT) Received: from huawei.com (szxga04-in.huawei.com. [45.249.212.190]) by mx.google.com with ESMTPS id d31-v6si21398656pld.23.2018.05.24.05.35.48 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 May 2018 05:35:48 -0700 (PDT) Subject: Re: [PATCH v2 13/40] vfio: Add support for Shared Virtual Addressing References: <20180511190641.23008-1-jean-philippe.brucker@arm.com> <20180511190641.23008-14-jean-philippe.brucker@arm.com> <5B0536A3.1000304@huawei.com> From: Xu Zaibo Message-ID: <5B06B17C.1090809@huawei.com> Date: Thu, 24 May 2018 20:35:08 +0800 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------070003080700080202060305" Sender: owner-linux-mm@kvack.org List-ID: To: Jean-Philippe Brucker , linux-arm-kernel@lists.infradead.org, linux-pci@vger.kernel.org, linux-acpi@vger.kernel.org, devicetree@vger.kernel.org, iommu@lists.linux-foundation.org, kvm@vger.kernel.org, linux-mm@kvack.org Cc: will.deacon@arm.com, okaya@codeaurora.org, liguozhu , ashok.raj@intel.com, bharatku@xilinx.com, rfranz@cavium.com, rgummal@xilinx.com, dwmw2@infradead.org, ilias.apalodimas@linaro.org, christian.koenig@amd.com --------------070003080700080202060305 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 7bit On 2018/5/24 19:44, Jean-Philippe Brucker wrote: > Hi, > > On 23/05/18 10:38, Xu Zaibo wrote: >>> +static int vfio_iommu_bind_group(struct vfio_iommu *iommu, >>> + struct vfio_group *group, >>> + struct vfio_mm *vfio_mm) >>> +{ >>> + int ret; >>> + bool enabled_sva = false; >>> + struct vfio_iommu_sva_bind_data data = { >>> + .vfio_mm = vfio_mm, >>> + .iommu = iommu, >>> + .count = 0, >>> + }; >>> + >>> + if (!group->sva_enabled) { >>> + ret = iommu_group_for_each_dev(group->iommu_group, NULL, >>> + vfio_iommu_sva_init); >> Do we need to do *sva_init here or do anything to avoid repeated >> initiation? >> while another process already did initiation at this device, I think >> that current process will get an EEXIST. > Right, sva_init() must be called once for any device that intends to use > bind(). For the second process though, group->sva_enabled will be true > so we won't call sva_init() again, only bind(). > Well, while I create mediated devices based on one parent device to support multiple processes(a new process will create a new 'vfio_group' for the corresponding mediated device, and 'sva_enabled' cannot work any more), in fact, *sva_init and *sva_shutdown are basically working on parent device, so, as a result, I just only need sva initiation and shutdown on the parent device only once. So I change the two as following: /@@ -551,8 +565,18 @@ int iommu_sva_device_init(struct device *dev, unsigned long features,/ if (features & ~IOMMU_SVA_FEAT_IOPF) return -EINVAL; /+ /* If already exists, do nothing */// //+ mutex_lock(&dev->iommu_param->lock);// //+ if (dev->iommu_param->sva_param) {// //+ mutex_unlock(&dev->iommu_param->lock);// //+ return 0;// //+ }// //+ mutex_unlock(&dev->iommu_param->lock);// //// // if (features & IOMMU_SVA_FEAT_IOPF) {// // ret = iommu_register_device_fault_handler(dev, iommu_queue_iopf,///// / // // //@@ -621,6 +646,14 @@ int iommu_sva_device_shutdown(struct device *dev)// // if (!domain)// // return -ENODEV;// //// //+ /* If any other process is working on the device, shut down does nothing. */// //+ mutex_lock(&dev->iommu_param->lock);// //+ if (!list_empty(&dev->iommu_param->sva_param->mm_list)) {// //+ mutex_unlock(&dev->iommu_param->lock);// //+ return 0;// //+ }// //+ mutex_unlock(&dev->iommu_param->lock);// //+// // __iommu_sva_unbind_dev_all(dev);// //// // mutex_lock(&dev->iommu_param->lock);/ I add the above two checkings in both *sva_init and *sva_shutdown, it is working now, but i don't know if it will cause any new problems. What's more, i doubt if it is reasonable to check this to avoid repeating operation in VFIO, maybe it is better to check in IOMMU. :) Thanks Zaibo > > . > --------------070003080700080202060305 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 8bit

On 2018/5/24 19:44, Jean-Philippe Brucker wrote:
Hi,

On 23/05/18 10:38, Xu Zaibo wrote:
+static int vfio_iommu_bind_group(struct vfio_iommu *iommu,
+A A A A A A A A A A A A A A A A  struct vfio_group *group,
+A A A A A A A A A A A A A A A A  struct vfio_mm *vfio_mm)
+{
+A A A  int ret;
+A A A  bool enabled_sva = false;
+A A A  struct vfio_iommu_sva_bind_data data = {
+A A A A A A A  .vfio_mmA A A  = vfio_mm,
+A A A A A A A  .iommuA A A A A A A  = iommu,
+A A A A A A A  .countA A A A A A A  = 0,
+A A A  };
+
+A A A  if (!group->sva_enabled) {
+A A A A A A A  ret = iommu_group_for_each_dev(group->iommu_group, NULL,
+A A A A A A A A A A A A A A A A A A A A A A A A A A  vfio_iommu_sva_init);
Do we need to do *sva_init here or do anything to avoid repeated
initiation?
while another process already did initiation at this device, I think
that current process will get an EEXIST.
Right, sva_init() must be called once for any device that intends to use
bind(). For the second process though, group->sva_enabled will be true
so we won't call sva_init() again, only bind().

Well, while I create mediated devices based on one parent device to support multiple
processes(a new process will create a new 'vfio_group' for the corresponding mediated device,
and 'sva_enabled' cannot work any more), in fact, *sva_init and *sva_shutdown are basically
working on parent device, so, as a result, I just only need sva initiation and shutdown on the
parent device only once. So I change the two as following:

@@ -551,8 +565,18 @@ int iommu_sva_device_init(struct device *dev, unsigned long features,
A A A A A if (features & ~IOMMU_SVA_FEAT_IOPF)
A A A A A A A return -EINVAL;

+A A A /* If already exists, do nothingA */
+A A A mutex_lock(&dev->iommu_param->lock);
+A A A if (dev->iommu_param->sva_param) {
+A A A A A A mutex_unlock(&dev->iommu_param->lock);
+A A A A A A return 0;
+A A A }
+A A A mutex_unlock(&dev->iommu_param->lock);
A
A A A A if (features & IOMMU_SVA_FEAT_IOPF) {
A A A A A A A ret = iommu_register_device_fault_handler(dev, iommu_queue_iopf,


@@ -621,6 +646,14 @@ int iommu_sva_device_shutdown(struct device *dev)
A A A A if (!domain)
A A A A A A A return -ENODEV;
A
+A A A /* If any other process is working on the device, shut down does nothing. */
+A A A mutex_lock(&dev->iommu_param->lock);
+A A A if (!list_empty(&dev->iommu_param->sva_param->mm_list)) {
+A A A A A A mutex_unlock(&dev->iommu_param->lock);
+A A A A A A return 0;
+A A A }
+A A A mutex_unlock(&dev->iommu_param->lock);
+
A A A A __iommu_sva_unbind_dev_all(dev);
A
A A A A mutex_lock(&dev->iommu_param->lock);

I add the above two checkings in both *sva_init and *sva_shutdown, it is working now,
but i don't know if it will cause any new problems. What's more, i doubt if it is
reasonable to check this to avoid repeating operation in VFIO, maybe it is better to check
in IOMMU. :)

Thanks
Zaibo

.


--------------070003080700080202060305--