From nobody Tue Oct 7 21:52:24 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 F331B238C2C; Fri, 9 May 2025 16:51:38 +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=1746809499; cv=none; b=QOqxvpwrofoB+doGYefwH5C5qxBouuwifdpX1eUF+vcoQ8cdCKRnNByFYUVg3ubaZglK8Fe5n+0AkEGjgMlgfSSPt3snkv3TP6oYtnJWvJDBkAheg+HWFJj0ya4zaD9ge5N7jeuj2ft7NJKVgPfu5eE402DpMinW8ARvJ5neerI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746809499; c=relaxed/simple; bh=JTFH4J/aPfmXUm6zG2FJR1h1yEhf4M7031l6ypEpMj0=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=psf4qDXPKpSwKnNGJ1Sz5cBmPDU/PFsXLzLaXcUeaMzAKRuXYYoJFbIBm/NcUsPt6mKTC++kfVR3WLGkmbm6wTatmDwiIf8qifKXZ3fmrP/i7kpUQSx0JRwPgPN39BGiS/rPIHHntK7EZ4zv2i7DwHJu5F9jPfQ8iEtQioeNqWM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id A34D5C4CEE9; Fri, 9 May 2025 16:51:38 +0000 (UTC) Received: from rostedt by gandalf with local (Exim 4.98.2) (envelope-from ) id 1uDQx0-00000002gFC-1plN; Fri, 09 May 2025 12:51:54 -0400 Message-ID: <20250509165154.287839165@goodmis.org> User-Agent: quilt/0.68 Date: Fri, 09 May 2025 12:45:28 -0400 From: Steven Rostedt To: linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, bpf@vger.kernel.org, x86@kernel.org Cc: Masami Hiramatsu , Mathieu Desnoyers , Josh Poimboeuf , Peter Zijlstra , Ingo Molnar , Jiri Olsa , Namhyung Kim Subject: [PATCH v8 04/18] perf/x86: Rename and move get_segment_base() and make it global References: <20250509164524.448387100@goodmis.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: Josh Poimboeuf get_segment_base() will be used by the unwind_user code, so make it global and rename it so it doesn't conflict with a KVM function of the same name. As the function is no longer specific to perf, move it to ptrace.c as that seems to be a better location for a generic function like this. Signed-off-by: Josh Poimboeuf Signed-off-by: Steven Rostedt (Google) --- arch/x86/events/core.c | 44 ++++------------------------------- arch/x86/include/asm/ptrace.h | 2 ++ arch/x86/kernel/ptrace.c | 38 ++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 39 deletions(-) diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c index 2e10dcf897c5..cc6329235b68 100644 --- a/arch/x86/events/core.c +++ b/arch/x86/events/core.c @@ -43,6 +43,7 @@ #include #include #include +#include #include =20 #include "perf_event.h" @@ -2809,41 +2810,6 @@ valid_user_frame(const void __user *fp, unsigned lon= g size) return __access_ok(fp, size); } =20 -static unsigned long get_segment_base(unsigned int segment) -{ - struct desc_struct *desc; - unsigned int idx =3D segment >> 3; - - if ((segment & SEGMENT_TI_MASK) =3D=3D SEGMENT_LDT) { -#ifdef CONFIG_MODIFY_LDT_SYSCALL - struct ldt_struct *ldt; - - /* - * If we're not in a valid context with a real (not just lazy) - * user mm, then don't even try. - */ - if (!nmi_uaccess_okay()) - return 0; - - /* IRQs are off, so this synchronizes with smp_store_release */ - ldt =3D smp_load_acquire(¤t->mm->context.ldt); - if (!ldt || idx >=3D ldt->nr_entries) - return 0; - - desc =3D &ldt->entries[idx]; -#else - return 0; -#endif - } else { - if (idx >=3D GDT_ENTRIES) - return 0; - - desc =3D raw_cpu_ptr(gdt_page.gdt) + idx; - } - - return get_desc_base(desc); -} - #ifdef CONFIG_UPROBES /* * Heuristic-based check if uprobe is installed at the function entry. @@ -2900,8 +2866,8 @@ perf_callchain_user32(struct pt_regs *regs, struct pe= rf_callchain_entry_ctx *ent if (user_64bit_mode(regs)) return 0; =20 - cs_base =3D get_segment_base(regs->cs); - ss_base =3D get_segment_base(regs->ss); + cs_base =3D segment_base_address(regs->cs); + ss_base =3D segment_base_address(regs->ss); =20 fp =3D compat_ptr(ss_base + regs->bp); pagefault_disable(); @@ -3020,11 +2986,11 @@ static unsigned long code_segment_base(struct pt_re= gs *regs) return 0x10 * regs->cs; =20 if (user_mode(regs) && regs->cs !=3D __USER_CS) - return get_segment_base(regs->cs); + return segment_base_address(regs->cs); #else if (user_mode(regs) && !user_64bit_mode(regs) && regs->cs !=3D __USER32_CS) - return get_segment_base(regs->cs); + return segment_base_address(regs->cs); #endif return 0; } diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h index 50f75467f73d..59357ec98e52 100644 --- a/arch/x86/include/asm/ptrace.h +++ b/arch/x86/include/asm/ptrace.h @@ -314,6 +314,8 @@ static __always_inline bool regs_irqs_disabled(struct p= t_regs *regs) return !(regs->flags & X86_EFLAGS_IF); } =20 +unsigned long segment_base_address(unsigned int segment); + /* Query offset/name of register from its name/offset */ extern int regs_query_register_offset(const char *name); extern const char *regs_query_register_name(unsigned int offset); diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c index 095f04bdabdc..81353a09701b 100644 --- a/arch/x86/kernel/ptrace.c +++ b/arch/x86/kernel/ptrace.c @@ -41,6 +41,7 @@ #include #include #include +#include =20 #include "tls.h" =20 @@ -339,6 +340,43 @@ static int set_segment_reg(struct task_struct *task, =20 #endif /* CONFIG_X86_32 */ =20 +unsigned long segment_base_address(unsigned int segment) +{ + struct desc_struct *desc; + unsigned int idx =3D segment >> 3; + + lockdep_assert_irqs_disabled(); + + if ((segment & SEGMENT_TI_MASK) =3D=3D SEGMENT_LDT) { +#ifdef CONFIG_MODIFY_LDT_SYSCALL + struct ldt_struct *ldt; + + /* + * If we're not in a valid context with a real (not just lazy) + * user mm, then don't even try. + */ + if (!nmi_uaccess_okay()) + return 0; + + /* IRQs are off, so this synchronizes with smp_store_release */ + ldt =3D smp_load_acquire(¤t->mm->context.ldt); + if (!ldt || idx >=3D ldt->nr_entries) + return 0; + + desc =3D &ldt->entries[idx]; +#else + return 0; +#endif + } else { + if (idx >=3D GDT_ENTRIES) + return 0; + + desc =3D raw_cpu_ptr(gdt_page.gdt) + idx; + } + + return get_desc_base(desc); +} + static unsigned long get_flags(struct task_struct *task) { unsigned long retval =3D task_pt_regs(task)->flags; --=20 2.47.2