From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id D6F01A0577; Tue, 14 Apr 2020 09:27:10 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 23E211C136; Tue, 14 Apr 2020 09:27:10 +0200 (CEST) Received: from EUR05-VI1-obe.outbound.protection.outlook.com (mail-vi1eur05on2040.outbound.protection.outlook.com [40.107.21.40]) by dpdk.org (Postfix) with ESMTP id 9BB5B1C125 for ; Tue, 14 Apr 2020 09:27:08 +0200 (CEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BwQxv7juxcuztF1z6Y4eTzmyoLP+nELELoDB6lOspoZIbpPk2Sb0jlnndkinf/jbjoiLv+WCvYQkFFXSiOSwjVXc+D+U01U1C/d/eKNoYcV8bfEQUjdiqmGlwRe1ZK3WNoffR3bQvOsmb2uXrooQwKoUzUXjNFmzbpC6MC5Esf8IM7nyqw+fjgSNe72c+aNvT/3dpGYR779+BTareypIh1LGvp2yWZ7wVnsy0dn+aYdyNPjipWjJ3Re121kgZNBk8sxIb7caNInV/eHysPlHKJ3KHaWqHlV/wR7toUHDe4EYsRmcNpkmtij+c+CX8C1yAHHbUxAWXo+UDN+dma71ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BpBqRB/m+w0QsJ1KqtG2xYaU9hgsxQyVMoSijo5FeRo=; b=mCw0bdYRYvHZKMV8gjfpu0KrGRtd4bIRZMYV3bdjwYcMZt24TQg575iqwTqu8ThAUExR180TPqG9X/uuemPj+Lfzwnq3zMkOKC5JQd7+wO0t0gfOcpotsmFYNN3G3pxb0bD7mkNTdXLHlMGv9V9+Ej3SvWt+Upr7Ri+7ftymfhIF7HX9zTG8aVFqG+cHXzbNAB2GBYV0YYRlRURoG6BDXsT+cJHaWVSfPKBOisFHjKwxg+ht5FCRmXemcqn69rXMvibMrW/1ctmfQ30nkwxucYkDf5lq+D5PQ3SHbPd0gyHIHJfwG585993ygORf/FEat6YAJyYu6PfO5KtuKcu+gA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=mellanox.com; dmarc=pass action=none header.from=mellanox.com; dkim=pass header.d=mellanox.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Mellanox.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BpBqRB/m+w0QsJ1KqtG2xYaU9hgsxQyVMoSijo5FeRo=; b=oZSQU4xI8oBsNdDyFqLi55nX/jnOpkAFiT4B63bUF1cdYuz7p0F1nH23cRyDT7JAkC4QwN+zkMT6TnFI1qq2jEui7m4LTcwbxur+AKRGi9QAY/EyseW6i36v8mG0jseFScWJGLAkRdDmrB5sDk+W/DSMj8NYmBCaoi8CBiNqazg= Received: from AM4PR05MB3265.eurprd05.prod.outlook.com (2603:10a6:205:8::26) by AM4PR05MB3489.eurprd05.prod.outlook.com (2603:10a6:205:11::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2900.15; Tue, 14 Apr 2020 07:27:06 +0000 Received: from AM4PR05MB3265.eurprd05.prod.outlook.com ([fe80::bdf0:88a3:3a39:4be4]) by AM4PR05MB3265.eurprd05.prod.outlook.com ([fe80::bdf0:88a3:3a39:4be4%7]) with mapi id 15.20.2900.028; Tue, 14 Apr 2020 07:27:06 +0000 From: Slava Ovsiienko To: Vu Pham , "dev@dpdk.org" CC: Ori Kam , Matan Azrad , Raslan Darawsheh , Vu Pham Thread-Topic: [PATCH v4 2/2] common/mlx5: refactor memory management codes Thread-Index: AQHWEdkoSqiEdN29hEWElXEkS2fPzah4OGVw Date: Tue, 14 Apr 2020 07:27:05 +0000 Message-ID: References: <20200402192105.11606-1-vuhuoong@mellanox.com> <20200413211748.20500-1-vuhuong@mellanox.com> <20200413211748.20500-3-vuhuong@mellanox.com> In-Reply-To: <20200413211748.20500-3-vuhuong@mellanox.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=viacheslavo@mellanox.com; x-originating-ip: [95.164.10.10] x-ms-publictraffictype: Email x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 9c787fbf-192d-4314-c03f-08d7e0453c58 x-ms-traffictypediagnostic: AM4PR05MB3489:|AM4PR05MB3489: x-ld-processed: a652971c-7d2e-4d9b-a6a4-d149256f461b,ExtFwd x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:352; x-forefront-prvs: 0373D94D15 x-forefront-antispam-report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM4PR05MB3265.eurprd05.prod.outlook.com; PTR:; CAT:NONE; SFTY:; SFS:(10009020)(4636009)(346002)(366004)(39850400004)(376002)(136003)(396003)(8676002)(54906003)(81156014)(71200400001)(30864003)(316002)(110136005)(478600001)(45080400002)(33656002)(66946007)(186003)(107886003)(4326008)(2906002)(86362001)(55016002)(8936002)(5660300002)(53546011)(66446008)(66556008)(7696005)(52536014)(64756008)(6506007)(9686003)(26005)(76116006)(66476007)(559001)(569008); DIR:OUT; SFP:1101; received-spf: None (protection.outlook.com: mellanox.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: R2jVveCZcIi8JjZKrFLqCbAzTVuBxPNLMm+ruQCU9sWH8oCWeQJEvQtrdKoRYmp4Y/19DSQUBT18Ee0Dr+yXMUPH7Rr/S1msSkAwvjXOSBWw7gqdf3aQ7l6lZykM58kTsC+eOtCNs3xzfIginurb1fISt3xSNPeNreePCoMEkRGDR614uh3icfxbxWNF4ut+L1JpIlOnmiF8GzQpAZwF1B60HoM7nixbm7UaMqIMsheN7f9gufQj+LNeCEJKZKTXwIOIv5ngmjQMAys7JRTjO/hQWxj3L7KDY+ReQl0x3uAVaMZ2d4P74TYhhvPCfRhLGRowoKe5a5b8blxtuDwtrg6PtZoRr0uieT9rcSFuAB26K0RXq0GU8VPzXK5K8lCJjt12YaYI8mzH8t+lnBoHdgg3U+1J8fp1fgJDOwsWrO0or5Ctrxw2VsNnPIajNCaVcBMEIml1tvmX/GcXnknqVKkRRGXuOe6tAEXmthlT0lk= x-ms-exchange-antispam-messagedata: 4Oylq6lvMJBftyZgn0UK39OsJQJQEikKKeQ+MZP1jSdDkEyNdAOwdpF/qxVcbAGwkbHoXi9k6faoIb229KiIOL7+I2JcUYNmme3jE59iD0ECLbmZRpKR3SSnVSox0OtWobDU5cZmYxzIZFzrNTA58Q== Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: Mellanox.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9c787fbf-192d-4314-c03f-08d7e0453c58 X-MS-Exchange-CrossTenant-originalarrivaltime: 14 Apr 2020 07:27:06.4020 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: a652971c-7d2e-4d9b-a6a4-d149256f461b X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: /dvfeJxzz73Jnbq2xbvChC7KEwNwE+ueOzkkI7PSyGtaS35wRqqJ7rm+pgcohRh1PibuqdkpPNU3qq1w9ZzxzMAKTn23qmd3qSX1t6YMpJM= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM4PR05MB3489 Subject: Re: [dpdk-dev] [PATCH v4 2/2] common/mlx5: refactor memory management codes X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" > -----Original Message----- > From: Vu Pham > Sent: Tuesday, April 14, 2020 0:18 > To: dev@dpdk.org > Cc: Slava Ovsiienko ; Ori Kam > ; Matan Azrad ; Raslan > Darawsheh ; Vu Pham > Subject: [PATCH v4 2/2] common/mlx5: refactor memory management codes >=20 > Refactor common memory btree and cache management to common driver. > Replace some input parameters of MR APIs to more common datastructure > like PD, port_id, share_cache,... so that muliptle PMD drivers can > use those MR APIs. >=20 > Modify mlx5 net pmd driver to use MR management APIs from common > driver. >=20 > Signed-off-by: Vu Pham Acked-by: Viacheslav Ovsiienko > --- > drivers/common/mlx5/Makefile | 1 + > drivers/common/mlx5/meson.build | 1 + > drivers/common/mlx5/mlx5_common_mr.c | 1108 > +++++++++++++++++++++ > drivers/common/mlx5/mlx5_common_mr.h | 160 ++++ > drivers/common/mlx5/rte_common_mlx5_version.map | 14 + > drivers/net/mlx5/mlx5.c | 4 +- > drivers/net/mlx5/mlx5.h | 12 +- > drivers/net/mlx5/mlx5_mp.c | 8 +- > drivers/net/mlx5/mlx5_mr.c | 1169 +----------------= ------ > drivers/net/mlx5/mlx5_mr.h | 87 +- > drivers/net/mlx5/mlx5_rxtx.c | 1 + > drivers/net/mlx5/mlx5_rxtx.h | 10 +- > drivers/net/mlx5/mlx5_rxtx_vec.h | 2 + > drivers/net/mlx5/mlx5_trigger.c | 1 + > drivers/net/mlx5/mlx5_txq.c | 3 +- > 15 files changed, 1357 insertions(+), 1224 deletions(-) > create mode 100644 drivers/common/mlx5/mlx5_common_mr.c > create mode 100644 drivers/common/mlx5/mlx5_common_mr.h >=20 > diff --git a/drivers/common/mlx5/Makefile b/drivers/common/mlx5/Makefile > index 2a88492731..26267c957a 100644 > --- a/drivers/common/mlx5/Makefile > +++ b/drivers/common/mlx5/Makefile > @@ -18,6 +18,7 @@ SRCS-y +=3D mlx5_devx_cmds.c > SRCS-y +=3D mlx5_common.c > SRCS-y +=3D mlx5_nl.c > SRCS-y +=3D mlx5_common_mp.c > +SRCS-y +=3D mlx5_common_mr.c > ifeq ($(CONFIG_RTE_IBVERBS_LINK_DLOPEN),y) > INSTALL-y-lib +=3D $(LIB_GLUE) > endif > diff --git a/drivers/common/mlx5/meson.build > b/drivers/common/mlx5/meson.build > index 83671861c9..175251b691 100644 > --- a/drivers/common/mlx5/meson.build > +++ b/drivers/common/mlx5/meson.build > @@ -56,6 +56,7 @@ sources =3D files( > 'mlx5_common.c', > 'mlx5_nl.c', > 'mlx5_common_mp.c', > + 'mlx5_common_mr.c', > ) > if not dlopen_ibverbs > sources +=3D files('mlx5_glue.c') > diff --git a/drivers/common/mlx5/mlx5_common_mr.c > b/drivers/common/mlx5/mlx5_common_mr.c > new file mode 100644 > index 0000000000..9d4a06dd5b > --- /dev/null > +++ b/drivers/common/mlx5/mlx5_common_mr.c > @@ -0,0 +1,1108 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright 2016 6WIND S.A. > + * Copyright 2020 Mellanox Technologies, Ltd > + */ > +#include > +#include > +#include > +#include > +#include > + > +#include "mlx5_glue.h" > +#include "mlx5_common_mp.h" > +#include "mlx5_common_mr.h" > +#include "mlx5_common_utils.h" > + > +struct mr_find_contig_memsegs_data { > + uintptr_t addr; > + uintptr_t start; > + uintptr_t end; > + const struct rte_memseg_list *msl; > +}; > + > +/** > + * Expand B-tree table to a given size. Can't be called with holding > + * memory_hotplug_lock or share_cache.rwlock due to rte_realloc(). > + * > + * @param bt > + * Pointer to B-tree structure. > + * @param n > + * Number of entries for expansion. > + * > + * @return > + * 0 on success, -1 on failure. > + */ > +static int > +mr_btree_expand(struct mlx5_mr_btree *bt, int n) > +{ > + void *mem; > + int ret =3D 0; > + > + if (n <=3D bt->size) > + return ret; > + /* > + * Downside of directly using rte_realloc() is that SOCKET_ID_ANY is > + * used inside if there's no room to expand. Because this is a quite > + * rare case and a part of very slow path, it is very acceptable. > + * Initially cache_bh[] will be given practically enough space and once > + * it is expanded, expansion wouldn't be needed again ever. > + */ > + mem =3D rte_realloc(bt->table, n * sizeof(struct mr_cache_entry), 0); > + if (mem =3D=3D NULL) { > + /* Not an error, B-tree search will be skipped. */ > + DRV_LOG(WARNING, "failed to expand MR B-tree (%p) table", > + (void *)bt); > + ret =3D -1; > + } else { > + DRV_LOG(DEBUG, "expanded MR B-tree table (size=3D%u)", n); > + bt->table =3D mem; > + bt->size =3D n; > + } > + return ret; > +} > + > +/** > + * Look up LKey from given B-tree lookup table, store the last index and > return > + * searched LKey. > + * > + * @param bt > + * Pointer to B-tree structure. > + * @param[out] idx > + * Pointer to index. Even on search failure, returns index where it st= ops > + * searching so that index can be used when inserting a new entry. > + * @param addr > + * Search key. > + * > + * @return > + * Searched LKey on success, UINT32_MAX on no match. > + */ > +static uint32_t > +mr_btree_lookup(struct mlx5_mr_btree *bt, uint16_t *idx, uintptr_t addr) > +{ > + struct mr_cache_entry *lkp_tbl; > + uint16_t n; > + uint16_t base =3D 0; > + > + MLX5_ASSERT(bt !=3D NULL); > + lkp_tbl =3D *bt->table; > + n =3D bt->len; > + /* First entry must be NULL for comparison. */ > + MLX5_ASSERT(bt->len > 0 || (lkp_tbl[0].start =3D=3D 0 && > + lkp_tbl[0].lkey =3D=3D UINT32_MAX)); > + /* Binary search. */ > + do { > + register uint16_t delta =3D n >> 1; > + > + if (addr < lkp_tbl[base + delta].start) { > + n =3D delta; > + } else { > + base +=3D delta; > + n -=3D delta; > + } > + } while (n > 1); > + MLX5_ASSERT(addr >=3D lkp_tbl[base].start); > + *idx =3D base; > + if (addr < lkp_tbl[base].end) > + return lkp_tbl[base].lkey; > + /* Not found. */ > + return UINT32_MAX; > +} > + > +/** > + * Insert an entry to B-tree lookup table. > + * > + * @param bt > + * Pointer to B-tree structure. > + * @param entry > + * Pointer to new entry to insert. > + * > + * @return > + * 0 on success, -1 on failure. > + */ > +static int > +mr_btree_insert(struct mlx5_mr_btree *bt, struct mr_cache_entry *entry) > +{ > + struct mr_cache_entry *lkp_tbl; > + uint16_t idx =3D 0; > + size_t shift; > + > + MLX5_ASSERT(bt !=3D NULL); > + MLX5_ASSERT(bt->len <=3D bt->size); > + MLX5_ASSERT(bt->len > 0); > + lkp_tbl =3D *bt->table; > + /* Find out the slot for insertion. */ > + if (mr_btree_lookup(bt, &idx, entry->start) !=3D UINT32_MAX) { > + DRV_LOG(DEBUG, > + "abort insertion to B-tree(%p): already exist at" > + " idx=3D%u [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=3D0x%x", > + (void *)bt, idx, entry->start, entry->end, entry->lkey); > + /* Already exist, return. */ > + return 0; > + } > + /* If table is full, return error. */ > + if (unlikely(bt->len =3D=3D bt->size)) { > + bt->overflow =3D 1; > + return -1; > + } > + /* Insert entry. */ > + ++idx; > + shift =3D (bt->len - idx) * sizeof(struct mr_cache_entry); > + if (shift) > + memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift); > + lkp_tbl[idx] =3D *entry; > + bt->len++; > + DRV_LOG(DEBUG, > + "inserted B-tree(%p)[%u]," > + " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=3D0x%x", > + (void *)bt, idx, entry->start, entry->end, entry->lkey); > + return 0; > +} > + > +/** > + * Initialize B-tree and allocate memory for lookup table. > + * > + * @param bt > + * Pointer to B-tree structure. > + * @param n > + * Number of entries to allocate. > + * @param socket > + * NUMA socket on which memory must be allocated. > + * > + * @return > + * 0 on success, a negative errno value otherwise and rte_errno is set= . > + */ > +int > +mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket) > +{ > + if (bt =3D=3D NULL) { > + rte_errno =3D EINVAL; > + return -rte_errno; > + } > + MLX5_ASSERT(!bt->table && !bt->size); > + memset(bt, 0, sizeof(*bt)); > + bt->table =3D rte_calloc_socket("B-tree table", > + n, sizeof(struct mr_cache_entry), > + 0, socket); > + if (bt->table =3D=3D NULL) { > + rte_errno =3D ENOMEM; > + DEBUG("failed to allocate memory for btree cache on socket > %d", > + socket); > + return -rte_errno; > + } > + bt->size =3D n; > + /* First entry must be NULL for binary search. */ > + (*bt->table)[bt->len++] =3D (struct mr_cache_entry) { > + .lkey =3D UINT32_MAX, > + }; > + DEBUG("initialized B-tree %p with table %p", > + (void *)bt, (void *)bt->table); > + return 0; > +} > + > +/** > + * Free B-tree resources. > + * > + * @param bt > + * Pointer to B-tree structure. > + */ > +void > +mlx5_mr_btree_free(struct mlx5_mr_btree *bt) > +{ > + if (bt =3D=3D NULL) > + return; > + DEBUG("freeing B-tree %p with table %p", > + (void *)bt, (void *)bt->table); > + rte_free(bt->table); > + memset(bt, 0, sizeof(*bt)); > +} > + > +/** > + * Dump all the entries in a B-tree > + * > + * @param bt > + * Pointer to B-tree structure. > + */ > +void > +mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused) > +{ > +#ifdef RTE_LIBRTE_MLX5_DEBUG > + int idx; > + struct mr_cache_entry *lkp_tbl; > + > + if (bt =3D=3D NULL) > + return; > + lkp_tbl =3D *bt->table; > + for (idx =3D 0; idx < bt->len; ++idx) { > + struct mr_cache_entry *entry =3D &lkp_tbl[idx]; > + > + DEBUG("B-tree(%p)[%u]," > + " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=3D0x%x", > + (void *)bt, idx, entry->start, entry->end, entry->lkey); > + } > +#endif > +} > + > +/** > + * Find virtually contiguous memory chunk in a given MR. > + * > + * @param dev > + * Pointer to MR structure. > + * @param[out] entry > + * Pointer to returning MR cache entry. If not found, this will not be > + * updated. > + * @param start_idx > + * Start index of the memseg bitmap. > + * > + * @return > + * Next index to go on lookup. > + */ > +static int > +mr_find_next_chunk(struct mlx5_mr *mr, struct mr_cache_entry *entry, > + int base_idx) > +{ > + uintptr_t start =3D 0; > + uintptr_t end =3D 0; > + uint32_t idx =3D 0; > + > + /* MR for external memory doesn't have memseg list. */ > + if (mr->msl =3D=3D NULL) { > + struct ibv_mr *ibv_mr =3D mr->ibv_mr; > + > + MLX5_ASSERT(mr->ms_bmp_n =3D=3D 1); > + MLX5_ASSERT(mr->ms_n =3D=3D 1); > + MLX5_ASSERT(base_idx =3D=3D 0); > + /* > + * Can't search it from memseg list but get it directly from > + * verbs MR as there's only one chunk. > + */ > + entry->start =3D (uintptr_t)ibv_mr->addr; > + entry->end =3D (uintptr_t)ibv_mr->addr + mr->ibv_mr->length; > + entry->lkey =3D rte_cpu_to_be_32(mr->ibv_mr->lkey); > + /* Returning 1 ends iteration. */ > + return 1; > + } > + for (idx =3D base_idx; idx < mr->ms_bmp_n; ++idx) { > + if (rte_bitmap_get(mr->ms_bmp, idx)) { > + const struct rte_memseg_list *msl; > + const struct rte_memseg *ms; > + > + msl =3D mr->msl; > + ms =3D rte_fbarray_get(&msl->memseg_arr, > + mr->ms_base_idx + idx); > + MLX5_ASSERT(msl->page_sz =3D=3D ms->hugepage_sz); > + if (!start) > + start =3D ms->addr_64; > + end =3D ms->addr_64 + ms->hugepage_sz; > + } else if (start) { > + /* Passed the end of a fragment. */ > + break; > + } > + } > + if (start) { > + /* Found one chunk. */ > + entry->start =3D start; > + entry->end =3D end; > + entry->lkey =3D rte_cpu_to_be_32(mr->ibv_mr->lkey); > + } > + return idx; > +} > + > +/** > + * Insert a MR to the global B-tree cache. It may fail due to low-on-mem= ory. > + * Then, this entry will have to be searched by mr_lookup_list() in > + * mlx5_mr_create() on miss. > + * > + * @param share_cache > + * Pointer to a global shared MR cache. > + * @param mr > + * Pointer to MR to insert. > + * > + * @return > + * 0 on success, -1 on failure. > + */ > +int > +mlx5_mr_insert_cache(struct mlx5_mr_share_cache *share_cache, > + struct mlx5_mr *mr) > +{ > + unsigned int n; > + > + DRV_LOG(DEBUG, "Inserting MR(%p) to global cache(%p)", > + (void *)mr, (void *)share_cache); > + for (n =3D 0; n < mr->ms_bmp_n; ) { > + struct mr_cache_entry entry; > + > + memset(&entry, 0, sizeof(entry)); > + /* Find a contiguous chunk and advance the index. */ > + n =3D mr_find_next_chunk(mr, &entry, n); > + if (!entry.end) > + break; > + if (mr_btree_insert(&share_cache->cache, &entry) < 0) { > + /* > + * Overflowed, but the global table cannot be > expanded > + * because of deadlock. > + */ > + return -1; > + } > + } > + return 0; > +} > + > +/** > + * Look up address in the original global MR list. > + * > + * @param share_cache > + * Pointer to a global shared MR cache. > + * @param[out] entry > + * Pointer to returning MR cache entry. If no match, this will not be > updated. > + * @param addr > + * Search key. > + * > + * @return > + * Found MR on match, NULL otherwise. > + */ > +struct mlx5_mr * > +mlx5_mr_lookup_list(struct mlx5_mr_share_cache *share_cache, > + struct mr_cache_entry *entry, uintptr_t addr) > +{ > + struct mlx5_mr *mr; > + > + /* Iterate all the existing MRs. */ > + LIST_FOREACH(mr, &share_cache->mr_list, mr) { > + unsigned int n; > + > + if (mr->ms_n =3D=3D 0) > + continue; > + for (n =3D 0; n < mr->ms_bmp_n; ) { > + struct mr_cache_entry ret; > + > + memset(&ret, 0, sizeof(ret)); > + n =3D mr_find_next_chunk(mr, &ret, n); > + if (addr >=3D ret.start && addr < ret.end) { > + /* Found. */ > + *entry =3D ret; > + return mr; > + } > + } > + } > + return NULL; > +} > + > +/** > + * Look up address on global MR cache. > + * > + * @param share_cache > + * Pointer to a global shared MR cache. > + * @param[out] entry > + * Pointer to returning MR cache entry. If no match, this will not be > updated. > + * @param addr > + * Search key. > + * > + * @return > + * Searched LKey on success, UINT32_MAX on failure and rte_errno is se= t. > + */ > +uint32_t > +mlx5_mr_lookup_cache(struct mlx5_mr_share_cache *share_cache, > + struct mr_cache_entry *entry, uintptr_t addr) > +{ > + uint16_t idx; > + uint32_t lkey =3D UINT32_MAX; > + struct mlx5_mr *mr; > + > + /* > + * If the global cache has overflowed since it failed to expand the > + * B-tree table, it can't have all the existing MRs. Then, the address > + * has to be searched by traversing the original MR list instead, which > + * is very slow path. Otherwise, the global cache is all inclusive. > + */ > + if (!unlikely(share_cache->cache.overflow)) { > + lkey =3D mr_btree_lookup(&share_cache->cache, &idx, addr); > + if (lkey !=3D UINT32_MAX) > + *entry =3D (*share_cache->cache.table)[idx]; > + } else { > + /* Falling back to the slowest path. */ > + mr =3D mlx5_mr_lookup_list(share_cache, entry, addr); > + if (mr !=3D NULL) > + lkey =3D entry->lkey; > + } > + MLX5_ASSERT(lkey =3D=3D UINT32_MAX || (addr >=3D entry->start && > + addr < entry->end)); > + return lkey; > +} > + > +/** > + * Free MR resources. MR lock must not be held to avoid a deadlock. > rte_free() > + * can raise memory free event and the callback function will spin on th= e > lock. > + * > + * @param mr > + * Pointer to MR to free. > + */ > +static void > +mr_free(struct mlx5_mr *mr) > +{ > + if (mr =3D=3D NULL) > + return; > + DRV_LOG(DEBUG, "freeing MR(%p):", (void *)mr); > + if (mr->ibv_mr !=3D NULL) > + claim_zero(mlx5_glue->dereg_mr(mr->ibv_mr)); > + if (mr->ms_bmp !=3D NULL) > + rte_bitmap_free(mr->ms_bmp); > + rte_free(mr); > +} > + > +void > +mlx5_mr_rebuild_cache(struct mlx5_mr_share_cache *share_cache) > +{ > + struct mlx5_mr *mr; > + > + DRV_LOG(DEBUG, "Rebuild dev cache[] %p", (void *)share_cache); > + /* Flush cache to rebuild. */ > + share_cache->cache.len =3D 1; > + share_cache->cache.overflow =3D 0; > + /* Iterate all the existing MRs. */ > + LIST_FOREACH(mr, &share_cache->mr_list, mr) > + if (mlx5_mr_insert_cache(share_cache, mr) < 0) > + return; > +} > + > +/** > + * Release resources of detached MR having no online entry. > + * > + * @param share_cache > + * Pointer to a global shared MR cache. > + */ > +static void > +mlx5_mr_garbage_collect(struct mlx5_mr_share_cache *share_cache) > +{ > + struct mlx5_mr *mr_next; > + struct mlx5_mr_list free_list =3D LIST_HEAD_INITIALIZER(free_list); > + > + /* Must be called from the primary process. */ > + MLX5_ASSERT(rte_eal_process_type() =3D=3D RTE_PROC_PRIMARY); > + /* > + * MR can't be freed with holding the lock because rte_free() could > call > + * memory free callback function. This will be a deadlock situation. > + */ > + rte_rwlock_write_lock(&share_cache->rwlock); > + /* Detach the whole free list and release it after unlocking. */ > + free_list =3D share_cache->mr_free_list; > + LIST_INIT(&share_cache->mr_free_list); > + rte_rwlock_write_unlock(&share_cache->rwlock); > + /* Release resources. */ > + mr_next =3D LIST_FIRST(&free_list); > + while (mr_next !=3D NULL) { > + struct mlx5_mr *mr =3D mr_next; > + > + mr_next =3D LIST_NEXT(mr, mr); > + mr_free(mr); > + } > +} > + > +/* Called during rte_memseg_contig_walk() by mlx5_mr_create(). */ > +static int > +mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl, > + const struct rte_memseg *ms, size_t len, void *arg) > +{ > + struct mr_find_contig_memsegs_data *data =3D arg; > + > + if (data->addr < ms->addr_64 || data->addr >=3D ms->addr_64 + len) > + return 0; > + /* Found, save it and stop walking. */ > + data->start =3D ms->addr_64; > + data->end =3D ms->addr_64 + len; > + data->msl =3D msl; > + return 1; > +} > + > +/** > + * Create a new global Memory Region (MR) for a missing virtual address. > + * This API should be called on a secondary process, then a request is s= ent to > + * the primary process in order to create a MR for the address. As the g= lobal > MR > + * list is on the shared memory, following LKey lookup should succeed un= less > the > + * request fails. > + * > + * @param pd > + * Pointer to ibv_pd of a device (net, regex, vdpa,...). > + * @param share_cache > + * Pointer to a global shared MR cache. > + * @param[out] entry > + * Pointer to returning MR cache entry, found in the global cache or n= ewly > + * created. If failed to create one, this will not be updated. > + * @param addr > + * Target virtual address to register. > + * @param mr_ext_memseg_en > + * Configurable flag about external memory segment enable or not. > + * > + * @return > + * Searched LKey on success, UINT32_MAX on failure and rte_errno is se= t. > + */ > +static uint32_t > +mlx5_mr_create_secondary(struct ibv_pd *pd __rte_unused, > + struct mlx5_mp_id *mp_id, > + struct mlx5_mr_share_cache *share_cache, > + struct mr_cache_entry *entry, uintptr_t addr, > + unsigned int mr_ext_memseg_en __rte_unused) > +{ > + int ret; > + > + DEBUG("port %u requesting MR creation for address (%p)", > + mp_id->port_id, (void *)addr); > + ret =3D mlx5_mp_req_mr_create(mp_id, addr); > + if (ret) { > + DEBUG("Fail to request MR creation for address (%p)", > + (void *)addr); > + return UINT32_MAX; > + } > + rte_rwlock_read_lock(&share_cache->rwlock); > + /* Fill in output data. */ > + mlx5_mr_lookup_cache(share_cache, entry, addr); > + /* Lookup can't fail. */ > + MLX5_ASSERT(entry->lkey !=3D UINT32_MAX); > + rte_rwlock_read_unlock(&share_cache->rwlock); > + DEBUG("MR CREATED by primary process for %p:\n" > + " [0x%" PRIxPTR ", 0x%" PRIxPTR "), lkey=3D0x%x", > + (void *)addr, entry->start, entry->end, entry->lkey); > + return entry->lkey; > +} > + > +/** > + * Create a new global Memory Region (MR) for a missing virtual address. > + * Register entire virtually contiguous memory chunk around the address. > + * > + * @param pd > + * Pointer to ibv_pd of a device (net, regex, vdpa,...). > + * @param share_cache > + * Pointer to a global shared MR cache. > + * @param[out] entry > + * Pointer to returning MR cache entry, found in the global cache or n= ewly > + * created. If failed to create one, this will not be updated. > + * @param addr > + * Target virtual address to register. > + * @param mr_ext_memseg_en > + * Configurable flag about external memory segment enable or not. > + * > + * @return > + * Searched LKey on success, UINT32_MAX on failure and rte_errno is se= t. > + */ > +uint32_t > +mlx5_mr_create_primary(struct ibv_pd *pd, > + struct mlx5_mr_share_cache *share_cache, > + struct mr_cache_entry *entry, uintptr_t addr, > + unsigned int mr_ext_memseg_en) > +{ > + struct mr_find_contig_memsegs_data data =3D {.addr =3D addr, }; > + struct mr_find_contig_memsegs_data data_re; > + const struct rte_memseg_list *msl; > + const struct rte_memseg *ms; > + struct mlx5_mr *mr =3D NULL; > + int ms_idx_shift =3D -1; > + uint32_t bmp_size; > + void *bmp_mem; > + uint32_t ms_n; > + uint32_t n; > + size_t len; > + > + DRV_LOG(DEBUG, "Creating a MR using address (%p)", (void *)addr); > + /* > + * Release detached MRs if any. This can't be called with holding > either > + * memory_hotplug_lock or share_cache->rwlock. MRs on the free list > have > + * been detached by the memory free event but it couldn't be > released > + * inside the callback due to deadlock. As a result, releasing resource= s > + * is quite opportunistic. > + */ > + mlx5_mr_garbage_collect(share_cache); > + /* > + * If enabled, find out a contiguous virtual address chunk in use, to > + * which the given address belongs, in order to register maximum > range. > + * In the best case where mempools are not dynamically recreated > and > + * '--socket-mem' is specified as an EAL option, it is very likely to > + * have only one MR(LKey) per a socket and per a hugepage-size even > + * though the system memory is highly fragmented. As the whole > memory > + * chunk will be pinned by kernel, it can't be reused unless entire > + * chunk is freed from EAL. > + * > + * If disabled, just register one memseg (page). Then, memory > + * consumption will be minimized but it may drop performance if > there > + * are many MRs to lookup on the datapath. > + */ > + if (!mr_ext_memseg_en) { > + data.msl =3D rte_mem_virt2memseg_list((void *)addr); > + data.start =3D RTE_ALIGN_FLOOR(addr, data.msl->page_sz); > + data.end =3D data.start + data.msl->page_sz; > + } else if (!rte_memseg_contig_walk(mr_find_contig_memsegs_cb, > &data)) { > + DRV_LOG(WARNING, > + "Unable to find virtually contiguous" > + " chunk for address (%p)." > + " rte_memseg_contig_walk() failed.", (void *)addr); > + rte_errno =3D ENXIO; > + goto err_nolock; > + } > +alloc_resources: > + /* Addresses must be page-aligned. */ > + MLX5_ASSERT(data.msl); > + MLX5_ASSERT(rte_is_aligned((void *)data.start, data.msl->page_sz)); > + MLX5_ASSERT(rte_is_aligned((void *)data.end, data.msl->page_sz)); > + msl =3D data.msl; > + ms =3D rte_mem_virt2memseg((void *)data.start, msl); > + len =3D data.end - data.start; > + MLX5_ASSERT(ms); > + MLX5_ASSERT(msl->page_sz =3D=3D ms->hugepage_sz); > + /* Number of memsegs in the range. */ > + ms_n =3D len / msl->page_sz; > + DEBUG("Extending %p to [0x%" PRIxPTR ", 0x%" PRIxPTR ")," > + " page_sz=3D0x%" PRIx64 ", ms_n=3D%u", > + (void *)addr, data.start, data.end, msl->page_sz, ms_n); > + /* Size of memory for bitmap. */ > + bmp_size =3D rte_bitmap_get_memory_footprint(ms_n); > + mr =3D rte_zmalloc_socket(NULL, > + RTE_ALIGN_CEIL(sizeof(*mr), > + RTE_CACHE_LINE_SIZE) + > + bmp_size, > + RTE_CACHE_LINE_SIZE, msl->socket_id); > + if (mr =3D=3D NULL) { > + DEBUG("Unable to allocate memory for a new MR of" > + " address (%p).", (void *)addr); > + rte_errno =3D ENOMEM; > + goto err_nolock; > + } > + mr->msl =3D msl; > + /* > + * Save the index of the first memseg and initialize memseg bitmap. > To > + * see if a memseg of ms_idx in the memseg-list is still valid, check: > + * rte_bitmap_get(mr->bmp, ms_idx - mr->ms_base_idx) > + */ > + mr->ms_base_idx =3D rte_fbarray_find_idx(&msl->memseg_arr, ms); > + bmp_mem =3D RTE_PTR_ALIGN_CEIL(mr + 1, RTE_CACHE_LINE_SIZE); > + mr->ms_bmp =3D rte_bitmap_init(ms_n, bmp_mem, bmp_size); > + if (mr->ms_bmp =3D=3D NULL) { > + DEBUG("Unable to initialize bitmap for a new MR of" > + " address (%p).", (void *)addr); > + rte_errno =3D EINVAL; > + goto err_nolock; > + } > + /* > + * Should recheck whether the extended contiguous chunk is still > valid. > + * Because memory_hotplug_lock can't be held if there's any memory > + * related calls in a critical path, resource allocation above can't be > + * locked. If the memory has been changed at this point, try again > with > + * just single page. If not, go on with the big chunk atomically from > + * here. > + */ > + rte_mcfg_mem_read_lock(); > + data_re =3D data; > + if (len > msl->page_sz && > + !rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data_re)) > { > + DEBUG("Unable to find virtually contiguous" > + " chunk for address (%p)." > + " rte_memseg_contig_walk() failed.", (void *)addr); > + rte_errno =3D ENXIO; > + goto err_memlock; > + } > + if (data.start !=3D data_re.start || data.end !=3D data_re.end) { > + /* > + * The extended contiguous chunk has been changed. Try > again > + * with single memseg instead. > + */ > + data.start =3D RTE_ALIGN_FLOOR(addr, msl->page_sz); > + data.end =3D data.start + msl->page_sz; > + rte_mcfg_mem_read_unlock(); > + mr_free(mr); > + goto alloc_resources; > + } > + MLX5_ASSERT(data.msl =3D=3D data_re.msl); > + rte_rwlock_write_lock(&share_cache->rwlock); > + /* > + * Check the address is really missing. If other thread already created > + * one or it is not found due to overflow, abort and return. > + */ > + if (mlx5_mr_lookup_cache(share_cache, entry, addr) !=3D UINT32_MAX) > { > + /* > + * Insert to the global cache table. It may fail due to > + * low-on-memory. Then, this entry will have to be searched > + * here again. > + */ > + mr_btree_insert(&share_cache->cache, entry); > + DEBUG("Found MR for %p on final lookup, abort", (void > *)addr); > + rte_rwlock_write_unlock(&share_cache->rwlock); > + rte_mcfg_mem_read_unlock(); > + /* > + * Must be unlocked before calling rte_free() because > + * mlx5_mr_mem_event_free_cb() can be called inside. > + */ > + mr_free(mr); > + return entry->lkey; > + } > + /* > + * Trim start and end addresses for verbs MR. Set bits for registering > + * memsegs but exclude already registered ones. Bitmap can be > + * fragmented. > + */ > + for (n =3D 0; n < ms_n; ++n) { > + uintptr_t start; > + struct mr_cache_entry ret; > + > + memset(&ret, 0, sizeof(ret)); > + start =3D data_re.start + n * msl->page_sz; > + /* Exclude memsegs already registered by other MRs. */ > + if (mlx5_mr_lookup_cache(share_cache, &ret, start) =3D=3D > + UINT32_MAX) { > + /* > + * Start from the first unregistered memseg in the > + * extended range. > + */ > + if (ms_idx_shift =3D=3D -1) { > + mr->ms_base_idx +=3D n; > + data.start =3D start; > + ms_idx_shift =3D n; > + } > + data.end =3D start + msl->page_sz; > + rte_bitmap_set(mr->ms_bmp, n - ms_idx_shift); > + ++mr->ms_n; > + } > + } > + len =3D data.end - data.start; > + mr->ms_bmp_n =3D len / msl->page_sz; > + MLX5_ASSERT(ms_idx_shift + mr->ms_bmp_n <=3D ms_n); > + /* > + * Finally create a verbs MR for the memory chunk. ibv_reg_mr() can > be > + * called with holding the memory lock because it doesn't use > + * mlx5_alloc_buf_extern() which eventually calls rte_malloc_socket() > + * through mlx5_alloc_verbs_buf(). > + */ > + mr->ibv_mr =3D mlx5_glue->reg_mr(pd, (void *)data.start, len, > + IBV_ACCESS_LOCAL_WRITE | > + IBV_ACCESS_RELAXED_ORDERING); > + if (mr->ibv_mr =3D=3D NULL) { > + DEBUG("Fail to create a verbs MR for address (%p)", > + (void *)addr); > + rte_errno =3D EINVAL; > + goto err_mrlock; > + } > + MLX5_ASSERT((uintptr_t)mr->ibv_mr->addr =3D=3D data.start); > + MLX5_ASSERT(mr->ibv_mr->length =3D=3D len); > + LIST_INSERT_HEAD(&share_cache->mr_list, mr, mr); > + DEBUG("MR CREATED (%p) for %p:\n" > + " [0x%" PRIxPTR ", 0x%" PRIxPTR ")," > + " lkey=3D0x%x base_idx=3D%u ms_n=3D%u, ms_bmp_n=3D%u", > + (void *)mr, (void *)addr, data.start, data.end, > + rte_cpu_to_be_32(mr->ibv_mr->lkey), > + mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n); > + /* Insert to the global cache table. */ > + mlx5_mr_insert_cache(share_cache, mr); > + /* Fill in output data. */ > + mlx5_mr_lookup_cache(share_cache, entry, addr); > + /* Lookup can't fail. */ > + MLX5_ASSERT(entry->lkey !=3D UINT32_MAX); > + rte_rwlock_write_unlock(&share_cache->rwlock); > + rte_mcfg_mem_read_unlock(); > + return entry->lkey; > +err_mrlock: > + rte_rwlock_write_unlock(&share_cache->rwlock); > +err_memlock: > + rte_mcfg_mem_read_unlock(); > +err_nolock: > + /* > + * In case of error, as this can be called in a datapath, a warning > + * message per an error is preferable instead. Must be unlocked > before > + * calling rte_free() because mlx5_mr_mem_event_free_cb() can be > called > + * inside. > + */ > + mr_free(mr); > + return UINT32_MAX; > +} > + > +/** > + * Create a new global Memory Region (MR) for a missing virtual address. > + * This can be called from primary and secondary process. > + * > + * @param pd > + * Pointer to ibv_pd of a device (net, regex, vdpa,...). > + * @param share_cache > + * Pointer to a global shared MR cache. > + * @param[out] entry > + * Pointer to returning MR cache entry, found in the global cache or n= ewly > + * created. If failed to create one, this will not be updated. > + * @param addr > + * Target virtual address to register. > + * > + * @return > + * Searched LKey on success, UINT32_MAX on failure and rte_errno is se= t. > + */ > +static uint32_t > +mlx5_mr_create(struct ibv_pd *pd, struct mlx5_mp_id *mp_id, > + struct mlx5_mr_share_cache *share_cache, > + struct mr_cache_entry *entry, uintptr_t addr, > + unsigned int mr_ext_memseg_en) > +{ > + uint32_t ret =3D 0; > + > + switch (rte_eal_process_type()) { > + case RTE_PROC_PRIMARY: > + ret =3D mlx5_mr_create_primary(pd, share_cache, entry, > + addr, mr_ext_memseg_en); > + break; > + case RTE_PROC_SECONDARY: > + ret =3D mlx5_mr_create_secondary(pd, mp_id, share_cache, > entry, > + addr, mr_ext_memseg_en); > + break; > + default: > + break; > + } > + return ret; > +} > + > +/** > + * Look up address in the global MR cache table. If not found, create a = new > MR. > + * Insert the found/created entry to local bottom-half cache table. > + * > + * @param pd > + * Pointer to ibv_pd of a device (net, regex, vdpa,...). > + * @param share_cache > + * Pointer to a global shared MR cache. > + * @param mr_ctrl > + * Pointer to per-queue MR control structure. > + * @param[out] entry > + * Pointer to returning MR cache entry, found in the global cache or n= ewly > + * created. If failed to create one, this is not written. > + * @param addr > + * Search key. > + * > + * @return > + * Searched LKey on success, UINT32_MAX on no match. > + */ > +static uint32_t > +mr_lookup_caches(struct ibv_pd *pd, struct mlx5_mp_id *mp_id, > + struct mlx5_mr_share_cache *share_cache, > + struct mlx5_mr_ctrl *mr_ctrl, > + struct mr_cache_entry *entry, uintptr_t addr, > + unsigned int mr_ext_memseg_en) > +{ > + struct mlx5_mr_btree *bt =3D &mr_ctrl->cache_bh; > + uint32_t lkey; > + uint16_t idx; > + > + /* If local cache table is full, try to double it. */ > + if (unlikely(bt->len =3D=3D bt->size)) > + mr_btree_expand(bt, bt->size << 1); > + /* Look up in the global cache. */ > + rte_rwlock_read_lock(&share_cache->rwlock); > + lkey =3D mr_btree_lookup(&share_cache->cache, &idx, addr); > + if (lkey !=3D UINT32_MAX) { > + /* Found. */ > + *entry =3D (*share_cache->cache.table)[idx]; > + rte_rwlock_read_unlock(&share_cache->rwlock); > + /* > + * Update local cache. Even if it fails, return the found entry > + * to update top-half cache. Next time, this entry will be > found > + * in the global cache. > + */ > + mr_btree_insert(bt, entry); > + return lkey; > + } > + rte_rwlock_read_unlock(&share_cache->rwlock); > + /* First time to see the address? Create a new MR. */ > + lkey =3D mlx5_mr_create(pd, mp_id, share_cache, entry, addr, > + mr_ext_memseg_en); > + /* > + * Update the local cache if successfully created a new global MR. > Even > + * if failed to create one, there's no action to take in this datapath > + * code. As returning LKey is invalid, this will eventually make HW > + * fail. > + */ > + if (lkey !=3D UINT32_MAX) > + mr_btree_insert(bt, entry); > + return lkey; > +} > + > +/** > + * Bottom-half of LKey search on datapath. First search in cache_bh[] an= d if > + * misses, search in the global MR cache table and update the new entry = to > + * per-queue local caches. > + * > + * @param pd > + * Pointer to ibv_pd of a device (net, regex, vdpa,...). > + * @param share_cache > + * Pointer to a global shared MR cache. > + * @param mr_ctrl > + * Pointer to per-queue MR control structure. > + * @param addr > + * Search key. > + * > + * @return > + * Searched LKey on success, UINT32_MAX on no match. > + */ > +uint32_t mlx5_mr_addr2mr_bh(struct ibv_pd *pd, struct mlx5_mp_id > *mp_id, > + struct mlx5_mr_share_cache *share_cache, > + struct mlx5_mr_ctrl *mr_ctrl, > + uintptr_t addr, unsigned int mr_ext_memseg_en) > +{ > + uint32_t lkey; > + uint16_t bh_idx =3D 0; > + /* Victim in top-half cache to replace with new entry. */ > + struct mr_cache_entry *repl =3D &mr_ctrl->cache[mr_ctrl->head]; > + > + /* Binary-search MR translation table. */ > + lkey =3D mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr); > + /* Update top-half cache. */ > + if (likely(lkey !=3D UINT32_MAX)) { > + *repl =3D (*mr_ctrl->cache_bh.table)[bh_idx]; > + } else { > + /* > + * If missed in local lookup table, search in the global cache > + * and local cache_bh[] will be updated inside if possible. > + * Top-half cache entry will also be updated. > + */ > + lkey =3D mr_lookup_caches(pd, mp_id, share_cache, mr_ctrl, > + repl, addr, mr_ext_memseg_en); > + if (unlikely(lkey =3D=3D UINT32_MAX)) > + return UINT32_MAX; > + } > + /* Update the most recently used entry. */ > + mr_ctrl->mru =3D mr_ctrl->head; > + /* Point to the next victim, the oldest. */ > + mr_ctrl->head =3D (mr_ctrl->head + 1) % MLX5_MR_CACHE_N; > + return lkey; > +} > + > +/** > + * Release all the created MRs and resources on global MR cache of a dev= ice. > + * list. > + * > + * @param share_cache > + * Pointer to a global shared MR cache. > + */ > +void > +mlx5_mr_release_cache(struct mlx5_mr_share_cache *share_cache) > +{ > + struct mlx5_mr *mr_next; > + > + rte_rwlock_write_lock(&share_cache->rwlock); > + /* Detach from MR list and move to free list. */ > + mr_next =3D LIST_FIRST(&share_cache->mr_list); > + while (mr_next !=3D NULL) { > + struct mlx5_mr *mr =3D mr_next; > + > + mr_next =3D LIST_NEXT(mr, mr); > + LIST_REMOVE(mr, mr); > + LIST_INSERT_HEAD(&share_cache->mr_free_list, mr, mr); > + } > + LIST_INIT(&share_cache->mr_list); > + /* Free global cache. */ > + mlx5_mr_btree_free(&share_cache->cache); > + rte_rwlock_write_unlock(&share_cache->rwlock); > + /* Free all remaining MRs. */ > + mlx5_mr_garbage_collect(share_cache); > +} > + > +/** > + * Flush all of the local cache entries. > + * > + * @param mr_ctrl > + * Pointer to per-queue MR local cache. > + */ > +void > +mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl) > +{ > + /* Reset the most-recently-used index. */ > + mr_ctrl->mru =3D 0; > + /* Reset the linear search array. */ > + mr_ctrl->head =3D 0; > + memset(mr_ctrl->cache, 0, sizeof(mr_ctrl->cache)); > + /* Reset the B-tree table. */ > + mr_ctrl->cache_bh.len =3D 1; > + mr_ctrl->cache_bh.overflow =3D 0; > + /* Update the generation number. */ > + mr_ctrl->cur_gen =3D *mr_ctrl->dev_gen_ptr; > + DRV_LOG(DEBUG, "mr_ctrl(%p): flushed, cur_gen=3D%d", > + (void *)mr_ctrl, mr_ctrl->cur_gen); > +} > + > +/** > + * Creates a memory region for external memory, that is memory which is > not > + * part of the DPDK memory segments. > + * > + * @param pd > + * Pointer to ibv_pd of a device (net, regex, vdpa,...). > + * @param addr > + * Starting virtual address of memory. > + * @param len > + * Length of memory segment being mapped. > + * @param socked_id > + * Socket to allocate heap memory for the control structures. > + * > + * @return > + * Pointer to MR structure on success, NULL otherwise. > + */ > +struct mlx5_mr * > +mlx5_create_mr_ext(struct ibv_pd *pd, uintptr_t addr, size_t len, int > socket_id) > +{ > + struct mlx5_mr *mr =3D NULL; > + > + mr =3D rte_zmalloc_socket(NULL, > + RTE_ALIGN_CEIL(sizeof(*mr), > + RTE_CACHE_LINE_SIZE), > + RTE_CACHE_LINE_SIZE, socket_id); > + if (mr =3D=3D NULL) > + return NULL; > + mr->ibv_mr =3D mlx5_glue->reg_mr(pd, (void *)addr, len, > + IBV_ACCESS_LOCAL_WRITE | > + IBV_ACCESS_RELAXED_ORDERING); > + if (mr->ibv_mr =3D=3D NULL) { > + DRV_LOG(WARNING, > + "Fail to create a verbs MR for address (%p)", > + (void *)addr); > + rte_free(mr); > + return NULL; > + } > + mr->msl =3D NULL; /* Mark it is external memory. */ > + mr->ms_bmp =3D NULL; > + mr->ms_n =3D 1; > + mr->ms_bmp_n =3D 1; > + DRV_LOG(DEBUG, > + "MR CREATED (%p) for external memory %p:\n" > + " [0x%" PRIxPTR ", 0x%" PRIxPTR ")," > + " lkey=3D0x%x base_idx=3D%u ms_n=3D%u, ms_bmp_n=3D%u", > + (void *)mr, (void *)addr, > + addr, addr + len, rte_cpu_to_be_32(mr->ibv_mr->lkey), > + mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n); > + return mr; > +} > + > +/** > + * Dump all the created MRs and the global cache entries. > + * > + * @param sh > + * Pointer to Ethernet device shared context. > + */ > +void > +mlx5_mr_dump_cache(struct mlx5_mr_share_cache *share_cache > __rte_unused) > +{ > +#ifdef RTE_LIBRTE_MLX5_DEBUG > + struct mlx5_mr *mr; > + int mr_n =3D 0; > + int chunk_n =3D 0; > + > + rte_rwlock_read_lock(&share_cache->rwlock); > + /* Iterate all the existing MRs. */ > + LIST_FOREACH(mr, &share_cache->mr_list, mr) { > + unsigned int n; > + > + DEBUG("MR[%u], LKey =3D 0x%x, ms_n =3D %u, ms_bmp_n =3D %u", > + mr_n++, rte_cpu_to_be_32(mr->ibv_mr->lkey), > + mr->ms_n, mr->ms_bmp_n); > + if (mr->ms_n =3D=3D 0) > + continue; > + for (n =3D 0; n < mr->ms_bmp_n; ) { > + struct mr_cache_entry ret =3D { 0, }; > + > + n =3D mr_find_next_chunk(mr, &ret, n); > + if (!ret.end) > + break; > + DEBUG(" chunk[%u], [0x%" PRIxPTR ", 0x%" PRIxPTR > ")", > + chunk_n++, ret.start, ret.end); > + } > + } > + DEBUG("Dumping global cache %p", (void *)share_cache); > + mlx5_mr_btree_dump(&share_cache->cache); > + rte_rwlock_read_unlock(&share_cache->rwlock); > +#endif > +} > diff --git a/drivers/common/mlx5/mlx5_common_mr.h > b/drivers/common/mlx5/mlx5_common_mr.h > new file mode 100644 > index 0000000000..e805f96375 > --- /dev/null > +++ b/drivers/common/mlx5/mlx5_common_mr.h > @@ -0,0 +1,160 @@ > +/* SPDX-License-Identifier: BSD-3-Clause > + * Copyright 2018 6WIND S.A. > + * Copyright 2018 Mellanox Technologies, Ltd > + */ > + > +#ifndef RTE_PMD_MLX5_COMMON_MR_H_ > +#define RTE_PMD_MLX5_COMMON_MR_H_ > + > +#include > +#include > +#include > + > +/* Verbs header. */ > +/* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */ > +#ifdef PEDANTIC > +#pragma GCC diagnostic ignored "-Wpedantic" > +#endif > +#include > +#include > +#ifdef PEDANTIC > +#pragma GCC diagnostic error "-Wpedantic" > +#endif > + > +#include > +#include > +#include > + > +#include "mlx5_common_mp.h" > + > +/* Size of per-queue MR cache array for linear search. */ > +#define MLX5_MR_CACHE_N 8 > +#define MLX5_MR_BTREE_CACHE_N 256 > + > +/* Memory Region object. */ > +struct mlx5_mr { > + LIST_ENTRY(mlx5_mr) mr; /**< Pointer to the prev/next entry. */ > + struct ibv_mr *ibv_mr; /* Verbs Memory Region. */ > + const struct rte_memseg_list *msl; > + int ms_base_idx; /* Start index of msl->memseg_arr[]. */ > + int ms_n; /* Number of memsegs in use. */ > + uint32_t ms_bmp_n; /* Number of bits in memsegs bit-mask. */ > + struct rte_bitmap *ms_bmp; /* Bit-mask of memsegs belonged to MR. > */ > +}; > + > +/* Cache entry for Memory Region. */ > +struct mr_cache_entry { > + uintptr_t start; /* Start address of MR. */ > + uintptr_t end; /* End address of MR. */ > + uint32_t lkey; /* rte_cpu_to_be_32(ibv_mr->lkey). */ > +} __rte_packed; > + > +/* MR Cache table for Binary search. */ > +struct mlx5_mr_btree { > + uint16_t len; /* Number of entries. */ > + uint16_t size; /* Total number of entries. */ > + int overflow; /* Mark failure of table expansion. */ > + struct mr_cache_entry (*table)[]; > +} __rte_packed; > + > +/* Per-queue MR control descriptor. */ > +struct mlx5_mr_ctrl { > + uint32_t *dev_gen_ptr; /* Generation number of device to poll. */ > + uint32_t cur_gen; /* Generation number saved to flush caches. */ > + uint16_t mru; /* Index of last hit entry in top-half cache. */ > + uint16_t head; /* Index of the oldest entry in top-half cache. */ > + struct mr_cache_entry cache[MLX5_MR_CACHE_N]; /* Cache for top- > half. */ > + struct mlx5_mr_btree cache_bh; /* Cache for bottom-half. */ > +} __rte_packed; > + > +LIST_HEAD(mlx5_mr_list, mlx5_mr); > + > +/* Global per-device MR cache. */ > +struct mlx5_mr_share_cache { > + uint32_t dev_gen; /* Generation number to flush local caches. */ > + rte_rwlock_t rwlock; /* MR cache Lock. */ > + struct mlx5_mr_btree cache; /* Global MR cache table. */ > + struct mlx5_mr_list mr_list; /* Registered MR list. */ > + struct mlx5_mr_list mr_free_list; /* Freed MR list. */ > +} __rte_packed; > + > +/** > + * Look up LKey from given lookup table by linear search. Firstly look u= p the > + * last-hit entry. If miss, the entire array is searched. If found, upda= te the > + * last-hit index and return LKey. > + * > + * @param lkp_tbl > + * Pointer to lookup table. > + * @param[in,out] cached_idx > + * Pointer to last-hit index. > + * @param n > + * Size of lookup table. > + * @param addr > + * Search key. > + * > + * @return > + * Searched LKey on success, UINT32_MAX on no match. > + */ > +static __rte_always_inline uint32_t > +mlx5_mr_lookup_lkey(struct mr_cache_entry *lkp_tbl, uint16_t *cached_idx= , > + uint16_t n, uintptr_t addr) > +{ > + uint16_t idx; > + > + if (likely(addr >=3D lkp_tbl[*cached_idx].start && > + addr < lkp_tbl[*cached_idx].end)) > + return lkp_tbl[*cached_idx].lkey; > + for (idx =3D 0; idx < n && lkp_tbl[idx].start !=3D 0; ++idx) { > + if (addr >=3D lkp_tbl[idx].start && > + addr < lkp_tbl[idx].end) { > + /* Found. */ > + *cached_idx =3D idx; > + return lkp_tbl[idx].lkey; > + } > + } > + return UINT32_MAX; > +} > + > +__rte_experimental > +int mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket); > +__rte_experimental > +void mlx5_mr_btree_free(struct mlx5_mr_btree *bt); > +__rte_experimental > +void mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused); > +__rte_experimental > +uint32_t mlx5_mr_addr2mr_bh(struct ibv_pd *pd, struct mlx5_mp_id > *mp_id, > + struct mlx5_mr_share_cache *share_cache, > + struct mlx5_mr_ctrl *mr_ctrl, > + uintptr_t addr, unsigned int mr_ext_memseg_en); > +__rte_experimental > +void mlx5_mr_release_cache(struct mlx5_mr_share_cache *mr_cache); > +__rte_experimental > +void mlx5_mr_dump_cache(struct mlx5_mr_share_cache *share_cache > __rte_unused); > +__rte_experimental > +void mlx5_mr_rebuild_cache(struct mlx5_mr_share_cache *share_cache); > +__rte_experimental > +void mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl); > +__rte_experimental > +int > +mlx5_mr_insert_cache(struct mlx5_mr_share_cache *share_cache, > + struct mlx5_mr *mr); > +__rte_experimental > +uint32_t > +mlx5_mr_lookup_cache(struct mlx5_mr_share_cache *share_cache, > + struct mr_cache_entry *entry, uintptr_t addr); > +__rte_experimental > +struct mlx5_mr * > +mlx5_mr_lookup_list(struct mlx5_mr_share_cache *share_cache, > + struct mr_cache_entry *entry, uintptr_t addr); > +__rte_experimental > +struct mlx5_mr * > +mlx5_create_mr_ext(struct ibv_pd *pd, uintptr_t addr, size_t len, > + int socket_id); > +__rte_experimental > +uint32_t > +mlx5_mr_create_primary(struct ibv_pd *pd, > + struct mlx5_mr_share_cache *share_cache, > + struct mr_cache_entry *entry, uintptr_t addr, > + unsigned int mr_ext_memseg_en); > + > +#endif /* RTE_PMD_MLX5_COMMON_MR_H_ */ > diff --git a/drivers/common/mlx5/rte_common_mlx5_version.map > b/drivers/common/mlx5/rte_common_mlx5_version.map > index 265703d1c9..b58a378278 100644 > --- a/drivers/common/mlx5/rte_common_mlx5_version.map > +++ b/drivers/common/mlx5/rte_common_mlx5_version.map > @@ -61,4 +61,18 @@ EXPERIMENTAL { > mlx5_mp_req_mr_create; > mlx5_mp_req_queue_state_modify; > mlx5_mp_req_verbs_cmd_fd; > + > + mlx5_mr_btree_init; > + mlx5_mr_btree_free; > + mlx5_mr_btree_dump; > + mlx5_mr_addr2mr_bh; > + mlx5_mr_release_cache; > + mlx5_mr_dump_cache; > + mlx5_mr_rebuild_cache; > + mlx5_mr_insert_cache; > + mlx5_mr_lookup_cache; > + mlx5_mr_lookup_list; > + mlx5_create_mr_ext; > + mlx5_mr_create_primary; > + mlx5_mr_flush_local_cache; > }; > diff --git a/drivers/net/mlx5/mlx5.c b/drivers/net/mlx5/mlx5.c > index d87c384422..f8b134ca66 100644 > --- a/drivers/net/mlx5/mlx5.c > +++ b/drivers/net/mlx5/mlx5.c > @@ -623,7 +623,7 @@ mlx5_alloc_shared_ibctx(const struct > mlx5_dev_spawn_data *spawn, > * At this point the device is not added to the memory > * event list yet, context is just being created. > */ > - err =3D mlx5_mr_btree_init(&sh->mr.cache, > + err =3D mlx5_mr_btree_init(&sh->share_cache.cache, > MLX5_MR_BTREE_CACHE_N * 2, > spawn->pci_dev->device.numa_node); > if (err) { > @@ -695,7 +695,7 @@ mlx5_free_shared_ibctx(struct mlx5_ibv_shared *sh) > LIST_REMOVE(sh, mem_event_cb); > rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock); > /* Release created Memory Regions. */ > - mlx5_mr_release(sh); > + mlx5_mr_release_cache(&sh->share_cache); > /* Remove context from the global device list. */ > LIST_REMOVE(sh, next); > /* > diff --git a/drivers/net/mlx5/mlx5.h b/drivers/net/mlx5/mlx5.h > index e9d5868883..c45c01e916 100644 > --- a/drivers/net/mlx5/mlx5.h > +++ b/drivers/net/mlx5/mlx5.h > @@ -37,10 +37,10 @@ > #include > #include > #include > +#include >=20 > #include "mlx5_defs.h" > #include "mlx5_utils.h" > -#include "mlx5_mr.h" > #include "mlx5_autoconf.h" >=20 > /** Key string for IPC. */ > @@ -199,8 +199,6 @@ struct mlx5_verbs_alloc_ctx { > const void *obj; /* Pointer to the DPDK object. */ > }; >=20 > -LIST_HEAD(mlx5_mr_list, mlx5_mr); > - > /* Flow drop context necessary due to Verbs API. */ > struct mlx5_drop { > struct mlx5_hrxq *hrxq; /* Hash Rx queue queue. */ > @@ -411,13 +409,7 @@ struct mlx5_ibv_shared { > struct ibv_device_attr_ex device_attr; /* Device properties. */ > LIST_ENTRY(mlx5_ibv_shared) mem_event_cb; > /**< Called by memory event callback. */ > - struct { > - uint32_t dev_gen; /* Generation number to flush local > caches. */ > - rte_rwlock_t rwlock; /* MR Lock. */ > - struct mlx5_mr_btree cache; /* Global MR cache table. */ > - struct mlx5_mr_list mr_list; /* Registered MR list. */ > - struct mlx5_mr_list mr_free_list; /* Freed MR list. */ > - } mr; > + struct mlx5_mr_share_cache share_cache; > /* Shared DV/DR flow data section. */ > pthread_mutex_t dv_mutex; /* DV context mutex. */ > uint32_t dv_meta_mask; /* flow META metadata supported mask. */ > diff --git a/drivers/net/mlx5/mlx5_mp.c b/drivers/net/mlx5/mlx5_mp.c > index 43684dbc3a..7ad322d474 100644 > --- a/drivers/net/mlx5/mlx5_mp.c > +++ b/drivers/net/mlx5/mlx5_mp.c > @@ -11,6 +11,7 @@ > #include >=20 > #include > +#include >=20 > #include "mlx5.h" > #include "mlx5_rxtx.h" > @@ -25,7 +26,7 @@ mlx5_mp_primary_handle(const struct rte_mp_msg > *mp_msg, const void *peer) > (const struct mlx5_mp_param *)mp_msg->param; > struct rte_eth_dev *dev; > struct mlx5_priv *priv; > - struct mlx5_mr_cache entry; > + struct mr_cache_entry entry; > uint32_t lkey; > int ret; >=20 > @@ -40,7 +41,10 @@ mlx5_mp_primary_handle(const struct rte_mp_msg > *mp_msg, const void *peer) > switch (param->type) { > case MLX5_MP_REQ_CREATE_MR: > mp_init_msg(&priv->mp_id, &mp_res, param->type); > - lkey =3D mlx5_mr_create_primary(dev, &entry, param- > >args.addr); > + lkey =3D mlx5_mr_create_primary(priv->sh->pd, > + &priv->sh->share_cache, > + &entry, param->args.addr, > + priv->config.mr_ext_memseg_en); > if (lkey =3D=3D UINT32_MAX) > res->result =3D -rte_errno; > ret =3D rte_mp_reply(&mp_res, peer); > diff --git a/drivers/net/mlx5/mlx5_mr.c b/drivers/net/mlx5/mlx5_mr.c > index 9151992a72..2b4b3e2891 100644 > --- a/drivers/net/mlx5/mlx5_mr.c > +++ b/drivers/net/mlx5/mlx5_mr.c > @@ -18,6 +18,8 @@ > #include >=20 > #include > +#include > +#include >=20 > #include "mlx5.h" > #include "mlx5_mr.h" > @@ -36,834 +38,6 @@ struct mr_update_mp_data { > int ret; > }; >=20 > -/** > - * Expand B-tree table to a given size. Can't be called with holding > - * memory_hotplug_lock or sh->mr.rwlock due to rte_realloc(). > - * > - * @param bt > - * Pointer to B-tree structure. > - * @param n > - * Number of entries for expansion. > - * > - * @return > - * 0 on success, -1 on failure. > - */ > -static int > -mr_btree_expand(struct mlx5_mr_btree *bt, int n) > -{ > - void *mem; > - int ret =3D 0; > - > - if (n <=3D bt->size) > - return ret; > - /* > - * Downside of directly using rte_realloc() is that SOCKET_ID_ANY is > - * used inside if there's no room to expand. Because this is a quite > - * rare case and a part of very slow path, it is very acceptable. > - * Initially cache_bh[] will be given practically enough space and once > - * it is expanded, expansion wouldn't be needed again ever. > - */ > - mem =3D rte_realloc(bt->table, n * sizeof(struct mlx5_mr_cache), 0); > - if (mem =3D=3D NULL) { > - /* Not an error, B-tree search will be skipped. */ > - DRV_LOG(WARNING, "failed to expand MR B-tree (%p) table", > - (void *)bt); > - ret =3D -1; > - } else { > - DRV_LOG(DEBUG, "expanded MR B-tree table (size=3D%u)", n); > - bt->table =3D mem; > - bt->size =3D n; > - } > - return ret; > -} > - > -/** > - * Look up LKey from given B-tree lookup table, store the last index and > return > - * searched LKey. > - * > - * @param bt > - * Pointer to B-tree structure. > - * @param[out] idx > - * Pointer to index. Even on search failure, returns index where it st= ops > - * searching so that index can be used when inserting a new entry. > - * @param addr > - * Search key. > - * > - * @return > - * Searched LKey on success, UINT32_MAX on no match. > - */ > -static uint32_t > -mr_btree_lookup(struct mlx5_mr_btree *bt, uint16_t *idx, uintptr_t addr) > -{ > - struct mlx5_mr_cache *lkp_tbl; > - uint16_t n; > - uint16_t base =3D 0; > - > - MLX5_ASSERT(bt !=3D NULL); > - lkp_tbl =3D *bt->table; > - n =3D bt->len; > - /* First entry must be NULL for comparison. */ > - MLX5_ASSERT(bt->len > 0 || (lkp_tbl[0].start =3D=3D 0 && > - lkp_tbl[0].lkey =3D=3D UINT32_MAX)); > - /* Binary search. */ > - do { > - register uint16_t delta =3D n >> 1; > - > - if (addr < lkp_tbl[base + delta].start) { > - n =3D delta; > - } else { > - base +=3D delta; > - n -=3D delta; > - } > - } while (n > 1); > - MLX5_ASSERT(addr >=3D lkp_tbl[base].start); > - *idx =3D base; > - if (addr < lkp_tbl[base].end) > - return lkp_tbl[base].lkey; > - /* Not found. */ > - return UINT32_MAX; > -} > - > -/** > - * Insert an entry to B-tree lookup table. > - * > - * @param bt > - * Pointer to B-tree structure. > - * @param entry > - * Pointer to new entry to insert. > - * > - * @return > - * 0 on success, -1 on failure. > - */ > -static int > -mr_btree_insert(struct mlx5_mr_btree *bt, struct mlx5_mr_cache *entry) > -{ > - struct mlx5_mr_cache *lkp_tbl; > - uint16_t idx =3D 0; > - size_t shift; > - > - MLX5_ASSERT(bt !=3D NULL); > - MLX5_ASSERT(bt->len <=3D bt->size); > - MLX5_ASSERT(bt->len > 0); > - lkp_tbl =3D *bt->table; > - /* Find out the slot for insertion. */ > - if (mr_btree_lookup(bt, &idx, entry->start) !=3D UINT32_MAX) { > - DRV_LOG(DEBUG, > - "abort insertion to B-tree(%p): already exist at" > - " idx=3D%u [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=3D0x%x", > - (void *)bt, idx, entry->start, entry->end, entry->lkey); > - /* Already exist, return. */ > - return 0; > - } > - /* If table is full, return error. */ > - if (unlikely(bt->len =3D=3D bt->size)) { > - bt->overflow =3D 1; > - return -1; > - } > - /* Insert entry. */ > - ++idx; > - shift =3D (bt->len - idx) * sizeof(struct mlx5_mr_cache); > - if (shift) > - memmove(&lkp_tbl[idx + 1], &lkp_tbl[idx], shift); > - lkp_tbl[idx] =3D *entry; > - bt->len++; > - DRV_LOG(DEBUG, > - "inserted B-tree(%p)[%u]," > - " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=3D0x%x", > - (void *)bt, idx, entry->start, entry->end, entry->lkey); > - return 0; > -} > - > -/** > - * Initialize B-tree and allocate memory for lookup table. > - * > - * @param bt > - * Pointer to B-tree structure. > - * @param n > - * Number of entries to allocate. > - * @param socket > - * NUMA socket on which memory must be allocated. > - * > - * @return > - * 0 on success, a negative errno value otherwise and rte_errno is set= . > - */ > -int > -mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket) > -{ > - if (bt =3D=3D NULL) { > - rte_errno =3D EINVAL; > - return -rte_errno; > - } > - MLX5_ASSERT(!bt->table && !bt->size); > - memset(bt, 0, sizeof(*bt)); > - bt->table =3D rte_calloc_socket("B-tree table", > - n, sizeof(struct mlx5_mr_cache), > - 0, socket); > - if (bt->table =3D=3D NULL) { > - rte_errno =3D ENOMEM; > - DEBUG("failed to allocate memory for btree cache on socket > %d", > - socket); > - return -rte_errno; > - } > - bt->size =3D n; > - /* First entry must be NULL for binary search. */ > - (*bt->table)[bt->len++] =3D (struct mlx5_mr_cache) { > - .lkey =3D UINT32_MAX, > - }; > - DEBUG("initialized B-tree %p with table %p", > - (void *)bt, (void *)bt->table); > - return 0; > -} > - > -/** > - * Free B-tree resources. > - * > - * @param bt > - * Pointer to B-tree structure. > - */ > -void > -mlx5_mr_btree_free(struct mlx5_mr_btree *bt) > -{ > - if (bt =3D=3D NULL) > - return; > - DEBUG("freeing B-tree %p with table %p", > - (void *)bt, (void *)bt->table); > - rte_free(bt->table); > - memset(bt, 0, sizeof(*bt)); > -} > - > -/** > - * Dump all the entries in a B-tree > - * > - * @param bt > - * Pointer to B-tree structure. > - */ > -void > -mlx5_mr_btree_dump(struct mlx5_mr_btree *bt __rte_unused) > -{ > -#ifdef RTE_LIBRTE_MLX5_DEBUG > - int idx; > - struct mlx5_mr_cache *lkp_tbl; > - > - if (bt =3D=3D NULL) > - return; > - lkp_tbl =3D *bt->table; > - for (idx =3D 0; idx < bt->len; ++idx) { > - struct mlx5_mr_cache *entry =3D &lkp_tbl[idx]; > - > - DEBUG("B-tree(%p)[%u]," > - " [0x%" PRIxPTR ", 0x%" PRIxPTR ") lkey=3D0x%x", > - (void *)bt, idx, entry->start, entry->end, entry->lkey); > - } > -#endif > -} > - > -/** > - * Find virtually contiguous memory chunk in a given MR. > - * > - * @param dev > - * Pointer to MR structure. > - * @param[out] entry > - * Pointer to returning MR cache entry. If not found, this will not be > - * updated. > - * @param start_idx > - * Start index of the memseg bitmap. > - * > - * @return > - * Next index to go on lookup. > - */ > -static int > -mr_find_next_chunk(struct mlx5_mr *mr, struct mlx5_mr_cache *entry, > - int base_idx) > -{ > - uintptr_t start =3D 0; > - uintptr_t end =3D 0; > - uint32_t idx =3D 0; > - > - /* MR for external memory doesn't have memseg list. */ > - if (mr->msl =3D=3D NULL) { > - struct ibv_mr *ibv_mr =3D mr->ibv_mr; > - > - MLX5_ASSERT(mr->ms_bmp_n =3D=3D 1); > - MLX5_ASSERT(mr->ms_n =3D=3D 1); > - MLX5_ASSERT(base_idx =3D=3D 0); > - /* > - * Can't search it from memseg list but get it directly from > - * verbs MR as there's only one chunk. > - */ > - entry->start =3D (uintptr_t)ibv_mr->addr; > - entry->end =3D (uintptr_t)ibv_mr->addr + mr->ibv_mr->length; > - entry->lkey =3D rte_cpu_to_be_32(mr->ibv_mr->lkey); > - /* Returning 1 ends iteration. */ > - return 1; > - } > - for (idx =3D base_idx; idx < mr->ms_bmp_n; ++idx) { > - if (rte_bitmap_get(mr->ms_bmp, idx)) { > - const struct rte_memseg_list *msl; > - const struct rte_memseg *ms; > - > - msl =3D mr->msl; > - ms =3D rte_fbarray_get(&msl->memseg_arr, > - mr->ms_base_idx + idx); > - MLX5_ASSERT(msl->page_sz =3D=3D ms->hugepage_sz); > - if (!start) > - start =3D ms->addr_64; > - end =3D ms->addr_64 + ms->hugepage_sz; > - } else if (start) { > - /* Passed the end of a fragment. */ > - break; > - } > - } > - if (start) { > - /* Found one chunk. */ > - entry->start =3D start; > - entry->end =3D end; > - entry->lkey =3D rte_cpu_to_be_32(mr->ibv_mr->lkey); > - } > - return idx; > -} > - > -/** > - * Insert a MR to the global B-tree cache. It may fail due to low-on-mem= ory. > - * Then, this entry will have to be searched by mr_lookup_dev_list() in > - * mlx5_mr_create() on miss. > - * > - * @param dev > - * Pointer to Ethernet device shared context. > - * @param mr > - * Pointer to MR to insert. > - * > - * @return > - * 0 on success, -1 on failure. > - */ > -static int > -mr_insert_dev_cache(struct mlx5_ibv_shared *sh, struct mlx5_mr *mr) > -{ > - unsigned int n; > - > - DRV_LOG(DEBUG, "device %s inserting MR(%p) to global cache", > - sh->ibdev_name, (void *)mr); > - for (n =3D 0; n < mr->ms_bmp_n; ) { > - struct mlx5_mr_cache entry; > - > - memset(&entry, 0, sizeof(entry)); > - /* Find a contiguous chunk and advance the index. */ > - n =3D mr_find_next_chunk(mr, &entry, n); > - if (!entry.end) > - break; > - if (mr_btree_insert(&sh->mr.cache, &entry) < 0) { > - /* > - * Overflowed, but the global table cannot be > expanded > - * because of deadlock. > - */ > - return -1; > - } > - } > - return 0; > -} > - > -/** > - * Look up address in the original global MR list. > - * > - * @param sh > - * Pointer to Ethernet device shared context. > - * @param[out] entry > - * Pointer to returning MR cache entry. If no match, this will not be > updated. > - * @param addr > - * Search key. > - * > - * @return > - * Found MR on match, NULL otherwise. > - */ > -static struct mlx5_mr * > -mr_lookup_dev_list(struct mlx5_ibv_shared *sh, struct mlx5_mr_cache > *entry, > - uintptr_t addr) > -{ > - struct mlx5_mr *mr; > - > - /* Iterate all the existing MRs. */ > - LIST_FOREACH(mr, &sh->mr.mr_list, mr) { > - unsigned int n; > - > - if (mr->ms_n =3D=3D 0) > - continue; > - for (n =3D 0; n < mr->ms_bmp_n; ) { > - struct mlx5_mr_cache ret; > - > - memset(&ret, 0, sizeof(ret)); > - n =3D mr_find_next_chunk(mr, &ret, n); > - if (addr >=3D ret.start && addr < ret.end) { > - /* Found. */ > - *entry =3D ret; > - return mr; > - } > - } > - } > - return NULL; > -} > - > -/** > - * Look up address on device. > - * > - * @param dev > - * Pointer to Ethernet device shared context. > - * @param[out] entry > - * Pointer to returning MR cache entry. If no match, this will not be > updated. > - * @param addr > - * Search key. > - * > - * @return > - * Searched LKey on success, UINT32_MAX on failure and rte_errno is se= t. > - */ > -static uint32_t > -mr_lookup_dev(struct mlx5_ibv_shared *sh, struct mlx5_mr_cache *entry, > - uintptr_t addr) > -{ > - uint16_t idx; > - uint32_t lkey =3D UINT32_MAX; > - struct mlx5_mr *mr; > - > - /* > - * If the global cache has overflowed since it failed to expand the > - * B-tree table, it can't have all the existing MRs. Then, the address > - * has to be searched by traversing the original MR list instead, which > - * is very slow path. Otherwise, the global cache is all inclusive. > - */ > - if (!unlikely(sh->mr.cache.overflow)) { > - lkey =3D mr_btree_lookup(&sh->mr.cache, &idx, addr); > - if (lkey !=3D UINT32_MAX) > - *entry =3D (*sh->mr.cache.table)[idx]; > - } else { > - /* Falling back to the slowest path. */ > - mr =3D mr_lookup_dev_list(sh, entry, addr); > - if (mr !=3D NULL) > - lkey =3D entry->lkey; > - } > - MLX5_ASSERT(lkey =3D=3D UINT32_MAX || (addr >=3D entry->start && > - addr < entry->end)); > - return lkey; > -} > - > -/** > - * Free MR resources. MR lock must not be held to avoid a deadlock. > rte_free() > - * can raise memory free event and the callback function will spin on th= e > lock. > - * > - * @param mr > - * Pointer to MR to free. > - */ > -static void > -mr_free(struct mlx5_mr *mr) > -{ > - if (mr =3D=3D NULL) > - return; > - DRV_LOG(DEBUG, "freeing MR(%p):", (void *)mr); > - if (mr->ibv_mr !=3D NULL) > - claim_zero(mlx5_glue->dereg_mr(mr->ibv_mr)); > - if (mr->ms_bmp !=3D NULL) > - rte_bitmap_free(mr->ms_bmp); > - rte_free(mr); > -} > - > -/** > - * Release resources of detached MR having no online entry. > - * > - * @param sh > - * Pointer to Ethernet device shared context. > - */ > -static void > -mlx5_mr_garbage_collect(struct mlx5_ibv_shared *sh) > -{ > - struct mlx5_mr *mr_next; > - struct mlx5_mr_list free_list =3D LIST_HEAD_INITIALIZER(free_list); > - > - /* Must be called from the primary process. */ > - MLX5_ASSERT(rte_eal_process_type() =3D=3D RTE_PROC_PRIMARY); > - /* > - * MR can't be freed with holding the lock because rte_free() could > call > - * memory free callback function. This will be a deadlock situation. > - */ > - rte_rwlock_write_lock(&sh->mr.rwlock); > - /* Detach the whole free list and release it after unlocking. */ > - free_list =3D sh->mr.mr_free_list; > - LIST_INIT(&sh->mr.mr_free_list); > - rte_rwlock_write_unlock(&sh->mr.rwlock); > - /* Release resources. */ > - mr_next =3D LIST_FIRST(&free_list); > - while (mr_next !=3D NULL) { > - struct mlx5_mr *mr =3D mr_next; > - > - mr_next =3D LIST_NEXT(mr, mr); > - mr_free(mr); > - } > -} > - > -/* Called during rte_memseg_contig_walk() by mlx5_mr_create(). */ > -static int > -mr_find_contig_memsegs_cb(const struct rte_memseg_list *msl, > - const struct rte_memseg *ms, size_t len, void *arg) > -{ > - struct mr_find_contig_memsegs_data *data =3D arg; > - > - if (data->addr < ms->addr_64 || data->addr >=3D ms->addr_64 + len) > - return 0; > - /* Found, save it and stop walking. */ > - data->start =3D ms->addr_64; > - data->end =3D ms->addr_64 + len; > - data->msl =3D msl; > - return 1; > -} > - > -/** > - * Create a new global Memory Region (MR) for a missing virtual address. > - * This API should be called on a secondary process, then a request is s= ent to > - * the primary process in order to create a MR for the address. As the g= lobal > MR > - * list is on the shared memory, following LKey lookup should succeed un= less > the > - * request fails. > - * > - * @param dev > - * Pointer to Ethernet device. > - * @param[out] entry > - * Pointer to returning MR cache entry, found in the global cache or n= ewly > - * created. If failed to create one, this will not be updated. > - * @param addr > - * Target virtual address to register. > - * > - * @return > - * Searched LKey on success, UINT32_MAX on failure and rte_errno is se= t. > - */ > -static uint32_t > -mlx5_mr_create_secondary(struct rte_eth_dev *dev, struct mlx5_mr_cache > *entry, > - uintptr_t addr) > -{ > - struct mlx5_priv *priv =3D dev->data->dev_private; > - int ret; > - > - DEBUG("port %u requesting MR creation for address (%p)", > - dev->data->port_id, (void *)addr); > - ret =3D mlx5_mp_req_mr_create(&priv->mp_id, addr); > - if (ret) { > - DEBUG("port %u fail to request MR creation for address > (%p)", > - dev->data->port_id, (void *)addr); > - return UINT32_MAX; > - } > - rte_rwlock_read_lock(&priv->sh->mr.rwlock); > - /* Fill in output data. */ > - mr_lookup_dev(priv->sh, entry, addr); > - /* Lookup can't fail. */ > - MLX5_ASSERT(entry->lkey !=3D UINT32_MAX); > - rte_rwlock_read_unlock(&priv->sh->mr.rwlock); > - DEBUG("port %u MR CREATED by primary process for %p:\n" > - " [0x%" PRIxPTR ", 0x%" PRIxPTR "), lkey=3D0x%x", > - dev->data->port_id, (void *)addr, > - entry->start, entry->end, entry->lkey); > - return entry->lkey; > -} > - > -/** > - * Create a new global Memory Region (MR) for a missing virtual address. > - * Register entire virtually contiguous memory chunk around the address. > - * This must be called from the primary process. > - * > - * @param dev > - * Pointer to Ethernet device. > - * @param[out] entry > - * Pointer to returning MR cache entry, found in the global cache or n= ewly > - * created. If failed to create one, this will not be updated. > - * @param addr > - * Target virtual address to register. > - * > - * @return > - * Searched LKey on success, UINT32_MAX on failure and rte_errno is se= t. > - */ > -uint32_t > -mlx5_mr_create_primary(struct rte_eth_dev *dev, struct mlx5_mr_cache > *entry, > - uintptr_t addr) > -{ > - struct mlx5_priv *priv =3D dev->data->dev_private; > - struct mlx5_ibv_shared *sh =3D priv->sh; > - struct mlx5_dev_config *config =3D &priv->config; > - const struct rte_memseg_list *msl; > - const struct rte_memseg *ms; > - struct mlx5_mr *mr =3D NULL; > - size_t len; > - uint32_t ms_n; > - uint32_t bmp_size; > - void *bmp_mem; > - int ms_idx_shift =3D -1; > - unsigned int n; > - struct mr_find_contig_memsegs_data data =3D { > - .addr =3D addr, > - }; > - struct mr_find_contig_memsegs_data data_re; > - > - DRV_LOG(DEBUG, "port %u creating a MR using address (%p)", > - dev->data->port_id, (void *)addr); > - /* > - * Release detached MRs if any. This can't be called with holding > either > - * memory_hotplug_lock or sh->mr.rwlock. MRs on the free list have > - * been detached by the memory free event but it couldn't be > released > - * inside the callback due to deadlock. As a result, releasing resource= s > - * is quite opportunistic. > - */ > - mlx5_mr_garbage_collect(sh); > - /* > - * If enabled, find out a contiguous virtual address chunk in use, to > - * which the given address belongs, in order to register maximum > range. > - * In the best case where mempools are not dynamically recreated > and > - * '--socket-mem' is specified as an EAL option, it is very likely to > - * have only one MR(LKey) per a socket and per a hugepage-size even > - * though the system memory is highly fragmented. As the whole > memory > - * chunk will be pinned by kernel, it can't be reused unless entire > - * chunk is freed from EAL. > - * > - * If disabled, just register one memseg (page). Then, memory > - * consumption will be minimized but it may drop performance if > there > - * are many MRs to lookup on the datapath. > - */ > - if (!config->mr_ext_memseg_en) { > - data.msl =3D rte_mem_virt2memseg_list((void *)addr); > - data.start =3D RTE_ALIGN_FLOOR(addr, data.msl->page_sz); > - data.end =3D data.start + data.msl->page_sz; > - } else if (!rte_memseg_contig_walk(mr_find_contig_memsegs_cb, > &data)) { > - DRV_LOG(WARNING, > - "port %u unable to find virtually contiguous" > - " chunk for address (%p)." > - " rte_memseg_contig_walk() failed.", > - dev->data->port_id, (void *)addr); > - rte_errno =3D ENXIO; > - goto err_nolock; > - } > -alloc_resources: > - /* Addresses must be page-aligned. */ > - MLX5_ASSERT(rte_is_aligned((void *)data.start, data.msl->page_sz)); > - MLX5_ASSERT(rte_is_aligned((void *)data.end, data.msl->page_sz)); > - msl =3D data.msl; > - ms =3D rte_mem_virt2memseg((void *)data.start, msl); > - len =3D data.end - data.start; > - MLX5_ASSERT(msl->page_sz =3D=3D ms->hugepage_sz); > - /* Number of memsegs in the range. */ > - ms_n =3D len / msl->page_sz; > - DEBUG("port %u extending %p to [0x%" PRIxPTR ", 0x%" PRIxPTR ")," > - " page_sz=3D0x%" PRIx64 ", ms_n=3D%u", > - dev->data->port_id, (void *)addr, > - data.start, data.end, msl->page_sz, ms_n); > - /* Size of memory for bitmap. */ > - bmp_size =3D rte_bitmap_get_memory_footprint(ms_n); > - mr =3D rte_zmalloc_socket(NULL, > - RTE_ALIGN_CEIL(sizeof(*mr), > - RTE_CACHE_LINE_SIZE) + > - bmp_size, > - RTE_CACHE_LINE_SIZE, msl->socket_id); > - if (mr =3D=3D NULL) { > - DEBUG("port %u unable to allocate memory for a new MR of" > - " address (%p).", > - dev->data->port_id, (void *)addr); > - rte_errno =3D ENOMEM; > - goto err_nolock; > - } > - mr->msl =3D msl; > - /* > - * Save the index of the first memseg and initialize memseg bitmap. > To > - * see if a memseg of ms_idx in the memseg-list is still valid, check: > - * rte_bitmap_get(mr->bmp, ms_idx - mr->ms_base_idx) > - */ > - mr->ms_base_idx =3D rte_fbarray_find_idx(&msl->memseg_arr, ms); > - bmp_mem =3D RTE_PTR_ALIGN_CEIL(mr + 1, RTE_CACHE_LINE_SIZE); > - mr->ms_bmp =3D rte_bitmap_init(ms_n, bmp_mem, bmp_size); > - if (mr->ms_bmp =3D=3D NULL) { > - DEBUG("port %u unable to initialize bitmap for a new MR of" > - " address (%p).", > - dev->data->port_id, (void *)addr); > - rte_errno =3D EINVAL; > - goto err_nolock; > - } > - /* > - * Should recheck whether the extended contiguous chunk is still > valid. > - * Because memory_hotplug_lock can't be held if there's any memory > - * related calls in a critical path, resource allocation above can't be > - * locked. If the memory has been changed at this point, try again > with > - * just single page. If not, go on with the big chunk atomically from > - * here. > - */ > - rte_mcfg_mem_read_lock(); > - data_re =3D data; > - if (len > msl->page_sz && > - !rte_memseg_contig_walk(mr_find_contig_memsegs_cb, &data_re)) > { > - DEBUG("port %u unable to find virtually contiguous" > - " chunk for address (%p)." > - " rte_memseg_contig_walk() failed.", > - dev->data->port_id, (void *)addr); > - rte_errno =3D ENXIO; > - goto err_memlock; > - } > - if (data.start !=3D data_re.start || data.end !=3D data_re.end) { > - /* > - * The extended contiguous chunk has been changed. Try > again > - * with single memseg instead. > - */ > - data.start =3D RTE_ALIGN_FLOOR(addr, msl->page_sz); > - data.end =3D data.start + msl->page_sz; > - rte_mcfg_mem_read_unlock(); > - mr_free(mr); > - goto alloc_resources; > - } > - MLX5_ASSERT(data.msl =3D=3D data_re.msl); > - rte_rwlock_write_lock(&sh->mr.rwlock); > - /* > - * Check the address is really missing. If other thread already created > - * one or it is not found due to overflow, abort and return. > - */ > - if (mr_lookup_dev(sh, entry, addr) !=3D UINT32_MAX) { > - /* > - * Insert to the global cache table. It may fail due to > - * low-on-memory. Then, this entry will have to be searched > - * here again. > - */ > - mr_btree_insert(&sh->mr.cache, entry); > - DEBUG("port %u found MR for %p on final lookup, abort", > - dev->data->port_id, (void *)addr); > - rte_rwlock_write_unlock(&sh->mr.rwlock); > - rte_mcfg_mem_read_unlock(); > - /* > - * Must be unlocked before calling rte_free() because > - * mlx5_mr_mem_event_free_cb() can be called inside. > - */ > - mr_free(mr); > - return entry->lkey; > - } > - /* > - * Trim start and end addresses for verbs MR. Set bits for registering > - * memsegs but exclude already registered ones. Bitmap can be > - * fragmented. > - */ > - for (n =3D 0; n < ms_n; ++n) { > - uintptr_t start; > - struct mlx5_mr_cache ret; > - > - memset(&ret, 0, sizeof(ret)); > - start =3D data_re.start + n * msl->page_sz; > - /* Exclude memsegs already registered by other MRs. */ > - if (mr_lookup_dev(sh, &ret, start) =3D=3D UINT32_MAX) { > - /* > - * Start from the first unregistered memseg in the > - * extended range. > - */ > - if (ms_idx_shift =3D=3D -1) { > - mr->ms_base_idx +=3D n; > - data.start =3D start; > - ms_idx_shift =3D n; > - } > - data.end =3D start + msl->page_sz; > - rte_bitmap_set(mr->ms_bmp, n - ms_idx_shift); > - ++mr->ms_n; > - } > - } > - len =3D data.end - data.start; > - mr->ms_bmp_n =3D len / msl->page_sz; > - MLX5_ASSERT(ms_idx_shift + mr->ms_bmp_n <=3D ms_n); > - /* > - * Finally create a verbs MR for the memory chunk. ibv_reg_mr() can > be > - * called with holding the memory lock because it doesn't use > - * mlx5_alloc_buf_extern() which eventually calls rte_malloc_socket() > - * through mlx5_alloc_verbs_buf(). > - */ > - mr->ibv_mr =3D mlx5_glue->reg_mr(sh->pd, (void *)data.start, len, > - IBV_ACCESS_LOCAL_WRITE | > - IBV_ACCESS_RELAXED_ORDERING); > - if (mr->ibv_mr =3D=3D NULL) { > - DEBUG("port %u fail to create a verbs MR for address (%p)", > - dev->data->port_id, (void *)addr); > - rte_errno =3D EINVAL; > - goto err_mrlock; > - } > - MLX5_ASSERT((uintptr_t)mr->ibv_mr->addr =3D=3D data.start); > - MLX5_ASSERT(mr->ibv_mr->length =3D=3D len); > - LIST_INSERT_HEAD(&sh->mr.mr_list, mr, mr); > - DEBUG("port %u MR CREATED (%p) for %p:\n" > - " [0x%" PRIxPTR ", 0x%" PRIxPTR ")," > - " lkey=3D0x%x base_idx=3D%u ms_n=3D%u, ms_bmp_n=3D%u", > - dev->data->port_id, (void *)mr, (void *)addr, > - data.start, data.end, rte_cpu_to_be_32(mr->ibv_mr->lkey), > - mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n); > - /* Insert to the global cache table. */ > - mr_insert_dev_cache(sh, mr); > - /* Fill in output data. */ > - mr_lookup_dev(sh, entry, addr); > - /* Lookup can't fail. */ > - MLX5_ASSERT(entry->lkey !=3D UINT32_MAX); > - rte_rwlock_write_unlock(&sh->mr.rwlock); > - rte_mcfg_mem_read_unlock(); > - return entry->lkey; > -err_mrlock: > - rte_rwlock_write_unlock(&sh->mr.rwlock); > -err_memlock: > - rte_mcfg_mem_read_unlock(); > -err_nolock: > - /* > - * In case of error, as this can be called in a datapath, a warning > - * message per an error is preferable instead. Must be unlocked > before > - * calling rte_free() because mlx5_mr_mem_event_free_cb() can be > called > - * inside. > - */ > - mr_free(mr); > - return UINT32_MAX; > -} > - > -/** > - * Create a new global Memory Region (MR) for a missing virtual address. > - * This can be called from primary and secondary process. > - * > - * @param dev > - * Pointer to Ethernet device. > - * @param[out] entry > - * Pointer to returning MR cache entry, found in the global cache or n= ewly > - * created. If failed to create one, this will not be updated. > - * @param addr > - * Target virtual address to register. > - * > - * @return > - * Searched LKey on success, UINT32_MAX on failure and rte_errno is se= t. > - */ > -static uint32_t > -mlx5_mr_create(struct rte_eth_dev *dev, struct mlx5_mr_cache *entry, > - uintptr_t addr) > -{ > - uint32_t ret =3D 0; > - > - switch (rte_eal_process_type()) { > - case RTE_PROC_PRIMARY: > - ret =3D mlx5_mr_create_primary(dev, entry, addr); > - break; > - case RTE_PROC_SECONDARY: > - ret =3D mlx5_mr_create_secondary(dev, entry, addr); > - break; > - default: > - break; > - } > - return ret; > -} > - > -/** > - * Rebuild the global B-tree cache of device from the original MR list. > - * > - * @param sh > - * Pointer to Ethernet device shared context. > - */ > -static void > -mr_rebuild_dev_cache(struct mlx5_ibv_shared *sh) > -{ > - struct mlx5_mr *mr; > - > - DRV_LOG(DEBUG, "device %s rebuild dev cache[]", sh->ibdev_name); > - /* Flush cache to rebuild. */ > - sh->mr.cache.len =3D 1; > - sh->mr.cache.overflow =3D 0; > - /* Iterate all the existing MRs. */ > - LIST_FOREACH(mr, &sh->mr.mr_list, mr) > - if (mr_insert_dev_cache(sh, mr) < 0) > - return; > -} > - > /** > * Callback for memory free event. Iterate freed memsegs and check wheth= er > it > * belongs to an existing MR. If found, clear the bit from bitmap of MR.= As a > @@ -900,18 +74,18 @@ mlx5_mr_mem_event_free_cb(struct > mlx5_ibv_shared *sh, > RTE_ALIGN((uintptr_t)addr, msl->page_sz)); > MLX5_ASSERT(len =3D=3D RTE_ALIGN(len, msl->page_sz)); > ms_n =3D len / msl->page_sz; > - rte_rwlock_write_lock(&sh->mr.rwlock); > + rte_rwlock_write_lock(&sh->share_cache.rwlock); > /* Clear bits of freed memsegs from MR. */ > for (i =3D 0; i < ms_n; ++i) { > const struct rte_memseg *ms; > - struct mlx5_mr_cache entry; > + struct mr_cache_entry entry; > uintptr_t start; > int ms_idx; > uint32_t pos; >=20 > /* Find MR having this memseg. */ > start =3D (uintptr_t)addr + i * msl->page_sz; > - mr =3D mr_lookup_dev_list(sh, &entry, start); > + mr =3D mlx5_mr_lookup_list(&sh->share_cache, &entry, start); > if (mr =3D=3D NULL) > continue; > MLX5_ASSERT(mr->msl); /* Can't be external memory. */ > @@ -927,7 +101,7 @@ mlx5_mr_mem_event_free_cb(struct mlx5_ibv_shared > *sh, > rte_bitmap_clear(mr->ms_bmp, pos); > if (--mr->ms_n =3D=3D 0) { > LIST_REMOVE(mr, mr); > - LIST_INSERT_HEAD(&sh->mr.mr_free_list, mr, mr); > + LIST_INSERT_HEAD(&sh->share_cache.mr_free_list, > mr, mr); > DEBUG("device %s remove MR(%p) from list", > sh->ibdev_name, (void *)mr); > } > @@ -938,7 +112,7 @@ mlx5_mr_mem_event_free_cb(struct mlx5_ibv_shared > *sh, > rebuild =3D 1; > } > if (rebuild) { > - mr_rebuild_dev_cache(sh); > + mlx5_mr_rebuild_cache(&sh->share_cache); > /* > * Flush local caches by propagating invalidation across cores. > * rte_smp_wmb() is enough to synchronize this event. If one > of > @@ -948,12 +122,12 @@ mlx5_mr_mem_event_free_cb(struct > mlx5_ibv_shared *sh, > * generation below) will be guaranteed to be seen by other > core > * before the core sees the newly allocated memory. > */ > - ++sh->mr.dev_gen; > + ++sh->share_cache.dev_gen; > DEBUG("broadcasting local cache flush, gen=3D%d", > - sh->mr.dev_gen); > + sh->share_cache.dev_gen); > rte_smp_wmb(); > } > - rte_rwlock_write_unlock(&sh->mr.rwlock); > + rte_rwlock_write_unlock(&sh->share_cache.rwlock); > } >=20 > /** > @@ -990,111 +164,6 @@ mlx5_mr_mem_event_cb(enum rte_mem_event > event_type, const void *addr, > } > } >=20 > -/** > - * Look up address in the global MR cache table. If not found, create a = new > MR. > - * Insert the found/created entry to local bottom-half cache table. > - * > - * @param dev > - * Pointer to Ethernet device. > - * @param mr_ctrl > - * Pointer to per-queue MR control structure. > - * @param[out] entry > - * Pointer to returning MR cache entry, found in the global cache or n= ewly > - * created. If failed to create one, this is not written. > - * @param addr > - * Search key. > - * > - * @return > - * Searched LKey on success, UINT32_MAX on no match. > - */ > -static uint32_t > -mlx5_mr_lookup_dev(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl= , > - struct mlx5_mr_cache *entry, uintptr_t addr) > -{ > - struct mlx5_priv *priv =3D dev->data->dev_private; > - struct mlx5_ibv_shared *sh =3D priv->sh; > - struct mlx5_mr_btree *bt =3D &mr_ctrl->cache_bh; > - uint16_t idx; > - uint32_t lkey; > - > - /* If local cache table is full, try to double it. */ > - if (unlikely(bt->len =3D=3D bt->size)) > - mr_btree_expand(bt, bt->size << 1); > - /* Look up in the global cache. */ > - rte_rwlock_read_lock(&sh->mr.rwlock); > - lkey =3D mr_btree_lookup(&sh->mr.cache, &idx, addr); > - if (lkey !=3D UINT32_MAX) { > - /* Found. */ > - *entry =3D (*sh->mr.cache.table)[idx]; > - rte_rwlock_read_unlock(&sh->mr.rwlock); > - /* > - * Update local cache. Even if it fails, return the found entry > - * to update top-half cache. Next time, this entry will be > found > - * in the global cache. > - */ > - mr_btree_insert(bt, entry); > - return lkey; > - } > - rte_rwlock_read_unlock(&sh->mr.rwlock); > - /* First time to see the address? Create a new MR. */ > - lkey =3D mlx5_mr_create(dev, entry, addr); > - /* > - * Update the local cache if successfully created a new global MR. > Even > - * if failed to create one, there's no action to take in this datapath > - * code. As returning LKey is invalid, this will eventually make HW > - * fail. > - */ > - if (lkey !=3D UINT32_MAX) > - mr_btree_insert(bt, entry); > - return lkey; > -} > - > -/** > - * Bottom-half of LKey search on datapath. Firstly search in cache_bh[] = and if > - * misses, search in the global MR cache table and update the new entry = to > - * per-queue local caches. > - * > - * @param dev > - * Pointer to Ethernet device. > - * @param mr_ctrl > - * Pointer to per-queue MR control structure. > - * @param addr > - * Search key. > - * > - * @return > - * Searched LKey on success, UINT32_MAX on no match. > - */ > -static uint32_t > -mlx5_mr_addr2mr_bh(struct rte_eth_dev *dev, struct mlx5_mr_ctrl *mr_ctrl= , > - uintptr_t addr) > -{ > - uint32_t lkey; > - uint16_t bh_idx =3D 0; > - /* Victim in top-half cache to replace with new entry. */ > - struct mlx5_mr_cache *repl =3D &mr_ctrl->cache[mr_ctrl->head]; > - > - /* Binary-search MR translation table. */ > - lkey =3D mr_btree_lookup(&mr_ctrl->cache_bh, &bh_idx, addr); > - /* Update top-half cache. */ > - if (likely(lkey !=3D UINT32_MAX)) { > - *repl =3D (*mr_ctrl->cache_bh.table)[bh_idx]; > - } else { > - /* > - * If missed in local lookup table, search in the global cache > - * and local cache_bh[] will be updated inside if possible. > - * Top-half cache entry will also be updated. > - */ > - lkey =3D mlx5_mr_lookup_dev(dev, mr_ctrl, repl, addr); > - if (unlikely(lkey =3D=3D UINT32_MAX)) > - return UINT32_MAX; > - } > - /* Update the most recently used entry. */ > - mr_ctrl->mru =3D mr_ctrl->head; > - /* Point to the next victim, the oldest. */ > - mr_ctrl->head =3D (mr_ctrl->head + 1) % MLX5_MR_CACHE_N; > - return lkey; > -} > - > /** > * Bottom-half of LKey search on Rx. > * > @@ -1114,7 +183,9 @@ mlx5_rx_addr2mr_bh(struct mlx5_rxq_data *rxq, > uintptr_t addr) > struct mlx5_mr_ctrl *mr_ctrl =3D &rxq->mr_ctrl; > struct mlx5_priv *priv =3D rxq_ctrl->priv; >=20 > - return mlx5_mr_addr2mr_bh(ETH_DEV(priv), mr_ctrl, addr); > + return mlx5_mr_addr2mr_bh(priv->sh->pd, &priv->mp_id, > + &priv->sh->share_cache, mr_ctrl, addr, > + priv->config.mr_ext_memseg_en); > } >=20 > /** > @@ -1136,7 +207,9 @@ mlx5_tx_addr2mr_bh(struct mlx5_txq_data *txq, > uintptr_t addr) > struct mlx5_mr_ctrl *mr_ctrl =3D &txq->mr_ctrl; > struct mlx5_priv *priv =3D txq_ctrl->priv; >=20 > - return mlx5_mr_addr2mr_bh(ETH_DEV(priv), mr_ctrl, addr); > + return mlx5_mr_addr2mr_bh(priv->sh->pd, &priv->mp_id, > + &priv->sh->share_cache, mr_ctrl, addr, > + priv->config.mr_ext_memseg_en); > } >=20 > /** > @@ -1165,82 +238,6 @@ mlx5_tx_mb2mr_bh(struct mlx5_txq_data *txq, > struct rte_mbuf *mb) > return lkey; > } >=20 > -/** > - * Flush all of the local cache entries. > - * > - * @param mr_ctrl > - * Pointer to per-queue MR control structure. > - */ > -void > -mlx5_mr_flush_local_cache(struct mlx5_mr_ctrl *mr_ctrl) > -{ > - /* Reset the most-recently-used index. */ > - mr_ctrl->mru =3D 0; > - /* Reset the linear search array. */ > - mr_ctrl->head =3D 0; > - memset(mr_ctrl->cache, 0, sizeof(mr_ctrl->cache)); > - /* Reset the B-tree table. */ > - mr_ctrl->cache_bh.len =3D 1; > - mr_ctrl->cache_bh.overflow =3D 0; > - /* Update the generation number. */ > - mr_ctrl->cur_gen =3D *mr_ctrl->dev_gen_ptr; > - DRV_LOG(DEBUG, "mr_ctrl(%p): flushed, cur_gen=3D%d", > - (void *)mr_ctrl, mr_ctrl->cur_gen); > -} > - > -/** > - * Creates a memory region for external memory, that is memory which is = not > - * part of the DPDK memory segments. > - * > - * @param dev > - * Pointer to the ethernet device. > - * @param addr > - * Starting virtual address of memory. > - * @param len > - * Length of memory segment being mapped. > - * @param socked_id > - * Socket to allocate heap memory for the control structures. > - * > - * @return > - * Pointer to MR structure on success, NULL otherwise. > - */ > -static struct mlx5_mr * > -mlx5_create_mr_ext(struct rte_eth_dev *dev, uintptr_t addr, size_t len, > - int socket_id) > -{ > - struct mlx5_priv *priv =3D dev->data->dev_private; > - struct mlx5_mr *mr =3D NULL; > - > - mr =3D rte_zmalloc_socket(NULL, > - RTE_ALIGN_CEIL(sizeof(*mr), > - RTE_CACHE_LINE_SIZE), > - RTE_CACHE_LINE_SIZE, socket_id); > - if (mr =3D=3D NULL) > - return NULL; > - mr->ibv_mr =3D mlx5_glue->reg_mr(priv->sh->pd, (void *)addr, len, > - IBV_ACCESS_LOCAL_WRITE | > - IBV_ACCESS_RELAXED_ORDERING); > - if (mr->ibv_mr =3D=3D NULL) { > - DRV_LOG(WARNING, > - "port %u fail to create a verbs MR for address (%p)", > - dev->data->port_id, (void *)addr); > - rte_free(mr); > - return NULL; > - } > - mr->msl =3D NULL; /* Mark it is external memory. */ > - mr->ms_bmp =3D NULL; > - mr->ms_n =3D 1; > - mr->ms_bmp_n =3D 1; > - DRV_LOG(DEBUG, > - "port %u MR CREATED (%p) for external memory %p:\n" > - " [0x%" PRIxPTR ", 0x%" PRIxPTR ")," > - " lkey=3D0x%x base_idx=3D%u ms_n=3D%u, ms_bmp_n=3D%u", > - dev->data->port_id, (void *)mr, (void *)addr, > - addr, addr + len, rte_cpu_to_be_32(mr->ibv_mr->lkey), > - mr->ms_base_idx, mr->ms_n, mr->ms_bmp_n); > - return mr; > -} > - > /** > * Called during rte_mempool_mem_iter() by mlx5_mr_update_ext_mp(). > * > @@ -1267,19 +264,19 @@ mlx5_mr_update_ext_mp_cb(struct rte_mempool > *mp, void *opaque, > struct mlx5_mr *mr =3D NULL; > uintptr_t addr =3D (uintptr_t)memhdr->addr; > size_t len =3D memhdr->len; > - struct mlx5_mr_cache entry; > + struct mr_cache_entry entry; > uint32_t lkey; >=20 > MLX5_ASSERT(rte_eal_process_type() =3D=3D RTE_PROC_PRIMARY); > /* If already registered, it should return. */ > - rte_rwlock_read_lock(&sh->mr.rwlock); > - lkey =3D mr_lookup_dev(sh, &entry, addr); > - rte_rwlock_read_unlock(&sh->mr.rwlock); > + rte_rwlock_read_lock(&sh->share_cache.rwlock); > + lkey =3D mlx5_mr_lookup_cache(&sh->share_cache, &entry, addr); > + rte_rwlock_read_unlock(&sh->share_cache.rwlock); > if (lkey !=3D UINT32_MAX) > return; > DRV_LOG(DEBUG, "port %u register MR for chunk #%d of mempool > (%s)", > dev->data->port_id, mem_idx, mp->name); > - mr =3D mlx5_create_mr_ext(dev, addr, len, mp->socket_id); > + mr =3D mlx5_create_mr_ext(sh->pd, addr, len, mp->socket_id); > if (!mr) { > DRV_LOG(WARNING, > "port %u unable to allocate a new MR of" > @@ -1288,13 +285,14 @@ mlx5_mr_update_ext_mp_cb(struct rte_mempool > *mp, void *opaque, > data->ret =3D -1; > return; > } > - rte_rwlock_write_lock(&sh->mr.rwlock); > - LIST_INSERT_HEAD(&sh->mr.mr_list, mr, mr); > + rte_rwlock_write_lock(&sh->share_cache.rwlock); > + LIST_INSERT_HEAD(&sh->share_cache.mr_list, mr, mr); > /* Insert to the global cache table. */ > - mr_insert_dev_cache(sh, mr); > - rte_rwlock_write_unlock(&sh->mr.rwlock); > + mlx5_mr_insert_cache(&sh->share_cache, mr); > + rte_rwlock_write_unlock(&sh->share_cache.rwlock); > /* Insert to the local cache table */ > - mlx5_mr_addr2mr_bh(dev, mr_ctrl, addr); > + mlx5_mr_addr2mr_bh(sh->pd, &priv->mp_id, &sh->share_cache, > + mr_ctrl, addr, priv->config.mr_ext_memseg_en); > } >=20 > /** > @@ -1351,19 +349,19 @@ mlx5_dma_map(struct rte_pci_device *pdev, void > *addr, > return -1; > } > priv =3D dev->data->dev_private; > - mr =3D mlx5_create_mr_ext(dev, (uintptr_t)addr, len, SOCKET_ID_ANY); > + sh =3D priv->sh; > + mr =3D mlx5_create_mr_ext(sh->pd, (uintptr_t)addr, len, > SOCKET_ID_ANY); > if (!mr) { > DRV_LOG(WARNING, > "port %u unable to dma map", dev->data->port_id); > rte_errno =3D EINVAL; > return -1; > } > - sh =3D priv->sh; > - rte_rwlock_write_lock(&sh->mr.rwlock); > - LIST_INSERT_HEAD(&sh->mr.mr_list, mr, mr); > + rte_rwlock_write_lock(&sh->share_cache.rwlock); > + LIST_INSERT_HEAD(&sh->share_cache.mr_list, mr, mr); > /* Insert to the global cache table. */ > - mr_insert_dev_cache(sh, mr); > - rte_rwlock_write_unlock(&sh->mr.rwlock); > + mlx5_mr_insert_cache(&sh->share_cache, mr); > + rte_rwlock_write_unlock(&sh->share_cache.rwlock); > return 0; > } >=20 > @@ -1390,7 +388,7 @@ mlx5_dma_unmap(struct rte_pci_device *pdev, void > *addr, > struct mlx5_priv *priv; > struct mlx5_ibv_shared *sh; > struct mlx5_mr *mr; > - struct mlx5_mr_cache entry; > + struct mr_cache_entry entry; >=20 > dev =3D pci_dev_to_eth_dev(pdev); > if (!dev) { > @@ -1401,10 +399,10 @@ mlx5_dma_unmap(struct rte_pci_device *pdev, > void *addr, > } > priv =3D dev->data->dev_private; > sh =3D priv->sh; > - rte_rwlock_read_lock(&sh->mr.rwlock); > - mr =3D mr_lookup_dev_list(sh, &entry, (uintptr_t)addr); > + rte_rwlock_read_lock(&sh->share_cache.rwlock); > + mr =3D mlx5_mr_lookup_list(&sh->share_cache, &entry, > (uintptr_t)addr); > if (!mr) { > - rte_rwlock_read_unlock(&sh->mr.rwlock); > + rte_rwlock_read_unlock(&sh->share_cache.rwlock); > DRV_LOG(WARNING, "address 0x%" PRIxPTR " wasn't > registered " > "to PCI device %p", (uintptr_t)addr, > (void *)pdev); > @@ -1412,10 +410,10 @@ mlx5_dma_unmap(struct rte_pci_device *pdev, > void *addr, > return -1; > } > LIST_REMOVE(mr, mr); > - LIST_INSERT_HEAD(&sh->mr.mr_free_list, mr, mr); > + LIST_INSERT_HEAD(&sh->share_cache.mr_free_list, mr, mr); > DEBUG("port %u remove MR(%p) from list", dev->data->port_id, > (void *)mr); > - mr_rebuild_dev_cache(sh); > + mlx5_mr_rebuild_cache(&sh->share_cache); > /* > * Flush local caches by propagating invalidation across cores. > * rte_smp_wmb() is enough to synchronize this event. If one of > @@ -1425,10 +423,11 @@ mlx5_dma_unmap(struct rte_pci_device *pdev, > void *addr, > * generation below) will be guaranteed to be seen by other core > * before the core sees the newly allocated memory. > */ > - ++sh->mr.dev_gen; > - DEBUG("broadcasting local cache flush, gen=3D%d", sh- > >mr.dev_gen); > + ++sh->share_cache.dev_gen; > + DEBUG("broadcasting local cache flush, gen=3D%d", > + sh->share_cache.dev_gen); > rte_smp_wmb(); > - rte_rwlock_read_unlock(&sh->mr.rwlock); > + rte_rwlock_read_unlock(&sh->share_cache.rwlock); > return 0; > } >=20 > @@ -1503,14 +502,19 @@ mlx5_mr_update_mp_cb(struct rte_mempool *mp > __rte_unused, void *opaque, > unsigned mem_idx __rte_unused) > { > struct mr_update_mp_data *data =3D opaque; > + struct rte_eth_dev *dev =3D data->dev; > + struct mlx5_priv *priv =3D dev->data->dev_private; > + > uint32_t lkey; >=20 > /* Stop iteration if failed in the previous walk. */ > if (data->ret < 0) > return; > /* Register address of the chunk and update local caches. */ > - lkey =3D mlx5_mr_addr2mr_bh(data->dev, data->mr_ctrl, > - (uintptr_t)memhdr->addr); > + lkey =3D mlx5_mr_addr2mr_bh(priv->sh->pd, &priv->mp_id, > + &priv->sh->share_cache, data->mr_ctrl, > + (uintptr_t)memhdr->addr, > + priv->config.mr_ext_memseg_en); > if (lkey =3D=3D UINT32_MAX) > data->ret =3D -1; > } > @@ -1545,76 +549,3 @@ mlx5_mr_update_mp(struct rte_eth_dev *dev, > struct mlx5_mr_ctrl *mr_ctrl, > } > return data.ret; > } > - > -/** > - * Dump all the created MRs and the global cache entries. > - * > - * @param sh > - * Pointer to Ethernet device shared context. > - */ > -void > -mlx5_mr_dump_dev(struct mlx5_ibv_shared *sh __rte_unused) > -{ > -#ifdef RTE_LIBRTE_MLX5_DEBUG > - struct mlx5_mr *mr; > - int mr_n =3D 0; > - int chunk_n =3D 0; > - > - rte_rwlock_read_lock(&sh->mr.rwlock); > - /* Iterate all the existing MRs. */ > - LIST_FOREACH(mr, &sh->mr.mr_list, mr) { > - unsigned int n; > - > - DEBUG("device %s MR[%u], LKey =3D 0x%x, ms_n =3D %u, > ms_bmp_n =3D %u", > - sh->ibdev_name, mr_n++, > - rte_cpu_to_be_32(mr->ibv_mr->lkey), > - mr->ms_n, mr->ms_bmp_n); > - if (mr->ms_n =3D=3D 0) > - continue; > - for (n =3D 0; n < mr->ms_bmp_n; ) { > - struct mlx5_mr_cache ret =3D { 0, }; > - > - n =3D mr_find_next_chunk(mr, &ret, n); > - if (!ret.end) > - break; > - DEBUG(" chunk[%u], [0x%" PRIxPTR ", 0x%" PRIxPTR > ")", > - chunk_n++, ret.start, ret.end); > - } > - } > - DEBUG("device %s dumping global cache", sh->ibdev_name); > - mlx5_mr_btree_dump(&sh->mr.cache); > - rte_rwlock_read_unlock(&sh->mr.rwlock); > -#endif > -} > - > -/** > - * Release all the created MRs and resources for shared device context. > - * list. > - * > - * @param sh > - * Pointer to Ethernet device shared context. > - */ > -void > -mlx5_mr_release(struct mlx5_ibv_shared *sh) > -{ > - struct mlx5_mr *mr_next; > - > - if (rte_log_can_log(mlx5_logtype, RTE_LOG_DEBUG)) > - mlx5_mr_dump_dev(sh); > - rte_rwlock_write_lock(&sh->mr.rwlock); > - /* Detach from MR list and move to free list. */ > - mr_next =3D LIST_FIRST(&sh->mr.mr_list); > - while (mr_next !=3D NULL) { > - struct mlx5_mr *mr =3D mr_next; > - > - mr_next =3D LIST_NEXT(mr, mr); > - LIST_REMOVE(mr, mr); > - LIST_INSERT_HEAD(&sh->mr.mr_free_list, mr, mr); > - } > - LIST_INIT(&sh->mr.mr_list); > - /* Free global cache. */ > - mlx5_mr_btree_free(&sh->mr.cache); > - rte_rwlock_write_unlock(&sh->mr.rwlock); > - /* Free all remaining MRs. */ > - mlx5_mr_garbage_collect(sh); > -} > diff --git a/drivers/net/mlx5/mlx5_mr.h b/drivers/net/mlx5/mlx5_mr.h > index 48264c8294..0c5877b3d6 100644 > --- a/drivers/net/mlx5/mlx5_mr.h > +++ b/drivers/net/mlx5/mlx5_mr.h > @@ -24,99 +24,16 @@ > #include > #include > #include > +#include >=20 > -/* Memory Region object. */ > -struct mlx5_mr { > - LIST_ENTRY(mlx5_mr) mr; /**< Pointer to the prev/next entry. */ > - struct ibv_mr *ibv_mr; /* Verbs Memory Region. */ > - const struct rte_memseg_list *msl; > - int ms_base_idx; /* Start index of msl->memseg_arr[]. */ > - int ms_n; /* Number of memsegs in use. */ > - uint32_t ms_bmp_n; /* Number of bits in memsegs bit-mask. */ > - struct rte_bitmap *ms_bmp; /* Bit-mask of memsegs belonged to MR. > */ > -}; > - > -/* Cache entry for Memory Region. */ > -struct mlx5_mr_cache { > - uintptr_t start; /* Start address of MR. */ > - uintptr_t end; /* End address of MR. */ > - uint32_t lkey; /* rte_cpu_to_be_32(ibv_mr->lkey). */ > -} __rte_packed; > - > -/* MR Cache table for Binary search. */ > -struct mlx5_mr_btree { > - uint16_t len; /* Number of entries. */ > - uint16_t size; /* Total number of entries. */ > - int overflow; /* Mark failure of table expansion. */ > - struct mlx5_mr_cache (*table)[]; > -} __rte_packed; > - > -/* Per-queue MR control descriptor. */ > -struct mlx5_mr_ctrl { > - uint32_t *dev_gen_ptr; /* Generation number of device to poll. */ > - uint32_t cur_gen; /* Generation number saved to flush caches. */ > - uint16_t mru; /* Index of last hit entry in top-half cache. */ > - uint16_t head; /* Index of the oldest entry in top-half cache. */ > - struct mlx5_mr_cache cache[MLX5_MR_CACHE_N]; /* Cache for top- > half. */ > - struct mlx5_mr_btree cache_bh; /* Cache for bottom-half. */ > -} __rte_packed; > - > -struct mlx5_ibv_shared; > -extern struct mlx5_dev_list mlx5_mem_event_cb_list; > -extern rte_rwlock_t mlx5_mem_event_rwlock; > +#include >=20 > /* First entry must be NULL for comparison. */ > #define mlx5_mr_btree_len(bt) ((bt)->len - 1) >=20 > -int mlx5_mr_btree_init(struct mlx5_mr_btree *bt, int n, int socket); > -void mlx5_mr_btree_free(struct mlx5_mr_btree *bt); > -uint32_t mlx5_mr_create_primary(struct rte_eth_dev *dev, > - struct mlx5_mr_cache *entry, uintptr_t addr); > void mlx5_mr_mem_event_cb(enum rte_mem_event event_type, const void > *addr, > size_t len, void *arg); > int mlx5_mr_update_mp(struct rte_eth_dev *dev, struct mlx5_mr_ctrl > *mr_ctrl, > struct rte_mempool *mp); > -void mlx5_mr_release(struct mlx5_ibv_shared *sh); > - > -/* Debug purpose functions. */ > -void mlx5_mr_btree_dump(struct mlx5_mr_btree *bt); > -void mlx5_mr_dump_dev(struct mlx5_ibv_shared *sh); > - > -/** > - * Look up LKey from given lookup table by linear search. Firstly look u= p the > - * last-hit entry. If miss, the entire array is searched. If found, upda= te the > - * last-hit index and return LKey. > - * > - * @param lkp_tbl > - * Pointer to lookup table. > - * @param[in,out] cached_idx > - * Pointer to last-hit index. > - * @param n > - * Size of lookup table. > - * @param addr > - * Search key. > - * > - * @return > - * Searched LKey on success, UINT32_MAX on no match. > - */ > -static __rte_always_inline uint32_t > -mlx5_mr_lookup_cache(struct mlx5_mr_cache *lkp_tbl, uint16_t > *cached_idx, > - uint16_t n, uintptr_t addr) > -{ > - uint16_t idx; > - > - if (likely(addr >=3D lkp_tbl[*cached_idx].start && > - addr < lkp_tbl[*cached_idx].end)) > - return lkp_tbl[*cached_idx].lkey; > - for (idx =3D 0; idx < n && lkp_tbl[idx].start !=3D 0; ++idx) { > - if (addr >=3D lkp_tbl[idx].start && > - addr < lkp_tbl[idx].end) { > - /* Found. */ > - *cached_idx =3D idx; > - return lkp_tbl[idx].lkey; > - } > - } > - return UINT32_MAX; > -} >=20 > #endif /* RTE_PMD_MLX5_MR_H_ */ > diff --git a/drivers/net/mlx5/mlx5_rxtx.c b/drivers/net/mlx5/mlx5_rxtx.c > index 42d7da8a4b..3e583d49a6 100644 > --- a/drivers/net/mlx5/mlx5_rxtx.c > +++ b/drivers/net/mlx5/mlx5_rxtx.c > @@ -33,6 +33,7 @@ >=20 > #include "mlx5_defs.h" > #include "mlx5.h" > +#include "mlx5_mr.h" > #include "mlx5_utils.h" > #include "mlx5_rxtx.h" > #include "mlx5_autoconf.h" > diff --git a/drivers/net/mlx5/mlx5_rxtx.h b/drivers/net/mlx5/mlx5_rxtx.h > index d155c241eb..537d449c88 100644 > --- a/drivers/net/mlx5/mlx5_rxtx.h > +++ b/drivers/net/mlx5/mlx5_rxtx.h > @@ -34,11 +34,11 @@ > #include > #include > #include > +#include >=20 > #include "mlx5_defs.h" > #include "mlx5_utils.h" > #include "mlx5.h" > -#include "mlx5_mr.h" > #include "mlx5_autoconf.h" >=20 > /* Support tunnel matching. */ > @@ -598,8 +598,8 @@ mlx5_rx_addr2mr(struct mlx5_rxq_data *rxq, > uintptr_t addr) > uint32_t lkey; >=20 > /* Linear search on MR cache array. */ > - lkey =3D mlx5_mr_lookup_cache(mr_ctrl->cache, &mr_ctrl->mru, > - MLX5_MR_CACHE_N, addr); > + lkey =3D mlx5_mr_lookup_lkey(mr_ctrl->cache, &mr_ctrl->mru, > + MLX5_MR_CACHE_N, addr); > if (likely(lkey !=3D UINT32_MAX)) > return lkey; > /* Take slower bottom-half (Binary Search) on miss. */ > @@ -630,8 +630,8 @@ mlx5_tx_mb2mr(struct mlx5_txq_data *txq, struct > rte_mbuf *mb) > if (unlikely(*mr_ctrl->dev_gen_ptr !=3D mr_ctrl->cur_gen)) > mlx5_mr_flush_local_cache(mr_ctrl); > /* Linear search on MR cache array. */ > - lkey =3D mlx5_mr_lookup_cache(mr_ctrl->cache, &mr_ctrl->mru, > - MLX5_MR_CACHE_N, addr); > + lkey =3D mlx5_mr_lookup_lkey(mr_ctrl->cache, &mr_ctrl->mru, > + MLX5_MR_CACHE_N, addr); > if (likely(lkey !=3D UINT32_MAX)) > return lkey; > /* Take slower bottom-half on miss. */ > diff --git a/drivers/net/mlx5/mlx5_rxtx_vec.h > b/drivers/net/mlx5/mlx5_rxtx_vec.h > index ea925156f0..6ddcbfb0ad 100644 > --- a/drivers/net/mlx5/mlx5_rxtx_vec.h > +++ b/drivers/net/mlx5/mlx5_rxtx_vec.h > @@ -13,6 +13,8 @@ >=20 > #include "mlx5_autoconf.h" >=20 > +#include "mlx5_mr.h" > + > /* HW checksum offload capabilities of vectorized Tx. */ > #define MLX5_VEC_TX_CKSUM_OFFLOAD_CAP \ > (DEV_TX_OFFLOAD_IPV4_CKSUM | \ > diff --git a/drivers/net/mlx5/mlx5_trigger.c b/drivers/net/mlx5/mlx5_trig= ger.c > index 438b705952..759670408b 100644 > --- a/drivers/net/mlx5/mlx5_trigger.c > +++ b/drivers/net/mlx5/mlx5_trigger.c > @@ -11,6 +11,7 @@ > #include >=20 > #include "mlx5.h" > +#include "mlx5_mr.h" > #include "mlx5_rxtx.h" > #include "mlx5_utils.h" > #include "rte_pmd_mlx5.h" > diff --git a/drivers/net/mlx5/mlx5_txq.c b/drivers/net/mlx5/mlx5_txq.c > index 0653f4cf30..29e5cabab6 100644 > --- a/drivers/net/mlx5/mlx5_txq.c > +++ b/drivers/net/mlx5/mlx5_txq.c > @@ -30,6 +30,7 @@ > #include > #include > #include > +#include >=20 > #include "mlx5_defs.h" > #include "mlx5_utils.h" > @@ -1289,7 +1290,7 @@ mlx5_txq_new(struct rte_eth_dev *dev, uint16_t > idx, uint16_t desc, > goto error; > } > /* Save pointer of global generation number to check memory event. > */ > - tmpl->txq.mr_ctrl.dev_gen_ptr =3D &priv->sh->mr.dev_gen; > + tmpl->txq.mr_ctrl.dev_gen_ptr =3D &priv->sh->share_cache.dev_gen; > MLX5_ASSERT(desc > MLX5_TX_COMP_THRESH); > tmpl->txq.offloads =3D conf->offloads | > dev->data->dev_conf.txmode.offloads; > -- > 2.16.6