From patchwork Fri Nov 22 15:40:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13883306 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 D5852E6916F for ; Fri, 22 Nov 2024 15:41:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D62A6B00B8; Fri, 22 Nov 2024 10:41:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1859A6B00BA; Fri, 22 Nov 2024 10:41:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F1D7E6B00BB; Fri, 22 Nov 2024 10:41:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CE44C6B00B8 for ; Fri, 22 Nov 2024 10:41:17 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 908F7121834 for ; Fri, 22 Nov 2024 15:41:17 +0000 (UTC) X-FDA: 82814142222.07.A6CC979 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf05.hostedemail.com (Postfix) with ESMTP id D4E18100007 for ; Fri, 22 Nov 2024 15:39:32 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=oTLgeNx6; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3GaZAZwkKCKgITQKMZgPTOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--aliceryhl.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3GaZAZwkKCKgITQKMZgPTOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--aliceryhl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732289888; 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=zQjQrA8l6WUO1Dv46YWyP2KzKGLvaWN5xjw1wa5cSc8=; b=e3zNxv5GDlslSnr/AOKEwdJgOYqCRi/oSefOyDGOonBK4cPrFotkYC5dM101Z3XSs9jNq7 +q/QfxYz5wlEGs/s6W2cqwi8RrOaviZhsuk/iGHQor8crSQEqJLstDHm5K+kDO9nKXsFSt wutvzxbQCrUdgUdPhmuHwVU6VorTmXQ= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=oTLgeNx6; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3GaZAZwkKCKgITQKMZgPTOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--aliceryhl.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3GaZAZwkKCKgITQKMZgPTOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--aliceryhl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732289888; a=rsa-sha256; cv=none; b=X/DCHWf0mFltOsWH6DN/sXZWWR6UlpCSxR/JZrt2RxGJWUtTpiUSmkN8Tq00GnmNYwGqWN 0MTF1pWx061cWpxFqo35ZLSpJ68hiyOdbUxiCiAiAJrB3lw328W2NlxD2CRuhbIllGHcSJ Y7Kj7VTp4tM3lMupO+G8YxplAdTtVlU= Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-382480686f2so1261101f8f.1 for ; Fri, 22 Nov 2024 07:41:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732290074; x=1732894874; 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=zQjQrA8l6WUO1Dv46YWyP2KzKGLvaWN5xjw1wa5cSc8=; b=oTLgeNx6bxV7I9hXaHsHRHO1x0v+Qa9TArTHrVYNPoYIJsDZzw4E9VUeFUI8SNPge2 9IHOwMSa6JLPw41epEUjckMOZ7Pros+9p7cOkojVXLrqmHKKkB2emZ68HoLSuAG+qoFx KvIfANtjhrmZU82liVXUr2e18R0GKudvpOQhuAIDqyV2Jhxt/H99ofH+daROQHtNd0Yi qBNJK4k+Y6zdlpmrQTicxSigjlozm42LLyEh06hUzjhrDM8r/63Enqx8ut5nO52lS2au HbpyqZiVkcBVY9/ggEUHS8y9IXAoeNU7A1iszblOZAljB3HMAvk3cYGUAAJ1Nl3OTdsI a43Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732290074; x=1732894874; 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=zQjQrA8l6WUO1Dv46YWyP2KzKGLvaWN5xjw1wa5cSc8=; b=oghae+76WkNLD5oYpdjqpHJAB0J4rET2BNuzUDCiSig3yz/5FPRL8qdZZmCVfROxks /u5YLuV9CLbU3JrgVDREH0HidD8O1KYkLVG3w+KxzS25KsWGmk51xVifkGBgqPt+sd5f TpLU02D6XdPHcG67Klyza0twFg9mzecWwmwmUEO6BN1d0mohNIWgPCfka4YQ4bKdZinb mCmIA2aGzGlLlPDgZB4F1KIn+eLeUuT8t+a9TrOa6f+uFCmpTR17NOy/G+SnrqMWNpSN eCbTZ88EMuLl0bGC1TmGv+rb+3CvQVs8KXVlI1EkgPjnwpmcNjuUOUMs1DgbPNddBFdv 52yQ== X-Forwarded-Encrypted: i=1; AJvYcCUeGZJkK0t7luWMdEmi8VTgL+rPjz4+iWqWXGQAR/yRxMSZFOuwkfuDHYMGZ6k9Wy9jTDM7ozRoIw==@kvack.org X-Gm-Message-State: AOJu0YzC/FWiFZLkHUiTGs6/4eSKnMGD5NGp/BcSqrMD1iCzryVEh6k+ etAfdeMmMhXhxaNFjmhIWmVnIAix5cVaxEnd1Y4wrutj6CxJKZU+t38RnMRbVJKc6sgCKWP/6Ot w72vqtr8TN+H6gQ== X-Google-Smtp-Source: AGHT+IH2pPS49cAT9mdCjiMFAVekUdtncedHBNDeVISatyX0fCCBayouO8NUq4KgmU1yCNwvAR5MiYLmHLx8PcE= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a5d:5e0c:0:b0:382:449d:a6ab with SMTP id ffacd0b85a97d-38260b452d1mr878f8f.1.1732290073825; Fri, 22 Nov 2024 07:41:13 -0800 (PST) Date: Fri, 22 Nov 2024 15:40:30 +0000 In-Reply-To: <20241122-vma-v9-0-7127bfcdd54e@google.com> Mime-Version: 1.0 References: <20241122-vma-v9-0-7127bfcdd54e@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=e90cBT06DiQlSWUKjCW8KaJiJvXBHZuG2g717B8IAkc=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnQKYJ73Kuhs7y8NsLUpQQMtbfBgw4kDNtzBVg2 tUbFMPyS9eJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ0CmCQAKCRAEWL7uWMY5 RkzaEACBEXTOYc+UZmggaAf5RKHc4bX3sRd3bEoHGg1TwzgV8P69eolFeBEDIMT4ozAtxjnoZMO 8H6ODCr80sTJ/14jsNEMfD8riO17u4t2Rct81AB6ASqN4eCABDEd93DFjjrtgNZxE5acubWPzlT rFWtgWhwIcyUvUkhX1ipVexr4/ZItTLyezntWLg4kBqVJ6uLepiaJctIk53F4rU8p9UZnAfC1P1 sPbE95Jb4S0oJhIGQoFaF6VtlUEV3joZaYJl1gGkuFnbWy6y1ije3pGCnWYv/099C2FtXKzKMEB e5mTT5LYuqJhZ3zf3gjfrNICehst0DXZPDIbpvzs199wF3CzRntxGAcRQkDApOhBkZj73Ntx6oj fO/MGb7TVIZgLhToYA91lnHYjCs18cIAwieUxn0k0tfq5NPUfYUYAad+koxMCGOauUvnCCB/DqC 2idZYlEiasHpiaOmj1judPSffxEnMkvDFLZtW2xroCdVECOMMYuFmW5fFv9hj5n2+yL3kmmcsoJ Sz/nfsJI5NF41/S/qA61Mo3l0d1XRZwMpxnEdzVm/KU47CaGRXonGb5rsfDf12bRSmzzvXthP/V vqtIKmQT4av/5On0EURF0tsQcFkV2DKivHNmMubOcOhsj3jFGHajd8ny39WSl7qKcFxdd3gePsV 7fPwbNtETlyL+wQ== X-Mailer: b4 0.13.0 Message-ID: <20241122-vma-v9-5-7127bfcdd54e@google.com> Subject: [PATCH v9 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 , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Andreas Hindborg X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: D4E18100007 X-Stat-Signature: c9kcn5xijp3aw94gd76r59zwr3xj9s7n X-Rspam-User: X-HE-Tag: 1732289972-555263 X-HE-Meta: U2FsdGVkX1+5UmwOu1mpwprw9T1xLBrI0e2yZWPARmyInD3tzGnYZjBgrig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Wln0fSCC3gLTIXwJegoSk57Lhgp9o3Tc8ncsFY76RlbCYQNSAZcas861NBhb+QPJa25S+CFgznE2sOxumNdiP1VMVifpWH9bHMQE5CfdTAePvH5v5D+t0KMBBSyTzo1gR718DfXkxFoMrIB9wtAIjT3flbmIcCZrtqluMAwxeRRuObi2fa1lqAdgojKfOjCacbuSnmRv6xVftSwxcNOOzX9Tr3ADFeaDD2cLttoHP0EIPrc6MYunKnfC+pNDfFC5q6e1Qr6w1956DBrYFFSyIfT5x2PLgFM+tJQRLCpZQdAwsTo/kjTT+0p5GAWCtV52SJoiX5w7qjfM1XueOE0QOuRBCLz1f2fuuHL2fQm867jNqhimPIOkZn3q2f+qPsM+UxSxR5DQgQCK13/HTmu+9/EZsn1ic02maEVoYF8xy14XpiReV2nbr+Yz7I/HANSGT/FTBAg6lsIkiZGRglWClMhS2lL2+EbVYSoxOmpOX0q3hs+tTFiV0AOISLtYiLV7aIexZekLGWumvmyxPXLEOG3qQlt4fE1sH3ltaPubIMKh2zFI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, 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 --- 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