From: David Hildenbrand <da...@redhat.com>

[ Upstream commit d9eb1417c77df7ce19abd2e41619e9dceccbdf2a ]

Patch series "mm/memory_hotplug: Better error handling when removing
memory", v1.

Error handling when removing memory is somewhat messed up right now.  Some
errors result in warnings, others are completely ignored.  Memory unplug
code can essentially not deal with errors properly as of now.
remove_memory() will never fail.

We have basically two choices:
1. Allow arch_remov_memory() and friends to fail, propagating errors via
   remove_memory(). Might be problematic (e.g. DIMMs consisting of multiple
   pieces added/removed separately).
2. Don't allow the functions to fail, handling errors in a nicer way.

It seems like most errors that can theoretically happen are really corner
cases and mostly theoretical (e.g.  "section not valid").  However e.g.
aborting removal of sections while all callers simply continue in case of
errors is not nice.

If we can gurantee that removal of memory always works (and WARN/skip in
case of theoretical errors so we can figure out what is going on), we can
go ahead and implement better error handling when adding memory.

E.g. via add_memory():

arch_add_memory()
ret = do_stuff()
if (ret) {
        arch_remove_memory();
        goto error;
}

Handling here that arch_remove_memory() might fail is basically
impossible.  So I suggest, let's avoid reporting errors while removing
memory, warning on theoretical errors instead and continuing instead of
aborting.

This patch (of 4):

__add_pages() doesn't add the memory resource, so __remove_pages()
shouldn't remove it.  Let's factor it out.  Especially as it is a special
case for memory used as system memory, added via add_memory() and friends.

We now remove the resource after removing the sections instead of doing it
the other way around.  I don't think this change is problematic.

add_memory()
        register memory resource
        arch_add_memory()

remove_memory
        arch_remove_memory()
        release memory resource

While at it, explain why we ignore errors and that it only happeny if
we remove memory in a different granularity as we added it.

[da...@redhat.com: fix printk warning]
  Link: http://lkml.kernel.org/r/20190417120204.6997-1-da...@redhat.com
Link: http://lkml.kernel.org/r/20190409100148.24703-2-da...@redhat.com
Signed-off-by: David Hildenbrand <da...@redhat.com>
Reviewed-by: Oscar Salvador <osalva...@suse.de>
Cc: Michal Hocko <mho...@suse.com>
Cc: David Hildenbrand <da...@redhat.com>
Cc: Pavel Tatashin <pasha.tatas...@soleen.com>
Cc: Wei Yang <richard.weiy...@gmail.com>
Cc: Qian Cai <c...@lca.pw>
Cc: Arun KS <aru...@codeaurora.org>
Cc: Mathieu Malaterre <ma...@debian.org>
Cc: Andrew Banman <andrew.ban...@hpe.com>
Cc: Andy Lutomirski <l...@kernel.org>
Cc: Benjamin Herrenschmidt <b...@kernel.crashing.org>
Cc: Borislav Petkov <b...@alien8.de>
Cc: Christophe Leroy <christophe.le...@c-s.fr>
Cc: Dave Hansen <dave.han...@linux.intel.com>
Cc: Fenghua Yu <fenghua...@intel.com>
Cc: Geert Uytterhoeven <ge...@linux-m68k.org>
Cc: Greg Kroah-Hartman <gre...@linuxfoundation.org>
Cc: Heiko Carstens <heiko.carst...@de.ibm.com>
Cc: "H. Peter Anvin" <h...@zytor.com>
Cc: Ingo Molnar <mi...@kernel.org>
Cc: Ingo Molnar <mi...@redhat.com>
Cc: Joonsoo Kim <iamjoonsoo....@lge.com>
Cc: "Kirill A. Shutemov" <kirill.shute...@linux.intel.com>
Cc: Martin Schwidefsky <schwidef...@de.ibm.com>
Cc: Masahiro Yamada <yamada.masah...@socionext.com>
Cc: Michael Ellerman <m...@ellerman.id.au>
Cc: Mike Rapoport <r...@linux.ibm.com>
Cc: Mike Travis <mike.tra...@hpe.com>
Cc: Nicholas Piggin <npig...@gmail.com>
Cc: Oscar Salvador <osalva...@suse.com>
Cc: Paul Mackerras <pau...@samba.org>
Cc: Peter Zijlstra <pet...@infradead.org>
Cc: "Rafael J. Wysocki" <raf...@kernel.org>
Cc: Rich Felker <dal...@libc.org>
Cc: Rob Herring <r...@kernel.org>
Cc: Stefan Agner <ste...@agner.ch>
Cc: Thomas Gleixner <t...@linutronix.de>
Cc: Tony Luck <tony.l...@intel.com>
Cc: Vasily Gorbik <g...@linux.ibm.com>
Cc: Yoshinori Sato <ys...@users.sourceforge.jp>
Signed-off-by: Andrew Morton <a...@linux-foundation.org>
Signed-off-by: Linus Torvalds <torva...@linux-foundation.org>
Signed-off-by: Sasha Levin <sas...@kernel.org>
---
 mm/memory_hotplug.c | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 7493f50ee8800..e06e7a89d0e5b 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -559,20 +559,6 @@ int __remove_pages(struct zone *zone, unsigned long 
phys_start_pfn,
        if (is_dev_zone(zone)) {
                if (altmap)
                        map_offset = vmem_altmap_offset(altmap);
-       } else {
-               resource_size_t start, size;
-
-               start = phys_start_pfn << PAGE_SHIFT;
-               size = nr_pages * PAGE_SIZE;
-
-               ret = release_mem_region_adjustable(&iomem_resource, start,
-                                       size);
-               if (ret) {
-                       resource_size_t endres = start + size - 1;
-
-                       pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
-                                       &start, &endres, ret);
-               }
        }
 
        clear_zone_contiguous(zone);
@@ -1828,6 +1814,26 @@ void try_offline_node(int nid)
 }
 EXPORT_SYMBOL(try_offline_node);
 
+static void __release_memory_resource(resource_size_t start,
+                                     resource_size_t size)
+{
+       int ret;
+
+       /*
+        * When removing memory in the same granularity as it was added,
+        * this function never fails. It might only fail if resources
+        * have to be adjusted or split. We'll ignore the error, as
+        * removing of memory cannot fail.
+        */
+       ret = release_mem_region_adjustable(&iomem_resource, start, size);
+       if (ret) {
+               resource_size_t endres = start + size - 1;
+
+               pr_warn("Unable to release resource <%pa-%pa> (%d)\n",
+                       &start, &endres, ret);
+       }
+}
+
 /**
  * remove_memory
  * @nid: the node ID
@@ -1862,6 +1868,7 @@ void __ref __remove_memory(int nid, u64 start, u64 size)
        memblock_remove(start, size);
 
        arch_remove_memory(nid, start, size, NULL);
+       __release_memory_resource(start, size);
 
        try_offline_node(nid);
 
-- 
2.20.1

Reply via email to