Re: [PATCH v13 2/4] usb: add apis for offload usage tracking

Guan-Yu Lin
2025-06-03T08:46:35Z

On Wed, May 28, 2025 at 5:16 PM Greg KH <gregkh@linuxfoundation.org> wrote:
>
> On Wed, May 28, 2025 at 09:04:07AM +0000, Guan-Yu Lin wrote:
> > @@ -2036,6 +2036,131 @@ int usb_disable_usb2_hardware_lpm(struct usb_device *udev)
> >
> > #endif /* CONFIG_PM */
> >
> > +#if IS_ENABLED(CONFIG_USB_XHCI_SIDEBAND_SUSPEND)
>
> ifdef in .c files are messy and hard to maintain.
>
> Also, why is an xhci-specific option enabling/disabling core USB
> functions like this? Shouldn't it be a generic USB config option name
> instead?
>

To address the above 2 suggestions, I'll move related codes into a new
file offload.c, and create a new config to manage them.

> > +int usb_offload_get(struct usb_device *udev)
> > +{
> > + int ret;
> > +
> > + usb_lock_device(udev);
> > + if (udev->state == USB_STATE_NOTATTACHED) {
> > + ret = -ENODEV;
> > + goto unlock_device;
>
> Shouldn't we using the guard logic here instead? That would make all of
> this look much simpler and easier to maintain over time.
>

Thanks for the suggestion. Guard logics would be adapted in the next version.

> > +
> > + if (ret < 0)
>
> Why the blank line?
>

I'll remove the blank line. Thanks for the heads up.

> > +bool usb_offload_check(struct usb_device *udev)
> > +{
> > + struct usb_device *child;
> > + bool active;
> > + int port1;
> > +
> > + usb_hub_for_each_child(udev, port1, child) {
> > + usb_lock_device(child);
> > + active = usb_offload_check(child);
> > + usb_unlock_device(child);
> > + if (active)
> > + return true;
> > + }
> > +
> > + return !!udev->offload_usage;
>
> I think you forgot to mark this function as requiring that the lock be
> held, right? Just documenting it isn't going to be simple to notice or
> maintain over time...
>
> thanks,
>
> greg k-h

__must_hold marco would be added in the next version. Thanks for the heads up.

Regards,
Guan-Yu