Skip to content

Commit ea2f35c

Browse files
tmlindgregkh
authored andcommitted
usb: musb: Fix sleeping function called from invalid context for hdrc glue
Commit 65b3f50 ("usb: musb: Add PM runtime support for MUSB DSPS glue layer") wrongly added a call for pm_runtime_get_sync to otg_timer that runs in softirq context. That causes a "BUG: sleeping function called from invalid context" every time when polling the cable status: [<c015ebb4>] (__might_sleep) from [<c0413d60>] (__pm_runtime_resume+0x9c/0xa0) [<c0413d60>] (__pm_runtime_resume) from [<c04d0bc4>] (otg_timer+0x3c/0x254) [<c04d0bc4>] (otg_timer) from [<c0191180>] (call_timer_fn+0xfc/0x41c) [<c0191180>] (call_timer_fn) from [<c01915c0>] (expire_timers+0x120/0x210) [<c01915c0>] (expire_timers) from [<c0191acc>] (run_timer_softirq+0xa4/0xdc) [<c0191acc>] (run_timer_softirq) from [<c010168c>] (__do_softirq+0x12c/0x594) I did not notice that as I did not have CONFIG_DEBUG_ATOMIC_SLEEP enabled. And looks like also musb_gadget_queue() suffers from the same problem. Let's fix the issue by using a list of delayed work then call it on resume. Note that we want to do this only when musb core and it's parent devices are awake, and we need to make sure the DSPS glue timer is stopped as noted by Johan Hovold <[email protected]>. Note that we already are re-enabling the timer with mod_timer() in dsps_musb_enable(). Later on we may be able to remove other delayed work in the musb driver and just do it from pending_resume_work. But this should be done only for delayed work that does not have other timing requirements beyond just being run on resume. Fixes: 65b3f50 ("usb: musb: Add PM runtime support for MUSB DSPS glue layer") Reported-by: Johan Hovold <[email protected]> Reviewed-by: Johan Hovold <[email protected]> Tested-by: Laurent Pinchart <[email protected]> Signed-off-by: Tony Lindgren <[email protected]> Signed-off-by: Bin Liu <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent c723bd6 commit ea2f35c

File tree

4 files changed

+167
-18
lines changed

4 files changed

+167
-18
lines changed

drivers/usb/musb/musb_core.c

+105-4
Original file line numberDiff line numberDiff line change
@@ -1969,6 +1969,7 @@ static struct musb *allocate_instance(struct device *dev,
19691969
INIT_LIST_HEAD(&musb->control);
19701970
INIT_LIST_HEAD(&musb->in_bulk);
19711971
INIT_LIST_HEAD(&musb->out_bulk);
1972+
INIT_LIST_HEAD(&musb->pending_list);
19721973

19731974
musb->vbuserr_retry = VBUSERR_RETRY_COUNT;
19741975
musb->a_wait_bcon = OTG_TIME_A_WAIT_BCON;
@@ -2018,6 +2019,84 @@ static void musb_free(struct musb *musb)
20182019
musb_host_free(musb);
20192020
}
20202021

2022+
struct musb_pending_work {
2023+
int (*callback)(struct musb *musb, void *data);
2024+
void *data;
2025+
struct list_head node;
2026+
};
2027+
2028+
/*
2029+
* Called from musb_runtime_resume(), musb_resume(), and
2030+
* musb_queue_resume_work(). Callers must take musb->lock.
2031+
*/
2032+
static int musb_run_resume_work(struct musb *musb)
2033+
{
2034+
struct musb_pending_work *w, *_w;
2035+
unsigned long flags;
2036+
int error = 0;
2037+
2038+
spin_lock_irqsave(&musb->list_lock, flags);
2039+
list_for_each_entry_safe(w, _w, &musb->pending_list, node) {
2040+
if (w->callback) {
2041+
error = w->callback(musb, w->data);
2042+
if (error < 0) {
2043+
dev_err(musb->controller,
2044+
"resume callback %p failed: %i\n",
2045+
w->callback, error);
2046+
}
2047+
}
2048+
list_del(&w->node);
2049+
devm_kfree(musb->controller, w);
2050+
}
2051+
spin_unlock_irqrestore(&musb->list_lock, flags);
2052+
2053+
return error;
2054+
}
2055+
2056+
/*
2057+
* Called to run work if device is active or else queue the work to happen
2058+
* on resume. Caller must take musb->lock and must hold an RPM reference.
2059+
*
2060+
* Note that we cowardly refuse queuing work after musb PM runtime
2061+
* resume is done calling musb_run_resume_work() and return -EINPROGRESS
2062+
* instead.
2063+
*/
2064+
int musb_queue_resume_work(struct musb *musb,
2065+
int (*callback)(struct musb *musb, void *data),
2066+
void *data)
2067+
{
2068+
struct musb_pending_work *w;
2069+
unsigned long flags;
2070+
int error;
2071+
2072+
if (WARN_ON(!callback))
2073+
return -EINVAL;
2074+
2075+
if (pm_runtime_active(musb->controller))
2076+
return callback(musb, data);
2077+
2078+
w = devm_kzalloc(musb->controller, sizeof(*w), GFP_ATOMIC);
2079+
if (!w)
2080+
return -ENOMEM;
2081+
2082+
w->callback = callback;
2083+
w->data = data;
2084+
spin_lock_irqsave(&musb->list_lock, flags);
2085+
if (musb->is_runtime_suspended) {
2086+
list_add_tail(&w->node, &musb->pending_list);
2087+
error = 0;
2088+
} else {
2089+
dev_err(musb->controller, "could not add resume work %p\n",
2090+
callback);
2091+
devm_kfree(musb->controller, w);
2092+
error = -EINPROGRESS;
2093+
}
2094+
spin_unlock_irqrestore(&musb->list_lock, flags);
2095+
2096+
return error;
2097+
}
2098+
EXPORT_SYMBOL_GPL(musb_queue_resume_work);
2099+
20212100
static void musb_deassert_reset(struct work_struct *work)
20222101
{
20232102
struct musb *musb;
@@ -2065,6 +2144,7 @@ musb_init_controller(struct device *dev, int nIrq, void __iomem *ctrl)
20652144
}
20662145

20672146
spin_lock_init(&musb->lock);
2147+
spin_lock_init(&musb->list_lock);
20682148
musb->board_set_power = plat->set_power;
20692149
musb->min_power = plat->min_power;
20702150
musb->ops = plat->platform_ops;
@@ -2558,6 +2638,7 @@ static int musb_suspend(struct device *dev)
25582638

25592639
musb_platform_disable(musb);
25602640
musb_generic_disable(musb);
2641+
WARN_ON(!list_empty(&musb->pending_list));
25612642

25622643
spin_lock_irqsave(&musb->lock, flags);
25632644

@@ -2579,9 +2660,11 @@ static int musb_suspend(struct device *dev)
25792660

25802661
static int musb_resume(struct device *dev)
25812662
{
2582-
struct musb *musb = dev_to_musb(dev);
2583-
u8 devctl;
2584-
u8 mask;
2663+
struct musb *musb = dev_to_musb(dev);
2664+
unsigned long flags;
2665+
int error;
2666+
u8 devctl;
2667+
u8 mask;
25852668

25862669
/*
25872670
* For static cmos like DaVinci, register values were preserved
@@ -2615,6 +2698,13 @@ static int musb_resume(struct device *dev)
26152698

26162699
musb_start(musb);
26172700

2701+
spin_lock_irqsave(&musb->lock, flags);
2702+
error = musb_run_resume_work(musb);
2703+
if (error)
2704+
dev_err(musb->controller, "resume work failed with %i\n",
2705+
error);
2706+
spin_unlock_irqrestore(&musb->lock, flags);
2707+
26182708
return 0;
26192709
}
26202710

@@ -2623,13 +2713,16 @@ static int musb_runtime_suspend(struct device *dev)
26232713
struct musb *musb = dev_to_musb(dev);
26242714

26252715
musb_save_context(musb);
2716+
musb->is_runtime_suspended = 1;
26262717

26272718
return 0;
26282719
}
26292720

26302721
static int musb_runtime_resume(struct device *dev)
26312722
{
2632-
struct musb *musb = dev_to_musb(dev);
2723+
struct musb *musb = dev_to_musb(dev);
2724+
unsigned long flags;
2725+
int error;
26332726

26342727
/*
26352728
* When pm_runtime_get_sync called for the first time in driver
@@ -2651,6 +2744,14 @@ static int musb_runtime_resume(struct device *dev)
26512744
msecs_to_jiffies(USB_RESUME_TIMEOUT));
26522745
}
26532746

2747+
spin_lock_irqsave(&musb->lock, flags);
2748+
error = musb_run_resume_work(musb);
2749+
if (error)
2750+
dev_err(musb->controller, "resume work failed with %i\n",
2751+
error);
2752+
musb->is_runtime_suspended = 0;
2753+
spin_unlock_irqrestore(&musb->lock, flags);
2754+
26542755
return 0;
26552756
}
26562757

drivers/usb/musb/musb_core.h

+7
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ struct musb_context_registers {
303303
struct musb {
304304
/* device lock */
305305
spinlock_t lock;
306+
spinlock_t list_lock; /* resume work list lock */
306307

307308
struct musb_io io;
308309
const struct musb_platform_ops *ops;
@@ -337,6 +338,7 @@ struct musb {
337338
struct list_head control; /* of musb_qh */
338339
struct list_head in_bulk; /* of musb_qh */
339340
struct list_head out_bulk; /* of musb_qh */
341+
struct list_head pending_list; /* pending work list */
340342

341343
struct timer_list otg_timer;
342344
struct notifier_block nb;
@@ -386,6 +388,7 @@ struct musb {
386388
unsigned long idle_timeout; /* Next timeout in jiffies */
387389

388390
unsigned is_initialized:1;
391+
unsigned is_runtime_suspended:1;
389392

390393
/* active means connected and not suspended */
391394
unsigned is_active:1;
@@ -542,6 +545,10 @@ extern irqreturn_t musb_interrupt(struct musb *);
542545

543546
extern void musb_hnp_stop(struct musb *musb);
544547

548+
int musb_queue_resume_work(struct musb *musb,
549+
int (*callback)(struct musb *musb, void *data),
550+
void *data);
551+
545552
static inline void musb_platform_set_vbus(struct musb *musb, int is_on)
546553
{
547554
if (musb->ops->set_vbus)

drivers/usb/musb/musb_dsps.c

+26-10
Original file line numberDiff line numberDiff line change
@@ -185,24 +185,19 @@ static void dsps_musb_disable(struct musb *musb)
185185
musb_writel(reg_base, wrp->coreintr_clear, wrp->usb_bitmap);
186186
musb_writel(reg_base, wrp->epintr_clear,
187187
wrp->txep_bitmap | wrp->rxep_bitmap);
188+
del_timer_sync(&glue->timer);
188189
musb_writeb(musb->mregs, MUSB_DEVCTL, 0);
189190
}
190191

191-
static void otg_timer(unsigned long _musb)
192+
/* Caller must take musb->lock */
193+
static int dsps_check_status(struct musb *musb, void *unused)
192194
{
193-
struct musb *musb = (void *)_musb;
194195
void __iomem *mregs = musb->mregs;
195196
struct device *dev = musb->controller;
196197
struct dsps_glue *glue = dev_get_drvdata(dev->parent);
197198
const struct dsps_musb_wrapper *wrp = glue->wrp;
198199
u8 devctl;
199-
unsigned long flags;
200200
int skip_session = 0;
201-
int err;
202-
203-
err = pm_runtime_get_sync(dev);
204-
if (err < 0)
205-
dev_err(dev, "Poll could not pm_runtime_get: %i\n", err);
206201

207202
/*
208203
* We poll because DSPS IP's won't expose several OTG-critical
@@ -212,7 +207,6 @@ static void otg_timer(unsigned long _musb)
212207
dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl,
213208
usb_otg_state_string(musb->xceiv->otg->state));
214209

215-
spin_lock_irqsave(&musb->lock, flags);
216210
switch (musb->xceiv->otg->state) {
217211
case OTG_STATE_A_WAIT_VRISE:
218212
mod_timer(&glue->timer, jiffies +
@@ -245,8 +239,30 @@ static void otg_timer(unsigned long _musb)
245239
default:
246240
break;
247241
}
248-
spin_unlock_irqrestore(&musb->lock, flags);
249242

243+
return 0;
244+
}
245+
246+
static void otg_timer(unsigned long _musb)
247+
{
248+
struct musb *musb = (void *)_musb;
249+
struct device *dev = musb->controller;
250+
unsigned long flags;
251+
int err;
252+
253+
err = pm_runtime_get(dev);
254+
if ((err != -EINPROGRESS) && err < 0) {
255+
dev_err(dev, "Poll could not pm_runtime_get: %i\n", err);
256+
pm_runtime_put_noidle(dev);
257+
258+
return;
259+
}
260+
261+
spin_lock_irqsave(&musb->lock, flags);
262+
err = musb_queue_resume_work(musb, dsps_check_status, NULL);
263+
if (err < 0)
264+
dev_err(dev, "%s resume work: %i\n", __func__, err);
265+
spin_unlock_irqrestore(&musb->lock, flags);
250266
pm_runtime_mark_last_busy(dev);
251267
pm_runtime_put_autosuspend(dev);
252268
}

drivers/usb/musb/musb_gadget.c

+29-4
Original file line numberDiff line numberDiff line change
@@ -1222,13 +1222,22 @@ void musb_ep_restart(struct musb *musb, struct musb_request *req)
12221222
rxstate(musb, req);
12231223
}
12241224

1225+
static int musb_ep_restart_resume_work(struct musb *musb, void *data)
1226+
{
1227+
struct musb_request *req = data;
1228+
1229+
musb_ep_restart(musb, req);
1230+
1231+
return 0;
1232+
}
1233+
12251234
static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
12261235
gfp_t gfp_flags)
12271236
{
12281237
struct musb_ep *musb_ep;
12291238
struct musb_request *request;
12301239
struct musb *musb;
1231-
int status = 0;
1240+
int status;
12321241
unsigned long lockflags;
12331242

12341243
if (!ep || !req)
@@ -1245,6 +1254,17 @@ static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
12451254
if (request->ep != musb_ep)
12461255
return -EINVAL;
12471256

1257+
status = pm_runtime_get(musb->controller);
1258+
if ((status != -EINPROGRESS) && status < 0) {
1259+
dev_err(musb->controller,
1260+
"pm runtime get failed in %s\n",
1261+
__func__);
1262+
pm_runtime_put_noidle(musb->controller);
1263+
1264+
return status;
1265+
}
1266+
status = 0;
1267+
12481268
trace_musb_req_enq(request);
12491269

12501270
/* request is mine now... */
@@ -1255,7 +1275,6 @@ static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
12551275

12561276
map_dma_buffer(request, musb, musb_ep);
12571277

1258-
pm_runtime_get_sync(musb->controller);
12591278
spin_lock_irqsave(&musb->lock, lockflags);
12601279

12611280
/* don't queue if the ep is down */
@@ -1271,8 +1290,14 @@ static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
12711290
list_add_tail(&request->list, &musb_ep->req_list);
12721291

12731292
/* it this is the head of the queue, start i/o ... */
1274-
if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
1275-
musb_ep_restart(musb, request);
1293+
if (!musb_ep->busy && &request->list == musb_ep->req_list.next) {
1294+
status = musb_queue_resume_work(musb,
1295+
musb_ep_restart_resume_work,
1296+
request);
1297+
if (status < 0)
1298+
dev_err(musb->controller, "%s resume work: %i\n",
1299+
__func__, status);
1300+
}
12761301

12771302
unlock:
12781303
spin_unlock_irqrestore(&musb->lock, lockflags);

0 commit comments

Comments
 (0)