usb: core: Improve device lifecycle debuggability
v2 cover usb: core: Improve device lifecycle debuggability
This series enhances USB core debuggability. The first patch refactors device state updates into a new update_usb_device_state() helper function, centralizing logic and preparing for tracing.
The second patch adds tracepoints for USB device allocation and state changes, providing better visibility into the device lifecycle.
---
Changes in v2:
- hub.c: Changed the inline function to a standard one.
- Makefile: Moved trace.o to the preceding line.
- trace.h: Changed the speed and state into strings.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Kuen-Han Tsai (2):
usb: core: Centralize device state update logic
usb: core: Add tracepoints for device allocation and state changes
drivers/usb/core/Makefile | 5 +++- drivers/usb/core/hub.c | 30 +++++++++++++---------- drivers/usb/core/trace.c | 6 +++++ drivers/usb/core/trace.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++ drivers/usb/core/usb.c | 2 ++ 5 files changed, 91 insertions(+), 13 deletions(-)
---
base-commit: 5472d60c129f75282d94ae5ad072ee6dfb7c7246
change-id: 20251012-usbcore-tracing-76f00c9b2b3e
Best regards,
Signature · 1 line
Kuen-Han Tsai <[email protected]>
PATCH v2 1/2 usb: core: Centralize device state update logic
Introduce a new static function, update_usb_device_state(), to centralize the process of changing a device's state, including the management of active_duration during suspend/resume transitions.
This change prepares for adding tracepoints, allowing tracing logic to be added in a single, central location within the new helper function.
--- drivers/usb/core/hub.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-)
drivers/usb/core/hub.c +16 -12
@@ -2147,6 +2147,20 @@ static void update_port_device_state(struct usb_device *udev) } } +static void update_usb_device_state (struct usb_device *udev, +enum usb_device_state new_state) +{ +if (udev->state ==USB_STATE_SUSPENDED && + new_state !=USB_STATE_SUSPENDED ) + udev->active_duration -= jiffies; +else if (new_state ==USB_STATE_SUSPENDED && + udev->state !=USB_STATE_SUSPENDED ) + udev->active_duration += jiffies; + + udev->state = new_state; +update_port_device_state (udev); +} +static void recursively_mark_NOTATTACHED (struct usb_device *udev) {struct usb_hub *hub =usb_hub_to_struct_hub (udev);
@@ -2156,10 +2170,7 @@ static void recursively_mark_NOTATTACHED(struct usb_device *udev)if (hub->ports[i]->child)recursively_mark_NOTATTACHED (hub->ports[i]->child); } -if (udev->state ==USB_STATE_SUSPENDED ) - udev->active_duration -= jiffies; - udev->state =USB_STATE_NOTATTACHED ; -update_port_device_state (udev); +update_usb_device_state (udev,USB_STATE_NOTATTACHED ); }/**
@@ -2209,14 +2220,7 @@ void usb_set_device_state(struct usb_device *udev,else wakeup =0 ; } -if (udev->state ==USB_STATE_SUSPENDED && - new_state !=USB_STATE_SUSPENDED ) - udev->active_duration -= jiffies; -else if (new_state ==USB_STATE_SUSPENDED && - udev->state !=USB_STATE_SUSPENDED ) - udev->active_duration += jiffies; - udev->state = new_state; -update_port_device_state (udev); +update_usb_device_state (udev, new_state); }else recursively_mark_NOTATTACHED (udev);spin_unlock_irqrestore (&device_state_lock, flags);
Signature · 1 line
2.51.0.788.g6d19910ace-goog
PATCH v2 2/2 usb: core: Add tracepoints for device allocation and state changes
In reply to Kuen-Han TsaiIntroduce new tracepoints to the USB core to improve debuggability of USB device lifecycle events.
The following tracepoints are added:
- usb_alloc_dev: Triggered when a new USB device structure is allocated, providing insights into early device setup.
- usb_set_device_state: Triggered when the USB device state changes, allowing observation of the device's state transitions.
These tracepoints capture detailed information about the USB device, including its name, speed, state, bus current value, and authorized flag. This will aid developers in diagnosing issues related to device enumeration within the USB subsystem.
Examples:
usb_alloc_dev: usb 1-1 speed UNKNOWN state attached 0mA [authorized]
usb_set_device_state: usb 1-1 speed UNKNOWN state powered 0mA [authorized]
usb_set_device_state: usb 1-1 speed full-speed state default 500mA [authorized]
usb_set_device_state: usb 1-1 speed full-speed state default 500mA [authorized]
usb_set_device_state: usb 1-1 speed full-speed state addressed 500mA [authorized]
usb_set_device_state: usb 1-1 speed full-speed state configured 500mA [authorized]
usb_set_device_state: usb 1-1 speed full-speed state suspended 500mA [authorized]
usb_set_device_state: usb 1-1 speed full-speed state not attached 500mA [authorized]
--- drivers/usb/core/Makefile | 5 +++- drivers/usb/core/hub.c | 2 ++ drivers/usb/core/trace.c | 6 +++++ drivers/usb/core/trace.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++ drivers/usb/core/usb.c | 2 ++ 5 files changed, 75 insertions(+), 1 deletion(-)
drivers/usb/core/Makefile +4 -1
@@ -3,10 +3,13 @@ # Makefile for USB Core files and filesystem # +# define_trace.h needs to know how to find our header +CFLAGS_trace.o := -I$(src) + usbcore-y := usb.o hub.o hcd.o urb.o message.o driver.o usbcore-y += config.o file.o buffer.o sysfs.o endpoint.o usbcore-y += devio.o notify.o generic.o quirks.o devices.o -usbcore-y += phy.o port.o +usbcore-y += phy.o port.o trace.o usbcore-$(CONFIG_OF) += of.o usbcore-$(CONFIG_USB_XHCI_SIDEBAND) += offload.o
drivers/usb/core/hub.c +2 -0
@@ -40,6 +40,7 @@#include "hub.h" #include "phy.h" #include "otg_productlist.h" +#include "trace.h" #define USB_VENDOR_GENESYS_LOGIC 0x05e3#define USB_VENDOR_SMSC 0x0424
@@ -2159,6 +2160,7 @@ static void update_usb_device_state(struct usb_device *udev, udev->state = new_state;update_port_device_state (udev); +trace_usb_set_device_state (udev); }static void recursively_mark_NOTATTACHED (struct usb_device *udev)
drivers/usb/core/trace.c new file +6 -0
@@ -0,0 +1,6 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2025 Google LLC +*/ +#define CREATE_TRACE_POINTS +#include "trace.h"
drivers/usb/core/trace.h new file +61 -0
61 lines not on this page · raw message
drivers/usb/core/usb.c +2 -0
@@ -46,6 +46,7 @@#include <linux/dma-mapping.h> #include "hub.h" +#include "trace.h" const char *usbcore_name ="usbcore" ;
@@ -746,6 +747,7 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,#endif dev->authorized =usb_dev_authorized (dev, usb_hcd); +trace_usb_alloc_dev (dev);return dev; }EXPORT_SYMBOL_GPL (usb_alloc_dev);
Signature · 1 line
2.51.0.788.g6d19910ace-goog
Quoted 9 lines · Kuen-Han Tsai
This series enhances USB core debuggability. The first patch refactors device state updates into a new
update_usb_device_state()helper function, centralizing logic and preparing for tracing.The second patch adds tracepoints for USB device allocation and state changes, providing better visibility into the device lifecycle.
Signed-off-by Kuen-Han Tsai <[email protected]>---
For both patches:
Quoted 25 lines
Changes in v2:
-hub.c: Changed the inline function to a standard one.
-Makefile: Moved trace.o to the preceding line.
-trace.h: Changed the speed and state into strings.
- Link to v1: https://lore.kernel.org/r/[email protected]---
Kuen-Han Tsai (2):
usb: core: Centralize device state update logic
usb: core: Add tracepoints for device allocation and state changesdrivers/usb/core/Makefile | 5 +++- drivers/usb/core/hub.c | 30 +++++++++++++---------- drivers/usb/core/trace.c | 6 +++++ drivers/usb/core/trace.h | 61 +++++++++++++++++++++++++++++++++++++++++++++++ drivers/usb/core/usb.c | 2 ++ 5 files changed, 91 insertions(+), 13 deletions(-)---
base-commit:5472d60c129f75282d94ae5ad072ee6dfb7c7246
change-id: 20251012-usbcore-tracing-76f00c9b2b3eBest regards,
Signature · 1 line
Kuen-Han Tsai <[email protected]>