All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lv Yunlong <lyl2019@mail.ustc.edu.cn>
To: gregkh@linuxfoundation.org, linux-usb@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Lv Yunlong <lyl2019@mail.ustc.edu.cn>
Subject: [PATCH v2] usb: Add a lock when freeing data in usbtmc_disconnect
Date: Tue, 23 Mar 2021 02:28:54 -0700	[thread overview]
Message-ID: <20210323092854.18911-1-lyl2019@mail.ustc.edu.cn> (raw)

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



             reply	other threads:[~2021-03-23  9:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-23  9:28 Lv Yunlong [this message]
2021-03-23  9:44 ` [PATCH v2] usb: Add a lock when freeing data in usbtmc_disconnect Greg KH
     [not found] <91ca594b3f434040a54485339c0e320f@rohde-schwarz.com>
2021-04-14 15:10 ` guido

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210323092854.18911-1-lyl2019@mail.ustc.edu.cn \
    --to=lyl2019@mail.ustc.edu.cn \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.