Skip to contentSkip to conversation

libhns: Add support for Dynamic Context Attachment

RFC PATCH In review Weihang Li 3 participants 16 messages Feb 7, 2021 – Feb 24, 2021 InfiniBand

lore
Share & tools
lore

Weihang Li

author
#0 lore raw

cover libhns: Add support for Dynamic Context Attachment

The HIP09 introduces the DCA(Dynamic Context Attachment) feature which supports many RC QPs to share the WQE buffer in a memory pool. If a QP enables DCA feature, the WQE's buffer will not be allocated when creating but when the users start to post WRs. This will reduce the memory consumption when there are too many QPs are inactive.

Please note that we didn't find the right way to get user's configuration, so in #4 we still use environment variable to achieve this. We will be appreciated if anyone can provide some sugggestions.

This series is associated with the kernel series "RDMA/hns: Add support for Dynamic Context Attachment", you can review it at:
https://patchwork.kernel.org/project/linux-rdma/cover/[email protected]/

Weihang Li (1):
Update kernel headers

Xi Wang (4):
libhns: Introduce DCA for RC QP
libhns: Add support for shrinking DCA memory pool
libhns: Add support for attaching QP's WQE buffer
libhns: Add support for configuring DCA

 kernel-headers/rdma/hns-abi.h    |  64 +++++++
 libibverbs/cmd_qp.c              |   3 +-
 libibverbs/verbs.h               |   1 +
 providers/hns/hns_roce_u.c       |  89 +++++++++
 providers/hns/hns_roce_u.h       |  42 +++++
 providers/hns/hns_roce_u_buf.c   | 384 +++++++++++++++++++++++++++++++++++++++
 providers/hns/hns_roce_u_hw_v2.c | 138 +++++++++++++-
 providers/hns/hns_roce_u_hw_v2.h |   7 +
 providers/hns/hns_roce_u_verbs.c |  44 ++++-
 9 files changed, 754 insertions(+), 18 deletions(-)
Signature · 1 line
2.8.1
author
#1 lore raw

RFC PATCH 1/5 Update kernel headers

To commit ?? ("RDMA/hns: Add method to query WQE buffer's address").

---
 kernel-headers/rdma/hns-abi.h | 64 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 64 insertions(+)
kernel-headers/rdma/hns-abi.h +64 -0
index 90b739d..b832f26 100644
@@ -77,19 +77,83 @@ enum hns_roce_qp_cap_flags {
 	HNS_ROCE_QP_CAP_RQ_RECORD_DB = 1 << 0,
 	HNS_ROCE_QP_CAP_SQ_RECORD_DB = 1 << 1,
 	HNS_ROCE_QP_CAP_OWNER_DB = 1 << 2,
+	HNS_ROCE_QP_CAP_DCA = 1 << 4,
 };
 
 struct hns_roce_ib_create_qp_resp {
 	__aligned_u64 cap_flags;
 };
 
+enum {
+	HNS_ROCE_CAP_FLAG_DCA_MODE = 1 << 15,
+};
+
 struct hns_roce_ib_alloc_ucontext_resp {
 	__u32	qp_tab_size;
 	__u32	cqe_size;
+	__aligned_u64 cap_flags;
 };
 
 struct hns_roce_ib_alloc_pd_resp {
 	__u32 pdn;
 };
 
+#define UVERBS_ID_NS_MASK 0xF000
+#define UVERBS_ID_NS_SHIFT 12
+
+enum hns_ib_objects {
+	HNS_IB_OBJECT_DCA_MEM = (1U << UVERBS_ID_NS_SHIFT),
+};
+
+enum hns_ib_dca_mem_methods {
+	HNS_IB_METHOD_DCA_MEM_REG = (1U << UVERBS_ID_NS_SHIFT),
+	HNS_IB_METHOD_DCA_MEM_DEREG,
+	HNS_IB_METHOD_DCA_MEM_SHRINK,
+	HNS_IB_METHOD_DCA_MEM_ATTACH,
+	HNS_IB_METHOD_DCA_MEM_DETACH,
+	HNS_IB_METHOD_DCA_MEM_QUERY,
+};
+
+enum hns_ib_dca_mem_reg_attrs {
+	HNS_IB_ATTR_DCA_MEM_REG_HANDLE = (1U << UVERBS_ID_NS_SHIFT),
+	HNS_IB_ATTR_DCA_MEM_REG_LEN,
+	HNS_IB_ATTR_DCA_MEM_REG_ADDR,
+	HNS_IB_ATTR_DCA_MEM_REG_KEY,
+};
+
+enum hns_ib_dca_mem_dereg_attrs {
+	HNS_IB_ATTR_DCA_MEM_DEREG_HANDLE = (1U << UVERBS_ID_NS_SHIFT),
+};
+
+enum hns_ib_dca_mem_shrink_attrs {
+	HNS_IB_ATTR_DCA_MEM_SHRINK_HANDLE = (1U << UVERBS_ID_NS_SHIFT),
+	HNS_IB_ATTR_DCA_MEM_SHRINK_RESERVED_SIZE,
+	HNS_IB_ATTR_DCA_MEM_SHRINK_OUT_FREE_KEY,
+	HNS_IB_ATTR_DCA_MEM_SHRINK_OUT_FREE_MEMS,
+};
+
+#define HNS_IB_ATTACH_FLAGS_NEW_BUFFER 1U
+
+enum hns_ib_dca_mem_attach_attrs {
+	HNS_IB_ATTR_DCA_MEM_ATTACH_HANDLE = (1U << UVERBS_ID_NS_SHIFT),
+	HNS_IB_ATTR_DCA_MEM_ATTACH_SQ_OFFSET,
+	HNS_IB_ATTR_DCA_MEM_ATTACH_SGE_OFFSET,
+	HNS_IB_ATTR_DCA_MEM_ATTACH_RQ_OFFSET,
+	HNS_IB_ATTR_DCA_MEM_ATTACH_OUT_ALLOC_FLAGS,
+	HNS_IB_ATTR_DCA_MEM_ATTACH_OUT_ALLOC_PAGES,
+};
+
+enum hns_ib_dca_mem_detach_attrs {
+	HNS_IB_ATTR_DCA_MEM_DETACH_HANDLE = (1U << UVERBS_ID_NS_SHIFT),
+	HNS_IB_ATTR_DCA_MEM_DETACH_SQ_INDEX,
+};
+
+enum hns_ib_dca_mem_query_attrs {
+	HNS_IB_ATTR_DCA_MEM_QUERY_HANDLE = (1U << UVERBS_ID_NS_SHIFT),
+	HNS_IB_ATTR_DCA_MEM_QUERY_PAGE_INDEX,
+	HNS_IB_ATTR_DCA_MEM_QUERY_OUT_KEY,
+	HNS_IB_ATTR_DCA_MEM_QUERY_OUT_OFFSET,
+	HNS_IB_ATTR_DCA_MEM_QUERY_OUT_PAGE_COUNT,
+};
+
 #endif /* HNS_ABI_USER_H */
Signature · 1 line
2.8.1
author
#2 lore raw

RFC PATCH 2/5 libhns: Introduce DCA for RC QP

In reply to Weihang Li

From: Xi Wang <[email protected]>

The HIP09 introduces the DCA(Dynamic context attachment) feature which supports many RC QPs to share the WQE buffer in a memory pool, this will reduce the memory consumption when there are too many QPs inactive.

This patch wraps two functions for adding buffers to memory pool and removing buffers from memory pool by calling ib cmd implemented in hns kernel driver.

If a QP enables DCA feature, the WQE's buffer will be attached to the memroy pool when the users start to post WRs and be detached when all CQEs has been polled.

---
 providers/hns/hns_roce_u.c     |  41 +++++++++++++
 providers/hns/hns_roce_u.h     |  18 ++++++
 providers/hns/hns_roce_u_buf.c | 134 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 193 insertions(+)
providers/hns/hns_roce_u.c +41 -0
index e63ef32..28b1130 100644
@@ -89,6 +89,40 @@ static const struct verbs_context_ops hns_common_ops = {
 	.destroy_ah = hns_roce_u_destroy_ah,
 };
 
+static int init_dca_context(struct hns_roce_context *ctx, int page_size)
+{
+	struct hns_roce_dca_ctx *dca_ctx = &ctx->dca_ctx;
+	int ret;
+
+	if (!(ctx->cap_flags & HNS_ROCE_CAP_FLAG_DCA_MODE))
+		return 0;
+
+	list_head_init(&dca_ctx->mem_list);
+	ret = pthread_spin_init(&dca_ctx->lock, PTHREAD_PROCESS_PRIVATE);
+	if (ret)
+		return ret;
+
+	dca_ctx->unit_size = page_size * HNS_DCA_DEFAULT_UNIT_PAGES;
+	dca_ctx->max_size = HNS_DCA_MAX_MEM_SIZE;
+	dca_ctx->mem_cnt = 0;
+
+	return 0;
+}
+
+static void uninit_dca_context(struct hns_roce_context *ctx)
+{
+	struct hns_roce_dca_ctx *dca_ctx = &ctx->dca_ctx;
+
+	if (!(ctx->cap_flags & HNS_ROCE_CAP_FLAG_DCA_MODE))
+		return;
+
+	pthread_spin_lock(&dca_ctx->lock);
+	hns_roce_cleanup_dca_mem(ctx);
+	pthread_spin_unlock(&dca_ctx->lock);
+
+	pthread_spin_destroy(&dca_ctx->lock);
+}
+
 static struct verbs_context *hns_roce_alloc_context(struct ibv_device *ibdev,
 						    int cmd_fd,
 						    void *private_data)
@@ -110,6 +144,8 @@ static struct verbs_context *hns_roce_alloc_context(struct ibv_device *ibdev,
 				&resp.ibv_resp, sizeof(resp)))
 		goto err_free;
 
+	context->cap_flags = resp.cap_flags;
+
 	context->num_qps = resp.qp_tab_size;
 	context->qp_table_shift = ffs(context->num_qps) - 1 -
 				  HNS_ROCE_QP_TABLE_BITS;
@@ -162,6 +198,9 @@ static struct verbs_context *hns_roce_alloc_context(struct ibv_device *ibdev,
 	verbs_set_ops(&context->ibv_ctx, &hns_common_ops);
 	verbs_set_ops(&context->ibv_ctx, &hr_dev->u_hw->hw_ops);
 
+	if (init_dca_context(context, hr_dev->page_size))
+		goto db_free;
+
 	return &context->ibv_ctx;
 
 db_free:
@@ -183,6 +222,8 @@ static void hns_roce_free_context(struct ibv_context *ibctx)
 	if (hr_dev->hw_version == HNS_ROCE_HW_VER1)
 		munmap(context->cq_tptr_base, HNS_ROCE_CQ_DB_BUF_SIZE);
 
+	uninit_dca_context(context);
+
 	verbs_uninit_context(&context->ibv_ctx);
 	free(context);
 }
providers/hns/hns_roce_u.h +18 -0
index 67b4433..619b060 100644
@@ -142,8 +142,21 @@ struct hns_roce_db_page {
 	bitmap			*bitmap;
 };
 
+#define HNS_DCA_MAX_MEM_SIZE ~0UL
+#define HNS_DCA_DEFAULT_UNIT_PAGES 16
+
+struct hns_roce_dca_ctx {
+	struct list_head mem_list;
+	pthread_spinlock_t lock;
+	uint64_t max_size;
+	uint64_t curr_size;
+	int mem_cnt;
+	unsigned int unit_size;
+};
+
 struct hns_roce_context {
 	struct verbs_context		ibv_ctx;
+	uint32_t			cap_flags;
 	void				*uar;
 	pthread_spinlock_t		uar_lock;
 
@@ -167,6 +180,8 @@ struct hns_roce_context {
 	unsigned int			max_sge;
 	int				max_cqe;
 	unsigned int			cqe_size;
+
+	struct hns_roce_dca_ctx		dca_ctx;
 };
 
 struct hns_roce_pd {
@@ -395,6 +410,9 @@ void hns_roce_free_buf(struct hns_roce_buf *buf);
 
 void hns_roce_free_qp_buf(struct hns_roce_qp *qp, struct hns_roce_context *ctx);
 
+void hns_roce_cleanup_dca_mem(struct hns_roce_context *ctx);
+int hns_roce_add_dca_mem(struct hns_roce_context *ctx, uint32_t size);
+
 void hns_roce_init_qp_indices(struct hns_roce_qp *qp);
 
 extern const struct hns_roce_u_hw hns_roce_u_hw_v1;
providers/hns/hns_roce_u_buf.c +134 -0

137 lines not on this page · raw message

Signature · 1 line
2.8.1
maintainer
#3 lore raw

Re: [PATCH RFC rdma-core 2/5] libhns: Introduce DCA for RC QP

providers/hns/hns_roce_u_buf.c void hns_roce_free_buf(struct hns_roce_buf *buf) · line 118

Quoted 10 lines · Weihang Li
providers/hns/hns_roce_u_buf.c +10 -0
+static int register_dca_mem(struct hns_roce_context *ctx, uint64_t key,
+			    void *addr, uint32_t size, uint32_t *handle)
+{
+	struct ib_uverbs_attr *attr;
+	int ret;
+
+	DECLARE_COMMAND_BUFFER(cmd, HNS_IB_OBJECT_DCA_MEM,
+			       HNS_IB_METHOD_DCA_MEM_REG, 4);
+	fill_attr_in_uint32(cmd, HNS_IB_ATTR_DCA_MEM_REG_LEN, size);
+	fill_attr_in_uint64(cmd, HNS_IB_ATTR_DCA_MEM_REG_ADDR, (intptr_t)addr);

This should use ioctl_ptr_to_u64(), the place this was copied from should also be fixed

providers/hns/hns_roce_u_buf.c void hns_roce_free_buf(struct hns_roce_buf *buf) · line 124

Quoted 6 lines
providers/hns/hns_roce_u_buf.c +6 -0
+	fill_attr_in_uint64(cmd, HNS_IB_ATTR_DCA_MEM_REG_KEY, key);
+	attr = fill_attr_out_obj(cmd, HNS_IB_ATTR_DCA_MEM_REG_HANDLE);
+
+	ret = execute_ioctl(&ctx->ibv_ctx.context, cmd);
+	if (!ret)
+		*handle = read_attr_obj(HNS_IB_ATTR_DCA_MEM_REG_HANDLE, attr);

Success oriented flow everywhere please

Jason

author
#4 lore raw

Re: [PATCH RFC rdma-core 2/5] libhns: Introduce DCA for RC QP

Quoted 16 lines · Jason Gunthorpe

providers/hns/hns_roce_u_buf.c void hns_roce_free_buf(struct hns_roce_buf *buf) · line 118

Quoted 10 lines · Weihang Li
providers/hns/hns_roce_u_buf.c +10 -0
+static int register_dca_mem(struct hns_roce_context *ctx, uint64_t key,
+			    void *addr, uint32_t size, uint32_t *handle)
+{
+	struct ib_uverbs_attr *attr;
+	int ret;
+
+	DECLARE_COMMAND_BUFFER(cmd, HNS_IB_OBJECT_DCA_MEM,
+			       HNS_IB_METHOD_DCA_MEM_REG, 4);
+	fill_attr_in_uint32(cmd, HNS_IB_ATTR_DCA_MEM_REG_LEN, size);
+	fill_attr_in_uint64(cmd, HNS_IB_ATTR_DCA_MEM_REG_ADDR, (intptr_t)addr);

This should use ioctl_ptr_to_u64(), the place this was copied from should also be fixed

OK, I will fix it. The kernel part has been defined as u64, so it doesn't need to be modified.

Quoted 11 lines

providers/hns/hns_roce_u_buf.c void hns_roce_free_buf(struct hns_roce_buf *buf) · line 124

Quoted 7 lines
providers/hns/hns_roce_u_buf.c +6 -0
+	fill_attr_in_uint64(cmd, HNS_IB_ATTR_DCA_MEM_REG_KEY, key);
+	attr = fill_attr_out_obj(cmd, HNS_IB_ATTR_DCA_MEM_REG_HANDLE);
+
+	ret = execute_ioctl(&ctx->ibv_ctx.context, cmd);
+	if (!ret)
+		*handle = read_attr_obj(HNS_IB_ATTR_DCA_MEM_REG_HANDLE, attr);

Success oriented flow everywhere please

Quoted 1 line

Jason

OK, thank you.

Weihang

author
#5 lore raw

RFC PATCH 3/5 libhns: Add support for shrinking DCA memory pool

In reply to Weihang Li

From: Xi Wang <[email protected]>

The QP's WQE buffer may be detached after QP is modified or CQE is polled, and the state of DCA mem object may be changed as clean for no QP is using it. So shrink the clean DCA mem from the memory pool and destroy the DCA mem's buffer to reduce the memory consumption.

---
 providers/hns/hns_roce_u.h       |  2 +
 providers/hns/hns_roce_u_buf.c   | 96 ++++++++++++++++++++++++++++++++++++++++
 providers/hns/hns_roce_u_hw_v2.c |  7 +++
 3 files changed, 105 insertions(+)
providers/hns/hns_roce_u.h +2 -0
index 619b060..7dc4a1e 100644
@@ -149,6 +149,7 @@ struct hns_roce_dca_ctx {
 	struct list_head mem_list;
 	pthread_spinlock_t lock;
 	uint64_t max_size;
+	uint64_t min_size;
 	uint64_t curr_size;
 	int mem_cnt;
 	unsigned int unit_size;
@@ -410,6 +411,7 @@ void hns_roce_free_buf(struct hns_roce_buf *buf);
 
 void hns_roce_free_qp_buf(struct hns_roce_qp *qp, struct hns_roce_context *ctx);
 
+void hns_roce_shrink_dca_mem(struct hns_roce_context *ctx);
 void hns_roce_cleanup_dca_mem(struct hns_roce_context *ctx);
 int hns_roce_add_dca_mem(struct hns_roce_context *ctx, uint32_t size);
 
providers/hns/hns_roce_u_buf.c +96 -0

111 lines not on this page · raw message

providers/hns/hns_roce_u_hw_v2.c +7 -0
index c8d273f..93f3546 100644
@@ -651,6 +651,10 @@ static int hns_roce_u_v2_poll_cq(struct ibv_cq *ibvcq, int ne,
 
 	pthread_spin_unlock(&cq->lock);
 
+	/* Try to shrink the DCA mem */
+	if (ctx->dca_ctx.mem_cnt > 0)
+		hns_roce_shrink_dca_mem(ctx);
+
 	return err == V2_CQ_POLL_ERR ? err : npolled;
 }
 
@@ -1478,6 +1482,9 @@ static int hns_roce_u_v2_destroy_qp(struct ibv_qp *ibqp)
 
 	free(qp);
 
+	if (ctx->dca_ctx.mem_cnt > 0)
+		hns_roce_shrink_dca_mem(ctx);
+
 	return ret;
 }
 
Signature · 1 line
2.8.1
author
#6 lore raw

RFC PATCH 4/5 libhns: Add support for attaching QP's WQE buffer

In reply to Weihang Li

From: Xi Wang <[email protected]>

If a uQP works in DCA mode, the WQE's buffer will be split as many blocks and be stored into a list. The blocks are allocated from the DCA's memory pool before posting WRs and are dropped when the QP's CI is equal to PI after polling CQ.

---
 providers/hns/hns_roce_u.h       |  24 +++++-
 providers/hns/hns_roce_u_buf.c   | 156 ++++++++++++++++++++++++++++++++++++++-
 providers/hns/hns_roce_u_hw_v2.c | 131 +++++++++++++++++++++++++++++---
 providers/hns/hns_roce_u_hw_v2.h |   7 ++
 providers/hns/hns_roce_u_verbs.c |  32 ++++++--
 5 files changed, 332 insertions(+), 18 deletions(-)
providers/hns/hns_roce_u.h +23 -1
index 7dc4a1e..ae72709 100644
@@ -119,6 +119,12 @@ struct hns_roce_device {
 	int				hw_version;
 };
 
+struct hns_roce_buf_list {
+	void **bufs;
+	unsigned int max_cnt;
+	unsigned int shift;
+};
+
 struct hns_roce_buf {
 	void				*buf;
 	unsigned int			length;
@@ -268,9 +274,20 @@ struct hns_roce_rinl_buf {
 	unsigned int			wqe_cnt;
 };
 
+struct hns_roce_dca_attach_attr {
+	uint32_t sq_offset;
+	uint32_t sge_offset;
+	uint32_t rq_offset;
+};
+
+struct hns_roce_dca_detach_attr {
+	uint32_t sq_index;
+};
+
 struct hns_roce_qp {
 	struct verbs_qp			verbs_qp;
 	struct hns_roce_buf		buf;
+	struct hns_roce_buf_list	page_list;
 	int				max_inline_data;
 	int				buf_size;
 	unsigned int			sq_signal_bits;
@@ -316,6 +333,7 @@ struct hns_roce_u_hw {
  * minimum page size.
  */
 #define hr_hw_page_align(x) align(x, HNS_HW_PAGE_SIZE)
+#define hr_hw_page_count(x) (hr_hw_page_align(x) / HNS_HW_PAGE_SIZE)
 
 static inline unsigned int to_hr_hem_entries_size(int count, int buf_shift)
 {
@@ -411,9 +429,13 @@ void hns_roce_free_buf(struct hns_roce_buf *buf);
 
 void hns_roce_free_qp_buf(struct hns_roce_qp *qp, struct hns_roce_context *ctx);
 
+int hns_roce_attach_dca_mem(struct hns_roce_context *ctx, uint32_t handle,
+			    struct hns_roce_dca_attach_attr *attr,
+			    uint32_t size, struct hns_roce_buf_list *buf_list);
+void hns_roce_detach_dca_mem(struct hns_roce_context *ctx, uint32_t handle,
+			     struct hns_roce_dca_detach_attr *attr);
 void hns_roce_shrink_dca_mem(struct hns_roce_context *ctx);
 void hns_roce_cleanup_dca_mem(struct hns_roce_context *ctx);
-int hns_roce_add_dca_mem(struct hns_roce_context *ctx, uint32_t size);
 
 void hns_roce_init_qp_indices(struct hns_roce_qp *qp);
 
providers/hns/hns_roce_u_buf.c +155 -1

171 lines not on this page · raw message

providers/hns/hns_roce_u_hw_v2.c +121 -10

202 lines not on this page · raw message

providers/hns/hns_roce_u_hw_v2.h +7 -0
index f5e6402..00716c6 100644
@@ -250,6 +250,13 @@ struct hns_roce_rc_sq_wqe {
 
 #define RC_SQ_WQE_BYTE_4_RDMA_WRITE_S 22
 
+#define RC_SQ_WQE_BYTE_4_SQPN_L_W 2
+#define RC_SQ_WQE_BYTE_4_SQPN_L_S 5
+#define RC_SQ_WQE_BYTE_4_SQPN_L_M GENMASK(5, 2)
+
+#define RC_SQ_WQE_BYTE_4_SQPN_H_S 13
+#define RC_SQ_WQE_BYTE_4_SQPN_H_M GENMASK(18, 13)
+
 #define RC_SQ_WQE_BYTE_16_XRC_SRQN_S 0
 #define RC_SQ_WQE_BYTE_16_XRC_SRQN_M \
 	(((1UL << 24) - 1) << RC_SQ_WQE_BYTE_16_XRC_SRQN_S)
providers/hns/hns_roce_u_verbs.c +26 -6
index f3d6fc1..6ec8b12 100644
@@ -688,6 +688,14 @@ static int calc_qp_buff_size(struct hns_roce_device *hr_dev,
 	return 0;
 }
 
+static inline bool check_qp_support_dca(bool pool_en, enum ibv_qp_type qp_type)
+{
+	if (pool_en && (qp_type == IBV_QPT_RC || qp_type == IBV_QPT_XRC_SEND))
+		return true;
+
+	return false;
+}
+
 static void qp_free_wqe(struct hns_roce_qp *qp)
 {
 	qp_free_recv_inl_buf(qp);
@@ -699,8 +707,8 @@ static void qp_free_wqe(struct hns_roce_qp *qp)
 	hns_roce_free_buf(&qp->buf);
 }
 
-static int qp_alloc_wqe(struct ibv_qp_cap *cap, struct hns_roce_qp *qp,
-			struct hns_roce_context *ctx)
+static int qp_alloc_wqe(struct ibv_qp_init_attr_ex *attr,
+			struct hns_roce_qp *qp, struct hns_roce_context *ctx)
 {
 	struct hns_roce_device *hr_dev = to_hr_dev(ctx->ibv_ctx.context.device);
 
@@ -718,12 +726,24 @@ static int qp_alloc_wqe(struct ibv_qp_cap *cap, struct hns_roce_qp *qp,
 	}
 
 	if (qp->rq_rinl_buf.wqe_cnt) {
-		if (qp_alloc_recv_inl_buf(cap, qp))
+		if (qp_alloc_recv_inl_buf(&attr->cap, qp))
 			goto err_alloc;
 	}
 
-	if (hns_roce_alloc_buf(&qp->buf, qp->buf_size, HNS_HW_PAGE_SIZE))
-		goto err_alloc;
+	if (check_qp_support_dca(ctx->dca_ctx.max_size != 0, attr->qp_type)) {
+		/* when DCA is enabled, use a buffer list to store page addr */
+		qp->buf.buf = NULL;
+		qp->page_list.max_cnt = hr_hw_page_count(qp->buf_size);
+		qp->page_list.shift = HNS_HW_PAGE_SHIFT;
+		qp->page_list.bufs = calloc(qp->page_list.max_cnt,
+					    sizeof(void *));
+		if (!qp->page_list.bufs)
+			goto err_alloc;
+	} else {
+		if (hns_roce_alloc_buf(&qp->buf, qp->buf_size,
+				       HNS_HW_PAGE_SIZE))
+			goto err_alloc;
+	}
 
 	return 0;
 
@@ -928,7 +948,7 @@ static int hns_roce_alloc_qp_buf(struct ibv_qp_init_attr_ex *attr,
 	    pthread_spin_init(&qp->rq.lock, PTHREAD_PROCESS_PRIVATE))
 		return -ENOMEM;
 
-	ret = qp_alloc_wqe(&attr->cap, qp, ctx);
+	ret = qp_alloc_wqe(attr, qp, ctx);
 	if (ret)
 		return ret;
 
Signature · 1 line
2.8.1
author
#7 lore raw

RFC PATCH 5/5 libhns: Add support for configuring DCA

In reply to Weihang Li

From: Xi Wang <[email protected]>

Add a group of environment variables for configuring DCA memory pool which is as follows:
HNS_DCA_MAX_SIZE - the max size when expand the memory pool. HNS_DCA_MIN_SIZE - the reverved size when shrink the memory pool. HNS_DCA_UNIT_SIZE - the increase unit size when expand the memory pool.

Also append 'IBV_QP_CREATE_DYNAMIC_CONTEXT_ATTACH' for creating a DCA QP.

---
 libibverbs/cmd_qp.c              |  3 ++-
 libibverbs/verbs.h               |  1 +
 providers/hns/hns_roce_u.c       | 52 ++++++++++++++++++++++++++++++++++++++--
 providers/hns/hns_roce_u_verbs.c | 18 ++++++++++----
 4 files changed, 67 insertions(+), 7 deletions(-)
libibverbs/cmd_qp.c +2 -1
index 056f397..f9899ad 100644
@@ -38,7 +38,8 @@ enum {
 					IBV_QP_CREATE_SCATTER_FCS |
 					IBV_QP_CREATE_CVLAN_STRIPPING |
 					IBV_QP_CREATE_SOURCE_QPN |
-					IBV_QP_CREATE_PCI_WRITE_END_PADDING
+					IBV_QP_CREATE_PCI_WRITE_END_PADDING |
+					IBV_QP_CREATE_DYNAMIC_CONTEXT_ATTACH
 };
 
 
libibverbs/verbs.h +1 -0
index 656b0f9..db37dce 100644
@@ -918,6 +918,7 @@ enum ibv_qp_create_flags {
 	IBV_QP_CREATE_CVLAN_STRIPPING		= 1 << 9,
 	IBV_QP_CREATE_SOURCE_QPN		= 1 << 10,
 	IBV_QP_CREATE_PCI_WRITE_END_PADDING	= 1 << 11,
+	IBV_QP_CREATE_DYNAMIC_CONTEXT_ATTACH	= 1 << 13,
 };
 
 enum ibv_qp_create_send_ops_flags {
providers/hns/hns_roce_u.c +50 -2

64 lines not on this page · raw message

providers/hns/hns_roce_u_verbs.c +14 -4
index 6ec8b12..0697328 100644
@@ -542,7 +542,7 @@ int hns_roce_u_destroy_srq(struct ibv_srq *srq)
 }
 
 enum {
-	CREATE_QP_SUP_CREATE_FLAGS = 0,
+	CREATE_QP_SUP_CREATE_FLAGS = IBV_QP_CREATE_DYNAMIC_CONTEXT_ATTACH,
 };
 
 enum {
@@ -688,10 +688,11 @@ static int calc_qp_buff_size(struct hns_roce_device *hr_dev,
 	return 0;
 }
 
-static inline bool check_qp_support_dca(bool pool_en, enum ibv_qp_type qp_type)
+static inline bool check_qp_support_dca(bool pool_en, enum ibv_qp_type qp_type,
+					uint32_t create_flags)
 {
 	if (pool_en && (qp_type == IBV_QPT_RC || qp_type == IBV_QPT_XRC_SEND))
-		return true;
+		return !!(create_flags & IBV_QP_CREATE_DYNAMIC_CONTEXT_ATTACH);
 
 	return false;
 }
@@ -730,7 +731,8 @@ static int qp_alloc_wqe(struct ibv_qp_init_attr_ex *attr,
 			goto err_alloc;
 	}
 
-	if (check_qp_support_dca(ctx->dca_ctx.max_size != 0, attr->qp_type)) {
+	if (check_qp_support_dca(ctx->dca_ctx.max_size != 0,
+				 attr->qp_type, attr->create_flags)) {
 		/* when DCA is enabled, use a buffer list to store page addr */
 		qp->buf.buf = NULL;
 		qp->page_list.max_cnt = hr_hw_page_count(qp->buf_size);
@@ -901,6 +903,14 @@ static int qp_exec_create_cmd(struct ibv_qp_init_attr_ex *attr,
 	struct hns_roce_create_qp_ex cmd_ex = {};
 	int ret;
 
+	/*
+	 * When handling the command in kernel space, the user QP enable
+	 * the DCA mode by checking whether the cmd.buf_addr is NULL but
+	 * not the attr->create_flags has the DCA enable bit, so clear
+	 * this bit before command is ready to run.
+	 */
+	attr->create_flags &= ~IBV_QP_CREATE_DYNAMIC_CONTEXT_ATTACH;
+
 	cmd_ex.sdb_addr = (uintptr_t)qp->sdb;
 	cmd_ex.db_addr = (uintptr_t)qp->rdb;
 	cmd_ex.buf_addr = (uintptr_t)qp->buf.buf;
Signature · 1 line
2.8.1
maintainer
#8 lore raw
In reply to Weihang Li
Quoted 9 lines · Weihang Li

The HIP09 introduces the DCA(Dynamic Context Attachment) feature which supports many RC QPs to share the WQE buffer in a memory pool. If a QP enables DCA feature, the WQE's buffer will not be allocated when creating but when the users start to post WRs. This will reduce the memory consumption when there are too many QPs are inactive.

Please note that we didn't find the right way to get user's configuration, so in #4 we still use environment variable to achieve this. We will be appreciated if anyone can provide some sugggestions.

That is definately not going to work.. It should be some dv thing, dv create qp or a dv customization of the parent domain spring to mind

Jason

author
#9 lore raw
Quoted 7 lines · Jason Gunthorpe
Quoted 6 lines · Weihang Li

The HIP09 introduces the DCA(Dynamic Context Attachment) feature which supports many RC QPs to share the WQE buffer in a memory pool. If a QP enables DCA feature, the WQE's buffer will not be allocated when creating but when the users start to post WRs. This will reduce the memory consumption when there are too many QPs are inactive.

Quoted 9 lines
Quoted 3 lines

Please note that we didn't find the right way to get user's configuration, so in #4 we still use environment variable to achieve this. We will be appreciated if anyone can provide some sugggestions.

That is definately not going to work.. It should be some dv thing, dv create qp or a dv customization of the parent domain spring to mind

Jason

Thank you, we will use the parameter of verbs_open_device() named private_data to allow user pass the DCA configurations into the driver according to the way of mlx5dv_open_device().

Weihang

maintainer
#10 lore raw
In reply to Weihang Li
Quoted 5 lines · Weihang Li

The HIP09 introduces the DCA(Dynamic Context Attachment) feature which supports many RC QPs to share the WQE buffer in a memory pool. If a QP enables DCA feature, the WQE's buffer will not be allocated when creating but when the users start to post WRs. This will reduce the memory consumption when there are too many QPs are inactive.

One a WQE buffer is allocated it still acts as a normal WQE ring buffer? So this DCA logic is to remap the send queue buffer based on demand for SQEs? How does it interact with the normal max send queue entries reported?

Would like to see proper man pages explaining how this all works for rdma-core.

Jason

author
#11 lore raw
Quoted 11 lines · Jason Gunthorpe
Quoted 6 lines · Weihang Li

The HIP09 introduces the DCA(Dynamic Context Attachment) feature which supports many RC QPs to share the WQE buffer in a memory pool. If a QP enables DCA feature, the WQE's buffer will not be allocated when creating but when the users start to post WRs. This will reduce the memory consumption when there are too many QPs are inactive.
One a WQE buffer is allocated it still acts as a normal WQE ring

buffer? So this DCA logic is to remap the send queue buffer based on demand for SQEs? How does it interact with the normal max send queue entries reported?

Not exactly. If DCA is enabled, we first allocate a memory pool with a default size when opening device. Each time we trying to post WR(s) to a QP, the driver will check if current QP has WQE buffer.

If not, the driver will check whether there is enough free memory in the DCA memory pool. If there is, the QP will get WQE buffer from the pool, including SQ buffer in size of max_send_wr, RQ buffer in size of max_recv_wr and extended sge buffer. If there is no enough space for the WQE buffer in the DCA pool, the driver will expand the size of pool and then assign the buffer to the QP.

And if the WQE buffer of a QP is not used, this buffer would be recycled and the DCA memory pool would be shrinked.

Quoted 5 lines

Would like to see proper man pages explaining how this all works for rdma-core.

Jason

OK, will add man pages for DCA in next version.

Thanks
Weihang

#12 lore raw
Quoted 13 lines · liweihang
Quoted 11 lines · Jason Gunthorpe
Quoted 6 lines · Weihang Li

The HIP09 introduces the DCA(Dynamic Context Attachment) feature which supports many RC QPs to share the WQE buffer in a memory pool. If a QP enables DCA feature, the WQE's buffer will not be allocated when creating but when the users start to post WRs. This will reduce the memory consumption when there are too many QPs are inactive.
One a WQE buffer is allocated it still acts as a normal WQE ring

buffer? So this DCA logic is to remap the send queue buffer based on demand for SQEs? How does it interact with the normal max send queue entries reported?

Quoted 7 lines

Not exactly. If DCA is enabled, we first allocate a memory pool with a default size when opening device. Each time we trying to post WR(s) to a QP, the driver will check if current QP has WQE buffer.

If not, the driver will check whether there is enough free memory in the DCA memory pool. If there is, the QP will get WQE buffer from the pool,

Does that mean the QP can have a non-contiguous list of buffers? Ie it isn't just a linear ring of memory?

Jason

author
#13 lore raw
Quoted 18 lines · Jason Gunthorpe
Quoted 17 lines · liweihang
Quoted 11 lines · Jason Gunthorpe
Quoted 6 lines · Weihang Li

The HIP09 introduces the DCA(Dynamic Context Attachment) feature which supports many RC QPs to share the WQE buffer in a memory pool. If a QP enables DCA feature, the WQE's buffer will not be allocated when creating but when the users start to post WRs. This will reduce the memory consumption when there are too many QPs are inactive.
One a WQE buffer is allocated it still acts as a normal WQE ring

buffer? So this DCA logic is to remap the send queue buffer based on demand for SQEs? How does it interact with the normal max send queue entries reported?

Not exactly. If DCA is enabled, we first allocate a memory pool with a default size when opening device. Each time we trying to post WR(s) to a QP, the driver will check if current QP has WQE buffer.

Quoted 9 lines
Quoted 3 lines

If not, the driver will check whether there is enough free memory in the DCA memory pool. If there is, the QP will get WQE buffer from the pool,

Does that mean the QP can have a non-contiguous list of buffers? Ie it isn't just a linear ring of memory?

Jason

Yes, when using multilevel addressing, the virtual address of QP's buffers may be non-contiguous to avoid memory fragmentation.

Weihang

author
#14 lore raw
In reply to Jason Gunthorpe
Quoted 12 lines · Jason Gunthorpe
Quoted 5 lines · Weihang Li

The HIP09 introduces the DCA(Dynamic Context Attachment) feature which supports many RC QPs to share the WQE buffer in a memory pool. If a QP enables DCA feature, the WQE's buffer will not be allocated when creating but when the users start to post WRs. This will reduce the memory consumption when there are too many QPs are inactive.

One a WQE buffer is allocated it still acts as a normal WQE ring buffer? So this DCA logic is to remap the send queue buffer based on demand for SQEs? How does it interact with the normal max send queue entries reported?

Quoted 5 lines

Would like to see proper man pages explaining how this all works for rdma-core.

Jason

Hi Jason,

I'm confused about how to introduce DCA in man pages. Current man pages in rdma-core can be classifed into public and vendor-defined ones. For example, ibv_create_qp.3 in libibverbs/man and mlx5dv.7 in providers/mlx5/man, but most of them is a description for a single interface. If we want to explain how to use DCA and how does it work, should we put a hns_dca.x file in providers/hns/man? Or add a file about hns_dca_open_device() and introduce DCA in it?

And another question, I know the files with a number suffix like ibv_create_qp.3 is for man pages in linux. What about the markdown files with .md suffix like ibv_fork_init.3.md? If we want to add a new one about DCA, which type should we choose?

Thanks
Weihang

#15 lore raw
Quoted 6 lines · liweihang

I'm confused about how to introduce DCA in man pages. Current man pages in rdma-core can be classifed into public and vendor-defined ones. For example, ibv_create_qp.3 in libibverbs/man and mlx5dv.7 in providers/mlx5/man, but most of them is a description for a single interface. If we want to explain how to use DCA and how does it work, should we put a hns_dca.x file in providers/hns/man?

You could do that, yes

Quoted 4 lines

And another question, I know the files with a number suffix like ibv_create_qp.3 is for man pages in linux. What about the markdown files with .md suffix like ibv_fork_init.3.md? If we want to add a new one about DCA, which type should we choose?

Always use the md files for new man pages please

Jason