Re: [PATCH] KVM: arm64, x86: make kvm_arch_has_irq_bypass() inline

2025-04-18 Thread Sean Christopherson
On Fri, Apr 18, 2025, Paolo Bonzini wrote: > kvm_arch_has_irq_bypass() is a small function and even though it does > not appear in any *really* hot paths, it's also not entirely rare. > Make it inline---it also works out nicely in preparation for using it in > kvm-intel.ko and kvm-amd.ko, since the

Re: [RFC kvm-unit-tests PATCH] lib: Use __ASSEMBLER__ instead of __ASSEMBLY__

2025-03-06 Thread Sean Christopherson
On Thu, Mar 06, 2025, Thomas Huth wrote: > On 22/02/2025 02.45, Sean Christopherson wrote: > > Convert all non-x86 #ifdefs from __ASSEMBLY__ to __ASSEMBLER__, and remove > > all manual __ASSEMBLY__ #defines. __ASSEMBLY_ was inherited blindly from > > the Linux kernel, and mus

Re: [Kernel 6.12.17] [PowerPC e5500] KVM HV compilation error

2025-03-05 Thread Sean Christopherson
Odd, what commit caused this problem? Any hint as to what commit is > missing to fix it? 833f69be62ac. It most definitely should be reverted. The "dependency" for commit 87ecfdbc699c ("KVM: e500: always restore irqs") is a superficial code conflict. Oof. The same buggy patch w

Re: [PATCH 1/7] KVM: x86: Free vCPUs before freeing VM state

2025-02-25 Thread Sean Christopherson
On Wed, Feb 26, 2025, Paolo Bonzini wrote: > On 2/25/25 00:55, Sean Christopherson wrote: > > Free vCPUs before freeing any VM state, as both SVM and VMX may access > > VM state when "freeing" a vCPU that is currently "in" L2, i.e. that needs > &

Re: [PATCH 1/7] KVM: x86: Free vCPUs before freeing VM state

2025-02-25 Thread Sean Christopherson
On Tue, Feb 25, 2025, Yan Zhao wrote: > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > index 58b82d6fd77c..045c61cc7e54 100644 > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -12890,11 +12890,11 @@ void kvm_arch_destroy_vm(struct kvm *kvm) > > mutex_unlock(&

Re: [PATCH 4/7] KVM: x86: Don't load/put vCPU when unloading its MMU during teardown

2025-02-25 Thread Sean Christopherson
On Tue, Feb 25, 2025, Yan Zhao wrote: > On Mon, Feb 24, 2025 at 03:55:39PM -0800, Sean Christopherson wrote: > > Don't load (and then put) a vCPU when unloading its MMU during VM > > destruction, as nothing in kvm_mmu_unload() accesses vCPU state beyond the > > root pag

Re: [PATCH 3/7] KVM: Assert that a destroyed/freed vCPU is no longer visible

2025-02-25 Thread Sean Christopherson
On Tue, Feb 25, 2025, Yan Zhao wrote: > On Mon, Feb 24, 2025 at 03:55:38PM -0800, Sean Christopherson wrote: > > After freeing a vCPU, assert that it is no longer reachable, and that > > kvm_get_vcpu() doesn't return garbage or a pointer to some other vCPU. > > While

[PATCH 7/7] KVM: Drop kvm_arch_sync_events() now that all implementations are nops

2025-02-24 Thread Sean Christopherson
Remove kvm_arch_sync_events() now that x86 no longer uses it (no other arch has ever used it). No functional change intended. Signed-off-by: Sean Christopherson --- arch/arm64/include/asm/kvm_host.h | 2 -- arch/loongarch/include/asm/kvm_host.h | 1 - arch/mips/include/asm/kvm_host.h

[PATCH 6/7] KVM: x86: Fold guts of kvm_arch_sync_events() into kvm_arch_pre_destroy_vm()

2025-02-24 Thread Sean Christopherson
_list. Note, the PIT also needs to be stopped before IRQ routing is freed (because KVM's IRQ routing is garbage and assumes there is always non-NULL routing). Opportunistically add comments to explain why KVM stops/frees certain assets early. Signed-off-by: Sean Christopherson --- arch/x86/kvm/

[PATCH 5/7] KVM: x86: Unload MMUs during vCPU destruction, not before

2025-02-24 Thread Sean Christopherson
by-one. I.e. triggering a VM-wide request would hit a use-after-free. Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 15 +++ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 9978ed4c0917..a61dbd1f0d01

[PATCH 4/7] KVM: x86: Don't load/put vCPU when unloading its MMU during teardown

2025-02-24 Thread Sean Christopherson
Don't load (and then put) a vCPU when unloading its MMU during VM destruction, as nothing in kvm_mmu_unload() accesses vCPU state beyond the root page/address of each MMU, i.e. can't possible need to run with the vCPU loaded. Signed-off-by: Sean Christopherson --- arch/x86/kvm

[PATCH 3/7] KVM: Assert that a destroyed/freed vCPU is no longer visible

2025-02-24 Thread Sean Christopherson
adding logic to the requests path is unnecessary, and trying to make requests illegal during teardown would be a fool's errand. Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 8 1 file changed, 8 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 2

[PATCH 1/7] KVM: x86: Free vCPUs before freeing VM state

2025-02-24 Thread Sean Christopherson
g/all/20240703175618.2304869-2-aaronle...@google.com Cc: Jim Mattson Cc: Yan Zhao Cc: Rick P Edgecombe Cc: Kai Huang Cc: Isaku Yamahata Signed-off-by: Sean Christopherson --- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/x86.c b/arch/x8

[PATCH 2/7] KVM: nVMX: Process events on nested VM-Exit if injectable IRQ or NMI is pending

2025-02-24 Thread Sean Christopherson
s PIC emulation that causes KVM to make spurious KVM_REQ_EVENT requests (and apparently no one ever ran the test with split IRQ chips). Cc: sta...@vger.kernel.org Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 11 +++ 1 file changed, 11 insertions(+) diff --git a/arch/x8

[PATCH 0/7] KVM: x86: nVMX IRQ fix and VM teardown cleanups

2025-02-24 Thread Sean Christopherson
ion from destroying vCPUs, all because when guest SMP support was added, KVM had a kludgy MMU teardown flow that broken when a VM had more than one 1 vCPU. And that oddity lived on, for 18 years... Sean Christopherson (7): KVM: x86: Free vCPUs before freeing VM state KVM: nVMX: Process events o

[RFC kvm-unit-tests PATCH] lib: Use __ASSEMBLER__ instead of __ASSEMBLY__

2025-02-21 Thread Sean Christopherson
. __ASSEMBLER__ on the other hand is automatically defined by the compiler when preprocessing assembly, i.e. doesn't require manually #defines for the code to function correctly. Ignore x86, as x86 doesn't actually rely on __ASSEMBLY__ at the moment, and is undergoing a parallel cleanup. Signed-of

Re: [PATCH 3/5] KVM: Add a common kvm_run flag to communicate an exit needs completion

2025-01-13 Thread Sean Christopherson
On Mon, Jan 13, 2025, Marc Zyngier wrote: > On Mon, 13 Jan 2025 18:58:45 +, > Sean Christopherson wrote: > > > > On Mon, Jan 13, 2025, Marc Zyngier wrote: > > > On Mon, 13 Jan 2025 15:44:28 +, > > > Sean Christopherson wrote: > > > >

Re: [PATCH 3/5] KVM: Add a common kvm_run flag to communicate an exit needs completion

2025-01-13 Thread Sean Christopherson
On Mon, Jan 13, 2025, Marc Zyngier wrote: > On Mon, 13 Jan 2025 15:44:28 +, > Sean Christopherson wrote: > > > > On Sat, Jan 11, 2025, Marc Zyngier wrote: > > > On Sat, 11 Jan 2025 01:24:48 +, > > > Sean Christopherson wrote: > > > > >

Re: [PATCH 3/5] KVM: Add a common kvm_run flag to communicate an exit needs completion

2025-01-13 Thread Sean Christopherson
On Mon, Jan 13, 2025, Chao Gao wrote: > On Fri, Jan 10, 2025 at 05:24:48PM -0800, Sean Christopherson wrote: > >Add a kvm_run flag, KVM_RUN_NEEDS_COMPLETION, to communicate to userspace > >that KVM_RUN needs to be re-executed prior to save/restore in order to > >complete the

Re: [PATCH 3/5] KVM: Add a common kvm_run flag to communicate an exit needs completion

2025-01-13 Thread Sean Christopherson
On Sat, Jan 11, 2025, Marc Zyngier wrote: > On Sat, 11 Jan 2025 01:24:48 +, > Sean Christopherson wrote: > > > > Add a kvm_run flag, KVM_RUN_NEEDS_COMPLETION, to communicate to userspace > > that KVM_RUN needs to be re-executed prior to save/restore in order to >

[PATCH 5/5] KVM: selftests: Rely on KVM_RUN_NEEDS_COMPLETION to complete userspace exits

2025-01-10 Thread Sean Christopherson
Add selftests coverage for KVM_RUN_NEEDS_COMPLETION by redoing KVM_RUN if and only if KVM states that completion is required. Opportunistically rename the helper to replace "io" with "exit", as exits that require completion are no longer limited to I/O. Signed-off-by:

[PATCH 4/5] KVM: selftests: Provide separate helper for KVM_RUN with immediate_exit

2025-01-10 Thread Sean Christopherson
immediate exit. No functional change intended. Signed-off-by: Sean Christopherson --- tools/testing/selftests/kvm/include/kvm_util.h | 9 - tools/testing/selftests/kvm/lib/kvm_util.c | 2 +- tools/testing/selftests/kvm/x86/nested_exceptions_test.c | 3 +-- 3 fil

[PATCH 3/5] KVM: Add a common kvm_run flag to communicate an exit needs completion

2025-01-10 Thread Sean Christopherson
lback, i.e. in theory, this is a one-time thing for KVM x86 (and other architectures could follow suit with additional refactoring). Signed-off-by: Sean Christopherson --- Documentation/virt/kvm/api.rst| 48 ++- arch/powerpc/kvm/book3s_emulate.c | 1 + arch/powerp

[PATCH 2/5] KVM: Clear vcpu->run->flags at start of KVM_RUN for all architectures

2025-01-10 Thread Sean Christopherson
Clear kvm_run.flags at the start of KVM_RUN for all architectures to minimize the probability of leaving a stale flag set. Signed-off-by: Sean Christopherson --- arch/arm64/kvm/arm.c | 1 - arch/arm64/kvm/handle_exit.c | 2 +- arch/powerpc/kvm/book3s_hv.c | 4 +--- arch/x86/kvm/x86.c

[PATCH 0/5] KVM: Add a kvm_run flag to signal need for completion

2025-01-10 Thread Sean Christopherson
error prone. Note, this series is *very* lightly tested (borderline RFC). Sean Christopherson (5): KVM: x86: Document that KVM_EXIT_HYPERCALL requires completion KVM: Clear vcpu->run->flags at start of KVM_RUN for all architectures KVM: Add a common kvm_run flag to communicate an exit

[PATCH 1/5] KVM: x86: Document that KVM_EXIT_HYPERCALL requires completion

2025-01-10 Thread Sean Christopherson
Fixes: 0dbb11230437 ("KVM: X86: Introduce KVM_HC_MAP_GPA_RANGE hypercall") Cc: sta...@vger.kernel.org Signed-off-by: Sean Christopherson --- Documentation/virt/kvm/api.rst | 39 +- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/Documen

Re: [PATCH 5/5] KVM: e500: perform hugepage check after looking up the PFN

2025-01-09 Thread Sean Christopherson
On Thu, Jan 09, 2025, Paolo Bonzini wrote: > @@ -483,7 +383,7 @@ static inline int kvmppc_e500_shadow_map(struct > kvmppc_vcpu_e500 *vcpu_e500, >* can't run hence pfn won't change. >*/ > local_irq_save(flags); > - ptep = find_linux_pte(pgdir, hva, NULL, NULL); > + pte

Re: [PATCH 2/5] KVM: e500: use shadow TLB entry as witness for writability

2025-01-09 Thread Sean Christopherson
On Thu, Jan 09, 2025, Paolo Bonzini wrote: > kvmppc_e500_ref_setup is returning whether the guest TLB entry is writable, > which is than passed to kvm_release_faultin_page. This makes little sense s/than/then > for two reasons: first, because the function sets up the private data for > the page

Re: [PATCH 1/5] KVM: e500: retry if no memslot is found

2025-01-09 Thread Sean Christopherson
On Thu, Jan 09, 2025, Paolo Bonzini wrote: > Avoid a NULL pointer dereference if the memslot table changes between the > exit and the call to kvmppc_e500_shadow_map(). > > Cc: sta...@vger.kernel.org > Signed-off-by: Paolo Bonzini > --- > arch/powerpc/kvm/e500_mmu_host.c | 5 + > 1 file chang

Re: [PATCH] KVM: allow NULL writable argument to __kvm_faultin_pfn

2025-01-08 Thread Sean Christopherson
On Mon, Jan 06, 2025, Sean Christopherson wrote: > On Wed, Jan 01, 2025, Paolo Bonzini wrote: > > kvm_follow_pfn() is able to work with NULL in the .map_writable field > > of the homonymous struct. But __kvm_faultin_pfn() rejects the combo > > despite KVM for e500 tryi

Re: [PATCH] KVM: allow NULL writable argument to __kvm_faultin_pfn

2025-01-06 Thread Sean Christopherson
On Wed, Jan 01, 2025, Paolo Bonzini wrote: > kvm_follow_pfn() is able to work with NULL in the .map_writable field > of the homonymous struct. But __kvm_faultin_pfn() rejects the combo > despite KVM for e500 trying to use it. Indeed .map_writable is not > particularly useful if the flags include

Re: [PATCH v13 00/85] KVM: Stop grabbing references to PFNMAP'd pages

2024-10-21 Thread Sean Christopherson
On Thu, Oct 17, 2024, Paolo Bonzini wrote: > On Thu, Oct 10, 2024 at 8:24 PM Sean Christopherson wrote: > > v13: > > - Rebased onto v6.12-rc2 > > - Collect reviews. [Alex and others] > > - Fix a transient bug in arm64 and RISC-V where KVM would leak a page > >

Re: [PATCH v13 51/85] KVM: VMX: Use __kvm_faultin_page() to get APIC access page/pfn

2024-10-21 Thread Sean Christopherson
On Mon, Oct 21, 2024, Yan Zhao wrote: > On Thu, Oct 10, 2024 at 11:23:53AM -0700, Sean Christopherson wrote: > > Use __kvm_faultin_page() get the APIC access page so that KVM can > > precisely release the refcounted page, i.e. to remove yet another user > > of kvm_pfn_to_refc

Re: [PATCH v13 34/85] KVM: Get writable mapping for __kvm_vcpu_map() only when necessary

2024-10-21 Thread Sean Christopherson
On Mon, Oct 21, 2024, Yan Zhao wrote: > On Thu, Oct 10, 2024 at 11:23:36AM -0700, Sean Christopherson wrote: > > When creating a memory map for read, don't request a writable pfn from the > > primary MMU. While creating read-only mappings can be theoretically slower, > &g

Re: [PATCH v13 19/85] KVM: Introduce kvm_follow_pfn() to eventually replace "gfn_to_pfn" APIs

2024-10-21 Thread Sean Christopherson
On Mon, Oct 21, 2024, Yan Zhao wrote: > On Thu, Oct 10, 2024 at 11:23:21AM -0700, Sean Christopherson wrote: > > --- a/virt/kvm/pfncache.c > > +++ b/virt/kvm/pfncache.c > > @@ -159,6 +159,12 @@ static kvm_pfn_t hva_to_pfn_retry(struct > > gfn_to_pfn_cache *gpc

[PATCH v13 85/85] KVM: Don't grab reference on VM_MIXEDMAP pfns that have a "struct page"

2024-10-10 Thread Sean Christopherson
art in KVM, this allows KVM to map non-refcounted struct page memory into the guest, e.g. for exposing GPU TTM buffers to KVM guests. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 3 -- virt/kvm/kvm_main.c | 75 ++---

[PATCH v13 84/85] KVM: Drop APIs that manipulate "struct page" via pfns

2024-10-10 Thread Sean Christopherson
Remove all kvm_{release,set}_pfn_*() APIs now that all users are gone. No functional change intended. Reviewed-by: Alex Bennée Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 5 virt/kvm/kvm_main.c | 55

[PATCH v13 83/85] KVM: arm64: Don't mark "struct page" accessed when making SPTE young

2024-10-10 Thread Sean Christopherson
push "accessed" updates to the primary MMU. Dropping use of kvm_set_pfn_accessed() also paves the way for removing kvm_pfn_to_refcounted_page() and all its users. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/arm64/include/asm/kvm_pgtable.h | 4 +--- arch/arm64/kvm/

[PATCH v13 82/85] KVM: x86/mmu: Don't mark "struct page" accessed when zapping SPTEs

2024-10-10 Thread Sean Christopherson
t KVM marks the page accessed. Dropping x86's use of kvm_set_pfn_accessed() also paves the way for removing kvm_pfn_to_refcounted_page() and all its users. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 17 - arch/x86/kvm/mmu/td

[PATCH v13 81/85] KVM: Make kvm_follow_pfn.refcounted_page a required field

2024-10-10 Thread Sean Christopherson
Now that the legacy gfn_to_pfn() APIs are gone, and all callers of hva_to_pfn() pass in a refcounted_page pointer, make it a required field to ensure all future usage in KVM plays nice. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 6 -- 1 file changed

[PATCH v13 80/85] KVM: s390: Use kvm_release_page_dirty() to unpin "struct page" memory

2024-10-10 Thread Sean Christopherson
Use kvm_release_page_dirty() when unpinning guest pages, as the pfn was retrieved via pin_guest_page(), i.e. is guaranteed to be backed by struct page memory. This will allow dropping kvm_release_pfn_dirty() and friends. Signed-off-by: Sean Christopherson --- arch/s390/kvm/vsie.c | 2 +- 1

[PATCH v13 79/85] KVM: Drop gfn_to_pfn() APIs now that all users are gone

2024-10-10 Thread Sean Christopherson
Drop gfn_to_pfn() and all its variants now that all users are gone. No functional change intended. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 11 virt/kvm/kvm_main.c | 59 2 files changed, 70

[PATCH v13 78/85] KVM: PPC: Explicitly require struct page memory for Ultravisor sharing

2024-10-10 Thread Sean Christopherson
ikely a bug fix, not a reduction in KVM capabilities. Switching to gfn_to_page() will eventually allow removing gfn_to_pfn() and kvm_pfn_to_refcounted_page(). Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/book3s_hv_uvmem.c | 25 - 1 file changed, 12 insertions(+

[PATCH v13 77/85] KVM: arm64: Use __gfn_to_page() when copying MTE tags to/from userspace

2024-10-10 Thread Sean Christopherson
Use __gfn_to_page() instead when copying MTE tags between guest and userspace. This will eventually allow removing gfn_to_pfn_prot(), gfn_to_pfn(), kvm_pfn_to_refcounted_page(), and related APIs. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/arm64/kvm/guest.c | 21

[PATCH v13 76/85] KVM: Add support for read-only usage of gfn_to_page()

2024-10-10 Thread Sean Christopherson
to do (though there are such scenarios). Add a FIXME to call out that KVM probably should be pinning pages, not just getting pages. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 7 ++- virt/kvm/kvm_main.c | 15 --- 2 files changed

[PATCH v13 75/85] KVM: Convert gfn_to_page() to use kvm_follow_pfn()

2024-10-10 Thread Sean Christopherson
Convert gfn_to_page() to the new kvm_follow_pfn() internal API, which will eventually allow removing gfn_to_pfn() and kvm_pfn_to_refcounted_page(). Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 16 +--- 1 file changed, 9 insertions(+), 7

[PATCH v13 74/85] KVM: PPC: Use kvm_vcpu_map() to map guest memory to patch dcbz instructions

2024-10-10 Thread Sean Christopherson
n the pages unpin_user_pages() INCORRECT (uses FOLL_GET calls): get_user_pages() write to the data within the pages put_page() As a happy bonus, using kvm_vcpu_{,un}map() takes care of creating a mapping and marking the page dirty. Signed-off-by:

[PATCH v13 73/85] KVM: PPC: Remove extra get_page() to fix page refcount leak

2024-10-10 Thread Sean Christopherson
Don't manually do get_page() when patching dcbz, as gfn_to_page() gifts the caller a reference. I.e. doing get_page() will leak the page due to not putting all references. Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/book3s_pr.c | 1 - 1 file changed, 1 deletion(-) diff --

[PATCH v13 72/85] KVM: MIPS: Use kvm_faultin_pfn() to map pfns into the guest

2024-10-10 Thread Sean Christopherson
Convert MIPS to kvm_faultin_pfn()+kvm_release_faultin_page(), which are new APIs to consolidate arch code and provide consistent behavior across all KVM architectures. Signed-off-by: Sean Christopherson --- arch/mips/kvm/mmu.c | 14 ++ 1 file changed, 6 insertions(+), 8 deletions

[PATCH v13 71/85] KVM: MIPS: Mark "struct page" pfns accessed prior to dropping mmu_lock

2024-10-10 Thread Sean Christopherson
Mark pages accessed before dropping mmu_lock when faulting in guest memory so that MIPS can convert to kvm_release_faultin_page() without tripping its lockdep assertion on mmu_lock being held. Signed-off-by: Sean Christopherson --- arch/mips/kvm/mmu.c | 5 ++--- 1 file changed, 2 insertions

[PATCH v13 70/85] KVM: MIPS: Mark "struct page" pfns accessed only in "slow" page fault path

2024-10-10 Thread Sean Christopherson
y MMU will call into KVM via mmu_notifiers when aging pages. I.e. KVM participates in a "pull" model, so there's no need to also "push" updates. Signed-off-by: Sean Christopherson --- arch/mips/kvm/mmu.c | 12 ++-- 1 file changed, 2 insertions(+), 10 deletions(-

[PATCH v13 69/85] KVM: MIPS: Mark "struct page" pfns dirty only in "slow" page fault path

2024-10-10 Thread Sean Christopherson
race is minuscule, hence the lack of complaints). See the link below for details. Link: https://lore.kernel.org/all/cover.1683044162.git.lstoa...@gmail.com Signed-off-by: Sean Christopherson --- arch/mips/kvm/mmu.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/mips

[PATCH v13 68/85] KVM: LoongArch: Use kvm_faultin_pfn() to map pfns into the guest

2024-10-10 Thread Sean Christopherson
Convert LoongArch to kvm_faultin_pfn()+kvm_release_faultin_page(), which are new APIs to consolidate arch code and provide consistent behavior across all KVM architectures. Signed-off-by: Sean Christopherson --- arch/loongarch/kvm/mmu.c | 14 ++ 1 file changed, 6 insertions(+), 8

[PATCH v13 67/85] KVM: LoongArch: Mark "struct page" pfn accessed before dropping mmu_lock

2024-10-10 Thread Sean Christopherson
Mark pages accessed before dropping mmu_lock when faulting in guest memory so that LoongArch can convert to kvm_release_faultin_page() without tripping its lockdep assertion on mmu_lock being held. Reviewed-by: Bibo Mao Signed-off-by: Sean Christopherson --- arch/loongarch/kvm/mmu.c | 2 +- 1

[PATCH v13 66/85] KVM: LoongArch: Mark "struct page" pfns accessed only in "slow" page fault path

2024-10-10 Thread Sean Christopherson
Mark pages accessed only in the slow path, before dropping mmu_lock when faulting in guest memory so that LoongArch can convert to kvm_release_faultin_page() without tripping its lockdep assertion on mmu_lock being held. Reviewed-by: Bibo Mao Signed-off-by: Sean Christopherson --- arch

[PATCH v13 65/85] KVM: LoongArch: Mark "struct page" pfns dirty only in "slow" page fault path

2024-10-10 Thread Sean Christopherson
race is minuscule, hence the lack of complaints). See the link below for details. Link: https://lore.kernel.org/all/cover.1683044162.git.lstoa...@gmail.com Reviewed-by: Bibo Mao Signed-off-by: Sean Christopherson --- arch/loongarch/kvm/mmu.c | 18 ++ 1 file changed, 10 insertions

[PATCH v13 64/85] KVM: PPC: Use kvm_faultin_pfn() to handle page faults on Book3s PR

2024-10-10 Thread Sean Christopherson
Convert Book3S PR to __kvm_faultin_pfn()+kvm_release_faultin_page(), which are new APIs to consolidate arch code and provide consistent behavior across all KVM architectures. Signed-off-by: Sean Christopherson --- arch/powerpc/include/asm/kvm_book3s.h | 2 +- arch/powerpc/kvm/book3s.c

[PATCH v13 63/85] KVM: PPC: Book3S: Mark "struct page" pfns dirty/accessed after installing PTE

2024-10-10 Thread Sean Christopherson
...@gmail.com Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/book3s_64_mmu_host.c | 10 +- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/arch/powerpc/kvm/book3s_64_mmu_host.c b/arch/powerpc/kvm/book3s_64_mmu_host.c index bc6a381b5346..d0e4f7bbdc3d 100644 --- a/arch

[PATCH v13 62/85] KVM: PPC: Drop unused @kvm_ro param from kvmppc_book3s_instantiate_page()

2024-10-10 Thread Sean Christopherson
Drop @kvm_ro from kvmppc_book3s_instantiate_page() as it is now only written, and never read. No functional change intended. Signed-off-by: Sean Christopherson --- arch/powerpc/include/asm/kvm_book3s.h | 2 +- arch/powerpc/kvm/book3s_64_mmu_radix.c | 6 ++ arch/powerpc/kvm

[PATCH v13 61/85] KVM: PPC: Use __kvm_faultin_pfn() to handle page faults on Book3s Radix

2024-10-10 Thread Sean Christopherson
changed (KVM tries fast GUP for all writable mappings). Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/book3s_64_mmu_radix.c | 29 +- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/arch/powerpc/kvm/book3s_64_mmu_radix.c b/arch/powerpc/kvm

[PATCH v13 60/85] KVM: PPC: Use __kvm_faultin_pfn() to handle page faults on Book3s HV

2024-10-10 Thread Sean Christopherson
es fast GUP for all writable mappings). Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/book3s_64_mmu_hv.c | 25 - 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/arch/powerpc/kvm/book3s_64_mmu_hv.c b/arch/powerpc/kvm/book3s_64_mmu_hv.c index 2f1d58984b4

[PATCH v13 59/85] KVM: RISC-V: Use kvm_faultin_pfn() when mapping pfns into the guest

2024-10-10 Thread Sean Christopherson
-off-by: Sean Christopherson --- arch/riscv/kvm/mmu.c | 11 --- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index 2e9aee518142..e11ad1b616f3 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/riscv/kvm/mmu.c @@ -601,6 +601,7 @@ int

[PATCH v13 58/85] KVM: RISC-V: Mark "struct page" pfns accessed before dropping mmu_lock

2024-10-10 Thread Sean Christopherson
outside of mmu_lock can make filesystems unhappy (see the link below). Do both under mmu_lock to minimize the chances of doing the wrong thing in the future. Link: https://lore.kernel.org/all/cover.1683044162.git.lstoa...@gmail.com Reviewed-by: Andrew Jones Acked-by: Anup Patel Signed-off-by: Sean

[PATCH v13 57/85] KVM: RISC-V: Mark "struct page" pfns dirty iff a stage-2 PTE is installed

2024-10-10 Thread Sean Christopherson
ed-by: Andrew Jones Acked-by: Anup Patel Signed-off-by: Sean Christopherson --- arch/riscv/kvm/mmu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/riscv/kvm/mmu.c b/arch/riscv/kvm/mmu.c index b63650f9b966..06aa5a0d056d 100644 --- a/arch/riscv/kvm/mmu.c +++ b/arch/risc

[PATCH v13 56/85] KVM: arm64: Use __kvm_faultin_pfn() to handle memory aborts

2024-10-10 Thread Sean Christopherson
Convert arm64 to use __kvm_faultin_pfn()+kvm_release_faultin_page(). Three down, six to go. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/arm64/kvm/mmu.c | 15 ++- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64

[PATCH v13 55/85] KVM: arm64: Mark "struct page" pfns accessed/dirty before dropping mmu_lock

2024-10-10 Thread Sean Christopherson
teback. See the link below for additional details. This will also allow converting arm64 to kvm_release_faultin_page(), which requires that mmu_lock be held (for the aforementioned reason). Link: https://lore.kernel.org/all/cover.1683044162.git.lstoa...@gmail.com Signed-off-by: Sean Christopherson

[PATCH v13 54/85] KVM: PPC: e500: Use __kvm_faultin_pfn() to handle page faults

2024-10-10 Thread Sean Christopherson
Convert PPC e500 to use __kvm_faultin_pfn()+kvm_release_faultin_page(), and continue the inexorable march towards the demise of kvm_pfn_to_refcounted_page(). Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/e500_mmu_host.c | 8 +++- 1 file changed, 3 insertions(+), 5 deletions

[PATCH v13 53/85] KVM: PPC: e500: Mark "struct page" pfn accessed before dropping mmu_lock

2024-10-10 Thread Sean Christopherson
_dirty_ outside of mmu_lock can make filesystems unhappy. Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/e500_mmu_host.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/arch/powerpc/kvm/e500_mmu_host.c b/arch/powerpc/kvm/e500_mmu_host.c index 5c2adfd19e12..334dd96f8081

[PATCH v13 52/85] KVM: PPC: e500: Mark "struct page" dirty in kvmppc_e500_shadow_map()

2024-10-10 Thread Sean Christopherson
ndant kvm_set_pfn_accessed(), as shadow_map() puts the page via kvm_release_pfn_clean(). Signed-off-by: Sean Christopherson --- arch/powerpc/kvm/e500_mmu_host.c | 13 ++--- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/arch/powerpc/kvm/e500_mmu_host.c b/arch/powerp

[PATCH v13 51/85] KVM: VMX: Use __kvm_faultin_page() to get APIC access page/pfn

2024-10-10 Thread Sean Christopherson
g the pfn into a VMCS field instead of a secondary MMU. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 13 + 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 851be0820e04..44cc25d

[PATCH v13 50/85] KVM: VMX: Hold mmu_lock until page is released when updating APIC access page

2024-10-10 Thread Sean Christopherson
eally just marking folios dirty that is problematic), but there's no reason not to be paranoid (moving the APIC access page isn't a hot path), and no reason to be different from other mmu_notifier-protected flows in KVM. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- ar

[PATCH v13 49/85] KVM: Move x86's API to release a faultin page to common KVM

2024-10-10 Thread Sean Christopherson
Move KVM x86's helper that "finishes" the faultin process to common KVM so that the logic can be shared across all architectures. Note, not all architectures implement a fast page fault path, but the gist of the comment applies to all architectures. Tested-by: Alex Bennée Sign

[PATCH v13 48/85] KVM: x86/mmu: Don't mark unused faultin pages as accessed

2024-10-10 Thread Sean Christopherson
about to access, it's far more likely that the gfn=>pfn mapping was was invalidated, e.g. due a memslot change, or because the corresponding VMA is being modified. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 4 +++- 1 file changed, 3 insertions(

[PATCH v13 47/85] KVM: x86/mmu: Put refcounted pages instead of blindly releasing pfns

2024-10-10 Thread Sean Christopherson
Now that all x86 page fault paths precisely track refcounted pages, use Use kvm_page_fault.refcounted_page to put references to struct page memory when finishing page faults. This is a baby step towards eliminating kvm_pfn_to_refcounted_page(). Tested-by: Alex Bennée Signed-off-by: Sean

[PATCH v13 46/85] KVM: guest_memfd: Provide "struct page" as output from kvm_gmem_get_pfn()

2024-10-10 Thread Sean Christopherson
Provide the "struct page" associated with a guest_memfd pfn as an output from __kvm_gmem_get_pfn() so that KVM guest page fault handlers can directly put the page instead of having to rely on kvm_pfn_to_refcounted_page(). Tested-by: Alex Bennée Signed-off-by: Sean Christopherson ---

[PATCH v13 45/85] KVM: guest_memfd: Pass index, not gfn, to __kvm_gmem_get_pfn()

2024-10-10 Thread Sean Christopherson
page", which requires the index into the folio, without a redoing the index calculation multiple times (which isn't costly, just hard to follow). Opportunistically add a kvm_gmem_get_index() helper to make the copy+pasted code easier to understand. Signed-off-by: Sean Christopherson

[PATCH v13 44/85] KVM: x86/mmu: Convert page fault paths to kvm_faultin_pfn()

2024-10-10 Thread Sean Christopherson
Convert KVM x86 to use the recently introduced __kvm_faultin_pfn(). Opportunstically capture the refcounted_page grabbed by KVM for use in future changes. No functional change intended. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 14

[PATCH v13 43/85] KVM: Add kvm_faultin_pfn() to specifically service guest page faults

2024-10-10 Thread Sean Christopherson
architectures need to be converted to a common API before that can happen. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 12 virt/kvm/kvm_main.c | 22 ++ 2 files changed, 34 insertions(+) diff --git a/include/linux/k

[PATCH v13 42/85] KVM: Move declarations of memslot accessors up in kvm_host.h

2024-10-10 Thread Sean Christopherson
Move the memslot lookup helpers further up in kvm_host.h so that they can be used by inlined "to pfn" wrappers. No functional change intended. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 8 +--- 1 file changed, 5 insertions(+), 3

[PATCH v13 41/85] KVM: x86/mmu: Mark pages/folios dirty at the origin of make_spte()

2024-10-10 Thread Sean Christopherson
page memory. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 30 -- arch/x86/kvm/mmu/paging_tmpl.h | 5 + arch/x86/kvm/mmu/spte.c| 11 --- 3 files changed, 33 insertions(+), 13 deletions(-) diff --

[PATCH v13 40/85] KVM: x86/mmu: Add helper to "finish" handling a guest page fault

2024-10-10 Thread Sean Christopherson
"finish" API across all architectures. No functional change intended. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 12 +--- arch/x86/kvm/mmu/paging_tmpl.h | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git

[PATCH v13 39/85] KVM: x86/mmu: Add common helper to handle prefetching SPTEs

2024-10-10 Thread Sean Christopherson
ennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 40 +- arch/x86/kvm/mmu/paging_tmpl.h | 13 +-- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 62924f9

[PATCH v13 38/85] KVM: x86/mmu: Put direct prefetched pages via kvm_release_page_clean()

2024-10-10 Thread Sean Christopherson
Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index e451e1b9a55a..62924f95a398 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -2965

[PATCH v13 37/85] KVM: x86/mmu: Add "mmu" prefix fault-in helpers to free up generic names

2024-10-10 Thread Sean Christopherson
Prefix x86's faultin_pfn helpers with "mmu" so that the mmu-less names can be used by common KVM for similar APIs. No functional change intended. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/mmu/mmu.c | 19 ++- a

[PATCH v13 36/85] KVM: x86: Don't fault-in APIC access page during initial allocation

2024-10-10 Thread Sean Christopherson
eturn -EFAULT on the future page fault. Suggested-by: Paolo Bonzini Signed-off-by: Sean Christopherson --- arch/x86/kvm/lapic.c | 12 1 file changed, 12 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 20526e4d6c62..65412640cfc7 100644 --- a/arch/x86/kvm/la

[PATCH v13 35/85] KVM: Disallow direct access (w/o mmu_notifier) to unpinned pfn by default

2024-10-10 Thread Sean Christopherson
necessary. The APIs are used exclusively by x86's nested virtualization support, and there is no known (or sane) use case for mapping PFN-mapped memory a KVM guest _and_ letting the guest use it for virtualization structures. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson ---

[PATCH v13 34/85] KVM: Get writable mapping for __kvm_vcpu_map() only when necessary

2024-10-10 Thread Sean Christopherson
a mapping isn't a super hot path, and getting a writable mapping for reading is weird and confusing. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/k

[PATCH v13 33/85] KVM: Pass in write/dirty to kvm_vcpu_map(), not kvm_vcpu_unmap()

2024-10-10 Thread Sean Christopherson
st GUP due to the need to break CoW before mapping the underlying PFN. But practically speaking, creating a mapping isn't a super hot path, and getting a writable mapping for reading is weird and confusing. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/svm/nested

[PATCH v13 32/85] KVM: nVMX: Mark vmcs12's APIC access page dirty when unmapping

2024-10-10 Thread Sean Christopherson
le to the guest (though obviously highly unlikely to be problematic in practice). Marking the map dirty will allow specifying the write vs. read-only when *mapping* the memory, which in turn will allow creating read-only maps. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/k

[PATCH v13 31/85] KVM: Pin (as in FOLL_PIN) pages during kvm_vcpu_map()

2024-10-10 Thread Sean Christopherson
field. Link: https://lwn.net/Articles/930667 Link: https://lore.kernel.org/all/cover.1683044162.git.lstoa...@gmail.com Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 54 +--- virt/kvm/

[PATCH v13 30/85] KVM: Migrate kvm_vcpu_map() to kvm_follow_pfn()

2024-10-10 Thread Sean Christopherson
[sean: use a pointer instead of a boolean] Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 2 +- virt/kvm/kvm_main.c | 26 -- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/include/linux/kvm_host.h b/include

[PATCH v13 29/85] KVM: pfncache: Precisely track refcounted pages

2024-10-10 Thread Sean Christopherson
Track refcounted struct page memory using kvm_follow_pfn.refcounted_page instead of relying on kvm_release_pfn_clean() to correctly detect that the pfn is associated with a struct page. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- virt/kvm/pfncache.c | 11 +++ 1 file

[PATCH v13 28/85] KVM: Move kvm_{set,release}_page_{clean,dirty}() helpers up in kvm_main.c

2024-10-10 Thread Sean Christopherson
Hoist the kvm_{set,release}_page_{clean,dirty}() APIs further up in kvm_main.c so that they can be used by the kvm_follow_pfn family of APIs. No functional change intended. Reviewed-by: Alex Bennée Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 82

[PATCH v13 27/85] KVM: Provide refcounted page as output field in struct kvm_follow_pfn

2024-10-10 Thread Sean Christopherson
is error prone and can't detect pfns that are valid, but aren't (currently) refcounted. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- virt/kvm/kvm_main.c | 99 + virt/kvm/kvm_mm.h | 9 + 2 files changed, 56 insertion

[PATCH v13 26/85] KVM: Use plain "struct page" pointer instead of single-entry array

2024-10-10 Thread Sean Christopherson
Use a single pointer instead of a single-entry array for the struct page pointer in hva_to_pfn_fast(). Using an array makes the code unnecessarily annoying to read and update. No functional change intended. Reviewed-by: Alex Bennée Tested-by: Alex Bennée Signed-off-by: Sean Christopherson

[PATCH v13 25/85] KVM: nVMX: Add helper to put (unmap) vmcs12 pages

2024-10-10 Thread Sean Christopherson
Add a helper to dedup unmapping the vmcs12 pages. This will reduce the amount of churn when a future patch refactors the kvm_vcpu_unmap() API. No functional change intended. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 32

[PATCH v13 24/85] KVM: nVMX: Drop pointless msr_bitmap_map field from struct nested_vmx

2024-10-10 Thread Sean Christopherson
Remove vcpu_vmx.msr_bitmap_map and instead use an on-stack structure in the one function that uses the map, nested_vmx_prepare_msr_bitmap(). Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 8 arch/x86/kvm/vmx/vmx.h| 2 -- 2 files changed

[PATCH v13 23/85] KVM: nVMX: Rely on kvm_vcpu_unmap() to track validity of eVMCS mapping

2024-10-10 Thread Sean Christopherson
pu_vmx, i.e. the map starts out invalid/NULL. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/nested.c | 7 ++- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c index a8e7bc04d9bf..e94a25373a59

[PATCH v13 22/85] KVM: Use NULL for struct page pointer to indicate mremapped memory

2024-10-10 Thread Sean Christopherson
e a blatant and egregious KVM bug. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson --- include/linux/kvm_host.h | 4 virt/kvm/kvm_main.c | 4 ++-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h ind

[PATCH v13 21/85] KVM: Explicitly initialize all fields at the start of kvm_vcpu_map()

2024-10-10 Thread Sean Christopherson
Explicitly initialize the entire kvm_host_map structure when mapping a pfn, as some callers declare their struct on the stack, i.e. don't zero-initialize the struct, which makes the map->hva in kvm_vcpu_unmap() *very* suspect. Tested-by: Alex Bennée Signed-off-by: Sean Christopherson -

  1   2   3   4   5   6   7   8   >