From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qk1-f177.google.com (mail-qk1-f177.google.com [209.85.222.177]) (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 55C67175D32; Sun, 29 Sep 2024 23:53:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727654005; cv=none; b=py98qHgkwY+dzVNuncXiD4vLlJaWXINL3L9egRp/YOHZ9jZkyvIxTzMSAHi4TfLdXhZtn1QKwTQpa7rg35fMFEVvSUPAecBueekT+vyPd6IUZIR6Pno+Xd+qfzG6oTRISpassuVxxgNQFSWAG3E+6S6hsZbgd7on1L8j6Mh7DGg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727654005; c=relaxed/simple; bh=UKRlj9fNkq2HxhGyfdf1m2RQP/aQuJaaWnuFF5+Td60=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=SNd6X4fxkCK49/ue0fSCVeFdVlTq2C8z1EOhDpJgW/Qh7AWcPov3zGdjOVMDtxER4B/ZZgnHOkXbX0kyoKWveX54bllkgE0pN1Aq5ui74dZ9Jpp0Xg6OIOpERR3cUMAh4/jc5J19pzI1CCwLMY6+AdcBABEb/MbLvkIMO26oMNQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ZVn3EfQB; arc=none smtp.client-ip=209.85.222.177 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ZVn3EfQB" Received: by mail-qk1-f177.google.com with SMTP id af79cd13be357-7a9aec89347so246366885a.0; Sun, 29 Sep 2024 16:53:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1727654003; x=1728258803; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=VAEKMLkD22MSDvK7Vrvcg2LidGBpc3vFN2oLL4LAdJQ=; b=ZVn3EfQB2APhY4OUJp1ZejTd1I+lNI78F0ausuwhlGPnbSMAnxMFO/8do3+si5vd5D oa9yUwjqE19k83toDbGC6ZaQnl3E2Kwwoeot1WZgYE3GuPtC7zZI/8F+UIq3V9LAXrbn b7rP3AdpuuxEHKRSSV5ynptqljfTk5OKjJrU2uoYc/KVc6p+QEwq2w9eKbiO7cF/deBN gWKBl9LbXEPUPvLEkhdOirF8hv2KIXqp8lnQKi04SRYHefAgwx6PpmE+glpxuwsPzerJ vE9izHc3ukRG5Icn5drc+J5KWd6savJaMc8PQUOj3wy2zi2liXb1t6kbLrpF0NQHNbKi 3HSQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727654003; x=1728258803; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VAEKMLkD22MSDvK7Vrvcg2LidGBpc3vFN2oLL4LAdJQ=; b=ayWWvBQhPB2L/JaOyJ9v3MDwIH95ot8oamic/z6aRfZgMCf6S0iPVqQoRiyfRsXa7t sS4+B6ppm7VNqaYQZ0hcDxSXPtVU/571dKaOSx9QxgskbXLCrUWYax5o2v/ESoUTl9M5 /ye9DtdJJhSoONq3zIpJE6M+1NT+ho0X7JzGA2un/aZymFt2jspMxpxtFRsaB78pFy+H GShMIEwTEOBW7FOpm9M0GUE/IkTC4rUE0XaHm/7BhAiBeYxRumz0ZgX3a647X2EJ5b8o DP+qd35cRKewtoDvREEK9EzFxo0FgU1Qphps+J2KLZfxfh0jYC6XuI/zK2f/PcqglS2R ALyg== X-Forwarded-Encrypted: i=1; AJvYcCUncC3RmoY5y8UUFr0FaasBnKtLsFTFwijAq6F/B2MYI9CQskTxoDEIWY8PrJnWrKEYNTo6Edv2NFAZKZ8=@vger.kernel.org X-Gm-Message-State: AOJu0Yz8VbUeVZd4Hdr3vsWrq3UiMCDa72aAVjNn4Ra0yxBBdh+ThINU o0OGOjdFRK6FgtBEDTnY2kTkj0r45VccVMH3/GmI+EQqbhrzxmhT X-Google-Smtp-Source: AGHT+IE3AlFTSmZRFXETlWkeqIHSyvDsuJb3Kq4JEYxwxzjbdjN5s8fx6qUx+YeHVOEnwXSWsnHhrw== X-Received: by 2002:a05:620a:1917:b0:7ac:b89f:5308 with SMTP id af79cd13be357-7ae37824642mr1928490985a.7.1727654003115; Sun, 29 Sep 2024 16:53:23 -0700 (PDT) Received: from fauth-a1-smtp.messagingengine.com (fauth-a1-smtp.messagingengine.com. [103.168.172.200]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7ae3782c6c0sm362741185a.88.2024.09.29.16.53.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Sep 2024 16:53:22 -0700 (PDT) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfauth.phl.internal (Postfix) with ESMTP id E574A1200043; Sun, 29 Sep 2024 19:53:21 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-01.internal (MEProxy); Sun, 29 Sep 2024 19:53:21 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvddugedgvdekucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepfffhvfevuffkfhggtggujgesthdtrodttddtvden ucfhrhhomhepuehoqhhunhcuhfgvnhhguceosghoqhhunhdrfhgvnhhgsehgmhgrihhlrd gtohhmqeenucggtffrrghtthgvrhhnpeeitdefvefhteeklefgtefhgeelkeefffelvdev hfehueektdevhfettddvteevvdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmh epmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhpvghrshhonhgrlhhi thihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrdhfvghngheppehgmh grihhlrdgtohhmsehfihigmhgvrdhnrghmvgdpnhgspghrtghpthhtohepvdefpdhmohgu vgepshhmthhpohhuthdprhgtphhtthhopehlhihuuggvsehrvgguhhgrthdrtghomhdprh gtphhtthhopehruhhsthdqfhhorhdqlhhinhhugiesvhhgvghrrdhkvghrnhgvlhdrohhr ghdprhgtphhtthhopegurghkrhesrhgvughhrghtrdgtohhmpdhrtghpthhtoheprghirh hlihgvugesrhgvughhrghtrdgtohhmpdhrtghpthhtohepmhhinhhgohesrhgvughhrght rdgtohhmpdhrtghpthhtohepfihilhhlsehkvghrnhgvlhdrohhrghdprhgtphhtthhope hlohhnghhmrghnsehrvgguhhgrthdrtghomhdprhgtphhtthhopehpvghtvghriiesihhn fhhrrgguvggrugdrohhrghdprhgtphhtthhopehtghhlgieslhhinhhuthhrohhnihigrd guvg X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 29 Sep 2024 19:53:21 -0400 (EDT) Date: Sun, 29 Sep 2024 16:52:25 -0700 From: Boqun Feng To: Lyude Paul Cc: rust-for-linux@vger.kernel.org, Danilo Krummrich , airlied@redhat.com, Ingo Molnar , Will Deacon , Waiman Long , Peter Zijlstra , Thomas Gleixner , linux-kernel@vger.kernel.org, Benno Lossin , Daniel Almeida , Gary Guo , Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , =?iso-8859-1?Q?Bj=F6rn?= Roy Baron , Andreas Hindborg , Alice Ryhl , Trevor Gross , Martin Rodriguez Reboredo , Valentin Obst Subject: Re: [PATCH v6 2/3] rust: sync: Introduce lock::Backend::Context Message-ID: References: <20240916213025.477225-1-lyude@redhat.com> <20240916213025.477225-3-lyude@redhat.com> Precedence: bulk X-Mailing-List: rust-for-linux@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240916213025.477225-3-lyude@redhat.com> On Mon, Sep 16, 2024 at 05:28:05PM -0400, Lyude Paul wrote: > Now that we've introduced an `IrqDisabled` token for marking contexts in > which IRQs are disabled, we need a way to be able to pass it to locks that > require that IRQs are disabled. In order to continue using the > `lock::Backend` type instead of inventing our own thing, we accomplish this > by adding the associated Context type, along with a `lock_with()` function > that can accept a Context when acquiring a lock. To allow current users of > context-less locks to keep using the normal `lock()` method, we take an > example from Wedson Almeida Filho's work and add a `where T<'a>: Default` > bound to `lock()` so that it can only be called on lock types where the > context is simply a placeholder value, then re-implement it through the new > `lock_with()` function. > Given that Backend::Context and "lock() where Context: Default" are basically Benno's idea, I think it's fair to have a Suggested-by tag here. > Signed-off-by: Lyude Paul > Reviewed-by: Benno Lossin > Reviewed-by: Daniel Almeida > Reviewed-by: Gary Guo > Reviewed-by: Boqun Feng Regards, Boqun > --- > > V3: > * Use explicit lifetimes in lock_with() to ensure self and _context have > the same lifetime (Benno) > * Use () for locks that don't need a Context instead of PhantomData (Benno) > V4: > * Fix typo (Dirk) > > --- > rust/kernel/sync/lock.rs | 17 +++++++++++++++-- > rust/kernel/sync/lock/mutex.rs | 1 + > rust/kernel/sync/lock/spinlock.rs | 1 + > 3 files changed, 17 insertions(+), 2 deletions(-) > > diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs > index d6e9bab114b87..cc39a3dc3f20b 100644 > --- a/rust/kernel/sync/lock.rs > +++ b/rust/kernel/sync/lock.rs > @@ -43,6 +43,9 @@ pub unsafe trait Backend { > /// [`unlock`]: Backend::unlock > type GuardState; > > + /// The context which must be provided to acquire the lock. > + type Context<'a>; > + > /// Initialises the lock. > /// > /// # Safety > @@ -125,14 +128,24 @@ pub fn new(t: T, name: &'static CStr, key: &'static LockClassKey) -> impl PinIni > } > > impl Lock { > - /// Acquires the lock and gives the caller access to the data protected by it. > - pub fn lock(&self) -> Guard<'_, T, B> { > + /// Acquires the lock with the given context and gives the caller access to the data protected > + /// by it. > + pub fn lock_with<'a>(&'a self, _context: B::Context<'a>) -> Guard<'a, T, B> { > // SAFETY: The constructor of the type calls `init`, so the existence of the object proves > // that `init` was called. > let state = unsafe { B::lock(self.state.get()) }; > // SAFETY: The lock was just acquired. > unsafe { Guard::new(self, state) } > } > + > + /// Acquires the lock and gives the caller access to the data protected by it. > + #[inline] > + pub fn lock<'a>(&'a self) -> Guard<'a, T, B> > + where > + B::Context<'a>: Default, > + { > + self.lock_with(B::Context::default()) > + } > } > > /// A lock guard. > diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs > index 30632070ee670..7c2c239944931 100644 > --- a/rust/kernel/sync/lock/mutex.rs > +++ b/rust/kernel/sync/lock/mutex.rs > @@ -93,6 +93,7 @@ macro_rules! new_mutex { > unsafe impl super::Backend for MutexBackend { > type State = bindings::mutex; > type GuardState = (); > + type Context<'a> = (); > > unsafe fn init( > ptr: *mut Self::State, > diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spinlock.rs > index ea5c5bc1ce12e..97d85a5576615 100644 > --- a/rust/kernel/sync/lock/spinlock.rs > +++ b/rust/kernel/sync/lock/spinlock.rs > @@ -92,6 +92,7 @@ macro_rules! new_spinlock { > unsafe impl super::Backend for SpinLockBackend { > type State = bindings::spinlock_t; > type GuardState = (); > + type Context<'a> = (); > > unsafe fn init( > ptr: *mut Self::State, > -- > 2.46.0 >