From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.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 9755E30EF94 for ; Mon, 13 Oct 2025 15:54:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760370882; cv=none; b=rmrZOkvgp1xpp8En/luhRGlD+/wawM5I49+77MjIXLpOO3CjynEB3yX9g3xOHL6ca6Xd1INZMShnWZHVeoFGDm6PWKUQLf0UQfwsTarEqFpyAc1RR3GoJO67Z+A10dGXT6tjJOXKBNxwXEBFsKwpAP2tRTM4LIKd7tFIpjmJNbI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1760370882; c=relaxed/simple; bh=vduko4sGUpP7pjMVck+KUjdVmIvmQ9r4zWWxZQSS4iA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dU0NCDRFmaPNMInB10CHPsBtM2IUACNDljhI28RneGRKqLKUertdINinwgSMPGhPYs28pVmgi1+EBx5RlQ2WRqT1QJzdkI+tEwgrIr/SNl1sv/6jGyRBNUnU8uXYH0G8EcKII7CBVTP0q8pfvzxmMAd4WGb1b27QtjbHNTlsTJE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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=i3zQMnZT; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine 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="i3zQMnZT" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760370879; 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=8oZkaOhh491/dt/LBPcIdZtDtOEojeqJg0p/jpHGuJg=; b=i3zQMnZT1b/2vwKq9sVLtOFlPJb49JDyBCkmdNfrSS9Y665YIRE1T6U5xtFCJWaId9XtJP w2WsQgr88f48JUgqZx5mv0txkyot3OGrbMGGht8fYMP0xSb+iPJztQJQuN3X6a+whBPpw5 z1UyItgZhqJg+WFSvF8REncZYuy0R5Q= Received: from mx-prod-mc-05.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-515-6wU7HW9KNG2nNODMD7Uq_Q-1; Mon, 13 Oct 2025 11:54:36 -0400 X-MC-Unique: 6wU7HW9KNG2nNODMD7Uq_Q-1 X-Mimecast-MFC-AGG-ID: 6wU7HW9KNG2nNODMD7Uq_Q_1760370874 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 349E419560B7; Mon, 13 Oct 2025 15:54:34 +0000 (UTC) Received: from chopper.lan (unknown [10.22.81.1]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 663A43000382; Mon, 13 Oct 2025 15:54:31 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Thomas Gleixner , Boqun Feng , linux-kernel@vger.kernel.org, Daniel Almeida Cc: Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Miguel Ojeda , Alex Gaynor , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich Subject: [PATCH v13 10/17] rust: sync: Introduce lock::Backend::Context Date: Mon, 13 Oct 2025 11:48:12 -0400 Message-ID: <20251013155205.2004838-11-lyude@redhat.com> In-Reply-To: <20251013155205.2004838-1-lyude@redhat.com> References: <20251013155205.2004838-1-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-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Now that we've introduced an `InterruptDisabled` token for marking contexts in which IRQs are disabled, we can have a way to avoid `SpinLockIrq` disabling interrupts if the interrupts have already been disabled. Basically, a `SpinLockIrq` should work like a `SpinLock` if interrupts are disabled. So a function: (&'a SpinLockIrq, &'a InterruptDisabled) -> Guard<'a, .., SpinLockBackend> makes senses. Note that due to `Guard` and `InterruptDisabled` having the same lifetime, interrupts cannot be enabled while the Guard exists. Add a `lock_with()` interface for `Lock`, and an associate type of `Backend` to describe the context. Signed-off-by: Lyude Paul Co-developed-by: Boqun Feng Signed-off-by: Boqun Feng --- V10: - Fix typos - Dirk rust/kernel/sync/lock.rs | 12 +++++++++++- rust/kernel/sync/lock/mutex.rs | 1 + rust/kernel/sync/lock/spinlock.rs | 4 +++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index 27202beef90c8..ae42458bff1c0 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -44,6 +44,9 @@ pub unsafe trait Backend { /// [`unlock`]: Backend::unlock type GuardState; + /// The context which can be provided to acquire the lock with a different backend. + type Context<'a>; + /// Initialises the lock. /// /// # Safety @@ -163,8 +166,15 @@ pub unsafe fn from_raw<'a>(ptr: *mut B::State) -> &'a Self { } impl Lock { + /// 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> { + todo!() + } + /// Acquires the lock and gives the caller access to the data protected by it. - pub fn lock(&self) -> Guard<'_, T, B> { + #[inline] + pub fn lock<'a>(&'a self) -> 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()) }; diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs index 581cee7ab842a..be1e2e18cf42d 100644 --- a/rust/kernel/sync/lock/mutex.rs +++ b/rust/kernel/sync/lock/mutex.rs @@ -101,6 +101,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 6e6d571acd90c..73a7ec554baac 100644 --- a/rust/kernel/sync/lock/spinlock.rs +++ b/rust/kernel/sync/lock/spinlock.rs @@ -3,7 +3,7 @@ //! A kernel spinlock. //! //! This module allows Rust code to use the kernel's `spinlock_t`. -use crate::prelude::*; +use crate::{interrupt::LocalInterruptDisabled, prelude::*}; /// Creates a [`SpinLock`] initialiser with the given name and a newly-created lock class. /// @@ -101,6 +101,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, @@ -243,6 +244,7 @@ macro_rules! new_spinlock_irq { unsafe impl super::Backend for SpinLockIrqBackend { type State = bindings::spinlock_t; type GuardState = (); + type Context<'a> = &'a LocalInterruptDisabled; unsafe fn init( ptr: *mut Self::State, -- 2.51.0