Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp956572pxb; Wed, 3 Mar 2021 22:27:44 -0800 (PST) X-Google-Smtp-Source: ABdhPJzU/5ikAECYR+UGU6/SUfI9aOICWa5x7nPfvsM/rSAgj4osup+Vz9S9IlP6ZmOvc4vBhoPy X-Received: by 2002:a17:907:211b:: with SMTP id qn27mr2536667ejb.203.1614839264629; Wed, 03 Mar 2021 22:27:44 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614839264; cv=none; d=google.com; s=arc-20160816; b=G9t6plilDK4jMUZN63HiZsTHAj8VSvj00wU9reWKStf6GjPhlYYSYVO2FDfKAG5/5/ wtnpXjM1mhcdiy9sV2onm4/+6hViOD1lp9wAsR1UvFpG+Zu0Hs21sVgE3g3d91aIeYAu tHZKkddWKFhgvdQ2+m5GLrbaR6/cutfqOOU17UrS9+2IboIZNv5cpTotqO3xa0VBjogq 4+GF8i11cnwFZ8Forp4AalBYNXkWNM1Xegg4rToV53WFKfxIHcatDT8OwkYOW02vZtg0 uTPVwQKPdXNFlmPgAuByZ6h8Q+/U+EvqgMHzzzNlSapVCGOGgI56BuB8Mw+HZu8c/DmS W18A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=Q3lcceJNMuhGV5gOmnXWRxilJICm39vdcV7GSA5hqqM=; b=gJ7UsDyAOWcfQti6fKAyH3JuUAPaJ6FS/LdhRqLjqZHckCkTNVxG6lCnLBGmBeu+MC /N4yNVfPB3Y76HPq6FlEFM14MswqLY0UqUq+BkYyTqfw0k8RS+hwRk/0RNwgZqHdSot0 EUKFW1y1/pDafMH0XyhdazKa1vPTYLHmjW87Nt6ZTSKK9AHXjCSULM9YOogfCaO459Uy y3gjH3Nj7bfs8M71vzXNVf7XR/32b5h+HqKZ3k2IbUw7tPka2185Zs/zolz1yoHV+ksU NOIv6chl0KWBk3J+XVPZ/GqnbEb608TSqpVGqGWBTIwNVdNe6My7ljQW2dd3hOYQAKNg OVMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Z9WPlybC; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u15si15271658edt.339.2021.03.03.22.27.22; Wed, 03 Mar 2021 22:27:44 -0800 (PST) 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; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Z9WPlybC; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1578983AbhCBPyR (ORCPT + 99 others); Tue, 2 Mar 2021 10:54:17 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:39433 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1447467AbhCBNaG (ORCPT ); Tue, 2 Mar 2021 08:30:06 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1614691719; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Q3lcceJNMuhGV5gOmnXWRxilJICm39vdcV7GSA5hqqM=; b=Z9WPlybCjIkKKhwkpqjGXME2cxfZjG/qzUdHfNLkssq8Zkt22TVVbuauNpE+Zsttm54I6/ BAqaTiojGt8xANXWEGv8PSTBeQ9LowlyaPOQuXEyBIZWN9PGSd4f2Onax0b+Qy9KWg6Z1Z WFTTn5w3x9wqhTAFTu5w3QqoM4IBBwY= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-304-yCJ1Q7l7Pfy3sCOqMUzYCg-1; Tue, 02 Mar 2021 08:04:18 -0500 X-MC-Unique: yCJ1Q7l7Pfy3sCOqMUzYCg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 5637B85EE62; Tue, 2 Mar 2021 13:04:16 +0000 (UTC) Received: from localhost (ovpn-12-141.pek2.redhat.com [10.72.12.141]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AD4CF5C1C2; Tue, 2 Mar 2021 13:04:11 +0000 (UTC) Date: Tue, 2 Mar 2021 21:04:09 +0800 From: Baoquan He To: Mike Rapoport Cc: x86@kernel.org, Andrew Morton , Andrea Arcangeli , Borislav Petkov , David Hildenbrand , "H. Peter Anvin" , Ingo Molnar , Mel Gorman , Michal Hocko , Mike Rapoport , Qian Cai , Thomas Gleixner , Vlastimil Babka , linux-kernel@vger.kernel.org, linux-mm@kvack.org, Borislav Petkov Subject: Re: [PATCH v3 1/2] x86/setup: consolidate early memory reservations Message-ID: <20210302130409.GA2962@MiWiFi-R3L-srv> References: <20210302100406.22059-1-rppt@kernel.org> <20210302100406.22059-2-rppt@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210302100406.22059-2-rppt@kernel.org> User-Agent: Mutt/1.10.1 (2018-07-13) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 03/02/21 at 12:04pm, Mike Rapoport wrote: > From: Mike Rapoport > > The early reservations of memory areas used by the firmware, bootloader, > kernel text and data are spread over setup_arch(). Moreover, some of them > happen *after* memblock allocations, e.g trim_platform_memory_ranges() and > trim_low_memory_range() are called after reserve_real_mode() that allocates > memory. > > There was no corruption of these memory regions because memblock always > allocates memory either from the end of memory (in top-down mode) or above > the kernel image (in bottom-up mode). However, the bottom up mode is going > to be updated to span the entire memory [1] to avoid limitations caused by > KASLR. > > Consolidate early memory reservations in a dedicated function to improve > robustness against future changes. Having the early reservations in one > place also makes it clearer what memory must be reserved before we allow > memblock allocations. > > [1] https://lore.kernel.org/lkml/20201217201214.3414100-2-guro@fb.com > > Signed-off-by: Mike Rapoport > Acked-by: Borislav Petkov > --- > arch/x86/kernel/setup.c | 92 ++++++++++++++++++++--------------------- > 1 file changed, 44 insertions(+), 48 deletions(-) > > diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c > index d883176ef2ce..3e3c6036b023 100644 > --- a/arch/x86/kernel/setup.c > +++ b/arch/x86/kernel/setup.c > @@ -645,18 +645,6 @@ static void __init trim_snb_memory(void) > } > } > > -/* > - * Here we put platform-specific memory range workarounds, i.e. > - * memory known to be corrupt or otherwise in need to be reserved on > - * specific platforms. > - * > - * If this gets used more widely it could use a real dispatch mechanism. > - */ > -static void __init trim_platform_memory_ranges(void) > -{ > - trim_snb_memory(); > -} > - > static void __init trim_bios_range(void) > { > /* > @@ -729,7 +717,38 @@ static void __init trim_low_memory_range(void) > { > memblock_reserve(0, ALIGN(reserve_low, PAGE_SIZE)); > } > - > + > +static void __init early_reserve_memory(void) > +{ > + /* > + * Reserve the memory occupied by the kernel between _text and > + * __end_of_kernel_reserve symbols. Any kernel sections after the > + * __end_of_kernel_reserve symbol must be explicitly reserved with a > + * separate memblock_reserve() or they will be discarded. > + */ > + memblock_reserve(__pa_symbol(_text), > + (unsigned long)__end_of_kernel_reserve - (unsigned long)_text); > + > + /* > + * Make sure page 0 is always reserved because on systems with > + * L1TF its contents can be leaked to user processes. > + */ > + memblock_reserve(0, PAGE_SIZE); > + > + early_reserve_initrd(); > + > + if (efi_enabled(EFI_BOOT)) > + efi_memblock_x86_reserve_range(); > + > + memblock_x86_reserve_range_setup_data(); This patch looks good to me, thanks for the effort. While at it, wondering if we can rename the above function to memblock_reserve_setup_data() just as its e820 counterpart e820__reserve_setup_data(), adding 'x86' to a function under arch/x86 seems redundant. FWIW, Reviewed-by: Baoquan He Thanks Baoquan > + > + reserve_ibft_region(); > + reserve_bios_regions(); > + > + trim_snb_memory(); > + trim_low_memory_range(); > +} > + > /* > * Dump out kernel offset information on panic. > */ > @@ -764,29 +783,6 @@ dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p) > > void __init setup_arch(char **cmdline_p) > { > - /* > - * Reserve the memory occupied by the kernel between _text and > - * __end_of_kernel_reserve symbols. Any kernel sections after the > - * __end_of_kernel_reserve symbol must be explicitly reserved with a > - * separate memblock_reserve() or they will be discarded. > - */ > - memblock_reserve(__pa_symbol(_text), > - (unsigned long)__end_of_kernel_reserve - (unsigned long)_text); > - > - /* > - * Make sure page 0 is always reserved because on systems with > - * L1TF its contents can be leaked to user processes. > - */ > - memblock_reserve(0, PAGE_SIZE); > - > - early_reserve_initrd(); > - > - /* > - * At this point everything still needed from the boot loader > - * or BIOS or kernel text should be early reserved or marked not > - * RAM in e820. All other memory is free game. > - */ > - > #ifdef CONFIG_X86_32 > memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data)); > > @@ -910,8 +906,18 @@ void __init setup_arch(char **cmdline_p) > > parse_early_param(); > > - if (efi_enabled(EFI_BOOT)) > - efi_memblock_x86_reserve_range(); > + /* > + * Do some memory reservations *before* memory is added to > + * memblock, so memblock allocations won't overwrite it. > + * Do it after early param, so we could get (unlikely) panic from > + * serial. > + * > + * After this point everything still needed from the boot loader or > + * firmware or kernel text should be early reserved or marked not > + * RAM in e820. All other memory is free game. > + */ > + early_reserve_memory(); > + > #ifdef CONFIG_MEMORY_HOTPLUG > /* > * Memory used by the kernel cannot be hot-removed because Linux > @@ -938,9 +944,6 @@ void __init setup_arch(char **cmdline_p) > > x86_report_nx(); > > - /* after early param, so could get panic from serial */ > - memblock_x86_reserve_range_setup_data(); > - > if (acpi_mps_check()) { > #ifdef CONFIG_X86_LOCAL_APIC > disable_apic = 1; > @@ -1032,8 +1035,6 @@ void __init setup_arch(char **cmdline_p) > */ > find_smp_config(); > > - reserve_ibft_region(); > - > early_alloc_pgt_buf(); > > /* > @@ -1054,8 +1055,6 @@ void __init setup_arch(char **cmdline_p) > */ > sev_setup_arch(); > > - reserve_bios_regions(); > - > efi_fake_memmap(); > efi_find_mirror(); > efi_esrt_init(); > @@ -1081,9 +1080,6 @@ void __init setup_arch(char **cmdline_p) > > reserve_real_mode(); > > - trim_platform_memory_ranges(); > - trim_low_memory_range(); > - > init_mem_mapping(); > > idt_setup_early_pf(); > -- > 2.28.0 >