From: Jun Yang <[email protected]>

Track allocated BPIDs in a static per-BPID flag table and register a
driver destructor that releases any BPIDs still marked as in use at
process exit. This prevents BPID leaks when an application exits without
calling rte_mempool_free(). Also tune the per-lcore mempool cache flush
threshold to match the hardware bulk release size (DPAA_MBUF_MAX_ACQ_REL)
so that buffers are returned to HW in optimal burst sizes.

Signed-off-by: Jun Yang <[email protected]>
---
 drivers/bus/dpaa/base/qbman/bman.c       |  8 +++
 drivers/bus/dpaa/dpaa_bus_base_symbols.c |  1 +
 drivers/bus/dpaa/include/fsl_bman.h      |  3 ++
 drivers/mempool/dpaa/dpaa_mempool.c      | 67 ++++++++++++++++++++++--
 drivers/mempool/dpaa/dpaa_mempool.h      |  3 +-
 5 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/drivers/bus/dpaa/base/qbman/bman.c 
b/drivers/bus/dpaa/base/qbman/bman.c
index 889d657052..d09266fa5b 100644
--- a/drivers/bus/dpaa/base/qbman/bman.c
+++ b/drivers/bus/dpaa/base/qbman/bman.c
@@ -237,6 +237,14 @@ void bman_free_pool(struct bman_pool *pool)
        kfree(pool);
 }
 
+void bman_free_bpid(u8 bpid, u32 flags)
+{
+       if (flags & BMAN_POOL_FLAG_THRESH)
+               bm_pool_set(bpid, zero_thresholds);
+       if (flags & BMAN_POOL_FLAG_DYNAMIC_BPID)
+               bman_release_bpid(bpid);
+}
+
 const struct bman_pool_params *bman_get_params(const struct bman_pool *pool)
 {
        return &pool->params;
diff --git a/drivers/bus/dpaa/dpaa_bus_base_symbols.c 
b/drivers/bus/dpaa/dpaa_bus_base_symbols.c
index 514ab7b1f1..8bd1a9bc6e 100644
--- a/drivers/bus/dpaa/dpaa_bus_base_symbols.c
+++ b/drivers/bus/dpaa/dpaa_bus_base_symbols.c
@@ -46,6 +46,7 @@ RTE_EXPORT_INTERNAL_SYMBOL(netcfg_acquire)
 RTE_EXPORT_INTERNAL_SYMBOL(netcfg_release)
 RTE_EXPORT_INTERNAL_SYMBOL(bman_new_pool)
 RTE_EXPORT_INTERNAL_SYMBOL(bman_free_pool)
+RTE_EXPORT_INTERNAL_SYMBOL(bman_free_bpid)
 RTE_EXPORT_INTERNAL_SYMBOL(bman_get_params)
 RTE_EXPORT_INTERNAL_SYMBOL(bman_release)
 RTE_EXPORT_INTERNAL_SYMBOL(bman_acquire)
diff --git a/drivers/bus/dpaa/include/fsl_bman.h 
b/drivers/bus/dpaa/include/fsl_bman.h
index 67a7a09618..6079eedff5 100644
--- a/drivers/bus/dpaa/include/fsl_bman.h
+++ b/drivers/bus/dpaa/include/fsl_bman.h
@@ -317,6 +317,9 @@ struct bman_pool *bman_new_pool(const struct 
bman_pool_params *params);
 __rte_internal
 void bman_free_pool(struct bman_pool *pool);
 
+__rte_internal
+void bman_free_bpid(u8 bpid, u32 flags);
+
 /**
  * bman_get_params - Returns a pool object's parameters.
  * @pool: the pool object
diff --git a/drivers/mempool/dpaa/dpaa_mempool.c 
b/drivers/mempool/dpaa/dpaa_mempool.c
index 3fdbcba646..210ea3bcf9 100644
--- a/drivers/mempool/dpaa/dpaa_mempool.c
+++ b/drivers/mempool/dpaa/dpaa_mempool.c
@@ -25,10 +25,22 @@
 #include <rte_eal.h>
 #include <rte_malloc.h>
 #include <rte_ring.h>
+#include <rte_common.h>
 
 #include <dpaa_mempool.h>
 #include <dpaax_iova_table.h>
 
+struct dpaa_bpid_flag {
+       uint32_t flags;
+       int used;
+};
+
+/** Be referenced in destructor to release bpid allocated.
+ * Destructor can't access bman_pool from eal mem,
+ * we release ID with flag directly.
+ */
+static struct dpaa_bpid_flag s_dpaa_bpid_allocated_flag[DPAA_MAX_BPOOLS];
+
 #define FMAN_ERRATA_BOUNDARY ((uint64_t)4096)
 #define FMAN_ERRATA_BOUNDARY_MASK (~(FMAN_ERRATA_BOUNDARY - 1))
 
@@ -58,6 +70,8 @@ dpaa_mbuf_create_pool(struct rte_mempool *mp)
        struct bman_pool_params params = {
                .flags = BMAN_POOL_FLAG_DYNAMIC_BPID
        };
+       unsigned int lcore_id;
+       struct rte_mempool_cache *cache;
 
        MEMPOOL_INIT_FUNC_TRACE();
 
@@ -115,7 +129,7 @@ dpaa_mbuf_create_pool(struct rte_mempool *mp)
        rte_dpaa_bpid_info[bpid].ptov_off = 0;
        rte_dpaa_bpid_info[bpid].flags = 0;
 
-       bp_info = rte_malloc(NULL,
+       bp_info = rte_zmalloc(NULL,
                             sizeof(struct dpaa_bp_info),
                             RTE_CACHE_LINE_SIZE);
        if (!bp_info) {
@@ -127,6 +141,20 @@ dpaa_mbuf_create_pool(struct rte_mempool *mp)
        rte_memcpy(bp_info, (void *)&rte_dpaa_bpid_info[bpid],
                   sizeof(struct dpaa_bp_info));
        mp->pool_data = (void *)bp_info;
+       s_dpaa_bpid_allocated_flag[bpid].flags = params.flags;
+       s_dpaa_bpid_allocated_flag[bpid].used = true;
+       /* Update per core mempool cache threshold to optimal value which is
+        * number of buffers that can be released to HW buffer pool in
+        * a single API call.
+        */
+       for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
+               cache = &mp->local_cache[lcore_id];
+               DPAA_MEMPOOL_DEBUG("lCore %d: cache->flushthresh %d -> %d",
+                       lcore_id, cache->flushthresh,
+                       (uint32_t)(cache->size + DPAA_MBUF_MAX_ACQ_REL));
+               if (cache->flushthresh)
+                       cache->flushthresh = cache->size + 
DPAA_MBUF_MAX_ACQ_REL;
+       }
 
        DPAA_MEMPOOL_INFO("BMAN pool created for bpid =%d", bpid);
        return 0;
@@ -136,6 +164,7 @@ static void
 dpaa_mbuf_free_pool(struct rte_mempool *mp)
 {
        struct dpaa_bp_info *bp_info = DPAA_MEMPOOL_TO_POOL_INFO(mp);
+       uint16_t i;
 
        MEMPOOL_INIT_FUNC_TRACE();
 
@@ -143,10 +172,25 @@ dpaa_mbuf_free_pool(struct rte_mempool *mp)
                bman_free_pool(bp_info->bp);
                DPAA_MEMPOOL_INFO("BMAN pool freed for bpid =%d",
                                  bp_info->bpid);
-               rte_free(mp->pool_data);
-               bp_info->bp = NULL;
+               rte_dpaa_bpid_info[bp_info->bpid].mp = NULL;
+               rte_dpaa_bpid_info[bp_info->bpid].bp = NULL;
+               s_dpaa_bpid_allocated_flag[bp_info->bpid].used = false;
+               rte_free(bp_info);
                mp->pool_data = NULL;
        }
+
+       if (!rte_dpaa_bpid_info)
+               return;
+
+       for (i = 0; i < DPAA_MAX_BPOOLS; i++) {
+               if (rte_dpaa_bpid_info[i].mp)
+                       break;
+       }
+
+       if (i == DPAA_MAX_BPOOLS) {
+               rte_free(rte_dpaa_bpid_info);
+               rte_dpaa_bpid_info = NULL;
+       }
 }
 
 static int
@@ -481,4 +525,21 @@ static const struct rte_mempool_ops dpaa_mpool_ops = {
        .populate = dpaa_populate,
 };
 
+#define RTE_PRIORITY_104 104
+
+RTE_FINI_PRIO(dpaa_mpool_finish, 104)
+{
+       uint16_t bpid;
+
+       for (bpid = 0; bpid < DPAA_MAX_BPOOLS; bpid++) {
+               if (s_dpaa_bpid_allocated_flag[bpid].used) {
+                       bman_free_bpid(bpid, 
s_dpaa_bpid_allocated_flag[bpid].flags);
+                       s_dpaa_bpid_allocated_flag[bpid].used = false;
+               }
+       }
+       /** The rte_dpaa_bpid_info and bman_pool from EAL mem have been released
+        * with EAL mem pool being destroyed.
+        */
+}
+
 RTE_MEMPOOL_REGISTER_OPS(dpaa_mpool_ops);
diff --git a/drivers/mempool/dpaa/dpaa_mempool.h 
b/drivers/mempool/dpaa/dpaa_mempool.h
index 865b533b8f..d7ee49b557 100644
--- a/drivers/mempool/dpaa/dpaa_mempool.h
+++ b/drivers/mempool/dpaa/dpaa_mempool.h
@@ -1,6 +1,6 @@
 /* SPDX-License-Identifier: BSD-3-Clause
  *
- *   Copyright 2017,2019,2024 -2025 NXP
+ *   Copyright 2017,2019,2024 -2026 NXP
  *
  */
 #ifndef __DPAA_MEMPOOL_H__
@@ -24,6 +24,7 @@
 
 /* total number of bpools on SoC */
 #define DPAA_MAX_BPOOLS        256
+#define DPAA_INVALID_BPID DPAA_MAX_BPOOLS
 
 /* Maximum release/acquire from BMAN */
 #define DPAA_MBUF_MAX_ACQ_REL  FSL_BM_BURST_MAX
-- 
2.25.1

Reply via email to