Skip to contentSkip to conversation

A General Accelerator Framework, WarpDrive

RFC PATCH Superseded Kenneth Lee 10 participants 58 messages Sep 3, 2018 – Sep 25, 2018

Subsystems (7)
Documentation KVM Crypto API IOMMU +3 more subsystems

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover A General Accelerator Framework, WarpDrive

From: Kenneth Lee <liguozhu-C8/M+/[email protected]>

WarpDrive is an accelerator framework to expose the hardware capabilities directly to the user space. It makes use of the exist vfio and vfio-mdev facilities. So the user application can send request and DMA to the hardware without interaction with the kernel. This removes the latency of syscall.

WarpDrive is the name for the whole framework. The component in kernel is called SDMDEV, Share Domain Mediated Device. Driver driver exposes its hardware resource by registering to SDMDEV as a VFIO-Mdev. So the user library of WarpDrive can access it via VFIO interface.

The patchset contains document for the detail. Please refer to it for more information.

This patchset is intended to be used with Jean Philippe Brucker's SVA patch [1], which enables not only IO side page fault, but also PASID support to IOMMU and VFIO.

With these features, WarpDrive can support non-pinned memory and multi-process in the same accelerator device. We tested it in our SoC integrated Accelerator (board ID: D06, Chip ID: HIP08). A reference work tree can be found here: [2].

But it is not mandatory. This patchset is tested in the latest mainline kernel without the SVA patches. So it supports only one process for each accelerator.

We have noticed the IOMMU aware mdev RFC announced recently [3].

The IOMMU aware mdev has similar idea but different intention comparing to WarpDrive. It intends to dedicate part of the hardware resource to a VM. And the design is supposed to be used with Scalable I/O Virtualization. While sdmdev is intended to share the hardware resource with a big amount of processes. It just requires the hardware supporting address translation per process (PCIE's PASID or ARM SMMU's substream ID).

But we don't see serious confliction on both design. We believe they can be normalized as one.

The patch 1 is document of the framework. The patch 2 and 3 add sdmdev support. The patch 4, 5 and 6 is drivers for Hislicon's ZIP Accelerator which is registered to both crypto and warpdrive(sdmdev) and can be used from kernel or user space at the same time. The patch 7 is a user space sample demonstrating how WarpDrive works.

Change History:
V2 changed from V1:
1. Change kernel framework name from SPIMDEV (Share Parent IOMMU Mdev) to SDMDEV (Share Domain Mdev).
2. Allocate Hardware Resource when a new mdev is created (While it is allocated when the mdev is openned)
3. Unmap pages from the shared domain when the sdmdev iommu group is detached. (This procedure is necessary, but missed in V1)
4. Update document accordingly.
5. Rebase to the latest kernel (4.19.0-rc1)

According the review comment on RFCv1, We did try to use dma-buf as back end of WarpDrive. It can work properly with the current solution [4], but it cannot make use of process's own memory address space directly. This is important to many acceleration scenario. So dma-buf will be taken as a backup alternative for noiommu scenario, it will be added in the future version.

Refernces:
[1] https://www.spinics.net/lists/kernel/msg2651481.html
[2] https://github.com/Kenneth-Lee/linux-kernel-warpdrive/tree/warpdrive-sva-v0.5
[3] https://lkml.org/lkml/2018/7/22/34
[4] https://github.com/Kenneth-Lee/linux-kernel-warpdrive/tree/warpdrive-v0.7-dmabuf

Best Regards
Kenneth Lee

Kenneth Lee (7):
vfio/sdmdev: Add documents for WarpDrive framework
iommu: Add share domain interface in iommu for sdmdev
vfio: add sdmdev support
crypto: add hisilicon Queue Manager driver
crypto: Add Hisilicon Zip driver
crypto: add sdmdev support to Hisilicon QM vfio/sdmdev: add user sample

 Documentation/00-INDEX                    |   2 +
 Documentation/warpdrive/warpdrive.rst     | 100 +++
 Documentation/warpdrive/wd-arch.svg       | 728 ++++++++++++++++
 drivers/crypto/Makefile                   |   2 +-
 drivers/crypto/hisilicon/Kconfig          |  25 +
 drivers/crypto/hisilicon/Makefile         |   2 +
 drivers/crypto/hisilicon/qm.c             | 979 ++++++++++++++++++++++
 drivers/crypto/hisilicon/qm.h             | 122 +++
 drivers/crypto/hisilicon/zip/Makefile     |   2 +
 drivers/crypto/hisilicon/zip/zip.h        |  57 ++
 drivers/crypto/hisilicon/zip/zip_crypto.c | 353 ++++++++
 drivers/crypto/hisilicon/zip/zip_crypto.h |   8 +
 drivers/crypto/hisilicon/zip/zip_main.c   | 195 +++++
 drivers/iommu/iommu.c                     |  29 +-
 drivers/vfio/Kconfig                      |   1 +
 drivers/vfio/Makefile                     |   1 +
 drivers/vfio/sdmdev/Kconfig               |  10 +
 drivers/vfio/sdmdev/Makefile              |   3 +
 drivers/vfio/sdmdev/vfio_sdmdev.c         | 363 ++++++++
 drivers/vfio/vfio_iommu_type1.c           | 151 +++-
 include/linux/iommu.h                     |  15 +
 include/linux/vfio_sdmdev.h               |  96 +++
 include/uapi/linux/vfio_sdmdev.h          |  29 +
 samples/warpdrive/AUTHORS                 |   2 +
 samples/warpdrive/ChangeLog               |   1 +
 samples/warpdrive/Makefile.am             |   9 +
 samples/warpdrive/NEWS                    |   1 +
 samples/warpdrive/README                  |  32 +
 samples/warpdrive/autogen.sh              |   3 +
 samples/warpdrive/cleanup.sh              |  13 +
 samples/warpdrive/configure.ac            |  52 ++
 samples/warpdrive/drv/hisi_qm_udrv.c      | 223 +++++
 samples/warpdrive/drv/hisi_qm_udrv.h      |  53 ++
 samples/warpdrive/test/Makefile.am        |   7 +
 samples/warpdrive/test/comp_hw.h          |  23 +
 samples/warpdrive/test/test_hisi_zip.c    | 206 +++++
 samples/warpdrive/wd.c                    | 309 +++++++
 samples/warpdrive/wd.h                    | 154 ++++
 samples/warpdrive/wd_adapter.c            |  74 ++
 samples/warpdrive/wd_adapter.h            |  43 +
 40 files changed, 4470 insertions(+), 8 deletions(-)
 create mode 100644 Documentation/warpdrive/warpdrive.rst
 create mode 100644 Documentation/warpdrive/wd-arch.svg
 create mode 100644 drivers/crypto/hisilicon/qm.c
 create mode 100644 drivers/crypto/hisilicon/qm.h
 create mode 100644 drivers/crypto/hisilicon/zip/Makefile
 create mode 100644 drivers/crypto/hisilicon/zip/zip.h
 create mode 100644 drivers/crypto/hisilicon/zip/zip_crypto.c
 create mode 100644 drivers/crypto/hisilicon/zip/zip_crypto.h
 create mode 100644 drivers/crypto/hisilicon/zip/zip_main.c
 create mode 100644 drivers/vfio/sdmdev/Kconfig
 create mode 100644 drivers/vfio/sdmdev/Makefile
 create mode 100644 drivers/vfio/sdmdev/vfio_sdmdev.c
 create mode 100644 include/linux/vfio_sdmdev.h
 create mode 100644 include/uapi/linux/vfio_sdmdev.h
 create mode 100644 samples/warpdrive/AUTHORS
 create mode 100644 samples/warpdrive/ChangeLog
 create mode 100644 samples/warpdrive/Makefile.am
 create mode 100644 samples/warpdrive/NEWS
 create mode 100644 samples/warpdrive/README
 create mode 100755 samples/warpdrive/autogen.sh
 create mode 100755 samples/warpdrive/cleanup.sh
 create mode 100644 samples/warpdrive/configure.ac
 create mode 100644 samples/warpdrive/drv/hisi_qm_udrv.c
 create mode 100644 samples/warpdrive/drv/hisi_qm_udrv.h
 create mode 100644 samples/warpdrive/test/Makefile.am
 create mode 100644 samples/warpdrive/test/comp_hw.h
 create mode 100644 samples/warpdrive/test/test_hisi_zip.c
 create mode 100644 samples/warpdrive/wd.c
 create mode 100644 samples/warpdrive/wd.h
 create mode 100644 samples/warpdrive/wd_adapter.c
 create mode 100644 samples/warpdrive/wd_adapter.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/7 vfio/sdmdev: Add documents for WarpDrive framework

From: Kenneth Lee <liguozhu-C8/M+/[email protected]>

WarpDrive is a common user space accelerator framework. Its main component in Kernel is called sdmdev, Share Domain Mediated Device. It exposes the hardware capabilities to the user space via vfio-mdev. So processes in user land can obtain a "queue" by open the device and direct access the hardware MMIO space or do DMA operation via VFIO interface.

WarpDrive is intended to be used with Jean Philippe Brucker's SVA patchset to support multi-process. But This is not a must. Without the SVA patches, WarpDrive can still work for one process for every hardware device.

This patch add detail documents for the framework.

---
 Documentation/00-INDEX                |   2 +
 Documentation/warpdrive/warpdrive.rst | 100 ++++
 Documentation/warpdrive/wd-arch.svg   | 728 ++++++++++++++++++++++++++
 3 files changed, 830 insertions(+)
 create mode 100644 Documentation/warpdrive/warpdrive.rst
 create mode 100644 Documentation/warpdrive/wd-arch.svg
Documentation/00-INDEX +2 -0
index 2754fe83f0d4..9959affab599 100644
@@ -410,6 +410,8 @@ vm/
 	- directory with info on the Linux vm code.
 w1/
 	- directory with documents regarding the 1-wire (w1) subsystem.
+warpdrive/
+	- directory with documents about WarpDrive accelerator framework.
 watchdog/
 	- how to auto-reboot Linux if it has "fallen and can't get up". ;-)
 wimax/
Documentation/warpdrive/warpdrive.rst new file +100 -0

100 lines not on this page · raw message

Documentation/warpdrive/wd-arch.svg new file +728 -0

728 lines not on this page · raw message

Signature · 1 line
2.17.1
reviewer
#2 lore raw

Re: [PATCH 1/7] vfio/sdmdev: Add documents for WarpDrive framework

Hi,

Quoted 30 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

WarpDrive is a common user space accelerator framework. Its main component in Kernel is called sdmdev, Share Domain Mediated Device. It exposes the hardware capabilities to the user space via vfio-mdev. So processes in user land can obtain a "queue" by open the device and direct access the hardware MMIO space or do DMA operation via VFIO interface.

WarpDrive is intended to be used with Jean Philippe Brucker's SVA patchset to support multi-process. But This is not a must. Without the SVA patches, WarpDrive can still work for one process for every hardware device.

This patch add detail documents for the framework.

---
 Documentation/00-INDEX                |   2 +
 Documentation/warpdrive/warpdrive.rst | 100 ++++
 Documentation/warpdrive/wd-arch.svg   | 728 ++++++++++++++++++++++++++
 3 files changed, 830 insertions(+)
 create mode 100644 Documentation/warpdrive/warpdrive.rst
 create mode 100644 Documentation/warpdrive/wd-arch.svg
Documentation/warpdrive/warpdrive.rst new file +0 -0
new file mode 100644
index 000000000000..6d2a5d1e08c4

Documentation/warpdrive/warpdrive.rst line 8

Quoted 8 lines
Documentation/warpdrive/warpdrive.rst new file +8 -0
@@ -0,0 +1,100 @@
+Introduction of WarpDrive
+=========================
+
+*WarpDrive* is a general accelerator framework for user space. It intends to
+provide interface for the user process to send request to hardware
+accelerator without heavy user-kernel interaction cost.
+
+The *WarpDrive* user library is supposed to provide a pipe-based API, such as:

Do you say "is supposed to" because it doesn't do that (yet)? Or you could just change that to say:

The WarpDrive user library provides a pipe-based API, such as:

Quoted 6 lines
Documentation/warpdrive/warpdrive.rst new file +6 -0
+        ::
+        int wd_request_queue(struct wd_queue *q);
+        void wd_release_queue(struct wd_queue *q);
+
+        int wd_send(struct wd_queue *q, void *req);
+        int wd_recv(struct wd_queue *q, void **req);

Documentation/warpdrive/warpdrive.rst line 24

Quoted 10 lines
Documentation/warpdrive/warpdrive.rst new file +10 -0
+        int wd_recv_sync(struct wd_queue *q, void **req);
+        int wd_flush(struct wd_queue *q);
+
+*wd_request_queue* creates the pipe connection, *queue*, between the
+application and the hardware. The application sends request and pulls the
+answer back by asynchronized wd_send/wd_recv, which directly interact with the
+hardware (by MMIO or share memory) without syscall.
+
+*WarpDrive* maintains a unified application address space among all involved
+accelerators.  With the following APIs: ::

Seems like an extra '.' there. How about:

accelerators with the following APIs: ::

Quoted 25 lines
Documentation/warpdrive/warpdrive.rst new file +25 -0
+
+        int wd_mem_share(struct wd_queue *q, const void *addr,
+                         size_t size, int flags);
+        void wd_mem_unshare(struct wd_queue *q, const void *addr, size_t size);
+
+The referred process space shared by these APIs can be directly referred by the
+hardware. The process can also dedicate its whole process space with flags,
+*WD_SHARE_ALL* (not in this patch yet).
+
+The name *WarpDrive* is simply a cool and general name meaning the framework
+makes the application faster. As it will be explained in this text later, the
+facility in kernel is called *SDMDEV*, namely "Share Domain Mediated Device".
+
+
+How does it work
+================
+
+*WarpDrive* is built upon *VFIO-MDEV*. The queue is wrapped as *mdev* in VFIO.
+So memory sharing can be done via standard VFIO standard DMA interface.
+
+The architecture is illustrated as follow figure:
+
+.. image:: wd-arch.svg
+        :alt: WarpDrive Architecture
+

Documentation/warpdrive/warpdrive.rst line 59

Quoted 10 lines
Documentation/warpdrive/warpdrive.rst new file +10 -0
+Accelerator driver shares its capability via *SDMDEV* API: ::
+
+        vfio_sdmdev_register(struct vfio_sdmdev *sdmdev);
+        vfio_sdmdev_unregister(struct vfio_sdmdev *sdmdev);
+        vfio_sdmdev_wake_up(struct spimdev_queue *q);
+
+*vfio_sdmdev_register* is a helper function to register the hardware to the
+*VFIO_MDEV* framework. The queue creation is done by *mdev* creation interface.
+
+*WarpDrive* User library mmap the mdev to access its mmio space and shared

s/mmio/MMIO/

Quoted 31 lines
Documentation/warpdrive/warpdrive.rst new file +31 -0
+memory. Request can be sent to, or receive from, hardware in this mmap-ed
+space until the queue is full or empty.
+
+The user library can wait on the queue by ioctl(VFIO_SDMDEV_CMD_WAIT) the mdev
+if the queue is full or empty. If the queue status is changed, the hardware
+driver use *vfio_sdmdev_wake_up* to wake up the waiting process.
+
+
+Multiple processes support
+==========================
+
+In the latest mainline kernel (4.18) when this document is written,
+multi-process is not supported in VFIO yet.
+
+Jean Philippe Brucker has a patchset to enable it[1]_. We have tested it
+with our hardware (which is known as *D06*). It works well. *WarpDrive* rely
+on them to support multiple processes. If it is not enabled, *WarpDrive* can
+still work, but it support only one mdev for a process, which will share the
+same io map table with kernel. (But it is not going to be a security problem,
+since the user application cannot access the kernel address space)
+
+When multiprocess is support, mdev can be created based on how many
+hardware resource (queue) is available. Because the VFIO framework accepts only
+one open from one mdev iommu_group. Mdev become the smallest unit for process
+to use queue. And the mdev will not be released if the user process exist. So
+it will need a resource agent to manage the mdev allocation for the user
+process. This is not in this document's range.
+
+
+Legacy Mode Support
+===================

Documentation/warpdrive/warpdrive.rst line 100

Quoted 10 lines
Documentation/warpdrive/warpdrive.rst new file +10 -0
+For the hardware on which IOMMU is not support, WarpDrive can run on *NOIOMMU*
+mode. That require some update to the mdev driver, which is not included in
+this version yet.
+
+
+References
+==========
+.. [1] https://patchwork.kernel.org/patch/10394851/
+
+.. vim: tw=78

thanks,

Signature · 1 line
~Randy
#3 lore raw

Re: [PATCH 1/7] vfio/sdmdev: Add documents for WarpDrive framework

Quoted 65 lines · Randy Dunlap

Date: Thu, 6 Sep 2018 11:36:36 -0700
From: Randy Dunlap <[email protected]>
To: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Alex Williamson <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>
CC: [email protected]
Subject: Re: [PATCH 1/7] vfio/sdmdev: Add documents for WarpDrive framework
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101
Thunderbird/52.9.1
Message-ID: <[email protected]>

Hi,

Quoted 30 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

WarpDrive is a common user space accelerator framework. Its main component in Kernel is called sdmdev, Share Domain Mediated Device. It exposes the hardware capabilities to the user space via vfio-mdev. So processes in user land can obtain a "queue" by open the device and direct access the hardware MMIO space or do DMA operation via VFIO interface.

WarpDrive is intended to be used with Jean Philippe Brucker's SVA patchset to support multi-process. But This is not a must. Without the SVA patches, WarpDrive can still work for one process for every hardware device.

This patch add detail documents for the framework.

---
 Documentation/00-INDEX                |   2 +
 Documentation/warpdrive/warpdrive.rst | 100 ++++
 Documentation/warpdrive/wd-arch.svg   | 728 ++++++++++++++++++++++++++
 3 files changed, 830 insertions(+)
 create mode 100644 Documentation/warpdrive/warpdrive.rst
 create mode 100644 Documentation/warpdrive/wd-arch.svg
Documentation/warpdrive/warpdrive.rst new file +0 -0
new file mode 100644
index 000000000000..6d2a5d1e08c4

Documentation/warpdrive/warpdrive.rst line 8

Quoted 8 lines
Documentation/warpdrive/warpdrive.rst new file +8 -0
@@ -0,0 +1,100 @@
+Introduction of WarpDrive
+=========================
+
+*WarpDrive* is a general accelerator framework for user space. It intends to
+provide interface for the user process to send request to hardware
+accelerator without heavy user-kernel interaction cost.
+
+The *WarpDrive* user library is supposed to provide a pipe-based API, such as:

Do you say "is supposed to" because it doesn't do that (yet)? Or you could just change that to say:

Quoted 2 lines

The WarpDrive user library provides a pipe-based API, such as:

Actually, I tried to say it can be defined like this. But people can choose other implementation with the same kernel API.

I will say it explicitly in the future version. Thank you.

Quoted 20 lines
Quoted 7 lines
Documentation/warpdrive/warpdrive.rst new file +6 -0
+        ::
+        int wd_request_queue(struct wd_queue *q);
+        void wd_release_queue(struct wd_queue *q);
+
+        int wd_send(struct wd_queue *q, void *req);
+        int wd_recv(struct wd_queue *q, void **req);

Documentation/warpdrive/warpdrive.rst line 24

Quoted 10 lines
Documentation/warpdrive/warpdrive.rst new file +10 -0
+        int wd_recv_sync(struct wd_queue *q, void **req);
+        int wd_flush(struct wd_queue *q);
+
+*wd_request_queue* creates the pipe connection, *queue*, between the
+application and the hardware. The application sends request and pulls the
+answer back by asynchronized wd_send/wd_recv, which directly interact with the
+hardware (by MMIO or share memory) without syscall.
+
+*WarpDrive* maintains a unified application address space among all involved
+accelerators.  With the following APIs: ::

Seems like an extra '.' there. How about:

Quoted 2 lines

accelerators with the following APIs: ::

Err, the "with..." clause belong to the following "The referred process space...".

Quoted 83 lines
Quoted 25 lines
Documentation/warpdrive/warpdrive.rst new file +25 -0
+
+        int wd_mem_share(struct wd_queue *q, const void *addr,
+                         size_t size, int flags);
+        void wd_mem_unshare(struct wd_queue *q, const void *addr, size_t size);
+
+The referred process space shared by these APIs can be directly referred by the
+hardware. The process can also dedicate its whole process space with flags,
+*WD_SHARE_ALL* (not in this patch yet).
+
+The name *WarpDrive* is simply a cool and general name meaning the framework
+makes the application faster. As it will be explained in this text later, the
+facility in kernel is called *SDMDEV*, namely "Share Domain Mediated Device".
+
+
+How does it work
+================
+
+*WarpDrive* is built upon *VFIO-MDEV*. The queue is wrapped as *mdev* in VFIO.
+So memory sharing can be done via standard VFIO standard DMA interface.
+
+The architecture is illustrated as follow figure:
+
+.. image:: wd-arch.svg
+        :alt: WarpDrive Architecture
+

Documentation/warpdrive/warpdrive.rst line 59

Quoted 10 lines
Documentation/warpdrive/warpdrive.rst new file +10 -0
+Accelerator driver shares its capability via *SDMDEV* API: ::
+
+        vfio_sdmdev_register(struct vfio_sdmdev *sdmdev);
+        vfio_sdmdev_unregister(struct vfio_sdmdev *sdmdev);
+        vfio_sdmdev_wake_up(struct spimdev_queue *q);
+
+*vfio_sdmdev_register* is a helper function to register the hardware to the
+*VFIO_MDEV* framework. The queue creation is done by *mdev* creation interface.
+
+*WarpDrive* User library mmap the mdev to access its mmio space and shared

s/mmio/MMIO/

Quoted 31 lines
Documentation/warpdrive/warpdrive.rst new file +31 -0
+memory. Request can be sent to, or receive from, hardware in this mmap-ed
+space until the queue is full or empty.
+
+The user library can wait on the queue by ioctl(VFIO_SDMDEV_CMD_WAIT) the mdev
+if the queue is full or empty. If the queue status is changed, the hardware
+driver use *vfio_sdmdev_wake_up* to wake up the waiting process.
+
+
+Multiple processes support
+==========================
+
+In the latest mainline kernel (4.18) when this document is written,
+multi-process is not supported in VFIO yet.
+
+Jean Philippe Brucker has a patchset to enable it[1]_. We have tested it
+with our hardware (which is known as *D06*). It works well. *WarpDrive* rely
+on them to support multiple processes. If it is not enabled, *WarpDrive* can
+still work, but it support only one mdev for a process, which will share the
+same io map table with kernel. (But it is not going to be a security problem,
+since the user application cannot access the kernel address space)
+
+When multiprocess is support, mdev can be created based on how many
+hardware resource (queue) is available. Because the VFIO framework accepts only
+one open from one mdev iommu_group. Mdev become the smallest unit for process
+to use queue. And the mdev will not be released if the user process exist. So
+it will need a resource agent to manage the mdev allocation for the user
+process. This is not in this document's range.
+
+
+Legacy Mode Support
+===================

Documentation/warpdrive/warpdrive.rst line 100

Quoted 10 lines
Documentation/warpdrive/warpdrive.rst new file +10 -0
+For the hardware on which IOMMU is not support, WarpDrive can run on *NOIOMMU*
+mode. That require some update to the mdev driver, which is not included in
+this version yet.
+
+
+References
+==========
+.. [1] https://patchwork.kernel.org/patch/10394851/
+
+.. vim: tw=78

thanks,

Signature · 1 line
~Randy
Signature · 14 lines
			-Kenneth(Hisilicon)

================================================================================
本邮件及其附件含有华为公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁
止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中
的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from HUAWEI,
which is intended only for the person or entity whose address is listed above.
Any use of the 
information contained herein in any way (including, but not limited to, total or
partial disclosure, reproduction, or dissemination) by persons other than the
intended 
recipient(s) is prohibited. If you receive this e-mail in error, please notify
the sender by phone or email immediately and delete it!
#4 lore raw

PATCH v2 2/7 iommu: Add share domain interface in iommu for sdmdev

In reply to Kenneth Lee

From: Kenneth Lee <[email protected]>

This patch add sharing interface for a iommu_group. The new interface:

iommu_group_share_domain()
iommu_group_unshare_domain()

can be used by some virtual iommu_group (such as iommu_group of sdmdev) to share their parent's iommu_group.

When the domain of a group is shared, it cannot be changed before being unshared. By this way, all domain users can assume the shared IOMMU have the same configuration. In the future, notification can be added if update is required.

---
 drivers/iommu/iommu.c | 29 ++++++++++++++++++++++++++++-
 include/linux/iommu.h | 15 +++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)
drivers/iommu/iommu.c +28 -1
index 8c15c5980299..8e567e1037dd 100644
@@ -58,6 +58,9 @@ struct iommu_group {
 	int id;
 	struct iommu_domain *default_domain;
 	struct iommu_domain *domain;
+	atomic_t domain_shared_ref; /* Number of user of current domain.
+				     * The domain cannot be modified if ref > 0
+				     */
 };
 
 struct group_device {
@@ -385,6 +388,7 @@ struct iommu_group *iommu_group_alloc(void)
 		return ERR_PTR(ret);
 	}
 	group->id = ret;
+	atomic_set(&group->domain_shared_ref, 0);
 
 	ret = kobject_init_and_add(&group->kobj, &iommu_group_ktype,
 				   NULL, "%d", group->id);
@@ -518,6 +522,26 @@ int iommu_group_set_name(struct iommu_group *group, const char *name)
 }
 EXPORT_SYMBOL_GPL(iommu_group_set_name);
 
+struct iommu_domain *iommu_group_share_domain(struct iommu_group *group)
+{
+	/* the domain can be shared only when the default domain is used */
+	/* todo: more shareable check */
+	if (group->domain != group->default_domain)
+		return ERR_PTR(-EINVAL);
+
+	atomic_inc(&group->domain_shared_ref);
+	return group->domain;
+}
+EXPORT_SYMBOL_GPL(iommu_group_share_domain);
+
+struct iommu_domain *iommu_group_unshare_domain(struct iommu_group *group)
+{
+	atomic_dec(&group->domain_shared_ref);
+	WARN_ON(atomic_read(&group->domain_shared_ref) < 0);
+	return group->domain;
+}
+EXPORT_SYMBOL_GPL(iommu_group_unshare_domain);
+
 static int iommu_group_create_direct_mappings(struct iommu_group *group,
 					      struct device *dev)
 {
@@ -1437,7 +1461,8 @@ static int __iommu_attach_group(struct iommu_domain *domain,
 {
 	int ret;
 
-	if (group->default_domain && group->domain != group->default_domain)
+	if ((group->default_domain && group->domain != group->default_domain) ||
+	     atomic_read(&group->domain_shared_ref) > 0)
 		return -EBUSY;
 
 	ret = __iommu_group_for_each_dev(group, domain,
@@ -1474,6 +1499,8 @@ static void __iommu_detach_group(struct iommu_domain *domain,
 {
 	int ret;
 
+	WARN_ON(atomic_read(&group->domain_shared_ref) > 0);
+
 	if (!group->default_domain) {
 		__iommu_group_for_each_dev(group, domain,
 					   iommu_group_do_detach_device);
include/linux/iommu.h +15 -0
index 87994c265bf5..013ac400b643 100644
@@ -344,6 +344,9 @@ extern int iommu_domain_get_attr(struct iommu_domain *domain, enum iommu_attr,
 				 void *data);
 extern int iommu_domain_set_attr(struct iommu_domain *domain, enum iommu_attr,
 				 void *data);
+extern struct iommu_domain *iommu_group_share_domain(struct iommu_group *group);
+extern struct iommu_domain *iommu_group_unshare_domain(
+		struct iommu_group *group);
 
 /* Window handling function prototypes */
 extern int iommu_domain_window_enable(struct iommu_domain *domain, u32 wnd_nr,
@@ -616,6 +619,18 @@ static inline int iommu_domain_set_attr(struct iommu_domain *domain,
 	return -EINVAL;
 }
 
+static inline struct iommu_domain *iommu_group_share_domain(
+		struct iommu_group *group)
+{
+	return NULL;
+}
+
+static inline struct iommu_domain *iommu_group_unshare_domain(
+		struct iommu_group *group)
+{
+	return NULL;
+}
+
 static inline int  iommu_device_register(struct iommu_device *iommu)
 {
 	return -ENODEV;
Signature · 1 line
2.17.1
#5 lore raw

PATCH v2 3/7 vfio: add sdmdev support

In reply to Kenneth Lee

From: Kenneth Lee <[email protected]>

SDMDEV is "Share Domain Mdev". It is a vfio-mdev. But differ from the general vfio-mdev, it shares its parent's IOMMU. If Multi-PASID support is enabled in the IOMMU (not yet in the current kernel HEAD), multiple process can share the IOMMU by different PASID. If it is not support, only one process can share the IOMMU with the kernel driver.

Currently only the vfio type-1 driver is updated to make it to be aware of.

---
 drivers/vfio/Kconfig              |   1 +
 drivers/vfio/Makefile             |   1 +
 drivers/vfio/sdmdev/Kconfig       |  10 +
 drivers/vfio/sdmdev/Makefile      |   3 +
 drivers/vfio/sdmdev/vfio_sdmdev.c | 363 ++++++++++++++++++++++++++++++
 drivers/vfio/vfio_iommu_type1.c   | 151 ++++++++++++-
 include/linux/vfio_sdmdev.h       |  96 ++++++++
 include/uapi/linux/vfio_sdmdev.h  |  29 +++
 8 files changed, 648 insertions(+), 6 deletions(-)
 create mode 100644 drivers/vfio/sdmdev/Kconfig
 create mode 100644 drivers/vfio/sdmdev/Makefile
 create mode 100644 drivers/vfio/sdmdev/vfio_sdmdev.c
 create mode 100644 include/linux/vfio_sdmdev.h
 create mode 100644 include/uapi/linux/vfio_sdmdev.h
drivers/vfio/Kconfig +1 -0
index c84333eb5eb5..5af7d1db505e 100644
@@ -47,4 +47,5 @@ menuconfig VFIO_NOIOMMU
 source "drivers/vfio/pci/Kconfig"
 source "drivers/vfio/platform/Kconfig"
 source "drivers/vfio/mdev/Kconfig"
+source "drivers/vfio/sdmdev/Kconfig"
 source "virt/lib/Kconfig"
drivers/vfio/Makefile +1 -0
index de67c4725cce..678592360a7a 100644
@@ -9,3 +9,4 @@ obj-$(CONFIG_VFIO_SPAPR_EEH) += vfio_spapr_eeh.o
 obj-$(CONFIG_VFIO_PCI) += pci/
 obj-$(CONFIG_VFIO_PLATFORM) += platform/
 obj-$(CONFIG_VFIO_MDEV) += mdev/
+obj-$(CONFIG_VFIO_SDMDEV) += sdmdev/
drivers/vfio/sdmdev/Kconfig new file +10 -0
new file mode 100644
index 000000000000..51474272870d
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0
+config VFIO_SDMDEV
+	tristate "Support for Share Domain MDEV"
+	depends on VFIO_MDEV_DEVICE
+	help
+	  Support for VFIO Share Domain MDEV, which enables the kernel to
+	  support light weight hardware accelerator framework, WarpDrive.
+
+	  To compile this as a module, choose M here: the module will be called
+	  sdmdev.
drivers/vfio/sdmdev/Makefile new file +3 -0
new file mode 100644
index 000000000000..ccaaa03f3184
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+sdmdev-y := sdmdev.o
+obj-$(CONFIG_VFIO_SDMDEV) += vfio_sdmdev.o
drivers/vfio/sdmdev/vfio_sdmdev.c new file +363 -0

363 lines not on this page · raw message

drivers/vfio/vfio_iommu_type1.c +145 -6

222 lines not on this page · raw message

include/linux/vfio_sdmdev.h new file +96 -0

96 lines not on this page · raw message

include/uapi/linux/vfio_sdmdev.h new file +29 -0
new file mode 100644
index 000000000000..79fa33fbc8c0
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+#ifndef _UAPIVFIO_SDMDEV_H
+#define _UAPIVFIO_SDMDEV_H
+
+#include <linux/ioctl.h>
+
+#define VFIO_SDMDEV_CLASS_NAME		"sdmdev"
+
+/* Device ATTRs in parent dev SYSFS DIR */
+#define VFIO_SDMDEV_PDEV_ATTRS_GRP_NAME	"params"
+
+/* Parent device attributes */
+#define SDMDEV_IOMMU_TYPE	"iommu_type"
+#define SDMDEV_DMA_FLAG		"dma_flag"
+
+/* Maximum length of algorithm name string */
+#define VFIO_SDMDEV_ALG_NAME_SIZE		64
+
+/* the bits used in SDMDEV_DMA_FLAG attributes */
+#define VFIO_SDMDEV_DMA_INVALID			0
+#define	VFIO_SDMDEV_DMA_SINGLE_PROC_MAP		1
+#define	VFIO_SDMDEV_DMA_MULTI_PROC_MAP		2
+#define	VFIO_SDMDEV_DMA_SVM			4
+#define	VFIO_SDMDEV_DMA_SVM_NO_FAULT		8
+#define	VFIO_SDMDEV_DMA_PHY			16
+
+#define VFIO_SDMDEV_CMD_WAIT		_IO('W', 1)
+#define VFIO_SDMDEV_CMD_BIND_PASID	_IO('W', 2)
+#endif
Signature · 1 line
2.17.1
reviewer
#6 lore raw

Re: [PATCH 3/7] vfio: add sdmdev support

drivers/vfio/sdmdev/Kconfig line 7

Quoted 13 lines · Kenneth Lee
drivers/vfio/sdmdev/Kconfig new file +7 -0
new file mode 100644
index 000000000000..51474272870d
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0
+config VFIO_SDMDEV
+	tristate "Support for Share Domain MDEV"
+	depends on VFIO_MDEV_DEVICE
+	help
+	  Support for VFIO Share Domain MDEV, which enables the kernel to
+	  support light weight hardware accelerator framework, WarpDrive.

lightweight

drivers/vfio/sdmdev/Kconfig line 10

Quoted 3 lines
drivers/vfio/sdmdev/Kconfig new file +3 -0
+
+	  To compile this as a module, choose M here: the module will be called
+	  sdmdev.

thanks,

Signature · 1 line
~Randy
#7 lore raw

Re: [PATCH 3/7] vfio: add sdmdev support

Quoted 21 lines · Randy Dunlap

Date: Sun, 2 Sep 2018 19:11:12 -0700
From: Randy Dunlap <[email protected]>
To: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Alex Williamson <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>
CC: [email protected]
Subject: Re: [PATCH 3/7] vfio: add sdmdev support
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101
Thunderbird/52.9.1
Message-ID: <[email protected]>

On 09/02/2018 05:52 PM, Kenneth Lee wrote:

Quoted 16 lines

drivers/vfio/sdmdev/Kconfig line 7

Quoted 13 lines
drivers/vfio/sdmdev/Kconfig new file +7 -0
new file mode 100644
index 000000000000..51474272870d
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: GPL-2.0
+config VFIO_SDMDEV
+	tristate "Support for Share Domain MDEV"
+	depends on VFIO_MDEV_DEVICE
+	help
+	  Support for VFIO Share Domain MDEV, which enables the kernel to
+	  support light weight hardware accelerator framework, WarpDrive.

lightweight

Thank you, will fix it.

Quoted 8 lines

drivers/vfio/sdmdev/Kconfig line 10

Quoted 3 lines
drivers/vfio/sdmdev/Kconfig new file +3 -0
+
+	  To compile this as a module, choose M here: the module will be called
+	  sdmdev.

thanks,

Signature · 1 line
~Randy
Signature · 1 line
			-Kenneth(Hisilicon)
#8 lore raw

Re: [PATCH 3/7] vfio: add sdmdev support

In reply to Kenneth Lee

Hi,

Quoted 8 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

SDMDEV is "Share Domain Mdev". It is a vfio-mdev. But differ from the general vfio-mdev, it shares its parent's IOMMU. If Multi-PASID support is enabled in the IOMMU (not yet in the current kernel HEAD), multiple process can share the IOMMU by different PASID. If it is not support, only one process can share the IOMMU with the kernel driver.

If only for share domain purpose, I don't think it's necessary to create a new device type.

Quoted 16 lines

Currently only the vfio type-1 driver is updated to make it to be aware of.

---
  drivers/vfio/Kconfig              |   1 +
  drivers/vfio/Makefile             |   1 +
  drivers/vfio/sdmdev/Kconfig       |  10 +
  drivers/vfio/sdmdev/Makefile      |   3 +
  drivers/vfio/sdmdev/vfio_sdmdev.c | 363 ++++++++++++++++++++++++++++++
  drivers/vfio/vfio_iommu_type1.c   | 151 ++++++++++++-
  include/linux/vfio_sdmdev.h       |  96 ++++++++
  include/uapi/linux/vfio_sdmdev.h  |  29 +++
  8 files changed, 648 insertions(+), 6 deletions(-)
Quoted 6 lines

create mode 100644 drivers/vfio/sdmdev/Kconfig create mode 100644 drivers/vfio/sdmdev/Makefile create mode 100644 drivers/vfio/sdmdev/vfio_sdmdev.c create mode 100644 include/linux/vfio_sdmdev.h create mode 100644 include/uapi/linux/vfio_sdmdev.h

[--cut for short --]

Quoted 109 lines
drivers/vfio/vfio_iommu_type1.c +87 -0
index d9fd3188615d..ba73231d8692 100644
@@ -41,6 +41,7 @@
  #include <linux/notifier.h>
  #include <linux/dma-iommu.h>
  #include <linux/irqdomain.h>
+#include <linux/vfio_sdmdev.h>
 
  #define DRIVER_VERSION  "0.2"
  #define DRIVER_AUTHOR   "Alex Williamson <[email protected]>"
@@ -89,6 +90,8 @@ struct vfio_dma {
  };
 
  struct vfio_group {
+	/* iommu_group of mdev's parent device */
+	struct iommu_group	*parent_group;
  	struct iommu_group	*iommu_group;
  	struct list_head	next;
  };
@@ -1327,6 +1330,109 @@ static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base)
  	return ret;
  }
 
+/* return 0 if the device is not sdmdev.
+ * return 1 if the device is sdmdev, the data will be updated with parent
+ *	device's group.
+ * return -errno if other error.
+ */
+static int vfio_sdmdev_type(struct device *dev, void *data)
+{
+	struct iommu_group **group = data;
+	struct iommu_group *pgroup;
+	int (*_is_sdmdev)(struct device *dev);
+	struct device *pdev;
+	int ret = 1;
+
+	/* vfio_sdmdev module is not configurated */
+	_is_sdmdev = symbol_get(vfio_sdmdev_is_sdmdev);
+	if (!_is_sdmdev)
+		return 0;
+
+	/* check if it belongs to vfio_sdmdev device */
+	if (!_is_sdmdev(dev)) {
+		ret = 0;
+		goto out;
+	}
+
+	pdev = dev->parent;
+	pgroup = iommu_group_get(pdev);
+	if (!pgroup) {
+		ret = -ENODEV;
+		goto out;
+	}
+
+	if (group) {
+		/* check if all parent devices is the same */
+		if (*group && *group != pgroup)
+			ret = -ENODEV;
+		else
+			*group = pgroup;
+	}
+
+	iommu_group_put(pgroup);
+
+out:
+	symbol_put(vfio_sdmdev_is_sdmdev);
+
+	return ret;
+}
+
+/* return 0 or -errno */
+static int vfio_sdmdev_bus(struct device *dev, void *data)
+{
+	struct bus_type **bus = data;
+
+	if (!dev->bus)
+		return -ENODEV;
+
+	/* ensure all devices has the same bus_type */
+	if (*bus && *bus != dev->bus)
+		return -EINVAL;
+
+	*bus = dev->bus;
+	return 0;
+}
+
+/* return 0 means it is not sd group, 1 means it is, or -EXXX for error */
+static int vfio_iommu_type1_attach_sdgroup(struct vfio_domain *domain,
+					    struct vfio_group *group,
+					    struct iommu_group *iommu_group)
+{
+	int ret;
+	struct bus_type *pbus = NULL;
+	struct iommu_group *pgroup = NULL;
+
+	ret = iommu_group_for_each_dev(iommu_group, &pgroup,
+				       vfio_sdmdev_type);
+	if (ret < 0)
+		goto out;
+	else if (ret > 0) {
+		domain->domain = iommu_group_share_domain(pgroup);
+		if (IS_ERR(domain->domain))
+			goto out;
+		ret = iommu_group_for_each_dev(pgroup, &pbus,
+				       vfio_sdmdev_bus);
+		if (ret < 0)
+			goto err_with_share_domain;

drivers/vfio/vfio_iommu_type1.c static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base) · line 1426

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+
+		if (pbus && iommu_capable(pbus, IOMMU_CAP_CACHE_COHERENCY))
+			domain->prot |= IOMMU_CACHE;
+
+		group->parent_group = pgroup;
+		INIT_LIST_HEAD(&domain->group_list);
+		list_add(&group->next, &domain->group_list);
+
+		return 1;
+	}

This doesn't match the function name. It only gets the domain from the parent device. It hasn't been really attached.

Quoted 24 lines
drivers/vfio/vfio_iommu_type1.c +11 -2
+
+	return 0;
+
+err_with_share_domain:
+	iommu_group_unshare_domain(pgroup);
+out:
+	return ret;
+}
+
  static int vfio_iommu_type1_attach_group(void *iommu_data,
  					 struct iommu_group *iommu_group)
  {
@@ -1335,8 +1441,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
  	struct vfio_domain *domain, *d;
  	struct bus_type *bus = NULL, *mdev_bus;
  	int ret;
-	bool resv_msi, msi_remap;
-	phys_addr_t resv_msi_base;
+	bool resv_msi = false, msi_remap;
+	phys_addr_t resv_msi_base = 0;
 
  	mutex_lock(&iommu->lock);
 

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1488

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +7 -0
@@ -1373,6 +1479,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
  	if (mdev_bus) {
  		if ((bus == mdev_bus) && !iommu_present(bus)) {
  			symbol_put(mdev_bus_type);
+
+			ret = vfio_iommu_type1_attach_sdgroup(domain, group,
+					iommu_group);
+			if (ret < 0)
+				goto out_free;
+			else if (ret > 0)
+				goto replay_check;

Here you get the domain from the parent device and save it for later use. The actual attaching is ignored.

I don't think this follows the philosophy of this function. It actually make all devices in the group with the same bus type to share a single domain.

Further more, the parent domain might be a domain of type IOMMU_DOMAIN_DMA. That will not be able to use as an IOMMU_DOMAIN_UNMANAGED domain for iommu APIs.

Best regards,
Lu Baolu

#9 lore raw

Re: [PATCH 3/7] vfio: add sdmdev support

Quoted 32 lines · Lu Baolu

Date: Mon, 3 Sep 2018 10:55:57 +0800
From: Lu Baolu <[email protected]>
To: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Alex Williamson <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Sanjay Kumar <[email protected]>
CC: [email protected], [email protected]
Subject: Re: [PATCH 3/7] vfio: add sdmdev support
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101
Thunderbird/52.9.1
Message-ID: <[email protected]>

Hi,

Quoted 8 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

SDMDEV is "Share Domain Mdev". It is a vfio-mdev. But differ from the general vfio-mdev, it shares its parent's IOMMU. If Multi-PASID support is enabled in the IOMMU (not yet in the current kernel HEAD), multiple process can share the IOMMU by different PASID. If it is not support, only one process can share the IOMMU with the kernel driver.

Quoted 3 lines

If only for share domain purpose, I don't think it's necessary to create a new device type.

Yes, if ONLY for share domain purpose. But we need also to share the interrupt.

Quoted 181 lines
Quoted 6 lines

Currently only the vfio type-1 driver is updated to make it to be aware of.

Quoted 16 lines
---
 drivers/vfio/Kconfig              |   1 +
 drivers/vfio/Makefile             |   1 +
 drivers/vfio/sdmdev/Kconfig       |  10 +
 drivers/vfio/sdmdev/Makefile      |   3 +
 drivers/vfio/sdmdev/vfio_sdmdev.c | 363 ++++++++++++++++++++++++++++++
 drivers/vfio/vfio_iommu_type1.c   | 151 ++++++++++++-
 include/linux/vfio_sdmdev.h       |  96 ++++++++
 include/uapi/linux/vfio_sdmdev.h  |  29 +++
 8 files changed, 648 insertions(+), 6 deletions(-)
 create mode 100644 drivers/vfio/sdmdev/Kconfig
 create mode 100644 drivers/vfio/sdmdev/Makefile
 create mode 100644 drivers/vfio/sdmdev/vfio_sdmdev.c
 create mode 100644 include/linux/vfio_sdmdev.h
 create mode 100644 include/uapi/linux/vfio_sdmdev.h

[--cut for short --]

Quoted 106 lines
drivers/vfio/vfio_iommu_type1.c +87 -0
index d9fd3188615d..ba73231d8692 100644
@@ -41,6 +41,7 @@
 #include <linux/notifier.h>
 #include <linux/dma-iommu.h>
 #include <linux/irqdomain.h>
+#include <linux/vfio_sdmdev.h>
 #define DRIVER_VERSION  "0.2"
 #define DRIVER_AUTHOR   "Alex Williamson <[email protected]>"
@@ -89,6 +90,8 @@ struct vfio_dma {
 };
 struct vfio_group {
+	/* iommu_group of mdev's parent device */
+	struct iommu_group	*parent_group;
 	struct iommu_group	*iommu_group;
 	struct list_head	next;
 };
@@ -1327,6 +1330,109 @@ static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base)
 	return ret;
 }
+/* return 0 if the device is not sdmdev.
+ * return 1 if the device is sdmdev, the data will be updated with parent
+ *	device's group.
+ * return -errno if other error.
+ */
+static int vfio_sdmdev_type(struct device *dev, void *data)
+{
+	struct iommu_group **group = data;
+	struct iommu_group *pgroup;
+	int (*_is_sdmdev)(struct device *dev);
+	struct device *pdev;
+	int ret = 1;
+
+	/* vfio_sdmdev module is not configurated */
+	_is_sdmdev = symbol_get(vfio_sdmdev_is_sdmdev);
+	if (!_is_sdmdev)
+		return 0;
+
+	/* check if it belongs to vfio_sdmdev device */
+	if (!_is_sdmdev(dev)) {
+		ret = 0;
+		goto out;
+	}
+
+	pdev = dev->parent;
+	pgroup = iommu_group_get(pdev);
+	if (!pgroup) {
+		ret = -ENODEV;
+		goto out;
+	}
+
+	if (group) {
+		/* check if all parent devices is the same */
+		if (*group && *group != pgroup)
+			ret = -ENODEV;
+		else
+			*group = pgroup;
+	}
+
+	iommu_group_put(pgroup);
+
+out:
+	symbol_put(vfio_sdmdev_is_sdmdev);
+
+	return ret;
+}
+
+/* return 0 or -errno */
+static int vfio_sdmdev_bus(struct device *dev, void *data)
+{
+	struct bus_type **bus = data;
+
+	if (!dev->bus)
+		return -ENODEV;
+
+	/* ensure all devices has the same bus_type */
+	if (*bus && *bus != dev->bus)
+		return -EINVAL;
+
+	*bus = dev->bus;
+	return 0;
+}
+
+/* return 0 means it is not sd group, 1 means it is, or -EXXX for error */
+static int vfio_iommu_type1_attach_sdgroup(struct vfio_domain *domain,
+					    struct vfio_group *group,
+					    struct iommu_group *iommu_group)
+{
+	int ret;
+	struct bus_type *pbus = NULL;
+	struct iommu_group *pgroup = NULL;
+
+	ret = iommu_group_for_each_dev(iommu_group, &pgroup,
+				       vfio_sdmdev_type);
+	if (ret < 0)
+		goto out;
+	else if (ret > 0) {
+		domain->domain = iommu_group_share_domain(pgroup);
+		if (IS_ERR(domain->domain))
+			goto out;
+		ret = iommu_group_for_each_dev(pgroup, &pbus,
+				       vfio_sdmdev_bus);
+		if (ret < 0)
+			goto err_with_share_domain;

drivers/vfio/vfio_iommu_type1.c static bool vfio_iommu_has_sw_msi(struct iommu_group *group, phys_addr_t *base) · line 1425

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +10 -0
+
+		if (pbus && iommu_capable(pbus, IOMMU_CAP_CACHE_COHERENCY))
+			domain->prot |= IOMMU_CACHE;
+
+		group->parent_group = pgroup;
+		INIT_LIST_HEAD(&domain->group_list);
+		list_add(&group->next, &domain->group_list);
+
+		return 1;
+	}

This doesn't match the function name. It only gets the domain from the parent device. It hasn't been really attached.

Quoted 22 lines
drivers/vfio/vfio_iommu_type1.c +11 -2
+
+	return 0;
+
+err_with_share_domain:
+	iommu_group_unshare_domain(pgroup);
+out:
+	return ret;
+}
+
 static int vfio_iommu_type1_attach_group(void *iommu_data,
 					 struct iommu_group *iommu_group)
 {
@@ -1335,8 +1441,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	struct vfio_domain *domain, *d;
 	struct bus_type *bus = NULL, *mdev_bus;
 	int ret;
-	bool resv_msi, msi_remap;
-	phys_addr_t resv_msi_base;
+	bool resv_msi = false, msi_remap;
+	phys_addr_t resv_msi_base = 0;
 	mutex_lock(&iommu->lock);

drivers/vfio/vfio_iommu_type1.c static int vfio_iommu_type1_attach_group(void *iommu_data, · line 1488

Quoted 10 lines
drivers/vfio/vfio_iommu_type1.c +7 -0
@@ -1373,6 +1479,14 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,
 	if (mdev_bus) {
 		if ((bus == mdev_bus) && !iommu_present(bus)) {
 			symbol_put(mdev_bus_type);
+
+			ret = vfio_iommu_type1_attach_sdgroup(domain, group,
+					iommu_group);
+			if (ret < 0)
+				goto out_free;
+			else if (ret > 0)
+				goto replay_check;

Here you get the domain from the parent device and save it for later use. The actual attaching is ignored.

Quoted 3 lines

I don't think this follows the philosophy of this function. It actually make all devices in the group with the same bus type to share a single domain.

I think the original logic here is:

1. Create a new vfio_domain along with a iommu_domain for the group attached to the container
2. Try to match the vfio_domain with the domain list in the container. If there is a match, free the created one and and reuse it, or add the new vfio_domain to the list.

With this design, the same configuration to the IOMMU(unit) will be applied only once.

For iommu_group that shares IOMMU with its parent, the configuration will never be the same (The PASID will be different), so it is not necessary to merge them.

Quoted 4 lines

Further more, the parent domain might be a domain of type IOMMU_DOMAIN_DMA. That will not be able to use as an IOMMU_DOMAIN_UNMANAGED domain for iommu APIs.

Indeed, it should be checked when the domain is shared. Unmanaged domain should not be used for sharing. I will update it in the future.

Quoted 3 lines

Best regards,
Lu Baolu

Signature · 14 lines
			-Kenneth(Hisilicon)

================================================================================
本邮件及其附件含有华为公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁
止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中
的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from HUAWEI,
which is intended only for the person or entity whose address is listed above.
Any use of the 
information contained herein in any way (including, but not limited to, total or
partial disclosure, reproduction, or dissemination) by persons other than the
intended 
recipient(s) is prohibited. If you receive this e-mail in error, please notify
the sender by phone or email immediately and delete it!
#10 lore raw

[RFC PATCH] vfio: vfio_sdmdev_groups[] can be static

In reply to Kenneth Lee
Fixes: 1e47d5e60865 ("vfio: add sdmdev support")
Fixes 1e47d5e60865 ("vfio: add sdmdev support")
---
 vfio_sdmdev.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/sdmdev/vfio_sdmdev.c +1 -1
index c6eb5d4..e7d3c23 100644
@@ -103,7 +103,7 @@ static const struct attribute_group vfio_sdmdev_group = {
 	.name  = VFIO_SDMDEV_PDEV_ATTRS_GRP_NAME,
 	.attrs = vfio_sdmdev_attrs,
 };
-const struct attribute_group *vfio_sdmdev_groups[] = {
+static const struct attribute_group *vfio_sdmdev_groups[] = {
 	&vfio_sdmdev_group,
 	NULL,
 };
#11 lore raw

Re: [PATCH 3/7] vfio: add sdmdev support

In reply to Kenneth Lee
Thank you for the patch! Perhaps something to improve:

Hi Kenneth,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on cryptodev/master]
[also build test WARNING on v4.19-rc2 next-20180831]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Kenneth-Lee/A-General-Accelerator-Framework-WarpDrive/20180903-162733
base:   https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master
reproduce:
        # apt-get install sparse
        make ARCH=x86_64 allmodconfig
        make C=1 CF=-D__CHECK_ENDIAN__
:::::: branch date: 3 hours ago
:::::: commit date: 3 hours ago
>> drivers/vfio/sdmdev/vfio_sdmdev.c:106:30: sparse: symbol 'vfio_sdmdev_groups' was not declared. Should it be static?
   drivers/vfio/sdmdev/vfio_sdmdev.c: In function 'vfio_sdmdev_mdev_remove':
   drivers/vfio/sdmdev/vfio_sdmdev.c:178:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
     if (sdmdev->ops->put_queue);
     ^~
   drivers/vfio/sdmdev/vfio_sdmdev.c:179:3: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
      sdmdev->ops->put_queue(q);
      ^~~~~~

Please review and possibly fold the followup patch.

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#12 lore raw

Re: [PATCH 3/7] vfio: add sdmdev support

In reply to Kenneth Lee
Thank you for the patch! Perhaps something to improve:

Hi Kenneth,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on cryptodev/master]
[also build test WARNING on v4.19-rc2 next-20180831]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Kenneth-Lee/A-General-Accelerator-Framework-WarpDrive/20180903-162733
base:   https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master
config: i386-allmodconfig (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=i386 
:::::: branch date: 2 hours ago
:::::: commit date: 2 hours ago

All warnings (new ones prefixed by >>):

drivers/vfio/sdmdev/vfio_sdmdev.c: In function 'vfio_sdmdev_mdev_remove':

>> drivers/vfio/sdmdev/vfio_sdmdev.c:178:2: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
     if (sdmdev->ops->put_queue);
     ^~
   drivers/vfio/sdmdev/vfio_sdmdev.c:179:3: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
      sdmdev->ops->put_queue(q);
      ^~~~~~

# https://github.com/0day-ci/linux/commit/1e47d5e608652b4a2c813dbeaf5aa6811f6ceaf7
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout 1e47d5e608652b4a2c813dbeaf5aa6811f6ceaf7
vim +/if +178 drivers/vfio/sdmdev/vfio_sdmdev.c

1e47d5e6 Kenneth Lee 2018-09-03  168  
1e47d5e6 Kenneth Lee 2018-09-03  169  static int vfio_sdmdev_mdev_remove(struct mdev_device *mdev)
1e47d5e6 Kenneth Lee 2018-09-03  170  {
1e47d5e6 Kenneth Lee 2018-09-03  171  	struct vfio_sdmdev_queue *q =
1e47d5e6 Kenneth Lee 2018-09-03  172  		(struct vfio_sdmdev_queue *)mdev_get_drvdata(mdev);
1e47d5e6 Kenneth Lee 2018-09-03  173  	struct vfio_sdmdev *sdmdev = q->sdmdev;
1e47d5e6 Kenneth Lee 2018-09-03  174  	struct device *pdev = mdev_parent_dev(mdev);
1e47d5e6 Kenneth Lee 2018-09-03  175  
1e47d5e6 Kenneth Lee 2018-09-03  176  	put_device(pdev);
1e47d5e6 Kenneth Lee 2018-09-03  177  
1e47d5e6 Kenneth Lee 2018-09-03 @178  	if (sdmdev->ops->put_queue);
1e47d5e6 Kenneth Lee 2018-09-03  179  		sdmdev->ops->put_queue(q);
1e47d5e6 Kenneth Lee 2018-09-03  180  
1e47d5e6 Kenneth Lee 2018-09-03  181  	return 0;
1e47d5e6 Kenneth Lee 2018-09-03  182  }
1e47d5e6 Kenneth Lee 2018-09-03  183  

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#13 lore raw

Re: [PATCH 3/7] vfio: add sdmdev support

In reply to Kenneth Lee

Hi Kenneth,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on cryptodev/master]
[also build test WARNING on v4.19-rc2 next-20180905]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Kenneth-Lee/A-General-Accelerator-Framework-WarpDrive/20180903-162733
base:   https://git.kernel.org/pub/scm/linux/kernel/git/herbert/cryptodev-2.6.git master

smatch warnings:
drivers/vfio/sdmdev/vfio_sdmdev.c:78 iommu_type_show() error: 'sdmdev' dereferencing possible ERR_PTR() drivers/vfio/sdmdev/vfio_sdmdev.c:91 dma_flag_show() error: 'sdmdev' dereferencing possible ERR_PTR() drivers/vfio/sdmdev/vfio_sdmdev.c:127 flags_show() error: 'sdmdev' dereferencing possible ERR_PTR() drivers/vfio/sdmdev/vfio_sdmdev.c:128 name_show() error: 'sdmdev' dereferencing possible ERR_PTR() drivers/vfio/sdmdev/vfio_sdmdev.c:130 device_api_show() error: 'sdmdev' dereferencing possible ERR_PTR() drivers/vfio/sdmdev/vfio_sdmdev.c:138 available_instances_show() error: 'sdmdev' dereferencing possible ERR_PTR() drivers/vfio/sdmdev/vfio_sdmdev.c:178 vfio_sdmdev_mdev_remove() warn: if();

# https://github.com/0day-ci/linux/commit/1e47d5e608652b4a2c813dbeaf5aa6811f6ceaf7
git remote add linux-review https://github.com/0day-ci/linux
git remote update linux-review
git checkout 1e47d5e608652b4a2c813dbeaf5aa6811f6ceaf7
vim +/sdmdev +78 drivers/vfio/sdmdev/vfio_sdmdev.c

1e47d5e6 Kenneth Lee 2018-09-03   69  
1e47d5e6 Kenneth Lee 2018-09-03   70  static ssize_t iommu_type_show(struct device *dev,
1e47d5e6 Kenneth Lee 2018-09-03   71  			       struct device_attribute *attr, char *buf)
1e47d5e6 Kenneth Lee 2018-09-03   72  {
1e47d5e6 Kenneth Lee 2018-09-03   73  	struct vfio_sdmdev *sdmdev = vfio_sdmdev_pdev_sdmdev(dev);
                                                                     ^^^^^^^^^^^^^^^^^^^^^^^

Presumably this returns error pointers instead of NULL?

1e47d5e6 Kenneth Lee 2018-09-03   74  
1e47d5e6 Kenneth Lee 2018-09-03   75  	if (!sdmdev)
1e47d5e6 Kenneth Lee 2018-09-03   76  		return -ENODEV;
1e47d5e6 Kenneth Lee 2018-09-03   77  
1e47d5e6 Kenneth Lee 2018-09-03  @78  	return sprintf(buf, "%d\n", sdmdev->iommu_type);
1e47d5e6 Kenneth Lee 2018-09-03   79  }
1e47d5e6 Kenneth Lee 2018-09-03   80  
1e47d5e6 Kenneth Lee 2018-09-03   81  static DEVICE_ATTR_RO(iommu_type);
1e47d5e6 Kenneth Lee 2018-09-03   82  
1e47d5e6 Kenneth Lee 2018-09-03   83  static ssize_t dma_flag_show(struct device *dev,
1e47d5e6 Kenneth Lee 2018-09-03   84  			     struct device_attribute *attr, char *buf)
1e47d5e6 Kenneth Lee 2018-09-03   85  {
1e47d5e6 Kenneth Lee 2018-09-03   86  	struct vfio_sdmdev *sdmdev = vfio_sdmdev_pdev_sdmdev(dev);
1e47d5e6 Kenneth Lee 2018-09-03   87  
1e47d5e6 Kenneth Lee 2018-09-03   88  	if (!sdmdev)
1e47d5e6 Kenneth Lee 2018-09-03   89  		return -ENODEV;
1e47d5e6 Kenneth Lee 2018-09-03   90  
1e47d5e6 Kenneth Lee 2018-09-03  @91  	return sprintf(buf, "%d\n", sdmdev->dma_flag);
1e47d5e6 Kenneth Lee 2018-09-03   92  }
1e47d5e6 Kenneth Lee 2018-09-03   93  
1e47d5e6 Kenneth Lee 2018-09-03   94  static DEVICE_ATTR_RO(dma_flag);
1e47d5e6 Kenneth Lee 2018-09-03   95  
1e47d5e6 Kenneth Lee 2018-09-03   96  /* mdev->dev_attr_groups */
1e47d5e6 Kenneth Lee 2018-09-03   97  static struct attribute *vfio_sdmdev_attrs[] = {
1e47d5e6 Kenneth Lee 2018-09-03   98  	&dev_attr_iommu_type.attr,
1e47d5e6 Kenneth Lee 2018-09-03   99  	&dev_attr_dma_flag.attr,
1e47d5e6 Kenneth Lee 2018-09-03  100  	NULL,
1e47d5e6 Kenneth Lee 2018-09-03  101  };
1e47d5e6 Kenneth Lee 2018-09-03  102  static const struct attribute_group vfio_sdmdev_group = {
1e47d5e6 Kenneth Lee 2018-09-03  103  	.name  = VFIO_SDMDEV_PDEV_ATTRS_GRP_NAME,
1e47d5e6 Kenneth Lee 2018-09-03  104  	.attrs = vfio_sdmdev_attrs,
1e47d5e6 Kenneth Lee 2018-09-03  105  };
1e47d5e6 Kenneth Lee 2018-09-03  106  const struct attribute_group *vfio_sdmdev_groups[] = {
1e47d5e6 Kenneth Lee 2018-09-03  107  	&vfio_sdmdev_group,
1e47d5e6 Kenneth Lee 2018-09-03  108  	NULL,
1e47d5e6 Kenneth Lee 2018-09-03  109  };
1e47d5e6 Kenneth Lee 2018-09-03  110  
1e47d5e6 Kenneth Lee 2018-09-03  111  /* default attributes for mdev->supported_type_groups, used by registerer*/
1e47d5e6 Kenneth Lee 2018-09-03  112  #define MDEV_TYPE_ATTR_RO_EXPORT(name) \
1e47d5e6 Kenneth Lee 2018-09-03  113  		MDEV_TYPE_ATTR_RO(name); \
1e47d5e6 Kenneth Lee 2018-09-03  114  		EXPORT_SYMBOL_GPL(mdev_type_attr_##name);
1e47d5e6 Kenneth Lee 2018-09-03  115  
1e47d5e6 Kenneth Lee 2018-09-03  116  #define DEF_SIMPLE_SDMDEV_ATTR(_name, sdmdev_member, format) \
1e47d5e6 Kenneth Lee 2018-09-03  117  static ssize_t _name##_show(struct kobject *kobj, struct device *dev, \
1e47d5e6 Kenneth Lee 2018-09-03  118  			    char *buf) \
1e47d5e6 Kenneth Lee 2018-09-03  119  { \
1e47d5e6 Kenneth Lee 2018-09-03  120  	struct vfio_sdmdev *sdmdev = vfio_sdmdev_pdev_sdmdev(dev); \
1e47d5e6 Kenneth Lee 2018-09-03  121  	if (!sdmdev) \
1e47d5e6 Kenneth Lee 2018-09-03  122  		return -ENODEV; \
1e47d5e6 Kenneth Lee 2018-09-03  123  	return sprintf(buf, format, sdmdev->sdmdev_member); \
1e47d5e6 Kenneth Lee 2018-09-03  124  } \
1e47d5e6 Kenneth Lee 2018-09-03  125  MDEV_TYPE_ATTR_RO_EXPORT(_name)
1e47d5e6 Kenneth Lee 2018-09-03  126  
1e47d5e6 Kenneth Lee 2018-09-03 @127  DEF_SIMPLE_SDMDEV_ATTR(flags, flags, "%d");
1e47d5e6 Kenneth Lee 2018-09-03 @128  DEF_SIMPLE_SDMDEV_ATTR(name, name, "%s"); /* this should be algorithm name, */
1e47d5e6 Kenneth Lee 2018-09-03  129  		/* but you would not care if you have only one algorithm */
1e47d5e6 Kenneth Lee 2018-09-03 @130  DEF_SIMPLE_SDMDEV_ATTR(device_api, api_ver, "%s");
1e47d5e6 Kenneth Lee 2018-09-03  131  
1e47d5e6 Kenneth Lee 2018-09-03  132  static ssize_t
1e47d5e6 Kenneth Lee 2018-09-03  133  available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
1e47d5e6 Kenneth Lee 2018-09-03  134  {
1e47d5e6 Kenneth Lee 2018-09-03  135  	struct vfio_sdmdev *sdmdev = vfio_sdmdev_pdev_sdmdev(dev);
1e47d5e6 Kenneth Lee 2018-09-03  136  	int nr_inst = 0;
1e47d5e6 Kenneth Lee 2018-09-03  137  
1e47d5e6 Kenneth Lee 2018-09-03 @138  	nr_inst = sdmdev->ops->get_available_instances ?
1e47d5e6 Kenneth Lee 2018-09-03  139  		sdmdev->ops->get_available_instances(sdmdev) : 0;
1e47d5e6 Kenneth Lee 2018-09-03  140  	return sprintf(buf, "%d", nr_inst);
1e47d5e6 Kenneth Lee 2018-09-03  141  }
1e47d5e6 Kenneth Lee 2018-09-03  142  MDEV_TYPE_ATTR_RO_EXPORT(available_instances);
1e47d5e6 Kenneth Lee 2018-09-03  143  
1e47d5e6 Kenneth Lee 2018-09-03  144  static int vfio_sdmdev_mdev_create(struct kobject *kobj,
1e47d5e6 Kenneth Lee 2018-09-03  145  	struct mdev_device *mdev)
1e47d5e6 Kenneth Lee 2018-09-03  146  {
1e47d5e6 Kenneth Lee 2018-09-03  147  	struct device *pdev = mdev_parent_dev(mdev);
1e47d5e6 Kenneth Lee 2018-09-03  148  	struct vfio_sdmdev_queue *q;
1e47d5e6 Kenneth Lee 2018-09-03  149  	struct vfio_sdmdev *sdmdev = mdev_sdmdev(mdev);
1e47d5e6 Kenneth Lee 2018-09-03  150  	int ret;
1e47d5e6 Kenneth Lee 2018-09-03  151  
1e47d5e6 Kenneth Lee 2018-09-03  152  	if (!sdmdev->ops->get_queue)
1e47d5e6 Kenneth Lee 2018-09-03  153  		return -ENODEV;
1e47d5e6 Kenneth Lee 2018-09-03  154  
1e47d5e6 Kenneth Lee 2018-09-03  155  	ret = sdmdev->ops->get_queue(sdmdev, &q);
1e47d5e6 Kenneth Lee 2018-09-03  156  	if (ret)
1e47d5e6 Kenneth Lee 2018-09-03  157  		return ret;
1e47d5e6 Kenneth Lee 2018-09-03  158  
1e47d5e6 Kenneth Lee 2018-09-03  159  	q->sdmdev = sdmdev;
1e47d5e6 Kenneth Lee 2018-09-03  160  	q->mdev = mdev;
1e47d5e6 Kenneth Lee 2018-09-03  161  	init_waitqueue_head(&q->wait);
1e47d5e6 Kenneth Lee 2018-09-03  162  
1e47d5e6 Kenneth Lee 2018-09-03  163  	mdev_set_drvdata(mdev, q);
1e47d5e6 Kenneth Lee 2018-09-03  164  	get_device(pdev);
1e47d5e6 Kenneth Lee 2018-09-03  165  
1e47d5e6 Kenneth Lee 2018-09-03  166  	return 0;
1e47d5e6 Kenneth Lee 2018-09-03  167  }
1e47d5e6 Kenneth Lee 2018-09-03  168  
1e47d5e6 Kenneth Lee 2018-09-03  169  static int vfio_sdmdev_mdev_remove(struct mdev_device *mdev)
1e47d5e6 Kenneth Lee 2018-09-03  170  {
1e47d5e6 Kenneth Lee 2018-09-03  171  	struct vfio_sdmdev_queue *q =
1e47d5e6 Kenneth Lee 2018-09-03  172  		(struct vfio_sdmdev_queue *)mdev_get_drvdata(mdev);
1e47d5e6 Kenneth Lee 2018-09-03  173  	struct vfio_sdmdev *sdmdev = q->sdmdev;
1e47d5e6 Kenneth Lee 2018-09-03  174  	struct device *pdev = mdev_parent_dev(mdev);
1e47d5e6 Kenneth Lee 2018-09-03  175  
1e47d5e6 Kenneth Lee 2018-09-03  176  	put_device(pdev);
1e47d5e6 Kenneth Lee 2018-09-03  177  
1e47d5e6 Kenneth Lee 2018-09-03 @178  	if (sdmdev->ops->put_queue);
                                                                   ^

Extra semicolon breaks the code.

1e47d5e6 Kenneth Lee 2018-09-03  179  		sdmdev->ops->put_queue(q);
1e47d5e6 Kenneth Lee 2018-09-03  180  
1e47d5e6 Kenneth Lee 2018-09-03  181  	return 0;
1e47d5e6 Kenneth Lee 2018-09-03  182  }
1e47d5e6 Kenneth Lee 2018-09-03  183  
#14 lore raw

PATCH v2 4/7 crypto: add hisilicon Queue Manager driver

In reply to Kenneth Lee

From: Kenneth Lee <[email protected]>

Hisilicon QM is a general IP used by some Hisilicon accelerators. It provides a general PCIE interface for the CPU and the accelerator to share a group of queues.

This commit includes a library used by the accelerator driver to access the QM hardware.

---
 drivers/crypto/Makefile           |   2 +-
 drivers/crypto/hisilicon/Kconfig  |   8 +
 drivers/crypto/hisilicon/Makefile |   1 +
 drivers/crypto/hisilicon/qm.c     | 820 ++++++++++++++++++++++++++++++
 drivers/crypto/hisilicon/qm.h     | 110 ++++
 5 files changed, 940 insertions(+), 1 deletion(-)
 create mode 100644 drivers/crypto/hisilicon/qm.c
 create mode 100644 drivers/crypto/hisilicon/qm.h
drivers/crypto/Makefile +1 -1
index c23396f32c8a..f3a7abe42424 100644
@@ -46,4 +46,4 @@ obj-$(CONFIG_CRYPTO_DEV_VMX) += vmx/
 obj-$(CONFIG_CRYPTO_DEV_BCM_SPU) += bcm/
 obj-$(CONFIG_CRYPTO_DEV_SAFEXCEL) += inside-secure/
 obj-$(CONFIG_CRYPTO_DEV_ARTPEC6) += axis/
-obj-y += hisilicon/
+obj-$(CONFIG_CRYPTO_DEV_HISILICON) += hisilicon/
drivers/crypto/hisilicon/Kconfig +8 -0
index 8ca9c503bcb0..02a6eef84101 100644
@@ -1,4 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
+config CRYPTO_DEV_HISILICON
+	tristate "Support for HISILICON CRYPTO ACCELERATOR"
+	help
+	  Enable this to use Hisilicon Hardware Accelerators
 
 config CRYPTO_DEV_HISI_SEC
 	tristate "Support for Hisilicon SEC crypto block cipher accelerator"
@@ -12,3 +16,7 @@ config CRYPTO_DEV_HISI_SEC
 
 	  To compile this as a module, choose M here: the module
 	  will be called hisi_sec.
+
+config CRYPTO_DEV_HISI_QM
+	tristate
+	depends on ARM64 && PCI
drivers/crypto/hisilicon/Makefile +1 -0
index 463f46ace182..05e9052e0f52 100644
@@ -1,2 +1,3 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CRYPTO_DEV_HISI_SEC) += sec/
+obj-$(CONFIG_CRYPTO_DEV_HISI_QM) += qm.o
drivers/crypto/hisilicon/qm.c new file +820 -0

820 lines not on this page · raw message

drivers/crypto/hisilicon/qm.h new file +110 -0

110 lines not on this page · raw message

Signature · 1 line
2.17.1
reviewer
#15 lore raw

Re: [PATCH 4/7] crypto: add hisilicon Queue Manager driver

drivers/crypto/hisilicon/Kconfig line 5

Quoted 10 lines · Kenneth Lee
drivers/crypto/hisilicon/Kconfig +4 -0
index 8ca9c503bcb0..02a6eef84101 100644
@@ -1,4 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
+config CRYPTO_DEV_HISILICON
+	tristate "Support for HISILICON CRYPTO ACCELERATOR"
+	help
+	  Enable this to use Hisilicon Hardware Accelerators

Accelerators.

Signature · 1 line
~Randy
#16 lore raw

Re: [PATCH 4/7] crypto: add hisilicon Queue Manager driver

Quoted 21 lines · Randy Dunlap

Date: Sun, 2 Sep 2018 19:15:07 -0700
From: Randy Dunlap <[email protected]>
To: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Alex Williamson <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>
CC: [email protected]
Subject: Re: [PATCH 4/7] crypto: add hisilicon Queue Manager driver
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101
Thunderbird/52.9.1
Message-ID: <[email protected]>

On 09/02/2018 05:52 PM, Kenneth Lee wrote:

Quoted 12 lines

drivers/crypto/hisilicon/Kconfig line 5

Quoted 10 lines
drivers/crypto/hisilicon/Kconfig +4 -0
index 8ca9c503bcb0..02a6eef84101 100644
@@ -1,4 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
+config CRYPTO_DEV_HISILICON
+	tristate "Support for HISILICON CRYPTO ACCELERATOR"
+	help
+	  Enable this to use Hisilicon Hardware Accelerators

Accelerators.

Thanks, will change it in next version.

Quoted 4 lines
Signature · 2 lines
-- 
~Randy
Signature · 19 lines
			-Kenneth(Hisilicon)

================================================================================
本邮件及其附件含有华为公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁
止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中
的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from HUAWEI,
which is intended only for the person or entity whose address is listed above.
Any use of the 
information contained herein in any way (including, but not limited to, total or
partial disclosure, reproduction, or dissemination) by persons other than the
intended 
recipient(s) is prohibited. If you receive this e-mail in error, please notify
the sender by phone or email immediately and delete it!

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#17 lore raw

PATCH v2 5/7 crypto: Add Hisilicon Zip driver

In reply to Kenneth Lee

From: Kenneth Lee <[email protected]>

The Hisilicon ZIP accelerator implements zlib and gzip algorithm support for the software. It uses Hisilicon QM as the interface to the CPU, so it is shown up as a PCIE device to the CPU with a group of queues.

This commit provides PCIE driver to the accelerator and register it to the crypto subsystem.

---
 drivers/crypto/hisilicon/Kconfig          |   7 +
 drivers/crypto/hisilicon/Makefile         |   1 +
 drivers/crypto/hisilicon/zip/Makefile     |   2 +
 drivers/crypto/hisilicon/zip/zip.h        |  57 ++++
 drivers/crypto/hisilicon/zip/zip_crypto.c | 353 ++++++++++++++++++++++
 drivers/crypto/hisilicon/zip/zip_crypto.h |   8 +
 drivers/crypto/hisilicon/zip/zip_main.c   | 195 ++++++++++++
 7 files changed, 623 insertions(+)
 create mode 100644 drivers/crypto/hisilicon/zip/Makefile
 create mode 100644 drivers/crypto/hisilicon/zip/zip.h
 create mode 100644 drivers/crypto/hisilicon/zip/zip_crypto.c
 create mode 100644 drivers/crypto/hisilicon/zip/zip_crypto.h
 create mode 100644 drivers/crypto/hisilicon/zip/zip_main.c
drivers/crypto/hisilicon/Kconfig +7 -0
index 02a6eef84101..1d155708cd69 100644
@@ -20,3 +20,10 @@ config CRYPTO_DEV_HISI_SEC
 config CRYPTO_DEV_HISI_QM
 	tristate
 	depends on ARM64 && PCI
+
+config CRYPTO_DEV_HISI_ZIP
+	tristate "Support for HISI ZIP Driver"
+	depends on ARM64 && CRYPTO_DEV_HISILICON
+	select CRYPTO_DEV_HISI_QM
+	help
+	  Support for HiSilicon HIP08 ZIP Driver
drivers/crypto/hisilicon/Makefile +1 -0
index 05e9052e0f52..c97c5b27c3cb 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0
 obj-$(CONFIG_CRYPTO_DEV_HISI_SEC) += sec/
 obj-$(CONFIG_CRYPTO_DEV_HISI_QM) += qm.o
+obj-$(CONFIG_CRYPTO_DEV_HISI_ZIP) += zip/
drivers/crypto/hisilicon/zip/Makefile new file +2 -0
new file mode 100644
index 000000000000..a936f099ee22
@@ -0,0 +1,2 @@
+obj-$(CONFIG_CRYPTO_DEV_HISI_ZIP) += hisi_zip.o
+hisi_zip-objs = zip_main.o zip_crypto.o
drivers/crypto/hisilicon/zip/zip.h new file +57 -0

57 lines not on this page · raw message

drivers/crypto/hisilicon/zip/zip_crypto.c new file +353 -0

353 lines not on this page · raw message

drivers/crypto/hisilicon/zip/zip_crypto.h new file +8 -0
new file mode 100644
index 000000000000..84eefd74c9c4
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+#ifndef HISI_ZIP_CRYPTO_H
+#define HISI_ZIP_CRYPTO_H
+
+int hisi_zip_register_to_crypto(void);
+void hisi_zip_unregister_from_crypto(void);
+
+#endif
drivers/crypto/hisilicon/zip/zip_main.c new file +195 -0

195 lines not on this page · raw message

Signature · 1 line
2.17.1
#18 lore raw

PATCH v2 6/7 crypto: add sdmdev support to Hisilicon QM

In reply to Kenneth Lee

From: Kenneth Lee <[email protected]>

This commit add spimdev support to the Hislicon QM driver, any accelerator that use QM can expose its queues to the user space.

---
 drivers/crypto/hisilicon/Kconfig |  10 ++
 drivers/crypto/hisilicon/qm.c    | 159 +++++++++++++++++++++++++++++++
 drivers/crypto/hisilicon/qm.h    |  12 +++
 3 files changed, 181 insertions(+)
drivers/crypto/hisilicon/Kconfig +10 -0
index 1d155708cd69..b85fab48fdab 100644
@@ -17,6 +17,16 @@ config CRYPTO_DEV_HISI_SEC
 	  To compile this as a module, choose M here: the module
 	  will be called hisi_sec.
 
+config CRYPTO_DEV_HISI_SDMDEV
+	bool "Enable SDMDEV interface"
+	depends on CRYPTO_DEV_HISILICON
+	select VFIO_SDMDEV
+	help
+	  Enable this enable the SDMDEV, "shared IOMMU Domain Mediated Device"
+	  interface for all Hisilicon accelerators if they can. The SDMDEV
+	  enable the WarpDrive user space accelerator driver to access the
+	  hardware function directly.
+
 config CRYPTO_DEV_HISI_QM
 	tristate
 	depends on ARM64 && PCI
drivers/crypto/hisilicon/qm.c +159 -0

177 lines not on this page · raw message

drivers/crypto/hisilicon/qm.h +12 -0
index 0e81182ac6a8..0d24e0bd42e8 100644
@@ -8,6 +8,10 @@
 #include <linux/pci.h>
 #include <linux/slab.h>
 
+#ifdef CONFIG_CRYPTO_DEV_HISI_SDMDEV
+#include <linux/vfio_sdmdev.h>
+#endif
+
 #define QM_CQE_SIZE			16
 /* default queue depth for sq/cq/eq */
 #define QM_Q_DEPTH			1024
@@ -59,6 +63,10 @@ struct qm_info {
 
 	struct hisi_acc_qm_hw_ops *ops;
 
+#ifdef CONFIG_CRYPTO_DEV_HISI_SDMDEV
+	struct vfio_sdmdev sdmdev;
+	const struct attribute_group **mdev_dev_groups;
+#endif
 };
 #define QM_ADDR(qm, off) ((qm)->io_base + off)
 
@@ -89,6 +97,10 @@ struct hisi_qp {
 
 	struct qm_info *qm;
 
+#ifdef CONFIG_CRYPTO_DEV_HISI_SDMDEV
+	struct vfio_sdmdev_queue *sdmdev_q;
+#endif
+
 	/* for crypto sync API */
 	struct completion completion;
 
Signature · 1 line
2.17.1
reviewer
#19 lore raw

Re: [PATCH 6/7] crypto: add sdmdev support to Hisilicon QM

Quoted 5 lines · Kenneth Lee
drivers/crypto/hisilicon/Kconfig +0 -0
index 1d155708cd69..b85fab48fdab 100644

drivers/crypto/hisilicon/Kconfig config CRYPTO_DEV_HISI_SEC · line 25

Quoted 9 lines
drivers/crypto/hisilicon/Kconfig +6 -0
@@ -17,6 +17,16 @@ config CRYPTO_DEV_HISI_SEC
 	  To compile this as a module, choose M here: the module
 	  will be called hisi_sec.
 
+config CRYPTO_DEV_HISI_SDMDEV
+	bool "Enable SDMDEV interface"
+	depends on CRYPTO_DEV_HISILICON
+	select VFIO_SDMDEV
+	help
+	  Enable this enable the SDMDEV, "shared IOMMU Domain Mediated Device"

At a minimum:
Enable this to enable the SDMDEV,

although that could be done better. Maybe just:
Enable the SDMDEV "shared IOMMU Domain Mediated Device"

drivers/crypto/hisilicon/Kconfig config CRYPTO_DEV_HISI_SEC · line 26

Quoted 1 line
drivers/crypto/hisilicon/Kconfig +1 -0
+	  interface for all Hisilicon accelerators if they can. The SDMDEV

probably drop "if they can": accelerators. The SDMDEV interface

drivers/crypto/hisilicon/Kconfig config CRYPTO_DEV_HISI_SEC · line 27

Quoted 1 line
drivers/crypto/hisilicon/Kconfig +1 -0
+	  enable the WarpDrive user space accelerator driver to access the

enables

Quoted 2 lines
drivers/crypto/hisilicon/Kconfig +2 -0
+	  hardware function directly.
+
Signature · 1 line
~Randy
#20 lore raw

Re: [PATCH 6/7] crypto: add sdmdev support to Hisilicon QM

Quoted 43 lines · Randy Dunlap

Date: Sun, 2 Sep 2018 19:19:03 -0700
From: Randy Dunlap <[email protected]>
To: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Alex Williamson <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>
CC: [email protected]
Subject: Re: [PATCH 6/7] crypto: add sdmdev support to Hisilicon QM
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101
Thunderbird/52.9.1
Message-ID: <[email protected]>

Quoted 5 lines · Kenneth Lee
drivers/crypto/hisilicon/Kconfig +0 -0
index 1d155708cd69..b85fab48fdab 100644

drivers/crypto/hisilicon/Kconfig config CRYPTO_DEV_HISI_SEC · line 25

Quoted 9 lines
drivers/crypto/hisilicon/Kconfig +6 -0
@@ -17,6 +17,16 @@ config CRYPTO_DEV_HISI_SEC
 	  To compile this as a module, choose M here: the module
 	  will be called hisi_sec.
 
+config CRYPTO_DEV_HISI_SDMDEV
+	bool "Enable SDMDEV interface"
+	depends on CRYPTO_DEV_HISILICON
+	select VFIO_SDMDEV
+	help
+	  Enable this enable the SDMDEV, "shared IOMMU Domain Mediated Device"

At a minimum:
Enable this to enable the SDMDEV,

although that could be done better. Maybe just:
Enable the SDMDEV "shared IOMMU Domain Mediated Device"

Quoted 7 lines

drivers/crypto/hisilicon/Kconfig config CRYPTO_DEV_HISI_SEC · line 26

Quoted 1 line
drivers/crypto/hisilicon/Kconfig +1 -0
+	  interface for all Hisilicon accelerators if they can. The SDMDEV

probably drop "if they can": accelerators. The SDMDEV interface

drivers/crypto/hisilicon/Kconfig config CRYPTO_DEV_HISI_SEC · line 27

Quoted 1 line
drivers/crypto/hisilicon/Kconfig +1 -0
+	  enable the WarpDrive user space accelerator driver to access the

enables

Thank you, will change them all in the coming version.

Quoted 7 lines
Quoted 3 lines
drivers/crypto/hisilicon/Kconfig +2 -0
+	  hardware function directly.
+
Signature · 1 line
~Randy
Signature · 14 lines
			-Kenneth(Hisilicon)

================================================================================
本邮件及其附件含有华为公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁
止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中
的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from HUAWEI,
which is intended only for the person or entity whose address is listed above.
Any use of the 
information contained herein in any way (including, but not limited to, total or
partial disclosure, reproduction, or dissemination) by persons other than the
intended 
recipient(s) is prohibited. If you receive this e-mail in error, please notify
the sender by phone or email immediately and delete it!
#21 lore raw

PATCH v2 7/7 vfio/sdmdev: add user sample

In reply to Kenneth Lee

From: Kenneth Lee <[email protected]>

This is the sample code to demostrate how WrapDrive user application should be.

It contains:

1. wd.[ch], the common library to provide WrapDrive interface.
2. drv/*, the user driver to access the hardware upon spimdev
3. test/*, the test application to use WrapDrive interface to access the hardware queue(s) of the accelerator.

The Hisilicon HIP08 ZIP accelerator is used in this sample.

---
 samples/warpdrive/AUTHORS              |   2 +
 samples/warpdrive/ChangeLog            |   1 +
 samples/warpdrive/Makefile.am          |   9 +
 samples/warpdrive/NEWS                 |   1 +
 samples/warpdrive/README               |  32 +++
 samples/warpdrive/autogen.sh           |   3 +
 samples/warpdrive/cleanup.sh           |  13 ++
 samples/warpdrive/configure.ac         |  52 +++++
 samples/warpdrive/drv/hisi_qm_udrv.c   | 223 ++++++++++++++++++
 samples/warpdrive/drv/hisi_qm_udrv.h   |  53 +++++
 samples/warpdrive/test/Makefile.am     |   7 +
 samples/warpdrive/test/comp_hw.h       |  23 ++
 samples/warpdrive/test/test_hisi_zip.c | 206 +++++++++++++++++
 samples/warpdrive/wd.c                 | 309 +++++++++++++++++++++++++
 samples/warpdrive/wd.h                 | 154 ++++++++++++
 samples/warpdrive/wd_adapter.c         |  74 ++++++
 samples/warpdrive/wd_adapter.h         |  43 ++++
 17 files changed, 1205 insertions(+)
 create mode 100644 samples/warpdrive/AUTHORS
 create mode 100644 samples/warpdrive/ChangeLog
 create mode 100644 samples/warpdrive/Makefile.am
 create mode 100644 samples/warpdrive/NEWS
 create mode 100644 samples/warpdrive/README
 create mode 100755 samples/warpdrive/autogen.sh
 create mode 100755 samples/warpdrive/cleanup.sh
 create mode 100644 samples/warpdrive/configure.ac
 create mode 100644 samples/warpdrive/drv/hisi_qm_udrv.c
 create mode 100644 samples/warpdrive/drv/hisi_qm_udrv.h
 create mode 100644 samples/warpdrive/test/Makefile.am
 create mode 100644 samples/warpdrive/test/comp_hw.h
 create mode 100644 samples/warpdrive/test/test_hisi_zip.c
 create mode 100644 samples/warpdrive/wd.c
 create mode 100644 samples/warpdrive/wd.h
 create mode 100644 samples/warpdrive/wd_adapter.c
 create mode 100644 samples/warpdrive/wd_adapter.h
samples/warpdrive/AUTHORS new file +2 -0
new file mode 100644
index 000000000000..fe7dc2413b0d
@@ -0,0 +1,2 @@
+Kenneth Lee<[email protected]>
+Zaibo Xu<[email protected]>
samples/warpdrive/ChangeLog new file +1 -0
new file mode 100644
index 000000000000..b1b716105590
@@ -0,0 +1 @@
+init
samples/warpdrive/Makefile.am new file +9 -0
new file mode 100644
index 000000000000..41154a880a97
@@ -0,0 +1,9 @@
+ACLOCAL_AMFLAGS = -I m4
+AUTOMAKE_OPTIONS = foreign subdir-objects
+AM_CFLAGS=-Wall -O0 -fno-strict-aliasing
+
+lib_LTLIBRARIES=libwd.la
+libwd_la_SOURCES=wd.c wd_adapter.c wd.h wd_adapter.h \
+		 drv/hisi_qm_udrv.c drv/hisi_qm_udrv.h
+
+SUBDIRS=. test
samples/warpdrive/NEWS new file +1 -0
new file mode 100644
index 000000000000..b1b716105590
@@ -0,0 +1 @@
+init
samples/warpdrive/README new file +32 -0
new file mode 100644
index 000000000000..3adf66b112fc
@@ -0,0 +1,32 @@
+WD User Land Demonstration
+==========================
+
+This directory contains some applications and libraries to demonstrate how a
+
+WrapDrive application can be constructed.
+
+
+As a demo, we try to make it simple and clear for understanding. It is not
+
+supposed to be used in business scenario.
+
+
+The directory contains the following elements:
+
+wd.[ch]
+	A demonstration WrapDrive fundamental library which wraps the basic
+	operations to the WrapDrive-ed device.
+
+wd_adapter.[ch]
+	User driver adaptor for wd.[ch]
+
+wd_utils.[ch]
+	Some utitlities function used by WD and its drivers
+
+drv/*
+	User drivers. It helps to fulfill the semantic of wd.[ch] for
+	particular hardware
+
+test/*
+	Test applications to use the wrapdrive library
+
samples/warpdrive/autogen.sh new file +3 -0
new file mode 100755
index 000000000000..58deaf49de2a
@@ -0,0 +1,3 @@
+#!/bin/sh -x
+
+autoreconf -i -f -v
samples/warpdrive/cleanup.sh new file +13 -0
new file mode 100755
index 000000000000..c5f3d21e5dc1
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+if [ -r Makefile ]; then
+	make distclean
+fi
+
+FILES="aclocal.m4 autom4te.cache compile config.guess config.h.in config.log \
+       config.status config.sub configure cscope.out depcomp install-sh      \
+       libsrc/Makefile libsrc/Makefile.in libtool ltmain.sh Makefile         \
+       ar-lib m4 \
+       Makefile.in missing src/Makefile src/Makefile.in test/Makefile.in"
+
+rm -vRf $FILES
samples/warpdrive/configure.ac new file +52 -0

52 lines not on this page · raw message

samples/warpdrive/drv/hisi_qm_udrv.c new file +223 -0

223 lines not on this page · raw message

samples/warpdrive/drv/hisi_qm_udrv.h new file +53 -0

53 lines not on this page · raw message

samples/warpdrive/test/Makefile.am new file +7 -0
new file mode 100644
index 000000000000..ad80e80a47d7
@@ -0,0 +1,7 @@
+AM_CFLAGS=-Wall -O0 -fno-strict-aliasing
+
+bin_PROGRAMS=test_hisi_zip
+
+test_hisi_zip_SOURCES=test_hisi_zip.c
+
+test_hisi_zip_LDADD=../.libs/libwd.a
samples/warpdrive/test/comp_hw.h new file +23 -0
new file mode 100644
index 000000000000..79328fd0c1a0
@@ -0,0 +1,23 @@
+// SPDX-License-Identifier: GPL-2.0
+/**
+ * This file is shared bewteen user and kernel space Wrapdrive which is
+ * including algorithm attibutions that both user and driver are caring for
+ */
+
+#ifndef __VFIO_WDEV_COMP_H
+#define __VFIO_WDEV_COMP_H
+
+/* De-compressing algorithms' parameters */
+struct vfio_wdev_comp_param {
+	__u32 window_size;
+	__u32 comp_level;
+	__u32 mode;
+	__u32 alg;
+};
+
+/* WD defines all the De-compressing algorithm names here */
+#define VFIO_WDEV_ZLIB			"zlib"
+#define VFIO_WDEV_GZIP			"gzip"
+#define VFIO_WDEV_LZ4			"lz4"
+
+#endif
samples/warpdrive/test/test_hisi_zip.c new file +206 -0

206 lines not on this page · raw message

samples/warpdrive/wd.c new file +309 -0

309 lines not on this page · raw message

samples/warpdrive/wd.h new file +154 -0

154 lines not on this page · raw message

samples/warpdrive/wd_adapter.c new file +74 -0

74 lines not on this page · raw message

samples/warpdrive/wd_adapter.h new file +43 -0

43 lines not on this page · raw message

Signature · 1 line
2.17.1
reviewer
#22 lore raw

Re: [PATCH 7/7] vfio/sdmdev: add user sample

Quoted 8 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

This is the sample code to demostrate how WrapDrive user application should be.

It contains:

1. wd.[ch], the common library to provide WrapDrive interface.

WarpDrive

Quoted 2 lines

2. drv/*, the user driver to access the hardware upon spimdev
3. test/*, the test application to use WrapDrive interface to access the

WarpDrive

Quoted 51 lines

hardware queue(s) of the accelerator.

The Hisilicon HIP08 ZIP accelerator is used in this sample.

---
 samples/warpdrive/AUTHORS              |   2 +
 samples/warpdrive/ChangeLog            |   1 +
 samples/warpdrive/Makefile.am          |   9 +
 samples/warpdrive/NEWS                 |   1 +
 samples/warpdrive/README               |  32 +++
 samples/warpdrive/autogen.sh           |   3 +
 samples/warpdrive/cleanup.sh           |  13 ++
 samples/warpdrive/configure.ac         |  52 +++++
 samples/warpdrive/drv/hisi_qm_udrv.c   | 223 ++++++++++++++++++
 samples/warpdrive/drv/hisi_qm_udrv.h   |  53 +++++
 samples/warpdrive/test/Makefile.am     |   7 +
 samples/warpdrive/test/comp_hw.h       |  23 ++
 samples/warpdrive/test/test_hisi_zip.c | 206 +++++++++++++++++
 samples/warpdrive/wd.c                 | 309 +++++++++++++++++++++++++
 samples/warpdrive/wd.h                 | 154 ++++++++++++
 samples/warpdrive/wd_adapter.c         |  74 ++++++
 samples/warpdrive/wd_adapter.h         |  43 ++++
 17 files changed, 1205 insertions(+)
 create mode 100644 samples/warpdrive/AUTHORS
 create mode 100644 samples/warpdrive/ChangeLog
 create mode 100644 samples/warpdrive/Makefile.am
 create mode 100644 samples/warpdrive/NEWS
 create mode 100644 samples/warpdrive/README
 create mode 100755 samples/warpdrive/autogen.sh
 create mode 100755 samples/warpdrive/cleanup.sh
 create mode 100644 samples/warpdrive/configure.ac
 create mode 100644 samples/warpdrive/drv/hisi_qm_udrv.c
 create mode 100644 samples/warpdrive/drv/hisi_qm_udrv.h
 create mode 100644 samples/warpdrive/test/Makefile.am
 create mode 100644 samples/warpdrive/test/comp_hw.h
 create mode 100644 samples/warpdrive/test/test_hisi_zip.c
 create mode 100644 samples/warpdrive/wd.c
 create mode 100644 samples/warpdrive/wd.h
 create mode 100644 samples/warpdrive/wd_adapter.c
 create mode 100644 samples/warpdrive/wd_adapter.h
samples/warpdrive/README new file +0 -0
new file mode 100644
index 000000000000..3adf66b112fc

samples/warpdrive/README line 6

Quoted 6 lines
samples/warpdrive/README new file +6 -0
@@ -0,0 +1,32 @@
+WD User Land Demonstration
+==========================
+
+This directory contains some applications and libraries to demonstrate how a
+
+WrapDrive application can be constructed.

WarpDrive

samples/warpdrive/README line 17

Quoted 11 lines
samples/warpdrive/README new file +11 -0
+
+
+As a demo, we try to make it simple and clear for understanding. It is not
+
+supposed to be used in business scenario.
+
+
+The directory contains the following elements:
+
+wd.[ch]
+	A demonstration WrapDrive fundamental library which wraps the basic

WarpDrive

samples/warpdrive/README line 18

Quoted 1 line
samples/warpdrive/README new file +1 -0
+	operations to the WrapDrive-ed device.

WarpDrive

samples/warpdrive/README line 31

Quoted 13 lines
samples/warpdrive/README new file +13 -0
+
+wd_adapter.[ch]
+	User driver adaptor for wd.[ch]
+
+wd_utils.[ch]
+	Some utitlities function used by WD and its drivers
+
+drv/*
+	User drivers. It helps to fulfill the semantic of wd.[ch] for
+	particular hardware
+
+test/*
+	Test applications to use the wrapdrive library

warpdrive

Signature · 1 line
~Randy
#23 lore raw

Re: [PATCH 7/7] vfio/sdmdev: add user sample

Quoted 115 lines · Randy Dunlap

Date: Sun, 2 Sep 2018 19:25:12 -0700
From: Randy Dunlap <[email protected]>
To: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Alex Williamson <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>
CC: [email protected]
Subject: Re: [PATCH 7/7] vfio/sdmdev: add user sample
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101
Thunderbird/52.9.1
Message-ID: <[email protected]>

Quoted 8 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

This is the sample code to demostrate how WrapDrive user application should be.

It contains:

1. wd.[ch], the common library to provide WrapDrive interface.

WarpDrive

Quoted 2 lines

2. drv/*, the user driver to access the hardware upon spimdev
3. test/*, the test application to use WrapDrive interface to access the

WarpDrive

Quoted 51 lines

hardware queue(s) of the accelerator.

The Hisilicon HIP08 ZIP accelerator is used in this sample.

---
 samples/warpdrive/AUTHORS              |   2 +
 samples/warpdrive/ChangeLog            |   1 +
 samples/warpdrive/Makefile.am          |   9 +
 samples/warpdrive/NEWS                 |   1 +
 samples/warpdrive/README               |  32 +++
 samples/warpdrive/autogen.sh           |   3 +
 samples/warpdrive/cleanup.sh           |  13 ++
 samples/warpdrive/configure.ac         |  52 +++++
 samples/warpdrive/drv/hisi_qm_udrv.c   | 223 ++++++++++++++++++
 samples/warpdrive/drv/hisi_qm_udrv.h   |  53 +++++
 samples/warpdrive/test/Makefile.am     |   7 +
 samples/warpdrive/test/comp_hw.h       |  23 ++
 samples/warpdrive/test/test_hisi_zip.c | 206 +++++++++++++++++
 samples/warpdrive/wd.c                 | 309 +++++++++++++++++++++++++
 samples/warpdrive/wd.h                 | 154 ++++++++++++
 samples/warpdrive/wd_adapter.c         |  74 ++++++
 samples/warpdrive/wd_adapter.h         |  43 ++++
 17 files changed, 1205 insertions(+)
 create mode 100644 samples/warpdrive/AUTHORS
 create mode 100644 samples/warpdrive/ChangeLog
 create mode 100644 samples/warpdrive/Makefile.am
 create mode 100644 samples/warpdrive/NEWS
 create mode 100644 samples/warpdrive/README
 create mode 100755 samples/warpdrive/autogen.sh
 create mode 100755 samples/warpdrive/cleanup.sh
 create mode 100644 samples/warpdrive/configure.ac
 create mode 100644 samples/warpdrive/drv/hisi_qm_udrv.c
 create mode 100644 samples/warpdrive/drv/hisi_qm_udrv.h
 create mode 100644 samples/warpdrive/test/Makefile.am
 create mode 100644 samples/warpdrive/test/comp_hw.h
 create mode 100644 samples/warpdrive/test/test_hisi_zip.c
 create mode 100644 samples/warpdrive/wd.c
 create mode 100644 samples/warpdrive/wd.h
 create mode 100644 samples/warpdrive/wd_adapter.c
 create mode 100644 samples/warpdrive/wd_adapter.h
samples/warpdrive/README new file +0 -0
new file mode 100644
index 000000000000..3adf66b112fc

samples/warpdrive/README line 6

Quoted 6 lines
samples/warpdrive/README new file +6 -0
@@ -0,0 +1,32 @@
+WD User Land Demonstration
+==========================
+
+This directory contains some applications and libraries to demonstrate how a
+
+WrapDrive application can be constructed.

WarpDrive

samples/warpdrive/README line 17

Quoted 11 lines
samples/warpdrive/README new file +11 -0
+
+
+As a demo, we try to make it simple and clear for understanding. It is not
+
+supposed to be used in business scenario.
+
+
+The directory contains the following elements:
+
+wd.[ch]
+	A demonstration WrapDrive fundamental library which wraps the basic

WarpDrive

samples/warpdrive/README line 18

Quoted 1 line
samples/warpdrive/README new file +1 -0
+	operations to the WrapDrive-ed device.

WarpDrive

Quoted 18 lines

samples/warpdrive/README line 31

Quoted 13 lines
samples/warpdrive/README new file +13 -0
+
+wd_adapter.[ch]
+	User driver adaptor for wd.[ch]
+
+wd_utils.[ch]
+	Some utitlities function used by WD and its drivers
+
+drv/*
+	User drivers. It helps to fulfill the semantic of wd.[ch] for
+	particular hardware
+
+test/*
+	Test applications to use the wrapdrive library

warpdrive

Signature · 1 line
~Randy

Thank you, will change them all in the coming version.

Signature · 14 lines
			-Kenneth(Hisilicon)

================================================================================
本邮件及其附件含有华为公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁
止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中
的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from HUAWEI,
which is intended only for the person or entity whose address is listed above.
Any use of the 
information contained herein in any way (including, but not limited to, total or
partial disclosure, reproduction, or dissemination) by persons other than the
intended 
recipient(s) is prohibited. If you receive this e-mail in error, please notify
the sender by phone or email immediately and delete it!
#24 lore raw
In reply to Kenneth Lee

Hi,

Quoted 68 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

WarpDrive is an accelerator framework to expose the hardware capabilities directly to the user space. It makes use of the exist vfio and vfio-mdev facilities. So the user application can send request and DMA to the hardware without interaction with the kernel. This removes the latency of syscall.

WarpDrive is the name for the whole framework. The component in kernel is called SDMDEV, Share Domain Mediated Device. Driver driver exposes its hardware resource by registering to SDMDEV as a VFIO-Mdev. So the user library of WarpDrive can access it via VFIO interface.

The patchset contains document for the detail. Please refer to it for more information.

This patchset is intended to be used with Jean Philippe Brucker's SVA patch [1], which enables not only IO side page fault, but also PASID support to IOMMU and VFIO.

With these features, WarpDrive can support non-pinned memory and multi-process in the same accelerator device. We tested it in our SoC integrated Accelerator (board ID: D06, Chip ID: HIP08). A reference work tree can be found here: [2].

But it is not mandatory. This patchset is tested in the latest mainline kernel without the SVA patches. So it supports only one process for each accelerator.

We have noticed the IOMMU aware mdev RFC announced recently [3].

The IOMMU aware mdev has similar idea but different intention comparing to WarpDrive. It intends to dedicate part of the hardware resource to a VM. And the design is supposed to be used with Scalable I/O Virtualization. While sdmdev is intended to share the hardware resource with a big amount of processes. It just requires the hardware supporting address translation per process (PCIE's PASID or ARM SMMU's substream ID).

But we don't see serious confliction on both design. We believe they can be normalized as one.

The patch 1 is document of the framework. The patch 2 and 3 add sdmdev support. The patch 4, 5 and 6 is drivers for Hislicon's ZIP Accelerator which is registered to both crypto and warpdrive(sdmdev) and can be used from kernel or user space at the same time. The patch 7 is a user space sample demonstrating how WarpDrive works.

Change History:
V2 changed from V1:
1. Change kernel framework name from SPIMDEV (Share Parent IOMMU Mdev) to SDMDEV (Share Domain Mdev).
2. Allocate Hardware Resource when a new mdev is created (While it is allocated when the mdev is openned)
3. Unmap pages from the shared domain when the sdmdev iommu group is detached. (This procedure is necessary, but missed in V1)
4. Update document accordingly.
5. Rebase to the latest kernel (4.19.0-rc1)

According the review comment on RFCv1, We did try to use dma-buf as back end of WarpDrive. It can work properly with the current solution [4], but it cannot make use of process's own memory address space directly. This is important to many acceleration scenario. So dma-buf will be taken as a backup alternative for noiommu scenario, it will be added in the future version.

Quoted 4 lines

Refernces:
[1] https://www.spinics.net/lists/kernel/msg2651481.html
[2] https://github.com/Kenneth-Lee/linux-kernel-warpdrive/tree/warpdrive-sva-v0.5
[3] https://lkml.org/lkml/2018/7/22/34

Please refer to the latest version posted here for discussion.

https://lkml.org/lkml/2018/8/30/107

Best regards,
Lu Baolu

#25 lore raw
Quoted 91 lines · Lu Baolu

Date: Mon, 3 Sep 2018 10:32:16 +0800
From: Lu Baolu <[email protected]>
To: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Alex Williamson <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Sanjay Kumar <[email protected]>
CC: [email protected], [email protected]
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101
Thunderbird/52.9.1
Message-ID: <[email protected]>

Hi,

Quoted 68 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

WarpDrive is an accelerator framework to expose the hardware capabilities directly to the user space. It makes use of the exist vfio and vfio-mdev facilities. So the user application can send request and DMA to the hardware without interaction with the kernel. This removes the latency of syscall.

WarpDrive is the name for the whole framework. The component in kernel is called SDMDEV, Share Domain Mediated Device. Driver driver exposes its hardware resource by registering to SDMDEV as a VFIO-Mdev. So the user library of WarpDrive can access it via VFIO interface.

The patchset contains document for the detail. Please refer to it for more information.

This patchset is intended to be used with Jean Philippe Brucker's SVA patch [1], which enables not only IO side page fault, but also PASID support to IOMMU and VFIO.

With these features, WarpDrive can support non-pinned memory and multi-process in the same accelerator device. We tested it in our SoC integrated Accelerator (board ID: D06, Chip ID: HIP08). A reference work tree can be found here: [2].

But it is not mandatory. This patchset is tested in the latest mainline kernel without the SVA patches. So it supports only one process for each accelerator.

We have noticed the IOMMU aware mdev RFC announced recently [3].

The IOMMU aware mdev has similar idea but different intention comparing to WarpDrive. It intends to dedicate part of the hardware resource to a VM. And the design is supposed to be used with Scalable I/O Virtualization. While sdmdev is intended to share the hardware resource with a big amount of processes. It just requires the hardware supporting address translation per process (PCIE's PASID or ARM SMMU's substream ID).

But we don't see serious confliction on both design. We believe they can be normalized as one.

The patch 1 is document of the framework. The patch 2 and 3 add sdmdev support. The patch 4, 5 and 6 is drivers for Hislicon's ZIP Accelerator which is registered to both crypto and warpdrive(sdmdev) and can be used from kernel or user space at the same time. The patch 7 is a user space sample demonstrating how WarpDrive works.

Change History:
V2 changed from V1:
1. Change kernel framework name from SPIMDEV (Share Parent IOMMU Mdev) to SDMDEV (Share Domain Mdev).
2. Allocate Hardware Resource when a new mdev is created (While it is allocated when the mdev is openned)
3. Unmap pages from the shared domain when the sdmdev iommu group is detached. (This procedure is necessary, but missed in V1)
4. Update document accordingly.
5. Rebase to the latest kernel (4.19.0-rc1)

According the review comment on RFCv1, We did try to use dma-buf as back end of WarpDrive. It can work properly with the current solution [4], but it cannot make use of process's own memory address space directly. This is important to many acceleration scenario. So dma-buf will be taken as a backup alternative for noiommu scenario, it will be added in the future version.

Quoted 8 lines
Quoted 4 lines

Refernces:
[1] https://www.spinics.net/lists/kernel/msg2651481.html
[2] https://github.com/Kenneth-Lee/linux-kernel-warpdrive/tree/warpdrive-sva-v0.5
[3] https://lkml.org/lkml/2018/7/22/34

Please refer to the latest version posted here for discussion.

https://lkml.org/lkml/2018/8/30/107

Sure. Thank you.

Quoted 3 lines

Best regards,
Lu Baolu

Signature · 19 lines
			-Kenneth(Hisilicon)

================================================================================
本邮件及其附件含有华为公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁
止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中
的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from HUAWEI,
which is intended only for the person or entity whose address is listed above.
Any use of the 
information contained herein in any way (including, but not limited to, total or
partial disclosure, reproduction, or dissemination) by persons other than the
intended 
recipient(s) is prohibited. If you receive this e-mail in error, please notify
the sender by phone or email immediately and delete it!

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#26 lore raw
In reply to Kenneth Lee
Quoted 38 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

WarpDrive is an accelerator framework to expose the hardware capabilities directly to the user space. It makes use of the exist vfio and vfio-mdev facilities. So the user application can send request and DMA to the hardware without interaction with the kernel. This removes the latency of syscall.

WarpDrive is the name for the whole framework. The component in kernel is called SDMDEV, Share Domain Mediated Device. Driver driver exposes its hardware resource by registering to SDMDEV as a VFIO-Mdev. So the user library of WarpDrive can access it via VFIO interface.

The patchset contains document for the detail. Please refer to it for more information.

This patchset is intended to be used with Jean Philippe Brucker's SVA patch [1], which enables not only IO side page fault, but also PASID support to IOMMU and VFIO.

With these features, WarpDrive can support non-pinned memory and multi-process in the same accelerator device. We tested it in our SoC integrated Accelerator (board ID: D06, Chip ID: HIP08). A reference work tree can be found here: [2].

But it is not mandatory. This patchset is tested in the latest mainline kernel without the SVA patches. So it supports only one process for each accelerator.

We have noticed the IOMMU aware mdev RFC announced recently [3].

The IOMMU aware mdev has similar idea but different intention comparing to WarpDrive. It intends to dedicate part of the hardware resource to a VM. And the design is supposed to be used with Scalable I/O Virtualization. While sdmdev is intended to share the hardware resource with a big amount of processes. It just requires the hardware supporting address translation per process (PCIE's PASID or ARM SMMU's substream ID).

Quoted 3 lines

But we don't see serious confliction on both design. We believe they can be normalized as one.

So once again i do not understand why you are trying to do things this way. Kernel already have tons of example of everything you want to do without a new framework. Moreover i believe you are confuse by VFIO. To me VFIO is for VM not to create general device driver frame work.

So here is your use case as i understand it. You have a device with a limited number of command queues (can be just one) and in some case it can support SVA/SVM (when hardware support it and it is not disabled). Final requirement is being able to schedule cmds from userspace without ioctl. All of this exists already exists upstream in few device drivers.

So here is how every body else is doing it. Please explain why this does not work.

1 Userspace open device file driver. Kernel device driver create
a context and associate it with on open. This context can be uniq to the process and can bind hardware resources (like a command queue) to the process.
2 Userspace bind/acquire a commands queue and initialize it with
an ioctl on the device file. Through that ioctl userspace can be inform wether either SVA/SVM works for the device. If SVA/ SVM works then kernel device driver bind the process to the device as part of this ioctl.
3 If SVM/SVA does not work userspace do an ioctl to create dma
buffer or something that does exactly the same thing.
4 Userspace mmap the command queue (mmap of the device file by
using informations gather at step 2)
5 Userspace can write commands into the queue it mapped 6 When userspace close the device file all resources are release
just like any existing device drivers.

Now if you want to create a device driver framework that expose a device file with generic API for all of the above steps fine. But it does not need to be part of VFIO whatsoever or explain why.

Note that if IOMMU is fully disabled you probably want to block userspace from being able to directly scheduling commands onto the hardware as it would allow userspace to DMA anywhere and thus would open the kernel to easy exploits. In this case you can still keeps the same API as above and use page fault tricks to valid commands written by userspace into fake commands ring. This will be as slow or maybe even slower than ioctl but at least it allows you to validate commands.

Cheers,
Jérôme

#27 lore raw
Quoted 43 lines · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:
Quoted 38 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

WarpDrive is an accelerator framework to expose the hardware capabilities directly to the user space. It makes use of the exist vfio and vfio-mdev facilities. So the user application can send request and DMA to the hardware without interaction with the kernel. This removes the latency of syscall.

WarpDrive is the name for the whole framework. The component in kernel is called SDMDEV, Share Domain Mediated Device. Driver driver exposes its hardware resource by registering to SDMDEV as a VFIO-Mdev. So the user library of WarpDrive can access it via VFIO interface.

The patchset contains document for the detail. Please refer to it for more information.

This patchset is intended to be used with Jean Philippe Brucker's SVA patch [1], which enables not only IO side page fault, but also PASID support to IOMMU and VFIO.

With these features, WarpDrive can support non-pinned memory and multi-process in the same accelerator device. We tested it in our SoC integrated Accelerator (board ID: D06, Chip ID: HIP08). A reference work tree can be found here: [2].

But it is not mandatory. This patchset is tested in the latest mainline kernel without the SVA patches. So it supports only one process for each accelerator.

We have noticed the IOMMU aware mdev RFC announced recently [3].

The IOMMU aware mdev has similar idea but different intention comparing to WarpDrive. It intends to dedicate part of the hardware resource to a VM. And the design is supposed to be used with Scalable I/O Virtualization. While sdmdev is intended to share the hardware resource with a big amount of processes. It just requires the hardware supporting address translation per process (PCIE's PASID or ARM SMMU's substream ID).

Quoted 3 lines

But we don't see serious confliction on both design. We believe they can be normalized as one.

Quoted 5 lines

So once again i do not understand why you are trying to do things this way. Kernel already have tons of example of everything you want to do without a new framework. Moreover i believe you are confuse by VFIO. To me VFIO is for VM not to create general device driver frame work.

VFIO is a userspace driver framework, the VM use case just happens to be a rather prolific one. VFIO was never intended to be solely a VM device interface and has several other userspace users, notably DPDK and SPDK, an NVMe backend in QEMU, a userspace NVMe driver, a ruby wrapper, and perhaps others that I'm not aware of. Whether vfio is appropriate interface here might certainly still be a debatable topic, but I would strongly disagree with your last sentence above. Thanks,

Alex

Quoted 45 lines

So here is your use case as i understand it. You have a device with a limited number of command queues (can be just one) and in some case it can support SVA/SVM (when hardware support it and it is not disabled). Final requirement is being able to schedule cmds from userspace without ioctl. All of this exists already exists upstream in few device drivers.

So here is how every body else is doing it. Please explain why this does not work.

1 Userspace open device file driver. Kernel device driver create
a context and associate it with on open. This context can be uniq to the process and can bind hardware resources (like a command queue) to the process.
2 Userspace bind/acquire a commands queue and initialize it with
an ioctl on the device file. Through that ioctl userspace can be inform wether either SVA/SVM works for the device. If SVA/ SVM works then kernel device driver bind the process to the device as part of this ioctl.
3 If SVM/SVA does not work userspace do an ioctl to create dma
buffer or something that does exactly the same thing.
4 Userspace mmap the command queue (mmap of the device file by
using informations gather at step 2)
5 Userspace can write commands into the queue it mapped 6 When userspace close the device file all resources are release
just like any existing device drivers.

Now if you want to create a device driver framework that expose a device file with generic API for all of the above steps fine. But it does not need to be part of VFIO whatsoever or explain why.

Note that if IOMMU is fully disabled you probably want to block userspace from being able to directly scheduling commands onto the hardware as it would allow userspace to DMA anywhere and thus would open the kernel to easy exploits. In this case you can still keeps the same API as above and use page fault tricks to valid commands written by userspace into fake commands ring. This will be as slow or maybe even slower than ioctl but at least it allows you to validate commands.

Cheers,
Jérôme

#28 lore raw
Quoted 78 lines · Alex Williamson

Date: Tue, 4 Sep 2018 10:15:09 -0600
From: Alex Williamson <[email protected]>
To: Jerome Glisse <[email protected]>
CC: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>, [email protected]
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
Message-ID: <[email protected]>

Quoted 43 lines · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:
Quoted 38 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

WarpDrive is an accelerator framework to expose the hardware capabilities directly to the user space. It makes use of the exist vfio and vfio-mdev facilities. So the user application can send request and DMA to the hardware without interaction with the kernel. This removes the latency of syscall.

WarpDrive is the name for the whole framework. The component in kernel is called SDMDEV, Share Domain Mediated Device. Driver driver exposes its hardware resource by registering to SDMDEV as a VFIO-Mdev. So the user library of WarpDrive can access it via VFIO interface.

The patchset contains document for the detail. Please refer to it for more information.

This patchset is intended to be used with Jean Philippe Brucker's SVA patch [1], which enables not only IO side page fault, but also PASID support to IOMMU and VFIO.

With these features, WarpDrive can support non-pinned memory and multi-process in the same accelerator device. We tested it in our SoC integrated Accelerator (board ID: D06, Chip ID: HIP08). A reference work tree can be found here: [2].

But it is not mandatory. This patchset is tested in the latest mainline kernel without the SVA patches. So it supports only one process for each accelerator.

We have noticed the IOMMU aware mdev RFC announced recently [3].

The IOMMU aware mdev has similar idea but different intention comparing to WarpDrive. It intends to dedicate part of the hardware resource to a VM. And the design is supposed to be used with Scalable I/O Virtualization. While sdmdev is intended to share the hardware resource with a big amount of processes. It just requires the hardware supporting address translation per process (PCIE's PASID or ARM SMMU's substream ID).

Quoted 3 lines

But we don't see serious confliction on both design. We believe they can be normalized as one.

Quoted 5 lines

So once again i do not understand why you are trying to do things this way. Kernel already have tons of example of everything you want to do without a new framework. Moreover i believe you are confuse by VFIO. To me VFIO is for VM not to create general device driver frame work.

VFIO is a userspace driver framework, the VM use case just happens to be a rather prolific one. VFIO was never intended to be solely a VM device interface and has several other userspace users, notably DPDK and SPDK, an NVMe backend in QEMU, a userspace NVMe driver, a ruby wrapper, and perhaps others that I'm not aware of. Whether vfio is appropriate interface here might certainly still be a debatable topic, but I would strongly disagree with your last sentence above. Thanks,

Quoted 2 lines

Alex

Yes, that is also my standpoint here.

Quoted 11 lines
Quoted 11 lines

So here is your use case as i understand it. You have a device with a limited number of command queues (can be just one) and in some case it can support SVA/SVM (when hardware support it and it is not disabled). Final requirement is being able to schedule cmds from userspace without ioctl. All of this exists already exists upstream in few device drivers.

So here is how every body else is doing it. Please explain why this does not work.

Quoted 16 lines
Quoted 8 lines

1 Userspace open device file driver. Kernel device driver create
a context and associate it with on open. This context can be uniq to the process and can bind hardware resources (like a command queue) to the process.
2 Userspace bind/acquire a commands queue and initialize it with
an ioctl on the device file. Through that ioctl userspace can be inform wether either SVA/SVM works for the device. If SVA/ SVM works then kernel device driver bind the process to the

Quoted 8 lines

device as part of this ioctl.
3 If SVM/SVA does not work userspace do an ioctl to create dma
buffer or something that does exactly the same thing.
4 Userspace mmap the command queue (mmap of the device file by
using informations gather at step 2)
5 Userspace can write commands into the queue it mapped 6 When userspace close the device file all resources are release
just like any existing device drivers.

Hi, Jerome,

Just one thing, as I said in the cover letter, dma-buf requires the application to use memory created by the driver for DMA. I did try the dma-buf way in WrapDrive (refer to [4] in the cover letter), it is a good backup for NOIOMMU mode or we cannot solve the problem in VFIO.

But, in many of my application scenario, the application already has some memory in hand, maybe allocated by the framework or libraries. Anyway, they don't get memory from my library, and they pass the poiter for data operation. And they may also have pointer in the buffer. Those pointer may be used by the accelerator. So I need hardware fully share the address space with the application. That is what dmabuf cannot do.

Quoted 18 lines
Quoted 18 lines

Now if you want to create a device driver framework that expose a device file with generic API for all of the above steps fine. But it does not need to be part of VFIO whatsoever or explain why.

Note that if IOMMU is fully disabled you probably want to block userspace from being able to directly scheduling commands onto the hardware as it would allow userspace to DMA anywhere and thus would open the kernel to easy exploits. In this case you can still keeps the same API as above and use page fault tricks to valid commands written by userspace into fake commands ring. This will be as slow or maybe even slower than ioctl but at least it allows you to validate commands.

Cheers,
Jérôme

Signature · 14 lines
			-Kenneth(Hisilicon)

================================================================================
本邮件及其附件含有华为公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁
止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中
的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from HUAWEI,
which is intended only for the person or entity whose address is listed above.
Any use of the 
information contained herein in any way (including, but not limited to, total or
partial disclosure, reproduction, or dissemination) by persons other than the
intended 
recipient(s) is prohibited. If you receive this e-mail in error, please notify
the sender by phone or email immediately and delete it!
#29 lore raw
Quoted 119 lines · Kenneth Lee
Quoted 78 lines · Alex Williamson

Date: Tue, 4 Sep 2018 10:15:09 -0600
From: Alex Williamson <[email protected]>
To: Jerome Glisse <[email protected]>
CC: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>, [email protected]
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
Message-ID: <[email protected]>

Quoted 43 lines · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:
Quoted 38 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

WarpDrive is an accelerator framework to expose the hardware capabilities directly to the user space. It makes use of the exist vfio and vfio-mdev facilities. So the user application can send request and DMA to the hardware without interaction with the kernel. This removes the latency of syscall.

WarpDrive is the name for the whole framework. The component in kernel is called SDMDEV, Share Domain Mediated Device. Driver driver exposes its hardware resource by registering to SDMDEV as a VFIO-Mdev. So the user library of WarpDrive can access it via VFIO interface.

The patchset contains document for the detail. Please refer to it for more information.

This patchset is intended to be used with Jean Philippe Brucker's SVA patch [1], which enables not only IO side page fault, but also PASID support to IOMMU and VFIO.

With these features, WarpDrive can support non-pinned memory and multi-process in the same accelerator device. We tested it in our SoC integrated Accelerator (board ID: D06, Chip ID: HIP08). A reference work tree can be found here: [2].

But it is not mandatory. This patchset is tested in the latest mainline kernel without the SVA patches. So it supports only one process for each accelerator.

We have noticed the IOMMU aware mdev RFC announced recently [3].

The IOMMU aware mdev has similar idea but different intention comparing to WarpDrive. It intends to dedicate part of the hardware resource to a VM. And the design is supposed to be used with Scalable I/O Virtualization. While sdmdev is intended to share the hardware resource with a big amount of processes. It just requires the hardware supporting address translation per process (PCIE's PASID or ARM SMMU's substream ID).

Quoted 3 lines

But we don't see serious confliction on both design. We believe they can be normalized as one.

Quoted 5 lines

So once again i do not understand why you are trying to do things this way. Kernel already have tons of example of everything you want to do without a new framework. Moreover i believe you are confuse by VFIO. To me VFIO is for VM not to create general device driver frame work.

VFIO is a userspace driver framework, the VM use case just happens to be a rather prolific one. VFIO was never intended to be solely a VM device interface and has several other userspace users, notably DPDK and SPDK, an NVMe backend in QEMU, a userspace NVMe driver, a ruby wrapper, and perhaps others that I'm not aware of. Whether vfio is appropriate interface here might certainly still be a debatable topic, but I would strongly disagree with your last sentence above. Thanks,

Quoted 2 lines

Alex

Yes, that is also my standpoint here.

Quoted 11 lines
Quoted 11 lines

So here is your use case as i understand it. You have a device with a limited number of command queues (can be just one) and in some case it can support SVA/SVM (when hardware support it and it is not disabled). Final requirement is being able to schedule cmds from userspace without ioctl. All of this exists already exists upstream in few device drivers.

So here is how every body else is doing it. Please explain why this does not work.

Quoted 16 lines
Quoted 8 lines

1 Userspace open device file driver. Kernel device driver create
a context and associate it with on open. This context can be uniq to the process and can bind hardware resources (like a command queue) to the process.
2 Userspace bind/acquire a commands queue and initialize it with
an ioctl on the device file. Through that ioctl userspace can be inform wether either SVA/SVM works for the device. If SVA/ SVM works then kernel device driver bind the process to the

Quoted 8 lines

device as part of this ioctl.
3 If SVM/SVA does not work userspace do an ioctl to create dma
buffer or something that does exactly the same thing.
4 Userspace mmap the command queue (mmap of the device file by
using informations gather at step 2)
5 Userspace can write commands into the queue it mapped 6 When userspace close the device file all resources are release
just like any existing device drivers.

Hi, Jerome,

Just one thing, as I said in the cover letter, dma-buf requires the application to use memory created by the driver for DMA. I did try the dma-buf way in WrapDrive (refer to [4] in the cover letter), it is a good backup for NOIOMMU mode or we cannot solve the problem in VFIO.

Quoted 6 lines

But, in many of my application scenario, the application already has some memory in hand, maybe allocated by the framework or libraries. Anyway, they don't get memory from my library, and they pass the poiter for data operation. And they may also have pointer in the buffer. Those pointer may be used by the accelerator. So I need hardware fully share the address space with the application. That is what dmabuf cannot do.

dmabuf can do that ... it is call uptr you can look at i915 for instance. Still this does not answer my question above, why do you need to be in VFIO to do any of the above thing ? Kernel has tons of examples that does all of the above and are not in VFIO (including usinng existing user pointer with device).

Cheers,
Jérôme

#30 lore raw
Quoted 146 lines · Jerome Glisse

Date: Thu, 6 Sep 2018 09:31:33 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <[email protected]>
CC: Alex Williamson <[email protected]>, Kenneth Lee
<[email protected]>, Jonathan Corbet <[email protected]>, Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>, [email protected]
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mutt/1.10.0 (2018-05-17)
Message-ID: <[email protected]>

Quoted 119 lines · Kenneth Lee
Quoted 78 lines · Alex Williamson

Date: Tue, 4 Sep 2018 10:15:09 -0600
From: Alex Williamson <[email protected]>
To: Jerome Glisse <[email protected]>
CC: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>, [email protected]
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
Message-ID: <[email protected]>

Quoted 43 lines · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:
Quoted 38 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

WarpDrive is an accelerator framework to expose the hardware capabilities directly to the user space. It makes use of the exist vfio and vfio-mdev facilities. So the user application can send request and DMA to the hardware without interaction with the kernel. This removes the latency of syscall.

WarpDrive is the name for the whole framework. The component in kernel is called SDMDEV, Share Domain Mediated Device. Driver driver exposes its hardware resource by registering to SDMDEV as a VFIO-Mdev. So the user library of WarpDrive can access it via VFIO interface.

The patchset contains document for the detail. Please refer to it for more information.

This patchset is intended to be used with Jean Philippe Brucker's SVA patch [1], which enables not only IO side page fault, but also PASID support to IOMMU and VFIO.

With these features, WarpDrive can support non-pinned memory and multi-process in the same accelerator device. We tested it in our SoC integrated Accelerator (board ID: D06, Chip ID: HIP08). A reference work tree can be found here: [2].

But it is not mandatory. This patchset is tested in the latest mainline kernel without the SVA patches. So it supports only one process for each accelerator.

We have noticed the IOMMU aware mdev RFC announced recently [3].

The IOMMU aware mdev has similar idea but different intention comparing to WarpDrive. It intends to dedicate part of the hardware resource to a VM. And the design is supposed to be used with Scalable I/O Virtualization. While sdmdev is intended to share the hardware resource with a big amount of processes. It just requires the hardware supporting address translation per process (PCIE's PASID or ARM SMMU's substream ID).

Quoted 3 lines

But we don't see serious confliction on both design. We believe they can be normalized as one.

Quoted 5 lines

So once again i do not understand why you are trying to do things this way. Kernel already have tons of example of everything you want to do without a new framework. Moreover i believe you are confuse by VFIO. To me VFIO is for VM not to create general device driver frame work.

VFIO is a userspace driver framework, the VM use case just happens to be a rather prolific one. VFIO was never intended to be solely a VM device interface and has several other userspace users, notably DPDK and SPDK, an NVMe backend in QEMU, a userspace NVMe driver, a ruby wrapper, and perhaps others that I'm not aware of. Whether vfio is appropriate interface here might certainly still be a debatable topic, but I would strongly disagree with your last sentence above. Thanks,

Quoted 2 lines

Alex

Yes, that is also my standpoint here.

Quoted 11 lines
Quoted 11 lines

So here is your use case as i understand it. You have a device with a limited number of command queues (can be just one) and in some case it can support SVA/SVM (when hardware support it and it is not disabled). Final requirement is being able to schedule cmds from userspace without ioctl. All of this exists already exists upstream in few device drivers.

So here is how every body else is doing it. Please explain why this does not work.

Quoted 16 lines
Quoted 8 lines

1 Userspace open device file driver. Kernel device driver create
a context and associate it with on open. This context can be uniq to the process and can bind hardware resources (like a command queue) to the process.
2 Userspace bind/acquire a commands queue and initialize it with
an ioctl on the device file. Through that ioctl userspace can be inform wether either SVA/SVM works for the device. If SVA/ SVM works then kernel device driver bind the process to the

Quoted 8 lines

device as part of this ioctl.
3 If SVM/SVA does not work userspace do an ioctl to create dma
buffer or something that does exactly the same thing.
4 Userspace mmap the command queue (mmap of the device file by
using informations gather at step 2)
5 Userspace can write commands into the queue it mapped 6 When userspace close the device file all resources are release
just like any existing device drivers.

Hi, Jerome,

Just one thing, as I said in the cover letter, dma-buf requires the application to use memory created by the driver for DMA. I did try the dma-buf way in WrapDrive (refer to [4] in the cover letter), it is a good backup for NOIOMMU mode or we cannot solve the problem in VFIO.

Quoted 6 lines

But, in many of my application scenario, the application already has some memory in hand, maybe allocated by the framework or libraries. Anyway, they don't get memory from my library, and they pass the poiter for data operation. And they may also have pointer in the buffer. Those pointer may be used by the accelerator. So I need hardware fully share the address space with the application. That is what dmabuf cannot do.

Quoted 8 lines

dmabuf can do that ... it is call uptr you can look at i915 for instance. Still this does not answer my question above, why do you need to be in VFIO to do any of the above thing ? Kernel has tons of examples that does all of the above and are not in VFIO (including usinng existing user pointer with device).

Cheers,
Jérôme

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

And personally, I believe the maturity and correctness of a framework are driven by applications. Now the problem in accelerator world is that we don't have a direction. If we believe the requirement is right, the method itself is not a big problem in the end. We just need to let people have a unify platform to share their work together.

Cheers

Signature · 19 lines
			-Kenneth(Hisilicon)

================================================================================
本邮件及其附件含有华为公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁
止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中
的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from HUAWEI,
which is intended only for the person or entity whose address is listed above.
Any use of the 
information contained herein in any way (including, but not limited to, total or
partial disclosure, reproduction, or dissemination) by persons other than the
intended 
recipient(s) is prohibited. If you receive this e-mail in error, please notify
the sender by phone or email immediately and delete it!

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#31 lore raw
Quoted 156 lines · Kenneth Lee
Quoted 146 lines · Jerome Glisse

Date: Thu, 6 Sep 2018 09:31:33 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <liguozhu-C8/M+/[email protected]>
CC: Alex Williamson <[email protected]>, Kenneth Lee
<[email protected]>, Jonathan Corbet <[email protected]>, Herbert Xu <[email protected]>, "David S . Miller" <davem-fT/PcQaiUtIeIZ0/[email protected]>, Joerg Roedel <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <wangzhou1-C8/M+/[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <pombredanne-od1rfyK75/[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org, [email protected], linux-accelerators-uLR06cmDAlY/[email protected], Lu Baolu <baolu.lu-VuQAYsv1563Yd54FQh9/[email protected]>, Sanjay Kumar <[email protected]>, [email protected]
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mutt/1.10.0 (2018-05-17)
Message-ID: <[email protected]>

Quoted 119 lines · Kenneth Lee
Quoted 78 lines · Alex Williamson

Date: Tue, 4 Sep 2018 10:15:09 -0600
From: Alex Williamson <[email protected]>
To: Jerome Glisse <[email protected]>
CC: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <davem-fT/PcQaiUtIeIZ0/[email protected]>, Joerg Roedel <[email protected]>, Kenneth Lee <liguozhu-C8/M+/[email protected]>, Hao Fang <[email protected]>, Zhou Wang <wangzhou1-C8/M+/[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <pombredanne-od1rfyK75/[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org, [email protected], linux-accelerators-uLR06cmDAlY/[email protected], Lu Baolu <baolu.lu-VuQAYsv1563Yd54FQh9/[email protected]>, Sanjay Kumar <[email protected]>, [email protected]
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
Message-ID: <20180904101509.62314b67-1yVPhWWZRC1BDLzU/[email protected]>

Quoted 43 lines · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse…
Quoted 38 lines · Kenneth Lee

From: Kenneth Lee <liguozhu-C8/M+/[email protected]>

WarpDrive is an accelerator framework to expose the hardware capabilities directly to the user space. It makes use of the exist vfio and vfio-mdev facilities. So the user application can send request and DMA to the hardware without interaction with the kernel. This removes the latency of syscall.

WarpDrive is the name for the whole framework. The component in kernel is called SDMDEV, Share Domain Mediated Device. Driver driver exposes its hardware resource by registering to SDMDEV as a VFIO-Mdev. So the user library of WarpDrive can access it via VFIO interface.

The patchset contains document for the detail. Please refer to it for more information.

This patchset is intended to be used with Jean Philippe Brucker's SVA patch [1], which enables not only IO side page fault, but also PASID support to IOMMU and VFIO.

With these features, WarpDrive can support non-pinned memory and multi-process in the same accelerator device. We tested it in our SoC integrated Accelerator (board ID: D06, Chip ID: HIP08). A reference work tree can be found here: [2].

But it is not mandatory. This patchset is tested in the latest mainline kernel without the SVA patches. So it supports only one process for each accelerator.

We have noticed the IOMMU aware mdev RFC announced recently [3].

The IOMMU aware mdev has similar idea but different intention comparing to WarpDrive. It intends to dedicate part of the hardware resource to a VM. And the design is supposed to be used with Scalable I/O Virtualization. While sdmdev is intended to share the hardware resource with a big amount of processes. It just requires the hardware supporting address translation per process (PCIE's PASID or ARM SMMU's substream ID).

Quoted 3 lines

But we don't see serious confliction on both design. We believe they can be normalized as one.

Quoted 5 lines

So once again i do not understand why you are trying to do things this way. Kernel already have tons of example of everything you want to do without a new framework. Moreover i believe you are confuse by VFIO. To me VFIO is for VM not to create general device driver frame work.

VFIO is a userspace driver framework, the VM use case just happens to be a rather prolific one. VFIO was never intended to be solely a VM device interface and has several other userspace users, notably DPDK and SPDK, an NVMe backend in QEMU, a userspace NVMe driver, a ruby wrapper, and perhaps others that I'm not aware of. Whether vfio is appropriate interface here might certainly still be a debatable topic, but I would strongly disagree with your last sentence above. Thanks,

Quoted 2 lines

Alex

Yes, that is also my standpoint here.

Quoted 11 lines
Quoted 11 lines

So here is your use case as i understand it. You have a device with a limited number of command queues (can be just one) and in some case it can support SVA/SVM (when hardware support it and it is not disabled). Final requirement is being able to schedule cmds from userspace without ioctl. All of this exists already exists upstream in few device drivers.

So here is how every body else is doing it. Please explain why this does not work.

Quoted 16 lines
Quoted 8 lines

1 Userspace open device file driver. Kernel device driver create
a context and associate it with on open. This context can be uniq to the process and can bind hardware resources (like a command queue) to the process.
2 Userspace bind/acquire a commands queue and initialize it with
an ioctl on the device file. Through that ioctl userspace can be inform wether either SVA/SVM works for the device. If SVA/ SVM works then kernel device driver bind the process to the

Quoted 8 lines

device as part of this ioctl.
3 If SVM/SVA does not work userspace do an ioctl to create dma
buffer or something that does exactly the same thing.
4 Userspace mmap the command queue (mmap of the device file by
using informations gather at step 2)
5 Userspace can write commands into the queue it mapped 6 When userspace close the device file all resources are release
just like any existing device drivers.

Hi, Jerome,

Just one thing, as I said in the cover letter, dma-buf requires the application to use memory created by the driver for DMA. I did try the dma-buf way in WrapDrive (refer to [4] in the cover letter), it is a good backup for NOIOMMU mode or we cannot solve the problem in VFIO.

Quoted 6 lines

But, in many of my application scenario, the application already has some memory in hand, maybe allocated by the framework or libraries. Anyway, they don't get memory from my library, and they pass the poiter for data operation. And they may also have pointer in the buffer. Those pointer may be used by the accelerator. So I need hardware fully share the address space with the application. That is what dmabuf cannot do.

Quoted 8 lines

dmabuf can do that ... it is call uptr you can look at i915 for instance. Still this does not answer my question above, why do you need to be in VFIO to do any of the above thing ? Kernel has tons of examples that does all of the above and are not in VFIO (including usinng existing user pointer with device).

Cheers,
Jérôme

Quoted 6 lines

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

You were not looking at right place see drivers/gpu/drm/i915/i915_gem_userptr.c It does GUP and create GEM object AFAICR you can wrap that GEM object into a dma buffer object.

Quoted 11 lines

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

Because tons of other drivers already do all of the above outside VFIO. Many driver have a sizeable userspace side to them (anything with ioctl do) so they can be construded as userspace driver too.

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

So i do not see any reasons to do anything you want inside VFIO. All you want to do can be done outside as easily. Moreover it would be better if you define clearly each scenario because from where i sit it looks like you are opening the door wide open to userspace to DMA anywhere when IOMMU is disabled.

When IOMMU is disabled you can _not_ expose command queue to userspace unless your device has its own page table and all commands are relative to that page table and the device page table is populated by kernel driver in secure way (ie by checking that what is populated can be access).

I do not believe your example device to have such page table nor do i see a fallback path when IOMMU is disabled that force user to do ioctl for each commands.

Yes i understand that you target SVA/SVM but still you claim to support non SVA/SVM. The point is that userspace can not be trusted if you want to have random program use your device. I am pretty sure that all user of VFIO are trusted process (like QEMU).

Finaly i am convince that the IOMMU grouping stuff related to VFIO is useless for your usecase. I really do not see the point of that, it does complicate things for you for no reasons AFAICT.

Quoted 6 lines

And personally, I believe the maturity and correctness of a framework are driven by applications. Now the problem in accelerator world is that we don't have a direction. If we believe the requirement is right, the method itself is not a big problem in the end. We just need to let people have a unify platform to share their work together.

I am not against that but it seems to me that all you want to do is only a matter of simplifying discovery of such devices and sharing few common ioctl (DMA mapping, creating command queue, managing command queue, ...) and again for all this i do not see the point of doing this under VFIO.

Cheers,
Jérôme

#32 lore raw
Quoted 9 lines · Jerome Glisse

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

If the IOMMU is disabled (not exactly a kernel default by the way, I think most IOMMU drivers enable it by default), your userspace driver can't bypass DMA isolation by accident. It just won't be allowed to access the device. VFIO requires an IOMMU unless the admin forces the NOIOMMU mode with the "enable_unsafe_noiommu_mode" module parameter, and the userspace explicitly asks for it with VFIO_NOIOMMU_IOMMU, which taints the kernel. Not for production. A normal userspace driver that uses VFIO can only do DMA to its own memory.

Thanks,
Jean

#33 lore raw
Quoted 11 lines · Jean-Philippe Brucker
Quoted 9 lines · Jerome Glisse

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

Quoted 9 lines

If the IOMMU is disabled (not exactly a kernel default by the way, I think most IOMMU drivers enable it by default), your userspace driver can't bypass DMA isolation by accident. It just won't be allowed to access the device. VFIO requires an IOMMU unless the admin forces the NOIOMMU mode with the "enable_unsafe_noiommu_mode" module parameter, and the userspace explicitly asks for it with VFIO_NOIOMMU_IOMMU, which taints the kernel. Not for production. A normal userspace driver that uses VFIO can only do DMA to its own memory.

Didn't know about VFIO check, which is a sane thing. On Intel IOMMU is disabled by default (see INTEL_IOMMU_DEFAULT_ON Kconfig option). I am pretty sure it use to be the same for AMD but maybe it is now enabled by default.

Cheers,
Jérôme

#34 lore raw
In reply to Jerome Glisse
Quoted 182 lines · Jerome Glisse

Date: Fri, 7 Sep 2018 12:53:06 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <[email protected]>
CC: Kenneth Lee <[email protected]>, Herbert Xu
<[email protected]>, [email protected], Jonathan Corbet <[email protected]>, Greg Kroah-Hartman <[email protected]>, Joerg Roedel <[email protected]>, [email protected], Sanjay Kumar <[email protected]>, Hao Fang <[email protected]>, [email protected], [email protected], [email protected], Alex Williamson <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], Zhou Wang <[email protected]>, Philippe Ombredanne <[email protected]>, Zaibo Xu <[email protected]>, "David S . Miller" <[email protected]>, [email protected], Lu Baolu <[email protected]>
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mutt/1.10.0 (2018-05-17)
Message-ID: <[email protected]>

Quoted 156 lines · Kenneth Lee
Quoted 146 lines · Jerome Glisse

Date: Thu, 6 Sep 2018 09:31:33 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <[email protected]>
CC: Alex Williamson <[email protected]>, Kenneth Lee
<[email protected]>, Jonathan Corbet <[email protected]>, Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>, [email protected]
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mutt/1.10.0 (2018-05-17)
Message-ID: <[email protected]>

Quoted 119 lines · Kenneth Lee
Quoted 78 lines · Alex Williamson

Date: Tue, 4 Sep 2018 10:15:09 -0600
From: Alex Williamson <[email protected]>
To: Jerome Glisse <[email protected]>
CC: Kenneth Lee <[email protected]>, Jonathan Corbet <[email protected]>,
Herbert Xu <[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>, [email protected]
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
Message-ID: <[email protected]>

Quoted 43 lines · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:
Quoted 38 lines · Kenneth Lee

From: Kenneth Lee <[email protected]>

WarpDrive is an accelerator framework to expose the hardware capabilities directly to the user space. It makes use of the exist vfio and vfio-mdev facilities. So the user application can send request and DMA to the hardware without interaction with the kernel. This removes the latency of syscall.

WarpDrive is the name for the whole framework. The component in kernel is called SDMDEV, Share Domain Mediated Device. Driver driver exposes its hardware resource by registering to SDMDEV as a VFIO-Mdev. So the user library of WarpDrive can access it via VFIO interface.

The patchset contains document for the detail. Please refer to it for more information.

This patchset is intended to be used with Jean Philippe Brucker's SVA patch [1], which enables not only IO side page fault, but also PASID support to IOMMU and VFIO.

With these features, WarpDrive can support non-pinned memory and multi-process in the same accelerator device. We tested it in our SoC integrated Accelerator (board ID: D06, Chip ID: HIP08). A reference work tree can be found here: [2].

But it is not mandatory. This patchset is tested in the latest mainline kernel without the SVA patches. So it supports only one process for each accelerator.

We have noticed the IOMMU aware mdev RFC announced recently [3].

The IOMMU aware mdev has similar idea but different intention comparing to WarpDrive. It intends to dedicate part of the hardware resource to a VM. And the design is supposed to be used with Scalable I/O Virtualization. While sdmdev is intended to share the hardware resource with a big amount of processes. It just requires the hardware supporting address translation per process (PCIE's PASID or ARM SMMU's substream ID).

Quoted 3 lines

But we don't see serious confliction on both design. We believe they can be normalized as one.

Quoted 5 lines

So once again i do not understand why you are trying to do things this way. Kernel already have tons of example of everything you want to do without a new framework. Moreover i believe you are confuse by VFIO. To me VFIO is for VM not to create general device driver frame work.

VFIO is a userspace driver framework, the VM use case just happens to be a rather prolific one. VFIO was never intended to be solely a VM device interface and has several other userspace users, notably DPDK and SPDK, an NVMe backend in QEMU, a userspace NVMe driver, a ruby wrapper, and perhaps others that I'm not aware of. Whether vfio is appropriate interface here might certainly still be a debatable topic, but I would strongly disagree with your last sentence above. Thanks,

Quoted 2 lines

Alex

Yes, that is also my standpoint here.

Quoted 11 lines
Quoted 11 lines

So here is your use case as i understand it. You have a device with a limited number of command queues (can be just one) and in some case it can support SVA/SVM (when hardware support it and it is not disabled). Final requirement is being able to schedule cmds from userspace without ioctl. All of this exists already exists upstream in few device drivers.

So here is how every body else is doing it. Please explain why this does not work.

Quoted 16 lines
Quoted 8 lines

1 Userspace open device file driver. Kernel device driver create
a context and associate it with on open. This context can be uniq to the process and can bind hardware resources (like a command queue) to the process.
2 Userspace bind/acquire a commands queue and initialize it with
an ioctl on the device file. Through that ioctl userspace can be inform wether either SVA/SVM works for the device. If SVA/ SVM works then kernel device driver bind the process to the

Quoted 8 lines

device as part of this ioctl.
3 If SVM/SVA does not work userspace do an ioctl to create dma
buffer or something that does exactly the same thing.
4 Userspace mmap the command queue (mmap of the device file by
using informations gather at step 2)
5 Userspace can write commands into the queue it mapped 6 When userspace close the device file all resources are release
just like any existing device drivers.

Hi, Jerome,

Just one thing, as I said in the cover letter, dma-buf requires the application to use memory created by the driver for DMA. I did try the dma-buf way in WrapDrive (refer to [4] in the cover letter), it is a good backup for NOIOMMU mode or we cannot solve the problem in VFIO.

Quoted 6 lines

But, in many of my application scenario, the application already has some memory in hand, maybe allocated by the framework or libraries. Anyway, they don't get memory from my library, and they pass the poiter for data operation. And they may also have pointer in the buffer. Those pointer may be used by the accelerator. So I need hardware fully share the address space with the application. That is what dmabuf cannot do.

Quoted 8 lines

dmabuf can do that ... it is call uptr you can look at i915 for instance. Still this does not answer my question above, why do you need to be in VFIO to do any of the above thing ? Kernel has tons of examples that does all of the above and are not in VFIO (including usinng existing user pointer with device).

Cheers,
Jérôme

Quoted 6 lines

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

Quoted 4 lines

You were not looking at right place see drivers/gpu/drm/i915/i915_gem_userptr.c It does GUP and create GEM object AFAICR you can wrap that GEM object into a dma buffer object.

Thank you for directing me to this implementation. It is interesting:).

But it is not yet solve my problem. If I understand it right, the userptr in i915 do the following:

1. The user process sets a user pointer with size to the kernel via ioctl.
2. The kernel wraps it as a dma-buf and keeps the process's mm for further reference.
3. The user pages are allocated, GUPed or DMA mapped to the device. So the data can be shared between the user space and the hardware.

But my scenario is:

1. The user process has some data in the user space, pointed by a pointer, say ptr1. And within the memory, there may be some other pointers, let's say one of them is ptr2.
2. Now I need to assign ptr1 *directly* to the hardware MMIO space. And the hardware must refer ptr1 and ptr2 *directly* for data.

Userptr lets the hardware and process share the same memory space. But I need them to share the same *address space*. So IOMMU is a MUST for WarpDrive, NOIOMMU mode, as Jean said, is just for verifying some of the procedure is OK.

Quoted 9 lines
Quoted 9 lines

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

Quoted 7 lines
Quoted 2 lines

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

Because tons of other drivers already do all of the above outside VFIO. Many driver have a sizeable userspace side to them (anything with ioctl do) so they can be construded as userspace driver too.

Ignoring if there are *tons* of drivers are doing that;), even I do the same as i915 and solve the address space problem. And if I don't need to with VFIO, why should I spend so much effort to do it again?

Quoted 10 lines

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

But as Alex explained, VFIO is not simply used by VM. So it need not to have all stuffs as a driver in host system. And I do need to share the user space as DMA buffer to the hardware. And I can get it with just a little update, then it can service me perfectly. I don't understand why I should choose a long route.

Quoted 21 lines

So i do not see any reasons to do anything you want inside VFIO. All you want to do can be done outside as easily. Moreover it would be better if you define clearly each scenario because from where i sit it looks like you are opening the door wide open to userspace to DMA anywhere when IOMMU is disabled.

When IOMMU is disabled you can _not_ expose command queue to userspace unless your device has its own page table and all commands are relative to that page table and the device page table is populated by kernel driver in secure way (ie by checking that what is populated can be access).

I do not believe your example device to have such page table nor do i see a fallback path when IOMMU is disabled that force user to do ioctl for each commands.

Yes i understand that you target SVA/SVM but still you claim to support non SVA/SVM. The point is that userspace can not be trusted if you want to have random program use your device. I am pretty sure that all user of VFIO are trusted process (like QEMU).

Quoted 3 lines

Finaly i am convince that the IOMMU grouping stuff related to VFIO is useless for your usecase. I really do not see the point of that, it does complicate things for you for no reasons AFAICT.

Indeed, I don't like the group thing. I believe VFIO's maintains would not like it very much either;). But the problem is, the group reflects to the same IOMMU(unit), which may shared with other devices. It is a security problem. I cannot ignore it. I have to take it into account event I don't use VFIO.

Quoted 9 lines
Quoted 8 lines

And personally, I believe the maturity and correctness of a framework are driven by applications. Now the problem in accelerator world is that we don't have a direction. If we believe the requirement is right, the method itself is not a big problem in the end. We just need to let people have a unify platform to share their work together.

Quoted 4 lines

I am not against that but it seems to me that all you want to do is only a matter of simplifying discovery of such devices and sharing few common ioctl (DMA mapping, creating command queue, managing command queue, ...) and again for all this i do not see the point of doing this under VFIO.

It is not a problem of device management, it is a problem of sharing address space.

Cheers,

Quoted 4 lines

Cheers,
Jérôme

Signature · 1 line
			-Kenneth(Hisilicon)
#35 lore raw
Quoted 7 lines · Kenneth Lee
Quoted 6 lines · Jerome Glisse
Quoted 5 lines · Kenneth Lee
Quoted 4 lines · Jerome Glisse
Quoted 3 lines · Kenneth Lee
Quoted 2 lines · Alex Williamson
Quoted 1 line · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:

On Mon, Sep 03, 2018 at 08:51:57AM +0800, Kenneth Lee wrote:

[...]

Quoted 31 lines
Quoted 11 lines
Quoted 6 lines

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

You were not looking at right place see drivers/gpu/drm/i915/i915_gem_userptr.c It does GUP and create GEM object AFAICR you can wrap that GEM object into a dma buffer object.

Thank you for directing me to this implementation. It is interesting:).

But it is not yet solve my problem. If I understand it right, the userptr in i915 do the following:

1. The user process sets a user pointer with size to the kernel via ioctl.
2. The kernel wraps it as a dma-buf and keeps the process's mm for further reference.
3. The user pages are allocated, GUPed or DMA mapped to the device. So the data can be shared between the user space and the hardware.

But my scenario is:

1. The user process has some data in the user space, pointed by a pointer, say ptr1. And within the memory, there may be some other pointers, let's say one of them is ptr2.
2. Now I need to assign ptr1 *directly* to the hardware MMIO space. And the hardware must refer ptr1 and ptr2 *directly* for data.

Quoted 3 lines

Userptr lets the hardware and process share the same memory space. But I need them to share the same *address space*. So IOMMU is a MUST for WarpDrive, NOIOMMU mode, as Jean said, is just for verifying some of the procedure is OK.

So to be 100% clear should we _ignore_ the non SVA/SVM case ? If so then wait for necessary SVA/SVM to land and do warp drive without non SVA/SVM path.

If you still want non SVA/SVM path what you want to do only works if both ptr1 and ptr2 are in a range that is DMA mapped to the device (moreover you need DMA address to match process address which is not an easy feat).

Now even if you only want SVA/SVM, i do not see what is the point of doing this inside VFIO. AMD GPU driver does not and there would be no benefit for them to be there. Well a AMD VFIO mdev device driver for QEMU guest might be useful but they have SVIO IIRC.

For SVA/SVM your usage model is:

Setup:
- user space create a warp drive context for the process
- user space create a device specific context for the process
- user space create a user space command queue for the device
- user space bind command queue

At this point the kernel driver has bound the process address space to the device with a command queue and userspace

Usage:
- user space schedule work and call appropriate flush/update ioctl from time to time. Might be optional depends on the hardware, but probably a good idea to enforce so that kernel can unbind the command queue to bind another process command queue.
...

Cleanup:
- user space unbind command queue
- user space destroy device specific context
- user space destroy warp drive context
All the above can be implicit when closing the device file.

So again in the above model i do not see anywhere something from VFIO that would benefit this model.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

Quoted 8 lines
Quoted 4 lines

Because tons of other drivers already do all of the above outside VFIO. Many driver have a sizeable userspace side to them (anything with ioctl do) so they can be construded as userspace driver too.

Ignoring if there are *tons* of drivers are doing that;), even I do the same as i915 and solve the address space problem. And if I don't need to with VFIO, why should I spend so much effort to do it again?

Because you do not need any code from VFIO, nor do you need to reinvent things. If non SVA/SVM matters to you then use dma buffer. If not then i do not see anything in VFIO that you need.

Quoted 11 lines
Quoted 10 lines

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

Quoted 4 lines

But as Alex explained, VFIO is not simply used by VM. So it need not to have all stuffs as a driver in host system. And I do need to share the user space as DMA buffer to the hardware. And I can get it with just a little update, then it can service me perfectly. I don't understand why I should choose a long route.

Again this is not the long route i do not see anything in VFIO that benefit you in the SVA/SVM case. A basic character device driver can do that.

Quoted 21 lines
Quoted 21 lines

So i do not see any reasons to do anything you want inside VFIO. All you want to do can be done outside as easily. Moreover it would be better if you define clearly each scenario because from where i sit it looks like you are opening the door wide open to userspace to DMA anywhere when IOMMU is disabled.

When IOMMU is disabled you can _not_ expose command queue to userspace unless your device has its own page table and all commands are relative to that page table and the device page table is populated by kernel driver in secure way (ie by checking that what is populated can be access).

I do not believe your example device to have such page table nor do i see a fallback path when IOMMU is disabled that force user to do ioctl for each commands.

Yes i understand that you target SVA/SVM but still you claim to support non SVA/SVM. The point is that userspace can not be trusted if you want to have random program use your device. I am pretty sure that all user of VFIO are trusted process (like QEMU).

Quoted 8 lines
Quoted 3 lines

Finaly i am convince that the IOMMU grouping stuff related to VFIO is useless for your usecase. I really do not see the point of that, it does complicate things for you for no reasons AFAICT.

Indeed, I don't like the group thing. I believe VFIO's maintains would not like it very much either;). But the problem is, the group reflects to the same IOMMU(unit), which may shared with other devices. It is a security problem. I cannot ignore it. I have to take it into account event I don't use VFIO.

To me it seems you are making a policy decission in kernel space ie wether the device should be isolated in its own group or not is a decission that is up to the sys admin or something in userspace. Right now existing user of SVA/SVM don't (at least AFAICT).

Do we really want to force such isolation ?

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

And personally, I believe the maturity and correctness of a framework are driven by applications. Now the problem in accelerator world is that we don't have a direction. If we believe the requirement is right, the method itself is not a big problem in the end. We just need to let people have a unify platform to share their work together.

Quoted 7 lines
Quoted 4 lines

I am not against that but it seems to me that all you want to do is only a matter of simplifying discovery of such devices and sharing few common ioctl (DMA mapping, creating command queue, managing command queue, ...) and again for all this i do not see the point of doing this under VFIO.

It is not a problem of device management, it is a problem of sharing address space.

This ties back to IOMMU SVA/SVM group isolation above.

Jérôme

#36 lore raw
Quoted 60 lines · Jerome Glisse

Date: Mon, 10 Sep 2018 10:54:23 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <liguozhu-C8/M+/[email protected]>
CC: Kenneth Lee <[email protected]>, Alex Williamson
<[email protected]>, Herbert Xu <[email protected]>, [email protected], Jonathan Corbet <[email protected]>, Greg Kroah-Hartman <[email protected]>, Joerg Roedel <[email protected]>, [email protected], Sanjay Kumar <[email protected]>, Hao Fang <[email protected]>, [email protected], [email protected], iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org, "David S . Miller" <davem-fT/PcQaiUtIeIZ0/[email protected]>, [email protected], Zhou Wang <wangzhou1-C8/M+/[email protected]>, Philippe Ombredanne <pombredanne-od1rfyK75/[email protected]>, Thomas Gleixner <[email protected]>, Zaibo Xu <[email protected]>, linux-accelerators-uLR06cmDAlY/[email protected], Lu Baolu <baolu.lu-VuQAYsv1563Yd54FQh9/[email protected]>
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mutt/1.10.0 (2018-05-17)
Message-ID: <[email protected]>

Quoted 7 lines · Kenneth Lee
Quoted 6 lines · Jerome Glisse
Quoted 5 lines · Kenneth Lee
Quoted 4 lines · Jerome Glisse
Quoted 3 lines · Kenneth Lee
Quoted 2 lines · Alex Williamson
Quoted 1 line · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:

On Mon, Sep 03, 2018 at 08:51:57AM +0800, Kenneth Lee wrote:

[...]

Quoted 31 lines
Quoted 11 lines
Quoted 6 lines

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

You were not looking at right place see drivers/gpu/drm/i915/i915_gem_userptr.c It does GUP and create GEM object AFAICR you can wrap that GEM object into a dma buffer object.

Thank you for directing me to this implementation. It is interesting:).

But it is not yet solve my problem. If I understand it right, the userptr in i915 do the following:

1. The user process sets a user pointer with size to the kernel via ioctl.
2. The kernel wraps it as a dma-buf and keeps the process's mm for further reference.
3. The user pages are allocated, GUPed or DMA mapped to the device. So the data can be shared between the user space and the hardware.

But my scenario is:

1. The user process has some data in the user space, pointed by a pointer, say ptr1. And within the memory, there may be some other pointers, let's say one of them is ptr2.
2. Now I need to assign ptr1 *directly* to the hardware MMIO space. And the hardware must refer ptr1 and ptr2 *directly* for data.

Quoted 8 lines
Quoted 3 lines

Userptr lets the hardware and process share the same memory space. But I need them to share the same *address space*. So IOMMU is a MUST for WarpDrive, NOIOMMU mode, as Jean said, is just for verifying some of the procedure is OK.

So to be 100% clear should we _ignore_ the non SVA/SVM case ? If so then wait for necessary SVA/SVM to land and do warp drive without non SVA/SVM path.

I think we should clear the concept of SVA/SVM here. As my understanding, Share Virtual Address/Memory means: any virtual address in a process can be used by device at the same time. This requires IOMMU device to support PASID. And optionally, it requires the feature of page-fault-from-device.

But before the feature is settled down, IOMMU can be used immediately in the current kernel. That make it possible to assign ONE process's virtual addresses to the device's IOMMU page table with GUP. This make WarpDrive work well for one process.

Now We are talking about SVA and PASID, just to make sure WarpDrive can benefit from the feature in the future. It dose not means WarpDrive is useless before that. And it works for our Zip and RSA accelerators in physical world.

Quoted 29 lines

If you still want non SVA/SVM path what you want to do only works if both ptr1 and ptr2 are in a range that is DMA mapped to the device (moreover you need DMA address to match process address which is not an easy feat).

Now even if you only want SVA/SVM, i do not see what is the point of doing this inside VFIO. AMD GPU driver does not and there would be no benefit for them to be there. Well a AMD VFIO mdev device driver for QEMU guest might be useful but they have SVIO IIRC.

For SVA/SVM your usage model is:

Setup:
- user space create a warp drive context for the process
- user space create a device specific context for the process
- user space create a user space command queue for the device
- user space bind command queue

At this point the kernel driver has bound the process address space to the device with a command queue and userspace

Usage:
- user space schedule work and call appropriate flush/update ioctl from time to time. Might be optional depends on the hardware, but probably a good idea to enforce so that kernel can unbind the command queue to bind another process command queue.
...

Quoted 9 lines

Cleanup:
- user space unbind command queue
- user space destroy device specific context
- user space destroy warp drive context
All the above can be implicit when closing the device file.

So again in the above model i do not see anywhere something from VFIO that would benefit this model.

Let me show you how the model will be if I use VFIO:

Setup (Kernel part)
- Kernel driver do every as usual to serve the other functionality, NIC can still be registered to netdev, encryptor can still be registered to crypto...
- At the same time, the driver can devote some of its hardware resource and register them as a mdev creator to the VFIO framework. This just need limited change to the VFIO type1 driver.

Setup (User space)
- System administrator create mdev via the mdev creator interface.
- Following VFIO setup routine, user space open the mdev's group, there is only one group for one device.
- Without PASID support, you don't need to do anything. With PASID, bind the PASID to the device via VFIO interface.
- Get the device from the group via VFIO interface and mmap it the user space for device's MMIO access (for the queue).
- Map whatever memory you need to share with the device with VFIO interface.
- (opt) Add more devices into the container if you want to share the same address space with them

Cleanup:
- User space close the group file handler
- There will be a problem to let the other process know the mdev is freed to be used again. My RFCv1 choose a file handler solution. Alex dose not like it. But it is not a big problem. We can always have a scheduler process to manage the state of the mdev or even we can switch back to the RFCv1 solution without too much effort if we like in the future.

Except for the minimum update to the type1 driver and use sdmdev to manage the interrupt sharing, I don't need any extra code to gain the address sharing capability. And the capability will be strengthen along with the upgrade of VFIO.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

Because tons of other drivers already do all of the above outside VFIO. Many driver have a sizeable userspace side to them (anything with ioctl do) so they can be construded as userspace driver too.

Quoted 8 lines
Quoted 3 lines

Ignoring if there are *tons* of drivers are doing that;), even I do the same as i915 and solve the address space problem. And if I don't need to with VFIO, why should I spend so much effort to do it again?

Because you do not need any code from VFIO, nor do you need to reinvent things. If non SVA/SVM matters to you then use dma buffer. If not then i do not see anything in VFIO that you need.

As I have explain, if I don't use VFIO, at lease I have to do all that has been done in i915 or even more than that.

Quoted 52 lines
Quoted 12 lines
Quoted 11 lines

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

Quoted 4 lines

But as Alex explained, VFIO is not simply used by VM. So it need not to have all stuffs as a driver in host system. And I do need to share the user space as DMA buffer to the hardware. And I can get it with just a little update, then it can service me perfectly. I don't understand why I should choose a long route.

Again this is not the long route i do not see anything in VFIO that benefit you in the SVA/SVM case. A basic character device driver can do that.

Quoted 21 lines
Quoted 21 lines

So i do not see any reasons to do anything you want inside VFIO. All you want to do can be done outside as easily. Moreover it would be better if you define clearly each scenario because from where i sit it looks like you are opening the door wide open to userspace to DMA anywhere when IOMMU is disabled.

When IOMMU is disabled you can _not_ expose command queue to userspace unless your device has its own page table and all commands are relative to that page table and the device page table is populated by kernel driver in secure way (ie by checking that what is populated can be access).

I do not believe your example device to have such page table nor do i see a fallback path when IOMMU is disabled that force user to do ioctl for each commands.

Yes i understand that you target SVA/SVM but still you claim to support non SVA/SVM. The point is that userspace can not be trusted if you want to have random program use your device. I am pretty sure that all user of VFIO are trusted process (like QEMU).

Quoted 8 lines
Quoted 3 lines

Finaly i am convince that the IOMMU grouping stuff related to VFIO is useless for your usecase. I really do not see the point of that, it does complicate things for you for no reasons AFAICT.

Indeed, I don't like the group thing. I believe VFIO's maintains would not like it very much either;). But the problem is, the group reflects to the same IOMMU(unit), which may shared with other devices. It is a security problem. I cannot ignore it. I have to take it into account event I don't use VFIO.

Quoted 7 lines

To me it seems you are making a policy decission in kernel space ie wether the device should be isolated in its own group or not is a decission that is up to the sys admin or something in userspace. Right now existing user of SVA/SVM don't (at least AFAICT).

Do we really want to force such isolation ?

But it is not my decision, that how the iommu subsystem is designed. Personally I don't like it at all, because all our hardwares have their own stream id (device id). I don't need the group concept at all. But the iommu subsystem assume some devices may share the name device ID to a single IOMMU.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
Quoted 6 lines

And personally, I believe the maturity and correctness of a framework are driven by applications. Now the problem in accelerator world is that we don't have a direction. If we believe the requirement is right, the method itself is not a big problem in the end. We just need to let people have a unify platform to share their work together.

I am not against that but it seems to me that all you want to do is only a matter of simplifying discovery of such devices and sharing few common ioctl (DMA mapping, creating command queue, managing command queue, ...) and again for all this i do not see the point of doing this under VFIO.

Quoted 6 lines
Quoted 2 lines

It is not a problem of device management, it is a problem of sharing address space.

This ties back to IOMMU SVA/SVM group isolation above.

Jérôme

Cheers

Signature · 1 line
			-Kenneth(Hisilicon)
#37 lore raw
Quoted 52 lines · Kenneth Lee
Quoted 42 lines · Jerome Glisse
Quoted 7 lines · Kenneth Lee
Quoted 6 lines · Jerome Glisse
Quoted 5 lines · Kenneth Lee
Quoted 4 lines · Jerome Glisse
Quoted 3 lines · Kenneth Lee
Quoted 2 lines · Alex Williamson
Quoted 1 line · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:

On Mon, Sep 03, 2018 at 08:51:57AM +0800, Kenneth Lee wrote:

[...]

Quoted 31 lines
Quoted 11 lines
Quoted 6 lines

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

You were not looking at right place see drivers/gpu/drm/i915/i915_gem_userptr.c It does GUP and create GEM object AFAICR you can wrap that GEM object into a dma buffer object.

Thank you for directing me to this implementation. It is interesting:).

But it is not yet solve my problem. If I understand it right, the userptr in i915 do the following:

1. The user process sets a user pointer with size to the kernel via ioctl.
2. The kernel wraps it as a dma-buf and keeps the process's mm for further reference.
3. The user pages are allocated, GUPed or DMA mapped to the device. So the data can be shared between the user space and the hardware.

But my scenario is:

1. The user process has some data in the user space, pointed by a pointer, say ptr1. And within the memory, there may be some other pointers, let's say one of them is ptr2.
2. Now I need to assign ptr1 *directly* to the hardware MMIO space. And the hardware must refer ptr1 and ptr2 *directly* for data.

Quoted 8 lines
Quoted 3 lines

Userptr lets the hardware and process share the same memory space. But I need them to share the same *address space*. So IOMMU is a MUST for WarpDrive, NOIOMMU mode, as Jean said, is just for verifying some of the procedure is OK.

So to be 100% clear should we _ignore_ the non SVA/SVM case ? If so then wait for necessary SVA/SVM to land and do warp drive without non SVA/SVM path.

Quoted 4 lines

I think we should clear the concept of SVA/SVM here. As my understanding, Share Virtual Address/Memory means: any virtual address in a process can be used by device at the same time. This requires IOMMU device to support PASID. And optionally, it requires the feature of page-fault-from-device.

Yes we agree on what SVA/SVM is. There is a one gotcha thought, access to range that are MMIO map ie CPU page table pointing to IO memory, IIRC it is undefined what happens on some platform for a device trying to access those using SVA/SVM.

Quoted 4 lines

But before the feature is settled down, IOMMU can be used immediately in the current kernel. That make it possible to assign ONE process's virtual addresses to the device's IOMMU page table with GUP. This make WarpDrive work well for one process.

UH ? How ? You want to GUP _every_ single valid address in the process and map it to the device ? How do you handle new vma, page being replace (despite GUP because of things that utimately calls zap pte) ...

Again here you said that the device must be able to access _any_ valid pointer. With GUP this is insane.

So i am assuming this is not what you want to do without SVA/SVM ie with GUP you have a different programming model, one in which the userspace must first bind _range_ of memory to the device and get a DMA address for the range.

Again, GUP range of process address space to map it to a device so that userspace can use the device on the mapped range is something that do exist in various places in the kernel.

Quoted 3 lines

Now We are talking about SVA and PASID, just to make sure WarpDrive can benefit from the feature in the future. It dose not means WarpDrive is useless before that. And it works for our Zip and RSA accelerators in physical world.

Just not with random process address ...

Quoted 41 lines
Quoted 29 lines

If you still want non SVA/SVM path what you want to do only works if both ptr1 and ptr2 are in a range that is DMA mapped to the device (moreover you need DMA address to match process address which is not an easy feat).

Now even if you only want SVA/SVM, i do not see what is the point of doing this inside VFIO. AMD GPU driver does not and there would be no benefit for them to be there. Well a AMD VFIO mdev device driver for QEMU guest might be useful but they have SVIO IIRC.

For SVA/SVM your usage model is:

Setup:
- user space create a warp drive context for the process
- user space create a device specific context for the process
- user space create a user space command queue for the device
- user space bind command queue

At this point the kernel driver has bound the process address space to the device with a command queue and userspace

Usage:
- user space schedule work and call appropriate flush/update ioctl from time to time. Might be optional depends on the hardware, but probably a good idea to enforce so that kernel can unbind the command queue to bind another process command queue.
...

Quoted 9 lines

Cleanup:
- user space unbind command queue
- user space destroy device specific context
- user space destroy warp drive context
All the above can be implicit when closing the device file.

So again in the above model i do not see anywhere something from VFIO that would benefit this model.

Let me show you how the model will be if I use VFIO:

Quoted 7 lines

Setup (Kernel part)
- Kernel driver do every as usual to serve the other functionality, NIC can still be registered to netdev, encryptor can still be registered to crypto...
- At the same time, the driver can devote some of its hardware resource and register them as a mdev creator to the VFIO framework. This just need limited change to the VFIO type1 driver.

In the above VFIO does not help you one bit ... you can do that with as much code with new common device as front end.

Quoted 4 lines

Setup (User space)
- System administrator create mdev via the mdev creator interface.
- Following VFIO setup routine, user space open the mdev's group, there is only one group for one device.

Quoted 8 lines

- Without PASID support, you don't need to do anything. With PASID, bind the PASID to the device via VFIO interface.
- Get the device from the group via VFIO interface and mmap it the user space for device's MMIO access (for the queue).
- Map whatever memory you need to share with the device with VFIO interface.
- (opt) Add more devices into the container if you want to share the same address space with them

So all VFIO buys you here is boiler plate code that does insert_pfn() to handle MMIO mapping. Which is just couple hundred lines of boiler plate code.

Quoted 9 lines

Cleanup:
- User space close the group file handler
- There will be a problem to let the other process know the mdev is freed to be used again. My RFCv1 choose a file handler solution. Alex dose not like it. But it is not a big problem. We can always have a scheduler process to manage the state of the mdev or even we can switch back to the RFCv1 solution without too much effort if we like in the future.

If you were outside VFIO you would have more freedom on how to do that. For instance process opening the device file can be placed on queue and first one in the queue get to use the device until it closes/release the device. Then next one in queue get the device ...

Quoted 25 lines

Except for the minimum update to the type1 driver and use sdmdev to manage the interrupt sharing, I don't need any extra code to gain the address sharing capability. And the capability will be strengthen along with the upgrade of VFIO.

Quoted 21 lines
Quoted 20 lines
Quoted 9 lines
Quoted 9 lines

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

Quoted 7 lines
Quoted 2 lines

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

Because tons of other drivers already do all of the above outside VFIO. Many driver have a sizeable userspace side to them (anything with ioctl do) so they can be construded as userspace driver too.

Ignoring if there are *tons* of drivers are doing that;), even I do the same as i915 and solve the address space problem. And if I don't need to with VFIO, why should I spend so much effort to do it again?

Quoted 7 lines
Quoted 4 lines

Because you do not need any code from VFIO, nor do you need to reinvent things. If non SVA/SVM matters to you then use dma buffer. If not then i do not see anything in VFIO that you need.

As I have explain, if I don't use VFIO, at lease I have to do all that has been done in i915 or even more than that.

So beside the MMIO mmap() handling and dma mapping of range of user space address space (again all very boiler plate code duplicated accross the kernel several time in different forms). You do not gain anything being inside VFIO right ?

Quoted 56 lines
Quoted 56 lines
Quoted 11 lines
Quoted 10 lines

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

Quoted 4 lines

But as Alex explained, VFIO is not simply used by VM. So it need not to have all stuffs as a driver in host system. And I do need to share the user space as DMA buffer to the hardware. And I can get it with just a little update, then it can service me perfectly. I don't understand why I should choose a long route.

Again this is not the long route i do not see anything in VFIO that benefit you in the SVA/SVM case. A basic character device driver can do that.

Quoted 21 lines
Quoted 21 lines

So i do not see any reasons to do anything you want inside VFIO. All you want to do can be done outside as easily. Moreover it would be better if you define clearly each scenario because from where i sit it looks like you are opening the door wide open to userspace to DMA anywhere when IOMMU is disabled.

When IOMMU is disabled you can _not_ expose command queue to userspace unless your device has its own page table and all commands are relative to that page table and the device page table is populated by kernel driver in secure way (ie by checking that what is populated can be access).

I do not believe your example device to have such page table nor do i see a fallback path when IOMMU is disabled that force user to do ioctl for each commands.

Yes i understand that you target SVA/SVM but still you claim to support non SVA/SVM. The point is that userspace can not be trusted if you want to have random program use your device. I am pretty sure that all user of VFIO are trusted process (like QEMU).

Quoted 8 lines
Quoted 3 lines

Finaly i am convince that the IOMMU grouping stuff related to VFIO is useless for your usecase. I really do not see the point of that, it does complicate things for you for no reasons AFAICT.

Indeed, I don't like the group thing. I believe VFIO's maintains would not like it very much either;). But the problem is, the group reflects to the same IOMMU(unit), which may shared with other devices. It is a security problem. I cannot ignore it. I have to take it into account event I don't use VFIO.

To me it seems you are making a policy decission in kernel space ie wether the device should be isolated in its own group or not is a decission that is up to the sys admin or something in userspace. Right now existing user of SVA/SVM don't (at least AFAICT).

Quoted 7 lines
Quoted 2 lines

Do we really want to force such isolation ?

But it is not my decision, that how the iommu subsystem is designed. Personally I don't like it at all, because all our hardwares have their own stream id (device id). I don't need the group concept at all. But the iommu subsystem assume some devices may share the name device ID to a single IOMMU.

My question was do you really want to force group isolation for the device ? Existing SVA/SVM capable driver do not force that, they let the userspace decide this (sysadm, distributions, ...). Being part of VFIO (in the way you do, likely ways to avoid this inside VFIO too) force this decision ie make a policy decision without userspace having anything to say about it.

The IOMMU group thing as always been doubt full to me, it is advertise as allowing to share resources (ie IOMMU page table) between devices. But this assume that all device driver in the group have some way of communicating with each other to share common DMA address that point to memory devices care. I believe only VFIO does that and probably only when use by QEMU.

Anyway my question is:

Is it that much useful to be inside VFIO (to avoid few hundred lines of boiler plate code) given that it forces you into a model (group isolation) that so far have never been the prefered way for all existing device driver that already do what you want to achieve ?

Quoted 1 line

From where i stand i do not see overwhelming reasons to do what you

are doing inside VFIO.

To me it would make more sense to have regular device driver. They all can have device file under same hierarchy to make devices with same programming model easy to discover.

Jérôme

#38 lore raw
Quoted 94 lines · Jerome Glisse

Date: Mon, 10 Sep 2018 23:33:59 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <[email protected]>
CC: Kenneth Lee <[email protected]>, Zaibo Xu <[email protected]>,
Herbert Xu <[email protected]>, [email protected], Jonathan Corbet <[email protected]>, Greg Kroah-Hartman <[email protected]>, Joerg Roedel <[email protected]>, [email protected], Sanjay Kumar <[email protected]>, Hao Fang <[email protected]>, [email protected], [email protected], [email protected], Alex Williamson <[email protected]>, [email protected], Zhou Wang <[email protected]>, Philippe Ombredanne <[email protected]>, Thomas Gleixner <[email protected]>, "David S . Miller" <[email protected]>, [email protected], Lu Baolu <[email protected]>
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mutt/1.10.1 (2018-07-13)
Message-ID: <[email protected]>

Quoted 52 lines · Kenneth Lee
Quoted 42 lines · Jerome Glisse
Quoted 7 lines · Kenneth Lee
Quoted 6 lines · Jerome Glisse
Quoted 5 lines · Kenneth Lee
Quoted 4 lines · Jerome Glisse
Quoted 3 lines · Kenneth Lee
Quoted 2 lines · Alex Williamson
Quoted 1 line · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:

On Mon, Sep 03, 2018 at 08:51:57AM +0800, Kenneth Lee wrote:

[...]

Quoted 31 lines
Quoted 11 lines
Quoted 6 lines

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

You were not looking at right place see drivers/gpu/drm/i915/i915_gem_userptr.c It does GUP and create GEM object AFAICR you can wrap that GEM object into a dma buffer object.

Thank you for directing me to this implementation. It is interesting:).

But it is not yet solve my problem. If I understand it right, the userptr in i915 do the following:

1. The user process sets a user pointer with size to the kernel via ioctl.
2. The kernel wraps it as a dma-buf and keeps the process's mm for further reference.
3. The user pages are allocated, GUPed or DMA mapped to the device. So the data can be shared between the user space and the hardware.

But my scenario is:

1. The user process has some data in the user space, pointed by a pointer, say ptr1. And within the memory, there may be some other pointers, let's say one of them is ptr2.
2. Now I need to assign ptr1 *directly* to the hardware MMIO space. And the hardware must refer ptr1 and ptr2 *directly* for data.

Quoted 8 lines
Quoted 3 lines

Userptr lets the hardware and process share the same memory space. But I need them to share the same *address space*. So IOMMU is a MUST for WarpDrive, NOIOMMU mode, as Jean said, is just for verifying some of the procedure is OK.

So to be 100% clear should we _ignore_ the non SVA/SVM case ? If so then wait for necessary SVA/SVM to land and do warp drive without non SVA/SVM path.

Quoted 4 lines

I think we should clear the concept of SVA/SVM here. As my understanding, Share Virtual Address/Memory means: any virtual address in a process can be used by device at the same time. This requires IOMMU device to support PASID. And optionally, it requires the feature of page-fault-from-device.

Yes we agree on what SVA/SVM is. There is a one gotcha thought, access to range that are MMIO map ie CPU page table pointing to IO memory, IIRC it is undefined what happens on some platform for a device trying to access those using SVA/SVM.

Quoted 4 lines

But before the feature is settled down, IOMMU can be used immediately in the current kernel. That make it possible to assign ONE process's virtual addresses to the device's IOMMU page table with GUP. This make WarpDrive work well for one process.

UH ? How ? You want to GUP _every_ single valid address in the process and map it to the device ? How do you handle new vma, page being replace (despite GUP because of things that utimately calls zap pte) ...

Again here you said that the device must be able to access _any_ valid pointer. With GUP this is insane.

Quoted 9 lines

So i am assuming this is not what you want to do without SVA/SVM ie with GUP you have a different programming model, one in which the userspace must first bind _range_ of memory to the device and get a DMA address for the range.

Again, GUP range of process address space to map it to a device so that userspace can use the device on the mapped range is something that do exist in various places in the kernel.

Yes same as your expectation, in WarpDrive, we use the concept of "sharing" to do so. If some memory is going to be shared among process and devices, we use wd_share_mem(queue, ptr, size) to share those memory. When the queue is working in this mode, the point is valid in those memory segments. The wd_share_mem call vfio dma map syscall which will do GUP.

If SVA/SVM is enabled, user space can set SHARE_ALL flags to the queue. Then wd_share_mem() is not necessary.

This is really not popular when we started the work on WarpDrive. The GUP document said it should be put within the scope of mm_sem is locked. Because GUP simply increase the page refcount, not keep the mapping between the page and the vma. We keep our work together with VFIO to make sure the problem can be solved in one deal.

And now we have GUP-longterm and many accounting work in VFIO, we don't want to do that again.

Quoted 71 lines
Quoted 3 lines

Now We are talking about SVA and PASID, just to make sure WarpDrive can benefit from the feature in the future. It dose not means WarpDrive is useless before that. And it works for our Zip and RSA accelerators in physical world.

Just not with random process address ...

Quoted 41 lines
Quoted 29 lines

If you still want non SVA/SVM path what you want to do only works if both ptr1 and ptr2 are in a range that is DMA mapped to the device (moreover you need DMA address to match process address which is not an easy feat).

Now even if you only want SVA/SVM, i do not see what is the point of doing this inside VFIO. AMD GPU driver does not and there would be no benefit for them to be there. Well a AMD VFIO mdev device driver for QEMU guest might be useful but they have SVIO IIRC.

For SVA/SVM your usage model is:

Setup:
- user space create a warp drive context for the process
- user space create a device specific context for the process
- user space create a user space command queue for the device
- user space bind command queue

At this point the kernel driver has bound the process address space to the device with a command queue and userspace

Usage:
- user space schedule work and call appropriate flush/update ioctl from time to time. Might be optional depends on the hardware, but probably a good idea to enforce so that kernel can unbind the command queue to bind another process command queue.
...

Quoted 9 lines

Cleanup:
- user space unbind command queue
- user space destroy device specific context
- user space destroy warp drive context
All the above can be implicit when closing the device file.

So again in the above model i do not see anywhere something from VFIO that would benefit this model.

Let me show you how the model will be if I use VFIO:

Quoted 7 lines

Setup (Kernel part)
- Kernel driver do every as usual to serve the other functionality, NIC can still be registered to netdev, encryptor can still be registered to crypto...
- At the same time, the driver can devote some of its hardware resource and register them as a mdev creator to the VFIO framework. This just need limited change to the VFIO type1 driver.

In the above VFIO does not help you one bit ... you can do that with as much code with new common device as front end.

Quoted 4 lines

Setup (User space)
- System administrator create mdev via the mdev creator interface.
- Following VFIO setup routine, user space open the mdev's group, there is only one group for one device.

Quoted 8 lines

- Without PASID support, you don't need to do anything. With PASID, bind the PASID to the device via VFIO interface.
- Get the device from the group via VFIO interface and mmap it the user space for device's MMIO access (for the queue).
- Map whatever memory you need to share with the device with VFIO interface.
- (opt) Add more devices into the container if you want to share the same address space with them

Quoted 4 lines

So all VFIO buys you here is boiler plate code that does insert_pfn() to handle MMIO mapping. Which is just couple hundred lines of boiler plate code.

No. With VFIO, I don't need to:

1. GUP and accounting for RLIMIT_MEMLOCK
2. Keep all GUP pages for releasing (VFIO uses the rb_tree to do so)
2. Handle the PASID on SMMU (ARM's IOMMU) myself.
3. Multiple devices menagement (VFIO uses container to manage this)

And even as a boiler plate, it is valueable, the memory thing is sensitive interface to user space, it can easily become a security problem. If I can achieve my target within the scope of VFIO, why not? At lease it has been proved to be safe for the time being.

Quoted 10 lines
Quoted 9 lines

Cleanup:
- User space close the group file handler
- There will be a problem to let the other process know the mdev is freed to be used again. My RFCv1 choose a file handler solution. Alex dose not like it. But it is not a big problem. We can always have a scheduler process to manage the state of the mdev or even we can switch back to the RFCv1 solution without too much effort if we like in the future.

Quoted 4 lines

If you were outside VFIO you would have more freedom on how to do that. For instance process opening the device file can be placed on queue and first one in the queue get to use the device until it closes/release the device. Then next one in queue get the device ...

Yes. I do like the file handle solution. But I hope the solution become mature as soon as possible. Many of our products, and as I know include some of our partners, are waiting for a long term solution as direction. If I rely on some unmature solution, they may choose some deviated, customized solution. That will be much harmful. Compare to this, the freedom is not so important...

Quoted 31 lines
Quoted 31 lines

Except for the minimum update to the type1 driver and use sdmdev to manage the interrupt sharing, I don't need any extra code to gain the address sharing capability. And the capability will be strengthen along with the upgrade of VFIO.

Quoted 25 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

Quoted 8 lines
Quoted 4 lines

Because tons of other drivers already do all of the above outside VFIO. Many driver have a sizeable userspace side to them (anything with ioctl do) so they can be construded as userspace driver too.

Ignoring if there are *tons* of drivers are doing that;), even I do the same as i915 and solve the address space problem. And if I don't need to with VFIO, why should I spend so much effort to do it again?

Because you do not need any code from VFIO, nor do you need to reinvent things. If non SVA/SVM matters to you then use dma buffer. If not then i do not see anything in VFIO that you need.

Quoted 8 lines
Quoted 2 lines

As I have explain, if I don't use VFIO, at lease I have to do all that has been done in i915 or even more than that.

So beside the MMIO mmap() handling and dma mapping of range of user space address space (again all very boiler plate code duplicated accross the kernel several time in different forms). You do not gain anything being inside VFIO right ?

As I said, rb-tree for gup, rlimit accounting, cooperation on SMMU, and mature user interface are our concern.

Quoted 81 lines
Quoted 57 lines
Quoted 57 lines
Quoted 12 lines
Quoted 11 lines

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

Quoted 4 lines

But as Alex explained, VFIO is not simply used by VM. So it need not to have all stuffs as a driver in host system. And I do need to share the user space as DMA buffer to the hardware. And I can get it with just a little update, then it can service me perfectly. I don't understand why I should choose a long route.

Again this is not the long route i do not see anything in VFIO that benefit you in the SVA/SVM case. A basic character device driver can do that.

Quoted 21 lines
Quoted 21 lines

So i do not see any reasons to do anything you want inside VFIO. All you want to do can be done outside as easily. Moreover it would be better if you define clearly each scenario because from where i sit it looks like you are opening the door wide open to userspace to DMA anywhere when IOMMU is disabled.

When IOMMU is disabled you can _not_ expose command queue to userspace unless your device has its own page table and all commands are relative to that page table and the device page table is populated by kernel driver in secure way (ie by checking that what is populated can be access).

I do not believe your example device to have such page table nor do i see a fallback path when IOMMU is disabled that force user to do ioctl for each commands.

Yes i understand that you target SVA/SVM but still you claim to support non SVA/SVM. The point is that userspace can not be trusted if you want to have random program use your device. I am pretty sure that all user of VFIO are trusted process (like QEMU).

Quoted 8 lines
Quoted 3 lines

Finaly i am convince that the IOMMU grouping stuff related to VFIO is useless for your usecase. I really do not see the point of that, it does complicate things for you for no reasons AFAICT.

Indeed, I don't like the group thing. I believe VFIO's maintains would not like it very much either;). But the problem is, the group reflects to the same IOMMU(unit), which may shared with other devices. It is a security problem. I cannot ignore it. I have to take it into account event I don't use VFIO.

To me it seems you are making a policy decission in kernel space ie wether the device should be isolated in its own group or not is a decission that is up to the sys admin or something in userspace. Right now existing user of SVA/SVM don't (at least AFAICT).

Quoted 7 lines
Quoted 2 lines

Do we really want to force such isolation ?

But it is not my decision, that how the iommu subsystem is designed. Personally I don't like it at all, because all our hardwares have their own stream id (device id). I don't need the group concept at all. But the iommu subsystem assume some devices may share the name device ID to a single IOMMU.

My question was do you really want to force group isolation for the device ? Existing SVA/SVM capable driver do not force that, they let the userspace decide this (sysadm, distributions, ...). Being part of VFIO (in the way you do, likely ways to avoid this inside VFIO too) force this decision ie make a policy decision without userspace having anything to say about it.

The IOMMU group thing as always been doubt full to me, it is advertise as allowing to share resources (ie IOMMU page table) between devices. But this assume that all device driver in the group have some way of communicating with each other to share common DMA address that point to memory devices care. I believe only VFIO does that and probably only when use by QEMU.

Quoted 7 lines

Anyway my question is:

Is it that much useful to be inside VFIO (to avoid few hundred lines of boiler plate code) given that it forces you into a model (group isolation) that so far have never been the prefered way for all existing device driver that already do what you want to achieve ?

You mean to say I create another framework and copy most of the code from VFIO? It is hard to believe the mainline kernel will take my code. So how about let me try the VFIO way first and try that if it won't work? ;)

Quoted 9 lines
Quoted 2 lines

From where i stand i do not see overwhelming reasons to do what you

are doing inside VFIO.

To me it would make more sense to have regular device driver. They all can have device file under same hierarchy to make devices with same programming model easy to discover.

Jérôme

Cheers

Signature · 19 lines
			-Kenneth(Hisilicon)

================================================================================
本邮件及其附件含有华为公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁
止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中
的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from HUAWEI,
which is intended only for the person or entity whose address is listed above.
Any use of the 
information contained herein in any way (including, but not limited to, total or
partial disclosure, reproduction, or dissemination) by persons other than the
intended 
recipient(s) is prohibited. If you receive this e-mail in error, please notify
the sender by phone or email immediately and delete it!

_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
#39 lore raw
Quoted 93 lines · Kenneth Lee
Quoted 76 lines · Jerome Glisse
Quoted 52 lines · Kenneth Lee
Quoted 42 lines · Jerome Glisse
Quoted 7 lines · Kenneth Lee
Quoted 6 lines · Jerome Glisse
Quoted 5 lines · Kenneth Lee
Quoted 4 lines · Jerome Glisse
Quoted 3 lines · Kenneth Lee
Quoted 2 lines · Alex Williamson
Quoted 1 line · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:

On Mon, Sep 03, 2018 at 08:51:57AM +0800, Kenneth Lee wrote:

[...]

Quoted 31 lines
Quoted 11 lines
Quoted 6 lines

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

You were not looking at right place see drivers/gpu/drm/i915/i915_gem_userptr.c It does GUP and create GEM object AFAICR you can wrap that GEM object into a dma buffer object.

Thank you for directing me to this implementation. It is interesting:).

But it is not yet solve my problem. If I understand it right, the userptr in i915 do the following:

1. The user process sets a user pointer with size to the kernel via ioctl.
2. The kernel wraps it as a dma-buf and keeps the process's mm for further reference.
3. The user pages are allocated, GUPed or DMA mapped to the device. So the data can be shared between the user space and the hardware.

But my scenario is:

1. The user process has some data in the user space, pointed by a pointer, say ptr1. And within the memory, there may be some other pointers, let's say one of them is ptr2.
2. Now I need to assign ptr1 *directly* to the hardware MMIO space. And the hardware must refer ptr1 and ptr2 *directly* for data.

Quoted 8 lines
Quoted 3 lines

Userptr lets the hardware and process share the same memory space. But I need them to share the same *address space*. So IOMMU is a MUST for WarpDrive, NOIOMMU mode, as Jean said, is just for verifying some of the procedure is OK.

So to be 100% clear should we _ignore_ the non SVA/SVM case ? If so then wait for necessary SVA/SVM to land and do warp drive without non SVA/SVM path.

Quoted 4 lines

I think we should clear the concept of SVA/SVM here. As my understanding, Share Virtual Address/Memory means: any virtual address in a process can be used by device at the same time. This requires IOMMU device to support PASID. And optionally, it requires the feature of page-fault-from-device.

Yes we agree on what SVA/SVM is. There is a one gotcha thought, access to range that are MMIO map ie CPU page table pointing to IO memory, IIRC it is undefined what happens on some platform for a device trying to access those using SVA/SVM.

Quoted 4 lines

But before the feature is settled down, IOMMU can be used immediately in the current kernel. That make it possible to assign ONE process's virtual addresses to the device's IOMMU page table with GUP. This make WarpDrive work well for one process.

UH ? How ? You want to GUP _every_ single valid address in the process and map it to the device ? How do you handle new vma, page being replace (despite GUP because of things that utimately calls zap pte) ...

Again here you said that the device must be able to access _any_ valid pointer. With GUP this is insane.

Quoted 9 lines

So i am assuming this is not what you want to do without SVA/SVM ie with GUP you have a different programming model, one in which the userspace must first bind _range_ of memory to the device and get a DMA address for the range.

Again, GUP range of process address space to map it to a device so that userspace can use the device on the mapped range is something that do exist in various places in the kernel.

Yes same as your expectation, in WarpDrive, we use the concept of "sharing" to do so. If some memory is going to be shared among process and devices, we use wd_share_mem(queue, ptr, size) to share those memory. When the queue is working in this mode, the point is valid in those memory segments. The wd_share_mem call vfio dma map syscall which will do GUP.

Quoted 8 lines

If SVA/SVM is enabled, user space can set SHARE_ALL flags to the queue. Then wd_share_mem() is not necessary.

This is really not popular when we started the work on WarpDrive. The GUP document said it should be put within the scope of mm_sem is locked. Because GUP simply increase the page refcount, not keep the mapping between the page and the vma. We keep our work together with VFIO to make sure the problem can be solved in one deal.

The problem can not be solved in one deal, you can not maintain vaddr pointing to same page after a fork() this can not be solve without the use of mmu notifier and device dma mapping invalidation ! So being part of VFIO will not help you there.

AFAIK VFIO is fine with the way it is as QEMU do not fork() once it is running a guest and thus the COW that would invalidate vaddr to physical page assumption is not broken. So i doubt VFIO folks have any incentive to go down the mmu notifier path and invalidate device mapping. They also have the replay thing that probably handle some of fork cases by trusting user space program to do it. In your case you can not trust the user space program.

In your case AFAICT i do not see any warning or gotcha so the following scenario is broken (in non SVA/SVM):
1) program setup the device (open container, mdev, setup queue, ...)
2) program map some range of its address space wih VFIO_IOMMU_MAP_DMA
3) program start using the device using map setup in 2)
...
4) program fork()
5) parent trigger COW inside the range setup in 2)

At this point it is the child process that can write to the page that are access by the device (which was map by the parent in 2)). The parent can no longer access that memory from the CPU.

There is just no sane way to fix this beside invalidating device mapping on fork (and you can not rely on userspace to do so) and thus stopping the device on fork (SVA/SVM case do not have any issue here).

Quoted 2 lines

And now we have GUP-longterm and many accounting work in VFIO, we don't want to do that again.

GUP-longterm does not solve any GUP problem, it just block people to do GUP on DAX backed vma to avoid pining persistent memory as it is a nightmare to handle in the block device driver and file system code.

The accounting is the rt limit thing and is litteraly 10 lines of code so i would not see that as hard to replicate.

Quoted 71 lines
Quoted 71 lines
Quoted 3 lines

Now We are talking about SVA and PASID, just to make sure WarpDrive can benefit from the feature in the future. It dose not means WarpDrive is useless before that. And it works for our Zip and RSA accelerators in physical world.

Just not with random process address ...

Quoted 41 lines
Quoted 29 lines

If you still want non SVA/SVM path what you want to do only works if both ptr1 and ptr2 are in a range that is DMA mapped to the device (moreover you need DMA address to match process address which is not an easy feat).

Now even if you only want SVA/SVM, i do not see what is the point of doing this inside VFIO. AMD GPU driver does not and there would be no benefit for them to be there. Well a AMD VFIO mdev device driver for QEMU guest might be useful but they have SVIO IIRC.

For SVA/SVM your usage model is:

Setup:
- user space create a warp drive context for the process
- user space create a device specific context for the process
- user space create a user space command queue for the device
- user space bind command queue

At this point the kernel driver has bound the process address space to the device with a command queue and userspace

Usage:
- user space schedule work and call appropriate flush/update ioctl from time to time. Might be optional depends on the hardware, but probably a good idea to enforce so that kernel can unbind the command queue to bind another process command queue.
...

Quoted 9 lines

Cleanup:
- user space unbind command queue
- user space destroy device specific context
- user space destroy warp drive context
All the above can be implicit when closing the device file.

So again in the above model i do not see anywhere something from VFIO that would benefit this model.

Let me show you how the model will be if I use VFIO:

Quoted 7 lines

Setup (Kernel part)
- Kernel driver do every as usual to serve the other functionality, NIC can still be registered to netdev, encryptor can still be registered to crypto...
- At the same time, the driver can devote some of its hardware resource and register them as a mdev creator to the VFIO framework. This just need limited change to the VFIO type1 driver.

In the above VFIO does not help you one bit ... you can do that with as much code with new common device as front end.

Quoted 12 lines

Setup (User space)
- System administrator create mdev via the mdev creator interface.
- Following VFIO setup routine, user space open the mdev's group, there is only one group for one device.
- Without PASID support, you don't need to do anything. With PASID, bind the PASID to the device via VFIO interface.
- Get the device from the group via VFIO interface and mmap it the user space for device's MMIO access (for the queue).
- Map whatever memory you need to share with the device with VFIO interface.
- (opt) Add more devices into the container if you want to share the same address space with them

Quoted 8 lines
Quoted 4 lines

So all VFIO buys you here is boiler plate code that does insert_pfn() to handle MMIO mapping. Which is just couple hundred lines of boiler plate code.

No. With VFIO, I don't need to:

1. GUP and accounting for RLIMIT_MEMLOCK

That's 10 line of code ...

Quoted 1 line

2. Keep all GUP pages for releasing (VFIO uses the rb_tree to do so)

GUP pages are not part of rb_tree and what you want to do can be done in few lines of code here is pseudo code:

warp_dma_map_range(ulong vaddr, ulong npages)

{
    struct page *pages = kvzalloc(npages);

    for (i = 0; i < npages; ++i, vaddr += PAGE_SIZE) {
        GUP(vaddr, &pages[i]);
        iommu_map(vaddr, page_to_pfn(pages[i]));
    }
    kvfree(pages);
}

warp_dma_unmap_range(ulong vaddr, ulong npages)

{
    for (i = 0; i < npages; ++i, vaddr += PAGE_SIZE) {
        unsigned long pfn;

        pfn = iommu_iova_to_phys(vaddr);
        iommu_unmap(vaddr);
        put_page(pfn_to_page(page)); /* set dirty if mapped write */
    }
}

Add locking, error handling, dirtying and comments and you are barely looking at couple hundred lines of code. You do not need any of the complexity of VFIO as you do not have the same requirements. Namely VFIO have to keep track of iova and physical mapping for things like migration (migrating guest between host) and few others very virtualization centric requirements.

Quoted 1 line

2. Handle the PASID on SMMU (ARM's IOMMU) myself.

Existing driver do that with 20 lines of with comments and error handling (see kfd_iommu_bind_process_to_device() for instance) i doubt you need much more than that.

Quoted 1 line

3. Multiple devices menagement (VFIO uses container to manage this)

All the vfio_group* stuff ? OK that's boiler plate code, note that hard to replicate thought.

Quoted 4 lines

And even as a boiler plate, it is valueable, the memory thing is sensitive interface to user space, it can easily become a security problem. If I can achieve my target within the scope of VFIO, why not? At lease it has been proved to be safe for the time being.

The thing is being part of VFIO impose things on you, things that you do not need. Like one device per group (maybe it is you imposing this, i am loosing track here). Or the complex dma mapping tracking ...

Quoted 14 lines
Quoted 9 lines
Quoted 8 lines

Cleanup:
- User space close the group file handler
- There will be a problem to let the other process know the mdev is freed to be used again. My RFCv1 choose a file handler solution. Alex dose not like it. But it is not a big problem. We can always have a scheduler process to manage the state of the mdev or even we can switch back to the RFCv1 solution without too much effort if we like in the future.

Quoted 4 lines

If you were outside VFIO you would have more freedom on how to do that. For instance process opening the device file can be placed on queue and first one in the queue get to use the device until it closes/release the device. Then next one in queue get the device ...

Quoted 5 lines

Yes. I do like the file handle solution. But I hope the solution become mature as soon as possible. Many of our products, and as I know include some of our partners, are waiting for a long term solution as direction. If I rely on some unmature solution, they may choose some deviated, customized solution. That will be much harmful. Compare to this, the freedom is not so important...

I do not see how being part of VFIO protect you from people doing crazy thing to their kernel ... Time to market being key in this world, i doubt that being part of VFIO would make anyone think twice before taking a shortcut.

I have seen horrible things on that front and only players like Google can impose a minimum level of sanity.

Quoted 33 lines
Quoted 33 lines
Quoted 32 lines

Except for the minimum update to the type1 driver and use sdmdev to manage the interrupt sharing, I don't need any extra code to gain the address sharing capability. And the capability will be strengthen along with the upgrade of VFIO.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

Because tons of other drivers already do all of the above outside VFIO. Many driver have a sizeable userspace side to them (anything with ioctl do) so they can be construded as userspace driver too.

Quoted 8 lines
Quoted 3 lines

Ignoring if there are *tons* of drivers are doing that;), even I do the same as i915 and solve the address space problem. And if I don't need to with VFIO, why should I spend so much effort to do it again?

Because you do not need any code from VFIO, nor do you need to reinvent things. If non SVA/SVM matters to you then use dma buffer. If not then i do not see anything in VFIO that you need.

As I have explain, if I don't use VFIO, at lease I have to do all that has been done in i915 or even more than that.

Quoted 79 lines
Quoted 5 lines

So beside the MMIO mmap() handling and dma mapping of range of user space address space (again all very boiler plate code duplicated accross the kernel several time in different forms). You do not gain anything being inside VFIO right ?

As I said, rb-tree for gup, rlimit accounting, cooperation on SMMU, and mature user interface are our concern.

Quoted 71 lines
Quoted 57 lines
Quoted 57 lines
Quoted 12 lines
Quoted 11 lines

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

Quoted 4 lines

But as Alex explained, VFIO is not simply used by VM. So it need not to have all stuffs as a driver in host system. And I do need to share the user space as DMA buffer to the hardware. And I can get it with just a little update, then it can service me perfectly. I don't understand why I should choose a long route.

Again this is not the long route i do not see anything in VFIO that benefit you in the SVA/SVM case. A basic character device driver can do that.

Quoted 21 lines
Quoted 21 lines

So i do not see any reasons to do anything you want inside VFIO. All you want to do can be done outside as easily. Moreover it would be better if you define clearly each scenario because from where i sit it looks like you are opening the door wide open to userspace to DMA anywhere when IOMMU is disabled.

When IOMMU is disabled you can _not_ expose command queue to userspace unless your device has its own page table and all commands are relative to that page table and the device page table is populated by kernel driver in secure way (ie by checking that what is populated can be access).

I do not believe your example device to have such page table nor do i see a fallback path when IOMMU is disabled that force user to do ioctl for each commands.

Yes i understand that you target SVA/SVM but still you claim to support non SVA/SVM. The point is that userspace can not be trusted if you want to have random program use your device. I am pretty sure that all user of VFIO are trusted process (like QEMU).

Quoted 8 lines
Quoted 3 lines

Finaly i am convince that the IOMMU grouping stuff related to VFIO is useless for your usecase. I really do not see the point of that, it does complicate things for you for no reasons AFAICT.

Indeed, I don't like the group thing. I believe VFIO's maintains would not like it very much either;). But the problem is, the group reflects to the same IOMMU(unit), which may shared with other devices. It is a security problem. I cannot ignore it. I have to take it into account event I don't use VFIO.

To me it seems you are making a policy decission in kernel space ie wether the device should be isolated in its own group or not is a decission that is up to the sys admin or something in userspace. Right now existing user of SVA/SVM don't (at least AFAICT).

Quoted 7 lines
Quoted 2 lines

Do we really want to force such isolation ?

But it is not my decision, that how the iommu subsystem is designed. Personally I don't like it at all, because all our hardwares have their own stream id (device id). I don't need the group concept at all. But the iommu subsystem assume some devices may share the name device ID to a single IOMMU.

My question was do you really want to force group isolation for the device ? Existing SVA/SVM capable driver do not force that, they let the userspace decide this (sysadm, distributions, ...). Being part of VFIO (in the way you do, likely ways to avoid this inside VFIO too) force this decision ie make a policy decision without userspace having anything to say about it.

You still do not answer my question, do you really want to force group isolation for device in your framework ? Which is a policy decision from my POV and thus belong to userspace and should not be enforce by kernel.

Quoted 16 lines
Quoted 8 lines

The IOMMU group thing as always been doubt full to me, it is advertise as allowing to share resources (ie IOMMU page table) between devices. But this assume that all device driver in the group have some way of communicating with each other to share common DMA address that point to memory devices care. I believe only VFIO does that and probably only when use by QEMU.

Quoted 7 lines

Anyway my question is:

Is it that much useful to be inside VFIO (to avoid few hundred lines of boiler plate code) given that it forces you into a model (group isolation) that so far have never been the prefered way for all existing device driver that already do what you want to achieve ?

Quoted 3 lines

You mean to say I create another framework and copy most of the code from VFIO? It is hard to believe the mainline kernel will take my code. So how about let me try the VFIO way first and try that if it won't work? ;)

There is no trying, this is the kernel, once you expose something to userspace you have to keep supporting it forever ... There is no, hey let's add this new framework and see how it goes and removing it few kernel version latter ...

That is why i am being pedantic :) on making sure there is good reasons to do what you do inside VFIO. I do believe that we want a common frame- work like the one you are proposing but i do not believe it should be part of VFIO given the baggages it comes with and that are not relevant to the use cases for this kind of devices.

Cheers,
Jérôme

#40 lore raw
Quoted 121 lines · Jerome Glisse

Date: Tue, 11 Sep 2018 09:40:14 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <liguozhu-C8/M+/[email protected]>
CC: Kenneth Lee <[email protected]>, Alex Williamson
<[email protected]>, Herbert Xu <[email protected]>, [email protected], Jonathan Corbet <[email protected]>, Greg Kroah-Hartman <[email protected]>, Zaibo Xu <[email protected]>, [email protected], Sanjay Kumar <[email protected]>, Hao Fang <[email protected]>, [email protected], [email protected], iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org, "David S . Miller" <davem-fT/PcQaiUtIeIZ0/[email protected]>, [email protected], Zhou Wang <wangzhou1-C8/M+/[email protected]>, Philippe Ombredanne <pombredanne-od1rfyK75/[email protected]>, Thomas Gleixner <[email protected]>, Joerg Roedel <[email protected]>, linux-accelerators-uLR06cmDAlY/[email protected], Lu Baolu <baolu.lu-VuQAYsv1563Yd54FQh9/[email protected]>
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mutt/1.10.1 (2018-07-13)
Message-ID: <[email protected]>

Quoted 93 lines · Kenneth Lee
Quoted 76 lines · Jerome Glisse
Quoted 52 lines · Kenneth Lee
Quoted 42 lines · Jerome Glisse
Quoted 7 lines · Kenneth Lee
Quoted 6 lines · Jerome Glisse
Quoted 5 lines · Kenneth Lee
Quoted 4 lines · Jerome Glisse
Quoted 3 lines · Kenneth Lee
Quoted 2 lines · Alex Williamson
Quoted 1 line · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse…

On Mon, Sep 03, 2018 at 08:51:57AM +0800, Kenneth Lee wrote:

[...]

Quoted 31 lines
Quoted 11 lines
Quoted 6 lines

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

You were not looking at right place see drivers/gpu/drm/i915/i915_gem_userptr.c It does GUP and create GEM object AFAICR you can wrap that GEM object into a dma buffer object.

Thank you for directing me to this implementation. It is interesting:).

But it is not yet solve my problem. If I understand it right, the userptr in i915 do the following:

1. The user process sets a user pointer with size to the kernel via ioctl.
2. The kernel wraps it as a dma-buf and keeps the process's mm for further reference.
3. The user pages are allocated, GUPed or DMA mapped to the device. So the data can be shared between the user space and the hardware.

But my scenario is:

1. The user process has some data in the user space, pointed by a pointer, say ptr1. And within the memory, there may be some other pointers, let's say one of them is ptr2.
2. Now I need to assign ptr1 *directly* to the hardware MMIO space. And the hardware must refer ptr1 and ptr2 *directly* for data.

Quoted 8 lines
Quoted 3 lines

Userptr lets the hardware and process share the same memory space. But I need them to share the same *address space*. So IOMMU is a MUST for WarpDrive, NOIOMMU mode, as Jean said, is just for verifying some of the procedure is OK.

So to be 100% clear should we _ignore_ the non SVA/SVM case ? If so then wait for necessary SVA/SVM to land and do warp drive without non SVA/SVM path.

Quoted 4 lines

I think we should clear the concept of SVA/SVM here. As my understanding, Share Virtual Address/Memory means: any virtual address in a process can be used by device at the same time. This requires IOMMU device to support PASID. And optionally, it requires the feature of page-fault-from-device.

Yes we agree on what SVA/SVM is. There is a one gotcha thought, access to range that are MMIO map ie CPU page table pointing to IO memory, IIRC it is undefined what happens on some platform for a device trying to access those using SVA/SVM.

Quoted 4 lines

But before the feature is settled down, IOMMU can be used immediately in the current kernel. That make it possible to assign ONE process's virtual addresses to the device's IOMMU page table with GUP. This make WarpDrive work well for one process.

UH ? How ? You want to GUP _every_ single valid address in the process and map it to the device ? How do you handle new vma, page being replace (despite GUP because of things that utimately calls zap pte) ...

Again here you said that the device must be able to access _any_ valid pointer. With GUP this is insane.

Quoted 9 lines

So i am assuming this is not what you want to do without SVA/SVM ie with GUP you have a different programming model, one in which the userspace must first bind _range_ of memory to the device and get a DMA address for the range.

Again, GUP range of process address space to map it to a device so that userspace can use the device on the mapped range is something that do exist in various places in the kernel.

Yes same as your expectation, in WarpDrive, we use the concept of "sharing" to do so. If some memory is going to be shared among process and devices, we use wd_share_mem(queue, ptr, size) to share those memory. When the queue is working in this mode, the point is valid in those memory segments. The wd_share_mem call vfio dma map syscall which will do GUP.

Quoted 8 lines

If SVA/SVM is enabled, user space can set SHARE_ALL flags to the queue. Then wd_share_mem() is not necessary.

This is really not popular when we started the work on WarpDrive. The GUP document said it should be put within the scope of mm_sem is locked. Because GUP simply increase the page refcount, not keep the mapping between the page and the vma. We keep our work together with VFIO to make sure the problem can be solved in one deal.

Quoted 4 lines

The problem can not be solved in one deal, you can not maintain vaddr pointing to same page after a fork() this can not be solve without the use of mmu notifier and device dma mapping invalidation ! So being part of VFIO will not help you there.

Good point. But sadly, even with mmu notifier and dma mapping invalidation, I cannot do anything here. If the process fork a sub-process, the sub-process need a new pasid and hardware resource. The IOMM space mapped should not be used. The parent process should be aware of this, unmap and close the device file before the fork. I have the same limitation as VFIO:(

I don't think I can change much here. If I can, VFIO can too:)

Quoted 18 lines

AFAIK VFIO is fine with the way it is as QEMU do not fork() once it is running a guest and thus the COW that would invalidate vaddr to physical page assumption is not broken. So i doubt VFIO folks have any incentive to go down the mmu notifier path and invalidate device mapping. They also have the replay thing that probably handle some of fork cases by trusting user space program to do it. In your case you can not trust the user space program.

In your case AFAICT i do not see any warning or gotcha so the following scenario is broken (in non SVA/SVM):
1) program setup the device (open container, mdev, setup queue, ...)
2) program map some range of its address space wih VFIO_IOMMU_MAP_DMA
3) program start using the device using map setup in 2)
...
4) program fork()
5) parent trigger COW inside the range setup in 2)

Quoted 7 lines

At this point it is the child process that can write to the page that are access by the device (which was map by the parent in 2)). The parent can no longer access that memory from the CPU.

There is just no sane way to fix this beside invalidating device mapping on fork (and you can not rely on userspace to do so) and thus stopping the device on fork (SVA/SVM case do not have any issue here).

Indeed. But as soon as we choose to expose the device space to the user space, the limitation is already there. If we want to solve the problem, we have to have a hook in the copy_process() procedure and copy the parent's queue state to a new queue, assign it to the child's fd and redirect the child's mmap to it. If I can do so, the same logic can also be applied to VFIO.

The good side is, this is not a security leak. The hardware has been given to the process. It is the process who choose to share it. If it won't work, it is the process's problem;)

Quoted 10 lines
Quoted 3 lines

And now we have GUP-longterm and many accounting work in VFIO, we don't want to do that again.

GUP-longterm does not solve any GUP problem, it just block people to do GUP on DAX backed vma to avoid pining persistent memory as it is a nightmare to handle in the block device driver and file system code.

The accounting is the rt limit thing and is litteraly 10 lines of code so i would not see that as hard to replicate.

OK. Agree.

Quoted 101 lines
Quoted 73 lines
Quoted 73 lines
Quoted 5 lines

Now We are talking about SVA and PASID, just to make sure WarpDrive can benefit from the feature in the future. It dose not means WarpDrive is useless before that. And it works for our Zip and RSA accelerators in physical world.

Just not with random process address ...

Quoted 41 lines
Quoted 29 lines

If you still want non SVA/SVM path what you want to do only works if both ptr1 and ptr2 are in a range that is DMA mapped to the device (moreover you need DMA address to match process address which is not an easy feat).

Now even if you only want SVA/SVM, i do not see what is the point of doing this inside VFIO. AMD GPU driver does not and there would be no benefit for them to be there. Well a AMD VFIO mdev device driver for QEMU guest might be useful but they have SVIO IIRC.

For SVA/SVM your usage model is:

Setup:
- user space create a warp drive context for the process
- user space create a device specific context for the process
- user space create a user space command queue for the device
- user space bind command queue

At this point the kernel driver has bound the process address space to the device with a command queue and userspace

Usage:
- user space schedule work and call appropriate flush/update ioctl from time to time. Might be optional depends on the hardware, but probably a good idea to enforce so that kernel can unbind the command queue to bind another process command queue.
...

Quoted 9 lines

Cleanup:
- user space unbind command queue
- user space destroy device specific context
- user space destroy warp drive context
All the above can be implicit when closing the device file.

So again in the above model i do not see anywhere something from VFIO that would benefit this model.

Let me show you how the model will be if I use VFIO:

Quoted 7 lines

Setup (Kernel part)
- Kernel driver do every as usual to serve the other functionality, NIC can still be registered to netdev, encryptor can still be registered to crypto...
- At the same time, the driver can devote some of its hardware resource and register them as a mdev creator to the VFIO framework. This just need limited change to the VFIO type1 driver.

In the above VFIO does not help you one bit ... you can do that with as much code with new common device as front end.

Quoted 12 lines

Setup (User space)
- System administrator create mdev via the mdev creator interface.
- Following VFIO setup routine, user space open the mdev's group, there is only one group for one device.
- Without PASID support, you don't need to do anything. With PASID, bind the PASID to the device via VFIO interface.
- Get the device from the group via VFIO interface and mmap it the user space for device's MMIO access (for the queue).
- Map whatever memory you need to share with the device with VFIO interface.
- (opt) Add more devices into the container if you want to share the same address space with them

Quoted 8 lines
Quoted 4 lines

So all VFIO buys you here is boiler plate code that does insert_pfn() to handle MMIO mapping. Which is just couple hundred lines of boiler plate code.

No. With VFIO, I don't need to:

1. GUP and accounting for RLIMIT_MEMLOCK

That's 10 line of code ...

Quoted 1 line

2. Keep all GUP pages for releasing (VFIO uses the rb_tree to do so)

GUP pages are not part of rb_tree and what you want to do can be done in few lines of code here is pseudo code:

warp_dma_map_range(ulong vaddr, ulong npages)

{
    struct page *pages = kvzalloc(npages);

    for (i = 0; i < npages; ++i, vaddr += PAGE_SIZE) {
        GUP(vaddr, &pages[i]);
        iommu_map(vaddr, page_to_pfn(pages[i]));
    }
    kvfree(pages);
}

warp_dma_unmap_range(ulong vaddr, ulong npages)

Quoted 10 lines
{
    for (i = 0; i < npages; ++i, vaddr += PAGE_SIZE) {
        unsigned long pfn;

        pfn = iommu_iova_to_phys(vaddr);
        iommu_unmap(vaddr);
        put_page(pfn_to_page(page)); /* set dirty if mapped write */
    }
}

But what if the process exist without unmapping? The pages will be pinned in the kernel forever.

Quoted 8 lines

Add locking, error handling, dirtying and comments and you are barely looking at couple hundred lines of code. You do not need any of the complexity of VFIO as you do not have the same requirements. Namely VFIO have to keep track of iova and physical mapping for things like migration (migrating guest between host) and few others very virtualization centric requirements.

Quoted 6 lines
Quoted 1 line

2. Handle the PASID on SMMU (ARM's IOMMU) myself.

Existing driver do that with 20 lines of with comments and error handling (see kfd_iommu_bind_process_to_device() for instance) i doubt you need much more than that.

OK, I agree.

Quoted 5 lines
Quoted 2 lines

3. Multiple devices menagement (VFIO uses container to manage this)

All the vfio_group* stuff ? OK that's boiler plate code, note that hard to replicate thought.

No, I meant the container thing. Several devices/group can be assigned to the same container and the DMA on the container can be assigned to all those devices. So we can have some devices to share the same name space.

Quoted 10 lines
Quoted 5 lines

And even as a boiler plate, it is valueable, the memory thing is sensitive interface to user space, it can easily become a security problem. If I can achieve my target within the scope of VFIO, why not? At lease it has been proved to be safe for the time being.

The thing is being part of VFIO impose things on you, things that you do not need. Like one device per group (maybe it is you imposing this, i am loosing track here). Or the complex dma mapping tracking ...

Err... But the one-device-per-group is not VFIO's decision. It is IOMMU's :). Unless I don't use IOMMU.

Quoted 21 lines
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines

Cleanup:
- User space close the group file handler
- There will be a problem to let the other process know the mdev is freed to be used again. My RFCv1 choose a file handler solution. Alex dose not like it. But it is not a big problem. We can always have a scheduler process to manage the state of the mdev or even we can switch back to the RFCv1 solution without too much effort if we like in the future.

Quoted 4 lines

If you were outside VFIO you would have more freedom on how to do that. For instance process opening the device file can be placed on queue and first one in the queue get to use the device until it closes/release the device. Then next one in queue get the device ...

Quoted 5 lines

Yes. I do like the file handle solution. But I hope the solution become mature as soon as possible. Many of our products, and as I know include some of our partners, are waiting for a long term solution as direction. If I rely on some unmature solution, they may choose some deviated, customized solution. That will be much harmful. Compare to this, the freedom is not so important...

Quoted 8 lines

I do not see how being part of VFIO protect you from people doing crazy thing to their kernel ... Time to market being key in this world, i doubt that being part of VFIO would make anyone think twice before taking a shortcut.

I have seen horrible things on that front and only players like Google can impose a minimum level of sanity.

OK. My fault, to talk about TTM. It has nothing doing with the architecture decision. But I don't yet see what harm will be brought if I use VFIO when it can fulfill almost all my requirements.

Quoted 114 lines
Quoted 34 lines
Quoted 34 lines
Quoted 33 lines

Except for the minimum update to the type1 driver and use sdmdev to manage the interrupt sharing, I don't need any extra code to gain the address sharing capability. And the capability will be strengthen along with the upgrade of VFIO.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

Because tons of other drivers already do all of the above outside VFIO. Many driver have a sizeable userspace side to them (anything with ioctl do) so they can be construded as userspace driver too.

Quoted 8 lines
Quoted 3 lines

Ignoring if there are *tons* of drivers are doing that;), even I do the same as i915 and solve the address space problem. And if I don't need to with VFIO, why should I spend so much effort to do it again?

Because you do not need any code from VFIO, nor do you need to reinvent things. If non SVA/SVM matters to you then use dma buffer. If not then i do not see anything in VFIO that you need.

As I have explain, if I don't use VFIO, at lease I have to do all that has been done in i915 or even more than that.

Quoted 79 lines
Quoted 5 lines

So beside the MMIO mmap() handling and dma mapping of range of user space address space (again all very boiler plate code duplicated accross the kernel several time in different forms). You do not gain anything being inside VFIO right ?

As I said, rb-tree for gup, rlimit accounting, cooperation on SMMU, and mature user interface are our concern.

Quoted 71 lines
Quoted 57 lines
Quoted 57 lines
Quoted 12 lines
Quoted 11 lines

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

Quoted 4 lines

But as Alex explained, VFIO is not simply used by VM. So it need not to have all stuffs as a driver in host system. And I do need to share the user space as DMA buffer to the hardware. And I can get it with just a little update, then it can service me perfectly. I don't understand why I should choose a long route.

Again this is not the long route i do not see anything in VFIO that benefit you in the SVA/SVM case. A basic character device driver can do that.

Quoted 21 lines
Quoted 21 lines

So i do not see any reasons to do anything you want inside VFIO. All you want to do can be done outside as easily. Moreover it would be better if you define clearly each scenario because from where i sit it looks like you are opening the door wide open to userspace to DMA anywhere when IOMMU is disabled.

When IOMMU is disabled you can _not_ expose command queue to userspace unless your device has its own page table and all commands are relative to that page table and the device page table is populated by kernel driver in secure way (ie by checking that what is populated can be access).

I do not believe your example device to have such page table nor do i see a fallback path when IOMMU is disabled that force user to do ioctl for each commands.

Yes i understand that you target SVA/SVM but still you claim to support non SVA/SVM. The point is that userspace can not be trusted if you want to have random program use your device. I am pretty sure that all user of VFIO are trusted process (like QEMU).

Quoted 8 lines
Quoted 3 lines

Finaly i am convince that the IOMMU grouping stuff related to VFIO is useless for your usecase. I really do not see the point of that, it does complicate things for you for no reasons AFAICT.

Indeed, I don't like the group thing. I believe VFIO's maintains would not like it very much either;). But the problem is, the group reflects to the same IOMMU(unit), which may shared with other devices. It is a security problem. I cannot ignore it. I have to take it into account event I don't use VFIO.

To me it seems you are making a policy decission in kernel space ie wether the device should be isolated in its own group or not is a decission that is up to the sys admin or something in userspace. Right now existing user of SVA/SVM don't (at least AFAICT).

Quoted 7 lines
Quoted 2 lines

Do we really want to force such isolation ?

But it is not my decision, that how the iommu subsystem is designed. Personally I don't like it at all, because all our hardwares have their own stream id (device id). I don't need the group concept at all. But the iommu subsystem assume some devices may share the name device ID to a single IOMMU.

My question was do you really want to force group isolation for the device ? Existing SVA/SVM capable driver do not force that, they let the userspace decide this (sysadm, distributions, ...). Being part of VFIO (in the way you do, likely ways to avoid this inside VFIO too) force this decision ie make a policy decision without userspace having anything to say about it.

Quoted 3 lines

You still do not answer my question, do you really want to force group isolation for device in your framework ? Which is a policy decision from my POV and thus belong to userspace and should not be enforce by kernel.

No. But I have to follow the rule defined by IOMMU, haven't I?

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines

The IOMMU group thing as always been doubt full to me, it is advertise as allowing to share resources (ie IOMMU page table) between devices. But this assume that all device driver in the group have some way of communicating with each other to share common DMA address that point to memory devices care. I believe only VFIO does that and probably only when use by QEMU.

Anyway my question is:

Is it that much useful to be inside VFIO (to avoid few hundred lines of boiler plate code) given that it forces you into a model (group isolation) that so far have never been the prefered way for all existing device driver that already do what you want to achieve ?

Quoted 9 lines
Quoted 3 lines

You mean to say I create another framework and copy most of the code from VFIO? It is hard to believe the mainline kernel will take my code. So how about let me try the VFIO way first and try that if it won't work? ;)

There is no trying, this is the kernel, once you expose something to userspace you have to keep supporting it forever ... There is no, hey let's add this new framework and see how it goes and removing it few kernel version latter ...

No, I don't meant it was unserious when I said "try". I was just not sure if the community can accept it.

Can Alex say something on this? Is this scenario in the future scope of VFIO? If it is, we have the season to solve the problem on the way. If it is not, we should choose other way even we have to copy most of the code.

Quoted 5 lines

That is why i am being pedantic :) on making sure there is good reasons to do what you do inside VFIO. I do believe that we want a common frame- work like the one you are proposing but i do not believe it should be part of VFIO given the baggages it comes with and that are not relevant to the use cases for this kind of devices.

Understood. And I appreciate the discussion and help:)

Cheers

Quoted 3 lines

Cheers,
Jérôme

Signature · 1 line
			-Kenneth(Hisilicon)
#41 lore raw
Quoted 109 lines · Kenneth Lee
Quoted 103 lines · Jerome Glisse
Quoted 93 lines · Kenneth Lee
Quoted 76 lines · Jerome Glisse
Quoted 52 lines · Kenneth Lee
Quoted 42 lines · Jerome Glisse
Quoted 7 lines · Kenneth Lee
Quoted 6 lines · Jerome Glisse
Quoted 5 lines · Kenneth Lee
Quoted 4 lines · Jerome Glisse
Quoted 3 lines · Kenneth Lee
Quoted 2 lines · Alex Williamson
Quoted 1 line · On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:

On Mon, Sep 03, 2018 at 08:51:57AM +0800, Kenneth Lee wrote:

[...]

Quoted 31 lines
Quoted 11 lines
Quoted 6 lines

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

You were not looking at right place see drivers/gpu/drm/i915/i915_gem_userptr.c It does GUP and create GEM object AFAICR you can wrap that GEM object into a dma buffer object.

Thank you for directing me to this implementation. It is interesting:).

But it is not yet solve my problem. If I understand it right, the userptr in i915 do the following:

1. The user process sets a user pointer with size to the kernel via ioctl.
2. The kernel wraps it as a dma-buf and keeps the process's mm for further reference.
3. The user pages are allocated, GUPed or DMA mapped to the device. So the data can be shared between the user space and the hardware.

But my scenario is:

1. The user process has some data in the user space, pointed by a pointer, say ptr1. And within the memory, there may be some other pointers, let's say one of them is ptr2.
2. Now I need to assign ptr1 *directly* to the hardware MMIO space. And the hardware must refer ptr1 and ptr2 *directly* for data.

Quoted 8 lines
Quoted 3 lines

Userptr lets the hardware and process share the same memory space. But I need them to share the same *address space*. So IOMMU is a MUST for WarpDrive, NOIOMMU mode, as Jean said, is just for verifying some of the procedure is OK.

So to be 100% clear should we _ignore_ the non SVA/SVM case ? If so then wait for necessary SVA/SVM to land and do warp drive without non SVA/SVM path.

Quoted 4 lines

I think we should clear the concept of SVA/SVM here. As my understanding, Share Virtual Address/Memory means: any virtual address in a process can be used by device at the same time. This requires IOMMU device to support PASID. And optionally, it requires the feature of page-fault-from-device.

Yes we agree on what SVA/SVM is. There is a one gotcha thought, access to range that are MMIO map ie CPU page table pointing to IO memory, IIRC it is undefined what happens on some platform for a device trying to access those using SVA/SVM.

Quoted 4 lines

But before the feature is settled down, IOMMU can be used immediately in the current kernel. That make it possible to assign ONE process's virtual addresses to the device's IOMMU page table with GUP. This make WarpDrive work well for one process.

UH ? How ? You want to GUP _every_ single valid address in the process and map it to the device ? How do you handle new vma, page being replace (despite GUP because of things that utimately calls zap pte) ...

Again here you said that the device must be able to access _any_ valid pointer. With GUP this is insane.

Quoted 9 lines

So i am assuming this is not what you want to do without SVA/SVM ie with GUP you have a different programming model, one in which the userspace must first bind _range_ of memory to the device and get a DMA address for the range.

Again, GUP range of process address space to map it to a device so that userspace can use the device on the mapped range is something that do exist in various places in the kernel.

Yes same as your expectation, in WarpDrive, we use the concept of "sharing" to do so. If some memory is going to be shared among process and devices, we use wd_share_mem(queue, ptr, size) to share those memory. When the queue is working in this mode, the point is valid in those memory segments. The wd_share_mem call vfio dma map syscall which will do GUP.

Quoted 8 lines

If SVA/SVM is enabled, user space can set SHARE_ALL flags to the queue. Then wd_share_mem() is not necessary.

This is really not popular when we started the work on WarpDrive. The GUP document said it should be put within the scope of mm_sem is locked. Because GUP simply increase the page refcount, not keep the mapping between the page and the vma. We keep our work together with VFIO to make sure the problem can be solved in one deal.

Quoted 4 lines

The problem can not be solved in one deal, you can not maintain vaddr pointing to same page after a fork() this can not be solve without the use of mmu notifier and device dma mapping invalidation ! So being part of VFIO will not help you there.

Quoted 7 lines

Good point. But sadly, even with mmu notifier and dma mapping invalidation, I cannot do anything here. If the process fork a sub-process, the sub-process need a new pasid and hardware resource. The IOMM space mapped should not be used. The parent process should be aware of this, unmap and close the device file before the fork. I have the same limitation as VFIO:(

I don't think I can change much here. If I can, VFIO can too:)

The forbid child to access the device is easy in the kernel whenever someone open the device file force set the OCLOEXEC flag on the file some device driver already do that and so should you. With that you should always have a struct file - mm struct one to one relationship and thus one PASID per struct file ie per open of the device file.

That does not solve the GUP/fork issue i describe below.

Quoted 25 lines
Quoted 17 lines

AFAIK VFIO is fine with the way it is as QEMU do not fork() once it is running a guest and thus the COW that would invalidate vaddr to physical page assumption is not broken. So i doubt VFIO folks have any incentive to go down the mmu notifier path and invalidate device mapping. They also have the replay thing that probably handle some of fork cases by trusting user space program to do it. In your case you can not trust the user space program.

In your case AFAICT i do not see any warning or gotcha so the following scenario is broken (in non SVA/SVM):
1) program setup the device (open container, mdev, setup queue, ...)
2) program map some range of its address space wih VFIO_IOMMU_MAP_DMA
3) program start using the device using map setup in 2)
...
4) program fork()
5) parent trigger COW inside the range setup in 2)

Quoted 7 lines

At this point it is the child process that can write to the page that are access by the device (which was map by the parent in 2)). The parent can no longer access that memory from the CPU.

There is just no sane way to fix this beside invalidating device mapping on fork (and you can not rely on userspace to do so) and thus stopping the device on fork (SVA/SVM case do not have any issue here).

Quoted 5 lines

Indeed. But as soon as we choose to expose the device space to the user space, the limitation is already there. If we want to solve the problem, we have to have a hook in the copy_process() procedure and copy the parent's queue state to a new queue, assign it to the child's fd and redirect the child's mmap to it. If I can do so, the same logic can also be applied to VFIO.

Except we do not want to do that and this does not solve the COW i describe above unless you disable COW altogether which is a big no.

Quoted 3 lines

The good side is, this is not a security leak. The hardware has been given to the process. It is the process who choose to share it. If it won't work, it is the process's problem;)

No this is bad, you can not expect every single userspace program to know and be aware of that. If some trusted application (say systemd or firefox, ...) start using your device and is unaware or does not comprehend all side effect it would allow the child to access/change its parent memory and that is bad. Device driver need to protect against user doing stupid thing. All existing device driver that do ATS/PASID already protect themself against child (ie the child can not use the device through a mix of OCLOEXEC and other checks).

You must set the OCLOEXEC flag but that does not solve the COW problem above.

My motto in life is "do not trust userspace" :) which also translate to "do not expect userspace will do the right thing".

Quoted 124 lines
Quoted 9 lines
Quoted 2 lines

And now we have GUP-longterm and many accounting work in VFIO, we don't want to do that again.

GUP-longterm does not solve any GUP problem, it just block people to do GUP on DAX backed vma to avoid pining persistent memory as it is a nightmare to handle in the block device driver and file system code.

The accounting is the rt limit thing and is litteraly 10 lines of code so i would not see that as hard to replicate.

OK. Agree.

Quoted 101 lines
Quoted 73 lines
Quoted 73 lines
Quoted 5 lines

Now We are talking about SVA and PASID, just to make sure WarpDrive can benefit from the feature in the future. It dose not means WarpDrive is useless before that. And it works for our Zip and RSA accelerators in physical world.

Just not with random process address ...

Quoted 41 lines
Quoted 29 lines

If you still want non SVA/SVM path what you want to do only works if both ptr1 and ptr2 are in a range that is DMA mapped to the device (moreover you need DMA address to match process address which is not an easy feat).

Now even if you only want SVA/SVM, i do not see what is the point of doing this inside VFIO. AMD GPU driver does not and there would be no benefit for them to be there. Well a AMD VFIO mdev device driver for QEMU guest might be useful but they have SVIO IIRC.

For SVA/SVM your usage model is:

Setup:
- user space create a warp drive context for the process
- user space create a device specific context for the process
- user space create a user space command queue for the device
- user space bind command queue

At this point the kernel driver has bound the process address space to the device with a command queue and userspace

Usage:
- user space schedule work and call appropriate flush/update ioctl from time to time. Might be optional depends on the hardware, but probably a good idea to enforce so that kernel can unbind the command queue to bind another process command queue.
...

Quoted 9 lines

Cleanup:
- user space unbind command queue
- user space destroy device specific context
- user space destroy warp drive context
All the above can be implicit when closing the device file.

So again in the above model i do not see anywhere something from VFIO that would benefit this model.

Let me show you how the model will be if I use VFIO:

Quoted 7 lines

Setup (Kernel part)
- Kernel driver do every as usual to serve the other functionality, NIC can still be registered to netdev, encryptor can still be registered to crypto...
- At the same time, the driver can devote some of its hardware resource and register them as a mdev creator to the VFIO framework. This just need limited change to the VFIO type1 driver.

In the above VFIO does not help you one bit ... you can do that with as much code with new common device as front end.

Quoted 12 lines

Setup (User space)
- System administrator create mdev via the mdev creator interface.
- Following VFIO setup routine, user space open the mdev's group, there is only one group for one device.
- Without PASID support, you don't need to do anything. With PASID, bind the PASID to the device via VFIO interface.
- Get the device from the group via VFIO interface and mmap it the user space for device's MMIO access (for the queue).
- Map whatever memory you need to share with the device with VFIO interface.
- (opt) Add more devices into the container if you want to share the same address space with them

Quoted 8 lines
Quoted 4 lines

So all VFIO buys you here is boiler plate code that does insert_pfn() to handle MMIO mapping. Which is just couple hundred lines of boiler plate code.

No. With VFIO, I don't need to:

1. GUP and accounting for RLIMIT_MEMLOCK

That's 10 line of code ...

Quoted 1 line

2. Keep all GUP pages for releasing (VFIO uses the rb_tree to do so)

GUP pages are not part of rb_tree and what you want to do can be done in few lines of code here is pseudo code:

warp_dma_map_range(ulong vaddr, ulong npages)

{
    struct page *pages = kvzalloc(npages);

    for (i = 0; i < npages; ++i, vaddr += PAGE_SIZE) {
        GUP(vaddr, &pages[i]);
        iommu_map(vaddr, page_to_pfn(pages[i]));
    }
    kvfree(pages);
}

warp_dma_unmap_range(ulong vaddr, ulong npages)

Quoted 10 lines
{
    for (i = 0; i < npages; ++i, vaddr += PAGE_SIZE) {
        unsigned long pfn;

        pfn = iommu_iova_to_phys(vaddr);
        iommu_unmap(vaddr);
        put_page(pfn_to_page(page)); /* set dirty if mapped write */
    }
}
Quoted 2 lines

But what if the process exist without unmapping? The pages will be pinned in the kernel forever.

Yeah add a struct warp_map { struct list_head list; unsigned long vaddr, unsigned long npages; } for every mapping, store the head into the device file private field of struct file and when the release fs callback is call you can walk done the list to force unmap any leftover. This is not that much code. You ca even use interval tree which is 3 lines of code with interval_tree_generic.h to speed up warp_map lookup on unmap ioctl.

Quoted 18 lines
Quoted 8 lines

Add locking, error handling, dirtying and comments and you are barely looking at couple hundred lines of code. You do not need any of the complexity of VFIO as you do not have the same requirements. Namely VFIO have to keep track of iova and physical mapping for things like migration (migrating guest between host) and few others very virtualization centric requirements.

Quoted 6 lines
Quoted 1 line

2. Handle the PASID on SMMU (ARM's IOMMU) myself.

Existing driver do that with 20 lines of with comments and error handling (see kfd_iommu_bind_process_to_device() for instance) i doubt you need much more than that.

OK, I agree.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

3. Multiple devices menagement (VFIO uses container to manage this)

All the vfio_group* stuff ? OK that's boiler plate code, note that hard to replicate thought.

No, I meant the container thing. Several devices/group can be assigned to the same container and the DMA on the container can be assigned to all those devices. So we can have some devices to share the same name space.

This was the motivation of my question below, to me this is a policy decision and it should be left to userspace to decide but not forced upon userspace because it uses a given device driver.

Maybe i am wrong but i think you can create container and device group without having a VFIO driver for the devices in the group. It is not something i do often so i might be wrong here.

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

And even as a boiler plate, it is valueable, the memory thing is sensitive interface to user space, it can easily become a security problem. If I can achieve my target within the scope of VFIO, why not? At lease it has been proved to be safe for the time being.

Quoted 7 lines
Quoted 4 lines

The thing is being part of VFIO impose things on you, things that you do not need. Like one device per group (maybe it is you imposing this, i am loosing track here). Or the complex dma mapping tracking ...

Err... But the one-device-per-group is not VFIO's decision. It is IOMMU's :). Unless I don't use IOMMU.

AFAIK, on x86 and PPC at least, all PCIE devices are in the same group by default at boot or at least all devices behind the same bridge.

Maybe they are kernel option to avoid that and userspace init program can definitly re-arrange that base on sysadmin policy).

Quoted 25 lines
Quoted 25 lines
Quoted 14 lines
Quoted 9 lines
Quoted 8 lines

Cleanup:
- User space close the group file handler
- There will be a problem to let the other process know the mdev is freed to be used again. My RFCv1 choose a file handler solution. Alex dose not like it. But it is not a big problem. We can always have a scheduler process to manage the state of the mdev or even we can switch back to the RFCv1 solution without too much effort if we like in the future.

Quoted 4 lines

If you were outside VFIO you would have more freedom on how to do that. For instance process opening the device file can be placed on queue and first one in the queue get to use the device until it closes/release the device. Then next one in queue get the device ...

Quoted 5 lines

Yes. I do like the file handle solution. But I hope the solution become mature as soon as possible. Many of our products, and as I know include some of our partners, are waiting for a long term solution as direction. If I rely on some unmature solution, they may choose some deviated, customized solution. That will be much harmful. Compare to this, the freedom is not so important...

I do not see how being part of VFIO protect you from people doing crazy thing to their kernel ... Time to market being key in this world, i doubt that being part of VFIO would make anyone think twice before taking a shortcut.

Quoted 7 lines
Quoted 3 lines

I have seen horrible things on that front and only players like Google can impose a minimum level of sanity.

OK. My fault, to talk about TTM. It has nothing doing with the architecture decision. But I don't yet see what harm will be brought if I use VFIO when it can fulfill almost all my requirements.

The harm is in forcing the device group isolation policy which is not necessary for all devices like you said so yourself on ARM with the device stream id so that IOMMU can identify individual devices.

So i would rather see the device isolation as something orthogonal to what you want to achieve and that should be forced upon user ie sysadmin should control that distribution can have sane default for each platform.

Quoted 113 lines
Quoted 113 lines
Quoted 112 lines
Quoted 30 lines
Quoted 30 lines

Except for the minimum update to the type1 driver and use sdmdev to manage the interrupt sharing, I don't need any extra code to gain the address sharing capability. And the capability will be strengthen along with the upgrade of VFIO.

Quoted 25 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

Quoted 8 lines
Quoted 4 lines

Because tons of other drivers already do all of the above outside VFIO. Many driver have a sizeable userspace side to them (anything with ioctl do) so they can be construded as userspace driver too.

Ignoring if there are *tons* of drivers are doing that;), even I do the same as i915 and solve the address space problem. And if I don't need to with VFIO, why should I spend so much effort to do it again?

Because you do not need any code from VFIO, nor do you need to reinvent things. If non SVA/SVM matters to you then use dma buffer. If not then i do not see anything in VFIO that you need.

Quoted 8 lines
Quoted 2 lines

As I have explain, if I don't use VFIO, at lease I have to do all that has been done in i915 or even more than that.

So beside the MMIO mmap() handling and dma mapping of range of user space address space (again all very boiler plate code duplicated accross the kernel several time in different forms). You do not gain anything being inside VFIO right ?

As I said, rb-tree for gup, rlimit accounting, cooperation on SMMU, and mature user interface are our concern.

Quoted 71 lines
Quoted 57 lines
Quoted 57 lines
Quoted 12 lines
Quoted 11 lines

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

Quoted 4 lines

But as Alex explained, VFIO is not simply used by VM. So it need not to have all stuffs as a driver in host system. And I do need to share the user space as DMA buffer to the hardware. And I can get it with just a little update, then it can service me perfectly. I don't understand why I should choose a long route.

Again this is not the long route i do not see anything in VFIO that benefit you in the SVA/SVM case. A basic character device driver can do that.

Quoted 21 lines
Quoted 21 lines

So i do not see any reasons to do anything you want inside VFIO. All you want to do can be done outside as easily. Moreover it would be better if you define clearly each scenario because from where i sit it looks like you are opening the door wide open to userspace to DMA anywhere when IOMMU is disabled.

When IOMMU is disabled you can _not_ expose command queue to userspace unless your device has its own page table and all commands are relative to that page table and the device page table is populated by kernel driver in secure way (ie by checking that what is populated can be access).

I do not believe your example device to have such page table nor do i see a fallback path when IOMMU is disabled that force user to do ioctl for each commands.

Yes i understand that you target SVA/SVM but still you claim to support non SVA/SVM. The point is that userspace can not be trusted if you want to have random program use your device. I am pretty sure that all user of VFIO are trusted process (like QEMU).

Quoted 8 lines
Quoted 3 lines

Finaly i am convince that the IOMMU grouping stuff related to VFIO is useless for your usecase. I really do not see the point of that, it does complicate things for you for no reasons AFAICT.

Indeed, I don't like the group thing. I believe VFIO's maintains would not like it very much either;). But the problem is, the group reflects to the same IOMMU(unit), which may shared with other devices. It is a security problem. I cannot ignore it. I have to take it into account event I don't use VFIO.

To me it seems you are making a policy decission in kernel space ie wether the device should be isolated in its own group or not is a decission that is up to the sys admin or something in userspace. Right now existing user of SVA/SVM don't (at least AFAICT).

Quoted 7 lines
Quoted 2 lines

Do we really want to force such isolation ?

But it is not my decision, that how the iommu subsystem is designed. Personally I don't like it at all, because all our hardwares have their own stream id (device id). I don't need the group concept at all. But the iommu subsystem assume some devices may share the name device ID to a single IOMMU.

My question was do you really want to force group isolation for the device ? Existing SVA/SVM capable driver do not force that, they let the userspace decide this (sysadm, distributions, ...). Being part of VFIO (in the way you do, likely ways to avoid this inside VFIO too) force this decision ie make a policy decision without userspace having anything to say about it.

Quoted 5 lines
Quoted 3 lines

You still do not answer my question, do you really want to force group isolation for device in your framework ? Which is a policy decision from my POV and thus belong to userspace and should not be enforce by kernel.

No. But I have to follow the rule defined by IOMMU, haven't I?

The IOMMU rule does not say that every device _must_ always be in one group and one domain only. I am pretty sure on x86 by default you get one domain for all PCIE devices behind same bridge.

My point is that the device grouping into domain/group should be an orthogonal decision ie it should not be a requirement by the device driver and should be under control of userspace as it is a policy decission.

One exception if it is unsafe to have device share a domain in which case following my motto the driver should refuse to work and return an error on open (and a kernel explaining why). But this depends on device and platform.

Quoted 33 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

The IOMMU group thing as always been doubt full to me, it is advertise as allowing to share resources (ie IOMMU page table) between devices. But this assume that all device driver in the group have some way of communicating with each other to share common DMA address that point to memory devices care. I believe only VFIO does that and probably only when use by QEMU.

Anyway my question is:

Is it that much useful to be inside VFIO (to avoid few hundred lines of boiler plate code) given that it forces you into a model (group isolation) that so far have never been the prefered way for all existing device driver that already do what you want to achieve ?

Quoted 9 lines
Quoted 3 lines

You mean to say I create another framework and copy most of the code from VFIO? It is hard to believe the mainline kernel will take my code. So how about let me try the VFIO way first and try that if it won't work? ;)

There is no trying, this is the kernel, once you expose something to userspace you have to keep supporting it forever ... There is no, hey let's add this new framework and see how it goes and removing it few kernel version latter ...

No, I don't meant it was unserious when I said "try". I was just not sure if the community can accept it.

Can Alex say something on this? Is this scenario in the future scope of VFIO? If it is, we have the season to solve the problem on the way. If it is not, we should choose other way even we have to copy most of the code.

Quoted 7 lines
Quoted 5 lines

That is why i am being pedantic :) on making sure there is good reasons to do what you do inside VFIO. I do believe that we want a common frame- work like the one you are proposing but i do not believe it should be part of VFIO given the baggages it comes with and that are not relevant to the use cases for this kind of devices.

Understood. And I appreciate the discussion and help:)

Thank you for bearing with in this long discussion :)

Cheers,
Jérôme

#42 lore raw
Quoted 134 lines · Jerome Glisse

Date: Thu, 13 Sep 2018 10:51:50 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <[email protected]>
CC: Kenneth Lee <[email protected]>, Alex Williamson
<[email protected]>, Herbert Xu <[email protected]>, [email protected], Jonathan Corbet <[email protected]>, Greg Kroah-Hartman <[email protected]>, Zaibo Xu <[email protected]>, [email protected], Sanjay Kumar <[email protected]>, Hao Fang <[email protected]>, [email protected], [email protected], [email protected], "David S . Miller" <[email protected]>, [email protected], Zhou Wang <[email protected]>, Philippe Ombredanne <[email protected]>, Thomas Gleixner <[email protected]>, Joerg Roedel <[email protected]>, [email protected], Lu Baolu <[email protected]>
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mutt/1.10.1 (2018-07-13)
Message-ID: <[email protected]>

Quoted 115 lines · Kenneth Lee
Quoted 103 lines · Jerome Glisse
Quoted 93 lines · Kenneth Lee
Quoted 76 lines · Jerome Glisse
Quoted 52 lines · Kenneth Lee
Quoted 42 lines · Jerome Glisse
Quoted 7 lines · Kenneth Lee
Quoted 6 lines · Jerome Glisse
Quoted 5 lines · Kenneth Lee
Quoted 4 lines · Jerome Glisse
Quoted 3 lines · Kenneth Lee
Quoted 2 lines · Alex Williamson

On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:
On Mon, Sep 03, 2018 at 08:51:57AM +0800, Kenneth Lee wrote:

[...]

Quoted 31 lines
Quoted 11 lines
Quoted 6 lines

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

You were not looking at right place see drivers/gpu/drm/i915/i915_gem_userptr.c It does GUP and create GEM object AFAICR you can wrap that GEM object into a dma buffer object.

Thank you for directing me to this implementation. It is interesting:).

But it is not yet solve my problem. If I understand it right, the userptr in i915 do the following:

1. The user process sets a user pointer with size to the kernel via ioctl.
2. The kernel wraps it as a dma-buf and keeps the process's mm for further reference.
3. The user pages are allocated, GUPed or DMA mapped to the device. So the data can be shared between the user space and the hardware.

But my scenario is:

1. The user process has some data in the user space, pointed by a pointer, say ptr1. And within the memory, there may be some other pointers, let's say one of them is ptr2.
2. Now I need to assign ptr1 *directly* to the hardware MMIO space. And the hardware must refer ptr1 and ptr2 *directly* for data.

Quoted 8 lines
Quoted 3 lines

Userptr lets the hardware and process share the same memory space. But I need them to share the same *address space*. So IOMMU is a MUST for WarpDrive, NOIOMMU mode, as Jean said, is just for verifying some of the procedure is OK.

So to be 100% clear should we _ignore_ the non SVA/SVM case ? If so then wait for necessary SVA/SVM to land and do warp drive without non SVA/SVM path.

Quoted 4 lines

I think we should clear the concept of SVA/SVM here. As my understanding, Share Virtual Address/Memory means: any virtual address in a process can be used by device at the same time. This requires IOMMU device to support PASID. And optionally, it requires the feature of page-fault-from-device.

Yes we agree on what SVA/SVM is. There is a one gotcha thought, access to range that are MMIO map ie CPU page table pointing to IO memory, IIRC it is undefined what happens on some platform for a device trying to access those using SVA/SVM.

Quoted 4 lines

But before the feature is settled down, IOMMU can be used immediately in the current kernel. That make it possible to assign ONE process's virtual addresses to the device's IOMMU page table with GUP. This make WarpDrive work well for one process.

UH ? How ? You want to GUP _every_ single valid address in the process and map it to the device ? How do you handle new vma, page being replace (despite GUP because of things that utimately calls zap pte) ...

Again here you said that the device must be able to access _any_ valid pointer. With GUP this is insane.

Quoted 9 lines

So i am assuming this is not what you want to do without SVA/SVM ie with GUP you have a different programming model, one in which the userspace must first bind _range_ of memory to the device and get a DMA address for the range.

Again, GUP range of process address space to map it to a device so that userspace can use the device on the mapped range is something that do exist in various places in the kernel.

Yes same as your expectation, in WarpDrive, we use the concept of "sharing" to do so. If some memory is going to be shared among process and devices, we use wd_share_mem(queue, ptr, size) to share those memory. When the queue is working in this mode, the point is valid in those memory segments. The wd_share_mem call vfio dma map syscall which will do GUP.

Quoted 8 lines

If SVA/SVM is enabled, user space can set SHARE_ALL flags to the queue. Then wd_share_mem() is not necessary.

This is really not popular when we started the work on WarpDrive. The GUP document said it should be put within the scope of mm_sem is locked. Because GUP simply increase the page refcount, not keep the mapping between the page and the vma. We keep our work together with VFIO to make sure the problem can be solved in one deal.

Quoted 4 lines

The problem can not be solved in one deal, you can not maintain vaddr pointing to same page after a fork() this can not be solve without the use of mmu notifier and device dma mapping invalidation ! So being part of VFIO will not help you there.

Good point. But sadly, even with mmu notifier and dma mapping invalidation, I cannot do anything here. If the process fork a sub-process, the sub-process need a new pasid and hardware resource. The IOMM space mapped should not be used. The parent process should be aware of this, unmap and close the device file before the fork. I have the same limitation as VFIO:(

Quoted 7 lines
Quoted 1 line

I don't think I can change much here. If I can, VFIO can too:)

The forbid child to access the device is easy in the kernel whenever someone open the device file force set the OCLOEXEC flag on the file some device driver already do that and so should you. With that you should always have a struct file - mm struct one to one relationship and thus one PASID per struct file ie per open of the device file.

I considerred the OCLOEXEC flag, but it seams it works only for exec, not fork.

Quoted 50 lines

That does not solve the GUP/fork issue i describe below.

Quoted 25 lines
Quoted 17 lines

AFAIK VFIO is fine with the way it is as QEMU do not fork() once it is running a guest and thus the COW that would invalidate vaddr to physical page assumption is not broken. So i doubt VFIO folks have any incentive to go down the mmu notifier path and invalidate device mapping. They also have the replay thing that probably handle some of fork cases by trusting user space program to do it. In your case you can not trust the user space program.

In your case AFAICT i do not see any warning or gotcha so the following scenario is broken (in non SVA/SVM):
1) program setup the device (open container, mdev, setup queue, ...)
2) program map some range of its address space wih VFIO_IOMMU_MAP_DMA
3) program start using the device using map setup in 2)
...
4) program fork()
5) parent trigger COW inside the range setup in 2)

Quoted 7 lines

At this point it is the child process that can write to the page that are access by the device (which was map by the parent in 2)). The parent can no longer access that memory from the CPU.

There is just no sane way to fix this beside invalidating device mapping on fork (and you can not rely on userspace to do so) and thus stopping the device on fork (SVA/SVM case do not have any issue here).

Quoted 5 lines

Indeed. But as soon as we choose to expose the device space to the user space, the limitation is already there. If we want to solve the problem, we have to have a hook in the copy_process() procedure and copy the parent's queue state to a new queue, assign it to the child's fd and redirect the child's mmap to it. If I can do so, the same logic can also be applied to VFIO.

Except we do not want to do that and this does not solve the COW i describe above unless you disable COW altogether which is a big no.

Quoted 3 lines

The good side is, this is not a security leak. The hardware has been given to the process. It is the process who choose to share it. If it won't work, it is the process's problem;)

No this is bad, you can not expect every single userspace program to know and be aware of that. If some trusted application (say systemd or firefox, ...) start using your device and is unaware or does not comprehend all side effect it would allow the child to access/change its parent memory and that is bad. Device driver need to protect against user doing stupid thing. All existing device driver that do ATS/PASID already protect themself against child (ie the child can not use the device through a mix of OCLOEXEC and other checks).

Quoted 5 lines

You must set the OCLOEXEC flag but that does not solve the COW problem above.

My motto in life is "do not trust userspace" :) which also translate to "do not expect userspace will do the right thing".

We don't really trust user space here. We trust that the process cannot do more than what has been given to it. If an application use WarpDrive, and share its memory with it, it should know what happen when it fork a sub-process. This is just like you clone a sub-process with CLONE_FILES or CLONE_VM, the parent process should know what happen.

Quoted 128 lines
Quoted 125 lines
Quoted 10 lines
Quoted 3 lines

And now we have GUP-longterm and many accounting work in VFIO, we don't want to do that again.

GUP-longterm does not solve any GUP problem, it just block people to do GUP on DAX backed vma to avoid pining persistent memory as it is a nightmare to handle in the block device driver and file system code.

The accounting is the rt limit thing and is litteraly 10 lines of code so i would not see that as hard to replicate.

OK. Agree.

Quoted 101 lines
Quoted 73 lines
Quoted 73 lines
Quoted 5 lines

Now We are talking about SVA and PASID, just to make sure WarpDrive can benefit from the feature in the future. It dose not means WarpDrive is useless before that. And it works for our Zip and RSA accelerators in physical world.

Just not with random process address ...

Quoted 41 lines
Quoted 29 lines

If you still want non SVA/SVM path what you want to do only works if both ptr1 and ptr2 are in a range that is DMA mapped to the device (moreover you need DMA address to match process address which is not an easy feat).

Now even if you only want SVA/SVM, i do not see what is the point of doing this inside VFIO. AMD GPU driver does not and there would be no benefit for them to be there. Well a AMD VFIO mdev device driver for QEMU guest might be useful but they have SVIO IIRC.

For SVA/SVM your usage model is:

Setup:
- user space create a warp drive context for the process
- user space create a device specific context for the process
- user space create a user space command queue for the device
- user space bind command queue

At this point the kernel driver has bound the process address space to the device with a command queue and userspace

Usage:
- user space schedule work and call appropriate flush/update ioctl from time to time. Might be optional depends on the hardware, but probably a good idea to enforce so that kernel can unbind the command queue to bind another process command queue.
...

Quoted 9 lines

Cleanup:
- user space unbind command queue
- user space destroy device specific context
- user space destroy warp drive context
All the above can be implicit when closing the device file.

So again in the above model i do not see anywhere something from VFIO that would benefit this model.

Let me show you how the model will be if I use VFIO:

Quoted 7 lines

Setup (Kernel part)
- Kernel driver do every as usual to serve the other functionality, NIC can still be registered to netdev, encryptor can still be registered to crypto...
- At the same time, the driver can devote some of its hardware resource and register them as a mdev creator to the VFIO framework. This just need limited change to the VFIO type1 driver.

In the above VFIO does not help you one bit ... you can do that with as much code with new common device as front end.

Quoted 12 lines

Setup (User space)
- System administrator create mdev via the mdev creator interface.
- Following VFIO setup routine, user space open the mdev's group, there is only one group for one device.
- Without PASID support, you don't need to do anything. With PASID, bind the PASID to the device via VFIO interface.
- Get the device from the group via VFIO interface and mmap it the user space for device's MMIO access (for the queue).
- Map whatever memory you need to share with the device with VFIO interface.
- (opt) Add more devices into the container if you want to share the same address space with them

Quoted 8 lines
Quoted 4 lines

So all VFIO buys you here is boiler plate code that does insert_pfn() to handle MMIO mapping. Which is just couple hundred lines of boiler plate code.

No. With VFIO, I don't need to:

1. GUP and accounting for RLIMIT_MEMLOCK

That's 10 line of code ...

Quoted 1 line

2. Keep all GUP pages for releasing (VFIO uses the rb_tree to do so)

GUP pages are not part of rb_tree and what you want to do can be done in few lines of code here is pseudo code:

warp_dma_map_range(ulong vaddr, ulong npages)

{
    struct page *pages = kvzalloc(npages);

    for (i = 0; i < npages; ++i, vaddr += PAGE_SIZE) {
        GUP(vaddr, &pages[i]);
        iommu_map(vaddr, page_to_pfn(pages[i]));
    }
    kvfree(pages);
}

warp_dma_unmap_range(ulong vaddr, ulong npages)

Quoted 10 lines
{
    for (i = 0; i < npages; ++i, vaddr += PAGE_SIZE) {
        unsigned long pfn;

        pfn = iommu_iova_to_phys(vaddr);
        iommu_unmap(vaddr);
        put_page(pfn_to_page(page)); /* set dirty if mapped write */
    }
}
Quoted 2 lines

But what if the process exist without unmapping? The pages will be pinned in the kernel forever.

Quoted 7 lines

Yeah add a struct warp_map { struct list_head list; unsigned long vaddr, unsigned long npages; } for every mapping, store the head into the device file private field of struct file and when the release fs callback is call you can walk done the list to force unmap any leftover. This is not that much code. You ca even use interval tree which is 3 lines of code with interval_tree_generic.h to speed up warp_map lookup on unmap ioctl.

Yes, when you add all of them... it is VFIO:)

Quoted 28 lines
Quoted 19 lines
Quoted 9 lines

Add locking, error handling, dirtying and comments and you are barely looking at couple hundred lines of code. You do not need any of the complexity of VFIO as you do not have the same requirements. Namely VFIO have to keep track of iova and physical mapping for things like migration (migrating guest between host) and few others very virtualization centric requirements.

Quoted 6 lines
Quoted 1 line

2. Handle the PASID on SMMU (ARM's IOMMU) myself.

Existing driver do that with 20 lines of with comments and error handling (see kfd_iommu_bind_process_to_device() for instance) i doubt you need much more than that.

OK, I agree.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

3. Multiple devices menagement (VFIO uses container to manage this)

All the vfio_group* stuff ? OK that's boiler plate code, note that hard to replicate thought.

No, I meant the container thing. Several devices/group can be assigned to the same container and the DMA on the container can be assigned to all those devices. So we can have some devices to share the same name space.

Quoted 8 lines

This was the motivation of my question below, to me this is a policy decision and it should be left to userspace to decide but not forced upon userspace because it uses a given device driver.

Maybe i am wrong but i think you can create container and device group without having a VFIO driver for the devices in the group. It is not something i do often so i might be wrong here.

Container maintains a virtual unify address space for all group/iommu which is added to it. It simplify the address management. But yes, you can choose to do it all in the user space.

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 5 lines

And even as a boiler plate, it is valueable, the memory thing is sensitive interface to user space, it can easily become a security problem. If I can achieve my target within the scope of VFIO, why not? At lease it has been proved to be safe for the time being.

The thing is being part of VFIO impose things on you, things that you do not need. Like one device per group (maybe it is you imposing this, i am loosing track here). Or the complex dma mapping tracking ...

Quoted 9 lines
Quoted 2 lines

Err... But the one-device-per-group is not VFIO's decision. It is IOMMU's :). Unless I don't use IOMMU.

AFAIK, on x86 and PPC at least, all PCIE devices are in the same group by default at boot or at least all devices behind the same bridge.

Maybe they are kernel option to avoid that and userspace init program can definitly re-arrange that base on sysadmin policy).

But if the IOMMU is enabled, all PCIE devices have their own device IDs. So the IOMMU can use different page table for every of them.

Quoted 30 lines
Quoted 30 lines
Quoted 29 lines
Quoted 15 lines
Quoted 10 lines
Quoted 9 lines

Cleanup:
- User space close the group file handler
- There will be a problem to let the other process know the mdev is freed to be used again. My RFCv1 choose a file handler solution. Alex dose not like it. But it is not a big problem. We can always have a scheduler process to manage the state of the mdev or even we can switch back to the RFCv1 solution without too much effort if we like in the future.

Quoted 4 lines

If you were outside VFIO you would have more freedom on how to do that. For instance process opening the device file can be placed on queue and first one in the queue get to use the device until it closes/release the device. Then next one in queue get the device ...

Quoted 5 lines

Yes. I do like the file handle solution. But I hope the solution become mature as soon as possible. Many of our products, and as I know include some of our partners, are waiting for a long term solution as direction. If I rely on some unmature solution, they may choose some deviated, customized solution. That will be much harmful. Compare to this, the freedom is not so important...

I do not see how being part of VFIO protect you from people doing crazy thing to their kernel ... Time to market being key in this world, i doubt that being part of VFIO would make anyone think twice before taking a shortcut.

I have seen horrible things on that front and only players like Google can impose a minimum level of sanity.

Quoted 7 lines
Quoted 3 lines

OK. My fault, to talk about TTM. It has nothing doing with the architecture decision. But I don't yet see what harm will be brought if I use VFIO when it can fulfill almost all my requirements.

The harm is in forcing the device group isolation policy which is not necessary for all devices like you said so yourself on ARM with the device stream id so that IOMMU can identify individual devices.

No. Some mini SoC share the same stream id among several small devices. The iommu has to treat them as the same. That is why IOMMU introduces the concept of iommu_group. Personally I don't like that. If they share the same stream id, they should be treated as the same hardware. But it is the decision for the time being...

Quoted 123 lines

So i would rather see the device isolation as something orthogonal to what you want to achieve and that should be forced upon user ie sysadmin should control that distribution can have sane default for each platform.

Quoted 117 lines
Quoted 116 lines
Quoted 33 lines
Quoted 33 lines
Quoted 32 lines

Except for the minimum update to the type1 driver and use sdmdev to manage the interrupt sharing, I don't need any extra code to gain the address sharing capability. And the capability will be strengthen along with the upgrade of VFIO.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

Because tons of other drivers already do all of the above outside VFIO. Many driver have a sizeable userspace side to them (anything with ioctl do) so they can be construded as userspace driver too.

Quoted 8 lines
Quoted 3 lines

Ignoring if there are *tons* of drivers are doing that;), even I do the same as i915 and solve the address space problem. And if I don't need to with VFIO, why should I spend so much effort to do it again?

Because you do not need any code from VFIO, nor do you need to reinvent things. If non SVA/SVM matters to you then use dma buffer. If not then i do not see anything in VFIO that you need.

As I have explain, if I don't use VFIO, at lease I have to do all that has been done in i915 or even more than that.

Quoted 79 lines
Quoted 5 lines

So beside the MMIO mmap() handling and dma mapping of range of user space address space (again all very boiler plate code duplicated accross the kernel several time in different forms). You do not gain anything being inside VFIO right ?

As I said, rb-tree for gup, rlimit accounting, cooperation on SMMU, and mature user interface are our concern.

Quoted 71 lines
Quoted 57 lines
Quoted 57 lines
Quoted 12 lines
Quoted 11 lines

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

Quoted 4 lines

But as Alex explained, VFIO is not simply used by VM. So it need not to have all stuffs as a driver in host system. And I do need to share the user space as DMA buffer to the hardware. And I can get it with just a little update, then it can service me perfectly. I don't understand why I should choose a long route.

Again this is not the long route i do not see anything in VFIO that benefit you in the SVA/SVM case. A basic character device driver can do that.

Quoted 21 lines
Quoted 21 lines

So i do not see any reasons to do anything you want inside VFIO. All you want to do can be done outside as easily. Moreover it would be better if you define clearly each scenario because from where i sit it looks like you are opening the door wide open to userspace to DMA anywhere when IOMMU is disabled.

When IOMMU is disabled you can _not_ expose command queue to userspace unless your device has its own page table and all commands are relative to that page table and the device page table is populated by kernel driver in secure way (ie by checking that what is populated can be access).

I do not believe your example device to have such page table nor do i see a fallback path when IOMMU is disabled that force user to do ioctl for each commands.

Yes i understand that you target SVA/SVM but still you claim to support non SVA/SVM. The point is that userspace can not be trusted if you want to have random program use your device. I am pretty sure that all user of VFIO are trusted process (like QEMU).

Quoted 8 lines
Quoted 3 lines

Finaly i am convince that the IOMMU grouping stuff related to VFIO is useless for your usecase. I really do not see the point of that, it does complicate things for you for no reasons AFAICT.

Indeed, I don't like the group thing. I believe VFIO's maintains would not like it very much either;). But the problem is, the group reflects to the same IOMMU(unit), which may shared with other devices. It is a security problem. I cannot ignore it. I have to take it into account event I don't use VFIO.

To me it seems you are making a policy decission in kernel space ie wether the device should be isolated in its own group or not is a decission that is up to the sys admin or something in userspace. Right now existing user of SVA/SVM don't (at least AFAICT).

Quoted 7 lines
Quoted 2 lines

Do we really want to force such isolation ?

But it is not my decision, that how the iommu subsystem is designed. Personally I don't like it at all, because all our hardwares have their own stream id (device id). I don't need the group concept at all. But the iommu subsystem assume some devices may share the name device ID to a single IOMMU.

My question was do you really want to force group isolation for the device ? Existing SVA/SVM capable driver do not force that, they let the userspace decide this (sysadm, distributions, ...). Being part of VFIO (in the way you do, likely ways to avoid this inside VFIO too) force this decision ie make a policy decision without userspace having anything to say about it.

You still do not answer my question, do you really want to force group isolation for device in your framework ? Which is a policy decision from my POV and thus belong to userspace and should not be enforce by kernel.

Quoted 6 lines
Quoted 1 line

No. But I have to follow the rule defined by IOMMU, haven't I?

The IOMMU rule does not say that every device _must_ always be in one group and one domain only. I am pretty sure on x86 by default you get one domain for all PCIE devices behind same bridge.

Really? Give me some more time. I need to check it out.

Quoted 50 lines

My point is that the device grouping into domain/group should be an orthogonal decision ie it should not be a requirement by the device driver and should be under control of userspace as it is a policy decission.

One exception if it is unsafe to have device share a domain in which case following my motto the driver should refuse to work and return an error on open (and a kernel explaining why). But this depends on device and platform.

Quoted 39 lines
Quoted 16 lines
Quoted 16 lines
Quoted 15 lines

The IOMMU group thing as always been doubt full to me, it is advertise as allowing to share resources (ie IOMMU page table) between devices. But this assume that all device driver in the group have some way of communicating with each other to share common DMA address that point to memory devices care. I believe only VFIO does that and probably only when use by QEMU.

Anyway my question is:

Is it that much useful to be inside VFIO (to avoid few hundred lines of boiler plate code) given that it forces you into a model (group isolation) that so far have never been the prefered way for all existing device driver that already do what you want to achieve ?

Quoted 9 lines
Quoted 3 lines

You mean to say I create another framework and copy most of the code from VFIO? It is hard to believe the mainline kernel will take my code. So how about let me try the VFIO way first and try that if it won't work? ;)

There is no trying, this is the kernel, once you expose something to userspace you have to keep supporting it forever ... There is no, hey let's add this new framework and see how it goes and removing it few kernel version latter ...

No, I don't meant it was unserious when I said "try". I was just not sure if the community can accept it.

Can Alex say something on this? Is this scenario in the future scope of VFIO? If it is, we have the season to solve the problem on the way. If it is not, we should choose other way even we have to copy most of the code.

Quoted 5 lines

That is why i am being pedantic :) on making sure there is good reasons to do what you do inside VFIO. I do believe that we want a common frame- work like the one you are proposing but i do not believe it should be part of VFIO given the baggages it comes with and that are not relevant to the use cases for this kind of devices.

Quoted 6 lines
Quoted 1 line

Understood. And I appreciate the discussion and help:)

Thank you for bearing with in this long discussion :)

Cheers,
Jérôme

Cheers

Signature · 1 line
			-Kenneth(Hisilicon)
#43 lore raw
Quoted 119 lines · Kenneth Lee
Quoted 118 lines · Jerome Glisse
Quoted 116 lines · Kenneth Lee
Quoted 103 lines · Jerome Glisse
Quoted 93 lines · Kenneth Lee
Quoted 76 lines · Jerome Glisse
Quoted 52 lines · Kenneth Lee
Quoted 42 lines · Jerome Glisse
Quoted 7 lines · Kenneth Lee
Quoted 6 lines · Jerome Glisse
Quoted 5 lines · Kenneth Lee
Quoted 4 lines · Jerome Glisse
Quoted 3 lines · Kenneth Lee

On Tue, Sep 04, 2018 at 10:15:09AM -0600, Alex Williamson wrote:
On Tue, 4 Sep 2018 11:00:19 -0400 Jerome Glisse <[email protected]> wrote:
On Mon, Sep 03, 2018 at 08:51:57AM +0800, Kenneth Lee wrote:

[...]

Quoted 31 lines
Quoted 11 lines
Quoted 6 lines

I took a look at i915_gem_execbuffer_ioctl(). It seems it "copy_from_user" the user memory to the kernel. That is not what we need. What we try to get is: the user application do something on its data, and push it away to the accelerator, and says: "I'm tied, it is your turn to do the job...". Then the accelerator has the memory, referring any portion of it with the same VAs of the application, even the VAs are stored inside the memory itself.

You were not looking at right place see drivers/gpu/drm/i915/i915_gem_userptr.c It does GUP and create GEM object AFAICR you can wrap that GEM object into a dma buffer object.

Thank you for directing me to this implementation. It is interesting:).

But it is not yet solve my problem. If I understand it right, the userptr in i915 do the following:

1. The user process sets a user pointer with size to the kernel via ioctl.
2. The kernel wraps it as a dma-buf and keeps the process's mm for further reference.
3. The user pages are allocated, GUPed or DMA mapped to the device. So the data can be shared between the user space and the hardware.

But my scenario is:

1. The user process has some data in the user space, pointed by a pointer, say ptr1. And within the memory, there may be some other pointers, let's say one of them is ptr2.
2. Now I need to assign ptr1 *directly* to the hardware MMIO space. And the hardware must refer ptr1 and ptr2 *directly* for data.

Quoted 8 lines
Quoted 3 lines

Userptr lets the hardware and process share the same memory space. But I need them to share the same *address space*. So IOMMU is a MUST for WarpDrive, NOIOMMU mode, as Jean said, is just for verifying some of the procedure is OK.

So to be 100% clear should we _ignore_ the non SVA/SVM case ? If so then wait for necessary SVA/SVM to land and do warp drive without non SVA/SVM path.

Quoted 4 lines

I think we should clear the concept of SVA/SVM here. As my understanding, Share Virtual Address/Memory means: any virtual address in a process can be used by device at the same time. This requires IOMMU device to support PASID. And optionally, it requires the feature of page-fault-from-device.

Yes we agree on what SVA/SVM is. There is a one gotcha thought, access to range that are MMIO map ie CPU page table pointing to IO memory, IIRC it is undefined what happens on some platform for a device trying to access those using SVA/SVM.

Quoted 4 lines

But before the feature is settled down, IOMMU can be used immediately in the current kernel. That make it possible to assign ONE process's virtual addresses to the device's IOMMU page table with GUP. This make WarpDrive work well for one process.

UH ? How ? You want to GUP _every_ single valid address in the process and map it to the device ? How do you handle new vma, page being replace (despite GUP because of things that utimately calls zap pte) ...

Again here you said that the device must be able to access _any_ valid pointer. With GUP this is insane.

Quoted 9 lines

So i am assuming this is not what you want to do without SVA/SVM ie with GUP you have a different programming model, one in which the userspace must first bind _range_ of memory to the device and get a DMA address for the range.

Again, GUP range of process address space to map it to a device so that userspace can use the device on the mapped range is something that do exist in various places in the kernel.

Yes same as your expectation, in WarpDrive, we use the concept of "sharing" to do so. If some memory is going to be shared among process and devices, we use wd_share_mem(queue, ptr, size) to share those memory. When the queue is working in this mode, the point is valid in those memory segments. The wd_share_mem call vfio dma map syscall which will do GUP.

Quoted 8 lines

If SVA/SVM is enabled, user space can set SHARE_ALL flags to the queue. Then wd_share_mem() is not necessary.

This is really not popular when we started the work on WarpDrive. The GUP document said it should be put within the scope of mm_sem is locked. Because GUP simply increase the page refcount, not keep the mapping between the page and the vma. We keep our work together with VFIO to make sure the problem can be solved in one deal.

Quoted 4 lines

The problem can not be solved in one deal, you can not maintain vaddr pointing to same page after a fork() this can not be solve without the use of mmu notifier and device dma mapping invalidation ! So being part of VFIO will not help you there.

Good point. But sadly, even with mmu notifier and dma mapping invalidation, I cannot do anything here. If the process fork a sub-process, the sub-process need a new pasid and hardware resource. The IOMM space mapped should not be used. The parent process should be aware of this, unmap and close the device file before the fork. I have the same limitation as VFIO:(

I don't think I can change much here. If I can, VFIO can too:)

Quoted 7 lines
Quoted 5 lines

The forbid child to access the device is easy in the kernel whenever someone open the device file force set the OCLOEXEC flag on the file some device driver already do that and so should you. With that you should always have a struct file - mm struct one to one relationship and thus one PASID per struct file ie per open of the device file.

I considerred the OCLOEXEC flag, but it seams it works only for exec, not fork.

Every device driver i know protect against that (child having access) by at least setting VM_DONTCOPY on mmap of device IO (or even all mmap of device file).

But you right OCLOEXEC does nothing on fork() sometimes i forget posix.

Quoted 55 lines
Quoted 49 lines

That does not solve the GUP/fork issue i describe below.

Quoted 25 lines
Quoted 17 lines

AFAIK VFIO is fine with the way it is as QEMU do not fork() once it is running a guest and thus the COW that would invalidate vaddr to physical page assumption is not broken. So i doubt VFIO folks have any incentive to go down the mmu notifier path and invalidate device mapping. They also have the replay thing that probably handle some of fork cases by trusting user space program to do it. In your case you can not trust the user space program.

In your case AFAICT i do not see any warning or gotcha so the following scenario is broken (in non SVA/SVM):
1) program setup the device (open container, mdev, setup queue, ...)
2) program map some range of its address space wih VFIO_IOMMU_MAP_DMA
3) program start using the device using map setup in 2)
...
4) program fork()
5) parent trigger COW inside the range setup in 2)

Quoted 7 lines

At this point it is the child process that can write to the page that are access by the device (which was map by the parent in 2)). The parent can no longer access that memory from the CPU.

There is just no sane way to fix this beside invalidating device mapping on fork (and you can not rely on userspace to do so) and thus stopping the device on fork (SVA/SVM case do not have any issue here).

Quoted 5 lines

Indeed. But as soon as we choose to expose the device space to the user space, the limitation is already there. If we want to solve the problem, we have to have a hook in the copy_process() procedure and copy the parent's queue state to a new queue, assign it to the child's fd and redirect the child's mmap to it. If I can do so, the same logic can also be applied to VFIO.

Except we do not want to do that and this does not solve the COW i describe above unless you disable COW altogether which is a big no.

Quoted 3 lines

The good side is, this is not a security leak. The hardware has been given to the process. It is the process who choose to share it. If it won't work, it is the process's problem;)

No this is bad, you can not expect every single userspace program to know and be aware of that. If some trusted application (say systemd or firefox, ...) start using your device and is unaware or does not comprehend all side effect it would allow the child to access/change its parent memory and that is bad. Device driver need to protect against user doing stupid thing. All existing device driver that do ATS/PASID already protect themself against child (ie the child can not use the device through a mix of OCLOEXEC and other checks).

Quoted 5 lines

You must set the OCLOEXEC flag but that does not solve the COW problem above.

My motto in life is "do not trust userspace" :) which also translate to "do not expect userspace will do the right thing".

Quoted 5 lines

We don't really trust user space here. We trust that the process cannot do more than what has been given to it. If an application use WarpDrive, and share its memory with it, it should know what happen when it fork a sub-process. This is just like you clone a sub-process with CLONE_FILES or CLONE_VM, the parent process should know what happen.

Except application do not always know, the whole container business is an example of that. Most application have tons of dependency and reliance on various library. There is no way they audit all the library all the times.

So if a library decide to use such device then this trickles down into application without their knowledge and you can open security issues through that.

To be clear here, what worry me is the non SVA/SVM case, there is no way for the device driver to intercept fork and thus noways for it to break COW for the parent, so child could potentialy write hardware commands for the device (depends on the device if for instance the cmd queue mmap only point to regular memory from which it fetches its commands). I guess relying on IOMMU might be good enough (ie only range mapped by the first user of the hardware would be accessible).

Quoted 135 lines
Quoted 127 lines
Quoted 124 lines
Quoted 9 lines
Quoted 2 lines

And now we have GUP-longterm and many accounting work in VFIO, we don't want to do that again.

GUP-longterm does not solve any GUP problem, it just block people to do GUP on DAX backed vma to avoid pining persistent memory as it is a nightmare to handle in the block device driver and file system code.

The accounting is the rt limit thing and is litteraly 10 lines of code so i would not see that as hard to replicate.

OK. Agree.

Quoted 101 lines
Quoted 73 lines
Quoted 73 lines
Quoted 5 lines

Now We are talking about SVA and PASID, just to make sure WarpDrive can benefit from the feature in the future. It dose not means WarpDrive is useless before that. And it works for our Zip and RSA accelerators in physical world.

Just not with random process address ...

Quoted 41 lines
Quoted 29 lines

If you still want non SVA/SVM path what you want to do only works if both ptr1 and ptr2 are in a range that is DMA mapped to the device (moreover you need DMA address to match process address which is not an easy feat).

Now even if you only want SVA/SVM, i do not see what is the point of doing this inside VFIO. AMD GPU driver does not and there would be no benefit for them to be there. Well a AMD VFIO mdev device driver for QEMU guest might be useful but they have SVIO IIRC.

For SVA/SVM your usage model is:

Setup:
- user space create a warp drive context for the process
- user space create a device specific context for the process
- user space create a user space command queue for the device
- user space bind command queue

At this point the kernel driver has bound the process address space to the device with a command queue and userspace

Usage:
- user space schedule work and call appropriate flush/update ioctl from time to time. Might be optional depends on the hardware, but probably a good idea to enforce so that kernel can unbind the command queue to bind another process command queue.
...

Quoted 9 lines

Cleanup:
- user space unbind command queue
- user space destroy device specific context
- user space destroy warp drive context
All the above can be implicit when closing the device file.

So again in the above model i do not see anywhere something from VFIO that would benefit this model.

Let me show you how the model will be if I use VFIO:

Quoted 7 lines

Setup (Kernel part)
- Kernel driver do every as usual to serve the other functionality, NIC can still be registered to netdev, encryptor can still be registered to crypto...
- At the same time, the driver can devote some of its hardware resource and register them as a mdev creator to the VFIO framework. This just need limited change to the VFIO type1 driver.

In the above VFIO does not help you one bit ... you can do that with as much code with new common device as front end.

Quoted 12 lines

Setup (User space)
- System administrator create mdev via the mdev creator interface.
- Following VFIO setup routine, user space open the mdev's group, there is only one group for one device.
- Without PASID support, you don't need to do anything. With PASID, bind the PASID to the device via VFIO interface.
- Get the device from the group via VFIO interface and mmap it the user space for device's MMIO access (for the queue).
- Map whatever memory you need to share with the device with VFIO interface.
- (opt) Add more devices into the container if you want to share the same address space with them

Quoted 8 lines
Quoted 4 lines

So all VFIO buys you here is boiler plate code that does insert_pfn() to handle MMIO mapping. Which is just couple hundred lines of boiler plate code.

No. With VFIO, I don't need to:

1. GUP and accounting for RLIMIT_MEMLOCK

That's 10 line of code ...

Quoted 1 line

2. Keep all GUP pages for releasing (VFIO uses the rb_tree to do so)

GUP pages are not part of rb_tree and what you want to do can be done in few lines of code here is pseudo code:

warp_dma_map_range(ulong vaddr, ulong npages)

{
    struct page *pages = kvzalloc(npages);

    for (i = 0; i < npages; ++i, vaddr += PAGE_SIZE) {
        GUP(vaddr, &pages[i]);
        iommu_map(vaddr, page_to_pfn(pages[i]));
    }
    kvfree(pages);
}

warp_dma_unmap_range(ulong vaddr, ulong npages)

Quoted 10 lines
{
    for (i = 0; i < npages; ++i, vaddr += PAGE_SIZE) {
        unsigned long pfn;

        pfn = iommu_iova_to_phys(vaddr);
        iommu_unmap(vaddr);
        put_page(pfn_to_page(page)); /* set dirty if mapped write */
    }
}
Quoted 2 lines

But what if the process exist without unmapping? The pages will be pinned in the kernel forever.

Quoted 7 lines

Yeah add a struct warp_map { struct list_head list; unsigned long vaddr, unsigned long npages; } for every mapping, store the head into the device file private field of struct file and when the release fs callback is call you can walk done the list to force unmap any leftover. This is not that much code. You ca even use interval tree which is 3 lines of code with interval_tree_generic.h to speed up warp_map lookup on unmap ioctl.

Quoted 1 line

Yes, when you add all of them... it is VFIO:)

No, VFIO not only need to track all mapping but it also need to track iova to vaddr which you do not need. The VFIO code is much more complex because of that. What you would need is only ~200lines of code.

Quoted 58 lines
Quoted 28 lines
Quoted 19 lines
Quoted 9 lines

Add locking, error handling, dirtying and comments and you are barely looking at couple hundred lines of code. You do not need any of the complexity of VFIO as you do not have the same requirements. Namely VFIO have to keep track of iova and physical mapping for things like migration (migrating guest between host) and few others very virtualization centric requirements.

Quoted 6 lines
Quoted 1 line

2. Handle the PASID on SMMU (ARM's IOMMU) myself.

Existing driver do that with 20 lines of with comments and error handling (see kfd_iommu_bind_process_to_device() for instance) i doubt you need much more than that.

OK, I agree.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

3. Multiple devices menagement (VFIO uses container to manage this)

All the vfio_group* stuff ? OK that's boiler plate code, note that hard to replicate thought.

No, I meant the container thing. Several devices/group can be assigned to the same container and the DMA on the container can be assigned to all those devices. So we can have some devices to share the same name space.

Quoted 8 lines

This was the motivation of my question below, to me this is a policy decision and it should be left to userspace to decide but not forced upon userspace because it uses a given device driver.

Maybe i am wrong but i think you can create container and device group without having a VFIO driver for the devices in the group. It is not something i do often so i might be wrong here.

Container maintains a virtual unify address space for all group/iommu which is added to it. It simplify the address management. But yes, you can choose to do it all in the user space.

Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

And even as a boiler plate, it is valueable, the memory thing is sensitive interface to user space, it can easily become a security problem. If I can achieve my target within the scope of VFIO, why not? At lease it has been proved to be safe for the time being.

Quoted 7 lines
Quoted 4 lines

The thing is being part of VFIO impose things on you, things that you do not need. Like one device per group (maybe it is you imposing this, i am loosing track here). Or the complex dma mapping tracking ...

Err... But the one-device-per-group is not VFIO's decision. It is IOMMU's :). Unless I don't use IOMMU.

AFAIK, on x86 and PPC at least, all PCIE devices are in the same group by default at boot or at least all devices behind the same bridge.

Quoted 6 lines
Quoted 3 lines

Maybe they are kernel option to avoid that and userspace init program can definitly re-arrange that base on sysadmin policy).

But if the IOMMU is enabled, all PCIE devices have their own device IDs. So the IOMMU can use different page table for every of them.

On PCIE AFAIR IOMMU can not differentiate between devices, hence why by default they end up with the same domain in the same group.

Quoted 37 lines
Quoted 29 lines
Quoted 29 lines
Quoted 28 lines
Quoted 14 lines
Quoted 9 lines
Quoted 8 lines

Cleanup:
- User space close the group file handler
- There will be a problem to let the other process know the mdev is freed to be used again. My RFCv1 choose a file handler solution. Alex dose not like it. But it is not a big problem. We can always have a scheduler process to manage the state of the mdev or even we can switch back to the RFCv1 solution without too much effort if we like in the future.

Quoted 4 lines

If you were outside VFIO you would have more freedom on how to do that. For instance process opening the device file can be placed on queue and first one in the queue get to use the device until it closes/release the device. Then next one in queue get the device ...

Quoted 5 lines

Yes. I do like the file handle solution. But I hope the solution become mature as soon as possible. Many of our products, and as I know include some of our partners, are waiting for a long term solution as direction. If I rely on some unmature solution, they may choose some deviated, customized solution. That will be much harmful. Compare to this, the freedom is not so important...

I do not see how being part of VFIO protect you from people doing crazy thing to their kernel ... Time to market being key in this world, i doubt that being part of VFIO would make anyone think twice before taking a shortcut.

I have seen horrible things on that front and only players like Google can impose a minimum level of sanity.

Quoted 7 lines
Quoted 3 lines

OK. My fault, to talk about TTM. It has nothing doing with the architecture decision. But I don't yet see what harm will be brought if I use VFIO when it can fulfill almost all my requirements.

The harm is in forcing the device group isolation policy which is not necessary for all devices like you said so yourself on ARM with the device stream id so that IOMMU can identify individual devices.

Quoted 5 lines

No. Some mini SoC share the same stream id among several small devices. The iommu has to treat them as the same. That is why IOMMU introduces the concept of iommu_group. Personally I don't like that. If they share the same stream id, they should be treated as the same hardware. But it is the decision for the time being...

Still policy decission ie how to group device together should be some- thing left to the sysadmin/distribution. When hardware have constraint it just limits the choice of userspace.

Quoted 122 lines
Quoted 122 lines

So i would rather see the device isolation as something orthogonal to what you want to achieve and that should be forced upon user ie sysadmin should control that distribution can have sane default for each platform.

Quoted 117 lines
Quoted 116 lines
Quoted 33 lines
Quoted 33 lines
Quoted 32 lines

Except for the minimum update to the type1 driver and use sdmdev to manage the interrupt sharing, I don't need any extra code to gain the address sharing capability. And the capability will be strengthen along with the upgrade of VFIO.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
Quoted 11 lines

And I don't understand why I should avoid to use VFIO? As Alex said, VFIO is the user driver framework. And I need exactly a user driver interface. Why should I invent another wheel? It has most of stuff I need:

1. Connecting multiple devices to the same application space
2. Pinning and DMA from the application space to the whole set of device
3. Managing hardware resource by device

We just need the last step: make sure multiple applications and the kernel can share the same IOMMU. Then why shouldn't we use VFIO?

Because tons of other drivers already do all of the above outside VFIO. Many driver have a sizeable userspace side to them (anything with ioctl do) so they can be construded as userspace driver too.

Quoted 8 lines
Quoted 3 lines

Ignoring if there are *tons* of drivers are doing that;), even I do the same as i915 and solve the address space problem. And if I don't need to with VFIO, why should I spend so much effort to do it again?

Because you do not need any code from VFIO, nor do you need to reinvent things. If non SVA/SVM matters to you then use dma buffer. If not then i do not see anything in VFIO that you need.

As I have explain, if I don't use VFIO, at lease I have to do all that has been done in i915 or even more than that.

Quoted 79 lines
Quoted 5 lines

So beside the MMIO mmap() handling and dma mapping of range of user space address space (again all very boiler plate code duplicated accross the kernel several time in different forms). You do not gain anything being inside VFIO right ?

As I said, rb-tree for gup, rlimit accounting, cooperation on SMMU, and mature user interface are our concern.

Quoted 71 lines
Quoted 57 lines
Quoted 57 lines
Quoted 12 lines
Quoted 11 lines

So there is no reasons to do that under VFIO. Especialy as in your example it is not a real user space device driver, the userspace portion only knows about writting command into command buffer AFAICT.

VFIO is for real userspace driver where interrupt, configurations, ... ie all the driver is handled in userspace. This means that the userspace have to be trusted as it could program the device to do DMA to anywhere (if IOMMU is disabled at boot which is still the default configuration in the kernel).

Quoted 4 lines

But as Alex explained, VFIO is not simply used by VM. So it need not to have all stuffs as a driver in host system. And I do need to share the user space as DMA buffer to the hardware. And I can get it with just a little update, then it can service me perfectly. I don't understand why I should choose a long route.

Again this is not the long route i do not see anything in VFIO that benefit you in the SVA/SVM case. A basic character device driver can do that.

Quoted 21 lines
Quoted 21 lines

So i do not see any reasons to do anything you want inside VFIO. All you want to do can be done outside as easily. Moreover it would be better if you define clearly each scenario because from where i sit it looks like you are opening the door wide open to userspace to DMA anywhere when IOMMU is disabled.

When IOMMU is disabled you can _not_ expose command queue to userspace unless your device has its own page table and all commands are relative to that page table and the device page table is populated by kernel driver in secure way (ie by checking that what is populated can be access).

I do not believe your example device to have such page table nor do i see a fallback path when IOMMU is disabled that force user to do ioctl for each commands.

Yes i understand that you target SVA/SVM but still you claim to support non SVA/SVM. The point is that userspace can not be trusted if you want to have random program use your device. I am pretty sure that all user of VFIO are trusted process (like QEMU).

Quoted 8 lines
Quoted 3 lines

Finaly i am convince that the IOMMU grouping stuff related to VFIO is useless for your usecase. I really do not see the point of that, it does complicate things for you for no reasons AFAICT.

Indeed, I don't like the group thing. I believe VFIO's maintains would not like it very much either;). But the problem is, the group reflects to the same IOMMU(unit), which may shared with other devices. It is a security problem. I cannot ignore it. I have to take it into account event I don't use VFIO.

To me it seems you are making a policy decission in kernel space ie wether the device should be isolated in its own group or not is a decission that is up to the sys admin or something in userspace. Right now existing user of SVA/SVM don't (at least AFAICT).

Quoted 7 lines
Quoted 2 lines

Do we really want to force such isolation ?

But it is not my decision, that how the iommu subsystem is designed. Personally I don't like it at all, because all our hardwares have their own stream id (device id). I don't need the group concept at all. But the iommu subsystem assume some devices may share the name device ID to a single IOMMU.

My question was do you really want to force group isolation for the device ? Existing SVA/SVM capable driver do not force that, they let the userspace decide this (sysadm, distributions, ...). Being part of VFIO (in the way you do, likely ways to avoid this inside VFIO too) force this decision ie make a policy decision without userspace having anything to say about it.

You still do not answer my question, do you really want to force group isolation for device in your framework ? Which is a policy decision from my POV and thus belong to userspace and should not be enforce by kernel.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

No. But I have to follow the rule defined by IOMMU, haven't I?

The IOMMU rule does not say that every device _must_ always be in one group and one domain only. I am pretty sure on x86 by default you get one domain for all PCIE devices behind same bridge.

Really? Give me some more time. I need to check it out.

On my computer right now i have one group per pcie bridge and all devices behind same bridge in same groups.

ls /sys/kernel/iommu_groups/*/devices

Quoted 10 lines
Quoted 10 lines

My point is that the device grouping into domain/group should be an orthogonal decision ie it should not be a requirement by the device driver and should be under control of userspace as it is a policy decission.

One exception if it is unsafe to have device share a domain in which case following my motto the driver should refuse to work and return an error on open (and a kernel explaining why). But this depends on device and platform.

Cheers
Jérôme

#44 lore raw
In reply to Jerome Glisse
Quoted 3 lines

From: Jerome Glisse
Sent: Thursday, September 13, 2018 10:52 PM

[...]

Quoted 2 lines

AFAIK, on x86 and PPC at least, all PCIE devices are in the same group by default at boot or at least all devices behind the same bridge.

the group thing reflects physical hierarchy limitation, not changed cross boot. Please note iommu group defines the minimal isolation boundary - all devices within same group must be attached to the same iommu domain or address space, because physically IOMMU cannot differentiate DMAs out of those devices. devices behind legacy PCI-X bridge is one example. other examples include devices behind a PCIe switch port which doesn't support ACS thus cannot route p2p transaction to IOMMU. If talking about typical PCIe endpoint (with upstreaming ports all supporting ACS), you'll get one device per group.

One iommu group today is attached to only one iommu domain. In the future one group may attach to multiple domains, as the aux domain concept being discussed in another thread.

Quoted 3 lines

Maybe they are kernel option to avoid that and userspace init program can definitly re-arrange that base on sysadmin policy).

I don't think there is such option, as it may break isolation model enabled by IOMMU.

[...]

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

That is why i am being pedantic :) on making sure there is good reasons to do what you do inside VFIO. I do believe that we want a common

frame-

Quoted 4 lines
Quoted 3 lines

work like the one you are proposing but i do not believe it should be part of VFIO given the baggages it comes with and that are not relevant to the use cases for this kind of devices.

The purpose of VFIO is clear - the kernel portal for granting generic device resource (mmio, irq, etc.) to user space. VFIO doesn't care what exactly a resource is used for (queue, cmd reg, etc.). If really pursuing VFIO path is necessary, maybe such common framework should lay down in user space, which gets all granted resource from kernel driver thru VFIO and then provides accelerator services to other processes?

Thanks
Kevin

#45 lore raw
Quoted 60 lines · Kevin

Date: Fri, 14 Sep 2018 06:50:55 +0000
From: "Tian, Kevin" <[email protected]>
To: Jerome Glisse <[email protected]>, Kenneth Lee <[email protected]>
CC: Kenneth Lee <[email protected]>, Alex Williamson
<[email protected]>, Herbert Xu <[email protected]>, "[email protected]" <[email protected]>, Jonathan Corbet <[email protected]>, Greg Kroah-Hartman <[email protected]>, Zaibo Xu <[email protected]>, "[email protected]" <[email protected]>, "Kumar, Sanjay K" <[email protected]>, Hao Fang <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>, "[email protected]" <[email protected]>, "David S . Miller" <[email protected]>, "[email protected]" <[email protected]>, Zhou Wang <[email protected]>, Philippe Ombredanne <[email protected]>, Thomas Gleixner <[email protected]>, Joerg Roedel <[email protected]>, "[email protected]" <[email protected]>, Lu Baolu <[email protected]>
Subject: RE: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
Message-ID: <AADFC41AFE54684AB9EE6CBC0274A5D191303A7F@SHSMSX101.ccr.corp.intel.com>

Quoted 3 lines

From: Jerome Glisse
Sent: Thursday, September 13, 2018 10:52 PM

[...]

Quoted 2 lines

AFAIK, on x86 and PPC at least, all PCIE devices are in the same group by default at boot or at least all devices behind the same bridge.

the group thing reflects physical hierarchy limitation, not changed cross boot. Please note iommu group defines the minimal isolation boundary - all devices within same group must be attached to the same iommu domain or address space, because physically IOMMU cannot differentiate DMAs out of those devices. devices behind legacy PCI-X bridge is one example. other examples include devices behind a PCIe switch port which doesn't support ACS thus cannot route p2p transaction to IOMMU. If talking about typical PCIe endpoint (with upstreaming ports all supporting ACS), you'll get one device per group.

One iommu group today is attached to only one iommu domain. In the future one group may attach to multiple domains, as the aux domain concept being discussed in another thread.

Quoted 3 lines

Maybe they are kernel option to avoid that and userspace init program can definitly re-arrange that base on sysadmin policy).

I don't think there is such option, as it may break isolation model enabled by IOMMU.

[...]

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

That is why i am being pedantic :) on making sure there is good reasons to do what you do inside VFIO. I do believe that we want a common

frame-

Quoted 4 lines
Quoted 3 lines

work like the one you are proposing but i do not believe it should be part of VFIO given the baggages it comes with and that are not relevant to the use cases for this kind of devices.

Quoted 7 lines

The purpose of VFIO is clear - the kernel portal for granting generic device resource (mmio, irq, etc.) to user space. VFIO doesn't care what exactly a resource is used for (queue, cmd reg, etc.). If really pursuing VFIO path is necessary, maybe such common framework should lay down in user space, which gets all granted resource from kernel driver thru VFIO and then provides accelerator services to other processes?

Yes. I think this is exactly what WarpDrive is now doing. This patch is just let the type1 driver use parent IOMMU for mdev.

Quoted 3 lines

Thanks
Kevin

Signature · 14 lines
			-Kenneth(Hisilicon)

================================================================================
本邮件及其附件含有华为公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁
止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中
的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from HUAWEI,
which is intended only for the person or entity whose address is listed above.
Any use of the 
information contained herein in any way (including, but not limited to, total or
partial disclosure, reproduction, or dissemination) by persons other than the
intended 
recipient(s) is prohibited. If you receive this e-mail in error, please notify
the sender by phone or email immediately and delete it!
#46 lore raw
In reply to Tian, Kevin
Quoted 18 lines · Kevin
Quoted 3 lines

From: Jerome Glisse
Sent: Thursday, September 13, 2018 10:52 PM

[...]

Quoted 2 lines

AFAIK, on x86 and PPC at least, all PCIE devices are in the same group by default at boot or at least all devices behind the same bridge.

the group thing reflects physical hierarchy limitation, not changed cross boot. Please note iommu group defines the minimal isolation boundary - all devices within same group must be attached to the same iommu domain or address space, because physically IOMMU cannot differentiate DMAs out of those devices. devices behind legacy PCI-X bridge is one example. other examples include devices behind a PCIe switch port which doesn't support ACS thus cannot route p2p transaction to IOMMU. If talking about typical PCIe endpoint (with upstreaming ports all supporting ACS), you'll get one device per group.

Quoted 3 lines

One iommu group today is attached to only one iommu domain. In the future one group may attach to multiple domains, as the aux domain concept being discussed in another thread.

Thanks for the info.

Quoted 17 lines
Quoted 4 lines

Maybe they are kernel option to avoid that and userspace init program can definitly re-arrange that base on sysadmin policy).

I don't think there is such option, as it may break isolation model enabled by IOMMU.

[...]

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines

That is why i am being pedantic :) on making sure there is good reasons to do what you do inside VFIO. I do believe that we want a common

frame-

Quoted 4 lines
Quoted 3 lines

work like the one you are proposing but i do not believe it should be part of VFIO given the baggages it comes with and that are not relevant to the use cases for this kind of devices.

Quoted 7 lines

The purpose of VFIO is clear - the kernel portal for granting generic device resource (mmio, irq, etc.) to user space. VFIO doesn't care what exactly a resource is used for (queue, cmd reg, etc.). If really pursuing VFIO path is necessary, maybe such common framework should lay down in user space, which gets all granted resource from kernel driver thru VFIO and then provides accelerator services to other processes?

Except that many existing device driver falls under that description (ie exposing mmio, command queues, ...) and are not under VFIO.

Up to mdev VFIO was all about handling a full device to userspace AFAIK. With the introduction of mdev a host kernel driver can "slice" its device and share it through VFIO to userspace. Note that in that case it might never end over any mmio, irq, ... the host driver might just be handling over memory and would be polling from it to schedule on the real hardware.

The question i am asking about warpdrive is wether being in VFIO is necessary ? as i do not see the requirement myself.

Cheers,
Jérôme

#47 lore raw
In reply to Kenneth Lee

So i want to summarize issues i have as this threads have dig deep into details. For this i would like to differentiate two cases first the easy one when relying on SVA/SVM. Then the second one when there is no SVA/SVM. In both cases your objectives as i understand them:

[R1]- expose a common user space API that make it easy to share boiler
plate code accross many devices (discovering devices, opening device, creating context, creating command queue ...).
[R2]- try to share the device as much as possible up to device limits
(number of independant queues the device has)
[R3]- minimize syscall by allowing user space to directly schedule on the
device queue without a round trip to the kernel

I don't think i missed any.

(1) Device with SVA/SVM

For that case it is easy, you do not need to be in VFIO or part of any thing specific in the kernel. There is no security risk (modulo bug in the SVA/SVM silicon). Fork/exec is properly handle and binding a process to a device is just couple dozen lines of code.

(2) Device does not have SVA/SVM (or it is disabled)

You want to still allow device to be part of your framework. However here i see fundamentals securities issues and you move the burden of being careful to user space which i think is a bad idea. We should never trus the userspace from kernel space.

To keep the same API for the user space code you want a 1:1 mapping between device physical address and process virtual address (ie if device access device physical address A it is accessing the same memory as what is backing the virtual address A in the process.

Security issues are on two things:
[I1]- fork/exec, a process who opened any such device and created an
active queue can transfer without its knowledge control of its commands queue through COW. The parent map some anonymous region to the device as a command queue buffer but because of COW the parent can be the first to copy on write and thus the child can inherit the original pages that are mapped to the hardware. Here parent lose control and child gain it.

[I2]- Because of [R3] you want to allow userspace to schedule commands
on the device without doing an ioctl and thus here user space can schedule any commands to the device with any address. What happens if that address have not been mapped by the user space is undefined and in fact can not be defined as what each IOMMU does on invalid address access is different from IOMMU to IOMMU.

In case of a bad IOMMU, or simply an IOMMU improperly setup by the kernel, this can potentialy allow user space to DMA anywhere.

[I3]- By relying on GUP in VFIO you are not abiding by the implicit
contract (at least i hope it is implicit) that you should not try to map to the device any file backed vma (private or share).

The VFIO code never check the vma controlling the addresses that are provided to VFIO_IOMMU_MAP_DMA ioctl. Which means that the user space can provide file backed range.

I am guessing that the VFIO code never had any issues because its number one user is QEMU and QEMU never does that (and that's good as no one should ever do that).

So if process does that you are opening your self to serious file system corruption (depending on file system this can lead to total data loss for the filesystem).

Issue is that once you GUP you never abide to file system flushing which write protect the page before writing to the disk. So because the page is still map with write permission to the device (assuming VFIO_IOMMU_MAP_DMA was a write map) then the device can write to the page while it is in the middle of being written back to disk. Consult your nearest file system specialist to ask him how bad that can be.

[I4]- Design issue, mdev design As Far As I Understand It is about
sharing a single device to multiple clients (most obvious case here is again QEMU guest). But you are going against that model, in fact AFAIUI you are doing the exect opposite. When there is no SVA/SVM you want only one mdev device that can not be share.

So this is counter intuitive to the mdev existing design. It is not about sharing device among multiple users but about giving exclusive access to the device to one user.

All the reasons above is why i believe a different model would serve you and your user better. Below is a design that avoids all of the above issues and still delivers all of your objectives with the exceptions of the third one [R3] when there is no SVA/SVM.

Create a subsystem (very much boiler plate code) which allow device to register themself against (very much like what you do in your current patchset but outside of VFIO).

That subsystem will create a device file for each registered system and expose a common API (ie set of ioctl) for each of those device files.

When user space create a queue (through an ioctl after opening the device file) the kernel can return -EBUSY if all the device queue are in use, or create a device queue and return a flag like SYNC_ONLY for device that do not have SVA/SVM.

For device with SVA/SVM at the time the process create a queue you bind the process PASID to the device queue. From there on the userspace can schedule commands and use the device without going to kernel space.

For device without SVA/SVM you create a fake queue that is just pure memory is not related to the device. From there on the userspace must call an ioctl every time it wants the device to consume its queue (hence why the SYNC_ONLY flag for synchronous operation only). The kernel portion read the fake queue expose to user space and copy commands into the real hardware queue but first it properly map any of the process memory needed for those commands to the device and adjust the device physical address with the one it gets from dma_map API.

With that model it is "easy" to listen to mmu_notifier and to abide by them to avoid issues [I1], [I3] and [I4]. You obviously avoid the [I2] issue by only mapping a fake device queue to userspace.

So yes with that models it means that every device that wish to support the non SVA/SVM case will have to do extra work (ie emulate its command queue in software in the kernel). But by doing so, you support an unlimited number of process on your device (ie all the process can share one single hardware command queues or multiple hardware queues).

The big advantages i see here is that the process do not have to worry about doing something wrong. You are protecting yourself and your user from stupid mistakes.

I hope this is useful to you.

Cheers,
Jérôme

#48 lore raw
Quoted 19 lines · Jerome Glisse

Date: Sun, 16 Sep 2018 21:42:44 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <[email protected]>
CC: Jonathan Corbet <[email protected]>, Herbert Xu
<[email protected]>, "David S . Miller" <davem-fT/PcQaiUtIeIZ0/[email protected]>, Joerg Roedel <[email protected]>, Alex Williamson <[email protected]>, Kenneth Lee <liguozhu-C8/M+/[email protected]>, Hao Fang <[email protected]>, Zhou Wang <wangzhou1-C8/M+/[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <pombredanne-od1rfyK75/[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org, [email protected], linux-accelerators-uLR06cmDAlY/[email protected], Lu Baolu <baolu.lu-VuQAYsv1563Yd54FQh9/[email protected]>, Sanjay Kumar <[email protected]>, [email protected]
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mutt/1.10.1 (2018-07-13)
Message-ID: <[email protected]>

Quoted 3 lines

So i want to summarize issues i have as this threads have dig deep into details. For this i would like to differentiate two cases first the easy one when relying on SVA/SVM. Then the second one when there is no SVA/SVM.

Thank you very much for the summary.

Quoted 13 lines

In both cases your objectives as i understand them:

[R1]- expose a common user space API that make it easy to share boiler
plate code accross many devices (discovering devices, opening device, creating context, creating command queue ...).
[R2]- try to share the device as much as possible up to device limits
(number of independant queues the device has)
[R3]- minimize syscall by allowing user space to directly schedule on the
device queue without a round trip to the kernel

I don't think i missed any.

Quoted 7 lines

(1) Device with SVA/SVM

For that case it is easy, you do not need to be in VFIO or part of any thing specific in the kernel. There is no security risk (modulo bug in the SVA/SVM silicon). Fork/exec is properly handle and binding a process to a device is just couple dozen lines of code.

This is right...logically. But the kernel has no clear definition about "Device with SVA/SVM" and no boiler plate for doing so. Then VFIO may become one of the boiler plate.

VFIO is one of the wrappers for IOMMU for user space. And maybe it is the only one. If we add that support within VFIO, which solve most of the problem of SVA/SVM, it will save a lot of work in the future.

I think this is the key confliction between us. So could Alex please say something here? If the VFIO is going to take this into its scope, we can try together to solve all the problem on the way. If it it is not, it is also simple, we can just go to another way to fulfill this part of requirements even we have to duplicate most of the code.

Another point I need to emphasis here: because we have to replace the hardware queue when fork, so it won't be very simple even in SVA/SVM case.

Quoted 13 lines

(2) Device does not have SVA/SVM (or it is disabled)

You want to still allow device to be part of your framework. However here i see fundamentals securities issues and you move the burden of being careful to user space which i think is a bad idea. We should never trus the userspace from kernel space.

To keep the same API for the user space code you want a 1:1 mapping between device physical address and process virtual address (ie if device access device physical address A it is accessing the same memory as what is backing the virtual address A in the process.

Quoted 8 lines

Security issues are on two things:
[I1]- fork/exec, a process who opened any such device and created an
active queue can transfer without its knowledge control of its commands queue through COW. The parent map some anonymous region to the device as a command queue buffer but because of COW the parent can be the first to copy on write and thus the child can inherit the original pages that are mapped to the hardware. Here parent lose control and child gain it.

This is indeed an issue. But it remains an issue only if you continue to use the queue and the memory after fork. We can use at_fork kinds of gadget to fix it in user space.

Quoted 1 line

From some perspectives, I think the issue can be solved by iommu_notifier. For

example, when the process is fork-ed, we can set the mapped device mmio space as COW for the child process, so a new queue can be created and set to the same state as the parent's if the space is accessed. Then we can have two separated queues for both the parent and the child. The memory part can be done in the same way.

The thing is, the same strategy can be applied to VFIO without changing its original feature.

Quoted 10 lines

[I2]- Because of [R3] you want to allow userspace to schedule commands
on the device without doing an ioctl and thus here user space can schedule any commands to the device with any address. What happens if that address have not been mapped by the user space is undefined and in fact can not be defined as what each IOMMU does on invalid address access is different from IOMMU to IOMMU.

In case of a bad IOMMU, or simply an IOMMU improperly setup by the kernel, this can potentialy allow user space to DMA anywhere.

I don't think this is an issue. If you cannot trust IOMMU and proper setup of IOMMU in kernel, you cannot trust anything. And the whole VFIO framework is untrustable.

Quoted 17 lines

[I3]- By relying on GUP in VFIO you are not abiding by the implicit
contract (at least i hope it is implicit) that you should not try to map to the device any file backed vma (private or share).

The VFIO code never check the vma controlling the addresses that are provided to VFIO_IOMMU_MAP_DMA ioctl. Which means that the user space can provide file backed range.

I am guessing that the VFIO code never had any issues because its number one user is QEMU and QEMU never does that (and that's good as no one should ever do that).

So if process does that you are opening your self to serious file system corruption (depending on file system this can lead to total data loss for the filesystem).

Quoted 7 lines

Issue is that once you GUP you never abide to file system flushing which write protect the page before writing to the disk. So because the page is still map with write permission to the device (assuming VFIO_IOMMU_MAP_DMA was a write map) then the device can write to the page while it is in the middle of being written back to disk. Consult your nearest file system specialist to ask him how bad that can be.

Same as I2, it is an issue, but the problem can be solved in VFIO if we really take it in the scope of VFIO.

Quoted 6 lines

[I4]- Design issue, mdev design As Far As I Understand It is about
sharing a single device to multiple clients (most obvious case here is again QEMU guest). But you are going against that model, in fact AFAIUI you are doing the exect opposite. When there is no SVA/SVM you want only one mdev device that can not be share.

Wait. It is NOT "I want only one mdev device when there is no SVA/SVM", it is "I can support only one mdev when there is no PASID support for the IOMMU".

Quoted 20 lines

So this is counter intuitive to the mdev existing design. It is not about sharing device among multiple users but about giving exclusive access to the device to one user.

All the reasons above is why i believe a different model would serve you and your user better. Below is a design that avoids all of the above issues and still delivers all of your objectives with the exceptions of the third one [R3] when there is no SVA/SVM.

Create a subsystem (very much boiler plate code) which allow device to register themself against (very much like what you do in your current patchset but outside of VFIO).

That subsystem will create a device file for each registered system and expose a common API (ie set of ioctl) for each of those device files.

Quoted 8 lines

When user space create a queue (through an ioctl after opening the device file) the kernel can return -EBUSY if all the device queue are in use, or create a device queue and return a flag like SYNC_ONLY for device that do not have SVA/SVM.

For device with SVA/SVM at the time the process create a queue you bind the process PASID to the device queue. From there on the userspace can schedule commands and use the device without going to kernel space.

As mentioned previously, this is not enough for fork scenario.

Quoted 11 lines

For device without SVA/SVM you create a fake queue that is just pure memory is not related to the device. From there on the userspace must call an ioctl every time it wants the device to consume its queue (hence why the SYNC_ONLY flag for synchronous operation only). The kernel portion read the fake queue expose to user space and copy commands into the real hardware queue but first it properly map any of the process memory needed for those commands to the device and adjust the device physical address with the one it gets from dma_map API.

But in this way, we will lost most of the benefit of avoiding syscall.

Quoted 9 lines

With that model it is "easy" to listen to mmu_notifier and to abide by them to avoid issues [I1], [I3] and [I4]. You obviously avoid the [I2] issue by only mapping a fake device queue to userspace.

So yes with that models it means that every device that wish to support the non SVA/SVM case will have to do extra work (ie emulate its command queue in software in the kernel). But by doing so, you support an unlimited number of process on your device (ie all the process can share one single hardware command queues or multiple hardware queues).

If I can do this, I will not need WarpDrive at all:(

Quoted 8 lines

The big advantages i see here is that the process do not have to worry about doing something wrong. You are protecting yourself and your user from stupid mistakes.

I hope this is useful to you.

Anyway, I will try to address the problem you mentioned in next version, and make both options (on-VFIO or off-VFIO) are available. Thanks.

Quoted 2 lines

Cheers,
Jérôme

Cheers

Signature · 1 line
			-Kenneth(Hisilicon)
#49 lore raw
Quoted 28 lines · Kenneth Lee
Quoted 3 lines · Jerome Glisse

So i want to summarize issues i have as this threads have dig deep into details. For this i would like to differentiate two cases first the easy one when relying on SVA/SVM. Then the second one when there is no SVA/SVM.

Thank you very much for the summary.

Quoted 13 lines

In both cases your objectives as i understand them:

[R1]- expose a common user space API that make it easy to share boiler
plate code accross many devices (discovering devices, opening device, creating context, creating command queue ...).
[R2]- try to share the device as much as possible up to device limits
(number of independant queues the device has)
[R3]- minimize syscall by allowing user space to directly schedule on the
device queue without a round trip to the kernel

I don't think i missed any.

Quoted 7 lines

(1) Device with SVA/SVM

For that case it is easy, you do not need to be in VFIO or part of any thing specific in the kernel. There is no security risk (modulo bug in the SVA/SVM silicon). Fork/exec is properly handle and binding a process to a device is just couple dozen lines of code.

Quoted 7 lines

This is right...logically. But the kernel has no clear definition about "Device with SVA/SVM" and no boiler plate for doing so. Then VFIO may become one of the boiler plate.

VFIO is one of the wrappers for IOMMU for user space. And maybe it is the only one. If we add that support within VFIO, which solve most of the problem of SVA/SVM, it will save a lot of work in the future.

You do not need to "wrap" IOMMU for SVA/SVM. Existing upstream SVA/SVM user all do the SVA/SVM setup in couple dozen lines and i failed to see how it would require any more than that in your case.

Quoted 8 lines

I think this is the key confliction between us. So could Alex please say something here? If the VFIO is going to take this into its scope, we can try together to solve all the problem on the way. If it it is not, it is also simple, we can just go to another way to fulfill this part of requirements even we have to duplicate most of the code.

Another point I need to emphasis here: because we have to replace the hardware queue when fork, so it won't be very simple even in SVA/SVM case.

I am assuming hardware queue can only be setup by the kernel and thus you are totaly safe forkwise as the queue is setup against a PASID and the child does not bind to any PASID and you use VM_DONTCOPY on the mmap of the hardware MMIO queue because you should really use that flag for that.

Quoted 21 lines
Quoted 12 lines

(2) Device does not have SVA/SVM (or it is disabled)

You want to still allow device to be part of your framework. However here i see fundamentals securities issues and you move the burden of being careful to user space which i think is a bad idea. We should never trus the userspace from kernel space.

To keep the same API for the user space code you want a 1:1 mapping between device physical address and process virtual address (ie if device access device physical address A it is accessing the same memory as what is backing the virtual address A in the process.

Quoted 8 lines

Security issues are on two things:
[I1]- fork/exec, a process who opened any such device and created an
active queue can transfer without its knowledge control of its commands queue through COW. The parent map some anonymous region to the device as a command queue buffer but because of COW the parent can be the first to copy on write and thus the child can inherit the original pages that are mapped to the hardware. Here parent lose control and child gain it.

Quoted 3 lines

This is indeed an issue. But it remains an issue only if you continue to use the queue and the memory after fork. We can use at_fork kinds of gadget to fix it in user space.

Trusting user space is a no go from my point of view.

Quoted 6 lines

From some perspectives, I think the issue can be solved by iommu_notifier. For example, when the process is fork-ed, we can set the mapped device mmio space as COW for the child process, so a new queue can be created and set to the same state as the parent's if the space is accessed. Then we can have two separated queues for both the parent and the child. The memory part can be done in the same way.

The mmap of mmio space for the queue is not an issue just use VM_DONTCOPY for it. Issue is with COW and IOMMU mapping of pages and this can not be solve in your model.

Quoted 2 lines

The thing is, the same strategy can be applied to VFIO without changing its original feature.

No it can not it would break existing VFIO contract (which only should be use against private anonymous vma).

Quoted 9 lines
Quoted 9 lines

[I2]- Because of [R3] you want to allow userspace to schedule commands
on the device without doing an ioctl and thus here user space can schedule any commands to the device with any address. What happens if that address have not been mapped by the user space is undefined and in fact can not be defined as what each IOMMU does on invalid address access is different from IOMMU to IOMMU.

Quoted 6 lines
Quoted 2 lines

In case of a bad IOMMU, or simply an IOMMU improperly setup by the kernel, this can potentialy allow user space to DMA anywhere.

I don't think this is an issue. If you cannot trust IOMMU and proper setup of IOMMU in kernel, you cannot trust anything. And the whole VFIO framework is untrustable.

VFIO device is usualy restricted to trusted user and other device that do DMA do various checks to make sure user space can not abuse them, the assumption i have always seen so far is to not trust that IOMMU will do all the work. So exposing user space access to device with DMA capabilities should be done carefuly IMHO.

To be thorough list of potential bugs i am concern about:
- IOMMU hardware bug
- IOMMU does not isolate device after too many fail attempt
- firmware setup the IOMMU in some unsafe way and linux kernel does not catch that (like passthrough on error or when there is no entry for the PA which i am told is a thing for debug)
- bug in the linux IOMMU kernel driver

Quoted 25 lines
Quoted 17 lines

[I3]- By relying on GUP in VFIO you are not abiding by the implicit
contract (at least i hope it is implicit) that you should not try to map to the device any file backed vma (private or share).

The VFIO code never check the vma controlling the addresses that are provided to VFIO_IOMMU_MAP_DMA ioctl. Which means that the user space can provide file backed range.

I am guessing that the VFIO code never had any issues because its number one user is QEMU and QEMU never does that (and that's good as no one should ever do that).

So if process does that you are opening your self to serious file system corruption (depending on file system this can lead to total data loss for the filesystem).

Quoted 7 lines

Issue is that once you GUP you never abide to file system flushing which write protect the page before writing to the disk. So because the page is still map with write permission to the device (assuming VFIO_IOMMU_MAP_DMA was a write map) then the device can write to the page while it is in the middle of being written back to disk. Consult your nearest file system specialist to ask him how bad that can be.

Quoted 2 lines

Same as I2, it is an issue, but the problem can be solved in VFIO if we really take it in the scope of VFIO.

Except it can not be solve without breaking VFIO. If you use mmu_notifier it means that the the IOMMU mapping can vanish at _any_ time and because you allow user space to directly schedule work on the hardware command queue than you do not have any synchronization point to use.

When notifier calls you must stop all hardware access and wait for any pending work that might still dereference affected range. Finaly you can restore mapping only once the notifier is done. AFAICT this would break all existing VFIO user.

So solving that for your case it means you would have to:
warp_invalidate_range_callback() {
- take some lock to protect against restore
- unmap the command queue (zap_range) from userspace to stop further commands
- wait for any pending commands on the hardware to complete
- clear all IOMMU mappings for the range
- put_pages()
- drop restore lock
return to let invalidation complete its works
}

warp_restore() {
// This is call by the page fault handler on the command queue mapped // to user space.
- take restore lock
- go over all IOMMU mapping and restore them (GUP)
- remap command queue to userspace
- drop restore lock
}

This model does not work for VFIO existing users AFAICT.

Quoted 8 lines
Quoted 5 lines

[I4]- Design issue, mdev design As Far As I Understand It is about
sharing a single device to multiple clients (most obvious case here is again QEMU guest). But you are going against that model, in fact AFAIUI you are doing the exect opposite. When there is no SVA/SVM you want only one mdev device that can not be share.

Wait. It is NOT "I want only one mdev device when there is no SVA/SVM", it is "I can support only one mdev when there is no PASID support for the IOMMU".

Except you can support more than one user when no SVA/SVM with the model i outlined below.

Quoted 25 lines
Quoted 25 lines

So this is counter intuitive to the mdev existing design. It is not about sharing device among multiple users but about giving exclusive access to the device to one user.

All the reasons above is why i believe a different model would serve you and your user better. Below is a design that avoids all of the above issues and still delivers all of your objectives with the exceptions of the third one [R3] when there is no SVA/SVM.

Create a subsystem (very much boiler plate code) which allow device to register themself against (very much like what you do in your current patchset but outside of VFIO).

That subsystem will create a device file for each registered system and expose a common API (ie set of ioctl) for each of those device files.

When user space create a queue (through an ioctl after opening the device file) the kernel can return -EBUSY if all the device queue are in use, or create a device queue and return a flag like SYNC_ONLY for device that do not have SVA/SVM.

Quoted 5 lines
Quoted 3 lines

For device with SVA/SVM at the time the process create a queue you bind the process PASID to the device queue. From there on the userspace can schedule commands and use the device without going to kernel space.

As mentioned previously, this is not enough for fork scenario.

It is for every existing user of SVA/SVM so i fail to see why it would be any different in your case. Note that they all use VM_DONTCOPY flag on the queue mapped to userspace.

Quoted 11 lines
Quoted 10 lines

For device without SVA/SVM you create a fake queue that is just pure memory is not related to the device. From there on the userspace must call an ioctl every time it wants the device to consume its queue (hence why the SYNC_ONLY flag for synchronous operation only). The kernel portion read the fake queue expose to user space and copy commands into the real hardware queue but first it properly map any of the process memory needed for those commands to the device and adjust the device physical address with the one it gets from dma_map API.

Quoted 1 line

But in this way, we will lost most of the benefit of avoiding syscall.

Yes but only when there is SVA/SVM. What i am trying to stress is that there is no sane way to mirror user space address space onto device without mmu_notifier so short of that this model where you have to syscall to schedules thing on the hardware is the easiest thing to do.

Quoted 11 lines
Quoted 9 lines

With that model it is "easy" to listen to mmu_notifier and to abide by them to avoid issues [I1], [I3] and [I4]. You obviously avoid the [I2] issue by only mapping a fake device queue to userspace.

So yes with that models it means that every device that wish to support the non SVA/SVM case will have to do extra work (ie emulate its command queue in software in the kernel). But by doing so, you support an unlimited number of process on your device (ie all the process can share one single hardware command queues or multiple hardware queues).

If I can do this, I will not need WarpDrive at all:(

This is only needed if you wish to support non SVA/SVM, shifting the burden to the kernel is always the thing to do especially if they are legitimate security concerns.

Cheers,
Jérôme

#50 lore raw
Quoted 97 lines · Jerome Glisse

Date: Mon, 17 Sep 2018 08:37:45 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <liguozhu-C8/M+/[email protected]>
CC: Kenneth Lee <[email protected]>, Herbert Xu
<[email protected]>, [email protected], Jonathan Corbet <[email protected]>, Greg Kroah-Hartman <[email protected]>, Joerg Roedel <[email protected]>, [email protected], Sanjay Kumar <[email protected]>, Hao Fang <[email protected]>, iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org, [email protected], [email protected], Alex Williamson <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], Zhou Wang <wangzhou1-C8/M+/[email protected]>, Philippe Ombredanne <pombredanne-od1rfyK75/[email protected]>, Zaibo Xu <[email protected]>, "David S . Miller" <davem-fT/PcQaiUtIeIZ0/[email protected]>, linux-accelerators-uLR06cmDAlY/[email protected], Lu Baolu <baolu.lu-VuQAYsv1563Yd54FQh9/[email protected]>
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mutt/1.10.1 (2018-07-13)
Message-ID: <[email protected]>

Quoted 28 lines · Kenneth Lee
Quoted 3 lines · Jerome Glisse

So i want to summarize issues i have as this threads have dig deep into details. For this i would like to differentiate two cases first the easy one when relying on SVA/SVM. Then the second one when there is no SVA/SVM.

Thank you very much for the summary.

Quoted 13 lines

In both cases your objectives as i understand them:

[R1]- expose a common user space API that make it easy to share boiler
plate code accross many devices (discovering devices, opening device, creating context, creating command queue ...).
[R2]- try to share the device as much as possible up to device limits
(number of independant queues the device has)
[R3]- minimize syscall by allowing user space to directly schedule on the
device queue without a round trip to the kernel

I don't think i missed any.

Quoted 7 lines

(1) Device with SVA/SVM

For that case it is easy, you do not need to be in VFIO or part of any thing specific in the kernel. There is no security risk (modulo bug in the SVA/SVM silicon). Fork/exec is properly handle and binding a process to a device is just couple dozen lines of code.

Quoted 7 lines

This is right...logically. But the kernel has no clear definition about "Device with SVA/SVM" and no boiler plate for doing so. Then VFIO may become one of the boiler plate.

VFIO is one of the wrappers for IOMMU for user space. And maybe it is the only one. If we add that support within VFIO, which solve most of the problem of SVA/SVM, it will save a lot of work in the future.

You do not need to "wrap" IOMMU for SVA/SVM. Existing upstream SVA/SVM user all do the SVA/SVM setup in couple dozen lines and i failed to see how it would require any more than that in your case.

Quoted 8 lines

I think this is the key confliction between us. So could Alex please say something here? If the VFIO is going to take this into its scope, we can try together to solve all the problem on the way. If it it is not, it is also simple, we can just go to another way to fulfill this part of requirements even we have to duplicate most of the code.

Another point I need to emphasis here: because we have to replace the hardware queue when fork, so it won't be very simple even in SVA/SVM case.

I am assuming hardware queue can only be setup by the kernel and thus you are totaly safe forkwise as the queue is setup against a PASID and the child does not bind to any PASID and you use VM_DONTCOPY on the mmap of the hardware MMIO queue because you should really use that flag for that.

Quoted 21 lines
Quoted 20 lines

(2) Device does not have SVA/SVM (or it is disabled)

You want to still allow device to be part of your framework. However here i see fundamentals securities issues and you move the burden of being careful to user space which i think is a bad idea. We should never trus the userspace from kernel space.

To keep the same API for the user space code you want a 1:1 mapping between device physical address and process virtual address (ie if device access device physical address A it is accessing the same memory as what is backing the virtual address A in the process.

Security issues are on two things:
[I1]- fork/exec, a process who opened any such device and created an
active queue can transfer without its knowledge control of its commands queue through COW. The parent map some anonymous region to the device as a command queue buffer but because of COW the parent can be the first to copy on write and thus the child can inherit the original pages that are mapped to the hardware. Here parent lose control and child gain it.

Quoted 5 lines
Quoted 3 lines

This is indeed an issue. But it remains an issue only if you continue to use the queue and the memory after fork. We can use at_fork kinds of gadget to fix it in user space.

Trusting user space is a no go from my point of view.

Can we dive deeper on this? Maybe we have different understanding on "Trusting user space". As my understanding, "trusting user space" means "no matter what the user process does, it should only hurt itself and anything give to it, no the kernel and the other process".

In our case, we create a channel between a process and the hardware. The process can do whateven it like to its own memory the channel itself. It won't hurt the other process and the kernel. And if the process fork a child and give the channel to the child, it should the freedom on those resource remain within the parent and the child. We are not trust another else.

So do you refer to something else here?

Quoted 191 lines
Quoted 8 lines

From some perspectives, I think the issue can be solved by iommu_notifier. For example, when the process is fork-ed, we can set the mapped device mmio space as COW for the child process, so a new queue can be created and set to the same state as the parent's if the space is accessed. Then we can have two separated queues for both the parent and the child. The memory part can be done in the same way.

The mmap of mmio space for the queue is not an issue just use VM_DONTCOPY for it. Issue is with COW and IOMMU mapping of pages and this can not be solve in your model.

Quoted 2 lines

The thing is, the same strategy can be applied to VFIO without changing its original feature.

No it can not it would break existing VFIO contract (which only should be use against private anonymous vma).

Quoted 9 lines
Quoted 9 lines

[I2]- Because of [R3] you want to allow userspace to schedule commands
on the device without doing an ioctl and thus here user space can schedule any commands to the device with any address. What happens if that address have not been mapped by the user space is undefined and in fact can not be defined as what each IOMMU does on invalid address access is different from IOMMU to IOMMU.

Quoted 6 lines
Quoted 2 lines

In case of a bad IOMMU, or simply an IOMMU improperly setup by the kernel, this can potentialy allow user space to DMA anywhere.

I don't think this is an issue. If you cannot trust IOMMU and proper setup of IOMMU in kernel, you cannot trust anything. And the whole VFIO framework is untrustable.

VFIO device is usualy restricted to trusted user and other device that do DMA do various checks to make sure user space can not abuse them, the assumption i have always seen so far is to not trust that IOMMU will do all the work. So exposing user space access to device with DMA capabilities should be done carefuly IMHO.

To be thorough list of potential bugs i am concern about:
- IOMMU hardware bug
- IOMMU does not isolate device after too many fail attempt
- firmware setup the IOMMU in some unsafe way and linux kernel does not catch that (like passthrough on error or when there is no entry for the PA which i am told is a thing for debug)
- bug in the linux IOMMU kernel driver

Quoted 25 lines
Quoted 17 lines

[I3]- By relying on GUP in VFIO you are not abiding by the implicit
contract (at least i hope it is implicit) that you should not try to map to the device any file backed vma (private or share).

The VFIO code never check the vma controlling the addresses that are provided to VFIO_IOMMU_MAP_DMA ioctl. Which means that the user space can provide file backed range.

I am guessing that the VFIO code never had any issues because its number one user is QEMU and QEMU never does that (and that's good as no one should ever do that).

So if process does that you are opening your self to serious file system corruption (depending on file system this can lead to total data loss for the filesystem).

Quoted 7 lines

Issue is that once you GUP you never abide to file system flushing which write protect the page before writing to the disk. So because the page is still map with write permission to the device (assuming VFIO_IOMMU_MAP_DMA was a write map) then the device can write to the page while it is in the middle of being written back to disk. Consult your nearest file system specialist to ask him how bad that can be.

Quoted 2 lines

Same as I2, it is an issue, but the problem can be solved in VFIO if we really take it in the scope of VFIO.

Except it can not be solve without breaking VFIO. If you use mmu_notifier it means that the the IOMMU mapping can vanish at _any_ time and because you allow user space to directly schedule work on the hardware command queue than you do not have any synchronization point to use.

When notifier calls you must stop all hardware access and wait for any pending work that might still dereference affected range. Finaly you can restore mapping only once the notifier is done. AFAICT this would break all existing VFIO user.

So solving that for your case it means you would have to:
warp_invalidate_range_callback() {
- take some lock to protect against restore
- unmap the command queue (zap_range) from userspace to stop further commands
- wait for any pending commands on the hardware to complete
- clear all IOMMU mappings for the range
- put_pages()
- drop restore lock
return to let invalidation complete its works
}

warp_restore() {
// This is call by the page fault handler on the command queue mapped // to user space.
- take restore lock
- go over all IOMMU mapping and restore them (GUP)
- remap command queue to userspace
- drop restore lock
}

This model does not work for VFIO existing users AFAICT.

Quoted 8 lines
Quoted 5 lines

[I4]- Design issue, mdev design As Far As I Understand It is about
sharing a single device to multiple clients (most obvious case here is again QEMU guest). But you are going against that model, in fact AFAIUI you are doing the exect opposite. When there is no SVA/SVM you want only one mdev device that can not be share.

Wait. It is NOT "I want only one mdev device when there is no SVA/SVM", it is "I can support only one mdev when there is no PASID support for the IOMMU".

Except you can support more than one user when no SVA/SVM with the model i outlined below.

Quoted 25 lines
Quoted 25 lines

So this is counter intuitive to the mdev existing design. It is not about sharing device among multiple users but about giving exclusive access to the device to one user.

All the reasons above is why i believe a different model would serve you and your user better. Below is a design that avoids all of the above issues and still delivers all of your objectives with the exceptions of the third one [R3] when there is no SVA/SVM.

Create a subsystem (very much boiler plate code) which allow device to register themself against (very much like what you do in your current patchset but outside of VFIO).

That subsystem will create a device file for each registered system and expose a common API (ie set of ioctl) for each of those device files.

When user space create a queue (through an ioctl after opening the device file) the kernel can return -EBUSY if all the device queue are in use, or create a device queue and return a flag like SYNC_ONLY for device that do not have SVA/SVM.

Quoted 5 lines
Quoted 3 lines

For device with SVA/SVM at the time the process create a queue you bind the process PASID to the device queue. From there on the userspace can schedule commands and use the device without going to kernel space.

As mentioned previously, this is not enough for fork scenario.

It is for every existing user of SVA/SVM so i fail to see why it would be any different in your case. Note that they all use VM_DONTCOPY flag on the queue mapped to userspace.

Quoted 11 lines
Quoted 10 lines

For device without SVA/SVM you create a fake queue that is just pure memory is not related to the device. From there on the userspace must call an ioctl every time it wants the device to consume its queue (hence why the SYNC_ONLY flag for synchronous operation only). The kernel portion read the fake queue expose to user space and copy commands into the real hardware queue but first it properly map any of the process memory needed for those commands to the device and adjust the device physical address with the one it gets from dma_map API.

Quoted 1 line

But in this way, we will lost most of the benefit of avoiding syscall.

Yes but only when there is SVA/SVM. What i am trying to stress is that there is no sane way to mirror user space address space onto device without mmu_notifier so short of that this model where you have to syscall to schedules thing on the hardware is the easiest thing to do.

Quoted 10 lines
Quoted 9 lines

With that model it is "easy" to listen to mmu_notifier and to abide by them to avoid issues [I1], [I3] and [I4]. You obviously avoid the [I2] issue by only mapping a fake device queue to userspace.

So yes with that models it means that every device that wish to support the non SVA/SVM case will have to do extra work (ie emulate its command queue in software in the kernel). But by doing so, you support an unlimited number of process on your device (ie all the process can share one single hardware command queues or multiple hardware queues).

Quoted 5 lines
Quoted 1 line

If I can do this, I will not need WarpDrive at all:(

This is only needed if you wish to support non SVA/SVM, shifting the burden to the kernel is always the thing to do especially if they are legitimate security concerns.

For the other part, please give me some more time to write some test code and come back to the discussino. Thank you.

Cheers

Quoted 4 lines

Cheers,
Jérôme

Signature · 1 line
			-Kenneth(Hisilicon)
#51 lore raw
Quoted 91 lines · Kenneth Lee
Quoted 79 lines · Jerome Glisse
Quoted 28 lines · Kenneth Lee
Quoted 3 lines · Jerome Glisse

So i want to summarize issues i have as this threads have dig deep into details. For this i would like to differentiate two cases first the easy one when relying on SVA/SVM. Then the second one when there is no SVA/SVM.

Thank you very much for the summary.

Quoted 13 lines

In both cases your objectives as i understand them:

[R1]- expose a common user space API that make it easy to share boiler
plate code accross many devices (discovering devices, opening device, creating context, creating command queue ...).
[R2]- try to share the device as much as possible up to device limits
(number of independant queues the device has)
[R3]- minimize syscall by allowing user space to directly schedule on the
device queue without a round trip to the kernel

I don't think i missed any.

Quoted 7 lines

(1) Device with SVA/SVM

For that case it is easy, you do not need to be in VFIO or part of any thing specific in the kernel. There is no security risk (modulo bug in the SVA/SVM silicon). Fork/exec is properly handle and binding a process to a device is just couple dozen lines of code.

Quoted 7 lines

This is right...logically. But the kernel has no clear definition about "Device with SVA/SVM" and no boiler plate for doing so. Then VFIO may become one of the boiler plate.

VFIO is one of the wrappers for IOMMU for user space. And maybe it is the only one. If we add that support within VFIO, which solve most of the problem of SVA/SVM, it will save a lot of work in the future.

You do not need to "wrap" IOMMU for SVA/SVM. Existing upstream SVA/SVM user all do the SVA/SVM setup in couple dozen lines and i failed to see how it would require any more than that in your case.

Quoted 8 lines

I think this is the key confliction between us. So could Alex please say something here? If the VFIO is going to take this into its scope, we can try together to solve all the problem on the way. If it it is not, it is also simple, we can just go to another way to fulfill this part of requirements even we have to duplicate most of the code.

Another point I need to emphasis here: because we have to replace the hardware queue when fork, so it won't be very simple even in SVA/SVM case.

I am assuming hardware queue can only be setup by the kernel and thus you are totaly safe forkwise as the queue is setup against a PASID and the child does not bind to any PASID and you use VM_DONTCOPY on the mmap of the hardware MMIO queue because you should really use that flag for that.

Quoted 21 lines
Quoted 20 lines

(2) Device does not have SVA/SVM (or it is disabled)

You want to still allow device to be part of your framework. However here i see fundamentals securities issues and you move the burden of being careful to user space which i think is a bad idea. We should never trus the userspace from kernel space.

To keep the same API for the user space code you want a 1:1 mapping between device physical address and process virtual address (ie if device access device physical address A it is accessing the same memory as what is backing the virtual address A in the process.

Security issues are on two things:
[I1]- fork/exec, a process who opened any such device and created an
active queue can transfer without its knowledge control of its commands queue through COW. The parent map some anonymous region to the device as a command queue buffer but because of COW the parent can be the first to copy on write and thus the child can inherit the original pages that are mapped to the hardware. Here parent lose control and child gain it.

Quoted 5 lines
Quoted 3 lines

This is indeed an issue. But it remains an issue only if you continue to use the queue and the memory after fork. We can use at_fork kinds of gadget to fix it in user space.

Trusting user space is a no go from my point of view.

Can we dive deeper on this? Maybe we have different understanding on "Trusting user space". As my understanding, "trusting user space" means "no matter what the user process does, it should only hurt itself and anything give to it, no the kernel and the other process".

Quoted 8 lines

In our case, we create a channel between a process and the hardware. The process can do whateven it like to its own memory the channel itself. It won't hurt the other process and the kernel. And if the process fork a child and give the channel to the child, it should the freedom on those resource remain within the parent and the child. We are not trust another else.

So do you refer to something else here?

I am refering to COW giving control to the child on to what happens in the parent from device point of view. A process hurting itself is fine, but if process now has to do special steps to protect from its child ie make sure that its childs can not hurt it, then i see that as a kernel bug. We can not ask user space process to know about all the thousands things that needs to be done to avoid issues with each device driver that the process may use (process can be totaly ignorant it is using a device if that device is use by a library it links to).

Maybe what needs to happen will explain it better. So if userspace wants to be secure and protect itself from its child taking over the device through COW:

- parent opened a device and is using it

... when parent wants to fork/exec it must:

- parent _must_ flush device command queue and wait for the device to finish all pending jobs

- parent _must_ unmap all range mapped to the device

- parent should first close device file (unless you force set the CLOEXEC flag in the kernel)/it could also just flush but if you are not mapping the device command queue with VM_DONTCOPY then you should really be closing the device

- now parent can fork/exec

- parent must force COW ie write at least one byte to _all_ pages in the range it wants to use with the device

- parent re-open the device and re-initialize everything

So this is putting quite a burden on a number of steps the parent _must_ do in order to keep control of memory exposed to the device. Not doing so can potentialy lead (it depends on who does the COW first) to the child taking control of memory use by the device, memory which was mapped by the parent before the child was created.

Forcing CLOEXEC and VM_DONTCOPY somewhat help to simplify this, but you still need to stop, flush, unmap, before fork/exec and then re-init everything after.

This is only when not using SVA/SVM, SVA/SVM is totaly fine from that point of view, no issues whatsoever.

The solution i outlined in previous email do not have that above issue either, no need to rely on user space doing that dance.

Cheers,
Jérôme

#52 lore raw
Quoted 167 lines · Jerome Glisse

Date: Tue, 18 Sep 2018 09:03:14 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <[email protected]>
CC: Kenneth Lee <[email protected]>, Alex Williamson
<[email protected]>, Herbert Xu <[email protected]>, [email protected], Jonathan Corbet <[email protected]>, Greg Kroah-Hartman <[email protected]>, Joerg Roedel <[email protected]>, [email protected], Sanjay Kumar <[email protected]>, Hao Fang <[email protected]>, [email protected], [email protected], [email protected], "David S . Miller" <[email protected]>, [email protected], Zhou Wang <[email protected]>, Philippe Ombredanne <[email protected]>, Thomas Gleixner <[email protected]>, Zaibo Xu <[email protected]>, [email protected], Lu Baolu <[email protected]>
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
User-Agent: Mutt/1.10.1 (2018-07-13)
Message-ID: <[email protected]>

Quoted 91 lines · Kenneth Lee
Quoted 79 lines · Jerome Glisse
Quoted 28 lines · Kenneth Lee
Quoted 3 lines · Jerome Glisse

So i want to summarize issues i have as this threads have dig deep into details. For this i would like to differentiate two cases first the easy one when relying on SVA/SVM. Then the second one when there is no SVA/SVM.

Thank you very much for the summary.

Quoted 13 lines

In both cases your objectives as i understand them:

[R1]- expose a common user space API that make it easy to share boiler
plate code accross many devices (discovering devices, opening device, creating context, creating command queue ...).
[R2]- try to share the device as much as possible up to device limits
(number of independant queues the device has)
[R3]- minimize syscall by allowing user space to directly schedule on the
device queue without a round trip to the kernel

I don't think i missed any.

Quoted 7 lines

(1) Device with SVA/SVM

For that case it is easy, you do not need to be in VFIO or part of any thing specific in the kernel. There is no security risk (modulo bug in the SVA/SVM silicon). Fork/exec is properly handle and binding a process to a device is just couple dozen lines of code.

Quoted 7 lines

This is right...logically. But the kernel has no clear definition about "Device with SVA/SVM" and no boiler plate for doing so. Then VFIO may become one of the boiler plate.

VFIO is one of the wrappers for IOMMU for user space. And maybe it is the only one. If we add that support within VFIO, which solve most of the problem of SVA/SVM, it will save a lot of work in the future.

You do not need to "wrap" IOMMU for SVA/SVM. Existing upstream SVA/SVM user all do the SVA/SVM setup in couple dozen lines and i failed to see how it would require any more than that in your case.

Quoted 8 lines

I think this is the key confliction between us. So could Alex please say something here? If the VFIO is going to take this into its scope, we can try together to solve all the problem on the way. If it it is not, it is also simple, we can just go to another way to fulfill this part of requirements even we have to duplicate most of the code.

Another point I need to emphasis here: because we have to replace the hardware queue when fork, so it won't be very simple even in SVA/SVM case.

I am assuming hardware queue can only be setup by the kernel and thus you are totaly safe forkwise as the queue is setup against a PASID and the child does not bind to any PASID and you use VM_DONTCOPY on the mmap of the hardware MMIO queue because you should really use that flag for that.

Quoted 21 lines
Quoted 20 lines

(2) Device does not have SVA/SVM (or it is disabled)

You want to still allow device to be part of your framework. However here i see fundamentals securities issues and you move the burden of being careful to user space which i think is a bad idea. We should never trus the userspace from kernel space.

To keep the same API for the user space code you want a 1:1 mapping between device physical address and process virtual address (ie if device access device physical address A it is accessing the same memory as what is backing the virtual address A in the process.

Security issues are on two things:
[I1]- fork/exec, a process who opened any such device and created an
active queue can transfer without its knowledge control of its commands queue through COW. The parent map some anonymous region to the device as a command queue buffer but because of COW the parent can be the first to copy on write and thus the child can inherit the original pages that are mapped to the hardware. Here parent lose control and child gain it.

Quoted 5 lines
Quoted 3 lines

This is indeed an issue. But it remains an issue only if you continue to use the queue and the memory after fork. We can use at_fork kinds of gadget to fix it in user space.

Trusting user space is a no go from my point of view.

Can we dive deeper on this? Maybe we have different understanding on "Trusting user space". As my understanding, "trusting user space" means "no matter what the user process does, it should only hurt itself and anything give to it, no the kernel and the other process".

Quoted 8 lines

In our case, we create a channel between a process and the hardware. The process can do whateven it like to its own memory the channel itself. It won't hurt the other process and the kernel. And if the process fork a child and give the channel to the child, it should the freedom on those resource remain within the parent and the child. We are not trust another else.

So do you refer to something else here?

I am refering to COW giving control to the child on to what happens in the parent from device point of view. A process hurting itself is fine, but if process now has to do special steps to protect from its child ie make sure that its childs can not hurt it, then i see that as a kernel bug. We can not ask user space process to know about all the thousands things that needs to be done to avoid issues with each device driver that the process may use (process can be totaly ignorant it is using a device if that device is use by a library it links to).

Maybe what needs to happen will explain it better. So if userspace wants to be secure and protect itself from its child taking over the device through COW:

- parent opened a device and is using it

... when parent wants to fork/exec it must:

- parent _must_ flush device command queue and wait for the device to finish all pending jobs

- parent _must_ unmap all range mapped to the device

- parent should first close device file (unless you force set the CLOEXEC flag in the kernel)/it could also just flush but if you are not mapping the device command queue with VM_DONTCOPY then you should really be closing the device

- now parent can fork/exec

- parent must force COW ie write at least one byte to _all_ pages in the range it wants to use with the device

- parent re-open the device and re-initialize everything

So this is putting quite a burden on a number of steps the parent _must_ do in order to keep control of memory exposed to the device. Not doing so can potentialy lead (it depends on who does the COW first) to the child taking control of memory use by the device, memory which was mapped by the parent before the child was created.

Forcing CLOEXEC and VM_DONTCOPY somewhat help to simplify this, but you still need to stop, flush, unmap, before fork/exec and then re-init everything after.

Quoted 5 lines

This is only when not using SVA/SVM, SVA/SVM is totaly fine from that point of view, no issues whatsoever.

The solution i outlined in previous email do not have that above issue either, no need to rely on user space doing that dance.

Thank you. I get the point. I'm now trying to see if I can solve the problem by seting the vma to VM_SHARED when the portiong is "shared to the hardware".

Quoted 3 lines

Cheers,
Jérôme

Signature · 14 lines
			-Kenneth(Hisilicon)

================================================================================
本邮件及其附件含有华为公司的保密信息,仅限于发送给上面地址中列出的个人或群组。禁
止任何其他人以任何形式使用(包括但不限于全部或部分地泄露、复制、或散发)本邮件中
的信息。如果您错收了本邮件,请您立即电话或邮件通知发件人并删除本邮件!
This e-mail and its attachments contain confidential information from HUAWEI,
which is intended only for the person or entity whose address is listed above.
Any use of the 
information contained herein in any way (including, but not limited to, total or
partial disclosure, reproduction, or dissemination) by persons other than the
intended 
recipient(s) is prohibited. If you receive this e-mail in error, please notify
the sender by phone or email immediately and delete it!
#53 lore raw
Quoted 150 lines · Kenneth Lee
Quoted 149 lines · Jerome Glisse
Quoted 91 lines · Kenneth Lee
Quoted 79 lines · Jerome Glisse
Quoted 28 lines · Kenneth Lee
Quoted 3 lines · Jerome Glisse

So i want to summarize issues i have as this threads have dig deep into details. For this i would like to differentiate two cases first the easy one when relying on SVA/SVM. Then the second one when there is no SVA/SVM.

Thank you very much for the summary.

Quoted 13 lines

In both cases your objectives as i understand them:

[R1]- expose a common user space API that make it easy to share boiler
plate code accross many devices (discovering devices, opening device, creating context, creating command queue ...).
[R2]- try to share the device as much as possible up to device limits
(number of independant queues the device has)
[R3]- minimize syscall by allowing user space to directly schedule on the
device queue without a round trip to the kernel

I don't think i missed any.

Quoted 7 lines

(1) Device with SVA/SVM

For that case it is easy, you do not need to be in VFIO or part of any thing specific in the kernel. There is no security risk (modulo bug in the SVA/SVM silicon). Fork/exec is properly handle and binding a process to a device is just couple dozen lines of code.

Quoted 7 lines

This is right...logically. But the kernel has no clear definition about "Device with SVA/SVM" and no boiler plate for doing so. Then VFIO may become one of the boiler plate.

VFIO is one of the wrappers for IOMMU for user space. And maybe it is the only one. If we add that support within VFIO, which solve most of the problem of SVA/SVM, it will save a lot of work in the future.

You do not need to "wrap" IOMMU for SVA/SVM. Existing upstream SVA/SVM user all do the SVA/SVM setup in couple dozen lines and i failed to see how it would require any more than that in your case.

Quoted 8 lines

I think this is the key confliction between us. So could Alex please say something here? If the VFIO is going to take this into its scope, we can try together to solve all the problem on the way. If it it is not, it is also simple, we can just go to another way to fulfill this part of requirements even we have to duplicate most of the code.

Another point I need to emphasis here: because we have to replace the hardware queue when fork, so it won't be very simple even in SVA/SVM case.

I am assuming hardware queue can only be setup by the kernel and thus you are totaly safe forkwise as the queue is setup against a PASID and the child does not bind to any PASID and you use VM_DONTCOPY on the mmap of the hardware MMIO queue because you should really use that flag for that.

Quoted 21 lines
Quoted 20 lines

(2) Device does not have SVA/SVM (or it is disabled)

You want to still allow device to be part of your framework. However here i see fundamentals securities issues and you move the burden of being careful to user space which i think is a bad idea. We should never trus the userspace from kernel space.

To keep the same API for the user space code you want a 1:1 mapping between device physical address and process virtual address (ie if device access device physical address A it is accessing the same memory as what is backing the virtual address A in the process.

Security issues are on two things:
[I1]- fork/exec, a process who opened any such device and created an
active queue can transfer without its knowledge control of its commands queue through COW. The parent map some anonymous region to the device as a command queue buffer but because of COW the parent can be the first to copy on write and thus the child can inherit the original pages that are mapped to the hardware. Here parent lose control and child gain it.

Quoted 5 lines
Quoted 3 lines

This is indeed an issue. But it remains an issue only if you continue to use the queue and the memory after fork. We can use at_fork kinds of gadget to fix it in user space.

Trusting user space is a no go from my point of view.

Can we dive deeper on this? Maybe we have different understanding on "Trusting user space". As my understanding, "trusting user space" means "no matter what the user process does, it should only hurt itself and anything give to it, no the kernel and the other process".

Quoted 8 lines

In our case, we create a channel between a process and the hardware. The process can do whateven it like to its own memory the channel itself. It won't hurt the other process and the kernel. And if the process fork a child and give the channel to the child, it should the freedom on those resource remain within the parent and the child. We are not trust another else.

So do you refer to something else here?

I am refering to COW giving control to the child on to what happens in the parent from device point of view. A process hurting itself is fine, but if process now has to do special steps to protect from its child ie make sure that its childs can not hurt it, then i see that as a kernel bug. We can not ask user space process to know about all the thousands things that needs to be done to avoid issues with each device driver that the process may use (process can be totaly ignorant it is using a device if that device is use by a library it links to).

Maybe what needs to happen will explain it better. So if userspace wants to be secure and protect itself from its child taking over the device through COW:

- parent opened a device and is using it

... when parent wants to fork/exec it must:

- parent _must_ flush device command queue and wait for the device to finish all pending jobs

- parent _must_ unmap all range mapped to the device

- parent should first close device file (unless you force set the CLOEXEC flag in the kernel)/it could also just flush but if you are not mapping the device command queue with VM_DONTCOPY then you should really be closing the device

- now parent can fork/exec

- parent must force COW ie write at least one byte to _all_ pages in the range it wants to use with the device

- parent re-open the device and re-initialize everything

So this is putting quite a burden on a number of steps the parent _must_ do in order to keep control of memory exposed to the device. Not doing so can potentialy lead (it depends on who does the COW first) to the child taking control of memory use by the device, memory which was mapped by the parent before the child was created.

Forcing CLOEXEC and VM_DONTCOPY somewhat help to simplify this, but you still need to stop, flush, unmap, before fork/exec and then re-init everything after.

Quoted 9 lines
Quoted 5 lines

This is only when not using SVA/SVM, SVA/SVM is totaly fine from that point of view, no issues whatsoever.

The solution i outlined in previous email do not have that above issue either, no need to rely on user space doing that dance.

Thank you. I get the point. I'm now trying to see if I can solve the problem by seting the vma to VM_SHARED when the portiong is "shared to the hardware".

FYI you can not convert a private anonymous vma to a share one it is illegal AFAIK at least i never heard of it and i am pretty sure the mm code would break if that happens. The user space is the one that decide what flags a vma has, not the kernel. Modulo few flags like DONTCOPY that can be force set by device driver for their vma ie vma of an mmap against the device file.

If you don't like my solution here is another one but it is ugly and i think it is a bad idea. Again this is for the non SVA/SVM case and it assumes that the command queue is a mmap() of the device file:
(A) register mmu_notifier
(B) on _every_ invalidate range callback (_no matter_ what is the range) you zap the command queue mapped to user space (this is because you can't tell if the callback happens for a fork or something else) wait for the hardware queue to finish and clear all the iommu/dma mapping and you unpin all the pages ie put_page()
(C) in device file vma page fault handler (vm_operations_struct. fault) you redo all the GUP and redo all the iommu/dma mapping and you remap the command queue to the userspace

In (C) you can remap different command queue if you are in the child than in the parent (just look at current->mm and compare it to the one the command queue was created against).

Note that this solution will be much __slower__ than what i described in my previous email. You will see that mmu notifier callbacks happens often and for tons of reasons and you will be _constantly_ undoing and redoing tons of work.

This can be mitigated if you can differentiate reasons behind a mmu notifier callback. I posted patchset to do that a while ago and i intend to post it again in the next month or so. But this would still be a bad idea and solution i described previously is much more sane.

Trying to pretend you can have the same thing as SVA/SVM without SVA is not a good idea. The non SVA case can still expose same API (like i described previously) but should go through kernel for _every_ hardware submission (you can batch multiple commands in one submission). Not doing so is way too risky from my POV.

Cheers,
Jérôme

#54 lore raw
Quoted 249 lines · Jerome Glisse

Received: from popscn.huawei.com [10.3.17.45] by Turing-Arch-b with POP3
(fetchmail-6.3.26) for <kenny@localhost> (single-drop); Thu, 20 Sep 2018 22:30:01 +0800 (CST)
Received: from DGGEMM401-HUB.china.huawei.com (10.3.20.209) by
dggeml405-hub.china.huawei.com (10.3.17.49) with Microsoft SMTP Server (TLS) id 14.3.382.0; Thu, 20 Sep 2018 22:23:57 +0800
Received: from dggwg01-in.huawei.com (172.30.65.35) by
DGGEMM401-HUB.china.huawei.com (10.3.20.209) with Microsoft SMTP Server id 14.3.399.0; Thu, 20 Sep 2018 22:23:56 +0800
Received: from mx1.redhat.com (unknown [209.132.183.28]) by Forcepoint
Email with ESMTPS id 18963FA9B6FA9; Thu, 20 Sep 2018 22:23:50 +0800 (CST)
Received: from smtp.corp.redhat.com
(int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using
TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client
certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id
CF1D9C058CBE; Thu, 20 Sep 2018 14:23:47 +0000 (UTC)
Received: from redhat.com (unknown [10.20.6.215]) by
smtp.corp.redhat.com (Postfix) with ESMTPS id 1B030106A780; Thu, 20 Sep
2018 14:23:41 +0000 (UTC)
Date: Thu, 20 Sep 2018 10:23:40 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <[email protected]>
CC: Kenneth Lee <[email protected]>, Alex Williamson
<[email protected]>, Herbert Xu <[email protected]>, [email protected], Jonathan Corbet <[email protected]>, Greg Kroah-Hartman <[email protected]>, Joerg Roedel <[email protected]>, [email protected], Sanjay Kumar <[email protected]>, Hao Fang <[email protected]>, [email protected], [email protected], [email protected], "David S . Miller" <[email protected]>, [email protected], Zhou Wang <[email protected]>, Philippe Ombredanne <[email protected]>, Thomas Gleixner <[email protected]>, Zaibo Xu <[email protected]>, [email protected], Lu Baolu <[email protected]>
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
Message-ID: <[email protected]>
References: <[email protected]>
<[email protected]> <20180917083940.GE207969@Turing-Arch-b> <[email protected]> <20180918060014.GF207969@Turing-Arch-b> <[email protected]> <20180920055543.GG207969@Turing-Arch-b>
Content-Type: text/plain; charset="iso-8859-1"
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
In-Reply-To: <20180920055543.GG207969@Turing-Arch-b>
User-Agent: Mutt/1.10.0 (2018-05-17)
X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22
X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16
(mx1.redhat.com [10.5.110.32]); Thu, 20 Sep 2018 14:23:48 +0000 (UTC)
Return-Path: [email protected] X-MS-Exchange-Organization-AuthSource: DGGEMM401-HUB.china.huawei.com X-MS-Exchange-Organization-AuthAs: Anonymous
MIME-Version: 1.0

Quoted 150 lines · Kenneth Lee
Quoted 149 lines · Jerome Glisse
Quoted 91 lines · Kenneth Lee
Quoted 79 lines · Jerome Glisse
Quoted 28 lines · Kenneth Lee
Quoted 3 lines · Jerome Glisse

So i want to summarize issues i have as this threads have dig deep into details. For this i would like to differentiate two cases first the easy one when relying on SVA/SVM. Then the second one when there is no SVA/SVM.

Thank you very much for the summary.

Quoted 13 lines

In both cases your objectives as i understand them:

[R1]- expose a common user space API that make it easy to share boiler
plate code accross many devices (discovering devices, opening device, creating context, creating command queue ...).
[R2]- try to share the device as much as possible up to device limits
(number of independant queues the device has)
[R3]- minimize syscall by allowing user space to directly schedule on the
device queue without a round trip to the kernel

I don't think i missed any.

Quoted 7 lines

(1) Device with SVA/SVM

For that case it is easy, you do not need to be in VFIO or part of any thing specific in the kernel. There is no security risk (modulo bug in the SVA/SVM silicon). Fork/exec is properly handle and binding a process to a device is just couple dozen lines of code.

Quoted 7 lines

This is right...logically. But the kernel has no clear definition about "Device with SVA/SVM" and no boiler plate for doing so. Then VFIO may become one of the boiler plate.

VFIO is one of the wrappers for IOMMU for user space. And maybe it is the only one. If we add that support within VFIO, which solve most of the problem of SVA/SVM, it will save a lot of work in the future.

You do not need to "wrap" IOMMU for SVA/SVM. Existing upstream SVA/SVM user all do the SVA/SVM setup in couple dozen lines and i failed to see how it would require any more than that in your case.

Quoted 8 lines

I think this is the key confliction between us. So could Alex please say something here? If the VFIO is going to take this into its scope, we can try together to solve all the problem on the way. If it it is not, it is also simple, we can just go to another way to fulfill this part of requirements even we have to duplicate most of the code.

Another point I need to emphasis here: because we have to replace the hardware queue when fork, so it won't be very simple even in SVA/SVM case.

I am assuming hardware queue can only be setup by the kernel and thus you are totaly safe forkwise as the queue is setup against a PASID and the child does not bind to any PASID and you use VM_DONTCOPY on the mmap of the hardware MMIO queue because you should really use that flag for that.

Quoted 21 lines
Quoted 20 lines

(2) Device does not have SVA/SVM (or it is disabled)

You want to still allow device to be part of your framework. However here i see fundamentals securities issues and you move the burden of being careful to user space which i think is a bad idea. We should never trus the userspace from kernel space.

To keep the same API for the user space code you want a 1:1 mapping between device physical address and process virtual address (ie if device access device physical address A it is accessing the same memory as what is backing the virtual address A in the process.

Security issues are on two things:
[I1]- fork/exec, a process who opened any such device and created an
active queue can transfer without its knowledge control of its commands queue through COW. The parent map some anonymous region to the device as a command queue buffer but because of COW the parent can be the first to copy on write and thus the child can inherit the original pages that are mapped to the hardware. Here parent lose control and child gain it.

Quoted 5 lines
Quoted 3 lines

This is indeed an issue. But it remains an issue only if you continue to use the queue and the memory after fork. We can use at_fork kinds of gadget to fix it in user space.

Trusting user space is a no go from my point of view.

Can we dive deeper on this? Maybe we have different understanding on "Trusting user space". As my understanding, "trusting user space" means "no matter what the user process does, it should only hurt itself and anything give to it, no the kernel and the other process".

Quoted 8 lines

In our case, we create a channel between a process and the hardware. The process can do whateven it like to its own memory the channel itself. It won't hurt the other process and the kernel. And if the process fork a child and give the channel to the child, it should the freedom on those resource remain within the parent and the child. We are not trust another else.

So do you refer to something else here?

I am refering to COW giving control to the child on to what happens in the parent from device point of view. A process hurting itself is fine, but if process now has to do special steps to protect from its child ie make sure that its childs can not hurt it, then i see that as a kernel bug. We can not ask user space process to know about all the thousands things that needs to be done to avoid issues with each device driver that the process may use (process can be totaly ignorant it is using a device if that device is use by a library it links to).

Maybe what needs to happen will explain it better. So if userspace wants to be secure and protect itself from its child taking over the device through COW:

- parent opened a device and is using it

... when parent wants to fork/exec it must:

- parent _must_ flush device command queue and wait for the device to finish all pending jobs

- parent _must_ unmap all range mapped to the device

- parent should first close device file (unless you force set the CLOEXEC flag in the kernel)/it could also just flush but if you are not mapping the device command queue with VM_DONTCOPY then you should really be closing the device

- now parent can fork/exec

- parent must force COW ie write at least one byte to _all_ pages in the range it wants to use with the device

- parent re-open the device and re-initialize everything

So this is putting quite a burden on a number of steps the parent _must_ do in order to keep control of memory exposed to the device. Not doing so can potentialy lead (it depends on who does the COW first) to the child taking control of memory use by the device, memory which was mapped by the parent before the child was created.

Forcing CLOEXEC and VM_DONTCOPY somewhat help to simplify this, but you still need to stop, flush, unmap, before fork/exec and then re-init everything after.

Quoted 9 lines
Quoted 5 lines

This is only when not using SVA/SVM, SVA/SVM is totaly fine from that point of view, no issues whatsoever.

The solution i outlined in previous email do not have that above issue either, no need to rely on user space doing that dance.

Thank you. I get the point. I'm now trying to see if I can solve the problem by seting the vma to VM_SHARED when the portiong is "shared to the hardware".

FYI you can not convert a private anonymous vma to a share one it is illegal AFAIK at least i never heard of it and i am pretty sure the mm code would break if that happens. The user space is the one that decide what flags a vma has, not the kernel. Modulo few flags like DONTCOPY that can be force set by device driver for their vma ie vma of an mmap against the device file.

If you don't like my solution here is another one but it is ugly and i think it is a bad idea. Again this is for the non SVA/SVM case and it assumes that the command queue is a mmap() of the device file:
(A) register mmu_notifier
(B) on _every_ invalidate range callback (_no matter_ what is the range) you zap the command queue mapped to user space (this is because you can't tell if the callback happens for a fork or something else) wait for the hardware queue to finish and clear all the iommu/dma mapping and you unpin all the pages ie put_page()
(C) in device file vma page fault handler (vm_operations_struct. fault) you redo all the GUP and redo all the iommu/dma mapping and you remap the command queue to the userspace

In (C) you can remap different command queue if you are in the child than in the parent (just look at current->mm and compare it to the one the command queue was created against).

Note that this solution will be much __slower__ than what i described in my previous email. You will see that mmu notifier callbacks happens often and for tons of reasons and you will be _constantly_ undoing and redoing tons of work.

This can be mitigated if you can differentiate reasons behind a mmu notifier callback. I posted patchset to do that a while ago and i intend to post it again in the next month or so. But this would still be a bad idea and solution i described previously is much more sane.

Quoted 8 lines

Trying to pretend you can have the same thing as SVA/SVM without SVA is not a good idea. The non SVA case can still expose same API (like i described previously) but should go through kernel for _every_ hardware submission (you can batch multiple commands in one submission). Not doing so is way too risky from my POV.

Cheers,
Jérôme

You are quite right. I tried all the way to find a leak in the mm system and fail to. I will tried other way or maybe discard the non-SVA scenario.

Cheers,

Signature · 1 line
			-Kenneth(Hisilicon)
#55 lore raw
In reply to Jerome Glisse
Quoted 86 lines · Jerome Glisse

Received: from POPSCN.huawei.com [10.3.17.45] by Turing-Arch-b with POP3
(fetchmail-6.3.26) for <kenny@localhost> (single-drop); Mon, 17 Sep 2018 09:45:02 +0800 (CST)
Received: from DGGEMM406-HUB.china.huawei.com (10.3.20.214) by
dggeml421-hub.china.huawei.com (10.1.199.38) with Microsoft SMTP Server (TLS) id 14.3.399.0; Mon, 17 Sep 2018 09:43:07 +0800
Received: from dggwg01-in.huawei.com (172.30.65.32) by
DGGEMM406-HUB.china.huawei.com (10.3.20.214) with Microsoft SMTP Server id 14.3.399.0; Mon, 17 Sep 2018 09:43:00 +0800
Received: from mx1.redhat.com (unknown [209.132.183.28]) by Forcepoint
Email with ESMTPS id A15E04AB7D1C3; Mon, 17 Sep 2018 09:42:56 +0800 (CST)
Received: from smtp.corp.redhat.com
(int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using
TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client
certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id
EC621308212D; Mon, 17 Sep 2018 01:42:52 +0000 (UTC)
Received: from redhat.com (ovpn-121-3.rdu2.redhat.com [10.10.121.3]) by
smtp.corp.redhat.com (Postfix) with ESMTPS id 8874530912F4; Mon, 17 Sep
2018 01:42:46 +0000 (UTC)
Date: Sun, 16 Sep 2018 21:42:44 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <[email protected]>
CC: Jonathan Corbet <[email protected]>, Herbert Xu
<[email protected]>, "David S . Miller" <[email protected]>, Joerg Roedel <[email protected]>, Alex Williamson <[email protected]>, Kenneth Lee <[email protected]>, Hao Fang <[email protected]>, Zhou Wang <[email protected]>, Zaibo Xu <[email protected]>, Philippe Ombredanne <[email protected]>, Greg Kroah-Hartman <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Lu Baolu <[email protected]>, Sanjay Kumar <[email protected]>, [email protected]
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
Message-ID: <[email protected]>
References: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
In-Reply-To: <[email protected]>
User-Agent: Mutt/1.10.1 (2018-07-13)
X-Scanned-By: MIMEDefang 2.84 on 10.5.11.26
X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16
(mx1.redhat.com [10.5.110.42]); Mon, 17 Sep 2018 01:42:53 +0000 (UTC)
Return-Path: [email protected] X-MS-Exchange-Organization-AuthSource: DGGEMM406-HUB.china.huawei.com X-MS-Exchange-Organization-AuthAs: Anonymous
MIME-Version: 1.0

So i want to summarize issues i have as this threads have dig deep into details. For this i would like to differentiate two cases first the easy one when relying on SVA/SVM. Then the second one when there is no SVA/SVM. In both cases your objectives as i understand them:

[R1]- expose a common user space API that make it easy to share boiler
plate code accross many devices (discovering devices, opening device, creating context, creating command queue ...).
[R2]- try to share the device as much as possible up to device limits
(number of independant queues the device has)
[R3]- minimize syscall by allowing user space to directly schedule on the
device queue without a round trip to the kernel

I don't think i missed any.

(1) Device with SVA/SVM

For that case it is easy, you do not need to be in VFIO or part of any thing specific in the kernel. There is no security risk (modulo bug in the SVA/SVM silicon). Fork/exec is properly handle and binding a process to a device is just couple dozen lines of code.

(2) Device does not have SVA/SVM (or it is disabled)

You want to still allow device to be part of your framework. However here i see fundamentals securities issues and you move the burden of being careful to user space which i think is a bad idea. We should never trus the userspace from kernel space.

To keep the same API for the user space code you want a 1:1 mapping between device physical address and process virtual address (ie if device access device physical address A it is accessing the same memory as what is backing the virtual address A in the process.

Quoted 9 lines

Security issues are on two things:
[I1]- fork/exec, a process who opened any such device and created an
active queue can transfer without its knowledge control of its commands queue through COW. The parent map some anonymous region to the device as a command queue buffer but because of COW the parent can be the first to copy on write and thus the child can inherit the original pages that are mapped to the hardware. Here parent lose control and child gain it.

Hi, Jerome,

I reconsider your logic. I think the problem can be solved. Let us separate the SVA/SVM feature into two: fault-from-device and device-va-awareness. A device with iommu can support only device-va-awareness or both.

VFIO works on top of iommu, so it will support at least device-va-awareness. For the COW problem, it can be taken as a mmu synchronization issue. If the mmu page table is changed, it should be synchronize to iommu (via iommu_notifier). In the case that the device support fault-from-device, it will work fine. In the case that it supports only device-va-awareness, we can prefault (handle_mm_fault) also via iommu_notifier and reset to iommu page table.

So this can be considered as a bug of VFIO, cannot it?

Quoted 26 lines

[I2]- Because of [R3] you want to allow userspace to schedule commands
on the device without doing an ioctl and thus here user space can schedule any commands to the device with any address. What happens if that address have not been mapped by the user space is undefined and in fact can not be defined as what each IOMMU does on invalid address access is different from IOMMU to IOMMU.

In case of a bad IOMMU, or simply an IOMMU improperly setup by the kernel, this can potentialy allow user space to DMA anywhere.

[I3]- By relying on GUP in VFIO you are not abiding by the implicit
contract (at least i hope it is implicit) that you should not try to map to the device any file backed vma (private or share).

The VFIO code never check the vma controlling the addresses that are provided to VFIO_IOMMU_MAP_DMA ioctl. Which means that the user space can provide file backed range.

I am guessing that the VFIO code never had any issues because its number one user is QEMU and QEMU never does that (and that's good as no one should ever do that).

So if process does that you are opening your self to serious file system corruption (depending on file system this can lead to total data loss for the filesystem).

Quoted 7 lines

Issue is that once you GUP you never abide to file system flushing which write protect the page before writing to the disk. So because the page is still map with write permission to the device (assuming VFIO_IOMMU_MAP_DMA was a write map) then the device can write to the page while it is in the middle of being written back to disk. Consult your nearest file system specialist to ask him how bad that can be.

In the case, we cannot do anything if the device do not support fault-from-device. But we can reject write map with file-backed mapping.

It seems both issues can be solved under VFIO framework:) (But of cause, I don't mean it has to)

Quoted 60 lines

[I4]- Design issue, mdev design As Far As I Understand It is about
sharing a single device to multiple clients (most obvious case here is again QEMU guest). But you are going against that model, in fact AFAIUI you are doing the exect opposite. When there is no SVA/SVM you want only one mdev device that can not be share.

So this is counter intuitive to the mdev existing design. It is not about sharing device among multiple users but about giving exclusive access to the device to one user.

All the reasons above is why i believe a different model would serve you and your user better. Below is a design that avoids all of the above issues and still delivers all of your objectives with the exceptions of the third one [R3] when there is no SVA/SVM.

Create a subsystem (very much boiler plate code) which allow device to register themself against (very much like what you do in your current patchset but outside of VFIO).

That subsystem will create a device file for each registered system and expose a common API (ie set of ioctl) for each of those device files.

When user space create a queue (through an ioctl after opening the device file) the kernel can return -EBUSY if all the device queue are in use, or create a device queue and return a flag like SYNC_ONLY for device that do not have SVA/SVM.

For device with SVA/SVM at the time the process create a queue you bind the process PASID to the device queue. From there on the userspace can schedule commands and use the device without going to kernel space.

For device without SVA/SVM you create a fake queue that is just pure memory is not related to the device. From there on the userspace must call an ioctl every time it wants the device to consume its queue (hence why the SYNC_ONLY flag for synchronous operation only). The kernel portion read the fake queue expose to user space and copy commands into the real hardware queue but first it properly map any of the process memory needed for those commands to the device and adjust the device physical address with the one it gets from dma_map API.

With that model it is "easy" to listen to mmu_notifier and to abide by them to avoid issues [I1], [I3] and [I4]. You obviously avoid the [I2] issue by only mapping a fake device queue to userspace.

So yes with that models it means that every device that wish to support the non SVA/SVM case will have to do extra work (ie emulate its command queue in software in the kernel). But by doing so, you support an unlimited number of process on your device (ie all the process can share one single hardware command queues or multiple hardware queues).

The big advantages i see here is that the process do not have to worry about doing something wrong. You are protecting yourself and your user from stupid mistakes.

Quoted 4 lines

I hope this is useful to you.

Cheers,
Jérôme

Cheers

Signature · 1 line
			-Kenneth(Hisilicon)
#56 lore raw
Quoted 48 lines · Kenneth Lee
Quoted 37 lines · Jerome Glisse

So i want to summarize issues i have as this threads have dig deep into details. For this i would like to differentiate two cases first the easy one when relying on SVA/SVM. Then the second one when there is no SVA/SVM. In both cases your objectives as i understand them:

[R1]- expose a common user space API that make it easy to share boiler
plate code accross many devices (discovering devices, opening device, creating context, creating command queue ...).
[R2]- try to share the device as much as possible up to device limits
(number of independant queues the device has)
[R3]- minimize syscall by allowing user space to directly schedule on the
device queue without a round trip to the kernel

I don't think i missed any.

(1) Device with SVA/SVM

For that case it is easy, you do not need to be in VFIO or part of any thing specific in the kernel. There is no security risk (modulo bug in the SVA/SVM silicon). Fork/exec is properly handle and binding a process to a device is just couple dozen lines of code.

(2) Device does not have SVA/SVM (or it is disabled)

You want to still allow device to be part of your framework. However here i see fundamentals securities issues and you move the burden of being careful to user space which i think is a bad idea. We should never trus the userspace from kernel space.

To keep the same API for the user space code you want a 1:1 mapping between device physical address and process virtual address (ie if device access device physical address A it is accessing the same memory as what is backing the virtual address A in the process.

Quoted 9 lines

Security issues are on two things:
[I1]- fork/exec, a process who opened any such device and created an
active queue can transfer without its knowledge control of its commands queue through COW. The parent map some anonymous region to the device as a command queue buffer but because of COW the parent can be the first to copy on write and thus the child can inherit the original pages that are mapped to the hardware. Here parent lose control and child gain it.

Quoted 5 lines

Hi, Jerome,

I reconsider your logic. I think the problem can be solved. Let us separate the SVA/SVM feature into two: fault-from-device and device-va-awareness. A device with iommu can support only device-va-awareness or both.

Not sure i follow, are you also talking about the non SVA/SVM case here ? Either device has SVA/SVM, either it does not. The fact that device can use same physical address PA for its access as the process virtual address VA does not change any of the issues listed here, same issues would apply if device was using PA != VA ...

Quoted 8 lines

VFIO works on top of iommu, so it will support at least device-va-awareness. For the COW problem, it can be taken as a mmu synchronization issue. If the mmu page table is changed, it should be synchronize to iommu (via iommu_notifier). In the case that the device support fault-from-device, it will work fine. In the case that it supports only device-va-awareness, we can prefault (handle_mm_fault) also via iommu_notifier and reset to iommu page table.

So this can be considered as a bug of VFIO, cannot it?

So again SVA/SVM is fine because it uses the same CPU page table so anything done to the process address space reflect automaticly to the device. Nothing to do for SVA/SVM.

For non SVA/SVM you _must_ unmap device command queues, flush and wait for all pending commands, unmap all the IOMMU mapping and wait for the process to fault on the unmapped device command queue before trying to restore any thing. This would be terribly slow i described it in another email.

Also you can not fault inside a mmu_notifier it is illegal, all you can do is unmap thing and wait for access to finish.

Quoted 34 lines
Quoted 26 lines

[I2]- Because of [R3] you want to allow userspace to schedule commands
on the device without doing an ioctl and thus here user space can schedule any commands to the device with any address. What happens if that address have not been mapped by the user space is undefined and in fact can not be defined as what each IOMMU does on invalid address access is different from IOMMU to IOMMU.

In case of a bad IOMMU, or simply an IOMMU improperly setup by the kernel, this can potentialy allow user space to DMA anywhere.

[I3]- By relying on GUP in VFIO you are not abiding by the implicit
contract (at least i hope it is implicit) that you should not try to map to the device any file backed vma (private or share).

The VFIO code never check the vma controlling the addresses that are provided to VFIO_IOMMU_MAP_DMA ioctl. Which means that the user space can provide file backed range.

I am guessing that the VFIO code never had any issues because its number one user is QEMU and QEMU never does that (and that's good as no one should ever do that).

So if process does that you are opening your self to serious file system corruption (depending on file system this can lead to total data loss for the filesystem).

Quoted 7 lines

Issue is that once you GUP you never abide to file system flushing which write protect the page before writing to the disk. So because the page is still map with write permission to the device (assuming VFIO_IOMMU_MAP_DMA was a write map) then the device can write to the page while it is in the middle of being written back to disk. Consult your nearest file system specialist to ask him how bad that can be.

Quoted 2 lines

In the case, we cannot do anything if the device do not support fault-from-device. But we can reject write map with file-backed mapping.

Yes this would avoid most issues with file-backed mapping, truncate would still cause weird behavior but only for your device. But then your non SVA/SVM case does not behave as the SVA/SVM case so why not do what i out- lined below that allow same behavior modulo command flushing ...

Quoted 2 lines

It seems both issues can be solved under VFIO framework:) (But of cause, I don't mean it has to)

The COW can not be solve without either the solution i described in this previous mail below or the other one with mmu notifier. But the one below is saner and has better performance.

Quoted 69 lines
Quoted 60 lines

[I4]- Design issue, mdev design As Far As I Understand It is about
sharing a single device to multiple clients (most obvious case here is again QEMU guest). But you are going against that model, in fact AFAIUI you are doing the exect opposite. When there is no SVA/SVM you want only one mdev device that can not be share.

So this is counter intuitive to the mdev existing design. It is not about sharing device among multiple users but about giving exclusive access to the device to one user.

All the reasons above is why i believe a different model would serve you and your user better. Below is a design that avoids all of the above issues and still delivers all of your objectives with the exceptions of the third one [R3] when there is no SVA/SVM.

Create a subsystem (very much boiler plate code) which allow device to register themself against (very much like what you do in your current patchset but outside of VFIO).

That subsystem will create a device file for each registered system and expose a common API (ie set of ioctl) for each of those device files.

When user space create a queue (through an ioctl after opening the device file) the kernel can return -EBUSY if all the device queue are in use, or create a device queue and return a flag like SYNC_ONLY for device that do not have SVA/SVM.

For device with SVA/SVM at the time the process create a queue you bind the process PASID to the device queue. From there on the userspace can schedule commands and use the device without going to kernel space.

For device without SVA/SVM you create a fake queue that is just pure memory is not related to the device. From there on the userspace must call an ioctl every time it wants the device to consume its queue (hence why the SYNC_ONLY flag for synchronous operation only). The kernel portion read the fake queue expose to user space and copy commands into the real hardware queue but first it properly map any of the process memory needed for those commands to the device and adjust the device physical address with the one it gets from dma_map API.

With that model it is "easy" to listen to mmu_notifier and to abide by them to avoid issues [I1], [I3] and [I4]. You obviously avoid the [I2] issue by only mapping a fake device queue to userspace.

So yes with that models it means that every device that wish to support the non SVA/SVM case will have to do extra work (ie emulate its command queue in software in the kernel). But by doing so, you support an unlimited number of process on your device (ie all the process can share one single hardware command queues or multiple hardware queues).

The big advantages i see here is that the process do not have to worry about doing something wrong. You are protecting yourself and your user from stupid mistakes.

Quoted 4 lines

I hope this is useful to you.

Cheers,
Jérôme

Cheers

Signature · 1 line
			-Kenneth(Hisilicon)
#57 lore raw
Quoted 176 lines · Jerome Glisse

Received: from popscn.huawei.com [10.3.17.45] by Turing-Arch-b with POP3
(fetchmail-6.3.26) for <kenny@localhost> (single-drop); Fri, 21 Sep 2018 23:00:01 +0800 (CST)
Received: from DGGEMM406-HUB.china.huawei.com (10.3.20.214) by
DGGEML403-HUB.china.huawei.com (10.3.17.33) with Microsoft SMTP Server (TLS) id 14.3.399.0; Fri, 21 Sep 2018 22:52:20 +0800
Received: from dggwg01-in.huawei.com (172.30.65.38) by
DGGEMM406-HUB.china.huawei.com (10.3.20.214) with Microsoft SMTP Server id 14.3.399.0; Fri, 21 Sep 2018 22:52:16 +0800
Received: from mx1.redhat.com (unknown [209.132.183.28]) by Forcepoint
Email with ESMTPS id 912ECA2EC6662; Fri, 21 Sep 2018 22:52:12 +0800 (CST)
Received: from smtp.corp.redhat.com
(int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using
TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client
certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id
25BC0792BB; Fri, 21 Sep 2018 14:52:10 +0000 (UTC)
Received: from redhat.com (ovpn-124-21.rdu2.redhat.com [10.10.124.21])

	by smtp.corp.redhat.com (Postfix) with ESMTPS id 67B25308BDA0;
	Fri, 21 Sep 2018 14:52:03 +0000 (UTC)

Date: Fri, 21 Sep 2018 10:52:01 -0400
From: Jerome Glisse <[email protected]>
To: Kenneth Lee <liguozhu-C8/M+/[email protected]>
CC: Kenneth Lee <[email protected]>, Herbert Xu
<[email protected]>, [email protected], Jonathan Corbet <[email protected]>, Greg Kroah-Hartman <[email protected]>, Joerg Roedel <[email protected]>, [email protected], Sanjay Kumar <[email protected]>, Hao Fang <[email protected]>, iommu-cunTk1MwBs9QetFLy7KEm3xJsTq8ys+cHZ5vskTnxNA@public.gmane.org, [email protected], [email protected], Alex Williamson <[email protected]>, Thomas Gleixner <[email protected]>, [email protected], Zhou Wang <wangzhou1-C8/M+/[email protected]>, Philippe Ombredanne <pombredanne-od1rfyK75/[email protected]>, Zaibo Xu <[email protected]>, "David S . Miller" <davem-fT/PcQaiUtIeIZ0/[email protected]>, linux-accelerators-uLR06cmDAlY/[email protected], Lu Baolu <baolu.lu-VuQAYsv1563Yd54FQh9/[email protected]>
Subject: Re: [RFCv2 PATCH 0/7] A General Accelerator Framework, WarpDrive
Message-ID: <[email protected]>
References: <20180903005204.26041-1-nek.in.cn-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
<[email protected]>
<20180921100314.GH207969@Turing-Arch-b>
Content-Type: text/plain; charset="iso-8859-1"
Content-Disposition: inline
Content-Transfer-Encoding: 8bit
In-Reply-To: <20180921100314.GH207969@Turing-Arch-b>
User-Agent: Mutt/1.10.1 (2018-07-13)
X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24
X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16
(mx1.redhat.com [10.5.110.39]); Fri, 21 Sep 2018 14:52:10 +0000 (UTC)
Return-Path: [email protected]
X-MS-Exchange-Organization-AuthSource: DGGEMM406-HUB.china.huawei.com
X-MS-Exchange-Organization-AuthAs: Anonymous
MIME-Version: 1.0

Quoted 48 lines · Kenneth Lee
Quoted 37 lines · Jerome Glisse

So i want to summarize issues i have as this threads have dig deep into details. For this i would like to differentiate two cases first the easy one when relying on SVA/SVM. Then the second one when there is no SVA/SVM. In both cases your objectives as i understand them:

[R1]- expose a common user space API that make it easy to share boiler
plate code accross many devices (discovering devices, opening device, creating context, creating command queue ...).
[R2]- try to share the device as much as possible up to device limits
(number of independant queues the device has)
[R3]- minimize syscall by allowing user space to directly schedule on the
device queue without a round trip to the kernel

I don't think i missed any.

(1) Device with SVA/SVM

For that case it is easy, you do not need to be in VFIO or part of any thing specific in the kernel. There is no security risk (modulo bug in the SVA/SVM silicon). Fork/exec is properly handle and binding a process to a device is just couple dozen lines of code.

(2) Device does not have SVA/SVM (or it is disabled)

You want to still allow device to be part of your framework. However here i see fundamentals securities issues and you move the burden of being careful to user space which i think is a bad idea. We should never trus the userspace from kernel space.

To keep the same API for the user space code you want a 1:1 mapping between device physical address and process virtual address (ie if device access device physical address A it is accessing the same memory as what is backing the virtual address A in the process.

Quoted 9 lines

Security issues are on two things:
[I1]- fork/exec, a process who opened any such device and created an
active queue can transfer without its knowledge control of its commands queue through COW. The parent map some anonymous region to the device as a command queue buffer but because of COW the parent can be the first to copy on write and thus the child can inherit the original pages that are mapped to the hardware. Here parent lose control and child gain it.

Quoted 5 lines

Hi, Jerome,

I reconsider your logic. I think the problem can be solved. Let us separate the SVA/SVM feature into two: fault-from-device and device-va-awareness. A device with iommu can support only device-va-awareness or both.

Not sure i follow, are you also talking about the non SVA/SVM case here ? Either device has SVA/SVM, either it does not. The fact that device can use same physical address PA for its access as the process virtual address VA does not change any of the issues listed here, same issues would apply if device was using PA != VA ...

Quoted 8 lines

VFIO works on top of iommu, so it will support at least device-va-awareness. For the COW problem, it can be taken as a mmu synchronization issue. If the mmu page table is changed, it should be synchronize to iommu (via iommu_notifier). In the case that the device support fault-from-device, it will work fine. In the case that it supports only device-va-awareness, we can prefault (handle_mm_fault) also via iommu_notifier and reset to iommu page table.

So this can be considered as a bug of VFIO, cannot it?

So again SVA/SVM is fine because it uses the same CPU page table so anything done to the process address space reflect automaticly to the device. Nothing to do for SVA/SVM.

For non SVA/SVM you _must_ unmap device command queues, flush and wait for all pending commands, unmap all the IOMMU mapping and wait for the process to fault on the unmapped device command queue before trying to restore any thing. This would be terribly slow i described it in another email.

Also you can not fault inside a mmu_notifier it is illegal, all you can do is unmap thing and wait for access to finish.

Quoted 34 lines
Quoted 26 lines

[I2]- Because of [R3] you want to allow userspace to schedule commands
on the device without doing an ioctl and thus here user space can schedule any commands to the device with any address. What happens if that address have not been mapped by the user space is undefined and in fact can not be defined as what each IOMMU does on invalid address access is different from IOMMU to IOMMU.

In case of a bad IOMMU, or simply an IOMMU improperly setup by the kernel, this can potentialy allow user space to DMA anywhere.

[I3]- By relying on GUP in VFIO you are not abiding by the implicit
contract (at least i hope it is implicit) that you should not try to map to the device any file backed vma (private or share).

The VFIO code never check the vma controlling the addresses that are provided to VFIO_IOMMU_MAP_DMA ioctl. Which means that the user space can provide file backed range.

I am guessing that the VFIO code never had any issues because its number one user is QEMU and QEMU never does that (and that's good as no one should ever do that).

So if process does that you are opening your self to serious file system corruption (depending on file system this can lead to total data loss for the filesystem).

Quoted 7 lines

Issue is that once you GUP you never abide to file system flushing which write protect the page before writing to the disk. So because the page is still map with write permission to the device (assuming VFIO_IOMMU_MAP_DMA was a write map) then the device can write to the page while it is in the middle of being written back to disk. Consult your nearest file system specialist to ask him how bad that can be.

Quoted 2 lines

In the case, we cannot do anything if the device do not support fault-from-device. But we can reject write map with file-backed mapping.

Yes this would avoid most issues with file-backed mapping, truncate would still cause weird behavior but only for your device. But then your non SVA/SVM case does not behave as the SVA/SVM case so why not do what i out- lined below that allow same behavior modulo command flushing ...

Quoted 77 lines
Quoted 2 lines

It seems both issues can be solved under VFIO framework:) (But of cause, I don't mean it has to)

The COW can not be solve without either the solution i described in this previous mail below or the other one with mmu notifier. But the one below is saner and has better performance.

Quoted 69 lines
Quoted 60 lines

[I4]- Design issue, mdev design As Far As I Understand It is about
sharing a single device to multiple clients (most obvious case here is again QEMU guest). But you are going against that model, in fact AFAIUI you are doing the exect opposite. When there is no SVA/SVM you want only one mdev device that can not be share.

So this is counter intuitive to the mdev existing design. It is not about sharing device among multiple users but about giving exclusive access to the device to one user.

All the reasons above is why i believe a different model would serve you and your user better. Below is a design that avoids all of the above issues and still delivers all of your objectives with the exceptions of the third one [R3] when there is no SVA/SVM.

Create a subsystem (very much boiler plate code) which allow device to register themself against (very much like what you do in your current patchset but outside of VFIO).

That subsystem will create a device file for each registered system and expose a common API (ie set of ioctl) for each of those device files.

When user space create a queue (through an ioctl after opening the device file) the kernel can return -EBUSY if all the device queue are in use, or create a device queue and return a flag like SYNC_ONLY for device that do not have SVA/SVM.

For device with SVA/SVM at the time the process create a queue you bind the process PASID to the device queue. From there on the userspace can schedule commands and use the device without going to kernel space.

For device without SVA/SVM you create a fake queue that is just pure memory is not related to the device. From there on the userspace must call an ioctl every time it wants the device to consume its queue (hence why the SYNC_ONLY flag for synchronous operation only). The kernel portion read the fake queue expose to user space and copy commands into the real hardware queue but first it properly map any of the process memory needed for those commands to the device and adjust the device physical address with the one it gets from dma_map API.

With that model it is "easy" to listen to mmu_notifier and to abide by them to avoid issues [I1], [I3] and [I4]. You obviously avoid the [I2] issue by only mapping a fake device queue to userspace.

So yes with that models it means that every device that wish to support the non SVA/SVM case will have to do extra work (ie emulate its command queue in software in the kernel). But by doing so, you support an unlimited number of process on your device (ie all the process can share one single hardware command queues or multiple hardware queues).

The big advantages i see here is that the process do not have to worry about doing something wrong. You are protecting yourself and your user from stupid mistakes.

Quoted 4 lines

I hope this is useful to you.

Cheers,
Jérôme

Cheers

Signature · 1 line
			-Kenneth(Hisilicon)

Thank you very much. Jerome. I will push RFCv3 without VFIO soon.

The basic idea will be:

1. Make a chrdev interface for any WarpDrive device instance. Allocate queue when it is openned.

2. For device with SVA/SVM, the device (in the queue context) share application's process page table via iommu.

3. For device without SVA/SVM, the application can mmap any WarpDrive device as its "shared memory". The memory will be shared with any queue openned by this process, before or after the queues are openned. The VAs will still be shared with the hardware (by iommu or by kernel translation). This will fulfill the requirement of "Sharing VA", but it will need the application to use those mmapped memory only.

Cheers

Signature · 1 line
			-Kenneth(Hisilicon)