linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jiang Liu <liuj97@gmail.com>
To: Yinghai Lu <yinghai@kernel.org>,
	Jesse Barnes <jbarnes@virtuousgeek.org>,
	Len Brown <lenb@kernel.org>
Cc: Jiang Liu <jiang.liu@huawei.com>,
	Bjorn Helgaas <bhelgaas@google.com>,
	Ashok Raj <ashok.raj@intel.com>, Jiang Liu <liuj97@gmail.com>,
	Keping Chen <chenkeping@huawei.com>,
	linux-pci@vger.kernel.org, linux-acpi@vger.kernel.org
Subject: [RFC PATCH 06/11] ACPI,PCI: update ACPI<->PCI binding information when pci hotplug event happens
Date: Fri, 23 Mar 2012 22:58:22 +0800	[thread overview]
Message-ID: <1332514707-9673-7-git-send-email-jiang.liu@huawei.com> (raw)
In-Reply-To: <1332514707-9673-1-git-send-email-jiang.liu@huawei.com>

When PCI hotplug event happens, the ACPI<->PCI binding relationship should be
updated. Otherwise the binding relationship may become stale and cause invalid
data access or wrong searching results. For example, thinking about following
sequences:
1. Function acpi_pci_bind() installs an ACPI PME event hanlder for the original
   PCI device (orig_device).
2. The orignal PCI device is replaced by a new PCI device, so orig_device
   destroyed and a new_device created.
3. The new PCI device triggers an ACPI PME event.
4. The registered PME event handle pci_acpi_wake_dev() still tries to access
   the orig_device, which may have already been released now.
5. Invalid memory access or event memory corruption.

The solution here is to update the binding relationship when PCI hotplug
event happens.

Signed-off-by: Jiang Liu <jiang.liu@huawei.com>
---
 drivers/acpi/pci_bind.c |  115 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 115 insertions(+), 0 deletions(-)

diff --git a/drivers/acpi/pci_bind.c b/drivers/acpi/pci_bind.c
index 2ef0409..1dd6379 100644
--- a/drivers/acpi/pci_bind.c
+++ b/drivers/acpi/pci_bind.c
@@ -118,3 +118,118 @@ int acpi_pci_bind_root(struct acpi_device *device)
 
 	return 0;
 }
+
+static struct acpi_device *
+acpi_pci_bind_notify_check(struct pci_dev *pci_dev)
+{
+	acpi_handle handle;
+	struct acpi_device *device;
+
+	handle = DEVICE_ACPI_HANDLE(&pci_dev->dev);
+	if (handle == NULL || acpi_bus_get_device(handle, &device))
+		return NULL;
+
+	if (device && device->parent && device->parent->ops.bind) 
+		return device;
+
+	return NULL;
+}
+
+static void acpi_pci_bind_notify_dev_add(struct pci_dev *pci_dev)
+{
+	struct acpi_device *device;
+
+	device = acpi_pci_bind_notify_check(pci_dev);
+	if (device) {
+		pci_acpi_add_pm_notifier(device, pci_dev);
+		if (device->wakeup.flags.run_wake)
+			device_set_run_wake(&pci_dev->dev, true);
+	}
+}
+
+static void acpi_pci_bind_notify_dev_remove(struct pci_dev *pci_dev)
+{
+	struct acpi_device *device;
+
+	device = acpi_pci_bind_notify_check(pci_dev);
+	if (device) {
+		device_set_run_wake(&pci_dev->dev, false);
+		pci_acpi_remove_pm_notifier(device);
+	}
+}
+
+static void acpi_pci_bind_notify_bus_add(struct pci_bus *bus)
+{
+	acpi_handle handle;
+	struct acpi_device *device;
+
+	/* Skip root buses */
+	if (!bus->self)
+		return;
+
+	device = acpi_pci_bind_notify_check(bus->self);
+	if (device) {
+		/*
+		 * Install the 'bind' function to facilitate callbacks for
+		 * children of the P2P bridge.
+		 */
+		device->ops.bind = acpi_pci_bind;
+		device->ops.unbind = acpi_pci_unbind;
+
+		/* Evaluate and parse _PRT, if exists. */
+		if (ACPI_SUCCESS(acpi_get_handle(device->handle,
+						 METHOD_NAME__PRT, &handle)))
+			acpi_pci_irq_add_prt(device->handle, bus);
+	}
+}
+
+static void acpi_pci_bind_notify_bus_remove(struct pci_bus *bus)
+{
+	struct acpi_device *device;
+
+	/* Skip root buses */
+	if (!bus->self)
+		return;
+
+	device = acpi_pci_bind_notify_check(bus->self);
+	if (device) {
+		acpi_pci_irq_del_prt(bus);
+
+		device->ops.bind = NULL;
+		device->ops.unbind = NULL;
+	}
+}
+
+static int acpi_pci_bind_notify_fn(struct notifier_block *nb,
+				   unsigned long event, void *data)
+{
+	switch (event) {
+	case PCI_HP_EVENT_DEV_ADD:
+		acpi_pci_bind_notify_dev_add(data);
+		break;
+	case PCI_HP_EVENT_DEV_REMOVE:
+		acpi_pci_bind_notify_dev_remove(data);
+		break;
+	case PCI_HP_EVENT_BUS_ADD:
+		acpi_pci_bind_notify_bus_add(data);
+		break;
+	case PCI_HP_EVENT_BUS_REMOVE:
+		acpi_pci_bind_notify_bus_remove(data);
+		break;
+	default:
+		return NOTIFY_DONE;
+	}
+
+	return NOTIFY_OK;
+}
+
+static struct notifier_block acpi_pci_bind_notifier = {
+	.notifier_call = &acpi_pci_bind_notify_fn,
+};
+
+static int __init acpi_pci_bind_init(void)
+{
+	return pci_register_hotplug_notifier(&acpi_pci_bind_notifier);
+}
+
+subsys_initcall(acpi_pci_bind_init);
-- 
1.7.5.4


  parent reply	other threads:[~2012-03-23 14:58 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-23 14:58 [PATCH 00/11] Enhancements and bugfixes to PCI hotplug subsystem Jiang Liu
2012-03-23 14:58 ` [PATCH 01/11] PCI: Fix device reference count leakage in pci_dev_present() Jiang Liu
2012-03-23 14:58 ` [RFC PATCH 02/11] PCI: introduce pci_bus_get()/pci_bus_put() to hide implementation details Jiang Liu
2012-03-23 14:58 ` [RFC PATCH 03/11] PCI: clean up root bridge related logic in acpiphp driver Jiang Liu
2012-03-23 14:58 ` [RFC PATCH 04/11] ACPI,PCI: fix race windows caused by alloc_acpi_hotplug_work() Jiang Liu
2012-03-23 14:58 ` [RFC PATCH 05/11] PCI: Add notification interfaces for PCI root/bus/device hotplug events Jiang Liu
2012-03-23 14:58 ` Jiang Liu [this message]
2012-03-23 14:58 ` [RFC PATCH 07/11] ACPI,PCI: update ACPI slots when PCI hotplug event happens Jiang Liu
2012-03-23 14:58 ` [RFC PATCH 08/11] PCI: Introduce recursive mutex to serialize PCI hotplug operations Jiang Liu
2012-03-23 14:58 ` [RFC PATCH 09/11] PCI: serialize hotplug operations triggered by PCI hotplug sysfs interfaces Jiang Liu
2012-03-23 14:58 ` [RFC PATCH 10/11] PCI,ACPI: serialize hotplug operations triggered by ACPI subsystem Jiang Liu
2012-03-23 14:58 ` [RFC PATCH 11/11] PCI: Serialize hotplug operations triggered by acpiphp driver Jiang Liu
2012-03-27  3:33 ` [PATCH 00/11] Enhancements and bugfixes to PCI hotplug subsystem Kenji Kaneshige
2012-03-27 14:31   ` Jiang Liu
2012-03-30  4:15     ` Kenji Kaneshige

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1332514707-9673-7-git-send-email-jiang.liu@huawei.com \
    --to=liuj97@gmail.com \
    --cc=ashok.raj@intel.com \
    --cc=bhelgaas@google.com \
    --cc=chenkeping@huawei.com \
    --cc=jbarnes@virtuousgeek.org \
    --cc=jiang.liu@huawei.com \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=yinghai@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).