Skip to contentSkip to conversation

Question on hugemmap34

LTP In review Wei Gao via ltp 3 participants 11 messages Dec 10, 2024 – Dec 13, 2024

lore
Share & tools
lore
#0 lore raw

Hi ALL

Is there any special config needed for this test case? Since the test failed with the following output on my test setup(opensuse15.5 with 6.12 kernel):

tst_hugepage.c:84: TINFO: 1 hugepage(s) reserved
tst_tmpdir.c:317: TINFO: Using /tmp/LTP_hugLSJb7r as tmpdir (btrfs filesystem)
tst_test.c:1100: TINFO: Mounting none to /tmp/LTP_hugLSJb7r/hugetlbfs fstyp=hugetlbfs flags=0 tst_test.c:1890: TINFO: LTP version: 20240930
tst_test.c:1894: TINFO: Tested kernel: 6.12.3-lp155.11.g78b0030-vanilla #1 SMP Fri Dec  6 08:56:39 UTC 2024 (78b0030) ppc64le tst_test.c:1727: TINFO: Timeout per run is 0h 00m 30s
tst_coredump.c:32: TINFO: Avoid dumping corefile for process(pid=6671)
hugemmap34.c:88: TBROK: waitpid(0,0x7fffd8baa220,0) failed: ECHILD (10)
hugemmap34.c:92: TFAIL: Child: exited with 2

The root cause is the mmap call encounter failure with EBUSY.

LTP mmap call
https://github.com/linux-test-project/ltp/blob/7bb960cc4f736d8860b6b266119e71e761e22b32/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c#L71

hit kernel code
https://elixir.bootlin.com/linux/v6.12/source/arch/powerpc/mm/book3s64/slice.c#L568

Let me give an example to explain why this happens base pmap of process in my test system:

Address           Kbytes     RSS     PSS   Dirty    Swap Mode  Mapping
0000000010000000     256     256     128     256       0 r-xp- /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34 0000000010040000      64      64      32      64       0 r--p- /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34 0000000010050000      64      64      64      64       0 rw-p- /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34 0000000010060000      64      64      64      64       0 rw-p-   [ anon ]
0000010010090000     192      64      64      64       0 rw-p-   [ anon ]
00007fff8f3b0000    2368    1408       0       0       0 r-xp- /lib64/libc.so.6
00007fff8f600000      64      64      32      64       0 r--p- /lib64/libc.so.6
00007fff8f610000      64      64      64      64       0 rw-p- /lib64/libc.so.6
00007fff8f620000      64      64      32      64       0 rw-s- /dev/shm/ltp_hugemmap34_15513 (deleted) 00007fff8f630000     128       0       0       0       0 r--p-   [ anon ]
00007fff8f650000      64       0       0       0       0 r-xp-   [ anon ]
00007fff8f660000     320     128       0       0       0 r-xp- /lib64/ld64.so.2
00007fff8f6b0000      64      64      32      64       0 r--p- /lib64/ld64.so.2
00007fff8f6c0000      64      64      64      64       0 rw-p- /lib64/ld64.so.2
00007fffc6740000     192      64      64      64       0 rw-p-   [ stack ] <<<<
---------------- ------- ------- ------- ------- -------
total kB           20416    2432    1318     896       0

Test power system config:
#getconf PAGE_SIZE
65536
#grep Hugepagesize /proc/meminfo
Hugepagesize:      16384 kB

Kenel split VM space into:
16 low_slice(64KB page size), each slice size is 256MB 4096 high_slice(64K page size), each slice size is 1TB

00007fffc6740000(stack) is belong 127th high_slice(range is 00007f0000000000 - 00007fffffffffff)

When mmap try to allocate a 16M space(with page size MMU_PAGE_16M) near stack address(00007fffc6740000), kernel will first check good_mask but failed, since the all slices is MMU_PAGE_64K
but mmap request page size is MMU_PAGE_16M.
https://elixir.bootlin.com/linux/v6.12/source/arch/powerpc/mm/book3s64/slice.c#L531

Next kernel start check potential_mask(seach all slice which has no any VM mapped), obviously the 127th slice is occupied and 126th is the good candidate one but address range not match the mmap's request address(0x00007fffc6740000 - 2 * hpage_size), so finally EBUSY returned.
https://elixir.bootlin.com/linux/v6.12/source/arch/powerpc/mm/book3s64/slice.c#L559

BTW: I have tested a scenario which disables MAP_FIXED_NOREPLACE, the kernel will allocate successfully within the range of 126th high slice.

Thanks.
Regards
Wei Gao

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw
Quoted 5 lines · Wei Gao

Hi ALL

Is there any special config needed for this test case? Since the test failed with the following output on my test setup(opensuse15.5 with 6.12 kernel):

Quoted 8 lines

tst_hugepage.c:84: TINFO: 1 hugepage(s) reserved
tst_tmpdir.c:317: TINFO: Using /tmp/LTP_hugLSJb7r as tmpdir (btrfs filesystem)
tst_test.c:1100: TINFO: Mounting none to /tmp/LTP_hugLSJb7r/hugetlbfs fstyp=hugetlbfs flags=0 tst_test.c:1890: TINFO: LTP version: 20240930
tst_test.c:1894: TINFO: Tested kernel: 6.12.3-lp155.11.g78b0030-vanilla #1 SMP Fri Dec 6 08:56:39 UTC 2024 (78b0030) ppc64le tst_test.c:1727: TINFO: Timeout per run is 0h 00m 30s
tst_coredump.c:32: TINFO: Avoid dumping corefile for process(pid=6671)
hugemmap34.c:88: TBROK: waitpid(0,0x7fffd8baa220,0) failed: ECHILD (10)

There's missing exit() in do_child(), but that's not the only issue with the test.

Quoted 60 lines

hugemmap34.c:92: TFAIL: Child: exited with 2

The root cause is the mmap call encounter failure with EBUSY.

LTP mmap call
https://github.com/linux-test-project/ltp/blob/7bb960cc4f736d8860b6b266119e71e761e22b32/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c#L71

hit kernel code
https://elixir.bootlin.com/linux/v6.12/source/arch/powerpc/mm/book3s64/slice.c#L568

Let me give an example to explain why this happens base pmap of process in my test system:

Address           Kbytes     RSS     PSS   Dirty    Swap Mode  Mapping
0000000010000000     256     256     128     256       0 r-xp- /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34
0000000010040000      64      64      32      64       0 r--p- /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34
0000000010050000      64      64      64      64       0 rw-p- /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34
0000000010060000      64      64      64      64       0 rw-p-   [ anon ]
0000010010090000     192      64      64      64       0 rw-p-   [ anon ]
00007fff8f3b0000    2368    1408       0       0       0 r-xp- /lib64/libc.so.6
00007fff8f600000      64      64      32      64       0 r--p- /lib64/libc.so.6
00007fff8f610000      64      64      64      64       0 rw-p- /lib64/libc.so.6
00007fff8f620000      64      64      32      64       0 rw-s- /dev/shm/ltp_hugemmap34_15513 (deleted)
00007fff8f630000     128       0       0       0       0 r--p-   [ anon ]
00007fff8f650000      64       0       0       0       0 r-xp-   [ anon ]
00007fff8f660000     320     128       0       0       0 r-xp- /lib64/ld64.so.2
00007fff8f6b0000      64      64      32      64       0 r--p- /lib64/ld64.so.2
00007fff8f6c0000      64      64      64      64       0 rw-p- /lib64/ld64.so.2
00007fffc6740000     192      64      64      64       0 rw-p-   [ stack ] <<<<

---------------- ------- ------- ------- ------- -------
total kB 20416 2432 1318 896 0

Test power system config:
#getconf PAGE_SIZE
65536
#grep Hugepagesize /proc/meminfo
Hugepagesize: 16384 kB

Kenel split VM space into:
16 low_slice(64KB page size), each slice size is 256MB 4096 high_slice(64K page size), each slice size is 1TB

00007fffc6740000(stack) is belong 127th high_slice(range is 00007f0000000000 - 00007fffffffffff)

When mmap try to allocate a 16M space(with page size MMU_PAGE_16M) near stack address(00007fffc6740000), kernel will first check good_mask but failed, since the all slices is MMU_PAGE_64K
but mmap request page size is MMU_PAGE_16M.
https://elixir.bootlin.com/linux/v6.12/source/arch/powerpc/mm/book3s64/slice.c#L531

Next kernel start check potential_mask(seach all slice which has no any VM mapped), obviously the 127th slice is occupied and 126th is the good candidate one but address range not match the mmap's request address(0x00007fffc6740000 - 2 * hpage_size), so finally EBUSY returned.
https://elixir.bootlin.com/linux/v6.12/source/arch/powerpc/mm/book3s64/slice.c#L559

BTW: I have tested a scenario which disables MAP_FIXED_NOREPLACE, the kernel will allocate successfully within the range of 126th high slice.

Thanks.
Regards
Wei Gao

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
Quoted 16 lines · Jan Stancek
Quoted 5 lines · Wei Gao

Hi ALL

Is there any special config needed for this test case? Since the test failed with the following output on my test setup(opensuse15.5 with 6.12 kernel):

Quoted 8 lines

tst_hugepage.c:84: TINFO: 1 hugepage(s) reserved
tst_tmpdir.c:317: TINFO: Using /tmp/LTP_hugLSJb7r as tmpdir (btrfs filesystem)
tst_test.c:1100: TINFO: Mounting none to /tmp/LTP_hugLSJb7r/hugetlbfs fstyp=hugetlbfs flags=0 tst_test.c:1890: TINFO: LTP version: 20240930
tst_test.c:1894: TINFO: Tested kernel: 6.12.3-lp155.11.g78b0030-vanilla #1 SMP Fri Dec 6 08:56:39 UTC 2024 (78b0030) ppc64le tst_test.c:1727: TINFO: Timeout per run is 0h 00m 30s
tst_coredump.c:32: TINFO: Avoid dumping corefile for process(pid=6671)
hugemmap34.c:88: TBROK: waitpid(0,0x7fffd8baa220,0) failed: ECHILD (10)

Quoted 2 lines

There's missing exit() in do_child(), but that's not the only issue with the test.

Could you give this patch a try?

(quoted diff) +0 -0

b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c index a7a88fbb2..818697be8 100644

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +10 -0
@@ -51,16 +51,23 @@ void do_child(void *stop_address)
                x = alloca(STACK_ALLOCATION_SIZE);
                *x = 1;
        } while ((void *)x >= stop_address);
+       exit(0);
 }
 
 static void run_test(void)
 {
        int pid, status;
        void *stack_address, *mmap_address, *heap_address, *map;
+       char tmp[128];
 
        stack_address = alloca(0);
        heap_address = sbrk(0);
 
+       tst_res(TINFO, "heap is at: %p", heap_address);
+       tst_res(TINFO, "stack is at: %p", stack_address);
+       sprintf(tmp, "cat /proc/%d/maps", getpid());
+       TST_EXP_PASS_SILENT(system(tmp));
+
        /*
         * paranoia: start mapping two hugepages below the start of the stack,
         * in case the alignment would cause us to map over something if we
@@ -70,6 +77,9 @@ static void run_test(void)
        do {
                map = mmap(mmap_address, hpage_size, PROT_READ|PROT_WRITE,
                                MAP_SHARED | MAP_FIXED_NOREPLACE, fd, 0);
+               if (map == mmap_address)
+                       break;
+
                if (map == MAP_FAILED) {
                        if (errno == ENOMEM) {
                                tst_res(TCONF, "There is no enough

memory in the system to do mmap");
@@ -80,7 +90,12 @@ static void run_test(void)

                /*
                 * if we get all the way down to the heap, stop trying
                 */

- } while (mmap_address <= heap_address);
+ } while (mmap_address > heap_address);
+
+ if (map == MAP_FAILED)
+ tst_brk(TBROK, "failed to map hpage_size area before
hitting heap");
+ tst_res(TINFO, "mapped at %p", mmap);
+

        pid = SAFE_FORK();
        if (pid == 0)
                do_child(mmap_address);
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw
Quoted 76 lines · Jan Stancek
Quoted 16 lines · Jan Stancek
Quoted 5 lines · Wei Gao

Hi ALL

Is there any special config needed for this test case? Since the test failed with the following output on my test setup(opensuse15.5 with 6.12 kernel):

Quoted 8 lines

tst_hugepage.c:84: TINFO: 1 hugepage(s) reserved
tst_tmpdir.c:317: TINFO: Using /tmp/LTP_hugLSJb7r as tmpdir (btrfs filesystem)
tst_test.c:1100: TINFO: Mounting none to /tmp/LTP_hugLSJb7r/hugetlbfs fstyp=hugetlbfs flags=0 tst_test.c:1890: TINFO: LTP version: 20240930
tst_test.c:1894: TINFO: Tested kernel: 6.12.3-lp155.11.g78b0030-vanilla #1 SMP Fri Dec 6 08:56:39 UTC 2024 (78b0030) ppc64le tst_test.c:1727: TINFO: Timeout per run is 0h 00m 30s
tst_coredump.c:32: TINFO: Avoid dumping corefile for process(pid=6671)
hugemmap34.c:88: TBROK: waitpid(0,0x7fffd8baa220,0) failed: ECHILD (10)

Quoted 2 lines

There's missing exit() in do_child(), but that's not the only issue with the test.

Could you give this patch a try?

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +10 -0
index a7a88fbb2..818697be8 100644
@@ -51,16 +51,23 @@ void do_child(void *stop_address)
                x = alloca(STACK_ALLOCATION_SIZE);
                *x = 1;
        } while ((void *)x >= stop_address);
+       exit(0);
 }
 
 static void run_test(void)
 {
        int pid, status;
        void *stack_address, *mmap_address, *heap_address, *map;
+       char tmp[128];
 
        stack_address = alloca(0);
        heap_address = sbrk(0);
 
+       tst_res(TINFO, "heap is at: %p", heap_address);
+       tst_res(TINFO, "stack is at: %p", stack_address);
+       sprintf(tmp, "cat /proc/%d/maps", getpid());
+       TST_EXP_PASS_SILENT(system(tmp));
+
        /*
         * paranoia: start mapping two hugepages below the start of the stack,
         * in case the alignment would cause us to map over something if we
@@ -70,6 +77,9 @@ static void run_test(void)
        do {
                map = mmap(mmap_address, hpage_size, PROT_READ|PROT_WRITE,
                                MAP_SHARED | MAP_FIXED_NOREPLACE, fd, 0);
+               if (map == mmap_address)
+                       break;
+
                if (map == MAP_FAILED) {
                        if (errno == ENOMEM) {
                                tst_res(TCONF, "There is no enough

memory in the system to do mmap");

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +6 -1
@@ -80,7 +90,12 @@ static void run_test(void)
                /*
                 * if we get all the way down to the heap, stop trying
                 */
-       } while (mmap_address <= heap_address);
+       } while (mmap_address > heap_address);
+
+       if (map == MAP_FAILED)
+               tst_brk(TBROK, "failed to map hpage_size area before
 hitting heap");
+       tst_res(TINFO, "mapped at %p", mmap);
+
        pid = SAFE_FORK();
        if (pid == 0)
Quoted 2 lines
                do_child(mmap_address);

Test will timeout if use default timeout so i do quick hack change for setup:

        .forks_child = 1,

+ .max_runtime = 3600,

Test result:

make -C "/root/ltp/lib" -f "/root/ltp/lib/Makefile" all
make[1]: Entering directory '/root/ltp/lib'
GEN ltp-version.h
make[2]: Nothing to be done for 'all'.
make[2]: Nothing to be done for 'all'.
make[1]: Leaving directory '/root/ltp/lib'
hugemmap34.c:150:1: warning: missing initializer for field ‘needs_cmds’ of ‘struct tst_test’ [-Wmissing-field-initializers]
};
^
In file included from /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/../lib/hugetlb.h:19:0,

                 from hugemmap34.c:24:
../../../../../include/tst_test.h:593:21: note: ‘needs_cmds’ declared here
  const char *const *needs_cmds;
                     ^~~~~~~~~~

CC testcases/kernel/mem/hugetlb/hugemmap/hugemmap34
tst_hugepage.c:84: TINFO: 1 hugepage(s) reserved
tst_tmpdir.c:317: TINFO: Using /tmp/LTP_hugkoiPEG as tmpdir (btrfs filesystem)
tst_test.c:1100: TINFO: Mounting none to /tmp/LTP_hugkoiPEG/hugetlbfs fstyp=hugetlbfs flags=0 tst_test.c:1890: TINFO: LTP version: 20240930
tst_test.c:1894: TINFO: Tested kernel: 6.12.3-lp155.11.g78b0030-vanilla #1 SMP Fri Dec 6 08:56:39 UTC 2024 (78b0030) ppc64le tst_test.c:1727: TINFO: Timeout per run is 1h 00m 30s
hugemmap34.c:66: TINFO: heap is at: 0x1002af30000
hugemmap34.c:67: TINFO: stack is at: 0x7ffff97cab30

10000000-10040000 r-xp 00000000 00:29 11656                              /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34
10040000-10050000 r--p 00030000 00:29 11656                              /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34
10050000-10060000 rw-p 00040000 00:29 11656                              /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34
10060000-10070000 rw-p 00000000 00:00 0
1002af00000-1002af30000 rw-p 00000000 00:00 0                            [heap]
7fff94b20000-7fff94d20000 r-xp 00000000 00:29 11464                      /lib64/libc-2.31.so
7fff94d20000-7fff94d30000 r--p 001f0000 00:29 11464                      /lib64/libc-2.31.so
7fff94d30000-7fff94d40000 rw-p 00200000 00:29 11464                      /lib64/libc-2.31.so
7fff94d40000-7fff94d70000 r-xp 00000000 00:29 11482                      /lib64/libpthread-2.31.so
7fff94d70000-7fff94d80000 r--p 00020000 00:29 11482                      /lib64/libpthread-2.31.so
7fff94d80000-7fff94d90000 rw-p 00030000 00:29 11482                      /lib64/libpthread-2.31.so
7fff94da0000-7fff94db0000 rw-s 00000000 00:17 9                          /dev/shm/ltp_hugemmap34_7730 (deleted)
7fff94db0000-7fff94dd0000 r--p 00000000 00:00 0                          [vvar]
7fff94dd0000-7fff94de0000 r-xp 00000000 00:00 0                          [vdso]
7fff94de0000-7fff94e20000 r-xp 00000000 00:29 11457                      /lib64/ld-2.31.so
7fff94e20000-7fff94e30000 r--p 00030000 00:29 11457                      /lib64/ld-2.31.so
7fff94e30000-7fff94e40000 rw-p 00040000 00:29 11457                      /lib64/ld-2.31.so
7ffff97a0000-7ffff97d0000 rw-p 00000000 00:00 0                          [stack]

hugemmap34.c:97: TINFO: mapped at 0x7fff94c51e80 tst_coredump.c:32: TINFO: Avoid dumping corefile for process(pid=7752) hugemmap34.c:105: TPASS: Child killed by SIGSEGV as expected

Summary:

passed   1
failed   0
broken   0
skipped  0

warnings 0

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

Hi Jan

Thanks for your quick support.
Since i use a kernel with my debug enabled so the test case timeout first time, when i use normal kernel run the test case with your patch, the test result show pass.
Then i guess you will send new separate patch for fix this later?

Regards
Gao Wei

Quoted 173 lines · Wei Gao
Quoted 7 lines · Jan Stancek
Quoted 6 lines · Jan Stancek
Quoted 4 lines · Wei Gao

Hi ALL

Is there any special config needed for this test case? Since the

test failed with the following output on my test setup(opensuse15.5 with 6.12 kernel):

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

tst_hugepage.c:84: TINFO: 1 hugepage(s) reserved tst_tmpdir.c:317: TINFO: Using /tmp/LTP_hugLSJb7r as tmpdir (btrfs

filesystem)

Quoted 1 line
Quoted 1 line
Quoted 1 line

tst_test.c:1100: TINFO: Mounting none to

/tmp/LTP_hugLSJb7r/hugetlbfs fstyp=hugetlbfs flags=0

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

tst_test.c:1890: TINFO: LTP version: 20240930
tst_test.c:1894: TINFO: Tested kernel:

6.12.3-lp155.11.g78b0030-vanilla #1 SMP Fri Dec 6 08:56:39 UTC 2024 (78b0030) ppc64le

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

tst_test.c:1727: TINFO: Timeout per run is 0h 00m 30s tst_coredump.c:32: TINFO: Avoid dumping corefile for

process(pid=6671)

Quoted 1 line
Quoted 1 line
Quoted 1 line

hugemmap34.c:88: TBROK: waitpid(0,0x7fffd8baa220,0) failed: ECHILD

(10)

Quoted 24 lines
Quoted 3 lines

There's missing exit() in do_child(), but that's not the only issue with the test.

Could you give this patch a try?

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +2 -0
index a7a88fbb2..818697be8 100644
@@ -51,16 +51,23 @@ void do_child(void *stop_address)
                x = alloca(STACK_ALLOCATION_SIZE);
                *x = 1;
        } while ((void *)x >= stop_address);
+       exit(0);
 }
 
 static void run_test(void)
 {
        int pid, status;
        void *stack_address, *mmap_address, *heap_address, *map;
+       char tmp[128];
 

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c void do_child(void *stop_address) · line 72

Quoted 10 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +5 -0
        stack_address = alloca(0);
        heap_address = sbrk(0);
 
+       tst_res(TINFO, "heap is at: %p", heap_address);
+       tst_res(TINFO, "stack is at: %p", stack_address);
+       sprintf(tmp, "cat /proc/%d/maps", getpid());
+       TST_EXP_PASS_SILENT(system(tmp));
+
        /*
         * paranoia: start mapping two hugepages below the start of the

stack,

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c void do_child(void *stop_address) · line 73

Quoted 1 line
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +0 -0
         * in case the alignment would cause us to map over something if

we

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c static void run_test(void) · line 78

Quoted 3 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +0 -0
@@ -70,6 +77,9 @@ static void run_test(void)
        do {
                map = mmap(mmap_address, hpage_size,

PROT_READ|PROT_WRITE,

Quoted 22 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +3 -0
                                MAP_SHARED | MAP_FIXED_NOREPLACE, fd, 0);
+               if (map == mmap_address)
+                       break;
+
                if (map == MAP_FAILED) {
                        if (errno == ENOMEM) {
                                tst_res(TCONF, "There is no enough

memory in the system to do mmap");

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +6 -1
@@ -80,7 +90,12 @@ static void run_test(void)
                /*
                 * if we get all the way down to the heap, stop trying
                 */
-       } while (mmap_address <= heap_address);
+       } while (mmap_address > heap_address);
+
+       if (map == MAP_FAILED)
+               tst_brk(TBROK, "failed to map hpage_size area before
 hitting heap");
+       tst_res(TINFO, "mapped at %p", mmap);
+
        pid = SAFE_FORK();
        if (pid == 0)
Quoted 2 lines
                do_child(mmap_address);

Test will timeout if use default timeout so i do quick hack change for setup:

        .forks_child = 1,

+ .max_runtime = 3600,

Test result:

make -C "/root/ltp/lib" -f "/root/ltp/lib/Makefile" all make[1]: Entering directory '/root/ltp/lib'
GEN ltp-version.h
make[2]: Nothing to be done for 'all'.
make[2]: Nothing to be done for 'all'.
make[1]: Leaving directory '/root/ltp/lib'
hugemmap34.c:150:1: warning: missing initializer for field ‘needs_cmds’ of ‘struct tst_test’ [-Wmissing-field-initializers]
};
^
In file included from /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/../lib/hugetlb.h:19:0,

                 from hugemmap34.c:24:
../../../../../include/tst_test.h:593:21: note: ‘needs_cmds’ declared here
  const char *const *needs_cmds;
                     ^~~~~~~~~~

CC testcases/kernel/mem/hugetlb/hugemmap/hugemmap34 tst_hugepage.c:84: TINFO: 1 hugepage(s) reserved tst_tmpdir.c:317: TINFO: Using /tmp/LTP_hugkoiPEG as tmpdir (btrfs filesystem)
tst_test.c:1100: TINFO: Mounting none to /tmp/LTP_hugkoiPEG/hugetlbfs fstyp=hugetlbfs flags=0
tst_test.c:1890: TINFO: LTP version: 20240930
tst_test.c:1894: TINFO: Tested kernel: 6.12.3-lp155.11.g78b0030-vanilla #1 SMP Fri Dec 6 08:56:39 UTC 2024 (78b0030) ppc64le tst_test.c:1727: TINFO: Timeout per run is 1h 00m 30s hugemmap34.c:66: TINFO: heap is at: 0x1002af30000 hugemmap34.c:67: TINFO: stack is at: 0x7ffff97cab30 10000000-10040000 r-xp 00000000 00:29 11656 /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34 10040000-10050000 r--p 00030000 00:29 11656 /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34 10050000-10060000 rw-p 00040000 00:29 11656 /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34 10060000-10070000 rw-p 00000000 00:00 0 1002af00000-1002af30000 rw-p 00000000 00:00 0
[heap]
7fff94b20000-7fff94d20000 r-xp 00000000 00:29 11464 /lib64/libc-2.31.so
7fff94d20000-7fff94d30000 r--p 001f0000 00:29 11464 /lib64/libc-2.31.so
7fff94d30000-7fff94d40000 rw-p 00200000 00:29 11464 /lib64/libc-2.31.so
7fff94d40000-7fff94d70000 r-xp 00000000 00:29 11482 /lib64/libpthread-2.31.so
7fff94d70000-7fff94d80000 r--p 00020000 00:29 11482 /lib64/libpthread-2.31.so
7fff94d80000-7fff94d90000 rw-p 00030000 00:29 11482 /lib64/libpthread-2.31.so
7fff94da0000-7fff94db0000 rw-s 00000000 00:17 9 /dev/shm/ltp_hugemmap34_7730 (deleted) 7fff94db0000-7fff94dd0000 r--p 00000000 00:00 0
[vvar]
7fff94dd0000-7fff94de0000 r-xp 00000000 00:00 0
[vdso]
7fff94de0000-7fff94e20000 r-xp 00000000 00:29 11457 /lib64/ld-2.31.so
7fff94e20000-7fff94e30000 r--p 00030000 00:29 11457 /lib64/ld-2.31.so
7fff94e30000-7fff94e40000 rw-p 00040000 00:29 11457 /lib64/ld-2.31.so
7ffff97a0000-7ffff97d0000 rw-p 00000000 00:00 0
[stack]
hugemmap34.c:97: TINFO: mapped at 0x7fff94c51e80 tst_coredump.c:32: TINFO: Avoid dumping corefile for process(pid=7752) hugemmap34.c:105: TPASS: Child killed by SIGSEGV as expected

Summary:

passed   1
failed   0
broken   0
skipped  0

warnings 0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw
Quoted 6 lines · Wei Gao

Hi Jan

Thanks for your quick support.
Since i use a kernel with my debug enabled so the test case timeout first time, when i use normal kernel run the test case with your patch, the test result show pass. Then i guess you will send new separate patch for fix this later?

I'm thinking if we can rewrite it with clone(), so that we can set up stack and hugepage for the child reliably.

Right now, "the mmap search" is slow, and we end up with address that's "too far". In your example
the child crashes long before it comes near the area test mapped.

Also the original test is mapping from libhugetlfs and LTP port just maps a temp file.

Quoted 144 lines

Regards
Gao Wei

Quoted 139 lines · Wei Gao
Quoted 76 lines · Jan Stancek
Quoted 16 lines · Jan Stancek
Quoted 5 lines · Wei Gao

Hi ALL

Is there any special config needed for this test case? Since the test failed with the following output on my test setup(opensuse15.5 with 6.12 kernel):

Quoted 8 lines

tst_hugepage.c:84: TINFO: 1 hugepage(s) reserved
tst_tmpdir.c:317: TINFO: Using /tmp/LTP_hugLSJb7r as tmpdir (btrfs filesystem)
tst_test.c:1100: TINFO: Mounting none to /tmp/LTP_hugLSJb7r/hugetlbfs fstyp=hugetlbfs flags=0 tst_test.c:1890: TINFO: LTP version: 20240930
tst_test.c:1894: TINFO: Tested kernel: 6.12.3-lp155.11.g78b0030-vanilla #1 SMP Fri Dec 6 08:56:39 UTC 2024 (78b0030) ppc64le tst_test.c:1727: TINFO: Timeout per run is 0h 00m 30s
tst_coredump.c:32: TINFO: Avoid dumping corefile for process(pid=6671)
hugemmap34.c:88: TBROK: waitpid(0,0x7fffd8baa220,0) failed: ECHILD (10)

Quoted 2 lines

There's missing exit() in do_child(), but that's not the only issue with the test.

Could you give this patch a try?

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +10 -0
index a7a88fbb2..818697be8 100644
@@ -51,16 +51,23 @@ void do_child(void *stop_address)
                x = alloca(STACK_ALLOCATION_SIZE);
                *x = 1;
        } while ((void *)x >= stop_address);
+       exit(0);
 }
 
 static void run_test(void)
 {
        int pid, status;
        void *stack_address, *mmap_address, *heap_address, *map;
+       char tmp[128];
 
        stack_address = alloca(0);
        heap_address = sbrk(0);
 
+       tst_res(TINFO, "heap is at: %p", heap_address);
+       tst_res(TINFO, "stack is at: %p", stack_address);
+       sprintf(tmp, "cat /proc/%d/maps", getpid());
+       TST_EXP_PASS_SILENT(system(tmp));
+
        /*
         * paranoia: start mapping two hugepages below the start of the stack,
         * in case the alignment would cause us to map over something if we
@@ -70,6 +77,9 @@ static void run_test(void)
        do {
                map = mmap(mmap_address, hpage_size, PROT_READ|PROT_WRITE,
                                MAP_SHARED | MAP_FIXED_NOREPLACE, fd, 0);
+               if (map == mmap_address)
+                       break;
+
                if (map == MAP_FAILED) {
                        if (errno == ENOMEM) {
                                tst_res(TCONF, "There is no enough

memory in the system to do mmap");

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +6 -1
@@ -80,7 +90,12 @@ static void run_test(void)
                /*
                 * if we get all the way down to the heap, stop trying
                 */
-       } while (mmap_address <= heap_address);
+       } while (mmap_address > heap_address);
+
+       if (map == MAP_FAILED)
+               tst_brk(TBROK, "failed to map hpage_size area before
 hitting heap");
+       tst_res(TINFO, "mapped at %p", mmap);
+
        pid = SAFE_FORK();
        if (pid == 0)
Quoted 2 lines
                do_child(mmap_address);

Test will timeout if use default timeout so i do quick hack change for setup:

        .forks_child = 1,

+ .max_runtime = 3600,

Test result:

make -C "/root/ltp/lib" -f "/root/ltp/lib/Makefile" all
make[1]: Entering directory '/root/ltp/lib'
GEN ltp-version.h
make[2]: Nothing to be done for 'all'.
make[2]: Nothing to be done for 'all'.
make[1]: Leaving directory '/root/ltp/lib'
hugemmap34.c:150:1: warning: missing initializer for field ‘needs_cmds’ of ‘struct tst_test’ [-Wmissing-field-initializers]
};
^
In file included from /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/../lib/hugetlb.h:19:0,

                 from hugemmap34.c:24:
../../../../../include/tst_test.h:593:21: note: ‘needs_cmds’ declared here
  const char *const *needs_cmds;
                     ^~~~~~~~~~

CC testcases/kernel/mem/hugetlb/hugemmap/hugemmap34
tst_hugepage.c:84: TINFO: 1 hugepage(s) reserved
tst_tmpdir.c:317: TINFO: Using /tmp/LTP_hugkoiPEG as tmpdir (btrfs filesystem)
tst_test.c:1100: TINFO: Mounting none to /tmp/LTP_hugkoiPEG/hugetlbfs fstyp=hugetlbfs flags=0 tst_test.c:1890: TINFO: LTP version: 20240930
tst_test.c:1894: TINFO: Tested kernel: 6.12.3-lp155.11.g78b0030-vanilla #1 SMP Fri Dec 6 08:56:39 UTC 2024 (78b0030) ppc64le tst_test.c:1727: TINFO: Timeout per run is 1h 00m 30s
hugemmap34.c:66: TINFO: heap is at: 0x1002af30000
hugemmap34.c:67: TINFO: stack is at: 0x7ffff97cab30

10000000-10040000 r-xp 00000000 00:29 11656                              /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34
10040000-10050000 r--p 00030000 00:29 11656                              /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34
10050000-10060000 rw-p 00040000 00:29 11656                              /root/ltp/testcases/kernel/mem/hugetlb/hugemmap/hugemmap34
10060000-10070000 rw-p 00000000 00:00 0
1002af00000-1002af30000 rw-p 00000000 00:00 0                            [heap]
7fff94b20000-7fff94d20000 r-xp 00000000 00:29 11464                      /lib64/libc-2.31.so
7fff94d20000-7fff94d30000 r--p 001f0000 00:29 11464                      /lib64/libc-2.31.so
7fff94d30000-7fff94d40000 rw-p 00200000 00:29 11464                      /lib64/libc-2.31.so
7fff94d40000-7fff94d70000 r-xp 00000000 00:29 11482                      /lib64/libpthread-2.31.so
7fff94d70000-7fff94d80000 r--p 00020000 00:29 11482                      /lib64/libpthread-2.31.so
7fff94d80000-7fff94d90000 rw-p 00030000 00:29 11482                      /lib64/libpthread-2.31.so
7fff94da0000-7fff94db0000 rw-s 00000000 00:17 9                          /dev/shm/ltp_hugemmap34_7730 (deleted)
7fff94db0000-7fff94dd0000 r--p 00000000 00:00 0                          [vvar]
7fff94dd0000-7fff94de0000 r-xp 00000000 00:00 0                          [vdso]
7fff94de0000-7fff94e20000 r-xp 00000000 00:29 11457                      /lib64/ld-2.31.so
7fff94e20000-7fff94e30000 r--p 00030000 00:29 11457                      /lib64/ld-2.31.so
7fff94e30000-7fff94e40000 rw-p 00040000 00:29 11457                      /lib64/ld-2.31.so
7ffff97a0000-7ffff97d0000 rw-p 00000000 00:00 0                          [stack]

hugemmap34.c:97: TINFO: mapped at 0x7fff94c51e80 tst_coredump.c:32: TINFO: Avoid dumping corefile for process(pid=7752) hugemmap34.c:105: TPASS: Child killed by SIGSEGV as expected

Summary:

passed   1
failed   0
broken   0
skipped  0

warnings 0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw
Quoted 12 lines · Jan Stancek
Quoted 6 lines · Wei Gao

Hi Jan

Thanks for your quick support.
Since i use a kernel with my debug enabled so the test case timeout first time, when i use normal kernel run the test case with your patch, the test result show pass. Then i guess you will send new separate patch for fix this later?

I'm thinking if we can rewrite it with clone(), so that we can set up stack and hugepage for the child reliably.

Quoted 6 lines

Right now, "the mmap search" is slow, and we end up with address that's "too far". In your example
the child crashes long before it comes near the area test mapped.

Also the original test is mapping from libhugetlfs and LTP port just maps a temp file.

My bad about this last part - there's actually needs_hugetlbfs = 1 in the test that I missed. Anyway, I'll give the clone() approach a try.

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

PATCH 1/1 hugemmap34: change how test finds suitable huge page and stack for test

In reply to Jan Stancek

The test is trying to find a place for huge page with mmap(MAP_FIXED_NOREPLACE) starting at top of stack and going downwards until it succeeds. The problem is that before it finds one, it goes over other existing mappings. So, child process does crash when trying to extend stack, but not because it hits mapped huge page, it hits one of the existing mappings. This search is slow, specially on -debug kernels.

This patch changes the test, by giving child process a separate stack that is always immediately above a huge page. Setup temporarily allocates huge pages and uses those addresses to map potential child stacks. When it allocates huge page and finds that mapping after it is one of child stacks, it uses that pair for the test.

---
 .../kernel/mem/hugetlb/hugemmap/hugemmap34.c  | 151 ++++++++++++++----
 1 file changed, 118 insertions(+), 33 deletions(-)
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +118 -33
index a7a88fbb2d9e..91b070a8462e 100644
@@ -20,25 +20,32 @@
  * huge page-only segment -- resulting in bugs.
  */
 
+#define _GNU_SOURCE
 #include "lapi/mmap.h"
 #include "hugetlb.h"
 #include <errno.h>
+#include <inttypes.h>
+#include <sched.h>
 
 #ifdef __LP64__
 #define STACK_ALLOCATION_SIZE	(256*1024*1024)
 #else
 #define STACK_ALLOCATION_SIZE	(16*1024*1024)
 #endif
-#define PALIGN(p, a) ((void *)LTP_ALIGN((unsigned long)(p), (a)))
 #define MNTPOINT "hugetlbfs/"
-static int  fd = -1;
-static unsigned long long hpage_size;
-static int page_size;
+#define PATH_HUGEPAGE "/sys/kernel/mm/hugepages"
+
+#define STACKS_MAX 64
 
+static int  fd = -1;
+static uintptr_t hpage_size, stacks[STACKS_MAX];
+static int stacks_num;
+static void *hpage_addr, *stack_addr;
+static void **shared_area;
 
-void do_child(void *stop_address)
+int do_child(void *stop_address)
 {
-	volatile int *x;
+	volatile char *x;
 
 	/* corefile from this process is not interesting and limiting
 	 * its size can save a lot of time. '1' is a special value,
@@ -46,57 +53,77 @@ void do_child(void *stop_address)
 	 * sets limit to RLIM_INFINITY.
 	 */
 	tst_no_corefile(1);
+	tst_res(TINFO, "Child process starting with top of stack at %p", &x);
 
 	do {
 		x = alloca(STACK_ALLOCATION_SIZE);
+		*shared_area = (void *)x;
 		*x = 1;
 	} while ((void *)x >= stop_address);
+	exit(0);
 }
 
 static void run_test(void)
 {
 	int pid, status;
-	void *stack_address, *mmap_address, *heap_address, *map;
 
-	stack_address = alloca(0);
-	heap_address = sbrk(0);
-
-	/*
-	 * paranoia: start mapping two hugepages below the start of the stack,
-	 * in case the alignment would cause us to map over something if we
-	 * only used a gap of one hugepage.
-	 */
-	mmap_address = PALIGN(stack_address - 2 * hpage_size, hpage_size);
-	do {
-		map = mmap(mmap_address, hpage_size, PROT_READ|PROT_WRITE,
-				MAP_SHARED | MAP_FIXED_NOREPLACE, fd, 0);
-		if (map == MAP_FAILED) {
-			if (errno == ENOMEM) {
-				tst_res(TCONF, "There is no enough memory in the system to do mmap");
-				return;
-			}
-		}
-		mmap_address -= hpage_size;
-		/*
-		 * if we get all the way down to the heap, stop trying
-		 */
-	} while (mmap_address <= heap_address);
-	pid = SAFE_FORK();
+	pid = ltp_clone(CLONE_VM | CLONE_VFORK | SIGCHLD, do_child,
+		hpage_addr, hpage_size, stack_addr);
 	if (pid == 0)
-		do_child(mmap_address);
+		do_child(hpage_addr);
 
 	SAFE_WAITPID(pid, &status, 0);
+	tst_res(TINFO, "Child process extended stack up to %p, failed at %p",
+		*shared_area, *shared_area - STACK_ALLOCATION_SIZE);
 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV)
 		tst_res(TPASS, "Child killed by %s as expected", tst_strsig(SIGSEGV));
 	else
 		tst_res(TFAIL, "Child: %s", tst_strstatus(status));
 }
 
+/* Return start address of next mapping or 0 */
+static uintptr_t  get_next_mapping_start(uintptr_t addr)
+{
+	FILE *fp = fopen("/proc/self/maps", "r");
+
+	if (fp == NULL)
+		tst_brk(TBROK | TERRNO, "Failed to open /proc/self/maps.");
+
+	while (!feof(fp)) {
+		uintptr_t start, end;
+		int ret;
+
+		ret = fscanf(fp, "%"PRIxPTR"-%"PRIxPTR" %*[^\n]\n", &start, &end);
+		if (ret != 2) {
+			fclose(fp);
+			tst_brk(TBROK | TERRNO, "Couldn't parse /proc/self/maps line.");
+		}
+
+		if (start > addr) {
+			fclose(fp);
+			return start;
+		}
+	}
+	fclose(fp);
+	return 0;
+}
+
+static int is_addr_in_stacks(uintptr_t addr)
+{
+	int i;
+
+	for (i = 0; i < stacks_num; i++) {
+		if (addr == stacks[i])
+			return 1;
+	}
+	return 0;
+}
+
 void setup(void)
 {
 	struct rlimit r;
+	int i;
 
-	page_size = getpagesize();
 	hpage_size = tst_get_hugepage_size();
 	/*
 	 * Setting the stack size to unlimited.
@@ -107,7 +134,65 @@ void setup(void)
 	SAFE_GETRLIMIT(RLIMIT_STACK, &r);
 	if (r.rlim_cur != RLIM_INFINITY)
 		tst_brk(TCONF, "Stack rlimit must be 'unlimited'");
+
+	if (access(PATH_HUGEPAGE, F_OK))
+		tst_brk(TCONF, "hugetlbfs is not supported");
+
 	fd = tst_creat_unlinked(MNTPOINT, 0);
+
+	/* shared memory to pass a (void *) from child process */
+	shared_area = SAFE_MMAP(0, getpagesize(), PROT_READ|PROT_WRITE,
+		MAP_SHARED|MAP_ANONYMOUS, -1, 0);
+
+	/*
+	 * We search for potential stack addresses by looking at
+	 * places where kernel would map next huge page and occupying that
+	 * address as potential stack. When huge page lands in such place
+	 * that next mapping is one of our stack mappings, we use those
+	 * two for the test. We try to map huge pages in child process so that
+	 * slices in parent are not affected.
+	 */
+	tst_res(TINFO, "searching for huge page and child stack placement");
+	for (i = 0; i < STACKS_MAX; i++) {
+		uintptr_t next_start;
+		int pid, status;
+
+		pid = SAFE_FORK();
+		if (pid == 0) {
+			*shared_area = SAFE_MMAP(0, hpage_size, PROT_READ|PROT_WRITE,
+				MAP_PRIVATE, fd, 0);
+			SAFE_MUNMAP(*shared_area, hpage_size);
+			exit(0);
+		}
+		SAFE_WAITPID(pid, &status, 0);
+		if (status != 0)
+			tst_brk(TFAIL, "Child: %s", tst_strstatus(status));
+
+		next_start = get_next_mapping_start((uintptr_t)*shared_area);
+		if (is_addr_in_stacks(next_start)) {
+			stack_addr = (void *)next_start;
+			hpage_addr = *shared_area;
+			break;
+		}
+
+		tst_res(TINFO, "potential stack at address %p", *shared_area);
+		stacks[stacks_num++] = (uintptr_t) SAFE_MMAP(*shared_area, hpage_size,
+			PROT_READ|PROT_WRITE,
+			MAP_ANONYMOUS|MAP_PRIVATE|MAP_GROWSDOWN, -1, 0);
+	}
+
+	if (!stack_addr)
+		tst_brk(TCONF, "failed to find good place for huge page and stack");
+
+	hpage_addr = mmap(hpage_addr, hpage_size, PROT_READ|PROT_WRITE,
+		MAP_SHARED|MAP_FIXED, fd, 0);
+	if (hpage_addr == MAP_FAILED) {
+		if (errno == ENOMEM)
+			tst_brk(TCONF, "Not enough memory.");
+		tst_brk(TBROK|TERRNO, "mmap failed");
+	}
+	tst_res(TINFO, "huge page is at address %p", hpage_addr);
+	tst_res(TINFO, "using stack is at address %p", stack_addr);
 }
 
 void cleanup(void)
Signature · 5 lines
2.43.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Reviewed-by on v1 1/1
#8 lore raw

Re: [LTP] [PATCH] hugemmap34: change how test finds suitable huge page and stack for test

Hi!

Quoted 122 lines
---
 .../kernel/mem/hugetlb/hugemmap/hugemmap34.c  | 151 ++++++++++++++----
 1 file changed, 118 insertions(+), 33 deletions(-)
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +37 -32
index a7a88fbb2d9e..91b070a8462e 100644
@@ -20,25 +20,32 @@
  * huge page-only segment -- resulting in bugs.
  */
 
+#define _GNU_SOURCE
 #include "lapi/mmap.h"
 #include "hugetlb.h"
 #include <errno.h>
+#include <inttypes.h>
+#include <sched.h>
 
 #ifdef __LP64__
 #define STACK_ALLOCATION_SIZE	(256*1024*1024)
 #else
 #define STACK_ALLOCATION_SIZE	(16*1024*1024)
 #endif
-#define PALIGN(p, a) ((void *)LTP_ALIGN((unsigned long)(p), (a)))
 #define MNTPOINT "hugetlbfs/"
-static int  fd = -1;
-static unsigned long long hpage_size;
-static int page_size;
+#define PATH_HUGEPAGE "/sys/kernel/mm/hugepages"
+
+#define STACKS_MAX 64
 
+static int  fd = -1;
+static uintptr_t hpage_size, stacks[STACKS_MAX];
+static int stacks_num;
+static void *hpage_addr, *stack_addr;
+static void **shared_area;
 
-void do_child(void *stop_address)
+int do_child(void *stop_address)
 {
-	volatile int *x;
+	volatile char *x;
 
 	/* corefile from this process is not interesting and limiting
 	 * its size can save a lot of time. '1' is a special value,
@@ -46,57 +53,77 @@ void do_child(void *stop_address)
 	 * sets limit to RLIM_INFINITY.
 	 */
 	tst_no_corefile(1);
+	tst_res(TINFO, "Child process starting with top of stack at %p", &x);
 
 	do {
 		x = alloca(STACK_ALLOCATION_SIZE);
+		*shared_area = (void *)x;
 		*x = 1;
 	} while ((void *)x >= stop_address);
+	exit(0);
 }
 
 static void run_test(void)
 {
 	int pid, status;
-	void *stack_address, *mmap_address, *heap_address, *map;
 
-	stack_address = alloca(0);
-	heap_address = sbrk(0);
-
-	/*
-	 * paranoia: start mapping two hugepages below the start of the stack,
-	 * in case the alignment would cause us to map over something if we
-	 * only used a gap of one hugepage.
-	 */
-	mmap_address = PALIGN(stack_address - 2 * hpage_size, hpage_size);
-	do {
-		map = mmap(mmap_address, hpage_size, PROT_READ|PROT_WRITE,
-				MAP_SHARED | MAP_FIXED_NOREPLACE, fd, 0);
-		if (map == MAP_FAILED) {
-			if (errno == ENOMEM) {
-				tst_res(TCONF, "There is no enough memory in the system to do mmap");
-				return;
-			}
-		}
-		mmap_address -= hpage_size;
-		/*
-		 * if we get all the way down to the heap, stop trying
-		 */
-	} while (mmap_address <= heap_address);
-	pid = SAFE_FORK();
+	pid = ltp_clone(CLONE_VM | CLONE_VFORK | SIGCHLD, do_child,
+		hpage_addr, hpage_size, stack_addr);
 	if (pid == 0)
-		do_child(mmap_address);
+		do_child(hpage_addr);
 
 	SAFE_WAITPID(pid, &status, 0);
+	tst_res(TINFO, "Child process extended stack up to %p, failed at %p",
+		*shared_area, *shared_area - STACK_ALLOCATION_SIZE);
 	if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV)
 		tst_res(TPASS, "Child killed by %s as expected", tst_strsig(SIGSEGV));
 	else
 		tst_res(TFAIL, "Child: %s", tst_strstatus(status));
 }
 
+/* Return start address of next mapping or 0 */
+static uintptr_t  get_next_mapping_start(uintptr_t addr)
+{
+	FILE *fp = fopen("/proc/self/maps", "r");
+
+	if (fp == NULL)
+		tst_brk(TBROK | TERRNO, "Failed to open /proc/self/maps.");
+
+	while (!feof(fp)) {
+		uintptr_t start, end;
+		int ret;
+
+		ret = fscanf(fp, "%"PRIxPTR"-%"PRIxPTR" %*[^\n]\n", &start, &end);
+		if (ret != 2) {
+			fclose(fp);
+			tst_brk(TBROK | TERRNO, "Couldn't parse /proc/self/maps line.");

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c void do_child(void *stop_address) · line 109

Quoted 10 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +10 -0
+		}
+
+		if (start > addr) {
+			fclose(fp);
+			return start;
+		}
+	}
+	fclose(fp);
+	return 0;
+}

Maybe we could read this once and cache it because besides of the additions of the potential stack mappings it would not change, but I guess that it's not worth of the effort.

Quoted 21 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +12 -1
+static int is_addr_in_stacks(uintptr_t addr)
+{
+	int i;
+
+	for (i = 0; i < stacks_num; i++) {
+		if (addr == stacks[i])
+			return 1;
+	}
+	return 0;
+}
+
 void setup(void)
 {
 	struct rlimit r;
+	int i;
 
-	page_size = getpagesize();
 	hpage_size = tst_get_hugepage_size();
 	/*
 	 * Setting the stack size to unlimited.

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c void setup(void) · line 139

Quoted 6 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +3 -0
@@ -107,7 +134,65 @@ void setup(void)
 	SAFE_GETRLIMIT(RLIMIT_STACK, &r);
 	if (r.rlim_cur != RLIM_INFINITY)
 		tst_brk(TCONF, "Stack rlimit must be 'unlimited'");
+
+	if (access(PATH_HUGEPAGE, F_OK))
+		tst_brk(TCONF, "hugetlbfs is not supported");

Hmm, we have .needs_hugetlbfs in the tst_test structure that isn't enough? Maybe the test library needs to be fixed then?

Quoted 45 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +44 -0
 	fd = tst_creat_unlinked(MNTPOINT, 0);
+
+	/* shared memory to pass a (void *) from child process */
+	shared_area = SAFE_MMAP(0, getpagesize(), PROT_READ|PROT_WRITE,
+		MAP_SHARED|MAP_ANONYMOUS, -1, 0);
+
+	/*
+	 * We search for potential stack addresses by looking at
+	 * places where kernel would map next huge page and occupying that
+	 * address as potential stack. When huge page lands in such place
+	 * that next mapping is one of our stack mappings, we use those
+	 * two for the test. We try to map huge pages in child process so that
+	 * slices in parent are not affected.
+	 */
+	tst_res(TINFO, "searching for huge page and child stack placement");
+	for (i = 0; i < STACKS_MAX; i++) {
+		uintptr_t next_start;
+		int pid, status;
+
+		pid = SAFE_FORK();
+		if (pid == 0) {
+			*shared_area = SAFE_MMAP(0, hpage_size, PROT_READ|PROT_WRITE,
+				MAP_PRIVATE, fd, 0);
+			SAFE_MUNMAP(*shared_area, hpage_size);
+			exit(0);
+		}
+		SAFE_WAITPID(pid, &status, 0);
+		if (status != 0)
+			tst_brk(TFAIL, "Child: %s", tst_strstatus(status));
+
+		next_start = get_next_mapping_start((uintptr_t)*shared_area);
+		if (is_addr_in_stacks(next_start)) {
+			stack_addr = (void *)next_start;
+			hpage_addr = *shared_area;
+			break;
+		}
+
+		tst_res(TINFO, "potential stack at address %p", *shared_area);
+		stacks[stacks_num++] = (uintptr_t) SAFE_MMAP(*shared_area, hpage_size,
+			PROT_READ|PROT_WRITE,
+			MAP_ANONYMOUS|MAP_PRIVATE|MAP_GROWSDOWN, -1, 0);
+	}
+
+	if (!stack_addr)
+		tst_brk(TCONF, "failed to find good place for huge page and stack");

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c void setup(void) · line 195

Quoted 10 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +10 -0
+
+	hpage_addr = mmap(hpage_addr, hpage_size, PROT_READ|PROT_WRITE,
+		MAP_SHARED|MAP_FIXED, fd, 0);
+	if (hpage_addr == MAP_FAILED) {
+		if (errno == ENOMEM)
+			tst_brk(TCONF, "Not enough memory.");
+		tst_brk(TBROK|TERRNO, "mmap failed");
+	}
+	tst_res(TINFO, "huge page is at address %p", hpage_addr);
+	tst_res(TINFO, "using stack is at address %p", stack_addr);

Maybe just one:

tst_res(TINFO, "stack addr = %p huge page addr = %p", ...);

Overall this looks much better than the original:

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author Reviewed-by Wei Gao
#9 lore raw

Re: [LTP] [PATCH] hugemmap34: change how test finds suitable huge page and stack for test

Hi, Jan

Thanks for your new patch, test on my setup and result is pass.

Regards
Gao Wei

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

Re: [LTP] [PATCH] hugemmap34: change how test finds suitable huge page and stack for test

In reply to Cyril Hrubis
Quoted 155 lines · Cyril Hrubis

Hi!

Quoted 122 lines
---
 .../kernel/mem/hugetlb/hugemmap/hugemmap34.c  | 151 ++++++++++++++----
 1 file changed, 118 insertions(+), 33 deletions(-)
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +37 -32
index a7a88fbb2d9e..91b070a8462e 100644
@@ -20,25 +20,32 @@
  * huge page-only segment -- resulting in bugs.
  */
 
+#define _GNU_SOURCE
 #include "lapi/mmap.h"
 #include "hugetlb.h"
 #include <errno.h>
+#include <inttypes.h>
+#include <sched.h>
 
 #ifdef __LP64__
 #define STACK_ALLOCATION_SIZE        (256*1024*1024)
 #else
 #define STACK_ALLOCATION_SIZE        (16*1024*1024)
 #endif
-#define PALIGN(p, a) ((void *)LTP_ALIGN((unsigned long)(p), (a)))
 #define MNTPOINT "hugetlbfs/"
-static int  fd = -1;
-static unsigned long long hpage_size;
-static int page_size;
+#define PATH_HUGEPAGE "/sys/kernel/mm/hugepages"
+
+#define STACKS_MAX 64
 
+static int  fd = -1;
+static uintptr_t hpage_size, stacks[STACKS_MAX];
+static int stacks_num;
+static void *hpage_addr, *stack_addr;
+static void **shared_area;
 
-void do_child(void *stop_address)
+int do_child(void *stop_address)
 {
-     volatile int *x;
+     volatile char *x;
 
      /* corefile from this process is not interesting and limiting
       * its size can save a lot of time. '1' is a special value,
@@ -46,57 +53,77 @@ void do_child(void *stop_address)
       * sets limit to RLIM_INFINITY.
       */
      tst_no_corefile(1);
+     tst_res(TINFO, "Child process starting with top of stack at %p", &x);
 
      do {
              x = alloca(STACK_ALLOCATION_SIZE);
+             *shared_area = (void *)x;
              *x = 1;
      } while ((void *)x >= stop_address);
+     exit(0);
 }
 
 static void run_test(void)
 {
      int pid, status;
-     void *stack_address, *mmap_address, *heap_address, *map;
 
-     stack_address = alloca(0);
-     heap_address = sbrk(0);
-
-     /*
-      * paranoia: start mapping two hugepages below the start of the stack,
-      * in case the alignment would cause us to map over something if we
-      * only used a gap of one hugepage.
-      */
-     mmap_address = PALIGN(stack_address - 2 * hpage_size, hpage_size);
-     do {
-             map = mmap(mmap_address, hpage_size, PROT_READ|PROT_WRITE,
-                             MAP_SHARED | MAP_FIXED_NOREPLACE, fd, 0);
-             if (map == MAP_FAILED) {
-                     if (errno == ENOMEM) {
-                             tst_res(TCONF, "There is no enough memory in the system to do mmap");
-                             return;
-                     }
-             }
-             mmap_address -= hpage_size;
-             /*
-              * if we get all the way down to the heap, stop trying
-              */
-     } while (mmap_address <= heap_address);
-     pid = SAFE_FORK();
+     pid = ltp_clone(CLONE_VM | CLONE_VFORK | SIGCHLD, do_child,
+             hpage_addr, hpage_size, stack_addr);
      if (pid == 0)
-             do_child(mmap_address);
+             do_child(hpage_addr);
 
      SAFE_WAITPID(pid, &status, 0);
+     tst_res(TINFO, "Child process extended stack up to %p, failed at %p",
+             *shared_area, *shared_area - STACK_ALLOCATION_SIZE);
      if (WIFSIGNALED(status) && WTERMSIG(status) == SIGSEGV)
              tst_res(TPASS, "Child killed by %s as expected", tst_strsig(SIGSEGV));
      else
              tst_res(TFAIL, "Child: %s", tst_strstatus(status));
 }
 
+/* Return start address of next mapping or 0 */
+static uintptr_t  get_next_mapping_start(uintptr_t addr)
+{
+     FILE *fp = fopen("/proc/self/maps", "r");
+
+     if (fp == NULL)
+             tst_brk(TBROK | TERRNO, "Failed to open /proc/self/maps.");
+
+     while (!feof(fp)) {
+             uintptr_t start, end;
+             int ret;
+
+             ret = fscanf(fp, "%"PRIxPTR"-%"PRIxPTR" %*[^\n]\n", &start, &end);
+             if (ret != 2) {
+                     fclose(fp);
+                     tst_brk(TBROK | TERRNO, "Couldn't parse /proc/self/maps line.");

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c void do_child(void *stop_address) · line 109

Quoted 10 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +10 -0
+             }
+
+             if (start > addr) {
+                     fclose(fp);
+                     return start;
+             }
+     }
+     fclose(fp);
+     return 0;
+}

Maybe we could read this once and cache it because besides of the additions of the potential stack mappings it would not change, but I guess that it's not worth of the effort.

Quoted 16 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +12 -0
+static int is_addr_in_stacks(uintptr_t addr)
+{
+     int i;
+
+     for (i = 0; i < stacks_num; i++) {
+             if (addr == stacks[i])
+                     return 1;
+     }
+     return 0;
+}
+
 void setup(void)
 {
      struct rlimit r;
+     int i;
 
Quoted 14 lines

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c void setup(void) · line 139

Quoted 11 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +3 -1
-     page_size = getpagesize();
      hpage_size = tst_get_hugepage_size();
      /*
       * Setting the stack size to unlimited.
@@ -107,7 +134,65 @@ void setup(void)
      SAFE_GETRLIMIT(RLIMIT_STACK, &r);
      if (r.rlim_cur != RLIM_INFINITY)
              tst_brk(TCONF, "Stack rlimit must be 'unlimited'");
+
+     if (access(PATH_HUGEPAGE, F_OK))
+             tst_brk(TCONF, "hugetlbfs is not supported");

Hmm, we have .needs_hugetlbfs in the tst_test structure that isn't enough? Maybe the test library needs to be fixed then?

I dropped it, it was leftover that shouldn't be needed.

Quoted 59 lines
Quoted 46 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +44 -0
      fd = tst_creat_unlinked(MNTPOINT, 0);
+
+     /* shared memory to pass a (void *) from child process */
+     shared_area = SAFE_MMAP(0, getpagesize(), PROT_READ|PROT_WRITE,
+             MAP_SHARED|MAP_ANONYMOUS, -1, 0);
+
+     /*
+      * We search for potential stack addresses by looking at
+      * places where kernel would map next huge page and occupying that
+      * address as potential stack. When huge page lands in such place
+      * that next mapping is one of our stack mappings, we use those
+      * two for the test. We try to map huge pages in child process so that
+      * slices in parent are not affected.
+      */
+     tst_res(TINFO, "searching for huge page and child stack placement");
+     for (i = 0; i < STACKS_MAX; i++) {
+             uintptr_t next_start;
+             int pid, status;
+
+             pid = SAFE_FORK();
+             if (pid == 0) {
+                     *shared_area = SAFE_MMAP(0, hpage_size, PROT_READ|PROT_WRITE,
+                             MAP_PRIVATE, fd, 0);
+                     SAFE_MUNMAP(*shared_area, hpage_size);
+                     exit(0);
+             }
+             SAFE_WAITPID(pid, &status, 0);
+             if (status != 0)
+                     tst_brk(TFAIL, "Child: %s", tst_strstatus(status));
+
+             next_start = get_next_mapping_start((uintptr_t)*shared_area);
+             if (is_addr_in_stacks(next_start)) {
+                     stack_addr = (void *)next_start;
+                     hpage_addr = *shared_area;
+                     break;
+             }
+
+             tst_res(TINFO, "potential stack at address %p", *shared_area);
+             stacks[stacks_num++] = (uintptr_t) SAFE_MMAP(*shared_area, hpage_size,
+                     PROT_READ|PROT_WRITE,
+                     MAP_ANONYMOUS|MAP_PRIVATE|MAP_GROWSDOWN, -1, 0);
+     }
+
+     if (!stack_addr)
+             tst_brk(TCONF, "failed to find good place for huge page and stack");

testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c void setup(void) · line 195

Quoted 10 lines
testcases/kernel/mem/hugetlb/hugemmap/hugemmap34.c +10 -0
+
+     hpage_addr = mmap(hpage_addr, hpage_size, PROT_READ|PROT_WRITE,
+             MAP_SHARED|MAP_FIXED, fd, 0);
+     if (hpage_addr == MAP_FAILED) {
+             if (errno == ENOMEM)
+                     tst_brk(TCONF, "Not enough memory.");
+             tst_brk(TBROK|TERRNO, "mmap failed");
+     }
+     tst_res(TINFO, "huge page is at address %p", hpage_addr);
+     tst_res(TINFO, "using stack is at address %p", stack_addr);

Maybe just one:

Quoted 1 line

tst_res(TINFO, "stack addr = %p huge page addr = %p", ...);

Applied your suggestion, I also printed upper bound, makes it easier to see that child stack address falls into the range.

Quoted 5 lines

Overall this looks much better than the original:

Thanks, pushed.

Quoted 5 lines

--
Cyril Hrubis
[email protected]

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