Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp351520imm; Wed, 11 Jul 2018 03:43:00 -0700 (PDT) X-Google-Smtp-Source: AAOMgpelJSmUmNWAGBo7XZGBRcssLVgAo6zDgzPNWVsi3DuyJ1VebTvLrYnpe/wUXSXyRLGxIIXt X-Received: by 2002:a62:5613:: with SMTP id k19-v6mr6012437pfb.212.1531305780731; Wed, 11 Jul 2018 03:43:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1531305780; cv=none; d=google.com; s=arc-20160816; b=N9PmH92Q/3L8Fu3C8tP7anln4N/ViLeNTu1QjpDuVo2g71L0vJI7uO9XgZ0ZNJQH7w 1C8w4hf8Nsea801Cs7lZgmGbafEUhYYLvcwctbX409QIExxVEABjvvRtV4QQ7QqRb2QO kOB/kBborSdkuEx+G31VMVIbhFj1ddrN+AwgIzoZJp6mJTibij++TM2yVLlLOOD67PIV nLHD7eMUcgNf8i9Sn5H4nMXG8NnxpY19XPsG+ggEaPSrh80rfAcwLmjFSO1MQoW0hI2J Ohib7nLHJvNGvsZIXQANSP2ZXb/a/FrUY8TIzqjJ/5l5hio3xlYdQ2mswbJv7sFeAVyK MvUw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:arc-authentication-results; bh=KR43E9yppDjx9yU9mT2Uk58r21/uB1VnI4Vzl3wwDE8=; b=kttXZVjgiDHBqfkhBKxedXmvpBQVR67jV5No77GbKgz3p7I9EuKLLtlbHfMKNEohpo WfiZpkZbYVyQYFol0z3HB0t5gHWYsddUiF4h7bcWcZIq7Brn/XUTYbGOb9FgrHBQL2AG P9I2GuZbHOQZrZlTYbdbLbxg/jlEkJ2NetLfncAjeL0czVX4hxFim417sJCH6X216eIP dSEUhzOmiWYNfeb0VlUh4scRs7lJlZuKg3L3HILJ8M7DZ95bfDWtkC80t2TwRltLKuNZ otKhws1ooP67QhTbnm1llZYJdcZxZItw60mEJhmw1EL+hFX/Guwwh7C9yhVbkVjjz83t TcTQ== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q28-v6si11501641pgm.362.2018.07.11.03.42.45; Wed, 11 Jul 2018 03:43:00 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732354AbeGKKpr (ORCPT + 99 others); Wed, 11 Jul 2018 06:45:47 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:33972 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726738AbeGKKpr (ORCPT ); Wed, 11 Jul 2018 06:45:47 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CE43E7C6CA; Wed, 11 Jul 2018 10:42:04 +0000 (UTC) Received: from localhost (ovpn-8-16.pek2.redhat.com [10.72.8.16]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7747A1C5A6; Wed, 11 Jul 2018 10:42:02 +0000 (UTC) Date: Wed, 11 Jul 2018 18:41:58 +0800 From: Baoquan He To: Chao Fan , akpm@linux-foundation.org, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, x86@kernel.org, yasu.isimatu@gmail.com, keescook@chromium.org, indou.takao@jp.fujitsu.com, caoj.fnst@cn.fujitsu.com, douly.fnst@cn.fujitsu.com, mhocko@suse.com, vbabka@suse.cz, mgorman@techsingularity.net Subject: Re: Bug report about KASLR and ZONE_MOVABLE Message-ID: <20180711104158.GE2070@MiWiFi-R3L-srv> References: <20180711094244.GA2019@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180711094244.GA2019@localhost.localdomain> User-Agent: Mutt/1.9.1 (2017-09-22) X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Wed, 11 Jul 2018 10:42:04 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.2]); Wed, 11 Jul 2018 10:42:04 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'bhe@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 07/11/18 at 05:42pm, Chao Fan wrote: > Hi all, > > I found there is a BUG about KASLR and ZONE_MOVABLE. > > When users use 'kernelcore=' parameter without 'movable_node', > movable memory is evenly distributed to all nodes. The size of > ZONE_MOVABLE depends on the kernel parameter 'kernelcore=' and > 'movablecore='. > But sometiomes, KASLR may put the uncompressed kernel to the > tail position of a node, which will cause the kernel memory > set as ZONE_MOVABLE. This region can not be offlined. > > Here is a very simple test in my qemu-kvm machine, there is > only one node: > > The command line: > [root@localhost ~]# cat /proc/cmdline > BOOT_IMAGE=/vmlinuz-4.18.0-rc3+ root=/dev/mapper/fedora_localhost--live-root > ro resume=/dev/mapper/fedora_localhost--live-swap > rd.lvm.lv=fedora_localhost-live/root rd.lvm.lv=fedora_localhost-live/swap > console=ttyS0 earlyprintk=ttyS0,115200n8 memblock=debug kernelcore=50% > > I use 'kernelcore=50%' here. > > Here is my early print result, I print the random_addr after KASLR chooses > physical memory: > early console in extract_kernel > input_data: 0x000000000266b3b1 > input_len: 0x00000000007d8802 > output: 0x0000000001000000 > output_len: 0x0000000001e15698 > kernel_total_size: 0x0000000001a8b000 > trampoline_32bit: 0x000000000009d000 > booted via startup_32() > Physical KASLR using RDRAND RDTSC... > random_addr: 0x000000012f000000 > Virtual KASLR using RDRAND RDTSC... > > The address for kernel is 0x000000012f000000 > > Here is the log of ZONE: > [ 0.000000] Zone ranges: > [ 0.000000] DMA [mem 0x0000000000001000-0x0000000000ffffff] > [ 0.000000] DMA32 [mem 0x0000000001000000-0x00000000ffffffff] > [ 0.000000] Normal [mem 0x0000000100000000-0x00000001f57fffff] > [ 0.000000] Device empty > [ 0.000000] Movable zone start for each node > [ 0.000000] Node 0: 0x000000011b000000 > [ 0.000000] Early memory node ranges > [ 0.000000] node 0: [mem 0x0000000000001000-0x000000000009efff] > [ 0.000000] node 0: [mem 0x0000000000100000-0x00000000bffd6fff] > [ 0.000000] node 0: [mem 0x0000000100000000-0x00000001f57fffff] > [ 0.000000] Initmem setup node 0 [mem > 0x0000000000001000-0x00000001f57fffff] > > Only one node in my machine, ZONE_MOVABLE begins from 0x000000011b000000, > which is lower than 0x000000012f000000. > So KASLR put the kernel to the ZONE_MOVABLE. > Try to solve this problem, I think there should be a new tactic in function > find_zone_movable_pfns_for_nodes() of mm/page_alloc.c. If kernel is uncompressed > in a tail position, then just set the memory after the kernel as ZONE_MOVABLE, > at the same time, memory in other nodes will be set as ZONE_MOVABLE. Hmm, it's an issue, worth fixing it. Otherwise the size of movable area will be smaller than we expect when add "kernel_core=" or "movable_core=". Add a check in find_zone_movable_pfns_for_nodes(), and use min() to get the starting address of movable area between aligned '_etext' and start_pfn. It will go to label 'restart' to calculate the 2nd round if not satisfiled. Hi Chao, Could you check if below patch works for you? From ab6e47c6a78d1a4ccb577b995b7b386f3149732f Mon Sep 17 00:00:00 2001 From: Baoquan He Date: Wed, 11 Jul 2018 18:30:04 +0800 Subject: [PATCH] mm, page_alloc: find movable zone after kernel text In find_zone_movable_pfns_for_nodes(), when try to find the starting PFN movable zone begins in each node, kernel text position is not considered. KASLR may put kernel after which movable zone begins. Fix it by finding movable zone after kernel text. Signed-off-by: Baoquan He --- mm/page_alloc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 1521100..fe346b4 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -6678,6 +6678,8 @@ static void __init find_zone_movable_pfns_for_nodes(void) unsigned long size_pages; start_pfn = max(start_pfn, zone_movable_pfn[nid]); + /* KASLR may put kernel after 'start_pfn', start after kernel */ + start_pfn = max(start_pfn, PAGE_ALIGN(_etext)); if (start_pfn >= end_pfn) continue; -- 2.1.0