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 3C3DD27293F for ; Thu, 27 Feb 2025 22:21:58 +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=1740694920; cv=none; b=cRRV1AGj4Fk6y2UNhqeL8KPcNFexzPKlTHSf/7kpF1JpVlxsWWyqtMB7ODD8hK6tyecDqpgJTWjVCdhPnstMkOTRoUrgzXZcrBM8SZggcQnzfMZB+BuWOR6/r+uFKIncbVoQ+kopntVvQTEUSuZLbR4wdWTYc60bLI1YoGt1bBQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740694920; c=relaxed/simple; bh=hQ4d00FYsK6PX4cJ5V/97dD4FXMQprfJGXKt3j2r7xc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qzJIaEIHerqLCU9SPWzRQqF+Tj0I43xRjnBVR6nXW5/MPWPNVE5aCE7T1AieD9OEzTLWe3sjUh66jqSFVcp3ikzp72XiPjUSZ/VbwbE6q6jLp6LU1Qpi/QDtX0yJQHh4J+1xcHvjv6OMrK+WtgThEa7XQv4JiZsC8wihIGjE2HA= 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=WIYBFrl3; arc=none smtp.client-ip=170.10.129.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="WIYBFrl3" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1740694918; 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=KiXnUiI7N6+p9PoDWTOf4LccHzWf5lzcZMcfLAHpP/s=; b=WIYBFrl3nEPChqVP/4x2t9FiGT3f7MddXjsgXZ9PKIEWFDYWtKkj3NRNFdxMF7jh4qIvMj 6FXpLJuIyZCP06QM6fCB2Nqw1LL9Mq83AdY54pEBBQ+yGOjvVNZ4658uR8ILU7VoGlwNm2 8gzTJxnP4G2qkEvP3QQHmY7JNynn2cQ= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-609-oYmzkNd8PB2pbr3nzbtKyQ-1; Thu, 27 Feb 2025 17:21:53 -0500 X-MC-Unique: oYmzkNd8PB2pbr3nzbtKyQ-1 X-Mimecast-MFC-AGG-ID: oYmzkNd8PB2pbr3nzbtKyQ_1740694911 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id B5D36180087F; Thu, 27 Feb 2025 22:21:51 +0000 (UTC) Received: from chopper.lyude.net (unknown [10.22.89.128]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id E0C321944D02; Thu, 27 Feb 2025 22:21:48 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org, Thomas Gleixner Cc: Boqun Feng , 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 , linux-kernel@vger.kernel.org (open list:LOCKING PRIMITIVES) Subject: [PATCH v9 7/9] rust: sync: Introduce lock::Backend::Context Date: Thu, 27 Feb 2025 17:10:18 -0500 Message-ID: <20250227221924.265259-8-lyude@redhat.com> In-Reply-To: <20250227221924.265259-1-lyude@redhat.com> References: <20250227221924.265259-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.0 on 10.30.177.17 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` has the same lifetime, interrupts cannot be enabled whiel the Guard exists. Add a `lock_with()` interface for `Lock`, and an associate type of `Backend` to describe the context. [Boqun: Change the interface a lot, now `SpinLockIrq` can use the `lock()` function, but it always disable the interrupts, reuse the `lock_with()` method to provide a way for locking if interrupts are already disabled. `lock_with()` implementation will be added later.] Signed-off-by: Lyude Paul Co-Developed-by: Boqun Feng Signed-off-by: Boqun Feng --- rust/kernel/sync/lock.rs | 12 +++++++++++- rust/kernel/sync/lock/mutex.rs | 1 + rust/kernel/sync/lock/spinlock.rs | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index eb80048e0110d..e7c1fd028435e 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -46,6 +46,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 @@ -165,8 +168,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 70cadbc2e8e23..53c8e8b90b85c 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 ac66493f681ce..6b355887bd3ea 100644 --- a/rust/kernel/sync/lock/spinlock.rs +++ b/rust/kernel/sync/lock/spinlock.rs @@ -3,6 +3,7 @@ //! A kernel spinlock. //! //! This module allows Rust code to use the kernel's `spinlock_t`. +use crate::interrupt::LocalInterruptDisabled; /// Creates a [`SpinLock`] initialiser with the given name and a newly-created lock class. /// @@ -100,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, @@ -241,6 +243,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.48.1