Re: [PATCH v4 1/2] overflow: Add offsetofend() variant of struct_offset()
From: Julia Lawall
Date: Wed Nov 18 2026 - 09:08:36 EST
On Wed, 18 Nov 2026, Bence Csokas wrote:
> 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 | 18 ++++++++++++++++++
> 2 files changed, 30 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..5a30647d3787
> --- /dev/null
> +++ b/scripts/coccinelle/api/struct_offsetof.cocci
> @@ -0,0 +1,18 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/// Use struct_offset() family of macros
> +///
> +// Confidence: High
> +// Options: --no-includes --ignore ./tools/
> +
> +virtual patch
> +
> +@offsetof depends on patch@
> +expression ptr, member;
> +@@
> +- offsetof(typeof(*(ptr)), member)
> ++ struct_offset(ptr, member)
> +@offsetofend depends on patch@
> +expression ptr, member;
> +@@
> +- offsetofend(typeof(*(ptr)), member)
> ++ struct_offsetofend(ptr, member)
Coccinelle script
Reviewed-by: Julia Lawall <julia.lawall@xxxxxxxx>