From nobody Mon Dec 1 22:00:25 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7E24323F41A for ; Fri, 28 Nov 2025 01:23:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764293003; cv=none; b=eURWgI/uCXeGfRGs2SqzLItDid6w86S/BmuaInJyG0fysbu14qlTbjuwtj5N1e7QORaXTX4ktOlAKHy3oAmfpgfhDMOOZUHwlEKEV3K6ih+3es0d7KfxYFnEGi8k5laS2rEpLH1H4N3JpHE0Lx/kx0+L+ChFoKzJ20piFGsP7uY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764293003; c=relaxed/simple; bh=Y25yl4jusVZJq50/Sr3d2jWXTo9Xt80Kh5nQnCdqQso=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=RDjjanUIToxyKV6Jyu/pPQL22VjW620VB8o4OcZysswRONWYV2h21uM3jpAbFvUQsjkPph26xiEt88eyLenf58xWvKnD9n8md9RVec5xR6v/0qLJluBwBIs7vdWcfN9myMH5eOEtk1r4VTxwCfgwBMAcWWp3CWTdJY4OSOjBKRc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ck7/XNxL; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Ck7/XNxL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5F957C116C6; Fri, 28 Nov 2025 01:23:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1764293003; bh=Y25yl4jusVZJq50/Sr3d2jWXTo9Xt80Kh5nQnCdqQso=; h=Date:From:To:Cc:Subject:References:From; b=Ck7/XNxLQ936DOxMIKyrTXQzVysP2jWlGaKIYfT/YF53i7fSYKIVyzJElfJzoqZxL nOa4dgSwVDVzMuDk9rcbvY7S1IwhPcXKVtQDmj7PO//lA53dvVgUmkuxbOO0ulSjbl a6bC9w1DG0dywniFMoYebmgRZd7jv5H6u0ECFUYBQaq5kOEGggVhI8abkYBjZAeLwR ilF0SyHoixKLwpwLq2p4c+4YM3oFS7JvqRvkGbkAzSsmnJb5nj8ExGBU8hPNsKkpf3 84h+zVELBLUckkUwsdgMn6gtZ5/E2M6UeH3165fgx3iZqr1KjlsNy6mevtTraiXcLJ a2gD1lHjD0XjQ== Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1vOnDZ-00000006gjk-0cxI; Thu, 27 Nov 2025 20:24:13 -0500 Message-ID: <20251128012413.003751279@kernel.org> User-Agent: quilt/0.68 Date: Thu, 27 Nov 2025 20:23:44 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Masami Hiramatsu , Mark Rutland , Mathieu Desnoyers , Andrew Morton , "Gustavo A. R. Silva" , Linus Torvalds , Kees Cook Subject: [for-next][PATCH 13/13] overflow: Introduce struct_offset() to get offset of member References: <20251128012331.307010654@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Steven Rostedt The trace_marker_raw file in tracefs takes a buffer from user space that contains an id as well as a raw data string which is usually a binary structure. The structure used has the following: struct raw_data_entry { struct trace_entry ent; unsigned int id; char buf[]; }; Since the passed in "cnt" variable is both the size of buf as well as the size of id, the code to allocate the location on the ring buffer had: size =3D struct_size(entry, buf, cnt - sizeof(entry->id)); Which is quite ugly and hard to understand. Instead, add a helper macro called struct_offset() which then changes the above to a simple and easy to understand: size =3D struct_offset(entry, id) + cnt; This will likely come in handy for other use cases too. Link: https://lore.kernel.org/all/CAHk-=3DwhYZVoEdfO1PmtbirPdBMTV9Nxt9f09CK= 0k6S+HJD3Zmg@mail.gmail.com/ Cc: Masami Hiramatsu Cc: Mathieu Desnoyers Cc: "Gustavo A. R. Silva" Link: https://patch.msgid.link/20251126145249.05b1770a@gandalf.local.home Suggested-by: Linus Torvalds Reviewed-by: Kees Cook Signed-off-by: Steven Rostedt (Google) --- include/linux/overflow.h | 12 ++++++++++++ kernel/trace/trace.c | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/linux/overflow.h b/include/linux/overflow.h index 725f95f7e416..736f633b2d5f 100644 --- a/include/linux/overflow.h +++ b/include/linux/overflow.h @@ -458,6 +458,18 @@ static inline size_t __must_check size_sub(size_t minu= end, size_t subtrahend) #define struct_size_t(type, member, count) \ struct_size((type *)NULL, member, count) =20 +/** + * struct_offset() - Calculate the offset of a member within a struct + * @p: Pointer to the struct + * @member: Name of the member to get the offset of + * + * Calculates the offset of a particular @member of the structure pointed + * to by @p. + * + * Return: number of bytes to the location of @member. + */ +#define struct_offset(p, member) (offsetof(typeof(*(p)), member)) + /** * __DEFINE_FLEX() - helper macro for DEFINE_FLEX() family. * Enables caller macro to pass arbitrary trailing expressions diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 73f8b79f1b0c..3d433a426e5f 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -7642,7 +7642,7 @@ static ssize_t write_raw_marker_to_buffer(struct trac= e_array *tr, size_t size; =20 /* cnt includes both the entry->id and the data behind it. */ - size =3D struct_size(entry, buf, cnt - sizeof(entry->id)); + size =3D struct_offset(entry, id) + cnt; =20 buffer =3D tr->array_buffer.buffer; =20 --=20 2.51.0