[PATCH 2/2] lib/graph: rte_malloc for cache-aligned structs
Jerin Jacob
jerinjacobk at gmail.com
Tue Jun 17 14:46:05 CEST 2025
On Tue, Jun 17, 2025 at 4:32 PM Marat Khalili <marat.khalili at huawei.com> wrote:
>
> This was flagged by undefined behaviour sanitizer: struct
> rte_graph_cluster_stats is declared as `__rte_cache_aligned` but was
> allocated using stdlib realloc which caused misaligned allocation. More
> than one test needs to be executed in series in order to reproduce the
> problem using graph_autotest, e.g:
>
> app/dpdk-test --no-huge --no-pci -m128 graph_autotest graph_autotest
>
> First sanitizer message (similar ones follow):
>
> lib/graph/graph_stats.c:209:13: runtime error: member access within
> misaligned address 0x606000008ea0 for type 'struct
> rte_graph_cluster_stats', which requires 64 byte alignment
>
> To fix the issue replace realloc calls with rte_malloc and rte_realloc
> specifying correct alignment, use rte_free to free the result.
>
> Signed-off-by: Marat Khalili <marat.khalili at huawei.com>
Since this memory is used in slowpath, heap memory is fine.
I think, better fix will be to remove cache alignment from
rte_graph_cluster_stats.
Not sure it will call for ABI change though.Run
devtools/test-meson-builds.sh to validate any ABI breakage.
> ---
> lib/graph/graph_stats.c | 9 +++++----
> 1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/lib/graph/graph_stats.c b/lib/graph/graph_stats.c
> index 57cd72e7cc..7ae8ee4987 100644
> --- a/lib/graph/graph_stats.c
> +++ b/lib/graph/graph_stats.c
> @@ -203,7 +203,7 @@ stats_mem_init(struct cluster *cluster,
> cluster_node_size += cluster->nb_graphs * sizeof(struct rte_node *);
> cluster_node_size = RTE_ALIGN(cluster_node_size, RTE_CACHE_LINE_SIZE);
>
> - stats = realloc(NULL, sz);
> + stats = rte_malloc(NULL, sz, RTE_CACHE_LINE_SIZE);
> if (stats) {
> memset(stats, 0, sz);
> stats->fn = fn;
> @@ -248,7 +248,8 @@ stats_mem_populate(struct rte_graph_cluster_stats **stats_in,
> }
>
> /* Hey, it is a new node, allocate space for it in the reel */
> - stats = realloc(stats, stats->sz + stats->cluster_node_size);
> + stats = rte_realloc(stats, stats->sz + stats->cluster_node_size,
> + RTE_CACHE_LINE_SIZE);
> if (stats == NULL)
> SET_ERR_JMP(ENOMEM, err, "Realloc failed");
> *stats_in = NULL;
> @@ -301,7 +302,7 @@ stats_mem_populate(struct rte_graph_cluster_stats **stats_in,
>
> return 0;
> free:
> - free(stats);
> + rte_free(stats);
> err:
> return -rte_errno;
> }
> @@ -309,7 +310,7 @@ stats_mem_populate(struct rte_graph_cluster_stats **stats_in,
> static void
> stats_mem_fini(struct rte_graph_cluster_stats *stats)
> {
> - free(stats);
> + rte_free(stats);
> }
>
> static void
> --
> 2.43.0
>
More information about the dev
mailing list