Skip to content

Commit 8b15c37

Browse files
sean-jcbonzini
authored andcommitted
KVM: Don't grab reference on VM_MIXEDMAP pfns that have a "struct page"
Now that KVM no longer relies on an ugly heuristic to find its struct page references, i.e. now that KVM can't get false positives on VM_MIXEDMAP pfns, remove KVM's hack to elevate the refcount for pfns that happen to have a valid struct page. In addition to removing a long-standing wart 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 <[email protected]> Signed-off-by: Sean Christopherson <[email protected]> Tested-by: Dmitry Osipenko <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> Message-ID: <[email protected]>
1 parent 93b7da4 commit 8b15c37

File tree

2 files changed

+2
-76
lines changed

2 files changed

+2
-76
lines changed

include/linux/kvm_host.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,9 +1730,6 @@ void kvm_arch_sync_events(struct kvm *kvm);
17301730

17311731
int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu);
17321732

1733-
struct page *kvm_pfn_to_refcounted_page(kvm_pfn_t pfn);
1734-
bool kvm_is_zone_device_page(struct page *page);
1735-
17361733
struct kvm_irq_ack_notifier {
17371734
struct hlist_node link;
17381735
unsigned gsi;

virt/kvm/kvm_main.c

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -160,52 +160,6 @@ __weak void kvm_arch_guest_memory_reclaimed(struct kvm *kvm)
160160
{
161161
}
162162

163-
bool kvm_is_zone_device_page(struct page *page)
164-
{
165-
/*
166-
* The metadata used by is_zone_device_page() to determine whether or
167-
* not a page is ZONE_DEVICE is guaranteed to be valid if and only if
168-
* the device has been pinned, e.g. by get_user_pages(). WARN if the
169-
* page_count() is zero to help detect bad usage of this helper.
170-
*/
171-
if (WARN_ON_ONCE(!page_count(page)))
172-
return false;
173-
174-
return is_zone_device_page(page);
175-
}
176-
177-
/*
178-
* Returns a 'struct page' if the pfn is "valid" and backed by a refcounted
179-
* page, NULL otherwise. Note, the list of refcounted PG_reserved page types
180-
* is likely incomplete, it has been compiled purely through people wanting to
181-
* back guest with a certain type of memory and encountering issues.
182-
*/
183-
struct page *kvm_pfn_to_refcounted_page(kvm_pfn_t pfn)
184-
{
185-
struct page *page;
186-
187-
if (!pfn_valid(pfn))
188-
return NULL;
189-
190-
page = pfn_to_page(pfn);
191-
if (!PageReserved(page))
192-
return page;
193-
194-
/* The ZERO_PAGE(s) is marked PG_reserved, but is refcounted. */
195-
if (is_zero_pfn(pfn))
196-
return page;
197-
198-
/*
199-
* ZONE_DEVICE pages currently set PG_reserved, but from a refcounting
200-
* perspective they are "normal" pages, albeit with slightly different
201-
* usage rules.
202-
*/
203-
if (kvm_is_zone_device_page(page))
204-
return page;
205-
206-
return NULL;
207-
}
208-
209163
/*
210164
* Switches to specified vcpu, until a matching vcpu_put()
211165
*/
@@ -2804,35 +2758,10 @@ static kvm_pfn_t kvm_resolve_pfn(struct kvm_follow_pfn *kfp, struct page *page,
28042758
if (kfp->map_writable)
28052759
*kfp->map_writable = writable;
28062760

2807-
/*
2808-
* FIXME: Remove this once KVM no longer blindly calls put_page() on
2809-
* every pfn that points at a struct page.
2810-
*
2811-
* Get a reference for follow_pte() pfns if they happen to point at a
2812-
* struct page, as KVM will ultimately call kvm_release_pfn_clean() on
2813-
* the returned pfn, i.e. KVM expects to have a reference.
2814-
*
2815-
* Certain IO or PFNMAP mappings can be backed with valid struct pages,
2816-
* but be allocated without refcounting, e.g. tail pages of
2817-
* non-compound higher order allocations. Grabbing and putting a
2818-
* reference to such pages would cause KVM to prematurely free a page
2819-
* it doesn't own (KVM gets and puts the one and only reference).
2820-
* Don't allow those pages until the FIXME is resolved.
2821-
*
2822-
* Don't grab a reference for pins, callers that pin pages are required
2823-
* to check refcounted_page, i.e. must not blindly release the pfn.
2824-
*/
2825-
if (map) {
2761+
if (map)
28262762
pfn = map->pfn;
2827-
2828-
if (!kfp->pin) {
2829-
page = kvm_pfn_to_refcounted_page(pfn);
2830-
if (page && !get_page_unless_zero(page))
2831-
return KVM_PFN_ERR_FAULT;
2832-
}
2833-
} else {
2763+
else
28342764
pfn = page_to_pfn(page);
2835-
}
28362765

28372766
*kfp->refcounted_page = page;
28382767

0 commit comments

Comments
 (0)