Skip to content

Commit 6d71f53

Browse files
committed
lib: packing: demote truncation error in pack() to a warning in __pack()
JIRA: https://issues.redhat.com/browse/RHEL-92666 Upstream commit(s): commit 48c2752 Author: Vladimir Oltean <vladimir.oltean@nxp.com> Date: Tue Dec 10 12:27:11 2024 -0800 lib: packing: demote truncation error in pack() to a warning in __pack() Most of the sanity checks in pack() and unpack() can be covered at compile time. There is only one exception, and that is truncation of the uval during a pack() operation. We'd like the error-less __pack() to catch that condition as well. But at the same time, it is currently the responsibility of consumer drivers (currently just sja1105) to print anything at all when this error occurs, and then discard the return code. We can just print a loud warning in the library code and continue with the truncated __pack() operation. In practice, having the warning is very important, see commit 24deec6 ("net: dsa: sja1105: disallow C45 transactions on the BASE-TX MDIO bus") where the bug was caught exactly by noticing this print. Add the first print to the packing library, and at the same time remove the print for the same condition from the sja1105 driver, to avoid double printing. Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com> Signed-off-by: Jacob Keller <jacob.e.keller@intel.com> Reviewed-by: Vladimir Oltean <vladimir.oltean@nxp.com> Link: https://patch.msgid.link/20241210-packing-pack-fields-and-ice-implementation-v10-2-ee56a47479ac@intel.com Signed-off-by: Jakub Kicinski <kuba@kernel.org> Signed-off-by: Petr Oros <poros@redhat.com>
1 parent ad1e6fd commit 6d71f53

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

drivers/net/dsa/sja1105/sja1105_static_config.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,8 @@ void sja1105_pack(void *buf, const u64 *val, int start, int end, size_t len)
2626
pr_err("Start bit (%d) expected to be larger than end (%d)\n",
2727
start, end);
2828
} else if (rc == -ERANGE) {
29-
if ((start - end + 1) > 64)
30-
pr_err("Field %d-%d too large for 64 bits!\n",
31-
start, end);
32-
else
33-
pr_err("Cannot store %llx inside bits %d-%d (would truncate)\n",
34-
*val, start, end);
29+
pr_err("Field %d-%d too large for 64 bits!\n",
30+
start, end);
3531
}
3632
dump_stack();
3733
}

lib/packing.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,17 @@ static void __pack(void *pbuf, u64 uval, size_t startbit, size_t endbit,
5959
*/
6060
int plogical_first_u8 = startbit / BITS_PER_BYTE;
6161
int plogical_last_u8 = endbit / BITS_PER_BYTE;
62+
int value_width = startbit - endbit + 1;
6263
int box;
6364

65+
/* Check if "uval" fits in "value_width" bits.
66+
* The test only works for value_width < 64, but in the latter case,
67+
* any 64-bit uval will surely fit.
68+
*/
69+
WARN(value_width < 64 && uval >= (1ull << value_width),
70+
"Cannot store 0x%llx inside bits %zu-%zu - will truncate\n",
71+
uval, startbit, endbit);
72+
6473
/* Iterate through an idealistic view of the pbuf as an u64 with
6574
* no quirks, u8 by u8 (aligned at u8 boundaries), from high to low
6675
* logical bit significance. "box" denotes the current logical u8.
@@ -143,29 +152,14 @@ static void __pack(void *pbuf, u64 uval, size_t startbit, size_t endbit,
143152
int pack(void *pbuf, u64 uval, size_t startbit, size_t endbit, size_t pbuflen,
144153
u8 quirks)
145154
{
146-
/* width of the field to access in the pbuf */
147-
u64 value_width;
148-
149155
/* startbit is expected to be larger than endbit, and both are
150156
* expected to be within the logically addressable range of the buffer.
151157
*/
152158
if (unlikely(startbit < endbit || startbit >= BITS_PER_BYTE * pbuflen))
153159
/* Invalid function call */
154160
return -EINVAL;
155161

156-
value_width = startbit - endbit + 1;
157-
if (unlikely(value_width > 64))
158-
return -ERANGE;
159-
160-
/* Check if "uval" fits in "value_width" bits.
161-
* If value_width is 64, the check will fail, but any
162-
* 64-bit uval will surely fit.
163-
*/
164-
if (value_width < 64 && uval >= (1ull << value_width))
165-
/* Cannot store "uval" inside "value_width" bits.
166-
* Truncating "uval" is most certainly not desirable,
167-
* so simply erroring out is appropriate.
168-
*/
162+
if (unlikely(startbit - endbit >= 64))
169163
return -ERANGE;
170164

171165
__pack(pbuf, uval, startbit, endbit, pbuflen, quirks);

0 commit comments

Comments
 (0)