Enable external callers to select how to online the memory rather than
implicitly depending on the system defauilt.

Refactor: Extract __add_memory_resource to take an explicit online type,
and update add_memory_resource to pass the system default.

Export mhp_get_default_online_type() and update existing callers of
add_memory_driver_managed to use it explicitly to make it clear what the
behavior of the function is.

dax_kmem and virtio_mem drivers were updated.

Signed-off-by: Gregory Price <[email protected]>
---
 drivers/dax/kmem.c             |  3 ++-
 drivers/virtio/virtio_mem.c    |  3 ++-
 include/linux/memory_hotplug.h |  2 +-
 mm/memory_hotplug.c            | 31 +++++++++++++++++++++++--------
 4 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c
index c036e4d0b610..bb13d9ced2e9 100644
--- a/drivers/dax/kmem.c
+++ b/drivers/dax/kmem.c
@@ -175,7 +175,8 @@ static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
                 * this as RAM automatically.
                 */
                rc = add_memory_driver_managed(data->mgid, range.start,
-                               range_len(&range), kmem_name, mhp_flags);
+                               range_len(&range), kmem_name, mhp_flags,
+                               mhp_get_default_online_type());
 
                if (rc) {
                        dev_warn(dev, "mapping%d: %#llx-%#llx memory add 
failed\n",
diff --git a/drivers/virtio/virtio_mem.c b/drivers/virtio/virtio_mem.c
index 1688ecd69a04..63c0b2b235ab 100644
--- a/drivers/virtio/virtio_mem.c
+++ b/drivers/virtio/virtio_mem.c
@@ -654,7 +654,8 @@ static int virtio_mem_add_memory(struct virtio_mem *vm, 
uint64_t addr,
        /* Memory might get onlined immediately. */
        atomic64_add(size, &vm->offline_size);
        rc = add_memory_driver_managed(vm->mgid, addr, size, vm->resource_name,
-                                      MHP_MERGE_RESOURCE | MHP_NID_IS_MGID);
+                                      MHP_MERGE_RESOURCE | MHP_NID_IS_MGID,
+                                      mhp_get_default_online_type());
        if (rc) {
                atomic64_sub(size, &vm->offline_size);
                dev_warn(&vm->vdev->dev, "adding memory failed: %d\n", rc);
diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h
index f2f16cdd73ee..b68bc410db67 100644
--- a/include/linux/memory_hotplug.h
+++ b/include/linux/memory_hotplug.h
@@ -295,7 +295,7 @@ extern int add_memory_resource(int nid, struct resource 
*resource,
                               mhp_t mhp_flags);
 extern int add_memory_driver_managed(int nid, u64 start, u64 size,
                                     const char *resource_name,
-                                    mhp_t mhp_flags);
+                                    mhp_t mhp_flags, int online_type);
 extern void move_pfn_range_to_zone(struct zone *zone, unsigned long start_pfn,
                                   unsigned long nr_pages,
                                   struct vmem_altmap *altmap, int migratetype,
diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c
index 5718556121f0..2b4e31161fc1 100644
--- a/mm/memory_hotplug.c
+++ b/mm/memory_hotplug.c
@@ -239,6 +239,7 @@ int mhp_get_default_online_type(void)
 
        return mhp_default_online_type;
 }
+EXPORT_SYMBOL_GPL(mhp_get_default_online_type);
 
 void mhp_set_default_online_type(int online_type)
 {
@@ -1490,7 +1491,8 @@ static int create_altmaps_and_memory_blocks(int nid, 
struct memory_group *group,
  *
  * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG
  */
-int add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
+static int __add_memory_resource(int nid, struct resource *res, mhp_t 
mhp_flags,
+                                int online_type)
 {
        struct mhp_params params = { .pgprot = pgprot_mhp(PAGE_KERNEL) };
        enum memblock_flags memblock_flags = MEMBLOCK_NONE;
@@ -1580,12 +1582,9 @@ int add_memory_resource(int nid, struct resource *res, 
mhp_t mhp_flags)
                merge_system_ram_resource(res);
 
        /* online pages if requested */
-       if (mhp_get_default_online_type() != MMOP_OFFLINE) {
-               int online_type = mhp_get_default_online_type();
-
+       if (online_type != MMOP_OFFLINE)
                walk_memory_blocks(start, size, &online_type,
                                   online_memory_block);
-       }
 
        return ret;
 error:
@@ -1601,7 +1600,13 @@ int add_memory_resource(int nid, struct resource *res, 
mhp_t mhp_flags)
        return ret;
 }
 
-/* requires device_hotplug_lock, see add_memory_resource() */
+int add_memory_resource(int nid, struct resource *res, mhp_t mhp_flags)
+{
+       return __add_memory_resource(nid, res, mhp_flags,
+                                    mhp_get_default_online_type());
+}
+
+/* requires device_hotplug_lock, see __add_memory_resource() */
 int __add_memory(int nid, u64 start, u64 size, mhp_t mhp_flags)
 {
        struct resource *res;
@@ -1649,9 +1654,16 @@ EXPORT_SYMBOL_GPL(add_memory);
  *
  * The resource_name (visible via /proc/iomem) has to have the format
  * "System RAM ($DRIVER)".
+ *
+ * @online_type specifies the online behavior: MMOP_ONLINE, MMOP_ONLINE_KERNEL,
+ * MMOP_ONLINE_MOVABLE to online with that type, MMOP_OFFLINE to leave offline.
+ * Users that want the system default should call 
mhp_get_default_online_type().
+ *
+ * Returns 0 on success, negative error code on failure.
  */
 int add_memory_driver_managed(int nid, u64 start, u64 size,
-                             const char *resource_name, mhp_t mhp_flags)
+                             const char *resource_name, mhp_t mhp_flags,
+                             int online_type)
 {
        struct resource *res;
        int rc;
@@ -1661,6 +1673,9 @@ int add_memory_driver_managed(int nid, u64 start, u64 
size,
            resource_name[strlen(resource_name) - 1] != ')')
                return -EINVAL;
 
+       if (online_type < 0 || online_type > MMOP_ONLINE_MOVABLE)
+               return -EINVAL;
+
        lock_device_hotplug();
 
        res = register_memory_resource(start, size, resource_name);
@@ -1669,7 +1684,7 @@ int add_memory_driver_managed(int nid, u64 start, u64 
size,
                goto out_unlock;
        }
 
-       rc = add_memory_resource(nid, res, mhp_flags);
+       rc = __add_memory_resource(nid, res, mhp_flags, online_type);
        if (rc < 0)
                release_memory_resource(res);
 
-- 
2.52.0


Reply via email to