[PATCH] linux/kernel.h: add container_from()
From: Allen Pais
Date: Wed Aug 26 2020 - 21:36:54 EST
Introduce container_from() as a generic helper instead of
sub-systems defining a private from_* API
(Eg: from_tasklets recently introduced in
12cc923f1ccc: Tasklet: Introduce new initialization API)
The helper is similar to container_of() in argument order
with the difference of naming the containing structure instead
of having to specify its type.
Suggested-by: James E.J. Bottomley <James.Bottomley@xxxxxxxxxxxxxxxxxxxxx>
Suggested-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>
Suggested-by: Jens Axboe <axboe@xxxxxxxxx>
Signed-off-by: Allen Pais <allen.lkml@xxxxxxxxx>
---
include/linux/kernel.h | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/include/linux/kernel.h b/include/linux/kernel.h
index 500def620d8f..9d446324a8be 100644
--- a/include/linux/kernel.h
+++ b/include/linux/kernel.h
@@ -1019,6 +1019,15 @@ static inline void ftrace_dump(enum ftrace_dump_mode oops_dump_mode) { }
"pointer type mismatch in container_of()"); \
IS_ERR_OR_NULL(__mptr) ? ERR_CAST(__mptr) : \
((type *)(__mptr - offsetof(type, member))); })
+/**
+ * container_from - cast a member of a structure out to the containing structure
+ * @ptr: the pointer to the member.
+ * @container: the type of the container struct.
+ * @member: the name of the member within the struct.
+ *
+ */
+#define container_from(ptr, container, member) \
+ container_of(ptr, typeof(*container), member)
/* Rebuild everything on CONFIG_FTRACE_MCOUNT_RECORD */
#ifdef CONFIG_FTRACE_MCOUNT_RECORD
--
2.25.1