gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / base / memory.c
blobdbec3a05590a5163da41494e2b434808688afbe7
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Memory subsystem support
5 * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
6 * Dave Hansen <haveblue@us.ibm.com>
8 * This file provides the necessary infrastructure to represent
9 * a SPARSEMEM-memory-model system's physical memory in /sysfs.
10 * All arch-independent code that assumes MEMORY_HOTPLUG requires
11 * SPARSEMEM should be contained here, or in mm/memory_hotplug.c.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/topology.h>
17 #include <linux/capability.h>
18 #include <linux/device.h>
19 #include <linux/memory.h>
20 #include <linux/memory_hotplug.h>
21 #include <linux/mm.h>
22 #include <linux/stat.h>
23 #include <linux/slab.h>
25 #include <linux/atomic.h>
26 #include <linux/uaccess.h>
28 #define MEMORY_CLASS_NAME "memory"
30 static const char *const online_type_to_str[] = {
31 [MMOP_OFFLINE] = "offline",
32 [MMOP_ONLINE] = "online",
33 [MMOP_ONLINE_KERNEL] = "online_kernel",
34 [MMOP_ONLINE_MOVABLE] = "online_movable",
37 int memhp_online_type_from_str(const char *str)
39 int i;
41 for (i = 0; i < ARRAY_SIZE(online_type_to_str); i++) {
42 if (sysfs_streq(str, online_type_to_str[i]))
43 return i;
45 return -EINVAL;
48 #define to_memory_block(dev) container_of(dev, struct memory_block, dev)
50 static int sections_per_block;
52 static inline unsigned long base_memory_block_id(unsigned long section_nr)
54 return section_nr / sections_per_block;
57 static inline unsigned long pfn_to_block_id(unsigned long pfn)
59 return base_memory_block_id(pfn_to_section_nr(pfn));
62 static inline unsigned long phys_to_block_id(unsigned long phys)
64 return pfn_to_block_id(PFN_DOWN(phys));
67 static int memory_subsys_online(struct device *dev);
68 static int memory_subsys_offline(struct device *dev);
70 static struct bus_type memory_subsys = {
71 .name = MEMORY_CLASS_NAME,
72 .dev_name = MEMORY_CLASS_NAME,
73 .online = memory_subsys_online,
74 .offline = memory_subsys_offline,
77 static BLOCKING_NOTIFIER_HEAD(memory_chain);
79 int register_memory_notifier(struct notifier_block *nb)
81 return blocking_notifier_chain_register(&memory_chain, nb);
83 EXPORT_SYMBOL(register_memory_notifier);
85 void unregister_memory_notifier(struct notifier_block *nb)
87 blocking_notifier_chain_unregister(&memory_chain, nb);
89 EXPORT_SYMBOL(unregister_memory_notifier);
91 static void memory_block_release(struct device *dev)
93 struct memory_block *mem = to_memory_block(dev);
95 kfree(mem);
98 unsigned long __weak memory_block_size_bytes(void)
100 return MIN_MEMORY_BLOCK_SIZE;
102 EXPORT_SYMBOL_GPL(memory_block_size_bytes);
105 * Show the first physical section index (number) of this memory block.
107 static ssize_t phys_index_show(struct device *dev,
108 struct device_attribute *attr, char *buf)
110 struct memory_block *mem = to_memory_block(dev);
111 unsigned long phys_index;
113 phys_index = mem->start_section_nr / sections_per_block;
114 return sprintf(buf, "%08lx\n", phys_index);
118 * Legacy interface that we cannot remove. Always indicate "removable"
119 * with CONFIG_MEMORY_HOTREMOVE - bad heuristic.
121 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
122 char *buf)
124 return sprintf(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE));
128 * online, offline, going offline, etc.
130 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
131 char *buf)
133 struct memory_block *mem = to_memory_block(dev);
134 ssize_t len = 0;
137 * We can probably put these states in a nice little array
138 * so that they're not open-coded
140 switch (mem->state) {
141 case MEM_ONLINE:
142 len = sprintf(buf, "online\n");
143 break;
144 case MEM_OFFLINE:
145 len = sprintf(buf, "offline\n");
146 break;
147 case MEM_GOING_OFFLINE:
148 len = sprintf(buf, "going-offline\n");
149 break;
150 default:
151 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
152 mem->state);
153 WARN_ON(1);
154 break;
157 return len;
160 int memory_notify(unsigned long val, void *v)
162 return blocking_notifier_call_chain(&memory_chain, val, v);
166 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
167 * OK to have direct references to sparsemem variables in here.
169 static int
170 memory_block_action(unsigned long start_section_nr, unsigned long action,
171 int online_type, int nid)
173 unsigned long start_pfn;
174 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
175 int ret;
177 start_pfn = section_nr_to_pfn(start_section_nr);
179 switch (action) {
180 case MEM_ONLINE:
181 ret = online_pages(start_pfn, nr_pages, online_type, nid);
182 break;
183 case MEM_OFFLINE:
184 ret = offline_pages(start_pfn, nr_pages);
185 break;
186 default:
187 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
188 "%ld\n", __func__, start_section_nr, action, action);
189 ret = -EINVAL;
192 return ret;
195 static int memory_block_change_state(struct memory_block *mem,
196 unsigned long to_state, unsigned long from_state_req)
198 int ret = 0;
200 if (mem->state != from_state_req)
201 return -EINVAL;
203 if (to_state == MEM_OFFLINE)
204 mem->state = MEM_GOING_OFFLINE;
206 ret = memory_block_action(mem->start_section_nr, to_state,
207 mem->online_type, mem->nid);
209 mem->state = ret ? from_state_req : to_state;
211 return ret;
214 /* The device lock serializes operations on memory_subsys_[online|offline] */
215 static int memory_subsys_online(struct device *dev)
217 struct memory_block *mem = to_memory_block(dev);
218 int ret;
220 if (mem->state == MEM_ONLINE)
221 return 0;
224 * When called via device_online() without configuring the online_type,
225 * we want to default to MMOP_ONLINE.
227 if (mem->online_type == MMOP_OFFLINE)
228 mem->online_type = MMOP_ONLINE;
230 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
231 mem->online_type = MMOP_OFFLINE;
233 return ret;
236 static int memory_subsys_offline(struct device *dev)
238 struct memory_block *mem = to_memory_block(dev);
240 if (mem->state == MEM_OFFLINE)
241 return 0;
243 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
246 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
247 const char *buf, size_t count)
249 const int online_type = memhp_online_type_from_str(buf);
250 struct memory_block *mem = to_memory_block(dev);
251 int ret;
253 if (online_type < 0)
254 return -EINVAL;
256 ret = lock_device_hotplug_sysfs();
257 if (ret)
258 return ret;
260 switch (online_type) {
261 case MMOP_ONLINE_KERNEL:
262 case MMOP_ONLINE_MOVABLE:
263 case MMOP_ONLINE:
264 /* mem->online_type is protected by device_hotplug_lock */
265 mem->online_type = online_type;
266 ret = device_online(&mem->dev);
267 break;
268 case MMOP_OFFLINE:
269 ret = device_offline(&mem->dev);
270 break;
271 default:
272 ret = -EINVAL; /* should never happen */
275 unlock_device_hotplug();
277 if (ret < 0)
278 return ret;
279 if (ret)
280 return -EINVAL;
282 return count;
286 * phys_device is a bad name for this. What I really want
287 * is a way to differentiate between memory ranges that
288 * are part of physical devices that constitute
289 * a complete removable unit or fru.
290 * i.e. do these ranges belong to the same physical device,
291 * s.t. if I offline all of these sections I can then
292 * remove the physical device?
294 static ssize_t phys_device_show(struct device *dev,
295 struct device_attribute *attr, char *buf)
297 struct memory_block *mem = to_memory_block(dev);
298 return sprintf(buf, "%d\n", mem->phys_device);
301 #ifdef CONFIG_MEMORY_HOTREMOVE
302 static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
303 unsigned long nr_pages, int online_type,
304 struct zone *default_zone)
306 struct zone *zone;
308 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
309 if (zone != default_zone) {
310 strcat(buf, " ");
311 strcat(buf, zone->name);
315 static ssize_t valid_zones_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct memory_block *mem = to_memory_block(dev);
319 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
320 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
321 struct zone *default_zone;
322 int nid;
325 * Check the existing zone. Make sure that we do that only on the
326 * online nodes otherwise the page_zone is not reliable
328 if (mem->state == MEM_ONLINE) {
330 * The block contains more than one zone can not be offlined.
331 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
333 default_zone = test_pages_in_a_zone(start_pfn,
334 start_pfn + nr_pages);
335 if (!default_zone)
336 return sprintf(buf, "none\n");
337 strcat(buf, default_zone->name);
338 goto out;
341 nid = mem->nid;
342 default_zone = zone_for_pfn_range(MMOP_ONLINE, nid, start_pfn,
343 nr_pages);
344 strcat(buf, default_zone->name);
346 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
347 default_zone);
348 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
349 default_zone);
350 out:
351 strcat(buf, "\n");
353 return strlen(buf);
355 static DEVICE_ATTR_RO(valid_zones);
356 #endif
358 static DEVICE_ATTR_RO(phys_index);
359 static DEVICE_ATTR_RW(state);
360 static DEVICE_ATTR_RO(phys_device);
361 static DEVICE_ATTR_RO(removable);
364 * Show the memory block size (shared by all memory blocks).
366 static ssize_t block_size_bytes_show(struct device *dev,
367 struct device_attribute *attr, char *buf)
369 return sprintf(buf, "%lx\n", memory_block_size_bytes());
372 static DEVICE_ATTR_RO(block_size_bytes);
375 * Memory auto online policy.
378 static ssize_t auto_online_blocks_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
381 return sprintf(buf, "%s\n",
382 online_type_to_str[memhp_default_online_type]);
385 static ssize_t auto_online_blocks_store(struct device *dev,
386 struct device_attribute *attr,
387 const char *buf, size_t count)
389 const int online_type = memhp_online_type_from_str(buf);
391 if (online_type < 0)
392 return -EINVAL;
394 memhp_default_online_type = online_type;
395 return count;
398 static DEVICE_ATTR_RW(auto_online_blocks);
401 * Some architectures will have custom drivers to do this, and
402 * will not need to do it from userspace. The fake hot-add code
403 * as well as ppc64 will do all of their discovery in userspace
404 * and will require this interface.
406 #ifdef CONFIG_ARCH_MEMORY_PROBE
407 static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
408 const char *buf, size_t count)
410 u64 phys_addr;
411 int nid, ret;
412 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
414 ret = kstrtoull(buf, 0, &phys_addr);
415 if (ret)
416 return ret;
418 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
419 return -EINVAL;
421 ret = lock_device_hotplug_sysfs();
422 if (ret)
423 return ret;
425 nid = memory_add_physaddr_to_nid(phys_addr);
426 ret = __add_memory(nid, phys_addr,
427 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
429 if (ret)
430 goto out;
432 ret = count;
433 out:
434 unlock_device_hotplug();
435 return ret;
438 static DEVICE_ATTR_WO(probe);
439 #endif
441 #ifdef CONFIG_MEMORY_FAILURE
443 * Support for offlining pages of memory
446 /* Soft offline a page */
447 static ssize_t soft_offline_page_store(struct device *dev,
448 struct device_attribute *attr,
449 const char *buf, size_t count)
451 int ret;
452 u64 pfn;
453 if (!capable(CAP_SYS_ADMIN))
454 return -EPERM;
455 if (kstrtoull(buf, 0, &pfn) < 0)
456 return -EINVAL;
457 pfn >>= PAGE_SHIFT;
458 ret = soft_offline_page(pfn, 0);
459 return ret == 0 ? count : ret;
462 /* Forcibly offline a page, including killing processes. */
463 static ssize_t hard_offline_page_store(struct device *dev,
464 struct device_attribute *attr,
465 const char *buf, size_t count)
467 int ret;
468 u64 pfn;
469 if (!capable(CAP_SYS_ADMIN))
470 return -EPERM;
471 if (kstrtoull(buf, 0, &pfn) < 0)
472 return -EINVAL;
473 pfn >>= PAGE_SHIFT;
474 ret = memory_failure(pfn, 0);
475 return ret ? ret : count;
478 static DEVICE_ATTR_WO(soft_offline_page);
479 static DEVICE_ATTR_WO(hard_offline_page);
480 #endif
483 * Note that phys_device is optional. It is here to allow for
484 * differentiation between which *physical* devices each
485 * section belongs to...
487 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
489 return 0;
492 /* A reference for the returned memory block device is acquired. */
493 static struct memory_block *find_memory_block_by_id(unsigned long block_id)
495 struct device *dev;
497 dev = subsys_find_device_by_id(&memory_subsys, block_id, NULL);
498 return dev ? to_memory_block(dev) : NULL;
502 * For now, we have a linear search to go find the appropriate
503 * memory_block corresponding to a particular phys_index. If
504 * this gets to be a real problem, we can always use a radix
505 * tree or something here.
507 * This could be made generic for all device subsystems.
509 struct memory_block *find_memory_block(struct mem_section *section)
511 unsigned long block_id = base_memory_block_id(__section_nr(section));
513 return find_memory_block_by_id(block_id);
516 static struct attribute *memory_memblk_attrs[] = {
517 &dev_attr_phys_index.attr,
518 &dev_attr_state.attr,
519 &dev_attr_phys_device.attr,
520 &dev_attr_removable.attr,
521 #ifdef CONFIG_MEMORY_HOTREMOVE
522 &dev_attr_valid_zones.attr,
523 #endif
524 NULL
527 static struct attribute_group memory_memblk_attr_group = {
528 .attrs = memory_memblk_attrs,
531 static const struct attribute_group *memory_memblk_attr_groups[] = {
532 &memory_memblk_attr_group,
533 NULL,
537 * register_memory - Setup a sysfs device for a memory block
539 static
540 int register_memory(struct memory_block *memory)
542 int ret;
544 memory->dev.bus = &memory_subsys;
545 memory->dev.id = memory->start_section_nr / sections_per_block;
546 memory->dev.release = memory_block_release;
547 memory->dev.groups = memory_memblk_attr_groups;
548 memory->dev.offline = memory->state == MEM_OFFLINE;
550 ret = device_register(&memory->dev);
551 if (ret)
552 put_device(&memory->dev);
554 return ret;
557 static int init_memory_block(struct memory_block **memory,
558 unsigned long block_id, unsigned long state)
560 struct memory_block *mem;
561 unsigned long start_pfn;
562 int ret = 0;
564 mem = find_memory_block_by_id(block_id);
565 if (mem) {
566 put_device(&mem->dev);
567 return -EEXIST;
569 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
570 if (!mem)
571 return -ENOMEM;
573 mem->start_section_nr = block_id * sections_per_block;
574 mem->state = state;
575 start_pfn = section_nr_to_pfn(mem->start_section_nr);
576 mem->phys_device = arch_get_memory_phys_device(start_pfn);
577 mem->nid = NUMA_NO_NODE;
579 ret = register_memory(mem);
581 *memory = mem;
582 return ret;
585 static int add_memory_block(unsigned long base_section_nr)
587 int section_count = 0;
588 struct memory_block *mem;
589 unsigned long nr;
591 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
592 nr++)
593 if (present_section_nr(nr))
594 section_count++;
596 if (section_count == 0)
597 return 0;
598 return init_memory_block(&mem, base_memory_block_id(base_section_nr),
599 MEM_ONLINE);
602 static void unregister_memory(struct memory_block *memory)
604 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
605 return;
607 /* drop the ref. we got via find_memory_block() */
608 put_device(&memory->dev);
609 device_unregister(&memory->dev);
613 * Create memory block devices for the given memory area. Start and size
614 * have to be aligned to memory block granularity. Memory block devices
615 * will be initialized as offline.
617 * Called under device_hotplug_lock.
619 int create_memory_block_devices(unsigned long start, unsigned long size)
621 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
622 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
623 struct memory_block *mem;
624 unsigned long block_id;
625 int ret = 0;
627 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
628 !IS_ALIGNED(size, memory_block_size_bytes())))
629 return -EINVAL;
631 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
632 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
633 if (ret)
634 break;
636 if (ret) {
637 end_block_id = block_id;
638 for (block_id = start_block_id; block_id != end_block_id;
639 block_id++) {
640 mem = find_memory_block_by_id(block_id);
641 if (WARN_ON_ONCE(!mem))
642 continue;
643 unregister_memory(mem);
646 return ret;
650 * Remove memory block devices for the given memory area. Start and size
651 * have to be aligned to memory block granularity. Memory block devices
652 * have to be offline.
654 * Called under device_hotplug_lock.
656 void remove_memory_block_devices(unsigned long start, unsigned long size)
658 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
659 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
660 struct memory_block *mem;
661 unsigned long block_id;
663 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
664 !IS_ALIGNED(size, memory_block_size_bytes())))
665 return;
667 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
668 mem = find_memory_block_by_id(block_id);
669 if (WARN_ON_ONCE(!mem))
670 continue;
671 unregister_memory_block_under_nodes(mem);
672 unregister_memory(mem);
676 /* return true if the memory block is offlined, otherwise, return false */
677 bool is_memblock_offlined(struct memory_block *mem)
679 return mem->state == MEM_OFFLINE;
682 static struct attribute *memory_root_attrs[] = {
683 #ifdef CONFIG_ARCH_MEMORY_PROBE
684 &dev_attr_probe.attr,
685 #endif
687 #ifdef CONFIG_MEMORY_FAILURE
688 &dev_attr_soft_offline_page.attr,
689 &dev_attr_hard_offline_page.attr,
690 #endif
692 &dev_attr_block_size_bytes.attr,
693 &dev_attr_auto_online_blocks.attr,
694 NULL
697 static struct attribute_group memory_root_attr_group = {
698 .attrs = memory_root_attrs,
701 static const struct attribute_group *memory_root_attr_groups[] = {
702 &memory_root_attr_group,
703 NULL,
707 * Initialize the sysfs support for memory devices. At the time this function
708 * is called, we cannot have concurrent creation/deletion of memory block
709 * devices, the device_hotplug_lock is not needed.
711 void __init memory_dev_init(void)
713 int ret;
714 unsigned long block_sz, nr;
716 /* Validate the configured memory block size */
717 block_sz = memory_block_size_bytes();
718 if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
719 panic("Memory block size not suitable: 0x%lx\n", block_sz);
720 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
722 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
723 if (ret)
724 panic("%s() failed to register subsystem: %d\n", __func__, ret);
727 * Create entries for memory sections that were found
728 * during boot and have been initialized
730 for (nr = 0; nr <= __highest_present_section_nr;
731 nr += sections_per_block) {
732 ret = add_memory_block(nr);
733 if (ret)
734 panic("%s() failed to add memory block: %d\n", __func__,
735 ret);
740 * walk_memory_blocks - walk through all present memory blocks overlapped
741 * by the range [start, start + size)
743 * @start: start address of the memory range
744 * @size: size of the memory range
745 * @arg: argument passed to func
746 * @func: callback for each memory section walked
748 * This function walks through all present memory blocks overlapped by the
749 * range [start, start + size), calling func on each memory block.
751 * In case func() returns an error, walking is aborted and the error is
752 * returned.
754 int walk_memory_blocks(unsigned long start, unsigned long size,
755 void *arg, walk_memory_blocks_func_t func)
757 const unsigned long start_block_id = phys_to_block_id(start);
758 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
759 struct memory_block *mem;
760 unsigned long block_id;
761 int ret = 0;
763 if (!size)
764 return 0;
766 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
767 mem = find_memory_block_by_id(block_id);
768 if (!mem)
769 continue;
771 ret = func(mem, arg);
772 put_device(&mem->dev);
773 if (ret)
774 break;
776 return ret;
779 struct for_each_memory_block_cb_data {
780 walk_memory_blocks_func_t func;
781 void *arg;
784 static int for_each_memory_block_cb(struct device *dev, void *data)
786 struct memory_block *mem = to_memory_block(dev);
787 struct for_each_memory_block_cb_data *cb_data = data;
789 return cb_data->func(mem, cb_data->arg);
793 * for_each_memory_block - walk through all present memory blocks
795 * @arg: argument passed to func
796 * @func: callback for each memory block walked
798 * This function walks through all present memory blocks, calling func on
799 * each memory block.
801 * In case func() returns an error, walking is aborted and the error is
802 * returned.
804 int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
806 struct for_each_memory_block_cb_data cb_data = {
807 .func = func,
808 .arg = arg,
811 return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
812 for_each_memory_block_cb);