1 From c2be6f93b383c873a4f9d521afa49b1b67d06085 Mon Sep 17 00:00:00 2001
2 From: Yijing Wang <wangyijing@huawei.com>
3 Date: Fri, 11 Jan 2013 10:15:54 +0800
4 Subject: PCI: pciehp: Use per-slot workqueues to avoid deadlock
6 From: Yijing Wang <wangyijing@huawei.com>
8 commit c2be6f93b383c873a4f9d521afa49b1b67d06085 upstream.
10 When we have a hotplug-capable PCIe port with a second hotplug-capable
11 PCIe port below it, removing the device below the upstream port causes
14 The deadlock happens because we use the pciehp_wq workqueue to run
15 pciehp_power_thread(), which uses pciehp_disable_slot() to remove devices
16 below the upstream port. When we remove the downstream PCIe port, we call
17 pciehp_remove(), the pciehp driver's .remove() method. That calls
18 flush_workqueue(pciehp_wq), which deadlocks because the
19 pciehp_power_thread() work item is still running.
21 This patch avoids the deadlock by creating a workqueue for every PCIe port
22 and removing the single shared workqueue.
24 Here's the call path that leads to the deadlock:
26 pciehp_queue_pushbutton_work
27 queue_work(pciehp_wq) # queue pciehp_power_thread
33 pciehp_unconfigure_device
34 pci_stop_and_remove_bus_device
36 pciehp_remove # pciehp driver .remove method
39 flush_workqueue(pciehp_wq)
41 This is fairly urgent because it can be caused by simply unplugging a
42 Thunderbolt adapter, as reported by Daniel below.
45 Reference: http://lkml.kernel.org/r/CAMVG2ssiRgcTD1bej2tkUUfsWmpL5eNtPcNif9va2-Gzb2u8nQ@mail.gmail.com
46 Reported-and-tested-by: Daniel J Blueman <daniel@quora.org>
47 Reviewed-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
48 Signed-off-by: Yijing Wang <wangyijing@huawei.com>
49 Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
50 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
53 drivers/pci/hotplug/pciehp.h | 2 +-
54 drivers/pci/hotplug/pciehp_core.c | 11 ++---------
55 drivers/pci/hotplug/pciehp_ctrl.c | 8 ++++----
56 drivers/pci/hotplug/pciehp_hpc.c | 11 ++++++++++-
57 4 files changed, 17 insertions(+), 15 deletions(-)
59 --- a/drivers/pci/hotplug/pciehp.h
60 +++ b/drivers/pci/hotplug/pciehp.h
61 @@ -44,7 +44,6 @@ extern bool pciehp_poll_mode;
62 extern int pciehp_poll_time;
63 extern bool pciehp_debug;
64 extern bool pciehp_force;
65 -extern struct workqueue_struct *pciehp_wq;
67 #define dbg(format, arg...) \
69 @@ -78,6 +77,7 @@ struct slot {
70 struct hotplug_slot *hotplug_slot;
71 struct delayed_work work; /* work for button event */
73 + struct workqueue_struct *wq;
77 --- a/drivers/pci/hotplug/pciehp_core.c
78 +++ b/drivers/pci/hotplug/pciehp_core.c
79 @@ -42,7 +42,6 @@ bool pciehp_debug;
80 bool pciehp_poll_mode;
83 -struct workqueue_struct *pciehp_wq;
85 #define DRIVER_VERSION "0.4"
86 #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
87 @@ -340,18 +339,13 @@ static int __init pcied_init(void)
91 - pciehp_wq = alloc_workqueue("pciehp", 0, 0);
95 pciehp_firmware_init();
96 retval = pcie_port_service_register(&hpdriver_portdrv);
97 dbg("pcie_port_service_register = %d\n", retval);
98 info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
100 - destroy_workqueue(pciehp_wq);
102 dbg("Failure to register service\n");
108 @@ -359,7 +353,6 @@ static void __exit pcied_cleanup(void)
110 dbg("unload_pciehpd()\n");
111 pcie_port_service_unregister(&hpdriver_portdrv);
112 - destroy_workqueue(pciehp_wq);
113 info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
116 --- a/drivers/pci/hotplug/pciehp_ctrl.c
117 +++ b/drivers/pci/hotplug/pciehp_ctrl.c
118 @@ -49,7 +49,7 @@ static int queue_interrupt_event(struct
119 info->p_slot = p_slot;
120 INIT_WORK(&info->work, interrupt_event_handler);
122 - queue_work(pciehp_wq, &info->work);
123 + queue_work(p_slot->wq, &info->work);
127 @@ -344,7 +344,7 @@ void pciehp_queue_pushbutton_work(struct
131 - queue_work(pciehp_wq, &info->work);
132 + queue_work(p_slot->wq, &info->work);
134 mutex_unlock(&p_slot->lock);
136 @@ -377,7 +377,7 @@ static void handle_button_press_event(st
138 pciehp_set_attention_status(p_slot, 0);
140 - queue_delayed_work(pciehp_wq, &p_slot->work, 5*HZ);
141 + queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
143 case BLINKINGOFF_STATE:
144 case BLINKINGON_STATE:
145 @@ -439,7 +439,7 @@ static void handle_surprise_event(struct
147 p_slot->state = POWERON_STATE;
149 - queue_work(pciehp_wq, &info->work);
150 + queue_work(p_slot->wq, &info->work);
153 static void interrupt_event_handler(struct work_struct *work)
154 --- a/drivers/pci/hotplug/pciehp_hpc.c
155 +++ b/drivers/pci/hotplug/pciehp_hpc.c
156 @@ -874,23 +874,32 @@ static void pcie_shutdown_notification(s
157 static int pcie_init_slot(struct controller *ctrl)
162 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
166 + snprintf(name, sizeof(name), "pciehp-%u", PSN(ctrl));
167 + slot->wq = alloc_workqueue(name, 0, 0);
172 mutex_init(&slot->lock);
173 INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
181 static void pcie_cleanup_slot(struct controller *ctrl)
183 struct slot *slot = ctrl->slot;
184 cancel_delayed_work(&slot->work);
185 - flush_workqueue(pciehp_wq);
186 + destroy_workqueue(slot->wq);