Skip to content

Commit 4c3b73c

Browse files
committed
Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip
Pull perf fixes from Ingo Molnar: "Misc kernel side fixes: - fix event leak - fix AMD PMU driver bug - fix core event handling bug - fix build bug on certain randconfigs Plus misc tooling fixes" * 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip: perf/x86/amd/ibs: Fix pmu::stop() nesting perf/core: Don't leak event in the syscall error path perf/core: Fix time tracking bug with multiplexing perf jit: genelf makes assumptions about endian perf hists: Fix determination of a callchain node's childlessness perf tools: Add missing initialization of perf_sample.cpumode in synthesized samples perf tools: Fix build break on powerpc perf/x86: Move events_sysfs_show() outside CPU_SUP_INTEL perf bench: Fix detached tarball building due to missing 'perf bench memcpy' headers perf tests: Fix tarpkg build test error output redirection
2 parents 7b367f5 + 85dc600 commit 4c3b73c

File tree

12 files changed

+98
-35
lines changed

12 files changed

+98
-35
lines changed

arch/x86/events/amd/ibs.c

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,46 @@ static u32 ibs_caps;
2828
#define IBS_FETCH_CONFIG_MASK (IBS_FETCH_RAND_EN | IBS_FETCH_MAX_CNT)
2929
#define IBS_OP_CONFIG_MASK IBS_OP_MAX_CNT
3030

31+
32+
/*
33+
* IBS states:
34+
*
35+
* ENABLED; tracks the pmu::add(), pmu::del() state, when set the counter is taken
36+
* and any further add()s must fail.
37+
*
38+
* STARTED/STOPPING/STOPPED; deal with pmu::start(), pmu::stop() state but are
39+
* complicated by the fact that the IBS hardware can send late NMIs (ie. after
40+
* we've cleared the EN bit).
41+
*
42+
* In order to consume these late NMIs we have the STOPPED state, any NMI that
43+
* happens after we've cleared the EN state will clear this bit and report the
44+
* NMI handled (this is fundamentally racy in the face or multiple NMI sources,
45+
* someone else can consume our BIT and our NMI will go unhandled).
46+
*
47+
* And since we cannot set/clear this separate bit together with the EN bit,
48+
* there are races; if we cleared STARTED early, an NMI could land in
49+
* between clearing STARTED and clearing the EN bit (in fact multiple NMIs
50+
* could happen if the period is small enough), and consume our STOPPED bit
51+
* and trigger streams of unhandled NMIs.
52+
*
53+
* If, however, we clear STARTED late, an NMI can hit between clearing the
54+
* EN bit and clearing STARTED, still see STARTED set and process the event.
55+
* If this event will have the VALID bit clear, we bail properly, but this
56+
* is not a given. With VALID set we can end up calling pmu::stop() again
57+
* (the throttle logic) and trigger the WARNs in there.
58+
*
59+
* So what we do is set STOPPING before clearing EN to avoid the pmu::stop()
60+
* nesting, and clear STARTED late, so that we have a well defined state over
61+
* the clearing of the EN bit.
62+
*
63+
* XXX: we could probably be using !atomic bitops for all this.
64+
*/
65+
3166
enum ibs_states {
3267
IBS_ENABLED = 0,
3368
IBS_STARTED = 1,
3469
IBS_STOPPING = 2,
70+
IBS_STOPPED = 3,
3571

3672
IBS_MAX_STATES,
3773
};
@@ -377,11 +413,10 @@ static void perf_ibs_start(struct perf_event *event, int flags)
377413

378414
perf_ibs_set_period(perf_ibs, hwc, &period);
379415
/*
380-
* Set STARTED before enabling the hardware, such that
381-
* a subsequent NMI must observe it. Then clear STOPPING
382-
* such that we don't consume NMIs by accident.
416+
* Set STARTED before enabling the hardware, such that a subsequent NMI
417+
* must observe it.
383418
*/
384-
set_bit(IBS_STARTED, pcpu->state);
419+
set_bit(IBS_STARTED, pcpu->state);
385420
clear_bit(IBS_STOPPING, pcpu->state);
386421
perf_ibs_enable_event(perf_ibs, hwc, period >> 4);
387422

@@ -396,6 +431,9 @@ static void perf_ibs_stop(struct perf_event *event, int flags)
396431
u64 config;
397432
int stopping;
398433

434+
if (test_and_set_bit(IBS_STOPPING, pcpu->state))
435+
return;
436+
399437
stopping = test_bit(IBS_STARTED, pcpu->state);
400438

401439
if (!stopping && (hwc->state & PERF_HES_UPTODATE))
@@ -405,12 +443,12 @@ static void perf_ibs_stop(struct perf_event *event, int flags)
405443

406444
if (stopping) {
407445
/*
408-
* Set STOPPING before disabling the hardware, such that it
446+
* Set STOPPED before disabling the hardware, such that it
409447
* must be visible to NMIs the moment we clear the EN bit,
410448
* at which point we can generate an !VALID sample which
411449
* we need to consume.
412450
*/
413-
set_bit(IBS_STOPPING, pcpu->state);
451+
set_bit(IBS_STOPPED, pcpu->state);
414452
perf_ibs_disable_event(perf_ibs, hwc, config);
415453
/*
416454
* Clear STARTED after disabling the hardware; if it were
@@ -556,7 +594,7 @@ static int perf_ibs_handle_irq(struct perf_ibs *perf_ibs, struct pt_regs *iregs)
556594
* with samples that even have the valid bit cleared.
557595
* Mark all this NMIs as handled.
558596
*/
559-
if (test_and_clear_bit(IBS_STOPPING, pcpu->state))
597+
if (test_and_clear_bit(IBS_STOPPED, pcpu->state))
560598
return 1;
561599

562600
return 0;

arch/x86/events/perf_event.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,9 @@ ssize_t intel_event_sysfs_show(char *page, u64 config);
800800

801801
struct attribute **merge_attr(struct attribute **a, struct attribute **b);
802802

803+
ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
804+
char *page);
805+
803806
#ifdef CONFIG_CPU_SUP_AMD
804807

805808
int amd_pmu_init(void);
@@ -930,9 +933,6 @@ int p6_pmu_init(void);
930933

931934
int knc_pmu_init(void);
932935

933-
ssize_t events_sysfs_show(struct device *dev, struct device_attribute *attr,
934-
char *page);
935-
936936
static inline int is_ht_workaround_enabled(void)
937937
{
938938
return !!(x86_pmu.flags & PMU_FL_EXCL_ENABLED);

kernel/events/core.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,14 +2417,24 @@ static void ctx_sched_out(struct perf_event_context *ctx,
24172417
cpuctx->task_ctx = NULL;
24182418
}
24192419

2420-
is_active ^= ctx->is_active; /* changed bits */
2421-
2420+
/*
2421+
* Always update time if it was set; not only when it changes.
2422+
* Otherwise we can 'forget' to update time for any but the last
2423+
* context we sched out. For example:
2424+
*
2425+
* ctx_sched_out(.event_type = EVENT_FLEXIBLE)
2426+
* ctx_sched_out(.event_type = EVENT_PINNED)
2427+
*
2428+
* would only update time for the pinned events.
2429+
*/
24222430
if (is_active & EVENT_TIME) {
24232431
/* update (and stop) ctx time */
24242432
update_context_time(ctx);
24252433
update_cgrp_time_from_cpuctx(cpuctx);
24262434
}
24272435

2436+
is_active ^= ctx->is_active; /* changed bits */
2437+
24282438
if (!ctx->nr_active || !(is_active & EVENT_ALL))
24292439
return;
24302440

@@ -8532,6 +8542,7 @@ SYSCALL_DEFINE5(perf_event_open,
85328542
f_flags);
85338543
if (IS_ERR(event_file)) {
85348544
err = PTR_ERR(event_file);
8545+
event_file = NULL;
85358546
goto err_context;
85368547
}
85378548

tools/perf/MANIFEST

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ arch/*/include/uapi/asm/unistd*.h
7474
arch/*/include/uapi/asm/perf_regs.h
7575
arch/*/lib/memcpy*.S
7676
arch/*/lib/memset*.S
77+
arch/*/include/asm/*features.h
7778
include/linux/poison.h
7879
include/linux/hw_breakpoint.h
7980
include/uapi/linux/perf_event.h

tools/perf/arch/powerpc/util/header.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include <stdlib.h>
55
#include <string.h>
66
#include <linux/stringify.h>
7+
#include "header.h"
8+
#include "util.h"
79

810
#define mfspr(rn) ({unsigned long rval; \
911
asm volatile("mfspr %0," __stringify(rn) \

tools/perf/tests/perf-targz-src-pkg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ TMP_DEST=$(mktemp -d)
1515
tar xf ${TARBALL} -C $TMP_DEST
1616
rm -f ${TARBALL}
1717
cd - > /dev/null
18-
make -C $TMP_DEST/perf*/tools/perf > /dev/null 2>&1
18+
make -C $TMP_DEST/perf*/tools/perf > /dev/null
1919
RC=$?
2020
rm -rf ${TMP_DEST}
2121
exit $RC

tools/perf/ui/browsers/hists.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ static void callchain_node__init_have_children(struct callchain_node *node,
337337
chain = list_entry(node->val.next, struct callchain_list, list);
338338
chain->has_children = has_sibling;
339339

340-
if (node->val.next != node->val.prev) {
340+
if (!list_empty(&node->val)) {
341341
chain = list_entry(node->val.prev, struct callchain_list, list);
342342
chain->has_children = !RB_EMPTY_ROOT(&node->rb_root);
343343
}

tools/perf/util/event.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,22 @@ const char *perf_event__name(unsigned int id)
5656
return perf_event__names[id];
5757
}
5858

59-
static struct perf_sample synth_sample = {
59+
static int perf_tool__process_synth_event(struct perf_tool *tool,
60+
union perf_event *event,
61+
struct machine *machine,
62+
perf_event__handler_t process)
63+
{
64+
struct perf_sample synth_sample = {
6065
.pid = -1,
6166
.tid = -1,
6267
.time = -1,
6368
.stream_id = -1,
6469
.cpu = -1,
6570
.period = 1,
71+
.cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK,
72+
};
73+
74+
return process(tool, event, &synth_sample, machine);
6675
};
6776

6877
/*
@@ -186,7 +195,7 @@ pid_t perf_event__synthesize_comm(struct perf_tool *tool,
186195
if (perf_event__prepare_comm(event, pid, machine, &tgid, &ppid) != 0)
187196
return -1;
188197

189-
if (process(tool, event, &synth_sample, machine) != 0)
198+
if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
190199
return -1;
191200

192201
return tgid;
@@ -218,7 +227,7 @@ static int perf_event__synthesize_fork(struct perf_tool *tool,
218227

219228
event->fork.header.size = (sizeof(event->fork) + machine->id_hdr_size);
220229

221-
if (process(tool, event, &synth_sample, machine) != 0)
230+
if (perf_tool__process_synth_event(tool, event, machine, process) != 0)
222231
return -1;
223232

224233
return 0;
@@ -344,7 +353,7 @@ int perf_event__synthesize_mmap_events(struct perf_tool *tool,
344353
event->mmap2.pid = tgid;
345354
event->mmap2.tid = pid;
346355

347-
if (process(tool, event, &synth_sample, machine) != 0) {
356+
if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
348357
rc = -1;
349358
break;
350359
}
@@ -402,7 +411,7 @@ int perf_event__synthesize_modules(struct perf_tool *tool,
402411

403412
memcpy(event->mmap.filename, pos->dso->long_name,
404413
pos->dso->long_name_len + 1);
405-
if (process(tool, event, &synth_sample, machine) != 0) {
414+
if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
406415
rc = -1;
407416
break;
408417
}
@@ -472,7 +481,7 @@ static int __event__synthesize_thread(union perf_event *comm_event,
472481
/*
473482
* Send the prepared comm event
474483
*/
475-
if (process(tool, comm_event, &synth_sample, machine) != 0)
484+
if (perf_tool__process_synth_event(tool, comm_event, machine, process) != 0)
476485
break;
477486

478487
rc = 0;
@@ -701,7 +710,7 @@ int perf_event__synthesize_kernel_mmap(struct perf_tool *tool,
701710
event->mmap.len = map->end - event->mmap.start;
702711
event->mmap.pid = machine->pid;
703712

704-
err = process(tool, event, &synth_sample, machine);
713+
err = perf_tool__process_synth_event(tool, event, machine, process);
705714
free(event);
706715

707716
return err;

tools/perf/util/genelf.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,32 @@ int jit_add_debug_info(Elf *e, uint64_t code_addr, void *debug, int nr_debug_ent
99

1010
#if defined(__arm__)
1111
#define GEN_ELF_ARCH EM_ARM
12-
#define GEN_ELF_ENDIAN ELFDATA2LSB
1312
#define GEN_ELF_CLASS ELFCLASS32
1413
#elif defined(__aarch64__)
1514
#define GEN_ELF_ARCH EM_AARCH64
16-
#define GEN_ELF_ENDIAN ELFDATA2LSB
1715
#define GEN_ELF_CLASS ELFCLASS64
1816
#elif defined(__x86_64__)
1917
#define GEN_ELF_ARCH EM_X86_64
20-
#define GEN_ELF_ENDIAN ELFDATA2LSB
2118
#define GEN_ELF_CLASS ELFCLASS64
2219
#elif defined(__i386__)
2320
#define GEN_ELF_ARCH EM_386
24-
#define GEN_ELF_ENDIAN ELFDATA2LSB
2521
#define GEN_ELF_CLASS ELFCLASS32
26-
#elif defined(__ppcle__)
27-
#define GEN_ELF_ARCH EM_PPC
28-
#define GEN_ELF_ENDIAN ELFDATA2LSB
29-
#define GEN_ELF_CLASS ELFCLASS64
30-
#elif defined(__powerpc__)
31-
#define GEN_ELF_ARCH EM_PPC64
32-
#define GEN_ELF_ENDIAN ELFDATA2MSB
33-
#define GEN_ELF_CLASS ELFCLASS64
34-
#elif defined(__powerpcle__)
22+
#elif defined(__powerpc64__)
3523
#define GEN_ELF_ARCH EM_PPC64
36-
#define GEN_ELF_ENDIAN ELFDATA2LSB
3724
#define GEN_ELF_CLASS ELFCLASS64
25+
#elif defined(__powerpc__)
26+
#define GEN_ELF_ARCH EM_PPC
27+
#define GEN_ELF_CLASS ELFCLASS32
3828
#else
3929
#error "unsupported architecture"
4030
#endif
4131

32+
#if __BYTE_ORDER == __BIG_ENDIAN
33+
#define GEN_ELF_ENDIAN ELFDATA2MSB
34+
#else
35+
#define GEN_ELF_ENDIAN ELFDATA2LSB
36+
#endif
37+
4238
#if GEN_ELF_CLASS == ELFCLASS64
4339
#define elf_newehdr elf64_newehdr
4440
#define elf_getshdr elf64_getshdr

tools/perf/util/intel-bts.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ static int intel_bts_synth_branch_sample(struct intel_bts_queue *btsq,
279279
event.sample.header.misc = PERF_RECORD_MISC_USER;
280280
event.sample.header.size = sizeof(struct perf_event_header);
281281

282+
sample.cpumode = PERF_RECORD_MISC_USER;
282283
sample.ip = le64_to_cpu(branch->from);
283284
sample.pid = btsq->pid;
284285
sample.tid = btsq->tid;

tools/perf/util/intel-pt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,7 @@ static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
979979
if (!pt->timeless_decoding)
980980
sample.time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
981981

982+
sample.cpumode = PERF_RECORD_MISC_USER;
982983
sample.ip = ptq->state->from_ip;
983984
sample.pid = ptq->pid;
984985
sample.tid = ptq->tid;
@@ -1035,6 +1036,7 @@ static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
10351036
if (!pt->timeless_decoding)
10361037
sample.time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
10371038

1039+
sample.cpumode = PERF_RECORD_MISC_USER;
10381040
sample.ip = ptq->state->from_ip;
10391041
sample.pid = ptq->pid;
10401042
sample.tid = ptq->tid;
@@ -1092,6 +1094,7 @@ static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
10921094
if (!pt->timeless_decoding)
10931095
sample.time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
10941096

1097+
sample.cpumode = PERF_RECORD_MISC_USER;
10951098
sample.ip = ptq->state->from_ip;
10961099
sample.pid = ptq->pid;
10971100
sample.tid = ptq->tid;

tools/perf/util/jitdump.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
417417
* use first address as sample address
418418
*/
419419
memset(&sample, 0, sizeof(sample));
420+
sample.cpumode = PERF_RECORD_MISC_USER;
420421
sample.pid = pid;
421422
sample.tid = tid;
422423
sample.time = id->time;
@@ -505,6 +506,7 @@ static int jit_repipe_code_move(struct jit_buf_desc *jd, union jr_entry *jr)
505506
* use first address as sample address
506507
*/
507508
memset(&sample, 0, sizeof(sample));
509+
sample.cpumode = PERF_RECORD_MISC_USER;
508510
sample.pid = pid;
509511
sample.tid = tid;
510512
sample.time = id->time;

0 commit comments

Comments
 (0)