From nobody Tue Nov 26 12:25:29 2024 Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) (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 B2907187FEB; Fri, 18 Oct 2024 05:52:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230728; cv=none; b=E4+zRPZJf8awBbBHa2PfxX4lcao9Nrv6PUy81J8HYiYZHlyyGxGLv4i+0gSzd/K/hxzkHBnPzBSpBHCLcdfjvzw1RiiV+vLw/47woPohDmZ3NMzsY8ABGS8o2TWzTQcM3oiZYdvIeKKdrBYwF2ybmjdUIgFD+uWCaQWQ2QTkWpU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729230728; c=relaxed/simple; bh=tbSrVABp3PaXJkMn+Cm+M441qbWN9NPoBe7KufBm1QA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oZ+JNfkbd40gVqh8/ywohU9xLUy57xEhPuFkFZUtVge8LDUz+9qqu2ktIiWWz3rpQYsUC7scDTVuPIB1NCiMUXNMpp35EQJOEHUtAkW7BwCZVtC/IujEgjtyZt3taQjwY2Yy1c44EVSIJKzaCUFYUQcEPn2/UORSzeWNnCgkIEE= 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=Ei5mxr0Y; arc=none smtp.client-ip=209.85.219.42 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="Ei5mxr0Y" Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-6cbd092f7f0so11489386d6.0; Thu, 17 Oct 2024 22:52:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729230726; x=1729835526; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:from:to:cc:subject :date:message-id:reply-to; bh=PiO8wSvLJP3CfC2CFEiFL3JHXiCvMRVN6nfnfklyn+E=; b=Ei5mxr0YqKaU2EWnfeJhBuY9eExmE4+a5alfG8X1OiSZTzzt9dIzgCPLPH6ZxQqy2N x+LHAhR3ABXKY4DJ3pOyPzOtjqTgboiV69U+4M7zhJSYRBzGAYn0EJHQITiAlHDnHx8y 806YbVwbCcbd9p8cfbmISj0fof346aj7kOqiMppbn5MvOosh32a4iClMoZfSk0uXneMC H3GfuPYUpvpK9yviPZQw1QWtRXMjjJ3gnm8p4DkNw540BZOE5SGD1YbL+GTZu96S0G4T MD54TKC1lpdeH6syq3IOoCFFoC8Ai0ICDzoXJIzbPmTca8Bi9dt4nHLHNzvsKD27tPg6 JdFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729230726; x=1729835526; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:feedback-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=PiO8wSvLJP3CfC2CFEiFL3JHXiCvMRVN6nfnfklyn+E=; b=xDLDlR15Lf9eVbW0w4tyFFA/bsFJ0YxlwYb5sGIOhgTvNtfeC9zKf/IK3T21t2lJE2 TuKeMk9oNaiX+ZIaaE9mPgL/Cn753yU14GqnSBI4WWMXnoy2ksg5m1sCa5Fs8LaRfbgr /klolBquDVnMXKwlFHRMK/dw2YxQ7ULxtjJeuQbgRXfrLFHTx2j1YR+vqNL48AFWzzCN GTmybu2ILNYXlmGDy7Mw0fup9yYK6ak0ZgIoUSSNDi/dlWQz9YMVbA+3tNiNwrP8pggu rAT0vKY6t2fcyMwpkiLHf1rUQtm5lMVaoEdewGLA2COrH7aXcjgK2Ge2XUzVPXSnlaXK ohQA== X-Forwarded-Encrypted: i=1; AJvYcCU1wXAfnWoGebijpEI5g6u++hzJpenRxAiwR+TEBduwE/5CPZJfm14hmAaQJ1wMtq6MPHo2+LkIE6Rh84m7kBw=@vger.kernel.org, AJvYcCVS6kGylnon3ngHgDf5fXdZ2T0jgiq6cQgm07IJyVdPBgKyMDmpTJKzbxNUJOZXmmdZVU/sEgp8Fk3ggB8=@vger.kernel.org X-Gm-Message-State: AOJu0Yw9/ojAUd2bOIcQZLhPDuNHpKP8MWFKgR0nDL1XxoSA9zNlN8+/ mwYay9hKLoDycO5ENX2LEL/WaMILkitoLRB16Z6y3AVAYakNWuTy X-Google-Smtp-Source: AGHT+IF4dS/pPKXEPCvmpweymPZSUAD0UOsQ1pCKGWPLMt2tDJw3rXN9Sx/soWflN4T5oAgrXMUObg== X-Received: by 2002:a05:6214:3b83:b0:6cc:42d:bbb with SMTP id 6a1803df08f44-6cde1695f11mr12452786d6.53.1729230725599; Thu, 17 Oct 2024 22:52:05 -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 6a1803df08f44-6cde114d0d7sm3899996d6.40.2024.10.17.22.52.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2024 22:52:05 -0700 (PDT) Received: from phl-compute-10.internal (phl-compute-10.phl.internal [10.202.2.50]) by mailfauth.phl.internal (Postfix) with ESMTP id 15B7D1200071; Fri, 18 Oct 2024 01:52:04 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-10.internal (MEProxy); Fri, 18 Oct 2024 01:52:04 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdehvddguddttdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefhvfevufffkffojghfggfgsedtkeertdertddt necuhfhrohhmpeeuohhquhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilh drtghomheqnecuggftrfgrthhtvghrnhepgeeljeeitdehvdehgefgjeevfeejjeekgfev ffeiueejhfeuiefggeeuheeggefgnecuvehluhhsthgvrhfuihiivgepudenucfrrghrrg hmpehmrghilhhfrhhomhepsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghl ihhthidqieelvdeghedtieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepgh hmrghilhdrtghomhesfhhigihmvgdrnhgrmhgvpdhnsggprhgtphhtthhopedvuddpmhho uggvpehsmhhtphhouhhtpdhrtghpthhtohepthhglhigsehlihhnuhhtrhhonhhigidrug gvpdhrtghpthhtohepughirhhkrdgsvghhmhgvsehgmhgrihhlrdgtohhmpdhrtghpthht oheplhihuhguvgesrhgvughhrghtrdgtohhmpdhrtghpthhtoheprhhushhtqdhfohhrqd hlihhnuhigsehvghgvrhdrkhgvrhhnvghlrdhorhhgpdhrtghpthhtohepuggrkhhrsehr vgguhhgrthdrtghomhdprhgtphhtthhopegrihhrlhhivggusehrvgguhhgrthdrtghomh dprhgtphhtthhopehmihhnghhosehrvgguhhgrthdrtghomhdprhgtphhtthhopeifihhl lheskhgvrhhnvghlrdhorhhgpdhrtghpthhtoheplhhonhhgmhgrnhesrhgvughhrghtrd gtohhm X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 18 Oct 2024 01:52:03 -0400 (EDT) From: Boqun Feng To: "Thomas Gleixner" Cc: Dirk Behme , Lyude Paul , rust-for-linux@vger.kernel.org, Danilo Krummrich , airlied@redhat.com, Ingo Molnar , will@kernel.org, Waiman Long , Peter Zijlstra , linux-kernel@vger.kernel.org, Miguel Ojeda , Alex Gaynor , wedsonaf@gmail.com, Gary Guo , =?UTF-8?q?Bj=C3=B6rn=20Roy=20Baron?= , Benno Lossin , Andreas Hindborg , aliceryhl@google.com, Trevor Gross , Boqun Feng Subject: [POC 5/6] rust: sync: Introduce lock::Backend::Context Date: Thu, 17 Oct 2024 22:51:24 -0700 Message-ID: <20241018055125.2784186-6-boqun.feng@gmail.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20241018055125.2784186-1-boqun.feng@gmail.com> References: <1eaf7f61-4458-4d15-bbe6-7fd2e34723f4@app.fastmail.com> <20241018055125.2784186-1-boqun.feng@gmail.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 Content-Type: text/plain; charset="utf-8" From: Lyude Paul 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.] Co-developed-by: Lyude Paul Signed-off-by: Lyude Paul 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 f6c34ca4d819..49b53433201c 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 can be provided to acquire the lock with a diffe= rent backend. + type Context<'a>; + /// Initialises the lock. /// /// # Safety @@ -120,8 +123,15 @@ pub fn new(t: T, name: &'static CStr, key: &'static Lo= ckClassKey) -> impl PinIni } =20 impl Lock { + /// 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> { + todo!() + } + /// Acquires the lock and gives the caller access to the data protecte= d 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 existe= nce of the object proves // that `init` was called. let state =3D unsafe { B::lock(self.state.get()) }; diff --git a/rust/kernel/sync/lock/mutex.rs b/rust/kernel/sync/lock/mutex.rs index 30632070ee67..7c2c23994493 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 884d4d1cbf23..8f9e1b27e474 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::InterruptDisabled; =20 /// Creates a [`SpinLock`] initialiser with the given name and a newly-cre= ated lock class. /// @@ -92,6 +93,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, @@ -183,6 +185,7 @@ macro_rules! new_spinlock_irq { unsafe impl super::Backend for SpinLockIrqBackend { type State =3D bindings::spinlock_t; type GuardState =3D (); + type Context<'a> =3D &'a InterruptDisabled; =20 unsafe fn init( ptr: *mut Self::State, --=20 2.45.2