Skip to contentSkip to conversation

ACPI / driver core: Store a device pointer in struct acpi_dev_node

PATCH In review Rafael J. Wysocki 5 participants 15 messages Nov 10, 2013 – Nov 14, 2013

Subsystems (19)
DRM drivers x86 USB DRM misc +15 more subsystems
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ACPI / driver core: Store a device pointer in struct acpi_dev_node

From: Rafael J. Wysocki <[email protected]>

Modify struct acpi_dev_node to contain a pointer to struct device ambedded in the struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to that struct acpi_device. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

The rationale for using a struct device pointer instead of a struct acpi_device one as the member of struct acpi_dev_node is that it allows device.h to avoid including linux/acpi.h which would introduce quite a bit of compilation overhead for stuff that doesn't care about ACPI. In turn, moving the macros to linux/acpi.h forces the stuff that does care about ACPI to include that file as appropriate anyway.

The main motivation for doing this is that there are things represented by struct acpi_device objects that don't have valid ACPI handles (so called fixed ACPI hardware features, such as power and sleep buttons) and we would like to create platform device objects for them and "glue" them to their ACPI companions in the usual way (which currently is impossible due to the lack of valid ACPI handles). However, there are more reasons why it may be useful.

First, struct device pointers allow of much better type checking than void pointers which are ACPI handles, so it should be more difficult to write buggy code using modified struct acpi_dev_node and the new macros. Second, it should help to reduce the number of places in which the result of ACPI_HANDLE() is passed to acpi_bus_get_device() in order to obtain a pointer to the struct acpi_device associated with the given "physical" device, because now that pointer can be obtained directly by applying to_acpi_device() to the result of the ACPI_COMPANION() macro. Finally, it should make it easier to write generic code that will build both for CONFIG_ACPI set and unset without adding explicit compiler directives to it.

---

Hi Everybody,

First of all, I haven't tested this yet, so caveat emptor. I have compiled it on x86-64 for CONFIG_ACPI set and unset and I'm going to feed it to the auto build system shortly in case I overlooked something build-related.

Please have a look and let me know if you have any problems with this in principle. If not, I'd like to queue it up for inclusion by the end of the merge window or in the -rc2 time frame (to avoid collisions with any big merges), as I'd like to be able to work on top of it during the 3.14 cycle if possible.

Thanks,
Rafael

---
 arch/ia64/include/asm/pci.h   |    2 -
 arch/ia64/pci/pci.c           |    6 ++---
 arch/x86/include/asm/pci.h    |    2 -
 arch/x86/pci/acpi.c           |    4 +--
 drivers/acpi/acpi_platform.c  |    2 -
 drivers/acpi/device_pm.c      |    6 -----
 drivers/acpi/glue.c           |   45 +++++++++++++++++++++---------------------
 drivers/ata/libata-acpi.c     |    4 +--
 drivers/base/platform.c       |    4 +--
 drivers/hid/i2c-hid/i2c-hid.c |    2 -
 drivers/i2c/i2c-core.c        |    4 +--
 drivers/mmc/core/sdio_bus.c   |    3 --
 drivers/spi/spi.c             |    2 -
 include/acpi/acpi_bus.h       |    2 -
 include/linux/acpi.h          |   14 +++++++++++++
 include/linux/device.h        |   10 ---------
 16 files changed, 57 insertions(+), 55 deletions(-)
include/linux/device.h +1 -9
Index: linux-pm/include/linux/device.h
===================================================================
@@ -625,7 +625,7 @@ struct device_dma_parameters {
 
 struct acpi_dev_node {
 #ifdef CONFIG_ACPI
-	void	*handle;
+	struct device *dev;
 #endif
 };
 
@@ -769,14 +769,6 @@ static inline struct device *kobj_to_dev
 	return container_of(kobj, struct device, kobj);
 }
 
-#ifdef CONFIG_ACPI
-#define ACPI_HANDLE(dev)	((dev)->acpi_node.handle)
-#define ACPI_HANDLE_SET(dev, _handle_)	(dev)->acpi_node.handle = (_handle_)
-#else
-#define ACPI_HANDLE(dev)	(NULL)
-#define ACPI_HANDLE_SET(dev, _handle_)	do { } while (0)
-#endif
-
 /* Get the wakeup routines, which depend on struct device */
 #include <linux/pm_wakeup.h>
 
include/acpi/acpi_bus.h +1 -1
Index: linux-pm/include/acpi/acpi_bus.h
===================================================================
@@ -431,9 +431,9 @@ static inline acpi_handle acpi_get_child
 {
 	return acpi_find_child(handle, addr, false);
 }
+void acpi_preset_companion(struct device *dev, acpi_handle parent, u64 addr);
 int acpi_is_root_bridge(acpi_handle);
 struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle);
-#define DEVICE_ACPI_HANDLE(dev) ((acpi_handle)ACPI_HANDLE(dev))
 
 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int state);
 int acpi_disable_wakeup_device_power(struct acpi_device *dev);
include/linux/acpi.h +14 -0
Index: linux-pm/include/linux/acpi.h
===================================================================
@@ -44,6 +44,14 @@
 #include <acpi/acpi_numa.h>
 #include <asm/acpi.h>
 
+#define ACPI_COMPANION(device)		((device)->acpi_node.dev)
+#define ACPI_COMPANION_SET(dev, acc)	ACPI_COMPANION(dev) = (acc)
+#define ACPI_HANDLE(dev)				\
+({							\
+	struct device *acc = ACPI_COMPANION(dev);	\
+	acc ? to_acpi_device(acc)->handle : NULL;	\
+})
+
 enum acpi_irq_model_id {
 	ACPI_IRQ_MODEL_PIC = 0,
 	ACPI_IRQ_MODEL_IOAPIC,
@@ -407,6 +415,10 @@ static inline bool acpi_driver_match_dev
 
 #define acpi_disabled 1
 
+#define ACPI_COMPANION(device)		(NULL)
+#define ACPI_COMPANION_SET(dev, acc)	do { } while (0)
+#define ACPI_HANDLE(dev)		(NULL)
+
 static inline void acpi_early_init(void) { }
 
 static inline int early_acpi_boot_init(void)
@@ -475,6 +487,8 @@ static inline bool acpi_driver_match_dev
 
 #endif	/* !CONFIG_ACPI */
 
+#define DEVICE_ACPI_HANDLE(dev)	ACPI_HANDLE(dev)
+
 #ifdef CONFIG_ACPI
 void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
 			       u32 pm1a_ctrl,  u32 pm1b_ctrl));
arch/ia64/include/asm/pci.h +1 -1
Index: linux-pm/arch/ia64/include/asm/pci.h
===================================================================
@@ -95,7 +95,7 @@ struct iospace_resource {
 };
 
 struct pci_controller {
-	void *acpi_handle;
+	struct device *acpi_companion;
 	void *iommu;
 	int segment;
 	int node;		/* nearest node with memory or -1 for global allocation */
arch/ia64/pci/pci.c +3 -3
Index: linux-pm/arch/ia64/pci/pci.c
===================================================================
@@ -436,9 +436,9 @@ struct pci_bus *pci_acpi_scan_root(struc
 	if (!controller)
 		return NULL;
 
-	controller->acpi_handle = device->handle;
+	controller->acpi_companion = &device->dev;
 
-	pxm = acpi_get_pxm(controller->acpi_handle);
+	pxm = acpi_get_pxm(device->handle);
 #ifdef CONFIG_NUMA
 	if (pxm >= 0)
 		controller->node = pxm_to_node(pxm);
@@ -489,7 +489,7 @@ int pcibios_root_bridge_prepare(struct p
 {
 	struct pci_controller *controller = bridge->bus->sysdata;
 
-	ACPI_HANDLE_SET(&bridge->dev, controller->acpi_handle);
+	ACPI_COMPANION_SET(&bridge->dev, controller->acpi_companion);
 	return 0;
 }
 
arch/x86/include/asm/pci.h +1 -1
Index: linux-pm/arch/x86/include/asm/pci.h
===================================================================
@@ -15,7 +15,7 @@ struct pci_sysdata {
 	int		domain;		/* PCI domain */
 	int		node;		/* NUMA node */
 #ifdef CONFIG_ACPI
-	void		*acpi;		/* ACPI-specific data */
+	struct device	*acpi_companion;	/* ACPI companion device */
 #endif
 #ifdef CONFIG_X86_64
 	void		*iommu;		/* IOMMU private data */
arch/x86/pci/acpi.c +2 -2
Index: linux-pm/arch/x86/pci/acpi.c
===================================================================
@@ -518,7 +518,7 @@ struct pci_bus *pci_acpi_scan_root(struc
 	sd = &info->sd;
 	sd->domain = domain;
 	sd->node = node;
-	sd->acpi = device->handle;
+	sd->acpi_companion = &device->dev;
 	/*
 	 * Maybe the desired pci bus has been already scanned. In such case
 	 * it is unnecessary to scan the pci bus with the given domain,busnum.
@@ -589,7 +589,7 @@ int pcibios_root_bridge_prepare(struct p
 {
 	struct pci_sysdata *sd = bridge->bus->sysdata;
 
-	ACPI_HANDLE_SET(&bridge->dev, sd->acpi);
+	ACPI_COMPANION_SET(&bridge->dev, sd->acpi_companion);
 	return 0;
 }
 
drivers/acpi/glue.c +23 -22

104 lines not on this page · raw message

drivers/ata/libata-acpi.c +2 -2
Index: linux-pm/drivers/ata/libata-acpi.c
===================================================================
@@ -185,7 +185,7 @@ void ata_acpi_bind_port(struct ata_port
 	if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA || !host_handle)
 		return;
 
-	ACPI_HANDLE_SET(&ap->tdev, acpi_get_child(host_handle, ap->port_no));
+	acpi_preset_companion(&ap->tdev, host_handle, ap->port_no);
 
 	if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
 		ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
@@ -222,7 +222,7 @@ void ata_acpi_bind_dev(struct ata_device
 		parent_handle = port_handle;
 	}
 
-	ACPI_HANDLE_SET(&dev->tdev, acpi_get_child(parent_handle, adr));
+	acpi_preset_companion(&dev->tdev, parent_handle, adr);
 
 	register_hotplug_dock_device(ata_dev_acpi_handle(dev),
 				     &ata_acpi_dev_dock_ops, dev, NULL, NULL);
drivers/base/platform.c +2 -2
Index: linux-pm/drivers/base/platform.c
===================================================================
@@ -432,7 +432,7 @@ struct platform_device *platform_device_
 		goto err_alloc;
 
 	pdev->dev.parent = pdevinfo->parent;
-	ACPI_HANDLE_SET(&pdev->dev, pdevinfo->acpi_node.handle);
+	ACPI_COMPANION_SET(&pdev->dev, pdevinfo->acpi_node.dev);
 
 	if (pdevinfo->dma_mask) {
 		/*
@@ -463,7 +463,7 @@ struct platform_device *platform_device_
 	ret = platform_device_add(pdev);
 	if (ret) {
 err:
-		ACPI_HANDLE_SET(&pdev->dev, NULL);
+		ACPI_COMPANION_SET(&pdev->dev, NULL);
 		kfree(pdev->dev.dma_mask);
 
 err_alloc:
drivers/acpi/acpi_platform.c +1 -1
Index: linux-pm/drivers/acpi/acpi_platform.c
===================================================================
@@ -111,7 +111,7 @@ int acpi_create_platform_device(struct a
 	pdevinfo.id = -1;
 	pdevinfo.res = resources;
 	pdevinfo.num_res = count;
-	pdevinfo.acpi_node.handle = adev->handle;
+	pdevinfo.acpi_node.dev = &adev->dev;
 	pdev = platform_device_register_full(&pdevinfo);
 	if (IS_ERR(pdev)) {
 		dev_err(&adev->dev, "platform device creation failed: %ld\n",
drivers/hid/i2c-hid/i2c-hid.c +1 -1
Index: linux-pm/drivers/hid/i2c-hid/i2c-hid.c
===================================================================
@@ -1012,7 +1012,7 @@ static int i2c_hid_probe(struct i2c_clie
 	hid->hid_get_raw_report = i2c_hid_get_raw_report;
 	hid->hid_output_raw_report = i2c_hid_output_raw_report;
 	hid->dev.parent = &client->dev;
-	ACPI_HANDLE_SET(&hid->dev, ACPI_HANDLE(&client->dev));
+	ACPI_COMPANION_SET(&hid->dev, ACPI_COMPANION(&client->dev));
 	hid->bus = BUS_I2C;
 	hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
 	hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
drivers/i2c/i2c-core.c +2 -2
Index: linux-pm/drivers/i2c/i2c-core.c
===================================================================
@@ -674,7 +674,7 @@ i2c_new_device(struct i2c_adapter *adap,
 	client->dev.bus = &i2c_bus_type;
 	client->dev.type = &i2c_client_type;
 	client->dev.of_node = info->of_node;
-	ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
+	ACPI_COMPANION_SET(&client->dev, info->acpi_node.dev);
 
 	/* For 10-bit clients, add an arbitrary offset to avoid collisions */
 	dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
@@ -1103,7 +1103,7 @@ static acpi_status acpi_i2c_add_device(a
 		return AE_OK;
 
 	memset(&info, 0, sizeof(info));
-	info.acpi_node.handle = handle;
+	info.acpi_node.dev = &adev->dev;
 	info.irq = -1;
 
 	INIT_LIST_HEAD(&resource_list);
drivers/mmc/core/sdio_bus.c +1 -2
Index: linux-pm/drivers/mmc/core/sdio_bus.c
===================================================================
@@ -305,8 +305,7 @@ static void sdio_acpi_set_handle(struct
 	struct mmc_host *host = func->card->host;
 	u64 addr = (host->slotno << 16) | func->num;
 
-	ACPI_HANDLE_SET(&func->dev,
-			acpi_get_child(ACPI_HANDLE(host->parent), addr));
+	acpi_preset_companion(&func->dev, ACPI_HANDLE(host->parent), addr);
 }
 #else
 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
drivers/spi/spi.c +1 -1
Index: linux-pm/drivers/spi/spi.c
===================================================================
@@ -1024,7 +1024,7 @@ static acpi_status acpi_spi_add_device(a
 		return AE_NO_MEMORY;
 	}
 
-	ACPI_HANDLE_SET(&spi->dev, handle);
+	ACPI_COMPANION_SET(&spi->dev, &adev->dev);
 	spi->irq = -1;
 
 	INIT_LIST_HEAD(&resource_list);
drivers/acpi/device_pm.c +1 -5
Index: linux-pm/drivers/acpi/device_pm.c
===================================================================
@@ -22,16 +22,12 @@
  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  */
 
-#include <linux/device.h>
+#include <linux/acpi.h>
 #include <linux/export.h>
 #include <linux/mutex.h>
 #include <linux/pm_qos.h>
 #include <linux/pm_runtime.h>
 
-#include <acpi/acpi.h>
-#include <acpi/acpi_bus.h>
-#include <acpi/acpi_drivers.h>
-
 #include "internal.h"
 
 #define _COMPONENT	ACPI_POWER_COMPONENT
#1 lore raw
Quoted 54 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Modify struct acpi_dev_node to contain a pointer to struct device ambedded in the struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to that struct acpi_device. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

The rationale for using a struct device pointer instead of a struct acpi_device one as the member of struct acpi_dev_node is that it allows device.h to avoid including linux/acpi.h which would introduce quite a bit of compilation overhead for stuff that doesn't care about ACPI. In turn, moving the macros to linux/acpi.h forces the stuff that does care about ACPI to include that file as appropriate anyway.

The main motivation for doing this is that there are things represented by struct acpi_device objects that don't have valid ACPI handles (so called fixed ACPI hardware features, such as power and sleep buttons) and we would like to create platform device objects for them and "glue" them to their ACPI companions in the usual way (which currently is impossible due to the lack of valid ACPI handles). However, there are more reasons why it may be useful.

First, struct device pointers allow of much better type checking than void pointers which are ACPI handles, so it should be more difficult to write buggy code using modified struct acpi_dev_node and the new macros. Second, it should help to reduce the number of places in which the result of ACPI_HANDLE() is passed to acpi_bus_get_device() in order to obtain a pointer to the struct acpi_device associated with the given "physical" device, because now that pointer can be obtained directly by applying to_acpi_device() to the result of the ACPI_COMPANION() macro. Finally, it should make it easier to write generic code that will build both for CONFIG_ACPI set and unset without adding explicit compiler directives to it.

---

Hi Everybody,

First of all, I haven't tested this yet, so caveat emptor. I have compiled it on x86-64 for CONFIG_ACPI set and unset and I'm going to feed it to the auto build system shortly in case I overlooked something build-related.

Quoted 5 lines

Please have a look and let me know if you have any problems with this in principle. If not, I'd like to queue it up for inclusion by the end of the merge window or in the -rc2 time frame (to avoid collisions with any big merges), as I'd like to be able to work on top of it during the 3.14 cycle if possible.

At first glance, this looks good to me, thanks for removing that void *, I like this a lot better now.

greg k-h

#2 lore raw
In reply to Rafael J. Wysocki
Quoted 15 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Modify struct acpi_dev_node to contain a pointer to struct device ambedded in the struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to that struct acpi_device. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

Quoted 8 lines

The rationale for using a struct device pointer instead of a struct acpi_device one as the member of struct acpi_dev_node is that it allows device.h to avoid including linux/acpi.h which would introduce quite a bit of compilation overhead for stuff that doesn't care about ACPI.
In turn, moving the macros to linux/acpi.h forces
the stuff that does care about ACPI to include that file as appropriate anyway.

How about declaring "struct acpi_device" in the device.h? This can help to use struct acpi_device without including linux/acpi.h.

struct iommu_ops and struct iommu_group have been used by the same way in the device.h.

Quoted 1,004 lines

The main motivation for doing this is that there are things represented by struct acpi_device objects that don't have valid ACPI handles (so called fixed ACPI hardware features, such as power and sleep buttons) and we would like to create platform device objects for them and "glue" them to their ACPI companions in the usual way (which currently is impossible due to the lack of valid ACPI handles). However, there are more reasons why it may be useful.

First, struct device pointers allow of much better type checking than void pointers which are ACPI handles, so it should be more difficult to write buggy code using modified struct acpi_dev_node and the new macros. Second, it should help to reduce the number of places in which the result of ACPI_HANDLE() is passed to acpi_bus_get_device() in order to obtain a pointer to the struct acpi_device associated with the given "physical" device, because now that pointer can be obtained directly by applying to_acpi_device() to the result of the ACPI_COMPANION() macro. Finally, it should make it easier to write generic code that will build both for CONFIG_ACPI set and unset without adding explicit compiler directives to it.

---

Hi Everybody,

First of all, I haven't tested this yet, so caveat emptor. I have compiled it on x86-64 for CONFIG_ACPI set and unset and I'm going to feed it to the auto build system shortly in case I overlooked something build-related.

Please have a look and let me know if you have any problems with this in principle. If not, I'd like to queue it up for inclusion by the end of the merge window or in the -rc2 time frame (to avoid collisions with any big merges), as I'd like to be able to work on top of it during the 3.14 cycle if possible.

Thanks,
Rafael

---
 arch/ia64/include/asm/pci.h   |    2 -
 arch/ia64/pci/pci.c           |    6 ++---
 arch/x86/include/asm/pci.h    |    2 -
 arch/x86/pci/acpi.c           |    4 +--
 drivers/acpi/acpi_platform.c  |    2 -
 drivers/acpi/device_pm.c      |    6 -----
 drivers/acpi/glue.c           |   45 +++++++++++++++++++++---------------------
 drivers/ata/libata-acpi.c     |    4 +--
 drivers/base/platform.c       |    4 +--
 drivers/hid/i2c-hid/i2c-hid.c |    2 -
 drivers/i2c/i2c-core.c        |    4 +--
 drivers/mmc/core/sdio_bus.c   |    3 --
 drivers/spi/spi.c             |    2 -
 include/acpi/acpi_bus.h       |    2 -
 include/linux/acpi.h          |   14 +++++++++++++
 include/linux/device.h        |   10 ---------
 16 files changed, 57 insertions(+), 55 deletions(-)

Index: linux-pm/include/linux/device.h
===================================================================

include/linux/device.h +1 -9
@@ -625,7 +625,7 @@ struct device_dma_parameters {
 
 struct acpi_dev_node {
 #ifdef CONFIG_ACPI
-	void	*handle;
+	struct device *dev;
 #endif
 };
 
@@ -769,14 +769,6 @@ static inline struct device *kobj_to_dev
 	return container_of(kobj, struct device, kobj);
 }
 
-#ifdef CONFIG_ACPI
-#define ACPI_HANDLE(dev)	((dev)->acpi_node.handle)
-#define ACPI_HANDLE_SET(dev, _handle_)	(dev)->acpi_node.handle = (_handle_)
-#else
-#define ACPI_HANDLE(dev)	(NULL)
-#define ACPI_HANDLE_SET(dev, _handle_)	do { } while (0)
-#endif
-
 /* Get the wakeup routines, which depend on struct device */
 #include <linux/pm_wakeup.h>
 

Index: linux-pm/include/acpi/acpi_bus.h
===================================================================

include/acpi/acpi_bus.h +1 -1
@@ -431,9 +431,9 @@ static inline acpi_handle acpi_get_child
 {
 	return acpi_find_child(handle, addr, false);
 }
+void acpi_preset_companion(struct device *dev, acpi_handle parent, u64 addr);
 int acpi_is_root_bridge(acpi_handle);
 struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle);
-#define DEVICE_ACPI_HANDLE(dev) ((acpi_handle)ACPI_HANDLE(dev))
 
 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int state);
 int acpi_disable_wakeup_device_power(struct acpi_device *dev);

Index: linux-pm/include/linux/acpi.h
===================================================================

include/linux/acpi.h +14 -0
@@ -44,6 +44,14 @@
 #include <acpi/acpi_numa.h>
 #include <asm/acpi.h>
 
+#define ACPI_COMPANION(device)		((device)->acpi_node.dev)
+#define ACPI_COMPANION_SET(dev, acc)	ACPI_COMPANION(dev) = (acc)
+#define ACPI_HANDLE(dev)				\
+({							\
+	struct device *acc = ACPI_COMPANION(dev);	\
+	acc ? to_acpi_device(acc)->handle : NULL;	\
+})
+
 enum acpi_irq_model_id {
 	ACPI_IRQ_MODEL_PIC = 0,
 	ACPI_IRQ_MODEL_IOAPIC,
@@ -407,6 +415,10 @@ static inline bool acpi_driver_match_dev
 
 #define acpi_disabled 1
 
+#define ACPI_COMPANION(device)		(NULL)
+#define ACPI_COMPANION_SET(dev, acc)	do { } while (0)
+#define ACPI_HANDLE(dev)		(NULL)
+
 static inline void acpi_early_init(void) { }
 
 static inline int early_acpi_boot_init(void)
@@ -475,6 +487,8 @@ static inline bool acpi_driver_match_dev
 
 #endif	/* !CONFIG_ACPI */
 
+#define DEVICE_ACPI_HANDLE(dev)	ACPI_HANDLE(dev)
+
 #ifdef CONFIG_ACPI
 void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
 			       u32 pm1a_ctrl,  u32 pm1b_ctrl));

Index: linux-pm/arch/ia64/include/asm/pci.h
===================================================================

arch/ia64/include/asm/pci.h +1 -1
@@ -95,7 +95,7 @@ struct iospace_resource {
 };
 
 struct pci_controller {
-	void *acpi_handle;
+	struct device *acpi_companion;
 	void *iommu;
 	int segment;
 	int node;		/* nearest node with memory or -1 for global allocation */

Index: linux-pm/arch/ia64/pci/pci.c
===================================================================

arch/ia64/pci/pci.c +3 -3
@@ -436,9 +436,9 @@ struct pci_bus *pci_acpi_scan_root(struc
 	if (!controller)
 		return NULL;
 
-	controller->acpi_handle = device->handle;
+	controller->acpi_companion = &device->dev;
 
-	pxm = acpi_get_pxm(controller->acpi_handle);
+	pxm = acpi_get_pxm(device->handle);
 #ifdef CONFIG_NUMA
 	if (pxm >= 0)
 		controller->node = pxm_to_node(pxm);
@@ -489,7 +489,7 @@ int pcibios_root_bridge_prepare(struct p
 {
 	struct pci_controller *controller = bridge->bus->sysdata;
 
-	ACPI_HANDLE_SET(&bridge->dev, controller->acpi_handle);
+	ACPI_COMPANION_SET(&bridge->dev, controller->acpi_companion);
 	return 0;
 }
 

Index: linux-pm/arch/x86/include/asm/pci.h
===================================================================

arch/x86/include/asm/pci.h +1 -1
@@ -15,7 +15,7 @@ struct pci_sysdata {
 	int		domain;		/* PCI domain */
 	int		node;		/* NUMA node */
 #ifdef CONFIG_ACPI
-	void		*acpi;		/* ACPI-specific data */
+	struct device	*acpi_companion;	/* ACPI companion device */
 #endif
 #ifdef CONFIG_X86_64
 	void		*iommu;		/* IOMMU private data */

Index: linux-pm/arch/x86/pci/acpi.c
===================================================================

arch/x86/pci/acpi.c +2 -2
@@ -518,7 +518,7 @@ struct pci_bus *pci_acpi_scan_root(struc
 	sd = &info->sd;
 	sd->domain = domain;
 	sd->node = node;
-	sd->acpi = device->handle;
+	sd->acpi_companion = &device->dev;
 	/*
 	 * Maybe the desired pci bus has been already scanned. In such case
 	 * it is unnecessary to scan the pci bus with the given domain,busnum.
@@ -589,7 +589,7 @@ int pcibios_root_bridge_prepare(struct p
 {
 	struct pci_sysdata *sd = bridge->bus->sysdata;
 
-	ACPI_HANDLE_SET(&bridge->dev, sd->acpi);
+	ACPI_COMPANION_SET(&bridge->dev, sd->acpi_companion);
 	return 0;
 }
 

Index: linux-pm/drivers/acpi/glue.c
===================================================================

drivers/acpi/glue.c +23 -22
@@ -197,30 +197,27 @@ static void acpi_physnode_link_name(char
 
 int acpi_bind_one(struct device *dev, acpi_handle handle)
 {
-	struct acpi_device *acpi_dev;
-	acpi_status status;
+	struct acpi_device *acpi_dev = NULL;
 	struct acpi_device_physical_node *physical_node, *pn;
 	char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
 	struct list_head *physnode_list;
 	unsigned int node_id;
 	int retval = -EINVAL;
 
-	if (ACPI_HANDLE(dev)) {
+	if (ACPI_COMPANION(dev)) {
 		if (handle) {
-			dev_warn(dev, "ACPI handle is already set\n");
+			dev_warn(dev, "ACPI companion already set\n");
 			return -EINVAL;
 		} else {
-			handle = ACPI_HANDLE(dev);
+			acpi_dev = to_acpi_device(ACPI_COMPANION(dev));
 		}
+	} else {
+		acpi_bus_get_device(handle, &acpi_dev);
 	}
-	if (!handle)
+	if (!acpi_dev)
 		return -EINVAL;
 
 	get_device(dev);
-	status = acpi_bus_get_device(handle, &acpi_dev);
-	if (ACPI_FAILURE(status))
-		goto err;
-
 	physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
 	if (!physical_node) {
 		retval = -ENOMEM;
@@ -242,7 +239,7 @@ int acpi_bind_one(struct device *dev, ac
 
 			dev_warn(dev, "Already associated with ACPI node\n");
 			kfree(physical_node);
-			if (ACPI_HANDLE(dev) != handle)
+			if (ACPI_COMPANION(dev) != &acpi_dev->dev)
 				goto err;
 
 			put_device(dev);
@@ -259,8 +256,8 @@ int acpi_bind_one(struct device *dev, ac
 	list_add(&physical_node->node, physnode_list);
 	acpi_dev->physical_node_count++;
 
-	if (!ACPI_HANDLE(dev))
-		ACPI_HANDLE_SET(dev, acpi_dev->handle);
+	if (!ACPI_COMPANION(dev))
+		ACPI_COMPANION_SET(dev, &acpi_dev->dev);
 
 	acpi_physnode_link_name(physical_node_name, node_id);
 	retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
@@ -283,7 +280,7 @@ int acpi_bind_one(struct device *dev, ac
 	return 0;
 
  err:
-	ACPI_HANDLE_SET(dev, NULL);
+	ACPI_COMPANION_SET(dev, NULL);
 	put_device(dev);
 	return retval;
 }
@@ -293,16 +290,11 @@ int acpi_unbind_one(struct device *dev)
 {
 	struct acpi_device_physical_node *entry;
 	struct acpi_device *acpi_dev;
-	acpi_status status;
 
-	if (!ACPI_HANDLE(dev))
+	if (!ACPI_COMPANION(dev))
 		return 0;
 
-	status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
-	if (ACPI_FAILURE(status)) {
-		dev_err(dev, "Oops, ACPI handle corrupt in %s()\n", __func__);
-		return -EINVAL;
-	}
+	acpi_dev = to_acpi_device(ACPI_COMPANION(dev));
 
 	mutex_lock(&acpi_dev->physical_node_lock);
 
@@ -316,7 +308,7 @@ int acpi_unbind_one(struct device *dev)
 			acpi_physnode_link_name(physnode_name, entry->node_id);
 			sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
 			sysfs_remove_link(&dev->kobj, "firmware_node");
-			ACPI_HANDLE_SET(dev, NULL);
+			ACPI_COMPANION_SET(dev, NULL);
 			/* acpi_bind_one() increase refcnt by one. */
 			put_device(dev);
 			kfree(entry);
@@ -328,6 +320,15 @@ int acpi_unbind_one(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(acpi_unbind_one);
 
+void acpi_preset_companion(struct device *dev, acpi_handle parent, u64 addr)
+{
+	struct acpi_device *adev;
+
+	if (!acpi_bus_get_device(acpi_get_child(parent, addr), &adev))
+		ACPI_COMPANION_SET(dev, &adev->dev);
+}
+EXPORT_SYMBOL_GPL(acpi_preset_companion);
+
 static int acpi_platform_notify(struct device *dev)
 {
 	struct acpi_bus_type *type = acpi_get_bus_type(dev);

Index: linux-pm/drivers/ata/libata-acpi.c
===================================================================

drivers/ata/libata-acpi.c +2 -2
@@ -185,7 +185,7 @@ void ata_acpi_bind_port(struct ata_port
 	if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA || !host_handle)
 		return;
 
-	ACPI_HANDLE_SET(&ap->tdev, acpi_get_child(host_handle, ap->port_no));
+	acpi_preset_companion(&ap->tdev, host_handle, ap->port_no);
 
 	if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
 		ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
@@ -222,7 +222,7 @@ void ata_acpi_bind_dev(struct ata_device
 		parent_handle = port_handle;
 	}
 
-	ACPI_HANDLE_SET(&dev->tdev, acpi_get_child(parent_handle, adr));
+	acpi_preset_companion(&dev->tdev, parent_handle, adr);
 
 	register_hotplug_dock_device(ata_dev_acpi_handle(dev),
 				     &ata_acpi_dev_dock_ops, dev, NULL, NULL);

Index: linux-pm/drivers/base/platform.c
===================================================================

drivers/base/platform.c +2 -2
@@ -432,7 +432,7 @@ struct platform_device *platform_device_
 		goto err_alloc;
 
 	pdev->dev.parent = pdevinfo->parent;
-	ACPI_HANDLE_SET(&pdev->dev, pdevinfo->acpi_node.handle);
+	ACPI_COMPANION_SET(&pdev->dev, pdevinfo->acpi_node.dev);
 
 	if (pdevinfo->dma_mask) {
 		/*
@@ -463,7 +463,7 @@ struct platform_device *platform_device_
 	ret = platform_device_add(pdev);
 	if (ret) {
 err:
-		ACPI_HANDLE_SET(&pdev->dev, NULL);
+		ACPI_COMPANION_SET(&pdev->dev, NULL);
 		kfree(pdev->dev.dma_mask);
 
 err_alloc:

Index: linux-pm/drivers/acpi/acpi_platform.c
===================================================================

drivers/acpi/acpi_platform.c +1 -1
@@ -111,7 +111,7 @@ int acpi_create_platform_device(struct a
 	pdevinfo.id = -1;
 	pdevinfo.res = resources;
 	pdevinfo.num_res = count;
-	pdevinfo.acpi_node.handle = adev->handle;
+	pdevinfo.acpi_node.dev = &adev->dev;
 	pdev = platform_device_register_full(&pdevinfo);
 	if (IS_ERR(pdev)) {
 		dev_err(&adev->dev, "platform device creation failed: %ld\n",

Index: linux-pm/drivers/hid/i2c-hid/i2c-hid.c
===================================================================

drivers/hid/i2c-hid/i2c-hid.c +1 -1
@@ -1012,7 +1012,7 @@ static int i2c_hid_probe(struct i2c_clie
 	hid->hid_get_raw_report = i2c_hid_get_raw_report;
 	hid->hid_output_raw_report = i2c_hid_output_raw_report;
 	hid->dev.parent = &client->dev;
-	ACPI_HANDLE_SET(&hid->dev, ACPI_HANDLE(&client->dev));
+	ACPI_COMPANION_SET(&hid->dev, ACPI_COMPANION(&client->dev));
 	hid->bus = BUS_I2C;
 	hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
 	hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);

Index: linux-pm/drivers/i2c/i2c-core.c
===================================================================

drivers/i2c/i2c-core.c +2 -2
@@ -674,7 +674,7 @@ i2c_new_device(struct i2c_adapter *adap,
 	client->dev.bus = &i2c_bus_type;
 	client->dev.type = &i2c_client_type;
 	client->dev.of_node = info->of_node;
-	ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
+	ACPI_COMPANION_SET(&client->dev, info->acpi_node.dev);
 
 	/* For 10-bit clients, add an arbitrary offset to avoid collisions */
 	dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
@@ -1103,7 +1103,7 @@ static acpi_status acpi_i2c_add_device(a
 		return AE_OK;
 
 	memset(&info, 0, sizeof(info));
-	info.acpi_node.handle = handle;
+	info.acpi_node.dev = &adev->dev;
 	info.irq = -1;
 
 	INIT_LIST_HEAD(&resource_list);

Index: linux-pm/drivers/mmc/core/sdio_bus.c
===================================================================

drivers/mmc/core/sdio_bus.c +1 -2
@@ -305,8 +305,7 @@ static void sdio_acpi_set_handle(struct
 	struct mmc_host *host = func->card->host;
 	u64 addr = (host->slotno << 16) | func->num;
 
-	ACPI_HANDLE_SET(&func->dev,
-			acpi_get_child(ACPI_HANDLE(host->parent), addr));
+	acpi_preset_companion(&func->dev, ACPI_HANDLE(host->parent), addr);
 }
 #else
 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}

Index: linux-pm/drivers/spi/spi.c
===================================================================

drivers/spi/spi.c +1 -1
@@ -1024,7 +1024,7 @@ static acpi_status acpi_spi_add_device(a
 		return AE_NO_MEMORY;
 	}
 
-	ACPI_HANDLE_SET(&spi->dev, handle);
+	ACPI_COMPANION_SET(&spi->dev, &adev->dev);
 	spi->irq = -1;
 
 	INIT_LIST_HEAD(&resource_list);

Index: linux-pm/drivers/acpi/device_pm.c
===================================================================

drivers/acpi/device_pm.c +1 -5
@@ -22,16 +22,12 @@
  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  */
 
-#include <linux/device.h>
+#include <linux/acpi.h>
 #include <linux/export.h>
 #include <linux/mutex.h>
 #include <linux/pm_qos.h>
 #include <linux/pm_runtime.h>
 
-#include <acpi/acpi.h>
-#include <acpi/acpi_bus.h>
-#include <acpi/acpi_drivers.h>
-
 #include "internal.h"
 
 #define _COMPONENT	ACPI_POWER_COMPONENT

Received: from fmsmsx107.amr.corp.intel.com (10.19.9.54) by
SHSMSX104.ccr.corp.intel.com (10.239.4.70) with Microsoft SMTP Server (TLS)
id 14.3.123.3; Sun, 10 Nov 2013 08:46:23 +0800
Received: from AZSMGA002.ch.intel.com (10.2.17.35) by FMSMSX107-1.fm.intel.com
(10.19.9.54) with Microsoft SMTP Server id 14.3.123.3; Sat, 9 Nov 2013
16:46:21 -0800
Received: from azsmga102.ch.intel.com ([10.2.16.52]) by
AZSMGA002-1.ch.intel.com with ESMTP; 09 Nov 2013 16:46:20 -0800
X-IronPort-Anti-Spam-Filtered: true
X-IronPort-Anti-Spam-Result: AssSAOTWflJPYKqG/2dsb2JhbABZgz+DR4IvsluHG4E+dIIlASkEUjAFAgUMBw4CEQE3FhOIBQGqWZIWgSmNBoEtCxGCYYFFA5Qug2GBL4kVh0aDJw
X-IronPort-AV: E=Sophos;i="4.93,669,1378882800";
d="scan'208";a="109266908"
Received: from v094114.home.net.pl ([79.96.170.134]) by mga14.intel.com with
SMTP; 09 Nov 2013 16:46:19 -0800
Received: from aayg161.neoplus.adsl.tpnet.pl [83.6.118.161] (HELO
vostro.rjw.lan) by serwer1319399.home.pl [79.96.170.134] with SMTP
(IdeaSmtpServer v0.80) id f863c1bc354b7571; Sun, 10 Nov 2013 01:46:16 +0100
From: "Rafael J. Wysocki" <[email protected]>
To: ACPI Devel Maling List <[email protected]>
CC: LKML <[email protected]>, Linux PCI
<[email protected]>, Greg Kroah-Hartman <[email protected]>,
Bjorn Helgaas <[email protected]>, Aaron Lu <[email protected]>, "Jarkko
Nikula" <[email protected]>, Lan Tianyu <[email protected]>,
Mika Westerberg <[email protected]>, "Luck, Tony"
<[email protected]>
Subject: [PATCH] ACPI / driver core: Store a device pointer in struct acpi_dev_node
Date: Sun, 10 Nov 2013 01:58:42 +0100
Message-ID: <[email protected]>
User-Agent: KMail/4.10.5 (Linux/3.12.0-rc6+; KDE/4.10.5; x86_64; ; )
Content-Transfer-Encoding: 7Bit
Content-Type: text/plain; charset="utf-8"
Return-Path: [email protected]
X-MS-Exchange-Organization-AVStamp-Mailbox: NAI;56073597;0;novirus
X-MS-Exchange-Organization-AuthSource: FMSMSX107.amr.corp.intel.com
X-MS-Exchange-Organization-AuthAs: Anonymous
MIME-Version: 1.0

From: Rafael J. Wysocki <[email protected]>

Modify struct acpi_dev_node to contain a pointer to struct device ambedded in the struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to that struct acpi_device. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

The rationale for using a struct device pointer instead of a struct acpi_device one as the member of struct acpi_dev_node is that it allows device.h to avoid including linux/acpi.h which would introduce quite a bit of compilation overhead for stuff that doesn't care about ACPI. In turn, moving the macros to linux/acpi.h forces the stuff that does care about ACPI to include that file as appropriate anyway.

The main motivation for doing this is that there are things represented by struct acpi_device objects that don't have valid ACPI handles (so called fixed ACPI hardware features, such as power and sleep buttons) and we would like to create platform device objects for them and "glue" them to their ACPI companions in the usual way (which currently is impossible due to the lack of valid ACPI handles). However, there are more reasons why it may be useful.

First, struct device pointers allow of much better type checking than void pointers which are ACPI handles, so it should be more difficult to write buggy code using modified struct acpi_dev_node and the new macros. Second, it should help to reduce the number of places in which the result of ACPI_HANDLE() is passed to acpi_bus_get_device() in order to obtain a pointer to the struct acpi_device associated with the given "physical" device, because now that pointer can be obtained directly by applying to_acpi_device() to the result of the ACPI_COMPANION() macro. Finally, it should make it easier to write generic code that will build both for CONFIG_ACPI set and unset without adding explicit compiler directives to it.

---

Hi Everybody,

First of all, I haven't tested this yet, so caveat emptor. I have compiled it on x86-64 for CONFIG_ACPI set and unset and I'm going to feed it to the auto build system shortly in case I overlooked something build-related.

Please have a look and let me know if you have any problems with this in principle. If not, I'd like to queue it up for inclusion by the end of the merge window or in the -rc2 time frame (to avoid collisions with any big merges), as I'd like to be able to work on top of it during the 3.14 cycle if possible.

Thanks,
Rafael

---
 arch/ia64/include/asm/pci.h   |    2 -
 arch/ia64/pci/pci.c           |    6 ++---
 arch/x86/include/asm/pci.h    |    2 -
 arch/x86/pci/acpi.c           |    4 +--
 drivers/acpi/acpi_platform.c  |    2 -
 drivers/acpi/device_pm.c      |    6 -----
 drivers/acpi/glue.c           |   45 +++++++++++++++++++++---------------------
 drivers/ata/libata-acpi.c     |    4 +--
 drivers/base/platform.c       |    4 +--
 drivers/hid/i2c-hid/i2c-hid.c |    2 -
 drivers/i2c/i2c-core.c        |    4 +--
 drivers/mmc/core/sdio_bus.c   |    3 --
 drivers/spi/spi.c             |    2 -
 include/acpi/acpi_bus.h       |    2 -
 include/linux/acpi.h          |   14 +++++++++++++
 include/linux/device.h        |   10 ---------
 16 files changed, 57 insertions(+), 55 deletions(-)

Index: linux-pm/include/linux/device.h
===================================================================

include/linux/device.h +1 -9
@@ -625,7 +625,7 @@ struct device_dma_parameters {
 
 struct acpi_dev_node {
 #ifdef CONFIG_ACPI
-	void	*handle;
+	struct device *dev;
 #endif
 };
 
@@ -769,14 +769,6 @@ static inline struct device *kobj_to_dev
 	return container_of(kobj, struct device, kobj);
 }
 
-#ifdef CONFIG_ACPI
-#define ACPI_HANDLE(dev)	((dev)->acpi_node.handle)
-#define ACPI_HANDLE_SET(dev, _handle_)	(dev)->acpi_node.handle = (_handle_)
-#else
-#define ACPI_HANDLE(dev)	(NULL)
-#define ACPI_HANDLE_SET(dev, _handle_)	do { } while (0)
-#endif
-
 /* Get the wakeup routines, which depend on struct device */
 #include <linux/pm_wakeup.h>
 

Index: linux-pm/include/acpi/acpi_bus.h
===================================================================

include/acpi/acpi_bus.h +1 -1
@@ -431,9 +431,9 @@ static inline acpi_handle acpi_get_child
 {
 	return acpi_find_child(handle, addr, false);
 }
+void acpi_preset_companion(struct device *dev, acpi_handle parent, u64 addr);
 int acpi_is_root_bridge(acpi_handle);
 struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle);
-#define DEVICE_ACPI_HANDLE(dev) ((acpi_handle)ACPI_HANDLE(dev))
 
 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int state);
 int acpi_disable_wakeup_device_power(struct acpi_device *dev);

Index: linux-pm/include/linux/acpi.h
===================================================================

include/linux/acpi.h +14 -0
@@ -44,6 +44,14 @@
 #include <acpi/acpi_numa.h>
 #include <asm/acpi.h>
 
+#define ACPI_COMPANION(device)		((device)->acpi_node.dev)
+#define ACPI_COMPANION_SET(dev, acc)	ACPI_COMPANION(dev) = (acc)
+#define ACPI_HANDLE(dev)				\
+({							\
+	struct device *acc = ACPI_COMPANION(dev);	\
+	acc ? to_acpi_device(acc)->handle : NULL;	\
+})
+
 enum acpi_irq_model_id {
 	ACPI_IRQ_MODEL_PIC = 0,
 	ACPI_IRQ_MODEL_IOAPIC,
@@ -407,6 +415,10 @@ static inline bool acpi_driver_match_dev
 
 #define acpi_disabled 1
 
+#define ACPI_COMPANION(device)		(NULL)
+#define ACPI_COMPANION_SET(dev, acc)	do { } while (0)
+#define ACPI_HANDLE(dev)		(NULL)
+
 static inline void acpi_early_init(void) { }
 
 static inline int early_acpi_boot_init(void)
@@ -475,6 +487,8 @@ static inline bool acpi_driver_match_dev
 
 #endif	/* !CONFIG_ACPI */
 
+#define DEVICE_ACPI_HANDLE(dev)	ACPI_HANDLE(dev)
+
 #ifdef CONFIG_ACPI
 void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
 			       u32 pm1a_ctrl,  u32 pm1b_ctrl));

Index: linux-pm/arch/ia64/include/asm/pci.h
===================================================================

arch/ia64/include/asm/pci.h +1 -1
@@ -95,7 +95,7 @@ struct iospace_resource {
 };
 
 struct pci_controller {
-	void *acpi_handle;
+	struct device *acpi_companion;
 	void *iommu;
 	int segment;
 	int node;		/* nearest node with memory or -1 for global allocation */

Index: linux-pm/arch/ia64/pci/pci.c
===================================================================

arch/ia64/pci/pci.c +3 -3
@@ -436,9 +436,9 @@ struct pci_bus *pci_acpi_scan_root(struc
 	if (!controller)
 		return NULL;
 
-	controller->acpi_handle = device->handle;
+	controller->acpi_companion = &device->dev;
 
-	pxm = acpi_get_pxm(controller->acpi_handle);
+	pxm = acpi_get_pxm(device->handle);
 #ifdef CONFIG_NUMA
 	if (pxm >= 0)
 		controller->node = pxm_to_node(pxm);
@@ -489,7 +489,7 @@ int pcibios_root_bridge_prepare(struct p
 {
 	struct pci_controller *controller = bridge->bus->sysdata;
 
-	ACPI_HANDLE_SET(&bridge->dev, controller->acpi_handle);
+	ACPI_COMPANION_SET(&bridge->dev, controller->acpi_companion);
 	return 0;
 }
 

Index: linux-pm/arch/x86/include/asm/pci.h
===================================================================

arch/x86/include/asm/pci.h +1 -1
@@ -15,7 +15,7 @@ struct pci_sysdata {
 	int		domain;		/* PCI domain */
 	int		node;		/* NUMA node */
 #ifdef CONFIG_ACPI
-	void		*acpi;		/* ACPI-specific data */
+	struct device	*acpi_companion;	/* ACPI companion device */
 #endif
 #ifdef CONFIG_X86_64
 	void		*iommu;		/* IOMMU private data */

Index: linux-pm/arch/x86/pci/acpi.c
===================================================================

arch/x86/pci/acpi.c +2 -2
@@ -518,7 +518,7 @@ struct pci_bus *pci_acpi_scan_root(struc
 	sd = &info->sd;
 	sd->domain = domain;
 	sd->node = node;
-	sd->acpi = device->handle;
+	sd->acpi_companion = &device->dev;
 	/*
 	 * Maybe the desired pci bus has been already scanned. In such case
 	 * it is unnecessary to scan the pci bus with the given domain,busnum.
@@ -589,7 +589,7 @@ int pcibios_root_bridge_prepare(struct p
 {
 	struct pci_sysdata *sd = bridge->bus->sysdata;
 
-	ACPI_HANDLE_SET(&bridge->dev, sd->acpi);
+	ACPI_COMPANION_SET(&bridge->dev, sd->acpi_companion);
 	return 0;
 }
 

Index: linux-pm/drivers/acpi/glue.c
===================================================================

drivers/acpi/glue.c +23 -22
@@ -197,30 +197,27 @@ static void acpi_physnode_link_name(char
 
 int acpi_bind_one(struct device *dev, acpi_handle handle)
 {
-	struct acpi_device *acpi_dev;
-	acpi_status status;
+	struct acpi_device *acpi_dev = NULL;
 	struct acpi_device_physical_node *physical_node, *pn;
 	char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
 	struct list_head *physnode_list;
 	unsigned int node_id;
 	int retval = -EINVAL;
 
-	if (ACPI_HANDLE(dev)) {
+	if (ACPI_COMPANION(dev)) {
 		if (handle) {
-			dev_warn(dev, "ACPI handle is already set\n");
+			dev_warn(dev, "ACPI companion already set\n");
 			return -EINVAL;
 		} else {
-			handle = ACPI_HANDLE(dev);
+			acpi_dev = to_acpi_device(ACPI_COMPANION(dev));
 		}
+	} else {
+		acpi_bus_get_device(handle, &acpi_dev);
 	}
-	if (!handle)
+	if (!acpi_dev)
 		return -EINVAL;
 
 	get_device(dev);
-	status = acpi_bus_get_device(handle, &acpi_dev);
-	if (ACPI_FAILURE(status))
-		goto err;
-
 	physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
 	if (!physical_node) {
 		retval = -ENOMEM;
@@ -242,7 +239,7 @@ int acpi_bind_one(struct device *dev, ac
 
 			dev_warn(dev, "Already associated with ACPI node\n");
 			kfree(physical_node);
-			if (ACPI_HANDLE(dev) != handle)
+			if (ACPI_COMPANION(dev) != &acpi_dev->dev)
 				goto err;
 
 			put_device(dev);
@@ -259,8 +256,8 @@ int acpi_bind_one(struct device *dev, ac
 	list_add(&physical_node->node, physnode_list);
 	acpi_dev->physical_node_count++;
 
-	if (!ACPI_HANDLE(dev))
-		ACPI_HANDLE_SET(dev, acpi_dev->handle);
+	if (!ACPI_COMPANION(dev))
+		ACPI_COMPANION_SET(dev, &acpi_dev->dev);
 
 	acpi_physnode_link_name(physical_node_name, node_id);
 	retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
@@ -283,7 +280,7 @@ int acpi_bind_one(struct device *dev, ac
 	return 0;
 
  err:
-	ACPI_HANDLE_SET(dev, NULL);
+	ACPI_COMPANION_SET(dev, NULL);
 	put_device(dev);
 	return retval;
 }
@@ -293,16 +290,11 @@ int acpi_unbind_one(struct device *dev)
 {
 	struct acpi_device_physical_node *entry;
 	struct acpi_device *acpi_dev;
-	acpi_status status;
 
-	if (!ACPI_HANDLE(dev))
+	if (!ACPI_COMPANION(dev))
 		return 0;
 
-	status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev);
-	if (ACPI_FAILURE(status)) {
-		dev_err(dev, "Oops, ACPI handle corrupt in %s()\n", __func__);
-		return -EINVAL;
-	}
+	acpi_dev = to_acpi_device(ACPI_COMPANION(dev));
 
 	mutex_lock(&acpi_dev->physical_node_lock);
 
@@ -316,7 +308,7 @@ int acpi_unbind_one(struct device *dev)
 			acpi_physnode_link_name(physnode_name, entry->node_id);
 			sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
 			sysfs_remove_link(&dev->kobj, "firmware_node");
-			ACPI_HANDLE_SET(dev, NULL);
+			ACPI_COMPANION_SET(dev, NULL);
 			/* acpi_bind_one() increase refcnt by one. */
 			put_device(dev);
 			kfree(entry);
@@ -328,6 +320,15 @@ int acpi_unbind_one(struct device *dev)
 }
 EXPORT_SYMBOL_GPL(acpi_unbind_one);
 
+void acpi_preset_companion(struct device *dev, acpi_handle parent, u64 addr)
+{
+	struct acpi_device *adev;
+
+	if (!acpi_bus_get_device(acpi_get_child(parent, addr), &adev))
+		ACPI_COMPANION_SET(dev, &adev->dev);
+}
+EXPORT_SYMBOL_GPL(acpi_preset_companion);
+
 static int acpi_platform_notify(struct device *dev)
 {
 	struct acpi_bus_type *type = acpi_get_bus_type(dev);

Index: linux-pm/drivers/ata/libata-acpi.c
===================================================================

drivers/ata/libata-acpi.c +2 -2
@@ -185,7 +185,7 @@ void ata_acpi_bind_port(struct ata_port
 	if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA || !host_handle)
 		return;
 
-	ACPI_HANDLE_SET(&ap->tdev, acpi_get_child(host_handle, ap->port_no));
+	acpi_preset_companion(&ap->tdev, host_handle, ap->port_no);
 
 	if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
 		ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
@@ -222,7 +222,7 @@ void ata_acpi_bind_dev(struct ata_device
 		parent_handle = port_handle;
 	}
 
-	ACPI_HANDLE_SET(&dev->tdev, acpi_get_child(parent_handle, adr));
+	acpi_preset_companion(&dev->tdev, parent_handle, adr);
 
 	register_hotplug_dock_device(ata_dev_acpi_handle(dev),
 				     &ata_acpi_dev_dock_ops, dev, NULL, NULL);

Index: linux-pm/drivers/base/platform.c
===================================================================

drivers/base/platform.c +2 -2
@@ -432,7 +432,7 @@ struct platform_device *platform_device_
 		goto err_alloc;
 
 	pdev->dev.parent = pdevinfo->parent;
-	ACPI_HANDLE_SET(&pdev->dev, pdevinfo->acpi_node.handle);
+	ACPI_COMPANION_SET(&pdev->dev, pdevinfo->acpi_node.dev);
 
 	if (pdevinfo->dma_mask) {
 		/*
@@ -463,7 +463,7 @@ struct platform_device *platform_device_
 	ret = platform_device_add(pdev);
 	if (ret) {
 err:
-		ACPI_HANDLE_SET(&pdev->dev, NULL);
+		ACPI_COMPANION_SET(&pdev->dev, NULL);
 		kfree(pdev->dev.dma_mask);
 
 err_alloc:

Index: linux-pm/drivers/acpi/acpi_platform.c
===================================================================

drivers/acpi/acpi_platform.c +1 -1
@@ -111,7 +111,7 @@ int acpi_create_platform_device(struct a
 	pdevinfo.id = -1;
 	pdevinfo.res = resources;
 	pdevinfo.num_res = count;
-	pdevinfo.acpi_node.handle = adev->handle;
+	pdevinfo.acpi_node.dev = &adev->dev;
 	pdev = platform_device_register_full(&pdevinfo);
 	if (IS_ERR(pdev)) {
 		dev_err(&adev->dev, "platform device creation failed: %ld\n",

Index: linux-pm/drivers/hid/i2c-hid/i2c-hid.c
===================================================================

drivers/hid/i2c-hid/i2c-hid.c +1 -1
@@ -1012,7 +1012,7 @@ static int i2c_hid_probe(struct i2c_clie
 	hid->hid_get_raw_report = i2c_hid_get_raw_report;
 	hid->hid_output_raw_report = i2c_hid_output_raw_report;
 	hid->dev.parent = &client->dev;
-	ACPI_HANDLE_SET(&hid->dev, ACPI_HANDLE(&client->dev));
+	ACPI_COMPANION_SET(&hid->dev, ACPI_COMPANION(&client->dev));
 	hid->bus = BUS_I2C;
 	hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
 	hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);

Index: linux-pm/drivers/i2c/i2c-core.c
===================================================================

drivers/i2c/i2c-core.c +2 -2
@@ -674,7 +674,7 @@ i2c_new_device(struct i2c_adapter *adap,
 	client->dev.bus = &i2c_bus_type;
 	client->dev.type = &i2c_client_type;
 	client->dev.of_node = info->of_node;
-	ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
+	ACPI_COMPANION_SET(&client->dev, info->acpi_node.dev);
 
 	/* For 10-bit clients, add an arbitrary offset to avoid collisions */
 	dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
@@ -1103,7 +1103,7 @@ static acpi_status acpi_i2c_add_device(a
 		return AE_OK;
 
 	memset(&info, 0, sizeof(info));
-	info.acpi_node.handle = handle;
+	info.acpi_node.dev = &adev->dev;
 	info.irq = -1;
 
 	INIT_LIST_HEAD(&resource_list);

Index: linux-pm/drivers/mmc/core/sdio_bus.c
===================================================================

drivers/mmc/core/sdio_bus.c +1 -2
@@ -305,8 +305,7 @@ static void sdio_acpi_set_handle(struct
 	struct mmc_host *host = func->card->host;
 	u64 addr = (host->slotno << 16) | func->num;
 
-	ACPI_HANDLE_SET(&func->dev,
-			acpi_get_child(ACPI_HANDLE(host->parent), addr));
+	acpi_preset_companion(&func->dev, ACPI_HANDLE(host->parent), addr);
 }
 #else
 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}

Index: linux-pm/drivers/spi/spi.c
===================================================================

drivers/spi/spi.c +1 -1
@@ -1024,7 +1024,7 @@ static acpi_status acpi_spi_add_device(a
 		return AE_NO_MEMORY;
 	}
 
-	ACPI_HANDLE_SET(&spi->dev, handle);
+	ACPI_COMPANION_SET(&spi->dev, &adev->dev);
 	spi->irq = -1;
 
 	INIT_LIST_HEAD(&resource_list);

Index: linux-pm/drivers/acpi/device_pm.c
===================================================================

drivers/acpi/device_pm.c +1 -5
@@ -22,16 +22,12 @@
  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  */
 
-#include <linux/device.h>
+#include <linux/acpi.h>
 #include <linux/export.h>
 #include <linux/mutex.h>
 #include <linux/pm_qos.h>
 #include <linux/pm_runtime.h>
 
-#include <acpi/acpi.h>
-#include <acpi/acpi_bus.h>
-#include <acpi/acpi_drivers.h>
-
 #include "internal.h"
 
 #define _COMPONENT	ACPI_POWER_COMPONENT
Signature · 2 lines
Best regards
Tianyu Lan
#3 lore raw
Quoted 25 lines · Lan Tianyu
Quoted 15 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Modify struct acpi_dev_node to contain a pointer to struct device ambedded in the struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to that struct acpi_device. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

Quoted 8 lines

The rationale for using a struct device pointer instead of a struct acpi_device one as the member of struct acpi_dev_node is that it allows device.h to avoid including linux/acpi.h which would introduce quite a bit of compilation overhead for stuff that doesn't care about ACPI.
In turn, moving the macros to linux/acpi.h forces
the stuff that does care about ACPI to include that file as appropriate anyway.

Quoted 5 lines

How about declaring "struct acpi_device" in the device.h? This can help to use struct acpi_device without including linux/acpi.h.

struct iommu_ops and struct iommu_group have been used by the same way in the device.h.

Yes, they are. Well, that appears to work too.

Updated patch is appended. It also contains some fixes for problems reported by the auto build system and it's been tested on x86-64 now, so it should be reasonably close to final.

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>
Subject: ACPI / driver core: Store an ACPI device pointer in struct acpi_dev_node

Modify struct acpi_dev_node to contain a pointer to struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to it. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

The main motivation for doing this is that there are things represented by struct acpi_device objects that don't have valid ACPI handles (so called fixed ACPI hardware features, such as power and sleep buttons) and we would like to create platform device objects for them and "glue" them to their ACPI companions in the usual way (which currently is impossible due to the lack of valid ACPI handles). However, there are more reasons why it may be useful.

First, struct acpi_device pointers allow of much better type checking than void pointers which are ACPI handles, so it should be more difficult to write buggy code using modified struct acpi_dev_node and the new macros. Second, the change should help to reduce (over time) the number of places in which the result of ACPI_HANDLE() is passed to acpi_bus_get_device() in order to obtain a pointer to the struct acpi_device associated with the given "physical" device, because now that pointer is returned by ACPI_COMPANION() directly. Finally, the change should make it easier to write generic code that will build both for CONFIG_ACPI set and unset without adding explicit compiler directives to it.

---
 arch/ia64/hp/common/sba_iommu.c              |    2 -
 arch/ia64/include/asm/pci.h                  |    2 -
 arch/ia64/pci/pci.c                          |    6 +--
 arch/ia64/sn/kernel/io_acpi_init.c           |    4 +-
 arch/x86/include/asm/pci.h                   |    2 -
 arch/x86/pci/acpi.c                          |    4 +-
 drivers/acpi/acpi_platform.c                 |    2 -
 drivers/acpi/device_pm.c                     |    6 ---
 drivers/acpi/glue.c                          |   47 +++++++++++++--------------
 drivers/ata/libata-acpi.c                    |    4 +-
 drivers/base/platform.c                      |    4 +-
 drivers/gpu/drm/radeon/radeon_atpx_handler.c |    3 -
 drivers/hid/i2c-hid/i2c-hid.c                |    2 -
 drivers/i2c/i2c-core.c                       |    4 +-
 drivers/ide/ide-acpi.c                       |    3 -
 drivers/mmc/core/sdio_bus.c                  |    3 -
 drivers/pci/hotplug/sgi_hotplug.c            |    4 +-
 drivers/spi/spi.c                            |    2 -
 include/acpi/acpi_bus.h                      |    2 -
 include/linux/acpi.h                         |   15 ++++++++
 include/linux/device.h                       |   12 +-----
 21 files changed, 67 insertions(+), 66 deletions(-)
include/linux/device.h +3 -9
Index: linux-pm/include/linux/device.h
===================================================================
@@ -623,9 +623,11 @@ struct device_dma_parameters {
 	unsigned long segment_boundary_mask;
 };
 
+struct acpi_device;
+
 struct acpi_dev_node {
 #ifdef CONFIG_ACPI
-	void	*handle;
+	struct acpi_device *companion;
 #endif
 };
 
@@ -769,14 +771,6 @@ static inline struct device *kobj_to_dev
 	return container_of(kobj, struct device, kobj);
 }
 
-#ifdef CONFIG_ACPI
-#define ACPI_HANDLE(dev)	((dev)->acpi_node.handle)
-#define ACPI_HANDLE_SET(dev, _handle_)	(dev)->acpi_node.handle = (_handle_)
-#else
-#define ACPI_HANDLE(dev)	(NULL)
-#define ACPI_HANDLE_SET(dev, _handle_)	do { } while (0)
-#endif
-
 /* Get the wakeup routines, which depend on struct device */
 #include <linux/pm_wakeup.h>
 
include/acpi/acpi_bus.h +1 -1
Index: linux-pm/include/acpi/acpi_bus.h
===================================================================
@@ -431,9 +431,9 @@ static inline acpi_handle acpi_get_child
 {
 	return acpi_find_child(handle, addr, false);
 }
+void acpi_preset_companion(struct device *dev, acpi_handle parent, u64 addr);
 int acpi_is_root_bridge(acpi_handle);
 struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle);
-#define DEVICE_ACPI_HANDLE(dev) ((acpi_handle)ACPI_HANDLE(dev))
 
 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int state);
 int acpi_disable_wakeup_device_power(struct acpi_device *dev);
include/linux/acpi.h +15 -0
Index: linux-pm/include/linux/acpi.h
===================================================================
@@ -44,6 +44,15 @@
 #include <acpi/acpi_numa.h>
 #include <asm/acpi.h>
 
+static inline acpi_handle acpi_device_handle(struct acpi_device *adev)
+{
+	return adev ? adev->handle : NULL;
+}
+
+#define ACPI_COMPANION(dev)		((dev)->acpi_node.companion)
+#define ACPI_COMPANION_SET(dev, adev)	ACPI_COMPANION(dev) = (adev)
+#define ACPI_HANDLE(dev)		acpi_device_handle(ACPI_COMPANION(dev))
+
 enum acpi_irq_model_id {
 	ACPI_IRQ_MODEL_PIC = 0,
 	ACPI_IRQ_MODEL_IOAPIC,
@@ -407,6 +416,10 @@ static inline bool acpi_driver_match_dev
 
 #define acpi_disabled 1
 
+#define ACPI_COMPANION(dev)		(NULL)
+#define ACPI_COMPANION_SET(dev, adev)	do { } while (0)
+#define ACPI_HANDLE(dev)		(NULL)
+
 static inline void acpi_early_init(void) { }
 
 static inline int early_acpi_boot_init(void)
@@ -475,6 +488,8 @@ static inline bool acpi_driver_match_dev
 
 #endif	/* !CONFIG_ACPI */
 
+#define DEVICE_ACPI_HANDLE(dev)	ACPI_HANDLE(dev)
+
 #ifdef CONFIG_ACPI
 void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
 			       u32 pm1a_ctrl,  u32 pm1b_ctrl));
arch/ia64/include/asm/pci.h +1 -1
Index: linux-pm/arch/ia64/include/asm/pci.h
===================================================================
@@ -95,7 +95,7 @@ struct iospace_resource {
 };
 
 struct pci_controller {
-	void *acpi_handle;
+	struct acpi_device *companion;
 	void *iommu;
 	int segment;
 	int node;		/* nearest node with memory or -1 for global allocation */
arch/ia64/pci/pci.c +3 -3
Index: linux-pm/arch/ia64/pci/pci.c
===================================================================
@@ -436,9 +436,9 @@ struct pci_bus *pci_acpi_scan_root(struc
 	if (!controller)
 		return NULL;
 
-	controller->acpi_handle = device->handle;
+	controller->companion = device;
 
-	pxm = acpi_get_pxm(controller->acpi_handle);
+	pxm = acpi_get_pxm(device->handle);
 #ifdef CONFIG_NUMA
 	if (pxm >= 0)
 		controller->node = pxm_to_node(pxm);
@@ -489,7 +489,7 @@ int pcibios_root_bridge_prepare(struct p
 {
 	struct pci_controller *controller = bridge->bus->sysdata;
 
-	ACPI_HANDLE_SET(&bridge->dev, controller->acpi_handle);
+	ACPI_COMPANION_SET(&bridge->dev, controller->companion);
 	return 0;
 }
 
arch/ia64/hp/common/sba_iommu.c +1 -1
Index: linux-pm/arch/ia64/hp/common/sba_iommu.c
===================================================================
@@ -1992,7 +1992,7 @@ sba_connect_bus(struct pci_bus *bus)
 	if (PCI_CONTROLLER(bus)->iommu)
 		return;
 
-	handle = PCI_CONTROLLER(bus)->acpi_handle;
+	handle = acpi_device_handle(PCI_CONTROLLER(bus)->companion);
 	if (!handle)
 		return;
 
arch/ia64/sn/kernel/io_acpi_init.c +2 -2
Index: linux-pm/arch/ia64/sn/kernel/io_acpi_init.c
===================================================================
@@ -132,7 +132,7 @@ sn_get_bussoft_ptr(struct pci_bus *bus)
 	struct acpi_resource_vendor_typed *vendor;
 
 
-	handle = PCI_CONTROLLER(bus)->acpi_handle;
+	handle = acpi_device_handle(PCI_CONTROLLER(bus)->companion);
 	status = acpi_get_vendor_resource(handle, METHOD_NAME__CRS,
 					  &sn_uuid, &buffer);
 	if (ACPI_FAILURE(status)) {
@@ -360,7 +360,7 @@ sn_acpi_get_pcidev_info(struct pci_dev *
 	acpi_status status;
 	struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL };
 
-	rootbus_handle = PCI_CONTROLLER(dev)->acpi_handle;
+	rootbus_handle = acpi_device_handle(PCI_CONTROLLER(dev)->companion);
         status = acpi_evaluate_integer(rootbus_handle, METHOD_NAME__SEG, NULL,
                                        &segment);
         if (ACPI_SUCCESS(status)) {
arch/x86/include/asm/pci.h +1 -1
Index: linux-pm/arch/x86/include/asm/pci.h
===================================================================
@@ -15,7 +15,7 @@ struct pci_sysdata {
 	int		domain;		/* PCI domain */
 	int		node;		/* NUMA node */
 #ifdef CONFIG_ACPI
-	void		*acpi;		/* ACPI-specific data */
+	struct acpi_device *companion;	/* ACPI companion device */
 #endif
 #ifdef CONFIG_X86_64
 	void		*iommu;		/* IOMMU private data */
arch/x86/pci/acpi.c +2 -2
Index: linux-pm/arch/x86/pci/acpi.c
===================================================================
@@ -518,7 +518,7 @@ struct pci_bus *pci_acpi_scan_root(struc
 	sd = &info->sd;
 	sd->domain = domain;
 	sd->node = node;
-	sd->acpi = device->handle;
+	sd->companion = device;
 	/*
 	 * Maybe the desired pci bus has been already scanned. In such case
 	 * it is unnecessary to scan the pci bus with the given domain,busnum.
@@ -589,7 +589,7 @@ int pcibios_root_bridge_prepare(struct p
 {
 	struct pci_sysdata *sd = bridge->bus->sysdata;
 
-	ACPI_HANDLE_SET(&bridge->dev, sd->acpi);
+	ACPI_COMPANION_SET(&bridge->dev, sd->companion);
 	return 0;
 }
 
drivers/acpi/glue.c +23 -24

107 lines not on this page · raw message

drivers/ata/libata-acpi.c +2 -2
Index: linux-pm/drivers/ata/libata-acpi.c
===================================================================
@@ -185,7 +185,7 @@ void ata_acpi_bind_port(struct ata_port
 	if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA || !host_handle)
 		return;
 
-	ACPI_HANDLE_SET(&ap->tdev, acpi_get_child(host_handle, ap->port_no));
+	acpi_preset_companion(&ap->tdev, host_handle, ap->port_no);
 
 	if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
 		ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
@@ -222,7 +222,7 @@ void ata_acpi_bind_dev(struct ata_device
 		parent_handle = port_handle;
 	}
 
-	ACPI_HANDLE_SET(&dev->tdev, acpi_get_child(parent_handle, adr));
+	acpi_preset_companion(&dev->tdev, parent_handle, adr);
 
 	register_hotplug_dock_device(ata_dev_acpi_handle(dev),
 				     &ata_acpi_dev_dock_ops, dev, NULL, NULL);
drivers/base/platform.c +2 -2
Index: linux-pm/drivers/base/platform.c
===================================================================
@@ -432,7 +432,7 @@ struct platform_device *platform_device_
 		goto err_alloc;
 
 	pdev->dev.parent = pdevinfo->parent;
-	ACPI_HANDLE_SET(&pdev->dev, pdevinfo->acpi_node.handle);
+	ACPI_COMPANION_SET(&pdev->dev, pdevinfo->acpi_node.companion);
 
 	if (pdevinfo->dma_mask) {
 		/*
@@ -463,7 +463,7 @@ struct platform_device *platform_device_
 	ret = platform_device_add(pdev);
 	if (ret) {
 err:
-		ACPI_HANDLE_SET(&pdev->dev, NULL);
+		ACPI_COMPANION_SET(&pdev->dev, NULL);
 		kfree(pdev->dev.dma_mask);
 
 err_alloc:
drivers/acpi/acpi_platform.c +1 -1
Index: linux-pm/drivers/acpi/acpi_platform.c
===================================================================
@@ -111,7 +111,7 @@ int acpi_create_platform_device(struct a
 	pdevinfo.id = -1;
 	pdevinfo.res = resources;
 	pdevinfo.num_res = count;
-	pdevinfo.acpi_node.handle = adev->handle;
+	pdevinfo.acpi_node.companion = adev;
 	pdev = platform_device_register_full(&pdevinfo);
 	if (IS_ERR(pdev)) {
 		dev_err(&adev->dev, "platform device creation failed: %ld\n",
drivers/hid/i2c-hid/i2c-hid.c +1 -1
Index: linux-pm/drivers/hid/i2c-hid/i2c-hid.c
===================================================================
@@ -1012,7 +1012,7 @@ static int i2c_hid_probe(struct i2c_clie
 	hid->hid_get_raw_report = i2c_hid_get_raw_report;
 	hid->hid_output_raw_report = i2c_hid_output_raw_report;
 	hid->dev.parent = &client->dev;
-	ACPI_HANDLE_SET(&hid->dev, ACPI_HANDLE(&client->dev));
+	ACPI_COMPANION_SET(&hid->dev, ACPI_COMPANION(&client->dev));
 	hid->bus = BUS_I2C;
 	hid->version = le16_to_cpu(ihid->hdesc.bcdVersion);
 	hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
drivers/i2c/i2c-core.c +2 -2
Index: linux-pm/drivers/i2c/i2c-core.c
===================================================================
@@ -674,7 +674,7 @@ i2c_new_device(struct i2c_adapter *adap,
 	client->dev.bus = &i2c_bus_type;
 	client->dev.type = &i2c_client_type;
 	client->dev.of_node = info->of_node;
-	ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
+	ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion);
 
 	/* For 10-bit clients, add an arbitrary offset to avoid collisions */
 	dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
@@ -1103,7 +1103,7 @@ static acpi_status acpi_i2c_add_device(a
 		return AE_OK;
 
 	memset(&info, 0, sizeof(info));
-	info.acpi_node.handle = handle;
+	info.acpi_node.companion = adev;
 	info.irq = -1;
 
 	INIT_LIST_HEAD(&resource_list);
drivers/mmc/core/sdio_bus.c +1 -2
Index: linux-pm/drivers/mmc/core/sdio_bus.c
===================================================================
@@ -305,8 +305,7 @@ static void sdio_acpi_set_handle(struct
 	struct mmc_host *host = func->card->host;
 	u64 addr = (host->slotno << 16) | func->num;
 
-	ACPI_HANDLE_SET(&func->dev,
-			acpi_get_child(ACPI_HANDLE(host->parent), addr));
+	acpi_preset_companion(&func->dev, ACPI_HANDLE(host->parent), addr);
 }
 #else
 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
drivers/spi/spi.c +1 -1
Index: linux-pm/drivers/spi/spi.c
===================================================================
@@ -1024,7 +1024,7 @@ static acpi_status acpi_spi_add_device(a
 		return AE_NO_MEMORY;
 	}
 
-	ACPI_HANDLE_SET(&spi->dev, handle);
+	ACPI_COMPANION_SET(&spi->dev, adev);
 	spi->irq = -1;
 
 	INIT_LIST_HEAD(&resource_list);
drivers/acpi/device_pm.c +1 -5
Index: linux-pm/drivers/acpi/device_pm.c
===================================================================
@@ -22,16 +22,12 @@
  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  */
 
-#include <linux/device.h>
+#include <linux/acpi.h>
 #include <linux/export.h>
 #include <linux/mutex.h>
 #include <linux/pm_qos.h>
 #include <linux/pm_runtime.h>
 
-#include <acpi/acpi.h>
-#include <acpi/acpi_bus.h>
-#include <acpi/acpi_drivers.h>
-
 #include "internal.h"
 
 #define _COMPONENT	ACPI_POWER_COMPONENT
drivers/ide/ide-acpi.c +1 -2
Index: linux-pm/drivers/ide/ide-acpi.c
===================================================================
@@ -7,6 +7,7 @@
  * Copyright (C) 2006 Hannes Reinecke
  */
 
+#include <linux/acpi.h>
 #include <linux/ata.h>
 #include <linux/delay.h>
 #include <linux/device.h>
@@ -19,8 +20,6 @@
 #include <linux/dmi.h>
 #include <linux/module.h>
 
-#include <acpi/acpi_bus.h>
-
 #define REGS_PER_GTF		7
 
 struct GTM_buffer {
drivers/pci/hotplug/sgi_hotplug.c +2 -2
Index: linux-pm/drivers/pci/hotplug/sgi_hotplug.c
===================================================================
@@ -9,6 +9,7 @@
  * Work to add BIOS PROM support was completed by Mike Habeck.
  */
 
+#include <linux/acpi.h>
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
@@ -29,7 +30,6 @@
 #include <asm/sn/sn_feature_sets.h>
 #include <asm/sn/sn_sal.h>
 #include <asm/sn/types.h>
-#include <linux/acpi.h>
 #include <asm/sn/acpi.h>
 
 #include "../pci.h"
@@ -414,7 +414,7 @@ static int enable_slot(struct hotplug_sl
 		acpi_handle rethandle;
 		acpi_status ret;
 
-		phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle;
+		phandle = acpi_device_handle(PCI_CONTROLLER(slot->pci_bus)->companion);
 
 		if (acpi_bus_get_device(phandle, &pdevice)) {
 			dev_dbg(&slot->pci_bus->self->dev,
drivers/gpu/drm/radeon/radeon_atpx_handler.c +1 -2
Index: linux-pm/drivers/gpu/drm/radeon/radeon_atpx_handler.c
===================================================================
@@ -8,8 +8,7 @@
  */
 #include <linux/vga_switcheroo.h>
 #include <linux/slab.h>
-#include <acpi/acpi.h>
-#include <acpi/acpi_bus.h>
+#include <linux/acpi.h>
 #include <linux/pci.h>
 
 #include "radeon_acpi.h"
maintainer Acked-by
#4 lore raw
Quoted 79 lines · Rafael J. Wysocki
Quoted 25 lines · Lan Tianyu
Quoted 15 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Modify struct acpi_dev_node to contain a pointer to struct device ambedded in the struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to that struct acpi_device. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

Quoted 8 lines

The rationale for using a struct device pointer instead of a struct acpi_device one as the member of struct acpi_dev_node is that it allows device.h to avoid including linux/acpi.h which would introduce quite a bit of compilation overhead for stuff that doesn't care about ACPI.
In turn, moving the macros to linux/acpi.h forces
the stuff that does care about ACPI to include that file as appropriate anyway.

Quoted 5 lines

How about declaring "struct acpi_device" in the device.h? This can help to use struct acpi_device without including linux/acpi.h.

struct iommu_ops and struct iommu_group have been used by the same way in the device.h.

Yes, they are. Well, that appears to work too.

Updated patch is appended. It also contains some fixes for problems reported by the auto build system and it's been tested on x86-64 now, so it should be reasonably close to final.

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>
Subject: ACPI / driver core: Store an ACPI device pointer in struct acpi_dev_node

Modify struct acpi_dev_node to contain a pointer to struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to it. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

The main motivation for doing this is that there are things represented by struct acpi_device objects that don't have valid ACPI handles (so called fixed ACPI hardware features, such as power and sleep buttons) and we would like to create platform device objects for them and "glue" them to their ACPI companions in the usual way (which currently is impossible due to the lack of valid ACPI handles). However, there are more reasons why it may be useful.

First, struct acpi_device pointers allow of much better type checking than void pointers which are ACPI handles, so it should be more difficult to write buggy code using modified struct acpi_dev_node and the new macros. Second, the change should help to reduce (over time) the number of places in which the result of ACPI_HANDLE() is passed to acpi_bus_get_device() in order to obtain a pointer to the struct acpi_device associated with the given "physical" device, because now that pointer is returned by ACPI_COMPANION() directly. Finally, the change should make it easier to write generic code that will build both for CONFIG_ACPI set and unset without adding explicit compiler directives to it.

Quoted 1 line

--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw
Quoted 80 lines · Greg Kroah-Hartman
Quoted 79 lines · Rafael J. Wysocki
Quoted 25 lines · Lan Tianyu
Quoted 15 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Modify struct acpi_dev_node to contain a pointer to struct device ambedded in the struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to that struct acpi_device. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

Quoted 8 lines

The rationale for using a struct device pointer instead of a struct acpi_device one as the member of struct acpi_dev_node is that it allows device.h to avoid including linux/acpi.h which would introduce quite a bit of compilation overhead for stuff that doesn't care about ACPI.
In turn, moving the macros to linux/acpi.h forces
the stuff that does care about ACPI to include that file as appropriate anyway.

Quoted 5 lines

How about declaring "struct acpi_device" in the device.h? This can help to use struct acpi_device without including linux/acpi.h.

struct iommu_ops and struct iommu_group have been used by the same way in the device.h.

Yes, they are. Well, that appears to work too.

Updated patch is appended. It also contains some fixes for problems reported by the auto build system and it's been tested on x86-64 now, so it should be reasonably close to final.

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>
Subject: ACPI / driver core: Store an ACPI device pointer in struct acpi_dev_node

Modify struct acpi_dev_node to contain a pointer to struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to it. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

The main motivation for doing this is that there are things represented by struct acpi_device objects that don't have valid ACPI handles (so called fixed ACPI hardware features, such as power and sleep buttons) and we would like to create platform device objects for them and "glue" them to their ACPI companions in the usual way (which currently is impossible due to the lack of valid ACPI handles). However, there are more reasons why it may be useful.

First, struct acpi_device pointers allow of much better type checking than void pointers which are ACPI handles, so it should be more difficult to write buggy code using modified struct acpi_dev_node and the new macros. Second, the change should help to reduce (over time) the number of places in which the result of ACPI_HANDLE() is passed to acpi_bus_get_device() in order to obtain a pointer to the struct acpi_device associated with the given "physical" device, because now that pointer is returned by ACPI_COMPANION() directly. Finally, the change should make it easier to write generic code that will build both for CONFIG_ACPI set and unset without adding explicit compiler directives to it.

Quoted 3 lines
Quoted 1 line

Thanks!

--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Tested-by Reviewed-by
#6 lore raw
In reply to Rafael J. Wysocki
Quoted 36 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>
Subject: ACPI / driver core: Store an ACPI device pointer in struct acpi_dev_node

Modify struct acpi_dev_node to contain a pointer to struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to it. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

The main motivation for doing this is that there are things represented by struct acpi_device objects that don't have valid ACPI handles (so called fixed ACPI hardware features, such as power and sleep buttons) and we would like to create platform device objects for them and "glue" them to their ACPI companions in the usual way (which currently is impossible due to the lack of valid ACPI handles). However, there are more reasons why it may be useful.

First, struct acpi_device pointers allow of much better type checking than void pointers which are ACPI handles, so it should be more difficult to write buggy code using modified struct acpi_dev_node and the new macros. Second, the change should help to reduce (over time) the number of places in which the result of ACPI_HANDLE() is passed to acpi_bus_get_device() in order to obtain a pointer to the struct acpi_device associated with the given "physical" device, because now that pointer is returned by ACPI_COMPANION() directly. Finally, the change should make it easier to write generic code that will build both for CONFIG_ACPI set and unset without adding explicit compiler directives to it.

Quoted 1 line

I tested this on Haswell as well and it works fine with ACPI enumerated platform, I2C and SPI devices.

#7 lore raw
Quoted 37 lines · Mika Westerberg
Quoted 36 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>
Subject: ACPI / driver core: Store an ACPI device pointer in struct acpi_dev_node

Modify struct acpi_dev_node to contain a pointer to struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to it. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

The main motivation for doing this is that there are things represented by struct acpi_device objects that don't have valid ACPI handles (so called fixed ACPI hardware features, such as power and sleep buttons) and we would like to create platform device objects for them and "glue" them to their ACPI companions in the usual way (which currently is impossible due to the lack of valid ACPI handles). However, there are more reasons why it may be useful.

First, struct acpi_device pointers allow of much better type checking than void pointers which are ACPI handles, so it should be more difficult to write buggy code using modified struct acpi_dev_node and the new macros. Second, the change should help to reduce (over time) the number of places in which the result of ACPI_HANDLE() is passed to acpi_bus_get_device() in order to obtain a pointer to the struct acpi_device associated with the given "physical" device, because now that pointer is returned by ACPI_COMPANION() directly. Finally, the change should make it easier to write generic code that will build both for CONFIG_ACPI set and unset without adding explicit compiler directives to it.

Quoted 7 lines
Quoted 1 line

I tested this on Haswell as well and it works fine with ACPI enumerated platform, I2C and SPI devices.

Thanks!

Signature · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
Reviewed-by
#8 lore raw
In reply to Rafael J. Wysocki
Quoted 79 lines · Rafael J. Wysocki
Quoted 25 lines · Lan Tianyu
Quoted 15 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Modify struct acpi_dev_node to contain a pointer to struct device ambedded in the struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to that struct acpi_device. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

Quoted 8 lines

The rationale for using a struct device pointer instead of a struct acpi_device one as the member of struct acpi_dev_node is that it allows device.h to avoid including linux/acpi.h which would introduce quite a bit of compilation overhead for stuff that doesn't care about ACPI.
In turn, moving the macros to linux/acpi.h forces
the stuff that does care about ACPI to include that file as appropriate anyway.

Quoted 5 lines

How about declaring "struct acpi_device" in the device.h? This can help to use struct acpi_device without including linux/acpi.h.

struct iommu_ops and struct iommu_group have been used by the same way in the device.h.

Yes, they are. Well, that appears to work too.

Updated patch is appended. It also contains some fixes for problems reported by the auto build system and it's been tested on x86-64 now, so it should be reasonably close to final.

Thanks,
Rafael

---
From: Rafael J. Wysocki <[email protected]>
Subject: ACPI / driver core: Store an ACPI device pointer in struct acpi_dev_node

Modify struct acpi_dev_node to contain a pointer to struct acpi_device associated with the given device object (that is, its ACPI companion device) instead of an ACPI handle corresponding to it. Introduce two new macros for manipulating that pointer in a CONFIG_ACPI-safe way, ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the ACPI_HANDLE() macro to take the above changes into account. Drop the ACPI_HANDLE_SET() macro entirely and rework its users to use ACPI_COMPANION_SET() instead. For some of them who used to pass the result of acpi_get_child() directly to ACPI_HANDLE_SET() introduce a helper routine acpi_preset_companion() doing an equivalent thing.

The main motivation for doing this is that there are things represented by struct acpi_device objects that don't have valid ACPI handles (so called fixed ACPI hardware features, such as power and sleep buttons) and we would like to create platform device objects for them and "glue" them to their ACPI companions in the usual way (which currently is impossible due to the lack of valid ACPI handles). However, there are more reasons why it may be useful.

First, struct acpi_device pointers allow of much better type checking than void pointers which are ACPI handles, so it should be more difficult to write buggy code using modified struct acpi_dev_node and the new macros. Second, the change should help to reduce (over time) the number of places in which the result of ACPI_HANDLE() is passed to acpi_bus_get_device() in order to obtain a pointer to the struct acpi_device associated with the given "physical" device, because now that pointer is returned by ACPI_COMPANION() directly. Finally, the change should make it easier to write generic code that will build both for CONFIG_ACPI set and unset without adding explicit compiler directives to it.

Quoted 1 line

Thanks,
Aaron

#9 lore raw

cover ACPI: Additional changes on top of "ACPI / driver core: Store a device pointer in struct acpi_dev_node"

In reply to Rafael J. Wysocki

Hi Everybody,

The following two patches make changes that in my opinion are worth making on top of https://patchwork.kernel.org/patch/3167391/ . The first one simply replaces DEVICE_ACPI_HANDLE with ACPI_HANDLE everywhere (as the former is now a simple alias of the latter) and the second one adds ACPI device reference counting to acpi_bind_one()/acpi_unbind_one() to help catch device removal ordering issues that would cause the kernel to crash.

Thanks,
Rafael

#10 lore raw

PATCH 1/2 ACPI: Eliminate the DEVICE_ACPI_HANDLE() macro

From: Rafael J. Wysocki <[email protected]>

Since DEVICE_ACPI_HANDLE() is now literally identical to ACPI_HANDLE(), replace it with the latter everywhere and drop its definition from include/acpi.h.

---
 drivers/acpi/device_pm.c                       |    8 ++++----
 drivers/gpu/drm/i915/intel_acpi.c              |    2 +-
 drivers/gpu/drm/i915/intel_opregion.c          |    2 +-
 drivers/gpu/drm/nouveau/core/subdev/mxm/base.c |    2 +-
 drivers/gpu/drm/nouveau/nouveau_acpi.c         |    6 +++---
 drivers/gpu/drm/radeon/radeon_acpi.c           |    8 ++++----
 drivers/gpu/drm/radeon/radeon_atpx_handler.c   |    4 ++--
 drivers/gpu/drm/radeon/radeon_bios.c           |    2 +-
 drivers/ide/ide-acpi.c                         |    2 +-
 drivers/pci/hotplug/acpi_pcihp.c               |    2 +-
 drivers/pci/hotplug/pciehp_acpi.c              |    4 ++--
 drivers/pci/ioapic.c                           |    2 +-
 drivers/pci/pci-acpi.c                         |    6 +++---
 drivers/pci/pci-label.c                        |    6 +++---
 drivers/platform/x86/apple-gmux.c              |    2 +-
 drivers/pnp/pnpacpi/core.c                     |   10 +++++-----
 drivers/usb/core/hub.c                         |    2 +-
 drivers/usb/core/usb-acpi.c                    |    4 ++--
 drivers/xen/pci.c                              |    6 +++---
 include/linux/acpi.h                           |    2 --
 include/linux/pci-acpi.h                       |    4 ++--
 21 files changed, 42 insertions(+), 44 deletions(-)
drivers/acpi/device_pm.c +4 -4
Index: linux-pm/drivers/acpi/device_pm.c
===================================================================
@@ -544,7 +544,7 @@ static int acpi_dev_pm_get_state(struct
  */
 int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
 {
-	acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
+	acpi_handle handle = ACPI_HANDLE(dev);
 	struct acpi_device *adev;
 	int ret, d_min, d_max;
 
@@ -652,7 +652,7 @@ int acpi_pm_device_run_wake(struct devic
 	if (!device_run_wake(phys_dev))
 		return -EINVAL;
 
-	handle = DEVICE_ACPI_HANDLE(phys_dev);
+	handle = ACPI_HANDLE(phys_dev);
 	if (!handle || acpi_bus_get_device(handle, &adev)) {
 		dev_dbg(phys_dev, "ACPI handle without context in %s!\n",
 			__func__);
@@ -696,7 +696,7 @@ int acpi_pm_device_sleep_wake(struct dev
 	if (!device_can_wakeup(dev))
 		return -EINVAL;
 
-	handle = DEVICE_ACPI_HANDLE(dev);
+	handle = ACPI_HANDLE(dev);
 	if (!handle || acpi_bus_get_device(handle, &adev)) {
 		dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
 		return -ENODEV;
@@ -718,7 +718,7 @@ int acpi_pm_device_sleep_wake(struct dev
  */
 struct acpi_device *acpi_dev_pm_get_node(struct device *dev)
 {
-	acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
+	acpi_handle handle = ACPI_HANDLE(dev);
 	struct acpi_device *adev;
 
 	return handle && !acpi_bus_get_device(handle, &adev) ? adev : NULL;
drivers/gpu/drm/i915/intel_acpi.c +1 -1
Index: linux-pm/drivers/gpu/drm/i915/intel_acpi.c
===================================================================
@@ -196,7 +196,7 @@ static bool intel_dsm_pci_probe(struct p
 	acpi_handle dhandle;
 	int ret;
 
-	dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
+	dhandle = ACPI_HANDLE(&pdev->dev);
 	if (!dhandle)
 		return false;
 
drivers/gpu/drm/i915/intel_opregion.c +1 -1
Index: linux-pm/drivers/gpu/drm/i915/intel_opregion.c
===================================================================
@@ -289,7 +289,7 @@ static void intel_didl_outputs(struct dr
 	u32 temp;
 	int i = 0;
 
-	handle = DEVICE_ACPI_HANDLE(&dev->pdev->dev);
+	handle = ACPI_HANDLE(&dev->pdev->dev);
 	if (!handle || acpi_bus_get_device(handle, &acpi_dev))
 		return;
 
drivers/gpu/drm/nouveau/core/subdev/mxm/base.c +1 -1
Index: linux-pm/drivers/gpu/drm/nouveau/core/subdev/mxm/base.c
===================================================================
@@ -116,7 +116,7 @@ mxm_shadow_dsm(struct nouveau_mxm *mxm,
 	acpi_handle handle;
 	int ret;
 
-	handle = DEVICE_ACPI_HANDLE(&device->pdev->dev);
+	handle = ACPI_HANDLE(&device->pdev->dev);
 	if (!handle)
 		return false;
 
drivers/gpu/drm/nouveau/nouveau_acpi.c +3 -3
Index: linux-pm/drivers/gpu/drm/nouveau/nouveau_acpi.c
===================================================================
@@ -256,7 +256,7 @@ static int nouveau_dsm_pci_probe(struct
 	acpi_handle dhandle;
 	int retval = 0;
 
-	dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
+	dhandle = ACPI_HANDLE(&pdev->dev);
 	if (!dhandle)
 		return false;
 
@@ -404,7 +404,7 @@ bool nouveau_acpi_rom_supported(struct p
 	if (!nouveau_dsm_priv.dsm_detected && !nouveau_dsm_priv.optimus_detected)
 		return false;
 
-	dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
+	dhandle = ACPI_HANDLE(&pdev->dev);
 	if (!dhandle)
 		return false;
 
@@ -438,7 +438,7 @@ nouveau_acpi_edid(struct drm_device *dev
 		return NULL;
 	}
 
-	handle = DEVICE_ACPI_HANDLE(&dev->pdev->dev);
+	handle = ACPI_HANDLE(&dev->pdev->dev);
 	if (!handle)
 		return NULL;
 
drivers/gpu/drm/radeon/radeon_acpi.c +4 -4
Index: linux-pm/drivers/gpu/drm/radeon/radeon_acpi.c
===================================================================
@@ -369,7 +369,7 @@ int radeon_atif_handler(struct radeon_de
 		return NOTIFY_DONE;
 
 	/* Check pending SBIOS requests */
-	handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
+	handle = ACPI_HANDLE(&rdev->pdev->dev);
 	count = radeon_atif_get_sbios_requests(handle, &req);
 
 	if (count <= 0)
@@ -556,7 +556,7 @@ int radeon_acpi_pcie_notify_device_ready
 	struct radeon_atcs *atcs = &rdev->atcs;
 
 	/* Get the device handle */
-	handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
+	handle = ACPI_HANDLE(&rdev->pdev->dev);
 	if (!handle)
 		return -EINVAL;
 
@@ -596,7 +596,7 @@ int radeon_acpi_pcie_performance_request
 	u32 retry = 3;
 
 	/* Get the device handle */
-	handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
+	handle = ACPI_HANDLE(&rdev->pdev->dev);
 	if (!handle)
 		return -EINVAL;
 
@@ -699,7 +699,7 @@ int radeon_acpi_init(struct radeon_devic
 	int ret;
 
 	/* Get the device handle */
-	handle = DEVICE_ACPI_HANDLE(&rdev->pdev->dev);
+	handle = ACPI_HANDLE(&rdev->pdev->dev);
 
 	/* No need to proceed if we're sure that ATIF is not supported */
 	if (!ASIC_IS_AVIVO(rdev) || !rdev->bios || !handle)
drivers/gpu/drm/radeon/radeon_atpx_handler.c +2 -2
Index: linux-pm/drivers/gpu/drm/radeon/radeon_atpx_handler.c
===================================================================
@@ -442,7 +442,7 @@ static bool radeon_atpx_pci_probe_handle
 	acpi_handle dhandle, atpx_handle;
 	acpi_status status;
 
-	dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
+	dhandle = ACPI_HANDLE(&pdev->dev);
 	if (!dhandle)
 		return false;
 
@@ -488,7 +488,7 @@ static int radeon_atpx_init(void)
  */
 static int radeon_atpx_get_client_id(struct pci_dev *pdev)
 {
-	if (radeon_atpx_priv.dhandle == DEVICE_ACPI_HANDLE(&pdev->dev))
+	if (radeon_atpx_priv.dhandle == ACPI_HANDLE(&pdev->dev))
 		return VGA_SWITCHEROO_IGD;
 	else
 		return VGA_SWITCHEROO_DIS;
drivers/gpu/drm/radeon/radeon_bios.c +1 -1
Index: linux-pm/drivers/gpu/drm/radeon/radeon_bios.c
===================================================================
@@ -185,7 +185,7 @@ static bool radeon_atrm_get_bios(struct
 		return false;
 
 	while ((pdev = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, pdev)) != NULL) {
-		dhandle = DEVICE_ACPI_HANDLE(&pdev->dev);
+		dhandle = ACPI_HANDLE(&pdev->dev);
 		if (!dhandle)
 			continue;
 
drivers/ide/ide-acpi.c +1 -1
Index: linux-pm/drivers/ide/ide-acpi.c
===================================================================
@@ -127,7 +127,7 @@ static int ide_get_dev_handle(struct dev
 
 	DEBPRINT("ENTER: pci %02x:%02x.%01x\n", bus, devnum, func);
 
-	dev_handle = DEVICE_ACPI_HANDLE(dev);
+	dev_handle = ACPI_HANDLE(dev);
 	if (!dev_handle) {
 		DEBPRINT("no acpi handle for device\n");
 		goto err;
drivers/pci/hotplug/acpi_pcihp.c +1 -1
Index: linux-pm/drivers/pci/hotplug/acpi_pcihp.c
===================================================================
@@ -367,7 +367,7 @@ int acpi_get_hp_hw_control_from_firmware
 		string = (struct acpi_buffer){ ACPI_ALLOCATE_BUFFER, NULL };
 	}
 
-	handle = DEVICE_ACPI_HANDLE(&pdev->dev);
+	handle = ACPI_HANDLE(&pdev->dev);
 	if (!handle) {
 		/*
 		 * This hotplug controller was not listed in the ACPI name
drivers/pci/hotplug/pciehp_acpi.c +2 -2
Index: linux-pm/drivers/pci/hotplug/pciehp_acpi.c
===================================================================
@@ -54,7 +54,7 @@ int pciehp_acpi_slot_detection_check(str
 {
 	if (slot_detection_mode != PCIEHP_DETECT_ACPI)
 		return 0;
-	if (acpi_pci_detect_ejectable(DEVICE_ACPI_HANDLE(&dev->dev)))
+	if (acpi_pci_detect_ejectable(ACPI_HANDLE(&dev->dev)))
 		return 0;
 	return -ENODEV;
 }
@@ -96,7 +96,7 @@ static int __init dummy_probe(struct pci
 			dup_slot_id++;
 	}
 	list_add_tail(&slot->list, &dummy_slots);
-	handle = DEVICE_ACPI_HANDLE(&pdev->dev);
+	handle = ACPI_HANDLE(&pdev->dev);
 	if (!acpi_slot_detected && acpi_pci_detect_ejectable(handle))
 		acpi_slot_detected = 1;
 	return -ENODEV;         /* dummy driver always returns error */
drivers/pci/ioapic.c +1 -1
Index: linux-pm/drivers/pci/ioapic.c
===================================================================
@@ -37,7 +37,7 @@ static int ioapic_probe(struct pci_dev *
 	char *type;
 	struct resource *res;
 
-	handle = DEVICE_ACPI_HANDLE(&dev->dev);
+	handle = ACPI_HANDLE(&dev->dev);
 	if (!handle)
 		return -EINVAL;
 
drivers/pci/pci-acpi.c +3 -3
Index: linux-pm/drivers/pci/pci-acpi.c
===================================================================
@@ -173,14 +173,14 @@ static pci_power_t acpi_pci_choose_state
 
 static bool acpi_pci_power_manageable(struct pci_dev *dev)
 {
-	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+	acpi_handle handle = ACPI_HANDLE(&dev->dev);
 
 	return handle ? acpi_bus_power_manageable(handle) : false;
 }
 
 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
 {
-	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+	acpi_handle handle = ACPI_HANDLE(&dev->dev);
 	static const u8 state_conv[] = {
 		[PCI_D0] = ACPI_STATE_D0,
 		[PCI_D1] = ACPI_STATE_D1,
@@ -217,7 +217,7 @@ static int acpi_pci_set_power_state(stru
 
 static bool acpi_pci_can_wakeup(struct pci_dev *dev)
 {
-	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+	acpi_handle handle = ACPI_HANDLE(&dev->dev);
 
 	return handle ? acpi_bus_can_wakeup(handle) : false;
 }
drivers/pci/pci-label.c +3 -3
Index: linux-pm/drivers/pci/pci-label.c
===================================================================
@@ -263,7 +263,7 @@ device_has_dsm(struct device *dev)
 	acpi_handle handle;
 	struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
 
-	handle = DEVICE_ACPI_HANDLE(dev);
+	handle = ACPI_HANDLE(dev);
 
 	if (!handle)
 		return FALSE;
@@ -295,7 +295,7 @@ acpilabel_show(struct device *dev, struc
 	acpi_handle handle;
 	int length;
 
-	handle = DEVICE_ACPI_HANDLE(dev);
+	handle = ACPI_HANDLE(dev);
 
 	if (!handle)
 		return -1;
@@ -316,7 +316,7 @@ acpiindex_show(struct device *dev, struc
 	acpi_handle handle;
 	int length;
 
-	handle = DEVICE_ACPI_HANDLE(dev);
+	handle = ACPI_HANDLE(dev);
 
 	if (!handle)
 		return -1;
drivers/platform/x86/apple-gmux.c +1 -1
Index: linux-pm/drivers/platform/x86/apple-gmux.c
===================================================================
@@ -519,7 +519,7 @@ static int gmux_probe(struct pnp_dev *pn
 
 	gmux_data->power_state = VGA_SWITCHEROO_ON;
 
-	gmux_data->dhandle = DEVICE_ACPI_HANDLE(&pnp->dev);
+	gmux_data->dhandle = ACPI_HANDLE(&pnp->dev);
 	if (!gmux_data->dhandle) {
 		pr_err("Cannot find acpi handle for pnp device %s\n",
 		       dev_name(&pnp->dev));
drivers/pnp/pnpacpi/core.c +5 -5
Index: linux-pm/drivers/pnp/pnpacpi/core.c
===================================================================
@@ -89,7 +89,7 @@ static int pnpacpi_set_resources(struct
 
 	pnp_dbg(&dev->dev, "set resources\n");
 
-	handle = DEVICE_ACPI_HANDLE(&dev->dev);
+	handle = ACPI_HANDLE(&dev->dev);
 	if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
 		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
 		return -ENODEV;
@@ -122,7 +122,7 @@ static int pnpacpi_disable_resources(str
 
 	dev_dbg(&dev->dev, "disable resources\n");
 
-	handle = DEVICE_ACPI_HANDLE(&dev->dev);
+	handle = ACPI_HANDLE(&dev->dev);
 	if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
 		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
 		return 0;
@@ -144,7 +144,7 @@ static bool pnpacpi_can_wakeup(struct pn
 	struct acpi_device *acpi_dev;
 	acpi_handle handle;
 
-	handle = DEVICE_ACPI_HANDLE(&dev->dev);
+	handle = ACPI_HANDLE(&dev->dev);
 	if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
 		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
 		return false;
@@ -159,7 +159,7 @@ static int pnpacpi_suspend(struct pnp_de
 	acpi_handle handle;
 	int error = 0;
 
-	handle = DEVICE_ACPI_HANDLE(&dev->dev);
+	handle = ACPI_HANDLE(&dev->dev);
 	if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
 		dev_dbg(&dev->dev, "ACPI device not found in %s!\n", __func__);
 		return 0;
@@ -194,7 +194,7 @@ static int pnpacpi_suspend(struct pnp_de
 static int pnpacpi_resume(struct pnp_dev *dev)
 {
 	struct acpi_device *acpi_dev;
-	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+	acpi_handle handle = ACPI_HANDLE(&dev->dev);
 	int error = 0;
 
 	if (!handle || acpi_bus_get_device(handle, &acpi_dev)) {
drivers/usb/core/hub.c +1 -1
Index: linux-pm/drivers/usb/core/hub.c
===================================================================
@@ -5461,6 +5461,6 @@ acpi_handle usb_get_hub_port_acpi_handle
 	if (!hub)
 		return NULL;
 
-	return DEVICE_ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
+	return ACPI_HANDLE(&hub->ports[port1 - 1]->dev);
 }
 #endif
drivers/usb/core/usb-acpi.c +2 -2
Index: linux-pm/drivers/usb/core/usb-acpi.c
===================================================================
@@ -173,7 +173,7 @@ static int usb_acpi_find_device(struct d
 		}
 
 		/* root hub's parent is the usb hcd. */
-		parent_handle = DEVICE_ACPI_HANDLE(dev->parent);
+		parent_handle = ACPI_HANDLE(dev->parent);
 		*handle = acpi_get_child(parent_handle, udev->portnum);
 		if (!*handle)
 			return -ENODEV;
@@ -194,7 +194,7 @@ static int usb_acpi_find_device(struct d
 
 			raw_port_num = usb_hcd_find_raw_port_number(hcd,
 				port_num);
-			*handle = acpi_get_child(DEVICE_ACPI_HANDLE(&udev->dev),
+			*handle = acpi_get_child(ACPI_HANDLE(&udev->dev),
 				raw_port_num);
 			if (!*handle)
 				return -ENODEV;
drivers/xen/pci.c +3 -3
Index: linux-pm/drivers/xen/pci.c
===================================================================
@@ -58,12 +58,12 @@ static int xen_add_device(struct device
 			add.flags = XEN_PCI_DEV_EXTFN;
 
 #ifdef CONFIG_ACPI
-		handle = DEVICE_ACPI_HANDLE(&pci_dev->dev);
+		handle = ACPI_HANDLE(&pci_dev->dev);
 		if (!handle && pci_dev->bus->bridge)
-			handle = DEVICE_ACPI_HANDLE(pci_dev->bus->bridge);
+			handle = ACPI_HANDLE(pci_dev->bus->bridge);
 #ifdef CONFIG_PCI_IOV
 		if (!handle && pci_dev->is_virtfn)
-			handle = DEVICE_ACPI_HANDLE(physfn->bus->bridge);
+			handle = ACPI_HANDLE(physfn->bus->bridge);
 #endif
 		if (handle) {
 			acpi_status status;
include/linux/acpi.h +0 -2
Index: linux-pm/include/linux/acpi.h
===================================================================
@@ -488,8 +488,6 @@ static inline bool acpi_driver_match_dev
 
 #endif	/* !CONFIG_ACPI */
 
-#define DEVICE_ACPI_HANDLE(dev)	ACPI_HANDLE(dev)
-
 #ifdef CONFIG_ACPI
 void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
 			       u32 pm1a_ctrl,  u32 pm1b_ctrl));
include/linux/pci-acpi.h +2 -2
Index: linux-pm/include/linux/pci-acpi.h
===================================================================
@@ -27,7 +27,7 @@ static inline acpi_handle acpi_find_root
 	while (!pci_is_root_bus(pbus))
 		pbus = pbus->parent;
 
-	return DEVICE_ACPI_HANDLE(pbus->bridge);
+	return ACPI_HANDLE(pbus->bridge);
 }
 
 static inline acpi_handle acpi_pci_get_bridge_handle(struct pci_bus *pbus)
@@ -39,7 +39,7 @@ static inline acpi_handle acpi_pci_get_b
 	else
 		dev = &pbus->self->dev;
 
-	return DEVICE_ACPI_HANDLE(dev);
+	return ACPI_HANDLE(dev);
 }
 
 void acpi_pci_add_bus(struct pci_bus *bus);
#12 lore raw

PATCH 2/2 ACPI / bind: Use (put|get)_device() on ACPI device objects too

In reply to Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

When associating a "physical" device with an ACPI device object acpi_bind_one() only uses get_device() to increment the reference counter of the former, but there is no reason not to do that with the latter too. Among other things, that may help to avoid use-after-free when an ACPI device object is freed without calling acpi_unbind_one() for all "physical" devices associated with it (that only can happen in buggy code, but then it's better if the kernel doesn't crash as a result of a bug).

For this reason, modify acpi_bind_one() to apply get_device() to the ACPI device object too and update acpi_unbind_one() to drop that reference using put_device() as appropriate.

---
 drivers/acpi/glue.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/acpi/glue.c +5 -1
Index: linux-pm/drivers/acpi/glue.c
===================================================================
@@ -217,6 +217,7 @@ int acpi_bind_one(struct device *dev, ac
 	if (!acpi_dev)
 		return -EINVAL;
 
+	get_device(&acpi_dev->dev);
 	get_device(dev);
 	physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
 	if (!physical_node) {
@@ -243,6 +244,7 @@ int acpi_bind_one(struct device *dev, ac
 				goto err;
 
 			put_device(dev);
+			put_device(&acpi_dev->dev);
 			return 0;
 		}
 		if (pn->node_id == node_id) {
@@ -282,6 +284,7 @@ int acpi_bind_one(struct device *dev, ac
  err:
 	ACPI_COMPANION_SET(dev, NULL);
 	put_device(dev);
+	put_device(&acpi_dev->dev);
 	return retval;
 }
 EXPORT_SYMBOL_GPL(acpi_bind_one);
@@ -307,8 +310,9 @@ int acpi_unbind_one(struct device *dev)
 			sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
 			sysfs_remove_link(&dev->kobj, "firmware_node");
 			ACPI_COMPANION_SET(dev, NULL);
-			/* acpi_bind_one() increase refcnt by one. */
+			/* Drop references taken by acpi_bind_one(). */
 			put_device(dev);
+			put_device(&acpi_dev->dev);
 			kfree(entry);
 			break;
 		}
maintainer Acked-by on v1 2/2
#13 lore raw

Re: [PATCH 2/2] ACPI / bind: Use (put|get)_device() on ACPI device objects too

Quoted 11 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

When associating a "physical" device with an ACPI device object acpi_bind_one() only uses get_device() to increment the reference counter of the former, but there is no reason not to do that with the latter too. Among other things, that may help to avoid use-after-free when an ACPI device object is freed without calling acpi_unbind_one() for all "physical" devices associated with it (that only can happen in buggy code, but then it's better if the kernel doesn't crash as a result of a bug).

Quoted 5 lines

For this reason, modify acpi_bind_one() to apply get_device() to the ACPI device object too and update acpi_unbind_one() to drop that reference using put_device() as appropriate.

Reviewed-by on v1 2/2
#14 lore raw

Re: [PATCH 2/2] ACPI / bind: Use (put|get)_device() on ACPI device objects too

In reply to Rafael J. Wysocki
Quoted 50 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

When associating a "physical" device with an ACPI device object acpi_bind_one() only uses get_device() to increment the reference counter of the former, but there is no reason not to do that with the latter too. Among other things, that may help to avoid use-after-free when an ACPI device object is freed without calling acpi_unbind_one() for all "physical" devices associated with it (that only can happen in buggy code, but then it's better if the kernel doesn't crash as a result of a bug).

For this reason, modify acpi_bind_one() to apply get_device() to the ACPI device object too and update acpi_unbind_one() to drop that reference using put_device() as appropriate.

---
 drivers/acpi/glue.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Index: linux-pm/drivers/acpi/glue.c
===================================================================

drivers/acpi/glue.c +3 -0
@@ -217,6 +217,7 @@ int acpi_bind_one(struct device *dev, ac
 	if (!acpi_dev)
 		return -EINVAL;
 
+	get_device(&acpi_dev->dev);
 	get_device(dev);
 	physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
 	if (!physical_node) {
@@ -243,6 +244,7 @@ int acpi_bind_one(struct device *dev, ac
 				goto err;
 
 			put_device(dev);
+			put_device(&acpi_dev->dev);
 			return 0;
 		}
 		if (pn->node_id == node_id) {
@@ -282,6 +284,7 @@ int acpi_bind_one(struct device *dev, ac
  err:
 	ACPI_COMPANION_SET(dev, NULL);
 	put_device(dev);
+	put_device(&acpi_dev->dev);
 	return retval;
 }
 EXPORT_SYMBOL_GPL(acpi_bind_one);

drivers/acpi/glue.c int acpi_unbind_one(struct device *dev) · line 318

Quoted 10 lines
drivers/acpi/glue.c +2 -1
@@ -307,8 +310,9 @@ int acpi_unbind_one(struct device *dev)
 			sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
 			sysfs_remove_link(&dev->kobj, "firmware_node");
 			ACPI_COMPANION_SET(dev, NULL);
-			/* acpi_bind_one() increase refcnt by one. */
+			/* Drop references taken by acpi_bind_one(). */
 			put_device(dev);
+			put_device(&acpi_dev->dev);
 			kfree(entry);
 			break;
 		}
Signature · 2 lines
Best regards
Tianyu Lan