From patchwork Wed Nov 20 14:49:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13881315 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 F0134D711A7 for ; Wed, 20 Nov 2024 14:56:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 37C366B009B; Wed, 20 Nov 2024 09:56:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 32C756B009C; Wed, 20 Nov 2024 09:56:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 131D16B009D; Wed, 20 Nov 2024 09:56:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id DD3C36B009B for ; Wed, 20 Nov 2024 09:56:28 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 92199A0BF5 for ; Wed, 20 Nov 2024 14:56:28 +0000 (UTC) X-FDA: 82806772104.08.A4846D4 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf05.hostedemail.com (Postfix) with ESMTP id 1F397100005 for ; Wed, 20 Nov 2024 14:54:48 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FIsAvbjw; spf=pass (imf05.hostedemail.com: domain of 3mfg9ZwkKCMIitqkmz6ptowwotm.kwutqv25-uus3iks.wzo@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3mfg9ZwkKCMIitqkmz6ptowwotm.kwutqv25-uus3iks.wzo@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=1732114383; 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=aoiANS11czrxWVbduQfnhGruCAXv4HeO9pq8nZpI/0M=; b=BzUEWL0tqYD6KLjaGLqyJf+kZ1TGmL8cfLA2FHxr0ja6O+Fx/Ed77kcS4/Rn+WDyryVTLD YgZjwNtkhNqnx+XpEAP+mThdjyQuiiHhQYXjRcqSm85gbk5mXv1QocwHQG/3a2Kf3kRubR uttb3nRJHfk8rKSDXBDcbiRCwxtfjYk= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FIsAvbjw; spf=pass (imf05.hostedemail.com: domain of 3mfg9ZwkKCMIitqkmz6ptowwotm.kwutqv25-uus3iks.wzo@flex--aliceryhl.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3mfg9ZwkKCMIitqkmz6ptowwotm.kwutqv25-uus3iks.wzo@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732114383; a=rsa-sha256; cv=none; b=4DvvcEetA9DqcTTh0gWcYlZHYkkKaz21per64COpMtTMVIlQGYZVeoR+q+ts86ujvtnGn+ spvIZSNpOxqlUdAwmwDPkDXvaGoR3zukYGoJpyP+OGK6y+2LeKJjLoY9//vc7EWf1gUcdd 2HvMs1X4CMJKdKAV2M120jXu2trVLdo= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4315c1b5befso16618125e9.1 for ; Wed, 20 Nov 2024 06:56:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732114585; x=1732719385; 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=aoiANS11czrxWVbduQfnhGruCAXv4HeO9pq8nZpI/0M=; b=FIsAvbjwpxO94DCSEz6kAdtv4sVuylY7kAmpUsZRQ9OolSZ2iVmk9A3UttRwLCwrPE WWhfzR/feu0hgdKyqZKjUTAG3SjDaJ8qT9vt7c3CLeuLGPoPf9KXa4tTQqbX2TWH6trV Kdczz/JlYgOm9zdBSnqAT93fv4WkTtnh7avG5TVLVZuHFcaJtYHsndn8zE7scMEtZUNb BEP1sgdXoE6O77vJBvfRtIDIDDMxEEEJXaI0x11a0R4Jr5MzX8/5JQbEKEKKPkzkU6I0 CW1zqKyPxwS1vpBk/CMlKx66wnMF0ebGxv0kYPP+RP7Y8DmMsVacEL0D64Z1PhfJPb56 YiyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732114585; x=1732719385; 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=aoiANS11czrxWVbduQfnhGruCAXv4HeO9pq8nZpI/0M=; b=giZn1UCXmpYUq2GgLzzVqLIu7MeoiRtPGQQVJ6dWZk6dT01Xz6iJC6opa2+UfY/iqZ OeSQtvXWe8APFto2UFYrZNUKhKaXPC456yrrbrLy4rdU7wZnxL+QfmjnrQiRoRzzBsDk 6aIv8XcXDP26vNyh7sYVuFqKc8mELMZzMCEmzonsTE4pU+Uzj9/QUT0Yecri/claIuiy 7l2cQ+lGfV+yFtJWR21l9eMusTn2uvX23rvWc9D6BcwKAFyVX+3taj2TvoRSQnK4hC/+ Hqq12s8Uv1X8w/S6OAzgum836Yk8odm+r9lERHyV2muZycR9ddnM7ziCnr/ll1S2hHrD rOgA== X-Forwarded-Encrypted: i=1; AJvYcCUqXZZRDaiDGW/UsAbEWGxXgY+MZgquX8dKGxvcNK/y5TPD6pEVy5sJpSu5DltCRCr2rRxFGvcwjQ==@kvack.org X-Gm-Message-State: AOJu0Yxlcy/o0Mh8MiASmBUS5G3foypc5nEVBD56g6LuaGawIV+mQtWy jba/TTCgQJe20Ud4q24w+GxmkJOKGSWyEcT7Eu/oob6hS6Bab2rCc9+n1lOcg6t2cZFWqiRYGAm 9m8higN9GNRcX3w== X-Google-Smtp-Source: AGHT+IE6KZ4EvG2+AyBS7X0AsuANcRIXcFSazw3+PxVfaxNaYxLQx8x02Y7MetgM2m0JyIIKmjeIwnAHQKtRbjc= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:a05:600c:46d0:b0:431:4162:6adf with SMTP id 5b1f17b1804b1-4334890546fmr124315e9.0.1732114585303; Wed, 20 Nov 2024 06:56:25 -0800 (PST) Date: Wed, 20 Nov 2024 14:49:58 +0000 In-Reply-To: <20241120-vma-v8-0-eb31425da66b@google.com> Mime-Version: 1.0 References: <20241120-vma-v8-0-eb31425da66b@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=3357; i=aliceryhl@google.com; h=from:subject:message-id; bh=B1j9P1BH0bxfU/zafa6FVQxd9ysL+wMPMIqevg6Rsd4=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnPfiKB1ELULsXYa5P1N0NhjBWmvy5M6u0nsExs po7gAjq23aJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZz34igAKCRAEWL7uWMY5 RtPwD/kBvIn5klKFHqApPc2S/WCAcomjz5SYvwUslE6FeJGQvZfkTdXTuEoZJq0lsxJmNbs6Hp5 Q9oxbRPey0lnHYyCJQ/nry7A6FNxk3n9Y8GhK4ctlNZr0glylb7YifGxTYNJhuEoFjnoYOvnlfb RZ5yfmziAthnvy/QooPSnoRzSB9L+hyK/vtg+ReOZ06qfImvN6lBFkaAyNklhP/zbjpFSClY2sy vNCOingHCvMKFKWxfGQhiVkonR4nQYVisrQZQRpkq2OJu+V9CMKBsR4tYGX/8bfJKXABOyYKV2X 7cxgf984gDAZfBzM/7VQAret1qyVxQh/R4Pb1Dc4hrXanMJy7Y1yp4nzlP3Q7P0iU6hfY/L01eQ YL6kMdNVuU+FIFSUEIP4qVOlcWZcQ9c+nJANEx1d6x4M7gXDzvusU6N1pBRXaqMS1Ll3aP1CvC5 sfIAd93vWwYZNuZ2yfoIxT/uXDmoVCG04mfKN1RpM8vGzcnonzACkX7DimqwXqGtZZhF/tHSsyu ze2wduW0hjSyD49/eZMEaNrlmxBFaxs9uZqjJUim+WJzRPNbzhgcbtSPUp3iw4RrqTVREicitdr 41uVHMtohgbKannriawy40MiB6pUjr6QUYmB+ljpC2gJ54cbV8uFiZ2Dl2vm7BAEwxR2NRTgesh 2OzPc8WMU/3D7Lg== X-Mailer: b4 0.13.0 Message-ID: <20241120-vma-v8-4-eb31425da66b@google.com> Subject: [PATCH v8 4/7] mm: rust: add lock_vma_under_rcu 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 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: rspam07 X-Rspamd-Queue-Id: 1F397100005 X-Stat-Signature: zanonorpci1e4q1gamgoq54ubua3fcs9 X-Rspam-User: X-HE-Tag: 1732114488-213971 X-HE-Meta: U2FsdGVkX18w7iCje0KauYp7zGYbF2aimb6il0z7u/3tNWS6GlQ+DKKMxTeA/3QU9ok/mGwsGtBJAsugAVANZC9HqHq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CtCvztefBJJLlRPuwLil0KiNndxlBKUAYlNW3BH++trbLPVGp0hkqTxQidRDVfMEAd4/305TtLsN5+KekYDSwY9wouxGLtg8oHifCJaD+1s3rHhvJgGFYYIfW3IHgMkMhEKgji1IUyPjsmzGesLhZxdYW4mFlwEkj25pp6/t06KOe/W0Jnege7zJCIpANALt+zv9+A0xr1FqIoZTL4PBbCikrqFLdttPGenFCaSt4Aa6R3mYxpVGlrx6ECoiJkBKLwVmTTqAizXQEJ2voUX28YoWpHVi/WB0xjHZf3YHa8+H6bBiwq00YT8Ul7fFTz02Xb0xUFQMotzADcdSvppXTnnjz032ccob/9bfy/FDUsUeyb0xCuR5h49jW5US+WLXZEqKvRgktLPRS7j0j15NjvMlet+i6DSLHruvp4s4t5ewBVFG4KtSvAvjYG2b7GRACQg6qMPptNVtsa79lkokQ2gJOTCApJFZyGZlw732fttlefYfvHxkC35vvqLSyHeqZN/0qXplmmZhDaISQ== 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: All of Rust Binder's existing calls to `vm_insert_page` could be optimized to first attempt to use `lock_vma_under_rcu`. This patch provides an abstraction to enable that. Signed-off-by: Alice Ryhl Acked-by: Lorenzo Stoakes (for mm bits) --- rust/helpers/mm.c | 5 +++++ rust/kernel/mm.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/rust/helpers/mm.c b/rust/helpers/mm.c index 7b72eb065a3e..81b510c96fd2 100644 --- a/rust/helpers/mm.c +++ b/rust/helpers/mm.c @@ -43,3 +43,8 @@ struct vm_area_struct *rust_helper_vma_lookup(struct mm_struct *mm, { return vma_lookup(mm, addr); } + +void rust_helper_vma_end_read(struct vm_area_struct *vma) +{ + vma_end_read(vma); +} diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs index ace8e7d57afe..a15acb546f68 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -13,6 +13,7 @@ use core::{ops::Deref, ptr::NonNull}; pub mod virt; +use virt::VmAreaRef; /// A wrapper for the kernel's `struct mm_struct`. /// @@ -170,6 +171,32 @@ pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_struct) -> &'a MmWithUser { unsafe { &*ptr.cast() } } + /// Try to lock the vma read lock under rcu. + /// + /// If this operation fails, the vma may still exist. In that case, you should take the mmap + /// read lock and try to use `vma_lookup` instead. + /// + /// When per-vma locks are disabled, this always returns `None`. + #[inline] + pub fn lock_vma_under_rcu(&self, vma_addr: usize) -> Option> { + #[cfg(CONFIG_PER_VMA_LOCK)] + { + // SAFETY: Calling `bindings::lock_vma_under_rcu` is always okay given an mm where + // `mm_users` is non-zero. + let vma = unsafe { bindings::lock_vma_under_rcu(self.as_raw(), vma_addr as _) }; + if !vma.is_null() { + return Some(VmaReadGuard { + // SAFETY: If `lock_vma_under_rcu` returns a non-null ptr, then it points at a + // valid vma. The vma is stable for as long as the vma read lock is held. + vma: unsafe { VmAreaRef::from_raw(vma) }, + _nts: NotThreadSafe, + }); + } + } + + None + } + /// Lock the mmap read lock. #[inline] pub fn mmap_read_lock(&self) -> MmapReadGuard<'_> { @@ -238,3 +265,32 @@ fn drop(&mut self) { unsafe { bindings::mmap_read_unlock(self.mm.as_raw()) }; } } + +/// A guard for the vma read lock. +/// +/// # Invariants +/// +/// This `VmaReadGuard` guard owns the vma read lock. +pub struct VmaReadGuard<'a> { + vma: &'a VmAreaRef, + // `vma_end_read` must be called on the same thread as where the lock was taken + _nts: NotThreadSafe, +} + +// Make all `VmAreaRef` methods available on `VmaReadGuard`. +impl Deref for VmaReadGuard<'_> { + type Target = VmAreaRef; + + #[inline] + fn deref(&self) -> &VmAreaRef { + self.vma + } +} + +impl Drop for VmaReadGuard<'_> { + #[inline] + fn drop(&mut self) { + // SAFETY: We hold the read lock by the type invariants. + unsafe { bindings::vma_end_read(self.vma.as_ptr()) }; + } +}