Received: by 2002:a05:6358:51dd:b0:131:369:b2a3 with SMTP id 29csp491486rwl; Wed, 9 Aug 2023 18:45:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFVg1uOaozBGYIlnpzN88Xhr1MNl+QnEkAmDrDlsfRqKr0JqOPZckc3t2QTN6+wSwL8/04f X-Received: by 2002:a05:6402:88e:b0:523:4061:3d76 with SMTP id e14-20020a056402088e00b0052340613d76mr749006edy.13.1691631927957; Wed, 09 Aug 2023 18:45:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691631927; cv=none; d=google.com; s=arc-20160816; b=EN4I64L6IkAe3nkz2VgqNXIVTCJEK1ZGi0kXeRnFlGWiBHie0WucdYx1j9nbFI0RlG +2c/9ZH6+uYRj1QTrzMssClMIg6HnkqVtM2y4lYTk8/eOPUfreP+ThnUbGBLKwgp5NqN UQeh5QoO5HxRUi+C8i43L4n/uGnXowe3HWCXNRpvuugiakqV7I4EA7P8UiqG7HGZJk1c Kbv5aPjTIZS3aGHCq7s08a3XwfTNpefY5K21m384TMZM+iH2bwkfV0ivI0VM/+bUxHER SLT2ozJ7MtwYu9GPYa6NQDzStYJ9EsdbhtUArP8SJZCrfoD6ZNlD492YmXO3ctVSRtof bVvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=BmBbsdQ/RomBbwS1vxZnTkZx2Et0MhurExBgxgdCP1I=; fh=7AUu9WDHec3ojD+PBWP8kj3GITT/pOnwiCBaYopXpyU=; b=ng1W/gh2APqMUuRMBIW6t/uWWFwzHjAHj3gNyqc3W8R3lG+BZv4sM9a4xYQy/QR4q1 gKx8Y/OFx6ELpqnmdYUrwCnMhBygsiTj9V5kUBA53/g8MsOIda2HpROgs/Kpgu/x1r0L hzyzik1O+iRCCzvVyFUboDRpfcx0UVPgamS9AC7M45YHWFBTj6o4ah1MDj4tLJo09s3f Ho3ynJxKK9cm0/0uB8oxT1HBkctX4RALfhl8lWAfiHkyK5fQvmrf/i+J6iYE/Q5E2wZk EbgJ1OKjK8In1OmJkXSzI4TbVuXXn0XXI4Nrz5gAGv59LJrqn+yymabfoB7Pm0Af9Arc Q/LA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=2+bjYsQu; 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 y17-20020aa7d511000000b00523437b4505si387579edq.448.2023.08.09.18.45.03; Wed, 09 Aug 2023 18:45:27 -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=2+bjYsQu; 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 S231794AbjHIXYZ (ORCPT + 99 others); Wed, 9 Aug 2023 19:24:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45850 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbjHIXYY (ORCPT ); Wed, 9 Aug 2023 19:24:24 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3E83D170F for ; Wed, 9 Aug 2023 16:24:23 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id d9443c01a7336-1bb7b8390e8so3326995ad.2 for ; Wed, 09 Aug 2023 16:24:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1691623463; x=1692228263; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=BmBbsdQ/RomBbwS1vxZnTkZx2Et0MhurExBgxgdCP1I=; b=2+bjYsQuCuir4dXzfsoxd+f2uLvMo+55BujM3RV74p9qc01puICn3suYXl9PdG27HR 6MlZXk8AAnRoaiKJL13ccMX1uf4QKbGt8Hh/bNm1YesgqMS51G0b/8fzdP9eA6NrA/1l rFgvFkJHzxu987W8DljvJYRi+n62AtZKjSJtheuMQvxpOPwMUgtQG8AF8UHYaoGKbmNX HUgJPK3bCetz7V1Yo7dT3SuJINERzhRHFq5fuSJUqF49V90xFMKgXrCbZNMkE42mVKF0 h+9/V8lzU/iPyiDJ2uY3OxrgT6eQp95NvTdYAGOmf4wohaiXftvKIMkcBpUDdiaSQh8P rrVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691623463; x=1692228263; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=BmBbsdQ/RomBbwS1vxZnTkZx2Et0MhurExBgxgdCP1I=; b=Dh1DtgBx1/LDYqzSJ7TslRxz5TpDMZS0pLhi8u/feT2X+v1bbqMXbvYQj1psoQVgfj JX+1DiLkH6peLXYUTFMmf+bJLeyQLOtTVuplMITRZ4uzoHbDmO27VOkDjENEBoTxo9X2 C6Y9LBKm0AuhZXj6iVl5OwJTkQ7EQpSu3qU1U0q4c231ZzZBD1D4381B7Jw9XCGidY3Z tmGSP0NFeNuu53gguj6sxVo+CEFwWshKD8+Ilt2JGVbWN+0lJ6MA+2xYhvThuf5NqzHr Wqr4PAFFhmFc4SsH/96tdK691Z1+wGfjNx22OhTfcHlLPgYn0eKuRs+UYJWVtOF2EtxN qs2Q== X-Gm-Message-State: AOJu0YwIrh6aw1v4iBwtNs52hH62PTlH7zuLqQU2oltuj/xGsxpDI4M9 gxuaiF63dqjogydQHIbUV/FOJg== X-Received: by 2002:a17:902:db06:b0:1bc:506a:58f2 with SMTP id m6-20020a170902db0600b001bc506a58f2mr632035plx.46.1691623462666; Wed, 09 Aug 2023 16:24:22 -0700 (PDT) Received: from charlie.ba.rivosinc.com ([66.220.2.162]) by smtp.gmail.com with ESMTPSA id l18-20020a170902d35200b001b54d064a4bsm82765plk.259.2023.08.09.16.24.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 16:24:22 -0700 (PDT) From: Charlie Jenkins To: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: charlie@rivosinc.com, 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: [PATCH v10 1/4] RISC-V: mm: Restrict address space for sv39,sv48,sv57 Date: Wed, 9 Aug 2023 16:22:01 -0700 Message-ID: <20230809232218.849726-2-charlie@rivosinc.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230809232218.849726-1-charlie@rivosinc.com> References: <20230809232218.849726-1-charlie@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable 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 Make sv48 the default address space for mmap as some applications currently depend on this assumption. A hint address passed to mmap will cause the largest address space that fits entirely into the hint to be used. If the hint is less than or equal to 1<<38, an sv39 address will be used. An exception is that if the hint address is 0, then a sv48 address will be used. After an address space is completely full, the next smallest address space will be used. Signed-off-by: Charlie Jenkins --- arch/riscv/include/asm/elf.h | 2 +- arch/riscv/include/asm/pgtable.h | 25 ++++++++++++-- arch/riscv/include/asm/processor.h | 52 ++++++++++++++++++++++++++---- 3 files changed, 70 insertions(+), 9 deletions(-) diff --git a/arch/riscv/include/asm/elf.h b/arch/riscv/include/asm/elf.h index c24280774caf..5d3368d5585c 100644 --- a/arch/riscv/include/asm/elf.h +++ b/arch/riscv/include/asm/elf.h @@ -49,7 +49,7 @@ extern bool compat_elf_check_arch(Elf32_Ehdr *hdr); * the loader. We need to make sure that it is out of the way of the program * that it will "exec", and that there is sufficient room for the brk. */ -#define ELF_ET_DYN_BASE ((TASK_SIZE / 3) * 2) +#define ELF_ET_DYN_BASE ((DEFAULT_MAP_WINDOW / 3) * 2) #ifdef CONFIG_64BIT #ifdef CONFIG_COMPAT diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 75970ee2bda2..bb0b9ac7b581 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -62,11 +62,16 @@ * struct pages to map half the virtual address space. Then * position vmemmap directly below the VMALLOC region. */ +#define VA_BITS_SV32 32 #ifdef CONFIG_64BIT +#define VA_BITS_SV39 39 +#define VA_BITS_SV48 48 +#define VA_BITS_SV57 57 + #define VA_BITS (pgtable_l5_enabled ? \ - 57 : (pgtable_l4_enabled ? 48 : 39)) + VA_BITS_SV57 : (pgtable_l4_enabled ? VA_BITS_SV48 : VA_BITS_SV39)) #else -#define VA_BITS 32 +#define VA_BITS VA_BITS_SV32 #endif #define VMEMMAP_SHIFT \ @@ -111,11 +116,27 @@ #include #include #include +#include #define __page_val_to_pfn(_val) (((_val) & _PAGE_PFN_MASK) >> _PAGE_PFN_SHIFT) #ifdef CONFIG_64BIT #include + +#define VA_USER_SV39 (UL(1) << (VA_BITS_SV39 - 1)) +#define VA_USER_SV48 (UL(1) << (VA_BITS_SV48 - 1)) +#define VA_USER_SV57 (UL(1) << (VA_BITS_SV57 - 1)) + +#ifdef CONFIG_COMPAT +#define MMAP_VA_BITS_64 ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS) +#define MMAP_MIN_VA_BITS_64 (VA_BITS_SV39) +#define MMAP_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_VA_BITS_64) +#define MMAP_MIN_VA_BITS (is_compat_task() ? VA_BITS_SV32 : MMAP_MIN_VA_BITS_64) +#else +#define MMAP_VA_BITS ((VA_BITS >= VA_BITS_SV48) ? VA_BITS_SV48 : VA_BITS) +#define MMAP_MIN_VA_BITS (VA_BITS_SV39) +#endif /* CONFIG_COMPAT */ + #else #include #endif /* CONFIG_64BIT */ diff --git a/arch/riscv/include/asm/processor.h b/arch/riscv/include/asm/processor.h index c950a8d9edef..3e23e1786d05 100644 --- a/arch/riscv/include/asm/processor.h +++ b/arch/riscv/include/asm/processor.h @@ -13,19 +13,59 @@ #include +#ifdef CONFIG_64BIT +#define DEFAULT_MAP_WINDOW (UL(1) << (MMAP_VA_BITS - 1)) +#define STACK_TOP_MAX TASK_SIZE_64 + +#define arch_get_mmap_end(addr, len, flags) \ +({ \ + unsigned long mmap_end; \ + typeof(addr) _addr = (addr); \ + if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \ + mmap_end = STACK_TOP_MAX; \ + else if ((_addr) >= VA_USER_SV57) \ + mmap_end = STACK_TOP_MAX; \ + else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= VA_BITS_SV48)) \ + mmap_end = VA_USER_SV48; \ + else \ + mmap_end = VA_USER_SV39; \ + mmap_end; \ +}) + +#define arch_get_mmap_base(addr, base) \ +({ \ + unsigned long mmap_base; \ + typeof(addr) _addr = (addr); \ + typeof(base) _base = (base); \ + unsigned long rnd_gap = DEFAULT_MAP_WINDOW - (_base); \ + if ((_addr) == 0 || (IS_ENABLED(CONFIG_COMPAT) && is_compat_task())) \ + mmap_base = (_base); \ + else if (((_addr) >= VA_USER_SV57) && (VA_BITS >= VA_BITS_SV57)) \ + mmap_base = VA_USER_SV57 - rnd_gap; \ + else if ((((_addr) >= VA_USER_SV48)) && (VA_BITS >= VA_BITS_SV48)) \ + mmap_base = VA_USER_SV48 - rnd_gap; \ + else \ + mmap_base = VA_USER_SV39 - rnd_gap; \ + mmap_base; \ +}) + +#else +#define DEFAULT_MAP_WINDOW TASK_SIZE +#define STACK_TOP_MAX TASK_SIZE +#endif +#define STACK_ALIGN 16 + +#define STACK_TOP DEFAULT_MAP_WINDOW + /* * This decides where the kernel will search for a free chunk of vm * space during mmap's. */ -#define TASK_UNMAPPED_BASE PAGE_ALIGN(TASK_SIZE / 3) - -#define STACK_TOP TASK_SIZE #ifdef CONFIG_64BIT -#define STACK_TOP_MAX TASK_SIZE_64 +#define TASK_UNMAPPED_BASE PAGE_ALIGN((UL(1) << MMAP_MIN_VA_BITS) / 3) #else -#define STACK_TOP_MAX TASK_SIZE +#define TASK_UNMAPPED_BASE PAGE_ALIGN(TASK_SIZE / 3) #endif -#define STACK_ALIGN 16 #ifndef __ASSEMBLY__ -- 2.34.1