Sashiko pointed out a sleeping in atomic bug in kvm_pmu_nested_transition(). BUG: sleeping function called from invalid context at ./include/linux/sched/mm.h:322 kmem_cache_alloc_node_noprof (mm/slub.c:4679) perf_event_alloc (kernel/events/core.c:13522) perf_event_create_kernel_counter (kernel/events/core.c:14500 kernel/events/core.c:14477) kvm_pmu_create_perf_event (arch/arm64/kvm/pmu-emul.c:743 (discriminator 1)) kvm_pmu_nested_transition (arch/arm64/kvm/pmu-emul.c:1360) kvm_inject_nested (arch/arm64/kvm/emulate-nested.c:2932) kvm_inject_nested_serror (arch/arm64/kvm/emulate-nested.c:2984) kvm_inject_serror_esr (arch/arm64/kvm/inject_fault.c:365) kvm_nested_sync_hwstate (arch/arm64/kvm/nested.c:2245 (discriminator 1)) kvm_arch_vcpu_ioctl_run (arch/arm64/kvm/arm.c:1560) The problem is that kvm_arch_vcpu_ioctl_run() calls preempt_disable() and perf_event_alloc() does a sleeping allocation. Fix this by moving the transition into a vCPU request. It is safe to delay the transition because the request is still handled before the vcpu runs again. Reported-by: Sashiko Closes: https://lore.kernel.org/all/sashiko-outbox-232776@kernel.org/ Suggested-by: Oliver Upton Fixes: ae323e035801 ("KVM: arm64: nv: Reprogram PMU events affected by nested transition") Cc: stable@vger.kernel.org Signed-off-by: Dan Carpenter --- v3: New patch --- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/arm.c | 3 +++ arch/arm64/kvm/emulate-nested.c | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 0614c3b3a40d..6a22156c9107 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -62,6 +62,7 @@ #define KVM_REQ_MAP_L1_VNCR_EL2 KVM_ARCH_REQ(10) #define KVM_REQ_VGIC_PROCESS_UPDATE KVM_ARCH_REQ(11) #define KVM_REQ_RELOAD_GICv5 KVM_ARCH_REQ(12) +#define KVM_REQ_NESTED_PMU_TRANSITION KVM_ARCH_REQ(13) /* Bit indices for kvm_arch::flags */ enum { diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index a148e80d0aef..fe5e4e47b935 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -1310,6 +1310,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu) return 0; check_nested_vcpu_requests(vcpu); + + if (kvm_check_request(KVM_REQ_NESTED_PMU_TRANSITION, vcpu)) + kvm_pmu_nested_transition(vcpu); } return 1; diff --git a/arch/arm64/kvm/emulate-nested.c b/arch/arm64/kvm/emulate-nested.c index 39136a89d74b..ee71dfc4b861 100644 --- a/arch/arm64/kvm/emulate-nested.c +++ b/arch/arm64/kvm/emulate-nested.c @@ -2837,7 +2837,7 @@ void kvm_emulate_nested_eret(struct kvm_vcpu *vcpu) preempt_enable(); if (kvm_vcpu_has_pmu(vcpu)) - kvm_pmu_nested_transition(vcpu); + kvm_make_request(KVM_REQ_NESTED_PMU_TRANSITION, vcpu); } static void kvm_inject_el2_exception(struct kvm_vcpu *vcpu, u64 esr_el2, @@ -2929,7 +2929,7 @@ static int kvm_inject_nested(struct kvm_vcpu *vcpu, u64 esr_el2, preempt_enable(); if (kvm_vcpu_has_pmu(vcpu)) - kvm_pmu_nested_transition(vcpu); + kvm_make_request(KVM_REQ_NESTED_PMU_TRANSITION, vcpu); return 1; } -- 2.53.0