Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1439268pxy; Thu, 29 Apr 2021 07:11:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyfCUWLKt8FWJinPg86S67mevPxaW5gZJPflcWy+YwwiKixA6QhkG+ezhvpZYO8ygU+loYl X-Received: by 2002:aa7:c981:: with SMTP id c1mr18539619edt.7.1619705483343; Thu, 29 Apr 2021 07:11:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619705483; cv=none; d=google.com; s=arc-20160816; b=Jc5qKaXKGE6ELQgBDEjA7R4dxRLJDc3fG9GnclBbsgrTqZfOuTZQYXZwWx/3E0oPic 06awfHM0eVPNx+kiid5tRDJuiA2YCicJRO2ydVGdk2TkJD+9rvpmSikgG/HwAMm3Eben WjIrRUZoav+2F8mYlVe6oOA6N5kg/zkfnWSl+2K00scrWSu0pBr144Gtgm+jH93s38G+ euCwieC2kUq7YIzvRSSCA0li/2gp1Cq2mQIgrs57o1Qq5JRmqlFRynAbmthVAvyKt71C NiXuAaCcTjjRjGNJ0q28RcPhkW2or26XBs/WGd0f6I7TA68iXBjS6W35d3SVdk0zxDiB c1XQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject; bh=OgFGXXSiEJ+QtTuD1AXEFx871ayTGwYCsReXPJqgq20=; b=uWEFGO0aBkusTsGPwGrV9LbhnY1T7b8aB0lpkYSVKB44P5k8LlwOpumZ1+T0+yGZf2 MEZfKz3t3u14uW7ACncvo4zYDoegom/gCSBqlUa3Vd3uxlM3JaZQCDNgJvXsGWAxHHAf PyEf/P+eBFmLfv+FZ/x9eLbl9V/tvUjjgnN7Qfaf4nricX9pxIg+MyoJArSombh/wqMp R6fJ+XBZFwqzKQWtzpmv/CqnWoZEIiVMPuQG0IXLonZ/xkyl5NoWXYx+JYmH1wZ6GNdK cB5NpjSdxG3nr3mfPCpGoMi3mY8Slkm/lyiCerJf7bk6GO2MdhKXuoy2UGxbUr7uniMu yy8g== 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 hr13si65321ejc.630.2021.04.29.07.10.57; Thu, 29 Apr 2021 07:11:23 -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 S239646AbhD2OKq (ORCPT + 99 others); Thu, 29 Apr 2021 10:10:46 -0400 Received: from relay7-d.mail.gandi.net ([217.70.183.200]:43485 "EHLO relay7-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239617AbhD2OKq (ORCPT ); Thu, 29 Apr 2021 10:10:46 -0400 X-Originating-IP: 2.7.49.219 Received: from [192.168.1.100] (lfbn-lyo-1-457-219.w2-7.abo.wanadoo.fr [2.7.49.219]) (Authenticated sender: alex@ghiti.fr) by relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 2EDBA2000A; Thu, 29 Apr 2021 14:09:53 +0000 (UTC) Subject: Re: [PATCH] riscv: Only extend kernel reservation if mapped read-only To: Geert Uytterhoeven , Paul Walmsley , Palmer Dabbelt , Albert Ou Cc: Damien Le Moal , Arnd Bergmann , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org References: <1a7660125046b94db9c6a7d62aa0ce88c8cd2f1d.1619617340.git.geert+renesas@glider.be> From: Alex Ghiti Message-ID: <605dc5e8-0a41-7458-6037-d6263b0ffd59@ghiti.fr> Date: Thu, 29 Apr 2021 10:09:53 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <1a7660125046b94db9c6a7d62aa0ce88c8cd2f1d.1619617340.git.geert+renesas@glider.be> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: fr Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Geert, Le 4/28/21 ? 9:45 AM, Geert Uytterhoeven a ?crit?: > 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) ARCH_HAS_STRICT_KERNEL_RWX depends on MMU and !XIP_KERNEL so I think you can get rid of those checks. > + /* > + * 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 > Thanks for fixing this, Alex