[PATCH 01/11] rust: helpers: io: use macro to generate io accessor functions

Andrew Ballance posted 11 patches 9 months ago
[PATCH 01/11] rust: helpers: io: use macro to generate io accessor functions
Posted by Andrew Ballance 9 months ago
From: Fiona Behrens <me@kloenk.dev>

Generate the `rust_helper_read{b,w,l,q}`, `rust_helper_write{b,w,l,q}`
and the relaxed version using a C macro.

This removes a lot of redundant code and is in preparation for pio
functions which uses a similar C macro.

Signed-off-by: Fiona Behrens <me@kloenk.dev>
Signed-off-by: Andrew Ballance <andrewjballance@gmail.com>
---
 rust/helpers/io.c | 104 +++++++++++++---------------------------------
 1 file changed, 28 insertions(+), 76 deletions(-)

diff --git a/rust/helpers/io.c b/rust/helpers/io.c
index 15ea187c5466..525af02f209e 100644
--- a/rust/helpers/io.c
+++ b/rust/helpers/io.c
@@ -12,90 +12,42 @@ void rust_helper_iounmap(void __iomem *addr)
 	iounmap(addr);
 }
 
-u8 rust_helper_readb(const void __iomem *addr)
-{
-	return readb(addr);
-}
-
-u16 rust_helper_readw(const void __iomem *addr)
-{
-	return readw(addr);
-}
-
-u32 rust_helper_readl(const void __iomem *addr)
-{
-	return readl(addr);
-}
-
+#define define_rust_mmio_read_helper(name, type)    \
+	type rust_helper_##name(void __iomem *addr) \
+	{                                           \
+		return name(addr);                  \
+	}
+
+#define define_rust_mmio_write_helper(name, type)               \
+	void rust_helper_##name(type value, void __iomem *addr) \
+	{                                                       \
+		name(value, addr);                              \
+	}
+
+define_rust_mmio_read_helper(readb, u8);
+define_rust_mmio_read_helper(readw, u16);
+define_rust_mmio_read_helper(readl, u32);
 #ifdef CONFIG_64BIT
-u64 rust_helper_readq(const void __iomem *addr)
-{
-	return readq(addr);
-}
+define_rust_mmio_read_helper(readq, u64);
 #endif
 
-void rust_helper_writeb(u8 value, void __iomem *addr)
-{
-	writeb(value, addr);
-}
-
-void rust_helper_writew(u16 value, void __iomem *addr)
-{
-	writew(value, addr);
-}
-
-void rust_helper_writel(u32 value, void __iomem *addr)
-{
-	writel(value, addr);
-}
-
+define_rust_mmio_write_helper(writeb, u8);
+define_rust_mmio_write_helper(writew, u16);
+define_rust_mmio_write_helper(writel, u32);
 #ifdef CONFIG_64BIT
-void rust_helper_writeq(u64 value, void __iomem *addr)
-{
-	writeq(value, addr);
-}
+define_rust_mmio_write_helper(writeq, u64);
 #endif
 
-u8 rust_helper_readb_relaxed(const void __iomem *addr)
-{
-	return readb_relaxed(addr);
-}
-
-u16 rust_helper_readw_relaxed(const void __iomem *addr)
-{
-	return readw_relaxed(addr);
-}
-
-u32 rust_helper_readl_relaxed(const void __iomem *addr)
-{
-	return readl_relaxed(addr);
-}
-
+define_rust_mmio_read_helper(readb_relaxed, u8);
+define_rust_mmio_read_helper(readw_relaxed, u16);
+define_rust_mmio_read_helper(readl_relaxed, u32);
 #ifdef CONFIG_64BIT
-u64 rust_helper_readq_relaxed(const void __iomem *addr)
-{
-	return readq_relaxed(addr);
-}
+define_rust_mmio_read_helper(readq_relaxed, u64);
 #endif
 
-void rust_helper_writeb_relaxed(u8 value, void __iomem *addr)
-{
-	writeb_relaxed(value, addr);
-}
-
-void rust_helper_writew_relaxed(u16 value, void __iomem *addr)
-{
-	writew_relaxed(value, addr);
-}
-
-void rust_helper_writel_relaxed(u32 value, void __iomem *addr)
-{
-	writel_relaxed(value, addr);
-}
-
+define_rust_mmio_write_helper(writeb_relaxed, u8);
+define_rust_mmio_write_helper(writew_relaxed, u16);
+define_rust_mmio_write_helper(writel_relaxed, u32);
 #ifdef CONFIG_64BIT
-void rust_helper_writeq_relaxed(u64 value, void __iomem *addr)
-{
-	writeq_relaxed(value, addr);
-}
+define_rust_mmio_write_helper(writeq_relaxed, u64);
 #endif
-- 
2.49.0
Re: [PATCH 01/11] rust: helpers: io: use macro to generate io accessor functions
Posted by kernel test robot 9 months ago
Hi Andrew,

kernel test robot noticed the following build errors:

[auto build test ERROR on 92a09c47464d040866cf2b4cd052bc60555185fb]

url:    https://github.com/intel-lab-lkp/linux/commits/Andrew-Ballance/rust-helpers-io-use-macro-to-generate-io-accessor-functions/20250509-111818
base:   92a09c47464d040866cf2b4cd052bc60555185fb
patch link:    https://lore.kernel.org/r/20250509031524.2604087-2-andrewjballance%40gmail.com
patch subject: [PATCH 01/11] rust: helpers: io: use macro to generate io accessor functions
config: x86_64-randconfig-073-20250512 (https://download.01.org/0day-ci/archive/20250513/202505130821.z5LcD77G-lkp@intel.com/config)
compiler: clang version 20.1.2 (https://github.com/llvm/llvm-project 58df0ef89dd64126512e4ee27b4ac3fd8ddf6247)
rustc: rustc 1.78.0 (9b00956e5 2024-04-29)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250513/202505130821.z5LcD77G-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202505130821.z5LcD77G-lkp@intel.com/

All errors (new ones prefixed by >>):

   ***
   *** Rust bindings generator 'bindgen' < 0.69.5 together with libclang >= 19.1
   *** may not work due to a bug (https://github.com/rust-lang/rust-bindgen/pull/2824),
   *** unless patched (like Debian's).
   ***   Your bindgen version:  0.65.1
   ***   Your libclang version: 20.1.2
   ***
   ***
   *** Please see Documentation/rust/quick-start.rst for details
   *** on how to set up the Rust support.
   ***
>> error[E0425]: cannot find function `readb` in crate `bindings`
   --> rust/kernel/io.rs:221:36
   |
   221 |     define_read!(read8, try_read8, readb -> u8);
   |                                    ^^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `readw` in crate `bindings`
   --> rust/kernel/io.rs:222:38
   |
   222 |     define_read!(read16, try_read16, readw -> u16);
   |                                      ^^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `writel` in crate `bindings`
   --> rust/kernel/io.rs:243:41
   |
   243 |     define_write!(write32, try_write32, writel <- u32);
   |                                         ^^^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `writeq` in crate `bindings`
   --> rust/kernel/io.rs:248:9
   |
   248 |         writeq <- u64
   |         ^^^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `writeb_relaxed` in crate `bindings`
   --> rust/kernel/io.rs:251:55
   |
   251 |     define_write!(write8_relaxed, try_write8_relaxed, writeb_relaxed <- u8);
   |                                                       ^^^^^^^^^^^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `writew_relaxed` in crate `bindings`
   --> rust/kernel/io.rs:252:57
   |
   252 |     define_write!(write16_relaxed, try_write16_relaxed, writew_relaxed <- u16);
   |                                                         ^^^^^^^^^^^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `writel_relaxed` in crate `bindings`
   --> rust/kernel/io.rs:253:57
   |
   253 |     define_write!(write32_relaxed, try_write32_relaxed, writel_relaxed <- u32);
   |                                                         ^^^^^^^^^^^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `writeq_relaxed` in crate `bindings`
   --> rust/kernel/io.rs:258:9
   |
   258 |         writeq_relaxed <- u64
   |         ^^^^^^^^^^^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `readl` in crate `bindings`
   --> rust/kernel/io.rs:223:38
   |
   223 |     define_read!(read32, try_read32, readl -> u32);
   |                                      ^^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `readq` in crate `bindings`
   --> rust/kernel/io.rs:228:9
   |
   228 |         readq -> u64
   |         ^^^^^ not found in `bindings`
--
>> error[E0425]: cannot find function `readb_relaxed` in crate `bindings`
   --> rust/kernel/io.rs:231:52
   |
   231 |     define_read!(read8_relaxed, try_read8_relaxed, readb_relaxed -> u8);
   |                                                    ^^^^^^^^^^^^^ not found in `bindings`
..

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 01/11] rust: helpers: io: use macro to generate io accessor functions
Posted by Arnd Bergmann 9 months ago
On Fri, May 9, 2025, at 05:15, Andrew Ballance wrote:
> +
> +define_rust_mmio_read_helper(readb, u8);
> +define_rust_mmio_read_helper(readw, u16);
> +define_rust_mmio_read_helper(readl, u32);

This makes it harder to grep for the definitions when trying
to follow the code flow. Can you find a way to have keep the actual
function body in source form, using the name of the generated
symbol?

     Arnd