KVM: Introduce gfn_to_index() which returns the index for a given level
authorTakuya Yoshikawa <yoshikawa.takuya@oss.ntt.co.jp>
Wed, 8 Feb 2012 03:59:10 +0000 (12:59 +0900)
committerAvi Kivity <avi@redhat.com>
Thu, 8 Mar 2012 12:10:19 +0000 (14:10 +0200)
This patch cleans up the code and removes the "(void)level;" warning
suppressor.

Note that we can also use this for PT_PAGE_TABLE_LEVEL to treat every
level uniformly later.

Signed-off-by: Takuya Yoshikawa <yoshikawa.takuya@oss.ntt.co.jp>
Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
Signed-off-by: Avi Kivity <avi@redhat.com>
arch/x86/kvm/mmu.c
include/linux/kvm_host.h
virt/kvm/kvm_main.c

index ae76cc3392e14b73962cfb80acb83628c5ccb301..37e7f100a0e05ef99a8f9a6004ec76469002d24e 100644 (file)
@@ -688,8 +688,7 @@ static struct kvm_lpage_info *lpage_info_slot(gfn_t gfn,
 {
        unsigned long idx;
 
-       idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
-             (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
+       idx = gfn_to_index(gfn, slot->base_gfn, level);
        return &slot->lpage_info[level - 2][idx];
 }
 
index 9698080c902bb26b2c5407a1a54e02a443c7c086..7a08496b974accb15fdd50329f7d612967aa2591 100644 (file)
@@ -681,6 +681,13 @@ static inline int memslot_id(struct kvm *kvm, gfn_t gfn)
        return gfn_to_memslot(kvm, gfn)->id;
 }
 
+static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
+{
+       /* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */
+       return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
+               (base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
+}
+
 static inline unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
                                               gfn_t gfn)
 {
index 470e30520fe877fcec570caab262e61522fe0b7d..415fe816fc154b8748a58343a4f7c76216cdc556 100644 (file)
@@ -784,15 +784,10 @@ int __kvm_set_memory_region(struct kvm *kvm,
                int lpages;
                int level = i + 2;
 
-               /* Avoid unused variable warning if no large pages */
-               (void)level;
-
                if (new.lpage_info[i])
                        continue;
 
-               lpages = 1 + ((base_gfn + npages - 1)
-                            >> KVM_HPAGE_GFN_SHIFT(level));
-               lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
+               lpages = gfn_to_index(base_gfn + npages - 1, base_gfn, level) + 1;
 
                new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));