From patchwork Fri Nov 22 15:40:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alice Ryhl X-Patchwork-Id: 13883309 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 1453AE6916D for ; Fri, 22 Nov 2024 15:41:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 29C608D000E; Fri, 22 Nov 2024 10:41:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 249578D0007; Fri, 22 Nov 2024 10:41:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C36E8D000E; Fri, 22 Nov 2024 10:41:25 -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 E0B3B8D0007 for ; Fri, 22 Nov 2024 10:41:24 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 95487C1817 for ; Fri, 22 Nov 2024 15:41:24 +0000 (UTC) X-FDA: 82814143188.06.FA7BAAE Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf26.hostedemail.com (Postfix) with ESMTP id 6B6B314000F for ; Fri, 22 Nov 2024 15:40:41 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=yg7pYCUd; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3IKZAZwkKCK8PaXRTgnWaVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3IKZAZwkKCK8PaXRTgnWaVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--aliceryhl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732290014; a=rsa-sha256; cv=none; b=8lblgJwOHct9MoME9TrSTX2U58M3qd/CiU5R8FRcZ74r0jlZyd/3dWQELMN0QZSeciaTx+ Cbif1ygc4l9N0aiWLhQzKC3PuGxzTJe6OxuZOneYEoryc+K9jJ0+SCDaiRBf5lysgOkgmW WN3oEf3vtcJDazdUT+6jqfTHzYPmJPA= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=yg7pYCUd; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3IKZAZwkKCK8PaXRTgnWaVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--aliceryhl.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3IKZAZwkKCK8PaXRTgnWaVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--aliceryhl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732290014; 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=roehYhV9fbym2F/vf27bAbe0CuzVSCLFEeCVaGrDIRQ=; b=z+Y9PiOTBJ8qSfs81DDnIColSex3s45SLcFcWWWAmhLCeP4P0GD4kdBNfEGaP2uImR8LkN 19kak3z0Z1FFrSqbk1D5ku0rDY9p6W/s/nLfcDtWA0Ms9f5EpVd4Nhhxm4/7i8kvNt6EXz P0L4GTUkZseHff8hzuS8cwjld1kSY18= Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-38236ca50d5so1086696f8f.0 for ; Fri, 22 Nov 2024 07:41:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732290081; x=1732894881; 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=roehYhV9fbym2F/vf27bAbe0CuzVSCLFEeCVaGrDIRQ=; b=yg7pYCUdO3kHMCUUCJG3/gMPwyHixrv+cb/0SxbFyYwWsZfDfwo/c7N2bz1OdXDM52 HSthU3fyr4UcEP6qS568vJJLJ8bhnHr6lYhh+tCBnk8KwAeWD4InbYeSaUCZqD6eSpk4 PbUUq0ezRZgR1wu1asSsr1T35HRhxoBVmGe20FKEdxoErmqYj6UzCd3d4r/6dKnWoY8/ N9LpRCOYi+OAG66YqCKFgVHZwAgwlEPQoNl5I8NBQIaW7QGbGGnEv6Y6sYuygVRytrNk TT9KEGPGpF/+99hSXeTxFDW1jG/YB2yyLfeMdBB6x0iGCiNzW9RzY+pH5uLIvliRqOfy ABsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732290081; x=1732894881; 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=roehYhV9fbym2F/vf27bAbe0CuzVSCLFEeCVaGrDIRQ=; b=YGTWkAbJXpX2+pxHXC2dMINm3mKOctfIkoaHsMZc6Tl1jdb/NZbAIyO19WaGo575CN IHKQntbJF6fpBQ7ifN5P1cMJIT0yHuufKMXSEolaQu692q/r41s91RIUFJWZbPdUlScL QY8MmzVX21BBe84BvmPtr01HRfBUkBxuPhYNn3AuMD3QMx9wHJ9Wf+LCoThI/c1DMGTW OOZUGbd2kREzIFYWTBGR0EDVBq08fv8A+Np/xHnvCCaVN9EYBczTQJJDcf6BBm+0qosj mbrfFrLsgasSX/1OgSe24FulqVQfZYajgJi9gOyFhUP98ffhT2HiJmc/QBQ0ePBPRWD5 mr1w== X-Forwarded-Encrypted: i=1; AJvYcCXi+DvvH+8JbMq6vdVDTY5OPYriUDbWVl/a2cEKRWgmxueCr8WnsmxNS94Jpr0OLu6PWpx1FG6Hjg==@kvack.org X-Gm-Message-State: AOJu0YwZoYuTH73vuameyu8IAQN6cA3zWNhdHLtNgpx/E/i3EJSVJarz 1myZ30X60toM4Za3uQlMnnzgKJziRd+r2Pjd+1Yktc5ECv8HxV+i1JRXgdDZ7Edjm/bRxR2izTj /q50RPgcfeqAR/g== X-Google-Smtp-Source: AGHT+IF6HBbIQggNCbWVHzfdpf5qfU2oWZDDUICEvmMpzMP0kJSXWVx7hqPiHvLxeddSVZjHgvUjH2UBZr7vJN0= X-Received: from aliceryhl.c.googlers.com ([fda3:e722:ac3:cc00:68:fe9:ac10:f29e]) (user=aliceryhl job=sendgmr) by 2002:adf:fd05:0:b0:382:49ff:ef75 with SMTP id ffacd0b85a97d-38260bc27dcmr780f8f.7.1732290080932; Fri, 22 Nov 2024 07:41:20 -0800 (PST) Date: Fri, 22 Nov 2024 15:40:33 +0000 In-Reply-To: <20241122-vma-v9-0-7127bfcdd54e@google.com> Mime-Version: 1.0 References: <20241122-vma-v9-0-7127bfcdd54e@google.com> X-Developer-Key: i=aliceryhl@google.com; a=openpgp; fpr=49F6C1FAA74960F43A5B86A1EE7A392FDE96209F X-Developer-Signature: v=1; a=openpgp-sha256; l=5755; i=aliceryhl@google.com; h=from:subject:message-id; bh=ELmBMFzYYsNvMdFa8fNaCj9uRohYnanctvoUqVOPrOk=; b=owEBbQKS/ZANAwAKAQRYvu5YxjlGAcsmYgBnQKYLQtIIMMayOTF0V1fptgYQ4tpbvUzXCBxvI JXOixUouJmJAjMEAAEKAB0WIQSDkqKUTWQHCvFIvbIEWL7uWMY5RgUCZ0CmCwAKCRAEWL7uWMY5 Rk7MEAC8HmptGWWIAH1NODYFTSG83oi5q2l+ljeKmvdiUHP1c39MD8JUUr8ZQY56Km1mjeH+R45 Bb+bq62Q5v4yR0Bmrf1RlL2cPshEqIV3SaIF1AMB972ROETbmB/IEny0owtXKbAJE8nuaGxBiW9 +Ni0SAFbcP30X2a80Cb2WQGk3ItYkrP3LfPzt43YJ52Y/9anw8UIoT3Q2tLGXMEaFRJ1qOqlsOh 2FxJkSANzWvBix5B/6xqMTf7BrLH60791UzrqLbz0FwyO2GFLZptvL45fwTdb8VCQAONjeNC6Px Q1YQnxM1+zc2qDrMjuFIaJ32y7wOsJNtrLDek249qEOCPFs4y7HYAUbOSy3fNlNcw8TLo7k48Sd 4+RbUJdtj/5k/Vw22wFASM8UZVyfIkvGegvIh2ETxFgNYbg6kFuOAVrxZJBBLN/TdeUgoZDOwpJ sACEu6DJGOnIWvhCKW0OVnw8hrEitbQC5tQz9YVTx2ejH9bDVn96NOSyhlvdNefp6z17zABlNUF 4qyGR6ezIJwaFa/6wtS0DYVYNM8+gGHWc2VjK7VLBH4KUkkAsFmk9F0xl0I87cgYZgWdh44pyge jLxj5Oa4qTkuqPfIZGEV8t+xZFHHKjHnp4P7oWDrnqJgAG26hj0Nv+fEuSr0tclsMTN/ap3OBJC wNxW0WcRdIeDHWQ== X-Mailer: b4 0.13.0 Message-ID: <20241122-vma-v9-8-7127bfcdd54e@google.com> Subject: [PATCH v9 8/8] task: rust: rework how current is accessed 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 , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Alice Ryhl , Andreas Hindborg X-Rspam-User: X-Rspamd-Queue-Id: 6B6B314000F X-Rspamd-Server: rspam01 X-Stat-Signature: kht59tiizsfiah68mnuiqsahdumsfqdk X-HE-Tag: 1732290041-614904 X-HE-Meta: U2FsdGVkX1+XbjPe9v/Et3gNkqJaSvCEMsC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2HCuDlQQ1AvGJVUVOu3GG4Xp5K6c1nwi6Y8ywMZwucEi3tpdZfyBoMOUmBlyVbWYAEeiNiG3jrwtnsxi9N7xs7wZsv4lDVuI8fdWuc9FOPGm1uwfrXVejOOi+6bfok/3nGKY7yXztxY+K92pwfyXlG7BUwalGULF5iIorC6SBLb16S31eMVfe/miBpMEcFzavKB4hywtfTUPozfNBQ//1qoQnMoyx0lNVY7Od9SqyuQJ6mLxH+/S7EZVOW1GXRc1sBlh5j/NjWwckeqF5txGdSceYi26el1hRXxMzEhyM1OC+1wm5U6EmPF2zY537DfdtMx0l+70ukt19mRlPwemCAHMZvJG/JwqQU1sFQ2/94RZpICMzCNw6U35LHaqTnrQCoK7FBKcASf6bVKtQCi7VH+tGY9HV/ruAJQyGmRtaP2gF8/0/67lf+y4FyP+HLunoj0/eM1DGe2C0zvBzpQXo5LkqqnbtCcJWa6arBEhm7qFyQiu2H1ZP/vS9GHf7JYfIllUBzrPxgqWw0lor0= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000002, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Introduce a new type called `CurrentTask` that lets you perform various operations that are only safe on the `current` task. Use the new type to provide a way to access the current mm without incrementing its refcount. With this change, you can write stuff such as let vma = current!().mm().lock_vma_under_rcu(addr); without incrementing any refcounts. Signed-off-by: Alice Ryhl Acked-by: Lorenzo Stoakes --- Reviewers: Does accessing task->mm on a non-current task require rcu protection? Christian: If you submit the PidNamespace abstractions this cycle, I'll update this to also apply to PidNamespace. --- rust/kernel/mm.rs | 22 ------------------ rust/kernel/task.rs | 64 ++++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 35 deletions(-) diff --git a/rust/kernel/mm.rs b/rust/kernel/mm.rs index 50f4861ae4b9..f7d1079391ef 100644 --- a/rust/kernel/mm.rs +++ b/rust/kernel/mm.rs @@ -142,28 +142,6 @@ fn deref(&self) -> &MmWithUser { // These methods are safe to call even if `mm_users` is zero. impl Mm { - /// Call `mmgrab` on `current.mm`. - #[inline] - pub fn mmgrab_current() -> Option> { - // SAFETY: It's safe to get the `mm` field from current. - let mm = unsafe { - let current = bindings::get_current(); - (*current).mm - }; - - if mm.is_null() { - return None; - } - - // SAFETY: The value of `current->mm` is guaranteed to be null or a valid `mm_struct`. We - // just checked that it's not null. Furthermore, the returned `&Mm` is valid only for the - // duration of this function, and `current->mm` will stay valid for that long. - let mm = unsafe { Mm::from_raw(mm) }; - - // This increments the refcount using `mmgrab`. - Some(ARef::from(mm)) - } - /// Returns a raw pointer to the inner `mm_struct`. #[inline] pub fn as_raw(&self) -> *mut bindings::mm_struct { diff --git a/rust/kernel/task.rs b/rust/kernel/task.rs index 9e59d86da42d..103d235eb844 100644 --- a/rust/kernel/task.rs +++ b/rust/kernel/task.rs @@ -94,6 +94,26 @@ unsafe impl Send for Task {} // synchronised by C code (e.g., `signal_pending`). unsafe impl Sync for Task {} +/// Represents a [`Task`] obtained from the `current` global. +/// +/// This type exists to provide more efficient operations that are only valid on the current task. +/// For example, to retrieve the pid-namespace of a task, you must use rcu protection unless it is +/// the current task. +/// +/// # Invariants +/// +/// Must be equal to `current` of some thread that is currently running somewhere. +pub struct CurrentTask(Task); + +// Make all `Task` methods available on `CurrentTask`. +impl Deref for CurrentTask { + type Target = Task; + #[inline] + fn deref(&self) -> &Task { + &self.0 + } +} + /// The type of process identifiers (PIDs). type Pid = bindings::pid_t; @@ -121,27 +141,25 @@ pub fn current_raw() -> *mut bindings::task_struct { /// # Safety /// /// Callers must ensure that the returned object doesn't outlive the current task/thread. - pub unsafe fn current() -> impl Deref { - struct TaskRef<'a> { - task: &'a Task, - _not_send: NotThreadSafe, + pub unsafe fn current() -> impl Deref { + struct TaskRef { + task: *const CurrentTask, } - impl Deref for TaskRef<'_> { - type Target = Task; + impl Deref for TaskRef { + type Target = CurrentTask; fn deref(&self) -> &Self::Target { - self.task + // SAFETY: The returned reference borrows from this `TaskRef`, so it cannot outlive + // the `TaskRef`, which the caller of `Task::current()` has promised will not + // outlive the task/thread for which `self.task` is the `current` pointer. Thus, it + // is okay to return a `CurrentTask` reference here. + unsafe { &*self.task } } } - let current = Task::current_raw(); TaskRef { - // SAFETY: If the current thread is still running, the current task is valid. Given - // that `TaskRef` is not `Send`, we know it cannot be transferred to another thread - // (where it could potentially outlive the caller). - task: unsafe { &*current.cast() }, - _not_send: NotThreadSafe, + task: Task::current_raw().cast(), } } @@ -203,6 +221,26 @@ pub fn wake_up(&self) { } } +impl CurrentTask { + /// Access the address space of this task. + /// + /// To increment the refcount of the referenced `mm`, you can use `ARef::from`. + #[inline] + pub fn mm(&self) -> Option<&MmWithUser> { + let mm = unsafe { (*self.as_ptr()).mm }; + + if mm.is_null() { + None + } else { + // SAFETY: If `current->mm` is non-null, then it references a valid mm with a non-zero + // value of `mm_users`. The returned `&MmWithUser` borrows from `CurrentTask`, so the + // `&MmWithUser` cannot escape the current task, meaning `mm_users` can't reach zero + // while the reference is still live. + Some(unsafe { MmWithUser::from_raw(mm) }) + } + } +} + // SAFETY: The type invariants guarantee that `Task` is always refcounted. unsafe impl crate::types::AlwaysRefCounted for Task { fn inc_ref(&self) {