Skip to contentSkip to conversation

lib/packing.c behaving weird if buffer length is not multiple of 4 with QUIRK_LSW32_IS_FIRST

DISCUSSION Jacob Keller 2 participants 12 messages Aug 15, 2024 – Aug 26, 2024

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

Hi Vladimir!

I am recently attempting to refactor some bespoke code for packing data in the ice networking driver into a bit packed hardware array, using the <linux/packing.h> and lib/packing.c infrastructure.

My hardware packed buffer needs QUIRK_LITTLE_ENDIAN and QUIRK_LSW32_IS_FIRST, as the entire buffer is packed as little endian, and the lower words come first in the buffer.

Everything was working ok in initial testing, until I tried packing a buffer that was 22 bytes long. Everything seemed to shift by 2 bytes:

Here, I print the initial details such as offset, lsb, width, then the hex dump of the u64 value I'm trying to insert.

I do the call to packing() with the appropriate quirks set, and then hex dump the 22-byte buffer after.

Quoted 4 lines

kernel: offset=0, size=8, lsb=0, width=57
kernel: value: 60 9b fe 01 00 00 00 00
kernel: 0x0000000001fe9b60 -> 000-056: fe 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00
kernel: 0x0000000001fe9b60 -> 000-056: 00 00 00 00 00 00

It seems to have failed to copy the first two bytes in to the buffer.

I discovered that if I pretend the buffer is 24 bytes (a multiple of 4 bytes, i.e. a word size), then everything works as expected:

Quoted 4 lines

kernel: offset=0, size=8, lsb=0, width=57
kernel: value: 60 fc fe 01 00 00 00 00
kernel: 0x0000000001fefc60 -> 000-056: 60 fc fe 01 00 00 00 00 00 00 00 00 00 00 00 00
kernel: 0x0000000001fefc60 -> 000-056: 00 00 00 00 00 00 00 00

It seems like this is either a misunderstanding of a fundamental requirement of the packing() infrastructure, a bug in the QUIRK_LSW32_IS_FIRST, or I need a new quirk for the packing infrastructure?

Essentially, I think the problem is that it uses the length of the buffer to find the word, but when the length is not a multiple of 2, the word offset picked is incorrect.

Using a larger length than the size of the buffer "works" as long as I never use a bit offset thats larger than the *actual* buffer.. but that seems like a poor solution.

Essentially, it seems like the default indexing for big endian is searching for each byte from the end of the array and then using the quirks to swap back to the inverse ending.

I think the fix is probably just to do a round-up division on the len/4 check in get_reverse_lsw32_offset, since its calculating the total number of words in the length, and effectively rounds down for lengths that aren't multiples of 4.

Does this seem like the correct analysis to you?

Thanks,
Jake

#1 lore raw
Quoted 46 lines · Jacob Keller

Hi Vladimir!

I am recently attempting to refactor some bespoke code for packing data in the ice networking driver into a bit packed hardware array, using the <linux/packing.h> and lib/packing.c infrastructure.

My hardware packed buffer needs QUIRK_LITTLE_ENDIAN and QUIRK_LSW32_IS_FIRST, as the entire buffer is packed as little endian, and the lower words come first in the buffer.

Everything was working ok in initial testing, until I tried packing a buffer that was 22 bytes long. Everything seemed to shift by 2 bytes:

Here, I print the initial details such as offset, lsb, width, then the hex dump of the u64 value I'm trying to insert.

I do the call to packing() with the appropriate quirks set, and then hex dump the 22-byte buffer after.

Quoted 4 lines

kernel: offset=0, size=8, lsb=0, width=57
kernel: value: 60 9b fe 01 00 00 00 00
kernel: 0x0000000001fe9b60 -> 000-056: fe 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00
kernel: 0x0000000001fe9b60 -> 000-056: 00 00 00 00 00 00

It seems to have failed to copy the first two bytes in to the buffer.

I discovered that if I pretend the buffer is 24 bytes (a multiple of 4 bytes, i.e. a word size), then everything works as expected:

Quoted 4 lines

kernel: offset=0, size=8, lsb=0, width=57
kernel: value: 60 fc fe 01 00 00 00 00
kernel: 0x0000000001fefc60 -> 000-056: 60 fc fe 01 00 00 00 00 00 00 00 00 00 00 00 00
kernel: 0x0000000001fefc60 -> 000-056: 00 00 00 00 00 00 00 00

It seems like this is either a misunderstanding of a fundamental requirement of the packing() infrastructure, a bug in the QUIRK_LSW32_IS_FIRST, or I need a new quirk for the packing infrastructure?

Essentially, I think the problem is that it uses the length of the buffer to find the word, but when the length is not a multiple of 2, the word offset picked is incorrect.

Using a larger length than the size of the buffer "works" as long as I never use a bit offset thats larger than the *actual* buffer.. but that seems like a poor solution.

Quoted 9 lines

Essentially, it seems like the default indexing for big endian is searching for each byte from the end of the array and then using the quirks to swap back to the inverse ending.

I think the fix is probably just to do a round-up division on the len/4 check in get_reverse_lsw32_offset, since its calculating the total number of words in the length, and effectively rounds down for lengths that aren't multiples of 4.

This doesn't fix the problem. I printed out the logical box number and its mapped box address in the various modes with a length of 22 bytes:

Quoted 14 lines

kernel: default: 00 => 21
kernel: default: 01 => 20
kernel: default: 02 => 19
kernel: default: 03 => 18
kernel: default: 04 => 17
kernel: default: 05 => 16
kernel: default: 06 => 15
kernel: default: 07 => 14
kernel: default: 08 => 13
kernel: default: 09 => 12
kernel: default: 10 => 11
kernel: default: 11 => 10
kernel: default: 12 => 09
kernel: default: 13 => 08

Quoted 8 lines

kernel: default: 14 => 07
kernel: default: 15 => 06
kernel: default: 16 => 05
kernel: default: 17 => 04
kernel: default: 18 => 03
kernel: default: 19 => 02
kernel: default: 20 => 01
kernel: default: 21 => 00

Here, you can see that it correctly maps in "big endian" order the entire array. Basically it just reverses the ordering. Ok, this seems reasonable.

With LITTLE_ENDIAN set, things get weird:

Quoted 14 lines

kernel: LITTLE_ENDIAN: 00 => 22
kernel: LITTLE_ENDIAN: 01 => 23
kernel: LITTLE_ENDIAN: 02 => 16
kernel: LITTLE_ENDIAN: 03 => 17
kernel: LITTLE_ENDIAN: 04 => 18
kernel: LITTLE_ENDIAN: 05 => 19
kernel: LITTLE_ENDIAN: 06 => 12
kernel: LITTLE_ENDIAN: 07 => 13
kernel: LITTLE_ENDIAN: 08 => 14
kernel: LITTLE_ENDIAN: 09 => 15
kernel: LITTLE_ENDIAN: 10 => 08
kernel: LITTLE_ENDIAN: 11 => 09
kernel: LITTLE_ENDIAN: 12 => 10
kernel: LITTLE_ENDIAN: 13 => 11

Quoted 8 lines

kernel: LITTLE_ENDIAN: 14 => 04
kernel: LITTLE_ENDIAN: 15 => 05
kernel: LITTLE_ENDIAN: 16 => 06
kernel: LITTLE_ENDIAN: 17 => 07
kernel: LITTLE_ENDIAN: 18 => 00
kernel: LITTLE_ENDIAN: 19 => 01
kernel: LITTLE_ENDIAN: 20 => 02
kernel: LITTLE_ENDIAN: 21 => 03

We map *outside* the buffer! 0 goes to 22, 1 goes to 23! Nothing at all maps into 20 or 21. This is extremely not-intuitive. In practice, without having read the docs I would have expected LITTLE_ENDIAN to directly map 0 to 0. After having read docs, (where it mentions that LITTLE_ENDIAN only does by word), I honestly have no idea what I would expect. If we try to just do 4 byte blocks at a time, we end up having to swap bytes from outside the length.

With LSW32_IS_FIRST set, things are even weirder:

Quoted 14 lines

kernel: LSW32_IS_FIRST: 00 => -3
kernel: LSW32_IS_FIRST: 01 => -4
kernel: LSW32_IS_FIRST: 02 => 03
kernel: LSW32_IS_FIRST: 03 => 02
kernel: LSW32_IS_FIRST: 04 => 01
kernel: LSW32_IS_FIRST: 05 => 00
kernel: LSW32_IS_FIRST: 06 => 07
kernel: LSW32_IS_FIRST: 07 => 06
kernel: LSW32_IS_FIRST: 08 => 05
kernel: LSW32_IS_FIRST: 09 => 04
kernel: LSW32_IS_FIRST: 10 => 11
kernel: LSW32_IS_FIRST: 11 => 10
kernel: LSW32_IS_FIRST: 12 => 09
kernel: LSW32_IS_FIRST: 13 => 08

Quoted 8 lines

kernel: LSW32_IS_FIRST: 14 => 15
kernel: LSW32_IS_FIRST: 15 => 14
kernel: LSW32_IS_FIRST: 16 => 13
kernel: LSW32_IS_FIRST: 17 => 12
kernel: LSW32_IS_FIRST: 18 => 19
kernel: LSW32_IS_FIRST: 19 => 18
kernel: LSW32_IS_FIRST: 20 => 17
kernel: LSW32_IS_FIRST: 21 => 16

We access bytes with negative indexes...

And with both set:

Quoted 14 lines

kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 00 => -2
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 01 => -1
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 02 => 00
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 03 => 01
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 04 => 02
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 05 => 03
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 06 => 04
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 07 => 05
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 08 => 06
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 09 => 07
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 10 => 08
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 11 => 09
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 12 => 10
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 13 => 11

Quoted 8 lines

kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 14 => 12
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 15 => 13
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 16 => 14
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 17 => 15
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 18 => 16
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 19 => 17
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 20 => 18
kernel: LITTLE_ENDIAN | LSW32_IS_FIRST: 21 => 19

We still get negative indexes, but things are close. Everything is off by 2 bytes from what I would expect.

I'm honestly not sure what the right solution here is, because the way LITTLE_ENDIAN and LSW32_IS_FIRST work they effectively *require* word-aligned sizes. If we use a word-aligned size, then they both make sense, but my hardware buffer isn't word aligned. I can cheat, and just make sure I never use bits that access the invalid parts of the buffer.. but that seems like the wrong solution... A larger size would break normal Big endian ordering without quirks...

Really, what my hardware buffer wants is to map the lowest byte of the data to the lowest byte of the buffer. This is what i would consider traditionally little endian ordering.

This also happens to be is equivalent to LSW32_IS_FIRST and LITTLE_ENDIAN when sizes are multiples of 4.

maintainer
#2 lore raw

Hi Jake,

Quoted 7 lines · Jacob Keller

I'm honestly not sure what the right solution here is, because the way LITTLE_ENDIAN and LSW32_IS_FIRST work they effectively *require* word-aligned sizes. If we use a word-aligned size, then they both make sense, but my hardware buffer isn't word aligned. I can cheat, and just make sure I never use bits that access the invalid parts of the buffer.. but that seems like the wrong solution... A larger size would break normal Big endian ordering without quirks...

It is a use case that I would like to support. Thanks for having the patience to explain the issue to me.

Quoted 6 lines

Really, what my hardware buffer wants is to map the lowest byte of the data to the lowest byte of the buffer. This is what i would consider traditionally little endian ordering.

This also happens to be is equivalent to LSW32_IS_FIRST and LITTLE_ENDIAN when sizes are multiples of 4.

Yes, "traditionally little endian" would indeed translate into QUIRK_LSW32_IS_FIRST | QUIRK_LITTLE_ENDIAN. Your use of the API seems correct. I did need that further distinction between "little endian within a group of 4 bytes" and "little endian among groups of 4 bytes" because the NXP SJA1105 memory layout is weird like that, and is "little endian" in one way but not in another. Anyway..

I've attached 2 patches which hopefully make the API usable for your driver. I've tested them locally and did not notice issues.

From 8314f556f30e5e76973b1bcf1c552ff4a23621b9 Mon Sep 17 00:00:00 2001
From: Vladimir Oltean <[email protected]>
Date: Sun, 18 Aug 2024 15:50:56 +0300
Subject: [PATCH 1/2] lib: packing: refuse operating on bit indices which
exceed size of buffer

While reworking the implementation, it became apparent that this check does not exist.

There is no functional issue yet, because at call sites, "startbit" and "endbit" are always hardcoded to correct values, and never come from the user.

Even with the upcoming support of arbitrary buffer lengths, the "startbit >= 8 * pbuflen" check will remain correct. This is because we intend to always interpret the packed buffer in a way that avoids discontinuities in the available bit indices.

---
 lib/packing.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
lib/packing.c +4 -2
index 3f656167c17e..1cb3e0b2a24e 100644
@@ -86,8 +86,10 @@ int packing(void *pbuf, u64 *uval, int startbit, int endbit, size_t pbuflen,
 	 */
 	int plogical_first_u8, plogical_last_u8, box;
 
-	/* startbit is expected to be larger than endbit */
-	if (startbit < endbit)
+	/* startbit is expected to be larger than endbit, and both are
+	 * expected to be within the logically addressable range of the buffer.
+	 */
+	if (startbit < endbit || startbit >= 8 * pbuflen || endbit < 0)
 		/* Invalid function call */
 		return -EINVAL;
 
Signature · 1 line
2.34.1

From c37c61b4de3dcc4b7837445f0884f7c67e73f623 Mon Sep 17 00:00:00 2001
From: Vladimir Oltean <[email protected]>
Date: Sun, 18 Aug 2024 15:48:26 +0300
Subject: [PATCH 2/2] lib: packing: adjust definitions and implementation for
arbitrary buffer lengths

Jacob Keller has a use case for packing() in the intel/ice networking driver, but it cannot be used as-is.

Simply put, the API quirks for LSW32_IS_FIRST and LITTLE_ENDIAN are naively implemented with the undocumented assumption that the buffer length must be a multiple of 4. All calculations of group offsets and offsets of bytes within groups assume that this is the case. But in the ice case, this does not hold true. For example, packing into a buffer of 22 bytes would yield wrong results, but pretending it was a 24 byte buffer would work.

Rather than requiring such hacks, and leaving a big question mark when it comes to discontinuities in the accessible bit fields of such buffer, we should extend the packing API to support this use case.

It turns out that we can keep the design in terms of groups of 4 bytes, but also make it work if the total length is not a multiple of 4. Just like before, imagine the buffer as a big number, and its most significant bytes (the ones that would make up to a multiple of 4) are missing. Thus, with a big endian (no quirks) interpretation of the buffer, those most significant bytes would be absent from the beginning of the buffer, and with a LSW32_IS_FIRST interpretation, they would be absent from the end of the buffer. The LITTLE_ENDIAN quirk, in the packing() API world, only affects byte ordering within groups of 4. Thus, it does not change which bytes are missing. Only the significance of the remaining bytes within the (smaller) group.

No change intended for buffer sizes which are multiples of 4. Tested with the sja1105 driver and with downstream unit tests.

---
 Documentation/core-api/packing.rst | 71 ++++++++++++++++++++++++++++++
 lib/packing.c                      | 70 +++++++++++++++++------------
 2 files changed, 114 insertions(+), 27 deletions(-)
Documentation/core-api/packing.rst +71 -0

77 lines not on this page · raw message

lib/packing.c +43 -27

88 lines not on this page · raw message

Signature · 1 line
2.34.1
#3 lore raw
Quoted 11 lines · Vladimir Oltean

Hi Jake,

Quoted 7 lines · Jacob Keller

I'm honestly not sure what the right solution here is, because the way LITTLE_ENDIAN and LSW32_IS_FIRST work they effectively *require* word-aligned sizes. If we use a word-aligned size, then they both make sense, but my hardware buffer isn't word aligned. I can cheat, and just make sure I never use bits that access the invalid parts of the buffer.. but that seems like the wrong solution... A larger size would break normal Big endian ordering without quirks...

Quoted 3 lines

It is a use case that I would like to support. Thanks for having the patience to explain the issue to me.

Great, thank!

Quoted 7 lines
Quoted 6 lines

Really, what my hardware buffer wants is to map the lowest byte of the data to the lowest byte of the buffer. This is what i would consider traditionally little endian ordering.

This also happens to be is equivalent to LSW32_IS_FIRST and LITTLE_ENDIAN when sizes are multiples of 4.

Quoted 6 lines

Yes, "traditionally little endian" would indeed translate into QUIRK_LSW32_IS_FIRST | QUIRK_LITTLE_ENDIAN. Your use of the API seems correct. I did need that further distinction between "little endian within a group of 4 bytes" and "little endian among groups of 4 bytes" because the NXP SJA1105 memory layout is weird like that, and is "little endian" in one way but not in another. Anyway..

Yea, I figured the distinction was based on real hardware.

Quoted 3 lines

I've attached 2 patches which hopefully make the API usable for your driver. I've tested them locally and did not notice issues.

I'll check these out and get back to you!

Thanks,
Jake

author Tested-by
#4 lore raw
Quoted 38 lines · Jacob Keller
Quoted 11 lines · Vladimir Oltean

Hi Jake,

Quoted 7 lines · Jacob Keller

I'm honestly not sure what the right solution here is, because the way LITTLE_ENDIAN and LSW32_IS_FIRST work they effectively *require* word-aligned sizes. If we use a word-aligned size, then they both make sense, but my hardware buffer isn't word aligned. I can cheat, and just make sure I never use bits that access the invalid parts of the buffer.. but that seems like the wrong solution... A larger size would break normal Big endian ordering without quirks...

Quoted 3 lines

It is a use case that I would like to support. Thanks for having the patience to explain the issue to me.

Great, thank!

Quoted 7 lines
Quoted 6 lines

Really, what my hardware buffer wants is to map the lowest byte of the data to the lowest byte of the buffer. This is what i would consider traditionally little endian ordering.

This also happens to be is equivalent to LSW32_IS_FIRST and LITTLE_ENDIAN when sizes are multiples of 4.

Quoted 6 lines

Yes, "traditionally little endian" would indeed translate into QUIRK_LSW32_IS_FIRST | QUIRK_LITTLE_ENDIAN. Your use of the API seems correct. I did need that further distinction between "little endian within a group of 4 bytes" and "little endian among groups of 4 bytes" because the NXP SJA1105 memory layout is weird like that, and is "little endian" in one way but not in another. Anyway..

Yea, I figured the distinction was based on real hardware.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines

I've attached 2 patches which hopefully make the API usable for your driver. I've tested them locally and did not notice issues.

I'll check these out and get back to you!

Thanks,
Jake

The patches work for my use-case! I also think it might be helpful to add some Kunit tests to cover the packing and unpacking, which I wouldn't mind trying to do.

If/when you send the patches, feel free to add:

Alternatively, I could send them as part of the series where I implement the changes to use lib/packing in the ice driver.

Thanks,
Jake

maintainer
#5 lore raw
Quoted 10 lines · Jacob Keller

The patches work for my use-case! I also think it might be helpful to add some Kunit tests to cover the packing and unpacking, which I wouldn't mind trying to do.

If/when you send the patches, feel free to add:

Alternatively, I could send them as part of the series where I implement the changes to use lib/packing in the ice driver.

This is good to hear!

I did have some boot-time selftests written, albeit in a very bare-bones format. I'm afraid I do not have as much energy as I'd wish to push this patch set forward. Especially not to learn to use KUnit and to adapt the tests to that format.

If you wish, here is a Github branch with what I have. You can pick from it for your submissions and adapt as you see fit.
https://github.com/vladimiroltean/linux/tree/packing-selftests

#6 lore raw
Quoted 12 lines · Vladimir Oltean
Quoted 10 lines · Jacob Keller

The patches work for my use-case! I also think it might be helpful to add some Kunit tests to cover the packing and unpacking, which I wouldn't mind trying to do.

If/when you send the patches, feel free to add:

Alternatively, I could send them as part of the series where I implement the changes to use lib/packing in the ice driver.

Quoted 6 lines

This is good to hear!

I did have some boot-time selftests written, albeit in a very bare-bones format. I'm afraid I do not have as much energy as I'd wish to push this patch set forward. Especially not to learn to use KUnit and to adapt the tests to that format.

No problem. I'm quite happy to work on that part.

Quoted 4 lines

If you wish, here is a Github branch with what I have. You can pick from it for your submissions and adapt as you see fit.
https://github.com/vladimiroltean/linux/tree/packing-selftests

Ok. I'll investigate this, and I will send the two fixes for lib/packing in my series to implement the support in ice. That would help on our end with managing the changes since it avoids an interdependence between multiple series in flight.

Regards,
Jake

maintainer
#7 lore raw
Quoted 4 lines · Jacob Keller

Ok. I'll investigate this, and I will send the two fixes for lib/packing in my series to implement the support in ice. That would help on our end with managing the changes since it avoids an interdependence between multiple series in flight.

There's one patch in there which replaces the packing(PACK) call with a dedicated pack() function, and packing(UNPACK) with unpack(). The idea being that it helps with const correctness. I still have some mixed feelings about this, because a multiplexed packing() call is in some ways more flexible, but apparently others felt bad enough about the packing() API to tell me about it, and that stuck with me.

I'm mentioning it because if you're going to use the API, you could at least consider using the const-correct form, so that there's one less driver to refactor later.

#8 lore raw
Quoted 13 lines · Vladimir Oltean
Quoted 4 lines · Jacob Keller

Ok. I'll investigate this, and I will send the two fixes for lib/packing in my series to implement the support in ice. That would help on our end with managing the changes since it avoids an interdependence between multiple series in flight.

There's one patch in there which replaces the packing(PACK) call with a dedicated pack() function, and packing(UNPACK) with unpack(). The idea being that it helps with const correctness. I still have some mixed feelings about this, because a multiplexed packing() call is in some ways more flexible, but apparently others felt bad enough about the packing() API to tell me about it, and that stuck with me.

Quoted 3 lines

I'm mentioning it because if you're going to use the API, you could at least consider using the const-correct form, so that there's one less driver to refactor later.

Yep! I've got those patches in my series now. Though I should note that I did not include any of the patches for the other drivers. I'll CC you when I send the series out, though it may likely go through our Intel-Wired-LAN tree first.

I've refactored your self tests into KUnit tests as well!

#9 lore raw
Quoted 20 lines · Jacob Keller
Quoted 13 lines · Vladimir Oltean
Quoted 4 lines · Jacob Keller

Ok. I'll investigate this, and I will send the two fixes for lib/packing in my series to implement the support in ice. That would help on our end with managing the changes since it avoids an interdependence between multiple series in flight.

There's one patch in there which replaces the packing(PACK) call with a dedicated pack() function, and packing(UNPACK) with unpack(). The idea being that it helps with const correctness. I still have some mixed feelings about this, because a multiplexed packing() call is in some ways more flexible, but apparently others felt bad enough about the packing() API to tell me about it, and that stuck with me.

Quoted 3 lines

I'm mentioning it because if you're going to use the API, you could at least consider using the const-correct form, so that there's one less driver to refactor later.

Quoted 7 lines

Yep! I've got those patches in my series now. Though I should note that I did not include any of the patches for the other drivers. I'll CC you when I send the series out, though it may likely go through our Intel-Wired-LAN tree first.

I've refactored your self tests into KUnit tests as well!

I was writing additional tests and I think I ran into another issue with QUIRK_MSB_ON_THE_RIGHT, when the bit offsets are not aligned to a byte boundary:

When trying to unpack 0x1122334455667788 from the buffer between offsets 106-43, the calculation appears to completely break.

When packing:

Quoted 43 lines

[18:34:50] box_bit_width = 3 [18:34:50] box_start_bit = 2 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 2 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 5 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 3

Quoted 8 lines

[18:34:50] new_box_start_bit = 1
[18:34:50] new_box_end_bit = -3
[18:34:50] # packing_test_unpack: EXPECTATION FAILED at lib/packing_test.c:264
[18:34:50] Expected uval == params->uval, but
[18:34:50] uval == 1234605616436508544 (0x1122334455667780)
[18:34:50] params->uval == 1234605616436508552 (0x1122334455667788)
[18:34:50] [FAILED] msb right, 16 bytes, non-aligned [18:34:50] # packing_test_unpack: pass:19 fail:1 skip:0 total:20

Notice that the box end bit is now negative. Specifically this is because the width is smaller than the start bit, so subtraction underflows.

When unpacking:

Quoted 43 lines

[18:34:50] box_bit_width = 3 [18:34:50] box_start_bit = 2 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 2 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 5 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 3

Quoted 8 lines

[18:34:50] new_box_start_bit = 1
[18:34:50] new_box_end_bit = -3
[18:34:50] # packing_test_unpack: EXPECTATION FAILED at lib/packing_test.c:264
[18:34:50] Expected uval == params->uval, but
[18:34:50] uval == 1234605616436508544 (0x1122334455667780)
[18:34:50] params->uval == 1234605616436508552 (0x1122334455667788)
[18:34:50] [FAILED] msb right, 16 bytes, non-aligned [18:34:50] # packing_test_unpack: pass:19 fail:1 skip:0 total:20

Specifically, it looks like we basically fail to calculate valid new box offsets.

What's weird to me is that when the box width is larger than the start bit position, we just calculate the same exact offsets, so I don't see why the existing calculations are there at all. Something is obviously wrong here.

#10 lore raw
Quoted 148 lines · Jacob Keller
Quoted 20 lines · Jacob Keller
Quoted 13 lines · Vladimir Oltean
Quoted 4 lines · Jacob Keller

Ok. I'll investigate this, and I will send the two fixes for lib/packing in my series to implement the support in ice. That would help on our end with managing the changes since it avoids an interdependence between multiple series in flight.

There's one patch in there which replaces the packing(PACK) call with a dedicated pack() function, and packing(UNPACK) with unpack(). The idea being that it helps with const correctness. I still have some mixed feelings about this, because a multiplexed packing() call is in some ways more flexible, but apparently others felt bad enough about the packing() API to tell me about it, and that stuck with me.

Quoted 3 lines

I'm mentioning it because if you're going to use the API, you could at least consider using the const-correct form, so that there's one less driver to refactor later.

Quoted 7 lines

Yep! I've got those patches in my series now. Though I should note that I did not include any of the patches for the other drivers. I'll CC you when I send the series out, though it may likely go through our Intel-Wired-LAN tree first.

I've refactored your self tests into KUnit tests as well!

I was writing additional tests and I think I ran into another issue with QUIRK_MSB_ON_THE_RIGHT, when the bit offsets are not aligned to a byte boundary:

When trying to unpack 0x1122334455667788 from the buffer between offsets 106-43, the calculation appears to completely break.

When packing:

Quoted 43 lines

[18:34:50] box_bit_width = 3 [18:34:50] box_start_bit = 2 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 2 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 5 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 3

Quoted 8 lines

[18:34:50] new_box_start_bit = 1
[18:34:50] new_box_end_bit = -3
[18:34:50] # packing_test_unpack: EXPECTATION FAILED at lib/packing_test.c:264
[18:34:50] Expected uval == params->uval, but
[18:34:50] uval == 1234605616436508544 (0x1122334455667780)
[18:34:50] params->uval == 1234605616436508552 (0x1122334455667788)
[18:34:50] [FAILED] msb right, 16 bytes, non-aligned [18:34:50] # packing_test_unpack: pass:19 fail:1 skip:0 total:20

Notice that the box end bit is now negative. Specifically this is because the width is smaller than the start bit, so subtraction underflows.

When unpacking:

Quoted 43 lines

[18:34:50] box_bit_width = 3 [18:34:50] box_start_bit = 2 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 2 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 8 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 0 [18:34:50] new_box_start_bit = 7 [18:34:50] new_box_end_bit = 0 [18:34:50] box_bit_width = 5 [18:34:50] box_start_bit = 7 [18:34:50] box_end_bit = 3

Quoted 8 lines

[18:34:50] new_box_start_bit = 1
[18:34:50] new_box_end_bit = -3
[18:34:50] # packing_test_unpack: EXPECTATION FAILED at lib/packing_test.c:264
[18:34:50] Expected uval == params->uval, but
[18:34:50] uval == 1234605616436508544 (0x1122334455667780)
[18:34:50] params->uval == 1234605616436508552 (0x1122334455667788)
[18:34:50] [FAILED] msb right, 16 bytes, non-aligned [18:34:50] # packing_test_unpack: pass:19 fail:1 skip:0 total:20

Quoted 8 lines

Specifically, it looks like we basically fail to calculate valid new box offsets.

What's weird to me is that when the box width is larger than the start bit position, we just calculate the same exact offsets, so I don't see why the existing calculations are there at all. Something is obviously wrong here.

Specifically this is making me question:

Does QUIRK_MSB_ON_THE_RIGHT mean that the msb of each bit field is on the right? or does it mean that every byte in the buffer has its 8 bits flipped? It seems to document that it applies to the bits within the byte, but not to the byte ordering.

The code seems to be trying to do a mix of different things though, and my attempts at fixing it haven't worked properly yet.

I think I was able to get pack() to behave correctly, but unpacking seems to still be erratic. I tried adding a few more tests but so far haven't figured out what is wrong with the unpacking code.

I think the attempt to re-use adjust_for_msb_right on both unpacking and packing isn't working correctly. When unpacking, I think we end up masking the wrong bits.

With the attached patch, I was able to get my test case for packing fixed, but the following couple of tests fail:

Quoted 9 lines
        {
                .desc = "msb right, 16 bytes, non-aligned",
                PBUF(0x00, 0x00, 0x00, 0x91, 0x88, 0x59, 0x44, 0xd5,
                     0xcc, 0x3d, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00),
                .uval = 0x1122334455667788,
                .start_bit = 106,
                .end_bit = 43,
                .quirks = QUIRK_MSB_ON_THE_RIGHT,
        },

packing for this test works with the attached patch, but fails to unpack:

Quoted 1 line

[12:49:53] # packing_test_unpack: EXPECTATION FAILED at

lib/packing_test.c:282

Quoted 3 lines

[12:49:53] Expected uval == params->uval, but
[12:49:53] uval == 1234605616436508544 (0x1122334455667780)
[12:49:53] params->uval == 1234605616436508552 (0x1122334455667788)

the last few bits don't seem to be included properly.

I also tried a test case with all bits of the u64 set:

Quoted 11 lines
        {
                .desc = "msb right, 16 bytes, non-aligned, 0xff",
                PBUF(0x00, 0x00, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff,
                     0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00),
                .uval = 0xffffffffffffffff,
                .start_bit = 106,
                .end_bit = 43,
                .quirks = QUIRK_MSB_ON_THE_RIGHT,
        },
Quoted 4 lines

[12:49:53] # packing_test_unpack: EXPECTATION FAILED at lib/packing_test.c:282
[12:49:53] Expected uval == params->uval, but
[12:49:53] uval == 2305843009213693944 (0x1ffffffffffffff8)
[12:49:53] params->uval == -1 (0xffffffffffffffff)

In this case, again it seems like the bits on the tail end partial bytes don't get unpacked properly.

This test also passes packing with the attempted fix.

I so far think the likely issue is with the way we handle the box and mask offsets. Somehow we must be shifting things in a strange way that causes us to discard bits, but only when we deal with the QUIRK_MSB_ON_THE_RIGHT.

Thanks,
Jake

lib/packing.c +9 -7
index 80c95dacbfaa..c7aab00b106c 100644
@@ -14,14 +14,16 @@ static void adjust_for_msb_right_quirk(u64 *to_write, size_t *box_start_bit,
 {
 	size_t box_bit_width = *box_start_bit - *box_end_bit + 1;
 	size_t new_box_start_bit, new_box_end_bit;
+	u8 new_box_mask;
 
-	*to_write >>= *box_end_bit;
-	*to_write = bitrev8(*to_write) >> (8 - box_bit_width);
-	*to_write <<= *box_end_bit;
 
-	new_box_end_bit   = box_bit_width - *box_start_bit - 1;
-	new_box_start_bit = box_bit_width - *box_end_bit - 1;
-	*box_mask = GENMASK_ULL(new_box_start_bit, new_box_end_bit);
+	*to_write = bitrev8(*to_write);
+
+	new_box_mask = bitrev8(*box_mask);
+	new_box_end_bit = ffs(new_box_mask) - 1;
+	new_box_start_bit = fls(new_box_mask) - 1;
+
+	*box_mask = new_box_mask;
 	*box_start_bit = new_box_start_bit;
 	*box_end_bit   = new_box_end_bit;
 }
@@ -170,13 +172,13 @@ int pack(void *pbuf, u64 uval, size_t startbit, size_t endbit, size_t pbuflen,
 		/* Write to pbuf, read from uval */
 		pval = uval & proj_mask;
 		pval >>= proj_end_bit;
+		pval <<= box_end_bit;
 		if (quirks & QUIRK_MSB_ON_THE_RIGHT)
 			adjust_for_msb_right_quirk(&pval,
 						   &box_start_bit,
 						   &box_end_bit,
 						   &box_mask);
 
-		pval <<= box_end_bit;
 		((u8 *)pbuf)[box_addr] &= ~box_mask;
 		((u8 *)pbuf)[box_addr] |= pval;
 	}
#11 lore raw
Quoted 7 lines · Jacob Keller

I so far think the likely issue is with the way we handle the box and mask offsets. Somehow we must be shifting things in a strange way that causes us to discard bits, but only when we deal with the QUIRK_MSB_ON_THE_RIGHT.

Thanks,
Jake

I eventually figured out what i believe is wrong here, and have a fix. I'm going to include it in the series I send for using <linux/packing.h> in the ice driver, along with the additional KUnit tests.

Thanks,
Jake