mm: kmemleak: factor leak confirmation into a helper
authorBreno Leitao <leitao@debian.org>
Mon, 13 Jul 2026 11:48:06 +0000 (04:48 -0700)
committerAndrew Morton <akpm@linux-foundation.org>
Tue, 25 Aug 2026 01:43:17 +0000 (18:43 -0700)
The reporting loop in kmemleak_scan() decided whether to tag an object as
a reported leak with a four-term compound condition whose last operand
also had a side effect (++object->unref_scans).  Mixing the candidate
tests with the counter update made the check hard to read.

Move the state transition into confirm_leak(): it returns true when a
still-unreferenced suspect crosses min_unref_scans consecutive scans and
is newly flagged OBJECT_REPORTED, leaving only the reporting bookkeeping
in the caller.  No functional change.

Link: https://lore.kernel.org/20260713-catalin_pto-v1-3-5b93b1131089@debian.org
Signed-off-by: Breno Leitao <leitao@debian.org>
Reviewed-by: Catalin Marinas <catalin.marinas@arm.com>
Cc: David Hildenbrand <david@kernel.org>
Cc: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Jonathan Corbet <corbet@lwn.net>
Cc: Liam R. Howlett <liam@infradead.org>
Cc: Lorenzo Stoakes <ljs@kernel.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Shuah Khan <shuah@kernel.org>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Vlastimil Babka <vbabka@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
mm/kmemleak.c

index 7afd08e..f63dfac 100644 (file)
@@ -2014,6 +2014,26 @@ scan_gray:
        return nr_suspects;
 }
 
+/*
+ * Promote a suspected object to a reported leak once it has stayed
+ * unreferenced for min_unref_scans consecutive scans. Called with
+ * object->lock held; returns true when the object is newly reported.
+ */
+static bool confirm_leak(struct kmemleak_object *object)
+{
+       if (!unreferenced_object(object) ||
+           !(object->flags & OBJECT_SUSPECT) ||
+           (object->flags & OBJECT_REPORTED))
+               return false;
+
+       object->unref_scans += 1;
+       if (object->unref_scans < min_unref_scans)
+               return false;
+
+       object->flags |= OBJECT_REPORTED;
+       return true;
+}
+
 /*
  * Scan the memory and report the unreferenced objects as leaks. Must be
  * called with the scan_mutex held.
@@ -2074,11 +2094,7 @@ static void kmemleak_scan(void)
                trace_handle = 0;
                dedup_print = false;
 
-               if (unreferenced_object(object) &&
-                   (object->flags & OBJECT_SUSPECT) &&
-                   !(object->flags & OBJECT_REPORTED) &&
-                   ++object->unref_scans >= min_unref_scans) {
-                       object->flags |= OBJECT_REPORTED;
+               if (confirm_leak(object)) {
                        if (kmemleak_verbose) {
                                trace_handle = object->trace_handle;
                                dedup_print = true;