Skip to content

Commit df1bd59

Browse files
heicarstMartin Schwidefsky
authored andcommitted
s390/mem_detect: limit memory detection loop to "mem=" parameter
The current memory detection loop will detect all present memory of a machine. This is true even if the user specified the "mem=" parameter on the kernel command line. This can be a problem since the memory detection may cause a fully populated host page table for the guest, even for those parts of the memory that the guest will never use afterwards. So fix this and only detect memory up to a user supplied "mem=" limit if specified. Reported-by: Michael Johanssen <johanssn@de.ibm.com> Reviewed-by: Michael Holzheu <holzheu@linux.vnet.ibm.com> Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
1 parent 67b5c3e commit df1bd59

6 files changed

Lines changed: 25 additions & 9 deletions

File tree

‎arch/s390/include/asm/setup.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ extern struct mem_chunk memory_chunk[];
4646
extern int memory_end_set;
4747
extern unsigned long memory_end;
4848

49-
void detect_memory_layout(struct mem_chunk chunk[]);
49+
void detect_memory_layout(struct mem_chunk chunk[], unsigned long maxsize);
5050
void create_mem_hole(struct mem_chunk memory_chunk[], unsigned long addr,
5151
unsigned long size, int type);
5252

‎arch/s390/kernel/crash_dump.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static struct mem_chunk *get_memory_layout(void)
8888
struct mem_chunk *chunk_array;
8989

9090
chunk_array = kzalloc_panic(MEMORY_CHUNKS * sizeof(struct mem_chunk));
91-
detect_memory_layout(chunk_array);
91+
detect_memory_layout(chunk_array, 0);
9292
create_mem_hole(chunk_array, OLDMEM_BASE, OLDMEM_SIZE, CHUNK_CRASHK);
9393
return chunk_array;
9494
}

‎arch/s390/kernel/early.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,6 @@ void __init startup_init(void)
482482
detect_machine_facilities();
483483
setup_topology();
484484
sclp_facilities_detect();
485-
detect_memory_layout(memory_chunk);
486485
#ifdef CONFIG_DYNAMIC_FTRACE
487486
S390_lowcore.ftrace_func = (unsigned long)ftrace_caller;
488487
#endif

‎arch/s390/kernel/setup.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,7 @@ void __init setup_arch(char **cmdline_p)
10661066
memcpy(&uaccess, &uaccess_std, sizeof(uaccess));
10671067

10681068
parse_early_param();
1069-
1069+
detect_memory_layout(memory_chunk, memory_end);
10701070
os_info_init();
10711071
setup_ipl();
10721072
reserve_oldmem();

‎arch/s390/mm/mem_detect.c‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#define ADDR2G (1ULL << 31)
1414

15-
static void find_memory_chunks(struct mem_chunk chunk[])
15+
static void find_memory_chunks(struct mem_chunk chunk[], unsigned long maxsize)
1616
{
1717
unsigned long long memsize, rnmax, rzm;
1818
unsigned long addr = 0, size;
@@ -27,6 +27,8 @@ static void find_memory_chunks(struct mem_chunk chunk[])
2727
rzm = min(ADDR2G, rzm);
2828
memsize = memsize ? min(ADDR2G, memsize) : ADDR2G;
2929
}
30+
if (maxsize)
31+
memsize = memsize ? min((unsigned long)memsize, maxsize) : maxsize;
3032
do {
3133
size = 0;
3234
type = tprot(addr);
@@ -36,6 +38,8 @@ static void find_memory_chunks(struct mem_chunk chunk[])
3638
break;
3739
} while (type == tprot(addr + size));
3840
if (type == CHUNK_READ_WRITE || type == CHUNK_READ_ONLY) {
41+
if (memsize && (addr + size > memsize))
42+
size = memsize - addr;
3943
chunk[i].addr = addr;
4044
chunk[i].size = size;
4145
chunk[i].type = type;
@@ -45,7 +49,20 @@ static void find_memory_chunks(struct mem_chunk chunk[])
4549
} while (addr < memsize && i < MEMORY_CHUNKS);
4650
}
4751

48-
void detect_memory_layout(struct mem_chunk chunk[])
52+
/**
53+
* detect_memory_layout - fill mem_chunk array with memory layout data
54+
* @chunk: mem_chunk array to be filled
55+
* @maxsize: maximum address where memory detection should stop
56+
*
57+
* Fills the passed in memory chunk array with the memory layout of the
58+
* machine. The array must have a size of at least MEMORY_CHUNKS and will
59+
* be fully initialized afterwards.
60+
* If the maxsize paramater has a value > 0 memory detection will stop at
61+
* that address. It is guaranteed that all chunks have an ending address
62+
* that is smaller than maxsize.
63+
* If maxsize is 0 all memory will be detected.
64+
*/
65+
void detect_memory_layout(struct mem_chunk chunk[], unsigned long maxsize)
4966
{
5067
unsigned long flags, flags_dat, cr0;
5168

@@ -69,7 +86,7 @@ void detect_memory_layout(struct mem_chunk chunk[])
6986
}
7087
__ctl_store(cr0, 0, 0);
7188
__ctl_clear_bit(0, 28);
72-
find_memory_chunks(chunk);
89+
find_memory_chunks(chunk, maxsize);
7390
__ctl_load(cr0, 0, 0);
7491
out:
7592
__arch_local_irq_ssm(flags_dat);

‎drivers/s390/char/zcore.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ static int zcore_memmap_open(struct inode *inode, struct file *filp)
426426
GFP_KERNEL);
427427
if (!chunk_array)
428428
return -ENOMEM;
429-
detect_memory_layout(chunk_array);
429+
detect_memory_layout(chunk_array, 0);
430430
buf = kzalloc(MEMORY_CHUNKS * CHUNK_INFO_SIZE, GFP_KERNEL);
431431
if (!buf) {
432432
kfree(chunk_array);
@@ -610,7 +610,7 @@ static int __init get_mem_info(unsigned long *mem, unsigned long *end)
610610
GFP_KERNEL);
611611
if (!chunk_array)
612612
return -ENOMEM;
613-
detect_memory_layout(chunk_array);
613+
detect_memory_layout(chunk_array, 0);
614614
for (i = 0; i < MEMORY_CHUNKS; i++) {
615615
if (chunk_array[i].size == 0)
616616
break;

0 commit comments

Comments
 (0)