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 0F71AE69186 for ; Fri, 22 Nov 2024 18:03:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 990E76B0085; Fri, 22 Nov 2024 13:03:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 93E476B0088; Fri, 22 Nov 2024 13:03:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7B7EF6B0089; Fri, 22 Nov 2024 13:03:55 -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 5790E6B0085 for ; Fri, 22 Nov 2024 13:03:55 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 08A4F161840 for ; Fri, 22 Nov 2024 18:03:55 +0000 (UTC) X-FDA: 82814502750.30.AA3F716 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) by imf18.hostedemail.com (Postfix) with ESMTP id 640171C0007 for ; Fri, 22 Nov 2024 18:03:28 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hk7DMtGE; spf=pass (imf18.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.160.182 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732298383; 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=gyQ4a39Lo6xL3ZOkg9Xh74/gk7J/C279DSGOjmi1ezA=; b=HEwHwwfKOn83uIyjymPRJZSlRiODLVeERAmOynmsrewk4F2Ac+X5r3V7shFMT1mjGBM5RF UUb2MwgCLez3asB2uRtPPhYsKjFbk5xYMueNPHTqCO34RPtgk2m5Z5mPr5nIC5vUUaAvpp 3fGsyP5j6xxgOo+Tg2eUXCbkAmRAQNE= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=hk7DMtGE; spf=pass (imf18.hostedemail.com: domain of boqun.feng@gmail.com designates 209.85.160.182 as permitted sender) smtp.mailfrom=boqun.feng@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732298383; a=rsa-sha256; cv=none; b=5VwqKJWxw2VX08OlYWNPx/c4B6k9XlVQwIFOLb/n+rJtG/WLw9t8TdoEMYk10Rr8jHGDaS kdoJ3TmKZPx0mtDCUZaAoIOd+dlJ1Stpx9L6XvRCJirPBquyfq4CVIvXdijMUZIUsxYtHn 5ORRX8QjzAdvYuoqGZ+kveBSfhTsMEM= Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-460b2e4c50fso14860011cf.0 for ; Fri, 22 Nov 2024 10:03:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732298631; x=1732903431; darn=kvack.org; h=in-reply-to:content-disposition:mime-version:references:subject:cc :to:from:date:feedback-id:message-id:from:to:cc:subject:date :message-id:reply-to; bh=gyQ4a39Lo6xL3ZOkg9Xh74/gk7J/C279DSGOjmi1ezA=; b=hk7DMtGEucwbc5eVSzkQJRrk3ATQIYSSdG8RyXLmaEodZt2UnMWJn4AS7iHfHrg2Gg EzkRHXn870N/0hPCENvadTsanDw+uAI3zMOP5U23qQy2n6O1c6JJ5BgEJkE/5NhayPS8 8pUYweEZ5w+cmc07tX9c6ISAlYdZD3ieucyqPaAMh8cM2om7VIVakjXExYh9sIxZW6J+ BmxnvwEYcYaVjXeNjXr6DBtck6Fyu0QxwE5VNtdDGu2vffdqzWKWap6OvjC93BQvEESs QmmtvhiTYrSXxi08Z1R3nJZA2YaMWHDFOHR78PTZFjqiuaTDYkjLc7BrxR8WHhjP0p3s gqRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732298631; x=1732903431; h=in-reply-to:content-disposition:mime-version:references:subject:cc :to:from:date:feedback-id:message-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gyQ4a39Lo6xL3ZOkg9Xh74/gk7J/C279DSGOjmi1ezA=; b=AlkFzBpD5OnrF68CXl2dgqV2njzLFHKBTIzBfPgXLMOOuPX9iMNf6hXYWdXRDlKWUN dkbIa7tQY2G05QEefmcC724p+F87uWYPA/gYWrraInOYEnGdw+Ny2p4kAmRmIAfSU0xk nzq46xHSYkHIPkKf3a7ZUbAqPYXrg2w4zy4Unyb8B0cpRus4Hfb09kTvvTlxl0jZuXEi dP909JAA1l0wD65PTpEOoNK7l13ixl5IfGdAvn+PpGS/K1+0TXMVAfovOtjnBHv2zCNj wWEt/IJeMdBvj9CmgSvpEY9wTmDxhmejVYw/S3jLfu7mvN5ijvQ+gBuxz8hi28Q1Uowf DOIw== X-Forwarded-Encrypted: i=1; AJvYcCX1D/+ycyHgejuaifnWopOdGX7CM+F5Zv4VEjIm5uB0hZf0z441W/Cu7zkBkC/uVvC2RJLsUIduig==@kvack.org X-Gm-Message-State: AOJu0Yx1RCTdL1WXi8ePKy66vt1tgMTnwcbczK2JlS5VAf4QoV6GFKdr 3igH8pOtN7cypizVO3/R7ufqwFXxD7jOImUWgZ0xIjZb0h8Vq9E/ X-Gm-Gg: ASbGncuZKKprzXV6tVRLk3/k7/MSTLnwuJHXMc+UK+RRvoSgM9/Xj/0hrE/PAf1AjQX HBRC6ZTxU2pR7Nz1yAUQQm22v5F+lHqS6O4MFsHy5pes75d1KaSXieYJbS6Xzyp1+RCMtVqoVK7 ZVQmt4eIpomYZSDtQlCVXq1ig5g8/JIt6yWIX67cb/QjlxWAyTIgHlLfUWDlTlpKMqCKgePe/lg dmrYJMiSVFA6YEtsrLN20dg88IRbn+/9/ukkoRjZ+j+SV9yYIyTmyCxfPlHJ56pWmEJeItP6inA cUuspl0d+8/aStD7RdzUJu2RMcmdfdEnoiN+QkjU X-Google-Smtp-Source: AGHT+IGFAFc7OWIyavb5BtUbqFJ5IGEgdtl2dxH04LafA2+Go9twvT6FtggFGFkQV4F4YRIFJAlu3g== X-Received: by 2002:a05:622a:4cf:b0:461:48f9:44e2 with SMTP id d75a77b69052e-4653d3e1205mr54132261cf.0.1732298631370; Fri, 22 Nov 2024 10:03:51 -0800 (PST) Received: from fauth-a2-smtp.messagingengine.com (fauth-a2-smtp.messagingengine.com. [103.168.172.201]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4653c3ee449sm14406221cf.26.2024.11.22.10.03.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Nov 2024 10:03:50 -0800 (PST) Message-ID: <6740c786.050a0220.31315a.5363@mx.google.com> X-Google-Original-Message-ID: Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfauth.phl.internal (Postfix) with ESMTP id 3394D1200043; Fri, 22 Nov 2024 13:03:50 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-05.internal (MEProxy); Fri, 22 Nov 2024 13:03:50 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefuddrfeelgdekfecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdpuffr tefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnth hsucdlqddutddtmdenucfjughrpeffhffvvefukfhfgggtuggjsehttdertddttddvnecu hfhrohhmpeeuohhquhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtg homheqnecuggftrfgrthhtvghrnhepuedttddvuedvudehffdvjeegtddvkedvleethfek udetgeejffekleeghfevveetnecuffhomhgrihhnpehmmhhifhhmmhdrihhsnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhepsghoqhhunhdomhgv shhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedtieegqddujeejkeehhe ehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfhhigihmvgdrnhgrmhgv pdhnsggprhgtphhtthhopedvvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtoheprg hlihgtvghrhihhlhesghhoohhglhgvrdgtohhmpdhrtghpthhtohepohhjvggurgeskhgv rhhnvghlrdhorhhgpdhrtghpthhtohepfihilhhlhiesihhnfhhrrgguvggrugdrohhrgh dprhgtphhtthhopehlohhrvghniihordhsthhorghkvghssehorhgrtghlvgdrtghomhdp rhgtphhtthhopehvsggrsghkrgesshhushgvrdgtiidprhgtphhtthhopehjhhhusggsrg hrugesnhhvihguihgrrdgtohhmpdhrtghpthhtoheplhhirghmrdhhohiflhgvthhtseho rhgrtghlvgdrtghomhdprhgtphhtthhopegrkhhpmheslhhinhhugidqfhhouhhnuggrth hiohhnrdhorhhgpdhrtghpthhtohepghhrvghgkhhhsehlihhnuhigfhhouhhnuggrthhi ohhnrdhorhhg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 22 Nov 2024 13:03:49 -0500 (EST) Date: Fri, 22 Nov 2024 10:03:48 -0800 From: Boqun Feng 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 , Gary Guo , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , linux-kernel@vger.kernel.org, linux-mm@kvack.org, rust-for-linux@vger.kernel.org, Andreas Hindborg Subject: Re: [PATCH v9 8/8] task: rust: rework how current is accessed References: <20241122-vma-v9-0-7127bfcdd54e@google.com> <20241122-vma-v9-8-7127bfcdd54e@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20241122-vma-v9-8-7127bfcdd54e@google.com> X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 640171C0007 X-Stat-Signature: dizt4f3437y5grfp5r79gicymmrhetxp X-Rspam-User: X-HE-Tag: 1732298608-432648 X-HE-Meta: U2FsdGVkX1/6W/YH9KNb263eYsQfhe/fPTBAiDxSb0v5yXpuqCaIfx1ecL3eK8Cr1rneQ9Sr52VQZWgZNB9HVrT8/JQlma3u6q2yMrt60E9fzG91IesBGDj5N7iZOApBkh2JY4GJPoJEiqbaghaddNXP4UcowKPTEVwhrtabLrngDSco0OTYanqLVu43MAK1xo0PnCidrMSAnTILiyKE+tl0JXNV5r7DkSe1ujSjMYNrAySvp8KgKdzGodz2s+kMrZivIzD7FzKJx1SqDiRmp89tiCd8GxarDfduyjbX6uGTakfXHXaHADFemV3bABi1/uPT69FCPnFjs4LT9JUA7FCJoRN3+Lkwo5Edsob5nlTlgZAxBZIaRjD6ukCp0vdaFKuPIS7O7POowi6A2B9IwWlqQiSpN431Im5AmBxjURgLtldFgqEuLGFt+hPSxuvPSUK9rs9QYnqnLBsrJcGV+wSGgCxZ/uQ+sgVZZTxpIjeAZSYzl+3sLzQUP/n/h66pc/R+6OHlM5j8l9anY2OPKFbmgThz+wB3XUQtwXvvNn4NYl3HNotbBfHK0xN1EahE6Jj7c05zlgOdP97CWB4GzZbjz7wvqts8fmiLX/amFeK4Ir9RedOWwct8VUiPmA7yRNHJxvTfrHT52MUEAovjYnLl+wt4TUMJBUJWTm6Ghl3O0N4uSy2JlvGP/MZtrLW4ZSf4ekan70DnnxYa5yFjGqzG3aF4RfjC6TGxrdd5qGo0Gsr8+AiCigDIjT+Zt2TsIal/4poJvdVHWvQa7cXEyTI5Z3qYfR3zBSeUsQug8qVjQ4BkpSPvVxrUZfsBeR1SIzTLXUiigk4Nn8J4mOCjn9JHZ4ri9hJ+Y3qmRHHPaYJd2MU1I4EMczvlkTurhsD9b/J8lt0I3H2AkYpeOKKLbqBJutgqHuVEQtFowcXGfMBLlg3etCYwYxUs0Eg2Ek+ajwwcgRZBeY9S5sdj30I k7OKgSKF J1hZjdKtXtedcdKTVpILe88jHF04ZFcIi5xqvrMJIl1/EDDptkdaLdOIxmXrr+fNukuBMd6yNDjWSBwznv7A+j+Gk0ITPwlYrg7en3NyZ2DGtIDgmE9pKLYvC8MyWQaNuFhzIwznvkSZT4QBtTavrjoDpAT7DTaR3Vi2ppsJibWX9kOIeCA5Tm3XaxiTYqhyqtLNj5lbZKHWMsr24BKkzhmTlXrN69TaJuZqiGrhzje3wABW+MHFkIxIs84ubk4AkIiZ+kpcyizyxk112fvuob4kqaHfQdjhW8Fka7JzXiZLJXiOOQ2OjKKg3qP3S5AueXLHiUYNweWk9KWQHxXJIoQ7vPGAb6p6cghYGYBQCs5O+H7NBNvKe/I6waQFHh8IeO2UWIXn7RfaglmmD+EHh4000+cfF8MVRj8v26O8FHtsXxkaZOUdELjPBiB1+IRjK2Rvo/fBxGktBnj9iOGEL4auNRQ== 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: On Fri, Nov 22, 2024 at 03:40:33PM +0000, Alice Ryhl wrote: > 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 > --- > 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); > + I think you need to make `CurrentTask` `!Sync`, right? Otherwise, other threads can access the shared reference of a `CurrentTask` and the ->mm field. I'm thinking if we have a scoped thread/workqueue support in the future: let task = current!(); Workqueue::scoped(|s| { s.spawn(|| { let mm = task.mm(); // do something with the mm }); }); > +// 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> { Hmm... similar issue, `MmWithUser` is `Sync`. > + 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. Regards, Boqun > + 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) { > > -- > 2.47.0.371.ga323438b13-goog >