5454#include "acpiphp.h"
5555
5656static LIST_HEAD (bridge_list );
57+ static DEFINE_MUTEX (bridge_mutex );
5758
5859#define MY_NAME "acpiphp_glue"
5960
6061static void handle_hotplug_event_bridge (acpi_handle , u32 , void * );
6162static void acpiphp_sanitize_bus (struct pci_bus * bus );
6263static void acpiphp_set_hpp_values (struct pci_bus * bus );
6364static void handle_hotplug_event_func (acpi_handle handle , u32 type , void * context );
65+ static void free_bridge (struct kref * kref );
6466
6567/* callback routine to check for the existence of a pci dock device */
6668static acpi_status
@@ -76,6 +78,39 @@ is_pci_dock_device(acpi_handle handle, u32 lvl, void *context, void **rv)
7678 }
7779}
7880
81+ static inline void get_bridge (struct acpiphp_bridge * bridge )
82+ {
83+ kref_get (& bridge -> ref );
84+ }
85+
86+ static inline void put_bridge (struct acpiphp_bridge * bridge )
87+ {
88+ kref_put (& bridge -> ref , free_bridge );
89+ }
90+
91+ static void free_bridge (struct kref * kref )
92+ {
93+ struct acpiphp_bridge * bridge ;
94+ struct acpiphp_slot * slot , * next ;
95+ struct acpiphp_func * func , * tmp ;
96+
97+ bridge = container_of (kref , struct acpiphp_bridge , ref );
98+
99+ list_for_each_entry_safe (slot , next , & bridge -> slots , node ) {
100+ list_for_each_entry_safe (func , tmp , & slot -> funcs , sibling ) {
101+ kfree (func );
102+ }
103+ kfree (slot );
104+ }
105+
106+ /* Release reference acquired by acpiphp_bridge_handle_to_function() */
107+ if ((bridge -> flags & BRIDGE_HAS_EJ0 ) && bridge -> func )
108+ put_bridge (bridge -> func -> slot -> bridge );
109+ put_device (& bridge -> pci_bus -> dev );
110+ pci_dev_put (bridge -> pci_dev );
111+ kfree (bridge );
112+ }
113+
79114/*
80115 * the _DCK method can do funny things... and sometimes not
81116 * hah-hah funny.
@@ -171,15 +206,14 @@ register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
171206 device = (adr >> 16 ) & 0xffff ;
172207 function = adr & 0xffff ;
173208
174- pdev = pbus -> self ;
209+ pdev = bridge -> pci_dev ;
175210 if (pdev && device_is_managed_by_native_pciehp (pdev ))
176211 return AE_OK ;
177212
178213 newfunc = kzalloc (sizeof (struct acpiphp_func ), GFP_KERNEL );
179214 if (!newfunc )
180215 return AE_NO_MEMORY ;
181216
182- INIT_LIST_HEAD (& newfunc -> sibling );
183217 newfunc -> handle = handle ;
184218 newfunc -> function = function ;
185219
@@ -229,7 +263,9 @@ register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
229263 INIT_LIST_HEAD (& slot -> funcs );
230264 mutex_init (& slot -> crit_sect );
231265
266+ mutex_lock (& bridge_mutex );
232267 list_add_tail (& slot -> node , & bridge -> slots );
268+ mutex_unlock (& bridge_mutex );
233269 bridge -> nr_slots ++ ;
234270
235271 dbg ("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n" ,
@@ -247,7 +283,9 @@ register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
247283 }
248284
249285 newfunc -> slot = slot ;
286+ mutex_lock (& bridge_mutex );
250287 list_add_tail (& newfunc -> sibling , & slot -> funcs );
288+ mutex_unlock (& bridge_mutex );
251289
252290 if (pci_bus_read_dev_vendor_id (pbus , PCI_DEVFN (device , function ),
253291 & val , 60 * 1000 ))
@@ -288,7 +326,9 @@ register_slot(acpi_handle handle, u32 lvl, void *context, void **rv)
288326
289327 err_exit :
290328 bridge -> nr_slots -- ;
329+ mutex_lock (& bridge_mutex );
291330 list_del (& slot -> node );
331+ mutex_unlock (& bridge_mutex );
292332 kfree (slot );
293333 kfree (newfunc );
294334
@@ -313,13 +353,17 @@ static void init_bridge_misc(struct acpiphp_bridge *bridge)
313353 acpi_status status ;
314354
315355 /* must be added to the list prior to calling register_slot */
356+ mutex_lock (& bridge_mutex );
316357 list_add (& bridge -> list , & bridge_list );
358+ mutex_unlock (& bridge_mutex );
317359
318360 /* register all slot objects under this bridge */
319361 status = acpi_walk_namespace (ACPI_TYPE_DEVICE , bridge -> handle , (u32 )1 ,
320362 register_slot , NULL , bridge , NULL );
321363 if (ACPI_FAILURE (status )) {
364+ mutex_lock (& bridge_mutex );
322365 list_del (& bridge -> list );
366+ mutex_unlock (& bridge_mutex );
323367 return ;
324368 }
325369
@@ -349,16 +393,21 @@ static struct acpiphp_func *acpiphp_bridge_handle_to_function(acpi_handle handle
349393{
350394 struct acpiphp_bridge * bridge ;
351395 struct acpiphp_slot * slot ;
352- struct acpiphp_func * func ;
396+ struct acpiphp_func * func = NULL ;
353397
398+ mutex_lock (& bridge_mutex );
354399 list_for_each_entry (bridge , & bridge_list , list ) {
355400 list_for_each_entry (slot , & bridge -> slots , node ) {
356401 list_for_each_entry (func , & slot -> funcs , sibling ) {
357- if (func -> handle == handle )
402+ if (func -> handle == handle ) {
403+ get_bridge (func -> slot -> bridge );
404+ mutex_unlock (& bridge_mutex );
358405 return func ;
406+ }
359407 }
360408 }
361409 }
410+ mutex_unlock (& bridge_mutex );
362411
363412 return NULL ;
364413}
@@ -368,17 +417,22 @@ static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
368417{
369418 struct acpiphp_bridge * bridge ;
370419
420+ mutex_lock (& bridge_mutex );
371421 list_for_each_entry (bridge , & bridge_list , list )
372- if (bridge -> handle == handle )
422+ if (bridge -> handle == handle ) {
423+ get_bridge (bridge );
424+ mutex_unlock (& bridge_mutex );
373425 return bridge ;
426+ }
427+ mutex_unlock (& bridge_mutex );
374428
375429 return NULL ;
376430}
377431
378432static void cleanup_bridge (struct acpiphp_bridge * bridge )
379433{
380- struct acpiphp_slot * slot , * next ;
381- struct acpiphp_func * func , * tmp ;
434+ struct acpiphp_slot * slot ;
435+ struct acpiphp_func * func ;
382436 acpi_status status ;
383437 acpi_handle handle = bridge -> handle ;
384438
@@ -399,8 +453,8 @@ static void cleanup_bridge(struct acpiphp_bridge *bridge)
399453 err ("failed to install interrupt notify handler\n" );
400454 }
401455
402- list_for_each_entry_safe (slot , next , & bridge -> slots , node ) {
403- list_for_each_entry_safe (func , tmp , & slot -> funcs , sibling ) {
456+ list_for_each_entry (slot , & bridge -> slots , node ) {
457+ list_for_each_entry (func , & slot -> funcs , sibling ) {
404458 if (is_dock_device (func -> handle )) {
405459 unregister_hotplug_dock_device (func -> handle );
406460 unregister_dock_notifier (& func -> nb );
@@ -412,18 +466,13 @@ static void cleanup_bridge(struct acpiphp_bridge *bridge)
412466 if (ACPI_FAILURE (status ))
413467 err ("failed to remove notify handler\n" );
414468 }
415- list_del (& func -> sibling );
416- kfree (func );
417469 }
418470 acpiphp_unregister_hotplug_slot (slot );
419- list_del (& slot -> funcs );
420- kfree (slot );
421471 }
422472
423- put_device (& bridge -> pci_bus -> dev );
424- pci_dev_put (bridge -> pci_dev );
473+ mutex_lock (& bridge_mutex );
425474 list_del (& bridge -> list );
426- kfree ( bridge );
475+ mutex_unlock ( & bridge_mutex );
427476}
428477
429478static int power_on_slot (struct acpiphp_slot * slot )
@@ -620,7 +669,6 @@ static int __ref enable_device(struct acpiphp_slot *slot)
620669 struct pci_dev * dev ;
621670 struct pci_bus * bus = slot -> bridge -> pci_bus ;
622671 struct acpiphp_func * func ;
623- int retval = 0 ;
624672 int num , max , pass ;
625673
626674 if (slot -> flags & SLOT_ENABLED )
@@ -680,7 +728,7 @@ static int __ref enable_device(struct acpiphp_slot *slot)
680728
681729
682730 err_exit :
683- return retval ;
731+ return 0 ;
684732}
685733
686734/* return first device in slot, acquiring a reference on it */
@@ -897,6 +945,7 @@ check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
897945 dbg ("%s: re-enumerating slots under %s\n" ,
898946 __func__ , objname );
899947 acpiphp_check_bridge (bridge );
948+ put_bridge (bridge );
900949 }
901950 return AE_OK ;
902951}
@@ -974,6 +1023,7 @@ static void _handle_hotplug_event_bridge(struct work_struct *work)
9741023
9751024 acpi_scan_lock_release ();
9761025 kfree (hp_work ); /* allocated in handle_hotplug_event_bridge */
1026+ put_bridge (bridge );
9771027}
9781028
9791029/**
@@ -987,6 +1037,8 @@ static void _handle_hotplug_event_bridge(struct work_struct *work)
9871037static void handle_hotplug_event_bridge (acpi_handle handle , u32 type ,
9881038 void * context )
9891039{
1040+ struct acpiphp_bridge * bridge = context ;
1041+
9901042 /*
9911043 * Currently the code adds all hotplug events to the kacpid_wq
9921044 * queue when it should add hotplug events to the kacpi_hotplug_wq.
@@ -995,6 +1047,7 @@ static void handle_hotplug_event_bridge(acpi_handle handle, u32 type,
9951047 * For now just re-add this work to the kacpi_hotplug_wq so we
9961048 * don't deadlock on hotplug actions.
9971049 */
1050+ get_bridge (bridge );
9981051 alloc_acpi_hp_work (handle , type , context , _handle_hotplug_event_bridge );
9991052}
10001053
@@ -1049,6 +1102,7 @@ static void _handle_hotplug_event_func(struct work_struct *work)
10491102
10501103 acpi_scan_lock_release ();
10511104 kfree (hp_work ); /* allocated in handle_hotplug_event_func */
1105+ put_bridge (func -> slot -> bridge );
10521106}
10531107
10541108/**
@@ -1062,6 +1116,8 @@ static void _handle_hotplug_event_func(struct work_struct *work)
10621116static void handle_hotplug_event_func (acpi_handle handle , u32 type ,
10631117 void * context )
10641118{
1119+ struct acpiphp_func * func = context ;
1120+
10651121 /*
10661122 * Currently the code adds all hotplug events to the kacpid_wq
10671123 * queue when it should add hotplug events to the kacpi_hotplug_wq.
@@ -1070,6 +1126,7 @@ static void handle_hotplug_event_func(acpi_handle handle, u32 type,
10701126 * For now just re-add this work to the kacpi_hotplug_wq so we
10711127 * don't deadlock on hotplug actions.
10721128 */
1129+ get_bridge (func -> slot -> bridge );
10731130 alloc_acpi_hp_work (handle , type , context , _handle_hotplug_event_func );
10741131}
10751132
@@ -1095,6 +1152,7 @@ void acpiphp_enumerate_slots(struct pci_bus *bus, acpi_handle handle)
10951152 }
10961153
10971154 INIT_LIST_HEAD (& bridge -> slots );
1155+ kref_init (& bridge -> ref );
10981156 bridge -> handle = handle ;
10991157 bridge -> pci_dev = pci_dev_get (bus -> self );
11001158 bridge -> pci_bus = bus ;
@@ -1128,6 +1186,7 @@ void acpiphp_remove_slots(struct pci_bus *bus)
11281186 list_for_each_entry_safe (bridge , tmp , & bridge_list , list )
11291187 if (bridge -> pci_bus == bus ) {
11301188 cleanup_bridge (bridge );
1189+ put_bridge (bridge );
11311190 break ;
11321191 }
11331192}
0 commit comments