[PATCH v3 1/2] overflow: Add offsetofend() variant of struct_offset()

From: Bence Csokas

Date: Fri Nov 13 2026 - 11:54:14 EST


Many call sites of offsetofend() use it for the purpose of pointer
artihmetic. Thus, a common usage pattern is:

offsetofend(typeof(*ptr), member)

Capturing this in one macro helps keep lines short and terse, while
still making the purpose clear. It also helps discourage spelling out
the type name on every callsite, leading to more maintainable code.

A similar struct_offset() macro already exists, so add an offsetofend()
variant in the same spirit.

Signed-off-by: Bence Csokas <bence.csokas@xxxxxxx>
---
include/linux/overflow.h | 12 ++++++++++++
scripts/coccinelle/api/struct_offsetof.cocci | 14 ++++++++++++++
2 files changed, 26 insertions(+)

diff --git a/include/linux/overflow.h b/include/linux/overflow.h
index d0f04436343e..c029d68daeca 100644
--- a/include/linux/overflow.h
+++ b/include/linux/overflow.h
@@ -464,6 +464,18 @@ static __always_inline size_t __must_check size_sub(size_t minuend, size_t subtr
*/
#define struct_offset(p, member) (offsetof(typeof(*(p)), member))

+/**
+ * struct_offsetofend() - Calculate the end of a member within a struct
+ * @p: Pointer to the struct
+ * @member: Name of the member to get the end offset of
+ *
+ * Calculates the offset of the end of a particular @member of the structure
+ * pointed to by @p.
+ *
+ * Return: number of bytes to the end of @member.
+ */
+#define struct_offsetofend(p, member) (offsetofend(typeof(*(p)), member))
+
/**
* __DEFINE_FLEX() - helper macro for DEFINE_FLEX() family.
* Enables caller macro to pass arbitrary trailing expressions
diff --git a/scripts/coccinelle/api/struct_offsetof.cocci b/scripts/coccinelle/api/struct_offsetof.cocci
new file mode 100644
index 000000000000..6cbc5596d673
--- /dev/null
+++ b/scripts/coccinelle/api/struct_offsetof.cocci
@@ -0,0 +1,14 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/// Use struct_offsetof() family of macros
+///
+// Confidence: High
+@@
+expression ptr, member;
+@@
+- offsetof(typeof(*ptr), member)
++ struct_offset(ptr, member)
+@@
+expression ptr, member;
+@@
+- offsetofend(typeof(*ptr), member)
++ struct_offsetofend(ptr, member)

--
2.56.1