From mboxrd@z Thu Jan 1 00:00:00 1970 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 5999422655B for ; Mon, 5 Jan 2026 10:52:46 +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=1767610368; cv=none; b=G0oaKWd8Cnvx2hgKbesHoTTLQry+ZT5eXQ4KQ0vDugF91mIJIrCfePi+Ow2UetjQd+KbUubf6HXiuzIGr94MIQzJWOPcX1gEpre0SPTx4e3eHjKb7bkm1gXTiIVk7KPjZ9HL0h8EsZRDzWNXtPyJqxuP13xwOhyfpZweTQrbt6k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767610368; c=relaxed/simple; bh=S+2jW6sr68INml29A9/xooxgR+jzLaZCiBjWWY+6t5M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jgwcyDV5/68PYDfYidiGkr+Ibtax64Bs2aH8Pe/WT5D+aN3mnO0TdLeVjJpypWPpamAmgoGaqeXkX23oLOTYbAhwHUPlaVXnqwBU1z0BtJuODp3SPfVlvhB9DLX5TfcpbzpAsQcWoICdFwwzpLUcYHPBqXdh0ddwu9+YgUchhM0= 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=XGNOBICu; 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="XGNOBICu" Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-430ffc4dc83so13394327f8f.3 for ; Mon, 05 Jan 2026 02:52:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1767610365; x=1768215165; 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=eZIE86Erp1QGbs2m/K1kc889xwmLfFaps0VsChU7D34=; b=XGNOBICuBd4BWpun70D0JQwg9GGyoI+In9wJZ3j1cKZ73ZlLI/j3uTRtRg7dTxnYFs nMS2bP9Wf1FMat23xwpG37OKPuw7LCShdPoti5LfimxbO7i9VrzlL7265o2SbiygxpN9 K9lhJOeDE03llzQ/PZ/zJxtm158L7P/vn4yojSckfCJ/Pk+RUwWTjQ0HwyhF22dMyJ5R o0HEKAlnzisuXWOWaX9WRy8YpU4a2+3SYzE9aIEzre+qc8HVvENeJ/57oMqDVVFrfRC2 IoY/U64lT7UCDrd/Ll+MjyJ/l5s4lI/Q7FcDDS44qVge0/rqpl83lIoflTESKovTCPWr Gpyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767610365; x=1768215165; 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=eZIE86Erp1QGbs2m/K1kc889xwmLfFaps0VsChU7D34=; b=hV2lrP9ABjqpj+Sl8qVaEI7uzp9yuriV2t+94AkMziSfQeSq3FLc6LRAZKt9dTOfRl H9ex/o4ySOSbJn6TS8ZbFAPRdqt7WS4KP9reaUDRNctmbY8+pZTM7uKB78G5Fwz/WOWG LOKcTRgJsnlP013B9b1yQVTbFlfbPSkZd02Bg6zzvtUMa+DWRoy3TTxQqSHlHagbjm0U K/JYohz5orpYmDDSTCw0z5YFRoKBphUlgvZb7dadylm89mY3I8Mdlg+X0CbIip1o2MGK RrlUbgUhcskRsG8/Qax8MH6YnsOn9pznW9YUXPIUTAiaB+5KODbDYpFrFfnoTzx8fVz2 4ZrA== X-Forwarded-Encrypted: i=1; AJvYcCXUR2gIVfJ2NRhdcyaaNKSIeMZCkJr1V7uCZBPZGbJV3dLWmML8i/kktUbbl/Fiq5VhgM9Yjq19XgRv6+Q=@vger.kernel.org X-Gm-Message-State: AOJu0YzLuT1x0A9ecUEdahSrkaRgXv71NT1Hw0PgK2Ee6J4RXsJFX2H5 14m+fdJc4PGY35Oc+piWl1fBGCBA3a+s+cjVK2STKd/ejTGciF63MMIjDKS/R7rxpx5232NZNGo lu1JV89c1eXPGtA== X-Google-Smtp-Source: AGHT+IEAJYABQdl0S3AMd+Kym0YbP2ev2YfxQ/hHue5b+xcTvswe3K5CPOaAuQwSBUuFhDhZ84cMtaq210atDg== X-Received: from wrbgv28.prod.google.com ([2002:a05:6000:461c:b0:42f:b138:c617]) (user=jackmanb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8718:b0:479:1a0a:ebbe with SMTP id 5b1f17b1804b1-47d25082cc8mr551544395e9.14.1767610364788; Mon, 05 Jan 2026 02:52:44 -0800 (PST) Date: Mon, 05 Jan 2026 10:52:44 +0000 In-Reply-To: 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> <20251223-b4-kunit-user-alloc-v1-2-fb910ae0e50c@google.com> X-Mailer: aerc 0.21.0 Message-ID: Subject: Re: [PATCH 2/3] kthread: Add kthread_take_mm() From: Brendan Jackman To: David Gow , Brendan Jackman Cc: Brendan Higgins , 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 , , , , Content-Type: text/plain; charset="UTF-8" On Mon Jan 5, 2026 at 9:56 AM UTC, David Gow wrote: > On Wed, 24 Dec 2025 at 00:18, Brendan Jackman wrote: >> >> 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 >> --- > > This makes some sense to me from the KUnit side, though it'd probably > be nicer to have a way of actually triggering kunit_unuse_mm() at the > right spot. I'm not sure if we'll want to have tests spawn additional > threads sharing the same mm in the future, too, which this shouldn't > make impossible, particularly if we have a requirement that those > threads don't outlast the original test thread. > > Otherwise, Is there a reason we can't mmdrop() from another kthread > instead of trying to kthread_unuse_mm()? I wouldn't be surprised (it > doesn't _seem_ right), but seems to work here. No I think this works and it's actually how I originally wrote the patch. However I think it's very messy, it depends very heavily on the implementation of kthread_use_mm(), i.e. it is saying "I assume that everything in kthread_use_mm() gets undone by kthread_exit(), except that there's exactly one mmdrop() missing". This seems like a natural conclusion when you've just spent half an hour staring at kthread.c and drawing up a stupid little ASCII diagram to try and drill this godforsaken refcount API into your head... But once you step away from this patchset I think it would look completely bonkers. Here I'm looking for a way to actually solve this with a proper API. On the other hand, I'm now adding a weird special kthread API just to solve this one little problem in KUnit, which people might reasonably object to. So yeah I probably should have laid out some other options in the cover letter. The ones I can obviously see are: 1. The current proposal. 2. Just call mmdrop() from the other kthread and spray comments everywhere to try and make it make sense. 3. Find a way to call kthread_unuse_mm() before the kthread dies, with some sort of magic in the kunit_try_catch logic. But presumably to make that all work with faulting tests etc is gonna mean more special APIs, probably worse than kthread_take_mm(). (I did not explore this very carefully so it's possible this is easier than I guess).