From patchwork Wed Jan 15 13:35:07 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13940447 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 43C8CC02180 for ; Wed, 15 Jan 2025 13:36:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9CF396B008C; Wed, 15 Jan 2025 08:36:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 92EFE280001; Wed, 15 Jan 2025 08:36:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7AA466B0093; Wed, 15 Jan 2025 08:36:17 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 5005C6B008C for ; Wed, 15 Jan 2025 08:36:17 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C3967812C4 for ; Wed, 15 Jan 2025 13:36:16 +0000 (UTC) X-FDA: 83009785152.19.488ABE8 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf14.hostedemail.com (Postfix) with ESMTP id D0A10100016 for ; Wed, 15 Jan 2025 13:36:14 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="HMQp/Esg"; spf=pass (imf14.hostedemail.com: domain of 3zbmHZwkKCKICNKEGTaJNIQQING.EQONKPWZ-OOMXCEM.QTI@flex--aliceryhl.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3zbmHZwkKCKICNKEGTaJNIQQING.EQONKPWZ-OOMXCEM.QTI@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=1736948174; 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=WyGEiOkhmdNY/fU37LXSLgzBjmVwObw0G8SyyivcNjI=; b=u0wB80ZiV7GlVRhmoV/6GcYhLsxUVNj3rG0jZdyyoeqaviZmYS7ioRtKbDOKeNdOOak4QP T6IPu+WngIoaGy+gt/092R2vbDuOMMm6TqWq/tMivRAKMwCrph956P7e2nXsbcMybGOpwj /Ks70hO79mpvRkyvq0LJAFQZQeQ8xJA= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="HMQp/Esg"; spf=pass (imf14.hostedemail.com: domain of 3zbmHZwkKCKICNKEGTaJNIQQING.EQONKPWZ-OOMXCEM.QTI@flex--aliceryhl.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3zbmHZwkKCKICNKEGTaJNIQQING.EQONKPWZ-OOMXCEM.QTI@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736948174; a=rsa-sha256; cv=none; b=PqwbJqeqazFEdsPISafkiI0vNvFGh2/Xh1xkD/gSHqWhGKYvPy847B8M979Vs3CgAnobgn I+fx9YjV8tgfBMjKNkfRJlcoV1y28ZankzoFPSWkW7+Cd52TUg8KW21q768T00Z4j63UHv s0RFEGhwlnFGYxwGOMTCHWN3a2u4Cgo= Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-385d80576abso4501091f8f.3 for ; Wed, 15 Jan 2025 05:36:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736948173; x=1737552973; 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=WyGEiOkhmdNY/fU37LXSLgzBjmVwObw0G8SyyivcNjI=; b=HMQp/Esga7gSsVF3cD8+3swcaQBCPzCQ4ABTDS4Zr6W8Vslv55MshR4VONMoDpbp2e ImJ0DvE9ybYT/9iTQ3zQa6oDMYTDBlzT7Nb0fkrW4HkDmlNla32eaAgya5CSKcHzcvHg JXba0XrungUejtN/rDopvmgwFRwhE3TLB1BRWP4JXwKQrfjGtAYisEWQ/X4Yn1G3rvJb HanQUkaIHNeNq11fN4fq7MTuVK8iZCi6NNgV7az4ZTJyKgFF+VTAqsJZGbp8FzPxKBEe lfc/DhkheE3MDp8TltmVg6fsPd9rsIqbRyl0UgeuKe64q7e5HMW52EqoywdMy2ZhO97f x8fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736948173; x=1737552973; 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=WyGEiOkhmdNY/fU37LXSLgzBjmVwObw0G8SyyivcNjI=; b=gLLAfHGOw2E+Kueet3unE93Cx6zl/7I70Hm0WPdHcqW2MRCOmqIgKMV8XNkks8QJ4I /0DHaOWdULYwP1nj2I5QTONGpn8ZeYtZALwPCwlCnipjN2TkZP5d5f6K4S2ReUGRBf33 3HOn/4+AudfySYLNqYWbqOe1AFCKfWUANmOzOcL+zYMQUsT4wt0NtE8ACOXJoUcgmz2p LppQ5tvY6SheeiGcWJM7jjkAgi2/qTWblrFRc0XXFgSFMw/786zTZypvdNw0iK8znWhZ KydwytlTmWE4CynxIpgxP11/CenM3GnNaKz/GT/eQN59TgYVDzoqsdo66YSujiNPmnBO OGEg== X-Forwarded-Encrypted: i=1; AJvYcCW8CxZFKgAnXtTwDuxBQtPF9tCf6IsyGt+YAEtsB70+xvUh9WlywWBPoMapaPsKYo28Z8bWrMDBxA==@kvack.org X-Gm-Message-State: AOJu0Yy9IcVafmzwCMo9XTqEaWA5TBNFHC0wPsCtL7/H5sXaD4XxyDlD HuOejv3VRJfDqJDCM316Lrrg95r7EjG43gbY52MuIqtcTB4xi1dEPYmJ0IKJA9wX9Nam/4+Anrf Kjmhb167JRXCEpg== X-Google-Smtp-Source: AGHT+IH6kBJKdmdDV5w19iOE843iDVldxJ9in2s0SPbV23iN1Txz+OJHqSf7ZYmSD6HRC4mZ8sg2KRgO82avk5s= X-Received: from wmbfl11.prod.google.com ([2002:a05:600c:b8b:b0:436:e755:a053]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:184e:b0:382:46ea:113f with SMTP id ffacd0b85a97d-38a872fc1b3mr27766198f8f.10.1736948173574; Wed, 15 Jan 2025 05:36:13 -0800 (PST) Date: Wed, 15 Jan 2025 13:35:07 +0000 In-Reply-To: <20250115-vma-v12-0-375099ae017a@google.com> Mime-Version: 1.0 References: <20250115-vma-v12-0-375099ae017a@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=4078; i=aliceryhl@google.com; h=from:subject:message-id; bh=fLfkFNdLYfHgKfZa0dsXjtc9votv5zCA9qLsVLeeLkE=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnh7m/l8GboGpN/qusvnLxbok1uTnWuhzUiY+SS PCNfbEvgfSJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ4e5vwAKCRAEWL7uWMY5 Rg80D/9JzLVe2s09/Ey8IChe2f9AxrZWGc5rUDwgfHi/y8u+2zvnVesQGWFxUhTaCR435B8OVxl Ytf7xmAkpx69EK9ykOXhedM7v8VjGiD4aQDdY6PVvJPSn2MqV1yR+vLN6qIFWT6yBAVpVIkP1td +w5eKL6SIUPPOy8Soh/Dc4TAsEXq+JL9JEW80gfxcGXh3wsQzWc13ckoRPk5LdcT5GCVnLiaKYN wfkm3PWmgvVayaKk2PLF1RmN3hzwu9K2dn8BVptE+M6Fr0SafY6i8Oi0VE1ofzMxxUX29xyFkhZ uQWnNU/gdc93rgpgmNxxmRznFlMnBmEtNTH4kmI6YlrR8xXpdMPx2VIXTpg9vFHzrrcRI4c9dLD TiqK5aexQj6lRpVzWPdj7Sv6HE0sfCR+Orv7MPYgn8EGY+X69KmsutvARRFIvuCesfCmmBwHwfV Gfk2+CiL4ynj6qx9lZDg5DmW+68O3r/5DMyFtqB7uevUcSZ/HPmC9i5KcWTAtvVS3uL0EmKPePb fd8Gi72oo1oQ3OjvzaS0SBeHHlVE0FWz1n+TAOa+uPSShuMSx+4ki2JShfkmUWc2kfYxZYPNTpq lsHyUCXtL46uBkw221SbwO0R/tyz+0ABzNVR7dKEpjkbktYrrQ0dYY4V32dS0IsS1gpv0UJnqli M1k0GU7PNicgsmg== X-Mailer: b4 0.13.0 Message-ID: <20250115-vma-v12-4-375099ae017a@google.com> Subject: [PATCH v12 4/8] 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 , 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-Queue-Id: D0A10100016 X-Rspamd-Server: rspam12 X-Stat-Signature: zakk19nqci1hh9xa19s19otpk83b71wh X-Rspam-User: X-HE-Tag: 1736948174-163925 X-HE-Meta: U2FsdGVkX18AgNFlnv9E+k9PTlAXKdxUgpD0WCwsMW2VRpyZ0iU6yGwp6J+pX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UrsM1GSXxghWTuwqHTrRNgPitSnSb10g5rD5DKURF9DJulWfeqWGFDDxFMI8kysfPOeqFXRGOdRExavl1LW4d1jck0ceOwRQtZhO0vpzTx73CkVtYxFhF1WP/lulkQwaI3/2/RW7YRII/k1F4rDquZ7KO3DGvbDWnzf9KmpTX3Yjd3q6GnklByJ6n238531FC9N8/mRU7h91k6NLOvu2cT3Q16VKugFNN+csVPDzKUY4tEfDehTwejYy9F/jOV4JtAviMdJQjuT33jk/jBHoBX1RD89p3iBQaF5EE1r7aB3L3zpWSaE0xSxORo0AdhXNO5g9KfVdcJ55PLiXbqmfp0yK+pgWoxVIKnRDWI8LK7alOcO2QnKnVU8Rus7TLjMbiQLbIDRQmlLqXddu/RRsMRFLSXP0LgOkSh24z7YAUDdwAa4OzPU2VpltFir3n8vKec3127gvMGmpXYIbDKebJlsFssWPIiP9WGHoydhPqghA//KJzi3yLLwkp677mVXuueUfGkzd0RQAJKLN/cM3vlGSK0/xN2y34g2tPI4xzMDjqv3Zg= X-Bogosity: Unsure, tests=bogofilter, spamicity=0.495037, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Currently, the binder driver always uses the mmap lock to make changes to its vma. Because the mmap lock is global to the process, this can involve significant contention. However, the kernel has a feature called per-vma locks, which can significantly reduce contention. For example, you can take a vma lock in parallel with an mmap write lock. This is important because contention on the mmap lock has been a long-term recurring challenge for the Binder driver. This patch introduces support for using `lock_vma_under_rcu` from Rust. The Rust Binder driver will be able to use this to reduce contention on the mmap lock. Acked-by: Lorenzo Stoakes (for mm bits) Reviewed-by: Jann Horn Signed-off-by: Alice Ryhl --- rust/helpers/mm.c | 5 +++++ rust/kernel/mm.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 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 ee1a062ec7d7..60dc66972576 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -18,6 +18,7 @@ use core::{ops::Deref, ptr::NonNull}; pub mod virt; +use virt::VmAreaRef; /// A wrapper for the kernel's `struct mm_struct`. /// @@ -160,6 +161,36 @@ pub unsafe fn from_raw<'a>(ptr: *const bindings::mm_struct) -> &'a MmWithUser { unsafe { &*ptr.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 + /// case, you should fall back to taking the mmap read lock. + /// + /// 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, + }); + } + } + + // Silence warnings about unused variables. + #[cfg(not(CONFIG_PER_VMA_LOCK))] + let _ = vma_addr; + + None + } + /// Lock the mmap read lock. #[inline] pub fn mmap_read_lock(&self) -> MmapReadGuard<'_> { @@ -228,3 +259,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()) }; + } +}