Skip to contentSkip to conversation

overcommit_memory: Fix integer overflow and 32-bit limits

PATCH Superseded Ben Copeland 3 participants 5 messages Oct 15, 2025 – Oct 16, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 overcommit_memory: Fix integer overflow and 32-bit limits

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.

---
 .../kernel/mem/tunable/overcommit_memory.c    | 33 +++++++++++++++----
 1 file changed, 27 insertions(+), 6 deletions(-)
testcases/kernel/mem/tunable/overcommit_memory.c +27 -6
index 9b2cb479d..7ff5a98d0 100644
@@ -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");
+	}
 
 }
 
Signature · 5 lines
2.51.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw
Quoted 6 lines

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

You still mention GB ...

Quoted 18 lines

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.

---
 .../kernel/mem/tunable/overcommit_memory.c    | 33 +++++++++++++++----
 1 file changed, 27 insertions(+), 6 deletions(-)
testcases/kernel/mem/tunable/overcommit_memory.c +0 -0
index 9b2cb479d..7ff5a98d0 100644

testcases/kernel/mem/tunable/overcommit_memory.c static void overcommit_memory_test(void) · line 135

Quoted 7 lines
testcases/kernel/mem/tunable/overcommit_memory.c +2 -2
@@ -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) {

... but TST_GB / TST_KB is actually MB (you could use TST_MB).

Yes, this fixes the problem on i686 as the tests are skipped as sum_total is smaller than 1MB.

Kind regards,
Petr

tst_kernel.c:90: TINFO: uname.machine=i686 kernel is 32bit
tst_test.c:2021: TINFO: LTP version: 20250930
tst_test.c:2024: TINFO: Tested kernel: 6.17.1-1-default #1 SMP PREEMPT_DYNAMIC Tue Oct 7 04:26:30 UTC 2025 (1bf5c2e) i686 tst_kconfig.c:88: TINFO: Parsing kernel config '/proc/config.gz'
tst_kconfig.c:676: TINFO: CONFIG_FAULT_INJECTION kernel option detected which might slow the execution tst_test.c:1842: TINFO: Overall timeout per run is 0h 02m 00s
overcommit_memory.c:104: TINFO: MemTotal is 2050308 kB
overcommit_memory.c:106: TINFO: SwapTotal is 2098152 kB
overcommit_memory.c:110: TINFO: CommitLimit is 3121256 kB
overcommit_memory.c:123: TINFO: Setting /proc/sys/vm/overcommit_ratio to 30
overcommit_memory.c:126: TINFO: TotalBatchSize is 8008 kB
overcommit_memory.c:132: TINFO: Setting /proc/sys/vm/overcommit_memory to 2
overcommit_memory.c:140: TCONF: Skipping large allocation tests due to address space limits overcommit_memory.c:146: TCONF: Skipping commit_left/2 allocation test due to address space limits overcommit_memory.c:150: TINFO: Setting /proc/sys/vm/overcommit_memory to 0
overcommit_memory.c:152: TINFO: ONE_GB: 1073741824
overcommit_memory.c:153: TINFO: TST_GB: 1073741824
overcommit_memory.c:154: TINFO: TST_MB: 1048576
overcommit_memory.c:155: TINFO: TST_GB / TST_KB: 1048576
overcommit_memory.c:156: TINFO: TST_MB * 1024: 1073741824
overcommit_memory.c:157: TINFO: tst_kernel_bits(): 32
overcommit_memory.c:158: TINFO: (unsigned long)sum_total <= ONE_GB: 1
overcommit_memory.c:159: TINFO: (unsigned long)sum_total <= TST_GB: 1
overcommit_memory.c:160: TINFO: condition: 1
overcommit_memory.c:193: TINFO: malloc 1669706 kB successfully
overcommit_memory.c:212: TPASS: alloc passed as expected
overcommit_memory.c:171: TCONF: Skipping large allocation test due to address space limits overcommit_memory.c:175: TINFO: Setting /proc/sys/vm/overcommit_memory to 1
overcommit_memory.c:193: TINFO: malloc 2074230 kB successfully
overcommit_memory.c:212: TPASS: alloc passed as expected
overcommit_memory.c:182: TCONF: Skipping large allocation tests due to address space limits

Quoted 43 lines
testcases/kernel/mem/tunable/overcommit_memory.c +25 -4
+		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");
+	}
 
 }
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
Quoted 9 lines · Petr Vorel
Quoted 6 lines

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

You still mention GB ...

Yep. It is GB.

Quoted 20 lines
Quoted 19 lines

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.

---
 .../kernel/mem/tunable/overcommit_memory.c    | 33 +++++++++++++++----
 1 file changed, 27 insertions(+), 6 deletions(-)
testcases/kernel/mem/tunable/overcommit_memory.c +0 -0
index 9b2cb479d..7ff5a98d0 100644
@@ -131,24 +131,45 @@ static void overcommit_memory_test(void)
 	TST_SYS_CONF_LONG_SET(OVERCOMMIT_MEMORY, 2, 1);
Quoted 8 lines

testcases/kernel/mem/tunable/overcommit_memory.c static void overcommit_memory_test(void) · line 135

Quoted 5 lines
testcases/kernel/mem/tunable/overcommit_memory.c +2 -2
 	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) {

... but TST_GB / TST_KB is actually MB (you could use TST_MB).

The sizes in this test are measured in KB, so it's 1GB but measured in terms of KB not bytes. Using TST_MB would work mathematically but it's misleading.

regards,
dan carpenter

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 42 lines · Petr Vorel
Quoted 8 lines
Quoted 6 lines

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

You still mention GB ...

Yep. It is GB.

Quoted 20 lines
Quoted 19 lines

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.

---
 .../kernel/mem/tunable/overcommit_memory.c    | 33 +++++++++++++++----
 1 file changed, 27 insertions(+), 6 deletions(-)
testcases/kernel/mem/tunable/overcommit_memory.c +0 -0
index 9b2cb479d..7ff5a98d0 100644
@@ -131,24 +131,45 @@ static void overcommit_memory_test(void)
 	TST_SYS_CONF_LONG_SET(OVERCOMMIT_MEMORY, 2, 1);
Quoted 7 lines

testcases/kernel/mem/tunable/overcommit_memory.c static void overcommit_memory_test(void) · line 135

Quoted 5 lines
testcases/kernel/mem/tunable/overcommit_memory.c +2 -2
 	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) {

... but TST_GB / TST_KB is actually MB (you could use TST_MB).

Quoted 3 lines

The sizes in this test are measured in KB, so it's 1GB but measured in terms of KB not bytes. Using TST_MB would work mathematically but it's misleading.

Ah, I'm sorry to overlook an obvious point.

Implementation details: thinking about the code twice, shouldn't be the check for overflow in alloc_and_check() instead of outside (to keep the condition on single place)?

Also, if kept outside the 1st and 2nd if could be joined:

	/* 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");
	}

instead of your proposal:

	update_mem_commit();
	/* 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();
	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");
	}

because update_mem_commit() IMHO just evaluates /proc/meminfo values, but when alloc_and_check() is skipped nothing has changed.

Kind regards,
Petr

Quoted 2 lines

regards,
dan carpenter

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Hello Petr,

Quoted 47 lines · Petr Vorel
Quoted 40 lines · Petr Vorel
Quoted 8 lines
Quoted 6 lines

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

You still mention GB ...

Yep. It is GB.

Quoted 25 lines
Quoted 25 lines

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.

---
 .../kernel/mem/tunable/overcommit_memory.c    | 33 +++++++++++++++----
 1 file changed, 27 insertions(+), 6 deletions(-)
testcases/kernel/mem/tunable/overcommit_memory.c +2 -2
index 9b2cb479d..7ff5a98d0 100644
@@ -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) {
Quoted 6 lines
Quoted 1 line

... but TST_GB / TST_KB is actually MB (you could use TST_MB).

The sizes in this test are measured in KB, so it's 1GB but measured in terms of KB not bytes. Using TST_MB would work mathematically but it's misleading.

Quoted 5 lines

Ah, I'm sorry to overlook an obvious point.

Implementation details: thinking about the code twice, shouldn't be the check for overflow in alloc_and_check() instead of outside (to keep the condition on single place)?

Excellent suggestion. It makes sense to simplify it like that. I will spin up a v3 patch with those changes.

Ben

Quoted 39 lines

Also, if kept outside the 1st and 2nd if could be joined:

        /* 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");
        }

instead of your proposal:

        update_mem_commit();
        /* 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();
        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");
        }

because update_mem_commit() IMHO just evaluates /proc/meminfo values, but when alloc_and_check() is skipped nothing has changed.

Kind regards,
Petr

Quoted 2 lines

regards,
dan carpenter

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp