Commit fb03cb6f44236f4bef62a0dda8e025ff5ca51417

Authored by Takuya Yoshikawa
Committed by Avi Kivity
1 parent 9eed0735ca

KVM: Introduce gfn_to_index() which returns the index for a given level

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>

Showing 3 changed files with 9 additions and 8 deletions Side-by-side Diff

... ... @@ -688,8 +688,7 @@
688 688 {
689 689 unsigned long idx;
690 690  
691   - idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
692   - (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
  691 + idx = gfn_to_index(gfn, slot->base_gfn, level);
693 692 return &slot->lpage_info[level - 2][idx];
694 693 }
695 694  
include/linux/kvm_host.h
... ... @@ -681,6 +681,13 @@
681 681 return gfn_to_memslot(kvm, gfn)->id;
682 682 }
683 683  
  684 +static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
  685 +{
  686 + /* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */
  687 + return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
  688 + (base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
  689 +}
  690 +
684 691 static inline unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
685 692 gfn_t gfn)
686 693 {
... ... @@ -784,15 +784,10 @@
784 784 int lpages;
785 785 int level = i + 2;
786 786  
787   - /* Avoid unused variable warning if no large pages */
788   - (void)level;
789   -
790 787 if (new.lpage_info[i])
791 788 continue;
792 789  
793   - lpages = 1 + ((base_gfn + npages - 1)
794   - >> KVM_HPAGE_GFN_SHIFT(level));
795   - lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
  790 + lpages = gfn_to_index(base_gfn + npages - 1, base_gfn, level) + 1;
796 791  
797 792 new.lpage_info[i] = vzalloc(lpages * sizeof(*new.lpage_info[i]));
798 793