[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[RFC PATCH v2 09/19] KVM: x86: Extend kvm_range_has_memory_attributes() with match_all



This enables to check if an attribute is tied to any memory page in a
range. This will be useful in a folling commit to check for
KVM_MEMORY_ATTRIBUTE_HEKI_IMMUTABLE.

Cc: Chao Peng <chao.p.peng@xxxxxxxxxxxxxxx>
Cc: Kees Cook <keescook@xxxxxxxxxxxx>
Cc: Madhavan T. Venkataraman <madvenka@xxxxxxxxxxxxxxxxxxx>
Cc: Sean Christopherson <seanjc@xxxxxxxxxx>
Cc: Yu Zhang <yu.c.zhang@xxxxxxxxxxxxxxx>
Signed-off-by: Mickaël Salaün <mic@xxxxxxxxxxx>
---

Changes since v1:
* New patch
---
 arch/x86/kvm/mmu/mmu.c   |  2 +-
 include/linux/kvm_host.h |  2 +-
 virt/kvm/kvm_main.c      | 27 ++++++++++++++++++---------
 3 files changed, 20 insertions(+), 11 deletions(-)

diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index d7010e09440d..2024ff21d036 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -7279,7 +7279,7 @@ static bool hugepage_has_attrs(struct kvm *kvm, struct 
kvm_memory_slot *slot,
        const unsigned long end = start + KVM_PAGES_PER_HPAGE(level);
 
        if (level == PG_LEVEL_2M)
-               return kvm_range_has_memory_attributes(kvm, start, end, attrs);
+               return kvm_range_has_memory_attributes(kvm, start, end, attrs, 
true);
 
        for (gfn = start; gfn < end; gfn += KVM_PAGES_PER_HPAGE(level - 1)) {
                if (hugepage_test_mixed(slot, gfn, level - 1) ||
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index de68390ab0f2..9ecb016a336f 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -2391,7 +2391,7 @@ static inline unsigned long 
kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn
 }
 
 bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
-                                    unsigned long attrs);
+                                    unsigned long attrs, bool match_all);
 bool kvm_arch_pre_set_memory_attributes(struct kvm *kvm,
                                        struct kvm_gfn_range *range);
 bool kvm_arch_post_set_memory_attributes(struct kvm *kvm,
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index e2c178db17d5..67dbaaf40c1c 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -2435,11 +2435,11 @@ static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm,
 
 #ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
 /*
- * Returns true if _all_ gfns in the range [@start, @end) have attributes
- * matching the @attrs bitmask.
+ * According to @match_all, returns true if _all_ (respectively _any_) gfns in
+ * the range [@start, @end) have attributes matching the @attrs bitmask.
  */
 bool kvm_range_has_memory_attributes(struct kvm *kvm, gfn_t start, gfn_t end,
-                                    unsigned long attrs)
+                                    unsigned long attrs, bool match_all)
 {
        XA_STATE(xas, &kvm->mem_attr_array, start);
        unsigned long index;
@@ -2453,16 +2453,25 @@ bool kvm_range_has_memory_attributes(struct kvm *kvm, 
gfn_t start, gfn_t end,
                goto out;
        }
 
-       has_attrs = true;
+       has_attrs = match_all;
        for (index = start; index < end; index++) {
                do {
                        entry = xas_next(&xas);
                } while (xas_retry(&xas, entry));
 
-               if (xas.xa_index != index ||
-                   (xa_to_value(entry) & attrs) != attrs) {
-                       has_attrs = false;
-                       break;
+               if (match_all) {
+                       if (xas.xa_index != index ||
+                           (xa_to_value(entry) & attrs) != attrs) {
+                               has_attrs = false;
+                               break;
+                       }
+               } else {
+                       index = xas.xa_index;
+                       if (index < end &&
+                           (xa_to_value(entry) & attrs) == attrs) {
+                               has_attrs = true;
+                               break;
+                       }
                }
        }
 
@@ -2578,7 +2587,7 @@ int kvm_vm_set_mem_attributes(struct kvm *kvm, gfn_t 
start, gfn_t end,
        lockdep_assert_held(&kvm->slots_arch_lock);
 
        /* Nothing to do if the entire range as the desired attributes. */
-       if (kvm_range_has_memory_attributes(kvm, start, end, attributes))
+       if (kvm_range_has_memory_attributes(kvm, start, end, attributes, true))
                return r;
 
        /*
-- 
2.42.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.