checkpatch: Deprecate container_of() in favour of container_of_const()
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
@@ -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
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. Whilecontainer_of_const()addresses this problem, the vast majority of new uses of the two use thecontainer_of()variant lacking the check.Stem the flow of new
container_of()use by adding it to the list of deprecated functions inscripts/checkpatch.pl. Once all existingcontainer_of()users have been fixed to respect the constness attribute, the check can be added tocontainer_of()and we can switch back tocontainer_of()again.Signed-off-by Sakari Ailus <[email protected]>--- scripts/checkpatch.pl | 1 + 1 file changed, 1 insertion(+)scripts/checkpatch.pl +1
-0@@ -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 belowSignature · 1 line
2.39.5
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. Whilecontainer_of_const()addresses this problem, the vast majority of new uses of the two use thecontainer_of()variant lacking the check.Stem the flow of new
container_of()use by adding it to the list of deprecated functions inscripts/checkpatch.pl. Once all existingcontainer_of()users have been fixed to respect the constness attribute, the check can be added tocontainer_of()and we can switch back tocontainer_of()again.Signed-off-by Sakari Ailus <[email protected]>--- scripts/checkpatch.pl | 1 + 1 file changed, 1 insertion(+)scripts/checkpatch.pl +1
-0@@ -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 belowSignature · 1 line
2.39.5
Signature · 1 line
Sakari Ailus
Joe Perches
maintainerQuoted 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. Whilecontainer_of_const()addresses this problem, the vast majority of new uses of the two use thecontainer_of()variant lacking the check.Stem the flow of new
container_of()use by adding it to the list of deprecated functions inscripts/checkpatch.pl. Once all existingcontainer_of()users have been fixed to respect the constness attribute, the check can be added tocontainer_of()and we can switch back tocontainer_of()again.Signed-off-by Sakari Ailus <[email protected]>--- scripts/checkpatch.pl | 1 + 1 file changed, 1 insertion(+)scripts/checkpatch.pl +1
-0@@ -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 belowSignature · 1 line
2.39.5
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.
Hi Christoph,
Quoted 9 lines · Christoph Hellwig
Well, if
container_of_constis a drop-in replacement for all uses ofcontainer_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 usingcontainer_ofmake 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