linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Alexandre Ghiti <alex@ghiti.fr>
To: Charlie Jenkins <charlie@rivosinc.com>,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org
Cc: conor@kernel.org, paul.walmsley@sifive.com, palmer@rivosinc.com,
	aou@eecs.berkeley.edu, anup@brainfault.org,
	konstantin@linuxfoundation.org, linux-doc@vger.kernel.org,
	linux-kselftest@vger.kernel.org, linux-mm@kvack.org,
	mick@ics.forth.gr, jrtc27@jrtc27.com, rdunlap@infradead.org,
	alexghiti@rivosinc.com
Subject: Re: [PATCH v8 2/4] RISC-V: mm: Add tests for RISC-V mm
Date: Sun, 6 Aug 2023 12:06:49 +0200	[thread overview]
Message-ID: <a96afa52-14aa-28f8-d4e5-34bbd3e400ef@ghiti.fr> (raw)
In-Reply-To: <20230727212647.4182407-3-charlie@rivosinc.com>


On 27/07/2023 23:26, Charlie Jenkins wrote:
> 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>
> ---
>   tools/testing/selftests/riscv/Makefile        |  2 +-
>   tools/testing/selftests/riscv/mm/.gitignore   |  2 +
>   tools/testing/selftests/riscv/mm/Makefile     | 15 +++++
>   .../riscv/mm/testcases/mmap_bottomup.c        | 35 ++++++++++
>   .../riscv/mm/testcases/mmap_default.c         | 35 ++++++++++
>   .../selftests/riscv/mm/testcases/mmap_test.h  | 64 +++++++++++++++++++
>   .../selftests/riscv/mm/testcases/run_mmap.sh  | 12 ++++
>   7 files changed, 164 insertions(+), 1 deletion(-)
>   create mode 100644 tools/testing/selftests/riscv/mm/.gitignore
>   create mode 100644 tools/testing/selftests/riscv/mm/Makefile
>   create mode 100644 tools/testing/selftests/riscv/mm/testcases/mmap_bottomup.c
>   create mode 100644 tools/testing/selftests/riscv/mm/testcases/mmap_default.c
>   create mode 100644 tools/testing/selftests/riscv/mm/testcases/mmap_test.h
>   create mode 100755 tools/testing/selftests/riscv/mm/testcases/run_mmap.sh
>
> diff --git a/tools/testing/selftests/riscv/Makefile b/tools/testing/selftests/riscv/Makefile
> index f4b3d5c9af5b..4a9ff515a3a0 100644
> --- a/tools/testing/selftests/riscv/Makefile
> +++ b/tools/testing/selftests/riscv/Makefile
> @@ -5,7 +5,7 @@
>   ARCH ?= $(shell uname -m 2>/dev/null || echo not)
>   
>   ifneq (,$(filter $(ARCH),riscv))
> -RISCV_SUBTARGETS ?= hwprobe vector
> +RISCV_SUBTARGETS ?= hwprobe vector mm
>   else
>   RISCV_SUBTARGETS :=
>   endif
> diff --git a/tools/testing/selftests/riscv/mm/.gitignore b/tools/testing/selftests/riscv/mm/.gitignore
> new file mode 100644
> index 000000000000..5c2c57cb950c
> --- /dev/null
> +++ b/tools/testing/selftests/riscv/mm/.gitignore
> @@ -0,0 +1,2 @@
> +mmap_bottomup
> +mmap_default
> diff --git a/tools/testing/selftests/riscv/mm/Makefile b/tools/testing/selftests/riscv/mm/Makefile
> new file mode 100644
> index 000000000000..11e0f0568923
> --- /dev/null
> +++ b/tools/testing/selftests/riscv/mm/Makefile
> @@ -0,0 +1,15 @@
> +# SPDX-License-Identifier: GPL-2.0
> +# Copyright (C) 2021 ARM Limited
> +# Originally tools/testing/arm64/abi/Makefile
> +
> +# Additional include paths needed by kselftest.h and local headers
> +CFLAGS += -D_GNU_SOURCE -std=gnu99 -I.
> +
> +TEST_GEN_FILES := testcases/mmap_default testcases/mmap_bottomup
> +
> +TEST_PROGS := testcases/run_mmap.sh
> +
> +include ../../lib.mk
> +
> +$(OUTPUT)/mm: testcases/mmap_default.c testcases/mmap_bottomup.c testcases/mmap_tests.h
> +	$(CC) -o$@ $(CFLAGS) $(LDFLAGS) $^
> diff --git a/tools/testing/selftests/riscv/mm/testcases/mmap_bottomup.c b/tools/testing/selftests/riscv/mm/testcases/mmap_bottomup.c
> new file mode 100644
> index 000000000000..b29379f7e478
> --- /dev/null
> +++ b/tools/testing/selftests/riscv/mm/testcases/mmap_bottomup.c
> @@ -0,0 +1,35 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include <sys/mman.h>
> +#include <testcases/mmap_test.h>
> +
> +#include "../../kselftest_harness.h"
> +
> +TEST(infinite_rlimit)
> +{
> +// Only works on 64 bit
> +#if __riscv_xlen == 64
> +	struct addresses mmap_addresses;
> +
> +	EXPECT_EQ(BOTTOM_UP, memory_layout());
> +
> +	do_mmaps(&mmap_addresses);
> +
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.no_hint);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_37_addr);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_38_addr);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_46_addr);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_47_addr);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_55_addr);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_56_addr);
> +
> +	EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.no_hint);


This test ^ will only work on sv48+ systems, if launched on a sv39 
system, it will fail


> +	EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_37_addr);
> +	EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_38_addr);
> +	EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_46_addr);
> +	EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_47_addr);
> +	EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_55_addr);
> +	EXPECT_GT(1UL << 56, (unsigned long)mmap_addresses.on_56_addr);
> +#endif
> +}
> +
> +TEST_HARNESS_MAIN
> diff --git a/tools/testing/selftests/riscv/mm/testcases/mmap_default.c b/tools/testing/selftests/riscv/mm/testcases/mmap_default.c
> new file mode 100644
> index 000000000000..d1accb91b726
> --- /dev/null
> +++ b/tools/testing/selftests/riscv/mm/testcases/mmap_default.c
> @@ -0,0 +1,35 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +#include <sys/mman.h>
> +#include <testcases/mmap_test.h>
> +
> +#include "../../kselftest_harness.h"
> +
> +TEST(default_rlimit)
> +{
> +// Only works on 64 bit
> +#if __riscv_xlen == 64
> +	struct addresses mmap_addresses;
> +
> +	EXPECT_EQ(TOP_DOWN, memory_layout());
> +
> +	do_mmaps(&mmap_addresses);
> +
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.no_hint);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_37_addr);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_38_addr);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_46_addr);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_47_addr);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_55_addr);
> +	EXPECT_NE(MAP_FAILED, mmap_addresses.on_56_addr);
> +
> +	EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.no_hint);
> +	EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_37_addr);
> +	EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_38_addr);
> +	EXPECT_GT(1UL << 38, (unsigned long)mmap_addresses.on_46_addr);
> +	EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_47_addr);
> +	EXPECT_GT(1UL << 47, (unsigned long)mmap_addresses.on_55_addr);
> +	EXPECT_GT(1UL << 56, (unsigned long)mmap_addresses.on_56_addr);
> +#endif
> +}
> +
> +TEST_HARNESS_MAIN
> diff --git a/tools/testing/selftests/riscv/mm/testcases/mmap_test.h b/tools/testing/selftests/riscv/mm/testcases/mmap_test.h
> new file mode 100644
> index 000000000000..98a892de5d19
> --- /dev/null
> +++ b/tools/testing/selftests/riscv/mm/testcases/mmap_test.h
> @@ -0,0 +1,64 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +#ifndef _TESTCASES_MMAP_TEST_H
> +#define _TESTCASES_MMAP_TEST_H
> +#include <sys/mman.h>
> +#include <sys/resource.h>
> +#include <stddef.h>
> +
> +#define TOP_DOWN 0
> +#define BOTTOM_UP 1
> +
> +struct addresses {
> +	int *no_hint;
> +	int *on_37_addr;
> +	int *on_38_addr;
> +	int *on_46_addr;
> +	int *on_47_addr;
> +	int *on_55_addr;
> +	int *on_56_addr;
> +};
> +
> +void do_mmaps(struct addresses *mmap_addresses)


I would static inline this function definition since you are in a header 
file.


> +{
> +	/*
> +	 * Place all of the hint addresses on the boundaries of mmap
> +	 * sv39, sv48, sv57
> +	 * User addresses end at 1<<38, 1<<47, 1<<56 respectively
> +	 */
> +	void *on_37_bits = (void *)(1UL << 37);
> +	void *on_38_bits = (void *)(1UL << 38);
> +	void *on_46_bits = (void *)(1UL << 46);
> +	void *on_47_bits = (void *)(1UL << 47);
> +	void *on_55_bits = (void *)(1UL << 55);
> +	void *on_56_bits = (void *)(1UL << 56);
> +
> +	int prot = PROT_READ | PROT_WRITE;
> +	int flags = MAP_PRIVATE | MAP_ANONYMOUS;
> +
> +	mmap_addresses->no_hint =
> +		mmap(NULL, 5 * sizeof(int), prot, flags, 0, 0);
> +	mmap_addresses->on_37_addr =
> +		mmap(on_37_bits, 5 * sizeof(int), prot, flags, 0, 0);
> +	mmap_addresses->on_38_addr =
> +		mmap(on_38_bits, 5 * sizeof(int), prot, flags, 0, 0);
> +	mmap_addresses->on_46_addr =
> +		mmap(on_46_bits, 5 * sizeof(int), prot, flags, 0, 0);
> +	mmap_addresses->on_47_addr =
> +		mmap(on_47_bits, 5 * sizeof(int), prot, flags, 0, 0);
> +	mmap_addresses->on_55_addr =
> +		mmap(on_55_bits, 5 * sizeof(int), prot, flags, 0, 0);
> +	mmap_addresses->on_56_addr =
> +		mmap(on_56_bits, 5 * sizeof(int), prot, flags, 0, 0);
> +}
> +
> +int memory_layout(void)
> +{
> +	int prot = PROT_READ | PROT_WRITE;
> +	int flags = MAP_PRIVATE | MAP_ANONYMOUS;
> +
> +	void *value1 = mmap(NULL, sizeof(int), prot, flags, 0, 0);
> +	void *value2 = mmap(NULL, sizeof(int), prot, flags, 0, 0);
> +
> +	return value2 > value1;
> +}
> +#endif /* _TESTCASES_MMAP_TEST_H */
> diff --git a/tools/testing/selftests/riscv/mm/testcases/run_mmap.sh b/tools/testing/selftests/riscv/mm/testcases/run_mmap.sh
> new file mode 100755
> index 000000000000..ca5ad7c48bad
> --- /dev/null
> +++ b/tools/testing/selftests/riscv/mm/testcases/run_mmap.sh
> @@ -0,0 +1,12 @@
> +#!/bin/sh
> +# SPDX-License-Identifier: GPL-2.0
> +
> +original_stack_limit=$(ulimit -s)
> +
> +./mmap_default
> +
> +# Force mmap_bottomup to be ran with bottomup memory due to
> +# the unlimited stack
> +ulimit -s unlimited
> +./mmap_bottomup
> +ulimit -s $original_stack_limit


  reply	other threads:[~2023-08-06 10:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-27 21:26 [PATCH v8 0/4] RISC-V: mm: Make SV48 the default address space Charlie Jenkins
2023-07-27 21:26 ` [PATCH v8 1/4] RISC-V: mm: Restrict address space for sv39,sv48,sv57 Charlie Jenkins
2023-08-06  9:53   ` Alexandre Ghiti
2023-08-07 21:12     ` Charlie Jenkins
2023-07-27 21:26 ` [PATCH v8 2/4] RISC-V: mm: Add tests for RISC-V mm Charlie Jenkins
2023-08-06 10:06   ` Alexandre Ghiti [this message]
2023-08-07 17:53     ` Charlie Jenkins
2023-07-27 21:26 ` [PATCH v8 3/4] RISC-V: mm: Update pgtable comment documentation Charlie Jenkins
2023-07-27 21:26 ` [PATCH v8 4/4] RISC-V: mm: Document mmap changes Charlie Jenkins
2023-08-06 10:08   ` Alexandre Ghiti

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=a96afa52-14aa-28f8-d4e5-34bbd3e400ef@ghiti.fr \
    --to=alex@ghiti.fr \
    --cc=alexghiti@rivosinc.com \
    --cc=anup@brainfault.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=charlie@rivosinc.com \
    --cc=conor@kernel.org \
    --cc=jrtc27@jrtc27.com \
    --cc=konstantin@linuxfoundation.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-riscv@lists.infradead.org \
    --cc=mick@ics.forth.gr \
    --cc=palmer@rivosinc.com \
    --cc=paul.walmsley@sifive.com \
    --cc=rdunlap@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox