Skip to contentSkip to conversation

Use u64 for gfn_t in KVM

PATCH Applied Joerg Roedel 3 participants 6 messages Jul 1, 2010 – Jul 2, 2010

Subsystems (7)
x86 KVM PowerPC s390 +3 more subsystems
lore
Share & tools
lore
#0 lore raw

cover Use u64 for gfn_t in KVM

Hi, Avi, Marcelo,

these two patches currently lie on the bottom of my npt-npt patch set. They came out of a discussion on the mailing list about the last version of the npt-npt patchset. These patches are independent of the rest of the patchset, so I post and discuss them seperatly. The changes to make gfn_t an u64 were larger than I expected which is another reason for the seperate post. Please let me know what you think about this change.

Thanks,

Joerg

Diffstat:

 arch/ia64/include/asm/kvm_host.h    |    1 +
 arch/powerpc/include/asm/kvm_host.h |    1 +
 arch/s390/include/asm/kvm_host.h    |    3 ++-
 arch/x86/include/asm/kvm_host.h     |    3 ++-
 arch/x86/kvm/mmu.c                  |    8 ++++----
 include/linux/kvm_types.h           |    4 ++--
 virt/kvm/iommu.c                    |    2 +-
 virt/kvm/kvm_main.c                 |   10 +++++-----
 8 files changed, 18 insertions(+), 14 deletions(-)

Shortlog:

Joerg Roedel (2):
KVM: Remove unnecessary divide operations
KVM: Use u64 for frame data types

#1 lore raw

PATCH 1/2 KVM: Remove unnecessary divide operations

This patch converts unnecessary divide and modulo operations in the KVM large page related code into logical operations. This allows to convert gfn_t to u64 while not breaking 32 bit builds.

---
 arch/ia64/include/asm/kvm_host.h    |    1 +
 arch/powerpc/include/asm/kvm_host.h |    1 +
 arch/s390/include/asm/kvm_host.h    |    3 ++-
 arch/x86/include/asm/kvm_host.h     |    3 ++-
 arch/x86/kvm/mmu.c                  |    8 ++++----
 virt/kvm/kvm_main.c                 |   10 +++++-----
 6 files changed, 15 insertions(+), 11 deletions(-)
arch/ia64/include/asm/kvm_host.h +1 -0
index a362e67..2f229e5 100644
@@ -235,6 +235,7 @@ struct kvm_vm_data {
 #define KVM_REQ_PTC_G		32
 #define KVM_REQ_RESUME		33
 
+#define KVM_HPAGE_GFN_SHIFT(x)	0
 #define KVM_NR_PAGE_SIZES	1
 #define KVM_PAGES_PER_HPAGE(x)	1
 
arch/powerpc/include/asm/kvm_host.h +1 -0
index 0c9ad86..3452dac 100644
@@ -35,6 +35,7 @@
 #define KVM_COALESCED_MMIO_PAGE_OFFSET 1
 
 /* We don't currently support large pages. */
+#define KVM_HPAGE_GFN_SHIFT(x)	0
 #define KVM_NR_PAGE_SIZES	1
 #define KVM_PAGES_PER_HPAGE(x)	(1UL<<31)
 
arch/s390/include/asm/kvm_host.h +2 -1
index b95710a..cef7dbf 100644
@@ -41,7 +41,8 @@ struct sca_block {
 } __attribute__((packed));
 
 #define KVM_NR_PAGE_SIZES 2
-#define KVM_HPAGE_SHIFT(x) (PAGE_SHIFT + ((x) - 1) * 8)
+#define KVM_HPAGE_GFN_SHIFT(x) (((x) - 1) * 8)
+#define KVM_HPAGE_SHIFT(x) (PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
 #define KVM_HPAGE_SIZE(x) (1UL << KVM_HPAGE_SHIFT(x))
 #define KVM_HPAGE_MASK(x)	(~(KVM_HPAGE_SIZE(x) - 1))
 #define KVM_PAGES_PER_HPAGE(x)	(KVM_HPAGE_SIZE(x) / PAGE_SIZE)
arch/x86/include/asm/kvm_host.h +2 -1
index a57cdea..2b3452f 100644
@@ -43,7 +43,8 @@
 
 /* KVM Hugepage definitions for x86 */
 #define KVM_NR_PAGE_SIZES	3
-#define KVM_HPAGE_SHIFT(x)	(PAGE_SHIFT + (((x) - 1) * 9))
+#define KVM_HPAGE_GFN_SHIFT(x)	(((x) - 1) * 9)
+#define KVM_HPAGE_SHIFT(x)	(PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
 #define KVM_HPAGE_SIZE(x)	(1UL << KVM_HPAGE_SHIFT(x))
 #define KVM_HPAGE_MASK(x)	(~(KVM_HPAGE_SIZE(x) - 1))
 #define KVM_PAGES_PER_HPAGE(x)	(KVM_HPAGE_SIZE(x) / PAGE_SIZE)
arch/x86/kvm/mmu.c +4 -4
index 045a0f9..8e0a325 100644
@@ -423,8 +423,8 @@ static int *slot_largepage_idx(gfn_t gfn,
 {
 	unsigned long idx;
 
-	idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
-	      (slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
+	idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
+	      (slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
 	return &slot->lpage_info[level - 2][idx].write_count;
 }
 
@@ -528,8 +528,8 @@ static unsigned long *gfn_to_rmap(struct kvm *kvm, gfn_t gfn, int level)
 	if (likely(level == PT_PAGE_TABLE_LEVEL))
 		return &slot->rmap[gfn - slot->base_gfn];
 
-	idx = (gfn / KVM_PAGES_PER_HPAGE(level)) -
-		(slot->base_gfn / KVM_PAGES_PER_HPAGE(level));
+	idx = (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
+		(slot->base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
 
 	return &slot->lpage_info[level - 2][idx].rmap_pde;
 }
virt/kvm/kvm_main.c +5 -5
index 885d3f5..57e3a6b 100644
@@ -626,9 +626,9 @@ int __kvm_set_memory_region(struct kvm *kvm,
 		if (new.lpage_info[i])
 			continue;
 
-		lpages = 1 + (base_gfn + npages - 1) /
-			     KVM_PAGES_PER_HPAGE(level);
-		lpages -= base_gfn / KVM_PAGES_PER_HPAGE(level);
+		lpages = 1 + ((base_gfn + npages - 1)
+			     >> KVM_HPAGE_GFN_SHIFT(level));
+		lpages -= base_gfn >> KVM_HPAGE_GFN_SHIFT(level);
 
 		new.lpage_info[i] = vmalloc(lpages * sizeof(*new.lpage_info[i]));
 
@@ -638,9 +638,9 @@ int __kvm_set_memory_region(struct kvm *kvm,
 		memset(new.lpage_info[i], 0,
 		       lpages * sizeof(*new.lpage_info[i]));
 
-		if (base_gfn % KVM_PAGES_PER_HPAGE(level))
+		if (base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
 			new.lpage_info[i][0].write_count = 1;
-		if ((base_gfn+npages) % KVM_PAGES_PER_HPAGE(level))
+		if ((base_gfn+npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
 			new.lpage_info[i][lpages - 1].write_count = 1;
 		ugfn = new.userspace_addr >> PAGE_SHIFT;
 		/*
Signature · 1 line
1.7.0.4
#2 lore raw

Re: [PATCH 1/2] KVM: Remove unnecessary divide operations

arch/s390/include/asm/kvm_host.h struct sca_block { · line 47

Quoted 13 lines · Joerg Roedel

This patch converts unnecessary divide and modulo operations in the KVM large page related code into logical operations. This allows to convert gfn_t to u64 while not breaking 32 bit builds.

  #define KVM_NR_PAGE_SIZES 2
arch/s390/include/asm/kvm_host.h +2 -1
-#define KVM_HPAGE_SHIFT(x) (PAGE_SHIFT + ((x) - 1) * 8)
+#define KVM_HPAGE_GFN_SHIFT(x) (((x) - 1) * 8)
+#define KVM_HPAGE_SHIFT(x) (PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
  #define KVM_HPAGE_SIZE(x) (1UL<<  KVM_HPAGE_SHIFT(x))
  #define KVM_HPAGE_MASK(x)	(~(KVM_HPAGE_SIZE(x) - 1))

In theory, KVM_HPAGE_SIZE() needs s/1UL/(u64)1/, or KVM_HPAGE_MASK becomes truncated if used against a gfn_t. In practice, KVM_HPAGE_MASK is not used at all, so this doesn't matter.

Signature · 1 line
error compiling committee.c: too many arguments to function
#3 lore raw

PATCH 2/2 KVM: Use u64 for frame data types

In reply to Joerg Roedel

For 32bit machines where the physical address width is larger than the virtual address width the frame number types in KVM may overflow. Fix this by changing them to u64.

---
 include/linux/kvm_types.h |    4 ++--
 virt/kvm/iommu.c          |    2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/kvm_types.h +2 -2
index fb46efb..7ac0d4e 100644
@@ -32,11 +32,11 @@
 
 typedef unsigned long  gva_t;
 typedef u64            gpa_t;
-typedef unsigned long  gfn_t;
+typedef u64            gfn_t;
 
 typedef unsigned long  hva_t;
 typedef u64            hpa_t;
-typedef unsigned long  hfn_t;
+typedef u64            hfn_t;
 
 typedef hfn_t pfn_t;
 
virt/kvm/iommu.c +1 -1
index 673c88a..22fc28b 100644
@@ -108,7 +108,7 @@ int kvm_iommu_map_pages(struct kvm *kvm, struct kvm_memory_slot *slot)
 			      get_order(page_size), flags);
 		if (r) {
 			printk(KERN_ERR "kvm_iommu_map_address:"
-			       "iommu failed to map pfn=%lx\n", pfn);
+			       "iommu failed to map pfn=%llx\n", pfn);
 			goto unmap_pages;
 		}
 
Signature · 1 line
1.7.0.4
#4 lore raw
In reply to Joerg Roedel
Quoted 10 lines · Joerg Roedel

Hi, Avi, Marcelo,

these two patches currently lie on the bottom of my npt-npt patch set. They came out of a discussion on the mailing list about the last version of the npt-npt patchset. These patches are independent of the rest of the patchset, so I post and discuss them seperatly. The changes to make gfn_t an u64 were larger than I expected which is another reason for the seperate post. Please let me know what you think about this change.

Looks good.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw
In reply to Joerg Roedel
Quoted 25 lines · Joerg Roedel

Hi, Avi, Marcelo,

these two patches currently lie on the bottom of my npt-npt patch set. They came out of a discussion on the mailing list about the last version of the npt-npt patchset. These patches are independent of the rest of the patchset, so I post and discuss them seperatly. The changes to make gfn_t an u64 were larger than I expected which is another reason for the seperate post. Please let me know what you think about this change.

Thanks,

Joerg

Diffstat:

 arch/ia64/include/asm/kvm_host.h    |    1 +
 arch/powerpc/include/asm/kvm_host.h |    1 +
 arch/s390/include/asm/kvm_host.h    |    3 ++-
 arch/x86/include/asm/kvm_host.h     |    3 ++-
 arch/x86/kvm/mmu.c                  |    8 ++++----
 include/linux/kvm_types.h           |    4 ++--
 virt/kvm/iommu.c                    |    2 +-
 virt/kvm/kvm_main.c                 |   10 +++++-----
 8 files changed, 18 insertions(+), 14 deletions(-)
Quoted 5 lines

Shortlog:

Joerg Roedel (2):
KVM: Remove unnecessary divide operations
KVM: Use u64 for frame data types

Applied, thanks.