Commit 8e1c6914 authored by Sean Christopherson's avatar Sean Christopherson Committed by Paolo Bonzini

KVM: Avoid pfn_to_page() and vice versa when releasing pages

Invert the order of KVM's page/pfn release helpers so that the "inner"
helper operates on a page instead of a pfn.  As pointed out by Linus[*],
converting between struct page and a pfn isn't necessarily cheap, and
that's not even counting the overhead of is_error_noslot_pfn() and
kvm_is_reserved_pfn().  Even if the checks were dirt cheap, there's no
reason to convert from a page to a pfn and back to a page, just to mark
the page dirty/accessed or to put a reference to the page.

Opportunistically drop a stale declaration of kvm_set_page_accessed()
from kvm_host.h (there was no implementation).

No functional change intended.

[*] https://lore.kernel.org/all/CAHk-=wifQimj2d6npq-wCi5onYPjzQg4vyO4tFcPJJZr268cRw@mail.gmail.comSigned-off-by: default avatarSean Christopherson <seanjc@google.com>
Message-Id: <20220429010416.2788472-5-seanjc@google.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
parent a1040b0d
...@@ -1139,7 +1139,6 @@ unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, gfn_t gfn, ...@@ -1139,7 +1139,6 @@ unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot, gfn_t gfn,
bool *writable); bool *writable);
void kvm_release_page_clean(struct page *page); void kvm_release_page_clean(struct page *page);
void kvm_release_page_dirty(struct page *page); void kvm_release_page_dirty(struct page *page);
void kvm_set_page_accessed(struct page *page);
kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn); kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn);
kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault, kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
......
...@@ -2820,18 +2820,40 @@ struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn) ...@@ -2820,18 +2820,40 @@ struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn)
} }
EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_page); EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_page);
static bool kvm_is_ad_tracked_page(struct page *page)
{
/*
* Per page-flags.h, pages tagged PG_reserved "should in general not be
* touched (e.g. set dirty) except by its owner".
*/
return !PageReserved(page);
}
static void kvm_set_page_dirty(struct page *page)
{
if (kvm_is_ad_tracked_page(page))
SetPageDirty(page);
}
static void kvm_set_page_accessed(struct page *page)
{
if (kvm_is_ad_tracked_page(page))
mark_page_accessed(page);
}
void kvm_release_page_clean(struct page *page) void kvm_release_page_clean(struct page *page)
{ {
WARN_ON(is_error_page(page)); WARN_ON(is_error_page(page));
kvm_release_pfn_clean(page_to_pfn(page)); kvm_set_page_accessed(page);
put_page(page);
} }
EXPORT_SYMBOL_GPL(kvm_release_page_clean); EXPORT_SYMBOL_GPL(kvm_release_page_clean);
void kvm_release_pfn_clean(kvm_pfn_t pfn) 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))
put_page(pfn_to_page(pfn)); kvm_release_page_clean(pfn_to_page(pfn));
} }
EXPORT_SYMBOL_GPL(kvm_release_pfn_clean); EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
...@@ -2839,40 +2861,40 @@ void kvm_release_page_dirty(struct page *page) ...@@ -2839,40 +2861,40 @@ void kvm_release_page_dirty(struct page *page)
{ {
WARN_ON(is_error_page(page)); WARN_ON(is_error_page(page));
kvm_release_pfn_dirty(page_to_pfn(page)); kvm_set_page_dirty(page);
kvm_release_page_clean(page);
} }
EXPORT_SYMBOL_GPL(kvm_release_page_dirty); EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
void kvm_release_pfn_dirty(kvm_pfn_t pfn) void kvm_release_pfn_dirty(kvm_pfn_t pfn)
{ {
kvm_set_pfn_dirty(pfn); if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn))
kvm_release_pfn_clean(pfn); kvm_release_page_dirty(pfn_to_page(pfn));
} }
EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty); EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
static bool kvm_is_ad_tracked_pfn(kvm_pfn_t pfn) /*
{ * Note, checking for an error/noslot pfn is the caller's responsibility when
if (!pfn_valid(pfn)) * directly marking a page dirty/accessed. Unlike the "release" helpers, the
return false; * "set" helpers are not to be used when the pfn might point at garbage.
*/
/*
* Per page-flags.h, pages tagged PG_reserved "should in general not be
* touched (e.g. set dirty) except by its owner".
*/
return !PageReserved(pfn_to_page(pfn));
}
void kvm_set_pfn_dirty(kvm_pfn_t pfn) void kvm_set_pfn_dirty(kvm_pfn_t pfn)
{ {
if (kvm_is_ad_tracked_pfn(pfn)) if (WARN_ON(is_error_noslot_pfn(pfn)))
SetPageDirty(pfn_to_page(pfn)); return;
if (pfn_valid(pfn))
kvm_set_page_dirty(pfn_to_page(pfn));
} }
EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty); EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
void kvm_set_pfn_accessed(kvm_pfn_t pfn) void kvm_set_pfn_accessed(kvm_pfn_t pfn)
{ {
if (kvm_is_ad_tracked_pfn(pfn)) if (WARN_ON(is_error_noslot_pfn(pfn)))
mark_page_accessed(pfn_to_page(pfn)); return;
if (pfn_valid(pfn))
kvm_set_page_accessed(pfn_to_page(pfn));
} }
EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed); EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment