On Thu, Jul 11, 2024 at 10:18:50PM -0700, Dmitry Torokhov wrote: > Use __free(kfree) cleanup facility when allocating temporary buffers > for USB transfers. > > Signed-off-by: Dmitry Torokhov <dmitry.torokhov@xxxxxxxxx> > --- > drivers/input/touchscreen/usbtouchscreen.c | 90 ++++++++-------------- > 1 file changed, 33 insertions(+), 57 deletions(-) > > diff --git a/drivers/input/touchscreen/usbtouchscreen.c b/drivers/input/touchscreen/usbtouchscreen.c > index 0015f0a6de01..7567efabe014 100644 > --- a/drivers/input/touchscreen/usbtouchscreen.c > +++ b/drivers/input/touchscreen/usbtouchscreen.c > @@ -167,9 +167,8 @@ static const struct usbtouch_device_info e2i_dev_info = { > > static int egalax_init(struct usbtouch_usb *usbtouch) > { > - int ret, i; > - unsigned char *buf; > struct usb_device *udev = interface_to_usbdev(usbtouch->interface); > + int ret, i; > > /* > * An eGalax diagnostic packet kicks the device into using the right > @@ -177,7 +176,7 @@ static int egalax_init(struct usbtouch_usb *usbtouch) > * read later and ignored. > */ > > - buf = kmalloc(3, GFP_KERNEL); > + u8 *buf __free(kfree) = kmalloc(3, GFP_KERNEL); > if (!buf) > return -ENOMEM; > > @@ -191,17 +190,11 @@ static int egalax_init(struct usbtouch_usb *usbtouch) > USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, > 0, 0, buf, 3, > USB_CTRL_SET_TIMEOUT); > - if (ret >= 0) { > - ret = 0; > - break; > - } > if (ret != -EPIPE) > break; > } > > - kfree(buf); > - > - return ret; > + return ret < 0 ? ret : 0; Personally I hate ? : usage, but it's your subsystem and it's used elsewhere in this file, so you have to live with it :) > } > > static int egalax_read_data(struct usbtouch_usb *dev, unsigned char *pkt) > @@ -358,10 +351,9 @@ static int mtouch_get_fw_revision(struct usbtouch_usb *usbtouch) > { > struct usb_device *udev = interface_to_usbdev(usbtouch->interface); > struct mtouch_priv *priv = usbtouch->priv; > - u8 *buf; > int ret; > > - buf = kzalloc(MTOUCHUSB_REQ_CTRLLR_ID_LEN, GFP_NOIO); > + u8 *buf __free(kfree) = kzalloc(MTOUCHUSB_REQ_CTRLLR_ID_LEN, GFP_NOIO); > if (!buf) > return -ENOMEM; > > @@ -373,18 +365,13 @@ static int mtouch_get_fw_revision(struct usbtouch_usb *usbtouch) > if (ret != MTOUCHUSB_REQ_CTRLLR_ID_LEN) { > dev_warn(&usbtouch->interface->dev, > "Failed to read FW rev: %d\n", ret); > - ret = ret < 0 ? ret : -EIO; > - goto free; > + return ret < 0 ? ret : -EIO; > } > > priv->fw_rev_major = buf[3]; > priv->fw_rev_minor = buf[4]; > > - ret = 0; > - > -free: > - kfree(buf); > - return ret; > + return 0; > } > > static int mtouch_alloc(struct usbtouch_usb *usbtouch) > @@ -636,24 +623,23 @@ static const struct usbtouch_device_info gunze_dev_info = { > static int dmc_tsc10_init(struct usbtouch_usb *usbtouch) > { > struct usb_device *dev = interface_to_usbdev(usbtouch->interface); > - int ret = -ENOMEM; > - unsigned char *buf; > + int ret; > > - buf = kmalloc(2, GFP_NOIO); > + u8 *buf __free(kfree) = kmalloc(2, GFP_NOIO); > if (!buf) > - goto err_nobuf; > + return -ENOMEM; > + > /* reset */ > buf[0] = buf[1] = 0xFF; > ret = usb_control_msg(dev, usb_rcvctrlpipe (dev, 0), > - TSC10_CMD_RESET, > - USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, > - 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT); > + TSC10_CMD_RESET, > + USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, > + 0, 0, buf, 2, USB_CTRL_SET_TIMEOUT); Unintentional whitespace change? Anyway, not a big deal, cleanup looks good: Reviewed-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>