DPDK patches and discussions
 help / color / mirror / Atom feed
From: Matan Azrad <matan@nvidia.com>
To: Viacheslav Ovsiienko <viacheslavo@nvidia.com>
Cc: <dev@dpdk.org>, Thomas Monjalon <thomas@monjalon.net>,
	<stable@dpdk.org>,  Michael Baum <michaelba@oss.nvidia.com>
Subject: [dpdk-dev] [PATCH v2 2/5] common/mlx5: add wrapped MR create API
Date: Mon, 8 Nov 2021 19:21:10 +0200	[thread overview]
Message-ID: <20211108172113.2241853-3-matan@nvidia.com> (raw)
In-Reply-To: <20211108172113.2241853-1-matan@nvidia.com>

The mlx5 PMD uses the kernel mlx5 driver to map physical memory to the
HW.

Using the Verbs API ibv_reg_mr, a mkey can be created for that.
In this case, the mkey is signed on the user ID of the kernel driver.

Using the DevX API, a mkey also can be created, but it should point an
umem object (represents the specific buffer mapping) created by the
kernel. In this case, the mkey is signed on the user ID of the process
DevX context.

In FW DevX control commands which get mkey as a parameter, there is
a security check on the user ID and Verbs mkeys are rejected.

Unfortunately, also when using DevX mkey, there is an error in the FW
command on umem validation because the umem is not designed to be used
for any mkey parameters.

As a workaround to the kernel driver/FW issue, it is needed to use a
wrapped MR, which is an indirect mkey(created by the DevX API) pointing to
direct mkey created by the kernel for any DevX command uses an MR.

Add an API to create and destroy this wrapped MR.

Fixes: 5382d28c2110 ("net/mlx5: accelerate DV flow counter transactions")
Fixes: 9d39e57f21ac ("vdpa/mlx5: support live migration")
Cc: stable@dpdk.org

Signed-off-by: Michael Baum <michaelba@oss.nvidia.com>
Signed-off-by: Matan Azrad <matan@nvidia.com>
---
 drivers/common/mlx5/linux/mlx5_common_os.c   | 56 ++++++++++++++++++++
 drivers/common/mlx5/mlx5_common.h            | 18 +++++++
 drivers/common/mlx5/version.map              |  3 ++
 drivers/common/mlx5/windows/mlx5_common_os.c | 40 ++++++++++++++
 4 files changed, 117 insertions(+)

diff --git a/drivers/common/mlx5/linux/mlx5_common_os.c b/drivers/common/mlx5/linux/mlx5_common_os.c
index b516564b79..0d3e24e04e 100644
--- a/drivers/common/mlx5/linux/mlx5_common_os.c
+++ b/drivers/common/mlx5/linux/mlx5_common_os.c
@@ -744,3 +744,59 @@ mlx5_get_device_guid(const struct rte_pci_addr *dev, uint8_t *guid, size_t len)
 	fclose(id_file);
 	return ret;
 }
+
+/*
+ * Create direct mkey using the kernel ibv_reg_mr API and wrap it with a new
+ * indirect mkey created by the DevX API.
+ * This mkey should be used for DevX commands requesting mkey as a parameter.
+ */
+int
+mlx5_os_wrapped_mkey_create(void *ctx, void *pd, uint32_t pdn, void *addr,
+			    size_t length, struct mlx5_pmd_wrapped_mr *pmd_mr)
+{
+	struct mlx5_klm klm = {
+		.byte_count = length,
+		.address = (uintptr_t)addr,
+	};
+	struct mlx5_devx_mkey_attr mkey_attr = {
+		.pd = pdn,
+		.klm_array = &klm,
+		.klm_num = 1,
+	};
+	struct mlx5_devx_obj *mkey;
+	struct ibv_mr *ibv_mr = mlx5_glue->reg_mr(pd, addr, length,
+						  IBV_ACCESS_LOCAL_WRITE |
+						  (haswell_broadwell_cpu ? 0 :
+						  IBV_ACCESS_RELAXED_ORDERING));
+
+	if (!ibv_mr) {
+		rte_errno = errno;
+		return -rte_errno;
+	}
+	klm.mkey = ibv_mr->lkey;
+	mkey_attr.addr = (uintptr_t)addr;
+	mkey_attr.size = length;
+	mkey = mlx5_devx_cmd_mkey_create(ctx, &mkey_attr);
+	if (!mkey) {
+		claim_zero(mlx5_glue->dereg_mr(ibv_mr));
+		return -rte_errno;
+	}
+	pmd_mr->addr = addr;
+	pmd_mr->len = length;
+	pmd_mr->obj = (void *)ibv_mr;
+	pmd_mr->imkey = mkey;
+	pmd_mr->lkey = mkey->id;
+	return 0;
+}
+
+void
+mlx5_os_wrapped_mkey_destroy(struct mlx5_pmd_wrapped_mr *pmd_mr)
+{
+	if (!pmd_mr)
+		return;
+	if (pmd_mr->imkey)
+		claim_zero(mlx5_devx_cmd_destroy(pmd_mr->imkey));
+	if (pmd_mr->obj)
+		claim_zero(mlx5_glue->dereg_mr(pmd_mr->obj));
+	memset(pmd_mr, 0, sizeof(*pmd_mr));
+}
diff --git a/drivers/common/mlx5/mlx5_common.h b/drivers/common/mlx5/mlx5_common.h
index 744c6a72b3..62109a671a 100644
--- a/drivers/common/mlx5/mlx5_common.h
+++ b/drivers/common/mlx5/mlx5_common.h
@@ -429,4 +429,22 @@ mlx5_mr_mb2mr(struct mlx5_common_device *cdev, struct mlx5_mp_id *mp_id,
 int mlx5_os_open_device(struct mlx5_common_device *cdev, uint32_t classes);
 int mlx5_os_pd_create(struct mlx5_common_device *cdev);
 
+/* mlx5 PMD wrapped MR struct. */
+struct mlx5_pmd_wrapped_mr {
+	uint32_t	     lkey;
+	void		     *addr;
+	size_t		     len;
+	void		     *obj; /* verbs mr object or devx umem object. */
+	void		     *imkey; /* DevX indirect mkey object. */
+};
+
+__rte_internal
+int
+mlx5_os_wrapped_mkey_create(void *ctx, void *pd, uint32_t pdn, void *addr,
+			    size_t length, struct mlx5_pmd_wrapped_mr *pmd_mr);
+
+__rte_internal
+void
+mlx5_os_wrapped_mkey_destroy(struct mlx5_pmd_wrapped_mr *pmd_mr);
+
 #endif /* RTE_PMD_MLX5_COMMON_H_ */
diff --git a/drivers/common/mlx5/version.map b/drivers/common/mlx5/version.map
index 6e17a7b8b8..28d685a7c6 100644
--- a/drivers/common/mlx5/version.map
+++ b/drivers/common/mlx5/version.map
@@ -133,6 +133,9 @@ INTERNAL {
 	mlx5_os_umem_dereg;
 	mlx5_os_umem_reg;
 
+	mlx5_os_wrapped_mkey_create;
+	mlx5_os_wrapped_mkey_destroy;
+
 	mlx5_realloc;
 
 	mlx5_translate_port_name; # WINDOWS_NO_EXPORT
diff --git a/drivers/common/mlx5/windows/mlx5_common_os.c b/drivers/common/mlx5/windows/mlx5_common_os.c
index ea478d7395..0d03344343 100644
--- a/drivers/common/mlx5/windows/mlx5_common_os.c
+++ b/drivers/common/mlx5/windows/mlx5_common_os.c
@@ -390,3 +390,43 @@ mlx5_os_set_reg_mr_cb(mlx5_reg_mr_t *reg_mr_cb, mlx5_dereg_mr_t *dereg_mr_cb)
 	*reg_mr_cb = mlx5_os_reg_mr;
 	*dereg_mr_cb = mlx5_os_dereg_mr;
 }
+
+/*
+ * In Windows, no need to wrap the MR, no known issue for it in kernel.
+ * Use the regular function to create direct MR.
+ */
+int
+mlx5_os_wrapped_mkey_create(void *ctx, void *pd, uint32_t pdn, void *addr,
+			    size_t length, struct mlx5_pmd_wrapped_mr *wpmd_mr)
+{
+	struct mlx5_pmd_mr pmd_mr = {0};
+	int ret = mlx5_os_reg_mr(pd, addr, length, &pmd_mr);
+
+	(void)pdn;
+	(void)ctx;
+	if (ret != 0)
+		return -1;
+	wpmd_mr->addr = addr;
+	wpmd_mr->len = length;
+	wpmd_mr->obj = pmd_mr.obj;
+	wpmd_mr->imkey = pmd_mr.mkey;
+	wpmd_mr->lkey = pmd_mr.mkey->id;
+	return 0;
+}
+
+void
+mlx5_os_wrapped_mkey_destroy(struct mlx5_pmd_wrapped_mr *wpmd_mr)
+{
+	struct mlx5_pmd_mr pmd_mr;
+
+	if (!wpmd_mr)
+		return;
+	pmd_mr.addr = wpmd_mr->addr;
+	pmd_mr.len = wpmd_mr->len;
+	pmd_mr.obj = wpmd_mr->obj;
+	pmd_mr.mkey = wpmd_mr->imkey;
+	pmd_mr.lkey = wpmd_mr->lkey;
+	mlx5_os_dereg_mr(&pmd_mr);
+	memset(wpmd_mr, 0, sizeof(*wpmd_mr));
+}
+
-- 
2.25.1


  parent reply	other threads:[~2021-11-08 17:22 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-07 15:29 [dpdk-dev] [PATCH 0/5] mlx5: workaround MR issues in FW\kernel Matan Azrad
2021-11-07 15:29 ` [dpdk-dev] [PATCH 1/5] common/mlx5: glue MR registration with IOVA Matan Azrad
2021-11-07 15:29 ` [dpdk-dev] [PATCH 2/5] common/mlx5: add wrapped MR create API Matan Azrad
2021-11-07 15:29 ` [dpdk-dev] [PATCH 3/5] vdpa/mlx5: workaround dirty bitmap MR creation Matan Azrad
2021-11-07 15:29 ` [dpdk-dev] [PATCH 4/5] vdpa/mlx5: workaround guest MR registrations Matan Azrad
2021-11-07 15:29 ` [dpdk-dev] [PATCH 5/5] net/mlx5: workaround counter memory region creation Matan Azrad
2021-11-08 17:21 ` [dpdk-dev] [PATCH v2 0/5] mlx5: workaround MR issues Matan Azrad
2021-11-08 17:21   ` [dpdk-dev] [PATCH v2 1/5] common/mlx5: glue MR registration with IOVA Matan Azrad
2021-11-08 17:21   ` Matan Azrad [this message]
2021-11-08 17:21   ` [dpdk-dev] [PATCH v2 3/5] vdpa/mlx5: workaround dirty bitmap MR creation Matan Azrad
2021-11-08 19:38     ` [dpdk-dev] [dpdk-stable] " Thomas Monjalon
2021-11-08 17:21   ` [dpdk-dev] [PATCH v2 4/5] vdpa/mlx5: workaround guest MR registrations Matan Azrad
2021-11-08 17:21   ` [dpdk-dev] [PATCH v2 5/5] net/mlx5: workaround MR creation for flow counter Matan Azrad
2021-11-09 12:23   ` [dpdk-dev] [PATCH v3 0/5] mlx5: workaround MR issues Matan Azrad
2021-11-09 12:36   ` Matan Azrad
2021-11-09 12:36     ` [dpdk-dev] [PATCH v3 1/5] common/mlx5: glue MR registration with IOVA Matan Azrad
2021-11-09 12:36     ` [dpdk-dev] [PATCH v3 2/5] common/mlx5: add wrapped MR create API Matan Azrad
2021-11-09 12:36     ` [dpdk-dev] [PATCH v3 3/5] vdpa/mlx5: workaround dirty bitmap MR creation Matan Azrad
2021-11-09 12:36     ` [dpdk-dev] [PATCH v3 4/5] vdpa/mlx5: workaround guest MR registrations Matan Azrad
2021-11-09 12:36     ` [dpdk-dev] [PATCH v3 5/5] net/mlx5: workaround MR creation for flow counter Matan Azrad
2021-11-10 14:55     ` [dpdk-dev] [PATCH v3 0/5] mlx5: workaround MR issues Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211108172113.2241853-3-matan@nvidia.com \
    --to=matan@nvidia.com \
    --cc=dev@dpdk.org \
    --cc=michaelba@oss.nvidia.com \
    --cc=stable@dpdk.org \
    --cc=thomas@monjalon.net \
    --cc=viacheslavo@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).