Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1490062pxy; Thu, 29 Apr 2021 08:08:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdcg5ep9lc48HXvveDBRmTiLR1c0Nqx5j0rh426Hc2CbQhvBlCA7MSIkCCvZquSQ03KHrY X-Received: by 2002:a62:2ac3:0:b029:27f:b98b:9efd with SMTP id q186-20020a622ac30000b029027fb98b9efdmr94379pfq.43.1619708891081; Thu, 29 Apr 2021 08:08:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619708891; cv=none; d=google.com; s=arc-20160816; b=qbAfK4iSuGP9Ok4C1Q37f9NPViRlFc6CgyT6flDHJxtVAIw7RVwQUw9hHmljRqpXL+ B4rHBqn/fBfnRDwDDxmMZqqInxKW8EhAEx6mQpj+7/iP5MR4dW/S6FHz0/GIhAedbB21 tQAFNdmGm280DMZQQ9ZXnfR2ECo2CR2EKGMDEk8onJbwQreGEMb9i8XLiKGcDr4QHp3J xmgvBTwKWEaGJDGaOHS9+PsT3UqKFQvN53EwZLw519pqvYRlQIlTqKtXdR8yXyOneITk VkiFg79ZKeEIUNE8ApHtwK8SzqliawdSBqpncMJcK6aW2Dhkix1Po3iEf3S9O3s5FSHG +wQg== 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 :message-id:date:subject:cc:to:from; bh=hTMOOgzfGIx1PKOZrURGjf5gY7SvqMN7rEkC9C+rECQ=; b=Wl9eR4ntvlIUoTsrcxjTlbgAoTC0XzYQP58y+4DtkUCM5Y5VJTlv5zUiMJmreUJrXG zB6UCFGgf2MYHhYhNMNQ2BXYcPbLuvzsrM4+EazHC5EPLLxRWO3+Z63tb+zawDf/eiG6 nbaebhGdmZDoCG5vYqkgABrObqN9r8IhtxJQFY1WV53C9yF6KRJ1wwTEaavfMUJuztF9 dD7h7n9NjB6z0Dka+XmQe5ZsBgjXGw3CylzKNgcawlNSo/XCPmXLEdBL5Z4qsVHdfUcd cE9UNqDs9cunoNDPh+XwvaVX5bG8XJbvEztud1DK/3dg1ACS3Mp9baKUfBH1buWIGsrr YAmA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id w1si34665plq.36.2021.04.29.08.07.54; Thu, 29 Apr 2021 08:08:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232989AbhD2PFz (ORCPT + 99 others); Thu, 29 Apr 2021 11:05:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233290AbhD2PFx (ORCPT ); Thu, 29 Apr 2021 11:05:53 -0400 Received: from albert.telenet-ops.be (albert.telenet-ops.be [IPv6:2a02:1800:110:4::f00:1a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1062DC06138B for ; Thu, 29 Apr 2021 08:05:05 -0700 (PDT) Received: from ramsan.of.borg ([IPv6:2a02:1810:ac12:ed20:d4dd:70b4:3264:8d97]) by albert.telenet-ops.be with bizsmtp id yf532400w4p6Y3806f535w; Thu, 29 Apr 2021 17:05:04 +0200 Received: from rox.of.borg ([192.168.97.57]) by ramsan.of.borg with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.93) (envelope-from ) id 1lc8Du-001HhU-BI; Thu, 29 Apr 2021 17:05:02 +0200 Received: from geert by rox.of.borg with local (Exim 4.93) (envelope-from ) id 1lc8Dt-009d6c-Pu; Thu, 29 Apr 2021 17:05:01 +0200 From: Geert Uytterhoeven To: Alexandre Ghiti , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Damien Le Moal , Arnd Bergmann , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Geert Uytterhoeven Subject: [PATCH v2] riscv: Only extend kernel reservation if mapped read-only Date: Thu, 29 Apr 2021 17:05:00 +0200 Message-Id: <02e05df076da23fc0f52c944bbf0a5cb99e95bd6.1619708542.git.geert+renesas@glider.be> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When the kernel mapping was moved outside of the linear mapping, the kernel memory reservation was increased, to take into account mapping granularity. However, this is done unconditionally, regardless of whether the kernel memory is mapped read-only or not. If this extension is not needed, up to 2 MiB may be lost, which has a big impact on e.g. Canaan K210 (64-bit nommu) platforms with only 8 MiB of RAM. Reclaim the lost memory by only extending the reserved region when needed, i.e. depending on a simplified version of the conditional logic around the call to protect_kernel_linear_mapping_text_rodata(). Fixes: 2bfc6cd81bd17e43 ("riscv: Move kernel mapping outside of linear mapping") Signed-off-by: Geert Uytterhoeven --- v2: - Simplify the conditional, as STRICT_KERNEL_RWX depends on MMU && !XIP_KERNEL. Only tested on K210 (SiPeed MAIX BiT): -Memory: 5852K/8192K available (1344K kernel code, 147K rwdata, 272K rodata, 106K init, 72K bss, 2340K reserved, 0K cma-reserved) +Memory: 5948K/8192K available (1344K kernel code, 147K rwdata, 272K rodata, 106K init, 72K bss, 2244K reserved, 0K cma-reserved) Yes, I was lucky, as only 96 KiB was lost ;-) --- arch/riscv/mm/init.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 788eb222deacf994..3ebc0f5d2b73b42b 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -136,11 +136,16 @@ void __init setup_bootmem(void) /* * Reserve from the start of the kernel to the end of the kernel - * and make sure we align the reservation on PMD_SIZE since we will + */ +#if defined(CONFIG_64BIT) && defined(CONFIG_STRICT_KERNEL_RWX) + /* + * Make sure we align the reservation on PMD_SIZE since we will * map the kernel in the linear mapping as read-only: we do not want * any allocation to happen between _end and the next pmd aligned page. */ - memblock_reserve(vmlinux_start, (vmlinux_end - vmlinux_start + PMD_SIZE - 1) & PMD_MASK); + vmlinux_end = (vmlinux_end + PMD_SIZE - 1) & PMD_MASK; +#endif + memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start); /* * memblock allocator is not aware of the fact that last 4K bytes of -- 2.25.1