From 08d7363fb2e7371bac331cbcdc693a092e6f6fad Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 27 Jun 2025 21:56:23 +0800 Subject: [PATCH] erasure-code/jerasure: fix memory leak in Galois field operations Fix a memory leak where Galois field acceleration functions created by ErasureCodeJerasure::prepare() were never freed. ASan detected this as a one-time leak when the plugin was unloaded. Add jerasure_finish() destructor function in jerasure_init.cc to free the allocated Galois field operations. Since jerasure_init.cc and galois.c are built into the same object library, jerasure_finish() can access and clean up the global static acceleration functions defined in galois.c. The destructor function is automatically called when the shared library (plugin) is unloaded, ensuring proper cleanup without requiring explicit calls from client code. Signed-off-by: Kefu Chai --- src/erasure-code/jerasure/jerasure_init.cc | 20 ++++++++++++++++++++ src/erasure-code/jerasure/jerasure_init.h | 1 + 2 files changed, 21 insertions(+) diff --git a/src/erasure-code/jerasure/jerasure_init.cc b/src/erasure-code/jerasure/jerasure_init.cc index 4c8c0e49dbef0..edc3e162461f1 100644 --- a/src/erasure-code/jerasure/jerasure_init.cc +++ b/src/erasure-code/jerasure/jerasure_init.cc @@ -35,3 +35,23 @@ extern "C" int jerasure_init(int count, int *words) } return 0; } + +void jerasure_finish() +{ + // jerasure based codings generate matrices using Galois field operations via + // the Jerasure library. The underlying acceleration functions for different + // word sizes are cached in global static variables and initialized lazily. + // These cached functions must be explicitly freed after erasure coding + // operations complete to prevent memory leaks. + // Note: + // - Operations for word sizes > 32 bits are not yet implemented + // - Jerasure only supports word sizes that are power of 2. + static const int words[] = {4, 8, 16, 32}; + for (auto w : words) { + gf_t* gf = galois_get_field_ptr(w); + if (gf) { + gf_free(gf, 0); + free(gf); + } + } +} diff --git a/src/erasure-code/jerasure/jerasure_init.h b/src/erasure-code/jerasure/jerasure_init.h index 758287ae57836..24694ee0f0175 100644 --- a/src/erasure-code/jerasure/jerasure_init.h +++ b/src/erasure-code/jerasure/jerasure_init.h @@ -19,6 +19,7 @@ #define CEPH_JERASURE_INIT_H extern "C" int jerasure_init(int count, int *words); +void jerasure_finish() __attribute__((destructor)); #endif -- 2.39.5