From nobody Wed Jan 22 11:35:08 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 97D331C5F26; Wed, 22 Jan 2025 02:32:12 +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=1737513132; cv=none; b=fMVukgPcYV/C66Ujhymwg+PSpEx3zZXQkqERjn+pDmQroKpq9uCVMgzIiPlfV/IzdaZ8MdBJe4ahCnhD2+EX4zzJ+9DQwotOxTieCT8VxVrxKzrNiT9oOgtTvmBqq9zPV0BsfJ6bJ4ON12oTddQU1nNDWLLOSK1U1Lse3uxtBSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737513132; c=relaxed/simple; bh=mYS+Vw6Dq5D1ZdtZnJxrSTmiPbgmYZPrXZ+DGFIIgIE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bjQNBruPwaH0yYnpPQNwzlYTLHX7y+qN+5pT+68p+YjGE6xWAXSPqHklSpxn+RrjCoDGy1cQHi4jiacWvGKZ5mKFlwOoRIJ5kF8rlTx4N20bZdEzgjygPVIaMsvXA40ivmjiC8h6hLzOH/fUF3niF+6ipC8OujCx6zI4wPkSypo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SvdM798C; 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="SvdM798C" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 29A78C4CEE7; Wed, 22 Jan 2025 02:32:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1737513132; bh=mYS+Vw6Dq5D1ZdtZnJxrSTmiPbgmYZPrXZ+DGFIIgIE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SvdM798Cc3MUELCKPZiFmXeAaWElaCetp2tNu414E5MU/BSBi26/N9zN90kAwNGa2 CX2nWVjbB8IQ018lPFTlv+MeKVvi8A0KzqDKP/YRdPZVnCsWaR7Che6M57ipfNGCm7 DE9pWjMFAbYvHjayFjEssF2Z0vtenQaS+BHYTdRHUhW2zpucRuj5Es0yzitLpaPZDm wRuUA6Aq/GM8epEviWAnrBXICLFwOMfykeYMvOpvoFA1262DVyLeH1lS//kQ60LF2U hkQhDfyfgWWh/DiDcmz7OxLKcx4AZDXU4E2ati1d+pYgahepIpBhIo7h7yNF2Uxla2 tKGD73MpFGOcw== From: Josh Poimboeuf To: x86@kernel.org Cc: Peter Zijlstra , Steven Rostedt , Ingo Molnar , Arnaldo Carvalho de Melo , linux-kernel@vger.kernel.org, Indu Bhagat , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , linux-perf-users@vger.kernel.org, Mark Brown , linux-toolchains@vger.kernel.org, Jordan Rome , Sam James , linux-trace-kernel@vger.kernel.org, Andrii Nakryiko , Jens Remus , Mathieu Desnoyers , Florian Weimer , Andy Lutomirski , Masami Hiramatsu , Weinan Liu Subject: [PATCH v4 19/39] unwind_user/sframe: Add support for reading .sframe contents Date: Tue, 21 Jan 2025 18:31:11 -0800 Message-ID: <77c0d1ec143bf2a53d66c4ecb190e7e0a576fbfd.1737511963.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: References: 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" In preparation for using sframe to unwind user space stacks, add an sframe_find() interface for finding the sframe information associated with a given text address. For performance, use user_read_access_begin() and the corresponding unsafe_*() accessors. Note that use of pr_debug() in uaccess-enabled regions would break noinstr validation, so there aren't any debug messages yet. That will be added in a subsequent commit. Signed-off-by: Josh Poimboeuf --- include/linux/sframe.h | 5 + kernel/unwind/sframe.c | 295 ++++++++++++++++++++++++++++++++++- kernel/unwind/sframe_debug.h | 35 +++++ 3 files changed, 331 insertions(+), 4 deletions(-) create mode 100644 kernel/unwind/sframe_debug.h diff --git a/include/linux/sframe.h b/include/linux/sframe.h index ff4b9d1dbd00..2e70085a1e89 100644 --- a/include/linux/sframe.h +++ b/include/linux/sframe.h @@ -3,11 +3,14 @@ #define _LINUX_SFRAME_H =20 #include +#include #include =20 #ifdef CONFIG_HAVE_UNWIND_USER_SFRAME =20 struct sframe_section { + struct rcu_head rcu; + unsigned long sframe_start; unsigned long sframe_end; unsigned long text_start; @@ -28,6 +31,7 @@ extern void sframe_free_mm(struct mm_struct *mm); extern int sframe_add_section(unsigned long sframe_start, unsigned long sf= rame_end, unsigned long text_start, unsigned long text_end); extern int sframe_remove_section(unsigned long sframe_addr); +extern int sframe_find(unsigned long ip, struct unwind_user_frame *frame); =20 static inline bool current_has_sframe(void) { @@ -42,6 +46,7 @@ static inline bool current_has_sframe(void) static inline void sframe_free_mm(struct mm_struct *mm) {} static inline int sframe_add_section(unsigned long sframe_start, unsigned = long sframe_end, unsigned long text_start, unsigned long text_end) { return= -ENOSYS; } static inline int sframe_remove_section(unsigned long sframe_addr) { retur= n -ENOSYS; } +static inline int sframe_find(unsigned long ip, struct unwind_user_frame *= frame) { return -ENOSYS; } static inline bool current_has_sframe(void) { return false; } =20 #endif /* CONFIG_HAVE_UNWIND_USER_SFRAME */ diff --git a/kernel/unwind/sframe.c b/kernel/unwind/sframe.c index fa7d87ffd00a..1a35615a361e 100644 --- a/kernel/unwind/sframe.c +++ b/kernel/unwind/sframe.c @@ -15,9 +15,287 @@ #include =20 #include "sframe.h" +#include "sframe_debug.h" =20 -#define dbg(fmt, ...) \ - pr_debug("%s (%d): " fmt, current->comm, current->pid, ##__VA_ARGS__) +struct sframe_fre { + unsigned int size; + s32 ip_off; + s32 cfa_off; + s32 ra_off; + s32 fp_off; + u8 info; +}; + +DEFINE_STATIC_SRCU(sframe_srcu); + +static __always_inline unsigned char fre_type_to_size(unsigned char fre_ty= pe) +{ + if (fre_type > 2) + return 0; + return 1 << fre_type; +} + +static __always_inline unsigned char offset_size_enum_to_size(unsigned cha= r off_size) +{ + if (off_size > 2) + return 0; + return 1 << off_size; +} + +static __always_inline int __read_fde(struct sframe_section *sec, + unsigned int fde_num, + struct sframe_fde *fde) +{ + unsigned long fde_addr, ip; + + fde_addr =3D sec->fdes_start + (fde_num * sizeof(struct sframe_fde)); + unsafe_copy_from_user(fde, (void __user *)fde_addr, + sizeof(struct sframe_fde), Efault); + + ip =3D sec->sframe_start + fde->start_addr; + if (ip < sec->text_start || ip > sec->text_end) + return -EINVAL; + + return 0; + +Efault: + return -EFAULT; +} + +static __always_inline int __find_fde(struct sframe_section *sec, + unsigned long ip, + struct sframe_fde *fde) +{ + s32 ip_off, func_off_low =3D S32_MIN, func_off_high =3D S32_MAX; + struct sframe_fde __user *first, *low, *high, *found =3D NULL; + int ret; + + ip_off =3D ip - sec->sframe_start; + + first =3D (void __user *)sec->fdes_start; + low =3D first; + high =3D first + sec->num_fdes - 1; + + while (low <=3D high) { + struct sframe_fde __user *mid; + s32 func_off; + + mid =3D low + ((high - low) / 2); + + unsafe_get_user(func_off, (s32 __user *)mid, Efault); + + if (ip_off >=3D func_off) { + if (func_off < func_off_low) + return -EFAULT; + + func_off_low =3D func_off; + + found =3D mid; + low =3D mid + 1; + } else { + if (func_off > func_off_high) + return -EFAULT; + + func_off_high =3D func_off; + + high =3D mid - 1; + } + } + + if (!found) + return -EINVAL; + + ret =3D __read_fde(sec, found - first, fde); + if (ret) + return ret; + + /* make sure it's not in a gap */ + if (ip_off < fde->start_addr || ip_off >=3D fde->start_addr + fde->func_s= ize) + return -EINVAL; + + return 0; + +Efault: + return -EFAULT; +} + +#define __UNSAFE_GET_USER_INC(to, from, type, label) \ +({ \ + type __to; \ + unsafe_get_user(__to, (type __user *)from, label); \ + from +=3D sizeof(__to); \ + to =3D (typeof(to))__to; \ +}) + +#define UNSAFE_GET_USER_INC(to, from, size, label) \ +({ \ + switch (size) { \ + case 1: \ + __UNSAFE_GET_USER_INC(to, from, u8, label); \ + break; \ + case 2: \ + __UNSAFE_GET_USER_INC(to, from, u16, label); \ + break; \ + case 4: \ + __UNSAFE_GET_USER_INC(to, from, u32, label); \ + break; \ + default: \ + return -EFAULT; \ + } \ +}) + +static __always_inline int __read_fre(struct sframe_section *sec, + struct sframe_fde *fde, + unsigned long fre_addr, + struct sframe_fre *fre) +{ + unsigned char fde_type =3D SFRAME_FUNC_FDE_TYPE(fde->info); + unsigned char fre_type =3D SFRAME_FUNC_FRE_TYPE(fde->info); + unsigned char offset_count, offset_size; + s32 ip_off, cfa_off, ra_off, fp_off; + unsigned long cur =3D fre_addr; + unsigned char addr_size; + u8 info; + + addr_size =3D fre_type_to_size(fre_type); + if (!addr_size) + return -EFAULT; + + if (fre_addr + addr_size + 1 > sec->fres_end) + return -EFAULT; + + UNSAFE_GET_USER_INC(ip_off, cur, addr_size, Efault); + if (fde_type =3D=3D SFRAME_FDE_TYPE_PCINC && ip_off > fde->func_size) + return -EFAULT; + + UNSAFE_GET_USER_INC(info, cur, 1, Efault); + offset_count =3D SFRAME_FRE_OFFSET_COUNT(info); + offset_size =3D offset_size_enum_to_size(SFRAME_FRE_OFFSET_SIZE(info)); + if (!offset_count || !offset_size) + return -EFAULT; + + if (cur + (offset_count * offset_size) > sec->fres_end) + return -EFAULT; + + fre->size =3D addr_size + 1 + (offset_count * offset_size); + + UNSAFE_GET_USER_INC(cfa_off, cur, offset_size, Efault); + offset_count--; + + ra_off =3D sec->ra_off; + if (!ra_off) { + if (!offset_count--) + return -EFAULT; + + UNSAFE_GET_USER_INC(ra_off, cur, offset_size, Efault); + } + + fp_off =3D sec->fp_off; + if (!fp_off && offset_count) { + offset_count--; + UNSAFE_GET_USER_INC(fp_off, cur, offset_size, Efault); + } + + if (offset_count) + return -EFAULT; + + fre->ip_off =3D ip_off; + fre->cfa_off =3D cfa_off; + fre->ra_off =3D ra_off; + fre->fp_off =3D fp_off; + fre->info =3D info; + + return 0; + +Efault: + return -EFAULT; +} + +static __always_inline int __find_fre(struct sframe_section *sec, + struct sframe_fde *fde, unsigned long ip, + struct unwind_user_frame *frame) +{ + unsigned char fde_type =3D SFRAME_FUNC_FDE_TYPE(fde->info); + struct sframe_fre *fre, *prev_fre =3D NULL; + struct sframe_fre fres[2]; + unsigned long fre_addr; + bool which =3D false; + unsigned int i; + s32 ip_off; + + ip_off =3D (s32)(ip - sec->sframe_start) - fde->start_addr; + + if (fde_type =3D=3D SFRAME_FDE_TYPE_PCMASK) + ip_off %=3D fde->rep_size; + + fre_addr =3D sec->fres_start + fde->fres_off; + + for (i =3D 0; i < fde->fres_num; i++) { + int ret; + + /* + * Alternate between the two fre_addr[] entries for 'fre' and + * 'prev_fre'. + */ + fre =3D which ? fres : fres + 1; + which =3D !which; + + ret =3D __read_fre(sec, fde, fre_addr, fre); + if (ret) + return ret; + + fre_addr +=3D fre->size; + + if (prev_fre && fre->ip_off <=3D prev_fre->ip_off) + return -EFAULT; + + if (fre->ip_off > ip_off) + break; + + prev_fre =3D fre; + } + + if (!prev_fre) + return -EINVAL; + fre =3D prev_fre; + + frame->cfa_off =3D fre->cfa_off; + frame->ra_off =3D fre->ra_off; + frame->fp_off =3D fre->fp_off; + frame->use_fp =3D SFRAME_FRE_CFA_BASE_REG_ID(fre->info) =3D=3D SFRAME_BA= SE_REG_FP; + + return 0; +} + +int sframe_find(unsigned long ip, struct unwind_user_frame *frame) +{ + struct mm_struct *mm =3D current->mm; + struct sframe_section *sec; + struct sframe_fde fde; + int ret; + + if (!mm) + return -EINVAL; + + guard(srcu)(&sframe_srcu); + + sec =3D mtree_load(&mm->sframe_mt, ip); + if (!sec) + return -EINVAL; + + if (!user_read_access_begin((void __user *)sec->sframe_start, + sec->sframe_end - sec->sframe_start)) + return -EFAULT; + + ret =3D __find_fde(sec, ip, &fde); + if (ret) + goto end; + + ret =3D __find_fre(sec, &fde, ip, frame); +end: + user_read_access_end(); + return ret; +} =20 static void free_section(struct sframe_section *sec) { @@ -119,8 +397,10 @@ int sframe_add_section(unsigned long sframe_start, uns= igned long sframe_end, sec->text_end =3D text_end; =20 ret =3D sframe_read_header(sec); - if (ret) + if (ret) { + dbg_print_header(sec); goto err_free; + } =20 ret =3D mtree_insert_range(sframe_mt, sec->text_start, sec->text_end, sec= , GFP_KERNEL); if (ret) { @@ -136,6 +416,13 @@ int sframe_add_section(unsigned long sframe_start, uns= igned long sframe_end, return ret; } =20 +static void sframe_free_srcu(struct rcu_head *rcu) +{ + struct sframe_section *sec =3D container_of(rcu, struct sframe_section, r= cu); + + free_section(sec); +} + static int __sframe_remove_section(struct mm_struct *mm, struct sframe_section *sec) { @@ -144,7 +431,7 @@ static int __sframe_remove_section(struct mm_struct *mm, return -EINVAL; } =20 - free_section(sec); + call_srcu(&sframe_srcu, &sec->rcu, sframe_free_srcu); =20 return 0; } diff --git a/kernel/unwind/sframe_debug.h b/kernel/unwind/sframe_debug.h new file mode 100644 index 000000000000..055c8c8fae24 --- /dev/null +++ b/kernel/unwind/sframe_debug.h @@ -0,0 +1,35 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _SFRAME_DEBUG_H +#define _SFRAME_DEBUG_H + +#include +#include "sframe.h" + +#ifdef CONFIG_DYNAMIC_DEBUG + +#define dbg(fmt, ...) \ + pr_debug("%s (%d): " fmt, current->comm, current->pid, ##__VA_ARGS__) + +static __always_inline void dbg_print_header(struct sframe_section *sec) +{ + unsigned long fdes_end; + + fdes_end =3D sec->fdes_start + (sec->num_fdes * sizeof(struct sframe_fde)= ); + + dbg("SEC: sframe:0x%lx-0x%lx text:0x%lx-0x%lx " + "fdes:0x%lx-0x%lx fres:0x%lx-0x%lx " + "ra_off:%d fp_off:%d\n", + sec->sframe_start, sec->sframe_end, sec->text_start, sec->text_end, + sec->fdes_start, fdes_end, sec->fres_start, sec->fres_end, + sec->ra_off, sec->fp_off); +} + +#else /* !CONFIG_DYNAMIC_DEBUG */ + +#define dbg(args...) no_printk(args) + +static inline void dbg_print_header(struct sframe_section *sec) {} + +#endif /* !CONFIG_DYNAMIC_DEBUG */ + +#endif /* _SFRAME_DEBUG_H */ --=20 2.48.1