drivers: base: use standard device online/offline for state change
[cascardo/linux.git] / drivers / base / memory.c
index 0187fe4..2a38cd2 100644 (file)
@@ -16,7 +16,6 @@
 #include <linux/capability.h>
 #include <linux/device.h>
 #include <linux/memory.h>
-#include <linux/kobject.h>
 #include <linux/memory_hotplug.h>
 #include <linux/mm.h>
 #include <linux/mutex.h>
@@ -261,9 +260,8 @@ memory_block_action(unsigned long phys_index, unsigned long action, int online_t
        return ret;
 }
 
-static int __memory_block_change_state(struct memory_block *mem,
-               unsigned long to_state, unsigned long from_state_req,
-               int online_type)
+static int memory_block_change_state(struct memory_block *mem,
+               unsigned long to_state, unsigned long from_state_req)
 {
        int ret = 0;
 
@@ -273,105 +271,91 @@ static int __memory_block_change_state(struct memory_block *mem,
        if (to_state == MEM_OFFLINE)
                mem->state = MEM_GOING_OFFLINE;
 
-       ret = memory_block_action(mem->start_section_nr, to_state, online_type);
+       ret = memory_block_action(mem->start_section_nr, to_state,
+                               mem->online_type);
+
        mem->state = ret ? from_state_req : to_state;
+
        return ret;
 }
 
+/* The device lock serializes operations on memory_subsys_[online|offline] */
 static int memory_subsys_online(struct device *dev)
 {
        struct memory_block *mem = container_of(dev, struct memory_block, dev);
        int ret;
 
-       mutex_lock(&mem->state_mutex);
+       if (mem->state == MEM_ONLINE)
+               return 0;
 
-       ret = mem->state == MEM_ONLINE ? 0 :
-               __memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE,
-                                           ONLINE_KEEP);
+       /*
+        * If we are called from store_mem_state(), online_type will be
+        * set >= 0 Otherwise we were called from the device online
+        * attribute and need to set the online_type.
+        */
+       if (mem->online_type < 0)
+               mem->online_type = ONLINE_KEEP;
+
+       ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
+
+       /* clear online_type */
+       mem->online_type = -1;
 
-       mutex_unlock(&mem->state_mutex);
        return ret;
 }
 
 static int memory_subsys_offline(struct device *dev)
 {
        struct memory_block *mem = container_of(dev, struct memory_block, dev);
-       int ret;
 
-       mutex_lock(&mem->state_mutex);
-
-       ret = mem->state == MEM_OFFLINE ? 0 :
-               __memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE, -1);
-
-       mutex_unlock(&mem->state_mutex);
-       return ret;
-}
+       if (mem->state == MEM_OFFLINE)
+               return 0;
 
-static int __memory_block_change_state_uevent(struct memory_block *mem,
-               unsigned long to_state, unsigned long from_state_req,
-               int online_type)
-{
-       int ret = __memory_block_change_state(mem, to_state, from_state_req,
-                                             online_type);
-       if (!ret) {
-               switch (mem->state) {
-               case MEM_OFFLINE:
-                       kobject_uevent(&mem->dev.kobj, KOBJ_OFFLINE);
-                       break;
-               case MEM_ONLINE:
-                       kobject_uevent(&mem->dev.kobj, KOBJ_ONLINE);
-                       break;
-               default:
-                       break;
-               }
-       }
-       return ret;
+       return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
 }
 
-static int memory_block_change_state(struct memory_block *mem,
-               unsigned long to_state, unsigned long from_state_req,
-               int online_type)
-{
-       int ret;
-
-       mutex_lock(&mem->state_mutex);
-       ret = __memory_block_change_state_uevent(mem, to_state, from_state_req,
-                                                online_type);
-       mutex_unlock(&mem->state_mutex);
-
-       return ret;
-}
 static ssize_t
 store_mem_state(struct device *dev,
                struct device_attribute *attr, const char *buf, size_t count)
 {
        struct memory_block *mem;
-       bool offline;
-       int ret = -EINVAL;
+       int ret, online_type;
 
        mem = container_of(dev, struct memory_block, dev);
 
        lock_device_hotplug();
 
-       if (!strncmp(buf, "online_kernel", min_t(int, count, 13))) {
-               offline = false;
-               ret = memory_block_change_state(mem, MEM_ONLINE,
-                                               MEM_OFFLINE, ONLINE_KERNEL);
-       } else if (!strncmp(buf, "online_movable", min_t(int, count, 14))) {
-               offline = false;
-               ret = memory_block_change_state(mem, MEM_ONLINE,
-                                               MEM_OFFLINE, ONLINE_MOVABLE);
-       } else if (!strncmp(buf, "online", min_t(int, count, 6))) {
-               offline = false;
-               ret = memory_block_change_state(mem, MEM_ONLINE,
-                                               MEM_OFFLINE, ONLINE_KEEP);
-       } else if(!strncmp(buf, "offline", min_t(int, count, 7))) {
-               offline = true;
-               ret = memory_block_change_state(mem, MEM_OFFLINE,
-                                               MEM_ONLINE, -1);
+       if (!strncmp(buf, "online_kernel", min_t(int, count, 13)))
+               online_type = ONLINE_KERNEL;
+       else if (!strncmp(buf, "online_movable", min_t(int, count, 14)))
+               online_type = ONLINE_MOVABLE;
+       else if (!strncmp(buf, "online", min_t(int, count, 6)))
+               online_type = ONLINE_KEEP;
+       else if (!strncmp(buf, "offline", min_t(int, count, 7)))
+               online_type = -1;
+       else
+               return -EINVAL;
+
+       switch (online_type) {
+       case ONLINE_KERNEL:
+       case ONLINE_MOVABLE:
+       case 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;
+       case -1:
+               ret = device_offline(&mem->dev);
+               break;
+       default:
+               ret = -EINVAL; /* should never happen */
        }
-       if (!ret)
-               dev->offline = offline;
 
        unlock_device_hotplug();
 
@@ -592,7 +576,6 @@ static int init_memory_block(struct memory_block **memory,
        mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
        mem->state = state;
        mem->section_count++;
-       mutex_init(&mem->state_mutex);
        start_pfn = section_nr_to_pfn(mem->start_section_nr);
        mem->phys_device = arch_get_memory_phys_device(start_pfn);