Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp625353pxy; Wed, 28 Apr 2021 10:43:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw7k6q/FfXFv8Pi8Kut4WuN/H50klioDdUGZ7LUz90fUG/Blm/Du+8aSCfDT3WBGn9esZb2 X-Received: by 2002:aa7:cf8d:: with SMTP id z13mr4302479edx.104.1619631785010; Wed, 28 Apr 2021 10:43:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619631785; cv=none; d=google.com; s=arc-20160816; b=h++TllBXkL2GbT3s32JWzmBgOU8jDDf6PNnbUc18EZYYRyT8Lpby4Sq1ksreaGSJia zKkHkngIjvHeu1j4BTvhbXCl4Z1cPlsG3fH1wM3IJxXocFpgDe+v8hnqMmF93wCWCeiG kEFUu3l2tSA7Zg6cUqJumIrx333NHnMo2PK3TfWaTiSUmQq+x+5/c/nlpVY20urxjOO6 WHw0S6xh8cw6ds/SxW7B7zKvQ4TRcNEuQwGI5vvpH67cwMSW8ciX/J4fdhbPTBh9oJMq PSmEwwCVvnG6m8HDS/rJYh8OZXgF4igbwEcoXandIUadk3UwZ85Tnixd0tUGNXoJU05z lZ7w== 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=/9IbZ25Vxd/3aqeNz7bfVhfcbanEzHFOogl+BUrrbKY=; b=xs0vhCI2vzoRYd8MU+SLMzVgPnqZBgODST6acQbb5fMe1CgiwW76aED9h1RKkbr0/J FUK9KECwaNPzWOm/FwSrkBRHJfJotb+YBTsaS3iHjVfvN0vUZk4fpdfoIZ7/bN1i5hGE k+2OJIlpmSb1gLBKQB0x5J/fDlhgDdsFGGrNnUKHmQLnzJbY030w8xGeHArbv2CSvruV CmwWb/pIm2uOwgtUKhPCpjxFMh6YhvlDT35XdmSRL3o7aAQm4heePwvYCrKMNBtike+Z lGZdELd+4RJSaznnhzpina/Pd6wMUH6eG07grcedXY0bzuLGwj75YUY7yUgIpQo1jtTP g4aw== 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 hp35si742581ejc.361.2021.04.28.10.42.40; Wed, 28 Apr 2021 10:43:05 -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 S241249AbhD1Qsz (ORCPT + 99 others); Wed, 28 Apr 2021 12:48:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241162AbhD1Qsv (ORCPT ); Wed, 28 Apr 2021 12:48:51 -0400 Received: from michel.telenet-ops.be (michel.telenet-ops.be [IPv6:2a02:1800:110:4::f00:18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2724BC061573 for ; Wed, 28 Apr 2021 09:48:05 -0700 (PDT) Received: from ramsan.of.borg ([IPv6:2a02:1810:ac12:ed20:91bb:828d:42f8:4e5f]) by michel.telenet-ops.be with bizsmtp id yGny2400d2ZBlDX06GnyTG; Wed, 28 Apr 2021 18:48:02 +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 1lbnLy-0014IZ-AY; Wed, 28 Apr 2021 18:47:58 +0200 Received: from geert by rox.of.borg with local (Exim 4.93) (envelope-from ) id 1lbkVD-00HLPF-Sr; Wed, 28 Apr 2021 15:45:19 +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] riscv: Only extend kernel reservation if mapped read-only Date: Wed, 28 Apr 2021 15:45:17 +0200 Message-Id: <1a7660125046b94db9c6a7d62aa0ce88c8cd2f1d.1619617340.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. matching 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 --- 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 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 788eb222deacf994..3439783f26abc488 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -136,11 +136,17 @@ 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_STRICT_KERNEL_RWX) && defined(CONFIG_64BIT) && \ + defined(CONFIG_MMU) && !defined(CONFIG_XIP_KERNEL) + /* + * 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