From patchwork Wed Dec 11 10:37:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13903326 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 538EDE7717D for ; Wed, 11 Dec 2024 10:37:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 594388D0016; Wed, 11 Dec 2024 05:37:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F4FE6B011A; Wed, 11 Dec 2024 05:37:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2AC448D0016; Wed, 11 Dec 2024 05:37:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E96546B0119 for ; Wed, 11 Dec 2024 05:37:46 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 92BF980853 for ; Wed, 11 Dec 2024 10:37:46 +0000 (UTC) X-FDA: 82882326282.19.63BE6D0 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf29.hostedemail.com (Postfix) with ESMTP id 7E40612000B for ; Wed, 11 Dec 2024 10:37:12 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3XTPYON2; spf=pass (imf29.hostedemail.com: domain of 3d2tZZwkKCPQWheYanudhckkcha.Ykihejqt-iigrWYg.knc@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3d2tZZwkKCPQWheYanudhckkcha.Ykihejqt-iigrWYg.knc@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1733913449; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=N4tIdMdsO0J6lrUvneIwXSrF/KOin5KwKJIBsiYufGA=; b=t0OTszYjW/nu9Ir+VNqCMchhdgDC7Q5okdGYBKAh9V3D+ie+yhG3TNefGdMX0GrJ/yy+6e 68GQyO+dZ91+ch3zFAO7U6m2aC+aKTz/K86crXPjdlcwplAmbKr4DsQaX1uMPuFOLZdzOB ptU5Gfs9uvRlHTaPLuSAQD/YG7GifkE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1733913449; a=rsa-sha256; cv=none; b=rnE79EGNAyiz3yHMokQRj21lVT/m7QdEt0s36ZJSZhdjH788hQVGxQlGSYg5MbVrkF6lek Fes+QVt+MYlZgyUH2SX7M9M7ZKMNIie8T2/uyEkTlntckFwaC2ivNkt+/0E/yErthXj0BV YQpB9Z/o+bU1ptCXHwoGwFkqPnAWmIc= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3XTPYON2; spf=pass (imf29.hostedemail.com: domain of 3d2tZZwkKCPQWheYanudhckkcha.Ykihejqt-iigrWYg.knc@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3d2tZZwkKCPQWheYanudhckkcha.Ykihejqt-iigrWYg.knc@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3862e986d17so1711730f8f.3 for ; Wed, 11 Dec 2024 02:37:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733913463; x=1734518263; darn=kvack.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=N4tIdMdsO0J6lrUvneIwXSrF/KOin5KwKJIBsiYufGA=; b=3XTPYON28vQjz0IwzIAaJo+FJqyIb2vilBybcJ4OPNasyWzwHYIW7BsZDq1AT9sf4/ CiSdS7SDQhVMC5BuYXwujmbtNeRB7AOYcLtLXZGAWZdC6pQ2K6lgth0Wv5NOd/UKXOJn +TZ5UP1X0uidZAAb7y31urI2HjuaLJe9xEFWG97+0GV1hi/DMTOUBmM+0ACr0SGGLDL4 gNwl1OOXDuYxwmmfSP6nBv2ZLoIE9eSMLY6JQ6kpyK9L7ulXIHtfq70oVd5rgdj6JxTu 5Wyrf+K0URuR9rJEkHYbw4zEMHum+XA24S7XOoDt3Ers1hJJ/UO1SRmQB0DWpWP9SNRw G75Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733913463; x=1734518263; 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=N4tIdMdsO0J6lrUvneIwXSrF/KOin5KwKJIBsiYufGA=; b=CHR6VoZ6ihXtv7akXpSuqRl48stXROnhe3DcXhEPGXd873ToH5fvSqVoDJ58VmVYMG sPN5tH+gZq4NDjp5Szdyvt3q6AAOJcGkQVPrkmL+LX9N5HjCd7NTpzBL4L1OLmeOK+4y E/UwWItyJ6ksRK+cRG5fk/KMW4JecnKdb0avdIRHlkpK9KviKfQq0y0dy741pW1wWDnJ I2HAP5nDiSgDgSwUw1M4GuocNbpE7ut/pZtwLdFTjOQ09wa17ZS8Jo5sMFRj57uZT/Hd GiRpMRB4/fie5FfmoItBXuh6Ggy7eFZNEnwxktsWdVmJggoBRFMpmVdpqlO1YKsEsee1 wLAg== X-Forwarded-Encrypted: i=1; AJvYcCXmFmSg3NTxN4ce2LQ+jqROiD/mAg5e34GwTu9FpuCPSCcyCYH46iD3qEeXXxCpnBBPmhRyD3PEsQ==@kvack.org X-Gm-Message-State: AOJu0YzyMGXeBpqoUzDPQk1Rl7mT1LN+9REP1q/DXuIBIoczR3qFE+jG bKBIUU/lui9ABnbcR0q7T8LVVkWXEvu5Gmq5drAWgE6f8bVDIQte1Wu6nPtE8QrHaRZ4HhDmP3x xgCgw/87XuP7vkQ== X-Google-Smtp-Source: AGHT+IGvJYqc2Db+rertFy18yncLZyz9Adh52dHN4rQJSEpkQ1PGruhxbsByd/HWJepWTHQaE7E4uK9LRFm4JnY= X-Received: from wmpr10.prod.google.com ([2002:a05:600c:320a:b0:435:dde5:2c3b]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:2d84:b0:385:f979:7664 with SMTP id ffacd0b85a97d-3864ced3035mr1190487f8f.58.1733913463310; Wed, 11 Dec 2024 02:37:43 -0800 (PST) Date: Wed, 11 Dec 2024 10:37:09 +0000 In-Reply-To: <20241211-vma-v11-0-466640428fc3@google.com> Mime-Version: 1.0 References: <20241211-vma-v11-0-466640428fc3@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3090; i=aliceryhl@google.com; h=from:subject:message-id; bh=o52ixmPVakbGqHXkdZGKle8ANx4alFbrYcfoTBoaBeY=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnWWtoRxaCSkuHkzNXUxE2qqNVnv4EQP+W4uD5c UetmRby79SJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ1lraAAKCRAEWL7uWMY5 RmGCD/9Xgd51fs3bqaJoNH1j26BxFH1sX0Yp3b23v/LQVFSNhncGa6OGK550wg7ov0OeRIedPKw vfoO7FS+v9j13zOMi4A9nogfpPp0lYkqOTWrjQl4lIX68/kck8B5nybxGBMNAvS2CWxD8J0Oomq 6SQ+cb/xgGWhD7xV2WvIifQANQyuin8/tmalAHCl1YX+yIhv+kuE9c0KrvQTAZoWjdTvymoHJIf 1HJepqC3TOgcM0g6dYzn1eNvyFJ9ohJMe9U/AuPZyQf7j0uZPdI87WRmSPQo4Nz3eWY1OLsDC3O UXjBgTWPCBw2u6qNS1ZBOlc/qK8zjohbdQlEjtXZmBJySD+w3BE4xtEeY3yFUM4dz74jwcOQlWK WDLxpWyPhDtHAwUZApsiExI7rM4qZ5YDprePBLCPvtvFW0iPN05pOnPqHip3EZB571ZotEyrQqH M6TPjtBZ0so7nD5X7bMEUx1AWeXuWpfqU9K+OuvbpcPa1bSLiKddGL3/aXfKcp89ZoPPHKRWgE4 qQKlEHkqh+G265jkYzw0ENNNu9TBPO/eqNsH9Vz4XpCRGS3WC0l5ZbXStkimQTxSBRvxIimx72u 12CtVhWEgk2lytcOj92QGdVSVd3m1qTGXFcJ2PHYgnugqt12OMZ/3fsD9gyqGz17LTTFnjZ+Rls mQogTRrM6ybWoCA== X-Mailer: b4 0.13.0 Message-ID: <20241211-vma-v11-5-466640428fc3@google.com> Subject: [PATCH v11 5/8] mm: rust: add mmput_async support From: Alice Ryhl To: Miguel Ojeda , Matthew Wilcox , Lorenzo Stoakes , Vlastimil Babka , John Hubbard , "Liam R. Howlett" , Andrew Morton , Greg Kroah-Hartman , Arnd Bergmann , Christian Brauner , Jann Horn , Suren Baghdasaryan Cc: Alex Gaynor , Boqun Feng , Gary Guo , " =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= " , Benno Lossin , Andreas Hindborg , Trevor Gross , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 7E40612000B X-Stat-Signature: wgeki17dkhy9bpixyey6eaqm6ma9zxs8 X-Rspam-User: X-HE-Tag: 1733913432-604707 X-HE-Meta: U2FsdGVkX1/assSDdVWbK0d3BgzelEtA/y3ipS6TBodRysRXiR/wGMiUL4V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bL6a299q2C6ccMIhb0mjrtwqw4VYwktzGvM7issyb1oKHC9usKCGgWgVvhZXBHtqFgTJvVjZIbe8n3IZ7pWssZ/CpIzYMsjKsYkwXBJixcFmUPU92a1uDMGA+uMCz79MWPSscwmoaeiTwDlUL9yAf14V+BciydgYVmpuJhQvsi26S5kIFOlHl0ng1RS3CNfGAjHR2cLN481HOkENAC894wjV1JBrG7g5ndM3QTHtyNntrYf3naUMQk3wHrK04e+jZt9mSKtFu/oWVVSE7SImce4rpadOb1W/QzIXGTU9flN7izgx3/dz9u5OUCDL5oM1wbxM3As+yn74DW+DUUUqcd0KhQPeHct7CDBIQSMvI+9N0YmXcEIJ+6DhH69a69EssRu83qwpCX7c3lhJCKtWD1n0Z5HK6g8yPVR/QMwQn9SlfyvGF2IBMl27KyhDtmjaWB/foenE4uD7TWJkXppea8j5MyKwRRboKNFrFkLk5QU24OZC5XRdxI9FjTeL0Kov+2YB2WdmcHZMRt4D7mI7gheGHCJqFVmazeEJVDQo5euUJbXedv84RBaWwnrSdtAaPl9vG1ewY/jkGusM1+TkoZ08ZBgdwo1w6fFfM6NnUG+l/fpVyONUvtJhNyyyKSr2SJkvJNtIo/Gz1ZVCWN X-Bogosity: Unsure, tests=bogofilter, spamicity=0.499322, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Adds an MmWithUserAsync type that uses mmput_async when dropped but is otherwise identical to MmWithUser. This has to be done using a separate type because the thing we are changing is the destructor. Rust Binder needs this to avoid a certain deadlock. See commit 9a9ab0d96362 ("binder: fix race between mmput() and do_exit()") for details. It's also needed in the shrinker to avoid cleaning up the mm in the shrinker's context. Acked-by: Lorenzo Stoakes (for mm bits) Signed-off-by: Alice Ryhl Reviewed-by: Andreas Hindborg --- rust/kernel/mm.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs index 425b73a9dfe6..50f4861ae4b9 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -98,6 +98,48 @@ fn deref(&self) -> &Mm { } } +/// A wrapper for the kernel's `struct mm_struct`. +/// +/// This type is identical to `MmWithUser` except that it uses `mmput_async` when dropping a +/// refcount. This means that the destructor of `ARef` is safe to call in atomic +/// context. +/// +/// # Invariants +/// +/// Values of this type are always refcounted using `mmget`. The value of `mm_users` is non-zero. +#[repr(transparent)] +pub struct MmWithUserAsync { + mm: MmWithUser, +} + +// SAFETY: It is safe to call `mmput_async` on another thread than where `mmget` was called. +unsafe impl Send for MmWithUserAsync {} +// SAFETY: All methods on `MmWithUserAsync` can be called in parallel from several threads. +unsafe impl Sync for MmWithUserAsync {} + +// SAFETY: By the type invariants, this type is always refcounted. +unsafe impl AlwaysRefCounted for MmWithUserAsync { + fn inc_ref(&self) { + // SAFETY: The pointer is valid since self is a reference. + unsafe { bindings::mmget(self.as_raw()) }; + } + + unsafe fn dec_ref(obj: NonNull) { + // SAFETY: The caller is giving up their refcount. + unsafe { bindings::mmput_async(obj.cast().as_ptr()) }; + } +} + +// Make all `MmWithUser` methods available on `MmWithUserAsync`. +impl Deref for MmWithUserAsync { + type Target = MmWithUser; + + #[inline] + fn deref(&self) -> &MmWithUser { + &self.mm + } +} + // These methods are safe to call even if `mm_users` is zero. impl Mm { /// Call `mmgrab` on `current.mm`. @@ -171,6 +213,13 @@ pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_struct) -> &'a MmWithUser { unsafe { &*ptr.cast() } } + /// Use `mmput_async` when dropping this refcount. + #[inline] + pub fn into_mmput_async(me: ARef) -> ARef { + // SAFETY: The layouts and invariants are compatible. + unsafe { ARef::from_raw(ARef::into_raw(me).cast()) } + } + /// Attempt to access a vma using the vma read lock. /// /// This is an optimistic trylock operation, so it may fail if there is contention. In that