From mboxrd@z Thu Jan 1 00:00:00 1970 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 4CD75E7717F for ; Mon, 16 Dec 2024 14:51:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C22566B0088; Mon, 16 Dec 2024 09:51:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BAB2F6B0089; Mon, 16 Dec 2024 09:51:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A25C06B008A; Mon, 16 Dec 2024 09:51:00 -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 7D6576B0088 for ; Mon, 16 Dec 2024 09:51:00 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2E8C5160FC3 for ; Mon, 16 Dec 2024 14:51:00 +0000 (UTC) X-FDA: 82901108808.05.C37A26B Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf04.hostedemail.com (Postfix) with ESMTP id A5A5640013 for ; Mon, 16 Dec 2024 14:50:27 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iY3MHIeD; spf=pass (imf04.hostedemail.com: domain of a.hindborg@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=a.hindborg@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734360638; 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=YuiFNo+I4UjCr9wn2havkwITWMct1l/ert+tITRkV8I=; b=oY8U9pz6G3tDQkWvQBPJeBNcXto/6ftfnKFD37v5Mwhp/Q7fhL2AxjyUs0j+MgRpAFt6nE R1HTSdJBjY9YDdkNUXdTroeK2W87sSh9g3y+UoADuh9gaoOZIMKC5guoqy0nrml6xdIFEM GYLq3D+LhDOuM2RYUQzXUbsHJpRXfr8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=iY3MHIeD; spf=pass (imf04.hostedemail.com: domain of a.hindborg@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=a.hindborg@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734360638; a=rsa-sha256; cv=none; b=C+SiPBzvZAmCOOe9/uMZW2Bqoa8XKSnyifNVBgvguHQ8BtR2RkJetOm9eUUmD43cjG8J+A uWnUhhybaPJO0dBpSr2a2UwmKLyVrPEuW2LkwQ3mUIMK6ICPq80K+wBP0qFnTavs/72rug mcO/+TSQlNxt2PT/PtAeOydApc84zYk= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id C94CEA40FF9; Mon, 16 Dec 2024 14:49:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B31E7C4CED0; Mon, 16 Dec 2024 14:50:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734360657; bh=FHSKQmzPdFG9CKTMPQ6TI+GuboZTe/7QRXLSQ47Adp4=; h=From:To:Cc:Subject:In-Reply-To:Date:References:From; b=iY3MHIeD+eKZcKR6936M4JiHCd9w4+Hm6gJ2fTm9HF2TC0lPxlMw4j/M0qkLF9lGE bp4tsX6m9Qw8mZjQSxjvufNA24PnvzlMEwDuLt2lsAxhb+8IpN+aVZBzVCCTtekdnx MJFeWJ0LlVHulmrQ9Ye7gloOmVeQPgUmZKZFN/YEzh+htZ2QhzF3i2Bfjtr2rNhM2E gd4nvxd4p2+wYLHlqZfp14Qod2FuXhB6u0tsMeorMiJIyzWsa1DvqEQevMstDQ4nm7 6earhRgnx37C5DVA0YN4ihLtUxqXHGvmq7peJk6GsgP+qAZOY876Zdj3i6wcCs6g4y 8zufe36yAuQvw== From: Andreas Hindborg To: "Alice Ryhl" Cc: "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" , "Alex Gaynor" , "Boqun Feng" , "Gary Guo" , =?us-ascii?Q?=3D=3Futf-8=3FQ=3FBj=3DC3?= =?us-ascii?Q?=3DB6rn=3F=3D?= Roy Baron , "Benno Lossin" , "Trevor Gross" , , , Subject: Re: [PATCH v11 4/8] mm: rust: add lock_vma_under_rcu In-Reply-To: <20241211-vma-v11-4-466640428fc3@google.com> (Alice Ryhl's message of "Wed, 11 Dec 2024 10:37:08 +0000") Date: Mon, 16 Dec 2024 13:47:11 +0100 Message-ID: <87jzbzbxrk.fsf@kernel.org> References: <20241211-vma-v11-0-466640428fc3@google.com> <20241211-vma-v11-4-466640428fc3@google.com> MIME-Version: 1.0 Content-Type: text/plain X-Rspamd-Server: rspam05 X-Stat-Signature: xw6pksq579rudk9shg5chn6o8xdxkoo3 X-Rspamd-Queue-Id: A5A5640013 X-Rspam-User: X-HE-Tag: 1734360627-537825 X-HE-Meta: U2FsdGVkX1+0Ze1w5Njbsro/gwWVBuCmEb/B01EoCVGQmZYH0PqCQo7uUQUscg/edqPd+UgLiKJwsU+jc+gJsUY92pWgDxjPDm8m8CDpeIoF3aVVlV4aZoaUTx60LZqFBNwYuxY9d2zr9kB5tLq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ko+oxWvBYHQtVVyC+GW/Nu9rbyYSA682Mf00VCmnaZgnCrnBK3wP/XMtHO1INP++CkKUJIP3b2udFIvFfVG71SNCrbaF1koPnxm7gyUAg1QKfpfYVxxgY1Zeuze5NuSgIjwCTiAyJXK2kt6Wk4h3tPSVDCT2g8oDhHrTTTK/WPjbkplZukEPVsT8vwQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.106291, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: "Alice Ryhl" writes: > 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 _) }; Is `as _` the right approach here? Best regards, Andreas Hindborg