Skip to contentSkip to conversation

usb: gadget: Use u16 types for 16-bit fields

PATCH New Kees Cook 1 participant 1 message Jul 9, 2024 USB

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 usb: gadget: Use u16 types for 16-bit fields

Since the beginning of time, struct usb_ep::maxpacket was a bitfield, and when new 16-bit members were added, the convention was followed:

1da177e4c3f41 (Linus Torvalds 2005-04-16 236) unsigned maxpacket:16;
e117e742d3106 (Robert Baldyga 2013-12-13 237) unsigned maxpacket_limit:16;
a59d6b91cbca5 (Tatyana Brokhman 2011-06-28 238) unsigned max_streams:16;

However, there is no need for this as a simple u16 can be used instead, simplifying the struct and the resulting compiler binary output. Switch to u16 for all three, and rearrange struct slightly to minimize holes. No change in the final size of the struct results; the 2 byte gap is just moved to the end, as seen with pahole:

- /* XXX 2 bytes hole, try to pack */

        ...
        /* size: 72, cachelines: 2, members: 15 */
        ...

+ /* padding: 2 */

Changing this simplifies future introspection[1] of maxpacket's type during allocations:

drivers/usb/gadget/function/f_tcm.c:330:24: error: 'typeof' applied to a bit-field

     330 |  fu->cmd.buf = kmalloc(fu->ep_out->maxpacket, GFP_KERNEL);

---
Cc: Greg Kroah-Hartman <[email protected]>
Cc: [email protected]

---
 include/linux/usb/gadget.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
include/linux/usb/gadget.h +5 -5
index 56dda8e1562d..df33333650a0 100644
@@ -229,18 +229,18 @@ struct usb_ep {
 
 	const char		*name;
 	const struct usb_ep_ops	*ops;
+	const struct usb_endpoint_descriptor	*desc;
+	const struct usb_ss_ep_comp_descriptor	*comp_desc;
 	struct list_head	ep_list;
 	struct usb_ep_caps	caps;
 	bool			claimed;
 	bool			enabled;
-	unsigned		maxpacket:16;
-	unsigned		maxpacket_limit:16;
-	unsigned		max_streams:16;
 	unsigned		mult:2;
 	unsigned		maxburst:5;
 	u8			address;
-	const struct usb_endpoint_descriptor	*desc;
-	const struct usb_ss_ep_comp_descriptor	*comp_desc;
+	u16			maxpacket;
+	u16			maxpacket_limit;
+	u16			max_streams;
 };
 
 /*-------------------------------------------------------------------------*/
Signature · 1 line
2.34.1