All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] usb: Add a lock when freeing data in usbtmc_disconnect
@ 2021-03-23  9:28 Lv Yunlong
  2021-03-23  9:44 ` Greg KH
  0 siblings, 1 reply; 3+ messages in thread
From: Lv Yunlong @ 2021-03-23  9:28 UTC (permalink / raw)
  To: gregkh, linux-usb; +Cc: linux-kernel, Lv Yunlong

In usbtmc_disconnect, data is got from intf with the initial reference.
There is no refcount inc operation before usbmc_free_int(data). In
usbmc_free_int(data), the data may be freed.

But later in usbtmc_disconnect, there is another put function of data.
It could cause errors in race.

My patch adds a lock to protect kref from changing in race.

Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
---
 drivers/usb/class/usbtmc.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
index 74d5a9c5238a..44f1fcabbb1e 100644
--- a/drivers/usb/class/usbtmc.c
+++ b/drivers/usb/class/usbtmc.c
@@ -2493,8 +2493,13 @@ static void usbtmc_disconnect(struct usb_interface *intf)
 		usb_scuttle_anchored_urbs(&file_data->in_anchor);
 	}
 	mutex_unlock(&data->io_mutex);
+
+	spinlock_t *dev_lock = &data->dev_lock;
+
+	spin_lock_irq(dev_lock);
 	usbtmc_free_int(data);
 	kref_put(&data->kref, usbtmc_delete);
+	spin_unlock_irq(dev_lock);
 }
 
 static void usbtmc_draw_down(struct usbtmc_file_data *file_data)
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] usb: Add a lock when freeing data in usbtmc_disconnect
  2021-03-23  9:28 [PATCH v2] usb: Add a lock when freeing data in usbtmc_disconnect Lv Yunlong
@ 2021-03-23  9:44 ` Greg KH
  0 siblings, 0 replies; 3+ messages in thread
From: Greg KH @ 2021-03-23  9:44 UTC (permalink / raw)
  To: Lv Yunlong; +Cc: linux-usb, linux-kernel

On Tue, Mar 23, 2021 at 02:28:54AM -0700, Lv Yunlong wrote:
> In usbtmc_disconnect, data is got from intf with the initial reference.
> There is no refcount inc operation before usbmc_free_int(data). In
> usbmc_free_int(data), the data may be freed.
> 
> But later in usbtmc_disconnect, there is another put function of data.
> It could cause errors in race.
> 
> My patch adds a lock to protect kref from changing in race.
> 
> Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
> ---
>  drivers/usb/class/usbtmc.c | 5 +++++
>  1 file changed, 5 insertions(+)
> 
> diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
> index 74d5a9c5238a..44f1fcabbb1e 100644
> --- a/drivers/usb/class/usbtmc.c
> +++ b/drivers/usb/class/usbtmc.c
> @@ -2493,8 +2493,13 @@ static void usbtmc_disconnect(struct usb_interface *intf)
>  		usb_scuttle_anchored_urbs(&file_data->in_anchor);
>  	}
>  	mutex_unlock(&data->io_mutex);
> +
> +	spinlock_t *dev_lock = &data->dev_lock;
> +
> +	spin_lock_irq(dev_lock);
>  	usbtmc_free_int(data);
>  	kref_put(&data->kref, usbtmc_delete);
> +	spin_unlock_irq(dev_lock);
>  }
>  
>  static void usbtmc_draw_down(struct usbtmc_file_data *file_data)
> -- 
> 2.25.1

You obviously did not even build this patch, let alone test it :(

Please do not waste maintainer's time by not doing the proper steps when
submitting patches.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] usb: Add a lock when freeing data in usbtmc_disconnect
       [not found] <91ca594b3f434040a54485339c0e320f@rohde-schwarz.com>
@ 2021-04-14 15:10 ` guido
  0 siblings, 0 replies; 3+ messages in thread
From: guido @ 2021-04-14 15:10 UTC (permalink / raw)
  To: dpenkler; +Cc: linux-usb, lyl2019, guido.kiener, gregkh


Hi Dave,

I just found this patch, which does not seem to be a correct solution  
to solve a race.
Maybe there is really an issue with the refcounting of data->kref, but  
currently I do
not have time to check this in home office.

At least I see a problem in usbtmc_probe()

After calling:
    /* Protect interrupt in endpoint data until iin_urb is freed */
    kref_get(&data->kref);
the refcounter is incremented again and if usbtmc_probe() fails, the  
counter is
only decremented with a single kref_put(..).

I don't know if this is the reason of Lv Yunglong's problem, but let  
me know if
you have time to track down this issue, and we will work on a correct  
and tested
patch.

Regards,

Guido


> -----Original Message-----
> From: Greg KH
> Sent: Tuesday, March 23, 2021 10:44 AM
> To: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
> Cc: linux-usb@vger.kernel.org; linux-kernel@vger.kernel.org
> Subject: Re: [PATCH v2] usb: Add a lock when freeing data in  
> usbtmc_disconnect
>
> On Tue, Mar 23, 2021 at 02:28:54AM -0700, Lv Yunlong wrote:
>> In usbtmc_disconnect, data is got from intf with the initial reference.
>> There is no refcount inc operation before usbmc_free_int(data). In
>> usbmc_free_int(data), the data may be freed.
>>
>> But later in usbtmc_disconnect, there is another put function of data.
>> It could cause errors in race.
>>
>> My patch adds a lock to protect kref from changing in race.
>>
>> Signed-off-by: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
>> ---
>>  drivers/usb/class/usbtmc.c | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/drivers/usb/class/usbtmc.c b/drivers/usb/class/usbtmc.c
>> index 74d5a9c5238a..44f1fcabbb1e 100644
>> --- a/drivers/usb/class/usbtmc.c
>> +++ b/drivers/usb/class/usbtmc.c
>> @@ -2493,8 +2493,13 @@ static void usbtmc_disconnect(struct  
>> usb_interface *intf)
>>  		usb_scuttle_anchored_urbs(&file_data->in_anchor);
>>  	}
>>  	mutex_unlock(&data->io_mutex);
>> +
>> +	spinlock_t *dev_lock = &data->dev_lock;
>> +
>> +	spin_lock_irq(dev_lock);
>>  	usbtmc_free_int(data);
>>  	kref_put(&data->kref, usbtmc_delete);
>> +	spin_unlock_irq(dev_lock);
>>  }
>>
>>  static void usbtmc_draw_down(struct usbtmc_file_data *file_data)
>> --
>> 2.25.1
>
> You obviously did not even build this patch, let alone test it :(
>
> Please do not waste maintainer's time by not doing the proper steps  
> when submitting patches.
>
> thanks,
>
> greg k-h




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-04-14 15:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-23  9:28 [PATCH v2] usb: Add a lock when freeing data in usbtmc_disconnect Lv Yunlong
2021-03-23  9:44 ` Greg KH
     [not found] <91ca594b3f434040a54485339c0e320f@rohde-schwarz.com>
2021-04-14 15:10 ` guido

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.