https://github.com/torvalds/linux
Revision f7d8a19f9a056a05c5c509fa65af472a322abfee authored by Sean Christopherson on 13 October 2021, 00:35:53 UTC, committed by Paolo Bonzini on 18 October 2021, 18:07:18 UTC
Revert a change to open code bits of kvm_lapic_set_base() when emulating
APIC RESET to fix an apic_hw_disabled underflow bug due to arch.apic_base
and apic_hw_disabled being unsyncrhonized when the APIC is created.  If
kvm_arch_vcpu_create() fails after creating the APIC, kvm_free_lapic()
will see the initialized-to-zero vcpu->arch.apic_base and decrement
apic_hw_disabled without KVM ever having incremented apic_hw_disabled.

Using kvm_lapic_set_base() in kvm_lapic_reset() is also desirable for a
potential future where KVM supports RESET outside of vCPU creation, in
which case all the side effects of kvm_lapic_set_base() are needed, e.g.
to handle the transition from x2APIC => xAPIC.

Alternatively, KVM could temporarily increment apic_hw_disabled (and call
kvm_lapic_set_base() at RESET), but that's a waste of cycles and would
impact the performance of other vCPUs and VMs.  The other subtle side
effect is that updating the xAPIC ID needs to be done at RESET regardless
of whether the APIC was previously enabled, i.e. kvm_lapic_reset() needs
an explicit call to kvm_apic_set_xapic_id() regardless of whether or not
kvm_lapic_set_base() also performs the update.  That makes stuffing the
enable bit at vCPU creation slightly more palatable, as doing so affects
only the apic_hw_disabled key.

Opportunistically tweak the comment to explicitly call out the connection
between vcpu->arch.apic_base and apic_hw_disabled, and add a comment to
call out the need to always do kvm_apic_set_xapic_id() at RESET.

Underflow scenario:

  kvm_vm_ioctl() {
    kvm_vm_ioctl_create_vcpu() {
      kvm_arch_vcpu_create() {
        if (something_went_wrong)
          goto fail_free_lapic;
        /* vcpu->arch.apic_base is initialized when something_went_wrong is false. */
        kvm_vcpu_reset() {
          kvm_lapic_reset(struct kvm_vcpu *vcpu, bool init_event) {
            vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE;
          }
        }
        return 0;
      fail_free_lapic:
        kvm_free_lapic() {
          /* vcpu->arch.apic_base is not yet initialized when something_went_wrong is true. */
          if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
            static_branch_slow_dec_deferred(&apic_hw_disabled); // <= underflow bug.
        }
        return r;
      }
    }
  }

This (mostly) reverts commit 421221234ada41b4a9f0beeb08e30b07388bd4bd.

Fixes: 421221234ada ("KVM: x86: Open code necessary bits of kvm_lapic_set_base() at vCPU RESET")
Reported-by: syzbot+9fc046ab2b0cf295a063@syzkaller.appspotmail.com
Debugged-by: Tetsuo Handa <penguin-kernel@i-love.sakura.ne.jp>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Message-Id: <20211013003554.47705-2-seanjc@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent baa1e5c
History
Tip revision: f7d8a19f9a056a05c5c509fa65af472a322abfee authored by Sean Christopherson on 13 October 2021, 00:35:53 UTC
Revert "KVM: x86: Open code necessary bits of kvm_lapic_set_base() at vCPU RESET"
Tip revision: f7d8a19
File Mode Size
atomic
basic
clang-tools
coccinelle
dtc
dummy-tools
gcc-plugins
gdb
genksyms
kconfig
ksymoops
mod
package
selinux
tracing
.gitignore -rw-r--r-- 160 bytes
Kbuild.include -rw-r--r-- 9.3 KB
Kconfig.include -rw-r--r-- 2.6 KB
Lindent -rwxr-xr-x 502 bytes
Makefile -rw-r--r-- 1.6 KB
Makefile.asm-generic -rw-r--r-- 1.8 KB
Makefile.build -rw-r--r-- 19.0 KB
Makefile.clang -rw-r--r-- 1.6 KB
Makefile.clean -rw-r--r-- 2.2 KB
Makefile.compiler -rw-r--r-- 2.7 KB
Makefile.dtbinst -rw-r--r-- 999 bytes
Makefile.extrawarn -rw-r--r-- 2.8 KB
Makefile.gcc-plugins -rw-r--r-- 2.5 KB
Makefile.headersinst -rw-r--r-- 2.9 KB
Makefile.host -rw-r--r-- 4.6 KB
Makefile.kasan -rw-r--r-- 1.7 KB
Makefile.kcov -rw-r--r-- 333 bytes
Makefile.kcsan -rw-r--r-- 739 bytes
Makefile.lib -rw-r--r-- 18.5 KB
Makefile.modfinal -rw-r--r-- 2.7 KB
Makefile.modinst -rw-r--r-- 2.4 KB
Makefile.modpost -rw-r--r-- 4.4 KB
Makefile.package -rw-r--r-- 6.6 KB
Makefile.ubsan -rw-r--r-- 837 bytes
Makefile.userprogs -rw-r--r-- 1.6 KB
adjust_autoksyms.sh -rwxr-xr-x 2.1 KB
as-version.sh -rwxr-xr-x 2.0 KB
asn1_compiler.c -rw-r--r-- 35.3 KB
bin2c.c -rw-r--r-- 743 bytes
bloat-o-meter -rwxr-xr-x 3.4 KB
bootgraph.pl -rwxr-xr-x 5.6 KB
bpf_doc.py -rwxr-xr-x 25.0 KB
cc-can-link.sh -rwxr-xr-x 166 bytes
cc-version.sh -rwxr-xr-x 1.5 KB
check-sysctl-docs -rwxr-xr-x 4.4 KB
check_extable.sh -rwxr-xr-x 4.9 KB
checkdeclares.pl -rwxr-xr-x 1.1 KB
checkincludes.pl -rwxr-xr-x 1.9 KB
checkkconfigsymbols.py -rwxr-xr-x 15.4 KB
checkpatch.pl -rwxr-xr-x 223.7 KB
checkstack.pl -rwxr-xr-x 5.9 KB
checksyscalls.sh -rwxr-xr-x 7.3 KB
checkversion.pl -rwxr-xr-x 2.2 KB
cleanfile -rwxr-xr-x 3.5 KB
cleanpatch -rwxr-xr-x 5.1 KB
coccicheck -rwxr-xr-x 7.9 KB
config -rwxr-xr-x 4.7 KB
const_structs.checkpatch -rw-r--r-- 1009 bytes
decode_stacktrace.sh -rwxr-xr-x 6.6 KB
decodecode -rwxr-xr-x 2.9 KB
depmod.sh -rwxr-xr-x 1.4 KB
dev-needs.sh -rwxr-xr-x 6.1 KB
diffconfig -rwxr-xr-x 3.7 KB
documentation-file-ref-check -rwxr-xr-x 5.6 KB
export_report.pl -rwxr-xr-x 4.5 KB
extract-cert.c -rw-r--r-- 3.5 KB
extract-ikconfig -rwxr-xr-x 1.7 KB
extract-module-sig.pl -rwxr-xr-x 3.7 KB
extract-sys-certs.pl -rwxr-xr-x 3.7 KB
extract-vmlinux -rwxr-xr-x 1.7 KB
extract_xc3028.pl -rwxr-xr-x 44.6 KB
faddr2line -rwxr-xr-x 6.2 KB
file-size.sh -rwxr-xr-x 86 bytes
find-unused-docs.sh -rwxr-xr-x 1.3 KB
gcc-goto.sh -rwxr-xr-x 511 bytes
gcc-ld -rwxr-xr-x 711 bytes
gcc-x86_32-has-stack-protector.sh -rwxr-xr-x 408 bytes
gcc-x86_64-has-stack-protector.sh -rwxr-xr-x 198 bytes
gen_autoksyms.sh -rwxr-xr-x 1.5 KB
gen_ksymdeps.sh -rwxr-xr-x 556 bytes
generate_initcall_order.pl -rwxr-xr-x 5.9 KB
get_abi.pl -rwxr-xr-x 15.1 KB
get_dvb_firmware -rwxr-xr-x 24.5 KB
get_feat.pl -rwxr-xr-x 14.3 KB
get_maintainer.pl -rwxr-xr-x 67.1 KB
gfp-translate -rwxr-xr-x 1.7 KB
headerdep.pl -rwxr-xr-x 3.5 KB
headers_check.pl -rwxr-xr-x 3.7 KB
headers_install.sh -rwxr-xr-x 3.4 KB
insert-sys-cert.c -rw-r--r-- 8.9 KB
jobserver-exec -rwxr-xr-x 2.2 KB
kallsyms.c -rw-r--r-- 18.1 KB
kernel-doc -rwxr-xr-x 68.2 KB
ld-version.sh -rwxr-xr-x 1.7 KB
leaking_addresses.pl -rwxr-xr-x 12.8 KB
link-vmlinux.sh -rwxr-xr-x 11.0 KB
makelst -rwxr-xr-x 808 bytes
markup_oops.pl -rwxr-xr-x 7.9 KB
min-tool-version.sh -rwxr-xr-x 558 bytes
mkcompile_h -rwxr-xr-x 2.5 KB
mksysmap -rwxr-xr-x 1.3 KB
mkuboot.sh -rwxr-xr-x 414 bytes
module.lds.S -rw-r--r-- 1.5 KB
modules-check.sh -rwxr-xr-x 427 bytes
nsdeps -rw-r--r-- 1.7 KB
objdiff -rwxr-xr-x 2.8 KB
parse-maintainers.pl -rwxr-xr-x 4.5 KB
patch-kernel -rwxr-xr-x 9.9 KB
profile2linkerlist.pl -rwxr-xr-x 414 bytes
prune-kernel -rwxr-xr-x 708 bytes
recordmcount.c -rw-r--r-- 16.7 KB
recordmcount.h -rw-r--r-- 19.4 KB
recordmcount.pl -rwxr-xr-x 17.6 KB
remove-stale-files -rwxr-xr-x 1.3 KB
setlocalversion -rwxr-xr-x 3.5 KB
show_delta -rwxr-xr-x 3.0 KB
sign-file.c -rw-r--r-- 9.8 KB
sorttable.c -rw-r--r-- 8.7 KB
sorttable.h -rw-r--r-- 9.7 KB
spdxcheck-test.sh -rw-r--r-- 277 bytes
spdxcheck.py -rwxr-xr-x 10.1 KB
spelling.txt -rw-r--r-- 31.9 KB
sphinx-pre-install -rwxr-xr-x 24.3 KB
split-man.pl -rwxr-xr-x 604 bytes
stackdelta -rwxr-xr-x 1.8 KB
stackusage -rwxr-xr-x 794 bytes
subarch.include -rw-r--r-- 619 bytes
syscallhdr.sh -rwxr-xr-x 1.9 KB
syscallnr.sh -rwxr-xr-x 1.5 KB
syscalltbl.sh -rwxr-xr-x 1.5 KB
tags.sh -rwxr-xr-x 9.6 KB
tools-support-relr.sh -rwxr-xr-x 546 bytes
unifdef.c -rw-r--r-- 34.8 KB
ver_linux -rwxr-xr-x 2.6 KB
xen-hypercalls.sh -rwxr-xr-x 386 bytes
xz_wrap.sh -rwxr-xr-x 563 bytes

back to top