From nobody Thu Nov 21 11:15:18 2024 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 E7A511C5799 for ; Wed, 20 Nov 2024 22:35:06 +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=1732142109; cv=none; b=qq1hM/e55+L4WtnckVKBoVaWQg9mEz/lJLEPJxGnIOKzy5Ezrf5XquD0/n3ePogn7FCiz9YRd4GGp0FchkD0HR7MtWt8hN1gEqCMf+y/85lH+a2N0T/QHE8aulrDuN38nsVhgzKX3UdB/e1fwIeO6tfQcGoCFAP6MsIEwezZx0g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732142109; c=relaxed/simple; bh=ZBGY0bpYhcq+yEmyUweUjKz74uchUlZYQcLYmgowHNQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Br1wuRrMMIxO9dK/3wxlZc3qnvGOyC1y5HIMSKN74LnHfOIMvJftTqD/ySLb0ksWObZdLKhFjsSlxMpcJ7xrM2W3UBLWHm6mnQR7fwD1x5fVC93PTquqY6YxmBbpmfLVH+V02ha3ss76XVAvmq+2RDYP/Ppax4QRBcRP/ryHIgw= 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=iYKqVZYh; 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="iYKqVZYh" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1732142106; 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=UYXybaTZfjauX1323Z8rJ0SA32Ax5BTepGpZK1kQKUg=; b=iYKqVZYhN7QBtBJ82bkdRdw5PpbYM9Bn9CMXjGjgyBDRrCfUDfgEVEsNxys91v50WviKYi xCo5xzeVvdq92uZ+2CxF7AeyeQDq2WIAXbkrofmj++WWk9KybZwU+wPQaCByTK4MA8I3bX t8cE0Pd1XVvC7sGAjFc2I1xC3FXEsuw= Received: from mx-prod-mc-02.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-371-m7ik8OiRPPKcxwYR6rIfcA-1; Wed, 20 Nov 2024 17:35:01 -0500 X-MC-Unique: m7ik8OiRPPKcxwYR6rIfcA-1 X-Mimecast-MFC-AGG-ID: m7ik8OiRPPKcxwYR6rIfcA 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-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 149BE1955EE7; Wed, 20 Nov 2024 22:34:59 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.12]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 3AA1F195E481; Wed, 20 Nov 2024 22:34:55 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , Boqun Feng , Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Tamir Duberstein , Filipe Xavier , Martin Rodriguez Reboredo , Valentin Obst , Danilo Krummrich , Wedson Almeida Filho Subject: [PATCH 1/3] rust: sync: Add Lock::is_locked() Date: Wed, 20 Nov 2024 17:30:41 -0500 Message-ID: <20241120223442.2491136-2-lyude@redhat.com> In-Reply-To: <20241120223442.2491136-1-lyude@redhat.com> References: <20241120223442.2491136-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 added a Lock::from_raw() function and exposed Guard::new(), it would be good to actually add the ability to assert the current state of a lock to ensure correctness for unsafe code using these functions. To do so, let's add Lock::is_locked() which simply returns whether or not a Lock is acquired. We'll use this in the next few commits to add some debug assertions. Signed-off-by: Lyude Paul --- rust/helpers/spinlock.c | 5 +++++ rust/kernel/sync/lock.rs | 18 ++++++++++++++++++ rust/kernel/sync/lock/mutex.rs | 5 +++++ rust/kernel/sync/lock/spinlock.rs | 5 +++++ 4 files changed, 33 insertions(+) diff --git a/rust/helpers/spinlock.c b/rust/helpers/spinlock.c index b7b0945e8b3cb..90216a69e3ea1 100644 --- a/rust/helpers/spinlock.c +++ b/rust/helpers/spinlock.c @@ -26,3 +26,8 @@ int rust_helper_spin_trylock(spinlock_t *lock) { return spin_trylock(lock); } + +bool rust_helper_spin_is_locked(spinlock_t *lock) +{ + return spin_is_locked(lock); +} diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index 139f17f2ec86b..542f846ac02b2 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -85,6 +85,13 @@ unsafe fn relock(ptr: *mut Self::State, guard_state: &mu= t Self::GuardState) { // SAFETY: The safety requirements ensure that the lock is initial= ised. *guard_state =3D unsafe { Self::lock(ptr) }; } + + /// Returns whether or not the lock is currently acquired. + /// + /// # Safety + /// + /// Callers must ensure that `ptr` is a valid initialised pointer to t= his lock type. + unsafe fn is_locked(ptr: *mut Self::State) -> bool; } =20 /// A mutual exclusion primitive. @@ -170,6 +177,17 @@ pub fn try_lock(&self) -> Option> { // that `init` was called. unsafe { B::try_lock(self.state.get()).map(|state| Guard::new(self= , state)) } } + + /// Return whether or not the lock is currently acquired. + /// + /// Keep in mind that this function is inherently racy: a lock could i= mmediately be acquired or + /// released after this function returns. As such, the return value fr= om this function should be + /// treated as a snapshot for debugging purposes. + pub fn is_locked(&self) -> bool { + // SAFETY: The constructor of the type calls `init`, so the existe= nce of the object proves + // that `init` was called. + unsafe { B::is_locked(self.state.get()) } + } } =20 /// A lock guard. diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs index 0e946ebefce12..f21b1f14cbe1b 100644 --- a/rust/kernel/sync/lock/mutex.rs +++ b/rust/kernel/sync/lock/mutex.rs @@ -126,4 +126,9 @@ unsafe fn try_lock(ptr: *mut Self::State) -> Option { None } } + + unsafe fn is_locked(ptr: *mut Self::State) -> bool { + // SAFETY: The `ptr` pointer is guaranteed to be valid and initial= ized before use. + unsafe { bindings::mutex_is_locked(ptr) } + } } diff --git a/rust/kernel/sync/lock/spinlock.rs b/rust/kernel/sync/lock/spin= lock.rs index 9f4d128bed983..cfccf5e900b80 100644 --- a/rust/kernel/sync/lock/spinlock.rs +++ b/rust/kernel/sync/lock/spinlock.rs @@ -125,4 +125,9 @@ unsafe fn try_lock(ptr: *mut Self::State) -> Option { None } } + + unsafe fn is_locked(ptr: *mut Self::State) -> bool { + // SAFETY: The `ptr` pointer is guaranteed to be valid and initial= ized before use. + unsafe { bindings::spin_is_locked(ptr) } + } } --=20 2.47.0 From nobody Thu Nov 21 11:15:18 2024 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 3D08B1C1F0B for ; Wed, 20 Nov 2024 22:35:11 +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=1732142112; cv=none; b=J7yLAIejdFdTytw6Eo01U0hvikQngxDWE3Kw7/pmFxZeciQdFuG+DTgdDTKEOmrxz5wiUD1JAzoamgDeIP74HUBzFmCRi6DSD/FX24qzQf28Z6MzWrKcphmFw9NQfMoMQOJYBVTZ0uY4D5XTJkoY/m5u1kyFXx/W21LNNBvRRFo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732142112; c=relaxed/simple; bh=AGuP6mqqY+U5SSoIM8xoU5UdQdxc7DGikyZUA1QDfq8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YkNPExw6si88sLN8eV/1h2K0gWtUrx2wCt/WUKWt1cuGhsJXT9+y7EoCeFuRQJ7NjFyFlyW3OX4ioHenOThonyvUNipFIsdIbtq8UF0UbLDjbEk07iJGjFQcLRgdb+wBo/dSZsOCRkkVN/wfvQB6L82WHrd+0DxLjU4dG6UDd0A= 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=GbsjBJlb; 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="GbsjBJlb" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1732142110; 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=TNvpkGFx/SLAIqcHoBd5o9UWLGuGk4c6Qh+E6yKcoe4=; b=GbsjBJlbycIqzuohKdTWby96+AR3VMD+QgBIPHjjUnHI1Ga9ynTTAlRrzaGzBHAPRFNPEK 0GMNEeVymECfmMQi6KYzwF6/CpSJlcujNdrhEVk6b7k1jfCsPG9qfK6qxrASA8dUQ2mMEf 0Oy98vBI2061LJNAVBpz406myoTJMLA= 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-301-6gnIxnq5Od6mkmVhuPlRZg-1; Wed, 20 Nov 2024 17:35:05 -0500 X-MC-Unique: 6gnIxnq5Od6mkmVhuPlRZg-1 X-Mimecast-MFC-AGG-ID: 6gnIxnq5Od6mkmVhuPlRZg 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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id E803A1956088; Wed, 20 Nov 2024 22:35:02 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.12]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C3200195E480; Wed, 20 Nov 2024 22:34:59 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , 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 , Filipe Xavier Subject: [PATCH 2/3] rust: sync: Assert Lock::is_locked in Guard::new for debug builds Date: Wed, 20 Nov 2024 17:30:42 -0500 Message-ID: <20241120223442.2491136-3-lyude@redhat.com> In-Reply-To: <20241120223442.2491136-1-lyude@redhat.com> References: <20241120223442.2491136-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" Since we're allowing code to unsafely claim that it's acquired a lock let's use the new Lock::is_locked() function so that when debug assertions are enabled, we can verify that the lock has actually been acquired. Signed-off-by: Lyude Paul --- rust/kernel/sync/lock.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index 542f846ac02b2..0a7f2ed767423 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -244,10 +244,17 @@ fn drop(&mut self) { impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> { /// Constructs a new immutable lock guard. /// + /// # Panics + /// + /// This function will panic if debug assertions are enabled and `lock= ` is not actually + /// acquired. + /// /// # Safety /// /// The caller must ensure that it owns the lock. pub unsafe fn new(lock: &'a Lock, state: B::GuardState) -> Self { + debug_assert!(lock.is_locked()); + Self { lock, state, --=20 2.47.0 From nobody Thu Nov 21 11:15:18 2024 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 4B9E61BD4EB for ; Wed, 20 Nov 2024 22:35:19 +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=1732142121; cv=none; b=AtK0KSqTj4dABhmqcvo02DQ0WIxN8WY1Jm+WQkGZ4464Tekno7bDQTKgxgPLgP68LnGz9fI7KmHU2JKG17la0cz06PO1YBbD2esrN1f3VaynRbaa+6Sz93Jk0Q/bgYaWM4lwshyUfpq5F0zx1xUWC2wU/vrPmEYK6Wx6HHNSgWg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732142121; c=relaxed/simple; bh=2oOh1nTY41cvATmIAlO+1LRQLtQSWc2a2JycbyTdgZo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OOxX9zJQPlzykvDVyapnUF1qkh7ICDnxhsMWNc6Ly8eYDygeDx9jDGQw6v0Ev27NjVCovexL5ch/U0S7E0RMUAfZZ7ti0p1BGaOODqqDg+i09uRl8D8hShOCiKBYlbEjpNMKovbR7enonIGkgBIhOOVdRf+r8s17CZxgHW3Lydo= 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=bmVzVyYP; 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="bmVzVyYP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1732142118; 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=xy6wj28XSU6G9iFcG9lOwNtV5S2Gmt1xXPacba/a7Ek=; b=bmVzVyYPk0u0i7/tF9qnwpgHUATUGtNQuCHO/YzyebQs5HfZVButWxVd0XSJOzc6nVU70B jEtureGLL9EZ5+FWmEaqSz2ecGtntTBxNGvOSJ1+o1cId13+9vaSlLQktM3AGIttlupv/v VQWOS3MbCtpuNSLvck9UHDsvQrDPID4= Received: from mx-prod-mc-04.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-269-f5nkY8HrPt2H34TZ8dItYA-1; Wed, 20 Nov 2024 17:35:14 -0500 X-MC-Unique: f5nkY8HrPt2H34TZ8dItYA-1 X-Mimecast-MFC-AGG-ID: f5nkY8HrPt2H34TZ8dItYA 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-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 283E419560B0; Wed, 20 Nov 2024 22:35:12 +0000 (UTC) Received: from chopper.redhat.com (unknown [10.22.88.12]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id B86C719560A3; Wed, 20 Nov 2024 22:35:03 +0000 (UTC) From: Lyude Paul To: rust-for-linux@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , 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 , Filipe Xavier Subject: [PATCH 3/3] rust: sync: Add Guard::new_unchecked() Date: Wed, 20 Nov 2024 17:30:43 -0500 Message-ID: <20241120223442.2491136-4-lyude@redhat.com> In-Reply-To: <20241120223442.2491136-1-lyude@redhat.com> References: <20241120223442.2491136-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" Asserting is_locked() for unsafe Guard::new() calls is quite useful for verifying safety for callers outside the lock module. But in the lock module, it's a bit unnecessary and a potential performance hit for safe rust code. Mainly because it implies all safe lock acquisitions in rust will have to run this debug assertion. So, let's split out Guard::new() by adding a Guard::new_unchecked() function that skips this debug assertion. Of course, we leave this function as private and note that it is only ever intended for use in this specific module. Signed-off-by: Lyude Paul --- rust/kernel/sync/lock.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/rust/kernel/sync/lock.rs b/rust/kernel/sync/lock.rs index 0a7f2ed767423..2fd4b665ffc9a 100644 --- a/rust/kernel/sync/lock.rs +++ b/rust/kernel/sync/lock.rs @@ -166,7 +166,7 @@ pub fn lock(&self) -> Guard<'_, T, B> { // that `init` was called. let state =3D unsafe { B::lock(self.state.get()) }; // SAFETY: The lock was just acquired. - unsafe { Guard::new(self, state) } + unsafe { Guard::new_unchecked(self, state) } } =20 /// Tries to acquire the lock. @@ -175,7 +175,7 @@ pub fn lock(&self) -> Guard<'_, T, B> { pub fn try_lock(&self) -> Option> { // SAFETY: The constructor of the type calls `init`, so the existe= nce of the object proves // that `init` was called. - unsafe { B::try_lock(self.state.get()).map(|state| Guard::new(self= , state)) } + unsafe { B::try_lock(self.state.get()).map(|state| Guard::new_unch= ecked(self, state)) } } =20 /// Return whether or not the lock is currently acquired. @@ -255,6 +255,19 @@ impl<'a, T: ?Sized, B: Backend> Guard<'a, T, B> { pub unsafe fn new(lock: &'a Lock, state: B::GuardState) -> Self { debug_assert!(lock.is_locked()); =20 + // SAFETY: Our safety requirements fulfill the requirements of thi= s function. + unsafe { Self::new_unchecked(lock, state) } + } + + /// Constructs a new immutable lock guard without assertions. + /// + /// Unlike [`Guard::new`], this function does not run a debug assertio= n to ensure the lock has + /// been acquired. It should only be used in this module. + /// + /// # Safety + /// + /// The caller must ensure that it owns the lock. + unsafe fn new_unchecked(lock: &'a Lock, state: B::GuardState) ->= Self { Self { lock, state, --=20 2.47.0