linux-trace-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jens Remus <jremus@linux.ibm.com>
To: rostedt@goodmis.org
Cc: aahringo@redhat.com, acme@kernel.org, adrian.hunter@intel.com,
	akpm@linux-foundation.org, alexander.shishkin@linux.intel.com,
	andrii.nakryiko@gmail.com, beaub@linux.microsoft.com,
	blakejones@google.com, broonie@kernel.org, fweimer@redhat.com,
	indu.bhagat@oracle.com, irogers@google.com, jemarch@gnu.org,
	jolsa@kernel.org, jordalgo@meta.com, jpoimboe@kernel.org,
	jremus@linux.ibm.com, linux-kernel@vger.kernel.org,
	linux-perf-users@vger.kernel.org,
	linux-toolchains@vger.kernel.org,
	linux-trace-kernel@vger.kernel.org, luto@kernel.org,
	mark.rutland@arm.com, mathieu.desnoyers@efficios.com,
	mhiramat@kernel.org, mingo@kernel.org, namhyung@kernel.org,
	peterz@infradead.org, sam@gentoo.org, wnliu@google.com,
	x86@kernel.org, hca@linux.ibm.com
Subject: [PATCH] fixup! unwind_user/sframe: Add support for reading .sframe contents
Date: Wed, 28 May 2025 12:26:55 +0200	[thread overview]
Message-ID: <20250528102655.1455423-1-jremus@linux.ibm.com> (raw)
In-Reply-To: <20250424201823.595811743@goodmis.org>

---

Notes (jremus):
    Link: https://lore.kernel.org/all/b35ca3a3-8de5-4d32-8d30-d4e562f6b0de@linux.ibm.com/
    
    The struct sframe_fre field ip_off must be u32, as the SFrame FRE start
    address (sfre_start_address) is unsigned 8-bit, 16-bit, or 32-bit:
    https://sourceware.org/binutils/docs/sframe-spec.html#SFrame-Frame-Row-Entries
    
    When searching for a FRE of a FDE for an IP, the IP offset from function
    start address (ip_off = ip - (sec->sframe_start + fde->start_addr)) is
    always positive, as the search for a FDE for the same IP returned a
    FDE with:  sec->sframe_start + fde->start_addr <= ip
    This enables comparison against the unsigned FDE ip_off.
    
    This fixup includes a proposed fix from Josh (with minor modification
    due to duplicate macro names) to correctly perform sign extension when
    reading (un-)signed SFrame FDE/FRE fields:
    https://lore.kernel.org/all/20250207210614.nks6bxad4jhdulwg@jpoimboe/

 kernel/unwind/sframe.c | 38 +++++++++++++++++++++++++++-----------
 1 file changed, 27 insertions(+), 11 deletions(-)

diff --git a/kernel/unwind/sframe.c b/kernel/unwind/sframe.c
index 3f7cc9bc27eb..8804ac59edfa 100644
--- a/kernel/unwind/sframe.c
+++ b/kernel/unwind/sframe.c
@@ -19,7 +19,7 @@
 
 struct sframe_fre {
 	unsigned int	size;
-	s32		ip_off;
+	u32		ip_off;
 	s32		cfa_off;
 	s32		ra_off;
 	s32		fp_off;
@@ -129,33 +129,48 @@ static __always_inline int __find_fde(struct sframe_section *sec,
 	return -EFAULT;
 }
 
-#define __UNSAFE_GET_USER_INC(to, from, type, label)			\
+#define ____UNSAFE_GET_USER_INC(to, from, type, label)			\
 ({									\
 	type __to;							\
 	unsafe_get_user(__to, (type __user *)from, label);		\
 	from += sizeof(__to);						\
-	to = (typeof(to))__to;							\
+	to = __to;							\
 })
 
-#define UNSAFE_GET_USER_INC(to, from, size, label)			\
+#define __UNSAFE_GET_USER_INC(to, from, size, label, u_or_s)		\
 ({									\
 	switch (size) {							\
 	case 1:								\
-		__UNSAFE_GET_USER_INC(to, from, u8, label);		\
+		____UNSAFE_GET_USER_INC(to, from, u_or_s##8, label);	\
 		break;							\
 	case 2:								\
-		__UNSAFE_GET_USER_INC(to, from, u16, label);		\
+		____UNSAFE_GET_USER_INC(to, from, u_or_s##16, label);	\
 		break;							\
 	case 4:								\
-		__UNSAFE_GET_USER_INC(to, from, u32, label);		\
+		____UNSAFE_GET_USER_INC(to, from, u_or_s##32, label);	\
 		break;							\
 	default:							\
-		dbg_sec_uaccess("%d: bad UNSAFE_GET_USER_INC size %u\n",\
+		dbg_sec_uaccess("%d: bad unsafe_get_user() size %u\n",	\
 				__LINE__, size);			\
 		return -EFAULT;						\
 	}								\
 })
 
+#define UNSAFE_GET_USER_UNSIGNED_INC(to, from, size, label)		\
+	__UNSAFE_GET_USER_INC(to, from, size, label, u)
+
+#define UNSAFE_GET_USER_SIGNED_INC(to, from, size, label)		\
+	__UNSAFE_GET_USER_INC(to, from, size, label, s)
+
+#define UNSAFE_GET_USER_INC(to, from, size, label)				\
+	_Generic(to,								\
+		 u8:	UNSAFE_GET_USER_UNSIGNED_INC(to, from, size, label),	\
+		 u16:	UNSAFE_GET_USER_UNSIGNED_INC(to, from, size, label),	\
+		 u32:	UNSAFE_GET_USER_UNSIGNED_INC(to, from, size, label),	\
+		 s8:	UNSAFE_GET_USER_SIGNED_INC(to, from, size, label),	\
+		 s16:	UNSAFE_GET_USER_SIGNED_INC(to, from, size, label),	\
+		 s32:	UNSAFE_GET_USER_SIGNED_INC(to, from, size, label))
+
 static __always_inline int __read_fre(struct sframe_section *sec,
 				      struct sframe_fde *fde,
 				      unsigned long fre_addr,
@@ -164,7 +179,8 @@ static __always_inline int __read_fre(struct sframe_section *sec,
 	unsigned char fde_type = SFRAME_FUNC_FDE_TYPE(fde->info);
 	unsigned char fre_type = SFRAME_FUNC_FRE_TYPE(fde->info);
 	unsigned char offset_count, offset_size;
-	s32 ip_off, cfa_off, ra_off, fp_off;
+	u32 ip_off;
+	s32 cfa_off, ra_off, fp_off;
 	unsigned long cur = fre_addr;
 	unsigned char addr_size;
 	u8 info;
@@ -248,9 +264,9 @@ static __always_inline int __find_fre(struct sframe_section *sec,
 	unsigned long fre_addr;
 	bool which = false;
 	unsigned int i;
-	s32 ip_off;
+	u32 ip_off;
 
-	ip_off = (s32)(ip - sec->sframe_start) - fde->start_addr;
+	ip_off = ip - (sec->sframe_start + fde->start_addr);
 
 	if (fde_type == SFRAME_FDE_TYPE_PCMASK)
 		ip_off %= fde->rep_size;
-- 
2.45.2


  reply	other threads:[~2025-05-28 10:27 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-24 20:15 [PATCH v5 00/12] unwind_deferred: Implement sframe handling Steven Rostedt
2025-04-24 20:15 ` [PATCH v5 01/12] unwind_user/sframe: Add support for reading .sframe headers Steven Rostedt
2025-05-28 10:26   ` Jens Remus [this message]
2025-04-24 20:15 ` [PATCH v5 02/12] unwind_user/sframe: Store sframe section data in per-mm maple tree Steven Rostedt
2025-04-24 20:15 ` [PATCH v5 03/12] x86/uaccess: Add unsafe_copy_from_user() implementation Steven Rostedt
2025-04-24 20:15 ` [PATCH v5 04/12] unwind_user/sframe: Add support for reading .sframe contents Steven Rostedt
2025-04-24 20:15 ` [PATCH v5 05/12] unwind_user/sframe: Detect .sframe sections in executables Steven Rostedt
2025-04-24 20:15 ` [PATCH v5 06/12] unwind_user/sframe: Add prctl() interface for registering .sframe sections Steven Rostedt
2025-04-28  9:00   ` Jens Remus
2025-04-24 20:15 ` [PATCH v5 07/12] unwind_user/sframe: Wire up unwind_user to sframe Steven Rostedt
2025-04-24 20:15 ` [PATCH v5 08/12] unwind_user/sframe/x86: Enable sframe unwinding on x86 Steven Rostedt
2025-04-24 20:15 ` [PATCH v5 09/12] unwind_user/sframe: Remove .sframe section on detected corruption Steven Rostedt
2025-04-24 20:15 ` [PATCH v5 10/12] unwind_user/sframe: Show file name in debug output Steven Rostedt
2025-04-24 20:15 ` [PATCH v5 11/12] unwind_user/sframe: Enable debugging in uaccess regions Steven Rostedt
2025-04-24 20:15 ` [PATCH v5 12/12] unwind_user/sframe: Add .sframe validation option Steven Rostedt
2025-04-24 20:28 ` [PATCH v5 00/12] unwind_deferred: Implement sframe handling Steven Rostedt

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=20250528102655.1455423-1-jremus@linux.ibm.com \
    --to=jremus@linux.ibm.com \
    --cc=aahringo@redhat.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=beaub@linux.microsoft.com \
    --cc=blakejones@google.com \
    --cc=broonie@kernel.org \
    --cc=fweimer@redhat.com \
    --cc=hca@linux.ibm.com \
    --cc=indu.bhagat@oracle.com \
    --cc=irogers@google.com \
    --cc=jemarch@gnu.org \
    --cc=jolsa@kernel.org \
    --cc=jordalgo@meta.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-toolchains@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=sam@gentoo.org \
    --cc=wnliu@google.com \
    --cc=x86@kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).