public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/1] container_of: Document container_of_const() is preferred
@ 2024-06-17 10:08 Sakari Ailus
  2024-06-17 10:44 ` Greg Kroah-Hartman
  2024-08-09 12:59 ` Andy Shevchenko
  0 siblings, 2 replies; 9+ messages in thread
From: Sakari Ailus @ 2024-06-17 10:08 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andy Shevchenko, Greg Kroah-Hartman, Jason Gunthorpe,
	Matthew Wilcox, Rafael J . Wysocki, Hans Verkuil

There is a warning in kerneldoc documentation of container_of() that
constness of @ptr is lost. While this is a suggestion container_of_const()
should be used instead, the vast majority of new code still uses
container_of():

$ git diff v6.8 v6.9|grep container_of\(|wc -l
788
$ git diff v6.8 v6.9|grep container_of_const|wc -l
11

Make an explicit recommendation to use container_of_const(), unless @ptr
is const but its container isn't.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
---
 include/linux/container_of.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/include/linux/container_of.h b/include/linux/container_of.h
index 713890c867be..7563015ff165 100644
--- a/include/linux/container_of.h
+++ b/include/linux/container_of.h
@@ -13,7 +13,9 @@
  * @type:	the type of the container struct this is embedded in.
  * @member:	the name of the member within the struct.
  *
- * WARNING: any const qualifier of @ptr is lost.
+ * WARNING: any const qualifier of @ptr is lost. container_of() should only be
+ * used in cases where @ptr is const and its container is not and you know what
+ * you're doing. Otherwise always use container_of_const().
  */
 #define container_of(ptr, type, member) ({				\
 	void *__mptr = (void *)(ptr);					\
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-08-09 12:59 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-17 10:08 [PATCH 1/1] container_of: Document container_of_const() is preferred Sakari Ailus
2024-06-17 10:44 ` Greg Kroah-Hartman
2024-06-18  9:09   ` Sakari Ailus
2024-06-18 10:01     ` Greg Kroah-Hartman
2024-06-18 11:52       ` Sakari Ailus
2024-06-18 12:54         ` Greg Kroah-Hartman
2024-06-18 13:56           ` Matthew Wilcox
2024-06-18 14:02             ` Greg Kroah-Hartman
2024-08-09 12:59 ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox