Skip to contentSkip to conversation

usb-serial: fix mos_parport refcount imbalance on error path

DISCUSSION Johan Hovold 1 participant 1 message Mar 20, 2019 USB

lore
Share & tools
lore
#0 lore raw
Quoted 3 lines · Lin Yi

write_parport_ref_nonblock increase mos_parport refcount without decrease it when return -ENOMEM code, so need a decrement before function return -ENOMEM.

Good catch!

Your patch looks good, but wouldn't it be better to move the kref_get() (and mos_parport assignment) to the end of the urbtrack initialisation instead?

That way you wouldn't have to add any kref_puts() at all.

Quoted 14 lines
---
 drivers/usb/serial/mos7720.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/usb/serial/mos7720.c +1 -0
index fc52ac7..6abb335 100644
@@ -371,12 +371,14 @@ static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport,
 	urbtrack->urb = usb_alloc_urb(0, GFP_ATOMIC);
 	if (!urbtrack->urb) {
 		kfree(urbtrack);
+		kref_put(&mos_parport->ref_count, destroy_mos_parport);

drivers/usb/serial/mos7720.c static int write_parport_reg_nonblock(struct mos7715_parport *mos_parport, · line 384

Quoted 10 lines
drivers/usb/serial/mos7720.c +1 -0
 		return -ENOMEM;
 	}
 	urbtrack->setup = kmalloc(sizeof(*urbtrack->setup), GFP_ATOMIC);
 	if (!urbtrack->setup) {
 		usb_free_urb(urbtrack->urb);
 		kfree(urbtrack);
+		kref_put(&mos_parport->ref_count, destroy_mos_parport);
 		return -ENOMEM;
 	}
 	urbtrack->setup->bRequestType = (__u8)0x40;

Thanks,
Johan