1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox
8 * Memory management functions for mlx4 driver.
18 /* Verbs headers do not support -pedantic. */
20 #pragma GCC diagnostic ignored "-Wpedantic"
22 #include <infiniband/verbs.h>
24 #pragma GCC diagnostic error "-Wpedantic"
27 #include <rte_branch_prediction.h>
28 #include <rte_common.h>
29 #include <rte_errno.h>
30 #include <rte_malloc.h>
31 #include <rte_memory.h>
32 #include <rte_mempool.h>
33 #include <rte_spinlock.h>
35 #include "mlx4_rxtx.h"
36 #include "mlx4_utils.h"
38 struct mlx4_check_mempool_data {
45 * Called by mlx4_check_mempool() when iterating the memory chunks.
48 * Pointer to memory pool (unused).
49 * @param[in, out] data
50 * Pointer to shared buffer with mlx4_check_mempool().
52 * Pointer to mempool chunk header.
54 * Mempool element index (unused).
57 mlx4_check_mempool_cb(struct rte_mempool *mp, void *opaque,
58 struct rte_mempool_memhdr *memhdr,
61 struct mlx4_check_mempool_data *data = opaque;
65 /* It already failed, skip the next chunks. */
68 /* It is the first chunk. */
69 if (data->start == NULL && data->end == NULL) {
70 data->start = memhdr->addr;
71 data->end = data->start + memhdr->len;
74 if (data->end == memhdr->addr) {
75 data->end += memhdr->len;
78 if (data->start == (char *)memhdr->addr + memhdr->len) {
79 data->start -= memhdr->len;
82 /* Error, mempool is not virtually contiguous. */
87 * Check if a mempool can be used: it must be virtually contiguous.
90 * Pointer to memory pool.
92 * Pointer to the start address of the mempool virtual memory area.
94 * Pointer to the end address of the mempool virtual memory area.
97 * 0 on success (mempool is virtually contiguous), -1 on error.
100 mlx4_check_mempool(struct rte_mempool *mp, uintptr_t *start, uintptr_t *end)
102 struct mlx4_check_mempool_data data;
104 memset(&data, 0, sizeof(data));
105 rte_mempool_mem_iter(mp, mlx4_check_mempool_cb, &data);
106 *start = (uintptr_t)data.start;
107 *end = (uintptr_t)data.end;
112 * Obtain a memory region from a memory pool.
114 * If a matching memory region already exists, it is returned with its
115 * reference count incremented, otherwise a new one is registered.
118 * Pointer to private structure.
120 * Pointer to memory pool.
123 * Memory region pointer, NULL in case of error and rte_errno is set.
126 mlx4_mr_get(struct priv *priv, struct rte_mempool *mp)
128 const struct rte_memseg *ms = rte_eal_get_physmem_layout();
134 if (mlx4_check_mempool(mp, &start, &end) != 0) {
136 ERROR("mempool %p: not virtually contiguous",
140 DEBUG("mempool %p area start=%p end=%p size=%zu",
141 (void *)mp, (void *)start, (void *)end,
142 (size_t)(end - start));
143 /* Round start and end to page boundary if found in memory segments. */
144 for (i = 0; (i < RTE_MAX_MEMSEG) && (ms[i].addr != NULL); ++i) {
145 uintptr_t addr = (uintptr_t)ms[i].addr;
146 size_t len = ms[i].len;
147 unsigned int align = ms[i].hugepage_sz;
149 if ((start > addr) && (start < addr + len))
150 start = RTE_ALIGN_FLOOR(start, align);
151 if ((end > addr) && (end < addr + len))
152 end = RTE_ALIGN_CEIL(end, align);
154 DEBUG("mempool %p using start=%p end=%p size=%zu for MR",
155 (void *)mp, (void *)start, (void *)end,
156 (size_t)(end - start));
157 rte_spinlock_lock(&priv->mr_lock);
158 LIST_FOREACH(mr, &priv->mr, next)
159 if (mp == mr->mp && start >= mr->start && end <= mr->end)
165 mr = rte_malloc(__func__, sizeof(*mr), 0);
170 *mr = (struct mlx4_mr){
175 .mr = ibv_reg_mr(priv->pd, (void *)start, end - start,
176 IBV_ACCESS_LOCAL_WRITE),
180 mr->lkey = mr->mr->lkey;
181 LIST_INSERT_HEAD(&priv->mr, mr, next);
185 rte_errno = errno ? errno : EINVAL;
188 rte_spinlock_unlock(&priv->mr_lock);
193 * Release a memory region.
195 * This function decrements its reference count and destroys it after
198 * Note to avoid race conditions given this function may be used from the
199 * data plane, it's extremely important that each user holds its own
203 * Memory region to release.
206 mlx4_mr_put(struct mlx4_mr *mr)
208 struct priv *priv = mr->priv;
210 rte_spinlock_lock(&priv->mr_lock);
214 LIST_REMOVE(mr, next);
215 claim_zero(ibv_dereg_mr(mr->mr));
218 rte_spinlock_unlock(&priv->mr_lock);
222 * Add memory region (MR) <-> memory pool (MP) association to txq->mp2mr[].
223 * If mp2mr[] is full, remove an entry first.
226 * Pointer to Tx queue structure.
228 * Memory pool for which a memory region lkey must be added.
230 * Index in memory pool (MP) where to add memory region (MR).
233 * Added mr->lkey on success, (uint32_t)-1 on failure.
236 mlx4_txq_add_mr(struct txq *txq, struct rte_mempool *mp, uint32_t i)
240 /* Add a new entry, register MR first. */
241 DEBUG("%p: discovered new memory pool \"%s\" (%p)",
242 (void *)txq, mp->name, (void *)mp);
243 mr = mlx4_mr_get(txq->priv, mp);
244 if (unlikely(mr == NULL)) {
245 DEBUG("%p: unable to configure MR, mlx4_mr_get() failed",
249 if (unlikely(i == RTE_DIM(txq->mp2mr))) {
250 /* Table is full, remove oldest entry. */
251 DEBUG("%p: MR <-> MP table full, dropping oldest entry.",
254 mlx4_mr_put(txq->mp2mr[0].mr);
255 memmove(&txq->mp2mr[0], &txq->mp2mr[1],
256 (sizeof(txq->mp2mr) - sizeof(txq->mp2mr[0])));
258 /* Store the new entry. */
259 txq->mp2mr[i].mp = mp;
260 txq->mp2mr[i].mr = mr;
261 txq->mp2mr[i].lkey = mr->lkey;
262 DEBUG("%p: new MR lkey for MP \"%s\" (%p): 0x%08" PRIu32,
263 (void *)txq, mp->name, (void *)mp, txq->mp2mr[i].lkey);
264 return txq->mp2mr[i].lkey;