All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vincent Mailhol <mailhol@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	 Sakari Ailus <sakari.ailus@linux.intel.com>,
	 Nick Desaulniers <ndesaulniers@google.com>,
	 Alexander Lobakin <alobakin@pm.me>,
	Arnd Bergmann <arnd@arndb.de>,  David Sterba <dsterba@suse.cz>,
	Ivo van Doorn <ivdoorn@gmail.com>,
	 Alexey Dobriyan <adobriyan@gmail.com>,
	linux-kernel@vger.kernel.org,
	 Vincent Mailhol <mailhol@kernel.org>
Subject: [PATCH 0/3] container_of: refactors
Date: Tue, 14 Jul 2026 20:18:00 +0200	[thread overview]
Message-ID: <20260714-containerof_refactor-v1-0-b5c31164d2ad@kernel.org> (raw)

This series refactors the container_of() function-like macro to improve
readability and remove a sparse/W=2 shadow warning. Further details in
each patch.

While I was expecting this series to be boring and purely cosmetic, the
bloat-o-meter stats gave some unexpected results:

  $ ./scripts/bloat-o-meter vmlinux7.2-rc3_before.o vmlinux7.2-rc3_after.o 
  add/remove: 0/0 grow/shrink: 133/93 up/down: 5914901/-14344137 (-8429236)
  < ... 227 lines redacted >
  Total: Before=2641674349, After=2633245113, chg -0.32%

(done on v7.2-rc3 with GCC 15.3.0 on an x86_64 defconfig)

Upon analysis, this change in size can be tracked down to places where
container_of() is used in combination with __builtin_constant_p().

Here is a minimal reproducer:

	struct foo {
		int a;
	};

	#define to_foo(a_ptr) container_of(a_ptr, struct foo, a)

	int f(int *a)
	{
		return __builtin_constant_p(to_foo(a)->a) || a;
	}

The assembly code before this series...:

	xor     eax, eax
	test    rdi, rdi
	setne   al
	ret

...and after:

	mov     eax, 1
	ret

Link: https://godbolt.org/z/fenbGexjY

__builtin_constant_p(to_foo(a)->a) evaluates to false but gives the
optimiser the hint that pointer a is not NULL because of the
assumption that no undefined behaviour occurs. With this, the
expression:

	__builtin_constant_p(to_foo(a)->a) || a

could be evaluated as true by the optimiser.

But the small variation in container_of() makes it that the optimiser
currently misses this optimisation but manages to do it after the
simplification of patch #3 of this series.

When __builtin_constant_p()'s argument is not trivially a compile time
constant, the result of __builtin_constant_p() comes late in the
evaluation process. And if it comes too late, after some other
optimisations were already done, the compiler will not retry and simply
miss these optimisations.

Note that the above example is very fragile and the results shown in
the godbolt link might not be reproducible under very small
variations.

Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
---
Vincent Mailhol (3):
      container_of: apply typeof_member() to container_of()
      container_of: remove useless pair of parentheses
      container_of: remove local __mptr variable

 include/linux/container_of.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
---
base-commit: a13c140cc289c0b7b3770bce5b3ad42ab35074aa
change-id: 20260110-containerof_refactor-63acf8118a18

Best regards,
-- 
Vincent Mailhol <mailhol@kernel.org>


             reply	other threads:[~2026-07-14 18:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-14 18:18 Vincent Mailhol [this message]
2026-07-14 18:18 ` [PATCH 1/3] container_of: apply typeof_member() to container_of() Vincent Mailhol
2026-07-14 18:18 ` [PATCH 2/3] container_of: remove useless pair of parentheses Vincent Mailhol
2026-07-15 14:30   ` Alexey Dobriyan
2026-07-15 14:51     ` Vincent Mailhol
2026-07-15 14:57       ` Greg Kroah-Hartman
2026-07-14 18:18 ` [PATCH 3/3] container_of: remove local __mptr variable Vincent Mailhol
2026-07-15  4:54 ` [PATCH 0/3] container_of: refactors Greg Kroah-Hartman
2026-07-15  5:33   ` Vincent Mailhol

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260714-containerof_refactor-v1-0-b5c31164d2ad@kernel.org \
    --to=mailhol@kernel.org \
    --cc=adobriyan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=alobakin@pm.me \
    --cc=arnd@arndb.de \
    --cc=dsterba@suse.cz \
    --cc=gregkh@linuxfoundation.org \
    --cc=ivdoorn@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=ndesaulniers@google.com \
    --cc=sakari.ailus@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.