Skip to content

Commit 2dbb51c

Browse files
gormanmtorvalds
authored andcommitted
mm: make defensive checks around PFN values registered for memory usage
There are a number of different views to how much memory is currently active. There is the arch-independent zone-sizing view, the bootmem allocator and memory models view. Architectures register this information at different times and is not necessarily in sync particularly with respect to some SPARSEMEM limitations. This patch introduces mminit_validate_memmodel_limits() which is able to validate and correct PFN ranges with respect to the memory model. It is only SPARSEMEM that currently validates itself. Signed-off-by: Mel Gorman <mel@csn.ul.ie> Cc: Christoph Lameter <cl@linux-foundation.org> Cc: Andy Whitcroft <apw@shadowen.org> Cc: Ingo Molnar <mingo@elte.hu> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 708614e commit 2dbb51c

4 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎mm/bootmem.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static unsigned long __init init_bootmem_core(pg_data_t *pgdat,
9191
bootmem_data_t *bdata = pgdat->bdata;
9292
unsigned long mapsize;
9393

94+
mminit_validate_memmodel_limits(&start, &end);
9495
bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
9596
bdata->node_boot_start = PFN_PHYS(start);
9697
bdata->node_low_pfn = end;

‎mm/internal.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,16 @@ static inline void mminit_verify_page_links(struct page *page,
9898
{
9999
}
100100
#endif /* CONFIG_DEBUG_MEMORY_INIT */
101+
102+
/* mminit_validate_memmodel_limits is independent of CONFIG_DEBUG_MEMORY_INIT */
103+
#if defined(CONFIG_SPARSEMEM)
104+
extern void mminit_validate_memmodel_limits(unsigned long *start_pfn,
105+
unsigned long *end_pfn);
106+
#else
107+
static inline void mminit_validate_memmodel_limits(unsigned long *start_pfn,
108+
unsigned long *end_pfn)
109+
{
110+
}
111+
#endif /* CONFIG_SPARSEMEM */
112+
101113
#endif

‎mm/page_alloc.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3536,6 +3536,8 @@ void __init add_active_range(unsigned int nid, unsigned long start_pfn,
35363536
nid, start_pfn, end_pfn,
35373537
nr_nodemap_entries, MAX_ACTIVE_REGIONS);
35383538

3539+
mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
3540+
35393541
/* Merge with existing active regions if possible */
35403542
for (i = 0; i < nr_nodemap_entries; i++) {
35413543
if (early_node_map[i].nid != nid)

‎mm/sparse.c‎

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <asm/dma.h>
1313
#include <asm/pgalloc.h>
1414
#include <asm/pgtable.h>
15+
#include "internal.h"
1516

1617
/*
1718
* Permanent SPARSEMEM data:
@@ -147,22 +148,41 @@ static inline int sparse_early_nid(struct mem_section *section)
147148
return (section->section_mem_map >> SECTION_NID_SHIFT);
148149
}
149150

150-
/* Record a memory area against a node. */
151-
void __init memory_present(int nid, unsigned long start, unsigned long end)
151+
/* Validate the physical addressing limitations of the model */
152+
void __meminit mminit_validate_memmodel_limits(unsigned long *start_pfn,
153+
unsigned long *end_pfn)
152154
{
153-
unsigned long max_arch_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
154-
unsigned long pfn;
155+
unsigned long max_sparsemem_pfn = 1UL << (MAX_PHYSMEM_BITS-PAGE_SHIFT);
155156

156157
/*
157158
* Sanity checks - do not allow an architecture to pass
158159
* in larger pfns than the maximum scope of sparsemem:
159160
*/
160-
if (start >= max_arch_pfn)
161-
return;
162-
if (end >= max_arch_pfn)
163-
end = max_arch_pfn;
161+
if (*start_pfn > max_sparsemem_pfn) {
162+
mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
163+
"Start of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
164+
*start_pfn, *end_pfn, max_sparsemem_pfn);
165+
WARN_ON_ONCE(1);
166+
*start_pfn = max_sparsemem_pfn;
167+
*end_pfn = max_sparsemem_pfn;
168+
}
169+
170+
if (*end_pfn > max_sparsemem_pfn) {
171+
mminit_dprintk(MMINIT_WARNING, "pfnvalidation",
172+
"End of range %lu -> %lu exceeds SPARSEMEM max %lu\n",
173+
*start_pfn, *end_pfn, max_sparsemem_pfn);
174+
WARN_ON_ONCE(1);
175+
*end_pfn = max_sparsemem_pfn;
176+
}
177+
}
178+
179+
/* Record a memory area against a node. */
180+
void __init memory_present(int nid, unsigned long start, unsigned long end)
181+
{
182+
unsigned long pfn;
164183

165184
start &= PAGE_SECTION_MASK;
185+
mminit_validate_memmodel_limits(&start, &end);
166186
for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) {
167187
unsigned long section = pfn_to_section_nr(pfn);
168188
struct mem_section *ms;
@@ -187,6 +207,7 @@ unsigned long __init node_memmap_size_bytes(int nid, unsigned long start_pfn,
187207
unsigned long pfn;
188208
unsigned long nr_pages = 0;
189209

210+
mminit_validate_memmodel_limits(&start_pfn, &end_pfn);
190211
for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
191212
if (nid != early_pfn_to_nid(pfn))
192213
continue;

0 commit comments

Comments
 (0)