From nobody Sat Nov 30 05:51:25 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA96F1C2327 for ; Thu, 12 Sep 2024 19:06:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726167972; cv=none; b=aojZC3i8QdhjlF39OLmrRiRx5kRaz/qeCxTA8s51Ak4+frYBNN37Bz3GEBrqQJUXTHFVkxaFIlRtozicrAKnEKMLJI3KPN1ib6jhEGnb+gqYGleQC+5kwGg5kILAgz+4ChgYfaCMXa7vzI9dOiKfYGEN8V66wlyRFqeBTCyDOBc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1726167972; c=relaxed/simple; bh=/rdYGX7UjvkB2gk5fsyYiBzOkcUaojbBXtAJ5ZK88U8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JaT8wcAjlktOZGUIT5dzO6z49/sOyR1Tq6JKHiVQxam8eTcHe+1Cv/HUgSmOdtf+UUD86LYEmGBzIkyJERBRVD0gp6tJqUaCVt/vk2HoWsKLtFhDn3IwjZKs+Yl18biqh1ZCGR098VW98zapWawnQk4R/LgBQiCyv1peuGN36hU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Zgp8AbKy; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Zgp8AbKy" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1726167969; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WKQklmomFwAhOEFxjHGrCGOmakddEyukxyf0Xx3I0gQ=; b=Zgp8AbKyEsRK0Ydv1GUGc8mydkQOUrh8HOFar5Aedb7snjFzMIP3axqFJ6nqlvICrFCt9I xfbr+rhoFrWMTPI/uys71CqyLwecYZdr9fC+gezRJKVJRRnF4UwU2ifRArEpNdg5OZTxC1 8jFwFv+8jXEl/u5Z2TBsfyI1FZJ0c2s= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-686-IQ4apj-_MBKDrbtMdN8_uw-1; Thu, 12 Sep 2024 15:06:08 -0400 X-MC-Unique: IQ4apj-_MBKDrbtMdN8_uw-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E369F1944AB1; Thu, 12 Sep 2024 19:06:05 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.9.159]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 4B24C19560B5; Thu, 12 Sep 2024 19:06:00 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org Cc: Danilo Krummrich , airlied@redhat.com, Ingo Molnar , Will Deacon , Waiman Long , Peter Zijlstra , Thomas Gleixner , linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Martin Rodriguez Reboredo , Valentin Obst Subject: [PATCH v5 2/3] rust: sync: Introduce lock::Backend::Context Date: Thu, 12 Sep 2024 15:04:17 -0400 Message-ID: <20240912190540.53221-3-lyude@redhat.com> In-Reply-To: <20240912190540.53221-1-lyude@redhat.com> References: <20240912190540.53221-1-lyude@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Lyude Paul Reviewed-by: Benno Lossin Reviewed-by: Daniel Almeida --- 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) Signed-off-by: Lyude Paul --- 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 f6c34ca4d819f..7429bda852173 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -38,6 +38,9 @@ pub unsafe trait Backend { /// [`unlock`]: Backend::unlock type GuardState; =20 + /// The context which must be provided to acquire the lock. + type Context<'a>; + /// Initialises the lock. /// /// # Safety @@ -120,14 +123,24 @@ pub fn new(t: T, name: &'static CStr, key: &'static L= ockClassKey) -> impl PinIni } =20 impl Lock { - /// Acquires the lock and gives the caller access to the data protecte= d by it. - pub fn lock(&self) -> Guard<'_, T, B> { + /// Acquires the lock with the given context and gives the caller acce= ss 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 existe= nce of the object proves // that `init` was called. let state =3D 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 protecte= d by it. + #[inline] + pub fn lock<'a>(&'a self) -> Guard<'a, T, B> + where + B::Context<'a>: Default, + { + self.lock_with(B::Context::default()) + } } =20 /// 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 =3D bindings::mutex; type GuardState =3D (); + type Context<'a> =3D (); =20 unsafe fn init( ptr: *mut Self::State, diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spin= lock.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 =3D bindings::spinlock_t; type GuardState =3D (); + type Context<'a> =3D (); =20 unsafe fn init( ptr: *mut Self::State, --=20 2.46.0