All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kai Huang <kai.huang@linux.intel.com>
To: Xiao Guangrong <guangrong.xiao@linux.intel.com>, pbonzini@redhat.com
Cc: gleb@kernel.org, mtosatti@redhat.com, kvm@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 06/11] KVM: MMU: let page fault handler be aware tracked page
Date: Tue, 15 Dec 2015 16:11:01 +0800	[thread overview]
Message-ID: <566FCB15.1000004@linux.intel.com> (raw)
In-Reply-To: <1448907973-36066-7-git-send-email-guangrong.xiao@linux.intel.com>



On 12/01/2015 02:26 AM, Xiao Guangrong wrote:
> The page fault caused by write access on the write tracked page can not
> be fixed, it always need to be emulated. page_fault_handle_page_track()
> is the fast path we introduce here to skip holding mmu-lock and shadow
> page table walking
Why can it be out side of mmu-lock? Is it OK that some other thread 
removes tracking of this page simultaneously? Shall we assuming the 
emulation code should handle this case?

Even it works for write protection, is it OK for other purpose tracking 
(as your tracking framework can be extended for other purpose)?
>
> However, if the page table is not present, it is worth making the page
> table entry present and readonly to make the read access happy
It's  fine for tracking write from guest. But what if I want to track 
every read from guest? Probably I am exaggerating :)

Thanks,
-Kai
>
> mmu_need_write_protect() need to be cooked to avoid page becoming writable
> when making page table present or sync/prefetch shadow page table entries
>
> Signed-off-by: Xiao Guangrong <guangrong.xiao@linux.intel.com>
> ---
>   arch/x86/include/asm/kvm_page_track.h |  2 ++
>   arch/x86/kvm/mmu.c                    | 44 +++++++++++++++++++++++++++++------
>   arch/x86/kvm/page_track.c             | 14 +++++++++++
>   arch/x86/kvm/paging_tmpl.h            |  3 +++
>   4 files changed, 56 insertions(+), 7 deletions(-)
>
> diff --git a/arch/x86/include/asm/kvm_page_track.h b/arch/x86/include/asm/kvm_page_track.h
> index 9cc17c6..f223201 100644
> --- a/arch/x86/include/asm/kvm_page_track.h
> +++ b/arch/x86/include/asm/kvm_page_track.h
> @@ -15,4 +15,6 @@ void kvm_page_track_add_page(struct kvm *kvm, gfn_t gfn,
>   			     enum kvm_page_track_mode mode);
>   void kvm_page_track_remove_page(struct kvm *kvm, gfn_t gfn,
>   				enum kvm_page_track_mode mode);
> +bool kvm_page_track_check_mode(struct kvm_vcpu *vcpu, gfn_t gfn,
> +			       enum kvm_page_track_mode mode);
>   #endif
> diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
> index 39809b8..b23f9fc 100644
> --- a/arch/x86/kvm/mmu.c
> +++ b/arch/x86/kvm/mmu.c
> @@ -41,6 +41,7 @@
>   #include <asm/cmpxchg.h>
>   #include <asm/io.h>
>   #include <asm/vmx.h>
> +#include <asm/kvm_page_track.h>
>   
>   /*
>    * When setting this variable to true it enables Two-Dimensional-Paging
> @@ -2456,25 +2457,29 @@ static void kvm_unsync_pages(struct kvm_vcpu *vcpu,  gfn_t gfn)
>   	}
>   }
>   
> -static int mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
> -				  bool can_unsync)
> +static bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
> +				   bool can_unsync)
>   {
>   	struct kvm_mmu_page *s;
>   	bool need_unsync = false;
>   
> +	if (kvm_page_track_check_mode(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
> +		return true;
> +
>   	for_each_gfn_indirect_valid_sp(vcpu->kvm, s, gfn) {
>   		if (!can_unsync)
> -			return 1;
> +			return true;
>   
>   		if (s->role.level != PT_PAGE_TABLE_LEVEL)
> -			return 1;
> +			return true;
>   
>   		if (!s->unsync)
>   			need_unsync = true;
>   	}
>   	if (need_unsync)
>   		kvm_unsync_pages(vcpu, gfn);
> -	return 0;
> +
> +	return false;
>   }
>   
>   static bool kvm_is_mmio_pfn(pfn_t pfn)
> @@ -3388,10 +3393,30 @@ int handle_mmio_page_fault(struct kvm_vcpu *vcpu, u64 addr, bool direct)
>   }
>   EXPORT_SYMBOL_GPL(handle_mmio_page_fault);
>   
> +static bool page_fault_handle_page_track(struct kvm_vcpu *vcpu,
> +					 u32 error_code, gfn_t gfn)
> +{
> +	if (unlikely(error_code & PFERR_RSVD_MASK))
> +		return false;
> +
> +	if (!(error_code & PFERR_PRESENT_MASK) ||
> +	      !(error_code & PFERR_WRITE_MASK))
> +		return false;
> +
> +	/*
> +	 * guest is writing the page which is write tracked which can
> +	 * not be fixed by page fault handler.
> +	 */
> +	if (kvm_page_track_check_mode(vcpu, gfn, KVM_PAGE_TRACK_WRITE))
> +		return true;
> +
> +	return false;
> +}
> +
>   static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
>   				u32 error_code, bool prefault)
>   {
> -	gfn_t gfn;
> +	gfn_t gfn = gva >> PAGE_SHIFT;
>   	int r;
>   
>   	pgprintk("%s: gva %lx error %x\n", __func__, gva, error_code);
> @@ -3403,13 +3428,15 @@ static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
>   			return r;
>   	}
>   
> +	if (page_fault_handle_page_track(vcpu, error_code, gfn))
> +		return 1;
> +
>   	r = mmu_topup_memory_caches(vcpu);
>   	if (r)
>   		return r;
>   
>   	MMU_WARN_ON(!VALID_PAGE(vcpu->arch.mmu.root_hpa));
>   
> -	gfn = gva >> PAGE_SHIFT;
>   
>   	return nonpaging_map(vcpu, gva & PAGE_MASK,
>   			     error_code, gfn, prefault);
> @@ -3493,6 +3520,9 @@ static int tdp_page_fault(struct kvm_vcpu *vcpu, gva_t gpa, u32 error_code,
>   			return r;
>   	}
>   
> +	if (page_fault_handle_page_track(vcpu, error_code, gfn))
> +		return 1;
> +
>   	r = mmu_topup_memory_caches(vcpu);
>   	if (r)
>   		return r;
> diff --git a/arch/x86/kvm/page_track.c b/arch/x86/kvm/page_track.c
> index ad510db..dc2da12 100644
> --- a/arch/x86/kvm/page_track.c
> +++ b/arch/x86/kvm/page_track.c
> @@ -151,3 +151,17 @@ void kvm_page_track_remove_page(struct kvm *kvm, gfn_t gfn,
>   		spin_unlock(&kvm->mmu_lock);
>   	}
>   }
> +
> +/*
> + * check if the corresponding access on the specified guest page is tracked.
> + */
> +bool kvm_page_track_check_mode(struct kvm_vcpu *vcpu, gfn_t gfn,
> +			       enum kvm_page_track_mode mode)
> +{
> +	struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
> +	int index = gfn_to_index(gfn, slot->base_gfn, PT_PAGE_TABLE_LEVEL);
> +
> +	WARN_ON(!check_mode(mode));
> +
> +	return !!ACCESS_ONCE(slot->arch.gfn_track[mode][index]);
> +}
> diff --git a/arch/x86/kvm/paging_tmpl.h b/arch/x86/kvm/paging_tmpl.h
> index 91e939b..ac85682 100644
> --- a/arch/x86/kvm/paging_tmpl.h
> +++ b/arch/x86/kvm/paging_tmpl.h
> @@ -735,6 +735,9 @@ static int FNAME(page_fault)(struct kvm_vcpu *vcpu, gva_t addr, u32 error_code,
>   		return 0;
>   	}
>   
> +	if (page_fault_handle_page_track(vcpu, error_code, walker.gfn))
> +		return 1;
> +
>   	vcpu->arch.write_fault_to_shadow_pgtable = false;
>   
>   	is_self_change_mapping = FNAME(is_self_change_mapping)(vcpu,


  reply	other threads:[~2015-12-15  8:15 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-30 18:26 [PATCH 00/11] KVM: x86: track guest page access Xiao Guangrong
2015-11-30 18:26 ` [PATCH 01/11] KVM: MMU: rename has_wrprotected_page to mmu_gfn_lpage_is_disallowed Xiao Guangrong
2015-11-30 18:26 ` [PATCH 02/11] KVM: MMU: introduce kvm_mmu_gfn_{allow,disallow}_lpage Xiao Guangrong
2015-11-30 18:26 ` [PATCH 03/11] KVM: MMU: introduce kvm_mmu_slot_gfn_write_protect Xiao Guangrong
2015-11-30 18:26 ` [PATCH 04/11] KVM: page track: add the framework of guest page tracking Xiao Guangrong
2015-12-15  7:06   ` Kai Huang
2015-12-15  8:46     ` Xiao Guangrong
2015-12-16  7:33       ` Kai Huang
2015-11-30 18:26 ` [PATCH 05/11] KVM: page track: introduce kvm_page_track_{add,remove}_page Xiao Guangrong
2015-12-15  7:15   ` Kai Huang
2015-12-15  7:56     ` Kai Huang
2015-11-30 18:26 ` [PATCH 06/11] KVM: MMU: let page fault handler be aware tracked page Xiao Guangrong
2015-12-15  8:11   ` Kai Huang [this message]
2015-12-15  9:03     ` Xiao Guangrong
2015-12-16  7:31       ` Kai Huang
2015-12-16  8:23         ` Xiao Guangrong
2015-11-30 18:26 ` [PATCH 07/11] KVM: page track: add notifier support Xiao Guangrong
2015-12-16  5:53   ` Jike Song
2015-12-16  6:26     ` Xiao Guangrong
2015-11-30 18:26 ` [PATCH 08/11] KVM: MMU: use page track for non-leaf shadow pages Xiao Guangrong
2015-12-15  7:52   ` Kai Huang
2015-12-15  7:59     ` Kai Huang
2015-12-15  9:10     ` Xiao Guangrong
2015-12-16  7:51       ` Kai Huang
2015-12-16  8:39         ` Xiao Guangrong
2015-12-17  2:44           ` Kai Huang
2015-12-17  4:07             ` Xiao Guangrong
2015-11-30 18:26 ` [PATCH 09/11] KVM: MMU: simplify mmu_need_write_protect Xiao Guangrong
2015-12-15  8:43   ` Kai Huang
2015-12-15  8:47     ` Kai Huang
2015-12-15  9:26       ` Xiao Guangrong
2015-12-15  9:25     ` Xiao Guangrong
2015-12-16  8:05       ` Kai Huang
2015-12-16  8:48         ` Xiao Guangrong
2015-12-17  2:51           ` Kai Huang
2015-11-30 18:26 ` [PATCH 10/11] KVM: MMU: clear write-flooding on the fast path of tracked page Xiao Guangrong
2015-11-30 18:26 ` [PATCH 11/11] KVM: MMU: apply page track notifier Xiao Guangrong
2015-12-01 10:17 ` [PATCH 00/11] KVM: x86: track guest page access Paolo Bonzini
2015-12-01 15:02   ` Andrea Arcangeli
2015-12-01 15:08     ` Paolo Bonzini
2015-12-01 17:00   ` Xiao Guangrong
2015-12-05 16:56     ` Xiao Guangrong
2016-02-24  9:51 [PATCH v4 " Xiao Guangrong
2016-02-24  9:51 ` [PATCH 06/11] KVM: MMU: let page fault handler be aware tracked page Xiao Guangrong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=566FCB15.1000004@linux.intel.com \
    --to=kai.huang@linux.intel.com \
    --cc=gleb@kernel.org \
    --cc=guangrong.xiao@linux.intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mtosatti@redhat.com \
    --cc=pbonzini@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.