Skip to contentSkip to conversation

x86-64 linux: unmap temporary mappings established for setup of 1:1 mappings

PATCH In review Jan Beulich 2 participants 6 messages May 31, 2006 – Jun 6, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86-64 linux: unmap temporary mappings established for setup of 1:1 mappings

The temporary mappings needed to set up the 1:1 mappings must be torn down after use; otherwise they may trigger the WARN_ON() in vmap_pte_range() (namely if the chunk allocated to hold kernel and initial page tables gets close to or exceeds 128Mb, or if a sufficiently high mem= argument causes the static allocations to grow beyond 128Mb, which in either case means these mappings extend into the modules area).

Also some minor cleanup.

arch/x86_64/mm/init-xen.c +24 -10
Index: head-2006-05-26/arch/x86_64/mm/init-xen.c
===================================================================
@@ -56,6 +56,8 @@
 struct dma_mapping_ops* dma_ops;
 EXPORT_SYMBOL(dma_ops);
 
+int after_bootmem;
+
 extern unsigned long *contiguous_bitmap;
 
 static unsigned long dma_reserve __initdata;
@@ -74,7 +76,7 @@ extern unsigned long start_pfn;
 	(((mfn_to_pfn((addr) >> PAGE_SHIFT)) << PAGE_SHIFT) +	\
 	__START_KERNEL_map)))
 
-static void early_make_page_readonly(void *va, unsigned int feature)
+static void __meminit early_make_page_readonly(void *va, unsigned int feature)
 {
 	unsigned long addr, _va = (unsigned long)va;
 	pte_t pte, *ptep;
@@ -83,6 +85,11 @@ static void early_make_page_readonly(voi
 	if (xen_feature(feature))
 		return;
 
+	if (after_bootmem) {
+		make_page_readonly(va, feature);
+		return;
+	}
+
 	addr = (unsigned long) page[pgd_index(_va)];
 	addr_to_page(addr, page);
 
@@ -198,10 +205,6 @@ void show_mem(void)
 	printk(KERN_INFO "%lu pages swap cached\n",cached);
 }
 
-/* References to section boundaries */
-
-int after_bootmem;
-
 static void *spp_getpage(void)
 { 
 	void *ptr;
@@ -448,9 +452,9 @@ phys_pmd_init(pmd_t *pmd, unsigned long 
 		pte = alloc_static_page(&pte_phys);
 		pte_save = pte;
 		for (k = 0; k < PTRS_PER_PTE; pte++, k++, address += PTE_SIZE) {
-			if ((address >= end) ||
-			    ((address >> PAGE_SHIFT) >=
-			     xen_start_info->nr_pages)) { 
+			if (address >= (after_bootmem
+			                ? end
+			                : xen_start_info->nr_pages << PAGE_SHIFT)) {
 				__set_pte(pte, __pte(0)); 
 				continue;
 			}
@@ -550,7 +554,7 @@ void __init xen_init_pt(void)
 		mk_kernel_pgd(__pa_symbol(level3_user_pgt)));
 }
 
-void __init extend_init_mapping(unsigned long tables_space)
+static void __init extend_init_mapping(unsigned long tables_space)
 {
 	unsigned long va = __START_KERNEL_map;
 	unsigned long phys, addr, *pte_page;
@@ -666,7 +671,18 @@ void __meminit init_memory_mapping(unsig
 			set_pgd(pgd_offset_k(start), mk_kernel_pgd(pud_phys));
 	}
 
-	BUG_ON(!after_bootmem && start_pfn != table_end);
+	if (!after_bootmem) {
+		BUG_ON(start_pfn != table_end);
+
+		/* Destroy the temporary mappings created above. */
+		start = __START_KERNEL_map + (table_start << PAGE_SHIFT);
+		end = start + tables_space;
+		for (; start < end; start += PAGE_SIZE) {
+			/* Should also clear out and reclaim any page table
+			   pages no longer needed... */
+			WARN_ON(HYPERVISOR_update_va_mapping(start, __pte_ma(0), 0));
+		}
+	}
 
 	__flush_tlb_all();
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 4 lines · Jan Beulich

The temporary mappings needed to set up the 1:1 mappings must be torn down after use; otherwise they may trigger the WARN_ON() in vmap_pte_range() (namely if the chunk allocated to hold kernel and initial page tables gets close to or exceeds 128Mb, or if a sufficiently high mem= argument causes the static allocations to grow beyond 128Mb, which in either case means these mappings extend into the modules area).

I've applied this to -unstable, but in this patch I only see code to destroy the extended init mappings. What happens if you have a really big initrd (for example)? That will push the initial pagetables up into the modules area -- is that handled correctly now or is more fixup required?

Also your patch didn't apply to 3.0-testing. You'll need to supply a version specifically for -testing if you want it applied there.

-- Keir

#2 lore raw
Quoted 6 lines · Keir Fraser
Quoted 4 lines

The temporary mappings needed to set up the 1:1 mappings must be torn down after use; otherwise they may trigger the WARN_ON() in vmap_pte_range() (namely if the chunk allocated to hold kernel and initial page tables gets close to or exceeds 128Mb, or if a sufficiently high mem= argument causes the static allocations to grow beyond 128Mb, which in either case means these mappings extend into the modules area).

I've applied this to -unstable, but in this patch I only see code to destroy the extended init mappings. What happens if you have a really big initrd (for example)? That will push the initial pagetables up into the modules area -- is that handled correctly now or is more fixup required?

Might it make sense to zap all init mappings (after kernel code/data) in mem_init(), where we leave 'bootmem mode'? At that point initrd etc. can all be accessed via the main 1:1 mapping. I think this should work so long as we don't have any residual pointers to the init mapping hanging around.

Another concern I have is that our x86/64 mm/init.c is still very complicated and rather different from native mm/init.c (even though I did clean it up a lot some time ago). I'm interested in any ideas about how to reduce the complexity...

-- Keir

#3 lore raw
In reply to Keir Fraser
Quoted 12 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 01.06.06 20:21 >>>

Quoted 7 lines · Jan Beulich

The temporary mappings needed to set up the 1:1 mappings must be torn down after use; otherwise they may trigger the
WARN_ON() in vmap_pte_range() (namely if the chunk allocated to hold kernel and initial page tables gets close to or
exceeds 128Mb, or if a sufficiently high mem= argument causes the static allocations to grow beyond 128Mb, which in
either case means these mappings extend into the modules area).

Quoted 5 lines

I've applied this to -unstable, but in this patch I only see code to destroy the extended init mappings. What happens if you have a really big initrd (for example)? That will push the initial pagetables up into the modules area -- is that handled correctly now or is more fixup required?

The initrd mappings, as I read it, get destroyed in setup_arch(). The thing that also came to my mind were the Xen-created page tables, which might need taking care of. The main issue here will be to precisely determine the boundary of where to start destruction - does Xen make any guarantees regarding the ordering of things within the initial allocation?

Quoted 2 lines

Also your patch didn't apply to 3.0-testing. You'll need to supply a version specifically for -testing if you want it applied there.

Yes, that patch would look different. As we have our own patch anyway, I don't think we insist on this being applied to 3.0-testing, but in case you want it I'm attaching the patch we have (one of its hunks applies with fuzz 1 only, but I think that should be okay).

Jan

From: [email protected]
Subject: Unmap temporary mappings established for setup of 1:1 mappings.
References: 175787

arch/x86_64/mm/init-xen.c +28 -12
Index: head-2006-05-26/arch/x86_64/mm/init-xen.c
===================================================================
@@ -56,6 +56,8 @@
 struct dma_mapping_ops* dma_ops;
 EXPORT_SYMBOL(dma_ops);
 
+int after_bootmem;
+
 extern unsigned long *contiguous_bitmap;
 
 #ifndef CONFIG_XEN
@@ -76,7 +78,7 @@ extern unsigned long start_pfn;
 	(((mfn_to_pfn((addr) >> PAGE_SHIFT)) << PAGE_SHIFT) +	\
 	__START_KERNEL_map)))
 
-static void early_make_page_readonly(void *va, unsigned int feature)
+static void __meminit early_make_page_readonly(void *va, unsigned int feature)
 {
 	unsigned long addr, _va = (unsigned long)va;
 	pte_t pte, *ptep;
@@ -85,6 +87,11 @@ static void early_make_page_readonly(voi
 	if (xen_feature(feature))
 		return;
 
+	if (after_bootmem) {
+		make_page_readonly(va, feature);
+		return;
+	}
+
 	addr = (unsigned long) page[pgd_index(_va)];
 	addr_to_page(addr, page);
 
@@ -200,10 +207,6 @@ void show_mem(void)
 	printk(KERN_INFO "%lu pages swap cached\n",cached);
 }
 
-/* References to section boundaries */
-
-int after_bootmem;
-
 static void *spp_getpage(void)
 { 
 	void *ptr;
@@ -372,7 +375,8 @@ void __set_fixmap_user (enum fixed_addre
 	set_pte_phys(address, phys, prot, SET_FIXMAP_USER); 
 }
 
-unsigned long __initdata table_start, tables_space; 
+unsigned long __initdata table_start;
+static unsigned long __initdata tables_space;
 
 unsigned long get_machine_pfn(unsigned long addr)
 {
@@ -444,9 +448,9 @@ phys_pmd_init(pmd_t *pmd, unsigned long 
 		pte = alloc_static_page(&pte_phys);
 		pte_save = pte;
 		for (k = 0; k < PTRS_PER_PTE; pte++, k++, address += PTE_SIZE) {
-			if ((address >= end) ||
-			    ((address >> PAGE_SHIFT) >=
-			     xen_start_info->nr_pages)) { 
+			if (address >= (after_bootmem
+			                ? end
+			                : xen_start_info->nr_pages << PAGE_SHIFT)) {
 				__set_pte(pte, __pte(0)); 
 				continue;
 			}
@@ -546,7 +550,7 @@ void __init xen_init_pt(void)
 		mk_kernel_pgd(__pa_symbol(level3_user_pgt)));
 }
 
-void __init extend_init_mapping(void) 
+static void __init extend_init_mapping(void)
 {
 	unsigned long va = __START_KERNEL_map;
 	unsigned long phys, addr, *pte_page;
@@ -617,7 +621,8 @@ static void __init find_early_table_spac
 	table_start = start_pfn;
 
 	early_printk("kernel direct mapping tables up to %lx @ %lx-%lx\n",
-		end, table_start << PAGE_SHIFT, start_pfn << PAGE_SHIFT);
+		end, table_start << PAGE_SHIFT,
+		(table_start << PAGE_SHIFT) + tables_space);
 }
 
 /* Setup the direct mapping of the physical memory at PAGE_OFFSET.
@@ -662,7 +667,18 @@ void __meminit init_memory_mapping(unsig
 			set_pgd(pgd_offset_k(start), mk_kernel_pgd(pud_phys));
 	}
 
-	BUG_ON(!after_bootmem && start_pfn != table_start + (tables_space >> PAGE_SHIFT));
+	if (!after_bootmem) {
+		BUG_ON(start_pfn != table_start + (tables_space >> PAGE_SHIFT));
+
+		/* Destroy the temporary mappings created above. */
+		start = __START_KERNEL_map + (table_start << PAGE_SHIFT);
+		end = start + tables_space;
+		for (; start < end; start += PAGE_SIZE) {
+			/* Should also clear out and reclaim any page table
+			   pages no longer needed... */
+			WARN_ON(HYPERVISOR_update_va_mapping(start, __pte_ma(0), 0));
+		}
+	}
 
 	__flush_tlb_all();
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#4 lore raw
Quoted 10 lines · Jan Beulich
Quoted 5 lines

I've applied this to -unstable, but in this patch I only see code to destroy the extended init mappings. What happens if you have a really big initrd (for example)? That will push the initial pagetables up into the modules area -- is that handled correctly now or is more fixup required?

The initrd mappings, as I read it, get destroyed in setup_arch(). The thing that also came to my mind were the Xen-created page tables, which might need taking care of. The main issue here will be to precisely determine the boundary of where to start destruction - does Xen make any guarantees regarding the ordering of things within the initial allocation?

Ah yes. Well, given that things like the console and xenbus pages are also above the initrd mapping, they must also be dealt with to properly be sure of a clear section of address space for modules.

I backed out the patch in -unstable since it didn't build. In any case I felt that it was doing more than just zapping the extra init mappings (it changed various bits and pieces, I think to allow safe calling of init_memory_mapping() after bootstrap, which I think we never currently do -- it belongs in a separate patch anyway).

I think I will wait until someone has a chance to look at this more thoroughly and can make a patch that zaps all the init mappings in one single place. I'll then look at backporting that patch to -testing.

-- Keir

#5 lore raw
Quoted 9 lines

I backed out the patch in -unstable since it didn't build. In any case I felt that it was doing more than just zapping the extra init mappings (it changed various bits and pieces, I think to allow safe calling of init_memory_mapping() after bootstrap, which I think we never currently do -- it belongs in a separate patch anyway).

I think I will wait until someone has a chance to look at this more thoroughly and can make a patch that zaps all the init mappings in one single place. I'll then look at backporting that patch to -testing.

While I saw that the essential part of the patch was applied (again), here is a patch to remove all early mappings (except for the kernel image itself, of course).

arch/x86_64/kernel/setup-xen.c +0 -16
Index: head-2006-06-06/arch/x86_64/kernel/setup-xen.c
===================================================================
@@ -819,14 +819,6 @@ void __init setup_arch(char **cmdline_p)
 #ifdef CONFIG_XEN
 	{
 		int i, j, k, fpp;
-		unsigned long va;
-
-		/* 'Initial mapping' of initrd must be destroyed. */
-		for (va = xen_start_info->mod_start;
-		     va < (xen_start_info->mod_start+xen_start_info->mod_len);
-		     va += PAGE_SIZE) {
-			HYPERVISOR_update_va_mapping(va, __pte_ma(0), 0);
-		}
 
 		if (!xen_feature(XENFEAT_auto_translated_physmap)) {
 			/* Make sure we have a large enough P->M table. */
@@ -842,14 +842,6 @@ void __init setup_arch(char **cmdline_p)
 				PFN_PHYS(PFN_UP(xen_start_info->nr_pages *
 						sizeof(unsigned long))));
 
-			/* Destroyed 'initial mapping' of old p2m table. */
-			for (va = xen_start_info->mfn_list;
-			     va < (xen_start_info->mfn_list +
-				   (xen_start_info->nr_pages*sizeof(unsigned long)));
-			     va += PAGE_SIZE) {
-				HYPERVISOR_update_va_mapping(va, __pte_ma(0), 0);
-			}
-
 			/*
 			 * Initialise the list of the frames that specify the
 			 * list of frames that make up the p2m table. Used by
arch/x86_64/mm/init-xen.c +17 -5
Index: head-2006-06-06/arch/x86_64/mm/init-xen.c
===================================================================
@@ -668,11 +668,23 @@ void __meminit init_memory_mapping(unsig
 
 	if (!after_bootmem) {
 		BUG_ON(start_pfn != table_end);
-		/*
-		 * Destroy the temporary mappings created above. Prevents
-		 * overlap with modules area (if init mapping is very big).
-		 */
-		start = __START_KERNEL_map + (table_start << PAGE_SHIFT);
+
+		/* Re-vector virtual addresses pointing into the initial
+		   mapping to the just established permanent ones. */
+		xen_start_info = __va(__pa(xen_start_info));
+		xen_start_info->pt_base = (unsigned long)__va(__pa(xen_start_info->pt_base));
+		if (!xen_feature(XENFEAT_auto_translated_physmap)) {
+			phys_to_machine_mapping = __va(__pa(xen_start_info->mfn_list));
+			xen_start_info->mfn_list = (unsigned long)phys_to_machine_mapping;
+		}
+		if (xen_start_info->mod_start)
+			xen_start_info->mod_start = (unsigned long)__va(__pa(xen_start_info->mod_start));
+
+		/* Destroy the Xen-created mappings beyond the kernel image as well
+		 * as the temporary mappings created above. Prevents overlap with
+		 * modules area (if init mapping is very big).
+		 */
+		start = PAGE_ALIGN((unsigned long)_end);
 		end   = __START_KERNEL_map + (table_end   << PAGE_SHIFT);
 		for (; start < end; start += PAGE_SIZE)
 			WARN_ON(HYPERVISOR_update_va_mapping(

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel