The overcommit test uses sum_total, the sum (memory and swap) to test
the overcommit settings.
This fixes two problems on 32-bit systems. The first is seen with a
integer overflow can occur when calculating sum_total * 2, if the
sum_total is larger than 2GB. The second is limited virtual address
space (2-3GB) means the test can fail from address space exhaustion
before overcommit has been tested.
Now the test runs correctly on low-memory 32-bit systems while avoiding
both the overflow bug and virtual address space issues.
Acked-by: Arnd Bergmann <arnd(a)arndb.de>
Reviewed-by: Petr Vorel <pvorel(a)suse.cz>
Signed-off-by: Ben Copeland <ben.copeland(a)linaro.org>
---
.../kernel/mem/tunable/overcommit_memory.c | 31 ++++++++++++++-----
1 file changed, 24 insertions(+), 7 deletions(-)
diff --git a/testcases/kernel/mem/tunable/overcommit_memory.c b/testcases/kernel/mem/tunable/overcommit_memory.c
index 9b2cb479d..d4faaeee0 100644
--- a/testcases/kernel/mem/tunable/overcommit_memory.c
+++ b/testcases/kernel/mem/tunable/overcommit_memory.c
@@ -131,24 +131,41 @@ static void overcommit_memory_test(void)
TST_SYS_CONF_LONG_SET(OVERCOMMIT_MEMORY, 2, 1);
update_mem_commit();
- alloc_and_check(commit_left * 2, EXPECT_FAIL);
- alloc_and_check(commit_limit + total_batch_size, EXPECT_FAIL);
- update_mem_commit();
- alloc_and_check(commit_left / 2, EXPECT_PASS);
+ /* Skip tests that would overflow or exceed 32-bit address space */
+ if (tst_kernel_bits() == 64 || (unsigned long)commit_left <= TST_GB / TST_KB) {
+ alloc_and_check(commit_left * 2, EXPECT_FAIL);
+ alloc_and_check(commit_limit + total_batch_size, EXPECT_FAIL);
+ update_mem_commit();
+ alloc_and_check(commit_left / 2, EXPECT_PASS);
+ } else {
+ tst_res(TCONF, "Skipping large allocation tests due to address space limits");
+ }
/* start to test overcommit_memory=0 */
TST_SYS_CONF_LONG_SET(OVERCOMMIT_MEMORY, 0, 1);
update_mem();
alloc_and_check(free_total / 2, EXPECT_PASS);
- alloc_and_check(sum_total * 2, EXPECT_FAIL);
+ /* Skip if sum_total * 2 would exceed address space.
+ * On 32-bit, skip when sum_total > ULONG_MAX/4 (~1GB).
+ * Most 32-bit systems with <=1GB RAM can map 2x that in 3GB vaddr space.
+ * Systems with 2GB+ RAM likely cannot fit allocations in vaddr space. */
+ if (tst_kernel_bits() == 64 || (unsigned long)sum_total <= TST_GB / TST_KB) {
+ alloc_and_check(sum_total * 2, EXPECT_FAIL);
+ } else {
+ tst_res(TCONF, "Skipping large allocation test due to address space limits");
+ }
/* start to test overcommit_memory=1 */
TST_SYS_CONF_LONG_SET(OVERCOMMIT_MEMORY, 1, 1);
alloc_and_check(sum_total / 2, EXPECT_PASS);
- alloc_and_check(sum_total, EXPECT_PASS);
- alloc_and_check(sum_total * 2, EXPECT_PASS);
+ if (tst_kernel_bits() == 64 || (unsigned long)sum_total <= TST_GB / TST_KB) {
+ alloc_and_check(sum_total, EXPECT_PASS);
+ alloc_and_check(sum_total * 2, EXPECT_PASS);
+ } else {
+ tst_res(TCONF, "Skipping large allocation tests due to address space limits");
+ }
}
--
2.51.0
The alloc_mem() function is supposed to test out of memory conditions.
How it works is it calls mmap() on a giant chunk of memory.
It's either LENGTH (2GB) or "TESTMEM * 2 + TST_MB" (3GB) bytes.
This mmap() is generally supposed to succeed. Then at the bottom of
the alloc_mem() function when we actually try to use all the memory,
the thread is supposed to die with a SIGKILL.
The problem is that length is signed so on a 32-bit system it will be
negative. That means that at the bottom of the function when we loop
through the memory, the for loop is a no-op and there is no SIGKILL.
Fix this by changing the type to size_t which is unsigned.
Signed-off-by: Ben Copeland <ben.copeland(a)linaro.org>
---
testcases/kernel/mem/oom/oom.h | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/testcases/kernel/mem/oom/oom.h b/testcases/kernel/mem/oom/oom.h
index 41cc681f9..42ed181b0 100644
--- a/testcases/kernel/mem/oom/oom.h
+++ b/testcases/kernel/mem/oom/oom.h
@@ -62,13 +62,14 @@ static inline void set_global_mempolicy(int mempolicy)
static void set_global_mempolicy(int mempolicy LTP_ATTRIBUTE_UNUSED) { }
#endif
-static int alloc_mem(long int length, int testcase)
+static int alloc_mem(size_t length, int testcase)
{
char *s;
- long i, pagesz = getpagesize();
+ size_t i;
+ long pagesz = getpagesize();
int loop = 10;
- tst_res(TINFO, "thread (%lx), allocating %ld bytes.",
+ tst_res(TINFO, "thread (%lx), allocating %zu bytes.",
(unsigned long) pthread_self(), length);
s = mmap(NULL, length, PROT_READ | PROT_WRITE,
@@ -111,7 +112,7 @@ static void child_alloc(int testcase, int lite, int threads)
pthread_t *th;
if (lite) {
- int ret = alloc_mem(TESTMEM * 2 + TST_MB, testcase);
+ int ret = alloc_mem((size_t)TESTMEM * 2 + TST_MB, testcase);
exit(ret);
}
--
2.51.0
The overcommit test uses sum_total, the sum (memory and swap) to test
the overcommit settings.
This fixes two problems on 32-bit systems. The first is seen with a
integer overflow can occur when calculating sum_total * 2, if the
sum_total is larger than 2GB. The second is limited virtual address
space (2-3GB) means the test can fail from address space exhaustion
before overcommit has been tested.
Now the test runs correctly on low-memory 32-bit systems while avoiding
both the overflow bug and virtual address space issues.
Signed-off-by: Ben Copeland <ben.copeland(a)linaro.org>
Acked-by: Arnd Bergmann <arnd(a)arndb.de>
Reviewed-by: Petr Vorel <pvorel(a)suse.cz>
---
.../kernel/mem/tunable/overcommit_memory.c | 33 +++++++++++++++----
1 file changed, 27 insertions(+), 6 deletions(-)
diff --git a/testcases/kernel/mem/tunable/overcommit_memory.c b/testcases/kernel/mem/tunable/overcommit_memory.c
index 9b2cb479d..7ff5a98d0 100644
--- a/testcases/kernel/mem/tunable/overcommit_memory.c
+++ b/testcases/kernel/mem/tunable/overcommit_memory.c
@@ -131,24 +131,45 @@ static void overcommit_memory_test(void)
TST_SYS_CONF_LONG_SET(OVERCOMMIT_MEMORY, 2, 1);
update_mem_commit();
- alloc_and_check(commit_left * 2, EXPECT_FAIL);
- alloc_and_check(commit_limit + total_batch_size, EXPECT_FAIL);
+ /* Skip tests that would overflow or exceed 32-bit address space */
+ if (tst_kernel_bits() == 64 || (unsigned long)commit_left <= TST_GB / TST_KB) {
+ alloc_and_check(commit_left * 2, EXPECT_FAIL);
+ alloc_and_check(commit_limit + total_batch_size, EXPECT_FAIL);
+ } else {
+ tst_res(TCONF, "Skipping large allocation tests due to address space limits");
+ }
update_mem_commit();
- alloc_and_check(commit_left / 2, EXPECT_PASS);
+ if (tst_kernel_bits() == 64 || (unsigned long)commit_left <= TST_GB / TST_KB) {
+ alloc_and_check(commit_left / 2, EXPECT_PASS);
+ } else {
+ tst_res(TCONF, "Skipping commit_left/2 allocation test due to address space limits");
+ }
/* start to test overcommit_memory=0 */
TST_SYS_CONF_LONG_SET(OVERCOMMIT_MEMORY, 0, 1);
update_mem();
alloc_and_check(free_total / 2, EXPECT_PASS);
- alloc_and_check(sum_total * 2, EXPECT_FAIL);
+ /* Skip if sum_total * 2 would exceed address space.
+ * On 32-bit, skip when sum_total > ULONG_MAX/4 (~1GB).
+ * Most 32-bit systems with <=1GB RAM can map 2x that in 3GB vaddr space.
+ * Systems with 2GB+ RAM likely cannot fit allocations in vaddr space. */
+ if (tst_kernel_bits() == 64 || (unsigned long)sum_total <= TST_GB / TST_KB) {
+ alloc_and_check(sum_total * 2, EXPECT_FAIL);
+ } else {
+ tst_res(TCONF, "Skipping large allocation test due to address space limits");
+ }
/* start to test overcommit_memory=1 */
TST_SYS_CONF_LONG_SET(OVERCOMMIT_MEMORY, 1, 1);
alloc_and_check(sum_total / 2, EXPECT_PASS);
- alloc_and_check(sum_total, EXPECT_PASS);
- alloc_and_check(sum_total * 2, EXPECT_PASS);
+ if (tst_kernel_bits() == 64 || (unsigned long)sum_total <= TST_GB / TST_KB) {
+ alloc_and_check(sum_total, EXPECT_PASS);
+ alloc_and_check(sum_total * 2, EXPECT_PASS);
+ } else {
+ tst_res(TCONF, "Skipping large allocation tests due to address space limits");
+ }
}
--
2.51.0