Skip to contentSkip to conversation

adjust/fix LDT handling for Xen

PATCH Applied Jan Beulich 4 participants 10 messages Jan 11, 2008 – Jan 15, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 adjust/fix LDT handling for Xen

Don't rely on kmalloc(PAGE_SIZE) returning PAGE_SIZE aligned memory (Xen requires GDT *and* LDT to be page-aligned). Using the page allocator interface also removes the (albeit small) slab allocator overhead. The same change being done for 64-bits for consistency.

Further, the Xen hypercall interface expects the LDT address to be virtual, not machine.

---
 arch/x86/kernel/ldt_32.c |    7 +++----
 arch/x86/kernel/ldt_64.c |    7 +++----
 arch/x86/xen/enlighten.c |    9 +--------
 3 files changed, 7 insertions(+), 16 deletions(-)
arch/x86/kernel/ldt_32.c +3 -4
@@ -9,7 +9,6 @@
 #include <linux/mm.h>
 #include <linux/smp.h>
 #include <linux/vmalloc.h>
-#include <linux/slab.h>
 
 #include <asm/uaccess.h>
 #include <asm/system.h>
@@ -38,7 +37,7 @@ static int alloc_ldt(mm_context_t *pc, i
 	if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
 		newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
 	else
-		newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
+		newldt = (void *)__get_free_page(GFP_KERNEL);
 
 	if (!newldt)
 		return -ENOMEM;
@@ -69,7 +68,7 @@ static int alloc_ldt(mm_context_t *pc, i
 		if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(oldldt);
 		else
-			kfree(oldldt);
+			put_page(virt_to_page(oldldt));
 	}
 	return 0;
 }
@@ -114,7 +113,7 @@ void destroy_context(struct mm_struct *m
 		if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(mm->context.ldt);
 		else
-			kfree(mm->context.ldt);
+			put_page(virt_to_page(mm->context.ldt));
 		mm->context.size = 0;
 	}
 }
arch/x86/kernel/ldt_64.c +3 -4
@@ -12,7 +12,6 @@
 #include <linux/mm.h>
 #include <linux/smp.h>
 #include <linux/vmalloc.h>
-#include <linux/slab.h>
 
 #include <asm/uaccess.h>
 #include <asm/system.h>
@@ -41,7 +40,7 @@ static int alloc_ldt(mm_context_t *pc, u
 	if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
 		newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
 	else
-		newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
+		newldt = (void *)__get_free_page(GFP_KERNEL);
 
 	if (!newldt)
 		return -ENOMEM;
@@ -73,7 +72,7 @@ static int alloc_ldt(mm_context_t *pc, u
 		if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(oldldt);
 		else
-			kfree(oldldt);
+			put_page(virt_to_page(oldldt));
 	}
 	return 0;
 }
@@ -117,7 +116,7 @@ void destroy_context(struct mm_struct *m
 		if ((unsigned)mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(mm->context.ldt);
 		else
-			kfree(mm->context.ldt);
+			put_page(virt_to_page(mm->context.ldt));
 		mm->context.size = 0;
 	}
 }
arch/x86/xen/enlighten.c +1 -8
@@ -275,19 +275,12 @@ static unsigned long xen_store_tr(void)
 
 static void xen_set_ldt(const void *addr, unsigned entries)
 {
-	unsigned long linear_addr = (unsigned long)addr;
 	struct mmuext_op *op;
 	struct multicall_space mcs = xen_mc_entry(sizeof(*op));
 
 	op = mcs.args;
 	op->cmd = MMUEXT_SET_LDT;
-	if (linear_addr) {
-		/* ldt my be vmalloced, use arbitrary_virt_to_machine */
-		xmaddr_t maddr;
-		maddr = arbitrary_virt_to_machine((unsigned long)addr);
-		linear_addr = (unsigned long)maddr.maddr;
-	}
-	op->arg1.linear_addr = linear_addr;
+	op->arg1.linear_addr = (unsigned long)addr;
 	op->arg2.nr_ents = entries;
 
 	MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
#1 lore raw
Quoted 19 lines · Jan Beulich

Don't rely on kmalloc(PAGE_SIZE) returning PAGE_SIZE aligned memory (Xen requires GDT *and* LDT to be page-aligned). Using the page allocator interface also removes the (albeit small) slab allocator overhead. The same change being done for 64-bits for consistency.

Further, the Xen hypercall interface expects the LDT address to be virtual, not machine.

---
 arch/x86/kernel/ldt_32.c |    7 +++----
 arch/x86/kernel/ldt_64.c |    7 +++----
 arch/x86/xen/enlighten.c |    9 +--------
 3 files changed, 7 insertions(+), 16 deletions(-)
arch/x86/kernel/ldt_32.c +0 -0

[...]

Quoted 10 lines
arch/x86/kernel/ldt_32.c +1 -1
@@ -73,7 +72,7 @@ static int alloc_ldt(mm_context_t *pc, u
 		if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(oldldt);
 		else
-			kfree(oldldt);
+			put_page(virt_to_page(oldldt));
 	}
 	return 0;
 }

arch/x86/kernel/ldt_32.c void destroy_context(struct mm_struct *m · line 119

Quoted 5 lines
arch/x86/kernel/ldt_32.c +1 -1
@@ -117,7 +116,7 @@ void destroy_context(struct mm_struct *m
 		if ((unsigned)mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(mm->context.ldt);
 		else
-			kfree(mm->context.ldt);
+			put_page(virt_to_page(mm->context.ldt));

why not free_page() with all those checks if it is correct virt address which it brings?

regards,

Signature · 3 lines
Jiri Slaby
Faculty of Informatics, Masaryk University
Suse Labs
#2 lore raw
Quoted 13 lines
Quoted 2 lines
arch/x86/kernel/ldt_32.c +0 -0

[...]

Quoted 10 lines
arch/x86/kernel/ldt_32.c +1 -1
@@ -73,7 +72,7 @@ static int alloc_ldt(mm_context_t *pc, u
 		if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(oldldt);
 		else
-			kfree(oldldt);
+			put_page(virt_to_page(oldldt));
 	}
 	return 0;
 }
@@ -117,7 +116,7 @@ void destroy_context(struct mm_struct *m
Quoted 8 lines

arch/x86/kernel/ldt_32.c void destroy_context(struct mm_struct *m · line 119

Quoted 5 lines
arch/x86/kernel/ldt_32.c +1 -1
 		if ((unsigned)mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(mm->context.ldt);
 		else
-			kfree(mm->context.ldt);
+			put_page(virt_to_page(mm->context.ldt));

why not free_page() with all those checks if it is correct virt address which it brings?

I followed the combination I found elsewhere - __get_free_page/put_page or alloc_page/free_page. I admit that I'm unclear about when/why to use one vs. the other pair.

Jan

Acked-by on v1 1/1
#3 lore raw
In reply to Jan Beulich
Quoted 2 lines · Jan Beulich

Don't rely on kmalloc(PAGE_SIZE) returning PAGE_SIZE aligned memory (Xen requires GDT *and* LDT to be page-aligned).

Can kmalloc return non-page-aligned PAGE_SIZE allocations?

Quoted 3 lines

Using the page
allocator interface also removes the (albeit small) slab allocator overhead.

Runtime or space overhead? Given that they're once-off allocations, the time part isn't a big factor. And apparently LDT is completely unused.

Seems like a reasonable change either way.

Quoted 5 lines

The same change being done for 64-bits for consistency.

Further, the Xen hypercall interface expects the LDT address to be virtual, not machine.

Shows how much the LDT gets used then... Hm, but I didn't make that code up; I wonder where it came from? But certainly, passing the mfn of the first page of a vmalloc area doesn't make any sense.

Quoted 3 lines
Quoted 107 lines
---
 arch/x86/kernel/ldt_32.c |    7 +++----
 arch/x86/kernel/ldt_64.c |    7 +++----
 arch/x86/xen/enlighten.c |    9 +--------
 3 files changed, 7 insertions(+), 16 deletions(-)
arch/x86/kernel/ldt_32.c +3 -4
@@ -9,7 +9,6 @@
 #include <linux/mm.h>
 #include <linux/smp.h>
 #include <linux/vmalloc.h>
-#include <linux/slab.h>
 
 #include <asm/uaccess.h>
 #include <asm/system.h>
@@ -38,7 +37,7 @@ static int alloc_ldt(mm_context_t *pc, i
 	if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
 		newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
 	else
-		newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
+		newldt = (void *)__get_free_page(GFP_KERNEL);
 
 	if (!newldt)
 		return -ENOMEM;
@@ -69,7 +68,7 @@ static int alloc_ldt(mm_context_t *pc, i
 		if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(oldldt);
 		else
-			kfree(oldldt);
+			put_page(virt_to_page(oldldt));
 	}
 	return 0;
 }
@@ -114,7 +113,7 @@ void destroy_context(struct mm_struct *m
 		if (mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(mm->context.ldt);
 		else
-			kfree(mm->context.ldt);
+			put_page(virt_to_page(mm->context.ldt));
 		mm->context.size = 0;
 	}
 }
arch/x86/kernel/ldt_64.c +3 -4
@@ -12,7 +12,6 @@
 #include <linux/mm.h>
 #include <linux/smp.h>
 #include <linux/vmalloc.h>
-#include <linux/slab.h>
 
 #include <asm/uaccess.h>
 #include <asm/system.h>
@@ -41,7 +40,7 @@ static int alloc_ldt(mm_context_t *pc, u
 	if (mincount*LDT_ENTRY_SIZE > PAGE_SIZE)
 		newldt = vmalloc(mincount*LDT_ENTRY_SIZE);
 	else
-		newldt = kmalloc(mincount*LDT_ENTRY_SIZE, GFP_KERNEL);
+		newldt = (void *)__get_free_page(GFP_KERNEL);
 
 	if (!newldt)
 		return -ENOMEM;
@@ -73,7 +72,7 @@ static int alloc_ldt(mm_context_t *pc, u
 		if (oldsize*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(oldldt);
 		else
-			kfree(oldldt);
+			put_page(virt_to_page(oldldt));
 	}
 	return 0;
 }
@@ -117,7 +116,7 @@ void destroy_context(struct mm_struct *m
 		if ((unsigned)mm->context.size*LDT_ENTRY_SIZE > PAGE_SIZE)
 			vfree(mm->context.ldt);
 		else
-			kfree(mm->context.ldt);
+			put_page(virt_to_page(mm->context.ldt));
 		mm->context.size = 0;
 	}
 }
arch/x86/xen/enlighten.c +1 -8
@@ -275,19 +275,12 @@ static unsigned long xen_store_tr(void)
 
 static void xen_set_ldt(const void *addr, unsigned entries)
 {
-	unsigned long linear_addr = (unsigned long)addr;
 	struct mmuext_op *op;
 	struct multicall_space mcs = xen_mc_entry(sizeof(*op));
 
 	op = mcs.args;
 	op->cmd = MMUEXT_SET_LDT;
-	if (linear_addr) {
-		/* ldt my be vmalloced, use arbitrary_virt_to_machine */
-		xmaddr_t maddr;
-		maddr = arbitrary_virt_to_machine((unsigned long)addr);
-		linear_addr = (unsigned long)maddr.maddr;
-	}
-	op->arg1.linear_addr = linear_addr;
+	op->arg1.linear_addr = (unsigned long)addr;
 	op->arg2.nr_ents = entries;
 
 	MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
#4 lore raw
Quoted 6 lines
Quoted 1 line
Quoted 1 line

Jeremy Fitzhardinge <[email protected]> 11.01.08 18:28 >>>

Quoted 2 lines · Jan Beulich

Don't rely on kmalloc(PAGE_SIZE) returning PAGE_SIZE aligned memory (Xen requires GDT *and* LDT to be page-aligned).

Can kmalloc return non-page-aligned PAGE_SIZE allocations?

Documentation says it's to return pointer-size aligned memory - any excess alignment is therefore an implementation detail. (Nevertheless, afaics all current allocators generate page-aligned chunks.)

Quoted 6 lines
Quoted 3 lines

Using the page
allocator interface also removes the (albeit small) slab allocator overhead.

Runtime or space overhead? Given that they're once-off allocations, the time part isn't a big factor. And apparently LDT is completely unused.

Both, but as I also said the saving is small.

Jan

#5 lore raw
In reply to Jan Beulich
Quoted 7 lines · Jan Beulich

Don't rely on kmalloc(PAGE_SIZE) returning PAGE_SIZE aligned memory (Xen requires GDT *and* LDT to be page-aligned). Using the page allocator interface also removes the (albeit small) slab allocator overhead. The same change being done for 64-bits for consistency.

Further, the Xen hypercall interface expects the LDT address to be virtual, not machine.

coul dyou try this against x86.git as well? We already unified ldt.c as part of the paravirt patches.

Ingo

#6 lore raw
Quoted 9 lines
Quoted 1 line
Quoted 1 line

Ingo Molnar <[email protected]> 14.01.08 16:48 >>>

Quoted 5 lines · Jan Beulich

Don't rely on kmalloc(PAGE_SIZE) returning PAGE_SIZE aligned memory (Xen requires GDT *and* LDT to be page-aligned). Using the page allocator interface also removes the (albeit small) slab allocator overhead. The same change being done for 64-bits for consistency.

Quoted 5 lines
Quoted 2 lines

Further, the Xen hypercall interface expects the LDT address to be virtual, not machine.

coul dyou try this against x86.git as well? We already unified ldt.c as part of the paravirt patches.

Sure, but it may take me a little while.

Jan

#7 lore raw
Quoted 5 lines · Jan Beulich
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

Ingo Molnar <[email protected]> 14.01.08 16:48 >>>

* Jan Beulich <[email protected]> wrote:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

Don't rely on kmalloc(PAGE_SIZE) returning PAGE_SIZE aligned memory (Xen requires GDT *and* LDT to be page-aligned). Using the page allocator interface also removes the (albeit small) slab allocator overhead. The same change being done for 64-bits for consistency.

Further, the Xen hypercall interface expects the LDT address to be virtual, not machine.

coul dyou try this against x86.git as well? We already unified ldt.c as part of the paravirt patches.

Sure, but it may take me a little while.

I've got the unified version here.

Subject: xen: adjust/fix LDT handling for Xen

From: "Jan Beulich" <[email protected]>

Don't rely on kmalloc(PAGE_SIZE) returning PAGE_SIZE aligned memory (Xen requires GDT *and* LDT to be page-aligned). Using the page allocator interface also removes the (albeit small) slab allocator overhead. The same change being done for 64-bits for consistency.

Further, the Xen hypercall interface expects the LDT address to be virtual, not machine.

[ Adjusted to unified ldt.c - Jeremy ]

---

arch/x86/kernel/ldt.c    |    7 +++----
arch/x86/xen/enlighten.c |    9 +--------
2 files changed, 4 insertions(+), 12 deletions(-)

===================================================================

arch/x86/kernel/ldt.c +0 -0
@@ -12,7 +12,6 @@

#include <linux/mm.h>
#include <linux/smp.h>
#include <linux/vmalloc.h>
-#include <linux/slab.h>

#include <asm/uaccess.h>
#include <asm/system.h>
@@ -40,7 +39,7 @@ static int alloc_ldt(mm_context_t *pc, i

	if (mincount * LDT_ENTRY_SIZE > PAGE_SIZE)
		newldt = vmalloc(mincount * LDT_ENTRY_SIZE);
	else

- newldt = kmalloc(mincount * LDT_ENTRY_SIZE, GFP_KERNEL);
+ newldt = (void *)__get_free_page(GFP_KERNEL);

	if (!newldt)
		return -ENOMEM;
@@ -78,7 +77,7 @@ static int alloc_ldt(mm_context_t *pc, i
		if (oldsize * LDT_ENTRY_SIZE > PAGE_SIZE)
			vfree(oldldt);
		else
-			kfree(oldldt);
+			put_page(virt_to_page(oldldt));
	}
	return 0;
}
@@ -129,7 +128,7 @@ void destroy_context(struct mm_struct *m
		if (mm->context.size * LDT_ENTRY_SIZE > PAGE_SIZE)
			vfree(mm->context.ldt);
		else
-			kfree(mm->context.ldt);
+			put_page(virt_to_page(mm->context.ldt));
		mm->context.size = 0;
	}
}

===================================================================

arch/x86/xen/enlighten.c +0 -0
@@ -275,19 +275,12 @@ static unsigned long xen_store_tr(void)
 

static void xen_set_ldt(const void *addr, unsigned entries) {

-	unsigned long linear_addr = (unsigned long)addr;
	struct mmuext_op *op;
	struct multicall_space mcs = xen_mc_entry(sizeof(*op));

	op = mcs.args;
	op->cmd = MMUEXT_SET_LDT;

- if (linear_addr) {
- /* ldt my be vmalloced, use arbitrary_virt_to_machine */
- xmaddr_t maddr;
- maddr = arbitrary_virt_to_machine((unsigned long)addr);
- linear_addr = (unsigned long)maddr.maddr;
- }
- op->arg1.linear_addr = linear_addr;

+	op->arg1.linear_addr = (unsigned long)addr;
	op->arg2.nr_ents = entries;

	MULTI_mmuext_op(mcs.mc, op, 1, NULL, DOMID_SELF);
Applied
#8 lore raw
Quoted 7 lines · Jeremy Fitzhardinge
Quoted 5 lines
Quoted 3 lines

coul dyou try this against x86.git as well? We already unified ldt.c as part of the paravirt patches.

Sure, but it may take me a little while.

I've got the unified version here.

thanks, applied.

Quoted 1 line

please use Signed-off-by when passing through (or modifying) patches.

Ingo

#9 lore raw
Quoted 11 lines · Ingo Molnar
Quoted 7 lines · Jeremy Fitzhardinge
Quoted 5 lines
Quoted 3 lines

coul dyou try this against x86.git as well? We already unified ldt.c as part of the paravirt patches.

Sure, but it may take me a little while.

I've got the unified version here.

Quoted 8 lines

thanks, applied.

Quoted 2 lines

please use Signed-off-by when passing through (or modifying) patches.

Will do. That was left over from trying to apply the original version.

J