KVM: use 'writable' as a hint to map writable pfn
authorXiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
Tue, 21 Aug 2012 03:00:49 +0000 (11:00 +0800)
committerAvi Kivity <avi@redhat.com>
Wed, 22 Aug 2012 12:08:55 +0000 (15:08 +0300)
In current code, we always map writable pfn for the read fault, in order
to support readonly memslot, we map writable pfn only if 'writable'
is not NULL

Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
virt/kvm/kvm_main.c

index aa4a38a..c89d1b5 100644 (file)
@@ -1054,6 +1054,14 @@ static bool hva_to_pfn_fast(unsigned long addr, bool atomic, bool *async,
        if (!(async || atomic))
                return false;
 
+       /*
+        * Fast pin a writable pfn only if it is a write fault request
+        * or the caller allows to map a writable pfn for a read fault
+        * request.
+        */
+       if (!(write_fault || writable))
+               return false;
+
        npages = __get_user_pages_fast(addr, 1, 1, page);
        if (npages == 1) {
                *pfn = page_to_pfn(page[0]);
@@ -1093,7 +1101,7 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
                return npages;
 
        /* map read fault as writable if possible */
-       if (unlikely(!write_fault)) {
+       if (unlikely(!write_fault) && writable) {
                struct page *wpage[1];
 
                npages = __get_user_pages_fast(addr, 1, 1, wpage);
@@ -1109,6 +1117,20 @@ static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
        return npages;
 }
 
+/*
+ * Pin guest page in memory and return its pfn.
+ * @addr: host virtual address which maps memory to the guest
+ * @atomic: whether this function can sleep
+ * @async: whether this function need to wait IO complete if the
+ *         host page is not in the memory
+ * @write_fault: whether we should get a writable host page
+ * @writable: whether it allows to map a writable host page for !@write_fault
+ *
+ * The function will map a writable host page for these two cases:
+ * 1): @write_fault = true
+ * 2): @write_fault = false && @writable, @writable will tell the caller
+ *     whether the mapping is writable.
+ */
 static pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
                        bool write_fault, bool *writable)
 {