Skip to contentSkip to conversation

RDMA/hns: Add a new mmap implementation

PATCH Superseded Wenpeng Liang 2 participants 7 messages Oct 12, 2021 – Oct 26, 2021

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 RDMA/hns: Add a new mmap implementation

From: Chengchang Tang <[email protected]>

Add a new implementation for mmap by using the new mmap entry API.

The new implementation prepares for subsequent features and is compatible with the old implementation. And the old implementation using hard-coded offset will not be extended in the future.

---
 drivers/infiniband/hw/hns/hns_roce_device.h |  23 +++
 drivers/infiniband/hw/hns/hns_roce_main.c   | 208 +++++++++++++++++---
 include/uapi/rdma/hns-abi.h                 |  21 +-
 3 files changed, 225 insertions(+), 27 deletions(-)
drivers/infiniband/hw/hns/hns_roce_device.h +23 -0
index 9467c39e3d28..1d4cf3f083c2 100644
@@ -225,11 +225,25 @@ struct hns_roce_uar {
 	unsigned long	logic_idx;
 };
 
+struct hns_user_mmap_entry {
+	struct rdma_user_mmap_entry rdma_entry;
+	u64 address;
+	u8 mmap_flag;
+};
+
+enum hns_roce_mmap_type {
+	HNS_ROCE_MMAP_TYPE_DB = 1,
+	HNS_ROCE_MMAP_TYPE_TPTR,
+};
+
 struct hns_roce_ucontext {
 	struct ib_ucontext	ibucontext;
 	struct hns_roce_uar	uar;
 	struct list_head	page_list;
 	struct mutex		page_mutex;
+	bool			mmap_key_support;
+	struct hns_user_mmap_entry *db_mmap_entry;
+	struct hns_user_mmap_entry *tptr_mmap_entry;
 };
 
 struct hns_roce_pd {
@@ -1049,6 +1063,12 @@ static inline struct hns_roce_srq *to_hr_srq(struct ib_srq *ibsrq)
 	return container_of(ibsrq, struct hns_roce_srq, ibsrq);
 }
 
+static inline struct hns_user_mmap_entry *to_hns_mmap(
+		struct rdma_user_mmap_entry *rdma_entry)
+{
+	return container_of(rdma_entry, struct hns_user_mmap_entry, rdma_entry);
+}
+
 static inline void hns_roce_write64_k(__le32 val[2], void __iomem *dest)
 {
 	writeq(*(u64 *)val, dest);
@@ -1259,4 +1279,7 @@ int hns_roce_init(struct hns_roce_dev *hr_dev);
 void hns_roce_exit(struct hns_roce_dev *hr_dev);
 int hns_roce_fill_res_cq_entry(struct sk_buff *msg,
 			       struct ib_cq *ib_cq);
+struct hns_user_mmap_entry *hns_roce_user_mmap_entry_insert(
+				struct ib_ucontext *ucontext, u64 address,
+				size_t length, u8 mmap_flag);
 #endif /* _HNS_ROCE_DEVICE_H */
drivers/infiniband/hw/hns/hns_roce_main.c +182 -26

263 lines not on this page · raw message

include/uapi/rdma/hns-abi.h +20 -1
index 42b177655560..ce1e39f21d73 100644
@@ -83,11 +83,30 @@ struct hns_roce_ib_create_qp_resp {
 	__aligned_u64 cap_flags;
 };
 
+enum hns_roce_alloc_uctx_comp_flag {
+	HNS_ROCE_ALLOC_UCTX_COMP_CONFIG = 1 << 0,
+};
+
+enum hns_roce_alloc_uctx_resp_config {
+	HNS_ROCE_UCTX_RESP_MMAP_KEY_EN = 1 << 0,
+};
+
+enum hns_roce_alloc_uctx_req_config {
+	HNS_ROCE_UCTX_REQ_MMAP_KEY_EN = 1 << 0,
+};
+
+struct hns_roce_ib_alloc_ucontext {
+	__u32 comp;
+	__u32 config;
+};
+
 struct hns_roce_ib_alloc_ucontext_resp {
 	__u32	qp_tab_size;
 	__u32	cqe_size;
 	__u32	srq_tab_size;
-	__u32	reserved;
+	__u8    config;
+	__u8    rsv[3];
+	__aligned_u64 db_mmap_key;
 };
 
 struct hns_roce_ib_alloc_pd_resp {
Signature · 1 line
2.33.0
maintainer
#1 lore raw
Quoted 19 lines · Wenpeng Liang

From: Chengchang Tang <[email protected]>

Add a new implementation for mmap by using the new mmap entry API.

The new implementation prepares for subsequent features and is compatible with the old implementation. And the old implementation using hard-coded offset will not be extended in the future.

 drivers/infiniband/hw/hns/hns_roce_device.h |  23 +++
 drivers/infiniband/hw/hns/hns_roce_main.c   | 208 +++++++++++++++++---
 include/uapi/rdma/hns-abi.h                 |  21 +-
 3 files changed, 225 insertions(+), 27 deletions(-)
drivers/infiniband/hw/hns/hns_roce_device.h +1 -0
index 9467c39e3d28..1d4cf3f083c2 100644
+++ b/drivers/infiniband/hw/hns/hns_roce_device.h

drivers/infiniband/hw/hns/hns_roce_device.h struct hns_roce_uar { · line 231

Quoted 7 lines
drivers/infiniband/hw/hns/hns_roce_device.h +4 -0
@@ -225,11 +225,25 @@ struct hns_roce_uar {
 	unsigned long	logic_idx;
 };
 
+struct hns_user_mmap_entry {
+	struct rdma_user_mmap_entry rdma_entry;
+	u64 address;
+	u8 mmap_flag;

Call this mmap_type and use the enum:

enum hns_roce_mmap_type mmap_type

Quoted 17 lines
drivers/infiniband/hw/hns/hns_roce_device.h +17 -0
+struct hns_user_mmap_entry *hns_roce_user_mmap_entry_insert(
+				struct ib_ucontext *ucontext, u64 address,
+				size_t length, u8 mmap_flag)
+{
+#define HNS_ROCE_PGOFFSET_TPTR 1
+#define HNS_ROCE_PGOFFSET_DB 0
+	struct hns_roce_ucontext *context = to_hr_ucontext(ucontext);
+	struct hns_user_mmap_entry *entry;
+	int ret;
+
+	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
+	if (!entry)
+		return NULL;
+
+	entry->address = address;
+	entry->mmap_flag = mmap_flag;
+

drivers/infiniband/hw/hns/hns_roce_device.h struct hns_roce_uar {

Quoted 10 lines
drivers/infiniband/hw/hns/hns_roce_device.h +10 -0
+	if (context->mmap_key_support) {
+		ret = rdma_user_mmap_entry_insert(ucontext, &entry->rdma_entry,
+						  length);
+	} else {
+		switch (mmap_flag) {
+		case HNS_ROCE_MMAP_TYPE_DB:
+			ret = rdma_user_mmap_entry_insert_range(ucontext,
+						&entry->rdma_entry, length,
+						HNS_ROCE_PGOFFSET_DB,
+						HNS_ROCE_PGOFFSET_DB);

Please add this to avoid the odd #defines:

static inline int
rdma_user_mmap_entry_insert_exact(struct ib_ucontext *ucontext,
struct rdma_user_mmap_entry *entry, size_t length, u32 pgoff)
{
return rdma_user_mmap_entry_insert_range(ucontext, entry, length, pgoff,
pgoff);
}

Quoted 26 lines
drivers/infiniband/hw/hns/hns_roce_device.h +9 -16
-static int hns_roce_mmap(struct ib_ucontext *context,
-			 struct vm_area_struct *vma)
+static int hns_roce_mmap(struct ib_ucontext *uctx, struct vm_area_struct *vma)
 {
-	struct hns_roce_dev *hr_dev = to_hr_dev(context->device);
-
-	switch (vma->vm_pgoff) {
-	case 0:
-		return rdma_user_mmap_io(context, vma,
-					 to_hr_ucontext(context)->uar.pfn,
-					 PAGE_SIZE,
-					 pgprot_noncached(vma->vm_page_prot),
-					 NULL);
-
-	/* vm_pgoff: 1 -- TPTR */
-	case 1:
-		if (!hr_dev->tptr_dma_addr || !hr_dev->tptr_size)
-			return -EINVAL;
+	struct hns_roce_dev *hr_dev = to_hr_dev(uctx->device);
+	struct ib_device *ibdev = &hr_dev->ib_dev;
+	struct rdma_user_mmap_entry *rdma_entry;
+	struct hns_user_mmap_entry *entry;
+	phys_addr_t pfn;
+	pgprot_t prot;
+	int ret;
+

drivers/infiniband/hw/hns/hns_roce_device.h

Quoted 10 lines
drivers/infiniband/hw/hns/hns_roce_device.h +10 -0
+	rdma_entry = rdma_user_mmap_entry_get_pgoff(uctx, vma->vm_pgoff);
+	if (!rdma_entry) {
+		ibdev_err(ibdev, "Invalid entry vm_pgoff %lu.\n",
+			  vma->vm_pgoff);
+		return -EINVAL;
+	}
+
+	entry = to_hns_mmap(rdma_entry);
+	pfn = entry->address >> PAGE_SHIFT;
+	prot = vma->vm_page_prot;

Just write

if (entry->mmap_type != HNS_ROCE_MMAP_TYPE_TPTR)

    prot = pgprot_noncached(prot);
 ret = rdma_user_mmap_io(uctx, vma, pfn,
			rdma_entry->npages * PAGE_SIZE,
			pgprot_noncached(prot), rdma_entry);

No need for the big case statement

Quoted 22 lines
include/uapi/rdma/hns-abi.h +16 -0
index 42b177655560..ce1e39f21d73 100644
+++ b/include/uapi/rdma/hns-abi.h
@@ -83,11 +83,30 @@ struct hns_roce_ib_create_qp_resp {
 	__aligned_u64 cap_flags;
 };
 
+enum hns_roce_alloc_uctx_comp_flag {
+	HNS_ROCE_ALLOC_UCTX_COMP_CONFIG = 1 << 0,
+};
+
+enum hns_roce_alloc_uctx_resp_config {
+	HNS_ROCE_UCTX_RESP_MMAP_KEY_EN = 1 << 0,
+};
+
+enum hns_roce_alloc_uctx_req_config {
+	HNS_ROCE_UCTX_REQ_MMAP_KEY_EN = 1 << 0,
+};
+
+struct hns_roce_ib_alloc_ucontext {
+	__u32 comp;
+	__u32 config;

include/uapi/rdma/hns-abi.h struct hns_roce_ib_create_qp_resp { · line 109

Quoted 10 lines
include/uapi/rdma/hns-abi.h +5 -1
+};
+
 struct hns_roce_ib_alloc_ucontext_resp {
 	__u32	qp_tab_size;
 	__u32	cqe_size;
 	__u32	srq_tab_size;
-	__u32	reserved;
+	__u8    config;
+	__u8    rsv[3];
+	__aligned_u64 db_mmap_key;

I'm confused, this doesn't change the uAPI, so why add this stuff? This should go in a later patch?

Jason

#2 lore raw
Quoted 23 lines · Jason Gunthorpe
Quoted 22 lines · Wenpeng Liang

From: Chengchang Tang <[email protected]>

Add a new implementation for mmap by using the new mmap entry API.

The new implementation prepares for subsequent features and is compatible with the old implementation. And the old implementation using hard-coded offset will not be extended in the future.

 drivers/infiniband/hw/hns/hns_roce_device.h |  23 +++
 drivers/infiniband/hw/hns/hns_roce_main.c   | 208 +++++++++++++++++---
 include/uapi/rdma/hns-abi.h                 |  21 +-
 3 files changed, 225 insertions(+), 27 deletions(-)
drivers/infiniband/hw/hns/hns_roce_device.h +1 -0
index 9467c39e3d28..1d4cf3f083c2 100644
+++ b/drivers/infiniband/hw/hns/hns_roce_device.h
@@ -225,11 +225,25 @@ struct hns_roce_uar {
 	unsigned long	logic_idx;
 };
 
Quoted 9 lines

drivers/infiniband/hw/hns/hns_roce_device.h struct hns_roce_uar { · line 231

Quoted 4 lines
drivers/infiniband/hw/hns/hns_roce_device.h +4 -0
+struct hns_user_mmap_entry {
+	struct rdma_user_mmap_entry rdma_entry;
+	u64 address;
+	u8 mmap_flag;

Call this mmap_type and use the enum:

enum hns_roce_mmap_type mmap_type

I will fix it in v3, thanks!

Quoted 30 lines
Quoted 17 lines
drivers/infiniband/hw/hns/hns_roce_device.h +17 -0
+struct hns_user_mmap_entry *hns_roce_user_mmap_entry_insert(
+				struct ib_ucontext *ucontext, u64 address,
+				size_t length, u8 mmap_flag)
+{
+#define HNS_ROCE_PGOFFSET_TPTR 1
+#define HNS_ROCE_PGOFFSET_DB 0
+	struct hns_roce_ucontext *context = to_hr_ucontext(ucontext);
+	struct hns_user_mmap_entry *entry;
+	int ret;
+
+	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
+	if (!entry)
+		return NULL;
+
+	entry->address = address;
+	entry->mmap_flag = mmap_flag;
+

drivers/infiniband/hw/hns/hns_roce_device.h struct hns_roce_uar {

Quoted 10 lines
drivers/infiniband/hw/hns/hns_roce_device.h +10 -0
+	if (context->mmap_key_support) {
+		ret = rdma_user_mmap_entry_insert(ucontext, &entry->rdma_entry,
+						  length);
+	} else {
+		switch (mmap_flag) {
+		case HNS_ROCE_MMAP_TYPE_DB:
+			ret = rdma_user_mmap_entry_insert_range(ucontext,
+						&entry->rdma_entry, length,
+						HNS_ROCE_PGOFFSET_DB,
+						HNS_ROCE_PGOFFSET_DB);

Please add this to avoid the odd #defines:

Quoted 9 lines

static inline int
rdma_user_mmap_entry_insert_exact(struct ib_ucontext *ucontext,
struct rdma_user_mmap_entry *entry, size_t length, u32 pgoff)
{
return rdma_user_mmap_entry_insert_range(ucontext, entry, length, pgoff,
pgoff);
}

ditto.

Quoted 39 lines
Quoted 26 lines
drivers/infiniband/hw/hns/hns_roce_device.h +9 -16
-static int hns_roce_mmap(struct ib_ucontext *context,
-			 struct vm_area_struct *vma)
+static int hns_roce_mmap(struct ib_ucontext *uctx, struct vm_area_struct *vma)
 {
-	struct hns_roce_dev *hr_dev = to_hr_dev(context->device);
-
-	switch (vma->vm_pgoff) {
-	case 0:
-		return rdma_user_mmap_io(context, vma,
-					 to_hr_ucontext(context)->uar.pfn,
-					 PAGE_SIZE,
-					 pgprot_noncached(vma->vm_page_prot),
-					 NULL);
-
-	/* vm_pgoff: 1 -- TPTR */
-	case 1:
-		if (!hr_dev->tptr_dma_addr || !hr_dev->tptr_size)
-			return -EINVAL;
+	struct hns_roce_dev *hr_dev = to_hr_dev(uctx->device);
+	struct ib_device *ibdev = &hr_dev->ib_dev;
+	struct rdma_user_mmap_entry *rdma_entry;
+	struct hns_user_mmap_entry *entry;
+	phys_addr_t pfn;
+	pgprot_t prot;
+	int ret;
+

drivers/infiniband/hw/hns/hns_roce_device.h

Quoted 10 lines
drivers/infiniband/hw/hns/hns_roce_device.h +10 -0
+	rdma_entry = rdma_user_mmap_entry_get_pgoff(uctx, vma->vm_pgoff);
+	if (!rdma_entry) {
+		ibdev_err(ibdev, "Invalid entry vm_pgoff %lu.\n",
+			  vma->vm_pgoff);
+		return -EINVAL;
+	}
+
+	entry = to_hns_mmap(rdma_entry);
+	pfn = entry->address >> PAGE_SHIFT;
+	prot = vma->vm_page_prot;

Just write

Quoted 8 lines

if (entry->mmap_type != HNS_ROCE_MMAP_TYPE_TPTR)

    prot = pgprot_noncached(prot);
 ret = rdma_user_mmap_io(uctx, vma, pfn,
			rdma_entry->npages * PAGE_SIZE,
			pgprot_noncached(prot), rdma_entry);

No need for the big case statement

There will be new features added to this switch branch in a later patch.

Quoted 36 lines
Quoted 22 lines
include/uapi/rdma/hns-abi.h +16 -0
index 42b177655560..ce1e39f21d73 100644
+++ b/include/uapi/rdma/hns-abi.h
@@ -83,11 +83,30 @@ struct hns_roce_ib_create_qp_resp {
 	__aligned_u64 cap_flags;
 };
 
+enum hns_roce_alloc_uctx_comp_flag {
+	HNS_ROCE_ALLOC_UCTX_COMP_CONFIG = 1 << 0,
+};
+
+enum hns_roce_alloc_uctx_resp_config {
+	HNS_ROCE_UCTX_RESP_MMAP_KEY_EN = 1 << 0,
+};
+
+enum hns_roce_alloc_uctx_req_config {
+	HNS_ROCE_UCTX_REQ_MMAP_KEY_EN = 1 << 0,
+};
+
+struct hns_roce_ib_alloc_ucontext {
+	__u32 comp;
+	__u32 config;

include/uapi/rdma/hns-abi.h struct hns_roce_ib_create_qp_resp { · line 109

Quoted 10 lines
include/uapi/rdma/hns-abi.h +5 -1
+};
+
 struct hns_roce_ib_alloc_ucontext_resp {
 	__u32	qp_tab_size;
 	__u32	cqe_size;
 	__u32	srq_tab_size;
-	__u32	reserved;
+	__u8    config;
+	__u8    rsv[3];
+	__aligned_u64 db_mmap_key;

I'm confused, this doesn't change the uAPI, so why add this stuff? This should go in a later patch?

Quoted 3 lines

Jason
.

The related userspace series is named "libhns: Add a new mmap implementation".

Thanks,
Wenpeng

#3 lore raw

Sorry, the reply is incomplete, please ignore this email.

Quoted 174 lines · Wenpeng Liang
Quoted 23 lines · Jason Gunthorpe
Quoted 22 lines · Wenpeng Liang

From: Chengchang Tang <[email protected]>

Add a new implementation for mmap by using the new mmap entry API.

The new implementation prepares for subsequent features and is compatible with the old implementation. And the old implementation using hard-coded offset will not be extended in the future.

 drivers/infiniband/hw/hns/hns_roce_device.h |  23 +++
 drivers/infiniband/hw/hns/hns_roce_main.c   | 208 +++++++++++++++++---
 include/uapi/rdma/hns-abi.h                 |  21 +-
 3 files changed, 225 insertions(+), 27 deletions(-)
drivers/infiniband/hw/hns/hns_roce_device.h +1 -0
index 9467c39e3d28..1d4cf3f083c2 100644
+++ b/drivers/infiniband/hw/hns/hns_roce_device.h
@@ -225,11 +225,25 @@ struct hns_roce_uar {
 	unsigned long	logic_idx;
 };
 
Quoted 9 lines

drivers/infiniband/hw/hns/hns_roce_device.h struct hns_roce_uar { · line 231

Quoted 4 lines
drivers/infiniband/hw/hns/hns_roce_device.h +4 -0
+struct hns_user_mmap_entry {
+	struct rdma_user_mmap_entry rdma_entry;
+	u64 address;
+	u8 mmap_flag;

Call this mmap_type and use the enum:

enum hns_roce_mmap_type mmap_type

I will fix it in v3, thanks!

Quoted 30 lines
Quoted 17 lines
drivers/infiniband/hw/hns/hns_roce_device.h +17 -0
+struct hns_user_mmap_entry *hns_roce_user_mmap_entry_insert(
+				struct ib_ucontext *ucontext, u64 address,
+				size_t length, u8 mmap_flag)
+{
+#define HNS_ROCE_PGOFFSET_TPTR 1
+#define HNS_ROCE_PGOFFSET_DB 0
+	struct hns_roce_ucontext *context = to_hr_ucontext(ucontext);
+	struct hns_user_mmap_entry *entry;
+	int ret;
+
+	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
+	if (!entry)
+		return NULL;
+
+	entry->address = address;
+	entry->mmap_flag = mmap_flag;
+

drivers/infiniband/hw/hns/hns_roce_device.h struct hns_roce_uar {

Quoted 10 lines
drivers/infiniband/hw/hns/hns_roce_device.h +10 -0
+	if (context->mmap_key_support) {
+		ret = rdma_user_mmap_entry_insert(ucontext, &entry->rdma_entry,
+						  length);
+	} else {
+		switch (mmap_flag) {
+		case HNS_ROCE_MMAP_TYPE_DB:
+			ret = rdma_user_mmap_entry_insert_range(ucontext,
+						&entry->rdma_entry, length,
+						HNS_ROCE_PGOFFSET_DB,
+						HNS_ROCE_PGOFFSET_DB);

Please add this to avoid the odd #defines:

Quoted 9 lines

static inline int
rdma_user_mmap_entry_insert_exact(struct ib_ucontext *ucontext,
struct rdma_user_mmap_entry *entry, size_t length, u32 pgoff)
{
return rdma_user_mmap_entry_insert_range(ucontext, entry, length, pgoff,
pgoff);
}

ditto.

Quoted 39 lines
Quoted 26 lines
drivers/infiniband/hw/hns/hns_roce_device.h +9 -16
-static int hns_roce_mmap(struct ib_ucontext *context,
-			 struct vm_area_struct *vma)
+static int hns_roce_mmap(struct ib_ucontext *uctx, struct vm_area_struct *vma)
 {
-	struct hns_roce_dev *hr_dev = to_hr_dev(context->device);
-
-	switch (vma->vm_pgoff) {
-	case 0:
-		return rdma_user_mmap_io(context, vma,
-					 to_hr_ucontext(context)->uar.pfn,
-					 PAGE_SIZE,
-					 pgprot_noncached(vma->vm_page_prot),
-					 NULL);
-
-	/* vm_pgoff: 1 -- TPTR */
-	case 1:
-		if (!hr_dev->tptr_dma_addr || !hr_dev->tptr_size)
-			return -EINVAL;
+	struct hns_roce_dev *hr_dev = to_hr_dev(uctx->device);
+	struct ib_device *ibdev = &hr_dev->ib_dev;
+	struct rdma_user_mmap_entry *rdma_entry;
+	struct hns_user_mmap_entry *entry;
+	phys_addr_t pfn;
+	pgprot_t prot;
+	int ret;
+

drivers/infiniband/hw/hns/hns_roce_device.h

Quoted 10 lines
drivers/infiniband/hw/hns/hns_roce_device.h +10 -0
+	rdma_entry = rdma_user_mmap_entry_get_pgoff(uctx, vma->vm_pgoff);
+	if (!rdma_entry) {
+		ibdev_err(ibdev, "Invalid entry vm_pgoff %lu.\n",
+			  vma->vm_pgoff);
+		return -EINVAL;
+	}
+
+	entry = to_hns_mmap(rdma_entry);
+	pfn = entry->address >> PAGE_SHIFT;
+	prot = vma->vm_page_prot;

Just write

Quoted 8 lines

if (entry->mmap_type != HNS_ROCE_MMAP_TYPE_TPTR)

    prot = pgprot_noncached(prot);
 ret = rdma_user_mmap_io(uctx, vma, pfn,
			rdma_entry->npages * PAGE_SIZE,
			pgprot_noncached(prot), rdma_entry);

No need for the big case statement

There will be new features added to this switch branch in a later patch.

Quoted 36 lines
Quoted 22 lines
include/uapi/rdma/hns-abi.h +16 -0
index 42b177655560..ce1e39f21d73 100644
+++ b/include/uapi/rdma/hns-abi.h
@@ -83,11 +83,30 @@ struct hns_roce_ib_create_qp_resp {
 	__aligned_u64 cap_flags;
 };
 
+enum hns_roce_alloc_uctx_comp_flag {
+	HNS_ROCE_ALLOC_UCTX_COMP_CONFIG = 1 << 0,
+};
+
+enum hns_roce_alloc_uctx_resp_config {
+	HNS_ROCE_UCTX_RESP_MMAP_KEY_EN = 1 << 0,
+};
+
+enum hns_roce_alloc_uctx_req_config {
+	HNS_ROCE_UCTX_REQ_MMAP_KEY_EN = 1 << 0,
+};
+
+struct hns_roce_ib_alloc_ucontext {
+	__u32 comp;
+	__u32 config;

include/uapi/rdma/hns-abi.h struct hns_roce_ib_create_qp_resp { · line 109

Quoted 10 lines
include/uapi/rdma/hns-abi.h +5 -1
+};
+
 struct hns_roce_ib_alloc_ucontext_resp {
 	__u32	qp_tab_size;
 	__u32	cqe_size;
 	__u32	srq_tab_size;
-	__u32	reserved;
+	__u8    config;
+	__u8    rsv[3];
+	__aligned_u64 db_mmap_key;

I'm confused, this doesn't change the uAPI, so why add this stuff? This should go in a later patch?

Quoted 3 lines

Jason
.

The related userspace series is named "libhns: Add a new mmap implementation".

Thanks,
Wenpeng
.

#4 lore raw
In reply to Jason Gunthorpe
Quoted 23 lines · Jason Gunthorpe
Quoted 22 lines · Wenpeng Liang

From: Chengchang Tang <[email protected]>

Add a new implementation for mmap by using the new mmap entry API.

The new implementation prepares for subsequent features and is compatible with the old implementation. And the old implementation using hard-coded offset will not be extended in the future.

 drivers/infiniband/hw/hns/hns_roce_device.h |  23 +++
 drivers/infiniband/hw/hns/hns_roce_main.c   | 208 +++++++++++++++++---
 include/uapi/rdma/hns-abi.h                 |  21 +-
 3 files changed, 225 insertions(+), 27 deletions(-)
drivers/infiniband/hw/hns/hns_roce_device.h +1 -0
index 9467c39e3d28..1d4cf3f083c2 100644
+++ b/drivers/infiniband/hw/hns/hns_roce_device.h
@@ -225,11 +225,25 @@ struct hns_roce_uar {
 	unsigned long	logic_idx;
 };
 
Quoted 9 lines

drivers/infiniband/hw/hns/hns_roce_device.h struct hns_roce_uar { · line 231

Quoted 4 lines
drivers/infiniband/hw/hns/hns_roce_device.h +4 -0
+struct hns_user_mmap_entry {
+	struct rdma_user_mmap_entry rdma_entry;
+	u64 address;
+	u8 mmap_flag;

Call this mmap_type and use the enum:

enum hns_roce_mmap_type mmap_type

I will fix it in v3, thanks!

Quoted 30 lines
Quoted 17 lines
drivers/infiniband/hw/hns/hns_roce_device.h +17 -0
+struct hns_user_mmap_entry *hns_roce_user_mmap_entry_insert(
+				struct ib_ucontext *ucontext, u64 address,
+				size_t length, u8 mmap_flag)
+{
+#define HNS_ROCE_PGOFFSET_TPTR 1
+#define HNS_ROCE_PGOFFSET_DB 0
+	struct hns_roce_ucontext *context = to_hr_ucontext(ucontext);
+	struct hns_user_mmap_entry *entry;
+	int ret;
+
+	entry = kzalloc(sizeof(*entry), GFP_KERNEL);
+	if (!entry)
+		return NULL;
+
+	entry->address = address;
+	entry->mmap_flag = mmap_flag;
+

drivers/infiniband/hw/hns/hns_roce_device.h struct hns_roce_uar {

Quoted 10 lines
drivers/infiniband/hw/hns/hns_roce_device.h +10 -0
+	if (context->mmap_key_support) {
+		ret = rdma_user_mmap_entry_insert(ucontext, &entry->rdma_entry,
+						  length);
+	} else {
+		switch (mmap_flag) {
+		case HNS_ROCE_MMAP_TYPE_DB:
+			ret = rdma_user_mmap_entry_insert_range(ucontext,
+						&entry->rdma_entry, length,
+						HNS_ROCE_PGOFFSET_DB,
+						HNS_ROCE_PGOFFSET_DB);

Please add this to avoid the odd #defines:

Quoted 9 lines

static inline int
rdma_user_mmap_entry_insert_exact(struct ib_ucontext *ucontext,
struct rdma_user_mmap_entry *entry, size_t length, u32 pgoff)
{
return rdma_user_mmap_entry_insert_range(ucontext, entry, length, pgoff,
pgoff);
}

ditto.

Quoted 39 lines
Quoted 26 lines
drivers/infiniband/hw/hns/hns_roce_device.h +9 -16
-static int hns_roce_mmap(struct ib_ucontext *context,
-			 struct vm_area_struct *vma)
+static int hns_roce_mmap(struct ib_ucontext *uctx, struct vm_area_struct *vma)
 {
-	struct hns_roce_dev *hr_dev = to_hr_dev(context->device);
-
-	switch (vma->vm_pgoff) {
-	case 0:
-		return rdma_user_mmap_io(context, vma,
-					 to_hr_ucontext(context)->uar.pfn,
-					 PAGE_SIZE,
-					 pgprot_noncached(vma->vm_page_prot),
-					 NULL);
-
-	/* vm_pgoff: 1 -- TPTR */
-	case 1:
-		if (!hr_dev->tptr_dma_addr || !hr_dev->tptr_size)
-			return -EINVAL;
+	struct hns_roce_dev *hr_dev = to_hr_dev(uctx->device);
+	struct ib_device *ibdev = &hr_dev->ib_dev;
+	struct rdma_user_mmap_entry *rdma_entry;
+	struct hns_user_mmap_entry *entry;
+	phys_addr_t pfn;
+	pgprot_t prot;
+	int ret;
+

drivers/infiniband/hw/hns/hns_roce_device.h

Quoted 10 lines
drivers/infiniband/hw/hns/hns_roce_device.h +10 -0
+	rdma_entry = rdma_user_mmap_entry_get_pgoff(uctx, vma->vm_pgoff);
+	if (!rdma_entry) {
+		ibdev_err(ibdev, "Invalid entry vm_pgoff %lu.\n",
+			  vma->vm_pgoff);
+		return -EINVAL;
+	}
+
+	entry = to_hns_mmap(rdma_entry);
+	pfn = entry->address >> PAGE_SHIFT;
+	prot = vma->vm_page_prot;

Just write

Quoted 8 lines

if (entry->mmap_type != HNS_ROCE_MMAP_TYPE_TPTR)

    prot = pgprot_noncached(prot);
 ret = rdma_user_mmap_io(uctx, vma, pfn,
			rdma_entry->npages * PAGE_SIZE,
			pgprot_noncached(prot), rdma_entry);

No need for the big case statement

In the future, we will have multiple new features that will add new branches to this switch. We hope to reduce the coupling between subsequent patches, so we hope to keep this switch.

Quoted 36 lines
Quoted 22 lines
include/uapi/rdma/hns-abi.h +16 -0
index 42b177655560..ce1e39f21d73 100644
+++ b/include/uapi/rdma/hns-abi.h
@@ -83,11 +83,30 @@ struct hns_roce_ib_create_qp_resp {
 	__aligned_u64 cap_flags;
 };
 
+enum hns_roce_alloc_uctx_comp_flag {
+	HNS_ROCE_ALLOC_UCTX_COMP_CONFIG = 1 << 0,
+};
+
+enum hns_roce_alloc_uctx_resp_config {
+	HNS_ROCE_UCTX_RESP_MMAP_KEY_EN = 1 << 0,
+};
+
+enum hns_roce_alloc_uctx_req_config {
+	HNS_ROCE_UCTX_REQ_MMAP_KEY_EN = 1 << 0,
+};
+
+struct hns_roce_ib_alloc_ucontext {
+	__u32 comp;
+	__u32 config;

include/uapi/rdma/hns-abi.h struct hns_roce_ib_create_qp_resp { · line 109

Quoted 10 lines
include/uapi/rdma/hns-abi.h +5 -1
+};
+
 struct hns_roce_ib_alloc_ucontext_resp {
 	__u32	qp_tab_size;
 	__u32	cqe_size;
 	__u32	srq_tab_size;
-	__u32	reserved;
+	__u8    config;
+	__u8    rsv[3];
+	__aligned_u64 db_mmap_key;

I'm confused, this doesn't change the uAPI, so why add this stuff? This should go in a later patch?

Quoted 3 lines

Jason
.

The related userspace has also been modified, the link is:
https://www.spinics.net/lists/linux-rdma/msg106056.html

I don’t know if I understood your question correctly. These fields are used for compatibility, so they are necessary. The user space and kernel space of different versions are handshake through 'config'.

Thanks,
Wenpeng

maintainer
#5 lore raw
Quoted 8 lines · Wenpeng Liang
Quoted 8 lines

Just write

if (entry->mmap_type != HNS_ROCE_MMAP_TYPE_TPTR)

    prot = pgprot_noncached(prot);
 ret = rdma_user_mmap_io(uctx, vma, pfn,
			rdma_entry->npages * PAGE_SIZE,
			pgprot_noncached(prot), rdma_entry);
Quoted 6 lines
Quoted 2 lines

No need for the big case statement

In the future, we will have multiple new features that will add new branches to this switch. We hope to reduce the coupling between subsequent patches, so we hope to keep this switch.

Why? The only choice that should be made at mmap time is if this is noncached or not - the address and everything else should be setup during the creation of the entry.

Quoted 13 lines
Quoted 12 lines

include/uapi/rdma/hns-abi.h struct hns_roce_ib_create_qp_resp { · line 109

Quoted 8 lines

struct hns_roce_ib_alloc_ucontext_resp {

include/uapi/rdma/hns-abi.h +3 -1
 	__u32	qp_tab_size;
 	__u32	cqe_size;
 	__u32	srq_tab_size;
-	__u32	reserved;
+	__u8    config;
+	__u8    rsv[3];
+	__aligned_u64 db_mmap_key;

I'm confused, this doesn't change the uAPI, so why add this stuff? This should go in a later patch?

Quoted 6 lines

The related userspace has also been modified, the link is:
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.spinics.net%2Flists%2Flinux-rdma%2Fmsg106056.html&amp;data=04%7C01%7Cjgg%40nvidia.com%7Cffac1b9f0afb458a4da308d99540ce99%7C43083d15727340c1b7db39efd9ccc17a%7C0%7C0%7C637704927843133708%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=BAszHlXQWWh%2F8hrb%2F8qEStm3VBrhvbMshfPN3pc6wKI%3D&amp;reserved=0

I don’t know if I understood your question correctly. These fields are used for compatibility, so they are necessary. The user space and kernel space of different versions are handshake through 'config'.

That is for later patches, this patch doesn't seem to change the uAPI at all. The compat mmaps remain at the same offsets they were always at, there is nothing to negotiate at this point.

Move it to a later series?

Jason

#6 lore raw
Quoted 12 lines · Jason Gunthorpe
Quoted 11 lines · Wenpeng Liang
Quoted 10 lines

Just write

if (entry->mmap_type != HNS_ROCE_MMAP_TYPE_TPTR)

    prot = pgprot_noncached(prot);
 ret = rdma_user_mmap_io(uctx, vma, pfn,
			rdma_entry->npages * PAGE_SIZE,
			pgprot_noncached(prot), rdma_entry);

No need for the big case statement

Quoted 8 lines
Quoted 3 lines

In the future, we will have multiple new features that will add new branches to this switch. We hope to reduce the coupling between subsequent patches, so we hope to keep this switch.

Why? The only choice that should be made at mmap time is if this is noncached or not - the address and everything else should be setup during the creation of the entry.

Thanks for your comment, I will merge 'HNS_ROCE_MMAP_TYPE_TPTR' and 'HNS_ROCE_MMAP_TYPE_DB' into one statement in v3.

Quoted 20 lines
Quoted 13 lines
Quoted 12 lines

include/uapi/rdma/hns-abi.h struct hns_roce_ib_create_qp_resp { · line 109

Quoted 8 lines

struct hns_roce_ib_alloc_ucontext_resp {

include/uapi/rdma/hns-abi.h +3 -1
 	__u32	qp_tab_size;
 	__u32	cqe_size;
 	__u32	srq_tab_size;
-	__u32	reserved;
+	__u8    config;
+	__u8    rsv[3];
+	__aligned_u64 db_mmap_key;

I'm confused, this doesn't change the uAPI, so why add this stuff? This should go in a later patch?

Quoted 6 lines

The related userspace has also been modified, the link is:
https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwww.spinics.net%2Flists%2Flinux-rdma%2Fmsg106056.html&amp;data=04%7C01%7Cjgg%40nvidia.com%7Cffac1b9f0afb458a4da308d99540ce99%7C43083d15727340c1b7db39efd9ccc17a%7C0%7C0%7C637704927843133708%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=BAszHlXQWWh%2F8hrb%2F8qEStm3VBrhvbMshfPN3pc6wKI%3D&amp;reserved=0

I don’t know if I understood your question correctly. These fields are used for compatibility, so they are necessary. The user space and kernel space of different versions are handshake through 'config'.

Quoted 9 lines

That is for later patches, this patch doesn't seem to change the uAPI at all. The compat mmaps remain at the same offsets they were always at, there is nothing to negotiate at this point.

Move it to a later series?

Jason
.

Thanks, I will remove it in v3.

Wenpeng