/* * online, offline, going offline, etc. */ static ssize_t show_mem_state(struct device *dev, struct device_attribute *attr, char *buf) { struct memory_block *mem = to_memory_block(dev); ssize_t len = 0; /* * We can probably put these states in a nice little array * so that they're not open-coded */ switch (mem->state) { case MEM_ONLINE: len = sprintf(buf, "online\n"); break; case MEM_OFFLINE: len = sprintf(buf, "offline\n"); break; case MEM_GOING_OFFLINE: len = sprintf(buf, "going-offline\n"); break; default: len = sprintf(buf, "ERROR-UNKNOWN-%ld\n", mem->state); WARN_ON(1); break; } return len; }
static ssize_t show_valid_zones(struct device *dev, struct device_attribute *attr, char *buf) { struct memory_block *mem = to_memory_block(dev); unsigned long start_pfn, end_pfn; unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; struct page *first_page; struct zone *zone; start_pfn = section_nr_to_pfn(mem->start_section_nr); end_pfn = start_pfn + nr_pages; first_page = pfn_to_page(start_pfn); /* The block contains more than one zone can not be offlined. */ if (!test_pages_in_a_zone(start_pfn, end_pfn)) return sprintf(buf, "none\n"); zone = page_zone(first_page); if (zone_idx(zone) == ZONE_MOVABLE - 1) { /*The mem block is the last memoryblock of this zone.*/ if (end_pfn == zone_end_pfn(zone)) return sprintf(buf, "%s %s\n", zone->name, (zone + 1)->name); } if (zone_idx(zone) == ZONE_MOVABLE) { /*The mem block is the first memoryblock of ZONE_MOVABLE.*/ if (start_pfn == zone->zone_start_pfn) return sprintf(buf, "%s %s\n", zone->name, (zone - 1)->name); } return sprintf(buf, "%s\n", zone->name); }
static ssize_t show_mem_start_phys_index(struct device *dev, struct device_attribute *attr, char *buf) { struct memory_block *mem = to_memory_block(dev); unsigned long phys_index; phys_index = mem->start_section_nr / sections_per_block; return sprintf(buf, "%08lx\n", phys_index); }
static int memory_subsys_offline(struct device *dev) { struct memory_block *mem = to_memory_block(dev); if (mem->state == MEM_OFFLINE) return 0; return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE); }
static ssize_t store_mem_state(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct memory_block *mem = to_memory_block(dev); int ret, online_type; ret = lock_device_hotplug_sysfs(); if (ret) return ret; if (sysfs_streq(buf, "online_kernel")) online_type = MMOP_ONLINE_KERNEL; else if (sysfs_streq(buf, "online_movable")) online_type = MMOP_ONLINE_MOVABLE; else if (sysfs_streq(buf, "online")) online_type = MMOP_ONLINE_KEEP; else if (sysfs_streq(buf, "offline")) online_type = MMOP_OFFLINE; else { ret = -EINVAL; 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 = online_type; ret = device_online(&mem->dev); break; case MMOP_OFFLINE: ret = device_offline(&mem->dev); break; default: ret = -EINVAL; /* should never happen */ } mem_hotplug_done(); err: unlock_device_hotplug(); if (ret) return ret; return count; }
static ssize_t store_mem_state(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct memory_block *mem = to_memory_block(dev); int ret, online_type; ret = lock_device_hotplug_sysfs(); if (ret) return ret; if (sysfs_streq(buf, "online_kernel")) online_type = MMOP_ONLINE_KERNEL; else if (sysfs_streq(buf, "online_movable")) online_type = MMOP_ONLINE_MOVABLE; else if (sysfs_streq(buf, "online")) online_type = MMOP_ONLINE_KEEP; else if (sysfs_streq(buf, "offline")) online_type = MMOP_OFFLINE; else { ret = -EINVAL; goto err; } 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; case MMOP_OFFLINE: ret = device_offline(&mem->dev); break; default: ret = -EINVAL; /* should never happen */ } err: unlock_device_hotplug(); if (ret) return ret; return count; }
static int memory_subsys_offline(struct device *dev) { struct memory_block *mem = to_memory_block(dev); if (mem->state == MEM_OFFLINE) return 0; /* Can't offline block with non-present sections */ if (mem->section_count != sections_per_block) return -EINVAL; return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE); }
/* * A reference for the returned object is held and the reference for the * hinted object is released. */ struct memory_block *find_memory_block_hinted(struct mem_section *section, struct memory_block *hint) { int block_id = base_memory_block_id(__section_nr(section)); struct device *hintdev = hint ? &hint->dev : NULL; struct device *dev; dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev); if (hint) put_device(&hint->dev); if (!dev) return NULL; return to_memory_block(dev); }
/* * Show whether the section of memory is likely to be hot-removable */ static ssize_t show_mem_removable(struct device *dev, struct device_attribute *attr, char *buf) { unsigned long i, pfn; int ret = 1; struct memory_block *mem = to_memory_block(dev); for (i = 0; i < sections_per_block; i++) { if (!present_section_nr(mem->start_section_nr + i)) continue; pfn = section_nr_to_pfn(mem->start_section_nr + i); ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION); } return sprintf(buf, "%d\n", ret); }
static ssize_t show_valid_zones(struct device *dev, struct device_attribute *attr, char *buf) { struct memory_block *mem = to_memory_block(dev); unsigned long start_pfn, end_pfn; unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block; struct page *first_page; struct zone *zone; int zone_shift = 0; start_pfn = section_nr_to_pfn(mem->start_section_nr); end_pfn = start_pfn + nr_pages; first_page = pfn_to_page(start_pfn); /* The block contains more than one zone can not be offlined. */ if (!test_pages_in_a_zone(start_pfn, end_pfn)) return sprintf(buf, "none\n"); zone = page_zone(first_page); /* MMOP_ONLINE_KEEP */ sprintf(buf, "%s", zone->name); /* MMOP_ONLINE_KERNEL */ zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_NORMAL); if (zone_shift) { strcat(buf, " "); strcat(buf, (zone + zone_shift)->name); } /* MMOP_ONLINE_MOVABLE */ zone_shift = zone_can_shift(start_pfn, nr_pages, ZONE_MOVABLE); if (zone_shift) { strcat(buf, " "); strcat(buf, (zone + zone_shift)->name); } strcat(buf, "\n"); return strlen(buf); }
/* The device lock serializes operations on memory_subsys_[online|offline] */ static int memory_subsys_online(struct device *dev) { struct memory_block *mem = to_memory_block(dev); int ret; if (mem->state == MEM_ONLINE) return 0; /* * 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 = MMOP_ONLINE_KEEP; ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE); /* clear online_type */ mem->online_type = -1; return ret; }
static void memory_block_release(struct device *dev) { struct memory_block *mem = to_memory_block(dev); kfree(mem); }
/* * phys_device is a bad name for this. What I really want * is a way to differentiate between memory ranges that * are part of physical devices that constitute * a complete removable unit or fru. * i.e. do these ranges belong to the same physical device, * s.t. if I offline all of these sections I can then * remove the physical device? */ static ssize_t show_phys_device(struct device *dev, struct device_attribute *attr, char *buf) { struct memory_block *mem = to_memory_block(dev); return sprintf(buf, "%d\n", mem->phys_device); }