diff mbox series

[v15,1/4] RDMA/umem: Support importing dma-buf as user memory region

Message ID 1607629058-141952-2-git-send-email-jianxin.xiong@intel.com (mailing list archive)
State New, archived
Headers show
Series RDMA: Add dma-buf support | expand

Commit Message

Xiong, Jianxin Dec. 10, 2020, 7:37 p.m. UTC
Dma-buf is a standard cross-driver buffer sharing mechanism that can be
used to support peer-to-peer access from RDMA devices.

Device memory exported via dma-buf is associated with a file descriptor.
This is passed to the user space as a property associated with the
buffer allocation. When the buffer is registered as a memory region,
the file descriptor is passed to the RDMA driver along with other
parameters.

Implement the common code for importing dma-buf object and mapping
dma-buf pages.

Signed-off-by: Jianxin Xiong <jianxin.xiong@intel.com>
Reviewed-by: Sean Hefty <sean.hefty@intel.com>
Acked-by: Michael J. Ruhl <michael.j.ruhl@intel.com>
Acked-by: Christian Koenig <christian.koenig@amd.com>
Acked-by: Daniel Vetter <daniel.vetter@ffwll.ch>
---
 drivers/infiniband/core/Makefile      |   2 +-
 drivers/infiniband/core/umem.c        |   3 +
 drivers/infiniband/core/umem_dmabuf.c | 174 ++++++++++++++++++++++++++++++++++
 include/rdma/ib_umem.h                |  48 +++++++++-
 4 files changed, 223 insertions(+), 4 deletions(-)
 create mode 100644 drivers/infiniband/core/umem_dmabuf.c

Comments

kernel test robot Dec. 14, 2020, 6:43 a.m. UTC | #1
Hi Jianxin,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on rdma/for-next]
[also build test ERROR on next-20201211]
[cannot apply to tegra-drm/drm/tegra/for-next v5.10]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Jianxin-Xiong/RDMA-Add-dma-buf-support/20201211-032359
base:   https://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma.git for-next
config: i386-randconfig-r025-20200629 (attached as .config)
compiler: gcc-9 (Debian 9.3.0-15) 9.3.0
reproduce (this is a W=1 build):
        # https://github.com/0day-ci/linux/commit/de7b8a9035076ce9b065dd12d421bab311c1cc43
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Jianxin-Xiong/RDMA-Add-dma-buf-support/20201211-032359
        git checkout de7b8a9035076ce9b065dd12d421bab311c1cc43
        # save the attached .config to linux build tree
        make W=1 ARCH=i386 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   ld: drivers/infiniband/core/umem_dmabuf.o: in function `dma_fence_wait':
>> include/linux/dma-fence.h:581: undefined reference to `dma_fence_wait_timeout'
   ld: drivers/infiniband/core/umem_dmabuf.o: in function `ib_umem_dmabuf_map_pages':
>> drivers/infiniband/core/umem_dmabuf.c:26: undefined reference to `dma_buf_map_attachment'
   ld: drivers/infiniband/core/umem_dmabuf.o: in function `ib_umem_dmabuf_unmap_pages':
>> drivers/infiniband/core/umem_dmabuf.c:100: undefined reference to `dma_buf_unmap_attachment'
   ld: drivers/infiniband/core/umem_dmabuf.o: in function `ib_umem_dmabuf_get':
>> drivers/infiniband/core/umem_dmabuf.c:124: undefined reference to `dma_buf_get'
>> ld: drivers/infiniband/core/umem_dmabuf.c:162: undefined reference to `dma_buf_put'
>> ld: drivers/infiniband/core/umem_dmabuf.c:147: undefined reference to `dma_buf_dynamic_attach'
   ld: drivers/infiniband/core/umem_dmabuf.o: in function `ib_umem_dmabuf_release':
>> drivers/infiniband/core/umem_dmabuf.c:171: undefined reference to `dma_buf_detach'
   ld: drivers/infiniband/core/umem_dmabuf.c:172: undefined reference to `dma_buf_put'

vim +26 drivers/infiniband/core/umem_dmabuf.c

    11	
    12	int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf)
    13	{
    14		struct sg_table *sgt;
    15		struct scatterlist *sg;
    16		struct dma_fence *fence;
    17		unsigned long start, end, cur = 0;
    18		unsigned int nmap = 0;
    19		int i;
    20	
    21		dma_resv_assert_held(umem_dmabuf->attach->dmabuf->resv);
    22	
    23		if (umem_dmabuf->sgt)
    24			goto wait_fence;
    25	
  > 26		sgt = dma_buf_map_attachment(umem_dmabuf->attach, DMA_BIDIRECTIONAL);
    27		if (IS_ERR(sgt))
    28			return PTR_ERR(sgt);
    29	
    30		/* modify the sg list in-place to match umem address and length */
    31	
    32		start = ALIGN_DOWN(umem_dmabuf->umem.address, PAGE_SIZE);
    33		end = ALIGN(umem_dmabuf->umem.address + umem_dmabuf->umem.length,
    34			    PAGE_SIZE);
    35		for_each_sgtable_dma_sg(sgt, sg, i) {
    36			if (start < cur + sg_dma_len(sg) && cur < end)
    37				nmap++;
    38			if (cur <= start && start < cur + sg_dma_len(sg)) {
    39				unsigned long offset = start - cur;
    40	
    41				umem_dmabuf->first_sg = sg;
    42				umem_dmabuf->first_sg_offset = offset;
    43				sg_dma_address(sg) += offset;
    44				sg_dma_len(sg) -= offset;
    45				cur += offset;
    46			}
    47			if (cur < end && end <= cur + sg_dma_len(sg)) {
    48				unsigned long trim = cur + sg_dma_len(sg) - end;
    49	
    50				umem_dmabuf->last_sg = sg;
    51				umem_dmabuf->last_sg_trim = trim;
    52				sg_dma_len(sg) -= trim;
    53				break;
    54			}
    55			cur += sg_dma_len(sg);
    56		}
    57	
    58		umem_dmabuf->umem.sg_head.sgl = umem_dmabuf->first_sg;
    59		umem_dmabuf->umem.sg_head.nents = nmap;
    60		umem_dmabuf->umem.nmap = nmap;
    61		umem_dmabuf->sgt = sgt;
    62	
    63	wait_fence:
    64		/*
    65		 * Although the sg list is valid now, the content of the pages
    66		 * may be not up-to-date. Wait for the exporter to finish
    67		 * the migration.
    68		 */
    69		fence = dma_resv_get_excl(umem_dmabuf->attach->dmabuf->resv);
    70		if (fence)
    71			return dma_fence_wait(fence, false);
    72	
    73		return 0;
    74	}
    75	EXPORT_SYMBOL(ib_umem_dmabuf_map_pages);
    76	
    77	void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf)
    78	{
    79		dma_resv_assert_held(umem_dmabuf->attach->dmabuf->resv);
    80	
    81		if (!umem_dmabuf->sgt)
    82			return;
    83	
    84		/* retore the original sg list */
    85		if (umem_dmabuf->first_sg) {
    86			sg_dma_address(umem_dmabuf->first_sg) -=
    87				umem_dmabuf->first_sg_offset;
    88			sg_dma_len(umem_dmabuf->first_sg) +=
    89				umem_dmabuf->first_sg_offset;
    90			umem_dmabuf->first_sg = NULL;
    91			umem_dmabuf->first_sg_offset = 0;
    92		}
    93		if (umem_dmabuf->last_sg) {
    94			sg_dma_len(umem_dmabuf->last_sg) +=
    95				umem_dmabuf->last_sg_trim;
    96			umem_dmabuf->last_sg = NULL;
    97			umem_dmabuf->last_sg_trim = 0;
    98		}
    99	
 > 100		dma_buf_unmap_attachment(umem_dmabuf->attach, umem_dmabuf->sgt,
   101					 DMA_BIDIRECTIONAL);
   102	
   103		umem_dmabuf->sgt = NULL;
   104	}
   105	EXPORT_SYMBOL(ib_umem_dmabuf_unmap_pages);
   106	
   107	struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
   108						  unsigned long offset, size_t size,
   109						  int fd, int access,
   110						  const struct dma_buf_attach_ops *ops)
   111	{
   112		struct dma_buf *dmabuf;
   113		struct ib_umem_dmabuf *umem_dmabuf;
   114		struct ib_umem *umem;
   115		unsigned long end;
   116		struct ib_umem_dmabuf *ret = ERR_PTR(-EINVAL);
   117	
   118		if (check_add_overflow(offset, (unsigned long)size, &end))
   119			return ret;
   120	
   121		if (unlikely(!ops || !ops->move_notify))
   122			return ret;
   123	
 > 124		dmabuf = dma_buf_get(fd);
   125		if (IS_ERR(dmabuf))
   126			return ERR_CAST(dmabuf);
   127	
   128		if (dmabuf->size < end)
   129			goto out_release_dmabuf;
   130	
   131		umem_dmabuf = kzalloc(sizeof(*umem_dmabuf), GFP_KERNEL);
   132		if (!umem_dmabuf) {
   133			ret = ERR_PTR(-ENOMEM);
   134			goto out_release_dmabuf;
   135		}
   136	
   137		umem = &umem_dmabuf->umem;
   138		umem->ibdev = device;
   139		umem->length = size;
   140		umem->address = offset;
   141		umem->writable = ib_access_writable(access);
   142		umem->is_dmabuf = 1;
   143	
   144		if (!ib_umem_num_pages(umem))
   145			goto out_free_umem;
   146	
 > 147		umem_dmabuf->attach = dma_buf_dynamic_attach(
   148						dmabuf,
   149						device->dma_device,
   150						ops,
   151						umem_dmabuf);
   152		if (IS_ERR(umem_dmabuf->attach)) {
   153			ret = ERR_CAST(umem_dmabuf->attach);
   154			goto out_free_umem;
   155		}
   156		return umem_dmabuf;
   157	
   158	out_free_umem:
   159		kfree(umem_dmabuf);
   160	
   161	out_release_dmabuf:
 > 162		dma_buf_put(dmabuf);
   163		return ret;
   164	}
   165	EXPORT_SYMBOL(ib_umem_dmabuf_get);
   166	
   167	void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf)
   168	{
   169		struct dma_buf *dmabuf = umem_dmabuf->attach->dmabuf;
   170	
 > 171		dma_buf_detach(dmabuf, umem_dmabuf->attach);

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org
diff mbox series

Patch

diff --git a/drivers/infiniband/core/Makefile b/drivers/infiniband/core/Makefile
index ccf2670..8ab4eea 100644
--- a/drivers/infiniband/core/Makefile
+++ b/drivers/infiniband/core/Makefile
@@ -40,5 +40,5 @@  ib_uverbs-y :=			uverbs_main.o uverbs_cmd.o uverbs_marshall.o \
 				uverbs_std_types_srq.o \
 				uverbs_std_types_wq.o \
 				uverbs_std_types_qp.o
-ib_uverbs-$(CONFIG_INFINIBAND_USER_MEM) += umem.o
+ib_uverbs-$(CONFIG_INFINIBAND_USER_MEM) += umem.o umem_dmabuf.o
 ib_uverbs-$(CONFIG_INFINIBAND_ON_DEMAND_PAGING) += umem_odp.o
diff --git a/drivers/infiniband/core/umem.c b/drivers/infiniband/core/umem.c
index 7ca4112..cc131f8 100644
--- a/drivers/infiniband/core/umem.c
+++ b/drivers/infiniband/core/umem.c
@@ -2,6 +2,7 @@ 
  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
  * Copyright (c) 2005 Cisco Systems.  All rights reserved.
  * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
+ * Copyright (c) 2020 Intel Corporation. All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -278,6 +279,8 @@  void ib_umem_release(struct ib_umem *umem)
 {
 	if (!umem)
 		return;
+	if (umem->is_dmabuf)
+		return ib_umem_dmabuf_release(to_ib_umem_dmabuf(umem));
 	if (umem->is_odp)
 		return ib_umem_odp_release(to_ib_umem_odp(umem));
 
diff --git a/drivers/infiniband/core/umem_dmabuf.c b/drivers/infiniband/core/umem_dmabuf.c
new file mode 100644
index 0000000..f9b5162
--- /dev/null
+++ b/drivers/infiniband/core/umem_dmabuf.c
@@ -0,0 +1,174 @@ 
+// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
+/*
+ * Copyright (c) 2020 Intel Corporation. All rights reserved.
+ */
+
+#include <linux/dma-buf.h>
+#include <linux/dma-resv.h>
+#include <linux/dma-mapping.h>
+
+#include "uverbs.h"
+
+int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf)
+{
+	struct sg_table *sgt;
+	struct scatterlist *sg;
+	struct dma_fence *fence;
+	unsigned long start, end, cur = 0;
+	unsigned int nmap = 0;
+	int i;
+
+	dma_resv_assert_held(umem_dmabuf->attach->dmabuf->resv);
+
+	if (umem_dmabuf->sgt)
+		goto wait_fence;
+
+	sgt = dma_buf_map_attachment(umem_dmabuf->attach, DMA_BIDIRECTIONAL);
+	if (IS_ERR(sgt))
+		return PTR_ERR(sgt);
+
+	/* modify the sg list in-place to match umem address and length */
+
+	start = ALIGN_DOWN(umem_dmabuf->umem.address, PAGE_SIZE);
+	end = ALIGN(umem_dmabuf->umem.address + umem_dmabuf->umem.length,
+		    PAGE_SIZE);
+	for_each_sgtable_dma_sg(sgt, sg, i) {
+		if (start < cur + sg_dma_len(sg) && cur < end)
+			nmap++;
+		if (cur <= start && start < cur + sg_dma_len(sg)) {
+			unsigned long offset = start - cur;
+
+			umem_dmabuf->first_sg = sg;
+			umem_dmabuf->first_sg_offset = offset;
+			sg_dma_address(sg) += offset;
+			sg_dma_len(sg) -= offset;
+			cur += offset;
+		}
+		if (cur < end && end <= cur + sg_dma_len(sg)) {
+			unsigned long trim = cur + sg_dma_len(sg) - end;
+
+			umem_dmabuf->last_sg = sg;
+			umem_dmabuf->last_sg_trim = trim;
+			sg_dma_len(sg) -= trim;
+			break;
+		}
+		cur += sg_dma_len(sg);
+	}
+
+	umem_dmabuf->umem.sg_head.sgl = umem_dmabuf->first_sg;
+	umem_dmabuf->umem.sg_head.nents = nmap;
+	umem_dmabuf->umem.nmap = nmap;
+	umem_dmabuf->sgt = sgt;
+
+wait_fence:
+	/*
+	 * Although the sg list is valid now, the content of the pages
+	 * may be not up-to-date. Wait for the exporter to finish
+	 * the migration.
+	 */
+	fence = dma_resv_get_excl(umem_dmabuf->attach->dmabuf->resv);
+	if (fence)
+		return dma_fence_wait(fence, false);
+
+	return 0;
+}
+EXPORT_SYMBOL(ib_umem_dmabuf_map_pages);
+
+void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf)
+{
+	dma_resv_assert_held(umem_dmabuf->attach->dmabuf->resv);
+
+	if (!umem_dmabuf->sgt)
+		return;
+
+	/* retore the original sg list */
+	if (umem_dmabuf->first_sg) {
+		sg_dma_address(umem_dmabuf->first_sg) -=
+			umem_dmabuf->first_sg_offset;
+		sg_dma_len(umem_dmabuf->first_sg) +=
+			umem_dmabuf->first_sg_offset;
+		umem_dmabuf->first_sg = NULL;
+		umem_dmabuf->first_sg_offset = 0;
+	}
+	if (umem_dmabuf->last_sg) {
+		sg_dma_len(umem_dmabuf->last_sg) +=
+			umem_dmabuf->last_sg_trim;
+		umem_dmabuf->last_sg = NULL;
+		umem_dmabuf->last_sg_trim = 0;
+	}
+
+	dma_buf_unmap_attachment(umem_dmabuf->attach, umem_dmabuf->sgt,
+				 DMA_BIDIRECTIONAL);
+
+	umem_dmabuf->sgt = NULL;
+}
+EXPORT_SYMBOL(ib_umem_dmabuf_unmap_pages);
+
+struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
+					  unsigned long offset, size_t size,
+					  int fd, int access,
+					  const struct dma_buf_attach_ops *ops)
+{
+	struct dma_buf *dmabuf;
+	struct ib_umem_dmabuf *umem_dmabuf;
+	struct ib_umem *umem;
+	unsigned long end;
+	struct ib_umem_dmabuf *ret = ERR_PTR(-EINVAL);
+
+	if (check_add_overflow(offset, (unsigned long)size, &end))
+		return ret;
+
+	if (unlikely(!ops || !ops->move_notify))
+		return ret;
+
+	dmabuf = dma_buf_get(fd);
+	if (IS_ERR(dmabuf))
+		return ERR_CAST(dmabuf);
+
+	if (dmabuf->size < end)
+		goto out_release_dmabuf;
+
+	umem_dmabuf = kzalloc(sizeof(*umem_dmabuf), GFP_KERNEL);
+	if (!umem_dmabuf) {
+		ret = ERR_PTR(-ENOMEM);
+		goto out_release_dmabuf;
+	}
+
+	umem = &umem_dmabuf->umem;
+	umem->ibdev = device;
+	umem->length = size;
+	umem->address = offset;
+	umem->writable = ib_access_writable(access);
+	umem->is_dmabuf = 1;
+
+	if (!ib_umem_num_pages(umem))
+		goto out_free_umem;
+
+	umem_dmabuf->attach = dma_buf_dynamic_attach(
+					dmabuf,
+					device->dma_device,
+					ops,
+					umem_dmabuf);
+	if (IS_ERR(umem_dmabuf->attach)) {
+		ret = ERR_CAST(umem_dmabuf->attach);
+		goto out_free_umem;
+	}
+	return umem_dmabuf;
+
+out_free_umem:
+	kfree(umem_dmabuf);
+
+out_release_dmabuf:
+	dma_buf_put(dmabuf);
+	return ret;
+}
+EXPORT_SYMBOL(ib_umem_dmabuf_get);
+
+void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf)
+{
+	struct dma_buf *dmabuf = umem_dmabuf->attach->dmabuf;
+
+	dma_buf_detach(dmabuf, umem_dmabuf->attach);
+	dma_buf_put(dmabuf);
+	kfree(umem_dmabuf);
+}
diff --git a/include/rdma/ib_umem.h b/include/rdma/ib_umem.h
index 7752211..676c57f 100644
--- a/include/rdma/ib_umem.h
+++ b/include/rdma/ib_umem.h
@@ -1,6 +1,7 @@ 
 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
 /*
  * Copyright (c) 2007 Cisco Systems.  All rights reserved.
+ * Copyright (c) 2020 Intel Corporation.  All rights reserved.
  */
 
 #ifndef IB_UMEM_H
@@ -13,6 +14,7 @@ 
 
 struct ib_ucontext;
 struct ib_umem_odp;
+struct dma_buf_attach_ops;
 
 struct ib_umem {
 	struct ib_device       *ibdev;
@@ -22,12 +24,29 @@  struct ib_umem {
 	unsigned long		address;
 	u32 writable : 1;
 	u32 is_odp : 1;
+	u32 is_dmabuf : 1;
 	struct work_struct	work;
 	struct sg_table sg_head;
 	int             nmap;
 	unsigned int    sg_nents;
 };
 
+struct ib_umem_dmabuf {
+	struct ib_umem umem;
+	struct dma_buf_attachment *attach;
+	struct sg_table *sgt;
+	struct scatterlist *first_sg;
+	struct scatterlist *last_sg;
+	unsigned long first_sg_offset;
+	unsigned long last_sg_trim;
+	void *private;
+};
+
+static inline struct ib_umem_dmabuf *to_ib_umem_dmabuf(struct ib_umem *umem)
+{
+	return container_of(umem, struct ib_umem_dmabuf, umem);
+}
+
 /* Returns the offset of the umem start relative to the first page. */
 static inline int ib_umem_offset(struct ib_umem *umem)
 {
@@ -86,6 +105,7 @@  int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
 unsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
 				     unsigned long pgsz_bitmap,
 				     unsigned long virt);
+
 /**
  * ib_umem_find_best_pgoff - Find best HW page size
  *
@@ -116,6 +136,14 @@  static inline unsigned long ib_umem_find_best_pgoff(struct ib_umem *umem,
 				      dma_addr & pgoff_bitmask);
 }
 
+struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
+					  unsigned long offset, size_t size,
+					  int fd, int access,
+					  const struct dma_buf_attach_ops *ops);
+int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf);
+void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf);
+void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf);
+
 #else /* CONFIG_INFINIBAND_USER_MEM */
 
 #include <linux/err.h>
@@ -124,12 +152,12 @@  static inline struct ib_umem *ib_umem_get(struct ib_device *device,
 					  unsigned long addr, size_t size,
 					  int access)
 {
-	return ERR_PTR(-EINVAL);
+	return ERR_PTR(-EOPNOTSUPP);
 }
 static inline void ib_umem_release(struct ib_umem *umem) { }
 static inline int ib_umem_copy_from(void *dst, struct ib_umem *umem, size_t offset,
 		      		    size_t length) {
-	return -EINVAL;
+	return -EOPNOTSUPP;
 }
 static inline unsigned long ib_umem_find_best_pgsz(struct ib_umem *umem,
 						   unsigned long pgsz_bitmap,
@@ -143,7 +171,21 @@  static inline unsigned long ib_umem_find_best_pgoff(struct ib_umem *umem,
 {
 	return 0;
 }
+static inline
+struct ib_umem_dmabuf *ib_umem_dmabuf_get(struct ib_device *device,
+					  unsigned long offset,
+					  size_t size, int fd,
+					  int access,
+					  struct dma_buf_attach_ops *ops)
+{
+	return ERR_PTR(-EOPNOTSUPP);
+}
+static inline int ib_umem_dmabuf_map_pages(struct ib_umem_dmabuf *umem_dmabuf)
+{
+	return -EOPNOTSUPP;
+}
+static inline void ib_umem_dmabuf_unmap_pages(struct ib_umem_dmabuf *umem_dmabuf) { }
+static inline void ib_umem_dmabuf_release(struct ib_umem_dmabuf *umem_dmabuf) { }
 
 #endif /* CONFIG_INFINIBAND_USER_MEM */
-
 #endif /* IB_UMEM_H */