f180427e48f4e59341795c3090d51d84a5f83dc8
[sfrench/cifs-2.6.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 int memory_subsys_online(struct device *dev);
43 static int memory_subsys_offline(struct device *dev);
44
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,
50 };
51
52 static BLOCKING_NOTIFIER_HEAD(memory_chain);
53
54 int register_memory_notifier(struct notifier_block *nb)
55 {
56         return blocking_notifier_chain_register(&memory_chain, nb);
57 }
58 EXPORT_SYMBOL(register_memory_notifier);
59
60 void unregister_memory_notifier(struct notifier_block *nb)
61 {
62         blocking_notifier_chain_unregister(&memory_chain, nb);
63 }
64 EXPORT_SYMBOL(unregister_memory_notifier);
65
66 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
67
68 int register_memory_isolate_notifier(struct notifier_block *nb)
69 {
70         return atomic_notifier_chain_register(&memory_isolate_chain, nb);
71 }
72 EXPORT_SYMBOL(register_memory_isolate_notifier);
73
74 void unregister_memory_isolate_notifier(struct notifier_block *nb)
75 {
76         atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
77 }
78 EXPORT_SYMBOL(unregister_memory_isolate_notifier);
79
80 static void memory_block_release(struct device *dev)
81 {
82         struct memory_block *mem = to_memory_block(dev);
83
84         kfree(mem);
85 }
86
87 unsigned long __weak memory_block_size_bytes(void)
88 {
89         return MIN_MEMORY_BLOCK_SIZE;
90 }
91 EXPORT_SYMBOL_GPL(memory_block_size_bytes);
92
93 static unsigned long get_memory_block_size(void)
94 {
95         unsigned long block_sz;
96
97         block_sz = memory_block_size_bytes();
98
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;
103         }
104
105         return block_sz;
106 }
107
108 /*
109  * use this as the physical section index that this memsection
110  * uses.
111  */
112
113 static ssize_t phys_index_show(struct device *dev,
114                                struct device_attribute *attr, char *buf)
115 {
116         struct memory_block *mem = to_memory_block(dev);
117         unsigned long phys_index;
118
119         phys_index = mem->start_section_nr / sections_per_block;
120         return sprintf(buf, "%08lx\n", phys_index);
121 }
122
123 /*
124  * Show whether the section of memory is likely to be hot-removable
125  */
126 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
127                               char *buf)
128 {
129         unsigned long i, pfn;
130         int ret = 1;
131         struct memory_block *mem = to_memory_block(dev);
132
133         if (mem->state != MEM_ONLINE)
134                 goto out;
135
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);
141         }
142
143 out:
144         return sprintf(buf, "%d\n", ret);
145 }
146
147 /*
148  * online, offline, going offline, etc.
149  */
150 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
151                           char *buf)
152 {
153         struct memory_block *mem = to_memory_block(dev);
154         ssize_t len = 0;
155
156         /*
157          * We can probably put these states in a nice little array
158          * so that they're not open-coded
159          */
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;
175         }
176
177         return len;
178 }
179
180 int memory_notify(unsigned long val, void *v)
181 {
182         return blocking_notifier_call_chain(&memory_chain, val, v);
183 }
184
185 int memory_isolate_notify(unsigned long val, void *v)
186 {
187         return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
188 }
189
190 /*
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.
194  */
195 static bool pages_correctly_probed(unsigned long start_pfn)
196 {
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;
200
201         /*
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
205          */
206         for (; section_nr < section_nr_end; section_nr++) {
207                 if (WARN_ON_ONCE(!pfn_valid(pfn)))
208                         return false;
209
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;
222                 }
223                 pfn += PAGES_PER_SECTION;
224         }
225
226         return true;
227 }
228
229 /*
230  * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
231  * OK to have direct references to sparsemem variables in here.
232  */
233 static int
234 memory_block_action(unsigned long start_section_nr, unsigned long action,
235                     int online_type)
236 {
237         unsigned long start_pfn;
238         unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
239         int ret;
240
241         start_pfn = section_nr_to_pfn(start_section_nr);
242
243         switch (action) {
244         case MEM_ONLINE:
245                 if (!pages_correctly_probed(start_pfn))
246                         return -EBUSY;
247
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;
257         }
258
259         return ret;
260 }
261
262 static int memory_block_change_state(struct memory_block *mem,
263                 unsigned long to_state, unsigned long from_state_req)
264 {
265         int ret = 0;
266
267         if (mem->state != from_state_req)
268                 return -EINVAL;
269
270         if (to_state == MEM_OFFLINE)
271                 mem->state = MEM_GOING_OFFLINE;
272
273         ret = memory_block_action(mem->start_section_nr, to_state,
274                                 mem->online_type);
275
276         mem->state = ret ? from_state_req : to_state;
277
278         return ret;
279 }
280
281 /* The device lock serializes operations on memory_subsys_[online|offline] */
282 static int memory_subsys_online(struct device *dev)
283 {
284         struct memory_block *mem = to_memory_block(dev);
285         int ret;
286
287         if (mem->state == MEM_ONLINE)
288                 return 0;
289
290         /*
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.
294          */
295         if (mem->online_type < 0)
296                 mem->online_type = MMOP_ONLINE_KEEP;
297
298         ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
299
300         /* clear online_type */
301         mem->online_type = -1;
302
303         return ret;
304 }
305
306 static int memory_subsys_offline(struct device *dev)
307 {
308         struct memory_block *mem = to_memory_block(dev);
309
310         if (mem->state == MEM_OFFLINE)
311                 return 0;
312
313         /* Can't offline block with non-present sections */
314         if (mem->section_count != sections_per_block)
315                 return -EINVAL;
316
317         return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
318 }
319
320 static ssize_t state_store(struct device *dev, struct device_attribute *attr,
321                            const char *buf, size_t count)
322 {
323         struct memory_block *mem = to_memory_block(dev);
324         int ret, online_type;
325
326         ret = lock_device_hotplug_sysfs();
327         if (ret)
328                 return ret;
329
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;
341         }
342
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 */
356         }
357
358 err:
359         unlock_device_hotplug();
360
361         if (ret < 0)
362                 return ret;
363         if (ret)
364                 return -EINVAL;
365
366         return count;
367 }
368
369 /*
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?
377  */
378 static ssize_t phys_device_show(struct device *dev,
379                                 struct device_attribute *attr, char *buf)
380 {
381         struct memory_block *mem = to_memory_block(dev);
382         return sprintf(buf, "%d\n", mem->phys_device);
383 }
384
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)
389 {
390         struct zone *zone;
391
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);
396         }
397 }
398
399 static ssize_t valid_zones_show(struct device *dev,
400                                 struct device_attribute *attr, char *buf)
401 {
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;
408
409         /*
410          * Check the existing zone. Make sure that we do that only on the
411          * online nodes otherwise the page_zone is not reliable
412          */
413         if (mem->state == MEM_ONLINE) {
414                 /*
415                  * The block contains more than one zone can not be offlined.
416                  * This can happen e.g. for ZONE_DMA and ZONE_DMA32
417                  */
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;
424         }
425
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);
429
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");
436
437         return strlen(buf);
438 }
439 static DEVICE_ATTR_RO(valid_zones);
440 #endif
441
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);
446
447 /*
448  * Block size attribute stuff
449  */
450 static ssize_t block_size_bytes_show(struct device *dev,
451                                      struct device_attribute *attr, char *buf)
452 {
453         return sprintf(buf, "%lx\n", get_memory_block_size());
454 }
455
456 static DEVICE_ATTR_RO(block_size_bytes);
457
458 /*
459  * Memory auto online policy.
460  */
461
462 static ssize_t auto_online_blocks_show(struct device *dev,
463                                        struct device_attribute *attr, char *buf)
464 {
465         if (memhp_auto_online)
466                 return sprintf(buf, "online\n");
467         else
468                 return sprintf(buf, "offline\n");
469 }
470
471 static ssize_t auto_online_blocks_store(struct device *dev,
472                                         struct device_attribute *attr,
473                                         const char *buf, size_t count)
474 {
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;
481
482         return count;
483 }
484
485 static DEVICE_ATTR_RW(auto_online_blocks);
486
487 /*
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.
492  */
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)
496 {
497         u64 phys_addr;
498         int nid, ret;
499         unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
500
501         ret = kstrtoull(buf, 0, &phys_addr);
502         if (ret)
503                 return ret;
504
505         if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
506                 return -EINVAL;
507
508         ret = lock_device_hotplug_sysfs();
509         if (ret)
510                 return ret;
511
512         nid = memory_add_physaddr_to_nid(phys_addr);
513         ret = __add_memory(nid, phys_addr,
514                            MIN_MEMORY_BLOCK_SIZE * sections_per_block);
515
516         if (ret)
517                 goto out;
518
519         ret = count;
520 out:
521         unlock_device_hotplug();
522         return ret;
523 }
524
525 static DEVICE_ATTR_WO(probe);
526 #endif
527
528 #ifdef CONFIG_MEMORY_FAILURE
529 /*
530  * Support for offlining pages of memory
531  */
532
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)
537 {
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;
549 }
550
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)
555 {
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;
565 }
566
567 static DEVICE_ATTR_WO(soft_offline_page);
568 static DEVICE_ATTR_WO(hard_offline_page);
569 #endif
570
571 /*
572  * Note that phys_device is optional.  It is here to allow for
573  * differentiation between which *physical* devices each
574  * section belongs to...
575  */
576 int __weak arch_get_memory_phys_device(unsigned long start_pfn)
577 {
578         return 0;
579 }
580
581 /*
582  * A reference for the returned object is held and the reference for the
583  * hinted object is released.
584  */
585 struct memory_block *find_memory_block_hinted(struct mem_section *section,
586                                               struct memory_block *hint)
587 {
588         int block_id = base_memory_block_id(__section_nr(section));
589         struct device *hintdev = hint ? &hint->dev : NULL;
590         struct device *dev;
591
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);
598 }
599
600 /*
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.
605  *
606  * This could be made generic for all device subsystems.
607  */
608 struct memory_block *find_memory_block(struct mem_section *section)
609 {
610         return find_memory_block_hinted(section, NULL);
611 }
612
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
622 };
623
624 static struct attribute_group memory_memblk_attr_group = {
625         .attrs = memory_memblk_attrs,
626 };
627
628 static const struct attribute_group *memory_memblk_attr_groups[] = {
629         &memory_memblk_attr_group,
630         NULL,
631 };
632
633 /*
634  * register_memory - Setup a sysfs device for a memory block
635  */
636 static
637 int register_memory(struct memory_block *memory)
638 {
639         int ret;
640
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;
646
647         ret = device_register(&memory->dev);
648         if (ret)
649                 put_device(&memory->dev);
650
651         return ret;
652 }
653
654 static int init_memory_block(struct memory_block **memory,
655                              struct mem_section *section, unsigned long state)
656 {
657         struct memory_block *mem;
658         unsigned long start_pfn;
659         int scn_nr;
660         int ret = 0;
661
662         mem = kzalloc(sizeof(*mem), GFP_KERNEL);
663         if (!mem)
664                 return -ENOMEM;
665
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);
673
674         ret = register_memory(mem);
675
676         *memory = mem;
677         return ret;
678 }
679
680 static int add_memory_block(int base_section_nr)
681 {
682         struct memory_block *mem;
683         int i, ret, section_count = 0, section_nr;
684
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++;
693         }
694
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;
702 }
703
704 /*
705  * need an interface for the VM to add new memory regions,
706  * but without onlining it.
707  */
708 int hotplug_memory_register(int nid, struct mem_section *section)
709 {
710         int ret = 0;
711         struct memory_block *mem;
712
713         mutex_lock(&mem_sysfs_mutex);
714
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++;
724         }
725
726 out:
727         mutex_unlock(&mem_sysfs_mutex);
728         return ret;
729 }
730
731 #ifdef CONFIG_MEMORY_HOTREMOVE
732 static void
733 unregister_memory(struct memory_block *memory)
734 {
735         BUG_ON(memory->dev.bus != &memory_subsys);
736
737         /* drop the ref. we got via find_memory_block() */
738         put_device(&memory->dev);
739         device_unregister(&memory->dev);
740 }
741
742 void unregister_memory_section(struct mem_section *section)
743 {
744         struct memory_block *mem;
745
746         if (WARN_ON_ONCE(!present_section(section)))
747                 return;
748
749         mutex_lock(&mem_sysfs_mutex);
750
751         /*
752          * Some users of the memory hotplug do not want/need memblock to
753          * track all sections. Skip over those.
754          */
755         mem = find_memory_block(section);
756         if (!mem)
757                 goto out_unlock;
758
759         unregister_mem_sect_under_nodes(mem, __section_nr(section));
760
761         mem->section_count--;
762         if (mem->section_count == 0)
763                 unregister_memory(mem);
764         else
765                 put_device(&mem->dev);
766
767 out_unlock:
768         mutex_unlock(&mem_sysfs_mutex);
769 }
770 #endif /* CONFIG_MEMORY_HOTREMOVE */
771
772 /* return true if the memory block is offlined, otherwise, return false */
773 bool is_memblock_offlined(struct memory_block *mem)
774 {
775         return mem->state == MEM_OFFLINE;
776 }
777
778 static struct attribute *memory_root_attrs[] = {
779 #ifdef CONFIG_ARCH_MEMORY_PROBE
780         &dev_attr_probe.attr,
781 #endif
782
783 #ifdef CONFIG_MEMORY_FAILURE
784         &dev_attr_soft_offline_page.attr,
785         &dev_attr_hard_offline_page.attr,
786 #endif
787
788         &dev_attr_block_size_bytes.attr,
789         &dev_attr_auto_online_blocks.attr,
790         NULL
791 };
792
793 static struct attribute_group memory_root_attr_group = {
794         .attrs = memory_root_attrs,
795 };
796
797 static const struct attribute_group *memory_root_attr_groups[] = {
798         &memory_root_attr_group,
799         NULL,
800 };
801
802 /*
803  * Initialize the sysfs support for memory devices...
804  */
805 int __init memory_dev_init(void)
806 {
807         unsigned int i;
808         int ret;
809         int err;
810         unsigned long block_sz;
811
812         ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
813         if (ret)
814                 goto out;
815
816         block_sz = get_memory_block_size();
817         sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
818
819         /*
820          * Create entries for memory sections that were found
821          * during boot and have been initialized
822          */
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;
829         }
830         mutex_unlock(&mem_sysfs_mutex);
831
832 out:
833         if (ret)
834                 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
835         return ret;
836 }