Skip to content

Commit 9c136ce

Browse files
committed
Merge: perf: sync with upstream v6.12
Locally merged due to gitlab conflict Signed-off-by: Rado Vrbovsky <[email protected]>
2 parents 5928120 + 50c2b1f commit 9c136ce

File tree

324 files changed

+16293
-4823
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

324 files changed

+16293
-4823
lines changed

arch/x86/events/core.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#include <asm/desc.h>
4242
#include <asm/ldt.h>
4343
#include <asm/unwind.h>
44+
#include <asm/uprobes.h>
45+
#include <asm/ibt.h>
4446

4547
#include "perf_event.h"
4648

@@ -2816,6 +2818,46 @@ static unsigned long get_segment_base(unsigned int segment)
28162818
return get_desc_base(desc);
28172819
}
28182820

2821+
#ifdef CONFIG_UPROBES
2822+
/*
2823+
* Heuristic-based check if uprobe is installed at the function entry.
2824+
*
2825+
* Under assumption of user code being compiled with frame pointers,
2826+
* `push %rbp/%ebp` is a good indicator that we indeed are.
2827+
*
2828+
* Similarly, `endbr64` (assuming 64-bit mode) is also a common pattern.
2829+
* If we get this wrong, captured stack trace might have one extra bogus
2830+
* entry, but the rest of stack trace will still be meaningful.
2831+
*/
2832+
static bool is_uprobe_at_func_entry(struct pt_regs *regs)
2833+
{
2834+
struct arch_uprobe *auprobe;
2835+
2836+
if (!current->utask)
2837+
return false;
2838+
2839+
auprobe = current->utask->auprobe;
2840+
if (!auprobe)
2841+
return false;
2842+
2843+
/* push %rbp/%ebp */
2844+
if (auprobe->insn[0] == 0x55)
2845+
return true;
2846+
2847+
/* endbr64 (64-bit only) */
2848+
if (user_64bit_mode(regs) && is_endbr(*(u32 *)auprobe->insn))
2849+
return true;
2850+
2851+
return false;
2852+
}
2853+
2854+
#else
2855+
static bool is_uprobe_at_func_entry(struct pt_regs *regs)
2856+
{
2857+
return false;
2858+
}
2859+
#endif /* CONFIG_UPROBES */
2860+
28192861
#ifdef CONFIG_IA32_EMULATION
28202862

28212863
#include <linux/compat.h>
@@ -2827,6 +2869,7 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *ent
28272869
unsigned long ss_base, cs_base;
28282870
struct stack_frame_ia32 frame;
28292871
const struct stack_frame_ia32 __user *fp;
2872+
u32 ret_addr;
28302873

28312874
if (user_64bit_mode(regs))
28322875
return 0;
@@ -2836,6 +2879,12 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *ent
28362879

28372880
fp = compat_ptr(ss_base + regs->bp);
28382881
pagefault_disable();
2882+
2883+
/* see perf_callchain_user() below for why we do this */
2884+
if (is_uprobe_at_func_entry(regs) &&
2885+
!get_user(ret_addr, (const u32 __user *)regs->sp))
2886+
perf_callchain_store(entry, ret_addr);
2887+
28392888
while (entry->nr < entry->max_stack) {
28402889
if (!valid_user_frame(fp, sizeof(frame)))
28412890
break;
@@ -2864,6 +2913,7 @@ perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs
28642913
{
28652914
struct stack_frame frame;
28662915
const struct stack_frame __user *fp;
2916+
unsigned long ret_addr;
28672917

28682918
if (perf_guest_state()) {
28692919
/* TODO: We don't support guest os callchain now */
@@ -2887,6 +2937,19 @@ perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs
28872937
return;
28882938

28892939
pagefault_disable();
2940+
2941+
/*
2942+
* If we are called from uprobe handler, and we are indeed at the very
2943+
* entry to user function (which is normally a `push %rbp` instruction,
2944+
* under assumption of application being compiled with frame pointers),
2945+
* we should read return address from *regs->sp before proceeding
2946+
* to follow frame pointers, otherwise we'll skip immediate caller
2947+
* as %rbp is not yet setup.
2948+
*/
2949+
if (is_uprobe_at_func_entry(regs) &&
2950+
!get_user(ret_addr, (const unsigned long __user *)regs->sp))
2951+
perf_callchain_store(entry, ret_addr);
2952+
28902953
while (entry->nr < entry->max_stack) {
28912954
if (!valid_user_frame(fp, sizeof(frame)))
28922955
break;

arch/x86/events/intel/bts.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,6 @@ static int bts_event_init(struct perf_event *event)
557557
* disabled, so disallow intel_bts driver for unprivileged
558558
* users on paranoid systems since it provides trace data
559559
* to the user in a zero-copy fashion.
560-
*
561-
* Note that the default paranoia setting permits unprivileged
562-
* users to profile the kernel.
563560
*/
564561
if (event->attr.exclude_kernel) {
565562
ret = perf_allow_kernel(&event->attr);

arch/x86/events/intel/core.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3971,8 +3971,12 @@ static int intel_pmu_hw_config(struct perf_event *event)
39713971
x86_pmu.pebs_aliases(event);
39723972
}
39733973

3974-
if (needs_branch_stack(event) && is_sampling_event(event))
3975-
event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
3974+
if (needs_branch_stack(event)) {
3975+
/* Avoid branch stack setup for counting events in SAMPLE READ */
3976+
if (is_sampling_event(event) ||
3977+
!(event->attr.sample_type & PERF_SAMPLE_READ))
3978+
event->hw.flags |= PERF_X86_EVENT_NEEDS_BRANCH_STACK;
3979+
}
39763980

39773981
if (branch_sample_counters(event)) {
39783982
struct perf_event *leader, *sibling;

arch/x86/events/intel/cstate.c

Lines changed: 5 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,6 @@ static ssize_t __cstate_##_var##_show(struct device *dev, \
128128
static struct device_attribute format_attr_##_var = \
129129
__ATTR(_name, 0444, __cstate_##_var##_show, NULL)
130130

131-
static ssize_t cstate_get_attr_cpumask(struct device *dev,
132-
struct device_attribute *attr,
133-
char *buf);
134-
135131
/* Model -> events mapping */
136132
struct cstate_model {
137133
unsigned long core_events;
@@ -206,22 +202,9 @@ static struct attribute_group cstate_format_attr_group = {
206202
.attrs = cstate_format_attrs,
207203
};
208204

209-
static cpumask_t cstate_core_cpu_mask;
210-
static DEVICE_ATTR(cpumask, S_IRUGO, cstate_get_attr_cpumask, NULL);
211-
212-
static struct attribute *cstate_cpumask_attrs[] = {
213-
&dev_attr_cpumask.attr,
214-
NULL,
215-
};
216-
217-
static struct attribute_group cpumask_attr_group = {
218-
.attrs = cstate_cpumask_attrs,
219-
};
220-
221205
static const struct attribute_group *cstate_attr_groups[] = {
222206
&cstate_events_attr_group,
223207
&cstate_format_attr_group,
224-
&cpumask_attr_group,
225208
NULL,
226209
};
227210

@@ -269,8 +252,6 @@ static struct perf_msr pkg_msr[] = {
269252
[PERF_CSTATE_PKG_C10_RES] = { MSR_PKG_C10_RESIDENCY, &group_cstate_pkg_c10, test_msr },
270253
};
271254

272-
static cpumask_t cstate_pkg_cpu_mask;
273-
274255
/* cstate_module PMU */
275256
static struct pmu cstate_module_pmu;
276257
static bool has_cstate_module;
@@ -291,28 +272,9 @@ static struct perf_msr module_msr[] = {
291272
[PERF_CSTATE_MODULE_C6_RES] = { MSR_MODULE_C6_RES_MS, &group_cstate_module_c6, test_msr },
292273
};
293274

294-
static cpumask_t cstate_module_cpu_mask;
295-
296-
static ssize_t cstate_get_attr_cpumask(struct device *dev,
297-
struct device_attribute *attr,
298-
char *buf)
299-
{
300-
struct pmu *pmu = dev_get_drvdata(dev);
301-
302-
if (pmu == &cstate_core_pmu)
303-
return cpumap_print_to_pagebuf(true, buf, &cstate_core_cpu_mask);
304-
else if (pmu == &cstate_pkg_pmu)
305-
return cpumap_print_to_pagebuf(true, buf, &cstate_pkg_cpu_mask);
306-
else if (pmu == &cstate_module_pmu)
307-
return cpumap_print_to_pagebuf(true, buf, &cstate_module_cpu_mask);
308-
else
309-
return 0;
310-
}
311-
312275
static int cstate_pmu_event_init(struct perf_event *event)
313276
{
314277
u64 cfg = event->attr.config;
315-
int cpu;
316278

317279
if (event->attr.type != event->pmu->type)
318280
return -ENOENT;
@@ -331,37 +293,24 @@ static int cstate_pmu_event_init(struct perf_event *event)
331293
if (!(core_msr_mask & (1 << cfg)))
332294
return -EINVAL;
333295
event->hw.event_base = core_msr[cfg].msr;
334-
cpu = cpumask_any_and(&cstate_core_cpu_mask,
335-
topology_sibling_cpumask(event->cpu));
336296
} else if (event->pmu == &cstate_pkg_pmu) {
337297
if (cfg >= PERF_CSTATE_PKG_EVENT_MAX)
338298
return -EINVAL;
339299
cfg = array_index_nospec((unsigned long)cfg, PERF_CSTATE_PKG_EVENT_MAX);
340300
if (!(pkg_msr_mask & (1 << cfg)))
341301
return -EINVAL;
342-
343-
event->event_caps |= PERF_EV_CAP_READ_ACTIVE_PKG;
344-
345302
event->hw.event_base = pkg_msr[cfg].msr;
346-
cpu = cpumask_any_and(&cstate_pkg_cpu_mask,
347-
topology_die_cpumask(event->cpu));
348303
} else if (event->pmu == &cstate_module_pmu) {
349304
if (cfg >= PERF_CSTATE_MODULE_EVENT_MAX)
350305
return -EINVAL;
351306
cfg = array_index_nospec((unsigned long)cfg, PERF_CSTATE_MODULE_EVENT_MAX);
352307
if (!(module_msr_mask & (1 << cfg)))
353308
return -EINVAL;
354309
event->hw.event_base = module_msr[cfg].msr;
355-
cpu = cpumask_any_and(&cstate_module_cpu_mask,
356-
topology_cluster_cpumask(event->cpu));
357310
} else {
358311
return -ENOENT;
359312
}
360313

361-
if (cpu >= nr_cpu_ids)
362-
return -ENODEV;
363-
364-
event->cpu = cpu;
365314
event->hw.config = cfg;
366315
event->hw.idx = -1;
367316
return 0;
@@ -412,84 +361,6 @@ static int cstate_pmu_event_add(struct perf_event *event, int mode)
412361
return 0;
413362
}
414363

415-
/*
416-
* Check if exiting cpu is the designated reader. If so migrate the
417-
* events when there is a valid target available
418-
*/
419-
static int cstate_cpu_exit(unsigned int cpu)
420-
{
421-
unsigned int target;
422-
423-
if (has_cstate_core &&
424-
cpumask_test_and_clear_cpu(cpu, &cstate_core_cpu_mask)) {
425-
426-
target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu);
427-
/* Migrate events if there is a valid target */
428-
if (target < nr_cpu_ids) {
429-
cpumask_set_cpu(target, &cstate_core_cpu_mask);
430-
perf_pmu_migrate_context(&cstate_core_pmu, cpu, target);
431-
}
432-
}
433-
434-
if (has_cstate_pkg &&
435-
cpumask_test_and_clear_cpu(cpu, &cstate_pkg_cpu_mask)) {
436-
437-
target = cpumask_any_but(topology_die_cpumask(cpu), cpu);
438-
/* Migrate events if there is a valid target */
439-
if (target < nr_cpu_ids) {
440-
cpumask_set_cpu(target, &cstate_pkg_cpu_mask);
441-
perf_pmu_migrate_context(&cstate_pkg_pmu, cpu, target);
442-
}
443-
}
444-
445-
if (has_cstate_module &&
446-
cpumask_test_and_clear_cpu(cpu, &cstate_module_cpu_mask)) {
447-
448-
target = cpumask_any_but(topology_cluster_cpumask(cpu), cpu);
449-
/* Migrate events if there is a valid target */
450-
if (target < nr_cpu_ids) {
451-
cpumask_set_cpu(target, &cstate_module_cpu_mask);
452-
perf_pmu_migrate_context(&cstate_module_pmu, cpu, target);
453-
}
454-
}
455-
return 0;
456-
}
457-
458-
static int cstate_cpu_init(unsigned int cpu)
459-
{
460-
unsigned int target;
461-
462-
/*
463-
* If this is the first online thread of that core, set it in
464-
* the core cpu mask as the designated reader.
465-
*/
466-
target = cpumask_any_and(&cstate_core_cpu_mask,
467-
topology_sibling_cpumask(cpu));
468-
469-
if (has_cstate_core && target >= nr_cpu_ids)
470-
cpumask_set_cpu(cpu, &cstate_core_cpu_mask);
471-
472-
/*
473-
* If this is the first online thread of that package, set it
474-
* in the package cpu mask as the designated reader.
475-
*/
476-
target = cpumask_any_and(&cstate_pkg_cpu_mask,
477-
topology_die_cpumask(cpu));
478-
if (has_cstate_pkg && target >= nr_cpu_ids)
479-
cpumask_set_cpu(cpu, &cstate_pkg_cpu_mask);
480-
481-
/*
482-
* If this is the first online thread of that cluster, set it
483-
* in the cluster cpu mask as the designated reader.
484-
*/
485-
target = cpumask_any_and(&cstate_module_cpu_mask,
486-
topology_cluster_cpumask(cpu));
487-
if (has_cstate_module && target >= nr_cpu_ids)
488-
cpumask_set_cpu(cpu, &cstate_module_cpu_mask);
489-
490-
return 0;
491-
}
492-
493364
static const struct attribute_group *core_attr_update[] = {
494365
&group_cstate_core_c1,
495366
&group_cstate_core_c3,
@@ -526,6 +397,7 @@ static struct pmu cstate_core_pmu = {
526397
.stop = cstate_pmu_event_stop,
527398
.read = cstate_pmu_event_update,
528399
.capabilities = PERF_PMU_CAP_NO_INTERRUPT | PERF_PMU_CAP_NO_EXCLUDE,
400+
.scope = PERF_PMU_SCOPE_CORE,
529401
.module = THIS_MODULE,
530402
};
531403

@@ -541,6 +413,7 @@ static struct pmu cstate_pkg_pmu = {
541413
.stop = cstate_pmu_event_stop,
542414
.read = cstate_pmu_event_update,
543415
.capabilities = PERF_PMU_CAP_NO_INTERRUPT | PERF_PMU_CAP_NO_EXCLUDE,
416+
.scope = PERF_PMU_SCOPE_PKG,
544417
.module = THIS_MODULE,
545418
};
546419

@@ -556,6 +429,7 @@ static struct pmu cstate_module_pmu = {
556429
.stop = cstate_pmu_event_stop,
557430
.read = cstate_pmu_event_update,
558431
.capabilities = PERF_PMU_CAP_NO_INTERRUPT | PERF_PMU_CAP_NO_EXCLUDE,
432+
.scope = PERF_PMU_SCOPE_CLUSTER,
559433
.module = THIS_MODULE,
560434
};
561435

@@ -810,9 +684,6 @@ static int __init cstate_probe(const struct cstate_model *cm)
810684

811685
static inline void cstate_cleanup(void)
812686
{
813-
cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_CSTATE_ONLINE);
814-
cpuhp_remove_state_nocalls(CPUHP_AP_PERF_X86_CSTATE_STARTING);
815-
816687
if (has_cstate_core)
817688
perf_pmu_unregister(&cstate_core_pmu);
818689

@@ -827,11 +698,6 @@ static int __init cstate_init(void)
827698
{
828699
int err;
829700

830-
cpuhp_setup_state(CPUHP_AP_PERF_X86_CSTATE_STARTING,
831-
"perf/x86/cstate:starting", cstate_cpu_init, NULL);
832-
cpuhp_setup_state(CPUHP_AP_PERF_X86_CSTATE_ONLINE,
833-
"perf/x86/cstate:online", NULL, cstate_cpu_exit);
834-
835701
if (has_cstate_core) {
836702
err = perf_pmu_register(&cstate_core_pmu, cstate_core_pmu.name, -1);
837703
if (err) {
@@ -844,6 +710,8 @@ static int __init cstate_init(void)
844710

845711
if (has_cstate_pkg) {
846712
if (topology_max_dies_per_package() > 1) {
713+
/* CLX-AP is multi-die and the cstate is die-scope */
714+
cstate_pkg_pmu.scope = PERF_PMU_SCOPE_DIE;
847715
err = perf_pmu_register(&cstate_pkg_pmu,
848716
"cstate_die", -1);
849717
} else {

0 commit comments

Comments
 (0)