From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754063Ab0CQKHa (ORCPT ); Wed, 17 Mar 2010 06:07:30 -0400 Received: from mail-ww0-f46.google.com ([74.125.82.46]:63778 "EHLO mail-ww0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754011Ab0CQKH0 (ORCPT ); Wed, 17 Mar 2010 06:07:26 -0400 DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :mime-version:content-type:content-transfer-encoding; b=xYPaMsoG2sW/5LGjX4HB+bKQe6but7L3zRNwq4ogFU7Ybi4glQ/on0JnutbGTr3rqL RmebzI4S8LIWL9NaEf51vQirRd2Xc1chEnryN61U2wCCFez0+XPMjrCLkschddO7U9Hz dlKKkEGtKAsXTCFNRfnWa1Q8ggLdK8HePLt/Q= From: =?UTF-8?q?T=C3=B6r=C3=B6k=20Edwin?= To: Ingo Molnar Cc: =?UTF-8?q?T=C3=B6r=C3=B6k=20Edwin?= , "H. Peter Anvin" , Frederic Weisbecker , Ingo Molnar , Thomas Gleixner , Peter Zijlstra , Paul Mackerras , x86@kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] perf: x86: fix callgraphs of 32-bit processes on 64-bit kernels V4. Date: Wed, 17 Mar 2010 12:07:16 +0200 Message-Id: <1268820436-13145-1-git-send-email-edwintorok@gmail.com> X-Mailer: git-send-email 1.7.0 In-Reply-To: <1268815776-29501-1-git-send-email-edwintorok@gmail.com> References: <1268815776-29501-1-git-send-email-edwintorok@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When profiling a 32-bit process on a 64-bit kernel, callgraph tracing stopped after the first function, because it has seen a garbage memory address (tried to interpret the frame pointer, and return address as a 64-bit pointer). Fix this by using a struct stack_frame with 32-bit pointers when the TIF_IA32 flag is set. Note that TIF_IA32 flag must be used, and not is_compat_task(), because the latter is only set when the 32-bit process is executing a syscall, which may not always be the case (when tracing page fault events for example). Cc: "H. Peter Anvin" Cc: Frederic Weisbecker Cc: Ingo Molnar Cc: Thomas Gleixner Cc: Peter Zijlstra Cc: Paul Mackerras Cc: x86@kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Török Edwin --- arch/x86/kernel/cpu/perf_event.c | 44 +++++++++++++++++++++++++++++++++---- arch/x86/kernel/dumpstack.h | 5 ++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/cpu/perf_event.c b/arch/x86/kernel/cpu/perf_event.c index 8c1c070..5f97f29 100644 --- a/arch/x86/kernel/cpu/perf_event.c +++ b/arch/x86/kernel/cpu/perf_event.c @@ -26,6 +26,7 @@ #include #include #include +#include static u64 perf_event_mask __read_mostly; @@ -2392,14 +2393,42 @@ copy_from_user_nmi(void *to, const void __user *from, unsigned long n) return len; } -static int copy_stack_frame(const void __user *fp, struct stack_frame *frame) +#ifdef CONFIG_COMPAT +static inline int +perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) { - unsigned long bytes; + /* 32-bit process in 64-bit kernel. */ + struct stack_frame_ia32 frame; + const void __user *fp; + + if (!test_thread_flag(TIF_IA32)) + return 0; + + fp = compat_ptr(regs->bp); + while (entry->nr < PERF_MAX_STACK_DEPTH) { + unsigned long bytes; + frame.next_frame = 0; + frame.return_address = 0; - bytes = copy_from_user_nmi(frame, fp, sizeof(*frame)); + bytes = copy_from_user_nmi(&frame, fp, sizeof(frame)); + if (bytes != sizeof(frame)) + break; + + if (fp < compat_ptr(regs->sp)) + break; - return bytes == sizeof(*frame); + callchain_store(entry, frame.return_address); + fp = compat_ptr(frame.next_frame); + } + return 1; } +#else +static inline int +perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry) +{ + return 0; +} +#endif static void perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry) @@ -2415,11 +2444,16 @@ perf_callchain_user(struct pt_regs *regs, struct perf_callchain_entry *entry) callchain_store(entry, PERF_CONTEXT_USER); callchain_store(entry, regs->ip); + if (perf_callchain_user32(regs, entry)) + return; + while (entry->nr < PERF_MAX_STACK_DEPTH) { + unsigned long bytes; frame.next_frame = NULL; frame.return_address = 0; - if (!copy_stack_frame(fp, &frame)) + bytes = copy_from_user_nmi(&frame, fp, sizeof(frame)); + if (bytes != sizeof(frame)) break; if ((unsigned long)fp < regs->sp) diff --git a/arch/x86/kernel/dumpstack.h b/arch/x86/kernel/dumpstack.h index 4fd1420..3f0a242 100644 --- a/arch/x86/kernel/dumpstack.h +++ b/arch/x86/kernel/dumpstack.h @@ -29,4 +29,9 @@ struct stack_frame { struct stack_frame *next_frame; unsigned long return_address; }; + +struct stack_frame_ia32 { + u32 next_frame; + u32 return_address; +}; #endif -- 1.7.0