Skip to content

Commit f6cc609

Browse files
oneukumgregkh
authored andcommitted
UAS: fix deadlock in error handling and PM flushing work
A SCSI error handler and block runtime PM must not allocate memory with GFP_KERNEL. Furthermore they must not wait for tasks allocating memory with GFP_KERNEL. That means that they cannot share a workqueue with arbitrary tasks. Fix this for UAS using a private workqueue. Signed-off-by: Oliver Neukum <[email protected]> Fixes: f9dc024 ("uas: pre_reset and suspend: Fix a few races") Cc: stable <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 5963dec commit f6cc609

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

drivers/usb/storage/uas.c

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,19 @@ static void uas_free_streams(struct uas_dev_info *devinfo);
8181
static void uas_log_cmd_state(struct scsi_cmnd *cmnd, const char *prefix,
8282
int status);
8383

84+
/*
85+
* This driver needs its own workqueue, as we need to control memory allocation.
86+
*
87+
* In the course of error handling and power management uas_wait_for_pending_cmnds()
88+
* needs to flush pending work items. In these contexts we cannot allocate memory
89+
* by doing block IO as we would deadlock. For the same reason we cannot wait
90+
* for anything allocating memory not heeding these constraints.
91+
*
92+
* So we have to control all work items that can be on the workqueue we flush.
93+
* Hence we cannot share a queue and need our own.
94+
*/
95+
static struct workqueue_struct *workqueue;
96+
8497
static void uas_do_work(struct work_struct *work)
8598
{
8699
struct uas_dev_info *devinfo =
@@ -109,7 +122,7 @@ static void uas_do_work(struct work_struct *work)
109122
if (!err)
110123
cmdinfo->state &= ~IS_IN_WORK_LIST;
111124
else
112-
schedule_work(&devinfo->work);
125+
queue_work(workqueue, &devinfo->work);
113126
}
114127
out:
115128
spin_unlock_irqrestore(&devinfo->lock, flags);
@@ -134,7 +147,7 @@ static void uas_add_work(struct uas_cmd_info *cmdinfo)
134147

135148
lockdep_assert_held(&devinfo->lock);
136149
cmdinfo->state |= IS_IN_WORK_LIST;
137-
schedule_work(&devinfo->work);
150+
queue_work(workqueue, &devinfo->work);
138151
}
139152

140153
static void uas_zap_pending(struct uas_dev_info *devinfo, int result)
@@ -1229,7 +1242,31 @@ static struct usb_driver uas_driver = {
12291242
.id_table = uas_usb_ids,
12301243
};
12311244

1232-
module_usb_driver(uas_driver);
1245+
static int __init uas_init(void)
1246+
{
1247+
int rv;
1248+
1249+
workqueue = alloc_workqueue("uas", WQ_MEM_RECLAIM, 0);
1250+
if (!workqueue)
1251+
return -ENOMEM;
1252+
1253+
rv = usb_register(&uas_driver);
1254+
if (rv) {
1255+
destroy_workqueue(workqueue);
1256+
return -ENOMEM;
1257+
}
1258+
1259+
return 0;
1260+
}
1261+
1262+
static void __exit uas_exit(void)
1263+
{
1264+
usb_deregister(&uas_driver);
1265+
destroy_workqueue(workqueue);
1266+
}
1267+
1268+
module_init(uas_init);
1269+
module_exit(uas_exit);
12331270

12341271
MODULE_LICENSE("GPL");
12351272
MODULE_IMPORT_NS(USB_STORAGE);

0 commit comments

Comments
 (0)