eal: add destructor to unregister tailq on unload
PATCH 1/1 eal: add destructor to unregister tailq on unload
Libraries that use EAL_REGISTER_TAILQ insert a pointer to a static struct rte_tailq_elem into the process-local tailq list via a constructor, but have no matching destructor. When such a library is loaded as a dependency of a plugin via dlopen() and later unloaded via dlclose(), the list retains a dangling pointer to the now-unmapped static. Reloading the plugin crashes in rte_eal_tailq_local_register() when it traverses the stale entry.
Add rte_eal_tailq_unregister() and extend the EAL_REGISTER_TAILQ macro to emit an RTE_FINI destructor alongside the existing RTE_INIT constructor. Every library that uses the macro automatically gets both sides; no per-library changes are needed.
Bugzilla ID: 1081
--- lib/eal/common/eal_common_tailqs.c | 8 ++++++++ lib/eal/include/rte_tailq.h | 17 +++++++++++++++++ 2 files changed, 25 insertions(+)
lib/eal/common/eal_common_tailqs.c +8 -0
@@ -148,6 +148,14 @@ rte_eal_tailq_register(struct rte_tailq_elem *t)return -1 ; } +RTE_EXPORT_SYMBOL (rte_eal_tailq_unregister) +void +rte_eal_tailq_unregister (struct rte_tailq_elem *t) +{ +TAILQ_REMOVE (&rte_tailq_elem_head, t, next); + t->head =NULL ; +} +int rte_eal_tailqs_init (void ) {
lib/eal/include/rte_tailq.h +17 -0
@@ -117,11 +117,28 @@ struct rte_tailq_head *rte_eal_tailq_lookup(const char *name);*/ int rte_eal_tailq_register (struct rte_tailq_elem *t); +/** +* Remove a tail queue element from the local list. +* This function is mainly used for EAL_REGISTER_TAILQ macro which pairs +* an RTE_FINI destructor with the existing RTE_INIT constructor. +* The destructor calls this function during dlclose() to prevent +* dangling pointers to unmapped library data. +* +* @param t +* The tailq element which contains the name of the tailq you want to +* delete +*/ +void rte_eal_tailq_unregister (struct rte_tailq_elem *t); +#define EAL_REGISTER_TAILQ (t) \ RTE_INIT(tailqinitfn_ ##t) \ { \ if (rte_eal_tailq_register(&t) < 0) \ rte_panic("Cannot initialize tailq: %s\n", t.name); \ +} \ +RTE_FINI(tailqfinifn_ ##t) \ +{ \ + rte_eal_tailq_unregister(&t); \ }/* This macro permits both remove and free var within the loop safely.*/
Signature · 1 line
2.53.0
Quoted 13 lines · Stephen Hemminger
Libraries that use
EAL_REGISTER_TAILQinsert a pointer to a static structrte_tailq_eleminto the process-local tailq list via a constructor, but have no matching destructor. When such a library is loaded as a dependency of a plugin viadlopen()and later unloaded viadlclose(), the list retains a dangling pointer to the now-unmapped static. Reloading the plugin crashes inrte_eal_tailq_local_register()when it traverses the stale entry.Add
rte_eal_tailq_unregister()and extend theEAL_REGISTER_TAILQmacro to emit anRTE_FINIdestructor alongside the existingRTE_INITconstructor. Every library that uses the macro automatically gets both sides; no per-library changes are needed.
Quoted 6 lines
Bugzilla ID: 1081
Fixes 873a61c7526b ("tailq: introduce dynamic register system")Signed-off-by Stephen Hemminger <[email protected]>---
Quoted 29 lines · Stephen Hemminger
Libraries that use
EAL_REGISTER_TAILQinsert a pointer to a static structrte_tailq_eleminto the process-local tailq list via a constructor, but have no matching destructor. When such a library is loaded as a dependency of a plugin viadlopen()and later unloaded viadlclose(), the list retains a dangling pointer to the now-unmapped static. Reloading the plugin crashes inrte_eal_tailq_local_register()when it traverses the stale entry.Add
rte_eal_tailq_unregister()and extend theEAL_REGISTER_TAILQmacro to emit anRTE_FINIdestructor alongside the existingRTE_INITconstructor. Every library that uses the macro automatically gets both sides; no per-library changes are needed.Bugzilla ID: 1081
Fixes 873a61c7526b ("tailq: introduce dynamic register system")Signed-off-by Stephen Hemminger <[email protected]>--- lib/eal/common/eal_common_tailqs.c | 8 ++++++++ lib/eal/include/rte_tailq.h | 17 +++++++++++++++++ 2 files changed, 25 insertions(+)lib/eal/common/eal_common_tailqs.c +0
-0
lib/eal/common/eal_common_tailqs.c rte_eal_tailq_register(struct rte_tailq_elem *t) · line 157
Quoted 10 lines
lib/eal/common/eal_common_tailqs.c +7
-0@@ -148,6 +148,14 @@ rte_eal_tailq_register(struct rte_tailq_elem *t)return -1 ; } +RTE_EXPORT_SYMBOL (rte_eal_tailq_unregister) +void +rte_eal_tailq_unregister (struct rte_tailq_elem *t) +{ +TAILQ_REMOVE (&rte_tailq_elem_head, t, next); + t->head =NULL ; +}
This cleans up the local storage which is good, but it leaves an entry reserved in the mcfg->tailq_head[] array.
Signature · 1 line
David Marchand
PATCH v2 1/1 eal: add destructor to unregister tailq on unload
In reply to Stephen HemmingerEAL_REGISTER_TAILQ registers a static rte_tailq_elem from a constructor but provides no destructor. If a library using the macro is loaded with dlopen() and later unloaded with dlclose(), the process-local list keeps a dangling pointer to the unmapped elem, and the next dlopen() crashes in rte_eal_tailq_local_register() while walking the list.
Add a new RTE_FINI destructor that is paired with the constructor in the macro. rte_eal_tailq_unregister() drops the local entry on unload. The shared mcfg->tailq_head[] slot is left reserved since it is keyed by name and shared between processes; rte_eal_tailq_update() now reattaches to that slot on re-register instead of failing.
Bugzilla ID: 1081
---
v2 - cover the case where name still is reserved
lib/eal/common/eal_common_tailqs.c | 13 +++++++++++++ lib/eal/include/rte_tailq.h | 16 ++++++++++++++++ 2 files changed, 29 insertions(+)
lib/eal/common/eal_common_tailqs.c +13 -0
@@ -113,6 +113,11 @@ rte_eal_tailq_update(struct rte_tailq_elem *t)if (rte_eal_process_type () ==RTE_PROC_PRIMARY ) {/* primary process is the only one that creates */ t->head =rte_eal_tailq_create (t->name); + +if (t->head ==NULL ) { +/* slot reserved by an earlier load -- reuse it */ + t->head =rte_eal_tailq_lookup (t->name); + } }else { t->head =rte_eal_tailq_lookup (t->name); }
@@ -148,6 +153,14 @@ rte_eal_tailq_register(struct rte_tailq_elem *t)return -1 ; } +RTE_EXPORT_SYMBOL (rte_eal_tailq_unregister) +void +rte_eal_tailq_unregister (struct rte_tailq_elem *t) +{ +TAILQ_REMOVE (&rte_tailq_elem_head, t, next); + t->head =NULL ; +} +int rte_eal_tailqs_init (void ) {
lib/eal/include/rte_tailq.h +16 -0
@@ -117,11 +117,27 @@ struct rte_tailq_head *rte_eal_tailq_lookup(const char *name);*/ int rte_eal_tailq_register (struct rte_tailq_elem *t); +/** +* Remove a tail queue element from the local list. +* This function is mainly used for EAL_REGISTER_TAILQ macro which pairs +* an RTE_FINI destructor with the existing RTE_INIT constructor. +* The destructor calls this function during dlclose() to prevent +* dangling pointers to unmapped library data. +* +* @param t +* The tailq element to remove from the EAL tailq list. +*/ +void rte_eal_tailq_unregister (struct rte_tailq_elem *t); +#define EAL_REGISTER_TAILQ (t) \ RTE_INIT(tailqinitfn_ ##t) \ { \ if (rte_eal_tailq_register(&t) < 0) \ rte_panic("Cannot initialize tailq: %s\n", t.name); \ +} \ +RTE_FINI(tailqfinifn_ ##t) \ +{ \ + rte_eal_tailq_unregister(&t); \ }/* This macro permits both remove and free var within the loop safely.*/
Signature · 1 line
2.53.0
Quoted 47 lines · Stephen Hemminger
EAL_REGISTER_TAILQregisters a staticrte_tailq_elemfrom a constructor but provides no destructor. If a library using the macro is loaded withdlopen()and later unloaded withdlclose(), the process-local list keeps a dangling pointer to the unmapped elem, and the nextdlopen()crashes inrte_eal_tailq_local_register()while walking the list.Add a new
RTE_FINIdestructor that is paired with the constructor in the macro.rte_eal_tailq_unregister()drops the local entry on unload. The shared mcfg->tailq_head[] slot is left reserved since it is keyed by name and shared between processes;rte_eal_tailq_update()now reattaches to that slot on re-register instead of failing.Bugzilla ID: 1081
Fixes 873a61c7526b ("tailq: introduce dynamic register system")Signed-off-by Stephen Hemminger <[email protected]>Acked-by Bruce Richardson <[email protected]>---
v2 - cover the case where name still is reservedlib/eal/common/eal_common_tailqs.c | 13 +++++++++++++ lib/eal/include/rte_tailq.h | 16 ++++++++++++++++ 2 files changed, 29 insertions(+)lib/eal/common/eal_common_tailqs.c +5
-0@@ -113,6 +113,11 @@ rte_eal_tailq_update(struct rte_tailq_elem *t)if (rte_eal_process_type () ==RTE_PROC_PRIMARY ) {/* primary process is the only one that creates */ t->head =rte_eal_tailq_create (t->name); + +if (t->head ==NULL ) { +/* slot reserved by an earlier load -- reuse it */ + t->head =rte_eal_tailq_lookup (t->name); + }lib/eal/common/eal_common_tailqs.c +0
-0}else { t->head =rte_eal_tailq_lookup (t->name); }
lib/eal/common/eal_common_tailqs.c rte_eal_tailq_register(struct rte_tailq_elem *t) · line 156
Quoted 4 lines
lib/eal/common/eal_common_tailqs.c +1
-0@@ -148,6 +153,14 @@ rte_eal_tailq_register(struct rte_tailq_elem *t)return -1 ; } +RTE_EXPORT_SYMBOL (rte_eal_tailq_unregister)
this should be with EXPERIMENTAL
lib/eal/common/eal_common_tailqs.c rte_eal_tailq_register(struct rte_tailq_elem *t) · line 160
Quoted 4 lines
lib/eal/common/eal_common_tailqs.c +4
-0+void +rte_eal_tailq_unregister (struct rte_tailq_elem *t) +{ +TAILQ_REMOVE (&rte_tailq_elem_head, t, next);
We need first make sure it exist the tailq, just like TAILQ_FOREACH rte_eal_tailq_local_register()
Quoted 11 lines
lib/eal/common/eal_common_tailqs.c +3
-0+ t->head =NULL ; +} +int rte_eal_tailqs_init (void ) {lib/eal/include/rte_tailq.h +0
-0
lib/eal/include/rte_tailq.h struct rte_tailq_head *rte_eal_tailq_lookup(const char *name); · line 125
Quoted 9 lines
lib/eal/include/rte_tailq.h +6
-0@@ -117,11 +117,27 @@ struct rte_tailq_head *rte_eal_tailq_lookup(const char *name);*/ int rte_eal_tailq_register (struct rte_tailq_elem *t); +/** +* Remove a tail queue element from the local list. +* This function is mainly used for EAL_REGISTER_TAILQ macro which pairs +* an RTE_FINI destructor with the existing RTE_INIT constructor. +* The destructor calls this function during dlclose() to prevent +* dangling pointers to unmapped library data.
Currently this patch only support next dlopen() the same so file. the global still exist. Suggest add document for the global entry still exist, just like commit-log:
"The shared mcfg->tailq_head[] slot is left reserved since
it is keyed by name and shared between processes;"
Quoted 18 lines
lib/eal/include/rte_tailq.h +10
-0+* +* @param t +* The tailq element to remove from the EAL tailq list. +*/ +void rte_eal_tailq_unregister (struct rte_tailq_elem *t); +#define EAL_REGISTER_TAILQ (t) \ RTE_INIT(tailqinitfn_ ##t) \ { \ if (rte_eal_tailq_register(&t) < 0) \ rte_panic("Cannot initialize tailq: %s\n", t.name); \ +} \ +RTE_FINI(tailqfinifn_ ##t) \ +{ \ + rte_eal_tailq_unregister(&t); \ }/* This macro permits both remove and free var within the loop safely.*/
Quoted 4 lines · On Wed, 10 Jun 2026 09:19:42 +0800 fengchengwen <[email protected]> wrote:
Quoted 2 lines
+RTE_EXPORT_SYMBOL(rte_eal_tailq_unregister)
this should be with EXPERIMENTAL
Not possible, this is part of the EAL_REGISTER_TAILQ macro and usage is under the covers. So if anything was marked experimental it would fail code that did not allow experimental
Quoted 7 lines
lib/eal/common/eal_common_tailqs.c rte_eal_tailq_register(struct rte_tailq_elem *t) · line 160
Quoted 5 lines
lib/eal/common/eal_common_tailqs.c +4
-0+void +rte_eal_tailq_unregister (struct rte_tailq_elem *t) +{ +TAILQ_REMOVE (&rte_tailq_elem_head, t, next);We need first make sure it exist the tailq, just like
TAILQ_FOREACHrte_eal_tailq_local_register()
Ok cheap scan since not in critical path.
Quoted 5 lines · Stephen Hemminger
Quoted 1 line · On Wed, 10 Jun 2026 09:19:42 +0800 fengchengwen <[email protected]> wrote:
Quoted 1 line
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
+RTE_EXPORT_SYMBOL(rte_eal_tailq_unregister)
this should be with EXPERIMENTAL
Not possible, this is part of the
EAL_REGISTER_TAILQmacro and usage is under the covers. So if anything was marked experimental it would fail code that did not allow experimental
Indeed.
Quoted 8 lines
Quoted 6 lines
lib/eal/common/eal_common_tailqs.c rte_eal_tailq_register(struct rte_tailq_elem *t) · line 160
Quoted 4 lines
lib/eal/common/eal_common_tailqs.c +4
-0+void +rte_eal_tailq_unregister (struct rte_tailq_elem *t) +{ +TAILQ_REMOVE (&rte_tailq_elem_head, t, next);We need first make sure it exist the tailq, just like
TAILQ_FOREACHrte_eal_tailq_local_register()Ok cheap scan since not in critical path.
I had excluded this point when looking at the v2 patch, considering that abort() is called in the constructor on failure, and destructors are not called after abort().
Is your concern that rte_eal_tailq_unregister could be called directory by the application with random pointer?
A check would be safer on paper, but it seems strange to protect here.
Am I missing another case?
Signature · 1 line
David Marchand
v3 cover eal: tailq fixes
In reply to Stephen HemmingerA couple of small fixes to EAL tailq
Stephen Hemminger (2):
eal: fix off by one in in tailq name init
eal: add destructor to unregister tailq on unload
lib/eal/common/eal_common_tailqs.c | 15 ++++++++++++++- lib/eal/include/rte_tailq.h | 16 ++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-)
Signature · 1 line
2.53.0
PATCH v3 1/2 eal: fix off by one in in tailq name init
The tailq name is defined as 32 bytes, but name would be silently truncated at 31 bytes. The function strlcpy() size already accounts for the nul character at the end.
---
Exposed by automated review of next patch.
lib/eal/common/eal_common_tailqs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/eal/common/eal_common_tailqs.c +1 -1
@@ -83,7 +83,7 @@ rte_eal_tailq_create(const char *name) mcfg =rte_eal_get_configuration ()->mem_config; head = &mcfg->tailq_head[rte_tailqs_count]; -strlcpy (head->name, name,sizeof (head->name) -1 ); +strlcpy (head->name, name,sizeof (head->name));TAILQ_INIT (&head->tailq_head); rte_tailqs_count++; }
Signature · 1 line
2.53.0
Re: [PATCH v3 1/2] eal: fix off by one in in tailq name init
Quoted 3 lines · Stephen Hemminger
The tailq name is defined as 32 bytes, but name would be silently truncated at 31 bytes. The function
strlcpy()size already accounts for the nul character at the end.
nul -> NUL
Quoted 6 lines
Fixes f9acaf84e923 ("replace snprintf with strlcpy without adding extra include")Signed-off-by Stephen Hemminger <[email protected]>
David Marchand
AppliedRe: [PATCH v3 1/2] eal: fix off by one in in tailq name init
Quoted 6 lines · fengchengwen
Quoted 4 lines · Stephen Hemminger
The tailq name is defined as 32 bytes, but name would be silently truncated at 31 bytes. The function
strlcpy()size already accounts for the NUL character at the end.
Bugzilla ID: 1954
Quoted 5 lines
Quoted 4 lines
Fixes f9acaf84e923 ("replace snprintf with strlcpy without adding extra include")Signed-off-by Stephen Hemminger <[email protected]>Acked-by Chengwen Feng <[email protected]>
Applied, thanks.
Signature · 1 line
David Marchand
PATCH v3 2/2 eal: add destructor to unregister tailq on unload
In reply to Stephen HemmingerEAL_REGISTER_TAILQ registers a static rte_tailq_elem from a constructor but provides no destructor. If a library using the macro is loaded with dlopen() and later unloaded with dlclose(), the process-local list keeps a dangling pointer to the unmapped elem, and the next dlopen() crashes in rte_eal_tailq_local_register() while walking the list.
Add a new RTE_FINI destructor that is paired with the constructor in the macro. rte_eal_tailq_unregister() drops the local entry on unload. The shared mcfg->tailq_head[] slot is left reserved since it is keyed by name and shared between processes; rte_eal_tailq_update() now reattaches to that slot on re-register instead of failing.
Bugzilla ID: 1081
--- lib/eal/common/eal_common_tailqs.c | 13 +++++++++++++ lib/eal/include/rte_tailq.h | 16 ++++++++++++++++ 2 files changed, 29 insertions(+)
lib/eal/common/eal_common_tailqs.c +13 -0
@@ -113,6 +113,11 @@ rte_eal_tailq_update(struct rte_tailq_elem *t)if (rte_eal_process_type () ==RTE_PROC_PRIMARY ) {/* primary process is the only one that creates */ t->head =rte_eal_tailq_create (t->name); + +if (t->head ==NULL ) { +/* slot reserved by an earlier load -- reuse it */ + t->head =rte_eal_tailq_lookup (t->name); + } }else { t->head =rte_eal_tailq_lookup (t->name); }
@@ -148,6 +153,14 @@ rte_eal_tailq_register(struct rte_tailq_elem *t)return -1 ; } +RTE_EXPORT_SYMBOL (rte_eal_tailq_unregister) +void +rte_eal_tailq_unregister (struct rte_tailq_elem *t) +{ +TAILQ_REMOVE (&rte_tailq_elem_head, t, next); + t->head =NULL ; +} +int rte_eal_tailqs_init (void ) {
lib/eal/include/rte_tailq.h +16 -0
@@ -117,11 +117,27 @@ struct rte_tailq_head *rte_eal_tailq_lookup(const char *name);*/ int rte_eal_tailq_register (struct rte_tailq_elem *t); +/** +* Remove a tail queue element from the local list. +* This function is mainly used for EAL_REGISTER_TAILQ macro which pairs +* an RTE_FINI destructor with the existing RTE_INIT constructor. +* The destructor calls this function during dlclose() to prevent +* dangling pointers to unmapped library data. +* +* @param t +* The tailq element to remove from the EAL tailq list. +*/ +void rte_eal_tailq_unregister (struct rte_tailq_elem *t); +#define EAL_REGISTER_TAILQ (t) \ RTE_INIT(tailqinitfn_ ##t) \ { \ if (rte_eal_tailq_register(&t) < 0) \ rte_panic("Cannot initialize tailq: %s\n", t.name); \ +} \ +RTE_FINI(tailqfinifn_ ##t) \ +{ \ + rte_eal_tailq_unregister(&t); \ }/* This macro permits both remove and free var within the loop safely.*/
Signature · 1 line
2.53.0