]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
erasure-code/jerasure: fix memory leak in Galois field operations 64226/head
authorKefu Chai <tchaikov@gmail.com>
Fri, 27 Jun 2025 13:56:23 +0000 (21:56 +0800)
committerKefu Chai <tchaikov@gmail.com>
Thu, 10 Jul 2025 09:31:13 +0000 (17:31 +0800)
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 <tchaikov@gmail.com>
src/erasure-code/jerasure/jerasure_init.cc
src/erasure-code/jerasure/jerasure_init.h

index 4c8c0e49dbef06cd3a19052b0849787f17ac897a..edc3e162461f138a87d78a169793142b99558cd7 100644 (file)
@@ -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);
+    }
+  }
+}
index 758287ae578363696e5debab6ea051d8bd79da5d..24694ee0f01753db3e621f5630c8ecb1af5783c0 100644 (file)
@@ -19,6 +19,7 @@
 #define CEPH_JERASURE_INIT_H
 
 extern "C" int jerasure_init(int count, int *words);
+void jerasure_finish() __attribute__((destructor));
 
 #endif