Re: [PATCH 19/27] KVM: x86/mmu: Use page-track notifiers iff there are external users

Yan Zhao
2022-12-28T07:19:31Z

On Fri, Dec 23, 2022 at 12:57:31AM +0000, Sean Christopherson wrote:
> Disable the page-track notifier code at compile time if there are no
> external users, i.e. if CONFIG_KVM_EXTERNAL_WRITE_TRACKING=n. KVM itself
> now hooks emulated writes directly instead of relying on the page-track
> mechanism.
>
> Signed-off-by: Sean Christopherson <seanjc@google.com>
> ---
> arch/x86/include/asm/kvm_host.h | 2 ++
> arch/x86/include/asm/kvm_page_track.h | 2 ++
> arch/x86/kvm/mmu/page_track.c | 9 ++++----
> arch/x86/kvm/mmu/page_track.h | 30 +++++++++++++++++++++++----
> 4 files changed, 35 insertions(+), 8 deletions(-)
>
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index eec424fac0ba..e8f8e1bd96c7 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -1223,7 +1223,9 @@ struct kvm_arch {
> * create an NX huge page (without hanging the guest).
> */
> struct list_head possible_nx_huge_pages;
> +#ifdef CONFIG_KVM_EXTERNAL_WRITE_TRACKING
> struct kvm_page_track_notifier_head track_notifier_head;
> +#endif
> /*
> * Protects marking pages unsync during page faults, as TDP MMU page
> * faults only take mmu_lock for read. For simplicity, the unsync
> diff --git a/arch/x86/include/asm/kvm_page_track.h b/arch/x86/include/asm/kvm_page_track.h
> index deece45936a5..53c2adb25a07 100644
> --- a/arch/x86/include/asm/kvm_page_track.h
> +++ b/arch/x86/include/asm/kvm_page_track.h
> @@ -55,6 +55,7 @@ void kvm_slot_page_track_remove_page(struct kvm *kvm,
> struct kvm_memory_slot *slot, gfn_t gfn,
> enum kvm_page_track_mode mode);
>
> +#ifdef CONFIG_KVM_EXTERNAL_WRITE_TRACKING
> enum pg_level kvm_page_track_max_mapping_level(struct kvm *kvm, gfn_t gfn,
> enum pg_level max_level);
>
> @@ -64,5 +65,6 @@ kvm_page_track_register_notifier(struct kvm *kvm,
> void
> kvm_page_track_unregister_notifier(struct kvm *kvm,
> struct kvm_page_track_notifier_node *n);
> +#endif /* CONFIG_KVM_EXTERNAL_WRITE_TRACKING */
>
> #endif
> diff --git a/arch/x86/kvm/mmu/page_track.c b/arch/x86/kvm/mmu/page_track.c
> index 2b302fd2c5dd..f932909aa9b5 100644
> --- a/arch/x86/kvm/mmu/page_track.c
> +++ b/arch/x86/kvm/mmu/page_track.c
> @@ -193,6 +193,7 @@ bool kvm_slot_page_track_is_active(struct kvm *kvm,
> return !!READ_ONCE(slot->arch.gfn_track[mode][index]);
> }
>
> +#ifdef CONFIG_KVM_EXTERNAL_WRITE_TRACKING
> void kvm_page_track_cleanup(struct kvm *kvm)
> {
> struct kvm_page_track_notifier_head *head;
> @@ -208,6 +209,7 @@ int kvm_page_track_init(struct kvm *kvm)
> head = &kvm->arch.track_notifier_head;
> INIT_HLIST_HEAD(&head->track_notifier_list);
> return init_srcu_struct(&head->track_srcu);
> + return 0;
Double "return"s.

> }
>
> /*
> @@ -254,8 +256,8 @@ EXPORT_SYMBOL_GPL(kvm_page_track_unregister_notifier);
> * The node should figure out if the written page is the one that node is
> * interested in by itself.
> */
> -void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
> - int bytes)
> +void __kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
> + int bytes)
> {
> struct kvm_page_track_notifier_head *head;
> struct kvm_page_track_notifier_node *n;
> @@ -272,8 +274,6 @@ void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
> if (n->track_write)
> n->track_write(gpa, new, bytes, n);
> srcu_read_unlock(&head->track_srcu, idx);
> -
> - kvm_mmu_track_write(vcpu, gpa, new, bytes);
> }
>
> /*
> @@ -316,3 +316,4 @@ enum pg_level kvm_page_track_max_mapping_level(struct kvm *kvm, gfn_t gfn,
> return max_level;
> }
> EXPORT_SYMBOL_GPL(kvm_page_track_max_mapping_level);
> +#endif
> diff --git a/arch/x86/kvm/mmu/page_track.h b/arch/x86/kvm/mmu/page_track.h
> index 89712f123ad3..1b363784aa4a 100644
> --- a/arch/x86/kvm/mmu/page_track.h
> +++ b/arch/x86/kvm/mmu/page_track.h
> @@ -6,8 +6,6 @@
>
> #include <asm/kvm_page_track.h>
>
> -int kvm_page_track_init(struct kvm *kvm);
> -void kvm_page_track_cleanup(struct kvm *kvm);
>
> bool kvm_page_track_write_tracking_enabled(struct kvm *kvm);
> int kvm_page_track_write_tracking_alloc(struct kvm_memory_slot *slot);
> @@ -21,13 +19,37 @@ bool kvm_slot_page_track_is_active(struct kvm *kvm,
> const struct kvm_memory_slot *slot,
> gfn_t gfn, enum kvm_page_track_mode mode);
>
> -void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
> - int bytes);
> +#ifdef CONFIG_KVM_EXTERNAL_WRITE_TRACKING
> +int kvm_page_track_init(struct kvm *kvm);
> +void kvm_page_track_cleanup(struct kvm *kvm);
> +
> +void __kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa, const u8 *new,
> + int bytes);
> void kvm_page_track_delete_slot(struct kvm *kvm, struct kvm_memory_slot *slot);
>
> static inline bool kvm_page_track_has_external_user(struct kvm *kvm)
> {
> return hlist_empty(&kvm->arch.track_notifier_head.track_notifier_list);
> }
> +#else
> +static inline int kvm_page_track_init(struct kvm *kvm) { return 0; }
> +static inline void kvm_page_track_cleanup(struct kvm *kvm) { }
> +
> +static inline void __kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
> + const u8 *new, int bytes) { }
> +static inline void kvm_page_track_delete_slot(struct kvm *kvm,
> + struct kvm_memory_slot *slot) { }
> +
> +static inline bool kvm_page_track_has_external_user(struct kvm *kvm) { return false; }
> +
> +#endif /* CONFIG_KVM_EXTERNAL_WRITE_TRACKING */
> +
> +static inline void kvm_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
> + const u8 *new, int bytes)
> +{
> + __kvm_page_track_write(vcpu, gpa, new, bytes);
> +
Why not convert "vcpu" to "kvm" in __kvm_page_track_write() ?
i.e.
void __kvm_page_track_write(struct kvm *kvm, gpa_t gpa, const u8 *new, int bytes);

Thanks
Yan

> + kvm_mmu_track_write(vcpu, gpa, new, bytes);
> +}
>
> #endif /* __KVM_X86_PAGE_TRACK_H */
> --
> 2.39.0.314.g84b9a713c41-goog
>