Received: by 10.223.185.116 with SMTP id b49csp4189161wrg; Mon, 26 Feb 2018 12:51:40 -0800 (PST) X-Google-Smtp-Source: AH8x2249iMr2jFM1eUW5oW6+53d41nYozICgjHJM4r4y1zNzOsGQijMxJOXlmi/4lC+KfAWIWMdI X-Received: by 10.99.116.28 with SMTP id p28mr9471643pgc.306.1519678300702; Mon, 26 Feb 2018 12:51:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519678300; cv=none; d=google.com; s=arc-20160816; b=TfD1gSKKfrSzx663mbIJEz3j+3fANtT54COKBtVgWLDGwzTVMybCSFVxrgPxvTxPQB AwG1+aj2YcNbghO+72Sz3o2hekeWcqxp6/CXoc4czVxcyeEjEoKssoYTZzCmU0EIbqjh s/pt7r3rtWOM3Fyzt8NrcnoDKVtUNaEHolLmL3MQATrOH85Fm5Fo710mxFqWQZ3qabBN N/ZvtHNGj+X3KXAD0YT/sH5d+iiRM/9ZP0+8dqHygitTGQ2rQGZYg8hLnzV4HaiU4qie eXYGyfNw2zZWZ9j4BwSNwch0wOYwuk5xrlhawfdeKYu/OHSCBjfzqiAK1MuBUTzUvtd+ s5PA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=nGFwPSN4u25FTBGu77/e3VmfECwDh8n9qA4nA9ztBtU=; b=q3cpj1bHVtp+vi8WMG2p0B5F2a+4y8Ufb7Jbyslw+6jzhS/GeaVJHDahHq+WhjNDGI roh6Gpp5pg9RtOFCjCi0aAzWfMyQxmc5Da8FEJhi0OhTxrQRq7iGqLX1JARuyTH2ri7F UxOeT0IHST/YS3ze+KOZ9cWpm66S/F0lfDE+ieF5q2Prdsz366zQ+PYrZRgLrGmpcKuu PyfRCaq1DL1R9J7Nzg9yn8tldDBwqdcqVrdx0bjw8P7QMLoxGfznyt6vZYUI2R9WrHPE Tp4rpN1j0Q17LIrf9+aF68EunGabXJyRVKW0eQmmWf533EBrpV0DBfIerp9CNoaOv3Z+ JS4Q== 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 m6si2544922pgu.633.2018.02.26.12.51.24; Mon, 26 Feb 2018 12:51:40 -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 S1752986AbeBZUYm (ORCPT + 99 others); Mon, 26 Feb 2018 15:24:42 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:35208 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751553AbeBZUYk (ORCPT ); Mon, 26 Feb 2018 15:24:40 -0500 Received: from localhost (clnet-b04-243.ikbnet.co.at [83.175.124.243]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 950B7FA7; Mon, 26 Feb 2018 20:24:39 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Max Filippov Subject: [PATCH 4.14 05/54] xtensa: fix high memory/reserved memory collision Date: Mon, 26 Feb 2018 21:21:42 +0100 Message-Id: <20180226202144.631427505@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180226202144.375869933@linuxfoundation.org> References: <20180226202144.375869933@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Max Filippov commit 6ac5a11dc674bc5016ea716e8082fff61f524dc1 upstream. Xtensa memory initialization code frees high memory pages without checking whether they are in the reserved memory regions or not. That results in invalid value of totalram_pages and duplicate page usage by CMA and highmem. It produces a bunch of BUGs at startup looking like this: BUG: Bad page state in process swapper pfn:70800 page:be60c000 count:0 mapcount:-127 mapping: (null) index:0x1 flags: 0x80000000() raw: 80000000 00000000 00000001 ffffff80 00000000 be60c014 be60c014 0000000a page dumped because: nonzero mapcount Modules linked in: CPU: 0 PID: 1 Comm: swapper Tainted: G B 4.16.0-rc1-00015-g7928b2cbe55b-dirty #23 Stack: bd839d33 00000000 00000018 ba97b64c a106578c bd839d70 be60c000 00000000 a1378054 bd86a000 00000003 ba97b64c a1066166 bd839da0 be60c000 ffe00000 a1066b58 bd839dc0 be504000 00000000 000002f4 bd838000 00000000 0000001e Call Trace: [] bad_page+0xac/0xd0 [] free_pages_check_bad+0x34/0x4c [] __free_pages_ok+0xae/0x14c [] __free_pages+0x30/0x64 [] init_cma_reserved_pageblock+0x35/0x44 [] cma_init_reserved_areas+0xf4/0x148 [] do_one_initcall+0x80/0xf8 [] kernel_init_freeable+0xda/0x13c [] kernel_init+0x9/0xd0 [] ret_from_kernel_thread+0xc/0x18 Only free high memory pages that are not reserved. Cc: stable@vger.kernel.org Signed-off-by: Max Filippov Signed-off-by: Greg Kroah-Hartman --- arch/xtensa/mm/init.c | 70 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 7 deletions(-) --- a/arch/xtensa/mm/init.c +++ b/arch/xtensa/mm/init.c @@ -79,19 +79,75 @@ void __init zones_init(void) free_area_init_node(0, zones_size, ARCH_PFN_OFFSET, NULL); } +#ifdef CONFIG_HIGHMEM +static void __init free_area_high(unsigned long pfn, unsigned long end) +{ + for (; pfn < end; pfn++) + free_highmem_page(pfn_to_page(pfn)); +} + +static void __init free_highpages(void) +{ + unsigned long max_low = max_low_pfn; + struct memblock_region *mem, *res; + + reset_all_zones_managed_pages(); + /* set highmem page free */ + for_each_memblock(memory, mem) { + unsigned long start = memblock_region_memory_base_pfn(mem); + unsigned long end = memblock_region_memory_end_pfn(mem); + + /* Ignore complete lowmem entries */ + if (end <= max_low) + continue; + + if (memblock_is_nomap(mem)) + continue; + + /* Truncate partial highmem entries */ + if (start < max_low) + start = max_low; + + /* Find and exclude any reserved regions */ + for_each_memblock(reserved, res) { + unsigned long res_start, res_end; + + res_start = memblock_region_reserved_base_pfn(res); + res_end = memblock_region_reserved_end_pfn(res); + + if (res_end < start) + continue; + if (res_start < start) + res_start = start; + if (res_start > end) + res_start = end; + if (res_end > end) + res_end = end; + if (res_start != start) + free_area_high(start, res_start); + start = res_end; + if (start == end) + break; + } + + /* And now free anything which remains */ + if (start < end) + free_area_high(start, end); + } +} +#else +static void __init free_highpages(void) +{ +} +#endif + /* * Initialize memory pages. */ void __init mem_init(void) { -#ifdef CONFIG_HIGHMEM - unsigned long tmp; - - reset_all_zones_managed_pages(); - for (tmp = max_low_pfn; tmp < max_pfn; tmp++) - free_highmem_page(pfn_to_page(tmp)); -#endif + free_highpages(); max_mapnr = max_pfn - ARCH_PFN_OFFSET; high_memory = (void *)__va(max_low_pfn << PAGE_SHIFT);