Skip to contentSkip to conversation

driver core: allow kobj_to_dev() to take a const pointer

PATCH In review Greg Kroah-Hartman 4 participants 7 messages Oct 16, 2022 – Oct 17, 2022 Driver core

lore
Share & tools
lore

Greg Kroah-Hartman

author maintainer
#0 lore raw

PATCH v2 1/1 driver core: allow kobj_to_dev() to take a const pointer

If a const * to a kobject is passed to kobj_to_dev(), we want to return back a const * to a device as the driver core shouldn't be modifying a constant structure. But when dealing with container_of() the pointer const attribute is cast away, so we need to manually handle this by determining the type of the pointer passed in to know the type of the pointer to pass out.

Luckily _Generic can do this type of magic, and as the kernel now supports C11 it is availble to us to handle this type of build-time type detection.

Cc Sakari Ailus <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>

---
v2 - use _Generic() to make this type safe as pointed out by Sakari

 include/linux/device.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
include/linux/device.h +17 -1
index 424b55df0272..023ea50b1916 100644
@@ -680,11 +680,27 @@ struct device_link {
 	bool supplier_preactivated; /* Owned by consumer probe. */
 };
 
-static inline struct device *kobj_to_dev(struct kobject *kobj)
+static inline struct device *__kobj_to_dev(struct kobject *kobj)
 {
 	return container_of(kobj, struct device, kobj);
 }
 
+static inline const struct device *__kobj_to_dev_const(const struct kobject *kobj)
+{
+	return container_of(kobj, const struct device, kobj);
+}
+
+/*
+ * container_of() will happily take a const * and spit back a non-const * as it
+ * is just doing pointer math.  But we want to be a bit more careful in the
+ * driver code, so manually force any const * of a kobject to also be a const *
+ * to a device.
+ */
+#define kobj_to_dev(kobj)					\
+	_Generic((kobj),					\
+		 const struct kobject *: __kobj_to_dev_const,	\
+		 struct kobject *: __kobj_to_dev)(kobj)
+
 /**
  * device_iommu_mapped - Returns true when the device DMA is translated
  *			 by an IOMMU
Signature · 1 line
2.38.0
#1 lore raw

Hi Greg,

Quoted 6 lines · Greg Kroah-Hartman

If a const * to a kobject is passed to kobj_to_dev(), we want to return back a const * to a device as the driver core shouldn't be modifying a constant structure. But when dealing with container_of() the pointer const attribute is cast away, so we need to manually handle this by determining the type of the pointer passed in to know the type of the pointer to pass out.

Alternatively container_of() could be fixed, but that will likely produce lots of warnings currently.

Quoted 30 lines

Luckily _Generic can do this type of magic, and as the kernel now supports C11 it is availble to us to handle this type of build-time type detection.

Cc Sakari Ailus <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>

---
v2 - use _Generic() to make this type safe as pointed out by Sakari

 include/linux/device.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
include/linux/device.h +2 -1
index 424b55df0272..023ea50b1916 100644
@@ -680,11 +680,27 @@ struct device_link {
 	bool supplier_preactivated; /* Owned by consumer probe. */
 };
 
-static inline struct device *kobj_to_dev(struct kobject *kobj)
+static inline struct device *__kobj_to_dev(struct kobject *kobj)
 {
 	return container_of(kobj, struct device, kobj);
 }
 
+static inline const struct device *__kobj_to_dev_const(const struct kobject *kobj)

include/linux/device.h struct device_link { · line 698

Quoted 10 lines
include/linux/device.h +10 -0
+{
+	return container_of(kobj, const struct device, kobj);
+}
+
+/*
+ * container_of() will happily take a const * and spit back a non-const * as it
+ * is just doing pointer math.  But we want to be a bit more careful in the
+ * driver code, so manually force any const * of a kobject to also be a const *
+ * to a device.
+ */

container_of() documentation has (probably?) never warned about this.

Wouldn't such a comment be more appropriate there? Albeit it wouldn't be needed if container_of() were fixed.

Quoted 8 lines
include/linux/device.h +5 -0
+#define kobj_to_dev(kobj)					\
+	_Generic((kobj),					\
+		 const struct kobject *: __kobj_to_dev_const,	\
+		 struct kobject *: __kobj_to_dev)(kobj)
+
 /**
  * device_iommu_mapped - Returns true when the device DMA is translated
  *			 by an IOMMU
Signature · 3 lines
Kind regards,

Sakari Ailus
author maintainer
#2 lore raw
Quoted 10 lines · Sakari Ailus

Hi Greg,

Quoted 6 lines · Greg Kroah-Hartman

If a const * to a kobject is passed to kobj_to_dev(), we want to return back a const * to a device as the driver core shouldn't be modifying a constant structure. But when dealing with container_of() the pointer const attribute is cast away, so we need to manually handle this by determining the type of the pointer passed in to know the type of the pointer to pass out.

Quoted 2 lines

Alternatively container_of() could be fixed, but that will likely produce lots of warnings currently.

Yeah, we can not do that because, as you found out, there's just too many warnings that it would cause. Let's work on the individual subsystems to clean them all up first before worrying about the core container_of() macro as that should fix the majority of the build warnings.

Quoted 28 lines
Quoted 28 lines

Luckily _Generic can do this type of magic, and as the kernel now supports C11 it is availble to us to handle this type of build-time type detection.

Cc Sakari Ailus <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>

---
v2 - use _Generic() to make this type safe as pointed out by Sakari

 include/linux/device.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
include/linux/device.h +1 -1
index 424b55df0272..023ea50b1916 100644
@@ -680,11 +680,27 @@ struct device_link {
 	bool supplier_preactivated; /* Owned by consumer probe. */
 };
 
-static inline struct device *kobj_to_dev(struct kobject *kobj)
+static inline struct device *__kobj_to_dev(struct kobject *kobj)
 {
 	return container_of(kobj, struct device, kobj);
 }
 
Quoted 13 lines

include/linux/device.h struct device_link { · line 698

Quoted 11 lines
include/linux/device.h +11 -0
+static inline const struct device *__kobj_to_dev_const(const struct kobject *kobj)
+{
+	return container_of(kobj, const struct device, kobj);
+}
+
+/*
+ * container_of() will happily take a const * and spit back a non-const * as it
+ * is just doing pointer math.  But we want to be a bit more careful in the
+ * driver code, so manually force any const * of a kobject to also be a const *
+ * to a device.
+ */

container_of() documentation has (probably?) never warned about this.

We never thought of it before :(

Quoted 2 lines

Wouldn't such a comment be more appropriate there? Albeit it wouldn't be needed if container_of() were fixed.

Some comment added to container_of() would be great, but that does not remove the need to keep this one.

thanks,

greg k-h

Reviewed-by on v2 1/1
#6 lore raw
In reply to Greg Kroah-Hartman
Quoted 10 lines · Greg Kroah-Hartman

If a const * to a kobject is passed to kobj_to_dev(), we want to return back a const * to a device as the driver core shouldn't be modifying a constant structure. But when dealing with container_of() the pointer const attribute is cast away, so we need to manually handle this by determining the type of the pointer passed in to know the type of the pointer to pass out.

Luckily _Generic can do this type of magic, and as the kernel now supports C11 it is availble to us to handle this type of build-time type detection.

I was following this in your branch and I find it good,

Quoted 46 lines
Cc Sakari Ailus <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>

---
v2 - use _Generic() to make this type safe as pointed out by Sakari

 include/linux/device.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
include/linux/device.h +17 -1
index 424b55df0272..023ea50b1916 100644
@@ -680,11 +680,27 @@ struct device_link {
 	bool supplier_preactivated; /* Owned by consumer probe. */
 };
 
-static inline struct device *kobj_to_dev(struct kobject *kobj)
+static inline struct device *__kobj_to_dev(struct kobject *kobj)
 {
 	return container_of(kobj, struct device, kobj);
 }
 
+static inline const struct device *__kobj_to_dev_const(const struct kobject *kobj)
+{
+	return container_of(kobj, const struct device, kobj);
+}
+
+/*
+ * container_of() will happily take a const * and spit back a non-const * as it
+ * is just doing pointer math.  But we want to be a bit more careful in the
+ * driver code, so manually force any const * of a kobject to also be a const *
+ * to a device.
+ */
+#define kobj_to_dev(kobj)					\
+	_Generic((kobj),					\
+		 const struct kobject *: __kobj_to_dev_const,	\
+		 struct kobject *: __kobj_to_dev)(kobj)
+
 /**
  * device_iommu_mapped - Returns true when the device DMA is translated
  *			 by an IOMMU
Signature · 1 line
2.38.0
Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by
#3 lore raw
In reply to Greg Kroah-Hartman

Hi Greg,

Quoted 11 lines · Greg Kroah-Hartman
Quoted 10 lines · Sakari Ailus

Hi Greg,

Quoted 6 lines · Greg Kroah-Hartman

If a const * to a kobject is passed to kobj_to_dev(), we want to return back a const * to a device as the driver core shouldn't be modifying a constant structure. But when dealing with container_of() the pointer const attribute is cast away, so we need to manually handle this by determining the type of the pointer passed in to know the type of the pointer to pass out.

Quoted 8 lines
Quoted 2 lines

Alternatively container_of() could be fixed, but that will likely produce lots of warnings currently.

Yeah, we can not do that because, as you found out, there's just too many warnings that it would cause. Let's work on the individual subsystems to clean them all up first before worrying about the core container_of() macro as that should fix the majority of the build warnings.

Sounds reasonable.

Quoted 43 lines
Quoted 29 lines
Quoted 29 lines

Luckily _Generic can do this type of magic, and as the kernel now supports C11 it is availble to us to handle this type of build-time type detection.

Cc Sakari Ailus <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>

---
v2 - use _Generic() to make this type safe as pointed out by Sakari

 include/linux/device.h | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
include/linux/device.h +1 -1
index 424b55df0272..023ea50b1916 100644
@@ -680,11 +680,27 @@ struct device_link {
 	bool supplier_preactivated; /* Owned by consumer probe. */
 };
 
-static inline struct device *kobj_to_dev(struct kobject *kobj)
+static inline struct device *__kobj_to_dev(struct kobject *kobj)
 {
 	return container_of(kobj, struct device, kobj);
 }
 
Quoted 13 lines

include/linux/device.h struct device_link { · line 698

Quoted 11 lines
include/linux/device.h +11 -0
+static inline const struct device *__kobj_to_dev_const(const struct kobject *kobj)
+{
+	return container_of(kobj, const struct device, kobj);
+}
+
+/*
+ * container_of() will happily take a const * and spit back a non-const * as it
+ * is just doing pointer math.  But we want to be a bit more careful in the
+ * driver code, so manually force any const * of a kobject to also be a const *
+ * to a device.
+ */

container_of() documentation has (probably?) never warned about this.

Quoted 7 lines

We never thought of it before :(

Quoted 2 lines

Wouldn't such a comment be more appropriate there? Albeit it wouldn't be needed if container_of() were fixed.

Some comment added to container_of() would be great, but that does not remove the need to keep this one.

I can send a patch for that.

For this one:

Signature · 3 lines
Kind regards,

Sakari Ailus
#4 lore raw
In reply to Greg Kroah-Hartman

From: Greg Kroah-Hartman

Quoted 13 lines

Sent: 17 October 2022 09:04

Quoted 10 lines · Sakari Ailus

Hi Greg,

Quoted 6 lines · Greg Kroah-Hartman

If a const * to a kobject is passed to kobj_to_dev(), we want to return back a const * to a device as the driver core shouldn't be modifying a constant structure. But when dealing with container_of() the pointer const attribute is cast away, so we need to manually handle this by determining the type of the pointer passed in to know the type of the pointer to pass out.

Quoted 8 lines
Quoted 2 lines

Alternatively container_of() could be fixed, but that will likely produce lots of warnings currently.

Yeah, we can not do that because, as you found out, there's just too many warnings that it would cause. Let's work on the individual subsystems to clean them all up first before worrying about the core container_of() macro as that should fix the majority of the build warnings.

Is it possible to generate a fixed container_of() with a different name and then use that to clean up the subsystems? Then finally rename it back?

That you probably be a lot less churn.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

author maintainer
#5 lore raw
Quoted 23 lines · David Laight

From: Greg Kroah-Hartman

Quoted 13 lines

Sent: 17 October 2022 09:04

Quoted 10 lines · Sakari Ailus

Hi Greg,

Quoted 6 lines · Greg Kroah-Hartman

If a const * to a kobject is passed to kobj_to_dev(), we want to return back a const * to a device as the driver core shouldn't be modifying a constant structure. But when dealing with container_of() the pointer const attribute is cast away, so we need to manually handle this by determining the type of the pointer passed in to know the type of the pointer to pass out.

Quoted 8 lines
Quoted 2 lines

Alternatively container_of() could be fixed, but that will likely produce lots of warnings currently.

Yeah, we can not do that because, as you found out, there's just too many warnings that it would cause. Let's work on the individual subsystems to clean them all up first before worrying about the core container_of() macro as that should fix the majority of the build warnings.

Quoted 5 lines

Is it possible to generate a fixed container_of() with a different name and then use that to clean up the subsystems? Then finally rename it back?

That you probably be a lot less churn.

That's the identical churn.