Skip to contentSkip to conversation

checkpatch: Deprecate container_of() in favour of container_of_const()

PATCH In review Sakari Ailus 5 participants 6 messages Aug 11, 2025 – Nov 5, 2025 Checkpatch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 checkpatch: Deprecate container_of() in favour of container_of_const()

container_of() discards constness of its first argument and thus its use may result in inadvertly unconstifying an object that is const. While container_of_const() addresses this problem, the vast majority of new uses of the two use the container_of() variant lacking the check.

Stem the flow of new container_of() use by adding it to the list of deprecated functions in scripts/checkpatch.pl. Once all existing container_of() users have been fixed to respect the constness attribute, the check can be added to container_of() and we can switch back to container_of() again.

---
 scripts/checkpatch.pl | 1 +
 1 file changed, 1 insertion(+)
scripts/checkpatch.pl +1 -0
index 664f7b7a622c..32edebfc1935 100755
@@ -859,6 +859,7 @@ our %deprecated_apis = (
 	"kunmap_atomic"				=> "kunmap_local",
 	"srcu_read_lock_lite"			=> "srcu_read_lock_fast",
 	"srcu_read_unlock_lite"			=> "srcu_read_unlock_fast",
+	"container_of"				=> "container_of_const",
 );
 
 #Create a search pattern for all these strings to speed up a loop below
Signature · 1 line
2.39.5
Acked-by on v1 1/1
#1 lore raw
Quoted 31 lines · Sakari Ailus

container_of() discards constness of its first argument and thus its use may result in inadvertly unconstifying an object that is const. While container_of_const() addresses this problem, the vast majority of new uses of the two use the container_of() variant lacking the check.

Stem the flow of new container_of() use by adding it to the list of deprecated functions in scripts/checkpatch.pl. Once all existing container_of() users have been fixed to respect the constness attribute, the check can be added to container_of() and we can switch back to container_of() again.

---
 scripts/checkpatch.pl | 1 +
 1 file changed, 1 insertion(+)
scripts/checkpatch.pl +1 -0
index 664f7b7a622c..32edebfc1935 100755
@@ -859,6 +859,7 @@ our %deprecated_apis = (
 	"kunmap_atomic"				=> "kunmap_local",
 	"srcu_read_lock_lite"			=> "srcu_read_lock_fast",
 	"srcu_read_unlock_lite"			=> "srcu_read_unlock_fast",
+	"container_of"				=> "container_of_const",
 );
 
 #Create a search pattern for all these strings to speed up a loop below
Signature · 1 line
2.39.5
#2 lore raw
In reply to Sakari Ailus

Ping.

Quoted 32 lines · Sakari Ailus

container_of() discards constness of its first argument and thus its use may result in inadvertly unconstifying an object that is const. While container_of_const() addresses this problem, the vast majority of new uses of the two use the container_of() variant lacking the check.

Stem the flow of new container_of() use by adding it to the list of deprecated functions in scripts/checkpatch.pl. Once all existing container_of() users have been fixed to respect the constness attribute, the check can be added to container_of() and we can switch back to container_of() again.

---
 scripts/checkpatch.pl | 1 +
 1 file changed, 1 insertion(+)
scripts/checkpatch.pl +1 -0
index 664f7b7a622c..32edebfc1935 100755
@@ -859,6 +859,7 @@ our %deprecated_apis = (
 	"kunmap_atomic"				=> "kunmap_local",
 	"srcu_read_lock_lite"			=> "srcu_read_lock_fast",
 	"srcu_read_unlock_lite"			=> "srcu_read_unlock_fast",
+	"container_of"				=> "container_of_const",
 );
 
 #Create a search pattern for all these strings to speed up a loop below
Signature · 1 line
2.39.5
Signature · 1 line
Sakari Ailus
maintainer
#3 lore raw
Quoted 35 lines · Sakari Ailus

Ping.

Quoted 32 lines · Sakari Ailus

container_of() discards constness of its first argument and thus its use may result in inadvertly unconstifying an object that is const. While container_of_const() addresses this problem, the vast majority of new uses of the two use the container_of() variant lacking the check.

Stem the flow of new container_of() use by adding it to the list of deprecated functions in scripts/checkpatch.pl. Once all existing container_of() users have been fixed to respect the constness attribute, the check can be added to container_of() and we can switch back to container_of() again.

---
 scripts/checkpatch.pl | 1 +
 1 file changed, 1 insertion(+)
scripts/checkpatch.pl +1 -0
index 664f7b7a622c..32edebfc1935 100755
@@ -859,6 +859,7 @@ our %deprecated_apis = (
 	"kunmap_atomic"				=> "kunmap_local",
 	"srcu_read_lock_lite"			=> "srcu_read_lock_fast",
 	"srcu_read_unlock_lite"			=> "srcu_read_unlock_fast",
+	"container_of"				=> "container_of_const",
 );
 
 #Create a search pattern for all these strings to speed up a loop below
Signature · 1 line
2.39.5
#4 lore raw
In reply to Sakari Ailus

Well, if container_of_const is a drop-in replacement for all uses of container_of, just fix the implementation? If it is an almost but not complete replacement, find a different name for the few uses. And if it's not anywhere near drop in, just using container_of make a lot more sense.

Just in case it wasn't clear: We really should keep the shorter nice name as the default if at all possible. Both because it reads better and because it avoids churn.

#5 lore raw

Hi Christoph,

Quoted 9 lines · Christoph Hellwig

Well, if container_of_const is a drop-in replacement for all uses of container_of, just fix the implementation? If it is an almost but not complete replacement, find a different name for the few uses. And if it's not anywhere near drop in, just using container_of make a lot more sense.

Just in case it wasn't clear: We really should keep the shorter nice name as the default if at all possible. Both because it reads better and because it avoids churn.

I'm in favour of the container_of() name instead of container_of_const(), but there are a vast number of uses of container_of() in the kernel and fixing these to respect const qualifier is going to take time. The intent here is to prevent adding new uses of container_of() that do not respect constness.

I tested this again on v6.18-rc2 and with my .config (far from allyesconfig) there were 840 warnings from discarding const qualifier. Presumably a lot of these are duplicates, but then again it'll be a thankless (and possibly endless?) task to fix these if we don't make effort to avoid new ones from being added at the same time.

Signature · 3 lines
Kind regards,

Sakari Ailus