linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ladislav Michl <ladis@linux-mips.org>
To: linux-usb@vger.kernel.org, linux-serial@vger.kernel.org
Cc: "Felipe Balbi" <balbi@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"Michał Mirosław" <mirq-linux@rere.qmqm.pl>
Subject: [PATCH v2 3/5] usb: gadget: u_serial: Fix console_req access race
Date: Sat, 13 Jul 2019 23:11:22 +0200	[thread overview]
Message-ID: <20190713211122.GD25753@lenoch> (raw)
In-Reply-To: <20190713210853.GA25753@lenoch>

console_req is always null when calling gs_console_connect, so
remove check and put access under con_lock as we are racing with
gs_console_thread.

Signed-off-by: Ladislav Michl <ladis@linux-mips.org>
---
 Changes:
 - v2: New patch

 drivers/usb/gadget/function/u_serial.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c
index cb88a640179e..558a6929ce68 100644
--- a/drivers/usb/gadget/function/u_serial.c
+++ b/drivers/usb/gadget/function/u_serial.c
@@ -930,15 +930,12 @@ static int gs_console_connect(int port_num)
 
 	port = ports[port_num].port;
 	ep = port->port_usb->in;
-	if (!info->console_req) {
-		info->console_req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
-		if (!info->console_req)
-			return -ENOMEM;
-		info->console_req->complete = gs_complete_out;
-	}
-
-	info->port = port;
 	spin_lock(&info->con_lock);
+	info->console_req = gs_alloc_req(ep, ep->maxpacket, GFP_ATOMIC);
+	if (!info->console_req)
+		return -ENOMEM;
+	info->console_req->complete = gs_complete_out;
+	info->port = port;
 	info->req_busy = 0;
 	spin_unlock(&info->con_lock);
 	pr_vdebug("port[%d] console connect!\n", port_num);
@@ -948,10 +945,11 @@ static int gs_console_connect(int port_num)
 static void gs_console_disconnect(struct usb_ep *ep)
 {
 	struct gscons_info *info = &gscons_info;
-	struct usb_request *req = info->console_req;
 
-	gs_free_req(ep, req);
+	spin_lock(&info->con_lock);
+	gs_free_req(ep, info->console_req);
 	info->console_req = NULL;
+	spin_unlock(&info->con_lock);
 }
 
 static int gs_console_thread(void *data)
@@ -1008,7 +1006,6 @@ static int gs_console_setup(struct console *co, char *options)
 	info->port = NULL;
 	info->console_req = NULL;
 	info->req_busy = 0;
-	spin_lock_init(&info->con_lock);
 
 	status = kfifo_alloc(&info->con_buf, GS_CONSOLE_BUF_SIZE, GFP_KERNEL);
 	if (status) {
@@ -1064,6 +1061,9 @@ static struct console gserial_cons = {
 
 static void gserial_console_init(void)
 {
+	struct gscons_info *info = &gscons_info;
+
+	spin_lock_init(&info->con_lock);
 	register_console(&gserial_cons);
 }
 
-- 
2.22.0


  parent reply	other threads:[~2019-07-13 21:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-13 21:08 [PATCH v2 0/5] usb: gadget: u_serial: Fix and cleanup Ladislav Michl
2019-07-13 21:10 ` [PATCH v2 1/5] usb: gadget: u_serial: Fix console_req complete event race Ladislav Michl
2019-07-13 21:10 ` [PATCH v2 2/5] usb: gadget: u_serial: Remove console specific alloc/free req functions Ladislav Michl
2019-07-13 21:11 ` Ladislav Michl [this message]
2019-07-13 21:11 ` [PATCH v2 4/5] usb: gadget: u_serial: Fix starving write Ladislav Michl
2019-07-13 21:12 ` [PATCH v2 5/5] usb: gadget: u_serial: Use bool for req_busy Ladislav Michl
2019-07-14 10:04 ` [PATCH v2 0/5] usb: gadget: u_serial: Fix and cleanup Michał Mirosław
2019-07-14 11:37   ` Michał Mirosław
2019-07-14 13:22     ` Ladislav Michl
2019-07-15 10:40   ` Ladislav Michl

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=20190713211122.GD25753@lenoch \
    --to=ladis@linux-mips.org \
    --cc=balbi@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mirq-linux@rere.qmqm.pl \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).