Skip to contentSkip to conversation

container_of: add container_of_const() that preserves const-ness of the pointer

PATCH Superseded Greg Kroah-Hartman 5 participants 14 messages Dec 1, 2022 – Dec 2, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
author maintainer
#1 lore raw

PATCH 2/4 device.h: move kobj_to_dev() to use container_of_const()

In reply to Greg Kroah-Hartman

Instead of rolling our own const-checking logic, use the newly introduced container_of_const() to handle it all for us automatically.

Cc Greg Kroah-Hartman <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Jason Gunthorpe <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
---
 include/linux/device.h | 22 ++--------------------
 1 file changed, 2 insertions(+), 20 deletions(-)
include/linux/device.h +2 -20
index 84ae52de6746..8ad2cd21b335 100644
@@ -680,26 +680,8 @@ struct device_link {
 	bool supplier_preactivated; /* Owned by consumer probe. */
 };
 
-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)
+#define kobj_to_dev(__kobj)	\
+	container_of_const(struct kobject, __kobj, struct device, kobj)
 
 /**
  * device_iommu_mapped - Returns true when the device DMA is translated
Signature · 1 line
2.38.1

Greg Kroah-Hartman

author maintainer
#0 lore raw

PATCH 1/4 container_of: add container_of_const() that preserves const-ness of the pointer

container_of does not preserve the const-ness of a pointer that is passed into it, which can cause C code that passes in a const pointer to get a pointer back that is not const and then scribble all over the data in it. To prevent this, container_of_const() will preserve the const status of the pointer passed into it using the newly available _Generic() method.

Cc Matthew Wilcox <[email protected]>
Cc Sakari Ailus <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
---
 include/linux/container_of.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/linux/container_of.h +14 -0
index 2008e9f4058c..3c290e865151 100644
@@ -22,4 +22,18 @@
 		      "pointer type mismatch in container_of()");	\
 	((type *)(__mptr - offsetof(type, member))); })
 
+/**
+ * container_of_const - cast a member of a structure out to the containing
+ *			structure and preserve the const-ness of the pointer
+ * @ptr_type:		the type of the pointer @ptr
+ * @ptr:		the pointer to the member
+ * @member_type:	the type of the container struct this is embedded in.
+ * @member:		the name of the member within the struct.
+ */
+#define container_of_const(ptr_type, ptr, member_type, member)		\
+	_Generic(ptr,							\
+		const ptr_type *: ((const member_type *)container_of(ptr, member_type, member)),\
+		ptr_type *: ((member_type *)container_of(ptr, member_type, member))	\
+	)
+
 #endif	/* _LINUX_CONTAINER_OF_H */
Signature · 1 line
2.38.1
author maintainer
#2 lore raw

PATCH 3/4 usb.h: take advantage of container_of_const()

Instead of rolling our own const-checking logic in to_usb_interface() and to_usb_device() use the newly added container_of_const() instead, making the logic much simpler overall.

---
 include/linux/usb.h | 44 ++++----------------------------------------
 1 file changed, 4 insertions(+), 40 deletions(-)
include/linux/usb.h +4 -40
index 4b463a5e4ba2..cd5637c950ae 100644
@@ -259,26 +259,8 @@ struct usb_interface {
 	struct work_struct reset_ws;	/* for resets in atomic context */
 };
 
-static inline struct usb_interface *__to_usb_interface(struct device *d)
-{
-	return container_of(d, struct usb_interface, dev);
-}
-
-static inline const struct usb_interface *__to_usb_interface_const(const struct device *d)
-{
-	return container_of(d, struct usb_interface, dev);
-}
-
-/*
- * 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 USB
- * driver code, so manually force any const * of a device to also be a const *
- * to a usb_device.
- */
-#define to_usb_interface(dev)						\
-	_Generic((dev),							\
-		 const struct device *: __to_usb_interface_const,	\
-		 struct device *: __to_usb_interface)(dev)
+#define to_usb_interface(__dev)	\
+	container_of_const(struct device, __dev, struct usb_interface, dev)
 
 static inline void *usb_get_intfdata(struct usb_interface *intf)
 {
@@ -730,26 +712,8 @@ struct usb_device {
 	unsigned use_generic_driver:1;
 };
 
-static inline struct usb_device *__to_usb_device(struct device *d)
-{
-	return container_of(d, struct usb_device, dev);
-}
-
-static inline const struct usb_device *__to_usb_device_const(const struct device *d)
-{
-	return container_of(d, struct usb_device, dev);
-}
-
-/*
- * 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 USB
- * driver code, so manually force any const * of a device to also be a const *
- * to a usb_device.
- */
-#define to_usb_device(dev)					\
-	_Generic((dev),						\
-		 const struct device *: __to_usb_device_const,	\
-		 struct device *: __to_usb_device)(dev)
+#define to_usb_device(__dev)	\
+	container_of_const(struct device, __dev, struct usb_device, dev)
 
 static inline struct usb_device *__intf_to_usbdev(struct usb_interface *intf)
 {
Signature · 1 line
2.38.1
author maintainer
#3 lore raw

PATCH 4/4 firmware_loader: fix up to_fw_sysfs() to preserve const

In reply to Greg Kroah-Hartman

to_fw_sysfs() was changed in commit 23680f0b7d7f ("driver core: make struct class.dev_uevent() take a const *") to pass in a const pointer but not pass it back out to handle some changes in the driver core. That isn't the best idea as it could cause problems if used incorrectly, so switch to use the container_of_const() macro instead which will preserve the const status of the pointer and enforce it by the compiler.

Cc Luis Chamberlain <[email protected]>
Cc Russ Weight <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Fixes 23680f0b7d7f ("driver core: make struct class.dev_uevent() take a const *")
---
 drivers/base/firmware_loader/sysfs.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/base/firmware_loader/sysfs.h +2 -5
index fd0b4ad9bdbb..0f0b1f7bf335 100644
@@ -80,11 +80,8 @@ struct fw_sysfs {
 	struct firmware *fw;
 	void *fw_upload_priv;
 };
-
-static inline struct fw_sysfs *to_fw_sysfs(const struct device *dev)
-{
-	return container_of(dev, struct fw_sysfs, dev);
-}
+#define to_fw_sysfs(__dev)	\
+	container_of_const(struct device, __dev, struct fw_sysfs, dev)
 
 void __fw_load_abort(struct fw_priv *fw_priv);
 
Signature · 1 line
2.38.1
Acked-by on v1 4/4
#4 lore raw

Re: [PATCH 4/4] firmware_loader: fix up to_fw_sysfs() to preserve const

Quoted 11 lines · Greg Kroah-Hartman

to_fw_sysfs() was changed in commit 23680f0b7d7f ("driver core: make struct class.dev_uevent() take a const *") to pass in a const pointer but not pass it back out to handle some changes in the driver core. That isn't the best idea as it could cause problems if used incorrectly, so switch to use the container_of_const() macro instead which will preserve the const status of the pointer and enforce it by the compiler.

Cc Luis Chamberlain <[email protected]>
Cc Russ Weight <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Fixes 23680f0b7d7f ("driver core: make struct class.dev_uevent() take a const *")
Quoted 23 lines
---
 drivers/base/firmware_loader/sysfs.h | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/base/firmware_loader/sysfs.h +2 -5
index fd0b4ad9bdbb..0f0b1f7bf335 100644
@@ -80,11 +80,8 @@ struct fw_sysfs {
 	struct firmware *fw;
 	void *fw_upload_priv;
 };
-
-static inline struct fw_sysfs *to_fw_sysfs(const struct device *dev)
-{
-	return container_of(dev, struct fw_sysfs, dev);
-}
+#define to_fw_sysfs(__dev)	\
+	container_of_const(struct device, __dev, struct fw_sysfs, dev)
 
 void __fw_load_abort(struct fw_priv *fw_priv);
 
Reviewed-by on v1 1/4
#5 lore raw
In reply to Greg Kroah-Hartman
Quoted 8 lines · Greg Kroah-Hartman

container_of does not preserve the const-ness of a pointer that is passed into it, which can cause C code that passes in a const pointer to get a pointer back that is not const and then scribble all over the data in it. To prevent this, container_of_const() will preserve the const status of the pointer passed into it using the newly available _Generic() method.

I believe this tag requires SoB of the co-develper. The code looks good to me, thanks!

Quoted 35 lines
Cc Matthew Wilcox <[email protected]>
Cc Sakari Ailus <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
---
 include/linux/container_of.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/linux/container_of.h +14 -0
index 2008e9f4058c..3c290e865151 100644
@@ -22,4 +22,18 @@
 		      "pointer type mismatch in container_of()");	\
 	((type *)(__mptr - offsetof(type, member))); })
 
+/**
+ * container_of_const - cast a member of a structure out to the containing
+ *			structure and preserve the const-ness of the pointer
+ * @ptr_type:		the type of the pointer @ptr
+ * @ptr:		the pointer to the member
+ * @member_type:	the type of the container struct this is embedded in.
+ * @member:		the name of the member within the struct.
+ */
+#define container_of_const(ptr_type, ptr, member_type, member)		\
+	_Generic(ptr,							\
+		const ptr_type *: ((const member_type *)container_of(ptr, member_type, member)),\
+		ptr_type *: ((member_type *)container_of(ptr, member_type, member))	\
+	)
+
 #endif	/* _LINUX_CONTAINER_OF_H */
Signature · 1 line
2.38.1
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
In reply to Greg Kroah-Hartman

Hi Greg,

Quoted 6 lines · Greg Kroah-Hartman

container_of does not preserve the const-ness of a pointer that is passed into it, which can cause C code that passes in a const pointer to get a pointer back that is not const and then scribble all over the data in it. To prevent this, container_of_const() will preserve the const status of the pointer passed into it using the newly available _Generic() method.

"_const" in the name suggests that the macro would always take a const argument. Could this be called e.g. container_of_safe() (for type-safe, but full type_safe would be a bit long)?

Signature · 3 lines
Kind regards,

Sakari Ailus
#6 lore raw
In reply to Andy Shevchenko
Quoted 11 lines · Andy Shevchenko
Quoted 8 lines · Greg Kroah-Hartman

container_of does not preserve the const-ness of a pointer that is passed into it, which can cause C code that passes in a const pointer to get a pointer back that is not const and then scribble all over the data in it. To prevent this, container_of_const() will preserve the const status of the pointer passed into it using the newly available _Generic() method.

I believe this tag requires SoB of the co-develper.

Sure, Greg you can add whatever tags are required

Did you look at possibly just calling this "container_of" ?

Thanks,
Jason

Reviewed-by on v1 1/4
#11 lore raw
In reply to Greg Kroah-Hartman
Quoted 13 lines · Greg Kroah-Hartman

container_of does not preserve the const-ness of a pointer that is passed into it, which can cause C code that passes in a const pointer to get a pointer back that is not const and then scribble all over the data in it. To prevent this, container_of_const() will preserve the const status of the pointer passed into it using the newly available _Generic() method.

Cc Matthew Wilcox <[email protected]>
Cc Sakari Ailus <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
Quoted 3 lines
---
 include/linux/container_of.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

For the whole series

Jason

author maintainer
#7 lore raw
In reply to Jason Gunthorpe
Quoted 9 lines · Jason Gunthorpe
Quoted 8 lines · Andy Shevchenko
Quoted 7 lines · Greg Kroah-Hartman

container_of does not preserve the const-ness of a pointer that is passed into it, which can cause C code that passes in a const pointer to get a pointer back that is not const and then scribble all over the data in it. To prevent this, container_of_const() will preserve the const status of the pointer passed into it using the newly available _Generic() method.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

I believe this tag requires SoB of the co-develper.

Sure, Greg you can add whatever tags are required

I need you to send me a signed-off-by line, I can't add that on my own for obvious reasons.

Quoted 1 line

Did you look at possibly just calling this "container_of" ?

Yes, but to do that would require all instances to be touched as this call takes 4 parameters, while container_of() takes 3, so that can't be done simply, AND there would be a lot of build errors all at once.

I'll work on moving code over to the new call as needed.

thanks,

greg k-h

author maintainer
#9 lore raw
In reply to Sakari Ailus
Quoted 10 lines · Sakari Ailus

Hi Greg,

Quoted 6 lines · Greg Kroah-Hartman

container_of does not preserve the const-ness of a pointer that is passed into it, which can cause C code that passes in a const pointer to get a pointer back that is not const and then scribble all over the data in it. To prevent this, container_of_const() will preserve the const status of the pointer passed into it using the newly available _Generic() method.

Quoted 2 lines

"_const" in the name suggests that the macro would always take a const argument.

I mean it here to be "this will preserve const" as yes, it can take a const. Or not.

Quoted 2 lines

Could this be called e.g. container_of_safe() (for type-safe, but full type_safe would be a bit long)?

const is an attribute of type safety, container_of is also type-safe as-is, it's just the const portion here that is the difference between the two.

Naming is hard :(

Quoted 5 lines
Signature · 4 lines
-- 
Kind regards,

Sakari Ailus
#10 lore raw
Quoted 15 lines · Greg Kroah-Hartman
Quoted 10 lines · Sakari Ailus
Quoted 6 lines · Greg Kroah-Hartman

container_of does not preserve the const-ness of a pointer that is passed into it, which can cause C code that passes in a const pointer to get a pointer back that is not const and then scribble all over the data in it. To prevent this, container_of_const() will preserve the const status of the pointer passed into it using the newly available _Generic() method.

"_const" in the name suggests that the macro would always take a const argument.

I mean it here to be "this will preserve const" as yes, it can take a const. Or not.

Quoted 6 lines
Quoted 2 lines

Could this be called e.g. container_of_safe() (for type-safe, but full type_safe would be a bit long)?

const is an attribute of type safety, container_of is also type-safe as-is, it's just the const portion here that is the difference between the two.

container_of_const_safe() :-)

Quoted 1 line

Naming is hard :(

True.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw
In reply to Greg Kroah-Hartman

Hi Greg,

Quoted 24 lines · Greg Kroah-Hartman

container_of does not preserve the const-ness of a pointer that is passed into it, which can cause C code that passes in a const pointer to get a pointer back that is not const and then scribble all over the data in it. To prevent this, container_of_const() will preserve the const status of the pointer passed into it using the newly available _Generic() method.

Cc Matthew Wilcox <[email protected]>
Cc Sakari Ailus <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
---
 include/linux/container_of.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/linux/container_of.h +0 -0
index 2008e9f4058c..3c290e865151 100644
@@ -22,4 +22,18 @@
 		      "pointer type mismatch in container_of()");	\
 	((type *)(__mptr - offsetof(type, member))); })

include/linux/container_of.h line 33

Quoted 10 lines
include/linux/container_of.h +9 -0
 
+/**
+ * container_of_const - cast a member of a structure out to the containing
+ *			structure and preserve the const-ness of the pointer
+ * @ptr_type:		the type of the pointer @ptr
+ * @ptr:		the pointer to the member
+ * @member_type:	the type of the container struct this is embedded in.
+ * @member:		the name of the member within the struct.
+ */
+#define container_of_const(ptr_type, ptr, member_type, member)		\

I missed earlier you had four arguments for the macro instead of three.

With default: this can be done with just three:

#define container_of_const(ptr, member_type, member)		\
	_Generic(ptr,							\
		 const typeof(*(ptr)) *: ((const member_type *)container_of(ptr, member_type, member)),\
		 default: ((member_type *)container_of(ptr, member_type, member))	\
	)

The const typeof(*(ptr)) * will match if ptr is const, otherwise default matches. But you can't have typeof(*(ptr)) * instead of default as the two types are still the same, hence default.

I've tested this on GCC 10.2.1 and clang 11.0.1.

This should also make it a bit easier for existing users to switch to the new macro and hopefully eventual rename back to container_of() once all users have been converted.

Quoted 6 lines
include/linux/container_of.h +5 -0
+	_Generic(ptr,							\
+		const ptr_type *: ((const member_type *)container_of(ptr, member_type, member)),\
+		ptr_type *: ((member_type *)container_of(ptr, member_type, member))	\
+	)
+
 #endif	/* _LINUX_CONTAINER_OF_H */
Signature · 3 lines
Kind regards,

Sakari Ailus
author maintainer
#13 lore raw
Quoted 42 lines · Sakari Ailus

Hi Greg,

Quoted 24 lines · Greg Kroah-Hartman

container_of does not preserve the const-ness of a pointer that is passed into it, which can cause C code that passes in a const pointer to get a pointer back that is not const and then scribble all over the data in it. To prevent this, container_of_const() will preserve the const status of the pointer passed into it using the newly available _Generic() method.

Cc Matthew Wilcox <[email protected]>
Cc Sakari Ailus <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc "Rafael J. Wysocki" <[email protected]>
---
 include/linux/container_of.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
include/linux/container_of.h +0 -0
index 2008e9f4058c..3c290e865151 100644
@@ -22,4 +22,18 @@
 		      "pointer type mismatch in container_of()");	\
 	((type *)(__mptr - offsetof(type, member))); })

include/linux/container_of.h line 33

Quoted 10 lines
include/linux/container_of.h +9 -0
 
+/**
+ * container_of_const - cast a member of a structure out to the containing
+ *			structure and preserve the const-ness of the pointer
+ * @ptr_type:		the type of the pointer @ptr
+ * @ptr:		the pointer to the member
+ * @member_type:	the type of the container struct this is embedded in.
+ * @member:		the name of the member within the struct.
+ */
+#define container_of_const(ptr_type, ptr, member_type, member)		\

I missed earlier you had four arguments for the macro instead of three.

With default: this can be done with just three:

Quoted 8 lines
#define container_of_const(ptr, member_type, member)		\
	_Generic(ptr,							\
		 const typeof(*(ptr)) *: ((const member_type *)container_of(ptr, member_type, member)),\
		 default: ((member_type *)container_of(ptr, member_type, member))	\
	)

The const typeof(*(ptr)) * will match if ptr is const, otherwise default matches.

I had tried to use typeof before, your way is easier, thanks, I couldn't get it to work myself...

Quoted 2 lines

But you can't have typeof(*(ptr)) * instead of default as the two types are still the same, hence default.

Ah. Ok, I really didn't want to use default, and that's probably why it wasn't working for me.

Quoted 5 lines

I've tested this on GCC 10.2.1 and clang 11.0.1.

This should also make it a bit easier for existing users to switch to the new macro and hopefully eventual rename back to container_of() once all users have been converted.

True. Ok, I'll try this later tomorrow and send out a new version if all goes well, thanks!

greg k-h