Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2725925imm; Mon, 10 Sep 2018 05:42:37 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZpIU52e6bpUjFFXg44jGvR0xK5Zuloxfr9tPeYoq/XFgAoxqPiU7L89tcdnzR0rM2vFWJd X-Received: by 2002:a63:3587:: with SMTP id c129-v6mr22556414pga.290.1536583357232; Mon, 10 Sep 2018 05:42:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536583357; cv=none; d=google.com; s=arc-20160816; b=EMJf6FCona0ILo0lPNbP0VUiu993bkK11dFkjhpIUko+/eB5inhzvxSxWJTR6IdQhj cRXiFwW4YE4sFGv9WpXzpXCJq84V2HrtNXifawakGGrxUTSWN9nkz+pJUfFn4ZwrcbEX uo6T+y6CzH6bQ8orthLEDsUiaDkm8BaKYMNXZiyZh7ndLyLcqxFWnqNQ452NqNx/JJ1t 3vNRRFbl3z8Lo3XkxsfmOy9HhclRS5mHHUhrM6eV6PuHJ/KwcZ1g9oCxmlyUgdm8owMQ UmCbWkIAgl/0HHSfpR9Y30DxRFeMyM0IE3NZwkR6NDiUi4w6ASy0A1L+rkiEPh8q57Md bV9g== 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; bh=ejDl6iJ42JvEoN73AIvehT5VLiDZr2zE0qyX67lDdJg=; b=zttjE8gT7rQpaIMjtz8TS+HfmZKCsoKA7mwo1yozvjMn4WU2fJqEbCtvuviPeFnYTK fRaH65DN5c60eslP0iBA6PNDEe42fUQhebFYBk27yVlFF6rFzpFElxHHzEe3i0ar+5FG DRjKjQV8vzV0ADPTSSoaQicApNgEgVVsFlNWAghLcFYxp029TmpxrK8Vdexxha6OMGg1 gFhZRy2ga6UqQG06Qi+FpFLVpCilEZBdqmcT+i/dfTWPuGcNXI3NKKI0p3YhjVRZdPv4 ev9GDvNufIyAt9CIe9Z09PxZeWc93R5hqbnD4DrC+XzD3pH38wDUwC3EH8zZt4gUH35L lC4A== 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 g16-v6si16502779pgi.373.2018.09.10.05.42.22; Mon, 10 Sep 2018 05:42:37 -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 S1728682AbeIJRen (ORCPT + 99 others); Mon, 10 Sep 2018 13:34:43 -0400 Received: from mail.cn.fujitsu.com ([183.91.158.132]:33076 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728482AbeIJRen (ORCPT ); Mon, 10 Sep 2018 13:34:43 -0400 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="44733321" Received: from unknown (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 10 Sep 2018 20:40:41 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 1B9AA4B6EC66; Mon, 10 Sep 2018 20:40:40 +0800 (CST) Received: from localhost (10.167.225.56) by G08CNEXCHPEKD01.g08.fujitsu.local (10.167.33.89) with Microsoft SMTP Server id 14.3.408.0; Mon, 10 Sep 2018 20:40:43 +0800 From: Chao Fan To: , , , , , , CC: , , , Chao Fan Subject: [PATCH v6 3/3] x86/boot/KASLR: Limit kaslr to choosing the immovable memory Date: Mon, 10 Sep 2018 20:40:15 +0800 Message-ID: <20180910124015.18073-4-fanc.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180910124015.18073-1-fanc.fnst@cn.fujitsu.com> References: <20180910124015.18073-1-fanc.fnst@cn.fujitsu.com> MIME-Version: 1.0 Content-Type: text/plain X-yoursite-MailScanner-ID: 1B9AA4B6EC66.ABB04 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 enabled and the amount of immovable memory regions is not zero. Calculate the intersection between memory regions from e820/efi memory table and immovable memory regions. 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 3d839040cc7f..3b0b305c049d 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -583,9 +583,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; @@ -662,6 +662,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) +{ + int i; + /* + * If no immovable memory found, or MEMORY_HOTREMOVE disabled, + * walk all the regions, so use region directely. + */ + if (num_immovable_mem == 0) { + 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_MEMORY_HOTREMOVE + /* + * If immovable memory found, filter the intersection between + * immovable memory and region to slots_count. + * Otherwise, go on old code. + */ + 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 +} + #ifdef CONFIG_EFI /* * Returns true if mirror region found (and must have been processed @@ -727,11 +777,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; } @@ -758,11 +805,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