Skip to contentSkip to conversation

Switch to container_of_const() in macros

PATCH In review Sakari Ailus 2 participants 6 messages Oct 2, 2025

Subsystems (2)
lore
Share & tools
lore

Sakari Ailus

author maintainer
#0 lore raw

cover Switch to container_of_const() in macros

Hi all,

This set switches to container_of_const(), in order to add constness check for the users of macros obtaining a container struct of a struct field.

The good thing was that these patches didn't add any warnings but the added checks will also prevent the issues from happening in the future, too.

Sakari Ailus (3):
media: v4l2-subdev: Make media_entity_to_v4l2_subdev() const-aware
media: v4l2-dev: Make macros to obtain containers const-aware
media: mc: Make macros to obtain containers const-aware

 include/media/media-entity.h | 10 +++++-----
 include/media/v4l2-dev.h     |  6 +++---
 include/media/v4l2-subdev.h  |  2 +-
 3 files changed, 9 insertions(+), 9 deletions(-)
Signature · 1 line
2.47.3
author maintainer
#1 lore raw

PATCH 1/3 media: v4l2-subdev: Make media_entity_to_v4l2_subdev() const-aware

Retain the constness of the object in media_entity_to_v4l2_subdev(), by switching to container_of_const().

---
 include/media/v4l2-subdev.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/media/v4l2-subdev.h +1 -1
index e0bb58cb6d04..a37d9a847196 100644
@@ -1103,7 +1103,7 @@ struct v4l2_subdev {
 	typeof(ent) __me_sd_ent = (ent);				\
 									\
 	__me_sd_ent ?							\
-		container_of(__me_sd_ent, struct v4l2_subdev, entity) :	\
+		container_of_const(__me_sd_ent, struct v4l2_subdev, entity) : \
 		NULL;							\
 })
 
Signature · 1 line
2.47.3
author maintainer
#2 lore raw

PATCH 2/3 media: v4l2-dev: Make macros to obtain containers const-aware

In reply to Sakari Ailus

Retain the constness of the object in media_entity_to_video_device() and to_video_device(), by switching to container_of_const().

---
 include/media/v4l2-dev.h | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
include/media/v4l2-dev.h +3 -3
index a213c3398dcf..2e0f6d2e6a78 100644
@@ -320,8 +320,8 @@ struct video_device {
 	typeof(__entity) __me_vdev_ent = __entity;			\
 									\
 	__me_vdev_ent ?							\
-		container_of(__me_vdev_ent,  struct video_device, entity) : \
-		NULL;							\
+		container_of_const(__me_vdev_ent,  struct video_device, \
+				   entity) : NULL;			\
 })
 
 /**
@@ -330,7 +330,7 @@ struct video_device {
  *
  * @cd: pointer to &struct device
  */
-#define to_video_device(cd) container_of(cd, struct video_device, dev)
+#define to_video_device(cd) container_of_const(cd, struct video_device, dev)
 
 /**
  * __video_register_device - register video4linux devices
Signature · 1 line
2.47.3
author maintainer
#3 lore raw

PATCH 3/3 media: mc: Make macros to obtain containers const-aware

In reply to Sakari Ailus

Retain the constness of the graph objects and interfaces in macros to obtain their containers, by switching to container_of_const().

---
 include/media/media-entity.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
include/media/media-entity.h +5 -5
index 64cf590b1134..b91ff6f8c3bb 100644
@@ -627,7 +627,7 @@ static inline bool media_entity_enum_intersects(
  * @gobj: Pointer to the struct &media_gobj graph object
  */
 #define gobj_to_entity(gobj) \
-		container_of(gobj, struct media_entity, graph_obj)
+		container_of_const(gobj, struct media_entity, graph_obj)
 
 /**
  * gobj_to_pad - returns the struct &media_pad pointer from the
@@ -636,7 +636,7 @@ static inline bool media_entity_enum_intersects(
  * @gobj: Pointer to the struct &media_gobj graph object
  */
 #define gobj_to_pad(gobj) \
-		container_of(gobj, struct media_pad, graph_obj)
+		container_of_const(gobj, struct media_pad, graph_obj)
 
 /**
  * gobj_to_link - returns the struct &media_link pointer from the
@@ -645,7 +645,7 @@ static inline bool media_entity_enum_intersects(
  * @gobj: Pointer to the struct &media_gobj graph object
  */
 #define gobj_to_link(gobj) \
-		container_of(gobj, struct media_link, graph_obj)
+		container_of_const(gobj, struct media_link, graph_obj)
 
 /**
  * gobj_to_intf - returns the struct &media_interface pointer from the
@@ -654,7 +654,7 @@ static inline bool media_entity_enum_intersects(
  * @gobj: Pointer to the struct &media_gobj graph object
  */
 #define gobj_to_intf(gobj) \
-		container_of(gobj, struct media_interface, graph_obj)
+		container_of_const(gobj, struct media_interface, graph_obj)
 
 /**
  * intf_to_devnode - returns the struct media_intf_devnode pointer from the
@@ -663,7 +663,7 @@ static inline bool media_entity_enum_intersects(
  * @intf: Pointer to struct &media_intf_devnode
  */
 #define intf_to_devnode(intf) \
-		container_of(intf, struct media_intf_devnode, intf)
+		container_of_const(intf, struct media_intf_devnode, intf)
 
 /**
  *  media_gobj_create - Initialize a graph object
Signature · 1 line
2.47.3
author maintainer
#4 lore raw

PATCH 3/3 media: v4l2-dev: Make macros to obtain containers const-aware

In reply to Sakari Ailus

Retain the constness of the graph objects and interfaces in macros to obtain their containers, by switching to container_of_const().

---
 include/media/media-entity.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
include/media/media-entity.h +5 -5
index 64cf590b1134..b91ff6f8c3bb 100644
@@ -627,7 +627,7 @@ static inline bool media_entity_enum_intersects(
  * @gobj: Pointer to the struct &media_gobj graph object
  */
 #define gobj_to_entity(gobj) \
-		container_of(gobj, struct media_entity, graph_obj)
+		container_of_const(gobj, struct media_entity, graph_obj)
 
 /**
  * gobj_to_pad - returns the struct &media_pad pointer from the
@@ -636,7 +636,7 @@ static inline bool media_entity_enum_intersects(
  * @gobj: Pointer to the struct &media_gobj graph object
  */
 #define gobj_to_pad(gobj) \
-		container_of(gobj, struct media_pad, graph_obj)
+		container_of_const(gobj, struct media_pad, graph_obj)
 
 /**
  * gobj_to_link - returns the struct &media_link pointer from the
@@ -645,7 +645,7 @@ static inline bool media_entity_enum_intersects(
  * @gobj: Pointer to the struct &media_gobj graph object
  */
 #define gobj_to_link(gobj) \
-		container_of(gobj, struct media_link, graph_obj)
+		container_of_const(gobj, struct media_link, graph_obj)
 
 /**
  * gobj_to_intf - returns the struct &media_interface pointer from the
@@ -654,7 +654,7 @@ static inline bool media_entity_enum_intersects(
  * @gobj: Pointer to the struct &media_gobj graph object
  */
 #define gobj_to_intf(gobj) \
-		container_of(gobj, struct media_interface, graph_obj)
+		container_of_const(gobj, struct media_interface, graph_obj)
 
 /**
  * intf_to_devnode - returns the struct media_intf_devnode pointer from the
@@ -663,7 +663,7 @@ static inline bool media_entity_enum_intersects(
  * @intf: Pointer to struct &media_intf_devnode
  */
 #define intf_to_devnode(intf) \
-		container_of(intf, struct media_intf_devnode, intf)
+		container_of_const(intf, struct media_intf_devnode, intf)
 
 /**
  *  media_gobj_create - Initialize a graph object
Signature · 1 line
2.47.3
#5 lore raw

Re: [PATCH 3/3] media: v4l2-dev: Make macros to obtain containers const-aware

Quoted 4 lines · Sakari Ailus

Retain the constness of the graph objects and interfaces in macros to obtain their containers, by switching to container_of_const().

Please ignore this patch.

Signature · 1 line
Sakari Ailus