hyper_dmabuf: Hyper_DMABUF driver
Subsystems (2)
v2 cover hyper_dmabuf: Hyper_DMABUF driver
This patch series contains the implementation of a new device driver, hyper_DMABUF driver, which provides a way to expand the boundary of Linux DMA-BUF sharing to across different VM instances in Multi-OS platform enabled by a Hypervisor (e.g. XEN)
This version 2 series is basically refactored version of old series starting with "[RFC PATCH 01/60] hyper_dmabuf: initial working version of hyper_dmabuf drv"
Implementation details of this driver are described in the reference guide added by the second patch, "[RFC PATCH v2 2/5] hyper_dmabuf: architecture specification and reference guide".
Attaching 'Overview' section here as a quick summary.
------------------------------------------------------------------------------
Section 1. Overview
------------------------------------------------------------------------------
Hyper_DMABUF driver is a Linux device driver running on multiple Virtual achines (VMs), which expands DMA-BUF sharing capability to the VM environment where multiple different OS instances need to share same physical data without data-copy across VMs.
To share a DMA_BUF across VMs, an instance of the Hyper_DMABUF drv on the exporting VM (so called, “exporter”) imports a local DMA_BUF from the original producer of the buffer, then re-exports it with an unique ID, hyper_dmabuf_id for the buffer to the importing VM (so called, “importer”).
Another instance of the Hyper_DMABUF driver on importer registers a hyper_dmabuf_id together with reference information for the shared physical pages associated with the DMA_BUF to its database when the export happens.
The actual mapping of the DMA_BUF on the importer’s side is done by the Hyper_DMABUF driver when user space issues the IOCTL command to access the shared DMA_BUF. The Hyper_DMABUF driver works as both an importing and exporting driver as is, that is, no special configuration is required. Consequently, only a single module per VM is needed to enable cross-VM DMA_BUF exchange.
------------------------------------------------------------------------------
There is a git repository at github.com where this series of patches are all integrated in Linux kernel tree based on the commit:
commit ae64f9bd1d3621b5e60d7363bc20afb46aede215
Author: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Date: Sun Dec 3 11:01:47 2018 -0500
Linux 4.15-rc2https://github.com/downor/linux_hyper_dmabuf.git hyper_dmabuf_integration_v4
Dongwon Kim, Mateusz Polrola (9):
hyper_dmabuf: initial upload of hyper_dmabuf drv core framework
hyper_dmabuf: architecture specification and reference guide
MAINTAINERS: adding Hyper_DMABUF driver section in MAINTAINERS
hyper_dmabuf: user private data attached to hyper_DMABUF
hyper_dmabuf: hyper_DMABUF synchronization across VM
hyper_dmabuf: query ioctl for retreiving various hyper_DMABUF info
hyper_dmabuf: event-polling mechanism for detecting a new hyper_DMABUF
hyper_dmabuf: threaded interrupt in Xen-backend
hyper_dmabuf: default backend for XEN hypervisor
Documentation/hyper-dmabuf-sharing.txt | 734 ++++++++++++++++ MAINTAINERS | 11 + drivers/dma-buf/Kconfig | 2 + drivers/dma-buf/Makefile | 1 + drivers/dma-buf/hyper_dmabuf/Kconfig | 50 ++ drivers/dma-buf/hyper_dmabuf/Makefile | 44 + .../backends/xen/hyper_dmabuf_xen_comm.c | 944 +++++++++++++++++++++ .../backends/xen/hyper_dmabuf_xen_comm.h | 78 ++ .../backends/xen/hyper_dmabuf_xen_comm_list.c | 158 ++++ .../backends/xen/hyper_dmabuf_xen_comm_list.h | 67 ++ .../backends/xen/hyper_dmabuf_xen_drv.c | 46 + .../backends/xen/hyper_dmabuf_xen_drv.h | 53 ++ .../backends/xen/hyper_dmabuf_xen_shm.c | 525 ++++++++++++ .../backends/xen/hyper_dmabuf_xen_shm.h | 46 + drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c | 410 +++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h | 122 +++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_event.c | 122 +++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_event.h | 38 + drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c | 135 +++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h | 53 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c | 794 +++++++++++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h | 52 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c | 295 +++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h | 73 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c | 416 +++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h | 89 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c | 415 +++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h | 34 + drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_query.c | 174 ++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_query.h | 36 + .../hyper_dmabuf/hyper_dmabuf_remote_sync.c | 324 +++++++ .../hyper_dmabuf/hyper_dmabuf_remote_sync.h | 32 + .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c | 257 ++++++ .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h | 43 + drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h | 143 ++++ include/uapi/linux/hyper_dmabuf.h | 134 +++ 36 files changed, 6950 insertions(+) create mode 100644 Documentation/hyper-dmabuf-sharing.txt create mode 100644 drivers/dma-buf/hyper_dmabuf/Kconfig create mode 100644 drivers/dma-buf/hyper_dmabuf/Makefile create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.h create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm_list.c create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm_list.h create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_drv.c create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_drv.h create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.c create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_event.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_event.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_query.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_query.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_remote_sync.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_remote_sync.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h create mode 100644 include/uapi/linux/hyper_dmabuf.h
Signature · 6 lines
2.16.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
RFC PATCH v2 1/9 hyper_dmabuf: initial upload of hyper_dmabuf drv core framework
Upload of intial version of core framework in hyper_DMABUF driver enabling DMA_BUF exchange between two different VMs in virtualized platform based on Hypervisor such as XEN.
Hyper_DMABUF drv's primary role is to import a DMA_BUF from originator then re-export it to another Linux VM so that it can be mapped and accessed in there.
This driver has two layers, one is so called, "core framework", which contains driver interface and core functions handling export/import of new hyper_DMABUF and its maintenance. This part of the driver is independent from Hypervisor so can work as is with any Hypervisor.
The other layer is called "Hypervisor Backend". This layer represents the interface between "core framework" and actual Hypervisor, handling memory sharing and communication. Not like "core framework", every Hypervisor needs it's own backend interface designed using its native mechanism for memory sharing and inter-VM communication.
This patch contains the first part, "core framework", which consists of 7 source files and 11 header files. Some brief description of these source code are attached below:
hyper_dmabuf_drv.c
- Linux driver interface and initialization/cleaning-up routines
hyper_dmabuf_ioctl.c
- IOCTLs calls for export/import of DMA-BUF comm channel's creation and destruction.
hyper_dmabuf_sgl_proc.c
- Provides methods to managing DMA-BUF for exporing and importing. For exporting, extraction of pages, sharing pages via procedures in "Backend" and notifying importing VM exist. For importing, all operations related to the reconstruction of DMA-BUF (with shared pages) on importer's side are defined.
hyper_dmabuf_ops.c
- Standard DMA-BUF operations for hyper_DMABUF reconstructed on importer's side.
hyper_dmabuf_list.c
- Lists for storing exported and imported hyper_DMABUF to keep track of remote usage of hyper_DMABUF currently being shared.
hyper_dmabuf_msg.c
- Defines messages exchanged between VMs (exporter and importer) and function calls for sending and parsing (when received) those.
hyper_dmabuf_id.c
- Contains methods to generate and manage "hyper_DMABUF id" for each hyper_DMABUF being exported. It is a global handle for a hyper_DMABUF, which another VM needs to know to import it.
hyper_dmabuf_struct.h
- Contains data structures of importer or exporter hyper_DMABUF
include/uapi/linux/hyper_dmabuf.h
- Contains definition of data types and structures referenced by user application to interact with driver
--- drivers/dma-buf/Kconfig | 2 + drivers/dma-buf/Makefile | 1 + drivers/dma-buf/hyper_dmabuf/Kconfig | 23 + drivers/dma-buf/hyper_dmabuf/Makefile | 34 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c | 254 ++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h | 111 ++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c | 135 +++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h | 53 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c | 672 +++++++++++++++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h | 52 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c | 294 +++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h | 73 +++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c | 320 ++++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h | 87 +++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c | 264 ++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h | 34 ++ .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c | 256 ++++++++ .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h | 43 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h | 131 ++++ include/uapi/linux/hyper_dmabuf.h | 87 +++ 20 files changed, 2926 insertions(+) create mode 100644 drivers/dma-buf/hyper_dmabuf/Kconfig create mode 100644 drivers/dma-buf/hyper_dmabuf/Makefile create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h create mode 100644 include/uapi/linux/hyper_dmabuf.h
drivers/dma-buf/Kconfig +2 -0
@@ -30,4 +30,6 @@ config SW_SYNC WARNING: improper use of this can result in deadlocking kernel drivers from userspace. Intended for test and debug only. +source "drivers/dma-buf/hyper_dmabuf/Kconfig" + endmenu
drivers/dma-buf/Makefile +1 -0
@@ -1,3 +1,4 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o reservation.o seqno-fence.o obj-$(CONFIG_SYNC_FILE) += sync_file.o obj-$(CONFIG_SW_SYNC) += sw_sync.o sync_debug.o +obj-$(CONFIG_HYPER_DMABUF) += ./hyper_dmabuf/
drivers/dma-buf/hyper_dmabuf/Kconfig new file +23 -0
@@ -0,0 +1,23 @@ +menu "HYPER_DMABUF" + +config HYPER_DMABUF + tristate "Enables hyper dmabuf driver" + default y + help + This option enables Hyper_DMABUF driver. + + This driver works as abstraction layer that export and import + DMA_BUF from/to another virtual OS running on the same HW platform + powered by a hypervisor + +config HYPER_DMABUF_SYSFS + bool "Enable sysfs information about hyper DMA buffers" + default y + depends on HYPER_DMABUF + help + Expose run-time information about currently imported and exported buffers + registered in EXPORT and IMPORT list in Hyper_DMABUF driver. + + The location of sysfs is under "...." + +endmenu
drivers/dma-buf/hyper_dmabuf/Makefile new file +34 -0
@@ -0,0 +1,34 @@ +TARGET_MODULE:=hyper_dmabuf + +# If we running by kernel building system +ifneq ($(KERNELRELEASE),) + $(TARGET_MODULE)-objs := hyper_dmabuf_drv.o \ + hyper_dmabuf_ioctl.o \ + hyper_dmabuf_list.o \ + hyper_dmabuf_sgl_proc.o \ + hyper_dmabuf_ops.o \ + hyper_dmabuf_msg.o \ + hyper_dmabuf_id.o \ + +obj-$(CONFIG_HYPER_DMABUF) := $(TARGET_MODULE).o + +# If we are running without kernel build system +else +BUILDSYSTEM_DIR?=../../../ +PWD:=$(shell pwd) + +all : +# run kernel build system to make module +$(MAKE) -C $(BUILDSYSTEM_DIR) M=$(PWD) modules + +clean: +# run kernel build system to cleanup in current directory +$(MAKE) -C $(BUILDSYSTEM_DIR) M=$(PWD) clean + +load: + insmod ./$(TARGET_MODULE).ko + +unload: + rmmod ./$(TARGET_MODULE).ko + +endif
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +254 -0
254 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h new file +111 -0
111 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +135 -0
135 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +53 -0
53 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +672 -0
672 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h new file +52 -0
52 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c new file +294 -0
294 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h new file +73 -0
73 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +320 -0
320 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +87 -0
87 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c new file +264 -0
264 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h new file +34 -0
@@ -0,0 +1,34 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_OPS_H__ +#define __HYPER_DMABUF_OPS_H__ + +int hyper_dmabuf_export_fd (struct imported_sgt_info *imported,int flags); + +void hyper_dmabuf_export_dma_buf (struct imported_sgt_info *imported); + +#endif /* __HYPER_DMABUF_IMP_H__ */
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +256 -0
256 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h new file +43 -0
43 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h new file +131 -0
131 lines not on this page · raw message
include/uapi/linux/hyper_dmabuf.h new file +87 -0
87 lines not on this page · raw message
Signature · 6 lines
2.16.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [RFC, v2, 1/9] hyper_dmabuf: initial upload of hyper_dmabuf drv core framework
Quoted 139 lines · Dongwon Kim
Upload of intial version of core framework in
hyper_DMABUFdriver enablingDMA_BUFexchange between two different VMs in virtualized platform based on Hypervisor such as XEN.
Hyper_DMABUFdrv's primary role is to import aDMA_BUFfrom originator then re-export it to another Linux VM so that it can be mapped and accessed in there.This driver has two layers, one is so called, "core framework", which contains driver interface and core functions handling export/import of new
hyper_DMABUFand its maintenance. This part of the driver is independent from Hypervisor so can work as is with any Hypervisor.The other layer is called "Hypervisor Backend". This layer represents the interface between "core framework" and actual Hypervisor, handling memory sharing and communication. Not like "core framework", every Hypervisor needs it's own backend interface designed using its native mechanism for memory sharing and inter-VM communication.
This patch contains the first part, "core framework", which consists of 7 source files and 11 header files. Some brief description of these source code are attached below:
hyper_dmabuf_drv.c- Linux driver interface and initialization/cleaning-up routines
hyper_dmabuf_ioctl.c- IOCTLs calls for export/import of DMA-BUF comm channel's creation and destruction.
hyper_dmabuf_sgl_proc.c- Provides methods to managing DMA-BUF for exporing and importing. For exporting, extraction of pages, sharing pages via procedures in "Backend" and notifying importing VM exist. For importing, all operations related to the reconstruction of DMA-BUF (with shared pages) on importer's side are defined.
hyper_dmabuf_ops.c- Standard DMA-BUF operations for
hyper_DMABUFreconstructed on importer's side.
hyper_dmabuf_list.c- Lists for storing exported and imported
hyper_DMABUFto keep track of remote usage ofhyper_DMABUFcurrently being shared.
hyper_dmabuf_msg.c- Defines messages exchanged between VMs (exporter and importer) and function calls for sending and parsing (when received) those.
hyper_dmabuf_id.c- Contains methods to generate and manage "
hyper_DMABUFid" for eachhyper_DMABUFbeing exported. It is a global handle for ahyper_DMABUF, which another VM needs to know to import it.
hyper_dmabuf_struct.h- Contains data structures of importer or exporter
hyper_DMABUF
include/uapi/linux/hyper_dmabuf.h- Contains definition of data types and structures referenced by user application to interact with driver
Signed-off-by Dongwon Kim <[email protected]>Signed-off-by Mateusz Polrola <[email protected]>--- drivers/dma-buf/Kconfig | 2 + drivers/dma-buf/Makefile | 1 + drivers/dma-buf/hyper_dmabuf/Kconfig | 23 + drivers/dma-buf/hyper_dmabuf/Makefile | 34 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c | 254 ++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h | 111 ++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c | 135 +++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h | 53 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c | 672 +++++++++++++++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h | 52 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c | 294 +++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h | 73 +++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c | 320 ++++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h | 87 +++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c | 264 ++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h | 34 ++ .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c | 256 ++++++++ .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h | 43 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h | 131 ++++ include/uapi/linux/hyper_dmabuf.h | 87 +++ 20 files changed, 2926 insertions(+)create mode 100644
drivers/dma-buf/hyper_dmabuf/Kconfigcreate mode 100644drivers/dma-buf/hyper_dmabuf/Makefilecreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.hcreate mode 100644include/uapi/linux/hyper_dmabuf.hdrivers/dma-buf/Kconfig +2
-0@@ -30,4 +30,6 @@ config SW_SYNC WARNING: improper use of this can result in deadlocking kernel drivers from userspace. Intended for test and debug only. +source "drivers/dma-buf/hyper_dmabuf/Kconfig" + endmenudrivers/dma-buf/Makefile +1
-0@@ -1,3 +1,4 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o reservation.o seqno-fence.o obj-$(CONFIG_SYNC_FILE) += sync_file.o obj-$(CONFIG_SW_SYNC) += sw_sync.o sync_debug.o +obj-$(CONFIG_HYPER_DMABUF) += ./hyper_dmabuf/drivers/dma-buf/hyper_dmabuf/Kconfig new file +0
-0
drivers/dma-buf/hyper_dmabuf/Kconfig line 5
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/Kconfig new file +5
-0@@ -0,0 +1,23 @@ +menu "HYPER_DMABUF" + +config HYPER_DMABUF + tristate "Enables hyper dmabuf driver" + default y
Not sure you want this enabled by default
drivers/dma-buf/hyper_dmabuf/Kconfig line 15
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/Kconfig new file +10
-0+ help + This option enables Hyper_DMABUF driver. + + This driver works as abstraction layer that export and import + DMA_BUF from/to another virtual OS running on the same HW platform + powered by a hypervisor + +config HYPER_DMABUF_SYSFS + bool "Enable sysfs information about hyper DMA buffers" + default y
Ditto
Quoted 14 lines
drivers/dma-buf/hyper_dmabuf/Kconfig new file +8
-0+ depends on HYPER_DMABUF + help + Expose run-time information about currently imported and exported buffers + registered in EXPORT and IMPORT list in Hyper_DMABUF driver. + + The location of sysfs is under "...." + +endmenudrivers/dma-buf/hyper_dmabuf/Makefile new file +0
-0
drivers/dma-buf/hyper_dmabuf/Makefile line 4
Quoted 4 lines
drivers/dma-buf/hyper_dmabuf/Makefile new file +4
-0@@ -0,0 +1,34 @@ +TARGET_MODULE:=hyper_dmabuf + +# If we running by kernel building system +ifneq ($(KERNELRELEASE),)
Not sure why you need this
drivers/dma-buf/hyper_dmabuf/Makefile line 15
Quoted 11 lines
drivers/dma-buf/hyper_dmabuf/Makefile new file +11
-0+ $(TARGET_MODULE)-objs := hyper_dmabuf_drv.o \ + hyper_dmabuf_ioctl.o \ + hyper_dmabuf_list.o \ + hyper_dmabuf_sgl_proc.o \ + hyper_dmabuf_ops.o \ + hyper_dmabuf_msg.o \ + hyper_dmabuf_id.o \ + +obj-$(CONFIG_HYPER_DMABUF) := $(TARGET_MODULE).o + +# If we are running without kernel build system
Ditto
Quoted 8 lines
drivers/dma-buf/hyper_dmabuf/Makefile new file +8
-0+else +BUILDSYSTEM_DIR?=../../../ +PWD:=$(shell pwd) + +all : +# run kernel build system to make module +$(MAKE) -C $(BUILDSYSTEM_DIR) M=$(PWD) modules +
drivers/dma-buf/hyper_dmabuf/Makefile line 33
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/Makefile new file +10
-0+clean: +# run kernel build system to cleanup in current directory +$(MAKE) -C $(BUILDSYSTEM_DIR) M=$(PWD) clean + +load: + insmod ./$(TARGET_MODULE).ko + +unload: + rmmod ./$(TARGET_MODULE).ko +
This seems to be some helper code you use while doing development which needs to be removed
Quoted 45 lines
drivers/dma-buf/hyper_dmabuf/Makefile new file +1
-0+endifdrivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +38
-0@@ -0,0 +1,254 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/init.h> +#include <linux/module.h> +#include <linux/miscdevice.h> +#include <linux/workqueue.h> +#include <linux/slab.h> +#include <linux/device.h> +#include <linux/uaccess.h> +#include <linux/poll.h>
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 48
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +10
-0+#include <linux/dma-buf.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_ioctl.h" +#include "hyper_dmabuf_list.h" +#include "hyper_dmabuf_id.h" + +MODULE_LICENSE ("GPL and additional rights" ); +MODULE_AUTHOR ("Intel Corporation" ); + +struct hyper_dmabuf_private *hy_drv_priv;
instead of using a global symbol here you might want to first allocate misc device and then use devm_kzalloc to allocate your private data
Quoted 41 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +41
-0+ +static void force_free (struct exported_sgt_info *exported, +void *attr) +{ +struct ioctl_hyper_dmabuf_unexport unexport_attr; +struct file *filp = (struct file *)attr; + +if (!filp || !exported) +return ; + +if (exported->filp == filp) { +dev_dbg (hy_drv_priv->dev, +"Forcefully releasing buffer {id:%d key:%d %d %d}\n" , + exported->hid.id, exported->hid.rng_key[0 ], + exported->hid.rng_key[1 ], exported->hid.rng_key[2 ]); + + unexport_attr.hid = exported->hid; + unexport_attr.delay_ms =0 ; + +hyper_dmabuf_unexport_ioctl (filp, &unexport_attr); + } +} + +static int hyper_dmabuf_open (struct inode *inode,struct file *filp) +{ +int ret =0 ; + +/* Do not allow exclusive open */ +if (filp->f_flags &O_EXCL ) +return -EBUSY ; + +return ret; +} + +static int hyper_dmabuf_release (struct inode *inode,struct file *filp) +{ +hyper_dmabuf_foreach_exported (force_free, filp); + +return 0 ; +} +
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 99
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +10
-0+static const struct file_operations hyper_dmabuf_driver_fops = { + .owner =THIS_MODULE , + .open = hyper_dmabuf_open, + .release = hyper_dmabuf_release, + .unlocked_ioctl = hyper_dmabuf_ioctl, +}; + +static struct miscdevice hyper_dmabuf_miscdev = { + .minor =MISC_DYNAMIC_MINOR , + .name ="hyper_dmabuf" ,
Can this string be a constant through the driver?
Quoted 49 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +49
-0+ .fops = &hyper_dmabuf_driver_fops, +}; + +static int register_device (void ) +{ +int ret =0 ; + + ret =misc_register (&hyper_dmabuf_miscdev); + +if (ret) { +pr_err ("hyper_dmabuf: driver can't be registered\n" ); +return ret; + } + + hy_drv_priv->dev = hyper_dmabuf_miscdev.this_device; + +/* TODO: Check if there is a different way to initialize dma mask */ +dma_coerce_mask_and_coherent (hy_drv_priv->dev,DMA_BIT_MASK (64 )); + +return ret; +} + +static void unregister_device (void ) +{ +dev_info (hy_drv_priv->dev, +"hyper_dmabuf: %s is called\n" , __func__); + +misc_deregister (&hyper_dmabuf_miscdev); +} + +static int __init hyper_dmabuf_drv_init (void ) +{ +int ret =0 ; + +pr_notice ("hyper_dmabuf_starting: Initialization started\n" ); + + hy_drv_priv =kcalloc (1 ,sizeof (struct hyper_dmabuf_private ), +GFP_KERNEL ); + +if (!hy_drv_priv) +return -ENOMEM ; + + ret =register_device (); +if (ret <0 ) { +kfree (hy_drv_priv); +return ret; + } + + hy_drv_priv->bknd_ops =NULL ;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 158
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +10
-0+ +if (hy_drv_priv->bknd_ops ==NULL ) { +pr_err ("Hyper_dmabuf: no backend found\n" ); +kfree (hy_drv_priv); +return -1 ; + } + +mutex_init (&hy_drv_priv->lock); + +mutex_lock (&hy_drv_priv->lock);
Why do you need to immediately lock here?
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 160
Quoted 2 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +2
-0+ + hy_drv_priv->initialized = false;
kcalloc allocates zeroed memory, so you might rely on that fact
Quoted 24 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +24
-0+ +dev_info (hy_drv_priv->dev, +"initializing database for imported/exported dmabufs\n" ); + + hy_drv_priv->work_queue =create_workqueue ("hyper_dmabuf_wqueue" ); + + ret =hyper_dmabuf_table_init (); +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"fail to init table for exported/imported entries\n" ); +mutex_unlock (&hy_drv_priv->lock); +kfree (hy_drv_priv); +return ret; + } + +#ifdef CONFIG_HYPER_DMABUF_SYSFS + ret =hyper_dmabuf_register_sysfs (hy_drv_priv->dev); +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"failed to initialize sysfs\n" ); +mutex_unlock (&hy_drv_priv->lock); +kfree (hy_drv_priv); +return ret; + }
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 194
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +10
-0+#endif + +if (hy_drv_priv->bknd_ops->init) { + ret = hy_drv_priv->bknd_ops->init (); + +if (ret <0 ) { +dev_dbg (hy_drv_priv->dev, +"failed to initialize backend.\n" ); +mutex_unlock (&hy_drv_priv->lock); +kfree (hy_drv_priv);
unregister sysfs?
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 200
Quoted 6 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +6
-0+return ret; + } + } + + hy_drv_priv->domid = hy_drv_priv->bknd_ops->get_vm_id (); +
This seems to be a bit inconsistent, e.g. domid vs vm_id
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 204
Quoted 4 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +4
-0+ ret = hy_drv_priv->bknd_ops->init_comm_env (); +if (ret <0 ) { +dev_dbg (hy_drv_priv->dev, +"failed to initialize comm-env.\n" );
bknd_ops->cleanup?
Quoted 4 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +4
-0+ }else { + hy_drv_priv->initialized = true; + } +
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 218
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +10
-0+mutex_unlock (&hy_drv_priv->lock); + +dev_info (hy_drv_priv->dev, +"Finishing up initialization of hyper_dmabuf drv\n" ); + +/* interrupt for comm should be registered here: */ +return ret; +} + +static void hyper_dmabuf_drv_exit (void )
__exit?
Quoted 185 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +36
-0+{ +#ifdef CONFIG_HYPER_DMABUF_SYSFS +hyper_dmabuf_unregister_sysfs (hy_drv_priv->dev); +#endif + +mutex_lock (&hy_drv_priv->lock); + +/* hash tables for export/import entries and ring_infos */ +hyper_dmabuf_table_destroy (); + + hy_drv_priv->bknd_ops->destroy_comm (); + +if (hy_drv_priv->bknd_ops->cleanup) { + hy_drv_priv->bknd_ops->cleanup (); + }; + +/* destroy workqueue */ +if (hy_drv_priv->work_queue) +destroy_workqueue (hy_drv_priv->work_queue); + +/* destroy id_queue */ +if (hy_drv_priv->id_queue) +hyper_dmabuf_free_hid_list (); + +mutex_unlock (&hy_drv_priv->lock); + +dev_info (hy_drv_priv->dev, +"hyper_dmabuf driver: Exiting\n" ); + +kfree (hy_drv_priv); + +unregister_device (); +} + +module_init (hyper_dmabuf_drv_init); +module_exit (hyper_dmabuf_drv_exit);drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h new file +111
-0@@ -0,0 +1,111 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __LINUX_HYPER_DMABUF_DRV_H__ +#define __LINUX_HYPER_DMABUF_DRV_H__ + +#include <linux/device.h> +#include <linux/hyper_dmabuf.h> + +struct hyper_dmabuf_req ; + +struct hyper_dmabuf_private { +struct device *dev; + +/* VM(domain) id of current VM instance */ +int domid; + +/* workqueue dedicated to hyper_dmabuf driver */ +struct workqueue_struct *work_queue; + +/* list of reusable hyper_dmabuf_ids */ +struct list_reusable_id *id_queue; + +/* backend ops - hypervisor specific */ +struct hyper_dmabuf_bknd_ops *bknd_ops; + +/* device global lock */ +/* TODO: might need a lock per resource (e.g. EXPORT LIST) */ +struct mutex lock; + +/* flag that shows whether backend is initialized */ +bool initialized; + +/* # of pending events */ +int pending; +}; + +struct list_reusable_id { +hyper_dmabuf_id_t hid; +struct list_head list; +}; + +struct hyper_dmabuf_bknd_ops { +/* backend initialization routine (optional) */ +int (*init)(void ); + +/* backend cleanup routine (optional) */ +int (*cleanup)(void ); + +/* retreiving id of current virtual machine */ +int (*get_vm_id)(void ); + +/* get pages shared via hypervisor-specific method */ +int (*share_pages)(struct page **pages,int vm_id, +int nents,void **refs_info); + +/* make shared pages unshared via hypervisor specific method */ +int (*unshare_pages)(void **refs_info,int nents); + +/* map remotely shared pages on importer's side via +* hypervisor-specific method +*/ +struct page ** (*map_shared_pages)(unsigned long ref,int vm_id, +int nents,void **refs_info); + +/* unmap and free shared pages on importer's side via +* hypervisor-specific method +*/ +int (*unmap_shared_pages)(void **refs_info,int nents); + +/* initialize communication environment */ +int (*init_comm_env)(void ); + +void (*destroy_comm)(void ); + +/* upstream ch setup (receiving and responding) */ +int (*init_rx_ch)(int vm_id); + +/* downstream ch setup (transmitting and parsing responses) */ +int (*init_tx_ch)(int vm_id); + +int (*send_req)(int vm_id,struct hyper_dmabuf_req *req,int wait); +}; + +/* exporting global drv private info */ +extern struct hyper_dmabuf_private *hy_drv_priv; + +#endif /* __LINUX_HYPER_DMABUF_DRV_H__ */ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +26
-0@@ -0,0 +1,135 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]>
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c line 36
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +10
-0+* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/list.h> +#include <linux/slab.h> +#include <linux/random.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_id.h" +
Common notes:
- I think even if hy_drv_priv is global you shouldn't touch it directly, but pass it as function parameter.
- Don't you need to protect reusable list with lock?
Quoted 47 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +47
-0+void hyper_dmabuf_store_hid (hyper_dmabuf_id_t hid) +{ +struct list_reusable_id *reusable_head = hy_drv_priv->id_queue; +struct list_reusable_id *new_reusable; + + new_reusable =kmalloc (sizeof (*new_reusable),GFP_KERNEL ); + +if (!new_reusable) +return ; + + new_reusable->hid = hid; + +list_add (&new_reusable->list, &reusable_head->list); +} + +static hyper_dmabuf_id_t get_reusable_hid (void ) +{ +struct list_reusable_id *reusable_head = hy_drv_priv->id_queue; +hyper_dmabuf_id_t hid = {-1 , {0 ,0 ,0 } }; + +/* check there is reusable id */ +if (!list_empty (&reusable_head->list)) { + reusable_head =list_first_entry (&reusable_head->list, + struct list_reusable_id, + list); + +list_del (&reusable_head->list); + hid = reusable_head->hid; +kfree (reusable_head); + } + +return hid; +} + +void hyper_dmabuf_free_hid_list (void ) +{ +struct list_reusable_id *reusable_head = hy_drv_priv->id_queue; +struct list_reusable_id *temp_head; + +if (reusable_head) { +/* freeing mem space all reusable ids in the stack */ +while (!list_empty (&reusable_head->list)) { + temp_head =list_first_entry (&reusable_head->list, + struct list_reusable_id, + list); +list_del (&temp_head->list); +kfree (temp_head);
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c line 93
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +10
-0+ } + +/* freeing head */ +kfree (reusable_head); + } +} + +hyper_dmabuf_id_t hyper_dmabuf_get_hid (void ) +{ +static int count;
could you please explicitly initialize this?
Quoted 9 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +9
-0+hyper_dmabuf_id_t hid; +struct list_reusable_id *reusable_head; + +/* first call to hyper_dmabuf_get_id */ +if (count ==0 ) { + reusable_head =kmalloc (sizeof (*reusable_head),GFP_KERNEL ); + +if (!reusable_head) +return (hyper_dmabuf_id_t ){-1 , {0 ,0 ,0 } };
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c line 112
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +10
-0+ +/* list head has an invalid count */ + reusable_head->hid.id =-1 ; +INIT_LIST_HEAD (&reusable_head->list); + hy_drv_priv->id_queue = reusable_head; + } + + hid =get_reusable_hid (); + +/*creating a new H-ID only if nothing in the reusable id queue
start the comment from a new line
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c line 119
Quoted 7 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +7
-0+* and count is less than maximum allowed +*/ +if (hid.id ==-1 && count <HYPER_DMABUF_ID_MAX ) + hid.id =HYPER_DMABUF_ID_CREATE (hy_drv_priv->domid, count++); + +/* random data embedded in the id for security */ +get_random_bytes (&hid.rng_key[0 ],12 );
can magic 12 be a defined constant?
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c line 129
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +10
-0+ +return hid; +} + +bool hyper_dmabuf_hid_keycomp (hyper_dmabuf_id_t hid1,hyper_dmabuf_id_t hid2) +{ +int i; + +/* compare keys */ +for (i =0 ; i <3 ; i++) {
can magic 3 be defined as a constant please?
Quoted 33 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +6
-0+if (hid1.rng_key[i] != hid2.rng_key[i]) +return false; + } + +return true; +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +21
-0@@ -0,0 +1,53 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE.
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h line 31
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +10
-0+* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_ID_H__ +#define __HYPER_DMABUF_ID_H__ + +#define HYPER_DMABUF_ID_CREATE (domid, cnt) \ + ((((domid) & 0xFF) << 24) | ((cnt) & 0xFFFFFF))
I would define hyper_dmabuf_id_t.id as a union or 2 separate fields to avoid his magic
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h line 39
Quoted 8 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +8
-0+ +#define HYPER_DMABUF_DOM_ID (hid) \ + (((hid.id) >> 24) & 0xFF) + +/* currently maximum number of buffers shared +* at any given moment is limited to 1000 +*/ +#define HYPER_DMABUF_ID_MAX 1000
Why 1000? Is it just to limit or is dictated by some use-cases/experiments?
Quoted 54 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +14
-0+ +/* adding freed hid to the reusable list */ +void hyper_dmabuf_store_hid (hyper_dmabuf_id_t hid); + +/* freeing the reusasble list */ +void hyper_dmabuf_free_hid_list (void ); + +/* getting a hid available to use. */ +hyper_dmabuf_id_t hyper_dmabuf_get_hid (void ); + +/* comparing two different hid */ +bool hyper_dmabuf_hid_keycomp (hyper_dmabuf_id_t hid1,hyper_dmabuf_id_t hid2); + +#endif /*__HYPER_DMABUF_ID_H*/ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +34
-0@@ -0,0 +1,672 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/uaccess.h>
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 44
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+#include <linux/dma-buf.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_id.h" +#include "hyper_dmabuf_struct.h" +#include "hyper_dmabuf_ioctl.h" +#include "hyper_dmabuf_list.h" +#include "hyper_dmabuf_msg.h" +#include "hyper_dmabuf_sgl_proc.h" +#include "hyper_dmabuf_ops.h" +
Here and below: please do not touch global hy_drv_priv
Quoted 40 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +40
-0+static int hyper_dmabuf_tx_ch_setup_ioctl (struct file *filp,void *data) +{ +struct ioctl_hyper_dmabuf_tx_ch_setup *tx_ch_attr; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +int ret =0 ; + +if (!data) { +dev_err (hy_drv_priv->dev,"user data is NULL\n" ); +return -EINVAL ; + } + tx_ch_attr = (struct ioctl_hyper_dmabuf_tx_ch_setup *)data; + + ret = bknd_ops->init_tx_ch (tx_ch_attr->remote_domain); + +return ret; +} + +static int hyper_dmabuf_rx_ch_setup_ioctl (struct file *filp,void *data) +{ +struct ioctl_hyper_dmabuf_rx_ch_setup *rx_ch_attr; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +int ret =0 ; + +if (!data) { +dev_err (hy_drv_priv->dev,"user data is NULL\n" ); +return -EINVAL ; + } + + rx_ch_attr = (struct ioctl_hyper_dmabuf_rx_ch_setup *)data; + + ret = bknd_ops->init_rx_ch (rx_ch_attr->source_domain); + +return ret; +} + +static int send_export_msg (struct exported_sgt_info *exported, +struct pages_info *pg_info) +{ +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +struct hyper_dmabuf_req *req;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 94
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+int op[MAX_NUMBER_OF_OPERANDS ] = {0 }; +int ret, i; + +/* now create request for importer via ring */ + op[0 ] = exported->hid.id; + +for (i =0 ; i <3 ; i++) + op[i+1 ] = exported->hid.rng_key[i]; + +if (pg_info) {
heh, can we have a well defined structures for requests/responses, so we don't have to put all these magics?
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 99
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +5
-0+ op[4 ] = pg_info->nents; + op[5 ] = pg_info->frst_ofst; + op[6 ] = pg_info->last_len; + op[7 ] = bknd_ops->share_pages (pg_info->pgs, exported->rdomid, + pg_info->nents, &exported->refs_info);
ret?
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +5
-0+if (op[7 ] <0 ) { +dev_err (hy_drv_priv->dev,"pages sharing failed\n" ); +return op[7 ]; + } + }
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 114
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+ + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL ); + +if (!req) +return -ENOMEM ; + +/* composing a message to the importer */ +hyper_dmabuf_create_req (req,HYPER_DMABUF_EXPORT , &op[0 ]); + + ret = bknd_ops->send_req (exported->rdomid, req, true);
can we allocate req on stack? and don't use kcalloc?
Quoted 11 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +11
-0+ +kfree (req); + +return ret; +} + +/* Fast path exporting routine in case same buffer is already exported. +* +* If same buffer is still valid and exist in EXPORT LIST it returns 0 so +* that remaining normal export process can be skipped. +*
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 135
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+* If "unexport" is scheduled for the buffer, it cancels it since the buffer +* is being re-exported. +* +* return '1' if reexport is needed, return '0' if succeeds, return +* Kernel error code if something goes wrong +*/ +static int fastpath_export (hyper_dmabuf_id_t hid) +{ +int reexport =1 ; +int ret =0 ;
why do you need these two variables?
Quoted 64 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +64
-0+struct exported_sgt_info *exported; + + exported =hyper_dmabuf_find_exported (hid); + +if (!exported) +return reexport; + +if (exported->valid == false) +return reexport; + +/* +* Check if unexport is already scheduled for that buffer, +* if so try to cancel it. If that will fail, buffer needs +* to be reexport once again. +*/ +if (exported->unexport_sched) { +if (!cancel_delayed_work_sync (&exported->unexport)) +return reexport; + + exported->unexport_sched = false; + } + +return ret; +} + +static int hyper_dmabuf_export_remote_ioctl (struct file *filp,void *data) +{ +struct ioctl_hyper_dmabuf_export_remote *export_remote_attr = + (struct ioctl_hyper_dmabuf_export_remote *)data; +struct dma_buf *dma_buf; +struct dma_buf_attachment *attachment; +struct sg_table *sgt; +struct pages_info *pg_info; +struct exported_sgt_info *exported; +hyper_dmabuf_id_t hid; +int ret =0 ; + +if (hy_drv_priv->domid == export_remote_attr->remote_domain) { +dev_err (hy_drv_priv->dev, +"exporting to the same VM is not permitted\n" ); +return -EINVAL ; + } + + dma_buf =dma_buf_get (export_remote_attr->dmabuf_fd); + +if (IS_ERR (dma_buf)) { +dev_err (hy_drv_priv->dev,"Cannot get dma buf\n" ); +return PTR_ERR (dma_buf); + } + +/* we check if this specific attachment was already exported +* to the same domain and if yes and it's valid sgt_info, +* it returns hyper_dmabuf_id of pre-exported sgt_info +*/ + hid =hyper_dmabuf_find_hid_exported (dma_buf, + export_remote_attr->remote_domain); + +if (hid.id !=-1 ) { + ret =fastpath_export (hid); + +/* return if fastpath_export succeeds or +* gets some fatal error +*/ +if (ret <=0 ) {
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 209
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+dma_buf_put (dma_buf); + export_remote_attr->hid = hid; +return ret; + } + } + + attachment =dma_buf_attach (dma_buf, hy_drv_priv->dev); +if (IS_ERR (attachment)) { +dev_err (hy_drv_priv->dev,"cannot get attachment\n" ); + ret =PTR_ERR (attachment);
here and below - if you have dma-buf from fastpath don't you need to release/handle it on error path here? E.g. fastpath may have canceled unexport work for this buffer
Quoted 145 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +145
-0+ goto fail_attach; + } + + sgt =dma_buf_map_attachment (attachment,DMA_BIDIRECTIONAL ); + +if (IS_ERR (sgt)) { +dev_err (hy_drv_priv->dev,"cannot map attachment\n" ); + ret =PTR_ERR (sgt); + goto fail_map_attachment; + } + + exported =kcalloc (1 ,sizeof (*exported),GFP_KERNEL ); + +if (!exported) { + ret = -ENOMEM ; + goto fail_sgt_info_creation; + } + + exported->hid =hyper_dmabuf_get_hid (); + +/* no more exported dmabuf allowed */ +if (exported->hid.id ==-1 ) { +dev_err (hy_drv_priv->dev, +"exceeds allowed number of dmabuf to be exported\n" ); + ret = -ENOMEM ; + goto fail_sgt_info_creation; + } + + exported->rdomid = export_remote_attr->remote_domain; + exported->dma_buf = dma_buf; + exported->valid = true; + + exported->active_sgts =kmalloc (sizeof (struct sgt_list ),GFP_KERNEL ); +if (!exported->active_sgts) { + ret = -ENOMEM ; + goto fail_map_active_sgts; + } + + exported->active_attached =kmalloc (sizeof (struct attachment_list ), +GFP_KERNEL ); +if (!exported->active_attached) { + ret = -ENOMEM ; + goto fail_map_active_attached; + } + + exported->va_kmapped =kmalloc (sizeof (struct kmap_vaddr_list ), +GFP_KERNEL ); +if (!exported->va_kmapped) { + ret = -ENOMEM ; + goto fail_map_va_kmapped; + } + + exported->va_vmapped =kmalloc (sizeof (struct vmap_vaddr_list ), +GFP_KERNEL ); +if (!exported->va_vmapped) { + ret = -ENOMEM ; + goto fail_map_va_vmapped; + } + + exported->active_sgts->sgt = sgt; + exported->active_attached->attach = attachment; + exported->va_kmapped->vaddr =NULL ; + exported->va_vmapped->vaddr =NULL ; + +/* initialize list of sgt, attachment and vaddr for dmabuf sync +* via shadow dma-buf +*/ +INIT_LIST_HEAD (&exported->active_sgts->list); +INIT_LIST_HEAD (&exported->active_attached->list); +INIT_LIST_HEAD (&exported->va_kmapped->list); +INIT_LIST_HEAD (&exported->va_vmapped->list); + +if (ret) { +dev_err (hy_drv_priv->dev, +"failed to load private data\n" ); + ret = -EINVAL ; + goto fail_export; + } + + pg_info =hyper_dmabuf_ext_pgs (sgt); +if (!pg_info) { +dev_err (hy_drv_priv->dev, +"failed to construct pg_info\n" ); + ret = -ENOMEM ; + goto fail_export; + } + + exported->nents = pg_info->nents; + +/* now register it to export list */ +hyper_dmabuf_register_exported (exported); + + export_remote_attr->hid = exported->hid; + + ret =send_export_msg (exported, pg_info); + +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"failed to send out the export request\n" ); + goto fail_send_request; + } + +/* free pg_info */ +kfree (pg_info->pgs); +kfree (pg_info); + + exported->filp = filp; + +return ret; + +/* Clean-up if error occurs */ + +fail_send_request: +hyper_dmabuf_remove_exported (exported->hid); + +/* free pg_info */ +kfree (pg_info->pgs); +kfree (pg_info); + +fail_export: +kfree (exported->va_vmapped); + +fail_map_va_vmapped: +kfree (exported->va_kmapped); + +fail_map_va_kmapped: +kfree (exported->active_attached); + +fail_map_active_attached: +kfree (exported->active_sgts); +kfree (exported); + +fail_map_active_sgts: +fail_sgt_info_creation: +dma_buf_unmap_attachment (attachment, sgt, +DMA_BIDIRECTIONAL ); + +fail_map_attachment: +dma_buf_detach (dma_buf, attachment); + +fail_attach: +dma_buf_put (dma_buf); + +return ret; +}
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 364
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+ +static int hyper_dmabuf_export_fd_ioctl (struct file *filp,void *data) +{ +struct ioctl_hyper_dmabuf_export_fd *export_fd_attr = + (struct ioctl_hyper_dmabuf_export_fd *)data; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +struct imported_sgt_info *imported; +struct hyper_dmabuf_req *req; +struct page **data_pgs; +int op[4 ];
don't you have hyper_dmabuf_id_t for that?
Quoted 19 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +19
-0+int i; +int ret =0 ; + +dev_dbg (hy_drv_priv->dev,"%s entry\n" , __func__); + +/* look for dmabuf for the id */ + imported =hyper_dmabuf_find_imported (export_fd_attr->hid); + +/* can't find sgt from the table */ +if (!imported) { +dev_err (hy_drv_priv->dev,"can't find the entry\n" ); +return -ENOENT ; + } + +mutex_lock (&hy_drv_priv->lock); + + imported->importers++; + +/* send notification for export_fd to exporter */
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 393
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+ op[0 ] = imported->hid.id; + +for (i =0 ; i <3 ; i++) + op[i+1 ] = imported->hid.rng_key[i]; + +dev_dbg (hy_drv_priv->dev,"Export FD of buffer {id:%d key:%d %d %d}\n" , + imported->hid.id, imported->hid.rng_key[0 ], + imported->hid.rng_key[1 ], imported->hid.rng_key[2 ]); + + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL );
can you have req allocated on stack?
Quoted 4 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +4
-0+ +if (!req) { +mutex_unlock (&hy_drv_priv->lock); +return -ENOMEM ;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 407
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+ } + +hyper_dmabuf_create_req (req,HYPER_DMABUF_EXPORT_FD , &op[0 ]); + + ret = bknd_ops->send_req (HYPER_DMABUF_DOM_ID (imported->hid), req, true); + +if (ret <0 ) { +/* in case of timeout other end eventually will receive request, +* so we need to undo it +*/
and what if there is a race condition? at the time you delete the buffer the corresponding response comes in?
Quoted 77 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +77
-0+hyper_dmabuf_create_req (req,HYPER_DMABUF_EXPORT_FD_FAILED , + &op[0 ]); + bknd_ops->send_req (HYPER_DMABUF_DOM_ID (imported->hid), + req, false); +kfree (req); +dev_err (hy_drv_priv->dev, +"Failed to create sgt or notify exporter\n" ); + imported->importers--; +mutex_unlock (&hy_drv_priv->lock); +return ret; + } + +kfree (req); + +if (ret ==HYPER_DMABUF_REQ_ERROR ) { +dev_err (hy_drv_priv->dev, +"Buffer invalid {id:%d key:%d %d %d}, cannot import\n" , + imported->hid.id, imported->hid.rng_key[0 ], + imported->hid.rng_key[1 ], imported->hid.rng_key[2 ]); + + imported->importers--; +mutex_unlock (&hy_drv_priv->lock); +return -EINVAL ; + } + + ret =0 ; + +dev_dbg (hy_drv_priv->dev, +"Found buffer gref %d off %d\n" , + imported->ref_handle, imported->frst_ofst); + +dev_dbg (hy_drv_priv->dev, +"last len %d nents %d domain %d\n" , + imported->last_len, imported->nents, +HYPER_DMABUF_DOM_ID (imported->hid)); + +if (!imported->sgt) { +dev_dbg (hy_drv_priv->dev, +"buffer {id:%d key:%d %d %d} pages not mapped yet\n" , + imported->hid.id, imported->hid.rng_key[0 ], + imported->hid.rng_key[1 ], imported->hid.rng_key[2 ]); + + data_pgs = bknd_ops->map_shared_pages (imported->ref_handle, +HYPER_DMABUF_DOM_ID (imported->hid), + imported->nents, + &imported->refs_info); + +if (!data_pgs) { +dev_err (hy_drv_priv->dev, +"can't map pages hid {id:%d key:%d %d %d}\n" , + imported->hid.id, imported->hid.rng_key[0 ], + imported->hid.rng_key[1 ], + imported->hid.rng_key[2 ]); + + imported->importers--; + + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL ); + +if (!req) { +mutex_unlock (&hy_drv_priv->lock); +return -ENOMEM ; + } + +hyper_dmabuf_create_req (req, +HYPER_DMABUF_EXPORT_FD_FAILED , + &op[0 ]); + + bknd_ops->send_req (HYPER_DMABUF_DOM_ID (imported->hid), + req, false); +kfree (req); +mutex_unlock (&hy_drv_priv->lock); +return -EINVAL ; + } + + imported->sgt = hyper_dmabuf_create_sgt(data_pgs, + imported->frst_ofst, + imported->last_len,
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 494
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+ imported->nents); + + } + + export_fd_attr->fd =hyper_dmabuf_export_fd (imported, + export_fd_attr->flags); + +if (export_fd_attr->fd <0 ) { +/* fail to get fd */ + ret = export_fd_attr->fd;
why don't you send HYPER_DMABUF_EXPORT_FD_FAILED in this case?
Quoted 8 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +8
-0+ } + +mutex_unlock (&hy_drv_priv->lock); + +dev_dbg (hy_drv_priv->dev,"%s exit\n" , __func__); +return ret; +} +
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 512
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+/* unexport dmabuf from the database and send int req to the source domain +* to unmap it. +*/ +static void delayed_unexport (struct work_struct *work) +{ +struct hyper_dmabuf_req *req; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +struct exported_sgt_info *exported = + container_of(work, struct exported_sgt_info, unexport.work); +int op[4 ];
use the struct defined for this
Quoted 6 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +6
-0+int i, ret; + +if (!exported) +return ; + + dev_dbg(hy_drv_priv->dev,
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 528
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+"Marking buffer {id:%d key:%d %d %d} as invalid\n" , + exported->hid.id, exported->hid.rng_key[0 ], + exported->hid.rng_key[1], exported->hid.rng_key[2]); + +/* no longer valid */ + exported->valid = false; + + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL ); + +if (!req)
will we leak the buffer because we return here?
Quoted 64 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +64
-0+return ; + + op[0 ] = exported->hid.id; + +for (i =0 ; i <3 ; i++) + op[i+1 ] = exported->hid.rng_key[i]; + +hyper_dmabuf_create_req (req,HYPER_DMABUF_NOTIFY_UNEXPORT , &op[0 ]); + +/* Now send unexport request to remote domain, marking +* that buffer should not be used anymore +*/ + ret = bknd_ops->send_req (exported->rdomid, req, true); +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"unexport message for buffer {id:%d key:%d %d %d} failed\n" , + exported->hid.id, exported->hid.rng_key[0 ], + exported->hid.rng_key[1 ], exported->hid.rng_key[2 ]); + } + +kfree (req); + exported->unexport_sched = false; + +/* Immediately clean-up if it has never been exported by importer +* (so no SGT is constructed on importer). +* clean it up later in remote sync when final release ops +* is called (importer does this only when there's no +* no consumer of locally exported FDs) +*/ +if (exported->active ==0 ) { +dev_dbg (hy_drv_priv->dev, +"claning up buffer {id:%d key:%d %d %d} completly\n" , + exported->hid.id, exported->hid.rng_key[0 ], + exported->hid.rng_key[1 ], exported->hid.rng_key[2 ]); + +hyper_dmabuf_cleanup_sgt_info (exported, false); +hyper_dmabuf_remove_exported (exported->hid); + +/* register hyper_dmabuf_id to the list for reuse */ +hyper_dmabuf_store_hid (exported->hid); + +kfree (exported); + } +} + +/* Schedule unexport of dmabuf. +*/ +int hyper_dmabuf_unexport_ioctl (struct file *filp,void *data) +{ +struct ioctl_hyper_dmabuf_unexport *unexport_attr = + (struct ioctl_hyper_dmabuf_unexport *)data; +struct exported_sgt_info *exported; + +dev_dbg (hy_drv_priv->dev,"%s entry\n" , __func__); + +/* find dmabuf in export list */ + exported =hyper_dmabuf_find_exported (unexport_attr->hid); + +dev_dbg (hy_drv_priv->dev, +"scheduling unexport of buffer {id:%d key:%d %d %d}\n" , + unexport_attr->hid.id, unexport_attr->hid.rng_key[0 ], + unexport_attr->hid.rng_key[1 ], unexport_attr->hid.rng_key[2 ]); + +/* failed to find corresponding entry in export list */
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 602
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+if (exported ==NULL ) { + unexport_attr->status = -ENOENT ; +return -ENOENT ; + } + +if (exported->unexport_sched) +return 0 ; + + exported->unexport_sched = true; +INIT_DELAYED_WORK (&exported->unexport, delayed_unexport);
why can't you just wait for the buffer to be unexported?
Quoted 549 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +70
-0+schedule_delayed_work (&exported->unexport, +msecs_to_jiffies (unexport_attr->delay_ms)); + +dev_dbg (hy_drv_priv->dev,"%s exit\n" , __func__); +return 0 ; +} + +const struct hyper_dmabuf_ioctl_desc hyper_dmabuf_ioctls[] = { +HYPER_DMABUF_IOCTL_DEF (IOCTL_HYPER_DMABUF_TX_CH_SETUP , + hyper_dmabuf_tx_ch_setup_ioctl,0 ), +HYPER_DMABUF_IOCTL_DEF (IOCTL_HYPER_DMABUF_RX_CH_SETUP , + hyper_dmabuf_rx_ch_setup_ioctl,0 ), +HYPER_DMABUF_IOCTL_DEF (IOCTL_HYPER_DMABUF_EXPORT_REMOTE , + hyper_dmabuf_export_remote_ioctl,0 ), +HYPER_DMABUF_IOCTL_DEF (IOCTL_HYPER_DMABUF_EXPORT_FD , + hyper_dmabuf_export_fd_ioctl,0 ), +HYPER_DMABUF_IOCTL_DEF (IOCTL_HYPER_DMABUF_UNEXPORT , + hyper_dmabuf_unexport_ioctl,0 ), +}; + +long hyper_dmabuf_ioctl (struct file *filp, +unsigned int cmd,unsigned long param) +{ +const struct hyper_dmabuf_ioctl_desc *ioctl =NULL ; +unsigned int nr =_IOC_NR (cmd); +int ret; +hyper_dmabuf_ioctl_t func; +char *kdata; + +if (nr >ARRAY_SIZE (hyper_dmabuf_ioctls)) { +dev_err (hy_drv_priv->dev,"invalid ioctl\n" ); +return -EINVAL ; + } + + ioctl = &hyper_dmabuf_ioctls[nr]; + + func = ioctl->func; + +if (unlikely (!func)) { +dev_err (hy_drv_priv->dev,"no function\n" ); +return -EINVAL ; + } + + kdata =kmalloc (_IOC_SIZE (cmd),GFP_KERNEL ); +if (!kdata) +return -ENOMEM ; + +if (copy_from_user (kdata, (void __user *)param, +_IOC_SIZE (cmd)) !=0 ) { +dev_err (hy_drv_priv->dev, +"failed to copy from user arguments\n" ); + ret = -EFAULT ; + goto ioctl_error; + } + + ret =func (filp, kdata); + +if (copy_to_user ((void __user *)param, kdata, +_IOC_SIZE (cmd)) !=0 ) { +dev_err (hy_drv_priv->dev, +"failed to copy to user arguments\n" ); + ret = -EFAULT ; + goto ioctl_error; + } + +ioctl_error: +kfree (kdata); + +return ret; +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h new file +52
-0@@ -0,0 +1,52 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_IOCTL_H__ +#define __HYPER_DMABUF_IOCTL_H__ + +typedef int (*hyper_dmabuf_ioctl_t )(struct file *filp,void *data); + +struct hyper_dmabuf_ioctl_desc { +unsigned int cmd; +int flags; +hyper_dmabuf_ioctl_t func; +const char *name; +}; + +#define HYPER_DMABUF_IOCTL_DEF (ioctl, _func, _flags) \ + [_IOC_NR(ioctl)] = { \ + .cmd = ioctl, \ + .func = _func, \ + .flags = _flags, \ + .name = #ioctl \ + } + +long hyper_dmabuf_ioctl (struct file *filp, +unsigned int cmd,unsigned long param); + +int hyper_dmabuf_unexport_ioctl (struct file *filp,void *data); + +#endif //__HYPER_DMABUF_IOCTL_H__ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c new file +294
-0@@ -0,0 +1,294 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/cdev.h> +#include <linux/hashtable.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_list.h" +#include "hyper_dmabuf_id.h" + +DECLARE_HASHTABLE (hyper_dmabuf_hash_imported,MAX_ENTRY_IMPORTED ); +DECLARE_HASHTABLE (hyper_dmabuf_hash_exported,MAX_ENTRY_EXPORTED ); + +#ifdef CONFIG_HYPER_DMABUF_SYSFS +static ssize_t hyper_dmabuf_imported_show (struct device *drv, +struct device_attribute *attr, +char *buf) +{ +struct list_entry_imported *info_entry; +int bkt; +ssize_t count =0 ; +size_t total =0 ; + +hash_for_each (hyper_dmabuf_hash_imported, bkt, info_entry, node) { +hyper_dmabuf_id_t hid = info_entry->imported->hid; +int nents = info_entry->imported->nents; +bool valid = info_entry->imported->valid; +int num_importers = info_entry->imported->importers; + + total += nents; + count +=scnprintf (buf + count,PAGE_SIZE - count, +"hid:{%d %d %d %d}, nent:%d, v:%c, numi:%d\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ], nents, (valid ?'t' :'f' ), + num_importers); + } + count +=scnprintf (buf + count,PAGE_SIZE - count, +"total nents: %lu\n" , total); + +return count; +} + +static ssize_t hyper_dmabuf_exported_show (struct device *drv, +struct device_attribute *attr, +char *buf) +{ +struct list_entry_exported *info_entry; +int bkt; +ssize_t count =0 ; +size_t total =0 ; + +hash_for_each (hyper_dmabuf_hash_exported, bkt, info_entry, node) { +hyper_dmabuf_id_t hid = info_entry->exported->hid; +int nents = info_entry->exported->nents; +bool valid = info_entry->exported->valid; +int importer_exported = info_entry->exported->active; + + total += nents; + count +=scnprintf (buf + count,PAGE_SIZE - count, +"hid:{%d %d %d %d}, nent:%d, v:%c, ie:%d\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ], nents, (valid ?'t' :'f' ), + importer_exported); + } + count +=scnprintf (buf + count,PAGE_SIZE - count, +"total nents: %lu\n" , total); + +return count; +} + +static DEVICE_ATTR(imported, 0400, hyper_dmabuf_imported_show, NULL); +static DEVICE_ATTR(exported, 0400, hyper_dmabuf_exported_show, NULL); + +int hyper_dmabuf_register_sysfs (struct device *dev) +{ +int err; + + err =device_create_file (dev, &dev_attr_imported); +if (err <0 ) + goto err1; + err =device_create_file (dev, &dev_attr_exported); +if (err <0 ) + goto err2; + +return 0 ; +err2: +device_remove_file (dev, &dev_attr_imported); +err1: +return -1 ; +} + +int hyper_dmabuf_unregister_sysfs (struct device *dev) +{ +device_remove_file (dev, &dev_attr_imported); +device_remove_file (dev, &dev_attr_exported); +return 0 ; +} + +#endif + +int hyper_dmabuf_table_init (void ) +{ +hash_init (hyper_dmabuf_hash_imported); +hash_init (hyper_dmabuf_hash_exported); +return 0 ; +} + +int hyper_dmabuf_table_destroy (void ) +{ +/* TODO: cleanup hyper_dmabuf_hash_imported +* and hyper_dmabuf_hash_exported +*/ +return 0 ; +} + +int hyper_dmabuf_register_exported (struct exported_sgt_info *exported) +{ +struct list_entry_exported *info_entry; + + info_entry =kmalloc (sizeof (*info_entry),GFP_KERNEL ); + +if (!info_entry) +return -ENOMEM ; + + info_entry->exported = exported; + +hash_add (hyper_dmabuf_hash_exported, &info_entry->node, + info_entry->exported->hid.id); + +return 0 ; +} + +int hyper_dmabuf_register_imported (struct imported_sgt_info *imported) +{ +struct list_entry_imported *info_entry; + + info_entry =kmalloc (sizeof (*info_entry),GFP_KERNEL ); + +if (!info_entry) +return -ENOMEM ; + + info_entry->imported = imported; + +hash_add (hyper_dmabuf_hash_imported, &info_entry->node, + info_entry->imported->hid.id); + +return 0 ; +} + +struct exported_sgt_info *hyper_dmabuf_find_exported (hyper_dmabuf_id_t hid) +{ +struct list_entry_exported *info_entry; +int bkt; + +hash_for_each (hyper_dmabuf_hash_exported, bkt, info_entry, node) +/* checking hid.id first */ +if (info_entry->exported->hid.id == hid.id) { +/* then key is compared */ +if (hyper_dmabuf_hid_keycomp (info_entry->exported->hid, + hid)) +return info_entry->exported; + +/* if key is unmatched, given HID is invalid, +* so returning NULL +*/ +break ; + } + +return NULL ; +} + +/* search for pre-exported sgt and return id of it if it exist */ +hyper_dmabuf_id_t hyper_dmabuf_find_hid_exported (struct dma_buf *dmabuf, +int domid) +{ +struct list_entry_exported *info_entry; +hyper_dmabuf_id_t hid = {-1 , {0 ,0 ,0 } }; +int bkt; + +hash_for_each (hyper_dmabuf_hash_exported, bkt, info_entry, node) +if (info_entry->exported->dma_buf == dmabuf && + info_entry->exported->rdomid == domid) +return info_entry->exported->hid; + +return hid; +} + +struct imported_sgt_info *hyper_dmabuf_find_imported (hyper_dmabuf_id_t hid) +{ +struct list_entry_imported *info_entry; +int bkt; + +hash_for_each (hyper_dmabuf_hash_imported, bkt, info_entry, node) +/* checking hid.id first */ +if (info_entry->imported->hid.id == hid.id) { +/* then key is compared */ +if (hyper_dmabuf_hid_keycomp (info_entry->imported->hid, + hid)) +return info_entry->imported; +/* if key is unmatched, given HID is invalid, +* so returning NULL +*/ +break ; + } + +return NULL ; +} + +int hyper_dmabuf_remove_exported (hyper_dmabuf_id_t hid) +{ +struct list_entry_exported *info_entry; +int bkt; + +hash_for_each (hyper_dmabuf_hash_exported, bkt, info_entry, node) +/* checking hid.id first */ +if (info_entry->exported->hid.id == hid.id) { +/* then key is compared */ +if (hyper_dmabuf_hid_keycomp (info_entry->exported->hid, + hid)) { +hash_del (&info_entry->node); +kfree (info_entry); +return 0 ; + } + +break ; + } + +return -ENOENT ; +} + +int hyper_dmabuf_remove_imported (hyper_dmabuf_id_t hid) +{ +struct list_entry_imported *info_entry; +int bkt; + +hash_for_each (hyper_dmabuf_hash_imported, bkt, info_entry, node) +/* checking hid.id first */ +if (info_entry->imported->hid.id == hid.id) { +/* then key is compared */ +if (hyper_dmabuf_hid_keycomp (info_entry->imported->hid, + hid)) { +hash_del (&info_entry->node); +kfree (info_entry); +return 0 ; + } + +break ; + } + +return -ENOENT ; +} + +void hyper_dmabuf_foreach_exported ( +void (*func)(struct exported_sgt_info *,void *attr), +void *attr) +{ +struct list_entry_exported *info_entry; +struct hlist_node *tmp; +int bkt; + +hash_for_each_safe (hyper_dmabuf_hash_exported, bkt, tmp, + info_entry, node) { +func (info_entry->exported, attr); + } +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h new file +73
-0@@ -0,0 +1,73 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_LIST_H__ +#define __HYPER_DMABUF_LIST_H__ + +#include "hyper_dmabuf_struct.h" + +/* number of bits to be used for exported dmabufs hash table */ +#define MAX_ENTRY_EXPORTED 7 +/* number of bits to be used for imported dmabufs hash table */ +#define MAX_ENTRY_IMPORTED 7 + +struct list_entry_exported { +struct exported_sgt_info *exported; +struct hlist_node node; +}; + +struct list_entry_imported { +struct imported_sgt_info *imported; +struct hlist_node node; +}; + +int hyper_dmabuf_table_init (void ); + +int hyper_dmabuf_table_destroy (void ); + +int hyper_dmabuf_register_exported (struct exported_sgt_info *info); + +/* search for pre-exported sgt and return id of it if it exist */ +hyper_dmabuf_id_t hyper_dmabuf_find_hid_exported (struct dma_buf *dmabuf, +int domid); + +int hyper_dmabuf_register_imported (struct imported_sgt_info *info); + +struct exported_sgt_info *hyper_dmabuf_find_exported (hyper_dmabuf_id_t hid); + +struct imported_sgt_info *hyper_dmabuf_find_imported (hyper_dmabuf_id_t hid); + +int hyper_dmabuf_remove_exported (hyper_dmabuf_id_t hid); + +int hyper_dmabuf_remove_imported (hyper_dmabuf_id_t hid); + +void hyper_dmabuf_foreach_exported (void (*func)(struct exported_sgt_info *, +void *attr),void *attr); + +int hyper_dmabuf_register_sysfs (struct device *dev); +int hyper_dmabuf_unregister_sysfs (struct device *dev); + +#endif /* __HYPER_DMABUF_LIST_H__ */ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +36
-0@@ -0,0 +1,320 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/workqueue.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_msg.h"
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c line 46
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +10
-0+#include "hyper_dmabuf_list.h" + +struct cmd_process { +struct work_struct work; +struct hyper_dmabuf_req *rq; +int domid; +}; + +void hyper_dmabuf_create_req (struct hyper_dmabuf_req *req, +enum hyper_dmabuf_command cmd,int *op)
can we have structures for all the types of requests/responses defined in some protocol header file? so we avoid hardcoding?
Quoted 108 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +108
-0+{ +int i; + + req->stat =HYPER_DMABUF_REQ_NOT_RESPONDED ; + req->cmd = cmd; + +switch (cmd) { +/* as exporter, commands to importer */ +case HYPER_DMABUF_EXPORT : +/* exporting pages for dmabuf */ +/* command : HYPER_DMABUF_EXPORT, +* op0~op3 : hyper_dmabuf_id +* op4 : number of pages to be shared +* op5 : offset of data in the first page +* op6 : length of data in the last page +* op7 : top-level reference number for shared pages +*/ + +memcpy (&req->op[0 ], &op[0 ],8 *sizeof (int ) + op[8 ]); +break ; + +case HYPER_DMABUF_NOTIFY_UNEXPORT : +/* destroy sg_list for hyper_dmabuf_id on remote side */ +/* command : DMABUF_DESTROY, +* op0~op3 : hyper_dmabuf_id_t hid +*/ + +for (i =0 ; i <4 ; i++) + req->op[i] = op[i]; +break ; + +case HYPER_DMABUF_EXPORT_FD : +case HYPER_DMABUF_EXPORT_FD_FAILED : +/* dmabuf fd is being created on imported side or importing +* failed +* +* command : HYPER_DMABUF_EXPORT_FD or +* HYPER_DMABUF_EXPORT_FD_FAILED, +* op0~op3 : hyper_dmabuf_id +*/ + +for (i =0 ; i <4 ; i++) + req->op[i] = op[i]; +break ; + +default : +/* no command found */ +return ; + } +} + +static void cmd_process_work (struct work_struct *work) +{ +struct imported_sgt_info *imported; +struct cmd_process *proc =container_of (work, + struct cmd_process, work); +struct hyper_dmabuf_req *req; +int domid; +int i; + + req = proc->rq; + domid = proc->domid; + +switch (req->cmd) { +case HYPER_DMABUF_EXPORT : +/* exporting pages for dmabuf */ +/* command : HYPER_DMABUF_EXPORT, +* op0~op3 : hyper_dmabuf_id +* op4 : number of pages to be shared +* op5 : offset of data in the first page +* op6 : length of data in the last page +* op7 : top-level reference number for shared pages +*/ + +/* if nents == 0, it means it is a message only for +* priv synchronization. for existing imported_sgt_info +* so not creating a new one +*/ +if (req->op[4 ] ==0 ) { +hyper_dmabuf_id_t exist = {req->op[0 ], + {req->op[1 ], req->op[2 ], + req->op[3 ] } }; + + imported =hyper_dmabuf_find_imported (exist); + +if (!imported) { +dev_err (hy_drv_priv->dev, +"Can't find imported sgt_info\n" ); +break ; + } + +break ; + } + + imported =kcalloc (1 ,sizeof (*imported),GFP_KERNEL ); + +if (!imported) +break ; + + imported->hid.id = req->op[0 ]; + +for (i =0 ; i <3 ; i++) + imported->hid.rng_key[i] = req->op[i+1 ]; + + imported->nents = req->op[4 ]; + imported->frst_ofst = req->op[5 ]; + imported->last_len = req->op[6 ]; + imported->ref_handle = req->op[7 ];
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c line 164
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +10
-0+ +dev_dbg (hy_drv_priv->dev,"DMABUF was exported\n" ); +dev_dbg (hy_drv_priv->dev,"\thid{id:%d key:%d %d %d}\n" , + req->op[0 ], req->op[1 ], req->op[2 ], + req->op[3 ]); +dev_dbg (hy_drv_priv->dev,"\tnents %d\n" , req->op[4 ]); +dev_dbg (hy_drv_priv->dev,"\tfirst offset %d\n" , req->op[5 ]); +dev_dbg (hy_drv_priv->dev,"\tlast len %d\n" , req->op[6 ]); +dev_dbg (hy_drv_priv->dev,"\tgrefid %d\n" , req->op[7 ]); +
heh, and what if you have to insert something at index 1, for example? you'll end up changing all the hardcodes...
Please have the protocol and its constants, structures etc. defined somewhere
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +5
-0+ imported->valid = true; +hyper_dmabuf_register_imported (imported); + +break ; +
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c line 179
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +10
-0+default : +/* shouldn't get here */ +break ; + } + +kfree (req); +kfree (proc); +} + +int hyper_dmabuf_msg_parse (int domid,struct hyper_dmabuf_req *req)
This seems to be a hyper_dmabuf_msg_*handle* rather than parse...
Quoted 126 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +126
-0+{ +struct cmd_process *proc; +struct hyper_dmabuf_req *temp_req; +struct imported_sgt_info *imported; +struct exported_sgt_info *exported; +hyper_dmabuf_id_t hid; + +if (!req) { +dev_err (hy_drv_priv->dev,"request is NULL\n" ); +return -EINVAL ; + } + + hid.id = req->op[0 ]; + hid.rng_key[0 ] = req->op[1 ]; + hid.rng_key[1 ] = req->op[2 ]; + hid.rng_key[2 ] = req->op[3 ]; + +if ((req->cmd <HYPER_DMABUF_EXPORT ) || + (req->cmd >HYPER_DMABUF_NOTIFY_UNEXPORT )) { +dev_err (hy_drv_priv->dev,"invalid command\n" ); +return -EINVAL ; + } + + req->stat =HYPER_DMABUF_REQ_PROCESSED ; + +/* HYPER_DMABUF_DESTROY requires immediate +* follow up so can't be processed in workqueue +*/ +if (req->cmd ==HYPER_DMABUF_NOTIFY_UNEXPORT ) { +/* destroy sg_list for hyper_dmabuf_id on remote side */ +/* command : HYPER_DMABUF_NOTIFY_UNEXPORT, +* op0~3 : hyper_dmabuf_id +*/ +dev_dbg (hy_drv_priv->dev, +"processing HYPER_DMABUF_NOTIFY_UNEXPORT\n" ); + + imported =hyper_dmabuf_find_imported (hid); + +if (imported) { +/* if anything is still using dma_buf */ +if (imported->importers) { +/* Buffer is still in use, just mark that +* it should not be allowed to export its fd +* anymore. +*/ + imported->valid = false; + }else { +/* No one is using buffer, remove it from +* imported list +*/ +hyper_dmabuf_remove_imported (hid); +kfree (imported); + } + }else { + req->stat =HYPER_DMABUF_REQ_ERROR ; + } + +return req->cmd; + } + +/* synchronous dma_buf_fd export */ +if (req->cmd ==HYPER_DMABUF_EXPORT_FD ) { +/* find a corresponding SGT for the id */ +dev_dbg (hy_drv_priv->dev, +"HYPER_DMABUF_EXPORT_FD for {id:%d key:%d %d %d}\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], hid.rng_key[2 ]); + + exported =hyper_dmabuf_find_exported (hid); + +if (!exported) { +dev_err (hy_drv_priv->dev, +"buffer {id:%d key:%d %d %d} not found\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ]); + + req->stat =HYPER_DMABUF_REQ_ERROR ; + }else if (!exported->valid) { +dev_dbg (hy_drv_priv->dev, +"Buffer no longer valid {id:%d key:%d %d %d}\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ]); + + req->stat =HYPER_DMABUF_REQ_ERROR ; + }else { +dev_dbg (hy_drv_priv->dev, +"Buffer still valid {id:%d key:%d %d %d}\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ]); + + exported->active++; + req->stat =HYPER_DMABUF_REQ_PROCESSED ; + } +return req->cmd; + } + +if (req->cmd ==HYPER_DMABUF_EXPORT_FD_FAILED ) { +dev_dbg (hy_drv_priv->dev, +"HYPER_DMABUF_EXPORT_FD_FAILED for {id:%d key:%d %d %d}\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], hid.rng_key[2 ]); + + exported =hyper_dmabuf_find_exported (hid); + +if (!exported) { +dev_err (hy_drv_priv->dev, +"buffer {id:%d key:%d %d %d} not found\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ]); + + req->stat =HYPER_DMABUF_REQ_ERROR ; + }else { + exported->active--; + req->stat =HYPER_DMABUF_REQ_PROCESSED ; + } +return req->cmd; + } + +dev_dbg (hy_drv_priv->dev, +"%s: putting request to workqueue\n" , __func__); + temp_req =kmalloc (sizeof (*temp_req),GFP_KERNEL ); + +if (!temp_req) +return -ENOMEM ; + +memcpy (temp_req, req,sizeof (*temp_req)); + + proc =kcalloc (1 ,sizeof (struct cmd_process ),GFP_KERNEL );
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c line 315
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +10
-0+ +if (!proc) { +kfree (temp_req); +return -ENOMEM ; + } + + proc->rq = temp_req; + proc->domid = domid; + +INIT_WORK (&(proc->work), cmd_process_work);
Why do you need to be so asynchronous and schedule a work for processing rather than handle it now?
Quoted 46 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +5
-0+ +queue_work (hy_drv_priv->work_queue, &(proc->work)); + +return req->cmd; +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +35
-0@@ -0,0 +1,87 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_MSG_H__ +#define __HYPER_DMABUF_MSG_H__ + +#define MAX_NUMBER_OF_OPERANDS 8 + +struct hyper_dmabuf_req { + unsigned int req_id; + unsigned int stat; + unsigned int cmd;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h line 45
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +10
-0+unsigned int op[MAX_NUMBER_OF_OPERANDS ]; +}; + +struct hyper_dmabuf_resp { +unsigned int resp_id; +unsigned int stat; +unsigned int cmd; +unsigned int op[MAX_NUMBER_OF_OPERANDS ]; +}; +
The structures above are of size of 11 * sizeof(int) == 44 bytes Can these be aligned to 64 for example,
From Xen POV: these will be sent over the shared ring, which is of PAGE_SIZE size, so 4096 / 44...
Quoted 15 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +15
-0+enum hyper_dmabuf_command { +HYPER_DMABUF_EXPORT =0x10 , +HYPER_DMABUF_EXPORT_FD , +HYPER_DMABUF_EXPORT_FD_FAILED , +HYPER_DMABUF_NOTIFY_UNEXPORT , +}; + +enum hyper_dmabuf_ops { +HYPER_DMABUF_OPS_ATTACH =0x1000 , +HYPER_DMABUF_OPS_DETACH , +HYPER_DMABUF_OPS_MAP , +HYPER_DMABUF_OPS_UNMAP , +HYPER_DMABUF_OPS_RELEASE , +HYPER_DMABUF_OPS_BEGIN_CPU_ACCESS , +HYPER_DMABUF_OPS_END_CPU_ACCESS ,
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h line 70
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +10
-0+HYPER_DMABUF_OPS_KMAP_ATOMIC , +HYPER_DMABUF_OPS_KUNMAP_ATOMIC , +HYPER_DMABUF_OPS_KMAP , +HYPER_DMABUF_OPS_KUNMAP , +HYPER_DMABUF_OPS_MMAP , +HYPER_DMABUF_OPS_VMAP , + HYPER_DMABUF_OPS_VUNMAP, +}; + +enum hyper_dmabuf_req_feedback {
This rather seems to be a status
Quoted 272 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +17
-0+HYPER_DMABUF_REQ_PROCESSED =0x100 , +HYPER_DMABUF_REQ_NEEDS_FOLLOW_UP , +HYPER_DMABUF_REQ_ERROR , +HYPER_DMABUF_REQ_NOT_RESPONDED +}; + +/* create a request packet with given command and operands */ +void hyper_dmabuf_create_req (struct hyper_dmabuf_req *req, +enum hyper_dmabuf_command command, +int *operands); + +/* parse incoming request packet (or response) and take +* appropriate actions for those +*/ +int hyper_dmabuf_msg_parse (int domid,struct hyper_dmabuf_req *req); + +#endif // __HYPER_DMABUF_MSG_H__ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c new file +249
-0@@ -0,0 +1,264 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/dma-buf.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_struct.h" +#include "hyper_dmabuf_ops.h" +#include "hyper_dmabuf_sgl_proc.h" +#include "hyper_dmabuf_id.h" +#include "hyper_dmabuf_msg.h" +#include "hyper_dmabuf_list.h" + +#define WAIT_AFTER_SYNC_REQ 0 +#define REFS_PER_PAGE (PAGE_SIZE/sizeof(grant_ref_t)) + +static int dmabuf_refcount (struct dma_buf *dma_buf) +{ +if ((dma_buf !=NULL ) && (dma_buf->file !=NULL )) +return file_count (dma_buf->file); + +return -EINVAL ; +} + +static int hyper_dmabuf_ops_attach (struct dma_buf *dmabuf, +struct device *dev, +struct dma_buf_attachment *attach) +{ +return 0 ; +} + +static void hyper_dmabuf_ops_detach (struct dma_buf *dmabuf, +struct dma_buf_attachment *attach) +{ +} + +static struct sg_table *hyper_dmabuf_ops_map ( +struct dma_buf_attachment *attachment, +enum dma_data_direction dir) +{ +struct sg_table *st; +struct imported_sgt_info *imported; +struct pages_info *pg_info; + +if (!attachment->dmabuf->priv) +return NULL ; + + imported = (struct imported_sgt_info *)attachment->dmabuf->priv; + +/* extract pages from sgt */ + pg_info =hyper_dmabuf_ext_pgs (imported->sgt); + +if (!pg_info) +return NULL ; + +/* create a new sg_table with extracted pages */ + st =hyper_dmabuf_create_sgt (pg_info->pgs, pg_info->frst_ofst, + pg_info->last_len, pg_info->nents); +if (!st) + goto err_free_sg; + +if (!dma_map_sg (attachment->dev, st->sgl, st->nents, dir)) + goto err_free_sg; + +kfree (pg_info->pgs); +kfree (pg_info); + +return st; + +err_free_sg: +if (st) { +sg_free_table (st); +kfree (st); + } + +kfree (pg_info->pgs); +kfree (pg_info); + +return NULL ; +} + +static void hyper_dmabuf_ops_unmap (struct dma_buf_attachment *attachment, +struct sg_table *sg, +enum dma_data_direction dir) +{ +struct imported_sgt_info *imported; + +if (!attachment->dmabuf->priv) +return ; + + imported = (struct imported_sgt_info *)attachment->dmabuf->priv; + +dma_unmap_sg (attachment->dev, sg->sgl, sg->nents, dir); + +sg_free_table (sg); +kfree (sg); +} + +static void hyper_dmabuf_ops_release (struct dma_buf *dma_buf) +{ +struct imported_sgt_info *imported; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +int finish; + +if (!dma_buf->priv) +return ; + + imported = (struct imported_sgt_info *)dma_buf->priv; + +if (!dmabuf_refcount (imported->dma_buf)) + imported->dma_buf =NULL ; + + imported->importers--; + +if (imported->importers ==0 ) { + bknd_ops->unmap_shared_pages (&imported->refs_info, + imported->nents); + +if (imported->sgt) { +sg_free_table (imported->sgt); +kfree (imported->sgt); + imported->sgt =NULL ; + } + } + + finish = imported && !imported->valid && + !imported->importers; + +/* +* Check if buffer is still valid and if not remove it +* from imported list. That has to be done after sending +* sync request +*/ +if (finish) { +hyper_dmabuf_remove_imported (imported->hid); +kfree (imported); + } +} + +static int hyper_dmabuf_ops_begin_cpu_access (struct dma_buf *dmabuf, +enum dma_data_direction dir) +{ +return 0 ; +} + +static int hyper_dmabuf_ops_end_cpu_access (struct dma_buf *dmabuf, +enum dma_data_direction dir) +{ +return 0 ; +} + +static void *hyper_dmabuf_ops_kmap_atomic (struct dma_buf *dmabuf, +unsigned long pgnum) +{ +/* TODO: NULL for now. Need to return the addr of mapped region */ +return NULL ; +} + +static void hyper_dmabuf_ops_kunmap_atomic (struct dma_buf *dmabuf, +unsigned long pgnum,void *vaddr) +{ +} + +static void *hyper_dmabuf_ops_kmap (struct dma_buf *dmabuf,unsigned long pgnum) +{ +/* for now NULL.. need to return the address of mapped region */ +return NULL ; +} + +static void hyper_dmabuf_ops_kunmap (struct dma_buf *dmabuf,unsigned long pgnum, +void *vaddr) +{ +} + +static int hyper_dmabuf_ops_mmap (struct dma_buf *dmabuf, +struct vm_area_struct *vma) +{ +return 0 ; +} + +static void *hyper_dmabuf_ops_vmap (struct dma_buf *dmabuf) +{ +return NULL ; +} + +static void hyper_dmabuf_ops_vunmap (struct dma_buf *dmabuf,void *vaddr) +{ +} + +static const struct dma_buf_ops hyper_dmabuf_ops = { + .attach = hyper_dmabuf_ops_attach, + .detach = hyper_dmabuf_ops_detach, + .map_dma_buf = hyper_dmabuf_ops_map, + .unmap_dma_buf = hyper_dmabuf_ops_unmap, + .release = hyper_dmabuf_ops_release, + .begin_cpu_access = (void *)hyper_dmabuf_ops_begin_cpu_access, + .end_cpu_access = (void *)hyper_dmabuf_ops_end_cpu_access, + .map_atomic = hyper_dmabuf_ops_kmap_atomic, + .unmap_atomic = hyper_dmabuf_ops_kunmap_atomic, + .map = hyper_dmabuf_ops_kmap, + .unmap = hyper_dmabuf_ops_kunmap, + .mmap = hyper_dmabuf_ops_mmap, + .vmap = hyper_dmabuf_ops_vmap, + .vunmap = hyper_dmabuf_ops_vunmap, +}; + +/* exporting dmabuf as fd */ +int hyper_dmabuf_export_fd (struct imported_sgt_info *imported,int flags) +{ +int fd =-1 ; + +/* call hyper_dmabuf_export_dmabuf and create +* and bind a handle for it then release +*/ +hyper_dmabuf_export_dma_buf (imported); + +if (imported->dma_buf) + fd =dma_buf_fd (imported->dma_buf, flags); + +return fd;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c line 259
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c new file +10
-0+} + +void hyper_dmabuf_export_dma_buf (struct imported_sgt_info *imported) +{ +DEFINE_DMA_BUF_EXPORT_INFO (exp_info); + + exp_info.ops = &hyper_dmabuf_ops; + +/* multiple of PAGE_SIZE, not considering offset */ + exp_info.size = imported->sgt->nents *PAGE_SIZE ;
Here and below: it can be that PAGE_SIZE differs across VMs
Quoted 97 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c new file +5
-0+ exp_info.flags =/* not sure about flag */ 0 ; + exp_info.priv = imported; + + imported->dma_buf =dma_buf_export (&exp_info); +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h new file +34
-0@@ -0,0 +1,34 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_OPS_H__ +#define __HYPER_DMABUF_OPS_H__ + +int hyper_dmabuf_export_fd (struct imported_sgt_info *imported,int flags); + +void hyper_dmabuf_export_dma_buf (struct imported_sgt_info *imported); + +#endif /* __HYPER_DMABUF_IMP_H__ */ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +46
-0@@ -0,0 +1,256 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/dma-buf.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_struct.h" +#include "hyper_dmabuf_sgl_proc.h" + +#define REFS_PER_PAGE (PAGE_SIZE/sizeof(grant_ref_t)) + +/* return total number of pages referenced by a sgt +* for pre-calculation of # of pages behind a given sgt +*/ +static int get_num_pgs (struct sg_table *sgt) +{ +struct scatterlist *sgl;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c line 56
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +10
-0+int length, i; +/* at least one page */ +int num_pages =1 ; + + sgl = sgt->sgl; + + length = sgl->length -PAGE_SIZE + sgl->offset; + +/* round-up */ + num_pages += ((length +PAGE_SIZE -1 )/PAGE_SIZE );
DIV_ROUND_UP
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c line 63
Quoted 7 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +7
-0+ +for (i =1 ; i < sgt->nents; i++) { + sgl =sg_next (sgl); + +/* round-up */ + num_pages += ((sgl->length +PAGE_SIZE -1 ) / +PAGE_SIZE );/* round-up */
Ditto
Quoted 163 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +163
-0+ } + +return num_pages; +} + +/* extract pages directly from struct sg_table */ +struct pages_info *hyper_dmabuf_ext_pgs (struct sg_table *sgt) +{ +struct pages_info *pg_info; +int i, j, k; +int length; +struct scatterlist *sgl; + + pg_info =kmalloc (sizeof (*pg_info),GFP_KERNEL ); +if (!pg_info) +return NULL ; + + pg_info->pgs =kmalloc_array (get_num_pgs (sgt), +sizeof (struct page *), +GFP_KERNEL ); + +if (!pg_info->pgs) { +kfree (pg_info); +return NULL ; + } + + sgl = sgt->sgl; + + pg_info->nents =1 ; + pg_info->frst_ofst = sgl->offset; + pg_info->pgs[0 ] =sg_page (sgl); + length = sgl->length -PAGE_SIZE + sgl->offset; + i =1 ; + +while (length >0 ) { + pg_info->pgs[i] =nth_page (sg_page (sgl), i); + length -=PAGE_SIZE ; + pg_info->nents++; + i++; + } + +for (j =1 ; j < sgt->nents; j++) { + sgl =sg_next (sgl); + pg_info->pgs[i++] =sg_page (sgl); + length = sgl->length -PAGE_SIZE ; + pg_info->nents++; + k =1 ; + +while (length >0 ) { + pg_info->pgs[i++] =nth_page (sg_page (sgl), k++); + length -=PAGE_SIZE ; + pg_info->nents++; + } + } + +/* +* lenght at that point will be 0 or negative, +* so to calculate last page size just add it to PAGE_SIZE +*/ + pg_info->last_len =PAGE_SIZE + length; + +return pg_info; +} + +/* create sg_table with given pages and other parameters */ +struct sg_table *hyper_dmabuf_create_sgt (struct page **pgs, +int frst_ofst,int last_len, +int nents) +{ +struct sg_table *sgt; +struct scatterlist *sgl; +int i, ret; + + sgt =kmalloc (sizeof (struct sg_table ),GFP_KERNEL ); +if (!sgt) +return NULL ; + + ret =sg_alloc_table (sgt, nents,GFP_KERNEL ); +if (ret) { +if (sgt) { +sg_free_table (sgt); +kfree (sgt); + } + +return NULL ; + } + + sgl = sgt->sgl; + +sg_set_page (sgl, pgs[0 ],PAGE_SIZE -frst_ofst, frst_ofst); + +for (i =1 ; i < nents-1 ; i++) { + sgl =sg_next (sgl); +sg_set_page (sgl, pgs[i],PAGE_SIZE ,0 ); + } + +if (nents >1 )/* more than one page */ { + sgl =sg_next (sgl); +sg_set_page (sgl, pgs[i], last_len,0 ); + } + +return sgt; +} + +int hyper_dmabuf_cleanup_sgt_info (struct exported_sgt_info *exported, +int force) +{ +struct sgt_list *sgtl; +struct attachment_list *attachl; +struct kmap_vaddr_list *va_kmapl; +struct vmap_vaddr_list *va_vmapl; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; + +if (!exported) { +dev_err (hy_drv_priv->dev,"invalid hyper_dmabuf_id\n" ); +return -EINVAL ; + } + +/* if force != 1, sgt_info can be released only if +* there's no activity on exported dma-buf on importer +* side. +*/ +if (!force && + exported->active) { +dev_warn (hy_drv_priv->dev, +"dma-buf is used by importer\n" ); + +return -EPERM ; + } + +/* force == 1 is not recommended */ +while (!list_empty (&exported->va_kmapped->list)) { + va_kmapl =list_first_entry (&exported->va_kmapped->list, + struct kmap_vaddr_list, list); + +dma_buf_kunmap (exported->dma_buf,1 , va_kmapl->vaddr); +list_del (&va_kmapl->list); +kfree (va_kmapl); + } + +while (!list_empty (&exported->va_vmapped->list)) { + va_vmapl =list_first_entry (&exported->va_vmapped->list, + struct vmap_vaddr_list, list); + +dma_buf_vunmap (exported->dma_buf, va_vmapl->vaddr); +list_del (&va_vmapl->list); +kfree (va_vmapl); + } + +while (!list_empty (&exported->active_sgts->list)) { + attachl =list_first_entry (&exported->active_attached->list, + struct attachment_list, list); + + sgtl =list_first_entry (&exported->active_sgts->list, + struct sgt_list, list); + +dma_buf_unmap_attachment (attachl->attach, sgtl->sgt, +DMA_BIDIRECTIONAL ); +list_del (&sgtl->list); +kfree (sgtl); + } + +while (!list_empty (&exported->active_sgts->list)) {
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c line 236
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +10
-0+ attachl =list_first_entry (&exported->active_attached->list, + struct attachment_list, list); + +dma_buf_detach (exported->dma_buf, attachl->attach); +list_del (&attachl->list); +kfree (attachl); + } + +/* Start cleanup of buffer in reverse order to exporting */ + bknd_ops->unshare_pages (&exported->refs_info, exported->nents);
is the above synchronous? can it be delayed?
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c line 241
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +5
-0+ +/* unmap dma-buf */ +dma_buf_unmap_attachment (exported->active_attached->attach, + exported->active_sgts->sgt, +DMA_BIDIRECTIONAL );
if the above is asynchronous then this might make troubles as we are unmapping yet shared pages
Quoted 226 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +15
-0+ +/* detatch dma-buf */ +dma_buf_detach (exported->dma_buf, exported->active_attached->attach); + +/* close connection to dma-buf completely */ +dma_buf_put (exported->dma_buf); + exported->dma_buf =NULL ; + +kfree (exported->active_sgts); +kfree (exported->active_attached); +kfree (exported->va_kmapped); +kfree (exported->va_vmapped); + +return 0 ; +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h new file +43
-0@@ -0,0 +1,43 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_IMP_H__ +#define __HYPER_DMABUF_IMP_H__ + +/* extract pages directly from struct sg_table */ +struct pages_info *hyper_dmabuf_ext_pgs (struct sg_table *sgt); + +/* create sg_table with given pages and other parameters */ +struct sg_table *hyper_dmabuf_create_sgt (struct page **pgs, +int frst_ofst,int last_len, +int nents); + +int hyper_dmabuf_cleanup_sgt_info (struct exported_sgt_info *exported, +int force); + +void hyper_dmabuf_free_sgt (struct sg_table *sgt); + +#endif /* __HYPER_DMABUF_IMP_H__ */ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h new file +131
-0@@ -0,0 +1,131 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_STRUCT_H__ +#define __HYPER_DMABUF_STRUCT_H__ + +/* stack of mapped sgts */ +struct sgt_list { +struct sg_table *sgt; +struct list_head list; +}; + +/* stack of attachments */ +struct attachment_list { +struct dma_buf_attachment *attach; +struct list_head list; +}; + +/* stack of vaddr mapped via kmap */ +struct kmap_vaddr_list { +void *vaddr; +struct list_head list; +}; + +/* stack of vaddr mapped via vmap */ +struct vmap_vaddr_list { +void *vaddr; +struct list_head list; +}; + +/* Exporter builds pages_info before sharing pages */ +struct pages_info { +int frst_ofst; +int last_len; +int nents; +struct page **pgs; +}; + + +/* Exporter stores references to sgt in a hash table +* Exporter keeps these references for synchronization +* and tracking purposes +*/ +struct exported_sgt_info { +hyper_dmabuf_id_t hid; + +/* VM ID of importer */ +int rdomid; + +struct dma_buf *dma_buf; +int nents; + +/* list for tracking activities on dma_buf */ +struct sgt_list *active_sgts; +struct attachment_list *active_attached; +struct kmap_vaddr_list *va_kmapped; +struct vmap_vaddr_list *va_vmapped; + +/* set to 0 when unexported. Importer doesn't +* do a new mapping of buffer if valid == false +*/ +bool valid; + +/* active == true if the buffer is actively used +* (mapped) by importer +*/ +int active; + +/* hypervisor specific reference data for shared pages */ +void *refs_info; + +struct delayed_work unexport; +bool unexport_sched; + +/* list for file pointers associated with all user space +* application that have exported this same buffer to +* another VM. This needs to be tracked to know whether +* the buffer can be completely freed. +*/ +struct file *filp; +}; + +/* imported_sgt_info contains information about imported DMA_BUF +* this info is kept in IMPORT list and asynchorously retrieved and +* used to map DMA_BUF on importer VM's side upon export fd ioctl +* request from user-space +*/ + +struct imported_sgt_info { +hyper_dmabuf_id_t hid;/* unique id for shared dmabuf imported */ + +/* hypervisor-specific handle to pages */ +int ref_handle; + +/* offset and size info of DMA_BUF */ +int frst_ofst; +int last_len; +int nents; + +struct dma_buf *dma_buf; +struct sg_table *sgt; + +void *refs_info; +bool valid; +int importers; +}; + +#endif /* __HYPER_DMABUF_STRUCT_H__ */ include/uapi/linux/hyper_dmabuf.h new file +19
-0@@ -0,0 +1,87 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
include/uapi/linux/hyper_dmabuf.h line 29
Quoted 10 lines
include/uapi/linux/hyper_dmabuf.h new file +10
-0+* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#ifndef __LINUX_PUBLIC_HYPER_DMABUF_H__ +#define __LINUX_PUBLIC_HYPER_DMABUF_H__ + +typedef struct { + int id;
can this be defined as a union as you seem to store count and vm_id in this field?
Quoted 59 lines
include/uapi/linux/hyper_dmabuf.h new file +58
-0+ int rng_key[3]; /* 12bytes long random number */ +} hyper_dmabuf_id_t; + +#define IOCTL_HYPER_DMABUF_TX_CH_SETUP \ +_IOC(_IOC_NONE, 'G', 0, sizeof(struct ioctl_hyper_dmabuf_tx_ch_setup)) +struct ioctl_hyper_dmabuf_tx_ch_setup { +/* IN parameters */ +/* Remote domain id */ +int remote_domain; +}; + +#define IOCTL_HYPER_DMABUF_RX_CH_SETUP \ +_IOC(_IOC_NONE, 'G', 1, sizeof(struct ioctl_hyper_dmabuf_rx_ch_setup)) +struct ioctl_hyper_dmabuf_rx_ch_setup { +/* IN parameters */ +/* Source domain id */ +int source_domain; +}; + +#define IOCTL_HYPER_DMABUF_EXPORT_REMOTE \ +_IOC(_IOC_NONE, 'G', 2, sizeof(struct ioctl_hyper_dmabuf_export_remote)) +struct ioctl_hyper_dmabuf_export_remote { +/* IN parameters */ +/* DMA buf fd to be exported */ +int dmabuf_fd; +/* Domain id to which buffer should be exported */ +int remote_domain; +/* exported dma buf id */ +hyper_dmabuf_id_t hid; +}; + +#define IOCTL_HYPER_DMABUF_EXPORT_FD \ +_IOC(_IOC_NONE, 'G', 3, sizeof(struct ioctl_hyper_dmabuf_export_fd)) +struct ioctl_hyper_dmabuf_export_fd { +/* IN parameters */ +/* hyper dmabuf id to be imported */ +hyper_dmabuf_id_t hid; +/* flags */ +int flags; +/* OUT parameters */ +/* exported dma buf fd */ +int fd; +}; + +#define IOCTL_HYPER_DMABUF_UNEXPORT \ +_IOC(_IOC_NONE, 'G', 4, sizeof(struct ioctl_hyper_dmabuf_unexport)) +struct ioctl_hyper_dmabuf_unexport { +/* IN parameters */ +/* hyper dmabuf id to be unexported */ +hyper_dmabuf_id_t hid; +/* delay in ms by which unexport processing will be postponed */ +int delay_ms; +/* OUT parameters */ +/* Status of request */ +int status; +}; + +#endif //__LINUX_PUBLIC_HYPER_DMABUF_H__
Re: [Xen-devel] [RFC, v2, 1/9] hyper_dmabuf: initial upload of hyper_dmabuf drv core framework
Hi,
Quoted 2 lines · Oleksandr Andrushchenko
Quoted 1 line · Dongwon Kim
(quoted diff) +0
-0
[...]
Quoted 7 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h line 31
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +5
-0+#ifndef __HYPER_DMABUF_ID_H__ +#define __HYPER_DMABUF_ID_H__ + +#define HYPER_DMABUF_ID_CREATE (domid, cnt) \ + ((((domid) & 0xFF) << 24) | ((cnt) & 0xFFFFFF))I would define hyper_dmabuf_id_t.id as a union or 2 separate fields to avoid his magic
I am not sure the union would be right here because the layout will differs between big and little endian. So does that value will be passed to other guest?
Cheers,
Signature · 1 line
Julien Grall
Re: [Xen-devel] [RFC, v2, 1/9] hyper_dmabuf: initial upload of hyper_dmabuf drv core framework
Quoted 16 lines · Julien Grall
Hi,
Quoted 2 lines · Oleksandr Andrushchenko
Quoted 1 line · Dongwon Kim
(quoted diff) +0
-0[...]
Quoted 7 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h line 31
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +5
-0+#ifndef __HYPER_DMABUF_ID_H__ +#define __HYPER_DMABUF_ID_H__ + +#define HYPER_DMABUF_ID_CREATE (domid, cnt) \ + ((((domid) & 0xFF) << 24) | ((cnt) & 0xFFFFFF))I would define hyper_dmabuf_id_t.id as a union or 2 separate fields to avoid his magic
Quoted 1 line
I am not sure the union would be right here because the layout will differs between big and little endian.
Agree
Quoted 1 line
So does that value will be passed to other guest?
As per my understanding yes, with HYPER_DMABUF_EXPORT request
Quoted 3 lines
Cheers,
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [RFC, v2, 1/9] hyper_dmabuf: initial upload of hyper_dmabuf drv core framework
In reply to Julien GrallQuoted 16 lines · Julien Grall
Hi,
Quoted 2 lines · Oleksandr Andrushchenko
Quoted 1 line · Dongwon Kim
(quoted diff) +0
-0[...]
Quoted 7 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h line 31
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +5
-0+#ifndef __HYPER_DMABUF_ID_H__ +#define __HYPER_DMABUF_ID_H__ + +#define HYPER_DMABUF_ID_CREATE (domid, cnt) \ + ((((domid) & 0xFF) << 24) | ((cnt) & 0xFFFFFF))I would define hyper_dmabuf_id_t.id as a union or 2 separate fields to avoid his magic
Quoted 1 line
I am not sure the union would be right here because the layout will differs between big and little endian.
Agree
Quoted 1 line
So does that value will be passed to other guest?
As per my understanding yes, with HYPER_DMABUF_EXPORT request
Quoted 3 lines
Cheers,
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [RFC, v2, 1/9] hyper_dmabuf: initial upload of hyper_dmabuf drv core framework
In reply to Oleksandr AndrushchenkoHi,
Quoted 2 lines · Oleksandr Andrushchenko
Quoted 1 line · Dongwon Kim
(quoted diff) +0
-0
[...]
Quoted 7 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h line 31
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +5
-0+#ifndef __HYPER_DMABUF_ID_H__ +#define __HYPER_DMABUF_ID_H__ + +#define HYPER_DMABUF_ID_CREATE (domid, cnt) \ + ((((domid) & 0xFF) << 24) | ((cnt) & 0xFFFFFF))I would define hyper_dmabuf_id_t.id as a union or 2 separate fields to avoid his magic
I am not sure the union would be right here because the layout will differs between big and little endian. So does that value will be passed to other guest?
Cheers,
Signature · 6 lines
Julien Grall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [RFC, v2, 1/9] hyper_dmabuf: initial upload of hyper_dmabuf drv core framework
In reply to Dongwon KimQuoted 139 lines · Dongwon Kim
Upload of intial version of core framework in
hyper_DMABUFdriver enablingDMA_BUFexchange between two different VMs in virtualized platform based on Hypervisor such as XEN.
Hyper_DMABUFdrv's primary role is to import aDMA_BUFfrom originator then re-export it to another Linux VM so that it can be mapped and accessed in there.This driver has two layers, one is so called, "core framework", which contains driver interface and core functions handling export/import of new
hyper_DMABUFand its maintenance. This part of the driver is independent from Hypervisor so can work as is with any Hypervisor.The other layer is called "Hypervisor Backend". This layer represents the interface between "core framework" and actual Hypervisor, handling memory sharing and communication. Not like "core framework", every Hypervisor needs it's own backend interface designed using its native mechanism for memory sharing and inter-VM communication.
This patch contains the first part, "core framework", which consists of 7 source files and 11 header files. Some brief description of these source code are attached below:
hyper_dmabuf_drv.c- Linux driver interface and initialization/cleaning-up routines
hyper_dmabuf_ioctl.c- IOCTLs calls for export/import of DMA-BUF comm channel's creation and destruction.
hyper_dmabuf_sgl_proc.c- Provides methods to managing DMA-BUF for exporing and importing. For exporting, extraction of pages, sharing pages via procedures in "Backend" and notifying importing VM exist. For importing, all operations related to the reconstruction of DMA-BUF (with shared pages) on importer's side are defined.
hyper_dmabuf_ops.c- Standard DMA-BUF operations for
hyper_DMABUFreconstructed on importer's side.
hyper_dmabuf_list.c- Lists for storing exported and imported
hyper_DMABUFto keep track of remote usage ofhyper_DMABUFcurrently being shared.
hyper_dmabuf_msg.c- Defines messages exchanged between VMs (exporter and importer) and function calls for sending and parsing (when received) those.
hyper_dmabuf_id.c- Contains methods to generate and manage "
hyper_DMABUFid" for eachhyper_DMABUFbeing exported. It is a global handle for ahyper_DMABUF, which another VM needs to know to import it.
hyper_dmabuf_struct.h- Contains data structures of importer or exporter
hyper_DMABUF
include/uapi/linux/hyper_dmabuf.h- Contains definition of data types and structures referenced by user application to interact with driver
Signed-off-by Dongwon Kim <[email protected]>Signed-off-by Mateusz Polrola <[email protected]>--- drivers/dma-buf/Kconfig | 2 + drivers/dma-buf/Makefile | 1 + drivers/dma-buf/hyper_dmabuf/Kconfig | 23 + drivers/dma-buf/hyper_dmabuf/Makefile | 34 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c | 254 ++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h | 111 ++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c | 135 +++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h | 53 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c | 672 +++++++++++++++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h | 52 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c | 294 +++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h | 73 +++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c | 320 ++++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h | 87 +++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c | 264 ++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h | 34 ++ .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c | 256 ++++++++ .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h | 43 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h | 131 ++++ include/uapi/linux/hyper_dmabuf.h | 87 +++ 20 files changed, 2926 insertions(+)create mode 100644
drivers/dma-buf/hyper_dmabuf/Kconfigcreate mode 100644drivers/dma-buf/hyper_dmabuf/Makefilecreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.hcreate mode 100644include/uapi/linux/hyper_dmabuf.hdrivers/dma-buf/Kconfig +2
-0@@ -30,4 +30,6 @@ config SW_SYNC WARNING: improper use of this can result in deadlocking kernel drivers from userspace. Intended for test and debug only. +source "drivers/dma-buf/hyper_dmabuf/Kconfig" + endmenudrivers/dma-buf/Makefile +1
-0@@ -1,3 +1,4 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o reservation.o seqno-fence.o obj-$(CONFIG_SYNC_FILE) += sync_file.o obj-$(CONFIG_SW_SYNC) += sw_sync.o sync_debug.o +obj-$(CONFIG_HYPER_DMABUF) += ./hyper_dmabuf/drivers/dma-buf/hyper_dmabuf/Kconfig new file +0
-0
drivers/dma-buf/hyper_dmabuf/Kconfig line 5
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/Kconfig new file +5
-0@@ -0,0 +1,23 @@ +menu "HYPER_DMABUF" + +config HYPER_DMABUF + tristate "Enables hyper dmabuf driver" + default y
Not sure you want this enabled by default
drivers/dma-buf/hyper_dmabuf/Kconfig line 15
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/Kconfig new file +10
-0+ help + This option enables Hyper_DMABUF driver. + + This driver works as abstraction layer that export and import + DMA_BUF from/to another virtual OS running on the same HW platform + powered by a hypervisor + +config HYPER_DMABUF_SYSFS + bool "Enable sysfs information about hyper DMA buffers" + default y
Ditto
Quoted 14 lines
drivers/dma-buf/hyper_dmabuf/Kconfig new file +8
-0+ depends on HYPER_DMABUF + help + Expose run-time information about currently imported and exported buffers + registered in EXPORT and IMPORT list in Hyper_DMABUF driver. + + The location of sysfs is under "...." + +endmenudrivers/dma-buf/hyper_dmabuf/Makefile new file +0
-0
drivers/dma-buf/hyper_dmabuf/Makefile line 4
Quoted 4 lines
drivers/dma-buf/hyper_dmabuf/Makefile new file +4
-0@@ -0,0 +1,34 @@ +TARGET_MODULE:=hyper_dmabuf + +# If we running by kernel building system +ifneq ($(KERNELRELEASE),)
Not sure why you need this
drivers/dma-buf/hyper_dmabuf/Makefile line 15
Quoted 11 lines
drivers/dma-buf/hyper_dmabuf/Makefile new file +11
-0+ $(TARGET_MODULE)-objs := hyper_dmabuf_drv.o \ + hyper_dmabuf_ioctl.o \ + hyper_dmabuf_list.o \ + hyper_dmabuf_sgl_proc.o \ + hyper_dmabuf_ops.o \ + hyper_dmabuf_msg.o \ + hyper_dmabuf_id.o \ + +obj-$(CONFIG_HYPER_DMABUF) := $(TARGET_MODULE).o + +# If we are running without kernel build system
Ditto
Quoted 8 lines
drivers/dma-buf/hyper_dmabuf/Makefile new file +8
-0+else +BUILDSYSTEM_DIR?=../../../ +PWD:=$(shell pwd) + +all : +# run kernel build system to make module +$(MAKE) -C $(BUILDSYSTEM_DIR) M=$(PWD) modules +
drivers/dma-buf/hyper_dmabuf/Makefile line 33
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/Makefile new file +10
-0+clean: +# run kernel build system to cleanup in current directory +$(MAKE) -C $(BUILDSYSTEM_DIR) M=$(PWD) clean + +load: + insmod ./$(TARGET_MODULE).ko + +unload: + rmmod ./$(TARGET_MODULE).ko +
This seems to be some helper code you use while doing development which needs to be removed
Quoted 45 lines
drivers/dma-buf/hyper_dmabuf/Makefile new file +1
-0+endifdrivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +38
-0@@ -0,0 +1,254 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/init.h> +#include <linux/module.h> +#include <linux/miscdevice.h> +#include <linux/workqueue.h> +#include <linux/slab.h> +#include <linux/device.h> +#include <linux/uaccess.h> +#include <linux/poll.h>
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 48
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +10
-0+#include <linux/dma-buf.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_ioctl.h" +#include "hyper_dmabuf_list.h" +#include "hyper_dmabuf_id.h" + +MODULE_LICENSE ("GPL and additional rights" ); +MODULE_AUTHOR ("Intel Corporation" ); + +struct hyper_dmabuf_private *hy_drv_priv;
instead of using a global symbol here you might want to first allocate misc device and then use devm_kzalloc to allocate your private data
Quoted 41 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +41
-0+ +static void force_free (struct exported_sgt_info *exported, +void *attr) +{ +struct ioctl_hyper_dmabuf_unexport unexport_attr; +struct file *filp = (struct file *)attr; + +if (!filp || !exported) +return ; + +if (exported->filp == filp) { +dev_dbg (hy_drv_priv->dev, +"Forcefully releasing buffer {id:%d key:%d %d %d}\n" , + exported->hid.id, exported->hid.rng_key[0 ], + exported->hid.rng_key[1 ], exported->hid.rng_key[2 ]); + + unexport_attr.hid = exported->hid; + unexport_attr.delay_ms =0 ; + +hyper_dmabuf_unexport_ioctl (filp, &unexport_attr); + } +} + +static int hyper_dmabuf_open (struct inode *inode,struct file *filp) +{ +int ret =0 ; + +/* Do not allow exclusive open */ +if (filp->f_flags &O_EXCL ) +return -EBUSY ; + +return ret; +} + +static int hyper_dmabuf_release (struct inode *inode,struct file *filp) +{ +hyper_dmabuf_foreach_exported (force_free, filp); + +return 0 ; +} +
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 99
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +10
-0+static const struct file_operations hyper_dmabuf_driver_fops = { + .owner =THIS_MODULE , + .open = hyper_dmabuf_open, + .release = hyper_dmabuf_release, + .unlocked_ioctl = hyper_dmabuf_ioctl, +}; + +static struct miscdevice hyper_dmabuf_miscdev = { + .minor =MISC_DYNAMIC_MINOR , + .name ="hyper_dmabuf" ,
Can this string be a constant through the driver?
Quoted 49 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +49
-0+ .fops = &hyper_dmabuf_driver_fops, +}; + +static int register_device (void ) +{ +int ret =0 ; + + ret =misc_register (&hyper_dmabuf_miscdev); + +if (ret) { +pr_err ("hyper_dmabuf: driver can't be registered\n" ); +return ret; + } + + hy_drv_priv->dev = hyper_dmabuf_miscdev.this_device; + +/* TODO: Check if there is a different way to initialize dma mask */ +dma_coerce_mask_and_coherent (hy_drv_priv->dev,DMA_BIT_MASK (64 )); + +return ret; +} + +static void unregister_device (void ) +{ +dev_info (hy_drv_priv->dev, +"hyper_dmabuf: %s is called\n" , __func__); + +misc_deregister (&hyper_dmabuf_miscdev); +} + +static int __init hyper_dmabuf_drv_init (void ) +{ +int ret =0 ; + +pr_notice ("hyper_dmabuf_starting: Initialization started\n" ); + + hy_drv_priv =kcalloc (1 ,sizeof (struct hyper_dmabuf_private ), +GFP_KERNEL ); + +if (!hy_drv_priv) +return -ENOMEM ; + + ret =register_device (); +if (ret <0 ) { +kfree (hy_drv_priv); +return ret; + } + + hy_drv_priv->bknd_ops =NULL ;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 158
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +10
-0+ +if (hy_drv_priv->bknd_ops ==NULL ) { +pr_err ("Hyper_dmabuf: no backend found\n" ); +kfree (hy_drv_priv); +return -1 ; + } + +mutex_init (&hy_drv_priv->lock); + +mutex_lock (&hy_drv_priv->lock);
Why do you need to immediately lock here?
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 160
Quoted 2 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +2
-0+ + hy_drv_priv->initialized = false;
kcalloc allocates zeroed memory, so you might rely on that fact
Quoted 24 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +24
-0+ +dev_info (hy_drv_priv->dev, +"initializing database for imported/exported dmabufs\n" ); + + hy_drv_priv->work_queue =create_workqueue ("hyper_dmabuf_wqueue" ); + + ret =hyper_dmabuf_table_init (); +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"fail to init table for exported/imported entries\n" ); +mutex_unlock (&hy_drv_priv->lock); +kfree (hy_drv_priv); +return ret; + } + +#ifdef CONFIG_HYPER_DMABUF_SYSFS + ret =hyper_dmabuf_register_sysfs (hy_drv_priv->dev); +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"failed to initialize sysfs\n" ); +mutex_unlock (&hy_drv_priv->lock); +kfree (hy_drv_priv); +return ret; + }
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 194
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +10
-0+#endif + +if (hy_drv_priv->bknd_ops->init) { + ret = hy_drv_priv->bknd_ops->init (); + +if (ret <0 ) { +dev_dbg (hy_drv_priv->dev, +"failed to initialize backend.\n" ); +mutex_unlock (&hy_drv_priv->lock); +kfree (hy_drv_priv);
unregister sysfs?
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 200
Quoted 6 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +6
-0+return ret; + } + } + + hy_drv_priv->domid = hy_drv_priv->bknd_ops->get_vm_id (); +
This seems to be a bit inconsistent, e.g. domid vs vm_id
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 204
Quoted 4 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +4
-0+ ret = hy_drv_priv->bknd_ops->init_comm_env (); +if (ret <0 ) { +dev_dbg (hy_drv_priv->dev, +"failed to initialize comm-env.\n" );
bknd_ops->cleanup?
Quoted 4 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +4
-0+ }else { + hy_drv_priv->initialized = true; + } +
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c line 218
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +10
-0+mutex_unlock (&hy_drv_priv->lock); + +dev_info (hy_drv_priv->dev, +"Finishing up initialization of hyper_dmabuf drv\n" ); + +/* interrupt for comm should be registered here: */ +return ret; +} + +static void hyper_dmabuf_drv_exit (void )
__exit?
Quoted 185 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +36
-0+{ +#ifdef CONFIG_HYPER_DMABUF_SYSFS +hyper_dmabuf_unregister_sysfs (hy_drv_priv->dev); +#endif + +mutex_lock (&hy_drv_priv->lock); + +/* hash tables for export/import entries and ring_infos */ +hyper_dmabuf_table_destroy (); + + hy_drv_priv->bknd_ops->destroy_comm (); + +if (hy_drv_priv->bknd_ops->cleanup) { + hy_drv_priv->bknd_ops->cleanup (); + }; + +/* destroy workqueue */ +if (hy_drv_priv->work_queue) +destroy_workqueue (hy_drv_priv->work_queue); + +/* destroy id_queue */ +if (hy_drv_priv->id_queue) +hyper_dmabuf_free_hid_list (); + +mutex_unlock (&hy_drv_priv->lock); + +dev_info (hy_drv_priv->dev, +"hyper_dmabuf driver: Exiting\n" ); + +kfree (hy_drv_priv); + +unregister_device (); +} + +module_init (hyper_dmabuf_drv_init); +module_exit (hyper_dmabuf_drv_exit);drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h new file +111
-0@@ -0,0 +1,111 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __LINUX_HYPER_DMABUF_DRV_H__ +#define __LINUX_HYPER_DMABUF_DRV_H__ + +#include <linux/device.h> +#include <linux/hyper_dmabuf.h> + +struct hyper_dmabuf_req ; + +struct hyper_dmabuf_private { +struct device *dev; + +/* VM(domain) id of current VM instance */ +int domid; + +/* workqueue dedicated to hyper_dmabuf driver */ +struct workqueue_struct *work_queue; + +/* list of reusable hyper_dmabuf_ids */ +struct list_reusable_id *id_queue; + +/* backend ops - hypervisor specific */ +struct hyper_dmabuf_bknd_ops *bknd_ops; + +/* device global lock */ +/* TODO: might need a lock per resource (e.g. EXPORT LIST) */ +struct mutex lock; + +/* flag that shows whether backend is initialized */ +bool initialized; + +/* # of pending events */ +int pending; +}; + +struct list_reusable_id { +hyper_dmabuf_id_t hid; +struct list_head list; +}; + +struct hyper_dmabuf_bknd_ops { +/* backend initialization routine (optional) */ +int (*init)(void ); + +/* backend cleanup routine (optional) */ +int (*cleanup)(void ); + +/* retreiving id of current virtual machine */ +int (*get_vm_id)(void ); + +/* get pages shared via hypervisor-specific method */ +int (*share_pages)(struct page **pages,int vm_id, +int nents,void **refs_info); + +/* make shared pages unshared via hypervisor specific method */ +int (*unshare_pages)(void **refs_info,int nents); + +/* map remotely shared pages on importer's side via +* hypervisor-specific method +*/ +struct page ** (*map_shared_pages)(unsigned long ref,int vm_id, +int nents,void **refs_info); + +/* unmap and free shared pages on importer's side via +* hypervisor-specific method +*/ +int (*unmap_shared_pages)(void **refs_info,int nents); + +/* initialize communication environment */ +int (*init_comm_env)(void ); + +void (*destroy_comm)(void ); + +/* upstream ch setup (receiving and responding) */ +int (*init_rx_ch)(int vm_id); + +/* downstream ch setup (transmitting and parsing responses) */ +int (*init_tx_ch)(int vm_id); + +int (*send_req)(int vm_id,struct hyper_dmabuf_req *req,int wait); +}; + +/* exporting global drv private info */ +extern struct hyper_dmabuf_private *hy_drv_priv; + +#endif /* __LINUX_HYPER_DMABUF_DRV_H__ */ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +26
-0@@ -0,0 +1,135 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]>
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c line 36
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +10
-0+* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/list.h> +#include <linux/slab.h> +#include <linux/random.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_id.h" +
Common notes:
- I think even if hy_drv_priv is global you shouldn't touch it directly, but pass it as function parameter.
- Don't you need to protect reusable list with lock?
Quoted 47 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +47
-0+void hyper_dmabuf_store_hid (hyper_dmabuf_id_t hid) +{ +struct list_reusable_id *reusable_head = hy_drv_priv->id_queue; +struct list_reusable_id *new_reusable; + + new_reusable =kmalloc (sizeof (*new_reusable),GFP_KERNEL ); + +if (!new_reusable) +return ; + + new_reusable->hid = hid; + +list_add (&new_reusable->list, &reusable_head->list); +} + +static hyper_dmabuf_id_t get_reusable_hid (void ) +{ +struct list_reusable_id *reusable_head = hy_drv_priv->id_queue; +hyper_dmabuf_id_t hid = {-1 , {0 ,0 ,0 } }; + +/* check there is reusable id */ +if (!list_empty (&reusable_head->list)) { + reusable_head =list_first_entry (&reusable_head->list, + struct list_reusable_id, + list); + +list_del (&reusable_head->list); + hid = reusable_head->hid; +kfree (reusable_head); + } + +return hid; +} + +void hyper_dmabuf_free_hid_list (void ) +{ +struct list_reusable_id *reusable_head = hy_drv_priv->id_queue; +struct list_reusable_id *temp_head; + +if (reusable_head) { +/* freeing mem space all reusable ids in the stack */ +while (!list_empty (&reusable_head->list)) { + temp_head =list_first_entry (&reusable_head->list, + struct list_reusable_id, + list); +list_del (&temp_head->list); +kfree (temp_head);
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c line 93
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +10
-0+ } + +/* freeing head */ +kfree (reusable_head); + } +} + +hyper_dmabuf_id_t hyper_dmabuf_get_hid (void ) +{ +static int count;
could you please explicitly initialize this?
Quoted 9 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +9
-0+hyper_dmabuf_id_t hid; +struct list_reusable_id *reusable_head; + +/* first call to hyper_dmabuf_get_id */ +if (count ==0 ) { + reusable_head =kmalloc (sizeof (*reusable_head),GFP_KERNEL ); + +if (!reusable_head) +return (hyper_dmabuf_id_t ){-1 , {0 ,0 ,0 } };
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c line 112
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +10
-0+ +/* list head has an invalid count */ + reusable_head->hid.id =-1 ; +INIT_LIST_HEAD (&reusable_head->list); + hy_drv_priv->id_queue = reusable_head; + } + + hid =get_reusable_hid (); + +/*creating a new H-ID only if nothing in the reusable id queue
start the comment from a new line
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c line 119
Quoted 7 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +7
-0+* and count is less than maximum allowed +*/ +if (hid.id ==-1 && count <HYPER_DMABUF_ID_MAX ) + hid.id =HYPER_DMABUF_ID_CREATE (hy_drv_priv->domid, count++); + +/* random data embedded in the id for security */ +get_random_bytes (&hid.rng_key[0 ],12 );
can magic 12 be a defined constant?
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c line 129
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +10
-0+ +return hid; +} + +bool hyper_dmabuf_hid_keycomp (hyper_dmabuf_id_t hid1,hyper_dmabuf_id_t hid2) +{ +int i; + +/* compare keys */ +for (i =0 ; i <3 ; i++) {
can magic 3 be defined as a constant please?
Quoted 33 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +6
-0+if (hid1.rng_key[i] != hid2.rng_key[i]) +return false; + } + +return true; +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +21
-0@@ -0,0 +1,53 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE.
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h line 31
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +10
-0+* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_ID_H__ +#define __HYPER_DMABUF_ID_H__ + +#define HYPER_DMABUF_ID_CREATE (domid, cnt) \ + ((((domid) & 0xFF) << 24) | ((cnt) & 0xFFFFFF))
I would define hyper_dmabuf_id_t.id as a union or 2 separate fields to avoid his magic
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h line 39
Quoted 8 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +8
-0+ +#define HYPER_DMABUF_DOM_ID (hid) \ + (((hid.id) >> 24) & 0xFF) + +/* currently maximum number of buffers shared +* at any given moment is limited to 1000 +*/ +#define HYPER_DMABUF_ID_MAX 1000
Why 1000? Is it just to limit or is dictated by some use-cases/experiments?
Quoted 54 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +14
-0+ +/* adding freed hid to the reusable list */ +void hyper_dmabuf_store_hid (hyper_dmabuf_id_t hid); + +/* freeing the reusasble list */ +void hyper_dmabuf_free_hid_list (void ); + +/* getting a hid available to use. */ +hyper_dmabuf_id_t hyper_dmabuf_get_hid (void ); + +/* comparing two different hid */ +bool hyper_dmabuf_hid_keycomp (hyper_dmabuf_id_t hid1,hyper_dmabuf_id_t hid2); + +#endif /*__HYPER_DMABUF_ID_H*/ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +34
-0@@ -0,0 +1,672 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/uaccess.h>
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 44
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+#include <linux/dma-buf.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_id.h" +#include "hyper_dmabuf_struct.h" +#include "hyper_dmabuf_ioctl.h" +#include "hyper_dmabuf_list.h" +#include "hyper_dmabuf_msg.h" +#include "hyper_dmabuf_sgl_proc.h" +#include "hyper_dmabuf_ops.h" +
Here and below: please do not touch global hy_drv_priv
Quoted 40 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +40
-0+static int hyper_dmabuf_tx_ch_setup_ioctl (struct file *filp,void *data) +{ +struct ioctl_hyper_dmabuf_tx_ch_setup *tx_ch_attr; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +int ret =0 ; + +if (!data) { +dev_err (hy_drv_priv->dev,"user data is NULL\n" ); +return -EINVAL ; + } + tx_ch_attr = (struct ioctl_hyper_dmabuf_tx_ch_setup *)data; + + ret = bknd_ops->init_tx_ch (tx_ch_attr->remote_domain); + +return ret; +} + +static int hyper_dmabuf_rx_ch_setup_ioctl (struct file *filp,void *data) +{ +struct ioctl_hyper_dmabuf_rx_ch_setup *rx_ch_attr; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +int ret =0 ; + +if (!data) { +dev_err (hy_drv_priv->dev,"user data is NULL\n" ); +return -EINVAL ; + } + + rx_ch_attr = (struct ioctl_hyper_dmabuf_rx_ch_setup *)data; + + ret = bknd_ops->init_rx_ch (rx_ch_attr->source_domain); + +return ret; +} + +static int send_export_msg (struct exported_sgt_info *exported, +struct pages_info *pg_info) +{ +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +struct hyper_dmabuf_req *req;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 94
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+int op[MAX_NUMBER_OF_OPERANDS ] = {0 }; +int ret, i; + +/* now create request for importer via ring */ + op[0 ] = exported->hid.id; + +for (i =0 ; i <3 ; i++) + op[i+1 ] = exported->hid.rng_key[i]; + +if (pg_info) {
heh, can we have a well defined structures for requests/responses, so we don't have to put all these magics?
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 99
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +5
-0+ op[4 ] = pg_info->nents; + op[5 ] = pg_info->frst_ofst; + op[6 ] = pg_info->last_len; + op[7 ] = bknd_ops->share_pages (pg_info->pgs, exported->rdomid, + pg_info->nents, &exported->refs_info);
ret?
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +5
-0+if (op[7 ] <0 ) { +dev_err (hy_drv_priv->dev,"pages sharing failed\n" ); +return op[7 ]; + } + }
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 114
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+ + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL ); + +if (!req) +return -ENOMEM ; + +/* composing a message to the importer */ +hyper_dmabuf_create_req (req,HYPER_DMABUF_EXPORT , &op[0 ]); + + ret = bknd_ops->send_req (exported->rdomid, req, true);
can we allocate req on stack? and don't use kcalloc?
Quoted 11 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +11
-0+ +kfree (req); + +return ret; +} + +/* Fast path exporting routine in case same buffer is already exported. +* +* If same buffer is still valid and exist in EXPORT LIST it returns 0 so +* that remaining normal export process can be skipped. +*
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 135
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+* If "unexport" is scheduled for the buffer, it cancels it since the buffer +* is being re-exported. +* +* return '1' if reexport is needed, return '0' if succeeds, return +* Kernel error code if something goes wrong +*/ +static int fastpath_export (hyper_dmabuf_id_t hid) +{ +int reexport =1 ; +int ret =0 ;
why do you need these two variables?
Quoted 64 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +64
-0+struct exported_sgt_info *exported; + + exported =hyper_dmabuf_find_exported (hid); + +if (!exported) +return reexport; + +if (exported->valid == false) +return reexport; + +/* +* Check if unexport is already scheduled for that buffer, +* if so try to cancel it. If that will fail, buffer needs +* to be reexport once again. +*/ +if (exported->unexport_sched) { +if (!cancel_delayed_work_sync (&exported->unexport)) +return reexport; + + exported->unexport_sched = false; + } + +return ret; +} + +static int hyper_dmabuf_export_remote_ioctl (struct file *filp,void *data) +{ +struct ioctl_hyper_dmabuf_export_remote *export_remote_attr = + (struct ioctl_hyper_dmabuf_export_remote *)data; +struct dma_buf *dma_buf; +struct dma_buf_attachment *attachment; +struct sg_table *sgt; +struct pages_info *pg_info; +struct exported_sgt_info *exported; +hyper_dmabuf_id_t hid; +int ret =0 ; + +if (hy_drv_priv->domid == export_remote_attr->remote_domain) { +dev_err (hy_drv_priv->dev, +"exporting to the same VM is not permitted\n" ); +return -EINVAL ; + } + + dma_buf =dma_buf_get (export_remote_attr->dmabuf_fd); + +if (IS_ERR (dma_buf)) { +dev_err (hy_drv_priv->dev,"Cannot get dma buf\n" ); +return PTR_ERR (dma_buf); + } + +/* we check if this specific attachment was already exported +* to the same domain and if yes and it's valid sgt_info, +* it returns hyper_dmabuf_id of pre-exported sgt_info +*/ + hid =hyper_dmabuf_find_hid_exported (dma_buf, + export_remote_attr->remote_domain); + +if (hid.id !=-1 ) { + ret =fastpath_export (hid); + +/* return if fastpath_export succeeds or +* gets some fatal error +*/ +if (ret <=0 ) {
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 209
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+dma_buf_put (dma_buf); + export_remote_attr->hid = hid; +return ret; + } + } + + attachment =dma_buf_attach (dma_buf, hy_drv_priv->dev); +if (IS_ERR (attachment)) { +dev_err (hy_drv_priv->dev,"cannot get attachment\n" ); + ret =PTR_ERR (attachment);
here and below - if you have dma-buf from fastpath don't you need to release/handle it on error path here? E.g. fastpath may have canceled unexport work for this buffer
Quoted 145 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +145
-0+ goto fail_attach; + } + + sgt =dma_buf_map_attachment (attachment,DMA_BIDIRECTIONAL ); + +if (IS_ERR (sgt)) { +dev_err (hy_drv_priv->dev,"cannot map attachment\n" ); + ret =PTR_ERR (sgt); + goto fail_map_attachment; + } + + exported =kcalloc (1 ,sizeof (*exported),GFP_KERNEL ); + +if (!exported) { + ret = -ENOMEM ; + goto fail_sgt_info_creation; + } + + exported->hid =hyper_dmabuf_get_hid (); + +/* no more exported dmabuf allowed */ +if (exported->hid.id ==-1 ) { +dev_err (hy_drv_priv->dev, +"exceeds allowed number of dmabuf to be exported\n" ); + ret = -ENOMEM ; + goto fail_sgt_info_creation; + } + + exported->rdomid = export_remote_attr->remote_domain; + exported->dma_buf = dma_buf; + exported->valid = true; + + exported->active_sgts =kmalloc (sizeof (struct sgt_list ),GFP_KERNEL ); +if (!exported->active_sgts) { + ret = -ENOMEM ; + goto fail_map_active_sgts; + } + + exported->active_attached =kmalloc (sizeof (struct attachment_list ), +GFP_KERNEL ); +if (!exported->active_attached) { + ret = -ENOMEM ; + goto fail_map_active_attached; + } + + exported->va_kmapped =kmalloc (sizeof (struct kmap_vaddr_list ), +GFP_KERNEL ); +if (!exported->va_kmapped) { + ret = -ENOMEM ; + goto fail_map_va_kmapped; + } + + exported->va_vmapped =kmalloc (sizeof (struct vmap_vaddr_list ), +GFP_KERNEL ); +if (!exported->va_vmapped) { + ret = -ENOMEM ; + goto fail_map_va_vmapped; + } + + exported->active_sgts->sgt = sgt; + exported->active_attached->attach = attachment; + exported->va_kmapped->vaddr =NULL ; + exported->va_vmapped->vaddr =NULL ; + +/* initialize list of sgt, attachment and vaddr for dmabuf sync +* via shadow dma-buf +*/ +INIT_LIST_HEAD (&exported->active_sgts->list); +INIT_LIST_HEAD (&exported->active_attached->list); +INIT_LIST_HEAD (&exported->va_kmapped->list); +INIT_LIST_HEAD (&exported->va_vmapped->list); + +if (ret) { +dev_err (hy_drv_priv->dev, +"failed to load private data\n" ); + ret = -EINVAL ; + goto fail_export; + } + + pg_info =hyper_dmabuf_ext_pgs (sgt); +if (!pg_info) { +dev_err (hy_drv_priv->dev, +"failed to construct pg_info\n" ); + ret = -ENOMEM ; + goto fail_export; + } + + exported->nents = pg_info->nents; + +/* now register it to export list */ +hyper_dmabuf_register_exported (exported); + + export_remote_attr->hid = exported->hid; + + ret =send_export_msg (exported, pg_info); + +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"failed to send out the export request\n" ); + goto fail_send_request; + } + +/* free pg_info */ +kfree (pg_info->pgs); +kfree (pg_info); + + exported->filp = filp; + +return ret; + +/* Clean-up if error occurs */ + +fail_send_request: +hyper_dmabuf_remove_exported (exported->hid); + +/* free pg_info */ +kfree (pg_info->pgs); +kfree (pg_info); + +fail_export: +kfree (exported->va_vmapped); + +fail_map_va_vmapped: +kfree (exported->va_kmapped); + +fail_map_va_kmapped: +kfree (exported->active_attached); + +fail_map_active_attached: +kfree (exported->active_sgts); +kfree (exported); + +fail_map_active_sgts: +fail_sgt_info_creation: +dma_buf_unmap_attachment (attachment, sgt, +DMA_BIDIRECTIONAL ); + +fail_map_attachment: +dma_buf_detach (dma_buf, attachment); + +fail_attach: +dma_buf_put (dma_buf); + +return ret; +}
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 364
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+ +static int hyper_dmabuf_export_fd_ioctl (struct file *filp,void *data) +{ +struct ioctl_hyper_dmabuf_export_fd *export_fd_attr = + (struct ioctl_hyper_dmabuf_export_fd *)data; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +struct imported_sgt_info *imported; +struct hyper_dmabuf_req *req; +struct page **data_pgs; +int op[4 ];
don't you have hyper_dmabuf_id_t for that?
Quoted 19 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +19
-0+int i; +int ret =0 ; + +dev_dbg (hy_drv_priv->dev,"%s entry\n" , __func__); + +/* look for dmabuf for the id */ + imported =hyper_dmabuf_find_imported (export_fd_attr->hid); + +/* can't find sgt from the table */ +if (!imported) { +dev_err (hy_drv_priv->dev,"can't find the entry\n" ); +return -ENOENT ; + } + +mutex_lock (&hy_drv_priv->lock); + + imported->importers++; + +/* send notification for export_fd to exporter */
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 393
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+ op[0 ] = imported->hid.id; + +for (i =0 ; i <3 ; i++) + op[i+1 ] = imported->hid.rng_key[i]; + +dev_dbg (hy_drv_priv->dev,"Export FD of buffer {id:%d key:%d %d %d}\n" , + imported->hid.id, imported->hid.rng_key[0 ], + imported->hid.rng_key[1 ], imported->hid.rng_key[2 ]); + + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL );
can you have req allocated on stack?
Quoted 4 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +4
-0+ +if (!req) { +mutex_unlock (&hy_drv_priv->lock); +return -ENOMEM ;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 407
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+ } + +hyper_dmabuf_create_req (req,HYPER_DMABUF_EXPORT_FD , &op[0 ]); + + ret = bknd_ops->send_req (HYPER_DMABUF_DOM_ID (imported->hid), req, true); + +if (ret <0 ) { +/* in case of timeout other end eventually will receive request, +* so we need to undo it +*/
and what if there is a race condition? at the time you delete the buffer the corresponding response comes in?
Quoted 77 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +77
-0+hyper_dmabuf_create_req (req,HYPER_DMABUF_EXPORT_FD_FAILED , + &op[0 ]); + bknd_ops->send_req (HYPER_DMABUF_DOM_ID (imported->hid), + req, false); +kfree (req); +dev_err (hy_drv_priv->dev, +"Failed to create sgt or notify exporter\n" ); + imported->importers--; +mutex_unlock (&hy_drv_priv->lock); +return ret; + } + +kfree (req); + +if (ret ==HYPER_DMABUF_REQ_ERROR ) { +dev_err (hy_drv_priv->dev, +"Buffer invalid {id:%d key:%d %d %d}, cannot import\n" , + imported->hid.id, imported->hid.rng_key[0 ], + imported->hid.rng_key[1 ], imported->hid.rng_key[2 ]); + + imported->importers--; +mutex_unlock (&hy_drv_priv->lock); +return -EINVAL ; + } + + ret =0 ; + +dev_dbg (hy_drv_priv->dev, +"Found buffer gref %d off %d\n" , + imported->ref_handle, imported->frst_ofst); + +dev_dbg (hy_drv_priv->dev, +"last len %d nents %d domain %d\n" , + imported->last_len, imported->nents, +HYPER_DMABUF_DOM_ID (imported->hid)); + +if (!imported->sgt) { +dev_dbg (hy_drv_priv->dev, +"buffer {id:%d key:%d %d %d} pages not mapped yet\n" , + imported->hid.id, imported->hid.rng_key[0 ], + imported->hid.rng_key[1 ], imported->hid.rng_key[2 ]); + + data_pgs = bknd_ops->map_shared_pages (imported->ref_handle, +HYPER_DMABUF_DOM_ID (imported->hid), + imported->nents, + &imported->refs_info); + +if (!data_pgs) { +dev_err (hy_drv_priv->dev, +"can't map pages hid {id:%d key:%d %d %d}\n" , + imported->hid.id, imported->hid.rng_key[0 ], + imported->hid.rng_key[1 ], + imported->hid.rng_key[2 ]); + + imported->importers--; + + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL ); + +if (!req) { +mutex_unlock (&hy_drv_priv->lock); +return -ENOMEM ; + } + +hyper_dmabuf_create_req (req, +HYPER_DMABUF_EXPORT_FD_FAILED , + &op[0 ]); + + bknd_ops->send_req (HYPER_DMABUF_DOM_ID (imported->hid), + req, false); +kfree (req); +mutex_unlock (&hy_drv_priv->lock); +return -EINVAL ; + } + + imported->sgt = hyper_dmabuf_create_sgt(data_pgs, + imported->frst_ofst, + imported->last_len,
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 494
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+ imported->nents); + + } + + export_fd_attr->fd =hyper_dmabuf_export_fd (imported, + export_fd_attr->flags); + +if (export_fd_attr->fd <0 ) { +/* fail to get fd */ + ret = export_fd_attr->fd;
why don't you send HYPER_DMABUF_EXPORT_FD_FAILED in this case?
Quoted 8 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +8
-0+ } + +mutex_unlock (&hy_drv_priv->lock); + +dev_dbg (hy_drv_priv->dev,"%s exit\n" , __func__); +return ret; +} +
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 512
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+/* unexport dmabuf from the database and send int req to the source domain +* to unmap it. +*/ +static void delayed_unexport (struct work_struct *work) +{ +struct hyper_dmabuf_req *req; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +struct exported_sgt_info *exported = + container_of(work, struct exported_sgt_info, unexport.work); +int op[4 ];
use the struct defined for this
Quoted 6 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +6
-0+int i, ret; + +if (!exported) +return ; + + dev_dbg(hy_drv_priv->dev,
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 528
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+"Marking buffer {id:%d key:%d %d %d} as invalid\n" , + exported->hid.id, exported->hid.rng_key[0 ], + exported->hid.rng_key[1], exported->hid.rng_key[2]); + +/* no longer valid */ + exported->valid = false; + + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL ); + +if (!req)
will we leak the buffer because we return here?
Quoted 64 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +64
-0+return ; + + op[0 ] = exported->hid.id; + +for (i =0 ; i <3 ; i++) + op[i+1 ] = exported->hid.rng_key[i]; + +hyper_dmabuf_create_req (req,HYPER_DMABUF_NOTIFY_UNEXPORT , &op[0 ]); + +/* Now send unexport request to remote domain, marking +* that buffer should not be used anymore +*/ + ret = bknd_ops->send_req (exported->rdomid, req, true); +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"unexport message for buffer {id:%d key:%d %d %d} failed\n" , + exported->hid.id, exported->hid.rng_key[0 ], + exported->hid.rng_key[1 ], exported->hid.rng_key[2 ]); + } + +kfree (req); + exported->unexport_sched = false; + +/* Immediately clean-up if it has never been exported by importer +* (so no SGT is constructed on importer). +* clean it up later in remote sync when final release ops +* is called (importer does this only when there's no +* no consumer of locally exported FDs) +*/ +if (exported->active ==0 ) { +dev_dbg (hy_drv_priv->dev, +"claning up buffer {id:%d key:%d %d %d} completly\n" , + exported->hid.id, exported->hid.rng_key[0 ], + exported->hid.rng_key[1 ], exported->hid.rng_key[2 ]); + +hyper_dmabuf_cleanup_sgt_info (exported, false); +hyper_dmabuf_remove_exported (exported->hid); + +/* register hyper_dmabuf_id to the list for reuse */ +hyper_dmabuf_store_hid (exported->hid); + +kfree (exported); + } +} + +/* Schedule unexport of dmabuf. +*/ +int hyper_dmabuf_unexport_ioctl (struct file *filp,void *data) +{ +struct ioctl_hyper_dmabuf_unexport *unexport_attr = + (struct ioctl_hyper_dmabuf_unexport *)data; +struct exported_sgt_info *exported; + +dev_dbg (hy_drv_priv->dev,"%s entry\n" , __func__); + +/* find dmabuf in export list */ + exported =hyper_dmabuf_find_exported (unexport_attr->hid); + +dev_dbg (hy_drv_priv->dev, +"scheduling unexport of buffer {id:%d key:%d %d %d}\n" , + unexport_attr->hid.id, unexport_attr->hid.rng_key[0 ], + unexport_attr->hid.rng_key[1 ], unexport_attr->hid.rng_key[2 ]); + +/* failed to find corresponding entry in export list */
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c line 602
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +10
-0+if (exported ==NULL ) { + unexport_attr->status = -ENOENT ; +return -ENOENT ; + } + +if (exported->unexport_sched) +return 0 ; + + exported->unexport_sched = true; +INIT_DELAYED_WORK (&exported->unexport, delayed_unexport);
why can't you just wait for the buffer to be unexported?
Quoted 549 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +70
-0+schedule_delayed_work (&exported->unexport, +msecs_to_jiffies (unexport_attr->delay_ms)); + +dev_dbg (hy_drv_priv->dev,"%s exit\n" , __func__); +return 0 ; +} + +const struct hyper_dmabuf_ioctl_desc hyper_dmabuf_ioctls[] = { +HYPER_DMABUF_IOCTL_DEF (IOCTL_HYPER_DMABUF_TX_CH_SETUP , + hyper_dmabuf_tx_ch_setup_ioctl,0 ), +HYPER_DMABUF_IOCTL_DEF (IOCTL_HYPER_DMABUF_RX_CH_SETUP , + hyper_dmabuf_rx_ch_setup_ioctl,0 ), +HYPER_DMABUF_IOCTL_DEF (IOCTL_HYPER_DMABUF_EXPORT_REMOTE , + hyper_dmabuf_export_remote_ioctl,0 ), +HYPER_DMABUF_IOCTL_DEF (IOCTL_HYPER_DMABUF_EXPORT_FD , + hyper_dmabuf_export_fd_ioctl,0 ), +HYPER_DMABUF_IOCTL_DEF (IOCTL_HYPER_DMABUF_UNEXPORT , + hyper_dmabuf_unexport_ioctl,0 ), +}; + +long hyper_dmabuf_ioctl (struct file *filp, +unsigned int cmd,unsigned long param) +{ +const struct hyper_dmabuf_ioctl_desc *ioctl =NULL ; +unsigned int nr =_IOC_NR (cmd); +int ret; +hyper_dmabuf_ioctl_t func; +char *kdata; + +if (nr >ARRAY_SIZE (hyper_dmabuf_ioctls)) { +dev_err (hy_drv_priv->dev,"invalid ioctl\n" ); +return -EINVAL ; + } + + ioctl = &hyper_dmabuf_ioctls[nr]; + + func = ioctl->func; + +if (unlikely (!func)) { +dev_err (hy_drv_priv->dev,"no function\n" ); +return -EINVAL ; + } + + kdata =kmalloc (_IOC_SIZE (cmd),GFP_KERNEL ); +if (!kdata) +return -ENOMEM ; + +if (copy_from_user (kdata, (void __user *)param, +_IOC_SIZE (cmd)) !=0 ) { +dev_err (hy_drv_priv->dev, +"failed to copy from user arguments\n" ); + ret = -EFAULT ; + goto ioctl_error; + } + + ret =func (filp, kdata); + +if (copy_to_user ((void __user *)param, kdata, +_IOC_SIZE (cmd)) !=0 ) { +dev_err (hy_drv_priv->dev, +"failed to copy to user arguments\n" ); + ret = -EFAULT ; + goto ioctl_error; + } + +ioctl_error: +kfree (kdata); + +return ret; +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h new file +52
-0@@ -0,0 +1,52 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_IOCTL_H__ +#define __HYPER_DMABUF_IOCTL_H__ + +typedef int (*hyper_dmabuf_ioctl_t )(struct file *filp,void *data); + +struct hyper_dmabuf_ioctl_desc { +unsigned int cmd; +int flags; +hyper_dmabuf_ioctl_t func; +const char *name; +}; + +#define HYPER_DMABUF_IOCTL_DEF (ioctl, _func, _flags) \ + [_IOC_NR(ioctl)] = { \ + .cmd = ioctl, \ + .func = _func, \ + .flags = _flags, \ + .name = #ioctl \ + } + +long hyper_dmabuf_ioctl (struct file *filp, +unsigned int cmd,unsigned long param); + +int hyper_dmabuf_unexport_ioctl (struct file *filp,void *data); + +#endif //__HYPER_DMABUF_IOCTL_H__ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c new file +294
-0@@ -0,0 +1,294 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/cdev.h> +#include <linux/hashtable.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_list.h" +#include "hyper_dmabuf_id.h" + +DECLARE_HASHTABLE (hyper_dmabuf_hash_imported,MAX_ENTRY_IMPORTED ); +DECLARE_HASHTABLE (hyper_dmabuf_hash_exported,MAX_ENTRY_EXPORTED ); + +#ifdef CONFIG_HYPER_DMABUF_SYSFS +static ssize_t hyper_dmabuf_imported_show (struct device *drv, +struct device_attribute *attr, +char *buf) +{ +struct list_entry_imported *info_entry; +int bkt; +ssize_t count =0 ; +size_t total =0 ; + +hash_for_each (hyper_dmabuf_hash_imported, bkt, info_entry, node) { +hyper_dmabuf_id_t hid = info_entry->imported->hid; +int nents = info_entry->imported->nents; +bool valid = info_entry->imported->valid; +int num_importers = info_entry->imported->importers; + + total += nents; + count +=scnprintf (buf + count,PAGE_SIZE - count, +"hid:{%d %d %d %d}, nent:%d, v:%c, numi:%d\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ], nents, (valid ?'t' :'f' ), + num_importers); + } + count +=scnprintf (buf + count,PAGE_SIZE - count, +"total nents: %lu\n" , total); + +return count; +} + +static ssize_t hyper_dmabuf_exported_show (struct device *drv, +struct device_attribute *attr, +char *buf) +{ +struct list_entry_exported *info_entry; +int bkt; +ssize_t count =0 ; +size_t total =0 ; + +hash_for_each (hyper_dmabuf_hash_exported, bkt, info_entry, node) { +hyper_dmabuf_id_t hid = info_entry->exported->hid; +int nents = info_entry->exported->nents; +bool valid = info_entry->exported->valid; +int importer_exported = info_entry->exported->active; + + total += nents; + count +=scnprintf (buf + count,PAGE_SIZE - count, +"hid:{%d %d %d %d}, nent:%d, v:%c, ie:%d\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ], nents, (valid ?'t' :'f' ), + importer_exported); + } + count +=scnprintf (buf + count,PAGE_SIZE - count, +"total nents: %lu\n" , total); + +return count; +} + +static DEVICE_ATTR(imported, 0400, hyper_dmabuf_imported_show, NULL); +static DEVICE_ATTR(exported, 0400, hyper_dmabuf_exported_show, NULL); + +int hyper_dmabuf_register_sysfs (struct device *dev) +{ +int err; + + err =device_create_file (dev, &dev_attr_imported); +if (err <0 ) + goto err1; + err =device_create_file (dev, &dev_attr_exported); +if (err <0 ) + goto err2; + +return 0 ; +err2: +device_remove_file (dev, &dev_attr_imported); +err1: +return -1 ; +} + +int hyper_dmabuf_unregister_sysfs (struct device *dev) +{ +device_remove_file (dev, &dev_attr_imported); +device_remove_file (dev, &dev_attr_exported); +return 0 ; +} + +#endif + +int hyper_dmabuf_table_init (void ) +{ +hash_init (hyper_dmabuf_hash_imported); +hash_init (hyper_dmabuf_hash_exported); +return 0 ; +} + +int hyper_dmabuf_table_destroy (void ) +{ +/* TODO: cleanup hyper_dmabuf_hash_imported +* and hyper_dmabuf_hash_exported +*/ +return 0 ; +} + +int hyper_dmabuf_register_exported (struct exported_sgt_info *exported) +{ +struct list_entry_exported *info_entry; + + info_entry =kmalloc (sizeof (*info_entry),GFP_KERNEL ); + +if (!info_entry) +return -ENOMEM ; + + info_entry->exported = exported; + +hash_add (hyper_dmabuf_hash_exported, &info_entry->node, + info_entry->exported->hid.id); + +return 0 ; +} + +int hyper_dmabuf_register_imported (struct imported_sgt_info *imported) +{ +struct list_entry_imported *info_entry; + + info_entry =kmalloc (sizeof (*info_entry),GFP_KERNEL ); + +if (!info_entry) +return -ENOMEM ; + + info_entry->imported = imported; + +hash_add (hyper_dmabuf_hash_imported, &info_entry->node, + info_entry->imported->hid.id); + +return 0 ; +} + +struct exported_sgt_info *hyper_dmabuf_find_exported (hyper_dmabuf_id_t hid) +{ +struct list_entry_exported *info_entry; +int bkt; + +hash_for_each (hyper_dmabuf_hash_exported, bkt, info_entry, node) +/* checking hid.id first */ +if (info_entry->exported->hid.id == hid.id) { +/* then key is compared */ +if (hyper_dmabuf_hid_keycomp (info_entry->exported->hid, + hid)) +return info_entry->exported; + +/* if key is unmatched, given HID is invalid, +* so returning NULL +*/ +break ; + } + +return NULL ; +} + +/* search for pre-exported sgt and return id of it if it exist */ +hyper_dmabuf_id_t hyper_dmabuf_find_hid_exported (struct dma_buf *dmabuf, +int domid) +{ +struct list_entry_exported *info_entry; +hyper_dmabuf_id_t hid = {-1 , {0 ,0 ,0 } }; +int bkt; + +hash_for_each (hyper_dmabuf_hash_exported, bkt, info_entry, node) +if (info_entry->exported->dma_buf == dmabuf && + info_entry->exported->rdomid == domid) +return info_entry->exported->hid; + +return hid; +} + +struct imported_sgt_info *hyper_dmabuf_find_imported (hyper_dmabuf_id_t hid) +{ +struct list_entry_imported *info_entry; +int bkt; + +hash_for_each (hyper_dmabuf_hash_imported, bkt, info_entry, node) +/* checking hid.id first */ +if (info_entry->imported->hid.id == hid.id) { +/* then key is compared */ +if (hyper_dmabuf_hid_keycomp (info_entry->imported->hid, + hid)) +return info_entry->imported; +/* if key is unmatched, given HID is invalid, +* so returning NULL +*/ +break ; + } + +return NULL ; +} + +int hyper_dmabuf_remove_exported (hyper_dmabuf_id_t hid) +{ +struct list_entry_exported *info_entry; +int bkt; + +hash_for_each (hyper_dmabuf_hash_exported, bkt, info_entry, node) +/* checking hid.id first */ +if (info_entry->exported->hid.id == hid.id) { +/* then key is compared */ +if (hyper_dmabuf_hid_keycomp (info_entry->exported->hid, + hid)) { +hash_del (&info_entry->node); +kfree (info_entry); +return 0 ; + } + +break ; + } + +return -ENOENT ; +} + +int hyper_dmabuf_remove_imported (hyper_dmabuf_id_t hid) +{ +struct list_entry_imported *info_entry; +int bkt; + +hash_for_each (hyper_dmabuf_hash_imported, bkt, info_entry, node) +/* checking hid.id first */ +if (info_entry->imported->hid.id == hid.id) { +/* then key is compared */ +if (hyper_dmabuf_hid_keycomp (info_entry->imported->hid, + hid)) { +hash_del (&info_entry->node); +kfree (info_entry); +return 0 ; + } + +break ; + } + +return -ENOENT ; +} + +void hyper_dmabuf_foreach_exported ( +void (*func)(struct exported_sgt_info *,void *attr), +void *attr) +{ +struct list_entry_exported *info_entry; +struct hlist_node *tmp; +int bkt; + +hash_for_each_safe (hyper_dmabuf_hash_exported, bkt, tmp, + info_entry, node) { +func (info_entry->exported, attr); + } +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h new file +73
-0@@ -0,0 +1,73 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_LIST_H__ +#define __HYPER_DMABUF_LIST_H__ + +#include "hyper_dmabuf_struct.h" + +/* number of bits to be used for exported dmabufs hash table */ +#define MAX_ENTRY_EXPORTED 7 +/* number of bits to be used for imported dmabufs hash table */ +#define MAX_ENTRY_IMPORTED 7 + +struct list_entry_exported { +struct exported_sgt_info *exported; +struct hlist_node node; +}; + +struct list_entry_imported { +struct imported_sgt_info *imported; +struct hlist_node node; +}; + +int hyper_dmabuf_table_init (void ); + +int hyper_dmabuf_table_destroy (void ); + +int hyper_dmabuf_register_exported (struct exported_sgt_info *info); + +/* search for pre-exported sgt and return id of it if it exist */ +hyper_dmabuf_id_t hyper_dmabuf_find_hid_exported (struct dma_buf *dmabuf, +int domid); + +int hyper_dmabuf_register_imported (struct imported_sgt_info *info); + +struct exported_sgt_info *hyper_dmabuf_find_exported (hyper_dmabuf_id_t hid); + +struct imported_sgt_info *hyper_dmabuf_find_imported (hyper_dmabuf_id_t hid); + +int hyper_dmabuf_remove_exported (hyper_dmabuf_id_t hid); + +int hyper_dmabuf_remove_imported (hyper_dmabuf_id_t hid); + +void hyper_dmabuf_foreach_exported (void (*func)(struct exported_sgt_info *, +void *attr),void *attr); + +int hyper_dmabuf_register_sysfs (struct device *dev); +int hyper_dmabuf_unregister_sysfs (struct device *dev); + +#endif /* __HYPER_DMABUF_LIST_H__ */ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +36
-0@@ -0,0 +1,320 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/workqueue.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_msg.h"
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c line 46
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +10
-0+#include "hyper_dmabuf_list.h" + +struct cmd_process { +struct work_struct work; +struct hyper_dmabuf_req *rq; +int domid; +}; + +void hyper_dmabuf_create_req (struct hyper_dmabuf_req *req, +enum hyper_dmabuf_command cmd,int *op)
can we have structures for all the types of requests/responses defined in some protocol header file? so we avoid hardcoding?
Quoted 108 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +108
-0+{ +int i; + + req->stat =HYPER_DMABUF_REQ_NOT_RESPONDED ; + req->cmd = cmd; + +switch (cmd) { +/* as exporter, commands to importer */ +case HYPER_DMABUF_EXPORT : +/* exporting pages for dmabuf */ +/* command : HYPER_DMABUF_EXPORT, +* op0~op3 : hyper_dmabuf_id +* op4 : number of pages to be shared +* op5 : offset of data in the first page +* op6 : length of data in the last page +* op7 : top-level reference number for shared pages +*/ + +memcpy (&req->op[0 ], &op[0 ],8 *sizeof (int ) + op[8 ]); +break ; + +case HYPER_DMABUF_NOTIFY_UNEXPORT : +/* destroy sg_list for hyper_dmabuf_id on remote side */ +/* command : DMABUF_DESTROY, +* op0~op3 : hyper_dmabuf_id_t hid +*/ + +for (i =0 ; i <4 ; i++) + req->op[i] = op[i]; +break ; + +case HYPER_DMABUF_EXPORT_FD : +case HYPER_DMABUF_EXPORT_FD_FAILED : +/* dmabuf fd is being created on imported side or importing +* failed +* +* command : HYPER_DMABUF_EXPORT_FD or +* HYPER_DMABUF_EXPORT_FD_FAILED, +* op0~op3 : hyper_dmabuf_id +*/ + +for (i =0 ; i <4 ; i++) + req->op[i] = op[i]; +break ; + +default : +/* no command found */ +return ; + } +} + +static void cmd_process_work (struct work_struct *work) +{ +struct imported_sgt_info *imported; +struct cmd_process *proc =container_of (work, + struct cmd_process, work); +struct hyper_dmabuf_req *req; +int domid; +int i; + + req = proc->rq; + domid = proc->domid; + +switch (req->cmd) { +case HYPER_DMABUF_EXPORT : +/* exporting pages for dmabuf */ +/* command : HYPER_DMABUF_EXPORT, +* op0~op3 : hyper_dmabuf_id +* op4 : number of pages to be shared +* op5 : offset of data in the first page +* op6 : length of data in the last page +* op7 : top-level reference number for shared pages +*/ + +/* if nents == 0, it means it is a message only for +* priv synchronization. for existing imported_sgt_info +* so not creating a new one +*/ +if (req->op[4 ] ==0 ) { +hyper_dmabuf_id_t exist = {req->op[0 ], + {req->op[1 ], req->op[2 ], + req->op[3 ] } }; + + imported =hyper_dmabuf_find_imported (exist); + +if (!imported) { +dev_err (hy_drv_priv->dev, +"Can't find imported sgt_info\n" ); +break ; + } + +break ; + } + + imported =kcalloc (1 ,sizeof (*imported),GFP_KERNEL ); + +if (!imported) +break ; + + imported->hid.id = req->op[0 ]; + +for (i =0 ; i <3 ; i++) + imported->hid.rng_key[i] = req->op[i+1 ]; + + imported->nents = req->op[4 ]; + imported->frst_ofst = req->op[5 ]; + imported->last_len = req->op[6 ]; + imported->ref_handle = req->op[7 ];
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c line 164
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +10
-0+ +dev_dbg (hy_drv_priv->dev,"DMABUF was exported\n" ); +dev_dbg (hy_drv_priv->dev,"\thid{id:%d key:%d %d %d}\n" , + req->op[0 ], req->op[1 ], req->op[2 ], + req->op[3 ]); +dev_dbg (hy_drv_priv->dev,"\tnents %d\n" , req->op[4 ]); +dev_dbg (hy_drv_priv->dev,"\tfirst offset %d\n" , req->op[5 ]); +dev_dbg (hy_drv_priv->dev,"\tlast len %d\n" , req->op[6 ]); +dev_dbg (hy_drv_priv->dev,"\tgrefid %d\n" , req->op[7 ]); +
heh, and what if you have to insert something at index 1, for example? you'll end up changing all the hardcodes...
Please have the protocol and its constants, structures etc. defined somewhere
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +5
-0+ imported->valid = true; +hyper_dmabuf_register_imported (imported); + +break ; +
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c line 179
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +10
-0+default : +/* shouldn't get here */ +break ; + } + +kfree (req); +kfree (proc); +} + +int hyper_dmabuf_msg_parse (int domid,struct hyper_dmabuf_req *req)
This seems to be a hyper_dmabuf_msg_*handle* rather than parse...
Quoted 126 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +126
-0+{ +struct cmd_process *proc; +struct hyper_dmabuf_req *temp_req; +struct imported_sgt_info *imported; +struct exported_sgt_info *exported; +hyper_dmabuf_id_t hid; + +if (!req) { +dev_err (hy_drv_priv->dev,"request is NULL\n" ); +return -EINVAL ; + } + + hid.id = req->op[0 ]; + hid.rng_key[0 ] = req->op[1 ]; + hid.rng_key[1 ] = req->op[2 ]; + hid.rng_key[2 ] = req->op[3 ]; + +if ((req->cmd <HYPER_DMABUF_EXPORT ) || + (req->cmd >HYPER_DMABUF_NOTIFY_UNEXPORT )) { +dev_err (hy_drv_priv->dev,"invalid command\n" ); +return -EINVAL ; + } + + req->stat =HYPER_DMABUF_REQ_PROCESSED ; + +/* HYPER_DMABUF_DESTROY requires immediate +* follow up so can't be processed in workqueue +*/ +if (req->cmd ==HYPER_DMABUF_NOTIFY_UNEXPORT ) { +/* destroy sg_list for hyper_dmabuf_id on remote side */ +/* command : HYPER_DMABUF_NOTIFY_UNEXPORT, +* op0~3 : hyper_dmabuf_id +*/ +dev_dbg (hy_drv_priv->dev, +"processing HYPER_DMABUF_NOTIFY_UNEXPORT\n" ); + + imported =hyper_dmabuf_find_imported (hid); + +if (imported) { +/* if anything is still using dma_buf */ +if (imported->importers) { +/* Buffer is still in use, just mark that +* it should not be allowed to export its fd +* anymore. +*/ + imported->valid = false; + }else { +/* No one is using buffer, remove it from +* imported list +*/ +hyper_dmabuf_remove_imported (hid); +kfree (imported); + } + }else { + req->stat =HYPER_DMABUF_REQ_ERROR ; + } + +return req->cmd; + } + +/* synchronous dma_buf_fd export */ +if (req->cmd ==HYPER_DMABUF_EXPORT_FD ) { +/* find a corresponding SGT for the id */ +dev_dbg (hy_drv_priv->dev, +"HYPER_DMABUF_EXPORT_FD for {id:%d key:%d %d %d}\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], hid.rng_key[2 ]); + + exported =hyper_dmabuf_find_exported (hid); + +if (!exported) { +dev_err (hy_drv_priv->dev, +"buffer {id:%d key:%d %d %d} not found\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ]); + + req->stat =HYPER_DMABUF_REQ_ERROR ; + }else if (!exported->valid) { +dev_dbg (hy_drv_priv->dev, +"Buffer no longer valid {id:%d key:%d %d %d}\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ]); + + req->stat =HYPER_DMABUF_REQ_ERROR ; + }else { +dev_dbg (hy_drv_priv->dev, +"Buffer still valid {id:%d key:%d %d %d}\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ]); + + exported->active++; + req->stat =HYPER_DMABUF_REQ_PROCESSED ; + } +return req->cmd; + } + +if (req->cmd ==HYPER_DMABUF_EXPORT_FD_FAILED ) { +dev_dbg (hy_drv_priv->dev, +"HYPER_DMABUF_EXPORT_FD_FAILED for {id:%d key:%d %d %d}\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], hid.rng_key[2 ]); + + exported =hyper_dmabuf_find_exported (hid); + +if (!exported) { +dev_err (hy_drv_priv->dev, +"buffer {id:%d key:%d %d %d} not found\n" , + hid.id, hid.rng_key[0 ], hid.rng_key[1 ], + hid.rng_key[2 ]); + + req->stat =HYPER_DMABUF_REQ_ERROR ; + }else { + exported->active--; + req->stat =HYPER_DMABUF_REQ_PROCESSED ; + } +return req->cmd; + } + +dev_dbg (hy_drv_priv->dev, +"%s: putting request to workqueue\n" , __func__); + temp_req =kmalloc (sizeof (*temp_req),GFP_KERNEL ); + +if (!temp_req) +return -ENOMEM ; + +memcpy (temp_req, req,sizeof (*temp_req)); + + proc =kcalloc (1 ,sizeof (struct cmd_process ),GFP_KERNEL );
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c line 315
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +10
-0+ +if (!proc) { +kfree (temp_req); +return -ENOMEM ; + } + + proc->rq = temp_req; + proc->domid = domid; + +INIT_WORK (&(proc->work), cmd_process_work);
Why do you need to be so asynchronous and schedule a work for processing rather than handle it now?
Quoted 46 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +5
-0+ +queue_work (hy_drv_priv->work_queue, &(proc->work)); + +return req->cmd; +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +35
-0@@ -0,0 +1,87 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_MSG_H__ +#define __HYPER_DMABUF_MSG_H__ + +#define MAX_NUMBER_OF_OPERANDS 8 + +struct hyper_dmabuf_req { + unsigned int req_id; + unsigned int stat; + unsigned int cmd;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h line 45
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +10
-0+unsigned int op[MAX_NUMBER_OF_OPERANDS ]; +}; + +struct hyper_dmabuf_resp { +unsigned int resp_id; +unsigned int stat; +unsigned int cmd; +unsigned int op[MAX_NUMBER_OF_OPERANDS ]; +}; +
The structures above are of size of 11 * sizeof(int) == 44 bytes Can these be aligned to 64 for example,
From Xen POV: these will be sent over the shared ring, which is of PAGE_SIZE size, so 4096 / 44...
Quoted 15 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +15
-0+enum hyper_dmabuf_command { +HYPER_DMABUF_EXPORT =0x10 , +HYPER_DMABUF_EXPORT_FD , +HYPER_DMABUF_EXPORT_FD_FAILED , +HYPER_DMABUF_NOTIFY_UNEXPORT , +}; + +enum hyper_dmabuf_ops { +HYPER_DMABUF_OPS_ATTACH =0x1000 , +HYPER_DMABUF_OPS_DETACH , +HYPER_DMABUF_OPS_MAP , +HYPER_DMABUF_OPS_UNMAP , +HYPER_DMABUF_OPS_RELEASE , +HYPER_DMABUF_OPS_BEGIN_CPU_ACCESS , +HYPER_DMABUF_OPS_END_CPU_ACCESS ,
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h line 70
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +10
-0+HYPER_DMABUF_OPS_KMAP_ATOMIC , +HYPER_DMABUF_OPS_KUNMAP_ATOMIC , +HYPER_DMABUF_OPS_KMAP , +HYPER_DMABUF_OPS_KUNMAP , +HYPER_DMABUF_OPS_MMAP , +HYPER_DMABUF_OPS_VMAP , + HYPER_DMABUF_OPS_VUNMAP, +}; + +enum hyper_dmabuf_req_feedback {
This rather seems to be a status
Quoted 272 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +17
-0+HYPER_DMABUF_REQ_PROCESSED =0x100 , +HYPER_DMABUF_REQ_NEEDS_FOLLOW_UP , +HYPER_DMABUF_REQ_ERROR , +HYPER_DMABUF_REQ_NOT_RESPONDED +}; + +/* create a request packet with given command and operands */ +void hyper_dmabuf_create_req (struct hyper_dmabuf_req *req, +enum hyper_dmabuf_command command, +int *operands); + +/* parse incoming request packet (or response) and take +* appropriate actions for those +*/ +int hyper_dmabuf_msg_parse (int domid,struct hyper_dmabuf_req *req); + +#endif // __HYPER_DMABUF_MSG_H__ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c new file +249
-0@@ -0,0 +1,264 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/dma-buf.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_struct.h" +#include "hyper_dmabuf_ops.h" +#include "hyper_dmabuf_sgl_proc.h" +#include "hyper_dmabuf_id.h" +#include "hyper_dmabuf_msg.h" +#include "hyper_dmabuf_list.h" + +#define WAIT_AFTER_SYNC_REQ 0 +#define REFS_PER_PAGE (PAGE_SIZE/sizeof(grant_ref_t)) + +static int dmabuf_refcount (struct dma_buf *dma_buf) +{ +if ((dma_buf !=NULL ) && (dma_buf->file !=NULL )) +return file_count (dma_buf->file); + +return -EINVAL ; +} + +static int hyper_dmabuf_ops_attach (struct dma_buf *dmabuf, +struct device *dev, +struct dma_buf_attachment *attach) +{ +return 0 ; +} + +static void hyper_dmabuf_ops_detach (struct dma_buf *dmabuf, +struct dma_buf_attachment *attach) +{ +} + +static struct sg_table *hyper_dmabuf_ops_map ( +struct dma_buf_attachment *attachment, +enum dma_data_direction dir) +{ +struct sg_table *st; +struct imported_sgt_info *imported; +struct pages_info *pg_info; + +if (!attachment->dmabuf->priv) +return NULL ; + + imported = (struct imported_sgt_info *)attachment->dmabuf->priv; + +/* extract pages from sgt */ + pg_info =hyper_dmabuf_ext_pgs (imported->sgt); + +if (!pg_info) +return NULL ; + +/* create a new sg_table with extracted pages */ + st =hyper_dmabuf_create_sgt (pg_info->pgs, pg_info->frst_ofst, + pg_info->last_len, pg_info->nents); +if (!st) + goto err_free_sg; + +if (!dma_map_sg (attachment->dev, st->sgl, st->nents, dir)) + goto err_free_sg; + +kfree (pg_info->pgs); +kfree (pg_info); + +return st; + +err_free_sg: +if (st) { +sg_free_table (st); +kfree (st); + } + +kfree (pg_info->pgs); +kfree (pg_info); + +return NULL ; +} + +static void hyper_dmabuf_ops_unmap (struct dma_buf_attachment *attachment, +struct sg_table *sg, +enum dma_data_direction dir) +{ +struct imported_sgt_info *imported; + +if (!attachment->dmabuf->priv) +return ; + + imported = (struct imported_sgt_info *)attachment->dmabuf->priv; + +dma_unmap_sg (attachment->dev, sg->sgl, sg->nents, dir); + +sg_free_table (sg); +kfree (sg); +} + +static void hyper_dmabuf_ops_release (struct dma_buf *dma_buf) +{ +struct imported_sgt_info *imported; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; +int finish; + +if (!dma_buf->priv) +return ; + + imported = (struct imported_sgt_info *)dma_buf->priv; + +if (!dmabuf_refcount (imported->dma_buf)) + imported->dma_buf =NULL ; + + imported->importers--; + +if (imported->importers ==0 ) { + bknd_ops->unmap_shared_pages (&imported->refs_info, + imported->nents); + +if (imported->sgt) { +sg_free_table (imported->sgt); +kfree (imported->sgt); + imported->sgt =NULL ; + } + } + + finish = imported && !imported->valid && + !imported->importers; + +/* +* Check if buffer is still valid and if not remove it +* from imported list. That has to be done after sending +* sync request +*/ +if (finish) { +hyper_dmabuf_remove_imported (imported->hid); +kfree (imported); + } +} + +static int hyper_dmabuf_ops_begin_cpu_access (struct dma_buf *dmabuf, +enum dma_data_direction dir) +{ +return 0 ; +} + +static int hyper_dmabuf_ops_end_cpu_access (struct dma_buf *dmabuf, +enum dma_data_direction dir) +{ +return 0 ; +} + +static void *hyper_dmabuf_ops_kmap_atomic (struct dma_buf *dmabuf, +unsigned long pgnum) +{ +/* TODO: NULL for now. Need to return the addr of mapped region */ +return NULL ; +} + +static void hyper_dmabuf_ops_kunmap_atomic (struct dma_buf *dmabuf, +unsigned long pgnum,void *vaddr) +{ +} + +static void *hyper_dmabuf_ops_kmap (struct dma_buf *dmabuf,unsigned long pgnum) +{ +/* for now NULL.. need to return the address of mapped region */ +return NULL ; +} + +static void hyper_dmabuf_ops_kunmap (struct dma_buf *dmabuf,unsigned long pgnum, +void *vaddr) +{ +} + +static int hyper_dmabuf_ops_mmap (struct dma_buf *dmabuf, +struct vm_area_struct *vma) +{ +return 0 ; +} + +static void *hyper_dmabuf_ops_vmap (struct dma_buf *dmabuf) +{ +return NULL ; +} + +static void hyper_dmabuf_ops_vunmap (struct dma_buf *dmabuf,void *vaddr) +{ +} + +static const struct dma_buf_ops hyper_dmabuf_ops = { + .attach = hyper_dmabuf_ops_attach, + .detach = hyper_dmabuf_ops_detach, + .map_dma_buf = hyper_dmabuf_ops_map, + .unmap_dma_buf = hyper_dmabuf_ops_unmap, + .release = hyper_dmabuf_ops_release, + .begin_cpu_access = (void *)hyper_dmabuf_ops_begin_cpu_access, + .end_cpu_access = (void *)hyper_dmabuf_ops_end_cpu_access, + .map_atomic = hyper_dmabuf_ops_kmap_atomic, + .unmap_atomic = hyper_dmabuf_ops_kunmap_atomic, + .map = hyper_dmabuf_ops_kmap, + .unmap = hyper_dmabuf_ops_kunmap, + .mmap = hyper_dmabuf_ops_mmap, + .vmap = hyper_dmabuf_ops_vmap, + .vunmap = hyper_dmabuf_ops_vunmap, +}; + +/* exporting dmabuf as fd */ +int hyper_dmabuf_export_fd (struct imported_sgt_info *imported,int flags) +{ +int fd =-1 ; + +/* call hyper_dmabuf_export_dmabuf and create +* and bind a handle for it then release +*/ +hyper_dmabuf_export_dma_buf (imported); + +if (imported->dma_buf) + fd =dma_buf_fd (imported->dma_buf, flags); + +return fd;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c line 259
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c new file +10
-0+} + +void hyper_dmabuf_export_dma_buf (struct imported_sgt_info *imported) +{ +DEFINE_DMA_BUF_EXPORT_INFO (exp_info); + + exp_info.ops = &hyper_dmabuf_ops; + +/* multiple of PAGE_SIZE, not considering offset */ + exp_info.size = imported->sgt->nents *PAGE_SIZE ;
Here and below: it can be that PAGE_SIZE differs across VMs
Quoted 97 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c new file +5
-0+ exp_info.flags =/* not sure about flag */ 0 ; + exp_info.priv = imported; + + imported->dma_buf =dma_buf_export (&exp_info); +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h new file +34
-0@@ -0,0 +1,34 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_OPS_H__ +#define __HYPER_DMABUF_OPS_H__ + +int hyper_dmabuf_export_fd (struct imported_sgt_info *imported,int flags); + +void hyper_dmabuf_export_dma_buf (struct imported_sgt_info *imported); + +#endif /* __HYPER_DMABUF_IMP_H__ */ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +46
-0@@ -0,0 +1,256 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/dma-buf.h> +#include "hyper_dmabuf_drv.h" +#include "hyper_dmabuf_struct.h" +#include "hyper_dmabuf_sgl_proc.h" + +#define REFS_PER_PAGE (PAGE_SIZE/sizeof(grant_ref_t)) + +/* return total number of pages referenced by a sgt +* for pre-calculation of # of pages behind a given sgt +*/ +static int get_num_pgs (struct sg_table *sgt) +{ +struct scatterlist *sgl;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c line 56
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +10
-0+int length, i; +/* at least one page */ +int num_pages =1 ; + + sgl = sgt->sgl; + + length = sgl->length -PAGE_SIZE + sgl->offset; + +/* round-up */ + num_pages += ((length +PAGE_SIZE -1 )/PAGE_SIZE );
DIV_ROUND_UP
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c line 63
Quoted 7 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +7
-0+ +for (i =1 ; i < sgt->nents; i++) { + sgl =sg_next (sgl); + +/* round-up */ + num_pages += ((sgl->length +PAGE_SIZE -1 ) / +PAGE_SIZE );/* round-up */
Ditto
Quoted 163 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +163
-0+ } + +return num_pages; +} + +/* extract pages directly from struct sg_table */ +struct pages_info *hyper_dmabuf_ext_pgs (struct sg_table *sgt) +{ +struct pages_info *pg_info; +int i, j, k; +int length; +struct scatterlist *sgl; + + pg_info =kmalloc (sizeof (*pg_info),GFP_KERNEL ); +if (!pg_info) +return NULL ; + + pg_info->pgs =kmalloc_array (get_num_pgs (sgt), +sizeof (struct page *), +GFP_KERNEL ); + +if (!pg_info->pgs) { +kfree (pg_info); +return NULL ; + } + + sgl = sgt->sgl; + + pg_info->nents =1 ; + pg_info->frst_ofst = sgl->offset; + pg_info->pgs[0 ] =sg_page (sgl); + length = sgl->length -PAGE_SIZE + sgl->offset; + i =1 ; + +while (length >0 ) { + pg_info->pgs[i] =nth_page (sg_page (sgl), i); + length -=PAGE_SIZE ; + pg_info->nents++; + i++; + } + +for (j =1 ; j < sgt->nents; j++) { + sgl =sg_next (sgl); + pg_info->pgs[i++] =sg_page (sgl); + length = sgl->length -PAGE_SIZE ; + pg_info->nents++; + k =1 ; + +while (length >0 ) { + pg_info->pgs[i++] =nth_page (sg_page (sgl), k++); + length -=PAGE_SIZE ; + pg_info->nents++; + } + } + +/* +* lenght at that point will be 0 or negative, +* so to calculate last page size just add it to PAGE_SIZE +*/ + pg_info->last_len =PAGE_SIZE + length; + +return pg_info; +} + +/* create sg_table with given pages and other parameters */ +struct sg_table *hyper_dmabuf_create_sgt (struct page **pgs, +int frst_ofst,int last_len, +int nents) +{ +struct sg_table *sgt; +struct scatterlist *sgl; +int i, ret; + + sgt =kmalloc (sizeof (struct sg_table ),GFP_KERNEL ); +if (!sgt) +return NULL ; + + ret =sg_alloc_table (sgt, nents,GFP_KERNEL ); +if (ret) { +if (sgt) { +sg_free_table (sgt); +kfree (sgt); + } + +return NULL ; + } + + sgl = sgt->sgl; + +sg_set_page (sgl, pgs[0 ],PAGE_SIZE -frst_ofst, frst_ofst); + +for (i =1 ; i < nents-1 ; i++) { + sgl =sg_next (sgl); +sg_set_page (sgl, pgs[i],PAGE_SIZE ,0 ); + } + +if (nents >1 )/* more than one page */ { + sgl =sg_next (sgl); +sg_set_page (sgl, pgs[i], last_len,0 ); + } + +return sgt; +} + +int hyper_dmabuf_cleanup_sgt_info (struct exported_sgt_info *exported, +int force) +{ +struct sgt_list *sgtl; +struct attachment_list *attachl; +struct kmap_vaddr_list *va_kmapl; +struct vmap_vaddr_list *va_vmapl; +struct hyper_dmabuf_bknd_ops *bknd_ops = hy_drv_priv->bknd_ops; + +if (!exported) { +dev_err (hy_drv_priv->dev,"invalid hyper_dmabuf_id\n" ); +return -EINVAL ; + } + +/* if force != 1, sgt_info can be released only if +* there's no activity on exported dma-buf on importer +* side. +*/ +if (!force && + exported->active) { +dev_warn (hy_drv_priv->dev, +"dma-buf is used by importer\n" ); + +return -EPERM ; + } + +/* force == 1 is not recommended */ +while (!list_empty (&exported->va_kmapped->list)) { + va_kmapl =list_first_entry (&exported->va_kmapped->list, + struct kmap_vaddr_list, list); + +dma_buf_kunmap (exported->dma_buf,1 , va_kmapl->vaddr); +list_del (&va_kmapl->list); +kfree (va_kmapl); + } + +while (!list_empty (&exported->va_vmapped->list)) { + va_vmapl =list_first_entry (&exported->va_vmapped->list, + struct vmap_vaddr_list, list); + +dma_buf_vunmap (exported->dma_buf, va_vmapl->vaddr); +list_del (&va_vmapl->list); +kfree (va_vmapl); + } + +while (!list_empty (&exported->active_sgts->list)) { + attachl =list_first_entry (&exported->active_attached->list, + struct attachment_list, list); + + sgtl =list_first_entry (&exported->active_sgts->list, + struct sgt_list, list); + +dma_buf_unmap_attachment (attachl->attach, sgtl->sgt, +DMA_BIDIRECTIONAL ); +list_del (&sgtl->list); +kfree (sgtl); + } + +while (!list_empty (&exported->active_sgts->list)) {
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c line 236
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +10
-0+ attachl =list_first_entry (&exported->active_attached->list, + struct attachment_list, list); + +dma_buf_detach (exported->dma_buf, attachl->attach); +list_del (&attachl->list); +kfree (attachl); + } + +/* Start cleanup of buffer in reverse order to exporting */ + bknd_ops->unshare_pages (&exported->refs_info, exported->nents);
is the above synchronous? can it be delayed?
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c line 241
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +5
-0+ +/* unmap dma-buf */ +dma_buf_unmap_attachment (exported->active_attached->attach, + exported->active_sgts->sgt, +DMA_BIDIRECTIONAL );
if the above is asynchronous then this might make troubles as we are unmapping yet shared pages
Quoted 226 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +15
-0+ +/* detatch dma-buf */ +dma_buf_detach (exported->dma_buf, exported->active_attached->attach); + +/* close connection to dma-buf completely */ +dma_buf_put (exported->dma_buf); + exported->dma_buf =NULL ; + +kfree (exported->active_sgts); +kfree (exported->active_attached); +kfree (exported->va_kmapped); +kfree (exported->va_vmapped); + +return 0 ; +}drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h new file +43
-0@@ -0,0 +1,43 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_IMP_H__ +#define __HYPER_DMABUF_IMP_H__ + +/* extract pages directly from struct sg_table */ +struct pages_info *hyper_dmabuf_ext_pgs (struct sg_table *sgt); + +/* create sg_table with given pages and other parameters */ +struct sg_table *hyper_dmabuf_create_sgt (struct page **pgs, +int frst_ofst,int last_len, +int nents); + +int hyper_dmabuf_cleanup_sgt_info (struct exported_sgt_info *exported, +int force); + +void hyper_dmabuf_free_sgt (struct sg_table *sgt); + +#endif /* __HYPER_DMABUF_IMP_H__ */ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h new file +131
-0@@ -0,0 +1,131 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_STRUCT_H__ +#define __HYPER_DMABUF_STRUCT_H__ + +/* stack of mapped sgts */ +struct sgt_list { +struct sg_table *sgt; +struct list_head list; +}; + +/* stack of attachments */ +struct attachment_list { +struct dma_buf_attachment *attach; +struct list_head list; +}; + +/* stack of vaddr mapped via kmap */ +struct kmap_vaddr_list { +void *vaddr; +struct list_head list; +}; + +/* stack of vaddr mapped via vmap */ +struct vmap_vaddr_list { +void *vaddr; +struct list_head list; +}; + +/* Exporter builds pages_info before sharing pages */ +struct pages_info { +int frst_ofst; +int last_len; +int nents; +struct page **pgs; +}; + + +/* Exporter stores references to sgt in a hash table +* Exporter keeps these references for synchronization +* and tracking purposes +*/ +struct exported_sgt_info { +hyper_dmabuf_id_t hid; + +/* VM ID of importer */ +int rdomid; + +struct dma_buf *dma_buf; +int nents; + +/* list for tracking activities on dma_buf */ +struct sgt_list *active_sgts; +struct attachment_list *active_attached; +struct kmap_vaddr_list *va_kmapped; +struct vmap_vaddr_list *va_vmapped; + +/* set to 0 when unexported. Importer doesn't +* do a new mapping of buffer if valid == false +*/ +bool valid; + +/* active == true if the buffer is actively used +* (mapped) by importer +*/ +int active; + +/* hypervisor specific reference data for shared pages */ +void *refs_info; + +struct delayed_work unexport; +bool unexport_sched; + +/* list for file pointers associated with all user space +* application that have exported this same buffer to +* another VM. This needs to be tracked to know whether +* the buffer can be completely freed. +*/ +struct file *filp; +}; + +/* imported_sgt_info contains information about imported DMA_BUF +* this info is kept in IMPORT list and asynchorously retrieved and +* used to map DMA_BUF on importer VM's side upon export fd ioctl +* request from user-space +*/ + +struct imported_sgt_info { +hyper_dmabuf_id_t hid;/* unique id for shared dmabuf imported */ + +/* hypervisor-specific handle to pages */ +int ref_handle; + +/* offset and size info of DMA_BUF */ +int frst_ofst; +int last_len; +int nents; + +struct dma_buf *dma_buf; +struct sg_table *sgt; + +void *refs_info; +bool valid; +int importers; +}; + +#endif /* __HYPER_DMABUF_STRUCT_H__ */ include/uapi/linux/hyper_dmabuf.h new file +19
-0@@ -0,0 +1,87 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
include/uapi/linux/hyper_dmabuf.h line 29
Quoted 10 lines
include/uapi/linux/hyper_dmabuf.h new file +10
-0+* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#ifndef __LINUX_PUBLIC_HYPER_DMABUF_H__ +#define __LINUX_PUBLIC_HYPER_DMABUF_H__ + +typedef struct { + int id;
can this be defined as a union as you seem to store count and vm_id in this field?
Quoted 49 lines
include/uapi/linux/hyper_dmabuf.h new file +48
-0+int rng_key[3 ];/* 12bytes long random number */ +} hyper_dmabuf_id_t; + +#define IOCTL_HYPER_DMABUF_TX_CH_SETUP \ +_IOC(_IOC_NONE, 'G', 0, sizeof(struct ioctl_hyper_dmabuf_tx_ch_setup)) +struct ioctl_hyper_dmabuf_tx_ch_setup { +/* IN parameters */ +/* Remote domain id */ +int remote_domain; +}; + +#define IOCTL_HYPER_DMABUF_RX_CH_SETUP \ +_IOC(_IOC_NONE, 'G', 1, sizeof(struct ioctl_hyper_dmabuf_rx_ch_setup)) +struct ioctl_hyper_dmabuf_rx_ch_setup { +/* IN parameters */ +/* Source domain id */ +int source_domain; +}; + +#define IOCTL_HYPER_DMABUF_EXPORT_REMOTE \ +_IOC(_IOC_NONE, 'G', 2, sizeof(struct ioctl_hyper_dmabuf_export_remote)) +struct ioctl_hyper_dmabuf_export_remote { +/* IN parameters */ +/* DMA buf fd to be exported */ +int dmabuf_fd; +/* Domain id to which buffer should be exported */ +int remote_domain; +/* exported dma buf id */ +hyper_dmabuf_id_t hid; +}; + +#define IOCTL_HYPER_DMABUF_EXPORT_FD \ +_IOC(_IOC_NONE, 'G', 3, sizeof(struct ioctl_hyper_dmabuf_export_fd)) +struct ioctl_hyper_dmabuf_export_fd { +/* IN parameters */ +/* hyper dmabuf id to be imported */ +hyper_dmabuf_id_t hid; +/* flags */ +int flags; +/* OUT parameters */ +/* exported dma buf fd */ +int fd; +}; + +#define IOCTL_HYPER_DMABUF_UNEXPORT \ +_IOC(_IOC_NONE, 'G', 4, sizeof(struct ioctl_hyper_dmabuf_unexport)) +struct ioctl_hyper_dmabuf_unexport { +/* IN parameters */
include/uapi/linux/hyper_dmabuf.h line 87
Quoted 10 lines
include/uapi/linux/hyper_dmabuf.h new file +10
-0+/* hyper dmabuf id to be unexported */ +hyper_dmabuf_id_t hid; +/* delay in ms by which unexport processing will be postponed */ +int delay_ms; +/* OUT parameters */ +/* Status of request */ + int status; +}; + +#endif //__LINUX_PUBLIC_HYPER_DMABUF_H__
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
RFC PATCH v2 1/9 hyper_dmabuf: initial upload of hyper_dmabuf drv core framework
In reply to Dongwon KimUpload of intial version of core framework in hyper_DMABUF driver enabling DMA_BUF exchange between two different VMs in virtualized platform based on Hypervisor such as XEN.
Hyper_DMABUF drv's primary role is to import a DMA_BUF from originator then re-export it to another Linux VM so that it can be mapped and accessed in there.
This driver has two layers, one is so called, "core framework", which contains driver interface and core functions handling export/import of new hyper_DMABUF and its maintenance. This part of the driver is independent from Hypervisor so can work as is with any Hypervisor.
The other layer is called "Hypervisor Backend". This layer represents the interface between "core framework" and actual Hypervisor, handling memory sharing and communication. Not like "core framework", every Hypervisor needs it's own backend interface designed using its native mechanism for memory sharing and inter-VM communication.
This patch contains the first part, "core framework", which consists of 7 source files and 11 header files. Some brief description of these source code are attached below:
hyper_dmabuf_drv.c
- Linux driver interface and initialization/cleaning-up routines
hyper_dmabuf_ioctl.c
- IOCTLs calls for export/import of DMA-BUF comm channel's creation and destruction.
hyper_dmabuf_sgl_proc.c
- Provides methods to managing DMA-BUF for exporing and importing. For exporting, extraction of pages, sharing pages via procedures in "Backend" and notifying importing VM exist. For importing, all operations related to the reconstruction of DMA-BUF (with shared pages) on importer's side are defined.
hyper_dmabuf_ops.c
- Standard DMA-BUF operations for hyper_DMABUF reconstructed on importer's side.
hyper_dmabuf_list.c
- Lists for storing exported and imported hyper_DMABUF to keep track of remote usage of hyper_DMABUF currently being shared.
hyper_dmabuf_msg.c
- Defines messages exchanged between VMs (exporter and importer) and function calls for sending and parsing (when received) those.
hyper_dmabuf_id.c
- Contains methods to generate and manage "hyper_DMABUF id" for each hyper_DMABUF being exported. It is a global handle for a hyper_DMABUF, which another VM needs to know to import it.
hyper_dmabuf_struct.h
- Contains data structures of importer or exporter hyper_DMABUF
include/uapi/linux/hyper_dmabuf.h
- Contains definition of data types and structures referenced by user application to interact with driver
--- drivers/dma-buf/Kconfig | 2 + drivers/dma-buf/Makefile | 1 + drivers/dma-buf/hyper_dmabuf/Kconfig | 23 + drivers/dma-buf/hyper_dmabuf/Makefile | 34 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c | 254 ++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h | 111 ++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c | 135 +++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h | 53 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c | 672 +++++++++++++++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h | 52 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c | 294 +++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h | 73 +++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c | 320 ++++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h | 87 +++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c | 264 ++++++++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h | 34 ++ .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c | 256 ++++++++ .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h | 43 ++ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h | 131 ++++ include/uapi/linux/hyper_dmabuf.h | 87 +++ 20 files changed, 2926 insertions(+) create mode 100644 drivers/dma-buf/hyper_dmabuf/Kconfig create mode 100644 drivers/dma-buf/hyper_dmabuf/Makefile create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h create mode 100644 drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h create mode 100644 include/uapi/linux/hyper_dmabuf.h
drivers/dma-buf/Kconfig +2 -0
@@ -30,4 +30,6 @@ config SW_SYNC WARNING: improper use of this can result in deadlocking kernel drivers from userspace. Intended for test and debug only. +source "drivers/dma-buf/hyper_dmabuf/Kconfig" + endmenu
drivers/dma-buf/Makefile +1 -0
@@ -1,3 +1,4 @@ obj-y := dma-buf.o dma-fence.o dma-fence-array.o reservation.o seqno-fence.o obj-$(CONFIG_SYNC_FILE) += sync_file.o obj-$(CONFIG_SW_SYNC) += sw_sync.o sync_debug.o +obj-$(CONFIG_HYPER_DMABUF) += ./hyper_dmabuf/
drivers/dma-buf/hyper_dmabuf/Kconfig new file +23 -0
@@ -0,0 +1,23 @@ +menu "HYPER_DMABUF" + +config HYPER_DMABUF + tristate "Enables hyper dmabuf driver" + default y + help + This option enables Hyper_DMABUF driver. + + This driver works as abstraction layer that export and import + DMA_BUF from/to another virtual OS running on the same HW platform + powered by a hypervisor + +config HYPER_DMABUF_SYSFS + bool "Enable sysfs information about hyper DMA buffers" + default y + depends on HYPER_DMABUF + help + Expose run-time information about currently imported and exported buffers + registered in EXPORT and IMPORT list in Hyper_DMABUF driver. + + The location of sysfs is under "...." + +endmenu
drivers/dma-buf/hyper_dmabuf/Makefile new file +34 -0
@@ -0,0 +1,34 @@ +TARGET_MODULE:=hyper_dmabuf + +# If we running by kernel building system +ifneq ($(KERNELRELEASE),) + $(TARGET_MODULE)-objs := hyper_dmabuf_drv.o \ + hyper_dmabuf_ioctl.o \ + hyper_dmabuf_list.o \ + hyper_dmabuf_sgl_proc.o \ + hyper_dmabuf_ops.o \ + hyper_dmabuf_msg.o \ + hyper_dmabuf_id.o \ + +obj-$(CONFIG_HYPER_DMABUF) := $(TARGET_MODULE).o + +# If we are running without kernel build system +else +BUILDSYSTEM_DIR?=../../../ +PWD:=$(shell pwd) + +all : +# run kernel build system to make module +$(MAKE) -C $(BUILDSYSTEM_DIR) M=$(PWD) modules + +clean: +# run kernel build system to cleanup in current directory +$(MAKE) -C $(BUILDSYSTEM_DIR) M=$(PWD) clean + +load: + insmod ./$(TARGET_MODULE).ko + +unload: + rmmod ./$(TARGET_MODULE).ko + +endif
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c new file +254 -0
254 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.h new file +111 -0
111 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.c new file +135 -0
135 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_id.h new file +53 -0
53 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c new file +672 -0
672 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.h new file +52 -0
52 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.c new file +294 -0
294 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_list.h new file +73 -0
73 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c new file +320 -0
320 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h new file +87 -0
87 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.c new file +264 -0
264 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ops.h new file +34 -0
@@ -0,0 +1,34 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* SPDX-License-Identifier: (MIT OR GPL-2.0) +* +*/ + +#ifndef __HYPER_DMABUF_OPS_H__ +#define __HYPER_DMABUF_OPS_H__ + +int hyper_dmabuf_export_fd (struct imported_sgt_info *imported,int flags); + +void hyper_dmabuf_export_dma_buf (struct imported_sgt_info *imported); + +#endif /* __HYPER_DMABUF_IMP_H__ */
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c new file +256 -0
256 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.h new file +43 -0
43 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h new file +131 -0
131 lines not on this page · raw message
include/uapi/linux/hyper_dmabuf.h new file +87 -0
87 lines not on this page · raw message
Signature · 7 lines
2.16.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
RFC PATCH v2 2/9 hyper_dmabuf: architecture specification and reference guide
In reply to Dongwon KimReference document for hyper_DMABUF driver
Documentation/hyper-dmabuf-sharing.txt
--- Documentation/hyper-dmabuf-sharing.txt | 734 +++++++++++++++++++++++++++++++++ 1 file changed, 734 insertions(+) create mode 100644 Documentation/hyper-dmabuf-sharing.txt
Documentation/hyper-dmabuf-sharing.txt new file +734 -0
@@ -0,0 +1,734 @@ +Linux Hyper DMABUF Driver + +------------------------------------------------------------------------------ +Section 1. Overview +------------------------------------------------------------------------------ + +Hyper_DMABUF driver is a Linux device driver running on multiple Virtual +achines (VMs), which expands DMA-BUF sharing capability to the VM environment +where multiple different OS instances need to share same physical data without +data-copy across VMs. + +To share a DMA_BUF across VMs, an instance of the Hyper_DMABUF drv on the +exporting VM (so called, “exporter”) imports a local DMA_BUF from the original +producer of the buffer, then re-exports it with an unique ID, hyper_dmabuf_id +for the buffer to the importing VM (so called, “importer”). + +Another instance of the Hyper_DMABUF driver on importer registers +a hyper_dmabuf_id together with reference information for the shared physical +pages associated with the DMA_BUF to its database when the export happens. + +The actual mapping of the DMA_BUF on the importer’s side is done by +the Hyper_DMABUF driver when user space issues the IOCTL command to access +the shared DMA_BUF. The Hyper_DMABUF driver works as both an importing and +exporting driver as is, that is, no special configuration is required. +Consequently, only a single module per VM is needed to enable cross-VM DMA_BUF +exchange. + +------------------------------------------------------------------------------ +Section 2. Architecture +------------------------------------------------------------------------------ + +1. Hyper_DMABUF ID + +hyper_dmabuf_id is a global handle for shared DMA BUFs, which is compatible +across VMs. It is a key used by the importer to retrieve information about +shared Kernel pages behind the DMA_BUF structure from the IMPORT list. When +a DMA_BUF is exported to another domain, its hyper_dmabuf_id and META data +are also kept in the EXPORT list by the exporter for further synchronization +of control over the DMA_BUF. + +hyper_dmabuf_id is “targeted”, meaning it is valid only in exporting (owner of +the buffer) and importing VMs, where the corresponding hyper_dmabuf_id is +stored in their database (EXPORT and IMPORT lists). + +A user-space application specifies the targeted VM id in the user parameter +when it calls the IOCTL command to export shared DMA_BUF to another VM. + +hyper_dmabuf_id_t is a data type for hyper_dmabuf_id. It is defined as 16-byte +data structure, and it contains id and rng_key[3] as elements for +the structure. + +typedef struct { + int id; + int rng_key[3]; /* 12bytes long random number */ +} hyper_dmabuf_id_t; + +The first element in the hyper_dmabuf_id structure, int id is combined data of +a count number generated by the driver running on the exporter and +the exporter’s ID. The VM’s ID is a one byte value and located at the field’s +SB in int id. The remaining three bytes in int id are reserved for a count +number. + +However, there is a limit related to this count number, which is 1000. +Therefore, only little more than a byte starting from the LSB is actually used +for storing this count number. + +#define HYPER_DMABUF_ID_CREATE(domid, id) \ + ((((domid) & 0xFF) << 24) | ((id) & 0xFFFFFF)) + +This limit on the count number directly means the maximum number of DMA BUFs +that can be shared simultaneously by one VM. The second element of +hyper_dmabuf_id, that is int rng_key[3], is an array of three integers. These +numbers are generated by Linux’s native random number generation mechanism. +This field is added to enhance the security of the Hyper DMABUF driver by +maximizing the entropy of hyper_dmabuf_id (that is, preventing it from being +guessed by a security attacker). + +Once DMA_BUF is no longer shared, the hyper_dmabuf_id associated with +the DMA_BUF is released, but the count number in hyper_dmabuf_id is saved in +the ID list for reuse. However, random keys stored in int rng_key[3] are not +reused. Instead, those keys are always filled with freshly generated random +keys for security. + +2. IOCTLs + +a. IOCTL_HYPER_DMABUF_TX_CH_SETUP + +This type of IOCTL is used for initialization of a one-directional transmit +communication channel with a remote domain. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_tx_ch_setup { + /* IN parameters */ + /* Remote domain id */ + int remote_domain; +}; + +b. IOCTL_HYPER_DMABUF_RX_CH_SETUP + +This type of IOCTL is used for initialization of a one-directional receive +communication channel with a remote domain. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_rx_ch_setup { + /* IN parameters */ + /* Source domain id */ + int source_domain; +}; + +c. IOCTL_HYPER_DMABUF_EXPORT_REMOTE + +This type of IOCTL is used to export a DMA BUF to another VM. When a user +space application makes this call to the driver, it extracts Kernel pages +associated with the DMA_BUF, then makes those shared with the importing VM. + +All reference information for this shared pages and hyper_dmabuf_id is +created, then passed to the importing domain through a communications +channel for synchronous registration. In the meantime, the hyper_dmabuf_id +for the shared DMA_BUF is also returned to user-space application. + +This IOCTL can accept a reference to “user-defined” data as well as a FD +for the DMA BUF. This private data is then attached to the DMA BUF and +exported together with it. + +More details regarding this private data can be found in chapter for +“Hyper_DMABUF Private Data”. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_export_remote { + /* IN parameters */ + /* DMA buf fd to be exported */ + int dmabuf_fd; + /* Domain id to which buffer should be exported */ + int remote_domain; + /* exported dma buf id */ + hyper_dmabuf_id_t hid; + /* size of private data */ + int sz_priv; + /* ptr to the private data for Hyper_DMABUF */ + char *priv; +}; + +d. IOCTL_HYPER_DMABUF_EXPORT_FD + +The importing VM uses this IOCTL to import and re-export a shared DMA_BUF +locally to the end-consumer using the standard Linux DMA_BUF framework. +Upon IOCTL call, the Hyper_DMABUF driver finds the reference information +of the shared DMA_BUF with the given hyper_dmabuf_id, then maps all shared +pages in its own Kernel space. The driver then constructs a scatter-gather +list with those mapped pages and creates a brand-new DMA_BUF with the list, +which is eventually exported with a file descriptor to the local consumer. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_export_fd { + /* IN parameters */ + /* hyper dmabuf id to be imported */ + int hyper_dmabuf_id; + /* flags */ + int flags; + /* OUT parameters */ + /* exported dma buf fd */ + int fd; +}; + +e. IOCTL_HYPER_DMABUF_UNEXPORT + +This type of IOCTL is used when it is necessary to terminate the current +sharing of a DMA_BUF. When called, the driver first checks if there are any +consumers actively using the DMA_BUF. Then, it unexports it if it is not +mapped or used by any consumers. Otherwise, it postpones unexporting, but +makes the buffer invalid to prevent any further import of the same DMA_BUF. +DMA_BUF is completely unexported after the last consumer releases it. + +”Unexport” means removing all reference information about the DMA_BUF from the +LISTs and make all pages private again. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_unexport { + /* IN parameters */ + /* hyper dmabuf id to be unexported */ + int hyper_dmabuf_id; + /* delay in ms by which unexport processing will be postponed */ + int delay_ms; + /* OUT parameters */ + /* Status of request */ + int status; +}; + +f. IOCTL_HYPER_DMABUF_QUERY + +This IOCTL is used to retrieve specific information about a DMA_BUF that +is being shared. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_query { + /* in parameters */ + /* hyper dmabuf id to be queried */ + int hyper_dmabuf_id; + /* item to be queried */ + int item; + /* OUT parameters */ + /* output of query */ + /* info can be either value or reference */ + unsigned long info; +}; + +<Available Queries> + +HYPER_DMABUF_QUERY_TYPE + - Return the type of DMA_BUF from the current domain, Exported or Imported. + +HYPER_DMABUF_QUERY_EXPORTER + - Return the exporting domain’s ID of a shared DMA_BUF. + +HYPER_DMABUF_QUERY_IMPORTER + - Return the importing domain’s ID of a shared DMA_BUF. + +HYPER_DMABUF_QUERY_SIZE + - Return the size of a shared DMA_BUF in bytes. + +HYPER_DMABUF_QUERY_BUSY + - Return ‘true’ if a shared DMA_BUF is currently used + (mapped by the end-consumer). + +HYPER_DMABUF_QUERY_UNEXPORTED + - Return ‘true’ if a shared DMA_BUF is not valid anymore + (so it does not allow a new consumer to map it). + +HYPER_DMABUF_QUERY_DELAYED_UNEXPORTED + - Return ‘true’ if a shared DMA_BUF is scheduled to be unexported + (but is still valid) within a fixed time. + +HYPER_DMABUF_QUERY_PRIV_INFO + - Return ‘private’ data attached to shared DMA_BUF to the user space. + ‘unsigned long info’ is the user space pointer for the buffer, where + private data will be copied to. + +HYPER_DMABUF_QUERY_PRIV_INFO_SIZE + - Return the size of the private data attached to the shared DMA_BUF. + +3. Event Polling + +Event-polling can be enabled optionally by selecting the Kernel config option, +Enable event-generation and polling operation under xen/hypervisor in Kernel’s +menuconfig. The event-polling mechanism includes the generation of +an import-event, adding it to the event-queue and providing a notification to +the application so that it can retrieve the event data from the queue. + +For this mechanism, “Poll” and “Read” operations are added to the Hyper_DMABUF +driver. A user application that polls the driver goes into a sleep state until +there is a new event added to the queue. An application uses “Read” to retrieve +event data from the event queue. Event data contains the hyper_dmabuf_id and +the private data of the buffer that has been received by the importer. + +For more information on private data, refer to Section 3.5). +Using this method, it is possible to lower the risk of the hyper_dmabuf_id and +other sensitive information about the shared buffer (for example, meta-data +for shared images) being leaked while being transferred to the importer because +all of this data is shared as “private info” at the driver level. However, +please note there should be a way for the importer to find the correct DMA_BUF +in this case when there are multiple Hyper_DMABUFs being shared simultaneously. +For example, the surface name or the surface ID of a specific rendering surface +needs to be sent to the importer in advance before it is exported in a surface- +sharing use-case. + +Each event data given to the user-space consists of a header and the private +information of the buffer. The data type is defined as follows: + +struct hyper_dmabuf_event_hdr { + int event_type; /* one type only for now - new import */ + hyper_dmabuf_id_t hid; /* hyper_dmabuf_id of specific hyper_dmabuf */ + int size; /* size of data */ +}; + +struct hyper_dmabuf_event_data { + struct hyper_dmabuf_event_hdr hdr; + void *data; /* private data */ +}; + +4. Hyper_DMABUF Private Data + +Each Hyper_DMABUF can come with private data, the size of which can be up to +AX_SIZE_PRIV_DATA (currently 192 byte). This private data is just a chunk of +plain data attached to every Hyper_DMABUF. It is guaranteed to be synchronized +across VMs, exporter and importer. This private data does not have any specific +structure defined at the driver level, so any “user-defined” format or +structure can be used. In addition, there is no dedicated use-case for this +data. It can be used virtually for any purpose. For example, it can be used to +share meta-data such as dimension and color formats for shared images in +a surface sharing model. Another example is when we share protected media +contents. + +This private data can be used to transfer flags related to content protection +information on streamed media to the importer. + +Private data is initially generated when a buffer is exported for the first +time. Then, it is updated whenever the same buffer is re-exported. During the +re-exporting process, the Hyper_DMABUF driver only updates private data on +both sides with new data from user-space since the same buffer already exists +on both the IMPORT LIST and EXPORT LIST. + +There are two different ways to retrieve this private data from user-space. +The first way is to use “Read” on the Hyper_DMABUF driver. “Read” returns the +data of events containing private data of the buffer. The second way is to +make a query to Hyper_DMABUF. There are two query items, +HYPER_DMABUF_QUERY_PRIV_INFO and HYPER_DMABUF_QUERY_PRIV_INFO_SIZE available +for retrieving private data and its size. + +5. Scatter-Gather List Table (SGT) Management + +SGT management is the core part of the Hyper_DMABUF driver that manages an +SGT, a representation of the group of kernel pages associated with a DMA_BUF. +This block includes four different sub-blocks: + +a. Hyper_DMABUF_id Manager + +This ID manager is responsible for generating a hyper_dmabuf_id for an +exported DMA_BUF. When an ID is requested, the ID Manager first checks if +there are any reusable IDs left in the list and returns one of those, +if available. Otherwise, it creates the next count number and returns it +to the caller. + +b. SGT Creator + +The SGT (struct sg_table) contains information about the DMA_BUF such as +references to all kernel pages for the buffer and their connections. The +SGT Creator creates a new SGT on the importer side with pages shared by +the hypervisor. + +c. Kernel Page Extractor + +The Page Extractor extracts pages from a given SGT before those pages +are shared. + +d. List Manager Interface + +The SGT manger also interacts with export and import list managers. It +sends out information (for example, hyper_dmabuf_id, reference, and +DMA_BUF information) about the exported or imported DMA_BUFs to the +list manager. Also, on IOCTL request, it asks the list manager to find +and return the information for a corresponding DMA_BUF in the list. + +6. DMA-BUF Interface + +The DMA-BUF interface provides standard methods to manage DMA_BUFs +reconstructed by the Hyper_DMABUF driver from shared pages. All of the +relevant operations are listed in struct dma_buf_ops. These operations +are standard DMA_BUF operations, therefore they follow standard DMA BUF +protocols. + +Each DMA_BUF operation communicates with the exporter at the end of the +routine for “indirect DMA_BUF synchronization”. + +7. Export/Import List Management + +Whenever a DMA_BUF is shared and exported, its information is added to the +database (EXPORT-list) on the exporting VM. Similarly, information about an +imported DMA_BUF is added to the importing database (IMPORT list) on the +importing VM, when the export happens. + +All of the entries in the lists are needed to manage the exported/imported +DMA_BUF more efficiently. Both lists are implemented as Linux hash tables. +The key to the list is hyper_dmabuf_id and the output is the information of +the DMA_BUF. The List Manager manages all requests from other blocks and +transactions within lists to ensure that all entries are up-to-date and +that the list structure is consistent. + +The List Manager provides basic functionality, such as: + +- Adding to the List +- Removal from the List +- Finding information about a DMA_BUF, given the hyper_dmabuf_id + +8. Page Sharing by Hypercalls + +The Hyper_DMABUF driver assumes that there is a native page-by-page memory +sharing mechanism available on the hypervisor. Referencing a group of pages +that are being shared is what the driver expects from “backend” APIs or the +hypervisor itself. + +For the example, xen backend integrated in current code base utilizes Xen’s +grant-table interface for sharing the underlying kernel pages (struct *page). + +More details about grant-table interface can be found at the following locations: + +https://wiki.xen.org/wiki/Grant_Table +https://xenbits.xen.org/docs/4.6-testing/misc/grant-tables.txt + +9. Message Handling + +The exporter and importer can each create a message that consists of an opcode +(command) and operands (parameters) and send it to each other. + +The message format is defined as: + +struct hyper_dmabuf_req { + unsigned int req_id; /* Sequence number. Used for RING BUF + synchronization */ + unsigned int stat; /* Status.Response from receiver. */ + unsigned int cmd; /* Opcode */ + unsigned int op[MAX_NUMBER_OF_OPERANDS]; /* Operands */ +}; + +The following table gives the list of opcodes: + +<Opcodes in Message to Exporter/Importer> + +HYPER_DMABUF_EXPORT (exporter --> importer) + - Export a DMA_BUF to the importer. The importer registers the corresponding + DMA_BUF in its IMPORT LIST when the message is received. + +HYPER_DMABUF_EXPORT_FD (importer --> exporter) + - Locally exported as FD. The importer sends out this command to the exporter + to notify that the buffer is now locally exported (mapped and used). + +HYPER_DMABUF_EXPORT_FD_FAILED (importer --> exporter) + - Failed while exporting locally. The importer sends out this command to the + exporter to notify the exporter that the EXPORT_FD failed. + +HYPER_DMABUF_NOTIFY_UNEXPORT (exporter --> importer) + - Termination of sharing. The exporter notifies the importer that the DMA_BUF + has been unexported. + +HYPER_DMABUF_OPS_TO_REMOTE (importer --> exporter) + - Not implemented yet. + +HYPER_DMABUF_OPS_TO_SOURCE (exporter --> importer) + - DMA_BUF ops to the exporter, for DMA_BUF upstream synchronization. + Note: Implemented but it is done asynchronously due to performance issues. + +The following table shows the list of operands for each opcode. + +<Operands in Message to Exporter/Importer> + +- HYPER_DMABUF_EXPORT + +op0 to op3 – hyper_dmabuf_id +op4 – number of pages to be shared +op5 – offset of data in the first page +op6 – length of data in the last page +op7 – reference number for the group of shared pages +op8 – size of private data +op9 to (op9+op8) – private data + +- HYPER_DMABUF_EXPORT_FD + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_EXPORT_FD_FAILED + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_NOTIFY_UNEXPORT + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_OPS_TO_REMOTE(Not implemented) + +- HYPER_DMABUF_OPS_TO_SOURCE + +op0 to op3 – hyper_dmabuf_id +op4 – type of DMA_BUF operation + +9. Inter VM (Domain) Communication + +Two different types of inter-domain communication channels are required, +one in kernel space and the other in user space. The communication channel +in user space is for transmitting or receiving the hyper_dmabuf_id. Since +there is no specific security (for example, encryption) involved in the +generation of a global id at the driver level, it is highly recommended that +the customer’s user application set up a very secure channel for exchanging +hyper_dmabuf_id between VMs. + +The communication channel in kernel space is required for exchanging messages +from “message management” block between two VMs. In the current reference +backend for Xen hypervisor, Xen ring-buffer and event-channel mechanisms are +used for message exchange between impoter and exporter. + +10. What are required in hypervisor + +emory sharing and message communication between VMs + +------------------------------------------------------------------------------ +Section 3. Hyper DMABUF Sharing Flow +------------------------------------------------------------------------------ + +1. Exporting + +To export a DMA_BUF to another VM, user space has to call an IOCTL +(IOCTL_HYPER_DMABUF_EXPORT_REMOTE) with a file descriptor for the buffer given +by the original exporter. The Hyper_DMABUF driver maps a DMA_BUF locally, then +issues a hyper_dmabuf_id and SGT for the DMA_BUF, which is registered to the +EXPORT list. Then, all pages for the SGT are extracted and each individual +page is shared via a hypervisor-specific memory sharing mechanism +(for example, in Xen this is grant-table). + +One important requirement on this memory sharing method is that it needs to +create a single integer value that represents the list of pages, which can +then be used by the importer for retrieving the group of shared pages. For +this, the “Backend” in the reference driver utilizes the multiple level +addressing mechanism. + +Once the integer reference to the list of pages is created, the exporter +builds the “export” command and sends it to the importer, then notifies the +importer. + +2. Importing + +The Import process is divided into two sections. One is the registration +of DMA_BUF from the exporter. The other is the actual mapping of the buffer +before accessing the data in the buffer. The former (termed “Registration”) +happens on an export event (that is, the export command with an interrupt) +in the exporter. + +The latter (termed “Mapping”) is done asynchronously when the driver gets the +IOCTL call from user space. When the importer gets an interrupt from the +exporter, it checks the command in the receiving queue and if it is an +“export” command, the registration process is started. It first finds +hyper_dmabuf_id and the integer reference for the shared pages, then stores +all of that information together with the “domain id” of the exporting domain +in the IMPORT LIST. + +In the case where “event-polling” is enabled (Kernel Config - Enable event- +generation and polling operation), a “new sharing available” event is +generated right after the reference info for the new shared DMA_BUF is +registered to the IMPORT LIST. This event is added to the event-queue. + +The user process that polls Hyper_DMABUF driver wakes up when this event-queue +is not empty and is able to read back event data from the queue using the +driver’s “Read” function. Once the user-application calls EXPORT_FD IOCTL with +the proper parameters including hyper_dmabuf_id, the Hyper_DMABUF driver +retrieves information about the matched DMA_BUF from the IMPORT LIST. Then, it +maps all pages shared (referenced by the integer reference) in its kernel +space and creates its own DMA_BUF referencing the same shared pages. After +this, it exports this new DMA_BUF to the other drivers with a file descriptor. +DMA_BUF can then be used just in the same way a local DMA_BUF is. + +3. Indirect Synchronization of DMA_BUF + +Synchronization of a DMA_BUF within a single OS is automatically achieved +because all of importer’s DMA_BUF operations are done using functions defined +on the exporter’s side, which means there is one central place that has full +control over the DMA_BUF. In other words, any primary activities such as +attaching/detaching and mapping/un-mapping are all captured by the exporter, +meaning that the exporter knows basic information such as who is using the +DMA_BUF and how it is being used. This, however, is not applicable if this +sharing is done beyond a single OS because kernel space (where the exporter’s +DMA_BUF operations reside) is simply not visible to the importing VM. + +Therefore, “indirect synchronization” was introduced as an alternative solution, +which is now implemented in the Hyper_DMABUF driver. This technique makes +the exporter create a shadow DMA_BUF when the end-consumer of the buffer maps +the DMA_BUF, then duplicates any DMA_BUF operations performed on +the importer’s side. Through this “indirect synchronization”, the exporter is +able to virtually track all activities done by the consumer (mostly reference +counter) as if those are done in exporter’s local system. + +------------------------------------------------------------------------------ +Section 4. Hypervisor Backend Interface +------------------------------------------------------------------------------ + +The Hyper_DMABUF driver has a standard “Backend” structure that contains +mappings to various functions designed for a specific Hypervisor. Most of +these API functions should provide a low-level implementation of communication +and memory sharing capability that utilize a Hypervisor’s native mechanisms. + +struct hyper_dmabuf_backend_ops { + /* retreiving id of current virtual machine */ + int (*get_vm_id)(void); + /* get pages shared via hypervisor-specific method */ + int (*share_pages)(struct page **, int, int, void **); + /* make shared pages unshared via hypervisor specific method */ + int (*unshare_pages)(void **, int); + /* map remotely shared pages on importer's side via + * hypervisor-specific method + */ + struct page ** (*map_shared_pages)(int, int, int, void **); + /* unmap and free shared pages on importer's side via + * hypervisor-specific method + */ + int (*unmap_shared_pages)(void **, int); + /* initialize communication environment */ + int (*init_comm_env)(void); + /* destroy communication channel */ + void (*destroy_comm)(void); + /* upstream ch setup (receiving and responding) */ + int (*init_rx_ch)(int); + /* downstream ch setup (transmitting and parsing responses) */ + int (*init_tx_ch)(int); + /* send msg via communication ch */ + int (*send_req)(int, struct hyper_dmabuf_req *, int); +}; + +<Hypervisor-specific Backend Structure> + +1. get_vm_id + + Returns the VM (domain) ID + + Input: + + -ID of the current domain + + Output: + + None + +2. share_pages + + Get pages shared via hypervisor-specific method and return one reference + ID that represents the complete list of shared pages + + Input: + + -Array of pages + -ID of importing VM + -Number of pages + -Hypervisor specific Representation of reference info of shared + pages + + Output: + + -Hypervisor specific integer value that represents all of + the shared pages + +3. unshare_pages + + Stop sharing pages + + Input: + + -Hypervisor specific Representation of reference info of shared + pages + -Number of shared pages + + Output: + + 0 + +4. map_shared_pages + + Map shared pages locally using a hypervisor-specific method + + Input: + + -Reference number that represents all of shared pages + -ID of exporting VM, Number of pages + -Reference information for any purpose + + Output: + + -An array of shared pages (struct page**) + +5. unmap_shared_pages + + Unmap shared pages + + Input: + + -Hypervisor specific Representation of reference info of shared pages + + Output: + + -0 (successful) or one of Standard Kernel errors + +6. init_comm_env + + Setup infrastructure needed for communication channel + + Input: + + None + + Output: + + None + +7. destroy_comm + + Cleanup everything done via init_comm_env + + Input: + + None + + Output: + + None + +8. init_rx_ch + + Configure receive channel + + Input: + + -ID of VM on the other side of the channel + + Output: + + -0 (successful) or one of Standard Kernel errors + +9. init_tx_ch + + Configure transmit channel + + Input: + + -ID of VM on the other side of the channel + + Output: + + -0 (success) or one of Standard Kernel errors + +10. send_req + + Send message to other VM + + Input: + + -ID of VM that receives the message + -Message + + Output: + + -0 (success) or one of Standard Kernel errors + +------------------------------------------------------------------------------- +-------------------------------------------------------------------------------
Signature · 6 lines
2.16.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [Xen-devel] [RFC PATCH v2 2/9] hyper_dmabuf: architecture specification and reference guide
Quoted 3 lines · Dongwon Kim
Reference document for
hyper_DMABUFdriver
Documentation/hyper-dmabuf-sharing.txt
This should likely be patch 1 in order for reviewers to have the appropriate context.
Quoted 17 lines
Signed-off-by Dongwon Kim <[email protected]>--- Documentation/hyper-dmabuf-sharing.txt | 734 +++++++++++++++++++++++++++++++++ 1 file changed, 734 insertions(+) create mode 100644 Documentation/hyper-dmabuf-sharing.txtDocumentation/hyper-dmabuf-sharing.txt new file +4
-0@@ -0,0 +1,734 @@ +Linux Hyper DMABUF Driver + +------------------------------------------------------------------------------ +Section 1. Overview
Documentation/hyper-dmabuf-sharing.txt line 14
Quoted 10 lines
Documentation/hyper-dmabuf-sharing.txt new file +10
-0+------------------------------------------------------------------------------ + +Hyper_DMABUF driver is a Linux device driver running on multiple Virtual +achines (VMs), which expands DMA-BUF sharing capability to the VM environment +where multiple different OS instances need to share same physical data without +data-copy across VMs. + +To share a DMA_BUF across VMs, an instance of the Hyper_DMABUF drv on the +exporting VM (so called, “exporter”) imports a local DMA_BUF from the original +producer of the buffer,
The usage of export and import in the above sentence makes it almost impossible to understand.
Quoted 2 lines
then re-exports it with an unique ID,
hyper_dmabuf_id+for the buffer to the importing VM (so called, “importer”).
And this is even worse.
Maybe it would help to have some kind of flow diagram of all this import/export operations, but please read below.
Documentation/hyper-dmabuf-sharing.txt line 26
Quoted 11 lines
Documentation/hyper-dmabuf-sharing.txt new file +11
-0+ +Another instance of the Hyper_DMABUF driver on importer registers +a hyper_dmabuf_id together with reference information for the shared physical +pages associated with the DMA_BUF to its database when the export happens. + +The actual mapping of the DMA_BUF on the importer’s side is done by +the Hyper_DMABUF driver when user space issues the IOCTL command to access +the shared DMA_BUF. The Hyper_DMABUF driver works as both an importing and +exporting driver as is, that is, no special configuration is required. +Consequently, only a single module per VM is needed to enable cross-VM DMA_BUF +exchange.
IMHO I need a more generic view of the problem you are trying to solve in the overview section. I've read the full overview, and I still have no idea why you need all this.
I think the overview should contain at least:
1. A description of the problem you are trying to solve.
2. A high level description of the proposed solution.
3. How the proposed solution deals with the problem described in 1.
This overview is not useful for people that don't know which problem you are trying to solve, like myself.
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [RFC PATCH v2 2/9] hyper_dmabuf: architecture specification and reference guide
Thanks for your comment, Roger
I will try to polish this doc and resubmit.
(I put some comments below as well.)
Quoted 30 lines · Roger Pau Monné
Quoted 3 lines · Dongwon Kim
Reference document for
hyper_DMABUFdriver
Documentation/hyper-dmabuf-sharing.txtThis should likely be patch 1 in order for reviewers to have the appropriate context.
Quoted 22 lines
Signed-off-by Dongwon Kim <[email protected]>--- Documentation/hyper-dmabuf-sharing.txt | 734 +++++++++++++++++++++++++++++++++ 1 file changed, 734 insertions(+) create mode 100644 Documentation/hyper-dmabuf-sharing.txtDocumentation/hyper-dmabuf-sharing.txt new file +9
-0@@ -0,0 +1,734 @@ +Linux Hyper DMABUF Driver + +------------------------------------------------------------------------------ +Section 1. Overview +------------------------------------------------------------------------------ + +Hyper_DMABUF driver is a Linux device driver running on multiple Virtual +achines (VMs), which expands DMA-BUF sharing capability to the VM environment +where multiple different OS instances need to share same physical data without
Quoted 8 lines
Documentation/hyper-dmabuf-sharing.txt
Quoted 5 lines
Documentation/hyper-dmabuf-sharing.txt new file +5
-0+data-copy across VMs. + +To share a DMA_BUF across VMs, an instance of the Hyper_DMABUF drv on the +exporting VM (so called, “exporter”) imports a local DMA_BUF from the original +producer of the buffer,The usage of export and import in the above sentence makes it almost impossible to understand.
Ok, it looks confusing. I think the problem is that those words are used for both local and cross-VMs cases. I will try to clarify those.
Quoted 8 lines
Quoted 3 lines
then re-exports it with an unique ID,
hyper_dmabuf_id+for the buffer to the importing VM (so called, “importer”).And this is even worse.
Maybe it would help to have some kind of flow diagram of all this import/export operations, but please read below.
I will add a diagram here.
Quoted 16 lines
Documentation/hyper-dmabuf-sharing.txt line 26
Quoted 12 lines
Documentation/hyper-dmabuf-sharing.txt new file +11
-0+ +Another instance of the Hyper_DMABUF driver on importer registers +a hyper_dmabuf_id together with reference information for the shared physical +pages associated with the DMA_BUF to its database when the export happens. + +The actual mapping of the DMA_BUF on the importer’s side is done by +the Hyper_DMABUF driver when user space issues the IOCTL command to access +the shared DMA_BUF. The Hyper_DMABUF driver works as both an importing and +exporting driver as is, that is, no special configuration is required. +Consequently, only a single module per VM is needed to enable cross-VM DMA_BUF +exchange.IMHO I need a more generic view of the problem you are trying to solve in the overview section. I've read the full overview, and I still have no idea why you need all this.
I will add some more paragrahs here to give some more generic view (and possibly diagrams) of this driver.
Quoted 9 lines
I think the overview should contain at least:
1. A description of the problem you are trying to solve.
2. A high level description of the proposed solution.
3. How the proposed solution deals with the problem described in 1.This overview is not useful for people that don't know which problem you are trying to solve, like myself.
Thanks again.
Quoted 2 lines
Thanks, Roger.
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Re: [Xen-devel] [RFC PATCH v2 2/9] hyper_dmabuf: architecture specification and reference guide
In reply to Roger Pau MonnéThanks for your comment, Roger
I will try to polish this doc and resubmit.
(I put some comments below as well.)
Quoted 30 lines · Roger Pau Monné
Quoted 3 lines · Dongwon Kim
Reference document for
hyper_DMABUFdriver
Documentation/hyper-dmabuf-sharing.txtThis should likely be patch 1 in order for reviewers to have the appropriate context.
Quoted 22 lines
Signed-off-by Dongwon Kim <[email protected]>--- Documentation/hyper-dmabuf-sharing.txt | 734 +++++++++++++++++++++++++++++++++ 1 file changed, 734 insertions(+) create mode 100644 Documentation/hyper-dmabuf-sharing.txtDocumentation/hyper-dmabuf-sharing.txt new file +9
-0@@ -0,0 +1,734 @@ +Linux Hyper DMABUF Driver + +------------------------------------------------------------------------------ +Section 1. Overview +------------------------------------------------------------------------------ + +Hyper_DMABUF driver is a Linux device driver running on multiple Virtual +achines (VMs), which expands DMA-BUF sharing capability to the VM environment +where multiple different OS instances need to share same physical data without
Quoted 8 lines
Documentation/hyper-dmabuf-sharing.txt
Quoted 5 lines
Documentation/hyper-dmabuf-sharing.txt new file +5
-0+data-copy across VMs. + +To share a DMA_BUF across VMs, an instance of the Hyper_DMABUF drv on the +exporting VM (so called, “exporter”) imports a local DMA_BUF from the original +producer of the buffer,The usage of export and import in the above sentence makes it almost impossible to understand.
Ok, it looks confusing. I think the problem is that those words are used for both local and cross-VMs cases. I will try to clarify those.
Quoted 8 lines
Quoted 3 lines
then re-exports it with an unique ID,
hyper_dmabuf_id+for the buffer to the importing VM (so called, “importer”).And this is even worse.
Maybe it would help to have some kind of flow diagram of all this import/export operations, but please read below.
I will add a diagram here.
Quoted 16 lines
Documentation/hyper-dmabuf-sharing.txt line 26
Quoted 12 lines
Documentation/hyper-dmabuf-sharing.txt new file +11
-0+ +Another instance of the Hyper_DMABUF driver on importer registers +a hyper_dmabuf_id together with reference information for the shared physical +pages associated with the DMA_BUF to its database when the export happens. + +The actual mapping of the DMA_BUF on the importer’s side is done by +the Hyper_DMABUF driver when user space issues the IOCTL command to access +the shared DMA_BUF. The Hyper_DMABUF driver works as both an importing and +exporting driver as is, that is, no special configuration is required. +Consequently, only a single module per VM is needed to enable cross-VM DMA_BUF +exchange.IMHO I need a more generic view of the problem you are trying to solve in the overview section. I've read the full overview, and I still have no idea why you need all this.
I will add some more paragrahs here to give some more generic view (and possibly diagrams) of this driver.
Quoted 9 lines
I think the overview should contain at least:
1. A description of the problem you are trying to solve.
2. A high level description of the proposed solution.
3. How the proposed solution deals with the problem described in 1.This overview is not useful for people that don't know which problem you are trying to solve, like myself.
Thanks again.
Quoted 2 lines
Thanks, Roger.
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [RFC, v2, 2/9] hyper_dmabuf: architecture specification and reference guide
In reply to Dongwon KimSorry for top-posting
Can we have all this go into some header file which
will not only describe the structures/commands/responses/etc,
but will also allow drivers to use those directly without
defining the same one more time in the code? For example,
this is how it is done in Xen [1]. This way, you can keep
documentation and the protocol implementation in sync easily
Quoted 741 lines · Dongwon Kim
Reference document for
hyper_DMABUFdriver
Documentation/hyper-dmabuf-sharing.txtSigned-off-by Dongwon Kim <[email protected]>--- Documentation/hyper-dmabuf-sharing.txt | 734 +++++++++++++++++++++++++++++++++ 1 file changed, 734 insertions(+)create mode 100644
Documentation/hyper-dmabuf-sharing.txtDocumentation/hyper-dmabuf-sharing.txt new file +724
-0@@ -0,0 +1,734 @@ +Linux Hyper DMABUF Driver + +------------------------------------------------------------------------------ +Section 1. Overview +------------------------------------------------------------------------------ + +Hyper_DMABUF driver is a Linux device driver running on multiple Virtual +achines (VMs), which expands DMA-BUF sharing capability to the VM environment +where multiple different OS instances need to share same physical data without +data-copy across VMs. + +To share a DMA_BUF across VMs, an instance of the Hyper_DMABUF drv on the +exporting VM (so called, “exporter”) imports a local DMA_BUF from the original +producer of the buffer, then re-exports it with an unique ID, hyper_dmabuf_id +for the buffer to the importing VM (so called, “importer”). + +Another instance of the Hyper_DMABUF driver on importer registers +a hyper_dmabuf_id together with reference information for the shared physical +pages associated with the DMA_BUF to its database when the export happens. + +The actual mapping of the DMA_BUF on the importer’s side is done by +the Hyper_DMABUF driver when user space issues the IOCTL command to access +the shared DMA_BUF. The Hyper_DMABUF driver works as both an importing and +exporting driver as is, that is, no special configuration is required. +Consequently, only a single module per VM is needed to enable cross-VM DMA_BUF +exchange. + +------------------------------------------------------------------------------ +Section 2. Architecture +------------------------------------------------------------------------------ + +1. Hyper_DMABUF ID + +hyper_dmabuf_id is a global handle for shared DMA BUFs, which is compatible +across VMs. It is a key used by the importer to retrieve information about +shared Kernel pages behind the DMA_BUF structure from the IMPORT list. When +a DMA_BUF is exported to another domain, its hyper_dmabuf_id and META data +are also kept in the EXPORT list by the exporter for further synchronization +of control over the DMA_BUF. + +hyper_dmabuf_id is “targeted”, meaning it is valid only in exporting (owner of +the buffer) and importing VMs, where the corresponding hyper_dmabuf_id is +stored in their database (EXPORT and IMPORT lists). + +A user-space application specifies the targeted VM id in the user parameter +when it calls the IOCTL command to export shared DMA_BUF to another VM. + +hyper_dmabuf_id_t is a data type for hyper_dmabuf_id. It is defined as 16-byte +data structure, and it contains id and rng_key[3] as elements for +the structure. + +typedef struct { + int id; + int rng_key[3]; /* 12bytes long random number */ +} hyper_dmabuf_id_t; + +The first element in the hyper_dmabuf_id structure, int id is combined data of +a count number generated by the driver running on the exporter and +the exporter’s ID. The VM’s ID is a one byte value and located at the field’s +SB in int id. The remaining three bytes in int id are reserved for a count +number. + +However, there is a limit related to this count number, which is 1000. +Therefore, only little more than a byte starting from the LSB is actually used +for storing this count number. + +#define HYPER_DMABUF_ID_CREATE(domid, id) \ + ((((domid) & 0xFF) << 24) | ((id) & 0xFFFFFF)) + +This limit on the count number directly means the maximum number of DMA BUFs +that can be shared simultaneously by one VM. The second element of +hyper_dmabuf_id, that is int rng_key[3], is an array of three integers. These +numbers are generated by Linux’s native random number generation mechanism. +This field is added to enhance the security of the Hyper DMABUF driver by +maximizing the entropy of hyper_dmabuf_id (that is, preventing it from being +guessed by a security attacker). + +Once DMA_BUF is no longer shared, the hyper_dmabuf_id associated with +the DMA_BUF is released, but the count number in hyper_dmabuf_id is saved in +the ID list for reuse. However, random keys stored in int rng_key[3] are not +reused. Instead, those keys are always filled with freshly generated random +keys for security. + +2. IOCTLs + +a. IOCTL_HYPER_DMABUF_TX_CH_SETUP + +This type of IOCTL is used for initialization of a one-directional transmit +communication channel with a remote domain. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_tx_ch_setup { + /* IN parameters */ + /* Remote domain id */ + int remote_domain; +}; + +b. IOCTL_HYPER_DMABUF_RX_CH_SETUP + +This type of IOCTL is used for initialization of a one-directional receive +communication channel with a remote domain. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_rx_ch_setup { + /* IN parameters */ + /* Source domain id */ + int source_domain; +}; + +c. IOCTL_HYPER_DMABUF_EXPORT_REMOTE + +This type of IOCTL is used to export a DMA BUF to another VM. When a user +space application makes this call to the driver, it extracts Kernel pages +associated with the DMA_BUF, then makes those shared with the importing VM. + +All reference information for this shared pages and hyper_dmabuf_id is +created, then passed to the importing domain through a communications +channel for synchronous registration. In the meantime, the hyper_dmabuf_id +for the shared DMA_BUF is also returned to user-space application. + +This IOCTL can accept a reference to “user-defined” data as well as a FD +for the DMA BUF. This private data is then attached to the DMA BUF and +exported together with it. + +More details regarding this private data can be found in chapter for +“Hyper_DMABUF Private Data”. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_export_remote { + /* IN parameters */ + /* DMA buf fd to be exported */ + int dmabuf_fd; + /* Domain id to which buffer should be exported */ + int remote_domain; + /* exported dma buf id */ + hyper_dmabuf_id_t hid; + /* size of private data */ + int sz_priv; + /* ptr to the private data for Hyper_DMABUF */ + char *priv; +}; + +d. IOCTL_HYPER_DMABUF_EXPORT_FD + +The importing VM uses this IOCTL to import and re-export a shared DMA_BUF +locally to the end-consumer using the standard Linux DMA_BUF framework. +Upon IOCTL call, the Hyper_DMABUF driver finds the reference information +of the shared DMA_BUF with the given hyper_dmabuf_id, then maps all shared +pages in its own Kernel space. The driver then constructs a scatter-gather +list with those mapped pages and creates a brand-new DMA_BUF with the list, +which is eventually exported with a file descriptor to the local consumer. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_export_fd { + /* IN parameters */ + /* hyper dmabuf id to be imported */ + int hyper_dmabuf_id; + /* flags */ + int flags; + /* OUT parameters */ + /* exported dma buf fd */ + int fd; +}; + +e. IOCTL_HYPER_DMABUF_UNEXPORT + +This type of IOCTL is used when it is necessary to terminate the current +sharing of a DMA_BUF. When called, the driver first checks if there are any +consumers actively using the DMA_BUF. Then, it unexports it if it is not +mapped or used by any consumers. Otherwise, it postpones unexporting, but +makes the buffer invalid to prevent any further import of the same DMA_BUF. +DMA_BUF is completely unexported after the last consumer releases it. + +”Unexport” means removing all reference information about the DMA_BUF from the +LISTs and make all pages private again. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_unexport { + /* IN parameters */ + /* hyper dmabuf id to be unexported */ + int hyper_dmabuf_id; + /* delay in ms by which unexport processing will be postponed */ + int delay_ms; + /* OUT parameters */ + /* Status of request */ + int status; +}; + +f. IOCTL_HYPER_DMABUF_QUERY + +This IOCTL is used to retrieve specific information about a DMA_BUF that +is being shared. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_query { + /* in parameters */ + /* hyper dmabuf id to be queried */ + int hyper_dmabuf_id; + /* item to be queried */ + int item; + /* OUT parameters */ + /* output of query */ + /* info can be either value or reference */ + unsigned long info; +}; + +<Available Queries> + +HYPER_DMABUF_QUERY_TYPE + - Return the type of DMA_BUF from the current domain, Exported or Imported. + +HYPER_DMABUF_QUERY_EXPORTER + - Return the exporting domain’s ID of a shared DMA_BUF. + +HYPER_DMABUF_QUERY_IMPORTER + - Return the importing domain’s ID of a shared DMA_BUF. + +HYPER_DMABUF_QUERY_SIZE + - Return the size of a shared DMA_BUF in bytes. + +HYPER_DMABUF_QUERY_BUSY + - Return ‘true’ if a shared DMA_BUF is currently used + (mapped by the end-consumer). + +HYPER_DMABUF_QUERY_UNEXPORTED + - Return ‘true’ if a shared DMA_BUF is not valid anymore + (so it does not allow a new consumer to map it). + +HYPER_DMABUF_QUERY_DELAYED_UNEXPORTED + - Return ‘true’ if a shared DMA_BUF is scheduled to be unexported + (but is still valid) within a fixed time. + +HYPER_DMABUF_QUERY_PRIV_INFO + - Return ‘private’ data attached to shared DMA_BUF to the user space. + ‘unsigned long info’ is the user space pointer for the buffer, where + private data will be copied to. + +HYPER_DMABUF_QUERY_PRIV_INFO_SIZE + - Return the size of the private data attached to the shared DMA_BUF. + +3. Event Polling + +Event-polling can be enabled optionally by selecting the Kernel config option, +Enable event-generation and polling operation under xen/hypervisor in Kernel’s +menuconfig. The event-polling mechanism includes the generation of +an import-event, adding it to the event-queue and providing a notification to +the application so that it can retrieve the event data from the queue. + +For this mechanism, “Poll” and “Read” operations are added to the Hyper_DMABUF +driver. A user application that polls the driver goes into a sleep state until +there is a new event added to the queue. An application uses “Read” to retrieve +event data from the event queue. Event data contains the hyper_dmabuf_id and +the private data of the buffer that has been received by the importer. + +For more information on private data, refer to Section 3.5). +Using this method, it is possible to lower the risk of the hyper_dmabuf_id and +other sensitive information about the shared buffer (for example, meta-data +for shared images) being leaked while being transferred to the importer because +all of this data is shared as “private info” at the driver level. However, +please note there should be a way for the importer to find the correct DMA_BUF +in this case when there are multiple Hyper_DMABUFs being shared simultaneously. +For example, the surface name or the surface ID of a specific rendering surface +needs to be sent to the importer in advance before it is exported in a surface- +sharing use-case. + +Each event data given to the user-space consists of a header and the private +information of the buffer. The data type is defined as follows: + +struct hyper_dmabuf_event_hdr { + int event_type; /* one type only for now - new import */ + hyper_dmabuf_id_t hid; /* hyper_dmabuf_id of specific hyper_dmabuf */ + int size; /* size of data */ +}; + +struct hyper_dmabuf_event_data { + struct hyper_dmabuf_event_hdr hdr; + void *data; /* private data */ +}; + +4. Hyper_DMABUF Private Data + +Each Hyper_DMABUF can come with private data, the size of which can be up to +AX_SIZE_PRIV_DATA (currently 192 byte). This private data is just a chunk of +plain data attached to every Hyper_DMABUF. It is guaranteed to be synchronized +across VMs, exporter and importer. This private data does not have any specific +structure defined at the driver level, so any “user-defined” format or +structure can be used. In addition, there is no dedicated use-case for this +data. It can be used virtually for any purpose. For example, it can be used to +share meta-data such as dimension and color formats for shared images in +a surface sharing model. Another example is when we share protected media +contents. + +This private data can be used to transfer flags related to content protection +information on streamed media to the importer. + +Private data is initially generated when a buffer is exported for the first +time. Then, it is updated whenever the same buffer is re-exported. During the +re-exporting process, the Hyper_DMABUF driver only updates private data on +both sides with new data from user-space since the same buffer already exists +on both the IMPORT LIST and EXPORT LIST. + +There are two different ways to retrieve this private data from user-space. +The first way is to use “Read” on the Hyper_DMABUF driver. “Read” returns the +data of events containing private data of the buffer. The second way is to +make a query to Hyper_DMABUF. There are two query items, +HYPER_DMABUF_QUERY_PRIV_INFO and HYPER_DMABUF_QUERY_PRIV_INFO_SIZE available +for retrieving private data and its size. + +5. Scatter-Gather List Table (SGT) Management + +SGT management is the core part of the Hyper_DMABUF driver that manages an +SGT, a representation of the group of kernel pages associated with a DMA_BUF. +This block includes four different sub-blocks: + +a. Hyper_DMABUF_id Manager + +This ID manager is responsible for generating a hyper_dmabuf_id for an +exported DMA_BUF. When an ID is requested, the ID Manager first checks if +there are any reusable IDs left in the list and returns one of those, +if available. Otherwise, it creates the next count number and returns it +to the caller. + +b. SGT Creator + +The SGT (struct sg_table) contains information about the DMA_BUF such as +references to all kernel pages for the buffer and their connections. The +SGT Creator creates a new SGT on the importer side with pages shared by +the hypervisor. + +c. Kernel Page Extractor + +The Page Extractor extracts pages from a given SGT before those pages +are shared. + +d. List Manager Interface + +The SGT manger also interacts with export and import list managers. It +sends out information (for example, hyper_dmabuf_id, reference, and +DMA_BUF information) about the exported or imported DMA_BUFs to the +list manager. Also, on IOCTL request, it asks the list manager to find +and return the information for a corresponding DMA_BUF in the list. + +6. DMA-BUF Interface + +The DMA-BUF interface provides standard methods to manage DMA_BUFs +reconstructed by the Hyper_DMABUF driver from shared pages. All of the +relevant operations are listed in struct dma_buf_ops. These operations +are standard DMA_BUF operations, therefore they follow standard DMA BUF +protocols. + +Each DMA_BUF operation communicates with the exporter at the end of the +routine for “indirect DMA_BUF synchronization”. + +7. Export/Import List Management + +Whenever a DMA_BUF is shared and exported, its information is added to the +database (EXPORT-list) on the exporting VM. Similarly, information about an +imported DMA_BUF is added to the importing database (IMPORT list) on the +importing VM, when the export happens. + +All of the entries in the lists are needed to manage the exported/imported +DMA_BUF more efficiently. Both lists are implemented as Linux hash tables. +The key to the list is hyper_dmabuf_id and the output is the information of +the DMA_BUF. The List Manager manages all requests from other blocks and +transactions within lists to ensure that all entries are up-to-date and +that the list structure is consistent. + +The List Manager provides basic functionality, such as: + +- Adding to the List +- Removal from the List +- Finding information about a DMA_BUF, given the hyper_dmabuf_id + +8. Page Sharing by Hypercalls + +The Hyper_DMABUF driver assumes that there is a native page-by-page memory +sharing mechanism available on the hypervisor. Referencing a group of pages +that are being shared is what the driver expects from “backend” APIs or the +hypervisor itself. + +For the example, xen backend integrated in current code base utilizes Xen’s +grant-table interface for sharing the underlying kernel pages (struct *page). + +More details about grant-table interface can be found at the following locations: + +https://wiki.xen.org/wiki/Grant_Table +https://xenbits.xen.org/docs/4.6-testing/misc/grant-tables.txt + +9. Message Handling + +The exporter and importer can each create a message that consists of an opcode +(command) and operands (parameters) and send it to each other. + +The message format is defined as: + +struct hyper_dmabuf_req { + unsigned int req_id; /* Sequence number. Used for RING BUF + synchronization */ + unsigned int stat; /* Status.Response from receiver. */ + unsigned int cmd; /* Opcode */ + unsigned int op[MAX_NUMBER_OF_OPERANDS]; /* Operands */ +}; + +The following table gives the list of opcodes: + +<Opcodes in Message to Exporter/Importer> + +HYPER_DMABUF_EXPORT (exporter --> importer) + - Export a DMA_BUF to the importer. The importer registers the corresponding + DMA_BUF in its IMPORT LIST when the message is received. + +HYPER_DMABUF_EXPORT_FD (importer --> exporter) + - Locally exported as FD. The importer sends out this command to the exporter + to notify that the buffer is now locally exported (mapped and used). + +HYPER_DMABUF_EXPORT_FD_FAILED (importer --> exporter) + - Failed while exporting locally. The importer sends out this command to the + exporter to notify the exporter that the EXPORT_FD failed. + +HYPER_DMABUF_NOTIFY_UNEXPORT (exporter --> importer) + - Termination of sharing. The exporter notifies the importer that the DMA_BUF + has been unexported. + +HYPER_DMABUF_OPS_TO_REMOTE (importer --> exporter) + - Not implemented yet. + +HYPER_DMABUF_OPS_TO_SOURCE (exporter --> importer) + - DMA_BUF ops to the exporter, for DMA_BUF upstream synchronization. + Note: Implemented but it is done asynchronously due to performance issues. + +The following table shows the list of operands for each opcode. + +<Operands in Message to Exporter/Importer> + +- HYPER_DMABUF_EXPORT + +op0 to op3 – hyper_dmabuf_id +op4 – number of pages to be shared +op5 – offset of data in the first page +op6 – length of data in the last page +op7 – reference number for the group of shared pages +op8 – size of private data +op9 to (op9+op8) – private data + +- HYPER_DMABUF_EXPORT_FD + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_EXPORT_FD_FAILED + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_NOTIFY_UNEXPORT + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_OPS_TO_REMOTE(Not implemented) + +- HYPER_DMABUF_OPS_TO_SOURCE + +op0 to op3 – hyper_dmabuf_id +op4 – type of DMA_BUF operation + +9. Inter VM (Domain) Communication + +Two different types of inter-domain communication channels are required, +one in kernel space and the other in user space. The communication channel +in user space is for transmitting or receiving the hyper_dmabuf_id. Since +there is no specific security (for example, encryption) involved in the +generation of a global id at the driver level, it is highly recommended that +the customer’s user application set up a very secure channel for exchanging +hyper_dmabuf_id between VMs. + +The communication channel in kernel space is required for exchanging messages +from “message management” block between two VMs. In the current reference +backend for Xen hypervisor, Xen ring-buffer and event-channel mechanisms are +used for message exchange between impoter and exporter. + +10. What are required in hypervisor + +emory sharing and message communication between VMs + +------------------------------------------------------------------------------ +Section 3. Hyper DMABUF Sharing Flow +------------------------------------------------------------------------------ + +1. Exporting + +To export a DMA_BUF to another VM, user space has to call an IOCTL +(IOCTL_HYPER_DMABUF_EXPORT_REMOTE) with a file descriptor for the buffer given +by the original exporter. The Hyper_DMABUF driver maps a DMA_BUF locally, then +issues a hyper_dmabuf_id and SGT for the DMA_BUF, which is registered to the +EXPORT list. Then, all pages for the SGT are extracted and each individual +page is shared via a hypervisor-specific memory sharing mechanism +(for example, in Xen this is grant-table). + +One important requirement on this memory sharing method is that it needs to +create a single integer value that represents the list of pages, which can +then be used by the importer for retrieving the group of shared pages. For +this, the “Backend” in the reference driver utilizes the multiple level +addressing mechanism. + +Once the integer reference to the list of pages is created, the exporter +builds the “export” command and sends it to the importer, then notifies the +importer. + +2. Importing + +The Import process is divided into two sections. One is the registration +of DMA_BUF from the exporter. The other is the actual mapping of the buffer +before accessing the data in the buffer. The former (termed “Registration”) +happens on an export event (that is, the export command with an interrupt) +in the exporter. + +The latter (termed “Mapping”) is done asynchronously when the driver gets the +IOCTL call from user space. When the importer gets an interrupt from the +exporter, it checks the command in the receiving queue and if it is an +“export” command, the registration process is started. It first finds +hyper_dmabuf_id and the integer reference for the shared pages, then stores +all of that information together with the “domain id” of the exporting domain +in the IMPORT LIST. + +In the case where “event-polling” is enabled (Kernel Config - Enable event- +generation and polling operation), a “new sharing available” event is +generated right after the reference info for the new shared DMA_BUF is +registered to the IMPORT LIST. This event is added to the event-queue. + +The user process that polls Hyper_DMABUF driver wakes up when this event-queue +is not empty and is able to read back event data from the queue using the +driver’s “Read” function. Once the user-application calls EXPORT_FD IOCTL with +the proper parameters including hyper_dmabuf_id, the Hyper_DMABUF driver +retrieves information about the matched DMA_BUF from the IMPORT LIST. Then, it +maps all pages shared (referenced by the integer reference) in its kernel +space and creates its own DMA_BUF referencing the same shared pages. After +this, it exports this new DMA_BUF to the other drivers with a file descriptor. +DMA_BUF can then be used just in the same way a local DMA_BUF is. + +3. Indirect Synchronization of DMA_BUF + +Synchronization of a DMA_BUF within a single OS is automatically achieved +because all of importer’s DMA_BUF operations are done using functions defined +on the exporter’s side, which means there is one central place that has full +control over the DMA_BUF. In other words, any primary activities such as +attaching/detaching and mapping/un-mapping are all captured by the exporter, +meaning that the exporter knows basic information such as who is using the +DMA_BUF and how it is being used. This, however, is not applicable if this +sharing is done beyond a single OS because kernel space (where the exporter’s +DMA_BUF operations reside) is simply not visible to the importing VM. + +Therefore, “indirect synchronization” was introduced as an alternative solution, +which is now implemented in the Hyper_DMABUF driver. This technique makes +the exporter create a shadow DMA_BUF when the end-consumer of the buffer maps +the DMA_BUF, then duplicates any DMA_BUF operations performed on +the importer’s side. Through this “indirect synchronization”, the exporter is +able to virtually track all activities done by the consumer (mostly reference +counter) as if those are done in exporter’s local system. + +------------------------------------------------------------------------------ +Section 4. Hypervisor Backend Interface +------------------------------------------------------------------------------ + +The Hyper_DMABUF driver has a standard “Backend” structure that contains +mappings to various functions designed for a specific Hypervisor. Most of +these API functions should provide a low-level implementation of communication +and memory sharing capability that utilize a Hypervisor’s native mechanisms. + +struct hyper_dmabuf_backend_ops { + /* retreiving id of current virtual machine */ + int (*get_vm_id)(void); + /* get pages shared via hypervisor-specific method */ + int (*share_pages)(struct page **, int, int, void **); + /* make shared pages unshared via hypervisor specific method */ + int (*unshare_pages)(void **, int); + /* map remotely shared pages on importer's side via + * hypervisor-specific method + */ + struct page ** (*map_shared_pages)(int, int, int, void **); + /* unmap and free shared pages on importer's side via + * hypervisor-specific method + */ + int (*unmap_shared_pages)(void **, int); + /* initialize communication environment */ + int (*init_comm_env)(void); + /* destroy communication channel */ + void (*destroy_comm)(void); + /* upstream ch setup (receiving and responding) */ + int (*init_rx_ch)(int); + /* downstream ch setup (transmitting and parsing responses) */ + int (*init_tx_ch)(int); + /* send msg via communication ch */ + int (*send_req)(int, struct hyper_dmabuf_req *, int); +}; + +<Hypervisor-specific Backend Structure> + +1. get_vm_id + + Returns the VM (domain) ID + + Input: + + -ID of the current domain + + Output: + + None + +2. share_pages + + Get pages shared via hypervisor-specific method and return one reference + ID that represents the complete list of shared pages + + Input: + + -Array of pages + -ID of importing VM + -Number of pages + -Hypervisor specific Representation of reference info of shared + pages + + Output: + + -Hypervisor specific integer value that represents all of + the shared pages + +3. unshare_pages + + Stop sharing pages + + Input: + + -Hypervisor specific Representation of reference info of shared + pages + -Number of shared pages + + Output: + + 0 + +4. map_shared_pages + + Map shared pages locally using a hypervisor-specific method + + Input: + + -Reference number that represents all of shared pages + -ID of exporting VM, Number of pages + -Reference information for any purpose + + Output: + + -An array of shared pages (struct page**) + +5. unmap_shared_pages + + Unmap shared pages + + Input: + + -Hypervisor specific Representation of reference info of shared pages + + Output: + + -0 (successful) or one of Standard Kernel errors + +6. init_comm_env + + Setup infrastructure needed for communication channel + + Input: + + None + + Output: + + None + +7. destroy_comm + + Cleanup everything done via init_comm_env + + Input: + + None + + Output: + + None + +8. init_rx_ch + + Configure receive channel + + Input: + + -ID of VM on the other side of the channel + + Output: + + -0 (successful) or one of Standard Kernel errors + +9. init_tx_ch + + Configure transmit channel + + Input: + + -ID of VM on the other side of the channel + + Output: + + -0 (success) or one of Standard Kernel errors + +10. send_req + + Send message to other VM + + Input:
Documentation/hyper-dmabuf-sharing.txt line 734
Quoted 10 lines
Documentation/hyper-dmabuf-sharing.txt new file +10
-0+ + -ID of VM that receives the message + -Message + + Output: + + -0 (success) or one of Standard Kernel errors + +------------------------------------------------------------------------------- +-------------------------------------------------------------------------------
[1] https://elixir.bootlin.com/linux/v4.16.1/source/include/xen/interface/io/kbdif.h
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [RFC, v2, 2/9] hyper_dmabuf: architecture specification and reference guide
In reply to Dongwon KimSorry for top-posting
Can we have all this go into some header file which
will not only describe the structures/commands/responses/etc,
but will also allow drivers to use those directly without
defining the same one more time in the code? For example,
this is how it is done in Xen [1]. This way, you can keep
documentation and the protocol implementation in sync easily
Quoted 741 lines · Dongwon Kim
Reference document for
hyper_DMABUFdriver
Documentation/hyper-dmabuf-sharing.txtSigned-off-by Dongwon Kim <[email protected]>--- Documentation/hyper-dmabuf-sharing.txt | 734 +++++++++++++++++++++++++++++++++ 1 file changed, 734 insertions(+)create mode 100644
Documentation/hyper-dmabuf-sharing.txtDocumentation/hyper-dmabuf-sharing.txt new file +724
-0@@ -0,0 +1,734 @@ +Linux Hyper DMABUF Driver + +------------------------------------------------------------------------------ +Section 1. Overview +------------------------------------------------------------------------------ + +Hyper_DMABUF driver is a Linux device driver running on multiple Virtual +achines (VMs), which expands DMA-BUF sharing capability to the VM environment +where multiple different OS instances need to share same physical data without +data-copy across VMs. + +To share a DMA_BUF across VMs, an instance of the Hyper_DMABUF drv on the +exporting VM (so called, “exporter”) imports a local DMA_BUF from the original +producer of the buffer, then re-exports it with an unique ID, hyper_dmabuf_id +for the buffer to the importing VM (so called, “importer”). + +Another instance of the Hyper_DMABUF driver on importer registers +a hyper_dmabuf_id together with reference information for the shared physical +pages associated with the DMA_BUF to its database when the export happens. + +The actual mapping of the DMA_BUF on the importer’s side is done by +the Hyper_DMABUF driver when user space issues the IOCTL command to access +the shared DMA_BUF. The Hyper_DMABUF driver works as both an importing and +exporting driver as is, that is, no special configuration is required. +Consequently, only a single module per VM is needed to enable cross-VM DMA_BUF +exchange. + +------------------------------------------------------------------------------ +Section 2. Architecture +------------------------------------------------------------------------------ + +1. Hyper_DMABUF ID + +hyper_dmabuf_id is a global handle for shared DMA BUFs, which is compatible +across VMs. It is a key used by the importer to retrieve information about +shared Kernel pages behind the DMA_BUF structure from the IMPORT list. When +a DMA_BUF is exported to another domain, its hyper_dmabuf_id and META data +are also kept in the EXPORT list by the exporter for further synchronization +of control over the DMA_BUF. + +hyper_dmabuf_id is “targeted”, meaning it is valid only in exporting (owner of +the buffer) and importing VMs, where the corresponding hyper_dmabuf_id is +stored in their database (EXPORT and IMPORT lists). + +A user-space application specifies the targeted VM id in the user parameter +when it calls the IOCTL command to export shared DMA_BUF to another VM. + +hyper_dmabuf_id_t is a data type for hyper_dmabuf_id. It is defined as 16-byte +data structure, and it contains id and rng_key[3] as elements for +the structure. + +typedef struct { + int id; + int rng_key[3]; /* 12bytes long random number */ +} hyper_dmabuf_id_t; + +The first element in the hyper_dmabuf_id structure, int id is combined data of +a count number generated by the driver running on the exporter and +the exporter’s ID. The VM’s ID is a one byte value and located at the field’s +SB in int id. The remaining three bytes in int id are reserved for a count +number. + +However, there is a limit related to this count number, which is 1000. +Therefore, only little more than a byte starting from the LSB is actually used +for storing this count number. + +#define HYPER_DMABUF_ID_CREATE(domid, id) \ + ((((domid) & 0xFF) << 24) | ((id) & 0xFFFFFF)) + +This limit on the count number directly means the maximum number of DMA BUFs +that can be shared simultaneously by one VM. The second element of +hyper_dmabuf_id, that is int rng_key[3], is an array of three integers. These +numbers are generated by Linux’s native random number generation mechanism. +This field is added to enhance the security of the Hyper DMABUF driver by +maximizing the entropy of hyper_dmabuf_id (that is, preventing it from being +guessed by a security attacker). + +Once DMA_BUF is no longer shared, the hyper_dmabuf_id associated with +the DMA_BUF is released, but the count number in hyper_dmabuf_id is saved in +the ID list for reuse. However, random keys stored in int rng_key[3] are not +reused. Instead, those keys are always filled with freshly generated random +keys for security. + +2. IOCTLs + +a. IOCTL_HYPER_DMABUF_TX_CH_SETUP + +This type of IOCTL is used for initialization of a one-directional transmit +communication channel with a remote domain. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_tx_ch_setup { + /* IN parameters */ + /* Remote domain id */ + int remote_domain; +}; + +b. IOCTL_HYPER_DMABUF_RX_CH_SETUP + +This type of IOCTL is used for initialization of a one-directional receive +communication channel with a remote domain. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_rx_ch_setup { + /* IN parameters */ + /* Source domain id */ + int source_domain; +}; + +c. IOCTL_HYPER_DMABUF_EXPORT_REMOTE + +This type of IOCTL is used to export a DMA BUF to another VM. When a user +space application makes this call to the driver, it extracts Kernel pages +associated with the DMA_BUF, then makes those shared with the importing VM. + +All reference information for this shared pages and hyper_dmabuf_id is +created, then passed to the importing domain through a communications +channel for synchronous registration. In the meantime, the hyper_dmabuf_id +for the shared DMA_BUF is also returned to user-space application. + +This IOCTL can accept a reference to “user-defined” data as well as a FD +for the DMA BUF. This private data is then attached to the DMA BUF and +exported together with it. + +More details regarding this private data can be found in chapter for +“Hyper_DMABUF Private Data”. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_export_remote { + /* IN parameters */ + /* DMA buf fd to be exported */ + int dmabuf_fd; + /* Domain id to which buffer should be exported */ + int remote_domain; + /* exported dma buf id */ + hyper_dmabuf_id_t hid; + /* size of private data */ + int sz_priv; + /* ptr to the private data for Hyper_DMABUF */ + char *priv; +}; + +d. IOCTL_HYPER_DMABUF_EXPORT_FD + +The importing VM uses this IOCTL to import and re-export a shared DMA_BUF +locally to the end-consumer using the standard Linux DMA_BUF framework. +Upon IOCTL call, the Hyper_DMABUF driver finds the reference information +of the shared DMA_BUF with the given hyper_dmabuf_id, then maps all shared +pages in its own Kernel space. The driver then constructs a scatter-gather +list with those mapped pages and creates a brand-new DMA_BUF with the list, +which is eventually exported with a file descriptor to the local consumer. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_export_fd { + /* IN parameters */ + /* hyper dmabuf id to be imported */ + int hyper_dmabuf_id; + /* flags */ + int flags; + /* OUT parameters */ + /* exported dma buf fd */ + int fd; +}; + +e. IOCTL_HYPER_DMABUF_UNEXPORT + +This type of IOCTL is used when it is necessary to terminate the current +sharing of a DMA_BUF. When called, the driver first checks if there are any +consumers actively using the DMA_BUF. Then, it unexports it if it is not +mapped or used by any consumers. Otherwise, it postpones unexporting, but +makes the buffer invalid to prevent any further import of the same DMA_BUF. +DMA_BUF is completely unexported after the last consumer releases it. + +”Unexport” means removing all reference information about the DMA_BUF from the +LISTs and make all pages private again. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_unexport { + /* IN parameters */ + /* hyper dmabuf id to be unexported */ + int hyper_dmabuf_id; + /* delay in ms by which unexport processing will be postponed */ + int delay_ms; + /* OUT parameters */ + /* Status of request */ + int status; +}; + +f. IOCTL_HYPER_DMABUF_QUERY + +This IOCTL is used to retrieve specific information about a DMA_BUF that +is being shared. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_query { + /* in parameters */ + /* hyper dmabuf id to be queried */ + int hyper_dmabuf_id; + /* item to be queried */ + int item; + /* OUT parameters */ + /* output of query */ + /* info can be either value or reference */ + unsigned long info; +}; + +<Available Queries> + +HYPER_DMABUF_QUERY_TYPE + - Return the type of DMA_BUF from the current domain, Exported or Imported. + +HYPER_DMABUF_QUERY_EXPORTER + - Return the exporting domain’s ID of a shared DMA_BUF. + +HYPER_DMABUF_QUERY_IMPORTER + - Return the importing domain’s ID of a shared DMA_BUF. + +HYPER_DMABUF_QUERY_SIZE + - Return the size of a shared DMA_BUF in bytes. + +HYPER_DMABUF_QUERY_BUSY + - Return ‘true’ if a shared DMA_BUF is currently used + (mapped by the end-consumer). + +HYPER_DMABUF_QUERY_UNEXPORTED + - Return ‘true’ if a shared DMA_BUF is not valid anymore + (so it does not allow a new consumer to map it). + +HYPER_DMABUF_QUERY_DELAYED_UNEXPORTED + - Return ‘true’ if a shared DMA_BUF is scheduled to be unexported + (but is still valid) within a fixed time. + +HYPER_DMABUF_QUERY_PRIV_INFO + - Return ‘private’ data attached to shared DMA_BUF to the user space. + ‘unsigned long info’ is the user space pointer for the buffer, where + private data will be copied to. + +HYPER_DMABUF_QUERY_PRIV_INFO_SIZE + - Return the size of the private data attached to the shared DMA_BUF. + +3. Event Polling + +Event-polling can be enabled optionally by selecting the Kernel config option, +Enable event-generation and polling operation under xen/hypervisor in Kernel’s +menuconfig. The event-polling mechanism includes the generation of +an import-event, adding it to the event-queue and providing a notification to +the application so that it can retrieve the event data from the queue. + +For this mechanism, “Poll” and “Read” operations are added to the Hyper_DMABUF +driver. A user application that polls the driver goes into a sleep state until +there is a new event added to the queue. An application uses “Read” to retrieve +event data from the event queue. Event data contains the hyper_dmabuf_id and +the private data of the buffer that has been received by the importer. + +For more information on private data, refer to Section 3.5). +Using this method, it is possible to lower the risk of the hyper_dmabuf_id and +other sensitive information about the shared buffer (for example, meta-data +for shared images) being leaked while being transferred to the importer because +all of this data is shared as “private info” at the driver level. However, +please note there should be a way for the importer to find the correct DMA_BUF +in this case when there are multiple Hyper_DMABUFs being shared simultaneously. +For example, the surface name or the surface ID of a specific rendering surface +needs to be sent to the importer in advance before it is exported in a surface- +sharing use-case. + +Each event data given to the user-space consists of a header and the private +information of the buffer. The data type is defined as follows: + +struct hyper_dmabuf_event_hdr { + int event_type; /* one type only for now - new import */ + hyper_dmabuf_id_t hid; /* hyper_dmabuf_id of specific hyper_dmabuf */ + int size; /* size of data */ +}; + +struct hyper_dmabuf_event_data { + struct hyper_dmabuf_event_hdr hdr; + void *data; /* private data */ +}; + +4. Hyper_DMABUF Private Data + +Each Hyper_DMABUF can come with private data, the size of which can be up to +AX_SIZE_PRIV_DATA (currently 192 byte). This private data is just a chunk of +plain data attached to every Hyper_DMABUF. It is guaranteed to be synchronized +across VMs, exporter and importer. This private data does not have any specific +structure defined at the driver level, so any “user-defined” format or +structure can be used. In addition, there is no dedicated use-case for this +data. It can be used virtually for any purpose. For example, it can be used to +share meta-data such as dimension and color formats for shared images in +a surface sharing model. Another example is when we share protected media +contents. + +This private data can be used to transfer flags related to content protection +information on streamed media to the importer. + +Private data is initially generated when a buffer is exported for the first +time. Then, it is updated whenever the same buffer is re-exported. During the +re-exporting process, the Hyper_DMABUF driver only updates private data on +both sides with new data from user-space since the same buffer already exists +on both the IMPORT LIST and EXPORT LIST. + +There are two different ways to retrieve this private data from user-space. +The first way is to use “Read” on the Hyper_DMABUF driver. “Read” returns the +data of events containing private data of the buffer. The second way is to +make a query to Hyper_DMABUF. There are two query items, +HYPER_DMABUF_QUERY_PRIV_INFO and HYPER_DMABUF_QUERY_PRIV_INFO_SIZE available +for retrieving private data and its size. + +5. Scatter-Gather List Table (SGT) Management + +SGT management is the core part of the Hyper_DMABUF driver that manages an +SGT, a representation of the group of kernel pages associated with a DMA_BUF. +This block includes four different sub-blocks: + +a. Hyper_DMABUF_id Manager + +This ID manager is responsible for generating a hyper_dmabuf_id for an +exported DMA_BUF. When an ID is requested, the ID Manager first checks if +there are any reusable IDs left in the list and returns one of those, +if available. Otherwise, it creates the next count number and returns it +to the caller. + +b. SGT Creator + +The SGT (struct sg_table) contains information about the DMA_BUF such as +references to all kernel pages for the buffer and their connections. The +SGT Creator creates a new SGT on the importer side with pages shared by +the hypervisor. + +c. Kernel Page Extractor + +The Page Extractor extracts pages from a given SGT before those pages +are shared. + +d. List Manager Interface + +The SGT manger also interacts with export and import list managers. It +sends out information (for example, hyper_dmabuf_id, reference, and +DMA_BUF information) about the exported or imported DMA_BUFs to the +list manager. Also, on IOCTL request, it asks the list manager to find +and return the information for a corresponding DMA_BUF in the list. + +6. DMA-BUF Interface + +The DMA-BUF interface provides standard methods to manage DMA_BUFs +reconstructed by the Hyper_DMABUF driver from shared pages. All of the +relevant operations are listed in struct dma_buf_ops. These operations +are standard DMA_BUF operations, therefore they follow standard DMA BUF +protocols. + +Each DMA_BUF operation communicates with the exporter at the end of the +routine for “indirect DMA_BUF synchronization”. + +7. Export/Import List Management + +Whenever a DMA_BUF is shared and exported, its information is added to the +database (EXPORT-list) on the exporting VM. Similarly, information about an +imported DMA_BUF is added to the importing database (IMPORT list) on the +importing VM, when the export happens. + +All of the entries in the lists are needed to manage the exported/imported +DMA_BUF more efficiently. Both lists are implemented as Linux hash tables. +The key to the list is hyper_dmabuf_id and the output is the information of +the DMA_BUF. The List Manager manages all requests from other blocks and +transactions within lists to ensure that all entries are up-to-date and +that the list structure is consistent. + +The List Manager provides basic functionality, such as: + +- Adding to the List +- Removal from the List +- Finding information about a DMA_BUF, given the hyper_dmabuf_id + +8. Page Sharing by Hypercalls + +The Hyper_DMABUF driver assumes that there is a native page-by-page memory +sharing mechanism available on the hypervisor. Referencing a group of pages +that are being shared is what the driver expects from “backend” APIs or the +hypervisor itself. + +For the example, xen backend integrated in current code base utilizes Xen’s +grant-table interface for sharing the underlying kernel pages (struct *page). + +More details about grant-table interface can be found at the following locations: + +https://wiki.xen.org/wiki/Grant_Table +https://xenbits.xen.org/docs/4.6-testing/misc/grant-tables.txt + +9. Message Handling + +The exporter and importer can each create a message that consists of an opcode +(command) and operands (parameters) and send it to each other. + +The message format is defined as: + +struct hyper_dmabuf_req { + unsigned int req_id; /* Sequence number. Used for RING BUF + synchronization */ + unsigned int stat; /* Status.Response from receiver. */ + unsigned int cmd; /* Opcode */ + unsigned int op[MAX_NUMBER_OF_OPERANDS]; /* Operands */ +}; + +The following table gives the list of opcodes: + +<Opcodes in Message to Exporter/Importer> + +HYPER_DMABUF_EXPORT (exporter --> importer) + - Export a DMA_BUF to the importer. The importer registers the corresponding + DMA_BUF in its IMPORT LIST when the message is received. + +HYPER_DMABUF_EXPORT_FD (importer --> exporter) + - Locally exported as FD. The importer sends out this command to the exporter + to notify that the buffer is now locally exported (mapped and used). + +HYPER_DMABUF_EXPORT_FD_FAILED (importer --> exporter) + - Failed while exporting locally. The importer sends out this command to the + exporter to notify the exporter that the EXPORT_FD failed. + +HYPER_DMABUF_NOTIFY_UNEXPORT (exporter --> importer) + - Termination of sharing. The exporter notifies the importer that the DMA_BUF + has been unexported. + +HYPER_DMABUF_OPS_TO_REMOTE (importer --> exporter) + - Not implemented yet. + +HYPER_DMABUF_OPS_TO_SOURCE (exporter --> importer) + - DMA_BUF ops to the exporter, for DMA_BUF upstream synchronization. + Note: Implemented but it is done asynchronously due to performance issues. + +The following table shows the list of operands for each opcode. + +<Operands in Message to Exporter/Importer> + +- HYPER_DMABUF_EXPORT + +op0 to op3 – hyper_dmabuf_id +op4 – number of pages to be shared +op5 – offset of data in the first page +op6 – length of data in the last page +op7 – reference number for the group of shared pages +op8 – size of private data +op9 to (op9+op8) – private data + +- HYPER_DMABUF_EXPORT_FD + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_EXPORT_FD_FAILED + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_NOTIFY_UNEXPORT + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_OPS_TO_REMOTE(Not implemented) + +- HYPER_DMABUF_OPS_TO_SOURCE + +op0 to op3 – hyper_dmabuf_id +op4 – type of DMA_BUF operation + +9. Inter VM (Domain) Communication + +Two different types of inter-domain communication channels are required, +one in kernel space and the other in user space. The communication channel +in user space is for transmitting or receiving the hyper_dmabuf_id. Since +there is no specific security (for example, encryption) involved in the +generation of a global id at the driver level, it is highly recommended that +the customer’s user application set up a very secure channel for exchanging +hyper_dmabuf_id between VMs. + +The communication channel in kernel space is required for exchanging messages +from “message management” block between two VMs. In the current reference +backend for Xen hypervisor, Xen ring-buffer and event-channel mechanisms are +used for message exchange between impoter and exporter. + +10. What are required in hypervisor + +emory sharing and message communication between VMs + +------------------------------------------------------------------------------ +Section 3. Hyper DMABUF Sharing Flow +------------------------------------------------------------------------------ + +1. Exporting + +To export a DMA_BUF to another VM, user space has to call an IOCTL +(IOCTL_HYPER_DMABUF_EXPORT_REMOTE) with a file descriptor for the buffer given +by the original exporter. The Hyper_DMABUF driver maps a DMA_BUF locally, then +issues a hyper_dmabuf_id and SGT for the DMA_BUF, which is registered to the +EXPORT list. Then, all pages for the SGT are extracted and each individual +page is shared via a hypervisor-specific memory sharing mechanism +(for example, in Xen this is grant-table). + +One important requirement on this memory sharing method is that it needs to +create a single integer value that represents the list of pages, which can +then be used by the importer for retrieving the group of shared pages. For +this, the “Backend” in the reference driver utilizes the multiple level +addressing mechanism. + +Once the integer reference to the list of pages is created, the exporter +builds the “export” command and sends it to the importer, then notifies the +importer. + +2. Importing + +The Import process is divided into two sections. One is the registration +of DMA_BUF from the exporter. The other is the actual mapping of the buffer +before accessing the data in the buffer. The former (termed “Registration”) +happens on an export event (that is, the export command with an interrupt) +in the exporter. + +The latter (termed “Mapping”) is done asynchronously when the driver gets the +IOCTL call from user space. When the importer gets an interrupt from the +exporter, it checks the command in the receiving queue and if it is an +“export” command, the registration process is started. It first finds +hyper_dmabuf_id and the integer reference for the shared pages, then stores +all of that information together with the “domain id” of the exporting domain +in the IMPORT LIST. + +In the case where “event-polling” is enabled (Kernel Config - Enable event- +generation and polling operation), a “new sharing available” event is +generated right after the reference info for the new shared DMA_BUF is +registered to the IMPORT LIST. This event is added to the event-queue. + +The user process that polls Hyper_DMABUF driver wakes up when this event-queue +is not empty and is able to read back event data from the queue using the +driver’s “Read” function. Once the user-application calls EXPORT_FD IOCTL with +the proper parameters including hyper_dmabuf_id, the Hyper_DMABUF driver +retrieves information about the matched DMA_BUF from the IMPORT LIST. Then, it +maps all pages shared (referenced by the integer reference) in its kernel +space and creates its own DMA_BUF referencing the same shared pages. After +this, it exports this new DMA_BUF to the other drivers with a file descriptor. +DMA_BUF can then be used just in the same way a local DMA_BUF is. + +3. Indirect Synchronization of DMA_BUF + +Synchronization of a DMA_BUF within a single OS is automatically achieved +because all of importer’s DMA_BUF operations are done using functions defined +on the exporter’s side, which means there is one central place that has full +control over the DMA_BUF. In other words, any primary activities such as +attaching/detaching and mapping/un-mapping are all captured by the exporter, +meaning that the exporter knows basic information such as who is using the +DMA_BUF and how it is being used. This, however, is not applicable if this +sharing is done beyond a single OS because kernel space (where the exporter’s +DMA_BUF operations reside) is simply not visible to the importing VM. + +Therefore, “indirect synchronization” was introduced as an alternative solution, +which is now implemented in the Hyper_DMABUF driver. This technique makes +the exporter create a shadow DMA_BUF when the end-consumer of the buffer maps +the DMA_BUF, then duplicates any DMA_BUF operations performed on +the importer’s side. Through this “indirect synchronization”, the exporter is +able to virtually track all activities done by the consumer (mostly reference +counter) as if those are done in exporter’s local system. + +------------------------------------------------------------------------------ +Section 4. Hypervisor Backend Interface +------------------------------------------------------------------------------ + +The Hyper_DMABUF driver has a standard “Backend” structure that contains +mappings to various functions designed for a specific Hypervisor. Most of +these API functions should provide a low-level implementation of communication +and memory sharing capability that utilize a Hypervisor’s native mechanisms. + +struct hyper_dmabuf_backend_ops { + /* retreiving id of current virtual machine */ + int (*get_vm_id)(void); + /* get pages shared via hypervisor-specific method */ + int (*share_pages)(struct page **, int, int, void **); + /* make shared pages unshared via hypervisor specific method */ + int (*unshare_pages)(void **, int); + /* map remotely shared pages on importer's side via + * hypervisor-specific method + */ + struct page ** (*map_shared_pages)(int, int, int, void **); + /* unmap and free shared pages on importer's side via + * hypervisor-specific method + */ + int (*unmap_shared_pages)(void **, int); + /* initialize communication environment */ + int (*init_comm_env)(void); + /* destroy communication channel */ + void (*destroy_comm)(void); + /* upstream ch setup (receiving and responding) */ + int (*init_rx_ch)(int); + /* downstream ch setup (transmitting and parsing responses) */ + int (*init_tx_ch)(int); + /* send msg via communication ch */ + int (*send_req)(int, struct hyper_dmabuf_req *, int); +}; + +<Hypervisor-specific Backend Structure> + +1. get_vm_id + + Returns the VM (domain) ID + + Input: + + -ID of the current domain + + Output: + + None + +2. share_pages + + Get pages shared via hypervisor-specific method and return one reference + ID that represents the complete list of shared pages + + Input: + + -Array of pages + -ID of importing VM + -Number of pages + -Hypervisor specific Representation of reference info of shared + pages + + Output: + + -Hypervisor specific integer value that represents all of + the shared pages + +3. unshare_pages + + Stop sharing pages + + Input: + + -Hypervisor specific Representation of reference info of shared + pages + -Number of shared pages + + Output: + + 0 + +4. map_shared_pages + + Map shared pages locally using a hypervisor-specific method + + Input: + + -Reference number that represents all of shared pages + -ID of exporting VM, Number of pages + -Reference information for any purpose + + Output: + + -An array of shared pages (struct page**) + +5. unmap_shared_pages + + Unmap shared pages + + Input: + + -Hypervisor specific Representation of reference info of shared pages + + Output: + + -0 (successful) or one of Standard Kernel errors + +6. init_comm_env + + Setup infrastructure needed for communication channel + + Input: + + None + + Output: + + None + +7. destroy_comm + + Cleanup everything done via init_comm_env + + Input: + + None + + Output: + + None + +8. init_rx_ch + + Configure receive channel + + Input: + + -ID of VM on the other side of the channel + + Output: + + -0 (successful) or one of Standard Kernel errors + +9. init_tx_ch + + Configure transmit channel + + Input: + + -ID of VM on the other side of the channel + + Output: + + -0 (success) or one of Standard Kernel errors + +10. send_req + + Send message to other VM + + Input:
Documentation/hyper-dmabuf-sharing.txt line 734
Quoted 10 lines
Documentation/hyper-dmabuf-sharing.txt new file +10
-0+ + -ID of VM that receives the message + -Message + + Output: + + -0 (success) or one of Standard Kernel errors + +------------------------------------------------------------------------------- +-------------------------------------------------------------------------------
[1] https://elixir.bootlin.com/linux/v4.16.1/source/include/xen/interface/io/kbdif.h
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
RFC PATCH v2 2/9 hyper_dmabuf: architecture specification and reference guide
In reply to Dongwon KimReference document for hyper_DMABUF driver
Documentation/hyper-dmabuf-sharing.txt
--- Documentation/hyper-dmabuf-sharing.txt | 734 +++++++++++++++++++++++++++++++++ 1 file changed, 734 insertions(+) create mode 100644 Documentation/hyper-dmabuf-sharing.txt
Documentation/hyper-dmabuf-sharing.txt new file +734 -0
@@ -0,0 +1,734 @@ +Linux Hyper DMABUF Driver + +------------------------------------------------------------------------------ +Section 1. Overview +------------------------------------------------------------------------------ + +Hyper_DMABUF driver is a Linux device driver running on multiple Virtual +achines (VMs), which expands DMA-BUF sharing capability to the VM environment +where multiple different OS instances need to share same physical data without +data-copy across VMs. + +To share a DMA_BUF across VMs, an instance of the Hyper_DMABUF drv on the +exporting VM (so called, “exporter”) imports a local DMA_BUF from the original +producer of the buffer, then re-exports it with an unique ID, hyper_dmabuf_id +for the buffer to the importing VM (so called, “importer”). + +Another instance of the Hyper_DMABUF driver on importer registers +a hyper_dmabuf_id together with reference information for the shared physical +pages associated with the DMA_BUF to its database when the export happens. + +The actual mapping of the DMA_BUF on the importer’s side is done by +the Hyper_DMABUF driver when user space issues the IOCTL command to access +the shared DMA_BUF. The Hyper_DMABUF driver works as both an importing and +exporting driver as is, that is, no special configuration is required. +Consequently, only a single module per VM is needed to enable cross-VM DMA_BUF +exchange. + +------------------------------------------------------------------------------ +Section 2. Architecture +------------------------------------------------------------------------------ + +1. Hyper_DMABUF ID + +hyper_dmabuf_id is a global handle for shared DMA BUFs, which is compatible +across VMs. It is a key used by the importer to retrieve information about +shared Kernel pages behind the DMA_BUF structure from the IMPORT list. When +a DMA_BUF is exported to another domain, its hyper_dmabuf_id and META data +are also kept in the EXPORT list by the exporter for further synchronization +of control over the DMA_BUF. + +hyper_dmabuf_id is “targeted”, meaning it is valid only in exporting (owner of +the buffer) and importing VMs, where the corresponding hyper_dmabuf_id is +stored in their database (EXPORT and IMPORT lists). + +A user-space application specifies the targeted VM id in the user parameter +when it calls the IOCTL command to export shared DMA_BUF to another VM. + +hyper_dmabuf_id_t is a data type for hyper_dmabuf_id. It is defined as 16-byte +data structure, and it contains id and rng_key[3] as elements for +the structure. + +typedef struct { + int id; + int rng_key[3]; /* 12bytes long random number */ +} hyper_dmabuf_id_t; + +The first element in the hyper_dmabuf_id structure, int id is combined data of +a count number generated by the driver running on the exporter and +the exporter’s ID. The VM’s ID is a one byte value and located at the field’s +SB in int id. The remaining three bytes in int id are reserved for a count +number. + +However, there is a limit related to this count number, which is 1000. +Therefore, only little more than a byte starting from the LSB is actually used +for storing this count number. + +#define HYPER_DMABUF_ID_CREATE(domid, id) \ + ((((domid) & 0xFF) << 24) | ((id) & 0xFFFFFF)) + +This limit on the count number directly means the maximum number of DMA BUFs +that can be shared simultaneously by one VM. The second element of +hyper_dmabuf_id, that is int rng_key[3], is an array of three integers. These +numbers are generated by Linux’s native random number generation mechanism. +This field is added to enhance the security of the Hyper DMABUF driver by +maximizing the entropy of hyper_dmabuf_id (that is, preventing it from being +guessed by a security attacker). + +Once DMA_BUF is no longer shared, the hyper_dmabuf_id associated with +the DMA_BUF is released, but the count number in hyper_dmabuf_id is saved in +the ID list for reuse. However, random keys stored in int rng_key[3] are not +reused. Instead, those keys are always filled with freshly generated random +keys for security. + +2. IOCTLs + +a. IOCTL_HYPER_DMABUF_TX_CH_SETUP + +This type of IOCTL is used for initialization of a one-directional transmit +communication channel with a remote domain. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_tx_ch_setup { + /* IN parameters */ + /* Remote domain id */ + int remote_domain; +}; + +b. IOCTL_HYPER_DMABUF_RX_CH_SETUP + +This type of IOCTL is used for initialization of a one-directional receive +communication channel with a remote domain. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_rx_ch_setup { + /* IN parameters */ + /* Source domain id */ + int source_domain; +}; + +c. IOCTL_HYPER_DMABUF_EXPORT_REMOTE + +This type of IOCTL is used to export a DMA BUF to another VM. When a user +space application makes this call to the driver, it extracts Kernel pages +associated with the DMA_BUF, then makes those shared with the importing VM. + +All reference information for this shared pages and hyper_dmabuf_id is +created, then passed to the importing domain through a communications +channel for synchronous registration. In the meantime, the hyper_dmabuf_id +for the shared DMA_BUF is also returned to user-space application. + +This IOCTL can accept a reference to “user-defined” data as well as a FD +for the DMA BUF. This private data is then attached to the DMA BUF and +exported together with it. + +More details regarding this private data can be found in chapter for +“Hyper_DMABUF Private Data”. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_export_remote { + /* IN parameters */ + /* DMA buf fd to be exported */ + int dmabuf_fd; + /* Domain id to which buffer should be exported */ + int remote_domain; + /* exported dma buf id */ + hyper_dmabuf_id_t hid; + /* size of private data */ + int sz_priv; + /* ptr to the private data for Hyper_DMABUF */ + char *priv; +}; + +d. IOCTL_HYPER_DMABUF_EXPORT_FD + +The importing VM uses this IOCTL to import and re-export a shared DMA_BUF +locally to the end-consumer using the standard Linux DMA_BUF framework. +Upon IOCTL call, the Hyper_DMABUF driver finds the reference information +of the shared DMA_BUF with the given hyper_dmabuf_id, then maps all shared +pages in its own Kernel space. The driver then constructs a scatter-gather +list with those mapped pages and creates a brand-new DMA_BUF with the list, +which is eventually exported with a file descriptor to the local consumer. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_export_fd { + /* IN parameters */ + /* hyper dmabuf id to be imported */ + int hyper_dmabuf_id; + /* flags */ + int flags; + /* OUT parameters */ + /* exported dma buf fd */ + int fd; +}; + +e. IOCTL_HYPER_DMABUF_UNEXPORT + +This type of IOCTL is used when it is necessary to terminate the current +sharing of a DMA_BUF. When called, the driver first checks if there are any +consumers actively using the DMA_BUF. Then, it unexports it if it is not +mapped or used by any consumers. Otherwise, it postpones unexporting, but +makes the buffer invalid to prevent any further import of the same DMA_BUF. +DMA_BUF is completely unexported after the last consumer releases it. + +”Unexport” means removing all reference information about the DMA_BUF from the +LISTs and make all pages private again. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_unexport { + /* IN parameters */ + /* hyper dmabuf id to be unexported */ + int hyper_dmabuf_id; + /* delay in ms by which unexport processing will be postponed */ + int delay_ms; + /* OUT parameters */ + /* Status of request */ + int status; +}; + +f. IOCTL_HYPER_DMABUF_QUERY + +This IOCTL is used to retrieve specific information about a DMA_BUF that +is being shared. + +The user space argument for this type of IOCTL is defined as: + +struct ioctl_hyper_dmabuf_query { + /* in parameters */ + /* hyper dmabuf id to be queried */ + int hyper_dmabuf_id; + /* item to be queried */ + int item; + /* OUT parameters */ + /* output of query */ + /* info can be either value or reference */ + unsigned long info; +}; + +<Available Queries> + +HYPER_DMABUF_QUERY_TYPE + - Return the type of DMA_BUF from the current domain, Exported or Imported. + +HYPER_DMABUF_QUERY_EXPORTER + - Return the exporting domain’s ID of a shared DMA_BUF. + +HYPER_DMABUF_QUERY_IMPORTER + - Return the importing domain’s ID of a shared DMA_BUF. + +HYPER_DMABUF_QUERY_SIZE + - Return the size of a shared DMA_BUF in bytes. + +HYPER_DMABUF_QUERY_BUSY + - Return ‘true’ if a shared DMA_BUF is currently used + (mapped by the end-consumer). + +HYPER_DMABUF_QUERY_UNEXPORTED + - Return ‘true’ if a shared DMA_BUF is not valid anymore + (so it does not allow a new consumer to map it). + +HYPER_DMABUF_QUERY_DELAYED_UNEXPORTED + - Return ‘true’ if a shared DMA_BUF is scheduled to be unexported + (but is still valid) within a fixed time. + +HYPER_DMABUF_QUERY_PRIV_INFO + - Return ‘private’ data attached to shared DMA_BUF to the user space. + ‘unsigned long info’ is the user space pointer for the buffer, where + private data will be copied to. + +HYPER_DMABUF_QUERY_PRIV_INFO_SIZE + - Return the size of the private data attached to the shared DMA_BUF. + +3. Event Polling + +Event-polling can be enabled optionally by selecting the Kernel config option, +Enable event-generation and polling operation under xen/hypervisor in Kernel’s +menuconfig. The event-polling mechanism includes the generation of +an import-event, adding it to the event-queue and providing a notification to +the application so that it can retrieve the event data from the queue. + +For this mechanism, “Poll” and “Read” operations are added to the Hyper_DMABUF +driver. A user application that polls the driver goes into a sleep state until +there is a new event added to the queue. An application uses “Read” to retrieve +event data from the event queue. Event data contains the hyper_dmabuf_id and +the private data of the buffer that has been received by the importer. + +For more information on private data, refer to Section 3.5). +Using this method, it is possible to lower the risk of the hyper_dmabuf_id and +other sensitive information about the shared buffer (for example, meta-data +for shared images) being leaked while being transferred to the importer because +all of this data is shared as “private info” at the driver level. However, +please note there should be a way for the importer to find the correct DMA_BUF +in this case when there are multiple Hyper_DMABUFs being shared simultaneously. +For example, the surface name or the surface ID of a specific rendering surface +needs to be sent to the importer in advance before it is exported in a surface- +sharing use-case. + +Each event data given to the user-space consists of a header and the private +information of the buffer. The data type is defined as follows: + +struct hyper_dmabuf_event_hdr { + int event_type; /* one type only for now - new import */ + hyper_dmabuf_id_t hid; /* hyper_dmabuf_id of specific hyper_dmabuf */ + int size; /* size of data */ +}; + +struct hyper_dmabuf_event_data { + struct hyper_dmabuf_event_hdr hdr; + void *data; /* private data */ +}; + +4. Hyper_DMABUF Private Data + +Each Hyper_DMABUF can come with private data, the size of which can be up to +AX_SIZE_PRIV_DATA (currently 192 byte). This private data is just a chunk of +plain data attached to every Hyper_DMABUF. It is guaranteed to be synchronized +across VMs, exporter and importer. This private data does not have any specific +structure defined at the driver level, so any “user-defined” format or +structure can be used. In addition, there is no dedicated use-case for this +data. It can be used virtually for any purpose. For example, it can be used to +share meta-data such as dimension and color formats for shared images in +a surface sharing model. Another example is when we share protected media +contents. + +This private data can be used to transfer flags related to content protection +information on streamed media to the importer. + +Private data is initially generated when a buffer is exported for the first +time. Then, it is updated whenever the same buffer is re-exported. During the +re-exporting process, the Hyper_DMABUF driver only updates private data on +both sides with new data from user-space since the same buffer already exists +on both the IMPORT LIST and EXPORT LIST. + +There are two different ways to retrieve this private data from user-space. +The first way is to use “Read” on the Hyper_DMABUF driver. “Read” returns the +data of events containing private data of the buffer. The second way is to +make a query to Hyper_DMABUF. There are two query items, +HYPER_DMABUF_QUERY_PRIV_INFO and HYPER_DMABUF_QUERY_PRIV_INFO_SIZE available +for retrieving private data and its size. + +5. Scatter-Gather List Table (SGT) Management + +SGT management is the core part of the Hyper_DMABUF driver that manages an +SGT, a representation of the group of kernel pages associated with a DMA_BUF. +This block includes four different sub-blocks: + +a. Hyper_DMABUF_id Manager + +This ID manager is responsible for generating a hyper_dmabuf_id for an +exported DMA_BUF. When an ID is requested, the ID Manager first checks if +there are any reusable IDs left in the list and returns one of those, +if available. Otherwise, it creates the next count number and returns it +to the caller. + +b. SGT Creator + +The SGT (struct sg_table) contains information about the DMA_BUF such as +references to all kernel pages for the buffer and their connections. The +SGT Creator creates a new SGT on the importer side with pages shared by +the hypervisor. + +c. Kernel Page Extractor + +The Page Extractor extracts pages from a given SGT before those pages +are shared. + +d. List Manager Interface + +The SGT manger also interacts with export and import list managers. It +sends out information (for example, hyper_dmabuf_id, reference, and +DMA_BUF information) about the exported or imported DMA_BUFs to the +list manager. Also, on IOCTL request, it asks the list manager to find +and return the information for a corresponding DMA_BUF in the list. + +6. DMA-BUF Interface + +The DMA-BUF interface provides standard methods to manage DMA_BUFs +reconstructed by the Hyper_DMABUF driver from shared pages. All of the +relevant operations are listed in struct dma_buf_ops. These operations +are standard DMA_BUF operations, therefore they follow standard DMA BUF +protocols. + +Each DMA_BUF operation communicates with the exporter at the end of the +routine for “indirect DMA_BUF synchronization”. + +7. Export/Import List Management + +Whenever a DMA_BUF is shared and exported, its information is added to the +database (EXPORT-list) on the exporting VM. Similarly, information about an +imported DMA_BUF is added to the importing database (IMPORT list) on the +importing VM, when the export happens. + +All of the entries in the lists are needed to manage the exported/imported +DMA_BUF more efficiently. Both lists are implemented as Linux hash tables. +The key to the list is hyper_dmabuf_id and the output is the information of +the DMA_BUF. The List Manager manages all requests from other blocks and +transactions within lists to ensure that all entries are up-to-date and +that the list structure is consistent. + +The List Manager provides basic functionality, such as: + +- Adding to the List +- Removal from the List +- Finding information about a DMA_BUF, given the hyper_dmabuf_id + +8. Page Sharing by Hypercalls + +The Hyper_DMABUF driver assumes that there is a native page-by-page memory +sharing mechanism available on the hypervisor. Referencing a group of pages +that are being shared is what the driver expects from “backend” APIs or the +hypervisor itself. + +For the example, xen backend integrated in current code base utilizes Xen’s +grant-table interface for sharing the underlying kernel pages (struct *page). + +More details about grant-table interface can be found at the following locations: + +https://wiki.xen.org/wiki/Grant_Table +https://xenbits.xen.org/docs/4.6-testing/misc/grant-tables.txt + +9. Message Handling + +The exporter and importer can each create a message that consists of an opcode +(command) and operands (parameters) and send it to each other. + +The message format is defined as: + +struct hyper_dmabuf_req { + unsigned int req_id; /* Sequence number. Used for RING BUF + synchronization */ + unsigned int stat; /* Status.Response from receiver. */ + unsigned int cmd; /* Opcode */ + unsigned int op[MAX_NUMBER_OF_OPERANDS]; /* Operands */ +}; + +The following table gives the list of opcodes: + +<Opcodes in Message to Exporter/Importer> + +HYPER_DMABUF_EXPORT (exporter --> importer) + - Export a DMA_BUF to the importer. The importer registers the corresponding + DMA_BUF in its IMPORT LIST when the message is received. + +HYPER_DMABUF_EXPORT_FD (importer --> exporter) + - Locally exported as FD. The importer sends out this command to the exporter + to notify that the buffer is now locally exported (mapped and used). + +HYPER_DMABUF_EXPORT_FD_FAILED (importer --> exporter) + - Failed while exporting locally. The importer sends out this command to the + exporter to notify the exporter that the EXPORT_FD failed. + +HYPER_DMABUF_NOTIFY_UNEXPORT (exporter --> importer) + - Termination of sharing. The exporter notifies the importer that the DMA_BUF + has been unexported. + +HYPER_DMABUF_OPS_TO_REMOTE (importer --> exporter) + - Not implemented yet. + +HYPER_DMABUF_OPS_TO_SOURCE (exporter --> importer) + - DMA_BUF ops to the exporter, for DMA_BUF upstream synchronization. + Note: Implemented but it is done asynchronously due to performance issues. + +The following table shows the list of operands for each opcode. + +<Operands in Message to Exporter/Importer> + +- HYPER_DMABUF_EXPORT + +op0 to op3 – hyper_dmabuf_id +op4 – number of pages to be shared +op5 – offset of data in the first page +op6 – length of data in the last page +op7 – reference number for the group of shared pages +op8 – size of private data +op9 to (op9+op8) – private data + +- HYPER_DMABUF_EXPORT_FD + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_EXPORT_FD_FAILED + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_NOTIFY_UNEXPORT + +op0 to op3 – hyper_dmabuf_id + +- HYPER_DMABUF_OPS_TO_REMOTE(Not implemented) + +- HYPER_DMABUF_OPS_TO_SOURCE + +op0 to op3 – hyper_dmabuf_id +op4 – type of DMA_BUF operation + +9. Inter VM (Domain) Communication + +Two different types of inter-domain communication channels are required, +one in kernel space and the other in user space. The communication channel +in user space is for transmitting or receiving the hyper_dmabuf_id. Since +there is no specific security (for example, encryption) involved in the +generation of a global id at the driver level, it is highly recommended that +the customer’s user application set up a very secure channel for exchanging +hyper_dmabuf_id between VMs. + +The communication channel in kernel space is required for exchanging messages +from “message management” block between two VMs. In the current reference +backend for Xen hypervisor, Xen ring-buffer and event-channel mechanisms are +used for message exchange between impoter and exporter. + +10. What are required in hypervisor + +emory sharing and message communication between VMs + +------------------------------------------------------------------------------ +Section 3. Hyper DMABUF Sharing Flow +------------------------------------------------------------------------------ + +1. Exporting + +To export a DMA_BUF to another VM, user space has to call an IOCTL +(IOCTL_HYPER_DMABUF_EXPORT_REMOTE) with a file descriptor for the buffer given +by the original exporter. The Hyper_DMABUF driver maps a DMA_BUF locally, then +issues a hyper_dmabuf_id and SGT for the DMA_BUF, which is registered to the +EXPORT list. Then, all pages for the SGT are extracted and each individual +page is shared via a hypervisor-specific memory sharing mechanism +(for example, in Xen this is grant-table). + +One important requirement on this memory sharing method is that it needs to +create a single integer value that represents the list of pages, which can +then be used by the importer for retrieving the group of shared pages. For +this, the “Backend” in the reference driver utilizes the multiple level +addressing mechanism. + +Once the integer reference to the list of pages is created, the exporter +builds the “export” command and sends it to the importer, then notifies the +importer. + +2. Importing + +The Import process is divided into two sections. One is the registration +of DMA_BUF from the exporter. The other is the actual mapping of the buffer +before accessing the data in the buffer. The former (termed “Registration”) +happens on an export event (that is, the export command with an interrupt) +in the exporter. + +The latter (termed “Mapping”) is done asynchronously when the driver gets the +IOCTL call from user space. When the importer gets an interrupt from the +exporter, it checks the command in the receiving queue and if it is an +“export” command, the registration process is started. It first finds +hyper_dmabuf_id and the integer reference for the shared pages, then stores +all of that information together with the “domain id” of the exporting domain +in the IMPORT LIST. + +In the case where “event-polling” is enabled (Kernel Config - Enable event- +generation and polling operation), a “new sharing available” event is +generated right after the reference info for the new shared DMA_BUF is +registered to the IMPORT LIST. This event is added to the event-queue. + +The user process that polls Hyper_DMABUF driver wakes up when this event-queue +is not empty and is able to read back event data from the queue using the +driver’s “Read” function. Once the user-application calls EXPORT_FD IOCTL with +the proper parameters including hyper_dmabuf_id, the Hyper_DMABUF driver +retrieves information about the matched DMA_BUF from the IMPORT LIST. Then, it +maps all pages shared (referenced by the integer reference) in its kernel +space and creates its own DMA_BUF referencing the same shared pages. After +this, it exports this new DMA_BUF to the other drivers with a file descriptor. +DMA_BUF can then be used just in the same way a local DMA_BUF is. + +3. Indirect Synchronization of DMA_BUF + +Synchronization of a DMA_BUF within a single OS is automatically achieved +because all of importer’s DMA_BUF operations are done using functions defined +on the exporter’s side, which means there is one central place that has full +control over the DMA_BUF. In other words, any primary activities such as +attaching/detaching and mapping/un-mapping are all captured by the exporter, +meaning that the exporter knows basic information such as who is using the +DMA_BUF and how it is being used. This, however, is not applicable if this +sharing is done beyond a single OS because kernel space (where the exporter’s +DMA_BUF operations reside) is simply not visible to the importing VM. + +Therefore, “indirect synchronization” was introduced as an alternative solution, +which is now implemented in the Hyper_DMABUF driver. This technique makes +the exporter create a shadow DMA_BUF when the end-consumer of the buffer maps +the DMA_BUF, then duplicates any DMA_BUF operations performed on +the importer’s side. Through this “indirect synchronization”, the exporter is +able to virtually track all activities done by the consumer (mostly reference +counter) as if those are done in exporter’s local system. + +------------------------------------------------------------------------------ +Section 4. Hypervisor Backend Interface +------------------------------------------------------------------------------ + +The Hyper_DMABUF driver has a standard “Backend” structure that contains +mappings to various functions designed for a specific Hypervisor. Most of +these API functions should provide a low-level implementation of communication +and memory sharing capability that utilize a Hypervisor’s native mechanisms. + +struct hyper_dmabuf_backend_ops { + /* retreiving id of current virtual machine */ + int (*get_vm_id)(void); + /* get pages shared via hypervisor-specific method */ + int (*share_pages)(struct page **, int, int, void **); + /* make shared pages unshared via hypervisor specific method */ + int (*unshare_pages)(void **, int); + /* map remotely shared pages on importer's side via + * hypervisor-specific method + */ + struct page ** (*map_shared_pages)(int, int, int, void **); + /* unmap and free shared pages on importer's side via + * hypervisor-specific method + */ + int (*unmap_shared_pages)(void **, int); + /* initialize communication environment */ + int (*init_comm_env)(void); + /* destroy communication channel */ + void (*destroy_comm)(void); + /* upstream ch setup (receiving and responding) */ + int (*init_rx_ch)(int); + /* downstream ch setup (transmitting and parsing responses) */ + int (*init_tx_ch)(int); + /* send msg via communication ch */ + int (*send_req)(int, struct hyper_dmabuf_req *, int); +}; + +<Hypervisor-specific Backend Structure> + +1. get_vm_id + + Returns the VM (domain) ID + + Input: + + -ID of the current domain + + Output: + + None + +2. share_pages + + Get pages shared via hypervisor-specific method and return one reference + ID that represents the complete list of shared pages + + Input: + + -Array of pages + -ID of importing VM + -Number of pages + -Hypervisor specific Representation of reference info of shared + pages + + Output: + + -Hypervisor specific integer value that represents all of + the shared pages + +3. unshare_pages + + Stop sharing pages + + Input: + + -Hypervisor specific Representation of reference info of shared + pages + -Number of shared pages + + Output: + + 0 + +4. map_shared_pages + + Map shared pages locally using a hypervisor-specific method + + Input: + + -Reference number that represents all of shared pages + -ID of exporting VM, Number of pages + -Reference information for any purpose + + Output: + + -An array of shared pages (struct page**) + +5. unmap_shared_pages + + Unmap shared pages + + Input: + + -Hypervisor specific Representation of reference info of shared pages + + Output: + + -0 (successful) or one of Standard Kernel errors + +6. init_comm_env + + Setup infrastructure needed for communication channel + + Input: + + None + + Output: + + None + +7. destroy_comm + + Cleanup everything done via init_comm_env + + Input: + + None + + Output: + + None + +8. init_rx_ch + + Configure receive channel + + Input: + + -ID of VM on the other side of the channel + + Output: + + -0 (successful) or one of Standard Kernel errors + +9. init_tx_ch + + Configure transmit channel + + Input: + + -ID of VM on the other side of the channel + + Output: + + -0 (success) or one of Standard Kernel errors + +10. send_req + + Send message to other VM + + Input: + + -ID of VM that receives the message + -Message + + Output: + + -0 (success) or one of Standard Kernel errors + +------------------------------------------------------------------------------- +-------------------------------------------------------------------------------
Signature · 7 lines
2.16.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
RFC PATCH v2 3/9 MAINTAINERS: adding Hyper_DMABUF driver section in MAINTAINERS
In reply to Dongwon KimHyper_DMABUF DRIVER
M: Dongwon Kim <[email protected]> M: Mateusz Polrola <[email protected]> L: [email protected] L: [email protected] S: Maintained F: drivers/dma-buf/hyper_dmabuf* F: include/uapi/linux/hyper_dmabuf.h F: Documentation/hyper-dmabuf-sharing.txt T: https://github.com/downor/linux_hyper_dmabuf/
--- MAINTAINERS | 11 +++++++++++ 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
@@ -6468,6 +6468,17 @@ S: Maintained F: mm/memory-failure.c F: mm/hwpoison-inject.c +Hyper_DMABUF DRIVER +M: Dongwon Kim <[email protected]> +M: Mateusz Polrola <[email protected]> +L: [email protected] +L: [email protected] +S: Maintained +F: drivers/dma-buf/hyper_dmabuf* +F: include/uapi/linux/hyper_dmabuf.h +F: Documentation/hyper-dmabuf-sharing.txt +T: https://github.com/downor/linux_hyper_dmabuf/ + Hyper-V CORE AND DRIVERS M: "K. Y. Srinivasan" <[email protected]> M: Haiyang Zhang <[email protected]>
Signature · 6 lines
2.16.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
RFC PATCH v2 3/9 MAINTAINERS: adding Hyper_DMABUF driver section in MAINTAINERS
In reply to Dongwon KimHyper_DMABUF DRIVER
M: Dongwon Kim <[email protected]> M: Mateusz Polrola <[email protected]> L: [email protected] L: [email protected] S: Maintained F: drivers/dma-buf/hyper_dmabuf* F: include/uapi/linux/hyper_dmabuf.h F: Documentation/hyper-dmabuf-sharing.txt T: https://github.com/downor/linux_hyper_dmabuf/
--- MAINTAINERS | 11 +++++++++++ 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
@@ -6468,6 +6468,17 @@ S: Maintained F: mm/memory-failure.c F: mm/hwpoison-inject.c +Hyper_DMABUF DRIVER +M: Dongwon Kim <[email protected]> +M: Mateusz Polrola <[email protected]> +L: [email protected] +L: [email protected] +S: Maintained +F: drivers/dma-buf/hyper_dmabuf* +F: include/uapi/linux/hyper_dmabuf.h +F: Documentation/hyper-dmabuf-sharing.txt +T: https://github.com/downor/linux_hyper_dmabuf/ + Hyper-V CORE AND DRIVERS M: "K. Y. Srinivasan" <[email protected]> M: Haiyang Zhang <[email protected]>
Signature · 7 lines
2.16.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
RFC PATCH v2 4/9 hyper_dmabuf: user private data attached to hyper_DMABUF
In reply to Dongwon KimDefine a private data (e.g. meta data for the buffer) attached to each hyper_DMABUF structure. This data is provided by userapace via export_remote IOCTL and its size can be up to 192 bytes.
--- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c | 83 ++++++++++++++++++++-- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c | 36 +++++++++- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h | 2 +- .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c | 1 + drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h | 12 ++++ include/uapi/linux/hyper_dmabuf.h | 4 ++ 6 files changed, 132 insertions(+), 6 deletions(-)
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c +79 -4
@@ -103,6 +103,11 @@ static int send_export_msg(struct exported_sgt_info *exported, } } + op[8 ] = exported->sz_priv; + +/* driver/application specific private info */ +memcpy (&op[9 ], exported->priv, op[8 ]); + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL );if (!req)
@@ -120,8 +125,9 @@ static int send_export_msg(struct exported_sgt_info *exported,/* Fast path exporting routine in case same buffer is already exported. * -* If same buffer is still valid and exist in EXPORT LIST it returns 0 so -* that remaining normal export process can be skipped. +* If same buffer is still valid and exist in EXPORT LIST, it only updates +* user-private data for the buffer and returns 0 so that that it can skip +* normal export process. * * If "unexport" is scheduled for the buffer, it cancels it since the buffer * is being re-exported.
@@ -129,7 +135,7 @@ static int send_export_msg(struct exported_sgt_info *exported,* return '1' if reexport is needed, return '0' if succeeds, return * Kernel error code if something goes wrong */ -static int fastpath_export (hyper_dmabuf_id_t hid) +static int fastpath_export (hyper_dmabuf_id_t hid,int sz_priv,char *priv) {int reexport =1 ;int ret =0 ;
@@ -155,6 +161,46 @@ static int fastpath_export(hyper_dmabuf_id_t hid) exported->unexport_sched = false; } +/* if there's any change in size of private data. +* we reallocate space for private data with new size +*/ +if (sz_priv != exported->sz_priv) { +kfree (exported->priv); + +/* truncating size */ +if (sz_priv >MAX_SIZE_PRIV_DATA ) + exported->sz_priv =MAX_SIZE_PRIV_DATA ; +else + exported->sz_priv = sz_priv; + + exported->priv =kcalloc (1 , exported->sz_priv, +GFP_KERNEL ); + +if (!exported->priv) { +hyper_dmabuf_remove_exported (exported->hid); +hyper_dmabuf_cleanup_sgt_info (exported, true); +kfree (exported); +return -ENOMEM ; + } + } + +/* update private data in sgt_info with new ones */ + ret =copy_from_user (exported->priv, priv, exported->sz_priv); +if (ret) { +dev_err (hy_drv_priv->dev, +"Failed to load a new private data\n" ); + ret = -EINVAL ; + }else { +/* send an export msg for updating priv in importer */ + ret =send_export_msg (exported,NULL ); + +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"Failed to send a new private data\n" ); + ret = -EBUSY ; + } + } +return ret; }
@@ -191,7 +237,8 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) export_remote_attr->remote_domain);if (hid.id !=-1 ) { - ret =fastpath_export (hid); + ret =fastpath_export (hid, export_remote_attr->sz_priv, + export_remote_attr->priv);/* return if fastpath_export succeeds or * gets some fatal error
@@ -225,6 +272,24 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) goto fail_sgt_info_creation; } +/* possible truncation */ +if (export_remote_attr->sz_priv >MAX_SIZE_PRIV_DATA ) + exported->sz_priv =MAX_SIZE_PRIV_DATA ; +else + exported->sz_priv = export_remote_attr->sz_priv; + +/* creating buffer for private data of buffer */ +if (exported->sz_priv !=0 ) { + exported->priv =kcalloc (1 , exported->sz_priv,GFP_KERNEL ); + +if (!exported->priv) { + ret = -ENOMEM ; + goto fail_priv_creation; + } + }else { +dev_err (hy_drv_priv->dev,"size is 0\n" ); + } + exported->hid =hyper_dmabuf_get_hid ();/* no more exported dmabuf allowed */
@@ -279,6 +344,10 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data)INIT_LIST_HEAD (&exported->va_kmapped->list);INIT_LIST_HEAD (&exported->va_vmapped->list); +/* copy private data to sgt_info */ + ret =copy_from_user (exported->priv, export_remote_attr->priv, + exported->sz_priv); +if (ret) {dev_err (hy_drv_priv->dev,"failed to load private data\n" );
@@ -337,6 +406,9 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) fail_map_active_attached:kfree (exported->active_sgts); +kfree (exported->priv); + +fail_priv_creation:kfree (exported); fail_map_active_sgts:
@@ -567,6 +639,9 @@ static void delayed_unexport(struct work_struct *work)/* register hyper_dmabuf_id to the list for reuse */ hyper_dmabuf_store_hid (exported->hid); +if (exported->sz_priv >0 && !exported->priv) +kfree (exported->priv); +kfree (exported); } }
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c +35 -1
@@ -60,9 +60,12 @@ void hyper_dmabuf_create_req(struct hyper_dmabuf_req *req,* op5 : offset of data in the first page * op6 : length of data in the last page * op7 : top-level reference number for shared pages +* op8 : size of private data (from op9) +* op9 ~ : Driver-specific private data +* (e.g. graphic buffer's meta info) */ -memcpy (&req->op[0 ], &op[0 ],8 *sizeof (int ) + op[8 ]); +memcpy (&req->op[0 ], &op[0 ],9 *sizeof (int ) + op[8 ]);break ;case HYPER_DMABUF_NOTIFY_UNEXPORT :
@@ -116,6 +119,9 @@ static void cmd_process_work(struct work_struct *work)* op5 : offset of data in the first page * op6 : length of data in the last page * op7 : top-level reference number for shared pages +* op8 : size of private data (from op9) +* op9 ~ : Driver-specific private data +* (e.g. graphic buffer's meta info) */ /* if nents == 0, it means it is a message only for
@@ -135,6 +141,24 @@ static void cmd_process_work(struct work_struct *work)break ; } +/* if size of new private data is different, +* we reallocate it. +*/ +if (imported->sz_priv != req->op[8 ]) { +kfree (imported->priv); + imported->sz_priv = req->op[8 ]; + imported->priv =kcalloc (1 , req->op[8 ], +GFP_KERNEL ); +if (!imported->priv) { +/* set it invalid */ + imported->valid =0 ; +break ; + } + } + +/* updating priv data */ +memcpy (imported->priv, &req->op[9 ], req->op[8 ]); +break ; }
@@ -143,6 +167,14 @@ static void cmd_process_work(struct work_struct *work)if (!imported)break ; + imported->sz_priv = req->op[8 ]; + imported->priv =kcalloc (1 , req->op[8 ],GFP_KERNEL ); + +if (!imported->priv) { +kfree (imported); +break ; + } + imported->hid.id = req->op[0 ];for (i =0 ; i <3 ; i++)
@@ -162,6 +194,8 @@ static void cmd_process_work(struct work_struct *work)dev_dbg (hy_drv_priv->dev,"\tlast len %d\n" , req->op[6 ]);dev_dbg (hy_drv_priv->dev,"\tgrefid %d\n" , req->op[7 ]); +memcpy (imported->priv, &req->op[9 ], req->op[8 ]); + imported->valid = true;hyper_dmabuf_register_imported (imported);
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h +1 -1
@@ -27,7 +27,7 @@ #ifndef __HYPER_DMABUF_MSG_H__ #define __HYPER_DMABUF_MSG_H__ -#define MAX_NUMBER_OF_OPERANDS 8 +#define MAX_NUMBER_OF_OPERANDS 64 struct hyper_dmabuf_req { unsigned int req_id;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c +1 -0
@@ -251,6 +251,7 @@ int hyper_dmabuf_cleanup_sgt_info(struct exported_sgt_info *exported,kfree (exported->active_attached);kfree (exported->va_kmapped);kfree (exported->va_vmapped); +kfree (exported->priv);return 0 ; }
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h +12 -0
@@ -101,6 +101,12 @@ struct exported_sgt_info {* the buffer can be completely freed. */ struct file *filp; + +/* size of private */ +size_t sz_priv; + +/* private data associated with the exported buffer */ +char *priv; };/* imported_sgt_info contains information about imported DMA_BUF
@@ -126,6 +132,12 @@ struct imported_sgt_info {void *refs_info;bool valid;int importers; + +/* size of private */ +size_t sz_priv; + +/* private data associated with the exported buffer */ + char *priv; };#endif /* __HYPER_DMABUF_STRUCT_H__ */
include/uapi/linux/hyper_dmabuf.h +4 -0
@@ -25,6 +25,8 @@ #ifndef __LINUX_PUBLIC_HYPER_DMABUF_H__ #define __LINUX_PUBLIC_HYPER_DMABUF_H__ +#define MAX_SIZE_PRIV_DATA 192 + typedef struct { int id; int rng_key[3]; /* 12bytes long random number */
@@ -56,6 +58,8 @@ struct ioctl_hyper_dmabuf_export_remote {int remote_domain;/* exported dma buf id */ hyper_dmabuf_id_t hid; +int sz_priv; +char *priv; };#define IOCTL_HYPER_DMABUF_EXPORT_FD \
Signature · 6 lines
2.16.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [RFC, v2, 4/9] hyper_dmabuf: user private data attached to hyper_DMABUF
Quoted 219 lines · Dongwon Kim
Define a private data (e.g. meta data for the buffer) attached to each
hyper_DMABUFstructure. This data is provided by userapace viaexport_remoteIOCTL and its size can be up to 192 bytes.Signed-off-by Dongwon Kim <[email protected]>Signed-off-by Mateusz Polrola <[email protected]>--- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c | 83 ++++++++++++++++++++-- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c | 36 +++++++++- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h | 2 +- .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c | 1 + drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h | 12 ++++ include/uapi/linux/hyper_dmabuf.h | 4 ++ 6 files changed, 132 insertions(+), 6 deletions(-)drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c +79
-4@@ -103,6 +103,11 @@ static int send_export_msg(struct exported_sgt_info *exported, } } + op[8 ] = exported->sz_priv; + +/* driver/application specific private info */ +memcpy (&op[9 ], exported->priv, op[8 ]); + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL );if (!req)@@ -120,8 +125,9 @@ static int send_export_msg(struct exported_sgt_info *exported,/* Fast path exporting routine in case same buffer is already exported. * -* If same buffer is still valid and exist in EXPORT LIST it returns 0 so -* that remaining normal export process can be skipped. +* If same buffer is still valid and exist in EXPORT LIST, it only updates +* user-private data for the buffer and returns 0 so that that it can skip +* normal export process. * * If "unexport" is scheduled for the buffer, it cancels it since the buffer * is being re-exported. @@ -129,7 +135,7 @@ static int send_export_msg(struct exported_sgt_info *exported,* return '1' if reexport is needed, return '0' if succeeds, return * Kernel error code if something goes wrong */ -static int fastpath_export (hyper_dmabuf_id_t hid) +static int fastpath_export (hyper_dmabuf_id_t hid,int sz_priv,char *priv) {int reexport =1 ;int ret =0 ;@@ -155,6 +161,46 @@ static int fastpath_export(hyper_dmabuf_id_t hid) exported->unexport_sched = false; } +/* if there's any change in size of private data. +* we reallocate space for private data with new size +*/ +if (sz_priv != exported->sz_priv) { +kfree (exported->priv); + +/* truncating size */ +if (sz_priv >MAX_SIZE_PRIV_DATA ) + exported->sz_priv =MAX_SIZE_PRIV_DATA ; +else + exported->sz_priv = sz_priv; + + exported->priv =kcalloc (1 , exported->sz_priv, +GFP_KERNEL ); + +if (!exported->priv) { +hyper_dmabuf_remove_exported (exported->hid); +hyper_dmabuf_cleanup_sgt_info (exported, true); +kfree (exported); +return -ENOMEM ; + } + } + +/* update private data in sgt_info with new ones */ + ret =copy_from_user (exported->priv, priv, exported->sz_priv); +if (ret) { +dev_err (hy_drv_priv->dev, +"Failed to load a new private data\n" ); + ret = -EINVAL ; + }else { +/* send an export msg for updating priv in importer */ + ret =send_export_msg (exported,NULL ); + +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"Failed to send a new private data\n" ); + ret = -EBUSY ; + } + } +return ret; }@@ -191,7 +237,8 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) export_remote_attr->remote_domain);if (hid.id !=-1 ) { - ret =fastpath_export (hid); + ret =fastpath_export (hid, export_remote_attr->sz_priv, + export_remote_attr->priv);/* return if fastpath_export succeeds or * gets some fatal error @@ -225,6 +272,24 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) goto fail_sgt_info_creation; } +/* possible truncation */ +if (export_remote_attr->sz_priv >MAX_SIZE_PRIV_DATA ) + exported->sz_priv =MAX_SIZE_PRIV_DATA ; +else + exported->sz_priv = export_remote_attr->sz_priv; + +/* creating buffer for private data of buffer */ +if (exported->sz_priv !=0 ) { + exported->priv =kcalloc (1 , exported->sz_priv,GFP_KERNEL ); + +if (!exported->priv) { + ret = -ENOMEM ; + goto fail_priv_creation; + } + }else { +dev_err (hy_drv_priv->dev,"size is 0\n" ); + } + exported->hid =hyper_dmabuf_get_hid ();/* no more exported dmabuf allowed */ @@ -279,6 +344,10 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data)INIT_LIST_HEAD (&exported->va_kmapped->list);INIT_LIST_HEAD (&exported->va_vmapped->list); +/* copy private data to sgt_info */ + ret =copy_from_user (exported->priv, export_remote_attr->priv, + exported->sz_priv); +if (ret) {dev_err (hy_drv_priv->dev,"failed to load private data\n" );@@ -337,6 +406,9 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) fail_map_active_attached:kfree (exported->active_sgts); +kfree (exported->priv); + +fail_priv_creation:kfree (exported); fail_map_active_sgts:@@ -567,6 +639,9 @@ static void delayed_unexport(struct work_struct *work)/* register hyper_dmabuf_id to the list for reuse */ hyper_dmabuf_store_hid (exported->hid); +if (exported->sz_priv >0 && !exported->priv) +kfree (exported->priv); +kfree (exported); } }drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c +25
-1@@ -60,9 +60,12 @@ void hyper_dmabuf_create_req(struct hyper_dmabuf_req *req,* op5 : offset of data in the first page * op6 : length of data in the last page * op7 : top-level reference number for shared pages +* op8 : size of private data (from op9) +* op9 ~ : Driver-specific private data +* (e.g. graphic buffer's meta info) */ -memcpy (&req->op[0 ], &op[0 ],8 *sizeof (int ) + op[8 ]); +memcpy (&req->op[0 ], &op[0 ],9 *sizeof (int ) + op[8 ]);break ;case HYPER_DMABUF_NOTIFY_UNEXPORT :@@ -116,6 +119,9 @@ static void cmd_process_work(struct work_struct *work)* op5 : offset of data in the first page * op6 : length of data in the last page * op7 : top-level reference number for shared pages +* op8 : size of private data (from op9) +* op9 ~ : Driver-specific private data +* (e.g. graphic buffer's meta info) */ /* if nents == 0, it means it is a message only for @@ -135,6 +141,24 @@ static void cmd_process_work(struct work_struct *work)break ; } +/* if size of new private data is different, +* we reallocate it. +*/ +if (imported->sz_priv != req->op[8 ]) { +kfree (imported->priv); + imported->sz_priv = req->op[8 ]; + imported->priv =kcalloc (1 , req->op[8 ], +GFP_KERNEL ); +if (!imported->priv) { +/* set it invalid */ + imported->valid =0 ; +break ; + } + } + +/* updating priv data */ +memcpy (imported->priv, &req->op[9 ], req->op[8 ]); +break ; }
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c static void cmd_process_work(struct work_struct *work) · line 171
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c +2
-0@@ -143,6 +167,14 @@ static void cmd_process_work(struct work_struct *work)if (!imported)break ; + imported->sz_priv = req->op[8 ]; + imported->priv =kcalloc (1 , req->op[8 ],GFP_KERNEL );
BTW, there are plenty of the code using kcalloc with 1 element Why not simply kzalloc?
Quoted 23 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c +8
-0+ +if (!imported->priv) { +kfree (imported); +break ; + } + imported->hid.id = req->op[0 ];for (i =0 ; i <3 ; i++)@@ -162,6 +194,8 @@ static void cmd_process_work(struct work_struct *work)dev_dbg (hy_drv_priv->dev,"\tlast len %d\n" , req->op[6 ]);dev_dbg (hy_drv_priv->dev,"\tgrefid %d\n" , req->op[7 ]); +memcpy (imported->priv, &req->op[9 ], req->op[8 ]); + imported->valid = true;hyper_dmabuf_register_imported (imported);drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h +0
-0
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h line 31
Quoted 6 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h +1
-1@@ -27,7 +27,7 @@#ifndef __HYPER_DMABUF_MSG_H__#define __HYPER_DMABUF_MSG_H__ -#define MAX_NUMBER_OF_OPERANDS 8 +#define MAX_NUMBER_OF_OPERANDS 64
So now the req/resp below become (64 + 3) ints long, 268 bytes 4096 / 268...
Quoted 59 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h +0
-0struct hyper_dmabuf_req {unsigned int req_id;drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c +1
-0@@ -251,6 +251,7 @@ int hyper_dmabuf_cleanup_sgt_info(struct exported_sgt_info *exported,kfree (exported->active_attached);kfree (exported->va_kmapped);kfree (exported->va_vmapped); +kfree (exported->priv);return 0 ; }drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h +12
-0@@ -101,6 +101,12 @@ struct exported_sgt_info {* the buffer can be completely freed. */ struct file *filp; + +/* size of private */ +size_t sz_priv; + +/* private data associated with the exported buffer */ +char *priv; };/* imported_sgt_info contains information about imported DMA_BUF @@ -126,6 +132,12 @@ struct imported_sgt_info {void *refs_info;bool valid;int importers; + +/* size of private */ +size_t sz_priv; + +/* private data associated with the exported buffer */ + char *priv; };#endif /* __HYPER_DMABUF_STRUCT_H__ */ include/uapi/linux/hyper_dmabuf.h +2
-0@@ -25,6 +25,8 @@ #ifndef __LINUX_PUBLIC_HYPER_DMABUF_H__ #define __LINUX_PUBLIC_HYPER_DMABUF_H__ +#define MAX_SIZE_PRIV_DATA 192 + typedef struct { int id; int rng_key[3]; /* 12bytes long random number */
include/uapi/linux/hyper_dmabuf.h struct ioctl_hyper_dmabuf_export_remote { · line 65
Quoted 8 lines
include/uapi/linux/hyper_dmabuf.h +2
-0@@ -56,6 +58,8 @@ struct ioctl_hyper_dmabuf_export_remote {int remote_domain;/* exported dma buf id */ hyper_dmabuf_id_t hid; +int sz_priv; +char *priv; };#define IOCTL_HYPER_DMABUF_EXPORT_FD \
_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [RFC, v2, 4/9] hyper_dmabuf: user private data attached to hyper_DMABUF
In reply to Dongwon KimQuoted 219 lines · Dongwon Kim
Define a private data (e.g. meta data for the buffer) attached to each
hyper_DMABUFstructure. This data is provided by userapace viaexport_remoteIOCTL and its size can be up to 192 bytes.Signed-off-by Dongwon Kim <[email protected]>Signed-off-by Mateusz Polrola <[email protected]>--- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c | 83 ++++++++++++++++++++-- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c | 36 +++++++++- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h | 2 +- .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c | 1 + drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h | 12 ++++ include/uapi/linux/hyper_dmabuf.h | 4 ++ 6 files changed, 132 insertions(+), 6 deletions(-)drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c +79
-4@@ -103,6 +103,11 @@ static int send_export_msg(struct exported_sgt_info *exported, } } + op[8 ] = exported->sz_priv; + +/* driver/application specific private info */ +memcpy (&op[9 ], exported->priv, op[8 ]); + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL );if (!req)@@ -120,8 +125,9 @@ static int send_export_msg(struct exported_sgt_info *exported,/* Fast path exporting routine in case same buffer is already exported. * -* If same buffer is still valid and exist in EXPORT LIST it returns 0 so -* that remaining normal export process can be skipped. +* If same buffer is still valid and exist in EXPORT LIST, it only updates +* user-private data for the buffer and returns 0 so that that it can skip +* normal export process. * * If "unexport" is scheduled for the buffer, it cancels it since the buffer * is being re-exported. @@ -129,7 +135,7 @@ static int send_export_msg(struct exported_sgt_info *exported,* return '1' if reexport is needed, return '0' if succeeds, return * Kernel error code if something goes wrong */ -static int fastpath_export (hyper_dmabuf_id_t hid) +static int fastpath_export (hyper_dmabuf_id_t hid,int sz_priv,char *priv) {int reexport =1 ;int ret =0 ;@@ -155,6 +161,46 @@ static int fastpath_export(hyper_dmabuf_id_t hid) exported->unexport_sched = false; } +/* if there's any change in size of private data. +* we reallocate space for private data with new size +*/ +if (sz_priv != exported->sz_priv) { +kfree (exported->priv); + +/* truncating size */ +if (sz_priv >MAX_SIZE_PRIV_DATA ) + exported->sz_priv =MAX_SIZE_PRIV_DATA ; +else + exported->sz_priv = sz_priv; + + exported->priv =kcalloc (1 , exported->sz_priv, +GFP_KERNEL ); + +if (!exported->priv) { +hyper_dmabuf_remove_exported (exported->hid); +hyper_dmabuf_cleanup_sgt_info (exported, true); +kfree (exported); +return -ENOMEM ; + } + } + +/* update private data in sgt_info with new ones */ + ret =copy_from_user (exported->priv, priv, exported->sz_priv); +if (ret) { +dev_err (hy_drv_priv->dev, +"Failed to load a new private data\n" ); + ret = -EINVAL ; + }else { +/* send an export msg for updating priv in importer */ + ret =send_export_msg (exported,NULL ); + +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"Failed to send a new private data\n" ); + ret = -EBUSY ; + } + } +return ret; }@@ -191,7 +237,8 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) export_remote_attr->remote_domain);if (hid.id !=-1 ) { - ret =fastpath_export (hid); + ret =fastpath_export (hid, export_remote_attr->sz_priv, + export_remote_attr->priv);/* return if fastpath_export succeeds or * gets some fatal error @@ -225,6 +272,24 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) goto fail_sgt_info_creation; } +/* possible truncation */ +if (export_remote_attr->sz_priv >MAX_SIZE_PRIV_DATA ) + exported->sz_priv =MAX_SIZE_PRIV_DATA ; +else + exported->sz_priv = export_remote_attr->sz_priv; + +/* creating buffer for private data of buffer */ +if (exported->sz_priv !=0 ) { + exported->priv =kcalloc (1 , exported->sz_priv,GFP_KERNEL ); + +if (!exported->priv) { + ret = -ENOMEM ; + goto fail_priv_creation; + } + }else { +dev_err (hy_drv_priv->dev,"size is 0\n" ); + } + exported->hid =hyper_dmabuf_get_hid ();/* no more exported dmabuf allowed */ @@ -279,6 +344,10 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data)INIT_LIST_HEAD (&exported->va_kmapped->list);INIT_LIST_HEAD (&exported->va_vmapped->list); +/* copy private data to sgt_info */ + ret =copy_from_user (exported->priv, export_remote_attr->priv, + exported->sz_priv); +if (ret) {dev_err (hy_drv_priv->dev,"failed to load private data\n" );@@ -337,6 +406,9 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) fail_map_active_attached:kfree (exported->active_sgts); +kfree (exported->priv); + +fail_priv_creation:kfree (exported); fail_map_active_sgts:@@ -567,6 +639,9 @@ static void delayed_unexport(struct work_struct *work)/* register hyper_dmabuf_id to the list for reuse */ hyper_dmabuf_store_hid (exported->hid); +if (exported->sz_priv >0 && !exported->priv) +kfree (exported->priv); +kfree (exported); } }drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c +25
-1@@ -60,9 +60,12 @@ void hyper_dmabuf_create_req(struct hyper_dmabuf_req *req,* op5 : offset of data in the first page * op6 : length of data in the last page * op7 : top-level reference number for shared pages +* op8 : size of private data (from op9) +* op9 ~ : Driver-specific private data +* (e.g. graphic buffer's meta info) */ -memcpy (&req->op[0 ], &op[0 ],8 *sizeof (int ) + op[8 ]); +memcpy (&req->op[0 ], &op[0 ],9 *sizeof (int ) + op[8 ]);break ;case HYPER_DMABUF_NOTIFY_UNEXPORT :@@ -116,6 +119,9 @@ static void cmd_process_work(struct work_struct *work)* op5 : offset of data in the first page * op6 : length of data in the last page * op7 : top-level reference number for shared pages +* op8 : size of private data (from op9) +* op9 ~ : Driver-specific private data +* (e.g. graphic buffer's meta info) */ /* if nents == 0, it means it is a message only for @@ -135,6 +141,24 @@ static void cmd_process_work(struct work_struct *work)break ; } +/* if size of new private data is different, +* we reallocate it. +*/ +if (imported->sz_priv != req->op[8 ]) { +kfree (imported->priv); + imported->sz_priv = req->op[8 ]; + imported->priv =kcalloc (1 , req->op[8 ], +GFP_KERNEL ); +if (!imported->priv) { +/* set it invalid */ + imported->valid =0 ; +break ; + } + } + +/* updating priv data */ +memcpy (imported->priv, &req->op[9 ], req->op[8 ]); +break ; }
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c static void cmd_process_work(struct work_struct *work) · line 171
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c +2
-0@@ -143,6 +167,14 @@ static void cmd_process_work(struct work_struct *work)if (!imported)break ; + imported->sz_priv = req->op[8 ]; + imported->priv =kcalloc (1 , req->op[8 ],GFP_KERNEL );
BTW, there are plenty of the code using kcalloc with 1 element Why not simply kzalloc?
Quoted 23 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c +8
-0+ +if (!imported->priv) { +kfree (imported); +break ; + } + imported->hid.id = req->op[0 ];for (i =0 ; i <3 ; i++)@@ -162,6 +194,8 @@ static void cmd_process_work(struct work_struct *work)dev_dbg (hy_drv_priv->dev,"\tlast len %d\n" , req->op[6 ]);dev_dbg (hy_drv_priv->dev,"\tgrefid %d\n" , req->op[7 ]); +memcpy (imported->priv, &req->op[9 ], req->op[8 ]); + imported->valid = true;hyper_dmabuf_register_imported (imported);drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h +0
-0
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h line 31
Quoted 6 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h +1
-1@@ -27,7 +27,7 @@#ifndef __HYPER_DMABUF_MSG_H__#define __HYPER_DMABUF_MSG_H__ -#define MAX_NUMBER_OF_OPERANDS 8 +#define MAX_NUMBER_OF_OPERANDS 64
So now the req/resp below become (64 + 3) ints long, 268 bytes 4096 / 268...
Quoted 59 lines
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h +0
-0struct hyper_dmabuf_req {unsigned int req_id;drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c +1
-0@@ -251,6 +251,7 @@ int hyper_dmabuf_cleanup_sgt_info(struct exported_sgt_info *exported,kfree (exported->active_attached);kfree (exported->va_kmapped);kfree (exported->va_vmapped); +kfree (exported->priv);return 0 ; }drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h +12
-0@@ -101,6 +101,12 @@ struct exported_sgt_info {* the buffer can be completely freed. */ struct file *filp; + +/* size of private */ +size_t sz_priv; + +/* private data associated with the exported buffer */ +char *priv; };/* imported_sgt_info contains information about imported DMA_BUF @@ -126,6 +132,12 @@ struct imported_sgt_info {void *refs_info;bool valid;int importers; + +/* size of private */ +size_t sz_priv; + +/* private data associated with the exported buffer */ + char *priv; };#endif /* __HYPER_DMABUF_STRUCT_H__ */ include/uapi/linux/hyper_dmabuf.h +2
-0@@ -25,6 +25,8 @@ #ifndef __LINUX_PUBLIC_HYPER_DMABUF_H__ #define __LINUX_PUBLIC_HYPER_DMABUF_H__ +#define MAX_SIZE_PRIV_DATA 192 + typedef struct { int id; int rng_key[3]; /* 12bytes long random number */
include/uapi/linux/hyper_dmabuf.h struct ioctl_hyper_dmabuf_export_remote { · line 65
Quoted 8 lines
include/uapi/linux/hyper_dmabuf.h +2
-0@@ -56,6 +58,8 @@ struct ioctl_hyper_dmabuf_export_remote {int remote_domain;/* exported dma buf id */ hyper_dmabuf_id_t hid; +int sz_priv; +char *priv; };#define IOCTL_HYPER_DMABUF_EXPORT_FD \
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
RFC PATCH v2 4/9 hyper_dmabuf: user private data attached to hyper_DMABUF
In reply to Dongwon KimDefine a private data (e.g. meta data for the buffer) attached to each hyper_DMABUF structure. This data is provided by userapace via export_remote IOCTL and its size can be up to 192 bytes.
--- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c | 83 ++++++++++++++++++++-- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c | 36 +++++++++- drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h | 2 +- .../dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c | 1 + drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h | 12 ++++ include/uapi/linux/hyper_dmabuf.h | 4 ++ 6 files changed, 132 insertions(+), 6 deletions(-)
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_ioctl.c +79 -4
@@ -103,6 +103,11 @@ static int send_export_msg(struct exported_sgt_info *exported, } } + op[8 ] = exported->sz_priv; + +/* driver/application specific private info */ +memcpy (&op[9 ], exported->priv, op[8 ]); + req =kcalloc (1 ,sizeof (*req),GFP_KERNEL );if (!req)
@@ -120,8 +125,9 @@ static int send_export_msg(struct exported_sgt_info *exported,/* Fast path exporting routine in case same buffer is already exported. * -* If same buffer is still valid and exist in EXPORT LIST it returns 0 so -* that remaining normal export process can be skipped. +* If same buffer is still valid and exist in EXPORT LIST, it only updates +* user-private data for the buffer and returns 0 so that that it can skip +* normal export process. * * If "unexport" is scheduled for the buffer, it cancels it since the buffer * is being re-exported.
@@ -129,7 +135,7 @@ static int send_export_msg(struct exported_sgt_info *exported,* return '1' if reexport is needed, return '0' if succeeds, return * Kernel error code if something goes wrong */ -static int fastpath_export (hyper_dmabuf_id_t hid) +static int fastpath_export (hyper_dmabuf_id_t hid,int sz_priv,char *priv) {int reexport =1 ;int ret =0 ;
@@ -155,6 +161,46 @@ static int fastpath_export(hyper_dmabuf_id_t hid) exported->unexport_sched = false; } +/* if there's any change in size of private data. +* we reallocate space for private data with new size +*/ +if (sz_priv != exported->sz_priv) { +kfree (exported->priv); + +/* truncating size */ +if (sz_priv >MAX_SIZE_PRIV_DATA ) + exported->sz_priv =MAX_SIZE_PRIV_DATA ; +else + exported->sz_priv = sz_priv; + + exported->priv =kcalloc (1 , exported->sz_priv, +GFP_KERNEL ); + +if (!exported->priv) { +hyper_dmabuf_remove_exported (exported->hid); +hyper_dmabuf_cleanup_sgt_info (exported, true); +kfree (exported); +return -ENOMEM ; + } + } + +/* update private data in sgt_info with new ones */ + ret =copy_from_user (exported->priv, priv, exported->sz_priv); +if (ret) { +dev_err (hy_drv_priv->dev, +"Failed to load a new private data\n" ); + ret = -EINVAL ; + }else { +/* send an export msg for updating priv in importer */ + ret =send_export_msg (exported,NULL ); + +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"Failed to send a new private data\n" ); + ret = -EBUSY ; + } + } +return ret; }
@@ -191,7 +237,8 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) export_remote_attr->remote_domain);if (hid.id !=-1 ) { - ret =fastpath_export (hid); + ret =fastpath_export (hid, export_remote_attr->sz_priv, + export_remote_attr->priv);/* return if fastpath_export succeeds or * gets some fatal error
@@ -225,6 +272,24 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) goto fail_sgt_info_creation; } +/* possible truncation */ +if (export_remote_attr->sz_priv >MAX_SIZE_PRIV_DATA ) + exported->sz_priv =MAX_SIZE_PRIV_DATA ; +else + exported->sz_priv = export_remote_attr->sz_priv; + +/* creating buffer for private data of buffer */ +if (exported->sz_priv !=0 ) { + exported->priv =kcalloc (1 , exported->sz_priv,GFP_KERNEL ); + +if (!exported->priv) { + ret = -ENOMEM ; + goto fail_priv_creation; + } + }else { +dev_err (hy_drv_priv->dev,"size is 0\n" ); + } + exported->hid =hyper_dmabuf_get_hid ();/* no more exported dmabuf allowed */
@@ -279,6 +344,10 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data)INIT_LIST_HEAD (&exported->va_kmapped->list);INIT_LIST_HEAD (&exported->va_vmapped->list); +/* copy private data to sgt_info */ + ret =copy_from_user (exported->priv, export_remote_attr->priv, + exported->sz_priv); +if (ret) {dev_err (hy_drv_priv->dev,"failed to load private data\n" );
@@ -337,6 +406,9 @@ static int hyper_dmabuf_export_remote_ioctl(struct file *filp, void *data) fail_map_active_attached:kfree (exported->active_sgts); +kfree (exported->priv); + +fail_priv_creation:kfree (exported); fail_map_active_sgts:
@@ -567,6 +639,9 @@ static void delayed_unexport(struct work_struct *work)/* register hyper_dmabuf_id to the list for reuse */ hyper_dmabuf_store_hid (exported->hid); +if (exported->sz_priv >0 && !exported->priv) +kfree (exported->priv); +kfree (exported); } }
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.c +35 -1
@@ -60,9 +60,12 @@ void hyper_dmabuf_create_req(struct hyper_dmabuf_req *req,* op5 : offset of data in the first page * op6 : length of data in the last page * op7 : top-level reference number for shared pages +* op8 : size of private data (from op9) +* op9 ~ : Driver-specific private data +* (e.g. graphic buffer's meta info) */ -memcpy (&req->op[0 ], &op[0 ],8 *sizeof (int ) + op[8 ]); +memcpy (&req->op[0 ], &op[0 ],9 *sizeof (int ) + op[8 ]);break ;case HYPER_DMABUF_NOTIFY_UNEXPORT :
@@ -116,6 +119,9 @@ static void cmd_process_work(struct work_struct *work)* op5 : offset of data in the first page * op6 : length of data in the last page * op7 : top-level reference number for shared pages +* op8 : size of private data (from op9) +* op9 ~ : Driver-specific private data +* (e.g. graphic buffer's meta info) */ /* if nents == 0, it means it is a message only for
@@ -135,6 +141,24 @@ static void cmd_process_work(struct work_struct *work)break ; } +/* if size of new private data is different, +* we reallocate it. +*/ +if (imported->sz_priv != req->op[8 ]) { +kfree (imported->priv); + imported->sz_priv = req->op[8 ]; + imported->priv =kcalloc (1 , req->op[8 ], +GFP_KERNEL ); +if (!imported->priv) { +/* set it invalid */ + imported->valid =0 ; +break ; + } + } + +/* updating priv data */ +memcpy (imported->priv, &req->op[9 ], req->op[8 ]); +break ; }
@@ -143,6 +167,14 @@ static void cmd_process_work(struct work_struct *work)if (!imported)break ; + imported->sz_priv = req->op[8 ]; + imported->priv =kcalloc (1 , req->op[8 ],GFP_KERNEL ); + +if (!imported->priv) { +kfree (imported); +break ; + } + imported->hid.id = req->op[0 ];for (i =0 ; i <3 ; i++)
@@ -162,6 +194,8 @@ static void cmd_process_work(struct work_struct *work)dev_dbg (hy_drv_priv->dev,"\tlast len %d\n" , req->op[6 ]);dev_dbg (hy_drv_priv->dev,"\tgrefid %d\n" , req->op[7 ]); +memcpy (imported->priv, &req->op[9 ], req->op[8 ]); + imported->valid = true;hyper_dmabuf_register_imported (imported);
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_msg.h +1 -1
@@ -27,7 +27,7 @@ #ifndef __HYPER_DMABUF_MSG_H__ #define __HYPER_DMABUF_MSG_H__ -#define MAX_NUMBER_OF_OPERANDS 8 +#define MAX_NUMBER_OF_OPERANDS 64 struct hyper_dmabuf_req { unsigned int req_id;
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_sgl_proc.c +1 -0
@@ -251,6 +251,7 @@ int hyper_dmabuf_cleanup_sgt_info(struct exported_sgt_info *exported,kfree (exported->active_attached);kfree (exported->va_kmapped);kfree (exported->va_vmapped); +kfree (exported->priv);return 0 ; }
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_struct.h +12 -0
@@ -101,6 +101,12 @@ struct exported_sgt_info {* the buffer can be completely freed. */ struct file *filp; + +/* size of private */ +size_t sz_priv; + +/* private data associated with the exported buffer */ +char *priv; };/* imported_sgt_info contains information about imported DMA_BUF
@@ -126,6 +132,12 @@ struct imported_sgt_info {void *refs_info;bool valid;int importers; + +/* size of private */ +size_t sz_priv; + +/* private data associated with the exported buffer */ + char *priv; };#endif /* __HYPER_DMABUF_STRUCT_H__ */
include/uapi/linux/hyper_dmabuf.h +4 -0
@@ -25,6 +25,8 @@ #ifndef __LINUX_PUBLIC_HYPER_DMABUF_H__ #define __LINUX_PUBLIC_HYPER_DMABUF_H__ +#define MAX_SIZE_PRIV_DATA 192 + typedef struct { int id; int rng_key[3]; /* 12bytes long random number */
@@ -56,6 +58,8 @@ struct ioctl_hyper_dmabuf_export_remote {int remote_domain;/* exported dma buf id */ hyper_dmabuf_id_t hid; +int sz_priv; +char *priv; };#define IOCTL_HYPER_DMABUF_EXPORT_FD \
Signature · 7 lines
2.16.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
RFC PATCH v2 5/9 hyper_dmabuf: default backend for XEN hypervisor
In reply to Dongwon KimFrom: "Matuesz Polrola" <[email protected]>
The default backend for XEN hypervisor. This backend contains actual implementation of individual methods defined in "struct hyper_dmabuf_bknd_ops" defined as:
struct hyper_dmabuf_bknd_ops {
/* backend initialization routine (optional) */
int (*init)(void);
/* backend cleanup routine (optional) */
int (*cleanup)(void);
/* retreiving id of current virtual machine */
int (*get_vm_id)(void);
/* get pages shared via hypervisor-specific method */
int (*share_pages)(struct page **, int, int, void **);
/* make shared pages unshared via hypervisor specific method */
int (*unshare_pages)(void **, int);
/* map remotely shared pages on importer's side via
* hypervisor-specific method
*/
struct page ** (*map_shared_pages)(unsigned long, int, int, void **);
/* unmap and free shared pages on importer's side via
* hypervisor-specific method
*/
int (*unmap_shared_pages)(void **, int);
/* initialize communication environment */
int (*init_comm_env)(void);
void (*destroy_comm)(void);
/* upstream ch setup (receiving and responding) */
int (*init_rx_ch)(int);
/* downstream ch setup (transmitting and parsing responses) */
int (*init_tx_ch)(int);
int (*send_req)(int, struct hyper_dmabuf_req *, int);
};First two methods are for extra initialization or cleaning up possibly required for the current Hypervisor (optional). Third method (.get_vm_id) provides a way to get current VM's id, which will be used as an identication of source VM of shared hyper_DMABUF later.
All other methods are related to either memory sharing or inter-VM communication, which are minimum requirement for hyper_DMABUF driver. (Brief description of role of each method is embedded as a comment in the definition of the structure above and header file.)
Actual implementation of each of these methods specific to XEN is under backends/xen/. Their mappings are done as followed:
struct hyper_dmabuf_bknd_ops xen_bknd_ops = {
.init = NULL, /* not needed for xen */ .cleanup = NULL, /* not needed for xen */ .get_vm_id = xen_be_get_domid, .share_pages = xen_be_share_pages, .unshare_pages = xen_be_unshare_pages, .map_shared_pages = (void *)xen_be_map_shared_pages, .unmap_shared_pages = xen_be_unmap_shared_pages, .init_comm_env = xen_be_init_comm_env, .destroy_comm = xen_be_destroy_comm, .init_rx_ch = xen_be_init_rx_rbuf, .init_tx_ch = xen_be_init_tx_rbuf, .send_req = xen_be_send_req,
};
A section for Hypervisor Backend has been added to
"Documentation/hyper-dmabuf-sharing.txt" accordingly
--- drivers/dma-buf/hyper_dmabuf/Kconfig | 7 + drivers/dma-buf/hyper_dmabuf/Makefile | 7 + .../backends/xen/hyper_dmabuf_xen_comm.c | 941 +++++++++++++++++++++ .../backends/xen/hyper_dmabuf_xen_comm.h | 78 ++ .../backends/xen/hyper_dmabuf_xen_comm_list.c | 158 ++++ .../backends/xen/hyper_dmabuf_xen_comm_list.h | 67 ++ .../backends/xen/hyper_dmabuf_xen_drv.c | 46 + .../backends/xen/hyper_dmabuf_xen_drv.h | 53 ++ .../backends/xen/hyper_dmabuf_xen_shm.c | 525 ++++++++++++ .../backends/xen/hyper_dmabuf_xen_shm.h | 46 + drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c | 10 + 11 files changed, 1938 insertions(+) create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.h create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm_list.c create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm_list.h create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_drv.c create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_drv.h create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.c create mode 100644 drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.h
drivers/dma-buf/hyper_dmabuf/Kconfig +7 -0
@@ -20,4 +20,11 @@ config HYPER_DMABUF_SYSFS The location of sysfs is under "...." +config HYPER_DMABUF_XEN + bool "Configure hyper_dmabuf for XEN hypervisor" + default y + depends on HYPER_DMABUF && XEN && XENFS + help + Enabling Hyper_DMABUF Backend for XEN hypervisor + endmenu
drivers/dma-buf/hyper_dmabuf/Makefile +7 -0
@@ -10,6 +10,13 @@ ifneq ($(KERNELRELEASE),) hyper_dmabuf_msg.o \ hyper_dmabuf_id.o \ +ifeq ($(CONFIG_HYPER_DMABUF_XEN), y) + $(TARGET_MODULE)-objs += backends/xen/hyper_dmabuf_xen_comm.o \ + backends/xen/hyper_dmabuf_xen_comm_list.o \ + backends/xen/hyper_dmabuf_xen_shm.o \ + backends/xen/hyper_dmabuf_xen_drv.o +endif + obj-$(CONFIG_HYPER_DMABUF) := $(TARGET_MODULE).o # If we are running without kernel build system
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +941 -0
941 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.h new file +78 -0
78 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm_list.c new file +158 -0
158 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm_list.h new file +67 -0
67 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_drv.c new file +46 -0
46 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_drv.h new file +53 -0
53 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.c new file +525 -0
525 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.h new file +46 -0
46 lines not on this page · raw message
drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c +10 -0
@@ -42,6 +42,10 @@#include "hyper_dmabuf_list.h" #include "hyper_dmabuf_id.h" +#ifdef CONFIG_HYPER_DMABUF_XEN +#include "backends/xen/hyper_dmabuf_xen_drv.h" +#endif +MODULE_LICENSE ("GPL and additional rights" );MODULE_AUTHOR ("Intel Corporation" );
@@ -145,7 +149,13 @@ static int __init hyper_dmabuf_drv_init(void)return ret; } +/* currently only supports XEN hypervisor */ +#ifdef CONFIG_HYPER_DMABUF_XEN + hy_drv_priv->bknd_ops = &xen_bknd_ops; +#else hy_drv_priv->bknd_ops =NULL ; +pr_err ("hyper_dmabuf drv currently supports XEN only.\n" ); +#endif if (hy_drv_priv->bknd_ops ==NULL ) {pr_err ("Hyper_dmabuf: no backend found\n" );
Signature · 6 lines
2.16.1 _______________________________________________ dri-devel mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/dri-devel
Re: [RFC,v2,5/9] hyper_dmabuf: default backend for XEN hypervisor
Quoted 107 lines · Dongwon Kim
From: "Matuesz Polrola" <[email protected]>
The default backend for XEN hypervisor. This backend contains actual implementation of individual methods defined in "struct
hyper_dmabuf_bknd_ops" defined as:struct hyper_dmabuf_bknd_ops { /* backend initialization routine (optional) */ int (*init)(void); /* backend cleanup routine (optional) */ int (*cleanup)(void); /* retreiving id of current virtual machine */ int (*get_vm_id)(void); /* get pages shared via hypervisor-specific method */ int (*share_pages)(struct page **, int, int, void **); /* make shared pages unshared via hypervisor specific method */ int (*unshare_pages)(void **, int); /* map remotely shared pages on importer's side via * hypervisor-specific method */ struct page ** (*map_shared_pages)(unsigned long, int, int, void **); /* unmap and free shared pages on importer's side via * hypervisor-specific method */ int (*unmap_shared_pages)(void **, int); /* initialize communication environment */ int (*init_comm_env)(void); void (*destroy_comm)(void); /* upstream ch setup (receiving and responding) */ int (*init_rx_ch)(int); /* downstream ch setup (transmitting and parsing responses) */ int (*init_tx_ch)(int); int (*send_req)(int, struct hyper_dmabuf_req *, int); };First two methods are for extra initialization or cleaning up possibly required for the current Hypervisor (optional). Third method (.get_vm_id) provides a way to get current VM's id, which will be used as an identication of source VM of shared
hyper_DMABUFlater.All other methods are related to either memory sharing or inter-VM communication, which are minimum requirement for
hyper_DMABUFdriver. (Brief description of role of each method is embedded as a comment in the definition of the structure above and header file.)Actual implementation of each of these methods specific to XEN is under backends/xen/. Their mappings are done as followed:
struct
hyper_dmabuf_bknd_opsxen_bknd_ops= {
.init = NULL, /* not needed for xen */ .cleanup = NULL, /* not needed for xen */ .get_vm_id =xen_be_get_domid, .share_pages =xen_be_share_pages, .unshare_pages =xen_be_unshare_pages, .map_shared_pages = (void *)xen_be_map_shared_pages, .unmap_shared_pages =xen_be_unmap_shared_pages, .init_comm_env =xen_be_init_comm_env, .destroy_comm =xen_be_destroy_comm, .init_rx_ch =xen_be_init_rx_rbuf, .init_tx_ch =xen_be_init_tx_rbuf, .send_req =xen_be_send_req,
};A section for Hypervisor Backend has been added to
"
Documentation/hyper-dmabuf-sharing.txt" accordinglySigned-off-by Dongwon Kim <[email protected]>Signed-off-by Mateusz Polrola <[email protected]>--- drivers/dma-buf/hyper_dmabuf/Kconfig | 7 + drivers/dma-buf/hyper_dmabuf/Makefile | 7 + .../backends/xen/hyper_dmabuf_xen_comm.c | 941 +++++++++++++++++++++ .../backends/xen/hyper_dmabuf_xen_comm.h | 78 ++ .../backends/xen/hyper_dmabuf_xen_comm_list.c | 158 ++++ .../backends/xen/hyper_dmabuf_xen_comm_list.h | 67 ++ .../backends/xen/hyper_dmabuf_xen_drv.c | 46 + .../backends/xen/hyper_dmabuf_xen_drv.h | 53 ++ .../backends/xen/hyper_dmabuf_xen_shm.c | 525 ++++++++++++ .../backends/xen/hyper_dmabuf_xen_shm.h | 46 + drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c | 10 + 11 files changed, 1938 insertions(+)create mode 100644
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm_list.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm_list.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_drv.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_drv.hcreate mode 100644drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.ccreate mode 100644drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.hdrivers/dma-buf/hyper_dmabuf/Kconfig +0
-0
drivers/dma-buf/hyper_dmabuf/Kconfig config HYPER_DMABUF_SYSFS · line 25
Quoted 6 lines
drivers/dma-buf/hyper_dmabuf/Kconfig +3
-0@@ -20,4 +20,11 @@ config HYPER_DMABUF_SYSFS The location of sysfs is under "...." +config HYPER_DMABUF_XEN + bool "Configure hyper_dmabuf for XEN hypervisor" + default y
n?
Quoted 60 lines
drivers/dma-buf/hyper_dmabuf/Kconfig +4
-0+ depends on HYPER_DMABUF && XEN && XENFS + help + Enabling Hyper_DMABUF Backend for XEN hypervisor + endmenudrivers/dma-buf/hyper_dmabuf/Makefile +7
-0@@ -10,6 +10,13 @@ ifneq ($(KERNELRELEASE),) hyper_dmabuf_msg.o \ hyper_dmabuf_id.o \ +ifeq ($(CONFIG_HYPER_DMABUF_XEN), y) + $(TARGET_MODULE)-objs += backends/xen/hyper_dmabuf_xen_comm.o \ + backends/xen/hyper_dmabuf_xen_comm_list.o \ + backends/xen/hyper_dmabuf_xen_shm.o \ + backends/xen/hyper_dmabuf_xen_drv.o +endif + obj-$(CONFIG_HYPER_DMABUF) := $(TARGET_MODULE).o # If we are running without kernel build systemdrivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +31
-0@@ -0,0 +1,941 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/workqueue.h>
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 41
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+#include <linux/delay.h> +#include <xen/grant_table.h> +#include <xen/events.h> +#include <xen/xenbus.h> +#include <asm/xen/page.h> +#include "hyper_dmabuf_xen_comm.h" +#include "hyper_dmabuf_xen_comm_list.h" +#include "../../hyper_dmabuf_drv.h" + +static int export_req_id;
can we avoid this?
Quoted 8 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +8
-0+ +struct hyper_dmabuf_req req_pending = {0 }; + +static void xen_get_domid_delayed (struct work_struct *unused); +static void xen_init_comm_env_delayed (struct work_struct *unused); + +static DECLARE_DELAYED_WORK(get_vm_id_work, xen_get_domid_delayed); +static DECLARE_DELAYED_WORK(xen_init_comm_env_work, xen_init_comm_env_delayed);
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 59
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+ +/* Creates entry in xen store that will keep details of all +* exporter rings created by this domain +*/ +static int xen_comm_setup_data_dir (void ) +{ +char buf[255 ]; + +sprintf (buf,"/local/domain/%d/data/hyper_dmabuf" , + hy_drv_priv->domid);
Here and below: please have a string constant for that
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 61
Quoted 2 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +2
-0+ +return xenbus_mkdir (XBT_NIL , buf,"" );
Please think of updating XenBus with a transaction, not XBT_NIL
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+} + +/* Removes entry from xenstore with exporter ring details. +* Other domains that has connected to any of exporter rings +* created by this domain, will be notified about removal of +* this entry and will treat that as signal to cleanup importer +* rings created for this domain +*/ +static int xen_comm_destroy_data_dir (void ) +{
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 81
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+char buf[255 ]; + +sprintf (buf,"/local/domain/%d/data/hyper_dmabuf" , + hy_drv_priv->domid); + +return xenbus_rm (XBT_NIL , buf,"" ); +} + +/* Adds xenstore entries with details of exporter ring created +* for given remote domain. It requires special daemon running
what is this special daemon?
Quoted 6 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +6
-0+* in dom0 to make sure that given remote domain will have right +* permissions to access that data. +*/ +static int xen_comm_expose_ring_details (int domid,int rdomid, +int gref,int port) +{
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 97
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+char buf[255 ]; +int ret; + +sprintf (buf,"/local/domain/%d/data/hyper_dmabuf/%d" , + domid, rdomid); + + ret =xenbus_printf (XBT_NIL , buf,"grefid" ,"%d" , gref); + +if (ret) { + dev_err(hy_drv_priv->dev,
Please do not touch global hy_drv_priv directly
Quoted 22 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +22
-0+"Failed to write xenbus entry %s: %d\n" , + buf, ret); + +return ret; + } + + ret =xenbus_printf (XBT_NIL , buf,"port" ,"%d" , port); + +if (ret) { +dev_err (hy_drv_priv->dev, +"Failed to write xenbus entry %s: %d\n" , + buf, ret); + +return ret; + } + +return 0 ; +} + +/* +* Queries details of ring exposed by remote domain. +*/
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 129
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+static int xen_comm_get_ring_details (int domid,int rdomid, +int *grefid,int *port) +{ +char buf[255 ]; +int ret; + +sprintf (buf,"/local/domain/%d/data/hyper_dmabuf/%d" , + rdomid, domid); + + ret =xenbus_scanf (XBT_NIL , buf,"grefid" ,"%d" , grefid);
You'll have a race condition here as you are not using transactions, so you might read partial data from XenBus
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 139
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+ +if (ret <=0 ) { +dev_err (hy_drv_priv->dev, +"Failed to read xenbus entry %s: %d\n" , + buf, ret); + +return ret; + } + + ret =xenbus_scanf (XBT_NIL , buf,"port" ,"%d" , port);
Ditto
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+ +if (ret <=0 ) { +dev_err (hy_drv_priv->dev, +"Failed to read xenbus entry %s: %d\n" , + buf, ret); + +return ret; + } + +return (ret <=0 ?1 :0 );
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 159
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+} + +static void xen_get_domid_delayed (struct work_struct *unused) +{ +struct xenbus_transaction xbt; +int domid, ret; + +/* scheduling another if driver is still running +* and xenstore has not been initialized +*/
Please think of using XenBus drivers for this (struct xenbus_driver) It might add some complexity in the backend (by dynamically registering/ unregistering XenBus driver), but will also let you run such code as you have here synchronously, e.g. see struct xenbus_driver.otherend_changed. This way you'll be able to implement XenBus state machine as other Xen front/back drivers do.
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 166
Quoted 7 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +7
-0+if (likely (xenstored_ready ==0 )) { +dev_dbg (hy_drv_priv->dev, +"Xenstore is not ready yet. Will retry in 500ms\n" ); +schedule_delayed_work (&get_vm_id_work,msecs_to_jiffies (500 )); + }else { +xenbus_transaction_start (&xbt); +
so, for consistency, please use transactions everywhere
Quoted 5 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +5
-0+ ret =xenbus_scanf (xbt,"domid" ,"" ,"%d" , &domid); + +if (ret <=0 ) + domid =-1 ; +
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 181
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+xenbus_transaction_end (xbt,0 ); + +/* try again since -1 is an invalid id for domain +* (but only if driver is still running) +*/ +if (unlikely (domid ==-1 )) { +dev_dbg (hy_drv_priv->dev, +"domid==-1 is invalid. Will retry it in 500ms\n" ); +schedule_delayed_work (&get_vm_id_work, +msecs_to_jiffies (500 ));
This doesn't seem to be designed right as you need to poll for values and have this worker
Quoted 32 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +32
-0+ }else { +dev_info (hy_drv_priv->dev, +"Successfully retrieved domid from Xenstore:%d\n" , + domid); + hy_drv_priv->domid = domid; + } + } +} + +int xen_be_get_domid (void ) +{ +struct xenbus_transaction xbt; +int domid; + +if (unlikely (xenstored_ready ==0 )) { +xen_get_domid_delayed (NULL ); +return -1 ; + } + +xenbus_transaction_start (&xbt); + +if (!xenbus_scanf (xbt,"domid" ,"" ,"%d" , &domid)) + domid =-1 ; + +xenbus_transaction_end (xbt,0 ); + +return domid; +} + +static int xen_comm_next_req_id (void ) +{ + export_req_id++;
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 223
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+return export_req_id; +} + +/* For now cache latast rings as global variables TODO: keep them in list*/ +static irqreturn_t front_ring_isr (int irq,void *info); +static irqreturn_t back_ring_isr (int irq,void *info); + +/* Callback function that will be called on any change of xenbus path +* being watched. Used for detecting creation/destruction of remote +* domain exporter ring.
If you implement xenbus_driver.otherend_changed and corresponding state machine this might not be needed
Quoted 93 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +93
-0+* +* When remote domain's exporter ring will be detected, importer ring +* on this domain will be created. +* +* When remote domain's exporter ring destruction will be detected it +* will celanup this domain importer ring. +* +* Destruction can be caused by unloading module by remote domain or +* it's crash/force shutdown. +*/ +static void remote_dom_exporter_watch_cb (struct xenbus_watch *watch, +const char *path,const char *token) +{ +int rdom, ret; +uint32_t grefid, port; +struct xen_comm_rx_ring_info *ring_info; + +/* Check which domain has changed its exporter rings */ + ret =sscanf (watch->node,"/local/domain/%d/" , &rdom); +if (ret <=0 ) +return ; + +/* Check if we have importer ring for given remote domain already +* created +*/ + ring_info =xen_comm_find_rx_ring (rdom); + +/* Try to query remote domain exporter ring details - if +* that will fail and we have importer ring that means remote +* domains has cleanup its exporter ring, so our importer ring +* is no longer useful. +* +* If querying details will succeed and we don't have importer ring, +* it means that remote domain has setup it for us and we should +* connect to it. +*/ + + ret =xen_comm_get_ring_details (xen_be_get_domid (), + rdom, &grefid, &port); + +if (ring_info && ret !=0 ) { +dev_info (hy_drv_priv->dev, +"Remote exporter closed, cleaninup importer\n" ); +xen_be_cleanup_rx_rbuf (rdom); + }else if (!ring_info && ret ==0 ) { +dev_info (hy_drv_priv->dev, +"Registering importer\n" ); +xen_be_init_rx_rbuf (rdom); + } +} + +/* exporter needs to generated info for page sharing */ +int xen_be_init_tx_rbuf (int domid) +{ +struct xen_comm_tx_ring_info *ring_info; +struct xen_comm_sring *sring; +struct evtchn_alloc_unbound alloc_unbound; +struct evtchn_close close; + +void *shared_ring; +int ret; + +/* check if there's any existing tx channel in the table */ + ring_info =xen_comm_find_tx_ring (domid); + +if (ring_info) { +dev_info (hy_drv_priv->dev, +"tx ring ch to domid = %d already exist\ngref = %d, port = %d\n" , + ring_info->rdomain, ring_info->gref_ring, ring_info->port); +return 0 ; + } + + ring_info =kmalloc (sizeof (*ring_info),GFP_KERNEL ); + +if (!ring_info) +return -ENOMEM ; + +/* from exporter to importer */ + shared_ring = (void *)__get_free_pages (GFP_KERNEL ,1 ); +if (shared_ring ==0 ) { +kfree (ring_info); +return -ENOMEM ; + } + + sring = (struct xen_comm_sring *) shared_ring; + +SHARED_RING_INIT (sring); + +FRONT_RING_INIT (&(ring_info->ring_front), sring,PAGE_SIZE ); + + ring_info->gref_ring =gnttab_grant_foreign_access (domid, +virt_to_mfn (shared_ring), +0 );
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 326
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+if (ring_info->gref_ring <0 ) { +/* fail to get gref */ +kfree (ring_info); +return -EFAULT ; + } + + alloc_unbound.dom =DOMID_SELF ; + alloc_unbound.remote_dom = domid; + ret =HYPERVISOR_event_channel_op (EVTCHNOP_alloc_unbound, + &alloc_unbound);
Please do not open-code: xenbus_alloc_evtchn
Quoted 7 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +7
-0+if (ret) { +dev_err (hy_drv_priv->dev, +"Cannot allocate event channel\n" ); +kfree (ring_info); +return -EIO ; + } +
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 343
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+/* setting up interrupt */ + ret =bind_evtchn_to_irqhandler (alloc_unbound.port, + front_ring_isr,0 , +NULL , (void *) ring_info); + +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"Failed to setup event channel\n" ); + close.port = alloc_unbound.port; +HYPERVISOR_event_channel_op (EVTCHNOP_close, &close);
Please do not open-code: xenbus_free_evtchn
Quoted 11 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +11
-0+gnttab_end_foreign_access (ring_info->gref_ring,0 , +virt_to_mfn (shared_ring)); +kfree (ring_info); +return -EIO ; + } + + ring_info->rdomain = domid; + ring_info->irq = ret; + ring_info->port = alloc_unbound.port; + +mutex_init (&ring_info->lock);
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 364
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+ +dev_dbg (hy_drv_priv->dev, +"%s: allocated eventchannel gref %d port: %d irq: %d\n" , + __func__, + ring_info->gref_ring, + ring_info->port, + ring_info->irq); + + ret =xen_comm_add_tx_ring (ring_info); +
And what if we fail?
Quoted 53 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +53
-0+ ret =xen_comm_expose_ring_details (xen_be_get_domid (), + domid, + ring_info->gref_ring, + ring_info->port); + +/* Register watch for remote domain exporter ring. +* When remote domain will setup its exporter ring, +* we will automatically connect our importer ring to it. +*/ + ring_info->watch.callback = remote_dom_exporter_watch_cb; + ring_info->watch.node =kmalloc (255 ,GFP_KERNEL ); + +if (!ring_info->watch.node) { +kfree (ring_info); +return -ENOMEM ; + } + +sprintf ((char *)ring_info->watch.node, +"/local/domain/%d/data/hyper_dmabuf/%d/port" , + domid,xen_be_get_domid ()); + +register_xenbus_watch (&ring_info->watch); + +return ret; +} + +/* cleans up exporter ring created for given remote domain */ +void xen_be_cleanup_tx_rbuf (int domid) +{ +struct xen_comm_tx_ring_info *ring_info; +struct xen_comm_rx_ring_info *rx_ring_info; + +/* check if we at all have exporter ring for given rdomain */ + ring_info =xen_comm_find_tx_ring (domid); + +if (!ring_info) +return ; + +xen_comm_remove_tx_ring (domid); + +unregister_xenbus_watch (&ring_info->watch); +kfree (ring_info->watch.node); + +/* No need to close communication channel, will be done by +* this function +*/ +unbind_from_irqhandler (ring_info->irq, (void *) ring_info); + +/* No need to free sring page, will be freed by this function +* when other side will end its access +*/ +gnttab_end_foreign_access (ring_info->gref_ring,0 , + (unsigned long ) ring_info->ring_front.sring);
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 427
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+ +kfree (ring_info); + + rx_ring_info =xen_comm_find_rx_ring (domid); +if (!rx_ring_info) +return ; + +BACK_RING_INIT (&(rx_ring_info->ring_back), + rx_ring_info->ring_back.sring, +PAGE_SIZE );
why init on cleanup?
Quoted 49 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +49
-0+} + +/* importer needs to know about shared page and port numbers for +* ring buffer and event channel +*/ +int xen_be_init_rx_rbuf (int domid) +{ +struct xen_comm_rx_ring_info *ring_info; +struct xen_comm_sring *sring; + +struct page *shared_ring; + +struct gnttab_map_grant_ref *map_ops; + +int ret; +int rx_gref, rx_port; + +/* check if there's existing rx ring channel */ + ring_info =xen_comm_find_rx_ring (domid); + +if (ring_info) { +dev_info (hy_drv_priv->dev, +"rx ring ch from domid = %d already exist\n" , + ring_info->sdomain); + +return 0 ; + } + + ret =xen_comm_get_ring_details (xen_be_get_domid (), domid, + &rx_gref, &rx_port); + +if (ret) { +dev_err (hy_drv_priv->dev, +"Domain %d has not created exporter ring for current domain\n" , + domid); + +return ret; + } + + ring_info =kmalloc (sizeof (*ring_info),GFP_KERNEL ); + +if (!ring_info) +return -ENOMEM ; + + ring_info->sdomain = domid; + ring_info->evtchn = rx_port; + + map_ops =kmalloc (sizeof (*map_ops),GFP_KERNEL ); +
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 486
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+if (!map_ops) { + ret = -ENOMEM ; + goto fail_no_map_ops; + } + +if (gnttab_alloc_pages (1 , &shared_ring)) { + ret = -ENOMEM ; + goto fail_others; + } +
Please see xenbus_grant_ring
Quoted 108 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +108
-0+gnttab_set_map_op (&map_ops[0 ], + (unsigned long )pfn_to_kaddr ( +page_to_pfn (shared_ring)), + GNTMAP_host_map, rx_gref, domid); + +gnttab_set_unmap_op (&ring_info->unmap_op, + (unsigned long )pfn_to_kaddr ( +page_to_pfn (shared_ring)), + GNTMAP_host_map,-1 ); + + ret =gnttab_map_refs (map_ops,NULL , &shared_ring,1 ); +if (ret <0 ) { +dev_err (hy_drv_priv->dev,"Cannot map ring\n" ); + ret = -EFAULT ; + goto fail_others; + } + +if (map_ops[0 ].status) { +dev_err (hy_drv_priv->dev,"Ring mapping failed\n" ); + ret = -EFAULT ; + goto fail_others; + }else { + ring_info->unmap_op.handle = map_ops[0 ].handle; + } + +kfree (map_ops); + + sring = (struct xen_comm_sring *)pfn_to_kaddr (page_to_pfn (shared_ring)); + +BACK_RING_INIT (&ring_info->ring_back, sring,PAGE_SIZE ); + + ret =bind_interdomain_evtchn_to_irq (domid, rx_port); + +if (ret <0 ) { + ret = -EIO ; + goto fail_others; + } + + ring_info->irq = ret; + +dev_dbg (hy_drv_priv->dev, +"%s: bound to eventchannel port: %d irq: %d\n" , __func__, + rx_port, + ring_info->irq); + + ret =xen_comm_add_rx_ring (ring_info); + +/* Setup communcation channel in opposite direction */ +if (!xen_comm_find_tx_ring (domid)) + ret =xen_be_init_tx_rbuf (domid); + + ret =request_irq (ring_info->irq, + back_ring_isr,0 , +NULL , (void *)ring_info); + +return ret; + +fail_others: +kfree (map_ops); + +fail_no_map_ops: +kfree (ring_info); + +return ret; +} + +/* clenas up importer ring create for given source domain */ +void xen_be_cleanup_rx_rbuf (int domid) +{ +struct xen_comm_rx_ring_info *ring_info; +struct xen_comm_tx_ring_info *tx_ring_info; +struct page *shared_ring; + +/* check if we have importer ring created for given sdomain */ + ring_info =xen_comm_find_rx_ring (domid); + +if (!ring_info) +return ; + +xen_comm_remove_rx_ring (domid); + +/* no need to close event channel, will be done by that function */ +unbind_from_irqhandler (ring_info->irq, (void *)ring_info); + +/* unmapping shared ring page */ + shared_ring =virt_to_page (ring_info->ring_back.sring); +gnttab_unmap_refs (&ring_info->unmap_op,NULL , &shared_ring,1 ); +gnttab_free_pages (1 , &shared_ring); + +kfree (ring_info); + + tx_ring_info =xen_comm_find_tx_ring (domid); +if (!tx_ring_info) +return ; + +SHARED_RING_INIT (tx_ring_info->ring_front.sring); +FRONT_RING_INIT (&(tx_ring_info->ring_front), + tx_ring_info->ring_front.sring, +PAGE_SIZE ); +} + +#ifdef CONFIG_HYPER_DMABUF_XEN_AUTO_RX_CH_ADD + +static void xen_rx_ch_add_delayed (struct work_struct *unused); + +static DECLARE_DELAYED_WORK(xen_rx_ch_auto_add_work, xen_rx_ch_add_delayed); + +#define DOMID_SCAN_START 1/* domid = 1 */
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 604
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+#define DOMID_SCAN_END 10/* domid = 10 */ + +static void xen_rx_ch_add_delayed (struct work_struct *unused) +{ +int ret; +char buf[128 ]; +int i, dummy; + +dev_dbg (hy_drv_priv->dev, +"Scanning new tx channel comming from another domain\n" );
This should be synchronous IMO, no scanners
Quoted 133 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +133
-0+ +/* check other domains and schedule another work if driver +* is still running and backend is valid +*/ +if (hy_drv_priv && + hy_drv_priv->initialized) { +for (i =DOMID_SCAN_START ; i <DOMID_SCAN_END +1 ; i++) { +if (i == hy_drv_priv->domid) +continue ; + +sprintf (buf,"/local/domain/%d/data/hyper_dmabuf/%d" , + i, hy_drv_priv->domid); + + ret =xenbus_scanf (XBT_NIL , buf,"port" ,"%d" , &dummy); + +if (ret >0 ) { +if (xen_comm_find_rx_ring (i) !=NULL ) +continue ; + + ret =xen_be_init_rx_rbuf (i); + +if (!ret) +dev_info (hy_drv_priv->dev, +"Done rx ch init for VM %d\n" , + i); + } + } + +/* check every 10 seconds */ +schedule_delayed_work (&xen_rx_ch_auto_add_work, +msecs_to_jiffies (10000 )); + } +} + +#endif /* CONFIG_HYPER_DMABUF_XEN_AUTO_RX_CH_ADD */ + +void xen_init_comm_env_delayed (struct work_struct *unused) +{ +int ret; + +/* scheduling another work if driver is still running +* and xenstore hasn't been initialized or dom_id hasn't +* been correctly retrieved. +*/ +if (likely (xenstored_ready ==0 || + hy_drv_priv->domid ==-1 )) { +dev_dbg (hy_drv_priv->dev, +"Xenstore not ready Will re-try in 500ms\n" ); +schedule_delayed_work (&xen_init_comm_env_work, +msecs_to_jiffies (500 )); + }else { + ret =xen_comm_setup_data_dir (); +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"Failed to create data dir in Xenstore\n" ); + }else { +dev_info (hy_drv_priv->dev, +"Successfully finished comm env init\n" ); + hy_drv_priv->initialized = true; + +#ifdef CONFIG_HYPER_DMABUF_XEN_AUTO_RX_CH_ADD +xen_rx_ch_add_delayed (NULL ); +#endif /* CONFIG_HYPER_DMABUF_XEN_AUTO_RX_CH_ADD */ + } + } +} + +int xen_be_init_comm_env (void ) +{ +int ret; + +xen_comm_ring_table_init (); + +if (unlikely (xenstored_ready ==0 || + hy_drv_priv->domid ==-1 )) { +xen_init_comm_env_delayed (NULL ); +return -1 ; + } + + ret =xen_comm_setup_data_dir (); +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"Failed to create data dir in Xenstore\n" ); + }else { +dev_info (hy_drv_priv->dev, +"Successfully finished comm env initialization\n" ); + + hy_drv_priv->initialized = true; + } + +return ret; +} + +/* cleans up all tx/rx rings */ +static void xen_be_cleanup_all_rbufs (void ) +{ +xen_comm_foreach_tx_ring (xen_be_cleanup_tx_rbuf); +xen_comm_foreach_rx_ring (xen_be_cleanup_rx_rbuf); +} + +void xen_be_destroy_comm (void ) +{ +xen_be_cleanup_all_rbufs (); +xen_comm_destroy_data_dir (); +} + +int xen_be_send_req (int domid,struct hyper_dmabuf_req *req, +int wait) +{ +struct xen_comm_front_ring *ring; +struct hyper_dmabuf_req *new_req; +struct xen_comm_tx_ring_info *ring_info; +int notify; + +struct timeval tv_start, tv_end; +struct timeval tv_diff; + +int timeout =1000 ; + +/* find a ring info for the channel */ + ring_info =xen_comm_find_tx_ring (domid); +if (!ring_info) { +dev_err (hy_drv_priv->dev, +"Can't find ring info for the channel\n" ); +return -ENOENT ; + } + + + ring = &ring_info->ring_front; + +do_gettimeofday (&tv_start); + +while (RING_FULL (ring)) {
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 747
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+dev_dbg (hy_drv_priv->dev,"RING_FULL\n" ); + +if (timeout ==0 ) { +dev_err (hy_drv_priv->dev, +"Timeout while waiting for an entry in the ring\n" ); +return -EIO ; + } +usleep_range (100 ,120 ); + timeout--; + }
Heh
Quoted 38 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +38
-0+ + timeout =1000 ; + +mutex_lock (&ring_info->lock); + + new_req =RING_GET_REQUEST (ring, ring->req_prod_pvt); +if (!new_req) { +mutex_unlock (&ring_info->lock); +dev_err (hy_drv_priv->dev, +"NULL REQUEST\n" ); +return -EIO ; + } + + req->req_id =xen_comm_next_req_id (); + +/* update req_pending with current request */ +memcpy (&req_pending, req,sizeof (req_pending)); + +/* pass current request to the ring */ +memcpy (new_req, req,sizeof (*new_req)); + + ring->req_prod_pvt++; + +RING_PUSH_REQUESTS_AND_CHECK_NOTIFY (ring, notify); +if (notify) +notify_remote_via_irq (ring_info->irq); + +if (wait) { +while (timeout--) { +if (req_pending.stat != +HYPER_DMABUF_REQ_NOT_RESPONDED ) +break ; +usleep_range (100 ,120 ); + } + +if (timeout <0 ) { +mutex_unlock (&ring_info->lock); + dev_err(hy_drv_priv->dev,
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c line 795
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +10
-0+ "request timed-out\n"); +return -EBUSY ; + } + +mutex_unlock (&ring_info->lock); +do_gettimeofday (&tv_end); + +/* checking time duration for round-trip of a request +* for debugging +*/
put it under debug #ifdef then?
Quoted 649 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.c new file +146
-0+if (tv_end.tv_usec >= tv_start.tv_usec) { + tv_diff.tv_sec = tv_end.tv_sec-tv_start.tv_sec; + tv_diff.tv_usec = tv_end.tv_usec-tv_start.tv_usec; + }else { + tv_diff.tv_sec = tv_end.tv_sec-tv_start.tv_sec-1 ; + tv_diff.tv_usec = tv_end.tv_usec+1000000 - + tv_start.tv_usec; + } + +if (tv_diff.tv_sec !=0 && tv_diff.tv_usec >16000 ) +dev_dbg (hy_drv_priv->dev, +"send_req:time diff: %ld sec, %ld usec\n" , + tv_diff.tv_sec, tv_diff.tv_usec); + } + +mutex_unlock (&ring_info->lock); + +return 0 ; +} + +/* ISR for handling request */ +static irqreturn_t back_ring_isr (int irq,void *info) +{ +RING_IDX rc, rp; +struct hyper_dmabuf_req req; +struct hyper_dmabuf_resp resp; + +int notify, more_to_do; +int ret; + +struct xen_comm_rx_ring_info *ring_info; +struct xen_comm_back_ring *ring; + + ring_info = (struct xen_comm_rx_ring_info *)info; + ring = &ring_info->ring_back; + +dev_dbg (hy_drv_priv->dev,"%s\n" , __func__); + +do { + rc = ring->req_cons; + rp = ring->sring->req_prod; + more_to_do =0 ; +while (rc != rp) { +if (RING_REQUEST_CONS_OVERFLOW (ring, rc)) +break ; + +memcpy (&req,RING_GET_REQUEST (ring, rc),sizeof (req)); + ring->req_cons = ++rc; + + ret =hyper_dmabuf_msg_parse (ring_info->sdomain, &req); + +if (ret >0 ) { +/* preparing a response for the request and +* send it to the requester +*/ +memcpy (&resp, &req,sizeof (resp)); +memcpy (RING_GET_RESPONSE (ring, + ring->rsp_prod_pvt), + &resp,sizeof (resp)); + ring->rsp_prod_pvt++; + +dev_dbg (hy_drv_priv->dev, +"responding to exporter for req:%d\n" , + resp.resp_id); + +RING_PUSH_RESPONSES_AND_CHECK_NOTIFY (ring, + notify); + +if (notify) +notify_remote_via_irq (ring_info->irq); + } + +RING_FINAL_CHECK_FOR_REQUESTS (ring, more_to_do); + } + }while (more_to_do); + +return IRQ_HANDLED ; +} + +/* ISR for handling responses */ +static irqreturn_t front_ring_isr (int irq,void *info) +{ +/* front ring only care about response from back */ +struct hyper_dmabuf_resp *resp; +RING_IDX i, rp; +int more_to_do, ret; + +struct xen_comm_tx_ring_info *ring_info; +struct xen_comm_front_ring *ring; + + ring_info = (struct xen_comm_tx_ring_info *)info; + ring = &ring_info->ring_front; + +dev_dbg (hy_drv_priv->dev,"%s\n" , __func__); + +do { + more_to_do =0 ; + rp = ring->sring->rsp_prod; +for (i = ring->rsp_cons; i != rp; i++) { + resp =RING_GET_RESPONSE (ring, i); + +/* update pending request's status with what is +* in the response +*/ + +dev_dbg (hy_drv_priv->dev, +"getting response from importer\n" ); + +if (req_pending.req_id == resp->resp_id) + req_pending.stat = resp->stat; + +if (resp->stat ==HYPER_DMABUF_REQ_NEEDS_FOLLOW_UP ) { +/* parsing response */ + ret =hyper_dmabuf_msg_parse (ring_info->rdomain, + (struct hyper_dmabuf_req *)resp); + +if (ret <0 ) { +dev_err (hy_drv_priv->dev, +"err while parsing resp\n" ); + } + }else if (resp->stat ==HYPER_DMABUF_REQ_PROCESSED ) { +/* for debugging dma_buf remote synch */ +dev_dbg (hy_drv_priv->dev, +"original request = 0x%x\n" , resp->cmd); +dev_dbg (hy_drv_priv->dev, +"got HYPER_DMABUF_REQ_PROCESSED\n" ); + }else if (resp->stat ==HYPER_DMABUF_REQ_ERROR ) { +/* for debugging dma_buf remote synch */ +dev_dbg (hy_drv_priv->dev, +"original request = 0x%x\n" , resp->cmd); +dev_dbg (hy_drv_priv->dev, +"got HYPER_DMABUF_REQ_ERROR\n" ); + } + } + + ring->rsp_cons = i; + +if (i != ring->req_prod_pvt) +RING_FINAL_CHECK_FOR_RESPONSES (ring, more_to_do); +else + ring->sring->rsp_event = i+1 ; + + }while (more_to_do); + +return IRQ_HANDLED ; +}drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm.h new file +78
-0@@ -0,0 +1,78 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#ifndef __HYPER_DMABUF_XEN_COMM_H__ +#define __HYPER_DMABUF_XEN_COMM_H__ + +#include "xen/interface/io/ring.h" +#include "xen/xenbus.h" +#include "../../hyper_dmabuf_msg.h" + +extern int xenstored_ready; + +DEFINE_RING_TYPES(xen_comm, struct hyper_dmabuf_req, struct hyper_dmabuf_resp); + +struct xen_comm_tx_ring_info { +struct xen_comm_front_ring ring_front; +int rdomain; +int gref_ring; +int irq; +int port; +struct mutex lock; +struct xenbus_watch watch; +}; + +struct xen_comm_rx_ring_info { +int sdomain; +int irq; +int evtchn; +struct xen_comm_back_ring ring_back; +struct gnttab_unmap_grant_ref unmap_op; +}; + +int xen_be_get_domid (void ); + +int xen_be_init_comm_env (void ); + +/* exporter needs to generated info for page sharing */ +int xen_be_init_tx_rbuf (int domid); + +/* importer needs to know about shared page and port numbers +* for ring buffer and event channel +*/ +int xen_be_init_rx_rbuf (int domid); + +/* cleans up exporter ring created for given domain */ +void xen_be_cleanup_tx_rbuf (int domid); + +/* cleans up importer ring created for given domain */ +void xen_be_cleanup_rx_rbuf (int domid); + +void xen_be_destroy_comm (void ); + +/* send request to the remote domain */ +int xen_be_send_req (int domid,struct hyper_dmabuf_req *req, +int wait); + +#endif /* __HYPER_DMABUF_XEN_COMM_H__ */ drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm_list.c new file +158
-0@@ -0,0 +1,158 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/kernel.h> +#include <linux/errno.h> +#include <linux/slab.h> +#include <linux/cdev.h> +#include <linux/hashtable.h> +#include <xen/grant_table.h> +#include "../../hyper_dmabuf_drv.h" +#include "hyper_dmabuf_xen_comm.h" +#include "hyper_dmabuf_xen_comm_list.h" + +DECLARE_HASHTABLE (xen_comm_tx_ring_hash,MAX_ENTRY_TX_RING ); +DECLARE_HASHTABLE (xen_comm_rx_ring_hash,MAX_ENTRY_RX_RING ); + +void xen_comm_ring_table_init (void ) +{ +hash_init (xen_comm_rx_ring_hash); +hash_init (xen_comm_tx_ring_hash); +} + +int xen_comm_add_tx_ring (struct xen_comm_tx_ring_info *ring_info) +{ +struct xen_comm_tx_ring_info_entry *info_entry; + + info_entry =kmalloc (sizeof (*info_entry),GFP_KERNEL ); + +if (!info_entry) +return -ENOMEM ; + + info_entry->info = ring_info; + +hash_add (xen_comm_tx_ring_hash, &info_entry->node, + info_entry->info->rdomain); + +return 0 ; +} + +int xen_comm_add_rx_ring (struct xen_comm_rx_ring_info *ring_info) +{ +struct xen_comm_rx_ring_info_entry *info_entry; + + info_entry =kmalloc (sizeof (*info_entry),GFP_KERNEL ); + +if (!info_entry) +return -ENOMEM ; + + info_entry->info = ring_info; + +hash_add (xen_comm_rx_ring_hash, &info_entry->node, + info_entry->info->sdomain); + +return 0 ; +} + +struct xen_comm_tx_ring_info *xen_comm_find_tx_ring (int domid) +{ +struct xen_comm_tx_ring_info_entry *info_entry; +int bkt; + +hash_for_each (xen_comm_tx_ring_hash, bkt, info_entry, node) +if (info_entry->info->rdomain == domid) +return info_entry->info; + +return NULL ; +} + +struct xen_comm_rx_ring_info *xen_comm_find_rx_ring (int domid) +{ +struct xen_comm_rx_ring_info_entry *info_entry; +int bkt; + +hash_for_each (xen_comm_rx_ring_hash, bkt, info_entry, node) +if (info_entry->info->sdomain == domid) +return info_entry->info; + +return NULL ; +} + +int xen_comm_remove_tx_ring (int domid) +{ +struct xen_comm_tx_ring_info_entry *info_entry; +int bkt; + +hash_for_each (xen_comm_tx_ring_hash, bkt, info_entry, node) +if (info_entry->info->rdomain == domid) { +hash_del (&info_entry->node); +kfree (info_entry); +return 0 ; + } + +return -ENOENT ; +} + +int xen_comm_remove_rx_ring (int domid) +{ +struct xen_comm_rx_ring_info_entry *info_entry; +int bkt; + +hash_for_each (xen_comm_rx_ring_hash, bkt, info_entry, node) +if (info_entry->info->sdomain == domid) { +hash_del (&info_entry->node); +kfree (info_entry); +return 0 ; + } + +return -ENOENT ; +} + +void xen_comm_foreach_tx_ring (void (*func)(int domid)) +{ +struct xen_comm_tx_ring_info_entry *info_entry; +struct hlist_node *tmp; +int bkt; + +hash_for_each_safe (xen_comm_tx_ring_hash, bkt, tmp, + info_entry, node) { +func (info_entry->info->rdomain); + } +} + +void xen_comm_foreach_rx_ring (void (*func)(int domid)) +{ +struct xen_comm_rx_ring_info_entry *info_entry; +struct hlist_node *tmp; +int bkt; + +hash_for_each_safe (xen_comm_rx_ring_hash, bkt, tmp, + info_entry, node) { +func (info_entry->info->sdomain); + } +}drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_comm_list.h new file +67
-0@@ -0,0 +1,67 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#ifndef __HYPER_DMABUF_XEN_COMM_LIST_H__ +#define __HYPER_DMABUF_XEN_COMM_LIST_H__ + +/* number of bits to be used for exported dmabufs hash table */ +#define MAX_ENTRY_TX_RING 7 +/* number of bits to be used for imported dmabufs hash table */ +#define MAX_ENTRY_RX_RING 7 + +struct xen_comm_tx_ring_info_entry { +struct xen_comm_tx_ring_info *info; +struct hlist_node node; +}; + +struct xen_comm_rx_ring_info_entry { +struct xen_comm_rx_ring_info *info; +struct hlist_node node; +}; + +void xen_comm_ring_table_init (void ); + +int xen_comm_add_tx_ring (struct xen_comm_tx_ring_info *ring_info); + +int xen_comm_add_rx_ring (struct xen_comm_rx_ring_info *ring_info); + +int xen_comm_remove_tx_ring (int domid); + +int xen_comm_remove_rx_ring (int domid); + +struct xen_comm_tx_ring_info *xen_comm_find_tx_ring (int domid); + +struct xen_comm_rx_ring_info *xen_comm_find_rx_ring (int domid); + +/* iterates over all exporter rings and calls provided +* function for each of them +*/ +void xen_comm_foreach_tx_ring (void (*func)(int domid)); + +/* iterates over all importer rings and calls provided +* function for each of them +*/ +void xen_comm_foreach_rx_ring (void (*func)(int domid)); + +#endif // __HYPER_DMABUF_XEN_COMM_LIST_H__ drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_drv.c new file +46
-0@@ -0,0 +1,46 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include "../../hyper_dmabuf_drv.h" +#include "hyper_dmabuf_xen_comm.h" +#include "hyper_dmabuf_xen_shm.h" + +struct hyper_dmabuf_bknd_ops xen_bknd_ops = { + .init =NULL ,/* not needed for xen */ + .cleanup =NULL ,/* not needed for xen */ + .get_vm_id = xen_be_get_domid, + .share_pages = xen_be_share_pages, + .unshare_pages = xen_be_unshare_pages, + .map_shared_pages = (void *)xen_be_map_shared_pages, + .unmap_shared_pages = xen_be_unmap_shared_pages, + .init_comm_env = xen_be_init_comm_env, + .destroy_comm = xen_be_destroy_comm, + .init_rx_ch = xen_be_init_rx_rbuf, + .init_tx_ch = xen_be_init_tx_rbuf, + .send_req = xen_be_send_req, +};drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_drv.h new file +53
-0@@ -0,0 +1,53 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#ifndef __HYPER_DMABUF_XEN_DRV_H__ +#define __HYPER_DMABUF_XEN_DRV_H__ +#include <xen/interface/grant_table.h> + +extern struct hyper_dmabuf_bknd_ops xen_bknd_ops; + +/* Main purpose of this structure is to keep +* all references created or acquired for sharing +* pages with another domain for freeing those later +* when unsharing. +*/ +struct xen_shared_pages_info { +/* top level refid */ +grant_ref_t lvl3_gref; + +/* page of top level addressing, it contains refids of 2nd lvl pages */ +grant_ref_t *lvl3_table; + +/* table of 2nd level pages, that contains refids to data pages */ +grant_ref_t *lvl2_table; + +/* unmap ops for mapped pages */ +struct gnttab_unmap_grant_ref *unmap_ops; + +/* data pages to be unmapped */ +struct page **data_pages; +}; + +#endif // __HYPER_DMABUF_XEN_COMM_H__ drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.c new file +65
-0@@ -0,0 +1,525 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +* Authors: +* Dongwon Kim <[email protected]> +* Mateusz Polrola <[email protected]> +* +*/ + +#include <linux/slab.h> +#include <xen/grant_table.h> +#include <asm/xen/page.h> +#include "hyper_dmabuf_xen_drv.h" +#include "../../hyper_dmabuf_drv.h" + +#define REFS_PER_PAGE (PAGE_SIZE/sizeof(grant_ref_t)) + +/* +* Creates 2 level page directory structure for referencing shared pages. +* Top level page is a single page that contains up to 1024 refids that +* point to 2nd level pages. +* +* Each 2nd level page contains up to 1024 refids that point to shared +* data pages. +* +* There will always be one top level page and number of 2nd level pages +* depends on number of shared data pages. +* +* 3rd level page 2nd level pages Data pages +* +-------------------------+ ┌>+--------------------+ ┌>+------------+ +* |2nd level page 0 refid |---┘ |Data page 0 refid |-┘ |Data page 0 | +* |2nd level page 1 refid |---┐ |Data page 1 refid |-┐ +------------+ +* | ... | | | .... | | +* |2nd level page 1023 refid|-┐ | |Data page 1023 refid| └>+------------+ +* +-------------------------+ | | +--------------------+ |Data page 1 | +* | | +------------+ +* | └>+--------------------+ +* | |Data page 1024 refid| +* | |Data page 1025 refid| +* | | ... | +* | |Data page 2047 refid| +* | +--------------------+ +* | +* | ..... +* └-->+-----------------------+ +* |Data page 1047552 refid|
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.c line 75
Quoted 10 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.c new file +10
-0+* |Data page 1047553 refid| +* | ... | +* |Data page 1048575 refid| +* +-----------------------+ +* +* Using such 2 level structure it is possible to reference up to 4GB of +* shared data using single refid pointing to top level page. +* +* Returns refid of top level page. +*/
This seems to be over-engineered, IMO
Quoted 532 lines
drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.c new file +450
-0+int xen_be_share_pages (struct page **pages,int domid,int nents, +void **refs_info) +{ +grant_ref_t lvl3_gref; +grant_ref_t *lvl2_table; +grant_ref_t *lvl3_table; + +/* +* Calculate number of pages needed for 2nd level addresing: +*/ +int n_lvl2_grefs = (nents/REFS_PER_PAGE + + ((nents %REFS_PER_PAGE ) ?1 :0 )); + +struct xen_shared_pages_info *sh_pages_info; +int i; + + lvl3_table = (grant_ref_t *)__get_free_pages (GFP_KERNEL ,1 ); + lvl2_table = (grant_ref_t *)__get_free_pages (GFP_KERNEL , n_lvl2_grefs); + + sh_pages_info =kmalloc (sizeof (*sh_pages_info),GFP_KERNEL ); + +if (!sh_pages_info) +return -ENOMEM ; + + *refs_info = (void *)sh_pages_info; + +/* share data pages in readonly mode for security */ +for (i =0 ; i < nents; i++) { + lvl2_table[i] =gnttab_grant_foreign_access (domid, +pfn_to_mfn (page_to_pfn (pages[i])), + true/* read only */ ); +if (lvl2_table[i] == -ENOSPC ) { +dev_err (hy_drv_priv->dev, +"No more space left in grant table\n" ); + +/* Unshare all already shared pages for lvl2 */ +while (i--) { +gnttab_end_foreign_access_ref (lvl2_table[i],0 ); +gnttab_free_grant_reference (lvl2_table[i]); + } + goto err_cleanup; + } + } + +/* Share 2nd level addressing pages in readonly mode*/ +for (i =0 ; i < n_lvl2_grefs; i++) { + lvl3_table[i] =gnttab_grant_foreign_access (domid, +virt_to_mfn ( + (unsigned long )lvl2_table+i*PAGE_SIZE ), + true); + +if (lvl3_table[i] == -ENOSPC ) { +dev_err (hy_drv_priv->dev, +"No more space left in grant table\n" ); + +/* Unshare all already shared pages for lvl3 */ +while (i--) { +gnttab_end_foreign_access_ref (lvl3_table[i],1 ); +gnttab_free_grant_reference (lvl3_table[i]); + } + +/* Unshare all pages for lvl2 */ +while (nents--) { +gnttab_end_foreign_access_ref ( + lvl2_table[nents],0 ); +gnttab_free_grant_reference (lvl2_table[nents]); + } + + goto err_cleanup; + } + } + +/* Share lvl3_table in readonly mode*/ + lvl3_gref =gnttab_grant_foreign_access (domid, +virt_to_mfn ((unsigned long )lvl3_table), + true); + +if (lvl3_gref == -ENOSPC ) { +dev_err (hy_drv_priv->dev, +"No more space left in grant table\n" ); + +/* Unshare all pages for lvl3 */ +while (i--) { +gnttab_end_foreign_access_ref (lvl3_table[i],1 ); +gnttab_free_grant_reference (lvl3_table[i]); + } + +/* Unshare all pages for lvl2 */ +while (nents--) { +gnttab_end_foreign_access_ref (lvl2_table[nents],0 ); +gnttab_free_grant_reference (lvl2_table[nents]); + } + + goto err_cleanup; + } + +/* Store lvl3_table page to be freed later */ + sh_pages_info->lvl3_table = lvl3_table; + +/* Store lvl2_table pages to be freed later */ + sh_pages_info->lvl2_table = lvl2_table; + + +/* Store exported pages refid to be unshared later */ + sh_pages_info->lvl3_gref = lvl3_gref; + +dev_dbg (hy_drv_priv->dev,"%s exit\n" , __func__); +return lvl3_gref; + +err_cleanup: +free_pages ((unsigned long )lvl2_table, n_lvl2_grefs); +free_pages ((unsigned long )lvl3_table,1 ); + +return -ENOSPC ; +} + +int xen_be_unshare_pages (void **refs_info,int nents) +{ +struct xen_shared_pages_info *sh_pages_info; +int n_lvl2_grefs = (nents/REFS_PER_PAGE + + ((nents %REFS_PER_PAGE ) ?1 :0 )); +int i; + +dev_dbg (hy_drv_priv->dev,"%s entry\n" , __func__); + sh_pages_info = (struct xen_shared_pages_info *)(*refs_info); + +if (sh_pages_info->lvl3_table ==NULL || + sh_pages_info->lvl2_table ==NULL || + sh_pages_info->lvl3_gref ==-1 ) { +dev_warn (hy_drv_priv->dev, +"gref table for hyper_dmabuf already cleaned up\n" ); +return 0 ; + } + +/* End foreign access for data pages, but do not free them */ +for (i =0 ; i < nents; i++) { +if (gnttab_query_foreign_access (sh_pages_info->lvl2_table[i])) +dev_warn (hy_drv_priv->dev,"refid not shared !!\n" ); + +gnttab_end_foreign_access_ref (sh_pages_info->lvl2_table[i],0 ); +gnttab_free_grant_reference (sh_pages_info->lvl2_table[i]); + } + +/* End foreign access for 2nd level addressing pages */ +for (i =0 ; i < n_lvl2_grefs; i++) { +if (gnttab_query_foreign_access (sh_pages_info->lvl3_table[i])) +dev_warn (hy_drv_priv->dev,"refid not shared !!\n" ); + +if (!gnttab_end_foreign_access_ref ( + sh_pages_info->lvl3_table[i],1 )) +dev_warn (hy_drv_priv->dev,"refid still in use!!!\n" ); + +gnttab_free_grant_reference (sh_pages_info->lvl3_table[i]); + } + +/* End foreign access for top level addressing page */ +if (gnttab_query_foreign_access (sh_pages_info->lvl3_gref)) +dev_warn (hy_drv_priv->dev,"gref not shared !!\n" ); + +gnttab_end_foreign_access_ref (sh_pages_info->lvl3_gref,1 ); +gnttab_free_grant_reference (sh_pages_info->lvl3_gref); + +/* freeing all pages used for 2 level addressing */ +free_pages ((unsigned long )sh_pages_info->lvl2_table, n_lvl2_grefs); +free_pages ((unsigned long )sh_pages_info->lvl3_table,1 ); + + sh_pages_info->lvl3_gref =-1 ; + sh_pages_info->lvl2_table =NULL ; + sh_pages_info->lvl3_table =NULL ; +kfree (sh_pages_info); + sh_pages_info =NULL ; + +dev_dbg (hy_drv_priv->dev,"%s exit\n" , __func__); +return 0 ; +} + +/* Maps provided top level ref id and then return array of pages +* containing data refs. +*/ +struct page **xen_be_map_shared_pages (unsigned long lvl3_gref,int domid, +int nents,void **refs_info) +{ +struct page *lvl3_table_page; +struct page **lvl2_table_pages; +struct page **data_pages; +struct xen_shared_pages_info *sh_pages_info; + +grant_ref_t *lvl3_table; +grant_ref_t *lvl2_table; + +struct gnttab_map_grant_ref lvl3_map_ops; +struct gnttab_unmap_grant_ref lvl3_unmap_ops; + +struct gnttab_map_grant_ref *lvl2_map_ops; +struct gnttab_unmap_grant_ref *lvl2_unmap_ops; + +struct gnttab_map_grant_ref *data_map_ops; +struct gnttab_unmap_grant_ref *data_unmap_ops; + +/* # of grefs in the last page of lvl2 table */ +int nents_last = (nents -1 ) %REFS_PER_PAGE +1 ; +int n_lvl2_grefs = (nents /REFS_PER_PAGE ) + + ((nents_last >0 ) ?1 :0 ) - + (nents_last ==REFS_PER_PAGE ); +int i, j, k; + +dev_dbg (hy_drv_priv->dev,"%s entry\n" , __func__); + + sh_pages_info =kmalloc (sizeof (*sh_pages_info),GFP_KERNEL ); + *refs_info = (void *) sh_pages_info; + + lvl2_table_pages =kcalloc (n_lvl2_grefs,sizeof (struct page *), +GFP_KERNEL ); + + data_pages =kcalloc (nents,sizeof (struct page *),GFP_KERNEL ); + + lvl2_map_ops =kcalloc (n_lvl2_grefs,sizeof (*lvl2_map_ops), +GFP_KERNEL ); + + lvl2_unmap_ops =kcalloc (n_lvl2_grefs,sizeof (*lvl2_unmap_ops), +GFP_KERNEL ); + + data_map_ops =kcalloc (nents,sizeof (*data_map_ops),GFP_KERNEL ); + data_unmap_ops =kcalloc (nents,sizeof (*data_unmap_ops),GFP_KERNEL ); + +/* Map top level addressing page */ +if (gnttab_alloc_pages (1 , &lvl3_table_page)) { +dev_err (hy_drv_priv->dev,"Cannot allocate pages\n" ); +return NULL ; + } + + lvl3_table = (grant_ref_t *)pfn_to_kaddr (page_to_pfn (lvl3_table_page)); + +gnttab_set_map_op (&lvl3_map_ops, (unsigned long )lvl3_table, + GNTMAP_host_map | GNTMAP_readonly, + (grant_ref_t )lvl3_gref, domid); + +gnttab_set_unmap_op (&lvl3_unmap_ops, (unsigned long )lvl3_table, + GNTMAP_host_map | GNTMAP_readonly,-1 ); + +if (gnttab_map_refs (&lvl3_map_ops,NULL , &lvl3_table_page,1 )) { +dev_err (hy_drv_priv->dev, +"HYPERVISOR map grant ref failed" ); +return NULL ; + } + +if (lvl3_map_ops.status) { +dev_err (hy_drv_priv->dev, +"HYPERVISOR map grant ref failed status = %d" , + lvl3_map_ops.status); + + goto error_cleanup_lvl3; + }else { + lvl3_unmap_ops.handle = lvl3_map_ops.handle; + } + +/* Map all second level pages */ +if (gnttab_alloc_pages (n_lvl2_grefs, lvl2_table_pages)) { +dev_err (hy_drv_priv->dev,"Cannot allocate pages\n" ); + goto error_cleanup_lvl3; + } + +for (i =0 ; i < n_lvl2_grefs; i++) { + lvl2_table = (grant_ref_t *)pfn_to_kaddr ( +page_to_pfn (lvl2_table_pages[i])); +gnttab_set_map_op (&lvl2_map_ops[i], + (unsigned long )lvl2_table, GNTMAP_host_map | + GNTMAP_readonly, + lvl3_table[i], domid); +gnttab_set_unmap_op (&lvl2_unmap_ops[i], + (unsigned long )lvl2_table, GNTMAP_host_map | + GNTMAP_readonly,-1 ); + } + +/* Unmap top level page, as it won't be needed any longer */ +if (gnttab_unmap_refs (&lvl3_unmap_ops,NULL , + &lvl3_table_page,1 )) { +dev_err (hy_drv_priv->dev, +"xen: cannot unmap top level page\n" ); +return NULL ; + } + +/* Mark that page was unmapped */ + lvl3_unmap_ops.handle =-1 ; + +if (gnttab_map_refs (lvl2_map_ops,NULL , + lvl2_table_pages, n_lvl2_grefs)) { +dev_err (hy_drv_priv->dev, +"HYPERVISOR map grant ref failed" ); +return NULL ; + } + +/* Checks if pages were mapped correctly */ +for (i =0 ; i < n_lvl2_grefs; i++) { +if (lvl2_map_ops[i].status) { +dev_err (hy_drv_priv->dev, +"HYPERVISOR map grant ref failed status = %d" , + lvl2_map_ops[i].status); + goto error_cleanup_lvl2; + }else { + lvl2_unmap_ops[i].handle = lvl2_map_ops[i].handle; + } + } + +if (gnttab_alloc_pages (nents, data_pages)) { +dev_err (hy_drv_priv->dev, +"Cannot allocate pages\n" ); + goto error_cleanup_lvl2; + } + + k =0 ; + +for (i =0 ; i < n_lvl2_grefs -1 ; i++) { + lvl2_table =pfn_to_kaddr (page_to_pfn (lvl2_table_pages[i])); +for (j =0 ; j <REFS_PER_PAGE ; j++) { +gnttab_set_map_op (&data_map_ops[k], + (unsigned long )pfn_to_kaddr ( +page_to_pfn (data_pages[k])), + GNTMAP_host_map | GNTMAP_readonly, + lvl2_table[j], domid); + +gnttab_set_unmap_op (&data_unmap_ops[k], + (unsigned long )pfn_to_kaddr ( +page_to_pfn (data_pages[k])), + GNTMAP_host_map | GNTMAP_readonly,-1 ); + k++; + } + } + +/* for grefs in the last lvl2 table page */ + lvl2_table =pfn_to_kaddr (page_to_pfn ( + lvl2_table_pages[n_lvl2_grefs -1 ])); + +for (j =0 ; j < nents_last; j++) { +gnttab_set_map_op (&data_map_ops[k], + (unsigned long )pfn_to_kaddr (page_to_pfn (data_pages[k])), + GNTMAP_host_map | GNTMAP_readonly, + lvl2_table[j], domid); + +gnttab_set_unmap_op (&data_unmap_ops[k], + (unsigned long )pfn_to_kaddr (page_to_pfn (data_pages[k])), + GNTMAP_host_map | GNTMAP_readonly,-1 ); + k++; + } + +if (gnttab_map_refs (data_map_ops,NULL , + data_pages, nents)) { +dev_err (hy_drv_priv->dev, +"HYPERVISOR map grant ref failed\n" ); +return NULL ; + } + +/* unmapping lvl2 table pages */ +if (gnttab_unmap_refs (lvl2_unmap_ops, +NULL , lvl2_table_pages, + n_lvl2_grefs)) { +dev_err (hy_drv_priv->dev, +"Cannot unmap 2nd level refs\n" ); +return NULL ; + } + +/* Mark that pages were unmapped */ +for (i =0 ; i < n_lvl2_grefs; i++) + lvl2_unmap_ops[i].handle =-1 ; + +for (i =0 ; i < nents; i++) { +if (data_map_ops[i].status) { +dev_err (hy_drv_priv->dev, +"HYPERVISOR map grant ref failed status = %d\n" , + data_map_ops[i].status); + goto error_cleanup_data; + }else { + data_unmap_ops[i].handle = data_map_ops[i].handle; + } + } + +/* store these references for unmapping in the future */ + sh_pages_info->unmap_ops = data_unmap_ops; + sh_pages_info->data_pages = data_pages; + +gnttab_free_pages (1 , &lvl3_table_page); +gnttab_free_pages (n_lvl2_grefs, lvl2_table_pages); +kfree (lvl2_table_pages); +kfree (lvl2_map_ops); +kfree (lvl2_unmap_ops); +kfree (data_map_ops); + +dev_dbg (hy_drv_priv->dev,"%s exit\n" , __func__); +return data_pages; + +error_cleanup_data: +gnttab_unmap_refs (data_unmap_ops,NULL , data_pages, + nents); + +gnttab_free_pages (nents, data_pages); + +error_cleanup_lvl2: +if (lvl2_unmap_ops[0 ].handle !=-1 ) +gnttab_unmap_refs (lvl2_unmap_ops,NULL , + lvl2_table_pages, n_lvl2_grefs); +gnttab_free_pages (n_lvl2_grefs, lvl2_table_pages); + +error_cleanup_lvl3: +if (lvl3_unmap_ops.handle !=-1 ) +gnttab_unmap_refs (&lvl3_unmap_ops,NULL , + &lvl3_table_page,1 ); +gnttab_free_pages (1 , &lvl3_table_page); + +kfree (lvl2_table_pages); +kfree (lvl2_map_ops); +kfree (lvl2_unmap_ops); +kfree (data_map_ops); + + +return NULL ; +} + +int xen_be_unmap_shared_pages (void **refs_info,int nents) +{ +struct xen_shared_pages_info *sh_pages_info; + +dev_dbg (hy_drv_priv->dev,"%s entry\n" , __func__); + + sh_pages_info = (struct xen_shared_pages_info *)(*refs_info); + +if (sh_pages_info->unmap_ops ==NULL || + sh_pages_info->data_pages ==NULL ) { +dev_warn (hy_drv_priv->dev, +"pages already cleaned up or buffer not imported yet\n" ); +return 0 ; + } + +if (gnttab_unmap_refs (sh_pages_info->unmap_ops,NULL , + sh_pages_info->data_pages, nents)) { +dev_err (hy_drv_priv->dev,"Cannot unmap data pages\n" ); +return -EFAULT ; + } + +gnttab_free_pages (nents, sh_pages_info->data_pages); + +kfree (sh_pages_info->data_pages); +kfree (sh_pages_info->unmap_ops); + sh_pages_info->unmap_ops =NULL ; + sh_pages_info->data_pages =NULL ; +kfree (sh_pages_info); + sh_pages_info =NULL ; + +dev_dbg (hy_drv_priv->dev,"%s exit\n" , __func__); +return 0 ; +}drivers/dma-buf/hyper_dmabuf/backends/xen/hyper_dmabuf_xen_shm.h new file +46
-0@@ -0,0 +1,46 @@ +/* +* Copyright © 2018 Intel Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice (including the next +* paragraph) shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +* IN THE SOFTWARE. +* +*/ + +#ifndef __HYPER_DMABUF_XEN_SHM_H__ +#define __HYPER_DMABUF_XEN_SHM_H__ + +/* This collects all reference numbers for 2nd level shared pages and +* create a table with those in 1st level shared pages then return reference +* numbers for this top level table. +*/ +int xen_be_share_pages (struct page **pages,int domid,int nents, +void **refs_info); + +int xen_be_unshare_pages (void **refs_info,int nents); + +/* Maps provided top level ref id and then return array of pages containing +* data refs. +*/ +struct page **xen_be_map_shared_pages (unsigned long lvl3_gref,int domid, +int nents, +void **refs_info); + +int xen_be_unmap_shared_pages (void **refs_info,int nents); + +#endif /* __HYPER_DMABUF_XEN_SHM_H__ */ drivers/dma-buf/hyper_dmabuf/hyper_dmabuf_drv.c +10
-0@@ -42,6 +42,10 @@#include "hyper_dmabuf_list.h" #include "hyper_dmabuf_id.h" +#ifdef CONFIG_HYPER_DMABUF_XEN +#include "backends/xen/hyper_dmabuf_xen_drv.h" +#endif +MODULE_LICENSE ("GPL and additional rights" );MODULE_AUTHOR ("Intel Corporation" );@@ -145,7 +149,13 @@ static int __init hyper_dmabuf_drv_init(void)return ret; } +/* currently only supports XEN hypervisor */ +#ifdef CONFIG_HYPER_DMABUF_XEN + hy_drv_priv->bknd_ops = &xen_bknd_ops; +#else hy_drv_priv->bknd_ops =NULL ; +pr_err ("hyper_dmabuf drv currently supports XEN only.\n" ); +#endif if (hy_drv_priv->bknd_ops ==NULL ) {pr_err ("Hyper_dmabuf: no backend found\n" );