Received: by 10.223.185.116 with SMTP id b49csp6034363wrg; Wed, 28 Feb 2018 02:54:00 -0800 (PST) X-Google-Smtp-Source: AG47ELu7xfu5MmGuiP0N4m0rCveXB2+RXzJV1y3NsZqoHWhORSCe5kVi9f35v47INdMwjyzCW5h4 X-Received: by 2002:a17:902:1763:: with SMTP id i90-v6mr1288037pli.309.1519815240074; Wed, 28 Feb 2018 02:54:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519815240; cv=none; d=google.com; s=arc-20160816; b=U+w9GUyycHrDIzgRC6rYK2CQlsrRVYzObSBin+gpuKsqVHfhTTuE7X1knlAdkMN2is 0moNES79m0HcOINsBzqaLo7oO1GGSh+ZwTCwXJ6ACorOCXgvucvOj9v1lOqB8yJfjH0O CB9plUXlTPf8Zt9r17h5HFucyKBAQH+c73GOATvYsaJ5oTFLXPFlWjnaCC/2hpGDFuJM Nl/60AfVGLa6h54+rHUrxTLKGtr5vtmYndYNrEIpDLdBLXQCAE9FKMosOMedveZp81cz 5snsGEVkCbj2elVArCA0UjxgpUFw873k0iRirFJlqg1P1+iC7tG+9jWPQM2wNyhsX9B8 iAtA== 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=6TrEdl1LHAYAQrGdwjt9Le8lu4HyDiFdmEpHeo6VaNg=; b=jLNgheTCApkowp3WlBix4GWiXcWSyOoDiGXpxbblHaUtQfKHGOlx6lUNr358AjK4cF +b6xfe1ndrirhq1PqgDogxY2H2hLkv+U+UkEG7ZDOSyWs/MB3ttgjJuq1GdS/A6/d589 OV+UZ7qgNTZsyv1MlDpyeTGgMuey8eu+KPuK9IOvBG+EM/BFPuuNqNdta1oF7tlbtUSQ Km76VkhqvTc2brSw9VMockqEl39t+j3DGQ1/7/OR7hD3YmEjFv8ATGgT+U/Cr7OxhuZT tflYSlIhHD6omYq1JqF5nR0EqEcEU8GmLg5deUfJ3ktE9lPeAs87idpyw0oZde1oPjIb mxYw== 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 w1si868203pge.362.2018.02.28.02.53.44; Wed, 28 Feb 2018 02:54:00 -0800 (PST) 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 S1752582AbeB1Kwf (ORCPT + 99 others); Wed, 28 Feb 2018 05:52:35 -0500 Received: from mail.cn.fujitsu.com ([183.91.158.132]:54770 "EHLO heian.cn.fujitsu.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752312AbeB1Kvk (ORCPT ); Wed, 28 Feb 2018 05:51:40 -0500 X-IronPort-AV: E=Sophos;i="5.43,368,1503331200"; d="scan'208";a="37332007" Received: from bogon (HELO cn.fujitsu.com) ([10.167.33.5]) by heian.cn.fujitsu.com with ESMTP; 28 Feb 2018 18:51:39 +0800 Received: from G08CNEXCHPEKD01.g08.fujitsu.local (unknown [10.167.33.80]) by cn.fujitsu.com (Postfix) with ESMTP id 628734D0EFD9; Wed, 28 Feb 2018 18:51:39 +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.361.1; Wed, 28 Feb 2018 18:51:43 +0800 From: Chao Fan To: , , , , , , , CC: , , Chao Fan Subject: [PATCH v9 2/5] x86/KASLR: Handle the memory regions specified in kaslr_boot_mem Date: Wed, 28 Feb 2018 18:51:02 +0800 Message-ID: <20180228105105.11487-3-fanc.fnst@cn.fujitsu.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180228105105.11487-1-fanc.fnst@cn.fujitsu.com> References: <20180228105105.11487-1-fanc.fnst@cn.fujitsu.com> MIME-Version: 1.0 Content-Type: text/plain X-yoursite-MailScanner-ID: 628734D0EFD9.AC786 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 no 'kaslr_boot_mem=' specified, just handle the e820/efi entries directly as before. Otherwise, limit kernel to memory regions specified in 'kaslr_boot_mem=' commandline. 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 Tested-by: Luiz Capitulino Acked-by: Baoquan He --- arch/x86/boot/compressed/kaslr.c | 64 +++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index e33e5cbf7244..eb47502fbe54 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -554,9 +554,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; @@ -633,6 +633,52 @@ 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) +{ + /* + * If 'kaslr_boot_mem=' specified, walk all the regions, and + * filter the intersection to slots_count. + */ + if (num_usable_region > 0) { + int i; + + for (i = 0; i < num_usable_region; i++) { + struct mem_vector entry; + unsigned long long start, end, entry_end, region_end; + + start = mem_usable[i].start; + end = start + mem_usable[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 < 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; + } + + /* + * If no kaslr_boot_mem stored, use region directly + */ + slots_count(®ion, 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 @@ -698,11 +744,9 @@ 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(region, minimum, image_size)) break; - } } return true; } @@ -729,11 +773,9 @@ 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(region, minimum, image_size)) break; - } } } -- 2.14.3