Skip to content

Commit d0e536d

Browse files
committed
context_tracking: avoid irq_save/irq_restore on guest entry and exit
guest_enter and guest_exit must be called with interrupts disabled, since they take the vtime_seqlock with write_seq{lock,unlock}. Therefore, it is not necessary to check for exceptions, nor to save/restore the IRQ state, when context tracking functions are called by guest_enter and guest_exit. Split the body of context_tracking_entry and context_tracking_exit out to __-prefixed functions, and use them from KVM. Rik van Riel has measured this to speed up a tight vmentry/vmexit loop by about 2%. Cc: Andy Lutomirski <luto@kernel.org> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Paul McKenney <paulmck@linux.vnet.ibm.com> Reviewed-by: Rik van Riel <riel@redhat.com> Tested-by: Rik van Riel <riel@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent f70cd6b commit d0e536d

2 files changed

Lines changed: 44 additions & 28 deletions

File tree

‎include/linux/context_tracking.h‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#ifdef CONFIG_CONTEXT_TRACKING
1111
extern void context_tracking_cpu_set(int cpu);
1212

13+
/* Called with interrupts disabled. */
14+
extern void __context_tracking_enter(enum ctx_state state);
15+
extern void __context_tracking_exit(enum ctx_state state);
16+
1317
extern void context_tracking_enter(enum ctx_state state);
1418
extern void context_tracking_exit(enum ctx_state state);
1519
extern void context_tracking_user_enter(void);
@@ -88,13 +92,13 @@ static inline void guest_enter(void)
8892
current->flags |= PF_VCPU;
8993

9094
if (context_tracking_is_enabled())
91-
context_tracking_enter(CONTEXT_GUEST);
95+
__context_tracking_enter(CONTEXT_GUEST);
9296
}
9397

9498
static inline void guest_exit(void)
9599
{
96100
if (context_tracking_is_enabled())
97-
context_tracking_exit(CONTEXT_GUEST);
101+
__context_tracking_exit(CONTEXT_GUEST);
98102

99103
if (vtime_accounting_enabled())
100104
vtime_guest_exit(current);

‎kernel/context_tracking.c‎

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,13 @@ static void context_tracking_recursion_exit(void)
5858
* instructions to execute won't use any RCU read side critical section
5959
* because this function sets RCU in extended quiescent state.
6060
*/
61-
void context_tracking_enter(enum ctx_state state)
61+
void __context_tracking_enter(enum ctx_state state)
6262
{
63-
unsigned long flags;
64-
65-
/*
66-
* Some contexts may involve an exception occuring in an irq,
67-
* leading to that nesting:
68-
* rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
69-
* This would mess up the dyntick_nesting count though. And rcu_irq_*()
70-
* helpers are enough to protect RCU uses inside the exception. So
71-
* just return immediately if we detect we are in an IRQ.
72-
*/
73-
if (in_interrupt())
74-
return;
75-
7663
/* Kernel threads aren't supposed to go to userspace */
7764
WARN_ON_ONCE(!current->mm);
7865

79-
local_irq_save(flags);
8066
if (!context_tracking_recursion_enter())
81-
goto out_irq_restore;
67+
return;
8268

8369
if ( __this_cpu_read(context_tracking.state) != state) {
8470
if (__this_cpu_read(context_tracking.active)) {
@@ -111,7 +97,27 @@ void context_tracking_enter(enum ctx_state state)
11197
__this_cpu_write(context_tracking.state, state);
11298
}
11399
context_tracking_recursion_exit();
114-
out_irq_restore:
100+
}
101+
NOKPROBE_SYMBOL(__context_tracking_enter);
102+
EXPORT_SYMBOL_GPL(__context_tracking_enter);
103+
104+
void context_tracking_enter(enum ctx_state state)
105+
{
106+
unsigned long flags;
107+
108+
/*
109+
* Some contexts may involve an exception occuring in an irq,
110+
* leading to that nesting:
111+
* rcu_irq_enter() rcu_user_exit() rcu_user_exit() rcu_irq_exit()
112+
* This would mess up the dyntick_nesting count though. And rcu_irq_*()
113+
* helpers are enough to protect RCU uses inside the exception. So
114+
* just return immediately if we detect we are in an IRQ.
115+
*/
116+
if (in_interrupt())
117+
return;
118+
119+
local_irq_save(flags);
120+
__context_tracking_enter(state);
115121
local_irq_restore(flags);
116122
}
117123
NOKPROBE_SYMBOL(context_tracking_enter);
@@ -135,16 +141,10 @@ NOKPROBE_SYMBOL(context_tracking_user_enter);
135141
* This call supports re-entrancy. This way it can be called from any exception
136142
* handler without needing to know if we came from userspace or not.
137143
*/
138-
void context_tracking_exit(enum ctx_state state)
144+
void __context_tracking_exit(enum ctx_state state)
139145
{
140-
unsigned long flags;
141-
142-
if (in_interrupt())
143-
return;
144-
145-
local_irq_save(flags);
146146
if (!context_tracking_recursion_enter())
147-
goto out_irq_restore;
147+
return;
148148

149149
if (__this_cpu_read(context_tracking.state) == state) {
150150
if (__this_cpu_read(context_tracking.active)) {
@@ -161,7 +161,19 @@ void context_tracking_exit(enum ctx_state state)
161161
__this_cpu_write(context_tracking.state, CONTEXT_KERNEL);
162162
}
163163
context_tracking_recursion_exit();
164-
out_irq_restore:
164+
}
165+
NOKPROBE_SYMBOL(__context_tracking_exit);
166+
EXPORT_SYMBOL_GPL(__context_tracking_exit);
167+
168+
void context_tracking_exit(enum ctx_state state)
169+
{
170+
unsigned long flags;
171+
172+
if (in_interrupt())
173+
return;
174+
175+
local_irq_save(flags);
176+
__context_tracking_exit(state);
165177
local_irq_restore(flags);
166178
}
167179
NOKPROBE_SYMBOL(context_tracking_exit);

0 commit comments

Comments
 (0)