Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp5596015imm; Mon, 23 Jul 2018 02:33:29 -0700 (PDT) X-Google-Smtp-Source: AAOMgpdt9yFB78/3VAGvNymHgChVQLmRfoF8ikuvzLLvNC3iky5fZOgUS7k1eZK/UbDQK9mHLxJe X-Received: by 2002:a17:902:4c88:: with SMTP id b8-v6mr11738390ple.285.1532338409343; Mon, 23 Jul 2018 02:33:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1532338409; cv=none; d=google.com; s=arc-20160816; b=LGkkYnr8POb1NxnHy+tEy9tghnT4wwmusLKR2Yq4OZERanp039D8DecYOgruQ9jPny IUyGoGNA8AWuch3YxGPu45g6GUzvIxLlv6WmaWPUyTya+3vmLowvkeAFePvnrFnz7MiE +0PTbGZF2fT88qba5T9zbXk5y3+9rRn2vNYmw932O9UAVBP4HeOAVVhRdLzgve6rmHd9 E9GYdTFgX9g5UqzfeOZLf1v73UgiPDgrQVX06gQs8iyyrEnaeWoZyExHTx92cuUJ2Jwq /Dgfybk5+EvLJBTiW2gPKcPQXAfTD4/3iMx0mV715oS4kO8Mw/CWeeaZOb4hgGpUrT0g L8sQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=t12d3y23xb8ZSEyE3olCBhDySvslkp3MJYABODXGNtU=; b=TcevsWgcCzS0NoOnmQLjQUmyZX3VrPYtLcICimjgqBSxvGLowoPh8UYi63SeQRgcZe YhbtrETkoryaYzjcaQCXzUoCY3G332okltoRaIHAKhOxRdjWvbJe4+3bI18oOdO1caMc 5QsLFG3ywnUYc6HMsaeuGncXdRKGkoXmMk9uAz+p0J62ORV3u6tElMqC9LJgWD+XH0YB 7Mq68kGV7DwrBH7zIUWcKnWe/Bsd4YzS99iliw0HQ6JNzY9jGZsM5sfgc6yXCJIRhhXm QlTvDm1O24R2TTm51hMai1JKVtx8PW0EWKb8NE+Fa0vtMBygbOOO6Ijudv36+tUpBeTv Mg3A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i64-v6si4886064pfb.314.2018.07.23.02.33.14; Mon, 23 Jul 2018 02:33:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388288AbeGWKbu (ORCPT + 99 others); Mon, 23 Jul 2018 06:31:50 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:25501 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2388281AbeGWKbt (ORCPT ); Mon, 23 Jul 2018 06:31:49 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="42543723" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 23 Jul 2018 17:31:19 +0800 Received: from G08CNEXCHPEKD02.g08.fujitsu.local (unknown [10.167.33.83]) by cn.fujitsu.com (Postfix) with ESMTP id 755804B66A09; Mon, 23 Jul 2018 17:31:16 +0800 (CST) Received: from localhost (10.167.225.56) by G08CNEXCHPEKD02.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server id 14.3.399.0; Mon, 23 Jul 2018 17:31:16 +0800 From: Chao Fan To: , , , , , , , CC: , , , Chao Fan Subject: [PATCH v4 4/4] x86/boot/KASLR: Limit kaslr to choosing the immovable memory Date: Mon, 23 Jul 2018 17:29:08 +0800 Message-ID: <20180723092908.32656-5-fanc.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180723092908.32656-1-fanc.fnst@cn.fujitsu.com> References: <20180723092908.32656-1-fanc.fnst@cn.fujitsu.com> MIME-Version: 1.0 Content-Type: text/plain X-yoursite-MailScanner-ID: 755804B66A09.A9649 X-yoursite-MailScanner: Found to be clean X-yoursite-MailScanner-From: fanc.fnst@cn.fujitsu.com X-Spam-Status: No Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If 'CONFIG_MEMORY_HOTREMOVE' specified and the account of immovable memory regions is not zero. Calculate the intersection between memory regions from e820/efi memory table and immovable memory regions. Or go on the old code. Rename process_mem_region to slots_count to match slots_fetch_random, and name new function as process_mem_region. Signed-off-by: Chao Fan --- arch/x86/boot/compressed/kaslr.c | 66 ++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index 4705682caf1f..10bda3a1fcaa 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -631,9 +631,9 @@ static unsigned long slots_fetch_random(void) return 0; } -static void process_mem_region(struct mem_vector *entry, - unsigned long minimum, - unsigned long image_size) +static void slots_count(struct mem_vector *entry, + unsigned long minimum, + unsigned long image_size) { struct mem_vector region, overlap; struct slot_area slot_area; @@ -710,6 +710,56 @@ static void process_mem_region(struct mem_vector *entry, } } +static bool process_mem_region(struct mem_vector *region, + unsigned long long minimum, + unsigned long long image_size) +{ +#ifdef CONFIG_MEMORY_HOTREMOVE + /* + * If immovable memory found, filter the intersection between + * immovable memory and region to slots_count. + * Otherwise, go on old code. + */ + if (num_immovable_mem > 0) { + int i; + + for (i = 0; i < num_immovable_mem; i++) { + struct mem_vector entry; + unsigned long long start, end, entry_end, region_end; + + start = immovable_mem[i].start; + end = start + immovable_mem[i].size; + region_end = region->start + region->size; + + entry.start = clamp(region->start, start, end); + entry_end = clamp(region_end, start, end); + + if (entry.start + image_size < entry_end) { + entry.size = entry_end - entry.start; + slots_count(&entry, minimum, image_size); + + if (slot_area_index == MAX_SLOT_AREA) { + debug_putstr("Aborted e820/efi memmap scan (slot_areas full)!\n"); + return 1; + } + } + } + return 0; + } +#endif + /* + * If no immovable memory found, or MEMORY_HOTREMOVE disabled, + * walk all the regions, so use region directely. + */ + slots_count(region, minimum, image_size); + + if (slot_area_index == MAX_SLOT_AREA) { + debug_putstr("Aborted e820/efi memmap scan (slot_areas full)!\n"); + return 1; + } + return 0; +} + #ifdef CONFIG_EFI /* * Returns true if mirror region found (and must have been processed @@ -775,11 +825,8 @@ process_efi_entries(unsigned long minimum, unsigned long image_size) region.start = md->phys_addr; region.size = md->num_pages << EFI_PAGE_SHIFT; - process_mem_region(®ion, minimum, image_size); - if (slot_area_index == MAX_SLOT_AREA) { - debug_putstr("Aborted EFI scan (slot_areas full)!\n"); + if (process_mem_region(®ion, minimum, image_size)) break; - } } return true; } @@ -806,11 +853,8 @@ static void process_e820_entries(unsigned long minimum, continue; region.start = entry->addr; region.size = entry->size; - process_mem_region(®ion, minimum, image_size); - if (slot_area_index == MAX_SLOT_AREA) { - debug_putstr("Aborted e820 scan (slot_areas full)!\n"); + if (process_mem_region(®ion, minimum, image_size)) break; - } } } -- 2.17.1