Received: by 2002:a05:6a10:a852:0:0:0:0 with SMTP id d18csp1944231pxy; Sun, 2 May 2021 07:09:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJywujD2SxoT3A27NE3Ob/otulzX8f1SdPN4lSqlzkCI9tw6CRfjDE9se7MBqzyCxd7fLeUu X-Received: by 2002:a05:6402:3131:: with SMTP id dd17mr728008edb.304.1619964580546; Sun, 02 May 2021 07:09:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619964580; cv=none; d=google.com; s=arc-20160816; b=WWNvoJlXvjFNoCTue5ZxjHXrIRY4PbNiNTKGmKlWShsicfC+6kbzkPu4kzDtOkvgbh pemdlx5d99D3jvR54u/90Day2BQNKxoTReG63IOeaYgVLShngwd5r0WaT6QF7qwt6Ghj oNUv6aeEJY8sPtoByRokV82A6SXDoNyjAmWc0cX6fzMJiPLyYyqno3pnSV/NM01lnohy gaHk/l7DcdhHh8d+/m+xT3LZr6bnqglBb5d39tcvTUyj+YQ64ApQZYuYFqDxl61lS8pC BqsnJWciKM56FtriZa4DpUSRj6cDMoAz7kZmJbKVJImo947qha/iCYqFf57KSJXvNPvo Tgnw== 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=GRXSP0g9Zaoyw+r1NcsNmqcb0c50DX+UbnLqRUJJUR4=; b=G9kyLP5/8//ZHX38Yap/gSVnjo5ujcnK2LAL9NwZWGQsDyvkXgVu2tCEwVRgeKacuQ BAl3yQFvS6LOtxk2Fc2VLYzBbrq78IPv7OK50BiFe21MGxCtbnSkeCa4eMNDvyS5nf4Q epS1s7ttiZ+mvAEm+rJu3MPDnbXTR/ECRZy3et6vnXvlGN4NgZfRh89p0kVqRKkjhK/D VTEidrNV7DPZ3uUjVU++XCtk1pQqKKErbbrh+dHLfHD3ZOPNm7mcYTKrqZJPXJZ83shY pKi3EsiDhqHMu4+XTzi9wmU5h58X4h65p3JOzOIbMjul+KX0iqnxzNivAk+gl8l5OlKu qpfQ== 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 dh5si6488233edb.454.2021.05.02.07.09.16; Sun, 02 May 2021 07:09:40 -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 S233520AbhEBOIY (ORCPT + 99 others); Sun, 2 May 2021 10:08:24 -0400 Received: from relay5-d.mail.gandi.net ([217.70.183.197]:60095 "EHLO relay5-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233239AbhEBOGC (ORCPT ); Sun, 2 May 2021 10:06:02 -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 relay5-d.mail.gandi.net (Postfix) with ESMTPSA id BCFFF1C0003; Sun, 2 May 2021 14:05:05 +0000 (UTC) Subject: Re: [PATCH v2] 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: <02e05df076da23fc0f52c944bbf0a5cb99e95bd6.1619708542.git.geert+renesas@glider.be> From: Alex Ghiti Message-ID: Date: Sun, 2 May 2021 10:05:05 -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: <02e05df076da23fc0f52c944bbf0a5cb99e95bd6.1619708542.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 Le 4/29/21 ? 11:05 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. 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 > I tested this on the following configs: - rv32_defconfig (build and valid on qemu) - defconfig (with and without CONFIG_STRICT_KERNEL_RWX) (build and valid on qemu) - xip kernel (build and valid on qemu) - nommu_k210_defconfig (build only) so you can add: Tested-by: Alexandre Ghiti Thank you again for that, Alex