blob: 5a8c430fb8ffa71601f135c854a1ad20e50079d4 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// 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
29static 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
35static int sections_per_block;
36
David Brazdil0f672f62019-12-10 10:32:29 +000037static inline unsigned long base_memory_block_id(unsigned long section_nr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000038{
39 return section_nr / sections_per_block;
40}
41
David Brazdil0f672f62019-12-10 10:32:29 +000042static inline unsigned long pfn_to_block_id(unsigned long pfn)
43{
44 return base_memory_block_id(pfn_to_section_nr(pfn));
45}
46
47static inline unsigned long phys_to_block_id(unsigned long phys)
48{
49 return pfn_to_block_id(PFN_DOWN(phys));
50}
51
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000052static int memory_subsys_online(struct device *dev);
53static int memory_subsys_offline(struct device *dev);
54
55static struct bus_type memory_subsys = {
56 .name = MEMORY_CLASS_NAME,
57 .dev_name = MEMORY_CLASS_NAME,
58 .online = memory_subsys_online,
59 .offline = memory_subsys_offline,
60};
61
62static BLOCKING_NOTIFIER_HEAD(memory_chain);
63
64int register_memory_notifier(struct notifier_block *nb)
65{
66 return blocking_notifier_chain_register(&memory_chain, nb);
67}
68EXPORT_SYMBOL(register_memory_notifier);
69
70void unregister_memory_notifier(struct notifier_block *nb)
71{
72 blocking_notifier_chain_unregister(&memory_chain, nb);
73}
74EXPORT_SYMBOL(unregister_memory_notifier);
75
76static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
77
78int register_memory_isolate_notifier(struct notifier_block *nb)
79{
80 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
81}
82EXPORT_SYMBOL(register_memory_isolate_notifier);
83
84void unregister_memory_isolate_notifier(struct notifier_block *nb)
85{
86 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
87}
88EXPORT_SYMBOL(unregister_memory_isolate_notifier);
89
90static void memory_block_release(struct device *dev)
91{
92 struct memory_block *mem = to_memory_block(dev);
93
94 kfree(mem);
95}
96
97unsigned long __weak memory_block_size_bytes(void)
98{
99 return MIN_MEMORY_BLOCK_SIZE;
100}
David Brazdil0f672f62019-12-10 10:32:29 +0000101EXPORT_SYMBOL_GPL(memory_block_size_bytes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000102
103/*
David Brazdil0f672f62019-12-10 10:32:29 +0000104 * Show the first physical section index (number) of this memory block.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105 */
David Brazdil0f672f62019-12-10 10:32:29 +0000106static ssize_t phys_index_show(struct device *dev,
107 struct device_attribute *attr, char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000108{
109 struct memory_block *mem = to_memory_block(dev);
110 unsigned long phys_index;
111
112 phys_index = mem->start_section_nr / sections_per_block;
113 return sprintf(buf, "%08lx\n", phys_index);
114}
115
116/*
Olivier Deprez0e641232021-09-23 10:07:05 +0200117 * Legacy interface that we cannot remove. Always indicate "removable"
118 * with CONFIG_MEMORY_HOTREMOVE - bad heuristic.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000119 */
David Brazdil0f672f62019-12-10 10:32:29 +0000120static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
121 char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000122{
Olivier Deprez0e641232021-09-23 10:07:05 +0200123 return sprintf(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000124}
125
126/*
127 * online, offline, going offline, etc.
128 */
David Brazdil0f672f62019-12-10 10:32:29 +0000129static ssize_t state_show(struct device *dev, struct device_attribute *attr,
130 char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131{
132 struct memory_block *mem = to_memory_block(dev);
133 ssize_t len = 0;
134
135 /*
136 * We can probably put these states in a nice little array
137 * so that they're not open-coded
138 */
139 switch (mem->state) {
140 case MEM_ONLINE:
141 len = sprintf(buf, "online\n");
142 break;
143 case MEM_OFFLINE:
144 len = sprintf(buf, "offline\n");
145 break;
146 case MEM_GOING_OFFLINE:
147 len = sprintf(buf, "going-offline\n");
148 break;
149 default:
150 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
151 mem->state);
152 WARN_ON(1);
153 break;
154 }
155
156 return len;
157}
158
159int memory_notify(unsigned long val, void *v)
160{
161 return blocking_notifier_call_chain(&memory_chain, val, v);
162}
163
164int memory_isolate_notify(unsigned long val, void *v)
165{
166 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
167}
168
169/*
170 * The probe routines leave the pages uninitialized, just as the bootmem code
171 * does. Make sure we do not access them, but instead use only information from
172 * within sections.
173 */
174static bool pages_correctly_probed(unsigned long start_pfn)
175{
176 unsigned long section_nr = pfn_to_section_nr(start_pfn);
177 unsigned long section_nr_end = section_nr + sections_per_block;
178 unsigned long pfn = start_pfn;
179
180 /*
181 * memmap between sections is not contiguous except with
182 * SPARSEMEM_VMEMMAP. We lookup the page once per section
183 * and assume memmap is contiguous within each section
184 */
185 for (; section_nr < section_nr_end; section_nr++) {
186 if (WARN_ON_ONCE(!pfn_valid(pfn)))
187 return false;
188
189 if (!present_section_nr(section_nr)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000190 pr_warn("section %ld pfn[%lx, %lx) not present\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000191 section_nr, pfn, pfn + PAGES_PER_SECTION);
192 return false;
193 } else if (!valid_section_nr(section_nr)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000194 pr_warn("section %ld pfn[%lx, %lx) no valid memmap\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195 section_nr, pfn, pfn + PAGES_PER_SECTION);
196 return false;
197 } else if (online_section_nr(section_nr)) {
David Brazdil0f672f62019-12-10 10:32:29 +0000198 pr_warn("section %ld pfn[%lx, %lx) is already online\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000199 section_nr, pfn, pfn + PAGES_PER_SECTION);
200 return false;
201 }
202 pfn += PAGES_PER_SECTION;
203 }
204
205 return true;
206}
207
208/*
209 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
210 * OK to have direct references to sparsemem variables in here.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000211 */
212static int
David Brazdil0f672f62019-12-10 10:32:29 +0000213memory_block_action(unsigned long start_section_nr, unsigned long action,
214 int online_type)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000215{
216 unsigned long start_pfn;
217 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
218 int ret;
219
David Brazdil0f672f62019-12-10 10:32:29 +0000220 start_pfn = section_nr_to_pfn(start_section_nr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000221
222 switch (action) {
223 case MEM_ONLINE:
224 if (!pages_correctly_probed(start_pfn))
225 return -EBUSY;
226
227 ret = online_pages(start_pfn, nr_pages, online_type);
228 break;
229 case MEM_OFFLINE:
230 ret = offline_pages(start_pfn, nr_pages);
231 break;
232 default:
233 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
David Brazdil0f672f62019-12-10 10:32:29 +0000234 "%ld\n", __func__, start_section_nr, action, action);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000235 ret = -EINVAL;
236 }
237
238 return ret;
239}
240
241static int memory_block_change_state(struct memory_block *mem,
242 unsigned long to_state, unsigned long from_state_req)
243{
244 int ret = 0;
245
246 if (mem->state != from_state_req)
247 return -EINVAL;
248
249 if (to_state == MEM_OFFLINE)
250 mem->state = MEM_GOING_OFFLINE;
251
252 ret = memory_block_action(mem->start_section_nr, to_state,
253 mem->online_type);
254
255 mem->state = ret ? from_state_req : to_state;
256
257 return ret;
258}
259
260/* The device lock serializes operations on memory_subsys_[online|offline] */
261static int memory_subsys_online(struct device *dev)
262{
263 struct memory_block *mem = to_memory_block(dev);
264 int ret;
265
266 if (mem->state == MEM_ONLINE)
267 return 0;
268
269 /*
David Brazdil0f672f62019-12-10 10:32:29 +0000270 * If we are called from state_store(), online_type will be
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000271 * set >= 0 Otherwise we were called from the device online
272 * attribute and need to set the online_type.
273 */
274 if (mem->online_type < 0)
275 mem->online_type = MMOP_ONLINE_KEEP;
276
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000277 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
278
279 /* clear online_type */
280 mem->online_type = -1;
281
282 return ret;
283}
284
285static int memory_subsys_offline(struct device *dev)
286{
287 struct memory_block *mem = to_memory_block(dev);
288
289 if (mem->state == MEM_OFFLINE)
290 return 0;
291
292 /* Can't offline block with non-present sections */
293 if (mem->section_count != sections_per_block)
294 return -EINVAL;
295
296 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
297}
298
David Brazdil0f672f62019-12-10 10:32:29 +0000299static ssize_t state_store(struct device *dev, struct device_attribute *attr,
300 const char *buf, size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000301{
302 struct memory_block *mem = to_memory_block(dev);
303 int ret, online_type;
304
305 ret = lock_device_hotplug_sysfs();
306 if (ret)
307 return ret;
308
309 if (sysfs_streq(buf, "online_kernel"))
310 online_type = MMOP_ONLINE_KERNEL;
311 else if (sysfs_streq(buf, "online_movable"))
312 online_type = MMOP_ONLINE_MOVABLE;
313 else if (sysfs_streq(buf, "online"))
314 online_type = MMOP_ONLINE_KEEP;
315 else if (sysfs_streq(buf, "offline"))
316 online_type = MMOP_OFFLINE;
317 else {
318 ret = -EINVAL;
319 goto err;
320 }
321
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000322 switch (online_type) {
323 case MMOP_ONLINE_KERNEL:
324 case MMOP_ONLINE_MOVABLE:
325 case MMOP_ONLINE_KEEP:
David Brazdil0f672f62019-12-10 10:32:29 +0000326 /* mem->online_type is protected by device_hotplug_lock */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000327 mem->online_type = online_type;
328 ret = device_online(&mem->dev);
329 break;
330 case MMOP_OFFLINE:
331 ret = device_offline(&mem->dev);
332 break;
333 default:
334 ret = -EINVAL; /* should never happen */
335 }
336
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000337err:
338 unlock_device_hotplug();
339
340 if (ret < 0)
341 return ret;
342 if (ret)
343 return -EINVAL;
344
345 return count;
346}
347
348/*
349 * phys_device is a bad name for this. What I really want
350 * is a way to differentiate between memory ranges that
351 * are part of physical devices that constitute
352 * a complete removable unit or fru.
353 * i.e. do these ranges belong to the same physical device,
354 * s.t. if I offline all of these sections I can then
355 * remove the physical device?
356 */
David Brazdil0f672f62019-12-10 10:32:29 +0000357static ssize_t phys_device_show(struct device *dev,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000358 struct device_attribute *attr, char *buf)
359{
360 struct memory_block *mem = to_memory_block(dev);
361 return sprintf(buf, "%d\n", mem->phys_device);
362}
363
364#ifdef CONFIG_MEMORY_HOTREMOVE
365static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
366 unsigned long nr_pages, int online_type,
367 struct zone *default_zone)
368{
369 struct zone *zone;
370
371 zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
372 if (zone != default_zone) {
373 strcat(buf, " ");
374 strcat(buf, zone->name);
375 }
376}
377
David Brazdil0f672f62019-12-10 10:32:29 +0000378static ssize_t valid_zones_show(struct device *dev,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000379 struct device_attribute *attr, char *buf)
380{
381 struct memory_block *mem = to_memory_block(dev);
382 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
383 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
384 unsigned long valid_start_pfn, valid_end_pfn;
385 struct zone *default_zone;
386 int nid;
387
388 /*
389 * Check the existing zone. Make sure that we do that only on the
390 * online nodes otherwise the page_zone is not reliable
391 */
392 if (mem->state == MEM_ONLINE) {
393 /*
394 * The block contains more than one zone can not be offlined.
395 * This can happen e.g. for ZONE_DMA and ZONE_DMA32
396 */
397 if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages,
398 &valid_start_pfn, &valid_end_pfn))
399 return sprintf(buf, "none\n");
400 start_pfn = valid_start_pfn;
401 strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
402 goto out;
403 }
404
405 nid = mem->nid;
406 default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
407 strcat(buf, default_zone->name);
408
409 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
410 default_zone);
411 print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
412 default_zone);
413out:
414 strcat(buf, "\n");
415
416 return strlen(buf);
417}
David Brazdil0f672f62019-12-10 10:32:29 +0000418static DEVICE_ATTR_RO(valid_zones);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000419#endif
420
David Brazdil0f672f62019-12-10 10:32:29 +0000421static DEVICE_ATTR_RO(phys_index);
422static DEVICE_ATTR_RW(state);
423static DEVICE_ATTR_RO(phys_device);
424static DEVICE_ATTR_RO(removable);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000425
426/*
David Brazdil0f672f62019-12-10 10:32:29 +0000427 * Show the memory block size (shared by all memory blocks).
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000428 */
David Brazdil0f672f62019-12-10 10:32:29 +0000429static ssize_t block_size_bytes_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000431{
David Brazdil0f672f62019-12-10 10:32:29 +0000432 return sprintf(buf, "%lx\n", memory_block_size_bytes());
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000433}
434
David Brazdil0f672f62019-12-10 10:32:29 +0000435static DEVICE_ATTR_RO(block_size_bytes);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000436
437/*
438 * Memory auto online policy.
439 */
440
David Brazdil0f672f62019-12-10 10:32:29 +0000441static ssize_t auto_online_blocks_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000443{
444 if (memhp_auto_online)
445 return sprintf(buf, "online\n");
446 else
447 return sprintf(buf, "offline\n");
448}
449
David Brazdil0f672f62019-12-10 10:32:29 +0000450static ssize_t auto_online_blocks_store(struct device *dev,
451 struct device_attribute *attr,
452 const char *buf, size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000453{
454 if (sysfs_streq(buf, "online"))
455 memhp_auto_online = true;
456 else if (sysfs_streq(buf, "offline"))
457 memhp_auto_online = false;
458 else
459 return -EINVAL;
460
461 return count;
462}
463
David Brazdil0f672f62019-12-10 10:32:29 +0000464static DEVICE_ATTR_RW(auto_online_blocks);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000465
466/*
467 * Some architectures will have custom drivers to do this, and
468 * will not need to do it from userspace. The fake hot-add code
469 * as well as ppc64 will do all of their discovery in userspace
470 * and will require this interface.
471 */
472#ifdef CONFIG_ARCH_MEMORY_PROBE
David Brazdil0f672f62019-12-10 10:32:29 +0000473static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
474 const char *buf, size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000475{
476 u64 phys_addr;
477 int nid, ret;
478 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
479
480 ret = kstrtoull(buf, 0, &phys_addr);
481 if (ret)
482 return ret;
483
484 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
485 return -EINVAL;
486
David Brazdil0f672f62019-12-10 10:32:29 +0000487 ret = lock_device_hotplug_sysfs();
488 if (ret)
489 return ret;
490
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000491 nid = memory_add_physaddr_to_nid(phys_addr);
David Brazdil0f672f62019-12-10 10:32:29 +0000492 ret = __add_memory(nid, phys_addr,
493 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000494
495 if (ret)
496 goto out;
497
498 ret = count;
499out:
David Brazdil0f672f62019-12-10 10:32:29 +0000500 unlock_device_hotplug();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000501 return ret;
502}
503
David Brazdil0f672f62019-12-10 10:32:29 +0000504static DEVICE_ATTR_WO(probe);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000505#endif
506
507#ifdef CONFIG_MEMORY_FAILURE
508/*
509 * Support for offlining pages of memory
510 */
511
512/* Soft offline a page */
David Brazdil0f672f62019-12-10 10:32:29 +0000513static ssize_t soft_offline_page_store(struct device *dev,
514 struct device_attribute *attr,
515 const char *buf, size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000516{
517 int ret;
518 u64 pfn;
519 if (!capable(CAP_SYS_ADMIN))
520 return -EPERM;
521 if (kstrtoull(buf, 0, &pfn) < 0)
522 return -EINVAL;
523 pfn >>= PAGE_SHIFT;
524 if (!pfn_valid(pfn))
525 return -ENXIO;
David Brazdil0f672f62019-12-10 10:32:29 +0000526 /* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
527 if (!pfn_to_online_page(pfn))
528 return -EIO;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000529 ret = soft_offline_page(pfn_to_page(pfn), 0);
530 return ret == 0 ? count : ret;
531}
532
533/* Forcibly offline a page, including killing processes. */
David Brazdil0f672f62019-12-10 10:32:29 +0000534static ssize_t hard_offline_page_store(struct device *dev,
535 struct device_attribute *attr,
536 const char *buf, size_t count)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000537{
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 ret = memory_failure(pfn, 0);
546 return ret ? ret : count;
547}
548
David Brazdil0f672f62019-12-10 10:32:29 +0000549static DEVICE_ATTR_WO(soft_offline_page);
550static DEVICE_ATTR_WO(hard_offline_page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000551#endif
552
553/*
554 * Note that phys_device is optional. It is here to allow for
555 * differentiation between which *physical* devices each
556 * section belongs to...
557 */
558int __weak arch_get_memory_phys_device(unsigned long start_pfn)
559{
560 return 0;
561}
562
David Brazdil0f672f62019-12-10 10:32:29 +0000563/* A reference for the returned memory block device is acquired. */
564static struct memory_block *find_memory_block_by_id(unsigned long block_id)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000565{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000566 struct device *dev;
567
David Brazdil0f672f62019-12-10 10:32:29 +0000568 dev = subsys_find_device_by_id(&memory_subsys, block_id, NULL);
569 return dev ? to_memory_block(dev) : NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000570}
571
572/*
573 * For now, we have a linear search to go find the appropriate
574 * memory_block corresponding to a particular phys_index. If
575 * this gets to be a real problem, we can always use a radix
576 * tree or something here.
577 *
578 * This could be made generic for all device subsystems.
579 */
580struct memory_block *find_memory_block(struct mem_section *section)
581{
David Brazdil0f672f62019-12-10 10:32:29 +0000582 unsigned long block_id = base_memory_block_id(__section_nr(section));
583
584 return find_memory_block_by_id(block_id);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000585}
586
587static struct attribute *memory_memblk_attrs[] = {
588 &dev_attr_phys_index.attr,
589 &dev_attr_state.attr,
590 &dev_attr_phys_device.attr,
591 &dev_attr_removable.attr,
592#ifdef CONFIG_MEMORY_HOTREMOVE
593 &dev_attr_valid_zones.attr,
594#endif
595 NULL
596};
597
598static struct attribute_group memory_memblk_attr_group = {
599 .attrs = memory_memblk_attrs,
600};
601
602static const struct attribute_group *memory_memblk_attr_groups[] = {
603 &memory_memblk_attr_group,
604 NULL,
605};
606
607/*
608 * register_memory - Setup a sysfs device for a memory block
609 */
610static
611int register_memory(struct memory_block *memory)
612{
613 int ret;
614
615 memory->dev.bus = &memory_subsys;
616 memory->dev.id = memory->start_section_nr / sections_per_block;
617 memory->dev.release = memory_block_release;
618 memory->dev.groups = memory_memblk_attr_groups;
619 memory->dev.offline = memory->state == MEM_OFFLINE;
620
621 ret = device_register(&memory->dev);
622 if (ret)
623 put_device(&memory->dev);
624
625 return ret;
626}
627
628static int init_memory_block(struct memory_block **memory,
David Brazdil0f672f62019-12-10 10:32:29 +0000629 unsigned long block_id, unsigned long state)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000630{
631 struct memory_block *mem;
632 unsigned long start_pfn;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000633 int ret = 0;
634
David Brazdil0f672f62019-12-10 10:32:29 +0000635 mem = find_memory_block_by_id(block_id);
636 if (mem) {
637 put_device(&mem->dev);
638 return -EEXIST;
639 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000640 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
641 if (!mem)
642 return -ENOMEM;
643
David Brazdil0f672f62019-12-10 10:32:29 +0000644 mem->start_section_nr = block_id * sections_per_block;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000645 mem->state = state;
646 start_pfn = section_nr_to_pfn(mem->start_section_nr);
647 mem->phys_device = arch_get_memory_phys_device(start_pfn);
David Brazdil0f672f62019-12-10 10:32:29 +0000648 mem->nid = NUMA_NO_NODE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000649
650 ret = register_memory(mem);
651
652 *memory = mem;
653 return ret;
654}
655
David Brazdil0f672f62019-12-10 10:32:29 +0000656static int add_memory_block(unsigned long base_section_nr)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000657{
David Brazdil0f672f62019-12-10 10:32:29 +0000658 int ret, section_count = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000659 struct memory_block *mem;
David Brazdil0f672f62019-12-10 10:32:29 +0000660 unsigned long nr;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000661
David Brazdil0f672f62019-12-10 10:32:29 +0000662 for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
663 nr++)
664 if (present_section_nr(nr))
665 section_count++;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000666
667 if (section_count == 0)
668 return 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000669 ret = init_memory_block(&mem, base_memory_block_id(base_section_nr),
670 MEM_ONLINE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000671 if (ret)
672 return ret;
673 mem->section_count = section_count;
674 return 0;
675}
676
David Brazdil0f672f62019-12-10 10:32:29 +0000677static void unregister_memory(struct memory_block *memory)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000678{
David Brazdil0f672f62019-12-10 10:32:29 +0000679 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
680 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000681
David Brazdil0f672f62019-12-10 10:32:29 +0000682 /* drop the ref. we got via find_memory_block() */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000683 put_device(&memory->dev);
684 device_unregister(&memory->dev);
685}
686
David Brazdil0f672f62019-12-10 10:32:29 +0000687/*
688 * Create memory block devices for the given memory area. Start and size
689 * have to be aligned to memory block granularity. Memory block devices
690 * will be initialized as offline.
691 */
692int create_memory_block_devices(unsigned long start, unsigned long size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000693{
David Brazdil0f672f62019-12-10 10:32:29 +0000694 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
695 unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000696 struct memory_block *mem;
David Brazdil0f672f62019-12-10 10:32:29 +0000697 unsigned long block_id;
698 int ret = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000699
David Brazdil0f672f62019-12-10 10:32:29 +0000700 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
701 !IS_ALIGNED(size, memory_block_size_bytes())))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000702 return -EINVAL;
703
David Brazdil0f672f62019-12-10 10:32:29 +0000704 mutex_lock(&mem_sysfs_mutex);
705 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
706 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
707 if (ret)
708 break;
709 mem->section_count = sections_per_block;
710 }
711 if (ret) {
712 end_block_id = block_id;
713 for (block_id = start_block_id; block_id != end_block_id;
714 block_id++) {
715 mem = find_memory_block_by_id(block_id);
716 mem->section_count = 0;
717 unregister_memory(mem);
718 }
719 }
720 mutex_unlock(&mem_sysfs_mutex);
721 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000722}
David Brazdil0f672f62019-12-10 10:32:29 +0000723
724/*
725 * Remove memory block devices for the given memory area. Start and size
726 * have to be aligned to memory block granularity. Memory block devices
727 * have to be offline.
728 */
729void remove_memory_block_devices(unsigned long start, unsigned long size)
730{
731 const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
732 const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
733 struct memory_block *mem;
734 unsigned long block_id;
735
736 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
737 !IS_ALIGNED(size, memory_block_size_bytes())))
738 return;
739
740 mutex_lock(&mem_sysfs_mutex);
741 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
742 mem = find_memory_block_by_id(block_id);
743 if (WARN_ON_ONCE(!mem))
744 continue;
745 mem->section_count = 0;
746 unregister_memory_block_under_nodes(mem);
747 unregister_memory(mem);
748 }
749 mutex_unlock(&mem_sysfs_mutex);
750}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000751
752/* return true if the memory block is offlined, otherwise, return false */
753bool is_memblock_offlined(struct memory_block *mem)
754{
755 return mem->state == MEM_OFFLINE;
756}
757
758static struct attribute *memory_root_attrs[] = {
759#ifdef CONFIG_ARCH_MEMORY_PROBE
760 &dev_attr_probe.attr,
761#endif
762
763#ifdef CONFIG_MEMORY_FAILURE
764 &dev_attr_soft_offline_page.attr,
765 &dev_attr_hard_offline_page.attr,
766#endif
767
768 &dev_attr_block_size_bytes.attr,
769 &dev_attr_auto_online_blocks.attr,
770 NULL
771};
772
773static struct attribute_group memory_root_attr_group = {
774 .attrs = memory_root_attrs,
775};
776
777static const struct attribute_group *memory_root_attr_groups[] = {
778 &memory_root_attr_group,
779 NULL,
780};
781
782/*
783 * Initialize the sysfs support for memory devices...
784 */
David Brazdil0f672f62019-12-10 10:32:29 +0000785void __init memory_dev_init(void)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000786{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000787 int ret;
788 int err;
David Brazdil0f672f62019-12-10 10:32:29 +0000789 unsigned long block_sz, nr;
790
791 /* Validate the configured memory block size */
792 block_sz = memory_block_size_bytes();
793 if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
794 panic("Memory block size not suitable: 0x%lx\n", block_sz);
795 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000796
797 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
798 if (ret)
799 goto out;
800
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000801 /*
802 * Create entries for memory sections that were found
803 * during boot and have been initialized
804 */
805 mutex_lock(&mem_sysfs_mutex);
David Brazdil0f672f62019-12-10 10:32:29 +0000806 for (nr = 0; nr <= __highest_present_section_nr;
807 nr += sections_per_block) {
808 err = add_memory_block(nr);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000809 if (!ret)
810 ret = err;
811 }
812 mutex_unlock(&mem_sysfs_mutex);
813
814out:
815 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +0000816 panic("%s() failed: %d\n", __func__, ret);
817}
818
819/**
820 * walk_memory_blocks - walk through all present memory blocks overlapped
821 * by the range [start, start + size)
822 *
823 * @start: start address of the memory range
824 * @size: size of the memory range
825 * @arg: argument passed to func
826 * @func: callback for each memory section walked
827 *
828 * This function walks through all present memory blocks overlapped by the
829 * range [start, start + size), calling func on each memory block.
830 *
831 * In case func() returns an error, walking is aborted and the error is
832 * returned.
833 */
834int walk_memory_blocks(unsigned long start, unsigned long size,
835 void *arg, walk_memory_blocks_func_t func)
836{
837 const unsigned long start_block_id = phys_to_block_id(start);
838 const unsigned long end_block_id = phys_to_block_id(start + size - 1);
839 struct memory_block *mem;
840 unsigned long block_id;
841 int ret = 0;
842
843 if (!size)
844 return 0;
845
846 for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
847 mem = find_memory_block_by_id(block_id);
848 if (!mem)
849 continue;
850
851 ret = func(mem, arg);
852 put_device(&mem->dev);
853 if (ret)
854 break;
855 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000856 return ret;
857}
David Brazdil0f672f62019-12-10 10:32:29 +0000858
859struct for_each_memory_block_cb_data {
860 walk_memory_blocks_func_t func;
861 void *arg;
862};
863
864static int for_each_memory_block_cb(struct device *dev, void *data)
865{
866 struct memory_block *mem = to_memory_block(dev);
867 struct for_each_memory_block_cb_data *cb_data = data;
868
869 return cb_data->func(mem, cb_data->arg);
870}
871
872/**
873 * for_each_memory_block - walk through all present memory blocks
874 *
875 * @arg: argument passed to func
876 * @func: callback for each memory block walked
877 *
878 * This function walks through all present memory blocks, calling func on
879 * each memory block.
880 *
881 * In case func() returns an error, walking is aborted and the error is
882 * returned.
883 */
884int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
885{
886 struct for_each_memory_block_cb_data cb_data = {
887 .func = func,
888 .arg = arg,
889 };
890
891 return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
892 for_each_memory_block_cb);
893}