diff options
author | Sean Christopherson <sean.j.christopherson@intel.com> | 2019-11-11 17:12:27 -0500 |
---|---|---|
committer | Paolo Bonzini <pbonzini@redhat.com> | 2019-11-12 04:17:42 -0500 |
commit | a78986aae9b2988f8493f9f65a587ee433e83bc3 (patch) | |
tree | a630d421bde6adbf63834c840e3276096ecae4b1 | |
parent | 29881b6ec6e453ff8df37ad8f44e17bf0d4e1e12 (diff) |
KVM: MMU: Do not treat ZONE_DEVICE pages as being reserved
Explicitly exempt ZONE_DEVICE pages from kvm_is_reserved_pfn() and
instead manually handle ZONE_DEVICE on a case-by-case basis. For things
like page refcounts, KVM needs to treat ZONE_DEVICE pages like normal
pages, e.g. put pages grabbed via gup(). But for flows such as setting
A/D bits or shifting refcounts for transparent huge pages, KVM needs to
to avoid processing ZONE_DEVICE pages as the flows in question lack the
underlying machinery for proper handling of ZONE_DEVICE pages.
This fixes a hang reported by Adam Borowski[*] in dev_pagemap_cleanup()
when running a KVM guest backed with /dev/dax memory, as KVM straight up
doesn't put any references to ZONE_DEVICE pages acquired by gup().
Note, Dan Williams proposed an alternative solution of doing put_page()
on ZONE_DEVICE pages immediately after gup() in order to simplify the
auditing needed to ensure is_zone_device_page() is called if and only if
the backing device is pinned (via gup()). But that approach would break
kvm_vcpu_{un}map() as KVM requires the page to be pinned from map() 'til
unmap() when accessing guest memory, unlike KVM's secondary MMU, which
coordinates with mmu_notifier invalidations to avoid creating stale
page references, i.e. doesn't rely on pages being pinned.
[*] http://lkml.kernel.org/r/20190919115547.GA17963@angband.pl
Reported-by: Adam Borowski <kilobyte@angband.pl>
Analyzed-by: David Hildenbrand <david@redhat.com>
Acked-by: Dan Williams <dan.j.williams@intel.com>
Cc: stable@vger.kernel.org
Fixes: 3565fce3a659 ("mm, x86: get_user_pages() for dax mappings")
Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
-rw-r--r-- | arch/x86/kvm/mmu.c | 8 | ||||
-rw-r--r-- | include/linux/kvm_host.h | 1 | ||||
-rw-r--r-- | virt/kvm/kvm_main.c | 26 |
3 files changed, 28 insertions, 7 deletions
diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c index 24c23c66b226..bf82b1f2e834 100644 --- a/arch/x86/kvm/mmu.c +++ b/arch/x86/kvm/mmu.c | |||
@@ -3306,7 +3306,7 @@ static void transparent_hugepage_adjust(struct kvm_vcpu *vcpu, | |||
3306 | * here. | 3306 | * here. |
3307 | */ | 3307 | */ |
3308 | if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn) && | 3308 | if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn) && |
3309 | level == PT_PAGE_TABLE_LEVEL && | 3309 | !kvm_is_zone_device_pfn(pfn) && level == PT_PAGE_TABLE_LEVEL && |
3310 | PageTransCompoundMap(pfn_to_page(pfn)) && | 3310 | PageTransCompoundMap(pfn_to_page(pfn)) && |
3311 | !mmu_gfn_lpage_is_disallowed(vcpu, gfn, PT_DIRECTORY_LEVEL)) { | 3311 | !mmu_gfn_lpage_is_disallowed(vcpu, gfn, PT_DIRECTORY_LEVEL)) { |
3312 | unsigned long mask; | 3312 | unsigned long mask; |
@@ -5914,9 +5914,9 @@ restart: | |||
5914 | * the guest, and the guest page table is using 4K page size | 5914 | * the guest, and the guest page table is using 4K page size |
5915 | * mapping if the indirect sp has level = 1. | 5915 | * mapping if the indirect sp has level = 1. |
5916 | */ | 5916 | */ |
5917 | if (sp->role.direct && | 5917 | if (sp->role.direct && !kvm_is_reserved_pfn(pfn) && |
5918 | !kvm_is_reserved_pfn(pfn) && | 5918 | !kvm_is_zone_device_pfn(pfn) && |
5919 | PageTransCompoundMap(pfn_to_page(pfn))) { | 5919 | PageTransCompoundMap(pfn_to_page(pfn))) { |
5920 | pte_list_remove(rmap_head, sptep); | 5920 | pte_list_remove(rmap_head, sptep); |
5921 | 5921 | ||
5922 | if (kvm_available_flush_tlb_with_range()) | 5922 | if (kvm_available_flush_tlb_with_range()) |
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 719fc3e15ea4..290dbe353a47 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h | |||
@@ -966,6 +966,7 @@ int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu); | |||
966 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu); | 966 | void kvm_vcpu_kick(struct kvm_vcpu *vcpu); |
967 | 967 | ||
968 | bool kvm_is_reserved_pfn(kvm_pfn_t pfn); | 968 | bool kvm_is_reserved_pfn(kvm_pfn_t pfn); |
969 | bool kvm_is_zone_device_pfn(kvm_pfn_t pfn); | ||
969 | 970 | ||
970 | struct kvm_irq_ack_notifier { | 971 | struct kvm_irq_ack_notifier { |
971 | struct hlist_node link; | 972 | struct hlist_node link; |
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index e7a07132cd7f..0dac149ead16 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c | |||
@@ -149,10 +149,30 @@ __weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, | |||
149 | return 0; | 149 | return 0; |
150 | } | 150 | } |
151 | 151 | ||
152 | bool kvm_is_zone_device_pfn(kvm_pfn_t pfn) | ||
153 | { | ||
154 | /* | ||
155 | * The metadata used by is_zone_device_page() to determine whether or | ||
156 | * not a page is ZONE_DEVICE is guaranteed to be valid if and only if | ||
157 | * the device has been pinned, e.g. by get_user_pages(). WARN if the | ||
158 | * page_count() is zero to help detect bad usage of this helper. | ||
159 | */ | ||
160 | if (!pfn_valid(pfn) || WARN_ON_ONCE(!page_count(pfn_to_page(pfn)))) | ||
161 | return false; | ||
162 | |||
163 | return is_zone_device_page(pfn_to_page(pfn)); | ||
164 | } | ||
165 | |||
152 | bool kvm_is_reserved_pfn(kvm_pfn_t pfn) | 166 | bool kvm_is_reserved_pfn(kvm_pfn_t pfn) |
153 | { | 167 | { |
168 | /* | ||
169 | * ZONE_DEVICE pages currently set PG_reserved, but from a refcounting | ||
170 | * perspective they are "normal" pages, albeit with slightly different | ||
171 | * usage rules. | ||
172 | */ | ||
154 | if (pfn_valid(pfn)) | 173 | if (pfn_valid(pfn)) |
155 | return PageReserved(pfn_to_page(pfn)); | 174 | return PageReserved(pfn_to_page(pfn)) && |
175 | !kvm_is_zone_device_pfn(pfn); | ||
156 | 176 | ||
157 | return true; | 177 | return true; |
158 | } | 178 | } |
@@ -1857,7 +1877,7 @@ EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty); | |||
1857 | 1877 | ||
1858 | void kvm_set_pfn_dirty(kvm_pfn_t pfn) | 1878 | void kvm_set_pfn_dirty(kvm_pfn_t pfn) |
1859 | { | 1879 | { |
1860 | if (!kvm_is_reserved_pfn(pfn)) { | 1880 | if (!kvm_is_reserved_pfn(pfn) && !kvm_is_zone_device_pfn(pfn)) { |
1861 | struct page *page = pfn_to_page(pfn); | 1881 | struct page *page = pfn_to_page(pfn); |
1862 | 1882 | ||
1863 | SetPageDirty(page); | 1883 | SetPageDirty(page); |
@@ -1867,7 +1887,7 @@ EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); | |||
1867 | 1887 | ||
1868 | void kvm_set_pfn_accessed(kvm_pfn_t pfn) | 1888 | void kvm_set_pfn_accessed(kvm_pfn_t pfn) |
1869 | { | 1889 | { |
1870 | if (!kvm_is_reserved_pfn(pfn)) | 1890 | if (!kvm_is_reserved_pfn(pfn) && !kvm_is_zone_device_pfn(pfn)) |
1871 | mark_page_accessed(pfn_to_page(pfn)); | 1891 | mark_page_accessed(pfn_to_page(pfn)); |
1872 | } | 1892 | } |
1873 | EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); | 1893 | EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); |