/*
  * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
  * OK to have direct references to sparsemem variables in here.
+ * Must already be protected by mem_hotplug_begin().
  */
 static int
 memory_block_action(unsigned long phys_index, unsigned long action, int online_type)
        if (mem->online_type < 0)
                mem->online_type = MMOP_ONLINE_KEEP;
 
+       /* Already under protection of mem_hotplug_begin() */
        ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
 
        /* clear online_type */
                goto err;
        }
 
+       /*
+        * Memory hotplug needs to hold mem_hotplug_begin() for probe to find
+        * the correct memory block to online before doing device_online(dev),
+        * which will take dev->mutex.  Take the lock early to prevent an
+        * inversion, memory_subsys_online() callbacks will be implemented by
+        * assuming it's already protected.
+        */
+       mem_hotplug_begin();
+
        switch (online_type) {
        case MMOP_ONLINE_KERNEL:
        case MMOP_ONLINE_MOVABLE:
        case MMOP_ONLINE_KEEP:
-               /*
-                * mem->online_type is not protected so there can be a
-                * race here.  However, when racing online, the first
-                * will succeed and the second will just return as the
-                * block will already be online.  The online type
-                * could be either one, but that is expected.
-                */
                mem->online_type = online_type;
                ret = device_online(&mem->dev);
                break;
                ret = -EINVAL; /* should never happen */
        }
 
+       mem_hotplug_done();
 err:
        unlock_device_hotplug();
 
 
 void get_online_mems(void);
 void put_online_mems(void);
 
+void mem_hotplug_begin(void);
+void mem_hotplug_done(void);
+
 #else /* ! CONFIG_MEMORY_HOTPLUG */
 /*
  * Stub functions for when hotplug is off
 static inline void get_online_mems(void) {}
 static inline void put_online_mems(void) {}
 
+static inline void mem_hotplug_begin(void) {}
+static inline void mem_hotplug_done(void) {}
+
 #endif /* ! CONFIG_MEMORY_HOTPLUG */
 
 #ifdef CONFIG_MEMORY_HOTREMOVE
 
 
 }
 
-static void mem_hotplug_begin(void)
+void mem_hotplug_begin(void)
 {
        mem_hotplug.active_writer = current;
 
        }
 }
 
-static void mem_hotplug_done(void)
+void mem_hotplug_done(void)
 {
        mem_hotplug.active_writer = NULL;
        mutex_unlock(&mem_hotplug.lock);
 }
 
 
+/* Must be protected by mem_hotplug_begin() */
 int __ref online_pages(unsigned long pfn, unsigned long nr_pages, int online_type)
 {
        unsigned long flags;
        int ret;
        struct memory_notify arg;
 
-       mem_hotplug_begin();
        /*
         * This doesn't need a lock to do pfn_to_page().
         * The section can't be removed here because of the
         */
        zone = page_zone(pfn_to_page(pfn));
 
-       ret = -EINVAL;
        if ((zone_idx(zone) > ZONE_NORMAL ||
            online_type == MMOP_ONLINE_MOVABLE) &&
            !can_online_high_movable(zone))
-               goto out;
+               return -EINVAL;
 
        if (online_type == MMOP_ONLINE_KERNEL &&
            zone_idx(zone) == ZONE_MOVABLE) {
                if (move_pfn_range_left(zone - 1, zone, pfn, pfn + nr_pages))
-                       goto out;
+                       return -EINVAL;
        }
        if (online_type == MMOP_ONLINE_MOVABLE &&
            zone_idx(zone) == ZONE_MOVABLE - 1) {
                if (move_pfn_range_right(zone, zone + 1, pfn, pfn + nr_pages))
-                       goto out;
+                       return -EINVAL;
        }
 
        /* Previous code may changed the zone of the pfn range */
        ret = notifier_to_errno(ret);
        if (ret) {
                memory_notify(MEM_CANCEL_ONLINE, &arg);
-               goto out;
+               return ret;
        }
        /*
         * If this zone is not populated, then it is not in zonelist.
                       (((unsigned long long) pfn + nr_pages)
                            << PAGE_SHIFT) - 1);
                memory_notify(MEM_CANCEL_ONLINE, &arg);
-               goto out;
+               return ret;
        }
 
        zone->present_pages += onlined_pages;
 
        if (onlined_pages)
                memory_notify(MEM_ONLINE, &arg);
-out:
-       mem_hotplug_done();
-       return ret;
+       return 0;
 }
 #endif /* CONFIG_MEMORY_HOTPLUG_SPARSE */
 
        if (!test_pages_in_a_zone(start_pfn, end_pfn))
                return -EINVAL;
 
-       mem_hotplug_begin();
-
        zone = page_zone(pfn_to_page(start_pfn));
        node = zone_to_nid(zone);
        nr_pages = end_pfn - start_pfn;
 
-       ret = -EINVAL;
        if (zone_idx(zone) <= ZONE_NORMAL && !can_offline_normal(zone, nr_pages))
-               goto out;
+               return -EINVAL;
 
        /* set above range as isolated */
        ret = start_isolate_page_range(start_pfn, end_pfn,
                                       MIGRATE_MOVABLE, true);
        if (ret)
-               goto out;
+               return ret;
 
        arg.start_pfn = start_pfn;
        arg.nr_pages = nr_pages;
        writeback_set_ratelimit();
 
        memory_notify(MEM_OFFLINE, &arg);
-       mem_hotplug_done();
        return 0;
 
 failed_removal:
        memory_notify(MEM_CANCEL_OFFLINE, &arg);
        /* pushback to free area */
        undo_isolate_page_range(start_pfn, end_pfn, MIGRATE_MOVABLE);
-
-out:
-       mem_hotplug_done();
        return ret;
 }
 
+/* Must be protected by mem_hotplug_begin() */
 int offline_pages(unsigned long start_pfn, unsigned long nr_pages)
 {
        return __offline_pages(start_pfn, start_pfn + nr_pages, 120 * HZ);