[PATCH v3] kmemleaak: survive in a low-memory situation

From: Qian Cai
Date: Tue Mar 26 2019 - 11:43:56 EST


Kmemleak could quickly fail to allocate an object structure and then
disable itself in a low-memory situation. For example, running a mmap()
workload triggering swapping and OOM. This is especially problematic for
running things like LTP testsuite where one OOM test case would disable
the whole kmemleak and render the rest of test cases without kmemleak
watching for leaking.

Kmemleak allocation could fail even though the tracked memory is
succeeded. Hence, it could still try to start a direct reclaim if it is
not executed in an atomic context (spinlock, irq-handler etc), or a
high-priority allocation in an atomic context as a last-ditch effort.
Since kmemleak is a debug feature, it is unlikely to be used in
production that memory resources is scarce where direct reclaim or
high-priority atomic allocations should not be granted lightly.

Unless there is a brave soul to reimplement the kmemleak to embed it's
metadata into the tracked memory itself in a foreseeable future, this
provides a good balance between enabling kmemleak in a low-memory
situation and not introducing too much hackiness into the existing
code for now.

Signed-off-by: Qian Cai <cai@xxxxxx>
---

v3: Update the commit log.
Simplify the code inspired by graph_trace_open() from ftrace.
v2: Remove the needless checking for NULL objects in slab_post_alloc_hook()
per Catalin.

mm/kmemleak.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index a2d894d3de07..239927166894 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -581,6 +581,17 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
unsigned long untagged_ptr;

object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
+ if (!object) {
+ /*
+ * The tracked memory was allocated successful, if the kmemleak
+ * object failed to allocate for some reasons, it ends up with
+ * the whole kmemleak disabled, so let it success at all cost.
+ */
+ gfp = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC :
+ gfp_kmemleak_mask(gfp) | __GFP_DIRECT_RECLAIM;
+ object = kmem_cache_alloc(object_cache, gfp);
+ }
+
if (!object) {
pr_warn("Cannot allocate a kmemleak_object structure\n");
kmemleak_disable();
--
2.17.2 (Apple Git-113)