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 3/3] container_of: remove local __mptr variable
Date: Tue, 14 Jul 2026 20:18:03 +0200 [thread overview]
Message-ID: <20260714-containerof_refactor-v1-3-b5c31164d2ad@kernel.org> (raw)
In-Reply-To: <20260714-containerof_refactor-v1-0-b5c31164d2ad@kernel.org>
container_of() can be called in a nested manner to retrieve the grand
parent structure as illustrated below:
struct foo {
int a;
};
struct bar {
struct foo foo;
};
#define to_foo(a_ptr) container_of(a_ptr, struct foo, a)
#define to_bar(a_ptr) container_of(to_foo(a_ptr), struct bar, foo)
The issue is that the above construct will cause __mptr, the local
variable of container_of(), to shadow itself because of the nested
call. This then triggers a warning in sparse and W=2 builds.
While this warning is benign, it still causes some overhead as proven by
below list of commits in which people made local workarounds:
- commit 7eab14de73a8 ("mdio, phy: fix -Wshadow warnings triggered by
nested container_of()")
- commit 8d8c3131248d ("clk: define to_clk_regmap() as inline
function")
- commit bfb972c5e1cb ("IB/verbs: avoid nested container_of()")
- commit 093adbcedf12 ("btrfs: switch helper macros to static inlines
in sysfs.h")
- commit c1d35dfa0f7d ("rt2x00: Fix sparse warning on nested
container_of()")
(the list is probably not exhaustive).
As a matter of fact, the local variable __mptr is only used once in
container_of(). As such, it is not strictly needed. Inline that local
__mptr variable to remove once and for all the risk of variable
shadowing when nesting container_of() and prevent people from writing
further local fixes.
Signed-off-by: Vincent Mailhol <mailhol@kernel.org>
---
include/linux/container_of.h | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/include/linux/container_of.h b/include/linux/container_of.h
index 68153170db32..28db38e9ee3e 100644
--- a/include/linux/container_of.h
+++ b/include/linux/container_of.h
@@ -17,11 +17,10 @@
* Do not use container_of() in new code.
*/
#define container_of(ptr, type, member) ({ \
- void *__mptr = (void *)(ptr); \
static_assert(__same_type(*(ptr), typeof_member(type, member)) || \
__same_type(*(ptr), void), \
"pointer type mismatch in container_of()"); \
- (type *)(__mptr - offsetof(type, member)); })
+ (type *)((void *)(ptr) - offsetof(type, member)); })
/**
* container_of_const - cast a member of a structure out to the containing
--
2.54.0
next prev parent reply other threads:[~2026-07-14 18:19 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-14 18:18 [PATCH 0/3] container_of: refactors Vincent Mailhol
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-15 16:27 ` Vincent Mailhol
2026-07-16 14:09 ` Greg Kroah-Hartman
2026-07-14 18:18 ` Vincent Mailhol [this message]
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-3-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox