From nobody Sat Feb 7 15:40:26 2026 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D477830BF60 for ; Tue, 23 Dec 2025 16:18:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766506707; cv=none; b=RiAI3fHd8SFFUza3zk+pHHfLQjqtzNJ2+nqvUeSsZSFR503jK9WIskkzZY8/guOMpMFSBa+e/EF3TYQqBpkahjF3buN18Itxd8MG39TbNGABEGX8qYcveSCgfOE4yHi6DXp5FE9bxuZht1EtKhvs7lceD5rENYQ9vrLwBkfFXv0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766506707; c=relaxed/simple; bh=s9uG3y0OEfewAV0wFIr6ZC5yVfYH9oBTmmtdzG+B/5o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=GjLVah/VeDJ+kING0VCUEJTxi9ERQWiwajNOBtcQh2WR+M/QXbMaR7+UW27cdu5h3jqWYuFWrWqw07gisJy9KwuvRSCV44StCCKmkL1T7iSM6KL4aVzGr0sXtz8LvKWJLqWJBbh0+kEJFAguCCIbzihYiQrrh+ldVTAxNdIjzss= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=FF/9fvCn; arc=none smtp.client-ip=209.85.218.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FF/9fvCn" Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-b7387d9bbb2so391326566b.0 for ; Tue, 23 Dec 2025 08:18:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1766506704; x=1767111504; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/0Hzxx2WqJaEHIGdZr2YZs1v307yu6j+9XrMYnyKbj4=; b=FF/9fvCnc2heZYNxaHme9ql97pjM1OeXrBbiC00EKYlV0ueLPJ1TA/1Xd5/dFFztQC 19OE0FmanBAU9LeBzrDz6DebuEEXS6dZz0Oh6E12UVUr7SGgDZaCJ61/fMIICKk/xZCd tmIRVkUWI+Gu6thXnTnkw+7OA9UAr0OhLuBx35gwdG3GmJgn0sstoZ3vA72TUHLv6lwR wHuSOPq4Csaya8F7NZ4r8bu1rQecXzn5hsN8keoslAG3NsGuiFxjMh16TOR7mv/RBzAi ygbYi1n9B2NHIxOKRM8kGwmC5DfiCBIBZdaEeVtR9puDjoZLFOqc1SArTlndutVOujHk o7+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766506704; x=1767111504; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/0Hzxx2WqJaEHIGdZr2YZs1v307yu6j+9XrMYnyKbj4=; b=dJKy+GOFY1sCzyuxa89yh2lfWyBZcrnLZVW+4/7meebzgf8OTRx8qHF8taBpR+xcsx y4YJgkZLUuKjJHdou/roetWpRw6s1KheBTg5UdttdLYoPTxbOvrho24gptE5qxXjxEDN DIYYhCyfJPZde8HGwYnpWtB6u7xk3FvJGxj2P0aM1nDIzGIG/oTDVSFNsQDyDZRAokre Kyl/c1OXSdZmFct8aOVFzqpJI1AQ9wSdTv+1xWnsTW9Yic6x9dhvU+Gz55Td5mAQwkij w/rs5XJXJDLCKAuPB+NwPgLYHL4GEUMe6pCUk53v5C0r+JvFD4f2U45XB51XIK6O+zDv nGNw== X-Forwarded-Encrypted: i=1; AJvYcCXwJZBv/U3qg1s7YGsuKYhSvhC7gCxrWdRoY7gcVvM1gPja9QPHe7IK4BIUWYYf9CjDc/qUWmeKN+c8Ylw=@vger.kernel.org X-Gm-Message-State: AOJu0YxBwjw6wDylA3mTBOvuCHuwsATBqJMRa0wfKpBNb8qQ74NLPzFf k77N2a0K/ao+UhUqXQ4mr+sNncVmv0+HnpIr58WCZOMdCP2JJXDr3bGNsybXGOrB2oVRUbwilWY ld008QIYNjye61A== X-Google-Smtp-Source: AGHT+IEtFcjxrDkWqyDW1Yh13A/N/vCOq+YRPV+smnuFFZgDp9YtoqP/3PaSXx1PlTrP2mudWqxqGIkWyOQF1g== X-Received: from edxn2.prod.google.com ([2002:a05:6402:5c2:b0:640:f53e:dd40]) (user=jackmanb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:906:f58f:b0:b80:50b:6c8f with SMTP id a640c23a62f3a-b8036f5ac89mr1727469766b.27.1766506704176; Tue, 23 Dec 2025 08:18:24 -0800 (PST) Date: Tue, 23 Dec 2025 16:18:10 +0000 In-Reply-To: <20251223-b4-kunit-user-alloc-v1-0-fb910ae0e50c@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251223-b4-kunit-user-alloc-v1-0-fb910ae0e50c@google.com> X-Mailer: b4 0.14.3 Message-ID: <20251223-b4-kunit-user-alloc-v1-1-fb910ae0e50c@google.com> Subject: [PATCH 1/3] kunit: test: Delete pointless resource API usage From: Brendan Jackman To: Brendan Higgins , David Gow , Rae Moar , Kees Cook , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Brendan Jackman Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable This code uses the low-level resource API to track parameters of the vm_mmap call, but it doesn't do anything with them, because the mm teardown code takes care of tearing down the mmaps. Delete it. Signed-off-by: Brendan Jackman Reviewed-by: David Gow --- lib/kunit/user_alloc.c | 76 ++++------------------------------------------= ---- 1 file changed, 6 insertions(+), 70 deletions(-) diff --git a/lib/kunit/user_alloc.c b/lib/kunit/user_alloc.c index b8cac765e6204..564f5566641d5 100644 --- a/lib/kunit/user_alloc.c +++ b/lib/kunit/user_alloc.c @@ -7,21 +7,6 @@ #include #include =20 -struct kunit_vm_mmap_resource { - unsigned long addr; - size_t size; -}; - -/* vm_mmap() arguments */ -struct kunit_vm_mmap_params { - struct file *file; - unsigned long addr; - unsigned long len; - unsigned long prot; - unsigned long flag; - unsigned long offset; -}; - int kunit_attach_mm(void) { struct mm_struct *mm; @@ -50,67 +35,18 @@ int kunit_attach_mm(void) } EXPORT_SYMBOL_GPL(kunit_attach_mm); =20 -static int kunit_vm_mmap_init(struct kunit_resource *res, void *context) -{ - struct kunit_vm_mmap_params *p =3D context; - struct kunit_vm_mmap_resource vres; - int ret; - - ret =3D kunit_attach_mm(); - if (ret) - return ret; - - vres.size =3D p->len; - vres.addr =3D vm_mmap(p->file, p->addr, p->len, p->prot, p->flag, p->offs= et); - if (!vres.addr) - return -ENOMEM; - res->data =3D kmemdup(&vres, sizeof(vres), GFP_KERNEL); - if (!res->data) { - vm_munmap(vres.addr, vres.size); - return -ENOMEM; - } - - return 0; -} - -static void kunit_vm_mmap_free(struct kunit_resource *res) -{ - struct kunit_vm_mmap_resource *vres =3D res->data; - - /* - * Since this is executed from the test monitoring process, - * the test's mm has already been torn down. We don't need - * to run vm_munmap(vres->addr, vres->size), only clean up - * the vres. - */ - - kfree(vres); - res->data =3D NULL; -} - unsigned long kunit_vm_mmap(struct kunit *test, struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flag, unsigned long offset) { - struct kunit_vm_mmap_params params =3D { - .file =3D file, - .addr =3D addr, - .len =3D len, - .prot =3D prot, - .flag =3D flag, - .offset =3D offset, - }; - struct kunit_vm_mmap_resource *vres; + int err; =20 - vres =3D kunit_alloc_resource(test, - kunit_vm_mmap_init, - kunit_vm_mmap_free, - GFP_KERNEL, - ¶ms); - if (vres) - return vres->addr; - return 0; + err =3D kunit_attach_mm(); + if (err) + return err; + + return vm_mmap(file, addr, len, prot, flag, offset); } EXPORT_SYMBOL_GPL(kunit_vm_mmap); =20 --=20 2.51.2 From nobody Sat Feb 7 15:40:26 2026 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id ECC44314D15 for ; Tue, 23 Dec 2025 16:18:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766506708; cv=none; b=CUqeMb9x6GTA1jFIut5/mTXIo6ZDiZyTvwTN64OphZz4CciZWacbNw8bkIhA9+PNf6HzuRIsTjEQmSpIl0h0kzHxpnS+JxK4LxqswZ8gDpTHlIdgTgVvlzwi8BOHE6r2D2qHCOLuZpWmM7hlz2LK8KFk9mbELTfD6bsJ6IZQurI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766506708; c=relaxed/simple; bh=5MbTezqVniVMWrWPh/jzCyIi8y2s09KemMIahGqctog=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tKvJ8fmIwzRSmyL4mK8rD+eLRr243bQnzPfQCPbpNYyCoWMUIs52/3Pqi72Y9EympVBnRkulybZOfPVIiyMujpxUl88mAJxpgulzDwzyHcYNTojvpGnDRfpFOF6N3+ppzmX5oAqAL4+dO0iIVYs61KpxIE8xsq5ovakbmZrtq2I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=X9xp9T2j; arc=none smtp.client-ip=209.85.221.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="X9xp9T2j" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-430fcb6b2ebso2830324f8f.2 for ; Tue, 23 Dec 2025 08:18:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1766506705; x=1767111505; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=R8qAbifDARtNQH6///Cfz2Z6dr98BHzjmVALCF1UKvo=; b=X9xp9T2jhsv063FNnaVSB9Fs/hlioAXGiPXM3LX0I9WTnd6IxmteV8yE7rY0OjbldY 5E3RLjxDprMN73J4+PPGwbx3cXuwFAzwrUQA7hh7FVumoDon0BLHMBksXCtmXbEOzMTO 2+vphi9MxoKUD+Wttk27IzriVTYsHIkYKTSHeIXS4XC5eIofiAkqWac1ygFioj1xF10V IoPmefKfjvnUQoELn9z0Aj1Igx7WBvoqo5CX1Btvu163wvwCDdDBUtqOIC+s2sZKvdxk 8UdctF42MH1lttXqqIJICzOLry//NOM4Tq5emTx0rs52tUiLNh3iGgJXOoGun/pvmHKq qy2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766506705; x=1767111505; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=R8qAbifDARtNQH6///Cfz2Z6dr98BHzjmVALCF1UKvo=; b=u5K0SqKF12NombjCHa03B3quC33kG7GqP7AVtp1PRAuREXPCuXNUbRwwXXdJgHohAq 5zORLPAVOm36XymlIWcG4ixtT+SbRqsrzbv1GCaZ4walmb2VtBBUJBAmoI26Kb6H1hxn NceCYJjCWKO3Ffx3tUV6zx5QZnmSb6LD8VgMpLEZiEH78KyVud3yhjuLCVXnkSoyazjT 1M7L58AtokYj4zqgja6q5no0S8V5M7MwYHdGIuArqP6yw06ffDups21ChSnMDyIDWIkD gNTxZQ/5DZi/w48E5uvfSx39bu/Ksl6Bei3YEvil07bjNBtKCt9oTrsJGcn3NvB6O+QP iECg== X-Forwarded-Encrypted: i=1; AJvYcCVrCkGsZ/KE9B/QUgbDConM2wng5Whk6/VGB0rFNSluGsobt3FoonDan+Be6hNMRsJP8WUwuOcEMbKp1SQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxnpNw6URwAAT10FMmjy/q/FbfF7WgYThk9XqAp9265e93f6t6d a0vpH4+8ad2263tKe+9Tj/O/r/xL8+t+AidDxVz6XRJzSXyiBL39JCtsPss7vUkZoqEUZZGxFYk JDm+0WIKrsLulTg== X-Google-Smtp-Source: AGHT+IE8oDnfqXf51PXB9j7+xWXr487pSfiP2TuENwnOyxXdASQaxwRWlpWlqrTmlw8rYWPUrC2vEA5s/m4GpQ== X-Received: from wrbfu10.prod.google.com ([2002:a05:6000:25ea:b0:42f:b7f1:749e]) (user=jackmanb job=prod-delivery.src-stubby-dispatcher) by 2002:a5d:5f54:0:b0:431:907:f307 with SMTP id ffacd0b85a97d-4324e506ab1mr15076109f8f.48.1766506705229; Tue, 23 Dec 2025 08:18:25 -0800 (PST) Date: Tue, 23 Dec 2025 16:18:11 +0000 In-Reply-To: <20251223-b4-kunit-user-alloc-v1-0-fb910ae0e50c@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251223-b4-kunit-user-alloc-v1-0-fb910ae0e50c@google.com> X-Mailer: b4 0.14.3 Message-ID: <20251223-b4-kunit-user-alloc-v1-2-fb910ae0e50c@google.com> Subject: [PATCH 2/3] kthread: Add kthread_take_mm() From: Brendan Jackman To: Brendan Higgins , David Gow , Rae Moar , Kees Cook , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Brendan Jackman Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable lib/kunit/user_alloc.c currently uses kthread_use_mm() without a corresponding kthread_unuse_mm(). This is a bug, but fixing it in KUnit makes writing tests that use mms more difficult, because of KUnit's resource/try-catch model. Therefore, introduce a new operation that does what kunit_attach_mm() wants, namely an unbalanced call with cleanup deferred to kthread_exit(). This is actually just the same as kthread_use_mm() but without taking a reference on the mm_struct. While adding this, clarify the reference returned by mm_alloc(), since that is what kthread_take_mm() is gonna be paired with, in practice. Signed-off-by: Brendan Jackman Reviewed-by: David Gow # For KUnit --- include/linux/kthread.h | 1 + kernel/fork.c | 3 ++- kernel/kthread.c | 36 +++++++++++++++++++++++++++--------- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/include/linux/kthread.h b/include/linux/kthread.h index 8d27403888ce9..2e6244d8ff1a3 100644 --- a/include/linux/kthread.h +++ b/include/linux/kthread.h @@ -259,6 +259,7 @@ bool kthread_cancel_delayed_work_sync(struct kthread_de= layed_work *work); =20 void kthread_destroy_worker(struct kthread_worker *worker); =20 +void kthread_take_mm(struct mm_struct *mm); void kthread_use_mm(struct mm_struct *mm); void kthread_unuse_mm(struct mm_struct *mm); =20 diff --git a/kernel/fork.c b/kernel/fork.c index b1f3915d5f8ec..761e6232ea75a 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1147,7 +1147,8 @@ static struct mm_struct *mm_init(struct mm_struct *mm= , struct task_struct *p, } =20 /* - * Allocate and initialize an mm_struct. + * Allocate and initialize an mm_struct. The caller gets a single referenc= e to + * the mm's address space, which should be released with a call to mmput(). */ struct mm_struct *mm_alloc(void) { diff --git a/kernel/kthread.c b/kernel/kthread.c index 99a3808d086f0..c660c04a1b627 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -1589,10 +1589,16 @@ void kthread_destroy_worker(struct kthread_worker *= worker) EXPORT_SYMBOL(kthread_destroy_worker); =20 /** - * kthread_use_mm - make the calling kthread operate on an address space + * kthread_take_mm - make the calling kthread own an address space. + * + * Unlike kthread_use_mm(), this doesn't have a cleanup, instead that happ= ens + * automatically on kthread exit. Correspondingly, it does not take any + * references, by calling this function you donate your reference to the a= ddress + * space (from mmget()/mm_users). + * * @mm: address space to operate on */ -void kthread_use_mm(struct mm_struct *mm) +void kthread_take_mm(struct mm_struct *mm) { struct mm_struct *active_mm; struct task_struct *tsk =3D current; @@ -1600,13 +1606,6 @@ void kthread_use_mm(struct mm_struct *mm) WARN_ON_ONCE(!(tsk->flags & PF_KTHREAD)); WARN_ON_ONCE(tsk->mm); =20 - /* - * It is possible for mm to be the same as tsk->active_mm, but - * we must still mmgrab(mm) and mmdrop_lazy_tlb(active_mm), - * because these references are not equivalent. - */ - mmgrab(mm); - task_lock(tsk); /* Hold off tlb flush IPIs while switching mm's */ local_irq_disable(); @@ -1632,6 +1631,25 @@ void kthread_use_mm(struct mm_struct *mm) */ mmdrop_lazy_tlb(active_mm); } +EXPORT_SYMBOL_GPL(kthread_take_mm); + +/** + * kthread_use_mm - make the calling kthread operate on an address space. + * + * This must be paired with a call to kthread_unuse_mm(). + * + * @mm: address space to operate on + */ +void kthread_use_mm(struct mm_struct *mm) +{ + /* + * It is possible for mm to be the same as tsk->active_mm, but we must + * still mmgrab(mm) and mmdrop_lazy_tlb(active_mm) (in + * kthread_take_mm()), because these references are not equivalent. + */ + mmgrab(mm); + kthread_take_mm(mm); +} EXPORT_SYMBOL_GPL(kthread_use_mm); =20 /** --=20 2.51.2 From nobody Sat Feb 7 15:40:26 2026 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E1B62324B35 for ; Tue, 23 Dec 2025 16:18:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766506709; cv=none; b=c1oO4mEzs2t9woAjsEE7RbUFuCo4Cavp4rUaLzi9JEuay0OOmbylGbW5CmO6zmYGf4h2LPEFsiJbMQ8u+TlA9Fxi9iR1gscbMs/mF0ObF7pjmTb7WOls7Y6k0dRlED4Shy75mX5B8Q8iRSDTQAedvx6QG/QPvLMquRZ02mQzmGQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766506709; c=relaxed/simple; bh=S9xPlp1JbKJpCxpnciHsOatx8AGtJWNQNsuUgj05tOc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=UR/jsIG5+E8uQZJmYXSEsCyTG+2oJNohBMn55sIJXNAOra2ao8qzyJpvCOYatQOx/+JFOXatMbm8hBaKqApFjB41noOHmS3rVlOJexmvLPxprT7D+LlpdXKVQnIBqGxZf2VboW6MWqR8ETz2FyEJaBWOV2jmMTkfFlbD4o5wZx4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pY+sf7a+; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pY+sf7a+" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-430f433419aso5030506f8f.0 for ; Tue, 23 Dec 2025 08:18:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1766506706; x=1767111506; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Ecq4ux8juxn6/rOyJrhpTEUfUCldUGcEOc8fd0njwT4=; b=pY+sf7a+1SnGvjPcQU4n0lhFwlvM1MZjUeaqfg1PpakUONFs171qNJy6yD0L1b2/Id T4zoGpJxDzz2OG6jSOC3Wpe3snfgNLhJQ3vSZHryYIdAKa8NKadV0wjsQ/84kZ7o7sHt 9hniT+iAS3POt2CPHdonmU/P7v0vgDFDziVWHeV+hBnhIdWmBKLDLjaYDoqYfD/+iL5b CHG9bItD3JEjjoIkuvtPdMfcPmnITU5u5dl3tXkW2Ox5jmTGVcA0RI7tSYO5FNavWQIp pq0SYvMbRkfxwXkPmN+9gEhCy711vJawFo7A35qAMnYyhjD+TbPuDRWYyAULne44u3uc gaHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766506706; x=1767111506; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Ecq4ux8juxn6/rOyJrhpTEUfUCldUGcEOc8fd0njwT4=; b=HUowTDYBBLzjppviO7EQ+E3ZrxlLdsGZUUMQuHi48RrrbtnDqat3ZwyXsatb8CsTBU +iCU4myAyZdhjYiSgSf0GDyba/tROgfe14VbxmBgdVP2QEAkkb11BLfxnNkIUtJClnfQ C8e7McFXizH2gYL1yYD+nfRhApZVK7uTVf6swv/dE05ReD680EANatbRqoGW7qakapLR Hj7Gq/s107+4G7U5mzE1RVFrpHOAQmX+8FJVndPZ9XMpukuM52PwmRc/V/dQ0mof+7EP PvPyKPw40jO5AgHxCsW9zP+tGP+hpx4RJ/uOj0QTcVz+r/tJ++0p5b9oTSUHf4rcT3aK ioPQ== X-Forwarded-Encrypted: i=1; AJvYcCV4Ze3TA6H+KhpzSYXOoCAwjTOnAihFojjJW4bLknYb4Z+7xTxkGmi2wG9kcWUpCF0WKxo5U5CZGbmNQI8=@vger.kernel.org X-Gm-Message-State: AOJu0YyLazApkPtZCgVxGHDpmExAFlPqPZ4er4yGNdSkPtgGn7UHScZP aFqZ56TUqN1E2NzTpaM375eYoynT1X3Zz9M0cfzv9yG7yo79troG1gAwn0pq2Jk05T1luJrgz2T wyeJ1FiRVhZzfmQ== X-Google-Smtp-Source: AGHT+IE63cPLMbu1UcIi3eff/7vrdmKmmwcAwevA03KE9Q9fKYy9YzX+rmosSiA8nVUqbpZhJUEcq4BVB0L3Ow== X-Received: from wre13.prod.google.com ([2002:a05:6000:4b0d:b0:430:f216:9f71]) (user=jackmanb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:24c7:b0:42f:bc6d:e46c with SMTP id ffacd0b85a97d-4324e4d1032mr17311664f8f.26.1766506706286; Tue, 23 Dec 2025 08:18:26 -0800 (PST) Date: Tue, 23 Dec 2025 16:18:12 +0000 In-Reply-To: <20251223-b4-kunit-user-alloc-v1-0-fb910ae0e50c@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251223-b4-kunit-user-alloc-v1-0-fb910ae0e50c@google.com> X-Mailer: b4 0.14.3 Message-ID: <20251223-b4-kunit-user-alloc-v1-3-fb910ae0e50c@google.com> Subject: [PATCH 3/3] kunit: test: fix mm_struct leak in kunit_attach_mm() From: Brendan Jackman To: Brendan Higgins , David Gow , Rae Moar , Kees Cook , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko Cc: linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Brendan Jackman Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=3D1, mm_count=3D1. 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 Reviewed-by: David Gow --- 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]); =20 /* Attach the mm. It will be cleaned up when the process dies. */ - kthread_use_mm(mm); + kthread_take_mm(mm); =20 return 0; } --=20 2.51.2