Skip to content

Commit ff083a2

Browse files
sean-jcPeter Zijlstra
authored andcommitted
perf: Protect perf_guest_cbs with RCU
Protect perf_guest_cbs with RCU to fix multiple possible errors. Luckily, all paths that read perf_guest_cbs already require RCU protection, e.g. to protect the callback chains, so only the direct perf_guest_cbs touchpoints need to be modified. Bug #1 is a simple lack of WRITE_ONCE/READ_ONCE behavior to ensure perf_guest_cbs isn't reloaded between a !NULL check and a dereference. Fixed via the READ_ONCE() in rcu_dereference(). Bug #2 is that on weakly-ordered architectures, updates to the callbacks themselves are not guaranteed to be visible before the pointer is made visible to readers. Fixed by the smp_store_release() in rcu_assign_pointer() when the new pointer is non-NULL. Bug #3 is that, because the callbacks are global, it's possible for readers to run in parallel with an unregisters, and thus a module implementing the callbacks can be unloaded while readers are in flight, resulting in a use-after-free. Fixed by a synchronize_rcu() call when unregistering callbacks. Bug #1 escaped notice because it's extremely unlikely a compiler will reload perf_guest_cbs in this sequence. perf_guest_cbs does get reloaded for future derefs, e.g. for ->is_user_mode(), but the ->is_in_guest() guard all but guarantees the consumer will win the race, e.g. to nullify perf_guest_cbs, KVM has to completely exit the guest and teardown down all VMs before KVM start its module unload / unregister sequence. This also makes it all but impossible to encounter bug #3. Bug #2 has not been a problem because all architectures that register callbacks are strongly ordered and/or have a static set of callbacks. But with help, unloading kvm_intel can trigger bug #1 e.g. wrapping perf_guest_cbs with READ_ONCE in perf_misc_flags() while spamming kvm_intel module load/unload leads to: BUG: kernel NULL pointer dereference, address: 0000000000000000 #PF: supervisor read access in kernel mode #PF: error_code(0x0000) - not-present page PGD 0 P4D 0 Oops: 0000 [#1] PREEMPT SMP CPU: 6 PID: 1825 Comm: stress Not tainted 5.14.0-rc2+ #459 Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 0.0.0 02/06/2015 RIP: 0010:perf_misc_flags+0x1c/0x70 Call Trace: perf_prepare_sample+0x53/0x6b0 perf_event_output_forward+0x67/0x160 __perf_event_overflow+0x52/0xf0 handle_pmi_common+0x207/0x300 intel_pmu_handle_irq+0xcf/0x410 perf_event_nmi_handler+0x28/0x50 nmi_handle+0xc7/0x260 default_do_nmi+0x6b/0x170 exc_nmi+0x103/0x130 asm_exc_nmi+0x76/0xbf Fixes: 39447b3 ("perf: Enhance perf to allow for guest statistic collection from host") Signed-off-by: Sean Christopherson <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Paolo Bonzini <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected]
1 parent fa55b7d commit ff083a2

File tree

9 files changed

+82
-35
lines changed

9 files changed

+82
-35
lines changed

arch/arm/kernel/perf_callchain.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,10 @@ user_backtrace(struct frame_tail __user *tail,
6262
void
6363
perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
6464
{
65+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
6566
struct frame_tail __user *tail;
6667

67-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
68+
if (guest_cbs && guest_cbs->is_in_guest()) {
6869
/* We don't support guest os callchain now */
6970
return;
7071
}
@@ -98,9 +99,10 @@ callchain_trace(struct stackframe *fr,
9899
void
99100
perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
100101
{
102+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
101103
struct stackframe fr;
102104

103-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
105+
if (guest_cbs && guest_cbs->is_in_guest()) {
104106
/* We don't support guest os callchain now */
105107
return;
106108
}
@@ -111,18 +113,21 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *re
111113

112114
unsigned long perf_instruction_pointer(struct pt_regs *regs)
113115
{
114-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
115-
return perf_guest_cbs->get_guest_ip();
116+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
117+
118+
if (guest_cbs && guest_cbs->is_in_guest())
119+
return guest_cbs->get_guest_ip();
116120

117121
return instruction_pointer(regs);
118122
}
119123

120124
unsigned long perf_misc_flags(struct pt_regs *regs)
121125
{
126+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
122127
int misc = 0;
123128

124-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
125-
if (perf_guest_cbs->is_user_mode())
129+
if (guest_cbs && guest_cbs->is_in_guest()) {
130+
if (guest_cbs->is_user_mode())
126131
misc |= PERF_RECORD_MISC_GUEST_USER;
127132
else
128133
misc |= PERF_RECORD_MISC_GUEST_KERNEL;

arch/arm64/kernel/perf_callchain.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ compat_user_backtrace(struct compat_frame_tail __user *tail,
102102
void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
103103
struct pt_regs *regs)
104104
{
105-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
105+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
106+
107+
if (guest_cbs && guest_cbs->is_in_guest()) {
106108
/* We don't support guest os callchain now */
107109
return;
108110
}
@@ -147,9 +149,10 @@ static bool callchain_trace(void *data, unsigned long pc)
147149
void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
148150
struct pt_regs *regs)
149151
{
152+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
150153
struct stackframe frame;
151154

152-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
155+
if (guest_cbs && guest_cbs->is_in_guest()) {
153156
/* We don't support guest os callchain now */
154157
return;
155158
}
@@ -160,18 +163,21 @@ void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
160163

161164
unsigned long perf_instruction_pointer(struct pt_regs *regs)
162165
{
163-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
164-
return perf_guest_cbs->get_guest_ip();
166+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
167+
168+
if (guest_cbs && guest_cbs->is_in_guest())
169+
return guest_cbs->get_guest_ip();
165170

166171
return instruction_pointer(regs);
167172
}
168173

169174
unsigned long perf_misc_flags(struct pt_regs *regs)
170175
{
176+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
171177
int misc = 0;
172178

173-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
174-
if (perf_guest_cbs->is_user_mode())
179+
if (guest_cbs && guest_cbs->is_in_guest()) {
180+
if (guest_cbs->is_user_mode())
175181
misc |= PERF_RECORD_MISC_GUEST_USER;
176182
else
177183
misc |= PERF_RECORD_MISC_GUEST_KERNEL;

arch/csky/kernel/perf_callchain.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry,
8686
void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
8787
struct pt_regs *regs)
8888
{
89+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
8990
unsigned long fp = 0;
9091

9192
/* C-SKY does not support virtualization. */
92-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
93+
if (guest_cbs && guest_cbs->is_in_guest())
9394
return;
9495

9596
fp = regs->regs[4];
@@ -110,10 +111,11 @@ void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
110111
void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
111112
struct pt_regs *regs)
112113
{
114+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
113115
struct stackframe fr;
114116

115117
/* C-SKY does not support virtualization. */
116-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
118+
if (guest_cbs && guest_cbs->is_in_guest()) {
117119
pr_warn("C-SKY does not support perf in guest mode!");
118120
return;
119121
}

arch/nds32/kernel/perf_event_cpu.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,7 @@ void
13631363
perf_callchain_user(struct perf_callchain_entry_ctx *entry,
13641364
struct pt_regs *regs)
13651365
{
1366+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
13661367
unsigned long fp = 0;
13671368
unsigned long gp = 0;
13681369
unsigned long lp = 0;
@@ -1371,7 +1372,7 @@ perf_callchain_user(struct perf_callchain_entry_ctx *entry,
13711372

13721373
leaf_fp = 0;
13731374

1374-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
1375+
if (guest_cbs && guest_cbs->is_in_guest()) {
13751376
/* We don't support guest os callchain now */
13761377
return;
13771378
}
@@ -1479,9 +1480,10 @@ void
14791480
perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
14801481
struct pt_regs *regs)
14811482
{
1483+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
14821484
struct stackframe fr;
14831485

1484-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
1486+
if (guest_cbs && guest_cbs->is_in_guest()) {
14851487
/* We don't support guest os callchain now */
14861488
return;
14871489
}
@@ -1493,20 +1495,23 @@ perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
14931495

14941496
unsigned long perf_instruction_pointer(struct pt_regs *regs)
14951497
{
1498+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
1499+
14961500
/* However, NDS32 does not support virtualization */
1497-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
1498-
return perf_guest_cbs->get_guest_ip();
1501+
if (guest_cbs && guest_cbs->is_in_guest())
1502+
return guest_cbs->get_guest_ip();
14991503

15001504
return instruction_pointer(regs);
15011505
}
15021506

15031507
unsigned long perf_misc_flags(struct pt_regs *regs)
15041508
{
1509+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
15051510
int misc = 0;
15061511

15071512
/* However, NDS32 does not support virtualization */
1508-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
1509-
if (perf_guest_cbs->is_user_mode())
1513+
if (guest_cbs && guest_cbs->is_in_guest()) {
1514+
if (guest_cbs->is_user_mode())
15101515
misc |= PERF_RECORD_MISC_GUEST_USER;
15111516
else
15121517
misc |= PERF_RECORD_MISC_GUEST_KERNEL;

arch/riscv/kernel/perf_callchain.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ static unsigned long user_backtrace(struct perf_callchain_entry_ctx *entry,
5656
void perf_callchain_user(struct perf_callchain_entry_ctx *entry,
5757
struct pt_regs *regs)
5858
{
59+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
5960
unsigned long fp = 0;
6061

6162
/* RISC-V does not support perf in guest mode. */
62-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
63+
if (guest_cbs && guest_cbs->is_in_guest())
6364
return;
6465

6566
fp = regs->s0;
@@ -78,8 +79,10 @@ static bool fill_callchain(void *entry, unsigned long pc)
7879
void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry,
7980
struct pt_regs *regs)
8081
{
82+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
83+
8184
/* RISC-V does not support perf in guest mode. */
82-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
85+
if (guest_cbs && guest_cbs->is_in_guest()) {
8386
pr_warn("RISC-V does not support perf in guest mode!");
8487
return;
8588
}

arch/x86/events/core.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,10 +2768,11 @@ static bool perf_hw_regs(struct pt_regs *regs)
27682768
void
27692769
perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
27702770
{
2771+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
27712772
struct unwind_state state;
27722773
unsigned long addr;
27732774

2774-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
2775+
if (guest_cbs && guest_cbs->is_in_guest()) {
27752776
/* TODO: We don't support guest os callchain now */
27762777
return;
27772778
}
@@ -2871,10 +2872,11 @@ perf_callchain_user32(struct pt_regs *regs, struct perf_callchain_entry_ctx *ent
28712872
void
28722873
perf_callchain_user(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs)
28732874
{
2875+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
28742876
struct stack_frame frame;
28752877
const struct stack_frame __user *fp;
28762878

2877-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
2879+
if (guest_cbs && guest_cbs->is_in_guest()) {
28782880
/* TODO: We don't support guest os callchain now */
28792881
return;
28802882
}
@@ -2951,18 +2953,21 @@ static unsigned long code_segment_base(struct pt_regs *regs)
29512953

29522954
unsigned long perf_instruction_pointer(struct pt_regs *regs)
29532955
{
2954-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
2955-
return perf_guest_cbs->get_guest_ip();
2956+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
2957+
2958+
if (guest_cbs && guest_cbs->is_in_guest())
2959+
return guest_cbs->get_guest_ip();
29562960

29572961
return regs->ip + code_segment_base(regs);
29582962
}
29592963

29602964
unsigned long perf_misc_flags(struct pt_regs *regs)
29612965
{
2966+
struct perf_guest_info_callbacks *guest_cbs = perf_get_guest_cbs();
29622967
int misc = 0;
29632968

2964-
if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
2965-
if (perf_guest_cbs->is_user_mode())
2969+
if (guest_cbs && guest_cbs->is_in_guest()) {
2970+
if (guest_cbs->is_user_mode())
29662971
misc |= PERF_RECORD_MISC_GUEST_USER;
29672972
else
29682973
misc |= PERF_RECORD_MISC_GUEST_KERNEL;

arch/x86/events/intel/core.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,6 +2837,7 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)
28372837
{
28382838
struct perf_sample_data data;
28392839
struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events);
2840+
struct perf_guest_info_callbacks *guest_cbs;
28402841
int bit;
28412842
int handled = 0;
28422843
u64 intel_ctrl = hybrid(cpuc->pmu, intel_ctrl);
@@ -2903,9 +2904,11 @@ static int handle_pmi_common(struct pt_regs *regs, u64 status)
29032904
*/
29042905
if (__test_and_clear_bit(GLOBAL_STATUS_TRACE_TOPAPMI_BIT, (unsigned long *)&status)) {
29052906
handled++;
2906-
if (unlikely(perf_guest_cbs && perf_guest_cbs->is_in_guest() &&
2907-
perf_guest_cbs->handle_intel_pt_intr))
2908-
perf_guest_cbs->handle_intel_pt_intr();
2907+
2908+
guest_cbs = perf_get_guest_cbs();
2909+
if (unlikely(guest_cbs && guest_cbs->is_in_guest() &&
2910+
guest_cbs->handle_intel_pt_intr))
2911+
guest_cbs->handle_intel_pt_intr();
29092912
else
29102913
intel_pt_interrupt();
29112914
}

include/linux/perf_event.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,18 @@ extern void perf_event_bpf_event(struct bpf_prog *prog,
12401240
enum perf_bpf_event_type type,
12411241
u16 flags);
12421242

1243-
extern struct perf_guest_info_callbacks *perf_guest_cbs;
1243+
extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs;
1244+
static inline struct perf_guest_info_callbacks *perf_get_guest_cbs(void)
1245+
{
1246+
/*
1247+
* Callbacks are RCU-protected and must be READ_ONCE to avoid reloading
1248+
* the callbacks between a !NULL check and dereferences, to ensure
1249+
* pending stores/changes to the callback pointers are visible before a
1250+
* non-NULL perf_guest_cbs is visible to readers, and to prevent a
1251+
* module from unloading callbacks while readers are active.
1252+
*/
1253+
return rcu_dereference(perf_guest_cbs);
1254+
}
12441255
extern int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
12451256
extern int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *callbacks);
12461257

kernel/events/core.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6526,18 +6526,25 @@ static void perf_pending_event(struct irq_work *entry)
65266526
* Later on, we might change it to a list if there is
65276527
* another virtualization implementation supporting the callbacks.
65286528
*/
6529-
struct perf_guest_info_callbacks *perf_guest_cbs;
6529+
struct perf_guest_info_callbacks __rcu *perf_guest_cbs;
65306530

65316531
int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
65326532
{
6533-
perf_guest_cbs = cbs;
6533+
if (WARN_ON_ONCE(rcu_access_pointer(perf_guest_cbs)))
6534+
return -EBUSY;
6535+
6536+
rcu_assign_pointer(perf_guest_cbs, cbs);
65346537
return 0;
65356538
}
65366539
EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
65376540

65386541
int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
65396542
{
6540-
perf_guest_cbs = NULL;
6543+
if (WARN_ON_ONCE(rcu_access_pointer(perf_guest_cbs) != cbs))
6544+
return -EINVAL;
6545+
6546+
rcu_assign_pointer(perf_guest_cbs, NULL);
6547+
synchronize_rcu();
65416548
return 0;
65426549
}
65436550
EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);

0 commit comments

Comments
 (0)