From patchwork Mon Jul 15 08:15:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 11043489 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5117613B1 for ; Mon, 15 Jul 2019 08:16:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4238926E51 for ; Mon, 15 Jul 2019 08:16:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 33CDB26E82; Mon, 15 Jul 2019 08:16:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1BDC226E51 for ; Mon, 15 Jul 2019 08:16:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E59EC6B0008; Mon, 15 Jul 2019 04:15:59 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E02F56B000A; Mon, 15 Jul 2019 04:15:59 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D01426B000C; Mon, 15 Jul 2019 04:15:59 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by kanga.kvack.org (Postfix) with ESMTP id 63B376B0008 for ; Mon, 15 Jul 2019 04:15:59 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id z20so13088313edr.15 for ; Mon, 15 Jul 2019 01:15:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=Ke5wH9Ag8u2fyYD2m3TWob2LGobe+h0dyRZV/UG23pM=; b=bg4zSuleZ0GBH6+cIFhUMqB8Y2uTK/OYMknJgNPqvwc0AorfQqeGQLKNXghIW0w8hQ HVgODhm8fs1E5aC6XotnkNf2R5nHcrJ2TL98lpwHTyPs1KwqBVqjtAuM2qfXILQ/nW7k jIkYiyvmuCCjMiD9Xxhgt2C2gz19tcta8F5g3Zlb9SFVrWkz3LBV4VMOxewoKPQP+6fC Yz47S5s6JknlZYgt9TTtneSnsPVeUJU7KmWEWqQ6bMnqBqoevMu4GSnXpRcp6VxUhj3F dfd/Ny9/VkAfzv65TNNcKSaisz1ovmXFEO+4puriKFLVkPetK76fZ75vfJt+hOLjyxgX Ofxw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of osalvador@suse.de designates 195.135.220.15 as permitted sender) smtp.mailfrom=osalvador@suse.de X-Gm-Message-State: APjAAAUCNMZqxu7hDaPJ4MmuDiYKTysyFbNArRjRTQgll59DoVcy9BHT Q9GJG9lCBy7UJR3NGl3usOpztwEdScWQk8aEWhgxBfuvtgCUbvtKqh5iDQj8f6Avf9xtvfBf/zB 6JzXhZ5n8Rof0ex8Yr+auB419kIwPIfB+PRxw2juXA7G0lXjggffBZ9rXs6LN2TQu8Q== X-Received: by 2002:aa7:d30b:: with SMTP id p11mr22391390edq.23.1563178558986; Mon, 15 Jul 2019 01:15:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqxhzr1FMWyF06ZvEmUqjmLCJy0oooqopEpTruNsuuHgCTZ8+wG63GepEUNl+bvmoS14TJAm X-Received: by 2002:aa7:d30b:: with SMTP id p11mr22391337edq.23.1563178558226; Mon, 15 Jul 2019 01:15:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563178558; cv=none; d=google.com; s=arc-20160816; b=bt9EaJKKUHaDQKsZY24GaUvzQE/0tITatkiU/cbJnGk/NcATLEduplEIX0JSx03Q1j /RrFaFQ/LQDujV12fAfKX0SIFilyXACSV3rZZerSgLc5cH8+NwLsNGIEKbBhHPdLGwxb O5wL3MAxDmdq4PA0MVEIj6SCu4pDmJW6/yjgmBPZmlQrMUzJnZ//rM0DNLnlV6z5T4SO Jz4pffhS3P9I9mMK1MfCGuzNip7zHxOmWlrpduJ8IK+qWbPDPdBFmZQ3ySPWlM96ICBw E+5i5x1rmmaVXK8dSmjxoiRP4z7E6X+z/qrOL2bvNd0i5zlukY+oe7ooskkzPOgcpgql 0XXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=Ke5wH9Ag8u2fyYD2m3TWob2LGobe+h0dyRZV/UG23pM=; b=amLZtq0WVsPfOXPDdPqrD0uPkhEF/LYAwMjYK09Iqlt1ohAtCB46u9SjyR60ZGIL5a obQBz+whA4SqmXHl+kgO8YeEPXfnCn0i1H82haWPEYcOMj2+/bySew8nOr8PFy+/AVvW HuH4tLLxyl/E0FQ4faxXAjP7T14uAeDf1tWV4X1eXl2jnNq+tQ5rHn6NzSj6XnOBBDGI RVqjlZQWsRsi8Slhi9Dgy5zuJHVihZkBQ3SaePlVcm5PRiYbaWJsAzuvLE3wM2XIsb1t Rxrc4L4uXQ8GmG9imU3Cggxg8bz9U5uhLqWVAfTF3a1TYSXqeQQwGkp7G1YUFQHst/j3 WzbA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of osalvador@suse.de designates 195.135.220.15 as permitted sender) smtp.mailfrom=osalvador@suse.de Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id x7si8602044ejv.380.2019.07.15.01.15.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Jul 2019 01:15:58 -0700 (PDT) Received-SPF: pass (google.com: domain of osalvador@suse.de designates 195.135.220.15 as permitted sender) client-ip=195.135.220.15; Authentication-Results: mx.google.com; spf=pass (google.com: domain of osalvador@suse.de designates 195.135.220.15 as permitted sender) smtp.mailfrom=osalvador@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id DB217AFDB; Mon, 15 Jul 2019 08:15:57 +0000 (UTC) From: Oscar Salvador To: akpm@linux-foundation.org Cc: dan.j.williams@intel.com, david@redhat.com, pasha.tatashin@soleen.com, mhocko@suse.com, aneesh.kumar@linux.ibm.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH 1/2] mm,sparse: Fix deactivate_section for early sections Date: Mon, 15 Jul 2019 10:15:48 +0200 Message-Id: <20190715081549.32577-2-osalvador@suse.de> X-Mailer: git-send-email 2.13.7 In-Reply-To: <20190715081549.32577-1-osalvador@suse.de> References: <20190715081549.32577-1-osalvador@suse.de> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP deactivate_section checks whether a section is early or not in order to either call free_map_bootmem() or depopulate_section_memmap(). Being the former for sections added at boot time, and the latter for sections hotplugged. The problem is that we zero section_mem_map, so the last early_section() will always report false and the section will not be removed. Fix this checking whether a section is early or not at function entry. Fixes: mmotm ("mm/sparsemem: Support sub-section hotplug") Signed-off-by: Oscar Salvador Reviewed-by: Aneesh Kumar K.V Reviewed-by: Dan Williams Reviewed-by: David Hildenbrand --- mm/sparse.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mm/sparse.c b/mm/sparse.c index 3267c4001c6d..1e224149aab6 100644 --- a/mm/sparse.c +++ b/mm/sparse.c @@ -738,6 +738,7 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages, DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 }; DECLARE_BITMAP(tmp, SUBSECTIONS_PER_SECTION) = { 0 }; struct mem_section *ms = __pfn_to_section(pfn); + bool section_is_early = early_section(ms); struct page *memmap = NULL; unsigned long *subsection_map = ms->usage ? &ms->usage->subsection_map[0] : NULL; @@ -772,7 +773,7 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages, if (bitmap_empty(subsection_map, SUBSECTIONS_PER_SECTION)) { unsigned long section_nr = pfn_to_section_nr(pfn); - if (!early_section(ms)) { + if (!section_is_early) { kfree(ms->usage); ms->usage = NULL; } @@ -780,7 +781,7 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages, ms->section_mem_map = sparse_encode_mem_map(NULL, section_nr); } - if (early_section(ms) && memmap) + if (section_is_early && memmap) free_map_bootmem(memmap); else depopulate_section_memmap(pfn, nr_pages, altmap); From patchwork Mon Jul 15 08:15:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 11043491 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1240B13B1 for ; Mon, 15 Jul 2019 08:16:08 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 032EA26E51 for ; Mon, 15 Jul 2019 08:16:08 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EBACA26E82; Mon, 15 Jul 2019 08:16:07 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CCE3726E51 for ; Mon, 15 Jul 2019 08:16:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 134596B000A; Mon, 15 Jul 2019 04:16:01 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0E6E36B000C; Mon, 15 Jul 2019 04:16:01 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EEF526B000D; Mon, 15 Jul 2019 04:16:00 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by kanga.kvack.org (Postfix) with ESMTP id 981AC6B000A for ; Mon, 15 Jul 2019 04:16:00 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id f19so13077619edv.16 for ; Mon, 15 Jul 2019 01:16:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=kWMxpz/Mp4/cQF0JE/+HE+JA6YesC6EdPlbWWhbxzck=; b=OVe9EcCKMSmJpdoxXqTWgif7XMjVRfJVDjuJ7tebgjTe1gjwd3CB/Pfan8Z9yWoaAQ l8eNFf8zj9pPb7iRt4D5EsHEGz7lTKXML/bCFLhkVLefG/Y3+xq8tN0F/zsg4RtHKMEa j6uSutWh9RLvkglZtL5xh8pCLwWxbv3nXtimnp1gJIsYPV8MXbxVJUSdTH3xNi5fPXat mXr7nb76B57bQJStKOiTYHXMBDrv2nHPBWtCX0dAows4HCbTGXz3u7eTPBM6ZUosXKhS 2ZmLLz6QzdZzd+MvfdobpwsBekdlbe8MgN/FVPhTTH0Y99HmBa7CbCzyVuOUODz/PeBg Wmwg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of osalvador@suse.de designates 195.135.220.15 as permitted sender) smtp.mailfrom=osalvador@suse.de X-Gm-Message-State: APjAAAVk8EKq/i8+S9hzQii9FawDvnjrLpOZCgO86CR/9+Wnid5pFibx CToIu2p4XYOGPATN+rFIGkH+2DDYh3Znb5u/mRHpsqK6TM5LqOOvc0Bzrl20qB+bhK22RItC/hN FmMMdtx8KhQ55bqd6OFVL+w6yRp1rN4XlotE2Q+yP8uFyiQHXaAceImjBZpHchyY6Qw== X-Received: by 2002:a17:906:710:: with SMTP id y16mr19726094ejb.58.1563178560187; Mon, 15 Jul 2019 01:16:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqweaM8X2P6AxQSzkVI5+P5SfboV/7MYXUHSoV4IL1YIn2Tsv9HdgV4VZ6se7kgNz2JlhwIH X-Received: by 2002:a17:906:710:: with SMTP id y16mr19726006ejb.58.1563178558844; Mon, 15 Jul 2019 01:15:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563178558; cv=none; d=google.com; s=arc-20160816; b=Brjy64hUVMIckoqSJFjcz1tXp0C45JX0d0enOLAOxOkYmfxhdwai0sxXr/XBzbnMt0 0FvAHGeK+KT3LimUyLYXIwPlJrLTBsZo7x/d9WrwmoKfGySMbg4IPCnzbQFtKVEeMO8H 5O8aeX9FFa+pQgP04DayffnlazxJplUFcD8XAVhTkNgBYBBS4MsLX2lbbiCmJZt9fsYc LqiVcaCG5S9kVA26kko9V2pM6njBvg2VrDRQXPt+7TrKfMvF/XD2xDj4Jzv5vbQ3gll9 muDrsVegYKe/cvTHEF1ToBbnf2P7p0ei4o1Q7rad4xoXADlI4flpJkdJ9b5D7wUu/Rzl u5HA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=kWMxpz/Mp4/cQF0JE/+HE+JA6YesC6EdPlbWWhbxzck=; b=JJ/xSYdJQFR9FeRT5UN2ueQ5gvnWlwPInTnuzzSvbJk9SRjrEqN1MYoK4RF4xz2IoD 7afqXYJoiebtzYzdCJakMwLeg3tGa/4ozTynerVomY3oWQk6OmybNcu1j2dK5xBfFkfa 09uBCCdAAhvQybNRbQROazOnj6o2T5SXe0HtKaVGZ8CY3geWZBRJfI1eAWb/Esv+S2Vg lHhlZzD71xQNyeEJopKaEIYO87gOxBR9BYyB192/QtHJv2xyI7e1mkKpXqsDTaP8G72X +eK9AzvJA+NsU7AHyq204H8qcpO8djzwk1JDiI/dJJy5gezEkCiwwkY3j4zuonO63q21 7ltA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of osalvador@suse.de designates 195.135.220.15 as permitted sender) smtp.mailfrom=osalvador@suse.de Received: from mx1.suse.de (mx2.suse.de. [195.135.220.15]) by mx.google.com with ESMTPS id b21si9403737edw.264.2019.07.15.01.15.58 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 15 Jul 2019 01:15:58 -0700 (PDT) Received-SPF: pass (google.com: domain of osalvador@suse.de designates 195.135.220.15 as permitted sender) client-ip=195.135.220.15; Authentication-Results: mx.google.com; spf=pass (google.com: domain of osalvador@suse.de designates 195.135.220.15 as permitted sender) smtp.mailfrom=osalvador@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 788A2AD1E; Mon, 15 Jul 2019 08:15:58 +0000 (UTC) From: Oscar Salvador To: akpm@linux-foundation.org Cc: dan.j.williams@intel.com, david@redhat.com, pasha.tatashin@soleen.com, mhocko@suse.com, aneesh.kumar@linux.ibm.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Oscar Salvador Subject: [PATCH 2/2] mm,memory_hotplug: Fix shrink_{zone,node}_span Date: Mon, 15 Jul 2019 10:15:49 +0200 Message-Id: <20190715081549.32577-3-osalvador@suse.de> X-Mailer: git-send-email 2.13.7 In-Reply-To: <20190715081549.32577-1-osalvador@suse.de> References: <20190715081549.32577-1-osalvador@suse.de> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Since [1], shrink_{zone,node}_span work on PAGES_PER_SUBSECTION granularity. The problem is that deactivation of the section occurs later on in sparse_remove_section, so pfn_valid()->pfn_section_valid() will always return true before we deactivate the {sub}section. I spotted this during hotplug hotremove tests, there I always saw that spanned_pages was, at least, left with PAGES_PER_SECTION, even if we removed all memory linked to that zone. Fix this by decoupling section_deactivate from sparse_remove_section, and re-order the function calls. Now, __remove_section will: 1) deactivate section 2) shrink {zone,node}'s pages 3) remove section [1] https://patchwork.kernel.org/patch/11003467/ Fixes: mmotm ("mm/hotplug: prepare shrink_{zone, pgdat}_span for sub-section removal") Signed-off-by: Oscar Salvador --- include/linux/memory_hotplug.h | 7 ++-- mm/memory_hotplug.c | 6 +++- mm/sparse.c | 77 +++++++++++++++++++++++++++++------------- 3 files changed, 62 insertions(+), 28 deletions(-) diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index f46ea71b4ffd..d2eb917aad5f 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -348,9 +348,10 @@ extern void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn, extern bool is_memblock_offlined(struct memory_block *mem); extern int sparse_add_section(int nid, unsigned long pfn, unsigned long nr_pages, struct vmem_altmap *altmap); -extern void sparse_remove_section(struct mem_section *ms, - unsigned long pfn, unsigned long nr_pages, - unsigned long map_offset, struct vmem_altmap *altmap); +int sparse_deactivate_section(unsigned long pfn, unsigned long nr_pages); +void sparse_remove_section(unsigned long pfn, unsigned long nr_pages, + unsigned long map_offset, struct vmem_altmap *altmap, + int section_empty); extern struct page *sparse_decode_mem_map(unsigned long coded_mem_map, unsigned long pnum); extern bool allow_online_pfn_range(int nid, unsigned long pfn, unsigned long nr_pages, diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index b9ba5b85f9f7..03d535eee60d 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -517,12 +517,16 @@ static void __remove_section(struct zone *zone, unsigned long pfn, struct vmem_altmap *altmap) { struct mem_section *ms = __nr_to_section(pfn_to_section_nr(pfn)); + int ret; if (WARN_ON_ONCE(!valid_section(ms))) return; + ret = sparse_deactivate_section(pfn, nr_pages); __remove_zone(zone, pfn, nr_pages); - sparse_remove_section(ms, pfn, nr_pages, map_offset, altmap); + if (ret >= 0) + sparse_remove_section(pfn, nr_pages, map_offset, altmap, + ret); } /** diff --git a/mm/sparse.c b/mm/sparse.c index 1e224149aab6..d4953ee1d087 100644 --- a/mm/sparse.c +++ b/mm/sparse.c @@ -732,16 +732,47 @@ static void free_map_bootmem(struct page *memmap) } #endif /* CONFIG_SPARSEMEM_VMEMMAP */ -static void section_deactivate(unsigned long pfn, unsigned long nr_pages, - struct vmem_altmap *altmap) +static void section_remove(unsigned long pfn, unsigned long nr_pages, + struct vmem_altmap *altmap, int section_empty) +{ + struct mem_section *ms = __pfn_to_section(pfn); + bool section_early = early_section(ms); + struct page *memmap = NULL; + + if (section_empty) { + unsigned long section_nr = pfn_to_section_nr(pfn); + + if (!section_early) { + kfree(ms->usage); + ms->usage = NULL; + } + memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); + ms->section_mem_map = sparse_encode_mem_map(NULL, section_nr); + } + + if (section_early && memmap) + free_map_bootmem(memmap); + else + depopulate_section_memmap(pfn, nr_pages, altmap); +} + +/** + * section_deactivate: Deactivate a {sub}section. + * + * Return: + * * -1 - {sub}section has already been deactivated. + * * 0 - Section is not empty + * * 1 - Section is empty + */ + +static int section_deactivate(unsigned long pfn, unsigned long nr_pages) { DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 }; DECLARE_BITMAP(tmp, SUBSECTIONS_PER_SECTION) = { 0 }; struct mem_section *ms = __pfn_to_section(pfn); - bool section_is_early = early_section(ms); - struct page *memmap = NULL; unsigned long *subsection_map = ms->usage ? &ms->usage->subsection_map[0] : NULL; + int section_empty = 0; subsection_mask_set(map, pfn, nr_pages); if (subsection_map) @@ -750,7 +781,7 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages, if (WARN(!subsection_map || !bitmap_equal(tmp, map, SUBSECTIONS_PER_SECTION), "section already deactivated (%#lx + %ld)\n", pfn, nr_pages)) - return; + return -1; /* * There are 3 cases to handle across two configurations @@ -770,21 +801,10 @@ static void section_deactivate(unsigned long pfn, unsigned long nr_pages, * For 2/ and 3/ the SPARSEMEM_VMEMMAP={y,n} cases are unified */ bitmap_xor(subsection_map, map, subsection_map, SUBSECTIONS_PER_SECTION); - if (bitmap_empty(subsection_map, SUBSECTIONS_PER_SECTION)) { - unsigned long section_nr = pfn_to_section_nr(pfn); - - if (!section_is_early) { - kfree(ms->usage); - ms->usage = NULL; - } - memmap = sparse_decode_mem_map(ms->section_mem_map, section_nr); - ms->section_mem_map = sparse_encode_mem_map(NULL, section_nr); - } + if (bitmap_empty(subsection_map, SUBSECTIONS_PER_SECTION)) + section_empty = 1; - if (section_is_early && memmap) - free_map_bootmem(memmap); - else - depopulate_section_memmap(pfn, nr_pages, altmap); + return section_empty; } static struct page * __meminit section_activate(int nid, unsigned long pfn, @@ -834,7 +854,11 @@ static struct page * __meminit section_activate(int nid, unsigned long pfn, memmap = populate_section_memmap(pfn, nr_pages, nid, altmap); if (!memmap) { - section_deactivate(pfn, nr_pages, altmap); + int ret; + + ret = section_deactivate(pfn, nr_pages); + if (ret >= 0) + section_remove(pfn, nr_pages, altmap, ret); return ERR_PTR(-ENOMEM); } @@ -919,12 +943,17 @@ static inline void clear_hwpoisoned_pages(struct page *memmap, int nr_pages) } #endif -void sparse_remove_section(struct mem_section *ms, unsigned long pfn, - unsigned long nr_pages, unsigned long map_offset, - struct vmem_altmap *altmap) +int sparse_deactivate_section(unsigned long pfn, unsigned long nr_pages) +{ + return section_deactivate(pfn, nr_pages); +} + +void sparse_remove_section(unsigned long pfn, unsigned long nr_pages, + unsigned long map_offset, struct vmem_altmap *altmap, + int section_empty) { clear_hwpoisoned_pages(pfn_to_page(pfn) + map_offset, nr_pages - map_offset); - section_deactivate(pfn, nr_pages, altmap); + section_remove(pfn, nr_pages, altmap, section_empty); } #endif /* CONFIG_MEMORY_HOTPLUG */