Skip to content

Commit 4d0c04e

Browse files
charlie-rivospalmer-dabbelt
authored andcommitted
RISC-V: mm: Add tests for RISC-V mm
Add tests that enforce mmap hint address behavior. mmap should default to sv48. mmap will provide an address at the highest address space that can fit into the hint address, unless the hint address is less than sv39 and not 0, then it will return a sv39 address. These tests are split into two files: mmap_default.c and mmap_bottomup.c because a new process must be exec'd in order to change the mmap layout. The run_mmap.sh script sets the stack to be unlimited for the mmap_bottomup.c test which triggers a bottomup layout. Signed-off-by: Charlie Jenkins <charlie@rivosinc.com> Link: https://lore.kernel.org/r/20230809232218.849726-3-charlie@rivosinc.com Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent add2cc6 commit 4d0c04e

File tree

7 files changed

+164
-1
lines changed

7 files changed

+164
-1
lines changed

tools/testing/selftests/riscv/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
ARCH ?= $(shell uname -m 2>/dev/null || echo not)
66

77
ifneq (,$(filter $(ARCH),riscv))
8-
RISCV_SUBTARGETS ?= hwprobe vector
8+
RISCV_SUBTARGETS ?= hwprobe vector mm
99
else
1010
RISCV_SUBTARGETS :=
1111
endif
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mmap_bottomup
2+
mmap_default
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
# Copyright (C) 2021 ARM Limited
3+
# Originally tools/testing/arm64/abi/Makefile
4+
5+
# Additional include paths needed by kselftest.h and local headers
6+
CFLAGS += -D_GNU_SOURCE -std=gnu99 -I.
7+
8+
TEST_GEN_FILES := testcases/mmap_default testcases/mmap_bottomup
9+
10+
TEST_PROGS := testcases/run_mmap.sh
11+
12+
include ../../lib.mk
13+
14+
$(OUTPUT)/mm: testcases/mmap_default.c testcases/mmap_bottomup.c testcases/mmap_tests.h
15+
$(CC) -o$@ $(CFLAGS) $(LDFLAGS) $^
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <sys/mman.h>
3+
#include <testcases/mmap_test.h>
4+
5+
#include "../../kselftest_harness.h"
6+
7+
TEST(infinite_rlimit)
8+
{
9+
// Only works on 64 bit
10+
#if __riscv_xlen == 64
11+
struct addresses mmap_addresses;
12+
13+
EXPECT_EQ(BOTTOM_UP, memory_layout());
14+
15+
do_mmaps(&mmap_addresses);
16+
17+
EXPECT_NE(MAP_FAILED, mmap_addresses.no_hint);
18+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_37_addr);
19+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_38_addr);
20+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_46_addr);
21+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_47_addr);
22+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_55_addr);
23+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_56_addr);
24+
25+
EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.no_hint);
26+
EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_37_addr);
27+
EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_38_addr);
28+
EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_46_addr);
29+
EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_47_addr);
30+
EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_55_addr);
31+
EXPECT_GT(1UL << 56, (unsigned long)mmap_addresses.on_56_addr);
32+
#endif
33+
}
34+
35+
TEST_HARNESS_MAIN
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <sys/mman.h>
3+
#include <testcases/mmap_test.h>
4+
5+
#include "../../kselftest_harness.h"
6+
7+
TEST(default_rlimit)
8+
{
9+
// Only works on 64 bit
10+
#if __riscv_xlen == 64
11+
struct addresses mmap_addresses;
12+
13+
EXPECT_EQ(TOP_DOWN, memory_layout());
14+
15+
do_mmaps(&mmap_addresses);
16+
17+
EXPECT_NE(MAP_FAILED, mmap_addresses.no_hint);
18+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_37_addr);
19+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_38_addr);
20+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_46_addr);
21+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_47_addr);
22+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_55_addr);
23+
EXPECT_NE(MAP_FAILED, mmap_addresses.on_56_addr);
24+
25+
EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.no_hint);
26+
EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_37_addr);
27+
EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_38_addr);
28+
EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_46_addr);
29+
EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_47_addr);
30+
EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_55_addr);
31+
EXPECT_GT(1UL << 56, (unsigned long)mmap_addresses.on_56_addr);
32+
#endif
33+
}
34+
35+
TEST_HARNESS_MAIN
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
#ifndef _TESTCASES_MMAP_TEST_H
3+
#define _TESTCASES_MMAP_TEST_H
4+
#include <sys/mman.h>
5+
#include <sys/resource.h>
6+
#include <stddef.h>
7+
8+
#define TOP_DOWN 0
9+
#define BOTTOM_UP 1
10+
11+
struct addresses {
12+
int *no_hint;
13+
int *on_37_addr;
14+
int *on_38_addr;
15+
int *on_46_addr;
16+
int *on_47_addr;
17+
int *on_55_addr;
18+
int *on_56_addr;
19+
};
20+
21+
static inline void do_mmaps(struct addresses *mmap_addresses)
22+
{
23+
/*
24+
* Place all of the hint addresses on the boundaries of mmap
25+
* sv39, sv48, sv57
26+
* User addresses end at 1<<38, 1<<47, 1<<56 respectively
27+
*/
28+
void *on_37_bits = (void *)(1UL << 37);
29+
void *on_38_bits = (void *)(1UL << 38);
30+
void *on_46_bits = (void *)(1UL << 46);
31+
void *on_47_bits = (void *)(1UL << 47);
32+
void *on_55_bits = (void *)(1UL << 55);
33+
void *on_56_bits = (void *)(1UL << 56);
34+
35+
int prot = PROT_READ | PROT_WRITE;
36+
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
37+
38+
mmap_addresses->no_hint =
39+
mmap(NULL, 5 * sizeof(int), prot, flags, 0, 0);
40+
mmap_addresses->on_37_addr =
41+
mmap(on_37_bits, 5 * sizeof(int), prot, flags, 0, 0);
42+
mmap_addresses->on_38_addr =
43+
mmap(on_38_bits, 5 * sizeof(int), prot, flags, 0, 0);
44+
mmap_addresses->on_46_addr =
45+
mmap(on_46_bits, 5 * sizeof(int), prot, flags, 0, 0);
46+
mmap_addresses->on_47_addr =
47+
mmap(on_47_bits, 5 * sizeof(int), prot, flags, 0, 0);
48+
mmap_addresses->on_55_addr =
49+
mmap(on_55_bits, 5 * sizeof(int), prot, flags, 0, 0);
50+
mmap_addresses->on_56_addr =
51+
mmap(on_56_bits, 5 * sizeof(int), prot, flags, 0, 0);
52+
}
53+
54+
static inline int memory_layout(void)
55+
{
56+
int prot = PROT_READ | PROT_WRITE;
57+
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
58+
59+
void *value1 = mmap(NULL, sizeof(int), prot, flags, 0, 0);
60+
void *value2 = mmap(NULL, sizeof(int), prot, flags, 0, 0);
61+
62+
return value2 > value1;
63+
}
64+
#endif /* _TESTCASES_MMAP_TEST_H */
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
original_stack_limit=$(ulimit -s)
5+
6+
./mmap_default
7+
8+
# Force mmap_bottomup to be ran with bottomup memory due to
9+
# the unlimited stack
10+
ulimit -s unlimited
11+
./mmap_bottomup
12+
ulimit -s $original_stack_limit

0 commit comments

Comments
 (0)