Skip to contentSkip to conversation

x86: Exclude E820_RESERVED regions and memory holes above 4 GB from direct mapping.

PATCH In review Jacob Shin 5 participants 18 messages Oct 20, 2011 – Oct 17, 2012 x86

lore
Share & tools
lore

Jacob Shin

author
#0 lore raw

PATCH 1/1 x86: Exclude E820_RESERVED regions and memory holes above 4 GB from direct mapping.

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

---
 arch/x86/kernel/setup.c |   17 +++++++++++++++--
 1 files changed, 15 insertions(+), 2 deletions(-)
arch/x86/kernel/setup.c +15 -2
index afaf384..af19a61 100644
@@ -937,8 +937,21 @@ void __init setup_arch(char **cmdline_p)
 
 #ifdef CONFIG_X86_64
 	if (max_pfn > max_low_pfn) {
-		max_pfn_mapped = init_memory_mapping(1UL<<32,
-						     max_pfn<<PAGE_SHIFT);
+		int i;
+		for (i = 0; i < e820.nr_map; i++) {
+			struct e820entry *ei = &e820.map[i];
+
+			if (ei->addr + ei->size <= 1UL << 32)
+				continue;
+
+			if (ei->type == E820_RESERVED)
+				continue;
+
+			max_pfn_mapped = init_memory_mapping(
+				ei->addr < 1UL << 32 ? 1UL << 32 : ei->addr,
+				ei->addr + ei->size);
+		}
+
 		/* can we preseve max_low_pfn ?*/
 		max_low_pfn = max_pfn;
 	}
Signature · 1 line
1.7.1
#1 lore raw
Quoted 3 lines · Jacob Shin

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

This doesn't make much sense. Holes above 4GB are completely legal.

If you need to workaround a specific broken BIOS you would need a quirk only matching that system, with a suitable "BIOS is broken" message.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
#2 lore raw
Quoted 11 lines · Andi Kleen
Quoted 3 lines · Jacob Shin

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

This doesn't make much sense. Holes above 4GB are completely legal.

If you need to workaround a specific broken BIOS you would need a quirk only matching that system, with a suitable "BIOS is broken" message.

The problem is that apparently right now we map those unconditionally into the 1:1 map and mark them cacheable in PAT, which we *don't* for the < 4 GiB map.

This thus makes the behavior match < 4 GiB, which is the correct behavior; this should be made clear in the patch description.

-hpa

#3 lore raw
Quoted 13 lines · H. Peter Anvin
Quoted 11 lines · Andi Kleen
Quoted 3 lines · Jacob Shin

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

This doesn't make much sense. Holes above 4GB are completely legal.

If you need to workaround a specific broken BIOS you would need a quirk only matching that system, with a suitable "BIOS is broken" message.

Quoted 7 lines

The problem is that apparently right now we map those unconditionally into the 1:1 map and mark them cacheable in PAT, which we *don't* for the < 4 GiB map.

This thus makes the behavior match < 4 GiB, which is the correct behavior; this should be made clear in the patch description.

Specifically, it's fine for them to be mapped; it's not fine for them to be mapped cacheable.

-hpa

author
#4 lore raw
In reply to H. Peter Anvin
Quoted 13 lines · H. Peter Anvin
Quoted 11 lines · Andi Kleen
Quoted 3 lines · Jacob Shin

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

This doesn't make much sense. Holes above 4GB are completely legal.

If you need to workaround a specific broken BIOS you would need a quirk only matching that system, with a suitable "BIOS is broken" message.

Quoted 6 lines

The problem is that apparently right now we map those unconditionally into the 1:1 map and mark them cacheable in PAT, which we *don't* for the < 4 GiB map.

This thus makes the behavior match < 4 GiB, which is the correct behavior; this should be made clear in the patch description.

Will something like:

"On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Avoid mapping them unconditionally into kernel 1:1 direct mapping as cacheable memory, as we also already do for the MMIO hole under 4 GB."

Work?

Otherwise, does the patch look acceptable?

Thanks!

-Jacob Shin
Advanced Micro Devices, Inc.

Quoted 3 lines

-hpa

#5 lore raw
Quoted 23 lines · Jacob Shin
Quoted 13 lines · H. Peter Anvin
Quoted 11 lines · Andi Kleen
Quoted 3 lines · Jacob Shin

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

This doesn't make much sense. Holes above 4GB are completely legal.

If you need to workaround a specific broken BIOS you would need a quirk only matching that system, with a suitable "BIOS is broken" message.

Quoted 6 lines

The problem is that apparently right now we map those unconditionally into the 1:1 map and mark them cacheable in PAT, which we *don't* for the < 4 GiB map.

This thus makes the behavior match < 4 GiB, which is the correct behavior; this should be made clear in the patch description.

Will something like:

Quoted 9 lines

"On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Avoid mapping them unconditionally into kernel 1:1 direct mapping as cacheable memory, as we also already do for the MMIO hole under 4 GB."

Work?

Otherwise, does the patch look acceptable?

Drop the first half, and stop talking about "the MMIO hole" or anything else with a definite article.

We're either doing this correctly for all holes or we have a bug. If we have a bug we should fix it in a general way, and I'm not convinced that your patch is general enough.

I'm very bandwidth-constrained between kernel.org remediation and about to leave for kernel summit, so I'm not sure how much detail I can look into it right now.

-hpa

#6 lore raw
In reply to Jacob Shin

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 948

Quoted 6 lines · Jacob Shin

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

arch/x86/kernel/setup.c +2 -0
+			if (ei->type == E820_RESERVED)
+				continue;

This should probably be ei->type != E820_RAM or something similar. I haven't looked yet, what does the < 4 GiB code do?

-hpa

author
#7 lore raw
Quoted 10 lines · H. Peter Anvin

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 948

Quoted 6 lines · Jacob Shin

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

arch/x86/kernel/setup.c +2 -0
+			if (ei->type == E820_RESERVED)
+				continue;

This should probably be ei->type != E820_RAM or something similar. I haven't looked yet, what does the < 4 GiB code do?

Hm, okay, it calls e820_end_of_low_ram_pfn() which effectively is != E820_RAM.

I'll fix this, test, then resend.

Thanks!

-Jacob Shin
Advanced Micro Devices, Inc.

Quoted 4 lines

-hpa

#8 lore raw
Quoted 12 lines · Jacob Shin
Quoted 10 lines · H. Peter Anvin

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 948

Quoted 6 lines · Jacob Shin

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

arch/x86/kernel/setup.c +2 -0
+			if (ei->type == E820_RESERVED)
+				continue;

This should probably be ei->type != E820_RAM or something similar. I haven't looked yet, what does the < 4 GiB code do?

Quoted 5 lines

Hm, okay, it calls e820_end_of_low_ram_pfn() which effectively is != E820_RAM.

I'll fix this, test, then resend.

I never got any kind of updated patch, did I?

-hpa

author
#9 lore raw
Quoted 13 lines · H. Peter Anvin
Quoted 12 lines · Jacob Shin
Quoted 10 lines · H. Peter Anvin

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 948

Quoted 6 lines · Jacob Shin

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

arch/x86/kernel/setup.c +2 -0
+			if (ei->type == E820_RESERVED)
+				continue;

This should probably be ei->type != E820_RAM or something similar. I haven't looked yet, what does the < 4 GiB code do?

Quoted 7 lines
Quoted 5 lines

Hm, okay, it calls e820_end_of_low_ram_pfn() which effectively is != E820_RAM.

I'll fix this, test, then resend.

I never got any kind of updated patch, did I?

No, I never sent one out, because it would have still only covered > 4GB, and in later emails, you said that you wanted a general one that covered all x86.

I'll give it another shot at the generic patch, making a special case for the < 1MB ISA region.

Thanks!

Quoted 4 lines

-hpa

author Reviewed-by Andreas Herrmann
#10 lore raw
Quoted 20 lines · Jacob Shin
Quoted 19 lines · H. Peter Anvin
Quoted 17 lines · Jacob Shin
Quoted 10 lines · H. Peter Anvin

Review

Quoted 6 lines · Jacob Shin

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

(quoted diff) +2 -0
+			if (ei->type == E820_RESERVED)
+				continue;

This should probably be ei->type != E820_RAM or something similar. I haven't looked yet, what does the < 4 GiB code do?

Hm, okay, it calls e820_end_of_low_ram_pfn() which effectively is != E820_RAM.

I'll fix this, test, then resend.

Quoted 8 lines
Quoted 1 line

I never got any kind of updated patch, did I?

No, I never sent one out, because it would have still only covered > 4GB, and in later emails, you said that you wanted a general one that covered all x86.

I'll give it another shot at the generic patch, making a special case for the < 1MB ISA region.

Here is the new patch, thanks!

Quoted 1 line

From dad99fe54eb26d4022a48f1f9b88c21f77809282 Mon Sep 17 00:00:00 2001

From: Jacob Shin <[email protected]>
Date: Thu, 15 Dec 2011 10:56:14 -0500
Subject: [PATCH] x86: Only include address ranges marked as E820_RAM in kernel direct mapping

Currently, 0 ~ max_low_pfn is first mapped, then 4GB ~ max_pfn is mapped. On some systems that have large memory holes that occur within those two regions, we end up with PATs that mark pages that are not backed by actual DRAM -- as cacheable.

This patch first maps 0 ~ 1MB ISA region, then iterates over the E820 to map useable E820_RAM ranges.

---
 arch/x86/kernel/setup.c |   29 ++++++++++++++++++++++++++---
 1 files changed, 26 insertions(+), 3 deletions(-)
arch/x86/kernel/setup.c +26 -3
index cf0ef98..eae6b41 100644
@@ -691,6 +691,8 @@ early_param("reservelow", parse_reservelow);
 
 void __init setup_arch(char **cmdline_p)
 {
+	int i;
+
 #ifdef CONFIG_X86_32
 	memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data));
 	visws_early_detect();
@@ -932,13 +934,34 @@ void __init setup_arch(char **cmdline_p)
 	init_gbpages();
 
 	/* max_pfn_mapped is updated here */
-	max_low_pfn_mapped = init_memory_mapping(0, max_low_pfn<<PAGE_SHIFT);
+	max_low_pfn_mapped = init_memory_mapping(0, 0x100000);
 	max_pfn_mapped = max_low_pfn_mapped;
 
+	for (i = 0; i < e820.nr_map; i++) {
+		struct e820entry *ei = &e820.map[i];
+		u64 start = ei->addr;
+		u64 end = ei->addr + ei->size;
+
+		if (ei->type != E820_RAM)
+			continue;
+
+		if (start < 0x100000)
+			continue;
+#ifdef CONFIG_X86_32
+		if ((start >> PAGE_SHIFT) >= max_low_pfn)
+			continue;
+
+		if ((end >> PAGE_SHIFT) > max_low_pfn)
+			end = max_low_pfn << PAGE_SHIFT;
+#endif
+		max_pfn_mapped = init_memory_mapping(start, end);
+
+		if ((end >> PAGE_SHIFT) == max_low_pfn)
+			max_low_pfn_mapped = max_pfn_mapped;
+	}
+
 #ifdef CONFIG_X86_64
 	if (max_pfn > max_low_pfn) {
-		max_pfn_mapped = init_memory_mapping(1UL<<32,
-						     max_pfn<<PAGE_SHIFT);
 		/* can we preseve max_low_pfn ?*/
 		max_low_pfn = max_pfn;
 	}
Signature · 1 line
1.7.1
#11 lore raw
Quoted 93 lines · Jacob Shin
Quoted 20 lines · Jacob Shin
Quoted 19 lines · H. Peter Anvin
Quoted 17 lines · Jacob Shin
Quoted 10 lines · H. Peter Anvin

Review

Quoted 6 lines · Jacob Shin

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

(quoted diff) +2 -0
+			if (ei->type == E820_RESERVED)
+				continue;

This should probably be ei->type != E820_RAM or something similar. I haven't looked yet, what does the < 4 GiB code do?

Hm, okay, it calls e820_end_of_low_ram_pfn() which effectively is != E820_RAM.

I'll fix this, test, then resend.

Quoted 8 lines
Quoted 1 line

I never got any kind of updated patch, did I?

No, I never sent one out, because it would have still only covered > 4GB, and in later emails, you said that you wanted a general one that covered all x86.

I'll give it another shot at the generic patch, making a special case for the < 1MB ISA region.

Here is the new patch, thanks!

From dad99fe54eb26d4022a48f1f9b88c21f77809282 Mon Sep 17 00:00:00 2001
From: Jacob Shin <[email protected]>
Date: Thu, 15 Dec 2011 10:56:14 -0500
Subject: [PATCH] x86: Only include address ranges marked as E820_RAM in kernel direct mapping

Currently, 0 ~ max_low_pfn is first mapped, then 4GB ~ max_pfn is mapped. On some systems that have large memory holes that occur within those two regions, we end up with PATs that mark pages that are not backed by actual DRAM -- as cacheable.

This patch first maps 0 ~ 1MB ISA region, then iterates over the E820 to map useable E820_RAM ranges.

---
 arch/x86/kernel/setup.c |   29 ++++++++++++++++++++++++++---
 1 files changed, 26 insertions(+), 3 deletions(-)
arch/x86/kernel/setup.c +23 -1
index cf0ef98..eae6b41 100644
@@ -691,6 +691,8 @@ early_param("reservelow", parse_reservelow);
 
 void __init setup_arch(char **cmdline_p)
 {
+	int i;
+
 #ifdef CONFIG_X86_32
 	memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data));
 	visws_early_detect();
@@ -932,13 +934,34 @@ void __init setup_arch(char **cmdline_p)
 	init_gbpages();
 
 	/* max_pfn_mapped is updated here */
-	max_low_pfn_mapped = init_memory_mapping(0, max_low_pfn<<PAGE_SHIFT);
+	max_low_pfn_mapped = init_memory_mapping(0, 0x100000);
 	max_pfn_mapped = max_low_pfn_mapped;
 
+	for (i = 0; i < e820.nr_map; i++) {
+		struct e820entry *ei = &e820.map[i];
+		u64 start = ei->addr;
+		u64 end = ei->addr + ei->size;
+
+		if (ei->type != E820_RAM)
+			continue;
+
+		if (start < 0x100000)
+			continue;
+#ifdef CONFIG_X86_32
+		if ((start >> PAGE_SHIFT) >= max_low_pfn)
+			continue;
+
+		if ((end >> PAGE_SHIFT) > max_low_pfn)
+			end = max_low_pfn << PAGE_SHIFT;
+#endif
+		max_pfn_mapped = init_memory_mapping(start, end);
+
+		if ((end >> PAGE_SHIFT) == max_low_pfn)

arch/x86/kernel/setup.c void __init setup_arch(char **cmdline_p) · line 967

Quoted 10 lines
arch/x86/kernel/setup.c +3 -2
+			max_low_pfn_mapped = max_pfn_mapped;
+	}
+
 #ifdef CONFIG_X86_64
 	if (max_pfn > max_low_pfn) {
-		max_pfn_mapped = init_memory_mapping(1UL<<32,
-						     max_pfn<<PAGE_SHIFT);
 		/* can we preseve max_low_pfn ?*/
 		max_low_pfn = max_pfn;
 	}

no, you change the meaning max_low_pfn_mapped and max_pfn_mapped for x86_64 at least.

before your patch:
max_low_pfn_mapped is the mapped pfn beblow 4g.
max_pfn_mapped: is mapped pfn.

after your patch, those two variables does not mean the memory [0, max_low_pfn_mapped) and [4g<<12, max_pfn_mapped) are really mapped.

so in arch/x86/platform/efi/efi.c

                if (end_pfn <= max_low_pfn_mapped
                    || (end_pfn > (1UL << (32 - PAGE_SHIFT))
                        && end_pfn <= max_pfn_mapped))
                        va = __va(md->phys_addr);
                else
                        va = efi_ioremap(md->phys_addr, size, md->type);

and others will have problem.

to solve your problem:
1. unmap the HT range ?
2. or introduce mapped_pfn_range array?

Thanks

Yinghai Lu

#12 lore raw
Quoted 10 lines · Yinghai Lu

no, you change the meaning max_low_pfn_mapped and max_pfn_mapped for x86_64 at least.

before your patch:
max_low_pfn_mapped is the mapped pfn beblow 4g.
max_pfn_mapped: is mapped pfn.

after your patch, those two variables does not mean the memory [0, max_low_pfn_mapped) and [4g<<12, max_pfn_mapped) are really mapped.

And that's exactly the problem. It is BROKEN -- as in fundamentally dangerous -- for these mappings to exist. It is because the model is too inflexible.

Quoted 10 lines

so in arch/x86/platform/efi/efi.c

                 if (end_pfn<= max_low_pfn_mapped
                     || (end_pfn>  (1UL<<  (32 - PAGE_SHIFT))
                         &&  end_pfn<= max_pfn_mapped))
                         va = __va(md->phys_addr);
                 else
                         va = efi_ioremap(md->phys_addr, size, md->type);
Quoted 5 lines

and others will have problem.

to solve your problem:
1. unmap the HT range ?
2. or introduce mapped_pfn_range array?

1 is fundamentally a braindead hack that solves one case without solving the overall problem.

For 2 - why can't we simply make the invariant that E820_RAM is mapped and nothing else, with the sole exceptions being the 1 MiB (fixed MTRR)?

For things like efi.c we should make sure to have interfaces instead of open-code this kind of stuff.

-hpa

#13 lore raw
Quoted 3 lines · H. Peter Anvin

For 2 - why can't we simply make the invariant that E820_RAM is mapped and nothing else, with the sole exceptions being the 1 MiB (fixed MTRR)?

Yes, we could do that. but need to track mapped area in simple way. like later memory hotadd or remove to update those tracking.

Thanks

Yinghai

#14 lore raw
Quoted 9 lines · Yinghai Lu
Quoted 3 lines · H. Peter Anvin

For 2 - why can't we simply make the invariant that E820_RAM is mapped and nothing else, with the sole exceptions being the 1 MiB (fixed MTRR)?

Yes, we could do that. but need to track mapped area in simple way. like later memory hotadd or remove to update those tracking.

Well, there are two options for memory hotplug: either we always leave address space that can be used for memory hotplug mapped at all times, or we need to track it anyway. Either way we need to know where there regions are. Am I correct that right now we always map memory hotpluggable regions, being below the top pfn?

-hpa

#15 lore raw
Quoted 6 lines · H. Peter Anvin

Well, there are two options for memory hotplug: either we always leave address space that can be used for memory hotplug mapped at all times, or we need to track it anyway.  Either way we need to know where there regions are.  Am I correct that right now we always map memory hotpluggable regions, being below the top pfn?

in arch/x86/mm/init_64.c::arch_add_memory()
it will call init_memory_mapping to map new added memory. and will update max_pfn_mapped, max-pfn, max_low_pfn.

Thanks

Yinghai Lu

#16 lore raw
Quoted 8 lines · Yinghai Lu
Quoted 6 lines · H. Peter Anvin

Well, there are two options for memory hotplug: either we always leave address space that can be used for memory hotplug mapped at all times, or we need to track it anyway. Either way we need to know where there regions are. Am I correct that right now we always map memory hotpluggable regions, being below the top pfn?

Quoted 4 lines

in arch/x86/mm/init_64.c::arch_add_memory()
it will call init_memory_mapping to map new added memory. and will update max_pfn_mapped, max-pfn, max_low_pfn.

OK, so that would seem to match the invariant that all the memory blocks which are RAM have mappings, and the ones that don't don't have them, or is that violated elsewhere? Either way, the same notion applies -- we should either leverage an existing range map or have a new one, but there is nothing magic about holes above or below the 4 GiB boundary; the only "magic" is for 32 bits the HIGHMEM boundary and < 1 MiB (because of the fixed MTRRs).

-hpa

#17 lore raw

[tip:x86/urgent] x86: Exclude E820_RESERVED regions and memory holes above 4 GB from direct mapping .

In reply to Jacob Shin
Commit-ID:  1bbbbe779aabe1f0768c2bf8f8c0a5583679b54a
Gitweb:     http://git.kernel.org/tip/1bbbbe779aabe1f0768c2bf8f8c0a5583679b54a
Author:     Jacob Shin <[email protected]>
AuthorDate: Thu, 20 Oct 2011 16:15:26 -0500
Committer:  H. Peter Anvin <[email protected]>
CommitDate: Wed, 17 Oct 2012 10:59:39 -0700

x86: Exclude E820_RESERVED regions and memory holes above 4 GB from direct mapping.

On systems with very large memory (1 TB in our case), BIOS may report a reserved region or a hole in the E820 map, even above the 4 GB range. Exclude these from the direct mapping.

[ hpa: this should be done not just for > 4 GB but for everything above the legacy
region (1 MB), at the very least. That, however, turns out to require significant restructuring. That work is well underway, but is not suitable for rc/stable. ]

---
 arch/x86/kernel/setup.c |   17 +++++++++++++++--
 1 files changed, 15 insertions(+), 2 deletions(-)
arch/x86/kernel/setup.c +15 -2
index f4b9b80..198e774 100644
@@ -919,8 +919,21 @@ void __init setup_arch(char **cmdline_p)
 
 #ifdef CONFIG_X86_64
 	if (max_pfn > max_low_pfn) {
-		max_pfn_mapped = init_memory_mapping(1UL<<32,
-						     max_pfn<<PAGE_SHIFT);
+		int i;
+		for (i = 0; i < e820.nr_map; i++) {
+			struct e820entry *ei = &e820.map[i];
+
+			if (ei->addr + ei->size <= 1UL << 32)
+				continue;
+
+			if (ei->type == E820_RESERVED)
+				continue;
+
+			max_pfn_mapped = init_memory_mapping(
+				ei->addr < 1UL << 32 ? 1UL << 32 : ei->addr,
+				ei->addr + ei->size);
+		}
+
 		/* can we preseve max_low_pfn ?*/
 		max_low_pfn = max_pfn;
 	}