On Wed, Nov 06, 2019 at 07:56:34AM +0100, David Hildenbrand wrote: > On 06.11.19 01:08, Dan Williams wrote: > >On Tue, Nov 5, 2019 at 4:03 PM Sean Christopherson > >>But David's proposed fix for the above refcount bug is to omit the patch > >>so that KVM no longer treats ZONE_DEVICE pages as reserved. That seems > >>like the right thing to do, including for thp_adjust(), e.g. it would > >>naturally let KVM use 2mb pages for the guest when a ZONE_DEVICE page is > >>mapped with a huge page (2mb or above) in the host. The only hiccup is > >>figuring out how to correctly transfer the reference. > > > >That might not be the only hiccup. There's currently no such thing as > >huge pages for ZONE_DEVICE, there are huge *mappings* (pmd and pud), > >but the result of pfn_to_page() on such a mapping does not yield a > >huge 'struct page'. It seems there are other paths in KVM that assume > >that more typical page machinery is active like SetPageDirty() based > >on kvm_is_reserved_pfn(). While I told David that I did not want to > >see more usage of is_zone_device_page(), this patch below (untested) > >seems a cleaner path with less surprises: > > > >diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > >index 4df0aa6b8e5c..fbea17c1810c 100644 > >--- a/virt/kvm/kvm_main.c > >+++ b/virt/kvm/kvm_main.c > >@@ -1831,7 +1831,8 @@ EXPORT_SYMBOL_GPL(kvm_release_page_clean); > > > > void kvm_release_pfn_clean(kvm_pfn_t pfn) > > { > >- if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) > >+ if ((!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn)) || The is_error_noslot_pfn() check shouldn't be overriden by zone_device. > >+ (pfn_valid(pfn) && is_zone_device_page(pfn_to_page(pfn)))) But rather than special case kvm_release_pfn_clean(), I'd rather KVM explicitly handle ZONE_DEVICE pages, there are other flows where KVM really should be aware of ZONE_DEVICE pages, e.g. for sanity checks and whatnot. There are surprisingly few callers of kvm_is_reserved_pfn(), so it's actually not too big of a change. > > put_page(pfn_to_page(pfn)); > > } > > EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); > > I had the same thought, but I do wonder about the kvm_get_pfn() users, > e.g.,: > > hva_to_pfn_remapped(): > r = follow_pfn(vma, addr, &pfn); > ... > kvm_get_pfn(pfn); > ... > > We would not take a reference for ZONE_DEVICE, but later drop one reference > via kvm_release_pfn_clean(). IOW, kvm_get_pfn() gets *really* dangerous to > use. I can't tell if this can happen right now. > > We do have 3 users of kvm_get_pfn() that we have to audit before this > change. Also, we should add a comment to kvm_get_pfn() that it should never > be used with possible ZONE_DEVICE pages. > > Also, we should add a comment to kvm_release_pfn_clean(), describing why we > treat ZONE_DEVICE in a special way here. > > > We can then progress like this > > 1. Get this fix upstream, it's somewhat unrelated to this series. > 2. This patch here remains as is in this series (+/- documentation update) > 3. Long term, rework KVM to not have to not treat ZONE_DEVICE like reserved > pages. E.g., get rid of kvm_get_pfn(). Then, this special zone check can go. Dropping kvm_get_pfn() is less than ideal, and at this point unnecessary. I'm 99% sure the existing call sites for kvm_get_pfn() can never be reached with ZONE_DEVICE pages. I think we can do: 1. Get a fix upstream to have KVM stop treating ZONE_DEVICE pages as reserved PFNs, i.e. exempt them in kvm_is_reserved_pfn() and change the callers of kvm_is_reserved_pfn() to handle ZONE_DEVICE pages. 2. Drop this patch from the series, and instead remove the special treatment of ZONE_DEVICE pages from kvm_is_reserved_pfn(). Give me a few minutes to prep a patch. > > -- > > Thanks, > > David / dhildenb >