Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp3335700rwb; Mon, 7 Aug 2023 11:46:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHHbEVE6R9FWMqR0oAaPwhlgmA9UCrGayUsLDrqEfHeS2jmElaHAzGZ4S++GPCGffKPwUun X-Received: by 2002:a17:902:a382:b0:1bc:4f04:17f4 with SMTP id x2-20020a170902a38200b001bc4f0417f4mr7414439pla.30.1691433995457; Mon, 07 Aug 2023 11:46:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691433995; cv=none; d=google.com; s=arc-20160816; b=ay6cYHBph8gV9BOf0Y64U/dLGidtzpfPP1kx6dDWFoB0AWC2EthB8PCS7ocyMToKHj xAYlyulGZJx1OKLRMPFlpitVao9+XzBwrJRKkLOEfrdpfk3g3siuZktjFBjXndXF2UwJ LSwqo2Ia5zd0YBZ9XOZS5AmeMhY6DFuOE7TWV3hjzothHn/1UTPFKPRoLy4dlwGMK8Lq o14N3zGyen6NTyFd959wzakqrKObcrcYlqay3abjRscdlrGw1y+SN8VPwwE2Db9mEY22 naTbhzbY3m8nR3w4Ia/VNxtf3ZOxtGEGa/JrfojDNmEGplpGLX4hJzSJgNJDBK35tAmk OGDg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=RCP5BEYE3PoYJ5/ScF2zz+qCzrPnvgCeGwlMhj3SY1E=; fh=kw66/VP8/nNzuAxmdgsKV4RxLa3i5KjpOpCBf0hO0jw=; b=Nw40ZjkbVm4REkXEW8E9ALVVa6bQQePnWg75fJHF2c+5T87HlMijWk4TsVHtObdUVJ 33rZWuVDmrryfq2/ta9oAZ9g5gDGGBy/QJoj72NVNTx39Ub6gkivoVbv8TkMw9SGRVJS RApzAHysjYK94n16VbcKO0qaVvCw8xJzhKFv9PVjjwZChvMP+r1FtJApvz6/CCVsStGg 0OZBNfEIQfba0rzeJiDxENp6Ql5W35GI7CsK1EpcN/J0Iyho2y4Gt0FuFPfV7At0k8PL Bmz0evZSUR47aXhvC7uFRuiYeOtrnxNU6IDcLtulBp+888wJ13Y4/iVX2X50TC71BGit QbJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b="wy/eJssM"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r22-20020a638f56000000b00563dc438e52si5922254pgn.518.2023.08.07.11.46.23; Mon, 07 Aug 2023 11:46:35 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b="wy/eJssM"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230288AbjHGRxk (ORCPT + 99 others); Mon, 7 Aug 2023 13:53:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229491AbjHGRxj (ORCPT ); Mon, 7 Aug 2023 13:53:39 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E411F10F3 for ; Mon, 7 Aug 2023 10:53:36 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-68730bafa6bso3841295b3a.1 for ; Mon, 07 Aug 2023 10:53:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1691430816; x=1692035616; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=RCP5BEYE3PoYJ5/ScF2zz+qCzrPnvgCeGwlMhj3SY1E=; b=wy/eJssMAH1COYyqyKXt40eoDoO5PZFm5eZaAslJDQuSmPNwiMxeydLomfZckhjuTD 3Ld79x4kdLmp41JnM+70vsTJqbGR8mXMbNfYVefIanrcfeKdmyQXtVVlOSceIRnPZMsF eJMM7xnVS3BtOzcj5IF4DTL8cup0otzN6J19WrXuoBkW1omwNZF0KRcBcaaytg/A7Ovv JoOeSxOb7AMsSai7BtOd7OdAg7KHF54i0xU6VCE5J1YV46ZMjmxR9inFypiTr+ElfWkw 51bkK748zC96T85wyt3gl4/FHjGbrnDMjro2oY0PtB3h3NX1+PH8hxNf0DOMIIZFolUu dYow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691430816; x=1692035616; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=RCP5BEYE3PoYJ5/ScF2zz+qCzrPnvgCeGwlMhj3SY1E=; b=TlSRPskX9cVEQ5G0FUYMh+96OPN0thaEmm1IgwD9GLVqA7HjKoacU+jSr4aE7Pwq5H 0oR7+8a78HS4dUs8tABN5nNI3talNx8iHNMaRde+jzMshkGzFPXTyPcOiVB5cNvpKWwD 4g1TncHuHJWMuENDlxy3MZvRfw69WvtN/7FUwlJ17zfWD8s5l/OdQ9WGnssV8sX/KEIt +OG1UdZDiAH4hIo0jCs84wy/eGecCDnlfCcbCgTfbo2yRdnCECQCEml98K5V4ZCBpquW Lyi2so4AhLghjPcdfDay248hS0sau2Vt+Z92h0iYw+m6I85eyzjqya3n/1SOVwHH1v/H 6IlQ== X-Gm-Message-State: AOJu0Yz63SXZWZt8dN4YA/fuLeVJ1egr9y9IS9uJ2iUVhaoCAw9zdJhh /JSXMZo7HivA6th2sVg8ZwZzvg== X-Received: by 2002:a05:6a20:42a8:b0:13f:c159:63ec with SMTP id o40-20020a056a2042a800b0013fc15963ecmr10577943pzj.24.1691430816123; Mon, 07 Aug 2023 10:53:36 -0700 (PDT) Received: from ghost ([50.221.140.188]) by smtp.gmail.com with ESMTPSA id e18-20020aa78c52000000b0068620bee456sm6417758pfd.209.2023.08.07.10.53.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 10:53:35 -0700 (PDT) Date: Mon, 7 Aug 2023 10:53:32 -0700 From: Charlie Jenkins To: Alexandre Ghiti Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, 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 Message-ID: References: <20230727212647.4182407-1-charlie@rivosinc.com> <20230727212647.4182407-3-charlie@rivosinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Aug 06, 2023 at 12:06:49PM +0200, Alexandre Ghiti wrote: > > 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 > > --- > > 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 > > +#include > > + > > +#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 > I may be missing something, but why would this test fail? It may not be particularily useful since the addresses will always be in the sv39 address space, but it shouldn't fail. I have tested in QEMU and it passes. This tests if 1UL << 47 is greater than the returned mmap address which will always be true. > > > > + 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 > > +#include > > + > > +#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 > > +#include > > +#include > > + > > +#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