Skip to content

Commit 320af55

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86: Add wrappers for setting/clearing APICv inhibits
Add set/clear wrappers for toggling APICv inhibits to make the call sites more readable, and opportunistically rename the inner helpers to align with the new wrappers and to make them more readable as well. Invert the flag from "activate" to "set"; activate is painfully ambiguous as it's not obvious if the inhibit is being activated, or if APICv is being activated, in which case the inhibit is being deactivated. For the functions that take @set, swap the order of the inhibit reason and @set so that the call sites are visually similar to those that bounce through the wrapper. Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20220311043517.17027-3-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 7491b7b commit 320af55

6 files changed

Lines changed: 49 additions & 36 deletions

File tree

arch/x86/include/asm/kvm_host.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,10 +1799,22 @@ gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
17991799

18001800
bool kvm_apicv_activated(struct kvm *kvm);
18011801
void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu);
1802-
void kvm_request_apicv_update(struct kvm *kvm, bool activate,
1803-
enum kvm_apicv_inhibit reason);
1804-
void __kvm_request_apicv_update(struct kvm *kvm, bool activate,
1805-
enum kvm_apicv_inhibit reason);
1802+
void __kvm_set_or_clear_apicv_inhibit(struct kvm *kvm,
1803+
enum kvm_apicv_inhibit reason, bool set);
1804+
void kvm_set_or_clear_apicv_inhibit(struct kvm *kvm,
1805+
enum kvm_apicv_inhibit reason, bool set);
1806+
1807+
static inline void kvm_set_apicv_inhibit(struct kvm *kvm,
1808+
enum kvm_apicv_inhibit reason)
1809+
{
1810+
kvm_set_or_clear_apicv_inhibit(kvm, reason, true);
1811+
}
1812+
1813+
static inline void kvm_clear_apicv_inhibit(struct kvm *kvm,
1814+
enum kvm_apicv_inhibit reason)
1815+
{
1816+
kvm_set_or_clear_apicv_inhibit(kvm, reason, false);
1817+
}
18061818

18071819
int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
18081820

arch/x86/kvm/hyperv.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,13 @@ static void synic_update_vector(struct kvm_vcpu_hv_synic *synic,
122122
else
123123
hv->synic_auto_eoi_used--;
124124

125-
__kvm_request_apicv_update(vcpu->kvm,
126-
!hv->synic_auto_eoi_used,
127-
APICV_INHIBIT_REASON_HYPERV);
125+
/*
126+
* Inhibit APICv if any vCPU is using SynIC's AutoEOI, which relies on
127+
* the hypervisor to manually inject IRQs.
128+
*/
129+
__kvm_set_or_clear_apicv_inhibit(vcpu->kvm,
130+
APICV_INHIBIT_REASON_HYPERV,
131+
!!hv->synic_auto_eoi_used);
128132

129133
up_write(&vcpu->kvm->arch.apicv_update_lock);
130134
}

arch/x86/kvm/i8254.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,13 @@ void kvm_pit_set_reinject(struct kvm_pit *pit, bool reinject)
305305
* So, deactivate APICv when PIT is in reinject mode.
306306
*/
307307
if (reinject) {
308-
kvm_request_apicv_update(kvm, false,
309-
APICV_INHIBIT_REASON_PIT_REINJ);
308+
kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PIT_REINJ);
310309
/* The initial state is preserved while ps->reinject == 0. */
311310
kvm_pit_reset_reinject(pit);
312311
kvm_register_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
313312
kvm_register_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
314313
} else {
315-
kvm_request_apicv_update(kvm, true,
316-
APICV_INHIBIT_REASON_PIT_REINJ);
314+
kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PIT_REINJ);
317315
kvm_unregister_irq_ack_notifier(kvm, &ps->irq_ack_notifier);
318316
kvm_unregister_irq_mask_notifier(kvm, 0, &pit->mask_notifier);
319317
}

arch/x86/kvm/svm/svm.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2918,7 +2918,7 @@ static int interrupt_window_interception(struct kvm_vcpu *vcpu)
29182918
* In this case AVIC was temporarily disabled for
29192919
* requesting the IRQ window and we have to re-enable it.
29202920
*/
2921-
kvm_request_apicv_update(vcpu->kvm, true, APICV_INHIBIT_REASON_IRQWIN);
2921+
kvm_clear_apicv_inhibit(vcpu->kvm, APICV_INHIBIT_REASON_IRQWIN);
29222922

29232923
++vcpu->stat.irq_window_exits;
29242924
return 1;
@@ -3516,7 +3516,7 @@ static void svm_enable_irq_window(struct kvm_vcpu *vcpu)
35163516
* via AVIC. In such case, we need to temporarily disable AVIC,
35173517
* and fallback to injecting IRQ via V_IRQ.
35183518
*/
3519-
kvm_request_apicv_update(vcpu->kvm, false, APICV_INHIBIT_REASON_IRQWIN);
3519+
kvm_set_apicv_inhibit(vcpu->kvm, APICV_INHIBIT_REASON_IRQWIN);
35203520
svm_set_vintr(svm);
35213521
}
35223522
}
@@ -3948,6 +3948,7 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
39483948
{
39493949
struct vcpu_svm *svm = to_svm(vcpu);
39503950
struct kvm_cpuid_entry2 *best;
3951+
struct kvm *kvm = vcpu->kvm;
39513952

39523953
vcpu->arch.xsaves_enabled = guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) &&
39533954
boot_cpu_has(X86_FEATURE_XSAVE) &&
@@ -3974,16 +3975,14 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
39743975
* is exposed to the guest, disable AVIC.
39753976
*/
39763977
if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC))
3977-
kvm_request_apicv_update(vcpu->kvm, false,
3978-
APICV_INHIBIT_REASON_X2APIC);
3978+
kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_X2APIC);
39793979

39803980
/*
39813981
* Currently, AVIC does not work with nested virtualization.
39823982
* So, we disable AVIC when cpuid for SVM is set in the L1 guest.
39833983
*/
39843984
if (nested && guest_cpuid_has(vcpu, X86_FEATURE_SVM))
3985-
kvm_request_apicv_update(vcpu->kvm, false,
3986-
APICV_INHIBIT_REASON_NESTED);
3985+
kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_NESTED);
39873986
}
39883987
init_vmcb_after_set_cpuid(vcpu);
39893988
}

arch/x86/kvm/trace.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,17 +1340,17 @@ TRACE_EVENT(kvm_hv_stimer_cleanup,
13401340
);
13411341

13421342
TRACE_EVENT(kvm_apicv_update_request,
1343-
TP_PROTO(bool activate, int reason),
1344-
TP_ARGS(activate, reason),
1343+
TP_PROTO(int reason, bool activate),
1344+
TP_ARGS(reason, activate),
13451345

13461346
TP_STRUCT__entry(
1347-
__field(bool, activate)
13481347
__field(int, reason)
1348+
__field(bool, activate)
13491349
),
13501350

13511351
TP_fast_assign(
1352-
__entry->activate = activate;
13531352
__entry->reason = reason;
1353+
__entry->activate = activate;
13541354
),
13551355

13561356
TP_printk("%s reason=%u",

arch/x86/kvm/x86.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5938,7 +5938,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
59385938
smp_wmb();
59395939
kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
59405940
kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
5941-
kvm_request_apicv_update(kvm, true, APICV_INHIBIT_REASON_ABSENT);
5941+
kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_ABSENT);
59425942
r = 0;
59435943
split_irqchip_unlock:
59445944
mutex_unlock(&kvm->lock);
@@ -6335,7 +6335,7 @@ long kvm_arch_vm_ioctl(struct file *filp,
63356335
/* Write kvm->irq_routing before enabling irqchip_in_kernel. */
63366336
smp_wmb();
63376337
kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
6338-
kvm_request_apicv_update(kvm, true, APICV_INHIBIT_REASON_ABSENT);
6338+
kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_ABSENT);
63396339
create_irqchip_unlock:
63406340
mutex_unlock(&kvm->lock);
63416341
break;
@@ -9744,8 +9744,8 @@ void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu)
97449744
}
97459745
EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv);
97469746

9747-
void __kvm_request_apicv_update(struct kvm *kvm, bool activate,
9748-
enum kvm_apicv_inhibit reason)
9747+
void __kvm_set_or_clear_apicv_inhibit(struct kvm *kvm,
9748+
enum kvm_apicv_inhibit reason, bool set)
97499749
{
97509750
unsigned long old, new;
97519751

@@ -9756,13 +9756,13 @@ void __kvm_request_apicv_update(struct kvm *kvm, bool activate,
97569756

97579757
old = new = kvm->arch.apicv_inhibit_reasons;
97589758

9759-
if (activate)
9760-
__clear_bit(reason, &new);
9761-
else
9759+
if (set)
97629760
__set_bit(reason, &new);
9761+
else
9762+
__clear_bit(reason, &new);
97639763

97649764
if (!!old != !!new) {
9765-
trace_kvm_apicv_update_request(activate, reason);
9765+
trace_kvm_apicv_update_request(reason, !set);
97669766
/*
97679767
* Kick all vCPUs before setting apicv_inhibit_reasons to avoid
97689768
* false positives in the sanity check WARN in svm_vcpu_run().
@@ -9786,17 +9786,17 @@ void __kvm_request_apicv_update(struct kvm *kvm, bool activate,
97869786
}
97879787
}
97889788

9789-
void kvm_request_apicv_update(struct kvm *kvm, bool activate,
9790-
enum kvm_apicv_inhibit reason)
9789+
void kvm_set_or_clear_apicv_inhibit(struct kvm *kvm,
9790+
enum kvm_apicv_inhibit reason, bool set)
97919791
{
97929792
if (!enable_apicv)
97939793
return;
97949794

97959795
down_write(&kvm->arch.apicv_update_lock);
9796-
__kvm_request_apicv_update(kvm, activate, reason);
9796+
__kvm_set_or_clear_apicv_inhibit(kvm, reason, set);
97979797
up_write(&kvm->arch.apicv_update_lock);
97989798
}
9799-
EXPORT_SYMBOL_GPL(kvm_request_apicv_update);
9799+
EXPORT_SYMBOL_GPL(kvm_set_or_clear_apicv_inhibit);
98009800

98019801
static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
98029802
{
@@ -10944,19 +10944,19 @@ int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
1094410944

1094510945
static void kvm_arch_vcpu_guestdbg_update_apicv_inhibit(struct kvm *kvm)
1094610946
{
10947-
bool inhibit = false;
10947+
bool set = false;
1094810948
struct kvm_vcpu *vcpu;
1094910949
unsigned long i;
1095010950

1095110951
down_write(&kvm->arch.apicv_update_lock);
1095210952

1095310953
kvm_for_each_vcpu(i, vcpu, kvm) {
1095410954
if (vcpu->guest_debug & KVM_GUESTDBG_BLOCKIRQ) {
10955-
inhibit = true;
10955+
set = true;
1095610956
break;
1095710957
}
1095810958
}
10959-
__kvm_request_apicv_update(kvm, !inhibit, APICV_INHIBIT_REASON_BLOCKIRQ);
10959+
__kvm_set_or_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_BLOCKIRQ, set);
1096010960
up_write(&kvm->arch.apicv_update_lock);
1096110961
}
1096210962

0 commit comments

Comments
 (0)