mfd: viperboard: fix native fields type in structures as little-endiand
PATCH 1/1 mfd: viperboard: fix native fields type in structures as little-endiand
Sparse reports several warnings about incorrect type in assignment (different base types) when building the i2c-viperboard.c driver:
warning: incorrect type in assignment (different base types)
expected unsigned short [usertype] addr
got restricted __le16 [usertype]
The structures vprbrd_i2c_write_hdr, vprbrd_i2c_read_hdr, and vprbrd_i2c_addr_msg describe raw USB packets and, as I understand it, require Little-Endian byte-order. Therefore, 16-bit field in these structures were annotated as __le16, not native unsigned short. This satisfies Sparse and ensures correct behavior on Big-Endian architectures.
--- include/linux/mfd/viperboard.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/mfd/viperboard.h +4 -4
@@ -57,15 +57,15 @@ struct vprbrd_i2c_write_hdr {struct vprbrd_i2c_read_hdr {u8 cmd; -u16 addr; +__le16 addr;u8 len0;u8 len1;u8 len2;u8 len3;u8 len4;u8 len5; -u16 tf1;/* transfer 1 length */ -u16 tf2;/* transfer 2 length */ +__le16 tf1;/* transfer 1 length */ +__le16 tf2;/* transfer 2 length */ } __packed;struct vprbrd_i2c_status {
@@ -89,7 +89,7 @@ struct vprbrd_i2c_addr_msg {u8 cmd;u8 addr;u8 unknown1; -u16 len; +__le16 len;u8 unknown2;u8 unknown3; } __packed;
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
New issues:
- [Medium] Incomplete fix: vprbrd_i2c_write_hdr was not updated as claimed by the commit message.
- [Low] Commit message violates MFD subsystem capitalization guidelines.
Pre-existing issues:
- [High] vprbrd_probe() hard-codes PLATFORM_DEVID_NONE (-1), preventing multiple devices from probing.
- [High] vprbrd_i2c_xfer() incorrectly returns 0 on success instead of the number of transferred messages.
- [High] Ignored return value of i2c_add_adapter() can lead to kernel crash on module removal.
- [High] Buffer overwrite during large I2C reads causes corrupted USB command headers to be sent.
- [High] DMA buffer shares a cacheline with a mutex, causing data corruption on non-coherent architectures. --
--- Patch [1]: [PATCH v1] mfd: viperboard: fix native fields type in structures as little-endiand --- commit cb2959fa53e5e3eb2ffeb7ffb3dcd0156c7fb289
Author: Mikhail Lukianchikov <[email protected]>
Quoted 1 line
mfd: viperboard: fix native fields type in structures as little-endiand
[Severity: Low]
This isn't a bug, but does the commit message violate the MFD subsystem capitalization guidelines? The word 'fix' is lowercase, but the guidelines request capitalizing the description after the subsystem prefix.
This commit updates 16-bit fields in raw USB packet structures to use __le16 instead of u16 to fix sparse warnings about endianness mismatches.
include/linux/mfd/viperboard.h struct vprbrd_i2c_write_hdr {
Quoted 5 lines
include/linux/mfd/viperboard.h +0
-0@@ -62,15 +62,15 @@ struct vprbrd_i2c_write_hdr {
[Severity: Medium]
Was struct vprbrd_i2c_write_hdr intended to be updated in this patch?
The commit message states that the fields in this structure require Little-Endian byte-order, but the structure remains unpatched as u16, leaving the sparse warning unresolved for the write path.
include/linux/mfd/viperboard.h struct vprbrd_i2c_write_hdr { · line 60
Quoted 5 lines
include/linux/mfd/viperboard.h +1
-1struct vprbrd_i2c_read_hdr {u8 cmd; -u16 addr; +__le16 addr;
[ ... ]
[Severity: High]
This isn't a bug introduced by this patch, but does vprbrd_probe() prevent multiple devices from probing by forcing static naming?
drivers/mfd/viperboard.c:vprbrd_probe() {
...
ret = mfd_add_devices(&interface->dev, -1, vprbrd_devs,
ARRAY_SIZE(vprbrd_devs), NULL, 0, NULL);
...
}Passing PLATFORM_DEVID_NONE (-1) instead of PLATFORM_DEVID_AUTO assigns static names to the subdevices. If a second device is connected, won't this cause a sysfs collision and probe failure?
[Severity: High]
This isn't a bug introduced by this patch, but does vprbrd_i2c_xfer() return the correct value on success?
drivers/i2c/busses/i2c-viperboard.c:vprbrd_i2c_xfer() {
...
mutex_unlock(&vb->lock);
}
return 0;
error:
mutex_unlock(&vb->lock);
return error;
}The I2C subsystem requires the master_xfer callback to return the number of successfully processed messages on success. Will returning 0 cause the I2C core and client drivers to erroneously believe the transfer failed?
[Severity: High]
This isn't a bug introduced by this patch, but can the ignored return value of i2c_add_adapter() lead to a kernel crash on module removal?
drivers/i2c/busses/i2c-viperboard.c:vprbrd_i2c_probe() {
...
/* attach to i2c layer */
i2c_add_adapter(&vb_i2c->i2c);
platform_set_drvdata(pdev, vb_i2c);
return 0;
}If registration fails, the driver probe still succeeds. Upon device removal, vprbrd_i2c_remove() will unconditionally call i2c_del_adapter(). Does this attempt to unregister a device that was never fully added, leading to list corruption or a crash?
[Severity: High]
This isn't a bug introduced by this patch, but is there a buffer overwrite during large I2C reads that causes corrupted USB command headers to be sent?
drivers/i2c/busses/i2c-viperboard.c:vprbrd_i2c_read() {
...
/* second read transfer if neccessary */
if (len2 > 0) {
ret = vprbrd_i2c_receive(vb->usb_dev, rmsg, len2);
if (ret < 0)
return ret;
/* copy the received data */
memcpy(msg->buf + start + 512, rmsg, len2);
}
...
}The rmsg buffer is used to send the read command header and receive the payload. The first USB IN transfer overwrites the command header with incoming I2C data. For reads requiring a second chunk, is the overwritten garbage data sent as a command header to the device?
[Severity: High]
This isn't a bug introduced by this patch, but does the DMA buffer share a cacheline with a mutex, risking memory corruption on non-coherent architectures?
include/linux/mfd/viperboard.h:struct vprbrd {
...
struct usb_device *usb_dev; /* the usb device for this device */
struct mutex lock;
u8 buf[sizeof(struct vprbrd_i2c_write_msg)];
struct platform_device pdev;
};The buf array is mapped for USB DMA and placed immediately after the lock mutex. If the mutex is accessed while DMA is active, will the cacheline be dirtied by the CPU and overwrite the DMA data, or conversely, will DMA cache invalidation destroy the CPU's modifications to the mutex waitqueue?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 1/1 mfd: viperboard: fix native fields type in structures as little-endiand
In reply to Mikhail LukianchikovSparse reports several warnings about incorrect type in assignment (different base types) when building the i2c-viperboard.c driver:
warning: incorrect type in assignment (different base types)
expected unsigned short [usertype] addr
got restricted __le16 [usertype]
Added missing change.
---
Change in v2:
- Added missing change in vprbrd_i2c_write_hdr structure.
include/linux/mfd/viperboard.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
include/linux/mfd/viperboard.h +5 -5
@@ -47,7 +47,7 @@struct vprbrd_i2c_write_hdr {u8 cmd; -u16 addr; +__le16 addr;u8 len1;u8 len2;u8 last;
@@ -57,15 +57,15 @@ struct vprbrd_i2c_write_hdr {struct vprbrd_i2c_read_hdr {u8 cmd; -u16 addr; +__le16 addr;u8 len0;u8 len1;u8 len2;u8 len3;u8 len4;u8 len5; -u16 tf1;/* transfer 1 length */ -u16 tf2;/* transfer 2 length */ +__le16 tf1;/* transfer 1 length */ +__le16 tf2;/* transfer 2 length */ } __packed;struct vprbrd_i2c_status {
@@ -89,7 +89,7 @@ struct vprbrd_i2c_addr_msg {u8 cmd;u8 addr;u8 unknown1; -u16 len; +__le16 len;u8 unknown2;u8 unknown3; } __packed;
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
New issues:
- [Low] The commit message description is not capitalized.
Pre-existing issues:
- [Critical] The DMA buffer buf in struct vprbrd shares a cacheline with struct mutex lock, leading to potential memory corruption during USB IN transfers.
- [High] The return value of i2c_add_adapter() is not checked in the probe function.
- [High] vprbrd_i2c_xfer() returns 0 on success instead of the number of messages successfully processed.
- [High] vprbrd_i2c_receive() uses the same struct pointer for the outgoing request header and the incoming data buffer, overwriting the header and breaking multi-chunk reads. --
--- Patch [1]: [PATCH v2] mfd: viperboard: fix native fields type in structures as little-endiand --- commit a510e96109198a25ffd2275333a8ed6cfaf2b401
Author: Mikhail Lukianchikov <[email protected]>
mfd: viperboard: fix native fields type in structures as little-endiand
This commit updates several structure fields in viperboard.h to use little-endian types to resolve sparse warnings.
Quoted 1 line
mfd: viperboard: fix native fields type in structures as little-endiand
[Severity: Low]
Does this commit message description violate the MFD subsystem formatting guidelines by not capitalizing the description after the prefix?
Quoted 4 lines
include/linux/mfd/viperboard.h +0
-0
[ ... ]
include/linux/mfd/viperboard.h struct vprbrd_i2c_addr_msg { · line 100
Quoted 9 lines
include/linux/mfd/viperboard.h +1
-1@@ -94,7 +94,7 @@ struct vprbrd_i2c_addr_msg {u8 cmd;u8 addr;u8 unknown1; -u16 len; +__le16 len;u8 unknown2;u8 unknown3; } __packed;
[Severity: Critical]
This is a pre-existing issue, but does this struct definition later in the file share a cacheline between the DMA buffer and the mutex?
include/linux/mfd/viperboard.h:struct vprbrd {
...
struct mutex lock;
u8 buf[sizeof(struct vprbrd_i2c_write_msg)];
...
}If a USB IN transfer is initiated in vprbrd_i2c_receive(), it maps this unaligned buffer for DMA.
Wouldn't a concurrent modification to the mutex (like adding to the wait queue) be discarded by the CPU's cacheline invalidation on non-cache-coherent architectures, corrupting the mutex state and causing a panic?
[Severity: High]
This isn't a bug introduced by this patch, but doesn't this code ignore the mandatory return value of i2c_add_adapter() during probe?
drivers/i2c/busses/i2c-viperboard.c:vprbrd_i2c_probe() {
...
/* attach to i2c layer */
i2c_add_adapter(&vb_i2c->i2c);
platform_set_drvdata(pdev, vb_i2c);
return 0;
}If i2c_add_adapter() fails, the adapter is not registered, but probe() still returns 0 (success). When the driver is later unbound, won't vprbrd_i2c_remove() unconditionally call i2c_del_adapter() on the unregistered adapter and crash the system?
[Severity: High]
This is a pre-existing issue, but does vprbrd_i2c_xfer() return 0 on success instead of the number of messages processed?
drivers/i2c/busses/i2c-viperboard.c:vprbrd_i2c_xfer() {
...
mutex_unlock(&vb->lock);
}
return 0;
error:
mutex_unlock(&vb->lock);
return error;
}The I2C subsystem requires the master_xfer callback to return the number of successfully transferred messages.
Doesn't returning 0 cause callers like i2c_transfer() to interpret a successful transfer as a failure, breaking I2C client drivers attached to this bus?
[Severity: High]
This isn't a bug introduced by this patch, but does vprbrd_i2c_receive() use the same struct pointer for the outgoing request header and the incoming data buffer?
drivers/i2c/busses/i2c-viperboard.c:vprbrd_i2c_receive() {
...
/* read the actual data */
ret = usb_bulk_msg(usb_dev,
usb_rcvbulkpipe(usb_dev, VPRBRD_EP_IN), rmsg,
bytes_xfer, &bytes_actual, VPRBRD_USB_TIMEOUT_MS);
...
}It receives incoming USB payload data directly into the rmsg pointer at offset 0, which overwrites rmsg->header.
If an I2C read message has len > 512, doesn't it trigger the second chunk transfer in vprbrd_i2c_read() and transmit the corrupted rmsg->header to the device?
drivers/i2c/busses/i2c-viperboard.c:vprbrd_i2c_read() {
...
/* second read transfer if neccessary */
if (len2 > 0) {
ret = vprbrd_i2c_receive(vb->usb_dev, rmsg, len2);
...
}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: (subset) [PATCH v2] mfd: viperboard: fix native fields type in structures as little-endiand
In reply to Mikhail LukianchikovQuoted 8 lines · Mikhail Lukianchikov
Sparse reports several warnings about incorrect type in assignment (different base types) when building the
i2c-viperboard.cdriver:warning: incorrect type in assignment (different base types)
expected unsigned short [usertype] addr
got restricted__le16[usertype][...]
Applied, thanks!
[1/1] mfd: viperboard: fix native fields type in structures as little-endiand
commit: d97a98f434af98c1a46f5dc98b0f861187c52f63
--
Lee Jones [李琼斯]