Skip to content

Commit b202952

Browse files
Gleb NatapovIngo Molnar
authored andcommitted
perf, core: Rate limit perf_sched_events jump_label patching
jump_lable patching is very expensive operation that involves pausing all cpus. The patching of perf_sched_events jump_label is easily controllable from userspace by unprivileged user. When te user runs a loop like this: "while true; do perf stat -e cycles true; done" ... the performance of my test application that just increments a counter for one second drops by 4%. This is on a 16 cpu box with my test application using only one of them. An impact on a real server doing real work will be worse. Performance of KVM PMU drops nearly 50% due to jump_lable for "perf record" since KVM PMU implementation creates and destroys perf event frequently. This patch introduces a way to rate limit jump_label patching and uses it to fix the above problem. I believe that as jump_label use will spread the problem will become more common and thus solving it in a generic code is appropriate. Also fixing it in the perf code would result in moving jump_label accounting logic to perf code with all the ifdefs in case of JUMP_LABEL=n kernel. With this patch all details are nicely hidden inside jump_label code. Signed-off-by: Gleb Natapov <gleb@redhat.com> Acked-by: Jason Baron <jbaron@redhat.com> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/20111127155909.GO2557@redhat.com Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent b79387e commit b202952

4 files changed

Lines changed: 68 additions & 10 deletions

File tree

‎include/linux/jump_label.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <linux/types.h>
55
#include <linux/compiler.h>
6+
#include <linux/workqueue.h>
67

78
#if defined(CC_HAVE_ASM_GOTO) && defined(CONFIG_JUMP_LABEL)
89

@@ -14,6 +15,12 @@ struct jump_label_key {
1415
#endif
1516
};
1617

18+
struct jump_label_key_deferred {
19+
struct jump_label_key key;
20+
unsigned long timeout;
21+
struct delayed_work work;
22+
};
23+
1724
# include <asm/jump_label.h>
1825
# define HAVE_JUMP_LABEL
1926
#endif /* CC_HAVE_ASM_GOTO && CONFIG_JUMP_LABEL */
@@ -51,8 +58,11 @@ extern void arch_jump_label_transform_static(struct jump_entry *entry,
5158
extern int jump_label_text_reserved(void *start, void *end);
5259
extern void jump_label_inc(struct jump_label_key *key);
5360
extern void jump_label_dec(struct jump_label_key *key);
61+
extern void jump_label_dec_deferred(struct jump_label_key_deferred *key);
5462
extern bool jump_label_enabled(struct jump_label_key *key);
5563
extern void jump_label_apply_nops(struct module *mod);
64+
extern void jump_label_rate_limit(struct jump_label_key_deferred *key,
65+
unsigned long rl);
5666

5767
#else /* !HAVE_JUMP_LABEL */
5868

@@ -68,6 +78,10 @@ static __always_inline void jump_label_init(void)
6878
{
6979
}
7080

81+
struct jump_label_key_deferred {
82+
struct jump_label_key key;
83+
};
84+
7185
static __always_inline bool static_branch(struct jump_label_key *key)
7286
{
7387
if (unlikely(atomic_read(&key->enabled)))
@@ -85,6 +99,11 @@ static inline void jump_label_dec(struct jump_label_key *key)
8599
atomic_dec(&key->enabled);
86100
}
87101

102+
static inline void jump_label_dec_deferred(struct jump_label_key_deferred *key)
103+
{
104+
jump_label_dec(&key->key);
105+
}
106+
88107
static inline int jump_label_text_reserved(void *start, void *end)
89108
{
90109
return 0;
@@ -102,6 +121,11 @@ static inline int jump_label_apply_nops(struct module *mod)
102121
{
103122
return 0;
104123
}
124+
125+
static inline void jump_label_rate_limit(struct jump_label_key_deferred *key,
126+
unsigned long rl)
127+
{
128+
}
105129
#endif /* HAVE_JUMP_LABEL */
106130

107131
#endif /* _LINUX_JUMP_LABEL_H */

‎include/linux/perf_event.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,12 +1064,12 @@ perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
10641064
}
10651065
}
10661066

1067-
extern struct jump_label_key perf_sched_events;
1067+
extern struct jump_label_key_deferred perf_sched_events;
10681068

10691069
static inline void perf_event_task_sched_in(struct task_struct *prev,
10701070
struct task_struct *task)
10711071
{
1072-
if (static_branch(&perf_sched_events))
1072+
if (static_branch(&perf_sched_events.key))
10731073
__perf_event_task_sched_in(prev, task);
10741074
}
10751075

@@ -1078,7 +1078,7 @@ static inline void perf_event_task_sched_out(struct task_struct *prev,
10781078
{
10791079
perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, NULL, 0);
10801080

1081-
if (static_branch(&perf_sched_events))
1081+
if (static_branch(&perf_sched_events.key))
10821082
__perf_event_task_sched_out(prev, next);
10831083
}
10841084

‎kernel/events/core.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ enum event_type_t {
128128
* perf_sched_events : >0 events exist
129129
* perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
130130
*/
131-
struct jump_label_key perf_sched_events __read_mostly;
131+
struct jump_label_key_deferred perf_sched_events __read_mostly;
132132
static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
133133

134134
static atomic_t nr_mmap_events __read_mostly;
@@ -2748,7 +2748,7 @@ static void free_event(struct perf_event *event)
27482748

27492749
if (!event->parent) {
27502750
if (event->attach_state & PERF_ATTACH_TASK)
2751-
jump_label_dec(&perf_sched_events);
2751+
jump_label_dec_deferred(&perf_sched_events);
27522752
if (event->attr.mmap || event->attr.mmap_data)
27532753
atomic_dec(&nr_mmap_events);
27542754
if (event->attr.comm)
@@ -2759,7 +2759,7 @@ static void free_event(struct perf_event *event)
27592759
put_callchain_buffers();
27602760
if (is_cgroup_event(event)) {
27612761
atomic_dec(&per_cpu(perf_cgroup_events, event->cpu));
2762-
jump_label_dec(&perf_sched_events);
2762+
jump_label_dec_deferred(&perf_sched_events);
27632763
}
27642764
}
27652765

@@ -5784,7 +5784,7 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
57845784

57855785
if (!event->parent) {
57865786
if (event->attach_state & PERF_ATTACH_TASK)
5787-
jump_label_inc(&perf_sched_events);
5787+
jump_label_inc(&perf_sched_events.key);
57885788
if (event->attr.mmap || event->attr.mmap_data)
57895789
atomic_inc(&nr_mmap_events);
57905790
if (event->attr.comm)
@@ -6022,7 +6022,7 @@ SYSCALL_DEFINE5(perf_event_open,
60226022
* - that may need work on context switch
60236023
*/
60246024
atomic_inc(&per_cpu(perf_cgroup_events, event->cpu));
6025-
jump_label_inc(&perf_sched_events);
6025+
jump_label_inc(&perf_sched_events.key);
60266026
}
60276027

60286028
/*
@@ -6868,6 +6868,9 @@ void __init perf_event_init(void)
68686868

68696869
ret = init_hw_breakpoint();
68706870
WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
6871+
6872+
/* do not patch jump label more than once per second */
6873+
jump_label_rate_limit(&perf_sched_events, HZ);
68716874
}
68726875

68736876
static int __init perf_event_sysfs_init(void)

‎kernel/jump_label.c‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,46 @@ void jump_label_inc(struct jump_label_key *key)
7272
jump_label_unlock();
7373
}
7474

75-
void jump_label_dec(struct jump_label_key *key)
75+
static void __jump_label_dec(struct jump_label_key *key,
76+
unsigned long rate_limit, struct delayed_work *work)
7677
{
7778
if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex))
7879
return;
7980

80-
jump_label_update(key, JUMP_LABEL_DISABLE);
81+
if (rate_limit) {
82+
atomic_inc(&key->enabled);
83+
schedule_delayed_work(work, rate_limit);
84+
} else
85+
jump_label_update(key, JUMP_LABEL_DISABLE);
86+
8187
jump_label_unlock();
8288
}
8389

90+
static void jump_label_update_timeout(struct work_struct *work)
91+
{
92+
struct jump_label_key_deferred *key =
93+
container_of(work, struct jump_label_key_deferred, work.work);
94+
__jump_label_dec(&key->key, 0, NULL);
95+
}
96+
97+
void jump_label_dec(struct jump_label_key *key)
98+
{
99+
__jump_label_dec(key, 0, NULL);
100+
}
101+
102+
void jump_label_dec_deferred(struct jump_label_key_deferred *key)
103+
{
104+
__jump_label_dec(&key->key, key->timeout, &key->work);
105+
}
106+
107+
108+
void jump_label_rate_limit(struct jump_label_key_deferred *key,
109+
unsigned long rl)
110+
{
111+
key->timeout = rl;
112+
INIT_DELAYED_WORK(&key->work, jump_label_update_timeout);
113+
}
114+
84115
static int addr_conflict(struct jump_entry *entry, void *start, void *end)
85116
{
86117
if (entry->code <= (unsigned long)end &&

0 commit comments

Comments
 (0)