Skip to contentSkip to conversation

driver core: Clean up dev_get/set_drvdata

PATCH In review Jean Delvare 2 participants 9 messages Apr 14, 2014 – May 27, 2014

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover driver core: Clean up dev_get/set_drvdata

This patch set is a proposal to revert most of commit b4028437. This would solve a memory leak and also allow to simplify and ultimately inline again functions dev_get_drvdata and dev_set_drvdata for smaller footprint and improved performance.

[PATCH 1/5] driver core: Move driver_data back to struct device [PATCH 2/5] driver core: dev_set_drvdata can no longer fail [PATCH 3/5] driver core: dev_set_drvdata returns void [PATCH 4/5] driver core: dev_get_drvdata: Don't check for NULL dev [PATCH 5/5] driver core: Inline dev_set/get_drvdata

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#1 lore raw

PATCH 1/5 driver core: Move driver_data back to struct device

Having to allocate memory as part of dev_set_drvdata() is a problem because that memory may never get freed if the device itself is not created. So move driver_data back to struct device.

This is a partial revert of commit b4028437.

Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/base/base.h    |    3 ---
 drivers/base/dd.c      |   13 +++----------
 include/linux/device.h |    3 +++
 3 files changed, 6 insertions(+), 13 deletions(-)
drivers/base/base.h +0 -3
@@ -63,8 +63,6 @@ struct driver_private {
  *	binding of drivers which were unable to get all the resources needed by
  *	the device; typically because it depends on another driver getting
  *	probed first.
- * @driver_data - private pointer for driver specific info.  Will turn into a
- * list soon.
  * @device - pointer back to the struct class that this structure is
  * associated with.
  *
@@ -76,7 +74,6 @@ struct device_private {
 	struct klist_node knode_driver;
 	struct klist_node knode_bus;
 	struct list_head deferred_probe;
-	void *driver_data;
 	struct device *device;
 };
 #define to_device_private_parent(obj)	\
drivers/base/dd.c +3 -10
@@ -577,22 +577,15 @@ void driver_detach(struct device_driver
  */
 void *dev_get_drvdata(const struct device *dev)
 {
-	if (dev && dev->p)
-		return dev->p->driver_data;
+	if (dev)
+		return dev->driver_data;
 	return NULL;
 }
 EXPORT_SYMBOL(dev_get_drvdata);
 
 int dev_set_drvdata(struct device *dev, void *data)
 {
-	int error;
-
-	if (!dev->p) {
-		error = device_private_init(dev);
-		if (error)
-			return error;
-	}
-	dev->p->driver_data = data;
+	dev->driver_data = data;
 	return 0;
 }
 EXPORT_SYMBOL(dev_set_drvdata);
include/linux/device.h +3 -0
@@ -679,6 +679,7 @@ struct acpi_dev_node {
  * 		variants, which GPIO pins act in what additional roles, and so
  * 		on.  This shrinks the "Board Support Packages" (BSPs) and
  * 		minimizes board-specific #ifdefs in drivers.
+ * @driver_data: Private pointer for driver specific info.
  * @power:	For device power management.
  * 		See Documentation/power/devices.txt for details.
  * @pm_domain:	Provide callbacks that are executed during system suspend,
@@ -740,6 +741,8 @@ struct device {
 					   device */
 	void		*platform_data;	/* Platform specific data, device
 					   core doesn't touch it */
+	void		*driver_data;	/* Driver data, set and get with
+					   dev_set/get_drvdata */
 	struct dev_pm_info	power;
 	struct dev_pm_domain	*pm_domain;
 
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#2 lore raw

PATCH 2/5 driver core: dev_set_drvdata can no longer fail

In reply to Jean Delvare

So there is no point in checking its return value, which will soon disappear.

Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/iommu/exynos-iommu.c |    7 +------
 drivers/vfio/vfio.c          |    8 +-------
 2 files changed, 2 insertions(+), 13 deletions(-)
drivers/iommu/exynos-iommu.c +1 -6
@@ -542,12 +542,7 @@ static int exynos_sysmmu_probe(struct pl
 		goto err_alloc;
 	}
 
-	ret = dev_set_drvdata(dev, data);
-	if (ret) {
-		dev_dbg(dev, "Unabled to initialize driver data\n");
-		goto err_init;
-	}
-
+	dev_set_drvdata(dev, data);
 	data->nsfrs = pdev->num_resources / 2;
 	data->sfrbases = kmalloc(sizeof(*data->sfrbases) * data->nsfrs,
 								GFP_KERNEL);
drivers/vfio/vfio.c +1 -7
@@ -349,7 +349,6 @@ struct vfio_device *vfio_group_create_de
 					     void *device_data)
 {
 	struct vfio_device *device;
-	int ret;
 
 	device = kzalloc(sizeof(*device), GFP_KERNEL);
 	if (!device)
@@ -360,12 +359,7 @@ struct vfio_device *vfio_group_create_de
 	device->group = group;
 	device->ops = ops;
 	device->device_data = device_data;
-
-	ret = dev_set_drvdata(dev, device);
-	if (ret) {
-		kfree(device);
-		return ERR_PTR(ret);
-	}
+	dev_set_drvdata(dev, device);
 
 	/* No need to get group_lock, caller has group reference */
 	vfio_group_get(group);
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#3 lore raw

PATCH 3/5 driver core: dev_set_drvdata returns void

In reply to Jean Delvare

dev_set_drvdata can no longer fail, so it could return void.

All callers have hopefully been updated to no longer check for the return value.

Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/base/dd.c      |    3 +--
 include/linux/device.h |    2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)
drivers/base/dd.c +1 -2
@@ -583,9 +583,8 @@ void *dev_get_drvdata(const struct devic
 }
 EXPORT_SYMBOL(dev_get_drvdata);
 
-int dev_set_drvdata(struct device *dev, void *data)
+void dev_set_drvdata(struct device *dev, void *data)
 {
 	dev->driver_data = data;
-	return 0;
 }
 EXPORT_SYMBOL(dev_set_drvdata);
include/linux/device.h +1 -1
@@ -917,7 +917,7 @@ extern const char *device_get_devnode(st
 				      umode_t *mode, kuid_t *uid, kgid_t *gid,
 				      const char **tmp);
 extern void *dev_get_drvdata(const struct device *dev);
-extern int dev_set_drvdata(struct device *dev, void *data);
+extern void dev_set_drvdata(struct device *dev, void *data);
 
 static inline bool device_supports_offline(struct device *dev)
 {
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#4 lore raw

PATCH 4/5 driver core: dev_get_drvdata: Don't check for NULL dev

In reply to Jean Delvare

There is no point in calling dev_get_drvdata without a valid device. So checking for dev == NULL is pointless. If such a check is ever needed - which I doubt - the driver should do it before calling dev_get_drvdata.

We were returning NULL if dev was NULL, which the caller certainly did not expect anyway, so that was only delaying the crash if the caller is not paying attention.

Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/base/dd.c |    4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/base/dd.c +1 -3
@@ -577,9 +577,7 @@ void driver_detach(struct device_driver
  */
 void *dev_get_drvdata(const struct device *dev)
 {
-	if (dev)
-		return dev->driver_data;
-	return NULL;
+	return dev->driver_data;
 }
 EXPORT_SYMBOL(dev_get_drvdata);
 
Signature · 2 lines
Jean Delvare
SUSE L3 Support
#5 lore raw

PATCH 5/5 driver core: Inline dev_set/get_drvdata

In reply to Jean Delvare

dev_set_drvdata and dev_get_drvdata are now simple enough again that we can inline them as they used to be before commit b40284378.

Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/base/dd.c      |   16 ----------------
 include/linux/device.h |   12 ++++++++++--
 2 files changed, 10 insertions(+), 18 deletions(-)
drivers/base/dd.c +0 -16
@@ -570,19 +570,3 @@ void driver_detach(struct device_driver
 		put_device(dev);
 	}
 }
-
-/*
- * These exports can't be _GPL due to .h files using this within them, and it
- * might break something that was previously working...
- */
-void *dev_get_drvdata(const struct device *dev)
-{
-	return dev->driver_data;
-}
-EXPORT_SYMBOL(dev_get_drvdata);
-
-void dev_set_drvdata(struct device *dev, void *data)
-{
-	dev->driver_data = data;
-}
-EXPORT_SYMBOL(dev_set_drvdata);
include/linux/device.h +10 -2
@@ -832,6 +832,16 @@ static inline void set_dev_node(struct d
 }
 #endif
 
+static inline void *dev_get_drvdata(const struct device *dev)
+{
+	return dev->driver_data;
+}
+
+static inline void dev_set_drvdata(struct device *dev, void *data)
+{
+	dev->driver_data = data;
+}
+
 static inline struct pm_subsys_data *dev_to_psd(struct device *dev)
 {
 	return dev ? dev->power.subsys_data : NULL;
@@ -916,8 +926,6 @@ extern int device_move(struct device *de
 extern const char *device_get_devnode(struct device *dev,
 				      umode_t *mode, kuid_t *uid, kgid_t *gid,
 				      const char **tmp);
-extern void *dev_get_drvdata(const struct device *dev);
-extern void dev_set_drvdata(struct device *dev, void *data);
 
 static inline bool device_supports_offline(struct device *dev)
 {
Signature · 2 lines
Jean Delvare
SUSE L3 Support
maintainer
#6 lore raw
In reply to Jean Delvare
Quoted 10 lines · Jean Delvare

This patch set is a proposal to revert most of commit b4028437. This would solve a memory leak and also allow to simplify and ultimately inline again functions dev_get_drvdata and dev_set_drvdata for smaller footprint and improved performance.

[PATCH 1/5] driver core: Move driver_data back to struct device [PATCH 2/5] driver core: dev_set_drvdata can no longer fail [PATCH 3/5] driver core: dev_set_drvdata returns void [PATCH 4/5] driver core: dev_get_drvdata: Don't check for NULL dev [PATCH 5/5] driver core: Inline dev_set/get_drvdata

Thanks, I'll queue this up later this week.

greg k-h

maintainer
#7 lore raw
In reply to Jean Delvare
Quoted 11 lines · Jean Delvare

This patch set is a proposal to revert most of commit b4028437. This would solve a memory leak and also allow to simplify and ultimately inline again functions dev_get_drvdata and dev_set_drvdata for smaller footprint and improved performance.

[PATCH 1/5] driver core: Move driver_data back to struct device [PATCH 2/5] driver core: dev_set_drvdata can no longer fail [PATCH 3/5] driver core: dev_set_drvdata returns void [PATCH 4/5] driver core: dev_get_drvdata: Don't check for NULL dev [PATCH 5/5] driver core: Inline dev_set/get_drvdata

Sorry for the long delay, these are finally all now applied to my tree.

greg k-h

#8 lore raw
Quoted 6 lines · Greg KH
Quoted 5 lines · Jean Delvare

This patch set is a proposal to revert most of commit b4028437. This would solve a memory leak and also allow to simplify and ultimately inline again functions dev_get_drvdata and dev_set_drvdata for smaller footprint and improved performance.

Quoted 8 lines
Quoted 6 lines

[PATCH 1/5] driver core: Move driver_data back to struct device [PATCH 2/5] driver core: dev_set_drvdata can no longer fail [PATCH 3/5] driver core: dev_set_drvdata returns void [PATCH 4/5] driver core: dev_get_drvdata: Don't check for NULL dev [PATCH 5/5] driver core: Inline dev_set/get_drvdata

Sorry for the long delay, these are finally all now applied to my tree.

Cool, thanks!

Signature · 2 lines
Jean Delvare
SUSE L3 Support