Oliver Neukum a73fcf
From 38203b8385bf6283537162bde7d499f830964711 Mon Sep 17 00:00:00 2001
Oliver Neukum a73fcf
From: Jerome Brunet <jbrunet@baylibre.com>
Oliver Neukum a73fcf
Date: Mon, 19 Oct 2020 19:07:02 +0200
Oliver Neukum a73fcf
Subject: [PATCH] usb: cdc-acm: fix cooldown mechanism
Oliver Neukum a73fcf
Git-commit: 38203b8385bf6283537162bde7d499f830964711
Oliver Neukum a73fcf
References: git-fixes
Oliver Neukum a73fcf
Patch-mainline: v5.10-rc2
Oliver Neukum a73fcf
Oliver Neukum a73fcf
Commit a4e7279cd1d1 ("cdc-acm: introduce a cool down") is causing
Oliver Neukum a73fcf
regression if there is some USB error, such as -EPROTO.
Oliver Neukum a73fcf
Oliver Neukum a73fcf
This has been reported on some samples of the Odroid-N2 using the Combee II
Oliver Neukum a73fcf
Zibgee USB dongle.
Oliver Neukum a73fcf
Oliver Neukum a73fcf
> struct acm *acm = container_of(work, struct acm, work)
Oliver Neukum a73fcf
Oliver Neukum a73fcf
is incorrect in case of a delayed work and causes warnings, usually from
Oliver Neukum a73fcf
the workqueue:
Oliver Neukum a73fcf
Oliver Neukum a73fcf
> WARNING: CPU: 0 PID: 0 at kernel/workqueue.c:1474 __queue_work+0x480/0x528.
Oliver Neukum a73fcf
Oliver Neukum a73fcf
When this happens, USB eventually stops working completely after a while.
Oliver Neukum a73fcf
Also the ACM_ERROR_DELAY bit is never set, so the cooldown mechanism
Oliver Neukum a73fcf
previously introduced cannot be triggered and acm_submit_read_urb() is
Oliver Neukum a73fcf
never called.
Oliver Neukum a73fcf
Oliver Neukum a73fcf
This changes makes the cdc-acm driver use a single delayed work, fixing the
Oliver Neukum a73fcf
pointer arithmetic in acm_softint() and set the ACM_ERROR_DELAY when the
Oliver Neukum a73fcf
cooldown mechanism appear to be needed.
Oliver Neukum a73fcf
Oliver Neukum a73fcf
Fixes: a4e7279cd1d1 ("cdc-acm: introduce a cool down")
Oliver Neukum a73fcf
Cc: Oliver Neukum <oneukum@suse.com>
Oliver Neukum a73fcf
Reported-by: Pascal Vizeli <pascal.vizeli@nabucasa.com>
Oliver Neukum a73fcf
Acked-by: Oliver Neukum <oneukum@suse.com>
Oliver Neukum a73fcf
Signed-off-by: Jerome Brunet <jbrunet@baylibre.com>
Oliver Neukum a73fcf
Link: https://lore.kernel.org/r/20201019170702.150534-1-jbrunet@baylibre.com
Oliver Neukum a73fcf
Cc: stable <stable@vger.kernel.org>
Oliver Neukum a73fcf
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Oliver Neukum a73fcf
Signed-off-by: Oliver Neukum <oneukum@suse.com>
Oliver Neukum a73fcf
---
Oliver Neukum a73fcf
 drivers/usb/class/cdc-acm.c |   12 +++++-------
Oliver Neukum a73fcf
 drivers/usb/class/cdc-acm.h |    3 +--
Oliver Neukum a73fcf
 2 files changed, 6 insertions(+), 9 deletions(-)
Oliver Neukum a73fcf
Oliver Neukum a73fcf
--- a/drivers/usb/class/cdc-acm.c
Oliver Neukum a73fcf
+++ b/drivers/usb/class/cdc-acm.c
Oliver Neukum a73fcf
@@ -520,6 +520,7 @@ static void acm_read_bulk_callback(struc
Oliver Neukum a73fcf
 			"%s - cooling babbling device\n", __func__);
Oliver Neukum a73fcf
 		usb_mark_last_busy(acm->dev);
Oliver Neukum a73fcf
 		set_bit(rb->index, &acm->urbs_in_error_delay);
Oliver Neukum a73fcf
+		set_bit(ACM_ERROR_DELAY, &acm->flags);
Oliver Neukum a73fcf
 		cooldown = true;
Oliver Neukum a73fcf
 		break;
Oliver Neukum a73fcf
 	default:
Oliver Neukum a73fcf
@@ -545,7 +546,7 @@ static void acm_read_bulk_callback(struc
Oliver Neukum a73fcf
 
Oliver Neukum a73fcf
 	if (stopped || stalled || cooldown) {
Oliver Neukum a73fcf
 		if (stalled)
Oliver Neukum a73fcf
-			schedule_work(&acm->work);
Oliver Neukum a73fcf
+			schedule_delayed_work(&acm->dwork, 0);
Oliver Neukum a73fcf
 		else if (cooldown)
Oliver Neukum a73fcf
 			schedule_delayed_work(&acm->dwork, HZ / 2);
Oliver Neukum a73fcf
 		return;
Oliver Neukum a73fcf
@@ -580,13 +581,13 @@ static void acm_write_bulk(struct urb *u
Oliver Neukum a73fcf
 	acm_write_done(acm, wb);
Oliver Neukum a73fcf
 	spin_unlock_irqrestore(&acm->write_lock, flags);
Oliver Neukum a73fcf
 	set_bit(EVENT_TTY_WAKEUP, &acm->flags);
Oliver Neukum a73fcf
-	schedule_work(&acm->work);
Oliver Neukum a73fcf
+	schedule_delayed_work(&acm->dwork, 0);
Oliver Neukum a73fcf
 }
Oliver Neukum a73fcf
 
Oliver Neukum a73fcf
 static void acm_softint(struct work_struct *work)
Oliver Neukum a73fcf
 {
Oliver Neukum a73fcf
 	int i;
Oliver Neukum a73fcf
-	struct acm *acm = container_of(work, struct acm, work);
Oliver Neukum a73fcf
+	struct acm *acm = container_of(work, struct acm, dwork.work);
Oliver Neukum a73fcf
 
Oliver Neukum a73fcf
 	if (test_bit(EVENT_RX_STALL, &acm->flags)) {
Oliver Neukum a73fcf
 		smp_mb(); /* against acm_suspend() */
Oliver Neukum a73fcf
@@ -602,7 +603,7 @@ static void acm_softint(struct work_stru
Oliver Neukum a73fcf
 	if (test_and_clear_bit(ACM_ERROR_DELAY, &acm->flags)) {
Oliver Neukum a73fcf
 		for (i = 0; i < acm->rx_buflimit; i++)
Oliver Neukum a73fcf
 			if (test_and_clear_bit(i, &acm->urbs_in_error_delay))
Oliver Neukum a73fcf
-					acm_submit_read_urb(acm, i, GFP_NOIO);
Oliver Neukum a73fcf
+				acm_submit_read_urb(acm, i, GFP_KERNEL);
Oliver Neukum a73fcf
 	}
Oliver Neukum a73fcf
 
Oliver Neukum a73fcf
 	if (test_and_clear_bit(EVENT_TTY_WAKEUP, &acm->flags))
Oliver Neukum a73fcf
@@ -1404,7 +1405,6 @@ made_compressed_probe:
Oliver Neukum a73fcf
 	acm->ctrlsize = ctrlsize;
Oliver Neukum a73fcf
 	acm->readsize = readsize;
Oliver Neukum a73fcf
 	acm->rx_buflimit = num_rx_buf;
Oliver Neukum a73fcf
-	INIT_WORK(&acm->work, acm_softint);
Oliver Neukum a73fcf
 	INIT_DELAYED_WORK(&acm->dwork, acm_softint);
Oliver Neukum a73fcf
 	init_waitqueue_head(&acm->wioctl);
Oliver Neukum a73fcf
 	spin_lock_init(&acm->write_lock);
Oliver Neukum a73fcf
@@ -1618,7 +1618,6 @@ static void acm_disconnect(struct usb_in
Oliver Neukum a73fcf
 	}
Oliver Neukum a73fcf
 
Oliver Neukum a73fcf
 	acm_kill_urbs(acm);
Oliver Neukum a73fcf
-	cancel_work_sync(&acm->work);
Oliver Neukum a73fcf
 	cancel_delayed_work_sync(&acm->dwork);
Oliver Neukum a73fcf
 
Oliver Neukum a73fcf
 	tty_unregister_device(acm_tty_driver, acm->minor);
Oliver Neukum a73fcf
@@ -1661,7 +1660,6 @@ static int acm_suspend(struct usb_interf
Oliver Neukum a73fcf
 		return 0;
Oliver Neukum a73fcf
 
Oliver Neukum a73fcf
 	acm_kill_urbs(acm);
Oliver Neukum a73fcf
-	cancel_work_sync(&acm->work);
Oliver Neukum a73fcf
 	cancel_delayed_work_sync(&acm->dwork);
Oliver Neukum a73fcf
 	acm->urbs_in_error_delay = 0;
Oliver Neukum a73fcf
 
Oliver Neukum a73fcf
--- a/drivers/usb/class/cdc-acm.h
Oliver Neukum a73fcf
+++ b/drivers/usb/class/cdc-acm.h
Oliver Neukum a73fcf
@@ -109,8 +109,7 @@ struct acm {
Oliver Neukum a73fcf
 #		define EVENT_RX_STALL	1
Oliver Neukum a73fcf
 #		define ACM_ERROR_DELAY	3
Oliver Neukum a73fcf
 	struct usb_cdc_line_coding line;		/* bits, stop, parity */
Oliver Neukum a73fcf
-	struct work_struct work;			/* work queue entry for various purposes */
Oliver Neukum a73fcf
-	struct delayed_work dwork;			/* for cool downs needed in error recovery */
Oliver Neukum a73fcf
+	struct delayed_work dwork;			/* work queue entry for various purposes */
Oliver Neukum a73fcf
 	unsigned long urbs_in_error_delay;		/* these need to be restarted after a delay */
Oliver Neukum a73fcf
 	unsigned int ctrlin;				/* input control lines (DCD, DSR, RI, break, overruns) */
Oliver Neukum a73fcf
 	unsigned int ctrlout;				/* output control lines (DTR, RTS) */