From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CBD6523027B for ; Wed, 11 Dec 2024 10:37:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733913464; cv=none; b=d9UNqo3lolablZq8xUuMwkREAanfPP7nTbAh9ltzYonZWRFMWiusDy30NFloTC39Ew0ZMIFIa1A/o34ara0T2TDBO8xwaZobO0ia5rmaX/sgHMgfp/9gZ4z61jSzLZDZzPPU3AuHbGiCjbEHfyyA88UHRFh0ic7pgXbGrpMRczU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733913464; c=relaxed/simple; bh=PSMNvAHImS1yuLQzsvSoD2e3+5nrD/G5jvs5aJHnybQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YDaojabCAHpxvbe2yNpFeO8NXB+mUXY7fPeYG1xCCCgWEDYf1B4XULa9jXDx07+SMUlPdyuEkk+o1zYV/Wkq6w0vIkop16FVZTRRpQbbj1o6SkVoKJ9vQ1Y/tBhGsjAux6apFex5x/Nsem/0C+qWX2I1bqS5HbJclubSTuzTcok= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=O/mARUAo; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--aliceryhl.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="O/mARUAo" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4361b090d23so6004645e9.0 for ; Wed, 11 Dec 2024 02:37:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1733913461; x=1734518261; darn=vger.kernel.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=jtfdo1hWu/gkdW8i64KIMP+Laz0H38tgBK8KPM9++n4=; b=O/mARUAo5zcESSbeW2dtzZmGvBLDJbSDzb9uIwlt9AxFVDXPIZY0iobtp3f030Yrbg lGZr24wcGWvFWTewd8dJ7at0SRp39oM7SK1/4st9OcbIOGkwyJ77UbIpFWAMQ67CTgtw GI55cNSrrAQ+5KHeIoh+YkcyGRWrM/FPr4BcLgfHwLOeD/fpFPX8kdGnuiprbl1ReCMK x0k/91c3tImqbmSLJvsUjh4qehUCihwH5vn+aJK/oSvTT9eQkMX1P/JCiRm0FvIfDBOZ OCpWMU2hFmxoE26EbeKAt3hpuDPc43YSOha5wDY5RJctspHJoCECUvM94/+qN+OPUPgR 8V4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733913461; x=1734518261; 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=jtfdo1hWu/gkdW8i64KIMP+Laz0H38tgBK8KPM9++n4=; b=dtwdQ/mkG1t4t26FjQ4BPMCcOyexZvm7uOXVwxy+QSNHe7M51Rj1KTVRpsJmfW5n0C KbPwQ8BVRGBehJY/2eq5YaE0HBIW1VftAQ0b+dRLe3W5yJ0T8oluocHSqQmnLlJQf1EG Tm/5BYqYkSVxmKQ/aQM3t6ZAQ7fochThA28zm/AGoT6Z2FcgD5HtAvEvgpYygm0vldII DOcfA+RnJmOEtDA9WgvuyScKyNf+czBaoDgpSiwn6HaPZNDO5jMLfPqSX/NU88FrmCvW skZlfZ+a44l9cP1RudxzphE1RKEVdO5X+IE8bFpgGMr6l86vM6PJvxTYN48GOs3FLUzx ZsUA== X-Forwarded-Encrypted: i=1; AJvYcCVcbqYF0a1eixruqUgopp/ZMWSQD8YulEi/4nF5Rb9tFzQzS3pU8B1zBNP1COypJYaJZnGGTo8G8Us7b2eiug==@vger.kernel.org X-Gm-Message-State: AOJu0Yw9+C35rt3YXk2wOX4ikQsqaE+fJJ9Dbio4QiEPM39xZIbPJMqo SdFJIdF9Cd0swzxiRbzG2skK/Ijgq08nVkZISO7HNRQPJu/SI8SozpYInGp68d96sfI/+Wq7Nrv nJAJWyhgF56izKQ== X-Google-Smtp-Source: AGHT+IEPSG2Hqz3P3TcvwAF16etBCufMRdEZ09D0s4Xc1JzGR3A143l7IWc1m3i2YQwgadn0owNcK8U+CXDiey4= X-Received: from wmok4.prod.google.com ([2002:a05:600c:4784:b0:434:a2c3:d51b]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:4023:b0:385:eb17:cd3d with SMTP id ffacd0b85a97d-3864ce49640mr1860462f8f.8.1733913461236; Wed, 11 Dec 2024 02:37:41 -0800 (PST) Date: Wed, 11 Dec 2024 10:37:08 +0000 In-Reply-To: <20241211-vma-v11-0-466640428fc3@google.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241211-vma-v11-0-466640428fc3@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=PSMNvAHImS1yuLQzsvSoD2e3+5nrD/G5jvs5aJHnybQ=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnWWtoiZME2oih+BwGJvgLeV9XdV6aNRW+Q12xj brbx3/LqYWJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ1lraAAKCRAEWL7uWMY5 RhRpEACoHtQCpffN1jELk1C4qWvx35jHIqYWia49W6YKDfgnOKbSh/PaQNsmFLQ5xqVgbm7v/UK aBQwg3EiD7cArhLxL66nlarZSNU4xAyIYRagufeEs62wVM8fiHrN0bLBQZaTeVR45ka6Dr/NpjT JDvwDHSpm37SvlfAmofc9S+xU4wW8bYutsCq3jCcK0SwgR0BZRcT+q5yYgBnR06vTwCt8Eo32tF 4eCgLuB09ENw8ozEaZuy3U/nIsKDw/Fs1cyweaV3JFTRhZu08ZCQFCMZIItw+z7S9L6SHZ7QOqo 2aszTbLLzdzJChsBBjyQ7S46UghGprkQh+8+iJ/+OOOK9T4A71euURV7yzgU5+I7pOVq7uuEUvc xMxwyyWyeR/cf/dst1SzUffc7jT2e/Ap4W1LCtdaFkEgNXR6/6vTsJ9ffKl5VQYaw5CW+v+L/NY BXwxO+E6cGtAf9afhviyTbXnazRnar/c2501rI4RBu/3zB+0LuIiuSBH4ZyHIkanDFBbekdJzfe j2fNRFns1Rv36w8SKGtQokraNhC7EX9sVaWLGkn778X7n00EcmfFH5uMSpERmffICVt/3aJa2rW nW09v5vj355pS053y8y4wrt1fWYw/tuQYoBi21rp2L+yWlr+nKcIRcoshluSVLDbH40G2H3jp6u boRup1nXBHzGN5w== X-Mailer: b4 0.13.0 Message-ID: <20241211-vma-v11-4-466640428fc3@google.com> Subject: [PATCH v11 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 Content-Type: text/plain; charset="utf-8" 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()) }; + } +} -- 2.47.1.613.gc27f4b7a9f-goog