Skip to contentSkip to conversation

samples: rust: fix endianness issue in rust_driver_pci

PATCH In review Marko Turk 5 participants 11 messages Nov 1, 2025 – Nov 13, 2025

Subsystems (3)
lore
Share & tools
lore

Marko Turk

author
#0 lore raw

PATCH 1/1 samples: rust: fix endianness issue in rust_driver_pci

QEMU PCI test device specifies all registers as little endian. OFFSET register is converted properly, but the COUNT register is not.

Apply the same conversion to the COUNT register also.

Fixes 685376d18e9a ("samples: rust: add Rust PCI sample driver")
---
 samples/rust/rust_driver_pci.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
samples/rust/rust_driver_pci.rs +1 -1
index 55a683c39ed9..3cbb3139fbcf 100644
@@ -56,7 +56,7 @@ fn testdev(index: &TestIndex, bar: &Bar0) -> Result<u32> {
         // Note that we need `try_write8`, since `offset` can't be checked at compile-time.
         bar.try_write8(data, offset)?;
 
-        Ok(bar.read32(Regs::COUNT))
+        Ok(u32::from_le(bar.read32(Regs::COUNT)))
     }
 }
 
Signature · 1 line
2.51.0
author
#1 lore raw
Quoted 7 lines · Marko Turk

QEMU PCI test device specifies all registers as little endian. OFFSET register is converted properly, but the COUNT register is not.

Apply the same conversion to the COUNT register also.

Fixes 685376d18e9a ("samples: rust: add Rust PCI sample driver")

Hi,

Can someone take a look?

Thanks.

Marko

#2 lore raw
Quoted 11 lines · Marko Turk
Quoted 7 lines · Marko Turk

QEMU PCI test device specifies all registers as little endian. OFFSET register is converted properly, but the COUNT register is not.

Apply the same conversion to the COUNT register also.

Fixes 685376d18e9a ("samples: rust: add Rust PCI sample driver")

Can someone take a look?

Your message was in my spam folder -- that may be affecting who saw it.

From https://www.qemu.org/docs/master/specs/pci-testdev.html:

"All registers are little endian."

So this seems right. A couple tags:

Cc'ing Dirk, since he tested the sample originally.

Thanks!

Cheers,
Miguel

#3 lore raw
Quoted 2 lines · Miguel Ojeda

Cc'ing Dirk, since he tested the sample originally.

i.e. a new Tested-by is appreciated if you have the time since you tested with QEMU originally, even if this is a no-op in x86 (where I think you tested).

I guess we could potentially consider something like returning a wrapper to force us to explicitly pick either LE or BE to prevent things like this.

Cheers,
Miguel

maintainer
#4 lore raw
Quoted 3 lines · Miguel Ojeda

I guess we could potentially consider something like returning a wrapper to force us to explicitly pick either LE or BE to prevent things like this.

In general, the I/O backend (e.g. MMIO, I2C, etc.) is supposed to take care of endianness.

In the case of MMIO we (currently) always assume that the device is little-endian; this is also what the C functions used to implement the MMIO backend (e.g. readl()) assume, i.e. they always convert from little-endian to CPU endianness.

I don't think we should do anything else; the cases where we need to deal with big-endian devices for MMIO should be extremely rare -- rare enough that there aren't even corresponding *_be() implementations for all architectures.

Given that, I think the proper fix is to drop the existing u32::from_le() call (that ended up in the sample driver by accident) and properly document the little-endian assumption of the MMIO backend.

(Currently, this simply is the Io structure, but there is a patch series [1] from Zhi splitting things up, so we can start supporting arbitrary I/O backends.)

@Marko: Two separate patches for this would be very welcome. :)

[1] https://lore.kernel.org/all/[email protected]/

#5 lore raw
Quoted 8 lines · Danilo Krummrich

I don't think we should do anything else; the cases where we need to deal with big-endian devices for MMIO should be extremely rare -- rare enough that there aren't even corresponding *_be() implementations for all architectures.

Given that, I think the proper fix is to drop the existing u32::from_le() call (that ended up in the sample driver by accident) and properly document the little-endian assumption of the MMIO backend.

Sounds good then, thanks!

Cheers,
Miguel

maintainer
#6 lore raw
In reply to Danilo Krummrich
Quoted 5 lines · Danilo Krummrich
Quoted 3 lines · Miguel Ojeda

I guess we could potentially consider something like returning a wrapper to force us to explicitly pick either LE or BE to prevent things like this.

Quoted 7 lines

In general, the I/O backend (e.g. MMIO, I2C, etc.) is supposed to take care of endianness.

In the case of MMIO we (currently) always assume that the device is little-endian; this is also what the C functions used to implement the MMIO backend (e.g. readl()) assume, i.e. they always convert from little-endian to CPU endianness.

I.e. if we'd support a big-endian architecture in Rust

	u32::from_le(bar.read32())

would be a bug.

Quoted 15 lines

I don't think we should do anything else; the cases where we need to deal with big-endian devices for MMIO should be extremely rare -- rare enough that there aren't even corresponding *_be() implementations for all architectures.

Given that, I think the proper fix is to drop the existing u32::from_le() call (that ended up in the sample driver by accident) and properly document the little-endian assumption of the MMIO backend.

(Currently, this simply is the Io structure, but there is a patch series [1] from Zhi splitting things up, so we can start supporting arbitrary I/O backends.)

@Marko: Two separate patches for this would be very welcome. :)

[1] https://lore.kernel.org/all/[email protected]/

#7 lore raw
In reply to Miguel Ojeda
Quoted 17 lines · Miguel Ojeda
Quoted 11 lines · Marko Turk
Quoted 7 lines · Marko Turk

QEMU PCI test device specifies all registers as little endian. OFFSET register is converted properly, but the COUNT register is not.

Apply the same conversion to the COUNT register also.

Fixes 685376d18e9a ("samples: rust: add Rust PCI sample driver")

Can someone take a look?

Your message was in my spam folder -- that may be affecting who saw it.

Quoted 1 line

From https://www.qemu.org/docs/master/specs/pci-testdev.html:

Quoted 8 lines

"All registers are little endian."

So this seems right. A couple tags:

Cc'ing Dirk, since he tested the sample originally.

Hmm, I can't find the initial patch in my Inbox. Even

https://lore.kernel.org/rust-for-linux/[email protected]/

doesn't seem to have it?

Thanks

Dirk

#8 lore raw
Quoted 25 lines · Dirk Behme
Quoted 24 lines · Miguel Ojeda
Quoted 11 lines · Marko Turk
Quoted 7 lines · Marko Turk

QEMU PCI test device specifies all registers as little endian. OFFSET register is converted properly, but the COUNT register is not.

Apply the same conversion to the COUNT register also.

Fixes 685376d18e9a ("samples: rust: add Rust PCI sample driver")

Can someone take a look?

Your message was in my spam folder -- that may be affecting who saw it.

Quoted 1 line

From https://www.qemu.org/docs/master/specs/pci-testdev.html:

"All registers are little endian."

So this seems right. A couple tags:

Quoted 8 lines
Quoted 1 line

Cc'ing Dirk, since he tested the sample originally.

Hmm, I can't find the initial patch in my Inbox. Even

https://lore.kernel.org/rust-for-linux/[email protected]/

doesn't seem to have it?

Initially I didn't send to rust-for-linux mailing list. It's here on lkml:
https://lore.kernel.org/lkml/[email protected]/

Marko

#9 lore raw
In reply to Dirk Behme
Quoted 6 lines · Dirk Behme

Hmm, I can't find the initial patch in my Inbox. Even

https://lore.kernel.org/rust-for-linux/[email protected]/

doesn't seem to have it?

It was only sent to the linux-pci list -- for situations like this, you can click in the "[not found]" message at the bottom, and then on

    Message-ID: <[email protected]>
    found in another inbox:

    ../../linux-pci/[email protected]/

where that last line is a link to the other list that you can click to find it.

I hope that helps!

Cheers,
Miguel

Tested-by
#10 lore raw
Quoted 11 lines · Miguel Ojeda
Quoted 6 lines · Dirk Behme

Hmm, I can't find the initial patch in my Inbox. Even

https://lore.kernel.org/rust-for-linux/[email protected]/

doesn't seem to have it?

It was only sent to the linux-pci list -- for situations like this, you can click in the "[not found]" message at the bottom, and then on

Quoted 8 lines
     Message-ID: <[email protected]>
     found in another inbox:

     ../../linux-pci/[email protected]/

where that last line is a link to the other list that you can click to find it.

I hope that helps!

Yes, thanks, that helps a lot!

And yes, the change itself looks good. I gave this a try with x86 QEMU and as expected (as mentioned it is a no-op on x86) I get with and without this patch

rust_driver_pci 0000:00:04.0: pci-testdev data-match count: 1

With this:

Thanks,

Dirk