Skip to content

Commit d07bdfd

Browse files
Peter Zijlstraingomolnar
authored andcommitted
perf/x86: Fix USER/KERNEL tagging of samples properly
Some PMUs don't provide a full register set for their sample, specifically 'advanced' PMUs like AMD IBS and Intel PEBS which provide 'better' than regular interrupt accuracy. In this case we use the interrupt regs as basis and over-write some fields (typically IP) with different information. The perf core however uses user_mode() to distinguish user/kernel samples, user_mode() relies on regs->cs. If the interrupt skid pushed us over a boundary the new IP might not be in the same domain as the interrupt. Commit ce5c1fe ("perf/x86: Fix USER/KERNEL tagging of samples") tried to fix this by making the perf core use kernel_ip(). This however is wrong (TM), as pointed out by Linus, since it doesn't allow for VM86 and non-zero based segments in IA32 mode. Therefore, provide a new helper to set the regs->ip field, set_linear_ip(), which massages the regs into a suitable state assuming the provided IP is in fact a linear address. Also modify perf_instruction_pointer() and perf_callchain_user() to deal with segments base offsets. Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/1341910954.3462.102.camel@twins Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 7740dfc commit d07bdfd

5 files changed

Lines changed: 114 additions & 17 deletions

File tree

‎arch/x86/include/asm/perf_event.h‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,16 @@ static inline u32 get_ibs_caps(void) { return 0; }
196196
extern void perf_events_lapic_init(void);
197197

198198
/*
199-
* Abuse bit 3 of the cpu eflags register to indicate proper PEBS IP fixups.
200-
* This flag is otherwise unused and ABI specified to be 0, so nobody should
201-
* care what we do with it.
199+
* Abuse bits {3,5} of the cpu eflags register. These flags are otherwise
200+
* unused and ABI specified to be 0, so nobody should care what we do with
201+
* them.
202+
*
203+
* EXACT - the IP points to the exact instruction that triggered the
204+
* event (HW bugs exempt).
205+
* VM - original X86_VM_MASK; see set_linear_ip().
202206
*/
203207
#define PERF_EFLAGS_EXACT (1UL << 3)
208+
#define PERF_EFLAGS_VM (1UL << 5)
204209

205210
struct pt_regs;
206211
extern unsigned long perf_instruction_pointer(struct pt_regs *regs);

‎arch/x86/kernel/cpu/perf_event.c‎

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include <asm/smp.h>
3333
#include <asm/alternative.h>
3434
#include <asm/timer.h>
35+
#include <asm/desc.h>
36+
#include <asm/ldt.h>
3537

3638
#include "perf_event.h"
3739

@@ -1738,6 +1740,29 @@ valid_user_frame(const void __user *fp, unsigned long size)
17381740
return (__range_not_ok(fp, size, TASK_SIZE) == 0);
17391741
}
17401742

1743+
static unsigned long get_segment_base(unsigned int segment)
1744+
{
1745+
struct desc_struct *desc;
1746+
int idx = segment >> 3;
1747+
1748+
if ((segment & SEGMENT_TI_MASK) == SEGMENT_LDT) {
1749+
if (idx > LDT_ENTRIES)
1750+
return 0;
1751+
1752+
if (idx > current->active_mm->context.size)
1753+
return 0;
1754+
1755+
desc = current->active_mm->context.ldt;
1756+
} else {
1757+
if (idx > GDT_ENTRIES)
1758+
return 0;
1759+
1760+
desc = __this_cpu_ptr(&gdt_page.gdt[0]);
1761+
}
1762+
1763+
return get_desc_base(desc + idx);
1764+
}
1765+
17411766
#ifdef CONFIG_COMPAT
17421767

17431768
#include <asm/compat.h>
@@ -1746,13 +1771,17 @@ static inline int
17461771
perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
17471772
{
17481773
/* 32-bit process in 64-bit kernel. */
1774+
unsigned long ss_base, cs_base;
17491775
struct stack_frame_ia32 frame;
17501776
const void __user *fp;
17511777

17521778
if (!test_thread_flag(TIF_IA32))
17531779
return 0;
17541780

1755-
fp = compat_ptr(regs->bp);
1781+
cs_base = get_segment_base(regs->cs);
1782+
ss_base = get_segment_base(regs->ss);
1783+
1784+
fp = compat_ptr(ss_base + regs->bp);
17561785
while (entry->nr < PERF_MAX_STACK_DEPTH) {
17571786
unsigned long bytes;
17581787
frame.next_frame = 0;
@@ -1765,8 +1794,8 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry *entry)
17651794
if (!valid_user_frame(fp, sizeof(frame)))
17661795
break;
17671796

1768-
perf_callchain_store(entry, frame.return_address);
1769-
fp = compat_ptr(frame.next_frame);
1797+
perf_callchain_store(entry, cs_base + frame.return_address);
1798+
fp = compat_ptr(ss_base + frame.next_frame);
17701799
}
17711800
return 1;
17721801
}
@@ -1789,6 +1818,12 @@ perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
17891818
return;
17901819
}
17911820

1821+
/*
1822+
* We don't know what to do with VM86 stacks.. ignore them for now.
1823+
*/
1824+
if (regs->flags & (X86_VM_MASK | PERF_EFLAGS_VM))
1825+
return;
1826+
17921827
fp = (void __user *)regs->bp;
17931828

17941829
perf_callchain_store(entry, regs->ip);
@@ -1816,16 +1851,50 @@ perf_callchain_user(struct perf_callchain_entry *entry, struct pt_regs *regs)
18161851
}
18171852
}
18181853

1819-
unsigned long perf_instruction_pointer(struct pt_regs *regs)
1854+
/*
1855+
* Deal with code segment offsets for the various execution modes:
1856+
*
1857+
* VM86 - the good olde 16 bit days, where the linear address is
1858+
* 20 bits and we use regs->ip + 0x10 * regs->cs.
1859+
*
1860+
* IA32 - Where we need to look at GDT/LDT segment descriptor tables
1861+
* to figure out what the 32bit base address is.
1862+
*
1863+
* X32 - has TIF_X32 set, but is running in x86_64
1864+
*
1865+
* X86_64 - CS,DS,SS,ES are all zero based.
1866+
*/
1867+
static unsigned long code_segment_base(struct pt_regs *regs)
18201868
{
1821-
unsigned long ip;
1869+
/*
1870+
* If we are in VM86 mode, add the segment offset to convert to a
1871+
* linear address.
1872+
*/
1873+
if (regs->flags & X86_VM_MASK)
1874+
return 0x10 * regs->cs;
1875+
1876+
/*
1877+
* For IA32 we look at the GDT/LDT segment base to convert the
1878+
* effective IP to a linear address.
1879+
*/
1880+
#ifdef CONFIG_X86_32
1881+
if (user_mode(regs) && regs->cs != __USER_CS)
1882+
return get_segment_base(regs->cs);
1883+
#else
1884+
if (test_thread_flag(TIF_IA32)) {
1885+
if (user_mode(regs) && regs->cs != __USER32_CS)
1886+
return get_segment_base(regs->cs);
1887+
}
1888+
#endif
1889+
return 0;
1890+
}
18221891

1892+
unsigned long perf_instruction_pointer(struct pt_regs *regs)
1893+
{
18231894
if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
1824-
ip = perf_guest_cbs->get_guest_ip();
1825-
else
1826-
ip = instruction_pointer(regs);
1895+
return perf_guest_cbs->get_guest_ip();
18271896

1828-
return ip;
1897+
return regs->ip + code_segment_base(regs);
18291898
}
18301899

18311900
unsigned long perf_misc_flags(struct pt_regs *regs)
@@ -1838,7 +1907,7 @@ unsigned long perf_misc_flags(struct pt_regs *regs)
18381907
else
18391908
misc |= PERF_RECORD_MISC_GUEST_KERNEL;
18401909
} else {
1841-
if (!kernel_ip(regs->ip))
1910+
if (user_mode(regs))
18421911
misc |= PERF_RECORD_MISC_USER;
18431912
else
18441913
misc |= PERF_RECORD_MISC_KERNEL;

‎arch/x86/kernel/cpu/perf_event.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,26 @@ static inline bool kernel_ip(unsigned long ip)
516516
#endif
517517
}
518518

519+
/*
520+
* Not all PMUs provide the right context information to place the reported IP
521+
* into full context. Specifically segment registers are typically not
522+
* supplied.
523+
*
524+
* Assuming the address is a linear address (it is for IBS), we fake the CS and
525+
* vm86 mode using the known zero-based code segment and 'fix up' the registers
526+
* to reflect this.
527+
*
528+
* Intel PEBS/LBR appear to typically provide the effective address, nothing
529+
* much we can do about that but pray and treat it like a linear address.
530+
*/
531+
static inline void set_linear_ip(struct pt_regs *regs, unsigned long ip)
532+
{
533+
regs->cs = kernel_ip(ip) ? __KERNEL_CS : __USER_CS;
534+
if (regs->flags & X86_VM_MASK)
535+
regs->flags ^= (PERF_EFLAGS_VM | X86_VM_MASK);
536+
regs->ip = ip;
537+
}
538+
519539
#ifdef CONFIG_CPU_SUP_AMD
520540

521541
int amd_pmu_init(void);

‎arch/x86/kernel/cpu/perf_event_amd_ibs.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
#include <asm/apic.h>
1515

16+
#include "perf_event.h"
17+
1618
static u32 ibs_caps;
1719

1820
#if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_AMD)
@@ -536,7 +538,7 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
536538
if (check_rip && (ibs_data.regs[2] & IBS_RIP_INVALID)) {
537539
regs.flags &= ~PERF_EFLAGS_EXACT;
538540
} else {
539-
instruction_pointer_set(&regs, ibs_data.regs[1]);
541+
set_linear_ip(&regs, ibs_data.regs[1]);
540542
regs.flags |= PERF_EFLAGS_EXACT;
541543
}
542544

‎arch/x86/kernel/cpu/perf_event_intel_ds.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
499499
* We sampled a branch insn, rewind using the LBR stack
500500
*/
501501
if (ip == to) {
502-
regs->ip = from;
502+
set_linear_ip(regs, from);
503503
return 1;
504504
}
505505

@@ -529,7 +529,7 @@ static int intel_pmu_pebs_fixup_ip(struct pt_regs *regs)
529529
} while (to < ip);
530530

531531
if (to == ip) {
532-
regs->ip = old_to;
532+
set_linear_ip(regs, old_to);
533533
return 1;
534534
}
535535

@@ -569,7 +569,8 @@ static void __intel_pmu_pebs_event(struct perf_event *event,
569569
* A possible PERF_SAMPLE_REGS will have to transfer all regs.
570570
*/
571571
regs = *iregs;
572-
regs.ip = pebs->ip;
572+
regs.flags = pebs->flags;
573+
set_linear_ip(&regs, pebs->ip);
573574
regs.bp = pebs->bp;
574575
regs.sp = pebs->sp;
575576

0 commit comments

Comments
 (0)