From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
Kan Liang <kan.liang@linux.intel.com>,
"Mike Rapoport (IBM)" <rppt@kernel.org>,
Wei Yang <richard.weiyang@gmail.com>,
James Clark <james.clark@linaro.org>,
Howard Chu <howardchu95@gmail.com>,
Kajol Jain <kjain@linux.ibm.com>,
Athira Rajeev <atrajeev@linux.vnet.ibm.com>,
Ze Gao <zegao2021@gmail.com>,
Weilin Wang <weilin.wang@intel.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org
Subject: [PATCH v1 3/8] tools headers: Update offsetof and container_of
Date: Fri, 13 Dec 2024 13:04:20 -0800 [thread overview]
Message-ID: <20241213210425.526512-4-irogers@google.com> (raw)
In-Reply-To: <20241213210425.526512-1-irogers@google.com>
Update to match kernel definitions in `include/linux/stddef.h` and
`include/linux/container_of.h`.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/include/linux/kernel.h | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)
diff --git a/tools/include/linux/kernel.h b/tools/include/linux/kernel.h
index 07cfad817d53..10f74f021d55 100644
--- a/tools/include/linux/kernel.h
+++ b/tools/include/linux/kernel.h
@@ -21,9 +21,8 @@
#define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
#define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
-#ifndef offsetof
-#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
-#endif
+#undef offsetof
+#define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
#ifndef container_of
/**
@@ -32,10 +31,14 @@
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
+ * WARNING: any const qualifier of @ptr is lost.
*/
-#define container_of(ptr, type, member) ({ \
- const typeof(((type *)0)->member) * __mptr = (ptr); \
- (type *)((char *)__mptr - offsetof(type, member)); })
+#define container_of(ptr, type, member) ({ \
+ void *__mptr = (void *)(ptr); \
+ static_assert(__same_type(*(ptr), ((type *)0)->member) || \
+ __same_type(*(ptr), void), \
+ "pointer type mismatch in container_of()"); \
+ ((type *)(__mptr - offsetof(type, member))); })
#endif
#ifndef max
--
2.47.1.613.gc27f4b7a9f-goog
next prev parent reply other threads:[~2024-12-13 21:04 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-13 21:04 [PATCH v1 0/8] Various fixes around undefined behavior Ian Rogers
2024-12-13 21:04 ` [PATCH v1 1/8] perf disasm: Avoid undefined behavior in incrementing NULL Ian Rogers
2024-12-14 12:33 ` Kuan-Wei Chiu
2024-12-13 21:04 ` [PATCH v1 2/8] perf test trace_btf_enum: Skip if permissions are insufficient Ian Rogers
2024-12-13 21:04 ` Ian Rogers [this message]
2024-12-14 12:33 ` [PATCH v1 3/8] tools headers: Update offsetof and container_of Kuan-Wei Chiu
2024-12-13 21:04 ` [PATCH v1 4/8] perf evsel: Avoid container_of on a NULL leader Ian Rogers
2024-12-13 21:04 ` [PATCH v1 5/8] tools headers: Add non-NULL assert to container_of Ian Rogers
2024-12-14 12:34 ` Kuan-Wei Chiu
2024-12-13 21:04 ` [PATCH v1 6/8] perf maps: Avoid UB passing NULL to bsearch Ian Rogers
2024-12-14 12:35 ` Kuan-Wei Chiu
2024-12-13 21:04 ` [PATCH v1 7/8] perf test shell lock_contention: Extra debug diagnostics Ian Rogers
2024-12-13 21:04 ` [PATCH v1 8/8] libperf event: Ensure tracing data is multiple of 8 sized Ian Rogers
2024-12-14 0:52 ` [PATCH v1 0/8] Various fixes around undefined behavior Namhyung Kim
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=20241213210425.526512-4-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=atrajeev@linux.vnet.ibm.com \
--cc=howardchu95@gmail.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=kjain@linux.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=richard.weiyang@gmail.com \
--cc=rppt@kernel.org \
--cc=weilin.wang@intel.com \
--cc=zegao2021@gmail.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