From patchwork Fri Nov 29 16:32:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13888849 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 9064AD729EC for ; Fri, 29 Nov 2024 16:33:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 237B06B00A0; Fri, 29 Nov 2024 11:33:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1E9316B00A1; Fri, 29 Nov 2024 11:33:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0138B6B00A2; Fri, 29 Nov 2024 11:33:17 -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 D5D0C6B00A0 for ; Fri, 29 Nov 2024 11:33:17 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 8586181289 for ; Fri, 29 Nov 2024 16:33:17 +0000 (UTC) X-FDA: 82839677466.05.B98F5E1 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf15.hostedemail.com (Postfix) with ESMTP id 936E0A003E for ; Fri, 29 Nov 2024 16:32:55 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="K+d/E8cJ"; spf=pass (imf15.hostedemail.com: domain of 3vuxJZwkKCAEbmjdfszimhpphmf.dpnmjovy-nnlwbdl.psh@flex--aliceryhl.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3vuxJZwkKCAEbmjdfszimhpphmf.dpnmjovy-nnlwbdl.psh@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=1732897974; 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=NxiAaOQixinTmMpuFlr9Z7Cs/OKNMeW1CE1Bg5Rf1Zc=; b=vG6IHsmz0Mp9hqpg337NfkgR/WFHpUZCU+WZs6vGOo/QLa3/q9JAEE9i0fTiYbm72Wnt+5 6k2ezi2pCPRHoD1uZ/YYxE/8hH84GewEgaADNZRa5xObzeGFQVeHR5z+CUz20G669dloX6 EAK13XSopl6wcCyOQQPC9LsT+kNVQ2c= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="K+d/E8cJ"; spf=pass (imf15.hostedemail.com: domain of 3vuxJZwkKCAEbmjdfszimhpphmf.dpnmjovy-nnlwbdl.psh@flex--aliceryhl.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3vuxJZwkKCAEbmjdfszimhpphmf.dpnmjovy-nnlwbdl.psh@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732897974; a=rsa-sha256; cv=none; b=05ebRvNq0RUcaOHYjQr9v0dL5Rd3RvpnY2yZ1yZE5Zk5tMBhy9fKRvLg2Ri/BZXVk5WC7E Jed7O+AvECXmgOyH1OkiZ1pcxPuSVdgXK2NE8lDwBVuo02yMpBkxDM7c54HmKp3fZT/vXn mTf1s+nm7tHSftEi0eRNeApy+EL6ORk= Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-381d0582ad3so1370672f8f.0 for ; Fri, 29 Nov 2024 08:33:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732897983; x=1733502783; 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=NxiAaOQixinTmMpuFlr9Z7Cs/OKNMeW1CE1Bg5Rf1Zc=; b=K+d/E8cJKqv7LpAxKU2Yw5SoGodnQsaoEJe5Kk/FlNMai0AqSYH+z8s38LMZz1y59J 0J5Jz1ok4BNjwperNHpP2DtBx5hi5jmqo7M9QEq1yo5DkXSCpZM1mUXy0dEgZTWZ5Rz5 e+hA7vhoXsl1iERT03+k3CBev2ZP8mPyLwvpF+c8uXoJyCaz+LztgNOpO9m2F5qHO3s7 FJXi8ffxJ10jWR7qtys0dZ5k+PmZo1MBKFo529L9fyS5mLXH0uEPQ3rahqnYH4oRZRAv NJe74TgerZORvrS3FQq7kZtHS/5U1iuwDs/0/vXREds2XK5ET/c3WUAEvoOFYmZciEgr 29qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732897983; x=1733502783; 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=NxiAaOQixinTmMpuFlr9Z7Cs/OKNMeW1CE1Bg5Rf1Zc=; b=OHvVo1HEZcDsd5y1VYiCICXMU2a9kWSxQIvkm0h6J647I9V8LO06+JHwZaitT55Lq7 sLvZEscD7AqI0RKPrtpWGLhBwFM2GFaZVOPa22s2uiXwbMXkFC+OxwjMuHMQMmvHSSeB 3lOi727GcYfGdA/8jAtCaudp+cjGbVzHygPbPlNH98vgsH+doO49XvVbVBwsOqLlhaFD 42mPCOgPsfXn1IDK2UaeW92FYDRyLXRw1DMn9V5/hJC+3+nYupsjjGhC068yxCyd1J69 mjNa3pzHZvaRymVJ4+3KW1ejQK2zF6sxrmE7yMGCAdFpTRRjGeV6NcoU1+ZueEDawVzI 7GzA== X-Forwarded-Encrypted: i=1; AJvYcCX8uJRCirLWYUroYmXf+ESz2SaZSHzyqi5D1ZXZgk3Fb8Qz0L3yCxnf87URUuZBddGNTG0ljG91WA==@kvack.org X-Gm-Message-State: AOJu0Ywv1HNkwiEWy1ZlgLhW8cwtLhhJxyUInHufRKHF9OYY9uk9eXn8 zYDRYj5US85dLO19Z56qLAvGunYyB1R0Dde7Ig4m2d/rJdt44OHQ1vgz9LoQuCrKD2YduVw2dku ycwfDpr8RRvd5rA== X-Google-Smtp-Source: AGHT+IGYb3T/wZgxg6wvfixNQvKu7OW4dXOoNjGb+FLVFT5jRj0xJ7uK1bZMJYsE5nPmUYdm2iSLsb9tE8N9Qhw= X-Received: from wmbjw2.prod.google.com ([2002:a05:600c:5742:b0:434:a4bc:534f]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:400b:b0:382:486f:1360 with SMTP id ffacd0b85a97d-385c6ed9824mr13201148f8f.44.1732897982795; Fri, 29 Nov 2024 08:33:02 -0800 (PST) Date: Fri, 29 Nov 2024 16:32:38 +0000 In-Reply-To: <20241129-vma-v10-0-4dfff05ba927@google.com> Mime-Version: 1.0 References: <20241129-vma-v10-0-4dfff05ba927@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=rBEGLc7KJG1nI+I3PCkS6QqEUHC7WTE77ZFEyztUDIc=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnSeywnZuEoxDobqs4lWzeHdYCGEpayHKpr1Fk9 ltPcXjwVWmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ0nssAAKCRAEWL7uWMY5 Rn6LD/9y8AHiDmndnB+qvZq4H5F9U2hmnc+pviQiE0r46f7JiYFx/m2fKqG1YdHnt/8lz0N/DOU 3Z11N+ebn5UZJO4QiuZExGBwoDzEonp95mZYKWwSEM5lYWr/tIPZPlrN4jY2tULm1UvEFZ6S/R5 otdlUsOhvEE3MOTA7EjE9pTiZ2vMp+i/hjeTCBqWzywDv3hAML9oTk/16e00eFdrY0mtCvLQxk0 2alwCGptjDvpQOvmUvELwBFKJtlVeTdIZ7RO617X6WtZaKBDvAKXnAxTyCaMPfFyfY6qjVQJgQ+ kbCteUJ0B9ryxuzxddA4Te9UE/wGptVhi8MF39q/huixQByiX9OPDdXnHo4vOoBJA3kahq1AgLl /oH4XHgOxJCX52Cqr9BaqgW7xTfMru7F3cijFzKaoeOzFZwuIamuH0x8a/OdpXML//YwQ80AY73 uz42oob1f8bbvTTPjmtA+wLBZW9ieYSDvbdWno+UOkAR3c8TFWnT/k9kluifCzxlwRN+gJGNkrP 4Ibj7905Fxe4hcKeErmMbzpalPlgTm0K7ERQN6XrD1aasl+GImueaArIqjEKrggKMJxZzl1boRd /CVUyTwmyJOHFEPjEahnBOGSWpVnqa2V18wURAduN8jAF8xde9A7xARVv9ZLP1pmo8kgeukX4f6 sVuweJAEgw6IiCg== X-Mailer: b4 0.13.0 Message-ID: <20241129-vma-v10-5-4dfff05ba927@google.com> Subject: [PATCH v10 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: rspam06 X-Rspamd-Queue-Id: 936E0A003E X-Rspam-User: X-Stat-Signature: fkyygo6qj51a18kyqttzdmcq4d4p7tuh X-HE-Tag: 1732897975-344734 X-HE-Meta: U2FsdGVkX18jIM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PGzO7MSB4P4H7Tg/avBfFjouI+jKSV9r6HaZFrGs42ACDqjHfjV46akgsACQpNKiuB1I7xNYmLKcGAMDdwBJT2Lp6DIQqhD6t6/YnY2YuULkDPsds7wXEh4ZKDHOPTfymQM3RG5xobmjHS5l5w1lw8WSeOY0pny8kX3nEiVMvPN2Du0xx48+O9ohxbydC+D+omx/JI0hDaUKtszuEdbux2LGKbXi6JPjWVj7OiQ0lJWQjN4O5TElbAbn5VXsc7tMK903yRrZWqbOYzo7W+lj6HmDbMpE2EWAWfA6ePWaUdOx2PoYBHJzn2FCCKGDvFIUpcj9Wb9XK339tSKTW/kEwCQftBH09HwA3oUbjTgQsoi9YE9LX53h/qY2vp7uriYJ0/2PP6TzxNhiONJeax3/qce9Fh2eOjwcH3hqz8FUmm2PcbRpZf8qjNzWYTyeiTfAb9Q3QsLQVHjPZWYmoFaDD1TV0osPQ72bcGXWueQGwyTZRtxMG/Ey9VWCD07C84XTnJTl5hC4QTs5aHSdWGhvyy8CyXueKh+JKlNtU+bayxKJ3cPHjWf7Z/NBYBnvgSReyFITUsW4lcTWI7aBKpGO+C3Okp2Dw84CvBKpoWRPg= 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