Skip to content

Commit ad60902

Browse files
Gnurougregkh
authored andcommitted
rust: io: always inline functions using build_assert with arguments
commit 33d19f6 upstream. `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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8776dfa commit ad60902

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
@@ -140,7 +140,8 @@ macro_rules! define_read {
140140
/// Bound checks are performed on compile time, hence if the offset is not known at compile
141141
/// time, the build will fail.
142142
$(#[$attr])*
143-
#[inline]
143+
// Always inline to optimize out error path of `io_addr_assert`.
144+
#[inline(always)]
144145
pub fn $name(&self, offset: usize) -> $type_name {
145146
let addr = self.io_addr_assert::<$type_name>(offset);
146147

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

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

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

rust/kernel/io/resource.rs

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

225+
// Always inline to optimize out error path of `build_assert`.
226+
#[inline(always)]
225227
const fn new(value: u32) -> Self {
226228
crate::build_assert!(value as u64 <= c_ulong::MAX as u64);
227229
Flags(value as c_ulong)

0 commit comments

Comments
 (0)