2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* ACPI PCI HotPlug glue functions to ACPI CA subsystem
|
|
|
|
*
|
|
|
|
* Copyright (C) 2002,2003 Takayoshi Kochi (t-kochi@bq.jp.nec.com)
|
|
|
|
* Copyright (C) 2002 Hiroshi Aono (h-aono@ap.jp.nec.com)
|
|
|
|
* Copyright (C) 2002,2003 NEC Corporation
|
2005-04-28 07:25:53 +00:00
|
|
|
* Copyright (C) 2003-2005 Matthew Wilcox (matthew.wilcox@hp.com)
|
|
|
|
* Copyright (C) 2003-2005 Hewlett Packard
|
2005-04-28 07:25:56 +00:00
|
|
|
* Copyright (C) 2005 Rajesh Shah (rajesh.shah@intel.com)
|
|
|
|
* Copyright (C) 2005 Intel Corporation
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
|
|
|
|
* NON INFRINGEMENT. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
2006-07-26 17:52:33 +00:00
|
|
|
* Send feedback to <kristen.c.accardi@intel.com>
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-04-28 07:25:53 +00:00
|
|
|
/*
|
|
|
|
* Lifetime rules for pci_dev:
|
|
|
|
* - The one in acpiphp_bridge has its refcount elevated by pci_get_slot()
|
|
|
|
* when the bridge is scanned and it loses a refcount when the bridge
|
|
|
|
* is removed.
|
2009-03-30 16:50:14 +00:00
|
|
|
* - When a P2P bridge is present, we elevate the refcount on the subordinate
|
|
|
|
* bus. It loses the refcount when the the driver unloads.
|
2005-04-28 07:25:53 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/pci.h>
|
2006-10-14 03:05:19 +00:00
|
|
|
#include <linux/pci_hotplug.h>
|
2008-12-17 03:09:12 +00:00
|
|
|
#include <linux/pci-acpi.h>
|
2006-01-13 15:02:15 +00:00
|
|
|
#include <linux/mutex.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2011-09-28 23:40:53 +00:00
|
|
|
#include <linux/acpi.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#include "../pci.h"
|
|
|
|
#include "acpiphp.h"
|
|
|
|
|
|
|
|
static LIST_HEAD(bridge_list);
|
2013-04-12 05:44:28 +00:00
|
|
|
static DEFINE_MUTEX(bridge_mutex);
|
2013-07-13 21:27:24 +00:00
|
|
|
static DEFINE_MUTEX(acpiphp_context_lock);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
#define MY_NAME "acpiphp_glue"
|
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
static void handle_hotplug_event(acpi_handle handle, u32 type, void *data);
|
2005-10-19 00:21:40 +00:00
|
|
|
static void acpiphp_sanitize_bus(struct pci_bus *bus);
|
2009-09-14 22:35:10 +00:00
|
|
|
static void acpiphp_set_hpp_values(struct pci_bus *bus);
|
2013-07-13 21:27:24 +00:00
|
|
|
static void hotplug_event(acpi_handle handle, u32 type, void *data);
|
2013-04-12 05:44:28 +00:00
|
|
|
static void free_bridge(struct kref *kref);
|
2005-10-19 00:21:40 +00:00
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
static void acpiphp_context_handler(acpi_handle handle, void *context)
|
|
|
|
{
|
|
|
|
/* Intentionally empty. */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* acpiphp_init_context - Create hotplug context and grab a reference to it.
|
|
|
|
* @handle: ACPI object handle to create the context for.
|
|
|
|
*
|
|
|
|
* Call under acpiphp_context_lock.
|
|
|
|
*/
|
|
|
|
static struct acpiphp_context *acpiphp_init_context(acpi_handle handle)
|
|
|
|
{
|
|
|
|
struct acpiphp_context *context;
|
|
|
|
acpi_status status;
|
|
|
|
|
|
|
|
context = kzalloc(sizeof(*context), GFP_KERNEL);
|
|
|
|
if (!context)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
context->handle = handle;
|
|
|
|
context->refcount = 1;
|
|
|
|
status = acpi_attach_data(handle, acpiphp_context_handler, context);
|
|
|
|
if (ACPI_FAILURE(status)) {
|
|
|
|
kfree(context);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* acpiphp_get_context - Get hotplug context and grab a reference to it.
|
|
|
|
* @handle: ACPI object handle to get the context for.
|
|
|
|
*
|
|
|
|
* Call under acpiphp_context_lock.
|
|
|
|
*/
|
|
|
|
static struct acpiphp_context *acpiphp_get_context(acpi_handle handle)
|
|
|
|
{
|
|
|
|
struct acpiphp_context *context = NULL;
|
|
|
|
acpi_status status;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
status = acpi_get_data(handle, acpiphp_context_handler, &data);
|
|
|
|
if (ACPI_SUCCESS(status)) {
|
|
|
|
context = data;
|
|
|
|
context->refcount++;
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* acpiphp_put_context - Drop a reference to ACPI hotplug context.
|
|
|
|
* @handle: ACPI object handle to put the context for.
|
|
|
|
*
|
|
|
|
* The context object is removed if there are no more references to it.
|
|
|
|
*
|
|
|
|
* Call under acpiphp_context_lock.
|
|
|
|
*/
|
|
|
|
static void acpiphp_put_context(struct acpiphp_context *context)
|
|
|
|
{
|
|
|
|
if (--context->refcount)
|
|
|
|
return;
|
|
|
|
|
2013-07-13 21:27:25 +00:00
|
|
|
WARN_ON(context->bridge);
|
2013-07-13 21:27:24 +00:00
|
|
|
acpi_detach_data(context->handle, acpiphp_context_handler);
|
|
|
|
kfree(context);
|
|
|
|
}
|
|
|
|
|
2013-04-12 05:44:28 +00:00
|
|
|
static inline void get_bridge(struct acpiphp_bridge *bridge)
|
|
|
|
{
|
|
|
|
kref_get(&bridge->ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void put_bridge(struct acpiphp_bridge *bridge)
|
|
|
|
{
|
|
|
|
kref_put(&bridge->ref, free_bridge);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free_bridge(struct kref *kref)
|
|
|
|
{
|
2013-07-13 21:27:24 +00:00
|
|
|
struct acpiphp_context *context;
|
2013-04-12 05:44:28 +00:00
|
|
|
struct acpiphp_bridge *bridge;
|
|
|
|
struct acpiphp_slot *slot, *next;
|
|
|
|
struct acpiphp_func *func, *tmp;
|
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
mutex_lock(&acpiphp_context_lock);
|
|
|
|
|
2013-04-12 05:44:28 +00:00
|
|
|
bridge = container_of(kref, struct acpiphp_bridge, ref);
|
|
|
|
|
|
|
|
list_for_each_entry_safe(slot, next, &bridge->slots, node) {
|
2013-07-13 21:27:25 +00:00
|
|
|
list_for_each_entry_safe(func, tmp, &slot->funcs, sibling)
|
|
|
|
acpiphp_put_context(func_to_context(func));
|
|
|
|
|
2013-04-12 05:44:28 +00:00
|
|
|
kfree(slot);
|
|
|
|
}
|
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
context = bridge->context;
|
2013-07-13 21:27:24 +00:00
|
|
|
/* Root bridges will not have hotplug context. */
|
|
|
|
if (context) {
|
|
|
|
/* Release the reference taken by acpiphp_enumerate_slots(). */
|
2013-07-13 21:27:25 +00:00
|
|
|
put_bridge(context->func.slot->bridge);
|
2013-07-13 21:27:24 +00:00
|
|
|
context->bridge = NULL;
|
|
|
|
acpiphp_put_context(context);
|
|
|
|
}
|
2013-07-13 21:27:24 +00:00
|
|
|
|
2013-04-12 05:44:28 +00:00
|
|
|
put_device(&bridge->pci_bus->dev);
|
|
|
|
pci_dev_put(bridge->pci_dev);
|
|
|
|
kfree(bridge);
|
2013-07-13 21:27:24 +00:00
|
|
|
|
|
|
|
mutex_unlock(&acpiphp_context_lock);
|
2013-04-12 05:44:28 +00:00
|
|
|
}
|
|
|
|
|
2006-06-28 07:08:06 +00:00
|
|
|
/*
|
|
|
|
* the _DCK method can do funny things... and sometimes not
|
|
|
|
* hah-hah funny.
|
|
|
|
*
|
|
|
|
* TBD - figure out a way to only call fixups for
|
|
|
|
* systems that require them.
|
|
|
|
*/
|
2013-07-05 01:03:25 +00:00
|
|
|
static void post_dock_fixups(acpi_handle not_used, u32 event, void *data)
|
2006-06-28 07:08:06 +00:00
|
|
|
{
|
2013-07-13 21:27:24 +00:00
|
|
|
struct acpiphp_context *context = data;
|
2013-07-13 21:27:25 +00:00
|
|
|
struct pci_bus *bus = context->func.slot->bridge->pci_bus;
|
2006-06-28 07:08:06 +00:00
|
|
|
u32 buses;
|
|
|
|
|
|
|
|
if (!bus->self)
|
2013-07-05 01:03:25 +00:00
|
|
|
return;
|
2006-06-28 07:08:06 +00:00
|
|
|
|
|
|
|
/* fixup bad _DCK function that rewrites
|
|
|
|
* secondary bridge on slot
|
|
|
|
*/
|
|
|
|
pci_read_config_dword(bus->self,
|
|
|
|
PCI_PRIMARY_BUS,
|
|
|
|
&buses);
|
|
|
|
|
2012-05-18 01:51:11 +00:00
|
|
|
if (((buses >> 8) & 0xff) != bus->busn_res.start) {
|
2006-06-28 07:08:06 +00:00
|
|
|
buses = (buses & 0xff000000)
|
2008-12-11 18:17:55 +00:00
|
|
|
| ((unsigned int)(bus->primary) << 0)
|
2012-05-18 01:51:11 +00:00
|
|
|
| ((unsigned int)(bus->busn_res.start) << 8)
|
|
|
|
| ((unsigned int)(bus->busn_res.end) << 16);
|
2006-06-28 07:08:06 +00:00
|
|
|
pci_write_config_dword(bus->self, PCI_PRIMARY_BUS, buses);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-06-25 17:07:52 +00:00
|
|
|
static const struct acpi_dock_ops acpiphp_dock_ops = {
|
2013-07-05 01:03:25 +00:00
|
|
|
.fixup = post_dock_fixups,
|
2013-07-13 21:27:24 +00:00
|
|
|
.handler = hotplug_event,
|
2008-08-28 02:06:16 +00:00
|
|
|
};
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2012-08-22 15:16:45 +00:00
|
|
|
/* Check whether the PCI device is managed by native PCIe hotplug driver */
|
|
|
|
static bool device_is_managed_by_native_pciehp(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
u32 reg32;
|
|
|
|
acpi_handle tmp;
|
|
|
|
struct acpi_pci_root *root;
|
|
|
|
|
|
|
|
/* Check whether the PCIe port supports native PCIe hotplug */
|
|
|
|
if (pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, ®32))
|
|
|
|
return false;
|
|
|
|
if (!(reg32 & PCI_EXP_SLTCAP_HPC))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check whether native PCIe hotplug has been enabled for
|
|
|
|
* this PCIe hierarchy.
|
|
|
|
*/
|
|
|
|
tmp = acpi_find_root_bridge_handle(pdev);
|
|
|
|
if (!tmp)
|
|
|
|
return false;
|
|
|
|
root = acpi_pci_find_root(tmp);
|
|
|
|
if (!root)
|
|
|
|
return false;
|
|
|
|
if (!(root->osc_control_set & OSC_PCI_EXPRESS_NATIVE_HP_CONTROL))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
ACPI / dock / PCI: Synchronous handling of dock events for PCI devices
The interactions between the ACPI dock driver and the ACPI-based PCI
hotplug (acpiphp) are currently problematic because of ordering
issues during hot-remove operations.
First of all, the current ACPI glue code expects that physical
devices will always be deleted before deleting the companion ACPI
device objects. Otherwise, acpi_unbind_one() will fail with a
warning message printed to the kernel log, for example:
[ 185.026073] usb usb5: Oops, 'acpi_handle' corrupt
[ 185.035150] pci 0000:1b:00.0: Oops, 'acpi_handle' corrupt
[ 185.035515] pci 0000:18:02.0: Oops, 'acpi_handle' corrupt
[ 180.013656] port1: Oops, 'acpi_handle' corrupt
This means, in particular, that struct pci_dev objects have to
be deleted before the struct acpi_device objects they are "glued"
with.
Now, the following happens the during the undocking of an ACPI-based
dock station:
1) hotplug_dock_devices() invokes registered hotplug callbacks to
destroy physical devices associated with the ACPI device objects
depending on the dock station. It calls dd->ops->handler() for
each of those device objects.
2) For PCI devices dd->ops->handler() points to
handle_hotplug_event_func() that queues up a separate work item
to execute _handle_hotplug_event_func() for the given device and
returns immediately. That work item will be executed later.
3) hotplug_dock_devices() calls dock_remove_acpi_device() for each
device depending on the dock station. This runs acpi_bus_trim()
for each of them, which causes the underlying ACPI device object
to be destroyed, but the work items queued up by
handle_hotplug_event_func() haven't been started yet.
4) _handle_hotplug_event_func() queued up in step 2) are executed
and cause the above failure to happen, because the PCI devices
they handle do not have the companion ACPI device objects any
more (those objects have been deleted in step 3).
The possible breakage doesn't end here, though, because
hotplug_dock_devices() may return before at least some of the
_handle_hotplug_event_func() work items spawned by it have a
chance to complete and then undock() will cause _DCK to be
evaluated and that will cause the devices handled by the
_handle_hotplug_event_func() to go away possibly while they are
being accessed.
This means that dd->ops->handler() for PCI devices should not point
to handle_hotplug_event_func(). Instead, it should point to a
function that will do the work of _handle_hotplug_event_func()
synchronously. For this reason, introduce such a function,
hotplug_event_func(), and modity acpiphp_dock_ops to point to
it as the handler.
Unfortunately, however, this is not sufficient, because if the dock
code were not changed further, hotplug_event_func() would now
deadlock with hotplug_dock_devices() that called it, since it would
run unregister_hotplug_dock_device() which in turn would attempt to
acquire the dock station's hp_lock mutex already acquired by
hotplug_dock_devices().
To resolve that deadlock use the observation that
unregister_hotplug_dock_device() won't need to acquire hp_lock
if PCI bridges the devices on the dock station depend on are
prevented from being removed prematurely while the first loop in
hotplug_dock_devices() is in progress.
To make that possible, introduce a mechanism by which the callers of
register_hotplug_dock_device() can provide "init" and "release"
routines that will be executed, respectively, during the addition
and removal of the physical device object associated with the
given ACPI device handle. Make acpiphp use two new functions,
acpiphp_dock_init() and acpiphp_dock_release(), that call
get_bridge() and put_bridge(), respectively, on the acpiphp bridge
holding the given device, for this purpose.
In addition to that, remove the dock station's list of
"hotplug devices" and make the dock code always walk the whole list
of "dependent devices" instead in such a way that the loops in
hotplug_dock_devices() and dock_event() (replacing the loops over
"hotplug devices") will take references to the list entries that
register_hotplug_dock_device() has been called for. That prevents
the "release" routines associated with those entries from being
called while the given entry is being processed and for PCI
devices this means that their bridges won't be removed (by a
concurrent thread) while hotplug_event_func() handling them is
being executed.
This change is based on two earlier patches from Jiang Liu.
References: https://bugzilla.kernel.org/show_bug.cgi?id=59501
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tracked-down-by: Jiang Liu <jiang.liu@huawei.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
2013-06-24 09:22:53 +00:00
|
|
|
static void acpiphp_dock_init(void *data)
|
|
|
|
{
|
2013-07-13 21:27:24 +00:00
|
|
|
struct acpiphp_context *context = data;
|
ACPI / dock / PCI: Synchronous handling of dock events for PCI devices
The interactions between the ACPI dock driver and the ACPI-based PCI
hotplug (acpiphp) are currently problematic because of ordering
issues during hot-remove operations.
First of all, the current ACPI glue code expects that physical
devices will always be deleted before deleting the companion ACPI
device objects. Otherwise, acpi_unbind_one() will fail with a
warning message printed to the kernel log, for example:
[ 185.026073] usb usb5: Oops, 'acpi_handle' corrupt
[ 185.035150] pci 0000:1b:00.0: Oops, 'acpi_handle' corrupt
[ 185.035515] pci 0000:18:02.0: Oops, 'acpi_handle' corrupt
[ 180.013656] port1: Oops, 'acpi_handle' corrupt
This means, in particular, that struct pci_dev objects have to
be deleted before the struct acpi_device objects they are "glued"
with.
Now, the following happens the during the undocking of an ACPI-based
dock station:
1) hotplug_dock_devices() invokes registered hotplug callbacks to
destroy physical devices associated with the ACPI device objects
depending on the dock station. It calls dd->ops->handler() for
each of those device objects.
2) For PCI devices dd->ops->handler() points to
handle_hotplug_event_func() that queues up a separate work item
to execute _handle_hotplug_event_func() for the given device and
returns immediately. That work item will be executed later.
3) hotplug_dock_devices() calls dock_remove_acpi_device() for each
device depending on the dock station. This runs acpi_bus_trim()
for each of them, which causes the underlying ACPI device object
to be destroyed, but the work items queued up by
handle_hotplug_event_func() haven't been started yet.
4) _handle_hotplug_event_func() queued up in step 2) are executed
and cause the above failure to happen, because the PCI devices
they handle do not have the companion ACPI device objects any
more (those objects have been deleted in step 3).
The possible breakage doesn't end here, though, because
hotplug_dock_devices() may return before at least some of the
_handle_hotplug_event_func() work items spawned by it have a
chance to complete and then undock() will cause _DCK to be
evaluated and that will cause the devices handled by the
_handle_hotplug_event_func() to go away possibly while they are
being accessed.
This means that dd->ops->handler() for PCI devices should not point
to handle_hotplug_event_func(). Instead, it should point to a
function that will do the work of _handle_hotplug_event_func()
synchronously. For this reason, introduce such a function,
hotplug_event_func(), and modity acpiphp_dock_ops to point to
it as the handler.
Unfortunately, however, this is not sufficient, because if the dock
code were not changed further, hotplug_event_func() would now
deadlock with hotplug_dock_devices() that called it, since it would
run unregister_hotplug_dock_device() which in turn would attempt to
acquire the dock station's hp_lock mutex already acquired by
hotplug_dock_devices().
To resolve that deadlock use the observation that
unregister_hotplug_dock_device() won't need to acquire hp_lock
if PCI bridges the devices on the dock station depend on are
prevented from being removed prematurely while the first loop in
hotplug_dock_devices() is in progress.
To make that possible, introduce a mechanism by which the callers of
register_hotplug_dock_device() can provide "init" and "release"
routines that will be executed, respectively, during the addition
and removal of the physical device object associated with the
given ACPI device handle. Make acpiphp use two new functions,
acpiphp_dock_init() and acpiphp_dock_release(), that call
get_bridge() and put_bridge(), respectively, on the acpiphp bridge
holding the given device, for this purpose.
In addition to that, remove the dock station's list of
"hotplug devices" and make the dock code always walk the whole list
of "dependent devices" instead in such a way that the loops in
hotplug_dock_devices() and dock_event() (replacing the loops over
"hotplug devices") will take references to the list entries that
register_hotplug_dock_device() has been called for. That prevents
the "release" routines associated with those entries from being
called while the given entry is being processed and for PCI
devices this means that their bridges won't be removed (by a
concurrent thread) while hotplug_event_func() handling them is
being executed.
This change is based on two earlier patches from Jiang Liu.
References: https://bugzilla.kernel.org/show_bug.cgi?id=59501
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tracked-down-by: Jiang Liu <jiang.liu@huawei.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
2013-06-24 09:22:53 +00:00
|
|
|
|
2013-07-13 21:27:25 +00:00
|
|
|
get_bridge(context->func.slot->bridge);
|
ACPI / dock / PCI: Synchronous handling of dock events for PCI devices
The interactions between the ACPI dock driver and the ACPI-based PCI
hotplug (acpiphp) are currently problematic because of ordering
issues during hot-remove operations.
First of all, the current ACPI glue code expects that physical
devices will always be deleted before deleting the companion ACPI
device objects. Otherwise, acpi_unbind_one() will fail with a
warning message printed to the kernel log, for example:
[ 185.026073] usb usb5: Oops, 'acpi_handle' corrupt
[ 185.035150] pci 0000:1b:00.0: Oops, 'acpi_handle' corrupt
[ 185.035515] pci 0000:18:02.0: Oops, 'acpi_handle' corrupt
[ 180.013656] port1: Oops, 'acpi_handle' corrupt
This means, in particular, that struct pci_dev objects have to
be deleted before the struct acpi_device objects they are "glued"
with.
Now, the following happens the during the undocking of an ACPI-based
dock station:
1) hotplug_dock_devices() invokes registered hotplug callbacks to
destroy physical devices associated with the ACPI device objects
depending on the dock station. It calls dd->ops->handler() for
each of those device objects.
2) For PCI devices dd->ops->handler() points to
handle_hotplug_event_func() that queues up a separate work item
to execute _handle_hotplug_event_func() for the given device and
returns immediately. That work item will be executed later.
3) hotplug_dock_devices() calls dock_remove_acpi_device() for each
device depending on the dock station. This runs acpi_bus_trim()
for each of them, which causes the underlying ACPI device object
to be destroyed, but the work items queued up by
handle_hotplug_event_func() haven't been started yet.
4) _handle_hotplug_event_func() queued up in step 2) are executed
and cause the above failure to happen, because the PCI devices
they handle do not have the companion ACPI device objects any
more (those objects have been deleted in step 3).
The possible breakage doesn't end here, though, because
hotplug_dock_devices() may return before at least some of the
_handle_hotplug_event_func() work items spawned by it have a
chance to complete and then undock() will cause _DCK to be
evaluated and that will cause the devices handled by the
_handle_hotplug_event_func() to go away possibly while they are
being accessed.
This means that dd->ops->handler() for PCI devices should not point
to handle_hotplug_event_func(). Instead, it should point to a
function that will do the work of _handle_hotplug_event_func()
synchronously. For this reason, introduce such a function,
hotplug_event_func(), and modity acpiphp_dock_ops to point to
it as the handler.
Unfortunately, however, this is not sufficient, because if the dock
code were not changed further, hotplug_event_func() would now
deadlock with hotplug_dock_devices() that called it, since it would
run unregister_hotplug_dock_device() which in turn would attempt to
acquire the dock station's hp_lock mutex already acquired by
hotplug_dock_devices().
To resolve that deadlock use the observation that
unregister_hotplug_dock_device() won't need to acquire hp_lock
if PCI bridges the devices on the dock station depend on are
prevented from being removed prematurely while the first loop in
hotplug_dock_devices() is in progress.
To make that possible, introduce a mechanism by which the callers of
register_hotplug_dock_device() can provide "init" and "release"
routines that will be executed, respectively, during the addition
and removal of the physical device object associated with the
given ACPI device handle. Make acpiphp use two new functions,
acpiphp_dock_init() and acpiphp_dock_release(), that call
get_bridge() and put_bridge(), respectively, on the acpiphp bridge
holding the given device, for this purpose.
In addition to that, remove the dock station's list of
"hotplug devices" and make the dock code always walk the whole list
of "dependent devices" instead in such a way that the loops in
hotplug_dock_devices() and dock_event() (replacing the loops over
"hotplug devices") will take references to the list entries that
register_hotplug_dock_device() has been called for. That prevents
the "release" routines associated with those entries from being
called while the given entry is being processed and for PCI
devices this means that their bridges won't be removed (by a
concurrent thread) while hotplug_event_func() handling them is
being executed.
This change is based on two earlier patches from Jiang Liu.
References: https://bugzilla.kernel.org/show_bug.cgi?id=59501
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tracked-down-by: Jiang Liu <jiang.liu@huawei.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
2013-06-24 09:22:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void acpiphp_dock_release(void *data)
|
|
|
|
{
|
2013-07-13 21:27:24 +00:00
|
|
|
struct acpiphp_context *context = data;
|
ACPI / dock / PCI: Synchronous handling of dock events for PCI devices
The interactions between the ACPI dock driver and the ACPI-based PCI
hotplug (acpiphp) are currently problematic because of ordering
issues during hot-remove operations.
First of all, the current ACPI glue code expects that physical
devices will always be deleted before deleting the companion ACPI
device objects. Otherwise, acpi_unbind_one() will fail with a
warning message printed to the kernel log, for example:
[ 185.026073] usb usb5: Oops, 'acpi_handle' corrupt
[ 185.035150] pci 0000:1b:00.0: Oops, 'acpi_handle' corrupt
[ 185.035515] pci 0000:18:02.0: Oops, 'acpi_handle' corrupt
[ 180.013656] port1: Oops, 'acpi_handle' corrupt
This means, in particular, that struct pci_dev objects have to
be deleted before the struct acpi_device objects they are "glued"
with.
Now, the following happens the during the undocking of an ACPI-based
dock station:
1) hotplug_dock_devices() invokes registered hotplug callbacks to
destroy physical devices associated with the ACPI device objects
depending on the dock station. It calls dd->ops->handler() for
each of those device objects.
2) For PCI devices dd->ops->handler() points to
handle_hotplug_event_func() that queues up a separate work item
to execute _handle_hotplug_event_func() for the given device and
returns immediately. That work item will be executed later.
3) hotplug_dock_devices() calls dock_remove_acpi_device() for each
device depending on the dock station. This runs acpi_bus_trim()
for each of them, which causes the underlying ACPI device object
to be destroyed, but the work items queued up by
handle_hotplug_event_func() haven't been started yet.
4) _handle_hotplug_event_func() queued up in step 2) are executed
and cause the above failure to happen, because the PCI devices
they handle do not have the companion ACPI device objects any
more (those objects have been deleted in step 3).
The possible breakage doesn't end here, though, because
hotplug_dock_devices() may return before at least some of the
_handle_hotplug_event_func() work items spawned by it have a
chance to complete and then undock() will cause _DCK to be
evaluated and that will cause the devices handled by the
_handle_hotplug_event_func() to go away possibly while they are
being accessed.
This means that dd->ops->handler() for PCI devices should not point
to handle_hotplug_event_func(). Instead, it should point to a
function that will do the work of _handle_hotplug_event_func()
synchronously. For this reason, introduce such a function,
hotplug_event_func(), and modity acpiphp_dock_ops to point to
it as the handler.
Unfortunately, however, this is not sufficient, because if the dock
code were not changed further, hotplug_event_func() would now
deadlock with hotplug_dock_devices() that called it, since it would
run unregister_hotplug_dock_device() which in turn would attempt to
acquire the dock station's hp_lock mutex already acquired by
hotplug_dock_devices().
To resolve that deadlock use the observation that
unregister_hotplug_dock_device() won't need to acquire hp_lock
if PCI bridges the devices on the dock station depend on are
prevented from being removed prematurely while the first loop in
hotplug_dock_devices() is in progress.
To make that possible, introduce a mechanism by which the callers of
register_hotplug_dock_device() can provide "init" and "release"
routines that will be executed, respectively, during the addition
and removal of the physical device object associated with the
given ACPI device handle. Make acpiphp use two new functions,
acpiphp_dock_init() and acpiphp_dock_release(), that call
get_bridge() and put_bridge(), respectively, on the acpiphp bridge
holding the given device, for this purpose.
In addition to that, remove the dock station's list of
"hotplug devices" and make the dock code always walk the whole list
of "dependent devices" instead in such a way that the loops in
hotplug_dock_devices() and dock_event() (replacing the loops over
"hotplug devices") will take references to the list entries that
register_hotplug_dock_device() has been called for. That prevents
the "release" routines associated with those entries from being
called while the given entry is being processed and for PCI
devices this means that their bridges won't be removed (by a
concurrent thread) while hotplug_event_func() handling them is
being executed.
This change is based on two earlier patches from Jiang Liu.
References: https://bugzilla.kernel.org/show_bug.cgi?id=59501
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tracked-down-by: Jiang Liu <jiang.liu@huawei.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
2013-06-24 09:22:53 +00:00
|
|
|
|
2013-07-13 21:27:25 +00:00
|
|
|
put_bridge(context->func.slot->bridge);
|
ACPI / dock / PCI: Synchronous handling of dock events for PCI devices
The interactions between the ACPI dock driver and the ACPI-based PCI
hotplug (acpiphp) are currently problematic because of ordering
issues during hot-remove operations.
First of all, the current ACPI glue code expects that physical
devices will always be deleted before deleting the companion ACPI
device objects. Otherwise, acpi_unbind_one() will fail with a
warning message printed to the kernel log, for example:
[ 185.026073] usb usb5: Oops, 'acpi_handle' corrupt
[ 185.035150] pci 0000:1b:00.0: Oops, 'acpi_handle' corrupt
[ 185.035515] pci 0000:18:02.0: Oops, 'acpi_handle' corrupt
[ 180.013656] port1: Oops, 'acpi_handle' corrupt
This means, in particular, that struct pci_dev objects have to
be deleted before the struct acpi_device objects they are "glued"
with.
Now, the following happens the during the undocking of an ACPI-based
dock station:
1) hotplug_dock_devices() invokes registered hotplug callbacks to
destroy physical devices associated with the ACPI device objects
depending on the dock station. It calls dd->ops->handler() for
each of those device objects.
2) For PCI devices dd->ops->handler() points to
handle_hotplug_event_func() that queues up a separate work item
to execute _handle_hotplug_event_func() for the given device and
returns immediately. That work item will be executed later.
3) hotplug_dock_devices() calls dock_remove_acpi_device() for each
device depending on the dock station. This runs acpi_bus_trim()
for each of them, which causes the underlying ACPI device object
to be destroyed, but the work items queued up by
handle_hotplug_event_func() haven't been started yet.
4) _handle_hotplug_event_func() queued up in step 2) are executed
and cause the above failure to happen, because the PCI devices
they handle do not have the companion ACPI device objects any
more (those objects have been deleted in step 3).
The possible breakage doesn't end here, though, because
hotplug_dock_devices() may return before at least some of the
_handle_hotplug_event_func() work items spawned by it have a
chance to complete and then undock() will cause _DCK to be
evaluated and that will cause the devices handled by the
_handle_hotplug_event_func() to go away possibly while they are
being accessed.
This means that dd->ops->handler() for PCI devices should not point
to handle_hotplug_event_func(). Instead, it should point to a
function that will do the work of _handle_hotplug_event_func()
synchronously. For this reason, introduce such a function,
hotplug_event_func(), and modity acpiphp_dock_ops to point to
it as the handler.
Unfortunately, however, this is not sufficient, because if the dock
code were not changed further, hotplug_event_func() would now
deadlock with hotplug_dock_devices() that called it, since it would
run unregister_hotplug_dock_device() which in turn would attempt to
acquire the dock station's hp_lock mutex already acquired by
hotplug_dock_devices().
To resolve that deadlock use the observation that
unregister_hotplug_dock_device() won't need to acquire hp_lock
if PCI bridges the devices on the dock station depend on are
prevented from being removed prematurely while the first loop in
hotplug_dock_devices() is in progress.
To make that possible, introduce a mechanism by which the callers of
register_hotplug_dock_device() can provide "init" and "release"
routines that will be executed, respectively, during the addition
and removal of the physical device object associated with the
given ACPI device handle. Make acpiphp use two new functions,
acpiphp_dock_init() and acpiphp_dock_release(), that call
get_bridge() and put_bridge(), respectively, on the acpiphp bridge
holding the given device, for this purpose.
In addition to that, remove the dock station's list of
"hotplug devices" and make the dock code always walk the whole list
of "dependent devices" instead in such a way that the loops in
hotplug_dock_devices() and dock_event() (replacing the loops over
"hotplug devices") will take references to the list entries that
register_hotplug_dock_device() has been called for. That prevents
the "release" routines associated with those entries from being
called while the given entry is being processed and for PCI
devices this means that their bridges won't be removed (by a
concurrent thread) while hotplug_event_func() handling them is
being executed.
This change is based on two earlier patches from Jiang Liu.
References: https://bugzilla.kernel.org/show_bug.cgi?id=59501
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tracked-down-by: Jiang Liu <jiang.liu@huawei.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
2013-06-24 09:22:53 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* callback routine to register each ACPI PCI slot object */
|
2013-07-13 21:27:24 +00:00
|
|
|
static acpi_status register_slot(acpi_handle handle, u32 lvl, void *data,
|
|
|
|
void **rv)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-07-13 21:27:24 +00:00
|
|
|
struct acpiphp_bridge *bridge = data;
|
|
|
|
struct acpiphp_context *context;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct acpiphp_slot *slot;
|
|
|
|
struct acpiphp_func *newfunc;
|
|
|
|
acpi_status status = AE_OK;
|
2013-07-13 21:27:24 +00:00
|
|
|
unsigned long long adr;
|
|
|
|
int device, function;
|
2008-12-17 03:09:12 +00:00
|
|
|
struct pci_bus *pbus = bridge->pci_bus;
|
2013-07-13 21:27:24 +00:00
|
|
|
struct pci_dev *pdev = bridge->pci_dev;
|
2013-04-12 05:44:26 +00:00
|
|
|
u32 val;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
if (pdev && device_is_managed_by_native_pciehp(pdev))
|
2005-04-16 22:20:36 +00:00
|
|
|
return AE_OK;
|
|
|
|
|
2012-06-20 22:18:29 +00:00
|
|
|
status = acpi_evaluate_integer(handle, "_ADR", NULL, &adr);
|
|
|
|
if (ACPI_FAILURE(status)) {
|
2013-07-13 21:27:24 +00:00
|
|
|
acpi_handle_warn(handle, "can't evaluate _ADR (%#x)\n", status);
|
2012-06-20 22:18:29 +00:00
|
|
|
return AE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
device = (adr >> 16) & 0xffff;
|
|
|
|
function = adr & 0xffff;
|
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
mutex_lock(&acpiphp_context_lock);
|
|
|
|
context = acpiphp_init_context(handle);
|
|
|
|
if (!context) {
|
|
|
|
mutex_unlock(&acpiphp_context_lock);
|
|
|
|
acpi_handle_err(handle, "No hotplug context\n");
|
|
|
|
return AE_NOT_EXIST;
|
|
|
|
}
|
2013-07-13 21:27:25 +00:00
|
|
|
newfunc = &context->func;
|
|
|
|
newfunc->function = function;
|
2013-07-13 21:27:24 +00:00
|
|
|
mutex_unlock(&acpiphp_context_lock);
|
|
|
|
|
2013-06-28 16:24:43 +00:00
|
|
|
if (acpi_has_method(handle, "_EJ0"))
|
2006-02-24 01:56:03 +00:00
|
|
|
newfunc->flags = FUNC_HAS_EJ0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-06-28 16:24:43 +00:00
|
|
|
if (acpi_has_method(handle, "_STA"))
|
2005-04-16 22:20:36 +00:00
|
|
|
newfunc->flags |= FUNC_HAS_STA;
|
|
|
|
|
2013-06-28 16:24:43 +00:00
|
|
|
if (acpi_has_method(handle, "_PS0"))
|
2005-04-16 22:20:36 +00:00
|
|
|
newfunc->flags |= FUNC_HAS_PS0;
|
|
|
|
|
2013-06-28 16:24:43 +00:00
|
|
|
if (acpi_has_method(handle, "_PS3"))
|
2005-04-16 22:20:36 +00:00
|
|
|
newfunc->flags |= FUNC_HAS_PS3;
|
|
|
|
|
2013-06-28 16:24:43 +00:00
|
|
|
if (acpi_has_method(handle, "_DCK"))
|
2006-02-24 01:56:03 +00:00
|
|
|
newfunc->flags |= FUNC_HAS_DCK;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* search for objects that share the same slot */
|
2013-04-12 05:44:27 +00:00
|
|
|
list_for_each_entry(slot, &bridge->slots, node)
|
2013-07-13 21:27:24 +00:00
|
|
|
if (slot->device == device)
|
2013-07-13 21:27:24 +00:00
|
|
|
goto slot_found;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
slot = kzalloc(sizeof(struct acpiphp_slot), GFP_KERNEL);
|
|
|
|
if (!slot) {
|
|
|
|
status = AE_NO_MEMORY;
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
slot->bridge = bridge;
|
|
|
|
slot->device = device;
|
|
|
|
INIT_LIST_HEAD(&slot->funcs);
|
|
|
|
mutex_init(&slot->crit_sect);
|
|
|
|
|
|
|
|
mutex_lock(&bridge_mutex);
|
|
|
|
list_add_tail(&slot->node, &bridge->slots);
|
|
|
|
mutex_unlock(&bridge_mutex);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
/* Register slots for ejectable funtions only. */
|
|
|
|
if (acpi_pci_check_ejectable(pbus, handle) || is_dock_device(handle)) {
|
|
|
|
unsigned long long sun;
|
|
|
|
int retval;
|
2013-07-13 21:27:24 +00:00
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
bridge->nr_slots++;
|
|
|
|
status = acpi_evaluate_integer(handle, "_SUN", NULL, &sun);
|
|
|
|
if (ACPI_FAILURE(status))
|
|
|
|
sun = bridge->nr_slots;
|
|
|
|
|
|
|
|
dbg("found ACPI PCI Hotplug slot %llu at PCI %04x:%02x:%02x\n",
|
2013-07-13 21:27:25 +00:00
|
|
|
sun, pci_domain_nr(pbus), pbus->number, device);
|
2013-07-13 21:27:24 +00:00
|
|
|
|
2013-07-13 21:27:25 +00:00
|
|
|
retval = acpiphp_register_hotplug_slot(slot, sun);
|
2013-07-13 21:27:24 +00:00
|
|
|
if (retval) {
|
|
|
|
bridge->nr_slots--;
|
|
|
|
if (retval == -EBUSY)
|
|
|
|
warn("Slot %llu already registered by another "
|
2013-07-13 21:27:25 +00:00
|
|
|
"hotplug driver\n", sun);
|
2013-07-13 21:27:24 +00:00
|
|
|
else
|
|
|
|
warn("acpiphp_register_hotplug_slot failed "
|
|
|
|
"(err code = 0x%x)\n", retval);
|
|
|
|
}
|
|
|
|
/* Even if the slot registration fails, we can still use it. */
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
slot_found:
|
2005-04-16 22:20:36 +00:00
|
|
|
newfunc->slot = slot;
|
2013-04-12 05:44:28 +00:00
|
|
|
mutex_lock(&bridge_mutex);
|
2005-04-16 22:20:36 +00:00
|
|
|
list_add_tail(&newfunc->sibling, &slot->funcs);
|
2013-04-12 05:44:28 +00:00
|
|
|
mutex_unlock(&bridge_mutex);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-04-12 05:44:26 +00:00
|
|
|
if (pci_bus_read_dev_vendor_id(pbus, PCI_DEVFN(device, function),
|
|
|
|
&val, 60*1000))
|
2005-04-16 22:20:36 +00:00
|
|
|
slot->flags |= (SLOT_ENABLED | SLOT_POWEREDON);
|
|
|
|
|
2006-06-28 07:08:06 +00:00
|
|
|
if (is_dock_device(handle)) {
|
|
|
|
/* we don't want to call this device's _EJ0
|
|
|
|
* because we want the dock notify handler
|
|
|
|
* to call it after it calls _DCK
|
2006-02-24 01:56:03 +00:00
|
|
|
*/
|
|
|
|
newfunc->flags &= ~FUNC_HAS_EJ0;
|
2006-06-28 07:08:06 +00:00
|
|
|
if (register_hotplug_dock_device(handle,
|
2013-07-13 21:27:24 +00:00
|
|
|
&acpiphp_dock_ops, context,
|
ACPI / dock / PCI: Synchronous handling of dock events for PCI devices
The interactions between the ACPI dock driver and the ACPI-based PCI
hotplug (acpiphp) are currently problematic because of ordering
issues during hot-remove operations.
First of all, the current ACPI glue code expects that physical
devices will always be deleted before deleting the companion ACPI
device objects. Otherwise, acpi_unbind_one() will fail with a
warning message printed to the kernel log, for example:
[ 185.026073] usb usb5: Oops, 'acpi_handle' corrupt
[ 185.035150] pci 0000:1b:00.0: Oops, 'acpi_handle' corrupt
[ 185.035515] pci 0000:18:02.0: Oops, 'acpi_handle' corrupt
[ 180.013656] port1: Oops, 'acpi_handle' corrupt
This means, in particular, that struct pci_dev objects have to
be deleted before the struct acpi_device objects they are "glued"
with.
Now, the following happens the during the undocking of an ACPI-based
dock station:
1) hotplug_dock_devices() invokes registered hotplug callbacks to
destroy physical devices associated with the ACPI device objects
depending on the dock station. It calls dd->ops->handler() for
each of those device objects.
2) For PCI devices dd->ops->handler() points to
handle_hotplug_event_func() that queues up a separate work item
to execute _handle_hotplug_event_func() for the given device and
returns immediately. That work item will be executed later.
3) hotplug_dock_devices() calls dock_remove_acpi_device() for each
device depending on the dock station. This runs acpi_bus_trim()
for each of them, which causes the underlying ACPI device object
to be destroyed, but the work items queued up by
handle_hotplug_event_func() haven't been started yet.
4) _handle_hotplug_event_func() queued up in step 2) are executed
and cause the above failure to happen, because the PCI devices
they handle do not have the companion ACPI device objects any
more (those objects have been deleted in step 3).
The possible breakage doesn't end here, though, because
hotplug_dock_devices() may return before at least some of the
_handle_hotplug_event_func() work items spawned by it have a
chance to complete and then undock() will cause _DCK to be
evaluated and that will cause the devices handled by the
_handle_hotplug_event_func() to go away possibly while they are
being accessed.
This means that dd->ops->handler() for PCI devices should not point
to handle_hotplug_event_func(). Instead, it should point to a
function that will do the work of _handle_hotplug_event_func()
synchronously. For this reason, introduce such a function,
hotplug_event_func(), and modity acpiphp_dock_ops to point to
it as the handler.
Unfortunately, however, this is not sufficient, because if the dock
code were not changed further, hotplug_event_func() would now
deadlock with hotplug_dock_devices() that called it, since it would
run unregister_hotplug_dock_device() which in turn would attempt to
acquire the dock station's hp_lock mutex already acquired by
hotplug_dock_devices().
To resolve that deadlock use the observation that
unregister_hotplug_dock_device() won't need to acquire hp_lock
if PCI bridges the devices on the dock station depend on are
prevented from being removed prematurely while the first loop in
hotplug_dock_devices() is in progress.
To make that possible, introduce a mechanism by which the callers of
register_hotplug_dock_device() can provide "init" and "release"
routines that will be executed, respectively, during the addition
and removal of the physical device object associated with the
given ACPI device handle. Make acpiphp use two new functions,
acpiphp_dock_init() and acpiphp_dock_release(), that call
get_bridge() and put_bridge(), respectively, on the acpiphp bridge
holding the given device, for this purpose.
In addition to that, remove the dock station's list of
"hotplug devices" and make the dock code always walk the whole list
of "dependent devices" instead in such a way that the loops in
hotplug_dock_devices() and dock_event() (replacing the loops over
"hotplug devices") will take references to the list entries that
register_hotplug_dock_device() has been called for. That prevents
the "release" routines associated with those entries from being
called while the given entry is being processed and for PCI
devices this means that their bridges won't be removed (by a
concurrent thread) while hotplug_event_func() handling them is
being executed.
This change is based on two earlier patches from Jiang Liu.
References: https://bugzilla.kernel.org/show_bug.cgi?id=59501
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tracked-down-by: Jiang Liu <jiang.liu@huawei.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
2013-06-24 09:22:53 +00:00
|
|
|
acpiphp_dock_init, acpiphp_dock_release))
|
2006-06-28 07:08:06 +00:00
|
|
|
dbg("failed to register dock device\n");
|
2006-02-24 01:56:03 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/* install notify handler */
|
2006-02-24 01:56:03 +00:00
|
|
|
if (!(newfunc->flags & FUNC_HAS_DCK)) {
|
2013-07-13 21:27:24 +00:00
|
|
|
status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
|
|
|
|
handle_hotplug_event,
|
|
|
|
context);
|
2013-07-13 21:27:24 +00:00
|
|
|
if (ACPI_FAILURE(status))
|
|
|
|
acpi_handle_err(handle,
|
|
|
|
"failed to install notify handler\n");
|
2013-07-13 21:27:23 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-07-13 21:27:23 +00:00
|
|
|
return AE_OK;
|
2006-02-24 01:56:08 +00:00
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
err:
|
|
|
|
mutex_lock(&acpiphp_context_lock);
|
|
|
|
acpiphp_put_context(context);
|
|
|
|
mutex_unlock(&acpiphp_context_lock);
|
|
|
|
return status;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2005-04-28 07:25:53 +00:00
|
|
|
static struct acpiphp_bridge *acpiphp_handle_to_bridge(acpi_handle handle)
|
|
|
|
{
|
2013-07-13 21:27:24 +00:00
|
|
|
struct acpiphp_context *context;
|
|
|
|
struct acpiphp_bridge *bridge = NULL;
|
2009-10-27 03:25:27 +00:00
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
mutex_lock(&acpiphp_context_lock);
|
|
|
|
context = acpiphp_get_context(handle);
|
|
|
|
if (context) {
|
|
|
|
bridge = context->bridge;
|
|
|
|
if (bridge)
|
2013-04-12 05:44:28 +00:00
|
|
|
get_bridge(bridge);
|
2005-04-28 07:25:53 +00:00
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
acpiphp_put_context(context);
|
|
|
|
}
|
|
|
|
mutex_unlock(&acpiphp_context_lock);
|
|
|
|
return bridge;
|
2005-04-28 07:25:53 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-04-28 07:25:54 +00:00
|
|
|
static void cleanup_bridge(struct acpiphp_bridge *bridge)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-04-12 05:44:28 +00:00
|
|
|
struct acpiphp_slot *slot;
|
|
|
|
struct acpiphp_func *func;
|
2005-04-28 07:25:53 +00:00
|
|
|
acpi_status status;
|
|
|
|
|
2013-04-12 05:44:28 +00:00
|
|
|
list_for_each_entry(slot, &bridge->slots, node) {
|
|
|
|
list_for_each_entry(func, &slot->funcs, sibling) {
|
2013-07-13 21:27:25 +00:00
|
|
|
acpi_handle handle = func_to_handle(func);
|
|
|
|
|
|
|
|
if (is_dock_device(handle))
|
|
|
|
unregister_hotplug_dock_device(handle);
|
|
|
|
|
2006-02-24 01:56:03 +00:00
|
|
|
if (!(func->flags & FUNC_HAS_DCK)) {
|
2013-07-13 21:27:25 +00:00
|
|
|
status = acpi_remove_notify_handler(handle,
|
2013-07-13 21:27:24 +00:00
|
|
|
ACPI_SYSTEM_NOTIFY,
|
|
|
|
handle_hotplug_event);
|
2006-02-24 01:56:03 +00:00
|
|
|
if (ACPI_FAILURE(status))
|
|
|
|
err("failed to remove notify handler\n");
|
|
|
|
}
|
2005-04-28 07:25:53 +00:00
|
|
|
}
|
2006-02-24 01:56:08 +00:00
|
|
|
acpiphp_unregister_hotplug_slot(slot);
|
2005-04-28 07:25:53 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 05:44:28 +00:00
|
|
|
mutex_lock(&bridge_mutex);
|
2005-04-28 07:25:53 +00:00
|
|
|
list_del(&bridge->list);
|
2013-04-12 05:44:28 +00:00
|
|
|
mutex_unlock(&bridge_mutex);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int power_on_slot(struct acpiphp_slot *slot)
|
|
|
|
{
|
|
|
|
acpi_status status;
|
|
|
|
struct acpiphp_func *func;
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
/* if already enabled, just skip */
|
|
|
|
if (slot->flags & SLOT_POWEREDON)
|
|
|
|
goto err_exit;
|
|
|
|
|
2009-10-27 03:25:27 +00:00
|
|
|
list_for_each_entry(func, &slot->funcs, sibling) {
|
2005-04-16 22:20:36 +00:00
|
|
|
if (func->flags & FUNC_HAS_PS0) {
|
2008-03-04 03:09:46 +00:00
|
|
|
dbg("%s: executing _PS0\n", __func__);
|
2013-07-13 21:27:25 +00:00
|
|
|
status = acpi_evaluate_object(func_to_handle(func),
|
|
|
|
"_PS0", NULL, NULL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ACPI_FAILURE(status)) {
|
2008-03-04 03:09:46 +00:00
|
|
|
warn("%s: _PS0 failed\n", __func__);
|
2005-04-16 22:20:36 +00:00
|
|
|
retval = -1;
|
|
|
|
goto err_exit;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TBD: evaluate _STA to check if the slot is enabled */
|
|
|
|
|
|
|
|
slot->flags |= SLOT_POWEREDON;
|
|
|
|
|
|
|
|
err_exit:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int power_off_slot(struct acpiphp_slot *slot)
|
|
|
|
{
|
|
|
|
acpi_status status;
|
|
|
|
struct acpiphp_func *func;
|
|
|
|
|
|
|
|
int retval = 0;
|
|
|
|
|
|
|
|
/* if already disabled, just skip */
|
|
|
|
if ((slot->flags & SLOT_POWEREDON) == 0)
|
|
|
|
goto err_exit;
|
|
|
|
|
2009-10-27 03:25:27 +00:00
|
|
|
list_for_each_entry(func, &slot->funcs, sibling) {
|
2005-04-28 07:25:55 +00:00
|
|
|
if (func->flags & FUNC_HAS_PS3) {
|
2013-07-13 21:27:25 +00:00
|
|
|
status = acpi_evaluate_object(func_to_handle(func),
|
|
|
|
"_PS3", NULL, NULL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ACPI_FAILURE(status)) {
|
2008-03-04 03:09:46 +00:00
|
|
|
warn("%s: _PS3 failed\n", __func__);
|
2005-04-16 22:20:36 +00:00
|
|
|
retval = -1;
|
|
|
|
goto err_exit;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TBD: evaluate _STA to check if the slot is disabled */
|
|
|
|
|
|
|
|
slot->flags &= (~SLOT_POWEREDON);
|
|
|
|
|
|
|
|
err_exit:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-24 01:55:58 +00:00
|
|
|
|
|
|
|
/**
|
2007-11-28 17:04:30 +00:00
|
|
|
* acpiphp_max_busnr - return the highest reserved bus number under the given bus.
|
2006-02-24 01:55:58 +00:00
|
|
|
* @bus: bus to start search with
|
|
|
|
*/
|
|
|
|
static unsigned char acpiphp_max_busnr(struct pci_bus *bus)
|
|
|
|
{
|
|
|
|
struct list_head *tmp;
|
|
|
|
unsigned char max, n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pci_bus_max_busnr will return the highest
|
|
|
|
* reserved busnr for all these children.
|
|
|
|
* that is equivalent to the bus->subordinate
|
|
|
|
* value. We don't want to use the parent's
|
|
|
|
* bus->subordinate value because it could have
|
|
|
|
* padding in it.
|
|
|
|
*/
|
2012-05-18 01:51:11 +00:00
|
|
|
max = bus->busn_res.start;
|
2006-02-24 01:55:58 +00:00
|
|
|
|
|
|
|
list_for_each(tmp, &bus->children) {
|
|
|
|
n = pci_bus_max_busnr(pci_bus_b(tmp));
|
|
|
|
if (n > max)
|
|
|
|
max = n;
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* acpiphp_bus_add - add a new bus to acpi subsystem
|
|
|
|
* @func: acpiphp_func of the bridge
|
|
|
|
*/
|
|
|
|
static int acpiphp_bus_add(struct acpiphp_func *func)
|
|
|
|
{
|
2013-07-13 21:27:25 +00:00
|
|
|
acpi_handle handle = func_to_handle(func);
|
2012-12-20 23:36:47 +00:00
|
|
|
struct acpi_device *device;
|
2006-02-24 01:55:58 +00:00
|
|
|
int ret_val;
|
|
|
|
|
2013-07-13 21:27:25 +00:00
|
|
|
if (!acpi_bus_get_device(handle, &device)) {
|
2006-02-24 01:56:03 +00:00
|
|
|
dbg("bus exists... trim\n");
|
|
|
|
/* this shouldn't be in here, so remove
|
|
|
|
* the bus then re-add it...
|
|
|
|
*/
|
2013-01-25 23:27:44 +00:00
|
|
|
acpi_bus_trim(device);
|
2006-02-24 01:56:03 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 21:27:25 +00:00
|
|
|
ret_val = acpi_bus_scan(handle);
|
2012-12-20 23:36:49 +00:00
|
|
|
if (!ret_val)
|
2013-07-13 21:27:25 +00:00
|
|
|
ret_val = acpi_bus_get_device(handle, &device);
|
2012-12-20 23:36:49 +00:00
|
|
|
|
|
|
|
if (ret_val)
|
|
|
|
dbg("error adding bus, %x\n", -ret_val);
|
2006-02-24 01:55:58 +00:00
|
|
|
|
|
|
|
return ret_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-22 05:49:09 +00:00
|
|
|
/**
|
|
|
|
* acpiphp_bus_trim - trim a bus from acpi subsystem
|
|
|
|
* @handle: handle to acpi namespace
|
|
|
|
*/
|
2006-08-15 06:07:38 +00:00
|
|
|
static int acpiphp_bus_trim(acpi_handle handle)
|
2006-03-22 05:49:09 +00:00
|
|
|
{
|
|
|
|
struct acpi_device *device;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = acpi_bus_get_device(handle, &device);
|
|
|
|
if (retval) {
|
|
|
|
dbg("acpi_device not found\n");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-01-25 23:27:44 +00:00
|
|
|
acpi_bus_trim(device);
|
|
|
|
return 0;
|
2006-03-22 05:49:09 +00:00
|
|
|
}
|
2006-02-24 01:55:58 +00:00
|
|
|
|
2010-02-25 02:59:34 +00:00
|
|
|
static void acpiphp_set_acpi_region(struct acpiphp_slot *slot)
|
|
|
|
{
|
|
|
|
struct acpiphp_func *func;
|
|
|
|
union acpi_object params[2];
|
|
|
|
struct acpi_object_list arg_list;
|
|
|
|
|
|
|
|
list_for_each_entry(func, &slot->funcs, sibling) {
|
|
|
|
arg_list.count = 2;
|
|
|
|
arg_list.pointer = params;
|
|
|
|
params[0].type = ACPI_TYPE_INTEGER;
|
|
|
|
params[0].integer.value = ACPI_ADR_SPACE_PCI_CONFIG;
|
|
|
|
params[1].type = ACPI_TYPE_INTEGER;
|
|
|
|
params[1].integer.value = 1;
|
|
|
|
/* _REG is optional, we don't care about if there is failure */
|
2013-07-13 21:27:25 +00:00
|
|
|
acpi_evaluate_object(func_to_handle(func), "_REG", &arg_list,
|
|
|
|
NULL);
|
2010-02-25 02:59:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-21 21:20:42 +00:00
|
|
|
static void check_hotplug_bridge(struct acpiphp_slot *slot, struct pci_dev *dev)
|
|
|
|
{
|
|
|
|
struct acpiphp_func *func;
|
|
|
|
|
|
|
|
if (!dev->subordinate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* quirk, or pcie could set it already */
|
|
|
|
if (dev->is_hotplug_bridge)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (PCI_SLOT(dev->devfn) != slot->device)
|
|
|
|
return;
|
|
|
|
|
|
|
|
list_for_each_entry(func, &slot->funcs, sibling) {
|
|
|
|
if (PCI_FUNC(dev->devfn) == func->function) {
|
2013-07-13 21:27:24 +00:00
|
|
|
dev->is_hotplug_bridge = 1;
|
2013-01-21 21:20:42 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-12 05:44:26 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
|
|
|
* enable_device - enable, configure a slot
|
|
|
|
* @slot: slot to be enabled
|
|
|
|
*
|
|
|
|
* This function should be called per *physical slot*,
|
|
|
|
* not per each slot object in ACPI namespace.
|
|
|
|
*/
|
2008-02-17 09:45:28 +00:00
|
|
|
static int __ref enable_device(struct acpiphp_slot *slot)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct pci_dev *dev;
|
2005-04-28 07:25:53 +00:00
|
|
|
struct pci_bus *bus = slot->bridge->pci_bus;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct acpiphp_func *func;
|
2005-04-28 07:25:53 +00:00
|
|
|
int num, max, pass;
|
PCI / ACPI: Use boot-time resource allocation rules during hotplug
On x86 platforms, the kernel respects PCI resource assignments from
the BIOS and only reassigns resources for unassigned BARs at boot
time. However, with the ACPI-based hotplug (acpiphp), it ignores the
BIOS' PCI resource assignments completely and reassigns all resources
by itself. This causes differences in PCI resource allocation
between boot time and runtime hotplug to occur, which is generally
undesirable and sometimes actively breaks things.
Namely, if there are enough resources, reassigning all PCI resources
during runtime hotplug should work, but it may fail if the resources
are constrained. This may happen, for instance, when some PCI
devices with huge MMIO BARs are involved in the runtime hotplug
operations, because the current PCI MMIO alignment algorithm may
waste huge chunks of MMIO address space in those cases.
On the Alexander's Sony VAIO VPCZ23A4R the BIOS allocates limited
MMIO resources for the dock station which contains a device
(graphics adapter) with a 256MB MMIO BAR. An attempt to reassign
that during runtime hotplug causes the dock station MMIO window to be
exhausted and acpiphp fails to allocate resources for the majority
of devices on the dock station as a result.
To prevent that from happening, modify acpiphp to follow the boot
time resources allocation behavior so that the BIOS' resource
assignments are respected during runtime hotplug too.
[rjw: Changelog]
References: https://bugzilla.kernel.org/show_bug.cgi?id=56531
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Jiang Liu <jiang.liu@huawei.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2013-06-22 23:01:35 +00:00
|
|
|
LIST_HEAD(add_list);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (slot->flags & SLOT_ENABLED)
|
|
|
|
goto err_exit;
|
|
|
|
|
2013-01-30 16:10:09 +00:00
|
|
|
list_for_each_entry(func, &slot->funcs, sibling)
|
|
|
|
acpiphp_bus_add(func);
|
|
|
|
|
2005-04-28 07:25:53 +00:00
|
|
|
num = pci_scan_slot(bus, PCI_DEVFN(slot->device, 0));
|
|
|
|
if (num == 0) {
|
PCI: Can continually add funcs after adding func0
Boot up a KVM guest, and hotplug multifunction
devices(func1,func2,func0,func3) to guest.
for i in 1 2 0 3;do
qemu-img create /tmp/resize$i.qcow2 1G -f qcow2
(qemu) drive_add 0x11.$i id=drv11$i,if=none,file=/tmp/resize$i.qcow2
(qemu) device_add virtio-blk-pci,id=dev11$i,drive=drv11$i,addr=0x11.$i,multifunction=on
done
In linux kernel, when func0 of the slot is hot-added, the whole
slot will be marked as 'enabled', then driver will ignore other new
hotadded funcs.
But in Win7 & WinXP, we can continaully add other funcs after adding
func0, all funcs will be added in guest.
drivers/pci/hotplug/acpiphp_glue.c:
static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
{
....
for (slot = bridge->slots; slot; slot = slot->next) {
if (slot->flags & SLOT_ENABLED) {
acpiphp_disable_slot()
else
acpiphp_enable_slot()
.... |
} v
enable_device()
|
v
//only don't enable slot if func0 is not added
list_for_each_entry(func, &slot->funcs, sibling) {
...
}
slot->flags |= SLOT_ENABLED; //mark slot to 'enabled'
This patch just make pci driver can continaully add funcs after adding
func 0. Only mark slot to 'enabled' when all funcs are added.
For pci multifunction hotplug, we can add functions one by one(func 0 is
necessary), and all functions will be removed in one time.
Signed-off-by: Amos Kong <akong@redhat.com>
Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
2011-11-25 07:03:07 +00:00
|
|
|
/* Maybe only part of funcs are added. */
|
|
|
|
dbg("No new device found\n");
|
2005-04-16 22:20:36 +00:00
|
|
|
goto err_exit;
|
|
|
|
}
|
|
|
|
|
2006-02-24 01:55:58 +00:00
|
|
|
max = acpiphp_max_busnr(bus);
|
2005-04-28 07:25:53 +00:00
|
|
|
for (pass = 0; pass < 2; pass++) {
|
|
|
|
list_for_each_entry(dev, &bus->devices, bus_list) {
|
|
|
|
if (PCI_SLOT(dev->devfn) != slot->device)
|
|
|
|
continue;
|
|
|
|
if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
|
2005-12-14 17:37:26 +00:00
|
|
|
dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) {
|
2005-04-28 07:25:53 +00:00
|
|
|
max = pci_scan_bridge(bus, dev, max, pass);
|
2013-01-21 21:20:42 +00:00
|
|
|
if (pass && dev->subordinate) {
|
|
|
|
check_hotplug_bridge(slot, dev);
|
PCI / ACPI: Use boot-time resource allocation rules during hotplug
On x86 platforms, the kernel respects PCI resource assignments from
the BIOS and only reassigns resources for unassigned BARs at boot
time. However, with the ACPI-based hotplug (acpiphp), it ignores the
BIOS' PCI resource assignments completely and reassigns all resources
by itself. This causes differences in PCI resource allocation
between boot time and runtime hotplug to occur, which is generally
undesirable and sometimes actively breaks things.
Namely, if there are enough resources, reassigning all PCI resources
during runtime hotplug should work, but it may fail if the resources
are constrained. This may happen, for instance, when some PCI
devices with huge MMIO BARs are involved in the runtime hotplug
operations, because the current PCI MMIO alignment algorithm may
waste huge chunks of MMIO address space in those cases.
On the Alexander's Sony VAIO VPCZ23A4R the BIOS allocates limited
MMIO resources for the dock station which contains a device
(graphics adapter) with a 256MB MMIO BAR. An attempt to reassign
that during runtime hotplug causes the dock station MMIO window to be
exhausted and acpiphp fails to allocate resources for the majority
of devices on the dock station as a result.
To prevent that from happening, modify acpiphp to follow the boot
time resources allocation behavior so that the BIOS' resource
assignments are respected during runtime hotplug too.
[rjw: Changelog]
References: https://bugzilla.kernel.org/show_bug.cgi?id=56531
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Jiang Liu <jiang.liu@huawei.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2013-06-22 23:01:35 +00:00
|
|
|
pcibios_resource_survey_bus(dev->subordinate);
|
|
|
|
__pci_bus_size_bridges(dev->subordinate,
|
|
|
|
&add_list);
|
2013-01-21 21:20:42 +00:00
|
|
|
}
|
2005-12-14 17:37:26 +00:00
|
|
|
}
|
2005-04-28 07:25:53 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
PCI / ACPI: Use boot-time resource allocation rules during hotplug
On x86 platforms, the kernel respects PCI resource assignments from
the BIOS and only reassigns resources for unassigned BARs at boot
time. However, with the ACPI-based hotplug (acpiphp), it ignores the
BIOS' PCI resource assignments completely and reassigns all resources
by itself. This causes differences in PCI resource allocation
between boot time and runtime hotplug to occur, which is generally
undesirable and sometimes actively breaks things.
Namely, if there are enough resources, reassigning all PCI resources
during runtime hotplug should work, but it may fail if the resources
are constrained. This may happen, for instance, when some PCI
devices with huge MMIO BARs are involved in the runtime hotplug
operations, because the current PCI MMIO alignment algorithm may
waste huge chunks of MMIO address space in those cases.
On the Alexander's Sony VAIO VPCZ23A4R the BIOS allocates limited
MMIO resources for the dock station which contains a device
(graphics adapter) with a 256MB MMIO BAR. An attempt to reassign
that during runtime hotplug causes the dock station MMIO window to be
exhausted and acpiphp fails to allocate resources for the majority
of devices on the dock station as a result.
To prevent that from happening, modify acpiphp to follow the boot
time resources allocation behavior so that the BIOS' resource
assignments are respected during runtime hotplug too.
[rjw: Changelog]
References: https://bugzilla.kernel.org/show_bug.cgi?id=56531
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Jiang Liu <jiang.liu@huawei.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
2013-06-22 23:01:35 +00:00
|
|
|
__pci_bus_assign_resources(bus, &add_list, NULL);
|
2005-10-19 00:21:40 +00:00
|
|
|
acpiphp_sanitize_bus(bus);
|
2009-09-14 22:35:10 +00:00
|
|
|
acpiphp_set_hpp_values(bus);
|
2010-02-25 02:59:34 +00:00
|
|
|
acpiphp_set_acpi_region(slot);
|
2005-10-19 00:21:40 +00:00
|
|
|
pci_enable_bridges(bus);
|
2011-05-11 16:00:32 +00:00
|
|
|
|
|
|
|
list_for_each_entry(dev, &bus->devices, bus_list) {
|
|
|
|
/* Assume that newly added devices are powered on already. */
|
|
|
|
if (!dev->is_added)
|
|
|
|
dev->current_state = PCI_D0;
|
|
|
|
}
|
|
|
|
|
2005-04-28 07:25:53 +00:00
|
|
|
pci_bus_add_devices(bus);
|
|
|
|
|
PCI: Can continually add funcs after adding func0
Boot up a KVM guest, and hotplug multifunction
devices(func1,func2,func0,func3) to guest.
for i in 1 2 0 3;do
qemu-img create /tmp/resize$i.qcow2 1G -f qcow2
(qemu) drive_add 0x11.$i id=drv11$i,if=none,file=/tmp/resize$i.qcow2
(qemu) device_add virtio-blk-pci,id=dev11$i,drive=drv11$i,addr=0x11.$i,multifunction=on
done
In linux kernel, when func0 of the slot is hot-added, the whole
slot will be marked as 'enabled', then driver will ignore other new
hotadded funcs.
But in Win7 & WinXP, we can continaully add other funcs after adding
func0, all funcs will be added in guest.
drivers/pci/hotplug/acpiphp_glue.c:
static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
{
....
for (slot = bridge->slots; slot; slot = slot->next) {
if (slot->flags & SLOT_ENABLED) {
acpiphp_disable_slot()
else
acpiphp_enable_slot()
.... |
} v
enable_device()
|
v
//only don't enable slot if func0 is not added
list_for_each_entry(func, &slot->funcs, sibling) {
...
}
slot->flags |= SLOT_ENABLED; //mark slot to 'enabled'
This patch just make pci driver can continaully add funcs after adding
func 0. Only mark slot to 'enabled' when all funcs are added.
For pci multifunction hotplug, we can add functions one by one(func 0 is
necessary), and all functions will be removed in one time.
Signed-off-by: Amos Kong <akong@redhat.com>
Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
2011-11-25 07:03:07 +00:00
|
|
|
slot->flags |= SLOT_ENABLED;
|
2009-10-27 03:25:27 +00:00
|
|
|
list_for_each_entry(func, &slot->funcs, sibling) {
|
2009-05-21 22:21:15 +00:00
|
|
|
dev = pci_get_slot(bus, PCI_DEVFN(slot->device,
|
|
|
|
func->function));
|
PCI: Can continually add funcs after adding func0
Boot up a KVM guest, and hotplug multifunction
devices(func1,func2,func0,func3) to guest.
for i in 1 2 0 3;do
qemu-img create /tmp/resize$i.qcow2 1G -f qcow2
(qemu) drive_add 0x11.$i id=drv11$i,if=none,file=/tmp/resize$i.qcow2
(qemu) device_add virtio-blk-pci,id=dev11$i,drive=drv11$i,addr=0x11.$i,multifunction=on
done
In linux kernel, when func0 of the slot is hot-added, the whole
slot will be marked as 'enabled', then driver will ignore other new
hotadded funcs.
But in Win7 & WinXP, we can continaully add other funcs after adding
func0, all funcs will be added in guest.
drivers/pci/hotplug/acpiphp_glue.c:
static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
{
....
for (slot = bridge->slots; slot; slot = slot->next) {
if (slot->flags & SLOT_ENABLED) {
acpiphp_disable_slot()
else
acpiphp_enable_slot()
.... |
} v
enable_device()
|
v
//only don't enable slot if func0 is not added
list_for_each_entry(func, &slot->funcs, sibling) {
...
}
slot->flags |= SLOT_ENABLED; //mark slot to 'enabled'
This patch just make pci driver can continaully add funcs after adding
func 0. Only mark slot to 'enabled' when all funcs are added.
For pci multifunction hotplug, we can add functions one by one(func 0 is
necessary), and all functions will be removed in one time.
Signed-off-by: Amos Kong <akong@redhat.com>
Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
2011-11-25 07:03:07 +00:00
|
|
|
if (!dev) {
|
|
|
|
/* Do not set SLOT_ENABLED flag if some funcs
|
|
|
|
are not added. */
|
|
|
|
slot->flags &= (~SLOT_ENABLED);
|
2006-03-22 05:49:20 +00:00
|
|
|
continue;
|
PCI: Can continually add funcs after adding func0
Boot up a KVM guest, and hotplug multifunction
devices(func1,func2,func0,func3) to guest.
for i in 1 2 0 3;do
qemu-img create /tmp/resize$i.qcow2 1G -f qcow2
(qemu) drive_add 0x11.$i id=drv11$i,if=none,file=/tmp/resize$i.qcow2
(qemu) device_add virtio-blk-pci,id=dev11$i,drive=drv11$i,addr=0x11.$i,multifunction=on
done
In linux kernel, when func0 of the slot is hot-added, the whole
slot will be marked as 'enabled', then driver will ignore other new
hotadded funcs.
But in Win7 & WinXP, we can continaully add other funcs after adding
func0, all funcs will be added in guest.
drivers/pci/hotplug/acpiphp_glue.c:
static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
{
....
for (slot = bridge->slots; slot; slot = slot->next) {
if (slot->flags & SLOT_ENABLED) {
acpiphp_disable_slot()
else
acpiphp_enable_slot()
.... |
} v
enable_device()
|
v
//only don't enable slot if func0 is not added
list_for_each_entry(func, &slot->funcs, sibling) {
...
}
slot->flags |= SLOT_ENABLED; //mark slot to 'enabled'
This patch just make pci driver can continaully add funcs after adding
func 0. Only mark slot to 'enabled' when all funcs are added.
For pci multifunction hotplug, we can add functions one by one(func 0 is
necessary), and all functions will be removed in one time.
Signed-off-by: Amos Kong <akong@redhat.com>
Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
2011-11-25 07:03:07 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
err_exit:
|
2013-04-12 05:44:28 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 16:20:35 +00:00
|
|
|
/* return first device in slot, acquiring a reference on it */
|
|
|
|
static struct pci_dev *dev_in_slot(struct acpiphp_slot *slot)
|
|
|
|
{
|
|
|
|
struct pci_bus *bus = slot->bridge->pci_bus;
|
|
|
|
struct pci_dev *dev;
|
|
|
|
struct pci_dev *ret = NULL;
|
|
|
|
|
|
|
|
down_read(&pci_bus_sem);
|
|
|
|
list_for_each_entry(dev, &bus->devices, bus_list)
|
|
|
|
if (PCI_SLOT(dev->devfn) == slot->device) {
|
|
|
|
ret = pci_dev_get(dev);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
up_read(&pci_bus_sem);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
|
|
|
* disable_device - disable a slot
|
2007-11-28 17:04:30 +00:00
|
|
|
* @slot: ACPI PHP slot
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
static int disable_device(struct acpiphp_slot *slot)
|
|
|
|
{
|
|
|
|
struct acpiphp_func *func;
|
2009-05-21 22:21:15 +00:00
|
|
|
struct pci_dev *pdev;
|
2006-03-22 05:49:20 +00:00
|
|
|
|
2012-05-23 16:20:35 +00:00
|
|
|
/*
|
|
|
|
* enable_device() enumerates all functions in this device via
|
|
|
|
* pci_scan_slot(), whether they have associated ACPI hotplug
|
|
|
|
* methods (_EJ0, etc.) or not. Therefore, we remove all functions
|
|
|
|
* here.
|
|
|
|
*/
|
|
|
|
while ((pdev = dev_in_slot(slot))) {
|
2012-08-17 16:03:47 +00:00
|
|
|
pci_stop_and_remove_bus_device(pdev);
|
2012-05-23 16:20:35 +00:00
|
|
|
pci_dev_put(pdev);
|
2006-09-12 17:22:53 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 21:27:25 +00:00
|
|
|
list_for_each_entry(func, &slot->funcs, sibling)
|
|
|
|
acpiphp_bus_trim(func_to_handle(func));
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
slot->flags &= (~SLOT_ENABLED);
|
|
|
|
|
2009-05-21 22:21:15 +00:00
|
|
|
return 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get_slot_status - get ACPI slot status
|
2007-11-28 17:04:30 +00:00
|
|
|
* @slot: ACPI PHP slot
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2007-11-28 17:04:30 +00:00
|
|
|
* If a slot has _STA for each function and if any one of them
|
|
|
|
* returned non-zero status, return it.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2007-11-28 17:04:30 +00:00
|
|
|
* If a slot doesn't have _STA and if any one of its functions'
|
|
|
|
* configuration space is configured, return 0x0f as a _STA.
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
2007-11-28 17:04:30 +00:00
|
|
|
* Otherwise return 0.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
static unsigned int get_slot_status(struct acpiphp_slot *slot)
|
|
|
|
{
|
2008-10-10 06:22:59 +00:00
|
|
|
unsigned long long sta = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct acpiphp_func *func;
|
|
|
|
|
2009-10-27 03:25:27 +00:00
|
|
|
list_for_each_entry(func, &slot->funcs, sibling) {
|
2005-04-16 22:20:36 +00:00
|
|
|
if (func->flags & FUNC_HAS_STA) {
|
2013-07-13 21:27:25 +00:00
|
|
|
acpi_status status;
|
|
|
|
|
|
|
|
status = acpi_evaluate_integer(func_to_handle(func),
|
|
|
|
"_STA", NULL, &sta);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (ACPI_SUCCESS(status) && sta)
|
|
|
|
break;
|
|
|
|
} else {
|
2013-07-13 21:27:25 +00:00
|
|
|
u32 dvid;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
pci_bus_read_config_dword(slot->bridge->pci_bus,
|
|
|
|
PCI_DEVFN(slot->device,
|
|
|
|
func->function),
|
|
|
|
PCI_VENDOR_ID, &dvid);
|
|
|
|
if (dvid != 0xffffffff) {
|
|
|
|
sta = ACPI_STA_ALL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (unsigned int)sta;
|
|
|
|
}
|
|
|
|
|
2005-04-28 07:25:57 +00:00
|
|
|
/**
|
|
|
|
* acpiphp_eject_slot - physically eject the slot
|
2007-11-28 17:04:30 +00:00
|
|
|
* @slot: ACPI PHP slot
|
2005-04-28 07:25:57 +00:00
|
|
|
*/
|
2007-07-05 18:10:46 +00:00
|
|
|
int acpiphp_eject_slot(struct acpiphp_slot *slot)
|
2005-04-28 07:25:57 +00:00
|
|
|
{
|
|
|
|
struct acpiphp_func *func;
|
|
|
|
|
2009-10-27 03:25:27 +00:00
|
|
|
list_for_each_entry(func, &slot->funcs, sibling) {
|
2005-04-28 07:25:57 +00:00
|
|
|
/* We don't want to call _EJ0 on non-existing functions. */
|
2013-07-13 21:27:25 +00:00
|
|
|
if (!(func->flags & FUNC_HAS_EJ0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ACPI_FAILURE(acpi_evaluate_ej0(func_to_handle(func))))
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
break;
|
2005-04-28 07:25:57 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/**
|
|
|
|
* acpiphp_check_bridge - re-enumerate devices
|
2007-11-28 17:04:30 +00:00
|
|
|
* @bridge: where to begin re-enumeration
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* Iterate over all slots under this bridge and make sure that if a
|
|
|
|
* card is present they are enabled, and if not they are disabled.
|
|
|
|
*/
|
|
|
|
static int acpiphp_check_bridge(struct acpiphp_bridge *bridge)
|
|
|
|
{
|
|
|
|
struct acpiphp_slot *slot;
|
|
|
|
int retval = 0;
|
|
|
|
int enabled, disabled;
|
|
|
|
|
|
|
|
enabled = disabled = 0;
|
|
|
|
|
2013-04-12 05:44:27 +00:00
|
|
|
list_for_each_entry(slot, &bridge->slots, node) {
|
2005-04-16 22:20:36 +00:00
|
|
|
unsigned int status = get_slot_status(slot);
|
|
|
|
if (slot->flags & SLOT_ENABLED) {
|
|
|
|
if (status == ACPI_STA_ALL)
|
|
|
|
continue;
|
|
|
|
retval = acpiphp_disable_slot(slot);
|
|
|
|
if (retval) {
|
|
|
|
err("Error occurred in disabling\n");
|
|
|
|
goto err_exit;
|
2005-04-28 07:25:57 +00:00
|
|
|
} else {
|
|
|
|
acpiphp_eject_slot(slot);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
disabled++;
|
|
|
|
} else {
|
|
|
|
if (status != ACPI_STA_ALL)
|
|
|
|
continue;
|
|
|
|
retval = acpiphp_enable_slot(slot);
|
|
|
|
if (retval) {
|
|
|
|
err("Error occurred in enabling\n");
|
|
|
|
goto err_exit;
|
|
|
|
}
|
|
|
|
enabled++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-04 03:09:46 +00:00
|
|
|
dbg("%s: %d enabled, %d disabled\n", __func__, enabled, disabled);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
err_exit:
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2009-09-14 22:35:10 +00:00
|
|
|
static void acpiphp_set_hpp_values(struct pci_bus *bus)
|
2005-04-28 07:25:56 +00:00
|
|
|
{
|
|
|
|
struct pci_dev *dev;
|
|
|
|
|
2009-09-14 22:35:35 +00:00
|
|
|
list_for_each_entry(dev, &bus->devices, bus_list)
|
|
|
|
pci_configure_slot(dev);
|
2005-04-28 07:25:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove devices for which we could not assign resources, call
|
|
|
|
* arch specific code to fix-up the bus
|
|
|
|
*/
|
|
|
|
static void acpiphp_sanitize_bus(struct pci_bus *bus)
|
|
|
|
{
|
2013-04-12 05:44:17 +00:00
|
|
|
struct pci_dev *dev, *tmp;
|
2005-04-28 07:25:56 +00:00
|
|
|
int i;
|
|
|
|
unsigned long type_mask = IORESOURCE_IO | IORESOURCE_MEM;
|
|
|
|
|
2013-04-12 05:44:17 +00:00
|
|
|
list_for_each_entry_safe(dev, tmp, &bus->devices, bus_list) {
|
2005-04-28 07:25:56 +00:00
|
|
|
for (i=0; i<PCI_BRIDGE_RESOURCES; i++) {
|
|
|
|
struct resource *res = &dev->resource[i];
|
|
|
|
if ((res->flags & type_mask) && !res->start &&
|
|
|
|
res->end) {
|
|
|
|
/* Could not assign a required resources
|
|
|
|
* for this device, remove it */
|
2012-02-25 21:54:20 +00:00
|
|
|
pci_stop_and_remove_bus_device(dev);
|
2005-04-28 07:25:56 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* ACPI event handlers
|
|
|
|
*/
|
|
|
|
|
2007-07-05 18:10:48 +00:00
|
|
|
static acpi_status
|
|
|
|
check_sub_bridges(acpi_handle handle, u32 lvl, void *context, void **rv)
|
|
|
|
{
|
|
|
|
struct acpiphp_bridge *bridge;
|
|
|
|
char objname[64];
|
|
|
|
struct acpi_buffer buffer = { .length = sizeof(objname),
|
|
|
|
.pointer = objname };
|
|
|
|
|
|
|
|
bridge = acpiphp_handle_to_bridge(handle);
|
|
|
|
if (bridge) {
|
|
|
|
acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
|
|
|
|
dbg("%s: re-enumerating slots under %s\n",
|
2008-03-04 03:09:46 +00:00
|
|
|
__func__, objname);
|
2007-07-05 18:10:48 +00:00
|
|
|
acpiphp_check_bridge(bridge);
|
2013-04-12 05:44:28 +00:00
|
|
|
put_bridge(bridge);
|
2007-07-05 18:10:48 +00:00
|
|
|
}
|
|
|
|
return AE_OK ;
|
|
|
|
}
|
|
|
|
|
2013-05-07 17:06:03 +00:00
|
|
|
void acpiphp_check_host_bridge(acpi_handle handle)
|
|
|
|
{
|
|
|
|
struct acpiphp_bridge *bridge;
|
|
|
|
|
|
|
|
bridge = acpiphp_handle_to_bridge(handle);
|
|
|
|
if (bridge) {
|
|
|
|
acpiphp_check_bridge(bridge);
|
|
|
|
put_bridge(bridge);
|
|
|
|
}
|
|
|
|
|
|
|
|
acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
|
|
|
|
ACPI_UINT32_MAX, check_sub_bridges, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
static void hotplug_event(acpi_handle handle, u32 type, void *data)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-07-13 21:27:24 +00:00
|
|
|
struct acpiphp_context *context = data;
|
2013-07-13 21:27:25 +00:00
|
|
|
struct acpiphp_func *func = &context->func;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct acpiphp_bridge *bridge;
|
|
|
|
char objname[64];
|
|
|
|
struct acpi_buffer buffer = { .length = sizeof(objname),
|
|
|
|
.pointer = objname };
|
2011-09-28 23:40:53 +00:00
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
mutex_lock(&acpiphp_context_lock);
|
2013-07-13 21:27:24 +00:00
|
|
|
bridge = context->bridge;
|
2013-07-13 21:27:24 +00:00
|
|
|
if (bridge)
|
|
|
|
get_bridge(bridge);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
mutex_unlock(&acpiphp_context_lock);
|
ACPI / hotplug: Fix concurrency issues and memory leaks
This changeset is aimed at fixing a few different but related
problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(), acpi_bus_trim() and acpi_bus_hot_remove_device()
and some of them are installed for ACPI handles that have no struct
acpi_device objects attached (i.e. before those objects are created),
those notify handlers have to take acpi_scan_lock to prevent races
from taking place (e.g. a struct acpi_device is found to be present
for the given ACPI handle, but right after that it is removed by
acpi_bus_trim() running in parallel to the given notify handler).
Moreover, since some of them call acpi_bus_scan() and
acpi_bus_trim(), this leads to the conclusion that acpi_scan_lock
should be acquired by the callers of these two funtions rather by
these functions themselves.
For these reasons, make all notify handlers that can handle device
addition and eject events take acpi_scan_lock and remove the
acpi_scan_lock locking from acpi_bus_scan() and acpi_bus_trim().
Accordingly, update all of their users to make sure that they
are always called under acpi_scan_lock.
Furthermore, since eject operations are carried out asynchronously
with respect to the notify events that trigger them, with the help
of acpi_bus_hot_remove_device(), even if notify handlers take the
ACPI scan lock, it still is possible that, for example,
acpi_bus_trim() will run between acpi_bus_hot_remove_device() and
the notify handler that scheduled its execution and that
acpi_bus_trim() will remove the device node passed to
acpi_bus_hot_remove_device() for ejection. In that case, the struct
acpi_device object obtained by acpi_bus_hot_remove_device() will be
invalid and not-so-funny things will ensue. To protect agaist that,
make the users of acpi_bus_hot_remove_device() run get_device() on
ACPI device node objects that are about to be passed to it and make
acpi_bus_hot_remove_device() run put_device() on them and check if
their ACPI handles are not NULL (make acpi_device_unregister() clear
the device nodes' ACPI handles for that check to work).
Finally, observe that acpi_os_hotplug_execute() actually can fail,
in which case its caller ought to free memory allocated for the
context object to prevent leaks from happening. It also needs to
run put_device() on the device node that it ran get_device() on
previously in that case. Modify the code accordingly.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
2013-02-13 13:36:47 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case ACPI_NOTIFY_BUS_CHECK:
|
|
|
|
/* bus re-enumerate */
|
2008-03-04 03:09:46 +00:00
|
|
|
dbg("%s: Bus check notify on %s\n", __func__, objname);
|
2013-01-30 16:10:10 +00:00
|
|
|
dbg("%s: re-enumerating slots under %s\n", __func__, objname);
|
2013-07-13 21:27:24 +00:00
|
|
|
if (bridge) {
|
|
|
|
acpiphp_check_bridge(bridge);
|
|
|
|
acpi_walk_namespace(ACPI_TYPE_DEVICE, handle,
|
|
|
|
ACPI_UINT32_MAX, check_sub_bridges,
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
} else {
|
|
|
|
acpiphp_enable_slot(func->slot);
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ACPI_NOTIFY_DEVICE_CHECK:
|
|
|
|
/* device check */
|
2008-03-04 03:09:46 +00:00
|
|
|
dbg("%s: Device check notify on %s\n", __func__, objname);
|
2013-07-13 21:27:24 +00:00
|
|
|
if (bridge)
|
|
|
|
acpiphp_check_bridge(bridge);
|
|
|
|
else
|
|
|
|
acpiphp_check_bridge(func->slot->bridge);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ACPI_NOTIFY_DEVICE_WAKE:
|
|
|
|
/* wake event */
|
2008-03-04 03:09:46 +00:00
|
|
|
dbg("%s: Device wake notify on %s\n", __func__, objname);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ACPI_NOTIFY_EJECT_REQUEST:
|
|
|
|
/* request device eject */
|
2008-03-04 03:09:46 +00:00
|
|
|
dbg("%s: Device eject notify on %s\n", __func__, objname);
|
2013-07-13 21:27:24 +00:00
|
|
|
if (!(acpiphp_disable_slot(func->slot)))
|
|
|
|
acpiphp_eject_slot(func->slot);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ACPI_NOTIFY_FREQUENCY_MISMATCH:
|
|
|
|
printk(KERN_ERR "Device %s cannot be configured due"
|
|
|
|
" to a frequency mismatch\n", objname);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACPI_NOTIFY_BUS_MODE_MISMATCH:
|
|
|
|
printk(KERN_ERR "Device %s cannot be configured due"
|
|
|
|
" to a bus mode mismatch\n", objname);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ACPI_NOTIFY_POWER_FAULT:
|
|
|
|
printk(KERN_ERR "Device %s has suffered a power fault\n",
|
|
|
|
objname);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2013-07-13 21:27:24 +00:00
|
|
|
warn("notify_handler: unknown event type 0x%x for %s\n", type,
|
|
|
|
objname);
|
2005-04-16 22:20:36 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-09-28 23:40:53 +00:00
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
if (bridge)
|
|
|
|
put_bridge(bridge);
|
ACPI / dock / PCI: Synchronous handling of dock events for PCI devices
The interactions between the ACPI dock driver and the ACPI-based PCI
hotplug (acpiphp) are currently problematic because of ordering
issues during hot-remove operations.
First of all, the current ACPI glue code expects that physical
devices will always be deleted before deleting the companion ACPI
device objects. Otherwise, acpi_unbind_one() will fail with a
warning message printed to the kernel log, for example:
[ 185.026073] usb usb5: Oops, 'acpi_handle' corrupt
[ 185.035150] pci 0000:1b:00.0: Oops, 'acpi_handle' corrupt
[ 185.035515] pci 0000:18:02.0: Oops, 'acpi_handle' corrupt
[ 180.013656] port1: Oops, 'acpi_handle' corrupt
This means, in particular, that struct pci_dev objects have to
be deleted before the struct acpi_device objects they are "glued"
with.
Now, the following happens the during the undocking of an ACPI-based
dock station:
1) hotplug_dock_devices() invokes registered hotplug callbacks to
destroy physical devices associated with the ACPI device objects
depending on the dock station. It calls dd->ops->handler() for
each of those device objects.
2) For PCI devices dd->ops->handler() points to
handle_hotplug_event_func() that queues up a separate work item
to execute _handle_hotplug_event_func() for the given device and
returns immediately. That work item will be executed later.
3) hotplug_dock_devices() calls dock_remove_acpi_device() for each
device depending on the dock station. This runs acpi_bus_trim()
for each of them, which causes the underlying ACPI device object
to be destroyed, but the work items queued up by
handle_hotplug_event_func() haven't been started yet.
4) _handle_hotplug_event_func() queued up in step 2) are executed
and cause the above failure to happen, because the PCI devices
they handle do not have the companion ACPI device objects any
more (those objects have been deleted in step 3).
The possible breakage doesn't end here, though, because
hotplug_dock_devices() may return before at least some of the
_handle_hotplug_event_func() work items spawned by it have a
chance to complete and then undock() will cause _DCK to be
evaluated and that will cause the devices handled by the
_handle_hotplug_event_func() to go away possibly while they are
being accessed.
This means that dd->ops->handler() for PCI devices should not point
to handle_hotplug_event_func(). Instead, it should point to a
function that will do the work of _handle_hotplug_event_func()
synchronously. For this reason, introduce such a function,
hotplug_event_func(), and modity acpiphp_dock_ops to point to
it as the handler.
Unfortunately, however, this is not sufficient, because if the dock
code were not changed further, hotplug_event_func() would now
deadlock with hotplug_dock_devices() that called it, since it would
run unregister_hotplug_dock_device() which in turn would attempt to
acquire the dock station's hp_lock mutex already acquired by
hotplug_dock_devices().
To resolve that deadlock use the observation that
unregister_hotplug_dock_device() won't need to acquire hp_lock
if PCI bridges the devices on the dock station depend on are
prevented from being removed prematurely while the first loop in
hotplug_dock_devices() is in progress.
To make that possible, introduce a mechanism by which the callers of
register_hotplug_dock_device() can provide "init" and "release"
routines that will be executed, respectively, during the addition
and removal of the physical device object associated with the
given ACPI device handle. Make acpiphp use two new functions,
acpiphp_dock_init() and acpiphp_dock_release(), that call
get_bridge() and put_bridge(), respectively, on the acpiphp bridge
holding the given device, for this purpose.
In addition to that, remove the dock station's list of
"hotplug devices" and make the dock code always walk the whole list
of "dependent devices" instead in such a way that the loops in
hotplug_dock_devices() and dock_event() (replacing the loops over
"hotplug devices") will take references to the list entries that
register_hotplug_dock_device() has been called for. That prevents
the "release" routines associated with those entries from being
called while the given entry is being processed and for PCI
devices this means that their bridges won't be removed (by a
concurrent thread) while hotplug_event_func() handling them is
being executed.
This change is based on two earlier patches from Jiang Liu.
References: https://bugzilla.kernel.org/show_bug.cgi?id=59501
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tracked-down-by: Jiang Liu <jiang.liu@huawei.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
2013-06-24 09:22:53 +00:00
|
|
|
}
|
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
static void hotplug_event_work(struct work_struct *work)
|
ACPI / dock / PCI: Synchronous handling of dock events for PCI devices
The interactions between the ACPI dock driver and the ACPI-based PCI
hotplug (acpiphp) are currently problematic because of ordering
issues during hot-remove operations.
First of all, the current ACPI glue code expects that physical
devices will always be deleted before deleting the companion ACPI
device objects. Otherwise, acpi_unbind_one() will fail with a
warning message printed to the kernel log, for example:
[ 185.026073] usb usb5: Oops, 'acpi_handle' corrupt
[ 185.035150] pci 0000:1b:00.0: Oops, 'acpi_handle' corrupt
[ 185.035515] pci 0000:18:02.0: Oops, 'acpi_handle' corrupt
[ 180.013656] port1: Oops, 'acpi_handle' corrupt
This means, in particular, that struct pci_dev objects have to
be deleted before the struct acpi_device objects they are "glued"
with.
Now, the following happens the during the undocking of an ACPI-based
dock station:
1) hotplug_dock_devices() invokes registered hotplug callbacks to
destroy physical devices associated with the ACPI device objects
depending on the dock station. It calls dd->ops->handler() for
each of those device objects.
2) For PCI devices dd->ops->handler() points to
handle_hotplug_event_func() that queues up a separate work item
to execute _handle_hotplug_event_func() for the given device and
returns immediately. That work item will be executed later.
3) hotplug_dock_devices() calls dock_remove_acpi_device() for each
device depending on the dock station. This runs acpi_bus_trim()
for each of them, which causes the underlying ACPI device object
to be destroyed, but the work items queued up by
handle_hotplug_event_func() haven't been started yet.
4) _handle_hotplug_event_func() queued up in step 2) are executed
and cause the above failure to happen, because the PCI devices
they handle do not have the companion ACPI device objects any
more (those objects have been deleted in step 3).
The possible breakage doesn't end here, though, because
hotplug_dock_devices() may return before at least some of the
_handle_hotplug_event_func() work items spawned by it have a
chance to complete and then undock() will cause _DCK to be
evaluated and that will cause the devices handled by the
_handle_hotplug_event_func() to go away possibly while they are
being accessed.
This means that dd->ops->handler() for PCI devices should not point
to handle_hotplug_event_func(). Instead, it should point to a
function that will do the work of _handle_hotplug_event_func()
synchronously. For this reason, introduce such a function,
hotplug_event_func(), and modity acpiphp_dock_ops to point to
it as the handler.
Unfortunately, however, this is not sufficient, because if the dock
code were not changed further, hotplug_event_func() would now
deadlock with hotplug_dock_devices() that called it, since it would
run unregister_hotplug_dock_device() which in turn would attempt to
acquire the dock station's hp_lock mutex already acquired by
hotplug_dock_devices().
To resolve that deadlock use the observation that
unregister_hotplug_dock_device() won't need to acquire hp_lock
if PCI bridges the devices on the dock station depend on are
prevented from being removed prematurely while the first loop in
hotplug_dock_devices() is in progress.
To make that possible, introduce a mechanism by which the callers of
register_hotplug_dock_device() can provide "init" and "release"
routines that will be executed, respectively, during the addition
and removal of the physical device object associated with the
given ACPI device handle. Make acpiphp use two new functions,
acpiphp_dock_init() and acpiphp_dock_release(), that call
get_bridge() and put_bridge(), respectively, on the acpiphp bridge
holding the given device, for this purpose.
In addition to that, remove the dock station's list of
"hotplug devices" and make the dock code always walk the whole list
of "dependent devices" instead in such a way that the loops in
hotplug_dock_devices() and dock_event() (replacing the loops over
"hotplug devices") will take references to the list entries that
register_hotplug_dock_device() has been called for. That prevents
the "release" routines associated with those entries from being
called while the given entry is being processed and for PCI
devices this means that their bridges won't be removed (by a
concurrent thread) while hotplug_event_func() handling them is
being executed.
This change is based on two earlier patches from Jiang Liu.
References: https://bugzilla.kernel.org/show_bug.cgi?id=59501
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tracked-down-by: Jiang Liu <jiang.liu@huawei.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
2013-06-24 09:22:53 +00:00
|
|
|
{
|
2013-07-13 21:27:24 +00:00
|
|
|
struct acpiphp_context *context;
|
ACPI / dock / PCI: Synchronous handling of dock events for PCI devices
The interactions between the ACPI dock driver and the ACPI-based PCI
hotplug (acpiphp) are currently problematic because of ordering
issues during hot-remove operations.
First of all, the current ACPI glue code expects that physical
devices will always be deleted before deleting the companion ACPI
device objects. Otherwise, acpi_unbind_one() will fail with a
warning message printed to the kernel log, for example:
[ 185.026073] usb usb5: Oops, 'acpi_handle' corrupt
[ 185.035150] pci 0000:1b:00.0: Oops, 'acpi_handle' corrupt
[ 185.035515] pci 0000:18:02.0: Oops, 'acpi_handle' corrupt
[ 180.013656] port1: Oops, 'acpi_handle' corrupt
This means, in particular, that struct pci_dev objects have to
be deleted before the struct acpi_device objects they are "glued"
with.
Now, the following happens the during the undocking of an ACPI-based
dock station:
1) hotplug_dock_devices() invokes registered hotplug callbacks to
destroy physical devices associated with the ACPI device objects
depending on the dock station. It calls dd->ops->handler() for
each of those device objects.
2) For PCI devices dd->ops->handler() points to
handle_hotplug_event_func() that queues up a separate work item
to execute _handle_hotplug_event_func() for the given device and
returns immediately. That work item will be executed later.
3) hotplug_dock_devices() calls dock_remove_acpi_device() for each
device depending on the dock station. This runs acpi_bus_trim()
for each of them, which causes the underlying ACPI device object
to be destroyed, but the work items queued up by
handle_hotplug_event_func() haven't been started yet.
4) _handle_hotplug_event_func() queued up in step 2) are executed
and cause the above failure to happen, because the PCI devices
they handle do not have the companion ACPI device objects any
more (those objects have been deleted in step 3).
The possible breakage doesn't end here, though, because
hotplug_dock_devices() may return before at least some of the
_handle_hotplug_event_func() work items spawned by it have a
chance to complete and then undock() will cause _DCK to be
evaluated and that will cause the devices handled by the
_handle_hotplug_event_func() to go away possibly while they are
being accessed.
This means that dd->ops->handler() for PCI devices should not point
to handle_hotplug_event_func(). Instead, it should point to a
function that will do the work of _handle_hotplug_event_func()
synchronously. For this reason, introduce such a function,
hotplug_event_func(), and modity acpiphp_dock_ops to point to
it as the handler.
Unfortunately, however, this is not sufficient, because if the dock
code were not changed further, hotplug_event_func() would now
deadlock with hotplug_dock_devices() that called it, since it would
run unregister_hotplug_dock_device() which in turn would attempt to
acquire the dock station's hp_lock mutex already acquired by
hotplug_dock_devices().
To resolve that deadlock use the observation that
unregister_hotplug_dock_device() won't need to acquire hp_lock
if PCI bridges the devices on the dock station depend on are
prevented from being removed prematurely while the first loop in
hotplug_dock_devices() is in progress.
To make that possible, introduce a mechanism by which the callers of
register_hotplug_dock_device() can provide "init" and "release"
routines that will be executed, respectively, during the addition
and removal of the physical device object associated with the
given ACPI device handle. Make acpiphp use two new functions,
acpiphp_dock_init() and acpiphp_dock_release(), that call
get_bridge() and put_bridge(), respectively, on the acpiphp bridge
holding the given device, for this purpose.
In addition to that, remove the dock station's list of
"hotplug devices" and make the dock code always walk the whole list
of "dependent devices" instead in such a way that the loops in
hotplug_dock_devices() and dock_event() (replacing the loops over
"hotplug devices") will take references to the list entries that
register_hotplug_dock_device() has been called for. That prevents
the "release" routines associated with those entries from being
called while the given entry is being processed and for PCI
devices this means that their bridges won't be removed (by a
concurrent thread) while hotplug_event_func() handling them is
being executed.
This change is based on two earlier patches from Jiang Liu.
References: https://bugzilla.kernel.org/show_bug.cgi?id=59501
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tracked-down-by: Jiang Liu <jiang.liu@huawei.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
2013-06-24 09:22:53 +00:00
|
|
|
struct acpi_hp_work *hp_work;
|
|
|
|
|
|
|
|
hp_work = container_of(work, struct acpi_hp_work, work);
|
2013-07-13 21:27:24 +00:00
|
|
|
context = hp_work->context;
|
ACPI / dock / PCI: Synchronous handling of dock events for PCI devices
The interactions between the ACPI dock driver and the ACPI-based PCI
hotplug (acpiphp) are currently problematic because of ordering
issues during hot-remove operations.
First of all, the current ACPI glue code expects that physical
devices will always be deleted before deleting the companion ACPI
device objects. Otherwise, acpi_unbind_one() will fail with a
warning message printed to the kernel log, for example:
[ 185.026073] usb usb5: Oops, 'acpi_handle' corrupt
[ 185.035150] pci 0000:1b:00.0: Oops, 'acpi_handle' corrupt
[ 185.035515] pci 0000:18:02.0: Oops, 'acpi_handle' corrupt
[ 180.013656] port1: Oops, 'acpi_handle' corrupt
This means, in particular, that struct pci_dev objects have to
be deleted before the struct acpi_device objects they are "glued"
with.
Now, the following happens the during the undocking of an ACPI-based
dock station:
1) hotplug_dock_devices() invokes registered hotplug callbacks to
destroy physical devices associated with the ACPI device objects
depending on the dock station. It calls dd->ops->handler() for
each of those device objects.
2) For PCI devices dd->ops->handler() points to
handle_hotplug_event_func() that queues up a separate work item
to execute _handle_hotplug_event_func() for the given device and
returns immediately. That work item will be executed later.
3) hotplug_dock_devices() calls dock_remove_acpi_device() for each
device depending on the dock station. This runs acpi_bus_trim()
for each of them, which causes the underlying ACPI device object
to be destroyed, but the work items queued up by
handle_hotplug_event_func() haven't been started yet.
4) _handle_hotplug_event_func() queued up in step 2) are executed
and cause the above failure to happen, because the PCI devices
they handle do not have the companion ACPI device objects any
more (those objects have been deleted in step 3).
The possible breakage doesn't end here, though, because
hotplug_dock_devices() may return before at least some of the
_handle_hotplug_event_func() work items spawned by it have a
chance to complete and then undock() will cause _DCK to be
evaluated and that will cause the devices handled by the
_handle_hotplug_event_func() to go away possibly while they are
being accessed.
This means that dd->ops->handler() for PCI devices should not point
to handle_hotplug_event_func(). Instead, it should point to a
function that will do the work of _handle_hotplug_event_func()
synchronously. For this reason, introduce such a function,
hotplug_event_func(), and modity acpiphp_dock_ops to point to
it as the handler.
Unfortunately, however, this is not sufficient, because if the dock
code were not changed further, hotplug_event_func() would now
deadlock with hotplug_dock_devices() that called it, since it would
run unregister_hotplug_dock_device() which in turn would attempt to
acquire the dock station's hp_lock mutex already acquired by
hotplug_dock_devices().
To resolve that deadlock use the observation that
unregister_hotplug_dock_device() won't need to acquire hp_lock
if PCI bridges the devices on the dock station depend on are
prevented from being removed prematurely while the first loop in
hotplug_dock_devices() is in progress.
To make that possible, introduce a mechanism by which the callers of
register_hotplug_dock_device() can provide "init" and "release"
routines that will be executed, respectively, during the addition
and removal of the physical device object associated with the
given ACPI device handle. Make acpiphp use two new functions,
acpiphp_dock_init() and acpiphp_dock_release(), that call
get_bridge() and put_bridge(), respectively, on the acpiphp bridge
holding the given device, for this purpose.
In addition to that, remove the dock station's list of
"hotplug devices" and make the dock code always walk the whole list
of "dependent devices" instead in such a way that the loops in
hotplug_dock_devices() and dock_event() (replacing the loops over
"hotplug devices") will take references to the list entries that
register_hotplug_dock_device() has been called for. That prevents
the "release" routines associated with those entries from being
called while the given entry is being processed and for PCI
devices this means that their bridges won't be removed (by a
concurrent thread) while hotplug_event_func() handling them is
being executed.
This change is based on two earlier patches from Jiang Liu.
References: https://bugzilla.kernel.org/show_bug.cgi?id=59501
Reported-and-tested-by: Alexander E. Patrakov <patrakov@gmail.com>
Tracked-down-by: Jiang Liu <jiang.liu@huawei.com>
Tested-by: Illya Klymov <xanf@xanf.me>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
Cc: 3.9+ <stable@vger.kernel.org>
2013-06-24 09:22:53 +00:00
|
|
|
acpi_scan_lock_acquire();
|
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
hotplug_event(hp_work->handle, hp_work->type, context);
|
2011-09-28 23:40:53 +00:00
|
|
|
|
ACPI / hotplug: Fix concurrency issues and memory leaks
This changeset is aimed at fixing a few different but related
problems in the ACPI hotplug infrastructure.
First of all, since notify handlers may be run in parallel with
acpi_bus_scan(), acpi_bus_trim() and acpi_bus_hot_remove_device()
and some of them are installed for ACPI handles that have no struct
acpi_device objects attached (i.e. before those objects are created),
those notify handlers have to take acpi_scan_lock to prevent races
from taking place (e.g. a struct acpi_device is found to be present
for the given ACPI handle, but right after that it is removed by
acpi_bus_trim() running in parallel to the given notify handler).
Moreover, since some of them call acpi_bus_scan() and
acpi_bus_trim(), this leads to the conclusion that acpi_scan_lock
should be acquired by the callers of these two funtions rather by
these functions themselves.
For these reasons, make all notify handlers that can handle device
addition and eject events take acpi_scan_lock and remove the
acpi_scan_lock locking from acpi_bus_scan() and acpi_bus_trim().
Accordingly, update all of their users to make sure that they
are always called under acpi_scan_lock.
Furthermore, since eject operations are carried out asynchronously
with respect to the notify events that trigger them, with the help
of acpi_bus_hot_remove_device(), even if notify handlers take the
ACPI scan lock, it still is possible that, for example,
acpi_bus_trim() will run between acpi_bus_hot_remove_device() and
the notify handler that scheduled its execution and that
acpi_bus_trim() will remove the device node passed to
acpi_bus_hot_remove_device() for ejection. In that case, the struct
acpi_device object obtained by acpi_bus_hot_remove_device() will be
invalid and not-so-funny things will ensue. To protect agaist that,
make the users of acpi_bus_hot_remove_device() run get_device() on
ACPI device node objects that are about to be passed to it and make
acpi_bus_hot_remove_device() run put_device() on them and check if
their ACPI handles are not NULL (make acpi_device_unregister() clear
the device nodes' ACPI handles for that check to work).
Finally, observe that acpi_os_hotplug_execute() actually can fail,
in which case its caller ought to free memory allocated for the
context object to prevent leaks from happening. It also needs to
run put_device() on the device node that it ran get_device() on
previously in that case. Modify the code accordingly.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Yinghai Lu <yinghai@kernel.org>
2013-02-13 13:36:47 +00:00
|
|
|
acpi_scan_lock_release();
|
2013-07-13 21:27:24 +00:00
|
|
|
kfree(hp_work); /* allocated in handle_hotplug_event() */
|
2013-07-13 21:27:25 +00:00
|
|
|
put_bridge(context->func.slot->bridge);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:40:53 +00:00
|
|
|
/**
|
2013-07-13 21:27:24 +00:00
|
|
|
* handle_hotplug_event - handle ACPI hotplug event
|
2011-09-28 23:40:53 +00:00
|
|
|
* @handle: Notify()'ed acpi_handle
|
|
|
|
* @type: Notify code
|
2013-07-13 21:27:24 +00:00
|
|
|
* @data: pointer to acpiphp_context structure
|
2011-09-28 23:40:53 +00:00
|
|
|
*
|
|
|
|
* Handles ACPI event notification on slots.
|
|
|
|
*/
|
2013-07-13 21:27:24 +00:00
|
|
|
static void handle_hotplug_event(acpi_handle handle, u32 type, void *data)
|
2011-09-28 23:40:53 +00:00
|
|
|
{
|
2013-07-13 21:27:24 +00:00
|
|
|
struct acpiphp_context *context;
|
|
|
|
|
|
|
|
mutex_lock(&acpiphp_context_lock);
|
|
|
|
context = acpiphp_get_context(handle);
|
|
|
|
if (context) {
|
2013-07-13 21:27:25 +00:00
|
|
|
get_bridge(context->func.slot->bridge);
|
2013-07-13 21:27:24 +00:00
|
|
|
acpiphp_put_context(context);
|
2013-07-13 21:27:24 +00:00
|
|
|
}
|
|
|
|
mutex_unlock(&acpiphp_context_lock);
|
2011-09-28 23:40:53 +00:00
|
|
|
/*
|
|
|
|
* Currently the code adds all hotplug events to the kacpid_wq
|
|
|
|
* queue when it should add hotplug events to the kacpi_hotplug_wq.
|
|
|
|
* The proper way to fix this is to reorganize the code so that
|
|
|
|
* drivers (dock, etc.) do not call acpi_os_execute(), etc.
|
|
|
|
* For now just re-add this work to the kacpi_hotplug_wq so we
|
|
|
|
* don't deadlock on hotplug actions.
|
|
|
|
*/
|
2013-07-13 21:27:24 +00:00
|
|
|
if (context)
|
|
|
|
alloc_acpi_hp_work(handle, type, context, hotplug_event_work);
|
2005-04-28 07:25:56 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-04-12 05:44:26 +00:00
|
|
|
/*
|
|
|
|
* Create hotplug slots for the PCI bus.
|
|
|
|
* It should always return 0 to avoid skipping following notifiers.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2013-07-13 21:27:23 +00:00
|
|
|
void acpiphp_enumerate_slots(struct pci_bus *bus)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-04-12 05:44:26 +00:00
|
|
|
struct acpiphp_bridge *bridge;
|
2013-07-13 21:27:23 +00:00
|
|
|
acpi_handle handle;
|
|
|
|
acpi_status status;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-04-12 05:44:26 +00:00
|
|
|
if (acpiphp_disabled)
|
|
|
|
return;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-07-13 21:27:23 +00:00
|
|
|
handle = ACPI_HANDLE(bus->bridge);
|
2013-07-13 21:27:24 +00:00
|
|
|
if (!handle)
|
2013-04-12 05:44:26 +00:00
|
|
|
return;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-04-12 05:44:26 +00:00
|
|
|
bridge = kzalloc(sizeof(struct acpiphp_bridge), GFP_KERNEL);
|
2013-07-13 21:27:24 +00:00
|
|
|
if (!bridge) {
|
|
|
|
acpi_handle_err(handle, "No memory for bridge object\n");
|
2013-04-12 05:44:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-12 05:44:27 +00:00
|
|
|
INIT_LIST_HEAD(&bridge->slots);
|
2013-04-12 05:44:28 +00:00
|
|
|
kref_init(&bridge->ref);
|
2013-04-12 05:44:26 +00:00
|
|
|
bridge->pci_dev = pci_dev_get(bus->self);
|
|
|
|
bridge->pci_bus = bus;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Grab a ref to the subordinate PCI bus in case the bus is
|
|
|
|
* removed via PCI core logical hotplug. The ref pins the bus
|
|
|
|
* (which we access during module unload).
|
|
|
|
*/
|
|
|
|
get_device(&bus->dev);
|
|
|
|
|
2013-07-13 21:27:24 +00:00
|
|
|
if (!pci_is_root_bus(bridge->pci_bus)) {
|
|
|
|
struct acpiphp_context *context;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This bridge should have been registered as a hotplug function
|
|
|
|
* under its parent, so the context has to be there. If not, we
|
|
|
|
* are in deep goo.
|
|
|
|
*/
|
|
|
|
mutex_lock(&acpiphp_context_lock);
|
|
|
|
context = acpiphp_get_context(handle);
|
2013-07-13 21:27:25 +00:00
|
|
|
if (WARN_ON(!context)) {
|
2013-07-13 21:27:24 +00:00
|
|
|
mutex_unlock(&acpiphp_context_lock);
|
|
|
|
put_device(&bus->dev);
|
|
|
|
kfree(bridge);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bridge->context = context;
|
|
|
|
context->bridge = bridge;
|
|
|
|
/* Get a reference to the parent bridge. */
|
2013-07-13 21:27:25 +00:00
|
|
|
get_bridge(context->func.slot->bridge);
|
2013-07-13 21:27:24 +00:00
|
|
|
mutex_unlock(&acpiphp_context_lock);
|
|
|
|
}
|
|
|
|
|
2013-07-13 21:27:23 +00:00
|
|
|
/* must be added to the list prior to calling register_slot */
|
|
|
|
mutex_lock(&bridge_mutex);
|
|
|
|
list_add(&bridge->list, &bridge_list);
|
|
|
|
mutex_unlock(&bridge_mutex);
|
|
|
|
|
|
|
|
/* register all slot objects under this bridge */
|
2013-07-13 21:27:25 +00:00
|
|
|
status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
|
2013-07-13 21:27:23 +00:00
|
|
|
register_slot, NULL, bridge, NULL);
|
|
|
|
if (ACPI_FAILURE(status)) {
|
2013-07-13 21:27:25 +00:00
|
|
|
acpi_handle_err(handle, "failed to register slots\n");
|
2013-07-13 21:27:24 +00:00
|
|
|
cleanup_bridge(bridge);
|
|
|
|
put_bridge(bridge);
|
2013-07-13 21:27:23 +00:00
|
|
|
}
|
2013-04-12 05:44:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy hotplug slots associated with the PCI bus */
|
|
|
|
void acpiphp_remove_slots(struct pci_bus *bus)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-04-12 05:44:26 +00:00
|
|
|
struct acpiphp_bridge *bridge, *tmp;
|
|
|
|
|
|
|
|
if (acpiphp_disabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
list_for_each_entry_safe(bridge, tmp, &bridge_list, list)
|
|
|
|
if (bridge->pci_bus == bus) {
|
|
|
|
cleanup_bridge(bridge);
|
2013-04-12 05:44:28 +00:00
|
|
|
put_bridge(bridge);
|
2013-04-12 05:44:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* acpiphp_enable_slot - power on slot
|
2007-11-28 17:04:30 +00:00
|
|
|
* @slot: ACPI PHP slot
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
int acpiphp_enable_slot(struct acpiphp_slot *slot)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
2006-01-13 15:02:15 +00:00
|
|
|
mutex_lock(&slot->crit_sect);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* wake up all functions */
|
|
|
|
retval = power_on_slot(slot);
|
|
|
|
if (retval)
|
|
|
|
goto err_exit;
|
|
|
|
|
2006-03-22 05:49:33 +00:00
|
|
|
if (get_slot_status(slot) == ACPI_STA_ALL) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/* configure all functions */
|
|
|
|
retval = enable_device(slot);
|
2006-03-22 05:49:33 +00:00
|
|
|
if (retval)
|
|
|
|
power_off_slot(slot);
|
|
|
|
} else {
|
2008-03-04 03:09:46 +00:00
|
|
|
dbg("%s: Slot status is not ACPI_STA_ALL\n", __func__);
|
2006-03-22 05:49:33 +00:00
|
|
|
power_off_slot(slot);
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
err_exit:
|
2006-01-13 15:02:15 +00:00
|
|
|
mutex_unlock(&slot->crit_sect);
|
2005-04-16 22:20:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* acpiphp_disable_slot - power off slot
|
2007-11-28 17:04:30 +00:00
|
|
|
* @slot: ACPI PHP slot
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
int acpiphp_disable_slot(struct acpiphp_slot *slot)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
|
2006-01-13 15:02:15 +00:00
|
|
|
mutex_lock(&slot->crit_sect);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* unconfigure all functions */
|
|
|
|
retval = disable_device(slot);
|
|
|
|
if (retval)
|
|
|
|
goto err_exit;
|
|
|
|
|
|
|
|
/* power off all functions */
|
|
|
|
retval = power_off_slot(slot);
|
|
|
|
if (retval)
|
|
|
|
goto err_exit;
|
|
|
|
|
|
|
|
err_exit:
|
2006-01-13 15:02:15 +00:00
|
|
|
mutex_unlock(&slot->crit_sect);
|
2005-04-16 22:20:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* slot enabled: 1
|
|
|
|
* slot disabled: 0
|
|
|
|
*/
|
|
|
|
u8 acpiphp_get_power_status(struct acpiphp_slot *slot)
|
|
|
|
{
|
2005-04-28 07:25:57 +00:00
|
|
|
return (slot->flags & SLOT_POWEREDON);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2006-10-25 18:44:57 +00:00
|
|
|
* latch open: 1
|
|
|
|
* latch closed: 0
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
u8 acpiphp_get_latch_status(struct acpiphp_slot *slot)
|
|
|
|
{
|
|
|
|
unsigned int sta;
|
|
|
|
|
|
|
|
sta = get_slot_status(slot);
|
|
|
|
|
2013-04-12 05:44:19 +00:00
|
|
|
return (sta & ACPI_STA_DEVICE_UI) ? 0 : 1;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* adapter presence : 1
|
|
|
|
* absence : 0
|
|
|
|
*/
|
|
|
|
u8 acpiphp_get_adapter_status(struct acpiphp_slot *slot)
|
|
|
|
{
|
|
|
|
unsigned int sta;
|
|
|
|
|
|
|
|
sta = get_slot_status(slot);
|
|
|
|
|
|
|
|
return (sta == 0) ? 0 : 1;
|
|
|
|
}
|