On Fri, Mar 10, 2023 at 04:22:56PM -0800, Sean Christopherson wrote: ... > +int kvm_write_track_add_gfn(struct kvm *kvm, gfn_t gfn) > +{ > + struct kvm_memory_slot *slot; > + int idx; > + > + idx = srcu_read_lock(&kvm->srcu); > + > + slot = gfn_to_memslot(kvm, gfn); > + if (!slot) { > + srcu_read_unlock(&kvm->srcu, idx); > + return -EINVAL; > + } > + Also fail if slot->flags & KVM_MEMSLOT_INVALID is true? There should exist a window for external users to see an invalid slot when a slot is about to get deleted/moved. (It happens before MOVE is rejected in kvm_arch_prepare_memory_region()). > + write_lock(&kvm->mmu_lock); > + __kvm_write_track_add_gfn(kvm, slot, gfn); > + write_unlock(&kvm->mmu_lock); > + > + srcu_read_unlock(&kvm->srcu, idx); > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(kvm_write_track_add_gfn); > + > +/* > + * remove the guest page from the tracking pool which stops the interception > + * of corresponding access on that page. > + * > + * @kvm: the guest instance we are interested in. > + * @gfn: the guest page. > + */ > +int kvm_write_track_remove_gfn(struct kvm *kvm, gfn_t gfn) > +{ > + struct kvm_memory_slot *slot; > + int idx; > + > + idx = srcu_read_lock(&kvm->srcu); > + > + slot = gfn_to_memslot(kvm, gfn); > + if (!slot) { > + srcu_read_unlock(&kvm->srcu, idx); > + return -EINVAL; > + } > + Ditto. > + write_lock(&kvm->mmu_lock); > + __kvm_write_track_remove_gfn(kvm, slot, gfn); > + write_unlock(&kvm->mmu_lock); > + > + srcu_read_unlock(&kvm->srcu, idx); > + > + return 0; > +} > +EXPORT_SYMBOL_GPL(kvm_write_track_remove_gfn);