Skip to contentSkip to conversation

perf/x86/intel/bts: Replace offsetof() with struct_size()

PATCH Applied Thorsten Blum 2 participants 2 messages Apr 13, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf/x86/intel/bts: Replace offsetof() with struct_size()

Use struct_size() to calculate the number of bytes to allocate for a new bts_buffer. Compared to offsetof(), struct_size() provides additional compile-time checks (e.g., __must_be_array()).

---
 arch/x86/events/intel/bts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/bts.c +1 -1
index da03f53bfa18..16bc89c8023b 100644
@@ -101,7 +101,7 @@ bts_buffer_setup_aux(struct perf_event *event, void **pages,
 	if (overwrite && nr_buf > 1)
 		return NULL;
 
-	bb = kzalloc_node(offsetof(struct bts_buffer, buf[nr_buf]), GFP_KERNEL, node);
+	bb = kzalloc_node(struct_size(bb, buf, nr_buf), GFP_KERNEL, node);
 	if (!bb)
 		return NULL;
 
Signature · 1 line
2.49.0
merge bot Applied
#1 lore raw
The following commit has been merged into the perf/core branch of tip:

The following commit has been merged into the perf/core branch of tip:

Commit-ID:     5c3627b6f0595f1ec27e6f5df903bd072e9b9136
Gitweb:        https://git.kernel.org/tip/5c3627b6f0595f1ec27e6f5df903bd072e9b9136
Author:        Thorsten Blum <[email protected]>
AuthorDate:    Sun, 13 Apr 2025 12:41:09 +02:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Sun, 13 Apr 2025 21:05:50 +02:00

perf/x86/intel/bts: Replace offsetof() with struct_size()

Use struct_size() to calculate the number of bytes to allocate for a new bts_buffer. Compared to offsetof(), struct_size() provides additional compile-time checks (e.g., __must_be_array()).

---
 arch/x86/events/intel/bts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/bts.c +1 -1
index da03f53..16bc89c 100644
@@ -101,7 +101,7 @@ bts_buffer_setup_aux(struct perf_event *event, void **pages,
 	if (overwrite && nr_buf > 1)
 		return NULL;
 
-	bb = kzalloc_node(offsetof(struct bts_buffer, buf[nr_buf]), GFP_KERNEL, node);
+	bb = kzalloc_node(struct_size(bb, buf, nr_buf), GFP_KERNEL, node);
 	if (!bb)
 		return NULL;