From nobody Fri Dec 19 16:04:50 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 4D9AB156677; Mon, 28 Apr 2025 14:01:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745848911; cv=none; b=a7VbXOOEeYkiYI10droMmNxm0OKL0xhfKPyWbjvQ6fZx9IsqEijvRXQamgMNvnDJV2ybhEgxQtk1nPRpPjf756zsezuObzX5pNb6VBQf75vokMPu1She5aZGWLOdtgDkGemn6T+PMEhePklLHUUyqYcIqSZvi+68HOmrPHmQzn0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745848911; c=relaxed/simple; bh=CUjbBsKsU/IXjZc/+tfdQ4SEXR2i4Xv3hgob/3UPMOY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=g5rM9Oxlrry5oHEH/W2OjXux5KVkpNPdsQ/JRpr+KydlUDUuUGwdNNSFoI/r5Z6jPINn7j4jyJntSl5lRbg7yNbClh4EQlcHxq1yn5s2PON7h2VDwBLLgW79/jkGY7YyGerfRlLHwGfGwgaR09DjMIvj6PlvNXcmhkGWttYVdwo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mNvwuZpN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="mNvwuZpN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B1B97C4CEEA; Mon, 28 Apr 2025 14:01:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745848910; bh=CUjbBsKsU/IXjZc/+tfdQ4SEXR2i4Xv3hgob/3UPMOY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mNvwuZpNdykQnyVoSxc1MCSSI7Y7ycTKp33Zzt5HnYlYQ6RQc+t94MNAY7ItWkwm/ 2pL9oaAofETHZzPCQe9wr1IAc30+waQW6caJB1Bt8Ee4gCLNAasyxsfxJ5D+j4a5I9 6RTo4Io0NvyBGSMyVkyIyp6JAIsGW4aiiWGVSkjcgZZM5LIWW6TGZRmxJ2cHiy58O8 UPa1t/+tqUaeNkK+YaOh8Ppuuu7q6DT82mlxKfCq/FK7Z5NQ//6wWrt5GculsCPtyD 2ftcFl+uSCMwxzZHXwBfFRYMCxwbOHHRjWwvIOieqczikakCouhhM6twMlreDrGhiU RKc1NjtPMCA1A== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, bhelgaas@google.com, kwilczynski@kernel.org, zhiw@nvidia.com, cjia@nvidia.com, jhubbard@nvidia.com, bskeggs@nvidia.com, acurrid@nvidia.com, joelagnelf@nvidia.com, ttabi@nvidia.com, acourbot@nvidia.com, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich , Christian Schrefl Subject: [PATCH v2 1/3] rust: revocable: implement Revocable::access() Date: Mon, 28 Apr 2025 16:00:27 +0200 Message-ID: <20250428140137.468709-2-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250428140137.468709-1-dakr@kernel.org> References: <20250428140137.468709-1-dakr@kernel.org> 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" Implement an unsafe direct accessor for the data stored within the Revocable. This is useful for cases where we can prove that the data stored within the Revocable is not and cannot be revoked for the duration of the lifetime of the returned reference. Reviewed-by: Christian Schrefl Reviewed-by: Benno Lossin Signed-off-by: Danilo Krummrich Acked-by: Boqun Feng Acked-by: Miguel Ojeda Reviewed-by: Alexandre Courbot Reviewed-by: Joel Fernandes --- rust/kernel/revocable.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rust/kernel/revocable.rs b/rust/kernel/revocable.rs index 971d0dc38d83..db4aa46bb121 100644 --- a/rust/kernel/revocable.rs +++ b/rust/kernel/revocable.rs @@ -139,6 +139,18 @@ pub fn try_access_with R>(&self, f= : F) -> Option { self.try_access().map(|t| f(&*t)) } =20 + /// Directly access the revocable wrapped object. + /// + /// # Safety + /// + /// The caller must ensure this [`Revocable`] instance hasn't been rev= oked and won't be revoked + /// as long as the returned `&T` lives. + pub unsafe fn access(&self) -> &T { + // SAFETY: By the safety requirement of this function it is guaran= teed that + // `self.data.get()` is a valid pointer to an instance of `T`. + unsafe { &*self.data.get() } + } + /// # Safety /// /// Callers must ensure that there are no more concurrent users of the= revocable object. --=20 2.49.0 From nobody Fri Dec 19 16:04:50 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 AC2B826B2D6; Mon, 28 Apr 2025 14:01:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745848917; cv=none; b=Svaj77cY0GO+eerR+8T9pbo59mKaxCmU9vRWCyQvkj422cxNY02U9ZqT+c/vvG5hjedXTsx9Z6NfvS52Se16ZRbUo28guLdpWKv/LtDGxIzXWNEsJX4tGnCZ2VPhfzjIhPsXf9c3Jk4xET5VcBbTDfpbXAS3bI/Rlu/J8LLKE7Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745848917; c=relaxed/simple; bh=5M8AWy35CAncMHrQE1PZU5bOW2RDloVLDLoTAFvZ3MA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aO6HIAtZxR08d+u6626yNKEoFWY6RMiZHI2eVhpbi2x5yGah//LytTqk2NYV7QLoOCGHi9cy7mBHGZ9TeuKYIkz6dfA0gbAyfT874D1YdKRV07H+dNzCsP/zEU12xOs+8SN94GS6CJyG1g7mB2CwRAsI0B9M1CGmWN2+fK7suXk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=id8IA0JP; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="id8IA0JP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29D5DC4CEE4; Mon, 28 Apr 2025 14:01:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745848916; bh=5M8AWy35CAncMHrQE1PZU5bOW2RDloVLDLoTAFvZ3MA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=id8IA0JP+++5cZz4RIVLAbb1dShoh3Nm0GaSCXncUuOVlz81ErC1HXtWd+Suk1g9m HUE3DDZ1Vw8xb/XR3aNaFc9YISQ14Jg7fliGaSF2AGIkM1fQYzi21XkNOxH7e2/p1O z/0HHzMrsepWG7+j3Vwt0+zcN92e7CLHkCzPeEHmRmZPA4AibqWiD5gEe1i3SISbc1 7hPcfWcbq7NuLDiflZLAe5JE0lKZ05i0f+rNN67oHR7p8CsS8EuRIzO+NrmWkB7QB8 HJRwbQej3AV021hXQfjzU0Ih41DqT4Ms6A5oYHOj+Fo4k2Dcrtr45sYs68h1gognBc gMuSy/53r/HgQ== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, bhelgaas@google.com, kwilczynski@kernel.org, zhiw@nvidia.com, cjia@nvidia.com, jhubbard@nvidia.com, bskeggs@nvidia.com, acurrid@nvidia.com, joelagnelf@nvidia.com, ttabi@nvidia.com, acourbot@nvidia.com, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich , Christian Schrefl Subject: [PATCH v2 2/3] rust: devres: implement Devres::access_with() Date: Mon, 28 Apr 2025 16:00:28 +0200 Message-ID: <20250428140137.468709-3-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250428140137.468709-1-dakr@kernel.org> References: <20250428140137.468709-1-dakr@kernel.org> 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" Implement a direct accessor for the data stored within the Devres for cases where we can prove that we own a reference to a Device (i.e. a bound device) of the same device that was used to create the corresponding Devres container. Usually, when accessing the data stored within a Devres container, it is not clear whether the data has been revoked already due to the device being unbound and, hence, we have to try whether the access is possible and subsequently keep holding the RCU read lock for the duration of the access. However, when we can prove that we hold a reference to Device matching the device the Devres container has been created with, we can guarantee that the device is not unbound for the duration of the lifetime of the Device reference and, hence, it is not possible for the data within the Devres container to be revoked. Therefore, in this case, we can bypass the atomic check and the RCU read lock, which is a great optimization and simplification for drivers. Reviewed-by: Christian Schrefl Signed-off-by: Danilo Krummrich Acked-by: Boqun Feng Reviewed-by: Alexandre Courbot Reviewed-by: Joel Fernandes --- rust/kernel/devres.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/rust/kernel/devres.rs b/rust/kernel/devres.rs index 1e58f5d22044..cf6fc09b2c05 100644 --- a/rust/kernel/devres.rs +++ b/rust/kernel/devres.rs @@ -181,6 +181,44 @@ pub fn new_foreign_owned(dev: &Device, data: T,= flags: Flags) -> Result { =20 Ok(()) } + + /// Obtain `&'a T`, bypassing the [`Revocable`]. + /// + /// This method allows to directly obtain a `&'a T`, bypassing the [`R= evocable`], by presenting + /// a `&'a Device` of the same [`Device`] this [`Devres`] insta= nce has been created with. + /// + /// # Errors + /// + /// An error is returned if `dev` does not match the same [`Device`] t= his [`Devres`] instance + /// has been created with. + /// + /// # Example + /// + /// ```no_run + /// # use kernel::{device::Core, devres::Devres, pci}; + /// + /// fn from_core(dev: &pci::Device, devres: Devres= >) -> Result<()> { + /// let bar =3D devres.access(dev.as_ref())?; + /// + /// let _ =3D bar.read32(0x0); + /// + /// // might_sleep() + /// + /// bar.write32(0x42, 0x0); + /// + /// Ok(()) + /// } + /// ``` + pub fn access<'a>(&'a self, dev: &'a Device) -> Result<&'a T> { + if self.0.dev.as_raw() !=3D dev.as_raw() { + return Err(EINVAL); + } + + // SAFETY: `dev` being the same device as the device this `Devres`= has been created for + // proves that `self.0.data` hasn't been revoked and is guaranteed= to not be revoked as + // long as `dev` lives; `dev` lives at least as long as `self`. + Ok(unsafe { self.deref().access() }) + } } =20 impl Deref for Devres { --=20 2.49.0 From nobody Fri Dec 19 16:04:50 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 E043F26B2D6; Mon, 28 Apr 2025 14:02:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745848922; cv=none; b=Zy0WDxYahfXUwNULOtnbp/DoAPhDYafMJnzWa/TrUnkaMBq3PQF/KkKfrwZ17PPFBOemH22Gycn7y55+X/f0LFu0/e1DGQAYTEkgRQWnfQDf5xkqf9XxNmBwaCGRvLxHJ/vz6FMpw9NbpWM1Qrg5+nqdrUK32NCxMjlGKJhuiW4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745848922; c=relaxed/simple; bh=RMj0v7yzxGxIBf2g3qGoGs/WIjraPNY4gUN8Pz9vsVA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DFFcnTX7lHU17YZ9vkcnLKLm6n6thv58E+cNtTu93RXx+5GbOgsCnv2lEdEqi2fCY/4TB/SlrirhDvicEyWGvqBScQ79TEm0/S10KqLJs8QhaQXgTbWYNedGr0z4Z1kULAe/qwU/rKmPkNrhxa6qDzQcXb7W4JCfv9fuTu5LNlY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kKb1YK+z; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kKb1YK+z" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 953B4C4CEEA; Mon, 28 Apr 2025 14:01:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745848921; bh=RMj0v7yzxGxIBf2g3qGoGs/WIjraPNY4gUN8Pz9vsVA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kKb1YK+zGe3UVTuzXgx/Y1C8eOIi8UVajcPkDn2bY36hCZliMXhZngY1WL8J68CdG dLB7n48KVFoWNBEUx6S65ssy+VPu5tYMp4YW6sZgh8oc/PHRtX7pIzOMhbxv3Xs0ov B/w3W0+TRRPGZP1rBSXXWfWTR8PP6QxD3yAyklJy9r1QmxTJMI5hGJTJOUDsuu/JAg Ulml5aQFOeH2rh5iRxiygBBstaVm0WGMjHr6V0UInTUu6rLN/2gKxFiv3qX8w5qyyw s7CiE4+F5OVPp+wstQEDdb0GNj622hy7sSotyz//W7xwWxD5DTECDJLe7np7SQnUCu tP/7RThTYMyhw== From: Danilo Krummrich To: gregkh@linuxfoundation.org, rafael@kernel.org, bhelgaas@google.com, kwilczynski@kernel.org, zhiw@nvidia.com, cjia@nvidia.com, jhubbard@nvidia.com, bskeggs@nvidia.com, acurrid@nvidia.com, joelagnelf@nvidia.com, ttabi@nvidia.com, acourbot@nvidia.com, ojeda@kernel.org, alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net, bjorn3_gh@protonmail.com, benno.lossin@proton.me, a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu Cc: linux-pci@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Danilo Krummrich Subject: [PATCH v2 3/3] samples: rust: pci: take advantage of Devres::access_with() Date: Mon, 28 Apr 2025 16:00:29 +0200 Message-ID: <20250428140137.468709-4-dakr@kernel.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250428140137.468709-1-dakr@kernel.org> References: <20250428140137.468709-1-dakr@kernel.org> 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" For the I/O operations executed from the probe() method, take advantage of Devres::access_with(), avoiding the atomic check and RCU read lock required otherwise entirely. Signed-off-by: Danilo Krummrich Acked-by: Boqun Feng Reviewed-by: Alexandre Courbot Reviewed-by: Joel Fernandes --- samples/rust/rust_driver_pci.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/samples/rust/rust_driver_pci.rs b/samples/rust/rust_driver_pci= .rs index 9ce3a7323a16..15147e4401b2 100644 --- a/samples/rust/rust_driver_pci.rs +++ b/samples/rust/rust_driver_pci.rs @@ -83,12 +83,12 @@ fn probe(pdev: &pci::Device, info: &Self::IdInfo)= -> Result GFP_KERNEL, )?; =20 - let res =3D drvdata - .bar - .try_access_with(|b| Self::testdev(info, b)) - .ok_or(ENXIO)??; - - dev_info!(pdev.as_ref(), "pci-testdev data-match count: {}\n", res= ); + let bar =3D drvdata.bar.access(pdev.as_ref())?; + dev_info!( + pdev.as_ref(), + "pci-testdev data-match count: {}\n", + Self::testdev(info, bar)? + ); =20 Ok(drvdata.into()) } --=20 2.49.0