Skip to content

Commit 33d19f6

Browse files
GnurouDanilo Krummrich
authored andcommitted
rust: io: always inline functions using build_assert with arguments
`build_assert` relies on the compiler to optimize out its error path. Functions using it with its arguments must thus always be inlined, otherwise the error path of `build_assert` might not be optimized out, triggering a build error. Cc: stable@vger.kernel.org Fixes: ce30d94 ("rust: add `io::{Io, IoRaw}` base types") Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com> Signed-off-by: Alexandre Courbot <acourbot@nvidia.com> Tested-by: Timur Tabi <ttabi@nvidia.com> Link: https://patch.msgid.link/20251208-io-build-assert-v3-2-98aded02c1ea@nvidia.com Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 0f61b18 commit 33d19f6

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

rust/kernel/io.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ macro_rules! define_read {
142142
/// Bound checks are performed on compile time, hence if the offset is not known at compile
143143
/// time, the build will fail.
144144
$(#[$attr])*
145-
#[inline]
145+
// Always inline to optimize out error path of `io_addr_assert`.
146+
#[inline(always)]
146147
pub fn $name(&self, offset: usize) -> $type_name {
147148
let addr = self.io_addr_assert::<$type_name>(offset);
148149

@@ -171,7 +172,8 @@ macro_rules! define_write {
171172
/// Bound checks are performed on compile time, hence if the offset is not known at compile
172173
/// time, the build will fail.
173174
$(#[$attr])*
174-
#[inline]
175+
// Always inline to optimize out error path of `io_addr_assert`.
176+
#[inline(always)]
175177
pub fn $name(&self, value: $type_name, offset: usize) {
176178
let addr = self.io_addr_assert::<$type_name>(offset);
177179

@@ -239,7 +241,8 @@ impl<const SIZE: usize> Io<SIZE> {
239241
self.addr().checked_add(offset).ok_or(EINVAL)
240242
}
241243

242-
#[inline]
244+
// Always inline to optimize out error path of `build_assert`.
245+
#[inline(always)]
243246
fn io_addr_assert<U>(&self, offset: usize) -> usize {
244247
build_assert!(Self::offset_valid::<U>(offset, SIZE));
245248

rust/kernel/io/resource.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ impl Flags {
226226
/// Resource represents a memory region that must be ioremaped using `ioremap_np`.
227227
pub const IORESOURCE_MEM_NONPOSTED: Flags = Flags::new(bindings::IORESOURCE_MEM_NONPOSTED);
228228

229+
// Always inline to optimize out error path of `build_assert`.
230+
#[inline(always)]
229231
const fn new(value: u32) -> Self {
230232
crate::build_assert!(value as u64 <= c_ulong::MAX as u64);
231233
Flags(value as c_ulong)

0 commit comments

Comments
 (0)