Skip to content

Commit

Permalink
eal: avoid calling cleanup twice
Browse files Browse the repository at this point in the history
[ upstream commit a4a2ac988679b3802a574e7bb72154da177449a4 ]

If an app calls rte_eal_cleanup() inside it's own code, then cleanup
could be called a second time automatically when the app exits. While
mostly harmless, we can avoid any potential issues by guaranteeing that
cleanup only gets called once, in the same way that eal_init only ever
gets called once.

Note: This patch only touches Linux and FreeBSD. Windows EAL does not
have run-once guard on the init function, so omitting it in the cleanup
function.

Fixes: aec9c13 ("eal: add function to release internal resources")

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
  • Loading branch information
bruce-richardson authored and kevintraynor committed Jul 11, 2023
1 parent c2f6df5 commit e506470
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/eal/common/eal_common_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
*/

#include <stdarg.h>
#include <errno.h>

#include <rte_eal.h>
#include <rte_log.h>
#include <rte_debug.h>
#include <rte_errno.h>

void
__rte_panic(const char *funcname, const char *format, ...)
Expand Down Expand Up @@ -37,7 +40,7 @@ rte_exit(int exit_code, const char *format, ...)
rte_vlog(RTE_LOG_CRIT, RTE_LOGTYPE_EAL, format, ap);
va_end(ap);

if (rte_eal_cleanup() != 0)
if (rte_eal_cleanup() != 0 && rte_errno != EALREADY)
RTE_LOG(CRIT, EAL,
"EAL could not release all resources\n");
exit(exit_code);
Expand Down
10 changes: 10 additions & 0 deletions lib/eal/freebsd/eal.c
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,16 @@ rte_eal_init(int argc, char **argv)
int
rte_eal_cleanup(void)
{
static uint32_t run_once;
uint32_t has_run = 0;

if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
RTE_LOG(WARNING, EAL, "Already called cleanup\n");
rte_errno = EALREADY;
return -1;
}

struct internal_config *internal_conf =
eal_get_internal_configuration();
rte_service_finalize();
Expand Down
10 changes: 10 additions & 0 deletions lib/eal/linux/eal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,16 @@ mark_freeable(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
int
rte_eal_cleanup(void)
{
static uint32_t run_once;
uint32_t has_run = 0;

if (!__atomic_compare_exchange_n(&run_once, &has_run, 1, 0,
__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
RTE_LOG(WARNING, EAL, "Already called cleanup\n");
rte_errno = EALREADY;
return -1;
}

/* if we're in a primary process, we need to mark hugepages as freeable
* so that finalization can release them back to the system.
*/
Expand Down

0 comments on commit e506470

Please sign in to comment.