From patchwork Fri Nov 29 16:32:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13888845 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 E1BBAD729E9 for ; Fri, 29 Nov 2024 16:33:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 127E16B0095; Fri, 29 Nov 2024 11:33:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0B1D16B009B; Fri, 29 Nov 2024 11:33:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DF7666B009A; Fri, 29 Nov 2024 11:33:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id AFB316B0095 for ; Fri, 29 Nov 2024 11:33:04 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4023A140721 for ; Fri, 29 Nov 2024 16:33:04 +0000 (UTC) X-FDA: 82839676878.15.D1EEA87 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf11.hostedemail.com (Postfix) with ESMTP id 6279E40005 for ; Fri, 29 Nov 2024 16:32:53 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=MlP9GKC0; spf=pass (imf11.hostedemail.com: domain of 3vOxJZwkKCP0fqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3vOxJZwkKCP0fqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@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=1732897978; 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=XEZERPJwfgVL12T7CsV2RsjLJQFd2+FhD7A3aSN4nWQ=; b=2nq+GQnGWCgG4F/cXHCy7VO4JaMSLKedw/cMhsJTTzaOf+Q/Qx035BrIB6BhvimBw3it79 uCodmauqL/zApxc6I39WrKVfP+DYYzrgIj+YVScdqvri0KBF69iJ/GcFYrzhEnJTcHOOOi DGgxUN4Xf4MIkkMkoflYLG/BQeZvBlQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732897978; a=rsa-sha256; cv=none; b=PzBW4HLE4BVYvlucD99rcnYIsMx6nvC/8IxvGWAtS7eYEE5aF8Yup9zLg3uNejNP6hsFep O6eE0BA61Hu6B5SV3we3sjjlktk63wI6ke/rOtbs5z6Ja+ofW+/aCaD9kpeoAYOKodMCtz DxVlGGTV02eH8QqZtI7JSRtUI5zHPWQ= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=MlP9GKC0; spf=pass (imf11.hostedemail.com: domain of 3vOxJZwkKCP0fqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3vOxJZwkKCP0fqnhjw3mqlttlqj.htrqnsz2-rrp0fhp.twl@flex--aliceryhl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-385d6ee042eso1085306f8f.0 for ; Fri, 29 Nov 2024 08:33:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732897981; x=1733502781; 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=XEZERPJwfgVL12T7CsV2RsjLJQFd2+FhD7A3aSN4nWQ=; b=MlP9GKC0RX+m5zo5xnHIjD/ryOQ2noszUjkOMl72MK9VeDH/FaTPadtAlG/y2Tq+lR jkWDuI4FfNt/AVmT7EcjALIbBXfDY1qNpYAG1z8lkXj7XuAGY6Mv5wvJ45UpfYh5F3pj 5IEJfsUk7CjkbEK3Ez6LaeQPtfBRQJ9eONGbqyF68iFdz7D86+SgO+NGP4UVrYKqrLif sfqZml6PzasLO8JvJsxGskcyQEEB8jD07gUcP3U6fOhhexqUXKMBY19rB7ZogYhgGEX/ F1N5wCfMz8/CGF8pGX8rw/vOcHGITqXthrhEyL4tRgvu2AgBFe/av69fwRQXI0I0zMnp t37Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732897981; x=1733502781; 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=XEZERPJwfgVL12T7CsV2RsjLJQFd2+FhD7A3aSN4nWQ=; b=k4eNTS7BxAA7iHX0uHol4JmgzI7rSeK0P7TVTCWR7LhUqRM+HHVhXKca3NokpK4DdA LokQs/ezYggub44rE1rU40xYEVex7prJLslN+fXj3Br5tto54KY2Ft3PTOefmQyXFh5L HLyAjWevKFmNadMMcSwHZNLDUQV1N9booEqo8MvioDj3ZJKMq9IZyabGbJ0WMyPBqQgm Ndb8oTdCMsRGUNcJUr1SK58UgMx83tEDBEMR1GioAqkKdKohe6udX+oevMaOjjcUGVa3 5Xy9Y380iR+CmGfNg8fw4g0bLzTJMfh7XXV3UstNdayVFPGIA/iseewKx+I+Wi0wpvLC ZShQ== X-Forwarded-Encrypted: i=1; AJvYcCUnieisyrcsG97yvBniI99mw+AHuNSS3odjwd+o28J9siElgfFcyLTbjpeh//90NDzBrG5JwbYwKg==@kvack.org X-Gm-Message-State: AOJu0YwLPQARX9YZvEptBlyBa34u2APHa8Yk6gYJlck/t6NVxjN4+ksr kNMhgp3V/ytDHbj8JOwkrH1Po/DCtymSVXn9zvdHrX7CinvwgfMYqXy0H1iJNmddjAHkXrQQa2X NRL6StSrlw4fzwQ== X-Google-Smtp-Source: AGHT+IEtMdEbBKfTi44I0hQxKs9SwxHv8NrQJqJsf3ce8ASa6YkaH62l4mchnBzjtU1EUoOkexS5OiFPJNiqBPU= X-Received: from wrbeh6.prod.google.com ([2002:a05:6000:4106:b0:382:30eb:98f7]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:491a:b0:385:bc42:52e4 with SMTP id ffacd0b85a97d-385c6eba8bemr11919119f8f.24.1732897980729; Fri, 29 Nov 2024 08:33:00 -0800 (PST) Date: Fri, 29 Nov 2024 16:32:37 +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=3946; i=aliceryhl@google.com; h=from:subject:message-id; bh=Vj+fM1HEWgk9zeZ3nCQftraSe6z/pjvOnZ1xL1atMIA=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnSeyve8NrjNvn5/AdaiW267/FN/DvoteK61Oo4 9jv/idgWVyJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ0nsrwAKCRAEWL7uWMY5 RpxAEACz7Q3Z15K9yn6UUnABmC93QguEXyoGn4fILedLcdfZiuIXMtFqpbgt30kuS+yGQJL41+B 1GNgFO57jYNMEHUZv87T8ItmFDJ2mKpQ7kteUR7+MXAXcpwOYI80gz695ogwkssoeMvfrllsf8c N+HTsQlzoRBWcwek+nZnh0RG5ZSR3KEsKtLBZ49/gqJi5p+7SNJnuvR5k6EJC75DGtQw7vYbmxe p+5Hqd2L/mzOLVGo3cBp53uEcawKUUU1NBCBlOI3pPhOssdZb2CETukZ5ZUbOQcACHFLeyOyt1Z MI/FAqyGKDlSINeXZsUn1izy/oxYKx+6bH/ldZ4G9/2+ffHrhgx0Z4vKQ2MCqrONvgsRM3t5U/h 13ldm0DP7VL6NEMeZ1EFquTl9VOWVd48ThVZgEOgK+BMN41A0S4mu2SkL+lOu7hnX+WtI8NWEJS DQ+5Nr1CLoaGxIEDMWfAs2ojsL6eW5NXDoVpmX+EG3dX69N95evSpj4kGm+Op6PKwMSTD8vbmtf 5h4v8zcXVcN5EFehLmSy+UqhnFASNEFy6uSDCZiEN2TZ6cgwWdNkhV+bu7OtvnwUViqGSts+rG2 44LoVmhzRJZhOZbXtPq+CGaRyvxebiBx35ZsGXGC4nQVfZz4wjyrRIfU3H2pHHhAYuG8JkqrETk HrhIta1iCfIzR/Q== X-Mailer: b4 0.13.0 Message-ID: <20241129-vma-v10-4-4dfff05ba927@google.com> Subject: [PATCH v10 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 , 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: rspam10 X-Rspamd-Queue-Id: 6279E40005 X-Stat-Signature: jp6nzrh38di3147ri5iae7e9dtdroap6 X-Rspam-User: X-HE-Tag: 1732897973-339528 X-HE-Meta: U2FsdGVkX1+k4aX3aeOouWWG5e6zSIvb+3AYYH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3qPQZrY5rTdl2JGw8ezJxoUhEUToyT0R9518UcLcJ2QGDa/h8w08G/+ZY570d6rMWJFUA1B5GsBm3bsRYgR7NNRlaZtbhGtxG/kvzRin1qsxycH/p7qpnEFMrSZUet1IfshxglSrR8hU53k3MnRuzTESNvsY+NLfP5m5GiSnH7z8Lrsb//rEx9aUX6MUmF4c1RzpNQLLOYf+QPjFlqSwwj+LbxIditUG8dq3rNxJi/3MzK6rqCf3Ho/e1pLOhx64cq+QYarry0r2q4e7NM5oFjd1OBHOPcPPiWm22rie6VPelbaemhrhYMZTC7m21IHk9LLAqb19Rvqp1iED0q2lsbjp7l8TaNJUZrQLW5kb68ptllT4AVSAs6AbHbIdhD6R7fwGXisD0j71a+U9xkbhhbtJeR/cQxXFeBkZrxeU8ODw3lRh8XV2j1S4H36HoDBDy0BjZENvC7lGzrc/2t7+8E2553WIzJyS64XYz+Gl1mitEWG/sZDvi/BzrYWF+syiDMjpk6Tt5Bl/hUt+qtRh4SPMTiWmcamfKZLvRBUMai2Kt7cNvrPeYtekv5qB44tF0H6qjOvpfY6zuzQD8NMeEPKTe+QNy4A== 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: 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 | 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..425b73a9dfe6 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() } } + /// 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, + }); + } + } + + 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()) }; + } +}