Skip to contentSkip to conversation

usb: gadget: uvc: don't pack struct uvcg_extension_unit_descriptor

PATCH In review Rituparna Warwatkar 2 participants 2 messages Aug 1 – Aug 2

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

PATCH 1/1 usb: gadget: uvc: don't pack struct uvcg_extension_unit_descriptor

kmemleak reports the baSourceID and bmControls arrays allocated by the UVC extension-unit configfs attributes as leaked, e.g.:

  BUG: memory leak
  unreferenced object 0xffff888114fee2c0 (size 8):
    __kmalloc_noprof
    uvcg_extension_ba_source_id_store
    configfs_write_iter
    vfs_write
    ksys_write

The arrays are not actually leaked: they are reachable through xu->desc.baSourceID / xu->desc.bmControls and are freed when the extension unit is removed. The problem is that struct uvcg_extension_unit_descriptor is marked __packed, so these two heap pointers are stored at unaligned offsets (22 and 31). kmemleak only scans memory on pointer-aligned boundaries, so it never sees the pointers and reports the arrays as unreferenced.

Unlike the UAPI struct uvc_extension_unit_descriptor, this is a purely in-memory staging structure: baSourceID and bmControls are pointers, not inline arrays, and the wire descriptor is assembled field by field in UVC_COPY_XU_DESCRIPTOR(). Nothing relies on the packed layout, so the __packed attribute is unnecessary and only serves to misalign the pointers.

Drop __packed so the pointers are naturally aligned and visible to kmemleak, silencing the false positive.

Fixes 0525210c9840 ("usb: gadget: uvc: Allow definition of XUs in configfs")
Reported-by [email protected]
---
 drivers/usb/gadget/function/uvc_configfs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/function/uvc_configfs.h +1 -1
index 9391614135e..5a882afbce4 100644
@@ -176,7 +176,7 @@ struct uvcg_extension_unit_descriptor {
        u8 bControlSize;
        u8 *bmControls;
        u8 iExtension;
-} __packed;
+};
 
 struct uvcg_extension {
        struct config_item item;

--
2.47.3

#1 lore raw
Quoted 42 lines · Rituparna Warwatkar

kmemleak reports the baSourceID and bmControls arrays allocated by the UVC extension-unit configfs attributes as leaked, e.g.:

  BUG: memory leak
  unreferenced object 0xffff888114fee2c0 (size 8):
    __kmalloc_noprof
    uvcg_extension_ba_source_id_store
    configfs_write_iter
    vfs_write
    ksys_write

The arrays are not actually leaked: they are reachable through xu->desc.baSourceID / xu->desc.bmControls and are freed when the extension unit is removed. The problem is that struct uvcg_extension_unit_descriptor is marked __packed, so these two heap pointers are stored at unaligned offsets (22 and 31). kmemleak only scans memory on pointer-aligned boundaries, so it never sees the pointers and reports the arrays as unreferenced.

Unlike the UAPI struct uvc_extension_unit_descriptor, this is a purely in-memory staging structure: baSourceID and bmControls are pointers, not inline arrays, and the wire descriptor is assembled field by field in UVC_COPY_XU_DESCRIPTOR(). Nothing relies on the packed layout, so the __packed attribute is unnecessary and only serves to misalign the pointers.

Drop __packed so the pointers are naturally aligned and visible to kmemleak, silencing the false positive.

Fixes 0525210c9840 ("usb: gadget: uvc: Allow definition of XUs in configfs")
Reported-by [email protected]
---
 drivers/usb/gadget/function/uvc_configfs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/usb/gadget/function/uvc_configfs.h +0 -0
index 9391614135e..5a882afbce4 100644

drivers/usb/gadget/function/uvc_configfs.h struct uvcg_extension_unit_descriptor { · line 179

Quoted 5 lines
drivers/usb/gadget/function/uvc_configfs.h +1 -1
@@ -176,7 +176,7 @@ struct uvcg_extension_unit_descriptor {
        u8 bControlSize;
        u8 *bmControls;
        u8 iExtension;
-} __packed;
+};

Why not rearrange the structure so that there is no space in it now instead?

thanks,

greg k-h