[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20251223-b4-kunit-user-alloc-v1-3-fb910ae0e50c@google.com>
Date: Tue, 23 Dec 2025 16:18:12 +0000
From: Brendan Jackman <jackmanb@...gle.com>
To: Brendan Higgins <brendan.higgins@...ux.dev>, David Gow <davidgow@...gle.com>,
Rae Moar <raemoar63@...il.com>, Kees Cook <kees@...nel.org>, Ingo Molnar <mingo@...hat.com>,
Peter Zijlstra <peterz@...radead.org>, Juri Lelli <juri.lelli@...hat.com>,
Vincent Guittot <vincent.guittot@...aro.org>, Dietmar Eggemann <dietmar.eggemann@....com>,
Steven Rostedt <rostedt@...dmis.org>, Ben Segall <bsegall@...gle.com>, Mel Gorman <mgorman@...e.de>,
Valentin Schneider <vschneid@...hat.com>, Andrew Morton <akpm@...ux-foundation.org>,
David Hildenbrand <david@...nel.org>, Lorenzo Stoakes <lorenzo.stoakes@...cle.com>,
"Liam R. Howlett" <Liam.Howlett@...cle.com>, Vlastimil Babka <vbabka@...e.cz>,
Mike Rapoport <rppt@...nel.org>, Suren Baghdasaryan <surenb@...gle.com>, Michal Hocko <mhocko@...e.com>
Cc: linux-kselftest@...r.kernel.org, kunit-dev@...glegroups.com,
linux-kernel@...r.kernel.org, linux-mm@...ck.org,
Brendan Jackman <jackmanb@...gle.com>
Subject: [PATCH 3/3] kunit: test: fix mm_struct leak in kunit_attach_mm()
Here's how I understand mm refcounts:
funcs | counter | manages lifecycle of...
--------------------------------------------------------
mmgrab()/mmdrop() | mm_count | mm_struct and PGD
--------------------------------------------------------
mmget()/mmput() | mm_users | userspace address space
All mm_users references share a single reference to the mm_struct.
mm_alloc() returns the mm with a single reference to the user address
space, i.e. with mm_users=1, mm_count=1.
kunit_attach_mm() then passes the mm to kthread_use_mm(). It does not
call kthread_unuse_mm(), instead it relies on the kthread exit path to
release the relevant resources. It does this because KUnit's resource
cleanup logic works by running cleanups in a different kthread from the
test. You can't have cleanups that operate on the kthread, because
the kthread is already gone by the time the cleanup is called.
The kthread exit path will indeed drop the reference to the address
space, i.e. it will call mmput(task->mm), decrementing mm_users.
However, it does not release the reference taken on the mm_struct when
kthread_use_mm() called mmgrab().
To fix this, use the new kthread_take_mm() which provides the API KUnit
needs.
Signed-off-by: Brendan Jackman <jackmanb@...gle.com>
---
lib/kunit/user_alloc.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/kunit/user_alloc.c b/lib/kunit/user_alloc.c
index 564f5566641d5..3fca4ae223f67 100644
--- a/lib/kunit/user_alloc.c
+++ b/lib/kunit/user_alloc.c
@@ -29,7 +29,7 @@ int kunit_attach_mm(void)
arch_pick_mmap_layout(mm, ¤t->signal->rlim[RLIMIT_STACK]);
/* Attach the mm. It will be cleaned up when the process dies. */
- kthread_use_mm(mm);
+ kthread_take_mm(mm);
return 0;
}
--
2.51.2
Powered by blists - more mailing lists