GNU Linux-libre 4.19.207-gnu1
[releases.git] / drivers / base / memory.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Memory subsystem support
4  *
5  * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
6  *            Dave Hansen <haveblue@us.ibm.com>
7  *
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.
12  */
13
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>
25
26 #include <linux/atomic.h>
27 #include <linux/uaccess.h>
28
29 static DEFINE_MUTEX(mem_sysfs_mutex);
30
31 #define MEMORY_CLASS_NAME       "memory"
32
33 #define to_memory_block(dev) container_of(dev, struct memory_block, dev)
34
35 static int sections_per_block;
36
37 static inline int base_memory_block_id(int section_nr)
38 {
39         return section_nr / sections_per_block;
40 }
41
42 static inline int pfn_to_block_id(unsigned long pfn)
43 {
44         return base_memory_block_id(pfn_to_section_nr(pfn));
45 }
46
47 static int memory_subsys_online(struct device *dev);
48 static int memory_subsys_offline(struct device *dev);
49
50 static struct bus_type memory_subsys = {
51         .name = MEMORY_CLASS_NAME,
52         .dev_name = MEMORY_CLASS_NAME,
53         .online = memory_subsys_online,
54         .offline = memory_subsys_offline,
55 };
56
57 static BLOCKING_NOTIFIER_HEAD(memory_chain);
58
59 int register_memory_notifier(struct notifier_block *nb)
60 {
61         return blocking_notifier_chain_register(&memory_chain, nb);
62 }
63 EXPORT_SYMBOL(register_memory_notifier);
64
65 void unregister_memory_notifier(struct notifier_block *nb)
66 {
67         blocking_notifier_chain_unregister(&memory_chain, nb);
68 }
69 EXPORT_SYMBOL(unregister_memory_notifier);
70
71 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
72
73 int register_memory_isolate_notifier(struct notifier_block *nb)
74 {
75         return atomic_notifier_chain_register(&memory_isolate_chain, nb);
76 }
77 EXPORT_SYMBOL(register_memory_isolate_notifier);
78
79 void unregister_memory_isolate_notifier(struct notifier_block *nb)
80 {
81         atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
82 }
83 EXPORT_SYMBOL(unregister_memory_isolate_notifier);
84
85 static void memory_block_release(struct device *dev)
86 {
87         struct memory_block *mem = to_memory_block(dev);
88
89         kfree(mem);
90 }
91
92 unsigned long __weak memory_block_size_bytes(void)
93 {
94         return MIN_MEMORY_BLOCK_SIZE;
95 }
96
97 static unsigned long get_memory_block_size(void)
98 {
99         unsigned long block_sz;
100
101         block_sz = memory_block_size_bytes();
102
103         /* Validate blk_sz is a power of 2 and not less than section size */
104         if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
105                 WARN_ON(1);
106                 block_sz = MIN_MEMORY_BLOCK_SIZE;
107         }
108
109         return block_sz;
110 }
111
112 /*
113  * use this as the physical section index that this memsection
114  * uses.
115  */
116
117 static ssize_t show_mem_start_phys_index(struct device *dev,
118                         struct device_attribute *attr, char *buf)
119 {
120         struct memory_block *mem = to_memory_block(dev);
121         unsigned long phys_index;
122
123         phys_index = mem->start_section_nr / sections_per_block;
124         return sprintf(buf, "%08lx\n", phys_index);
125 }
126
127 /*
128  * Show whether the section of memory is likely to be hot-removable
129  */
130 static ssize_t show_mem_removable(struct device *dev,
131                         struct device_attribute *attr, char *buf)
132 {
133         unsigned long i, pfn;
134         int ret = 1;
135         struct memory_block *mem = to_memory_block(dev);
136
137         if (mem->state != MEM_ONLINE)
138                 goto out;
139
140         for (i = 0; i < sections_per_block; i++) {
141                 if (!present_section_nr(mem->start_section_nr + i))
142                         continue;
143                 pfn = section_nr_to_pfn(mem->start_section_nr + i);
144                 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
145         }
146
147 out:
148         return sprintf(buf, "%d\n", ret);
149 }
150
151 /*
152  * online, offline, going offline, etc.
153  */
154 static ssize_t show_mem_state(struct device *dev,
155                         struct device_attribute *attr, char *buf)
156 {
157         struct memory_block *mem = to_memory_block(dev);
158         ssize_t len = 0;
159
160         /*
161          * We can probably put these states in a nice little array
162          * so that they're not open-coded
163          */
164         switch (mem->state) {
165         case MEM_ONLINE:
166                 len = sprintf(buf, "online\n");
167                 break;
168         case MEM_OFFLINE:
169                 len = sprintf(buf, "offline\n");
170                 break;
171         case MEM_GOING_OFFLINE:
172                 len = sprintf(buf, "going-offline\n");
173                 break;
174         default:
175                 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
176                                 mem->state);
177                 WARN_ON(1);
178                 break;
179         }
180
181         return len;
182 }
183
184 int memory_notify(unsigned long val, void *v)
185 {
186         return blocking_notifier_call_chain(&memory_chain, val, v);
187 }
188
189 int memory_isolate_notify(unsigned long val, void *v)
190 {
191         return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
192 }
193
194 /*
195  * The probe routines leave the pages uninitialized, just as the bootmem code
196  * does. Make sure we do not access them, but instead use only information from
197  * within sections.
198  */
199 static bool pages_correctly_probed(unsigned long start_pfn)
200 {
201         unsigned long section_nr = pfn_to_section_nr(start_pfn);
202         unsigned long section_nr_end = section_nr + sections_per_block;
203         unsigned long pfn = start_pfn;
204
205         /*
206          * memmap between sections is not contiguous except with
207          * SPARSEMEM_VMEMMAP. We lookup the page once per section
208          * and assume memmap is contiguous within each section
209          */
210         for (; section_nr < section_nr_end; section_nr++) {
211                 if (WARN_ON_ONCE(!pfn_valid(pfn)))
212                         return false;
213
214                 if (!present_section_nr(section_nr)) {
215                         pr_warn("section %ld pfn[%lx, %lx) not present",
216                                 section_nr, pfn, pfn + PAGES_PER_SECTION);
217                         return false;
218                 } else if (!valid_section_nr(section_nr)) {
219                         pr_warn("section %ld pfn[%lx, %lx) no valid memmap",
220                                 section_nr, pfn, pfn + PAGES_PER_SECTION);
221                         return false;
222                 } else if (online_section_nr(section_nr)) {
223                         pr_warn("section %ld pfn[%lx, %lx) is already online",
224                                 section_nr, pfn, pfn + PAGES_PER_SECTION);
225                         return false;
226                 }
227                 pfn += PAGES_PER_SECTION;
228         }
229
230         return true;
231 }
232
233 /*
234  * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
235  * OK to have direct references to sparsemem variables in here.
236  */
237 static int
238 memory_block_action(unsigned long start_section_nr, unsigned long action,
239                     int online_type)
240 {
241         unsigned long start_pfn;
242         unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
243         int ret;
244
245         start_pfn = section_nr_to_pfn(start_section_nr);
246
247         switch (action) {
248         case MEM_ONLINE:
249                 if (!pages_correctly_probed(start_pfn))
250                         return -EBUSY;
251
252                 ret = online_pages(start_pfn, nr_pages, online_type);
253                 break;
254         case MEM_OFFLINE:
255                 ret = offline_pages(start_pfn, nr_pages);
256                 break;
257         default:
258                 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
259                      "%ld\n", __func__, start_section_nr, action, action);
260                 ret = -EINVAL;
261         }
262
263         return ret;
264 }
265
266 static int memory_block_change_state(struct memory_block *mem,
267                 unsigned long to_state, unsigned long from_state_req)
268 {
269         int ret = 0;
270
271         if (mem->state != from_state_req)
272                 return -EINVAL;
273
274         if (to_state == MEM_OFFLINE)
275                 mem->state = MEM_GOING_OFFLINE;
276
277         ret = memory_block_action(mem->start_section_nr, to_state,
278                                 mem->online_type);
279
280         mem->state = ret ? from_state_req : to_state;
281
282         return ret;
283 }
284
285 /* The device lock serializes operations on memory_subsys_[online|offline] */
286 static int memory_subsys_online(struct device *dev)
287 {
288         struct memory_block *mem = to_memory_block(dev);
289         int ret;
290
291         if (mem->state == MEM_ONLINE)
292                 return 0;
293
294         /*
295          * If we are called from store_mem_state(), online_type will be
296          * set >= 0 Otherwise we were called from the device online
297          * attribute and need to set the online_type.
298          */
299         if (mem->online_type < 0)
300                 mem->online_type = MMOP_ONLINE_KEEP;
301
302         ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
303
304         /* clear online_type */
305         mem->online_type = -1;
306
307         return ret;
308 }
309
310 static int memory_subsys_offline(struct device *dev)
311 {
312         struct memory_block *mem = to_memory_block(dev);
313
314         if (mem->state == MEM_OFFLINE)
315                 return 0;
316
317         /* Can't offline block with non-present sections */
318         if (mem->section_count != sections_per_block)
319                 return -EINVAL;
320
321         return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
322 }
323
324 static ssize_t
325 store_mem_state(struct device *dev,
326                 struct device_attribute *attr, const char *buf, size_t count)
327 {
328         struct memory_block *mem = to_memory_block(dev);
329         int ret, online_type;
330
331         ret = lock_device_hotplug_sysfs();
332         if (ret)
333                 return ret;
334
335         if (sysfs_streq(buf, "online_kernel"))
336                 online_type = MMOP_ONLINE_KERNEL;
337         else if (sysfs_streq(buf, "online_movable"))
338                 online_type = MMOP_ONLINE_MOVABLE;
339         else if (sysfs_streq(buf, "online"))
340                 online_type = MMOP_ONLINE_KEEP;
341         else if (sysfs_streq(buf, "offline"))
342                 online_type = MMOP_OFFLINE;
343         else {
344                 ret = -EINVAL;
345                 goto err;
346         }
347
348         switch (online_type) {
349         case MMOP_ONLINE_KERNEL:
350         case MMOP_ONLINE_MOVABLE:
351         case MMOP_ONLINE_KEEP:
352                 /* mem->online_type is protected by device_hotplug_lock */
353                 mem->online_type = online_type;
354                 ret = device_online(&mem->dev);
355                 break;
356         case MMOP_OFFLINE:
357                 ret = device_offline(&mem->dev);
358                 break;
359         default:
360                 ret = -EINVAL; /* should never happen */
361         }
362
363 err:
364         unlock_device_hotplug();
365
366         if (ret < 0)
367                 return ret;
368         if (ret)
369                 return -EINVAL;
370
371         return count;
372 }
373
374 /*
375  * phys_device is a bad name for this.  What I really want
376  * is a way to differentiate between memory ranges that
377  * are part of physical devices that constitute
378  * a complete removable unit or fru.
379  * i.e. do these ranges belong to the same physical device,
380  * s.t. if I offline all of these sections I can then
381  * remove the physical device?
382  */
383 static ssize_t show_phys_device(struct device *dev,
384                                 struct device_attribute *attr, char *buf)
385 {
386         struct memory_block *mem = to_memory_block(dev);
387         return sprintf(buf, "%d\n", mem->phys_device);
388 }
389
390 #ifdef CONFIG_MEMORY_HOTREMOVE
391 static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
392                 unsigned long nr_pages, int online_type,
393                 struct zone *default_zone)
394 {
395         struct zone *zone;
396
397         zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
398         if (zone != default_zone) {
399                 strcat(buf, " ");
400                 strcat(buf, zone->name);
401         }
402 }
403
404 static ssize_t show_valid_zones(struct device *dev,
405                                 struct device_attribute *attr, char *buf)
406 {
407         struct memory_block *mem = to_memory_block(dev);
408         unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
409         unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
410         unsigned long valid_start_pfn, valid_end_pfn;
411         struct zone *default_zone;
412         int nid;
413
414         /*
415          * Check the existing zone. Make sure that we do that only on the
416          * online nodes otherwise the page_zone is not reliable
417          */
418         if (mem->state == MEM_ONLINE) {
419                 /*
420                  * The block contains more than one zone can not be offlined.
421                  * This can happen e.g. for ZONE_DMA and ZONE_DMA32
422                  */
423                 if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages,
424                                           &valid_start_pfn, &valid_end_pfn))
425                         return sprintf(buf, "none\n");
426                 start_pfn = valid_start_pfn;
427                 strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
428                 goto out;
429         }
430
431         nid = mem->nid;
432         default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
433         strcat(buf, default_zone->name);
434
435         print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
436                         default_zone);
437         print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
438                         default_zone);
439 out:
440         strcat(buf, "\n");
441
442         return strlen(buf);
443 }
444 static DEVICE_ATTR(valid_zones, 0444, show_valid_zones, NULL);
445 #endif
446
447 static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
448 static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state);
449 static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL);
450 static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL);
451
452 /*
453  * Block size attribute stuff
454  */
455 static ssize_t
456 print_block_size(struct device *dev, struct device_attribute *attr,
457                  char *buf)
458 {
459         return sprintf(buf, "%lx\n", get_memory_block_size());
460 }
461
462 static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL);
463
464 /*
465  * Memory auto online policy.
466  */
467
468 static ssize_t
469 show_auto_online_blocks(struct device *dev, struct device_attribute *attr,
470                         char *buf)
471 {
472         if (memhp_auto_online)
473                 return sprintf(buf, "online\n");
474         else
475                 return sprintf(buf, "offline\n");
476 }
477
478 static ssize_t
479 store_auto_online_blocks(struct device *dev, struct device_attribute *attr,
480                          const char *buf, size_t count)
481 {
482         if (sysfs_streq(buf, "online"))
483                 memhp_auto_online = true;
484         else if (sysfs_streq(buf, "offline"))
485                 memhp_auto_online = false;
486         else
487                 return -EINVAL;
488
489         return count;
490 }
491
492 static DEVICE_ATTR(auto_online_blocks, 0644, show_auto_online_blocks,
493                    store_auto_online_blocks);
494
495 /*
496  * Some architectures will have custom drivers to do this, and
497  * will not need to do it from userspace.  The fake hot-add code
498  * as well as ppc64 will do all of their discovery in userspace
499  * and will require this interface.
500  */
501 #ifdef CONFIG_ARCH_MEMORY_PROBE
502 static ssize_t
503 memory_probe_store(struct device *dev, struct device_attribute *attr,
504                    const char *buf, size_t count)
505 {
506         u64 phys_addr;
507         int nid, ret;
508         unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
509
510         ret = kstrtoull(buf, 0, &phys_addr);
511         if (ret)
512                 return ret;
513
514         if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
515                 return -EINVAL;
516
517         ret = lock_device_hotplug_sysfs();
518         if (ret)
519                 return ret;
520
521         nid = memory_add_physaddr_to_nid(phys_addr);
522         ret = __add_memory(nid, phys_addr,
523                            MIN_MEMORY_BLOCK_SIZE * sections_per_block);
524
525         if (ret)
526                 goto out;
527
528         ret = count;
529 out:
530         unlock_device_hotplug();
531         return ret;
532 }
533
534 static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
535 #endif
536
537 #ifdef CONFIG_MEMORY_FAILURE
538 /*
539  * Support for offlining pages of memory
540  */
541
542 /* Soft offline a page */
543 static ssize_t
544 store_soft_offline_page(struct device *dev,
545                         struct device_attribute *attr,
546                         const char *buf, size_t count)
547 {
548         int ret;
549         u64 pfn;
550         if (!capable(CAP_SYS_ADMIN))
551                 return -EPERM;
552         if (kstrtoull(buf, 0, &pfn) < 0)
553                 return -EINVAL;
554         pfn >>= PAGE_SHIFT;
555         if (!pfn_valid(pfn))
556                 return -ENXIO;
557         /* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
558         if (!pfn_to_online_page(pfn))
559                 return -EIO;
560         ret = soft_offline_page(pfn_to_page(pfn), 0);
561         return ret == 0 ? count : ret;
562 }
563
564 /* Forcibly offline a page, including killing processes. */
565 static ssize_t
566 store_hard_offline_page(struct device *dev,
567                         struct device_attribute *attr,
568                         const char *buf, size_t count)
569 {
570         int ret;
571         u64 pfn;
572         if (!capable(CAP_SYS_ADMIN))
573                 return -EPERM;
574         if (kstrtoull(buf, 0, &pfn) < 0)
575                 return -EINVAL;
576         pfn >>= PAGE_SHIFT;
577         ret = memory_failure(pfn, 0);
578         return ret ? ret : count;
579 }
580
581 static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page);
582 static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page);
583 #endif
584
585 /*
586  * Note that phys_device is optional.  It is here to allow for
587  * differentiation between which *physical* devices each
588  * section belongs to...
589  */
590 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
591 {
592         return 0;
593 }
594
595 /*
596  * A reference for the returned object is held and the reference for the
597  * hinted object is released.
598  */
599 static struct memory_block *find_memory_block_by_id(int block_id,
600                                                     struct memory_block *hint)
601 {
602         struct device *hintdev = hint ? &hint->dev : NULL;
603         struct device *dev;
604
605         dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
606         if (hint)
607                 put_device(&hint->dev);
608         if (!dev)
609                 return NULL;
610         return to_memory_block(dev);
611 }
612
613 struct memory_block *find_memory_block_hinted(struct mem_section *section,
614                                               struct memory_block *hint)
615 {
616         int block_id = base_memory_block_id(__section_nr(section));
617
618         return find_memory_block_by_id(block_id, hint);
619 }
620
621 /*
622  * For now, we have a linear search to go find the appropriate
623  * memory_block corresponding to a particular phys_index. If
624  * this gets to be a real problem, we can always use a radix
625  * tree or something here.
626  *
627  * This could be made generic for all device subsystems.
628  */
629 struct memory_block *find_memory_block(struct mem_section *section)
630 {
631         return find_memory_block_hinted(section, NULL);
632 }
633
634 static struct attribute *memory_memblk_attrs[] = {
635         &dev_attr_phys_index.attr,
636         &dev_attr_state.attr,
637         &dev_attr_phys_device.attr,
638         &dev_attr_removable.attr,
639 #ifdef CONFIG_MEMORY_HOTREMOVE
640         &dev_attr_valid_zones.attr,
641 #endif
642         NULL
643 };
644
645 static struct attribute_group memory_memblk_attr_group = {
646         .attrs = memory_memblk_attrs,
647 };
648
649 static const struct attribute_group *memory_memblk_attr_groups[] = {
650         &memory_memblk_attr_group,
651         NULL,
652 };
653
654 /*
655  * register_memory - Setup a sysfs device for a memory block
656  */
657 static
658 int register_memory(struct memory_block *memory)
659 {
660         int ret;
661
662         memory->dev.bus = &memory_subsys;
663         memory->dev.id = memory->start_section_nr / sections_per_block;
664         memory->dev.release = memory_block_release;
665         memory->dev.groups = memory_memblk_attr_groups;
666         memory->dev.offline = memory->state == MEM_OFFLINE;
667
668         ret = device_register(&memory->dev);
669         if (ret)
670                 put_device(&memory->dev);
671
672         return ret;
673 }
674
675 static int init_memory_block(struct memory_block **memory, int block_id,
676                              unsigned long state)
677 {
678         struct memory_block *mem;
679         unsigned long start_pfn;
680         int ret = 0;
681
682         mem = find_memory_block_by_id(block_id, NULL);
683         if (mem) {
684                 put_device(&mem->dev);
685                 return -EEXIST;
686         }
687         mem = kzalloc(sizeof(*mem), GFP_KERNEL);
688         if (!mem)
689                 return -ENOMEM;
690
691         mem->start_section_nr = block_id * sections_per_block;
692         mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
693         mem->state = state;
694         start_pfn = section_nr_to_pfn(mem->start_section_nr);
695         mem->phys_device = arch_get_memory_phys_device(start_pfn);
696         mem->nid = NUMA_NO_NODE;
697
698         ret = register_memory(mem);
699
700         *memory = mem;
701         return ret;
702 }
703
704 static int add_memory_block(int base_section_nr)
705 {
706         struct memory_block *mem;
707         int i, ret, section_count = 0;
708
709         for (i = base_section_nr;
710              i < base_section_nr + sections_per_block;
711              i++)
712                 if (present_section_nr(i))
713                         section_count++;
714
715         if (section_count == 0)
716                 return 0;
717         ret = init_memory_block(&mem, base_memory_block_id(base_section_nr),
718                                 MEM_ONLINE);
719         if (ret)
720                 return ret;
721         mem->section_count = section_count;
722         return 0;
723 }
724
725 static void unregister_memory(struct memory_block *memory)
726 {
727         if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
728                 return;
729
730         /* drop the ref. we got via find_memory_block() */
731         put_device(&memory->dev);
732         device_unregister(&memory->dev);
733 }
734
735 /*
736  * Create memory block devices for the given memory area. Start and size
737  * have to be aligned to memory block granularity. Memory block devices
738  * will be initialized as offline.
739  */
740 int create_memory_block_devices(unsigned long start, unsigned long size)
741 {
742         const int start_block_id = pfn_to_block_id(PFN_DOWN(start));
743         int end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
744         struct memory_block *mem;
745         unsigned long block_id;
746         int ret = 0;
747
748         if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
749                          !IS_ALIGNED(size, memory_block_size_bytes())))
750                 return -EINVAL;
751
752         mutex_lock(&mem_sysfs_mutex);
753         for (block_id = start_block_id; block_id != end_block_id; block_id++) {
754                 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
755                 if (ret)
756                         break;
757                 mem->section_count = sections_per_block;
758         }
759         if (ret) {
760                 end_block_id = block_id;
761                 for (block_id = start_block_id; block_id != end_block_id;
762                      block_id++) {
763                         mem = find_memory_block_by_id(block_id, NULL);
764                         mem->section_count = 0;
765                         unregister_memory(mem);
766                 }
767         }
768         mutex_unlock(&mem_sysfs_mutex);
769         return ret;
770 }
771
772 /*
773  * Remove memory block devices for the given memory area. Start and size
774  * have to be aligned to memory block granularity. Memory block devices
775  * have to be offline.
776  */
777 void remove_memory_block_devices(unsigned long start, unsigned long size)
778 {
779         const int start_block_id = pfn_to_block_id(PFN_DOWN(start));
780         const int end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
781         struct memory_block *mem;
782         int block_id;
783
784         if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
785                          !IS_ALIGNED(size, memory_block_size_bytes())))
786                 return;
787
788         mutex_lock(&mem_sysfs_mutex);
789         for (block_id = start_block_id; block_id != end_block_id; block_id++) {
790                 mem = find_memory_block_by_id(block_id, NULL);
791                 if (WARN_ON_ONCE(!mem))
792                         continue;
793                 mem->section_count = 0;
794                 unregister_memory_block_under_nodes(mem);
795                 unregister_memory(mem);
796         }
797         mutex_unlock(&mem_sysfs_mutex);
798 }
799
800 /* return true if the memory block is offlined, otherwise, return false */
801 bool is_memblock_offlined(struct memory_block *mem)
802 {
803         return mem->state == MEM_OFFLINE;
804 }
805
806 static struct attribute *memory_root_attrs[] = {
807 #ifdef CONFIG_ARCH_MEMORY_PROBE
808         &dev_attr_probe.attr,
809 #endif
810
811 #ifdef CONFIG_MEMORY_FAILURE
812         &dev_attr_soft_offline_page.attr,
813         &dev_attr_hard_offline_page.attr,
814 #endif
815
816         &dev_attr_block_size_bytes.attr,
817         &dev_attr_auto_online_blocks.attr,
818         NULL
819 };
820
821 static struct attribute_group memory_root_attr_group = {
822         .attrs = memory_root_attrs,
823 };
824
825 static const struct attribute_group *memory_root_attr_groups[] = {
826         &memory_root_attr_group,
827         NULL,
828 };
829
830 /*
831  * Initialize the sysfs support for memory devices...
832  */
833 int __init memory_dev_init(void)
834 {
835         unsigned int i;
836         int ret;
837         int err;
838         unsigned long block_sz;
839
840         ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
841         if (ret)
842                 goto out;
843
844         block_sz = get_memory_block_size();
845         sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
846
847         /*
848          * Create entries for memory sections that were found
849          * during boot and have been initialized
850          */
851         mutex_lock(&mem_sysfs_mutex);
852         for (i = 0; i <= __highest_present_section_nr;
853                 i += sections_per_block) {
854                 err = add_memory_block(i);
855                 if (!ret)
856                         ret = err;
857         }
858         mutex_unlock(&mem_sysfs_mutex);
859
860 out:
861         if (ret)
862                 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
863         return ret;
864 }
865
866 struct for_each_memory_block_cb_data {
867         walk_memory_blocks_func_t func;
868         void *arg;
869 };
870
871 static int for_each_memory_block_cb(struct device *dev, void *data)
872 {
873         struct memory_block *mem = to_memory_block(dev);
874         struct for_each_memory_block_cb_data *cb_data = data;
875
876         return cb_data->func(mem, cb_data->arg);
877 }
878
879 /**
880  * for_each_memory_block - walk through all present memory blocks
881  *
882  * @arg: argument passed to func
883  * @func: callback for each memory block walked
884  *
885  * This function walks through all present memory blocks, calling func on
886  * each memory block.
887  *
888  * In case func() returns an error, walking is aborted and the error is
889  * returned.
890  */
891 int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
892 {
893         struct for_each_memory_block_cb_data cb_data = {
894                 .func = func,
895                 .arg = arg,
896         };
897
898         return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
899                                 for_each_memory_block_cb);
900 }