From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 DEA6917C211 for ; Fri, 5 Jun 2026 14:04:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780668253; cv=none; b=r13zxUwxduX+/RSJZHRgxBzHBt2kezmX9DjmeJZNuTFfveGPtKEoOtQYE9vsYLV+anBGQojKlxTvSiICLFiezlbWtXIlv1uEdFNY6M9QrDHf+AZfpBVa2XovJkpqnu8U/8XtLA/Wo/dCgNvA+50fm66yojgtYyYekSVpwpclYa4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1780668253; c=relaxed/simple; bh=nE+jrhG/ppYv31sfWuxQUTVFQS7am2LwuFIGyuBECqE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=AmhJ/nK/obDAkiI9BJ/oNz5v2oKgD4TpiSAoKQ15WaWqFpdxQ/tE99pmA5O4HOa3llf/AXa73PPa5oRryozqN8SumJ0rcKzAKBVjaO5bISSiCRJLK72sgpu4z9h8oD4XI0Rd53hIyT/vnWoiSabV6uIRK80j0YplJcD1mLol/XQ= 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=GUoXzFnC; arc=none smtp.client-ip=209.85.128.74 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="GUoXzFnC" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-490ae3bcf4cso10363755e9.3 for ; Fri, 05 Jun 2026 07:04:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1780668250; x=1781273050; 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=KzxQflFVSTadDNXMRobclacHPyZHXHcPR4PL2Kdl0Ys=; b=GUoXzFnCTBFfQI6dDid1XcZGmj7m2tSJY7WIFu2ckaLvRyF1aJ3fw4aMzPHvtGcze2 QZtE3FTt88bH8jLLG1WFqSHA/WaYnVcx0syrgdlApwIkmT2ctoQ/OQjyqsGo+RH6LiQp t7dpVTiDiyi7PgEX/QK6OsVfFBqUjVXFWJ723rkfFIrHKbokTUMlKAgtrgZ0/oJn387Q UcevcjD8fNoA7vlfqBVR+FMkgvu5bbPMiHEBbZJD89PGagrKGuUSBHzUdl4LiwLDbMYc YKYiHNxind5lwKlIPAaCkYY4Etop/9jnA5MbnYuFV1ESvHOVoBN5q0Y3mPR66YR7KzSp v0/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1780668250; x=1781273050; 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=KzxQflFVSTadDNXMRobclacHPyZHXHcPR4PL2Kdl0Ys=; b=R91G/Nv+5XOREscyLTv1sIUiFd8VHiuplZr4LsC1P1wCcddIlXrgLOQ/zSaQ9XqlOi vDpNo0/wYjkcboJNgxEgg7HhbqWu6Utl8K4vIl8tEgItGG0TzoIzyUAkzHkwt2Ldj0QX KlPBRnVRwkxg5SIHFcWWfwg50niQ26zvKC0Ar8GXA2hk09cm9/evIYlrqM8/I12QmjO+ MTgdsanQ38wQYUb2pbx9nl+OPaW/rqQJuo5foSP033tCBLIwkuDY4YVdC7HdODebb0cg oBekvMCqWQkSg+sNi8zELQvXW2SdIMVs92+ASA+kfyPPuIxNhz/mXMjuKTt/FiiL2vIR h+7A== X-Forwarded-Encrypted: i=1; AFNElJ/uKkZ5iQkIGfuy/nDz9hUvLZpQw6hYwld2nV4fYnJk16LwNVG9XCV9NP9gSheUfaMWopq7DOMc+mVaO9MAHQ==@vger.kernel.org X-Gm-Message-State: AOJu0YyThR2ThkTJ09ODbpslh3BdEU18KUG2MSIFaBZTG3FDqjvqMDD/ 5iL9346M5g/zUlPwKlECizwqv8QNGySifa8uIokuAjiolYtLZ1wQ7Yz1YeOu7m6OPHcSHhzij4m uh6JYyf5VL2CHKOFh/Q== X-Received: from wmsk10.prod.google.com ([2002:a05:600d:848a:b0:490:acbb:67d7]) (user=aliceryhl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:45d1:b0:490:b9c3:6c62 with SMTP id 5b1f17b1804b1-490c261007dmr52711405e9.28.1780668249942; Fri, 05 Jun 2026 07:04:09 -0700 (PDT) Date: Fri, 5 Jun 2026 14:04:08 +0000 In-Reply-To: <20260605133541.22569-6-boqun@kernel.org> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260605133541.22569-1-boqun@kernel.org> <20260605133541.22569-6-boqun@kernel.org> Message-ID: Subject: Re: [RFC PATCH 3/3] rust: rcu: Introduce RcuFreeBox From: Alice Ryhl To: Boqun Feng Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org, rcu@vger.kernel.org, Miguel Ojeda , Gary Guo , "=?utf-8?B?QmrDtnJu?= Roy Baron" , Benno Lossin , Andreas Hindborg , Trevor Gross , Danilo Krummrich , "Liam R. Howlett" , Andrew Ballance , "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Uladzislau Rezki , Steven Rostedt , Mathieu Desnoyers , Lai Jiangshan , Zqiang , Sumit Semwal , "Christian =?utf-8?B?S8O2bmln?=" , maple-tree@lists.infradead.org, linux-mm@kvack.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, Philipp Stanner , Lyude Paul , Daniel Almeida , "Onur =?utf-8?B?w5Z6a2Fu?=" Content-Type: text/plain; charset="utf-8" On Fri, Jun 05, 2026 at 06:35:41AM -0700, Boqun Feng wrote: > The current RcuBox will call the `drop()` function after a grace period > inside an RCU callback. This suffices for maintaining a RCU-protected > object: > > RcuBox::drop(): > call_rcu( > |..| { // <- call back after one grace period. > T::drop(); // <- call the destructor of the inner object. > } > ) > > However, to support a different RCU usage pattern as below we need to > extend RcuBox: > > 1. clean up the object, and unshare it from future RCU readers. > 2. wait for an RCU grace period. > 3. no other RCU readers, we can free the memory. > > An `RcuFreeBox` is introduced to provide support for > this: > > RcuFreeBox::drop(): > T::drop_before_gp(); // clean up and ushare. > kfree_call_rcu(..); // free it after one grace period. > > Signed-off-by: Boqun Feng > --- > rust/kernel/sync/rcu.rs | 31 +++++++++++++++ > rust/kernel/sync/rcu/rcu_box.rs | 68 +++++++++++++++++++++++++++++++-- > 2 files changed, 95 insertions(+), 4 deletions(-) > > diff --git a/rust/kernel/sync/rcu.rs b/rust/kernel/sync/rcu.rs > index 7da6b8d22277..7c26591bb318 100644 > --- a/rust/kernel/sync/rcu.rs > +++ b/rust/kernel/sync/rcu.rs > @@ -4,6 +4,8 @@ > //! > //! C header: [`include/linux/rcupdate.h`](srctree/include/linux/rcupdate.h) > > +use core::pin::Pin; > + > use crate::{ > bindings, > types::{ > @@ -82,3 +84,32 @@ pub trait ForeignOwnableRcu: ForeignOwnable { > /// [`from_foreign`]: ForeignOwnable::from_foreign > unsafe fn rcu_borrow<'a>(ptr: *mut ffi::c_void) -> Self::RcuBorrowed<'a>; > } > + > +/// Declares a struct is safe to free after a grace period if all readers are guarded by RCU. > +/// > +/// # Safety > +/// > +/// Implementation must guarantee `drop_before_gp()` makes sure no future RCU reader will access > +/// any part of [`Self`], as a result, after `drop_before_gp()` return + one grace period, no RCU > +/// reader will be on the object, and it's safe to free it. > +/// > +/// Notes for implementators: implementing this trait in general requires `Self` being a > +/// [`UnsafePinned`], i.e. a `&mut Self` is not a noalias reference if `Self` has non-trivial > +/// `drop()` function. > +pub unsafe trait RcuFreeSafe { > + fn drop_before_gp(self: Pin<&mut Self>); > +} Should this have an associated type for the rcu-safe view? pub unsafe trait RcuFreeSafe { type RcuView<'a>; /// Access this value in a manner that is safe after /// `drop_before_gp` for one grace period. fn rcu_view<'a>(self: Pin<&'a Self>, _rcu: &'a RcuGuard) -> Self::RcuView<'a>; /// Drop this value in a manner where it may still be accessed via /// `rcu_view` for one grace period. /// /// # Safety /// /// All other accesses to this value must happen before the call to this /// method, except for accesses using `rcu_view`. fn drop_before_gp(self: Pin<&mut Self>); } The idea being that once you call `drop_before_gp()`, the value immediately becomes unusable as the type itself, but you can still use it via `rcu_view`. The `RcuView` type can then be a type that has a subset of the type's methods that is safe to use for one grace period after `drop_before_gp`. If you define the trait like this, then PollCondVar becomes RcuFreeSafe. It can't be RcuFreeSafe today because you must not create new waiters after `drop_before_gp()` is called. With this modified trait, it can simply not provide methods for registering new waiters from the RcuView type. Alice