samples: rust: fix endianness issue in rust_driver_pci
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.
--- samples/rust/rust_driver_pci.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
samples/rust/rust_driver_pci.rs +1 -1
@@ -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
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.
Signed-off-by Marko Turk <[email protected]>Fixes 685376d18e9a ("samples: rust: add Rust PCI sample driver")
Hi,
Can someone take a look?
Thanks.
Marko
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.
Signed-off-by Marko Turk <[email protected]>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
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
Danilo Krummrich
maintainerQuoted 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. :)
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
Danilo Krummrich
maintainerQuoted 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. :)
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.
Signed-off-by Marko Turk <[email protected]>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
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.
Signed-off-by Marko Turk <[email protected]>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
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
Dirk Behme
Tested-byQuoted 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