dmaengine: imx-sdma: Let the core do the device node validation
[linux/fpc-iii.git] / drivers / base / memory.c
blobf180427e48f4e59341795c3090d51d84a5f83dc8
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/mutex.h>
23 #include <linux/stat.h>
24 #include <linux/slab.h>
26 #include <linux/atomic.h>
27 #include <linux/uaccess.h>
29 static DEFINE_MUTEX(mem_sysfs_mutex);
31 #define MEMORY_CLASS_NAME "memory"
33 #define to_memory_block(dev) container_of(dev, struct memory_block, dev)
35 static int sections_per_block;
37 static inline int base_memory_block_id(int section_nr)
39 return section_nr / sections_per_block;
42 static int memory_subsys_online(struct device *dev);
43 static int memory_subsys_offline(struct device *dev);
45 static struct bus_type memory_subsys = {
46 .name = MEMORY_CLASS_NAME,
47 .dev_name = MEMORY_CLASS_NAME,
48 .online = memory_subsys_online,
49 .offline = memory_subsys_offline,
52 static BLOCKING_NOTIFIER_HEAD(memory_chain);
54 int register_memory_notifier(struct notifier_block *nb)
56 return blocking_notifier_chain_register(&memory_chain, nb);
58 EXPORT_SYMBOL(register_memory_notifier);
60 void unregister_memory_notifier(struct notifier_block *nb)
62 blocking_notifier_chain_unregister(&memory_chain, nb);
64 EXPORT_SYMBOL(unregister_memory_notifier);
66 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
68 int register_memory_isolate_notifier(struct notifier_block *nb)
70 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
72 EXPORT_SYMBOL(register_memory_isolate_notifier);
74 void unregister_memory_isolate_notifier(struct notifier_block *nb)
76 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
78 EXPORT_SYMBOL(unregister_memory_isolate_notifier);
80 static void memory_block_release(struct device *dev)
82 struct memory_block *mem = to_memory_block(dev);
84 kfree(mem);
87 unsigned long __weak memory_block_size_bytes(void)
89 return MIN_MEMORY_BLOCK_SIZE;
91 EXPORT_SYMBOL_GPL(memory_block_size_bytes);
93 static unsigned long get_memory_block_size(void)
95 unsigned long block_sz;
97 block_sz = memory_block_size_bytes();
99 /* Validate blk_sz is a power of 2 and not less than section size */
100 if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
101 WARN_ON(1);
102 block_sz = MIN_MEMORY_BLOCK_SIZE;
105 return block_sz;
109 * use this as the physical section index that this memsection
110 * uses.
113 static ssize_t phys_index_show(struct device *dev,
114 struct device_attribute *attr, char *buf)
116 struct memory_block *mem = to_memory_block(dev);
117 unsigned long phys_index;
119 phys_index = mem->start_section_nr / sections_per_block;
120 return sprintf(buf, "%08lx\n", phys_index);
124 * Show whether the section of memory is likely to be hot-removable
126 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
127 char *buf)
129 unsigned long i, pfn;
130 int ret = 1;
131 struct memory_block *mem = to_memory_block(dev);
133 if (mem->state != MEM_ONLINE)
134 goto out;
136 for (i = 0; i < sections_per_block; i++) {
137 if (!present_section_nr(mem->start_section_nr + i))
138 continue;
139 pfn = section_nr_to_pfn(mem->start_section_nr + i);
140 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
143 out:
144 return sprintf(buf, "%d\n", ret);
148 * online, offline, going offline, etc.
150 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
151 char *buf)
153 struct memory_block *mem = to_memory_block(dev);
154 ssize_t len = 0;
157 * We can probably put these states in a nice little array
158 * so that they're not open-coded
160 switch (mem->state) {
161 case MEM_ONLINE:
162 len = sprintf(buf, "online\n");
163 break;
164 case MEM_OFFLINE:
165 len = sprintf(buf, "offline\n");
166 break;
167 case MEM_GOING_OFFLINE:
168 len = sprintf(buf, "going-offline\n");
169 break;
170 default:
171 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
172 mem->state);
173 WARN_ON(1);
174 break;
177 return len;
180 int memory_notify(unsigned long val, void *v)
182 return blocking_notifier_call_chain(&memory_chain, val, v);
185 int memory_isolate_notify(unsigned long val, void *v)
187 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
191 * The probe routines leave the pages uninitialized, just as the bootmem code
192 * does. Make sure we do not access them, but instead use only information from
193 * within sections.
195 static bool pages_correctly_probed(unsigned long start_pfn)
197 unsigned long section_nr = pfn_to_section_nr(start_pfn);
198 unsigned long section_nr_end = section_nr + sections_per_block;
199 unsigned long pfn = start_pfn;
202 * memmap between sections is not contiguous except with
203 * SPARSEMEM_VMEMMAP. We lookup the page once per section
204 * and assume memmap is contiguous within each section
206 for (; section_nr < section_nr_end; section_nr++) {
207 if (WARN_ON_ONCE(!pfn_valid(pfn)))
208 return false;
210 if (!present_section_nr(section_nr)) {
211 pr_warn("section %ld pfn[%lx, %lx) not present\n",
212 section_nr, pfn, pfn + PAGES_PER_SECTION);
213 return false;
214 } else if (!valid_section_nr(section_nr)) {
215 pr_warn("section %ld pfn[%lx, %lx) no valid memmap\n",
216 section_nr, pfn, pfn + PAGES_PER_SECTION);
217 return false;
218 } else if (online_section_nr(section_nr)) {
219 pr_warn("section %ld pfn[%lx, %lx) is already online\n",
220 section_nr, pfn, pfn + PAGES_PER_SECTION);
221 return false;
223 pfn += PAGES_PER_SECTION;
226 return true;
230 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
231 * OK to have direct references to sparsemem variables in here.
233 static int
234 memory_block_action(unsigned long start_section_nr, unsigned long action,
235 int online_type)
237 unsigned long start_pfn;
238 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
239 int ret;
241 start_pfn = section_nr_to_pfn(start_section_nr);
243 switch (action) {
244 case MEM_ONLINE:
245 if (!pages_correctly_probed(start_pfn))
246 return -EBUSY;
248 ret = online_pages(start_pfn, nr_pages, online_type);
249 break;
250 case MEM_OFFLINE:
251 ret = offline_pages(start_pfn, nr_pages);
252 break;
253 default:
254 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
255 "%ld\n", __func__, start_section_nr, action, action);
256 ret = -EINVAL;
259 return ret;
262 static int memory_block_change_state(struct memory_block *mem,
263 unsigned long to_state, unsigned long from_state_req)
265 int ret = 0;
267 if (mem->state != from_state_req)
268 return -EINVAL;
270 if (to_state == MEM_OFFLINE)
271 mem->state = MEM_GOING_OFFLINE;
273 ret = memory_block_action(mem->start_section_nr, to_state,
274 mem->online_type);
276 mem->state = ret ? from_state_req : to_state;
278 return ret;
281 /* The device lock serializes operations on memory_subsys_[online|offline] */
282 static int memory_subsys_online(struct device *dev)
284 struct memory_block *mem = to_memory_block(dev);
285 int ret;
287 if (mem->state == MEM_ONLINE)
288 return 0;
291 * If we are called from state_store(), online_type will be
292 * set >= 0 Otherwise we were called from the device online
293 * attribute and need to set the online_type.
295 if (mem->online_type < 0)
296 mem->online_type = MMOP_ONLINE_KEEP;
298 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
300 /* clear online_type */
301 mem->online_type = -1;
303 return ret;
306 static int memory_subsys_offline(struct device *dev)
308 struct memory_block *mem = to_memory_block(dev);
310 if (mem->state == MEM_OFFLINE)
311 return 0;
313 /* Can't offline block with non-present sections */
314 if (mem->section_count != sections_per_block)
315 return -EINVAL;
317 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
320 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
321 const char *buf, size_t count)
323 struct memory_block *mem = to_memory_block(dev);
324 int ret, online_type;
326 ret = lock_device_hotplug_sysfs();
327 if (ret)
328 return ret;
330 if (sysfs_streq(buf, "online_kernel"))
331 online_type = MMOP_ONLINE_KERNEL;
332 else if (sysfs_streq(buf, "online_movable"))
333 online_type = MMOP_ONLINE_MOVABLE;
334 else if (sysfs_streq(buf, "online"))
335 online_type = MMOP_ONLINE_KEEP;
336 else if (sysfs_streq(buf, "offline"))
337 online_type = MMOP_OFFLINE;
338 else {
339 ret = -EINVAL;
340 goto err;
343 switch (online_type) {
344 case MMOP_ONLINE_KERNEL:
345 case MMOP_ONLINE_MOVABLE:
346 case MMOP_ONLINE_KEEP:
347 /* mem->online_type is protected by device_hotplug_lock */
348 mem->online_type = online_type;
349 ret = device_online(&mem->dev);
350 break;
351 case MMOP_OFFLINE:
352 ret = device_offline(&mem->dev);
353 break;
354 default:
355 ret = -EINVAL; /* should never happen */
358 err:
359 unlock_device_hotplug();
361 if (ret < 0)
362 return ret;
363 if (ret)
364 return -EINVAL;
366 return count;
370 * phys_device is a bad name for this. What I really want
371 * is a way to differentiate between memory ranges that
372 * are part of physical devices that constitute
373 * a complete removable unit or fru.
374 * i.e. do these ranges belong to the same physical device,
375 * s.t. if I offline all of these sections I can then
376 * remove the physical device?
378 static ssize_t phys_device_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
381 struct memory_block *mem = to_memory_block(dev);
382 return sprintf(buf, "%d\n", mem->phys_device);
385 #ifdef CONFIG_MEMORY_HOTREMOVE
386 static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
387 unsigned long nr_pages, int online_type,
388 struct zone *default_zone)
390 struct zone *zone;
392 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
393 if (zone != default_zone) {
394 strcat(buf, " ");
395 strcat(buf, zone->name);
399 static ssize_t valid_zones_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
402 struct memory_block *mem = to_memory_block(dev);
403 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
404 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
405 unsigned long valid_start_pfn, valid_end_pfn;
406 struct zone *default_zone;
407 int nid;
410 * Check the existing zone. Make sure that we do that only on the
411 * online nodes otherwise the page_zone is not reliable
413 if (mem->state == MEM_ONLINE) {
415 * The block contains more than one zone can not be offlined.
416 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
418 if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages,
419 &valid_start_pfn, &valid_end_pfn))
420 return sprintf(buf, "none\n");
421 start_pfn = valid_start_pfn;
422 strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
423 goto out;
426 nid = mem->nid;
427 default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
428 strcat(buf, default_zone->name);
430 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
431 default_zone);
432 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
433 default_zone);
434 out:
435 strcat(buf, "\n");
437 return strlen(buf);
439 static DEVICE_ATTR_RO(valid_zones);
440 #endif
442 static DEVICE_ATTR_RO(phys_index);
443 static DEVICE_ATTR_RW(state);
444 static DEVICE_ATTR_RO(phys_device);
445 static DEVICE_ATTR_RO(removable);
448 * Block size attribute stuff
450 static ssize_t block_size_bytes_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
453 return sprintf(buf, "%lx\n", get_memory_block_size());
456 static DEVICE_ATTR_RO(block_size_bytes);
459 * Memory auto online policy.
462 static ssize_t auto_online_blocks_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
465 if (memhp_auto_online)
466 return sprintf(buf, "online\n");
467 else
468 return sprintf(buf, "offline\n");
471 static ssize_t auto_online_blocks_store(struct device *dev,
472 struct device_attribute *attr,
473 const char *buf, size_t count)
475 if (sysfs_streq(buf, "online"))
476 memhp_auto_online = true;
477 else if (sysfs_streq(buf, "offline"))
478 memhp_auto_online = false;
479 else
480 return -EINVAL;
482 return count;
485 static DEVICE_ATTR_RW(auto_online_blocks);
488 * Some architectures will have custom drivers to do this, and
489 * will not need to do it from userspace. The fake hot-add code
490 * as well as ppc64 will do all of their discovery in userspace
491 * and will require this interface.
493 #ifdef CONFIG_ARCH_MEMORY_PROBE
494 static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
495 const char *buf, size_t count)
497 u64 phys_addr;
498 int nid, ret;
499 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
501 ret = kstrtoull(buf, 0, &phys_addr);
502 if (ret)
503 return ret;
505 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
506 return -EINVAL;
508 ret = lock_device_hotplug_sysfs();
509 if (ret)
510 return ret;
512 nid = memory_add_physaddr_to_nid(phys_addr);
513 ret = __add_memory(nid, phys_addr,
514 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
516 if (ret)
517 goto out;
519 ret = count;
520 out:
521 unlock_device_hotplug();
522 return ret;
525 static DEVICE_ATTR_WO(probe);
526 #endif
528 #ifdef CONFIG_MEMORY_FAILURE
530 * Support for offlining pages of memory
533 /* Soft offline a page */
534 static ssize_t soft_offline_page_store(struct device *dev,
535 struct device_attribute *attr,
536 const char *buf, size_t count)
538 int ret;
539 u64 pfn;
540 if (!capable(CAP_SYS_ADMIN))
541 return -EPERM;
542 if (kstrtoull(buf, 0, &pfn) < 0)
543 return -EINVAL;
544 pfn >>= PAGE_SHIFT;
545 if (!pfn_valid(pfn))
546 return -ENXIO;
547 ret = soft_offline_page(pfn_to_page(pfn), 0);
548 return ret == 0 ? count : ret;
551 /* Forcibly offline a page, including killing processes. */
552 static ssize_t hard_offline_page_store(struct device *dev,
553 struct device_attribute *attr,
554 const char *buf, size_t count)
556 int ret;
557 u64 pfn;
558 if (!capable(CAP_SYS_ADMIN))
559 return -EPERM;
560 if (kstrtoull(buf, 0, &pfn) < 0)
561 return -EINVAL;
562 pfn >>= PAGE_SHIFT;
563 ret = memory_failure(pfn, 0);
564 return ret ? ret : count;
567 static DEVICE_ATTR_WO(soft_offline_page);
568 static DEVICE_ATTR_WO(hard_offline_page);
569 #endif
572 * Note that phys_device is optional. It is here to allow for
573 * differentiation between which *physical* devices each
574 * section belongs to...
576 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
578 return 0;
582 * A reference for the returned object is held and the reference for the
583 * hinted object is released.
585 struct memory_block *find_memory_block_hinted(struct mem_section *section,
586 struct memory_block *hint)
588 int block_id = base_memory_block_id(__section_nr(section));
589 struct device *hintdev = hint ? &hint->dev : NULL;
590 struct device *dev;
592 dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
593 if (hint)
594 put_device(&hint->dev);
595 if (!dev)
596 return NULL;
597 return to_memory_block(dev);
601 * For now, we have a linear search to go find the appropriate
602 * memory_block corresponding to a particular phys_index. If
603 * this gets to be a real problem, we can always use a radix
604 * tree or something here.
606 * This could be made generic for all device subsystems.
608 struct memory_block *find_memory_block(struct mem_section *section)
610 return find_memory_block_hinted(section, NULL);
613 static struct attribute *memory_memblk_attrs[] = {
614 &dev_attr_phys_index.attr,
615 &dev_attr_state.attr,
616 &dev_attr_phys_device.attr,
617 &dev_attr_removable.attr,
618 #ifdef CONFIG_MEMORY_HOTREMOVE
619 &dev_attr_valid_zones.attr,
620 #endif
621 NULL
624 static struct attribute_group memory_memblk_attr_group = {
625 .attrs = memory_memblk_attrs,
628 static const struct attribute_group *memory_memblk_attr_groups[] = {
629 &memory_memblk_attr_group,
630 NULL,
634 * register_memory - Setup a sysfs device for a memory block
636 static
637 int register_memory(struct memory_block *memory)
639 int ret;
641 memory->dev.bus = &memory_subsys;
642 memory->dev.id = memory->start_section_nr / sections_per_block;
643 memory->dev.release = memory_block_release;
644 memory->dev.groups = memory_memblk_attr_groups;
645 memory->dev.offline = memory->state == MEM_OFFLINE;
647 ret = device_register(&memory->dev);
648 if (ret)
649 put_device(&memory->dev);
651 return ret;
654 static int init_memory_block(struct memory_block **memory,
655 struct mem_section *section, unsigned long state)
657 struct memory_block *mem;
658 unsigned long start_pfn;
659 int scn_nr;
660 int ret = 0;
662 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
663 if (!mem)
664 return -ENOMEM;
666 scn_nr = __section_nr(section);
667 mem->start_section_nr =
668 base_memory_block_id(scn_nr) * sections_per_block;
669 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
670 mem->state = state;
671 start_pfn = section_nr_to_pfn(mem->start_section_nr);
672 mem->phys_device = arch_get_memory_phys_device(start_pfn);
674 ret = register_memory(mem);
676 *memory = mem;
677 return ret;
680 static int add_memory_block(int base_section_nr)
682 struct memory_block *mem;
683 int i, ret, section_count = 0, section_nr;
685 for (i = base_section_nr;
686 i < base_section_nr + sections_per_block;
687 i++) {
688 if (!present_section_nr(i))
689 continue;
690 if (section_count == 0)
691 section_nr = i;
692 section_count++;
695 if (section_count == 0)
696 return 0;
697 ret = init_memory_block(&mem, __nr_to_section(section_nr), MEM_ONLINE);
698 if (ret)
699 return ret;
700 mem->section_count = section_count;
701 return 0;
705 * need an interface for the VM to add new memory regions,
706 * but without onlining it.
708 int hotplug_memory_register(int nid, struct mem_section *section)
710 int ret = 0;
711 struct memory_block *mem;
713 mutex_lock(&mem_sysfs_mutex);
715 mem = find_memory_block(section);
716 if (mem) {
717 mem->section_count++;
718 put_device(&mem->dev);
719 } else {
720 ret = init_memory_block(&mem, section, MEM_OFFLINE);
721 if (ret)
722 goto out;
723 mem->section_count++;
726 out:
727 mutex_unlock(&mem_sysfs_mutex);
728 return ret;
731 #ifdef CONFIG_MEMORY_HOTREMOVE
732 static void
733 unregister_memory(struct memory_block *memory)
735 BUG_ON(memory->dev.bus != &memory_subsys);
737 /* drop the ref. we got via find_memory_block() */
738 put_device(&memory->dev);
739 device_unregister(&memory->dev);
742 void unregister_memory_section(struct mem_section *section)
744 struct memory_block *mem;
746 if (WARN_ON_ONCE(!present_section(section)))
747 return;
749 mutex_lock(&mem_sysfs_mutex);
752 * Some users of the memory hotplug do not want/need memblock to
753 * track all sections. Skip over those.
755 mem = find_memory_block(section);
756 if (!mem)
757 goto out_unlock;
759 unregister_mem_sect_under_nodes(mem, __section_nr(section));
761 mem->section_count--;
762 if (mem->section_count == 0)
763 unregister_memory(mem);
764 else
765 put_device(&mem->dev);
767 out_unlock:
768 mutex_unlock(&mem_sysfs_mutex);
770 #endif /* CONFIG_MEMORY_HOTREMOVE */
772 /* return true if the memory block is offlined, otherwise, return false */
773 bool is_memblock_offlined(struct memory_block *mem)
775 return mem->state == MEM_OFFLINE;
778 static struct attribute *memory_root_attrs[] = {
779 #ifdef CONFIG_ARCH_MEMORY_PROBE
780 &dev_attr_probe.attr,
781 #endif
783 #ifdef CONFIG_MEMORY_FAILURE
784 &dev_attr_soft_offline_page.attr,
785 &dev_attr_hard_offline_page.attr,
786 #endif
788 &dev_attr_block_size_bytes.attr,
789 &dev_attr_auto_online_blocks.attr,
790 NULL
793 static struct attribute_group memory_root_attr_group = {
794 .attrs = memory_root_attrs,
797 static const struct attribute_group *memory_root_attr_groups[] = {
798 &memory_root_attr_group,
799 NULL,
803 * Initialize the sysfs support for memory devices...
805 int __init memory_dev_init(void)
807 unsigned int i;
808 int ret;
809 int err;
810 unsigned long block_sz;
812 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
813 if (ret)
814 goto out;
816 block_sz = get_memory_block_size();
817 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
820 * Create entries for memory sections that were found
821 * during boot and have been initialized
823 mutex_lock(&mem_sysfs_mutex);
824 for (i = 0; i <= __highest_present_section_nr;
825 i += sections_per_block) {
826 err = add_memory_block(i);
827 if (!ret)
828 ret = err;
830 mutex_unlock(&mem_sysfs_mutex);
832 out:
833 if (ret)
834 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
835 return ret;