Skip to content

Commit f3db22f

Browse files
Keith BuschMatthew Wilcox
authored andcommitted
NVMe: Fix hot cpu notification dead lock
There is a potential dead lock if a cpu event occurs during nvme probe since it registered with hot cpu notification. This fixes the race by having the module register with notification outside of probe rather than have each device register. The actual work is done in a scheduled work queue instead of in the notifier since assigning IO queues has the potential to block if the driver creates additional queues. Signed-off-by: Keith Busch <keith.busch@intel.com> Signed-off-by: Matthew Wilcox <matthew.r.wilcox@intel.com>
1 parent bd67608 commit f3db22f

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

‎drivers/block/nvme-core.c‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ static LIST_HEAD(dev_list);
7373
static struct task_struct *nvme_thread;
7474
static struct workqueue_struct *nvme_workq;
7575
static wait_queue_head_t nvme_kthread_wait;
76+
static struct notifier_block nvme_nb;
7677

7778
static void nvme_reset_failed_dev(struct work_struct *ws);
7879

@@ -2115,14 +2116,25 @@ static size_t db_bar_size(struct nvme_dev *dev, unsigned nr_io_queues)
21152116
return 4096 + ((nr_io_queues + 1) * 8 * dev->db_stride);
21162117
}
21172118

2119+
static void nvme_cpu_workfn(struct work_struct *work)
2120+
{
2121+
struct nvme_dev *dev = container_of(work, struct nvme_dev, cpu_work);
2122+
if (dev->initialized)
2123+
nvme_assign_io_queues(dev);
2124+
}
2125+
21182126
static int nvme_cpu_notify(struct notifier_block *self,
21192127
unsigned long action, void *hcpu)
21202128
{
2121-
struct nvme_dev *dev = container_of(self, struct nvme_dev, nb);
2129+
struct nvme_dev *dev;
2130+
21222131
switch (action) {
21232132
case CPU_ONLINE:
21242133
case CPU_DEAD:
2125-
nvme_assign_io_queues(dev);
2134+
spin_lock(&dev_list_lock);
2135+
list_for_each_entry(dev, &dev_list, node)
2136+
schedule_work(&dev->cpu_work);
2137+
spin_unlock(&dev_list_lock);
21262138
break;
21272139
}
21282140
return NOTIFY_OK;
@@ -2191,11 +2203,6 @@ static int nvme_setup_io_queues(struct nvme_dev *dev)
21912203
nvme_free_queues(dev, nr_io_queues + 1);
21922204
nvme_assign_io_queues(dev);
21932205

2194-
dev->nb.notifier_call = &nvme_cpu_notify;
2195-
result = register_hotcpu_notifier(&dev->nb);
2196-
if (result)
2197-
goto free_queues;
2198-
21992206
return 0;
22002207

22012208
free_queues:
@@ -2495,8 +2502,6 @@ static void nvme_dev_shutdown(struct nvme_dev *dev)
24952502
int i;
24962503

24972504
dev->initialized = 0;
2498-
unregister_hotcpu_notifier(&dev->nb);
2499-
25002505
nvme_dev_list_remove(dev);
25012506

25022507
if (!dev->bar || (dev->bar && readl(&dev->bar->csts) == -1)) {
@@ -2767,6 +2772,7 @@ static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id)
27672772
INIT_LIST_HEAD(&dev->namespaces);
27682773
dev->reset_workfn = nvme_reset_failed_dev;
27692774
INIT_WORK(&dev->reset_work, nvme_reset_workfn);
2775+
INIT_WORK(&dev->cpu_work, nvme_cpu_workfn);
27702776
dev->pci_dev = pdev;
27712777
pci_set_drvdata(pdev, dev);
27722778
result = nvme_set_instance(dev);
@@ -2836,6 +2842,7 @@ static void nvme_remove(struct pci_dev *pdev)
28362842

28372843
pci_set_drvdata(pdev, NULL);
28382844
flush_work(&dev->reset_work);
2845+
flush_work(&dev->cpu_work);
28392846
misc_deregister(&dev->miscdev);
28402847
nvme_dev_remove(dev);
28412848
nvme_dev_shutdown(dev);
@@ -2923,11 +2930,18 @@ static int __init nvme_init(void)
29232930
else if (result > 0)
29242931
nvme_major = result;
29252932

2926-
result = pci_register_driver(&nvme_driver);
2933+
nvme_nb.notifier_call = &nvme_cpu_notify;
2934+
result = register_hotcpu_notifier(&nvme_nb);
29272935
if (result)
29282936
goto unregister_blkdev;
2937+
2938+
result = pci_register_driver(&nvme_driver);
2939+
if (result)
2940+
goto unregister_hotcpu;
29292941
return 0;
29302942

2943+
unregister_hotcpu:
2944+
unregister_hotcpu_notifier(&nvme_nb);
29312945
unregister_blkdev:
29322946
unregister_blkdev(nvme_major, "nvme");
29332947
kill_workq:
@@ -2938,6 +2952,7 @@ static int __init nvme_init(void)
29382952
static void __exit nvme_exit(void)
29392953
{
29402954
pci_unregister_driver(&nvme_driver);
2955+
unregister_hotcpu_notifier(&nvme_nb);
29412956
unregister_blkdev(nvme_major, "nvme");
29422957
destroy_workqueue(nvme_workq);
29432958
BUG_ON(nvme_thread && !IS_ERR(nvme_thread));

‎include/linux/nvme.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ struct nvme_dev {
9090
struct miscdevice miscdev;
9191
work_func_t reset_workfn;
9292
struct work_struct reset_work;
93-
struct notifier_block nb;
93+
struct work_struct cpu_work;
9494
char name[12];
9595
char serial[20];
9696
char model[40];

0 commit comments

Comments
 (0)