blob: 826dd76f662e53b60fe63a705995c87b3b5208dc [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Hansen3947be12005-10-29 18:16:54 -07002/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08003 * Memory subsystem support
Dave Hansen3947be12005-10-29 18:16:54 -07004 *
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
Dave Hansen3947be12005-10-29 18:16:54 -070014#include <linux/module.h>
15#include <linux/init.h>
Dave Hansen3947be12005-10-29 18:16:54 -070016#include <linux/topology.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080017#include <linux/capability.h>
Dave Hansen3947be12005-10-29 18:16:54 -070018#include <linux/device.h>
19#include <linux/memory.h>
Dave Hansen3947be12005-10-29 18:16:54 -070020#include <linux/memory_hotplug.h>
21#include <linux/mm.h>
Daniel Walkerda19cbc2008-02-04 23:35:47 -080022#include <linux/mutex.h>
Shaohua Li9f1b16a2008-10-18 20:27:12 -070023#include <linux/stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Shaohua Li9f1b16a2008-10-18 20:27:12 -070025
Arun Sharma600634972011-07-26 16:09:06 -070026#include <linux/atomic.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080027#include <linux/uaccess.h>
Dave Hansen3947be12005-10-29 18:16:54 -070028
Nathan Fontenot2938ffb2010-10-19 12:45:24 -050029static DEFINE_MUTEX(mem_sysfs_mutex);
30
Dave Hansen3947be12005-10-29 18:16:54 -070031#define MEMORY_CLASS_NAME "memory"
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060032
Gu Zheng7315f0c2013-08-28 14:38:27 +080033#define to_memory_block(dev) container_of(dev, struct memory_block, dev)
34
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060035static int sections_per_block;
36
37static inline int base_memory_block_id(int section_nr)
38{
39 return section_nr / sections_per_block;
40}
Dave Hansen3947be12005-10-29 18:16:54 -070041
David Hildenbranddb051a02019-07-18 15:56:56 -070042static inline int pfn_to_block_id(unsigned long pfn)
43{
44 return base_memory_block_id(pfn_to_section_nr(pfn));
45}
46
Rafael J. Wysocki4960e052013-05-08 14:18:37 +020047static int memory_subsys_online(struct device *dev);
48static int memory_subsys_offline(struct device *dev);
49
Kay Sievers10fbcf42011-12-21 14:48:43 -080050static struct bus_type memory_subsys = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +010051 .name = MEMORY_CLASS_NAME,
Kay Sievers10fbcf42011-12-21 14:48:43 -080052 .dev_name = MEMORY_CLASS_NAME,
Rafael J. Wysocki4960e052013-05-08 14:18:37 +020053 .online = memory_subsys_online,
54 .offline = memory_subsys_offline,
Dave Hansen3947be12005-10-29 18:16:54 -070055};
56
Alan Sterne041c682006-03-27 01:16:30 -080057static BLOCKING_NOTIFIER_HEAD(memory_chain);
Dave Hansen3947be12005-10-29 18:16:54 -070058
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080059int register_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070060{
Ioana Ciornei2aeebca2015-03-08 12:48:35 +020061 return blocking_notifier_chain_register(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070062}
Hannes Hering3c82c302008-05-07 14:43:01 +020063EXPORT_SYMBOL(register_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070064
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080065void unregister_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070066{
Ioana Ciornei2aeebca2015-03-08 12:48:35 +020067 blocking_notifier_chain_unregister(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070068}
Hannes Hering3c82c302008-05-07 14:43:01 +020069EXPORT_SYMBOL(unregister_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070070
Robert Jennings925cc712009-12-17 14:44:38 +000071static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
72
73int register_memory_isolate_notifier(struct notifier_block *nb)
74{
75 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
76}
77EXPORT_SYMBOL(register_memory_isolate_notifier);
78
79void unregister_memory_isolate_notifier(struct notifier_block *nb)
80{
81 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
82}
83EXPORT_SYMBOL(unregister_memory_isolate_notifier);
84
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -080085static void memory_block_release(struct device *dev)
86{
Gu Zheng7315f0c2013-08-28 14:38:27 +080087 struct memory_block *mem = to_memory_block(dev);
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -080088
89 kfree(mem);
90}
91
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060092unsigned long __weak memory_block_size_bytes(void)
93{
94 return MIN_MEMORY_BLOCK_SIZE;
95}
Dave Hansenc221c0b2019-02-25 10:57:40 -080096EXPORT_SYMBOL_GPL(memory_block_size_bytes);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060097
98static unsigned long get_memory_block_size(void)
99{
100 unsigned long block_sz;
101
102 block_sz = memory_block_size_bytes();
103
104 /* Validate blk_sz is a power of 2 and not less than section size */
105 if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
106 WARN_ON(1);
107 block_sz = MIN_MEMORY_BLOCK_SIZE;
108 }
109
110 return block_sz;
111}
112
Dave Hansen3947be12005-10-29 18:16:54 -0700113/*
114 * use this as the physical section index that this memsection
115 * uses.
116 */
117
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100118static ssize_t phys_index_show(struct device *dev,
119 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700120{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800121 struct memory_block *mem = to_memory_block(dev);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600122 unsigned long phys_index;
123
124 phys_index = mem->start_section_nr / sections_per_block;
125 return sprintf(buf, "%08lx\n", phys_index);
126}
127
Dave Hansen3947be12005-10-29 18:16:54 -0700128/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700129 * Show whether the section of memory is likely to be hot-removable
130 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100131static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
132 char *buf)
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700133{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600134 unsigned long i, pfn;
135 int ret = 1;
Gu Zheng7315f0c2013-08-28 14:38:27 +0800136 struct memory_block *mem = to_memory_block(dev);
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700137
Michal Hocko8b0662f2017-07-06 15:37:53 -0700138 if (mem->state != MEM_ONLINE)
139 goto out;
140
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600141 for (i = 0; i < sections_per_block; i++) {
Russ Anderson21ea9f52013-08-28 16:35:18 -0700142 if (!present_section_nr(mem->start_section_nr + i))
143 continue;
Nathan Fontenotd3360162011-01-20 10:44:29 -0600144 pfn = section_nr_to_pfn(mem->start_section_nr + i);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600145 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
146 }
147
Michal Hocko8b0662f2017-07-06 15:37:53 -0700148out:
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700149 return sprintf(buf, "%d\n", ret);
150}
151
152/*
Dave Hansen3947be12005-10-29 18:16:54 -0700153 * online, offline, going offline, etc.
154 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100155static ssize_t state_show(struct device *dev, struct device_attribute *attr,
156 char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700157{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800158 struct memory_block *mem = to_memory_block(dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700159 ssize_t len = 0;
160
161 /*
162 * We can probably put these states in a nice little array
163 * so that they're not open-coded
164 */
165 switch (mem->state) {
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200166 case MEM_ONLINE:
167 len = sprintf(buf, "online\n");
168 break;
169 case MEM_OFFLINE:
170 len = sprintf(buf, "offline\n");
171 break;
172 case MEM_GOING_OFFLINE:
173 len = sprintf(buf, "going-offline\n");
174 break;
175 default:
176 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
177 mem->state);
178 WARN_ON(1);
179 break;
Dave Hansen3947be12005-10-29 18:16:54 -0700180 }
181
182 return len;
183}
184
Yasunori Goto7b78d332007-10-21 16:41:36 -0700185int memory_notify(unsigned long val, void *v)
Dave Hansen3947be12005-10-29 18:16:54 -0700186{
Alan Sterne041c682006-03-27 01:16:30 -0800187 return blocking_notifier_call_chain(&memory_chain, val, v);
Dave Hansen3947be12005-10-29 18:16:54 -0700188}
189
Robert Jennings925cc712009-12-17 14:44:38 +0000190int memory_isolate_notify(unsigned long val, void *v)
191{
192 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
193}
194
Dave Hansen3947be12005-10-29 18:16:54 -0700195/*
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700196 * The probe routines leave the pages uninitialized, just as the bootmem code
197 * does. Make sure we do not access them, but instead use only information from
198 * within sections.
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200199 */
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700200static bool pages_correctly_probed(unsigned long start_pfn)
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200201{
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700202 unsigned long section_nr = pfn_to_section_nr(start_pfn);
203 unsigned long section_nr_end = section_nr + sections_per_block;
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200204 unsigned long pfn = start_pfn;
205
206 /*
207 * memmap between sections is not contiguous except with
208 * SPARSEMEM_VMEMMAP. We lookup the page once per section
209 * and assume memmap is contiguous within each section
210 */
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700211 for (; section_nr < section_nr_end; section_nr++) {
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200212 if (WARN_ON_ONCE(!pfn_valid(pfn)))
213 return false;
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200214
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700215 if (!present_section_nr(section_nr)) {
Michal Hocko1ecc07f2018-12-28 00:39:34 -0800216 pr_warn("section %ld pfn[%lx, %lx) not present\n",
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700217 section_nr, pfn, pfn + PAGES_PER_SECTION);
218 return false;
219 } else if (!valid_section_nr(section_nr)) {
Michal Hocko1ecc07f2018-12-28 00:39:34 -0800220 pr_warn("section %ld pfn[%lx, %lx) no valid memmap\n",
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700221 section_nr, pfn, pfn + PAGES_PER_SECTION);
222 return false;
223 } else if (online_section_nr(section_nr)) {
Michal Hocko1ecc07f2018-12-28 00:39:34 -0800224 pr_warn("section %ld pfn[%lx, %lx) is already online\n",
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700225 section_nr, pfn, pfn + PAGES_PER_SECTION);
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200226 return false;
227 }
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700228 pfn += PAGES_PER_SECTION;
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200229 }
230
231 return true;
232}
233
234/*
Dave Hansen3947be12005-10-29 18:16:54 -0700235 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
236 * OK to have direct references to sparsemem variables in here.
237 */
238static int
Baoquan He063b8a42019-05-13 17:19:35 -0700239memory_block_action(unsigned long start_section_nr, unsigned long action,
240 int online_type)
Dave Hansen3947be12005-10-29 18:16:54 -0700241{
Wen Congyanga16cee12012-10-08 16:33:58 -0700242 unsigned long start_pfn;
Anton Blanchard5409d2c2011-05-11 17:25:14 +1000243 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700244 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700245
Baoquan He063b8a42019-05-13 17:19:35 -0700246 start_pfn = section_nr_to_pfn(start_section_nr);
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700247
Dave Hansen3947be12005-10-29 18:16:54 -0700248 switch (action) {
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200249 case MEM_ONLINE:
Pavel Tatashinb77eab72018-04-05 16:22:52 -0700250 if (!pages_correctly_probed(start_pfn))
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200251 return -EBUSY;
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200252
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200253 ret = online_pages(start_pfn, nr_pages, online_type);
254 break;
255 case MEM_OFFLINE:
256 ret = offline_pages(start_pfn, nr_pages);
257 break;
258 default:
259 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
Baoquan He063b8a42019-05-13 17:19:35 -0700260 "%ld\n", __func__, start_section_nr, action, action);
Ioana Ciornei3d3af6a2015-03-08 12:29:04 +0200261 ret = -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700262 }
Dave Hansen3947be12005-10-29 18:16:54 -0700263
264 return ret;
265}
266
Nathan Fontenotdc18d702017-02-24 15:00:02 -0800267static int memory_block_change_state(struct memory_block *mem,
Seth Jenningsfa2be402013-08-20 16:05:05 -0500268 unsigned long to_state, unsigned long from_state_req)
Dave Hansen3947be12005-10-29 18:16:54 -0700269{
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700270 int ret = 0;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600271
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200272 if (mem->state != from_state_req)
273 return -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700274
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600275 if (to_state == MEM_OFFLINE)
276 mem->state = MEM_GOING_OFFLINE;
277
Seth Jenningsfa2be402013-08-20 16:05:05 -0500278 ret = memory_block_action(mem->start_section_nr, to_state,
279 mem->online_type);
280
Rafael J. Wysockib2c064b2013-05-23 10:38:55 +0200281 mem->state = ret ? from_state_req : to_state;
Seth Jenningsfa2be402013-08-20 16:05:05 -0500282
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200283 return ret;
284}
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600285
Seth Jenningsfa2be402013-08-20 16:05:05 -0500286/* The device lock serializes operations on memory_subsys_[online|offline] */
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200287static int memory_subsys_online(struct device *dev)
288{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800289 struct memory_block *mem = to_memory_block(dev);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200290 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700291
Seth Jenningsfa2be402013-08-20 16:05:05 -0500292 if (mem->state == MEM_ONLINE)
293 return 0;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200294
Seth Jenningsfa2be402013-08-20 16:05:05 -0500295 /*
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100296 * If we are called from state_store(), online_type will be
Seth Jenningsfa2be402013-08-20 16:05:05 -0500297 * set >= 0 Otherwise we were called from the device online
298 * attribute and need to set the online_type.
299 */
300 if (mem->online_type < 0)
Tang Chen4f7c6b42014-08-06 16:05:13 -0700301 mem->online_type = MMOP_ONLINE_KEEP;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200302
Seth Jenningsfa2be402013-08-20 16:05:05 -0500303 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
304
305 /* clear online_type */
306 mem->online_type = -1;
307
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200308 return ret;
309}
310
311static int memory_subsys_offline(struct device *dev)
312{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800313 struct memory_block *mem = to_memory_block(dev);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200314
Seth Jenningsfa2be402013-08-20 16:05:05 -0500315 if (mem->state == MEM_OFFLINE)
316 return 0;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200317
Seth Jennings26bbe7e2015-12-11 13:40:57 -0800318 /* Can't offline block with non-present sections */
319 if (mem->section_count != sections_per_block)
320 return -EINVAL;
321
Seth Jenningsfa2be402013-08-20 16:05:05 -0500322 return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200323}
324
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100325static ssize_t state_store(struct device *dev, struct device_attribute *attr,
326 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700327{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800328 struct memory_block *mem = to_memory_block(dev);
Seth Jenningsfa2be402013-08-20 16:05:05 -0500329 int ret, online_type;
Dave Hansen3947be12005-10-29 18:16:54 -0700330
Rafael J. Wysocki5e33bc42013-08-28 21:41:01 +0200331 ret = lock_device_hotplug_sysfs();
332 if (ret)
333 return ret;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200334
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700335 if (sysfs_streq(buf, "online_kernel"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700336 online_type = MMOP_ONLINE_KERNEL;
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700337 else if (sysfs_streq(buf, "online_movable"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700338 online_type = MMOP_ONLINE_MOVABLE;
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700339 else if (sysfs_streq(buf, "online"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700340 online_type = MMOP_ONLINE_KEEP;
Tang Chen1f6a6cc2014-08-06 16:05:11 -0700341 else if (sysfs_streq(buf, "offline"))
Tang Chen4f7c6b42014-08-06 16:05:13 -0700342 online_type = MMOP_OFFLINE;
Yasuaki Ishimatsua37f8632013-10-11 15:36:25 +0900343 else {
344 ret = -EINVAL;
345 goto err;
346 }
Seth Jenningsfa2be402013-08-20 16:05:05 -0500347
348 switch (online_type) {
Tang Chen4f7c6b42014-08-06 16:05:13 -0700349 case MMOP_ONLINE_KERNEL:
350 case MMOP_ONLINE_MOVABLE:
351 case MMOP_ONLINE_KEEP:
David Hildenbrand381eab42018-10-30 15:10:29 -0700352 /* mem->online_type is protected by device_hotplug_lock */
Seth Jenningsfa2be402013-08-20 16:05:05 -0500353 mem->online_type = online_type;
354 ret = device_online(&mem->dev);
355 break;
Tang Chen4f7c6b42014-08-06 16:05:13 -0700356 case MMOP_OFFLINE:
Seth Jenningsfa2be402013-08-20 16:05:05 -0500357 ret = device_offline(&mem->dev);
358 break;
359 default:
360 ret = -EINVAL; /* should never happen */
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200361 }
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200362
Yasuaki Ishimatsua37f8632013-10-11 15:36:25 +0900363err:
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200364 unlock_device_hotplug();
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600365
Reza Arbabd66ba152016-10-07 17:00:15 -0700366 if (ret < 0)
Dave Hansen3947be12005-10-29 18:16:54 -0700367 return ret;
Reza Arbabd66ba152016-10-07 17:00:15 -0700368 if (ret)
369 return -EINVAL;
370
Dave Hansen3947be12005-10-29 18:16:54 -0700371 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 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100383static ssize_t phys_device_show(struct device *dev,
Kay Sievers10fbcf42011-12-21 14:48:43 -0800384 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700385{
Gu Zheng7315f0c2013-08-28 14:38:27 +0800386 struct memory_block *mem = to_memory_block(dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700387 return sprintf(buf, "%d\n", mem->phys_device);
388}
389
Zhang Zhened2f2402014-10-09 15:26:31 -0700390#ifdef CONFIG_MEMORY_HOTREMOVE
Michal Hockoe5e68932017-09-06 16:19:37 -0700391static 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
Michal Hockoe5e68932017-09-06 16:19:37 -0700397 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
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100404static ssize_t valid_zones_show(struct device *dev,
Zhang Zhened2f2402014-10-09 15:26:31 -0700405 struct device_attribute *attr, char *buf)
406{
407 struct memory_block *mem = to_memory_block(dev);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700408 unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
Zhang Zhened2f2402014-10-09 15:26:31 -0700409 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700410 unsigned long valid_start_pfn, valid_end_pfn;
Michal Hockoe5e68932017-09-06 16:19:37 -0700411 struct zone *default_zone;
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700412 int nid;
Zhang Zhened2f2402014-10-09 15:26:31 -0700413
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700414 /*
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700415 * 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) {
Mikhail Zaslonko4e8346d2018-09-04 15:46:09 -0700419 /*
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;
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700427 strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
428 goto out;
Zhang Zhened2f2402014-10-09 15:26:31 -0700429 }
430
Mikhail Zaslonko4e8346d2018-09-04 15:46:09 -0700431 nid = mem->nid;
Michal Hockoe5e68932017-09-06 16:19:37 -0700432 default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
433 strcat(buf, default_zone->name);
Zhang Zhened2f2402014-10-09 15:26:31 -0700434
Michal Hockoe5e68932017-09-06 16:19:37 -0700435 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);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700439out:
Reza Arbaba371d9f2016-07-26 15:22:27 -0700440 strcat(buf, "\n");
441
442 return strlen(buf);
Zhang Zhened2f2402014-10-09 15:26:31 -0700443}
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100444static DEVICE_ATTR_RO(valid_zones);
Zhang Zhened2f2402014-10-09 15:26:31 -0700445#endif
446
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100447static DEVICE_ATTR_RO(phys_index);
448static DEVICE_ATTR_RW(state);
449static DEVICE_ATTR_RO(phys_device);
450static DEVICE_ATTR_RO(removable);
Dave Hansen3947be12005-10-29 18:16:54 -0700451
Dave Hansen3947be12005-10-29 18:16:54 -0700452/*
453 * Block size attribute stuff
454 */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100455static ssize_t block_size_bytes_show(struct device *dev,
456 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700457{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600458 return sprintf(buf, "%lx\n", get_memory_block_size());
Dave Hansen3947be12005-10-29 18:16:54 -0700459}
460
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100461static DEVICE_ATTR_RO(block_size_bytes);
Dave Hansen3947be12005-10-29 18:16:54 -0700462
Dave Hansen3947be12005-10-29 18:16:54 -0700463/*
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700464 * Memory auto online policy.
465 */
466
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100467static ssize_t auto_online_blocks_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700469{
470 if (memhp_auto_online)
471 return sprintf(buf, "online\n");
472 else
473 return sprintf(buf, "offline\n");
474}
475
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100476static ssize_t auto_online_blocks_store(struct device *dev,
477 struct device_attribute *attr,
478 const char *buf, size_t count)
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700479{
480 if (sysfs_streq(buf, "online"))
481 memhp_auto_online = true;
482 else if (sysfs_streq(buf, "offline"))
483 memhp_auto_online = false;
484 else
485 return -EINVAL;
486
487 return count;
488}
489
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100490static DEVICE_ATTR_RW(auto_online_blocks);
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700491
492/*
Dave Hansen3947be12005-10-29 18:16:54 -0700493 * Some architectures will have custom drivers to do this, and
494 * will not need to do it from userspace. The fake hot-add code
495 * as well as ppc64 will do all of their discovery in userspace
496 * and will require this interface.
497 */
498#ifdef CONFIG_ARCH_MEMORY_PROBE
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100499static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
500 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700501{
502 u64 phys_addr;
John Allencb5490a2016-01-14 15:22:16 -0800503 int nid, ret;
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000504 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700505
Zhang Zhenb69deb22014-08-06 16:06:06 -0700506 ret = kstrtoull(buf, 0, &phys_addr);
507 if (ret)
508 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700509
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000510 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
511 return -EINVAL;
512
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700513 ret = lock_device_hotplug_sysfs();
514 if (ret)
zhong jiang37803842019-04-18 17:50:16 -0700515 return ret;
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700516
John Allencb5490a2016-01-14 15:22:16 -0800517 nid = memory_add_physaddr_to_nid(phys_addr);
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700518 ret = __add_memory(nid, phys_addr,
519 MIN_MEMORY_BLOCK_SIZE * sections_per_block);
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600520
John Allencb5490a2016-01-14 15:22:16 -0800521 if (ret)
522 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700523
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530524 ret = count;
525out:
David Hildenbrand8df1d0e2018-10-30 15:10:24 -0700526 unlock_device_hotplug();
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530527 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700528}
Dave Hansen3947be12005-10-29 18:16:54 -0700529
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100530static DEVICE_ATTR_WO(probe);
Dave Hansen3947be12005-10-29 18:16:54 -0700531#endif
532
Andi Kleenfacb6012009-12-16 12:20:00 +0100533#ifdef CONFIG_MEMORY_FAILURE
534/*
535 * Support for offlining pages of memory
536 */
537
538/* Soft offline a page */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100539static ssize_t soft_offline_page_store(struct device *dev,
540 struct device_attribute *attr,
541 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100542{
543 int ret;
544 u64 pfn;
545 if (!capable(CAP_SYS_ADMIN))
546 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900547 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100548 return -EINVAL;
549 pfn >>= PAGE_SHIFT;
550 if (!pfn_valid(pfn))
551 return -ENXIO;
552 ret = soft_offline_page(pfn_to_page(pfn), 0);
553 return ret == 0 ? count : ret;
554}
555
556/* Forcibly offline a page, including killing processes. */
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100557static ssize_t hard_offline_page_store(struct device *dev,
558 struct device_attribute *attr,
559 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100560{
561 int ret;
562 u64 pfn;
563 if (!capable(CAP_SYS_ADMIN))
564 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900565 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100566 return -EINVAL;
567 pfn >>= PAGE_SHIFT;
Eric W. Biederman83b57532017-07-09 18:14:01 -0500568 ret = memory_failure(pfn, 0);
Andi Kleenfacb6012009-12-16 12:20:00 +0100569 return ret ? ret : count;
570}
571
David Hildenbrand3f8e9172018-12-03 12:16:11 +0100572static DEVICE_ATTR_WO(soft_offline_page);
573static DEVICE_ATTR_WO(hard_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100574#endif
575
Dave Hansen3947be12005-10-29 18:16:54 -0700576/*
577 * Note that phys_device is optional. It is here to allow for
578 * differentiation between which *physical* devices each
579 * section belongs to...
580 */
Heiko Carstensbc32df02010-03-15 00:35:03 -0400581int __weak arch_get_memory_phys_device(unsigned long start_pfn)
582{
583 return 0;
584}
Dave Hansen3947be12005-10-29 18:16:54 -0700585
Kay Sievers10fbcf42011-12-21 14:48:43 -0800586/*
587 * A reference for the returned object is held and the reference for the
588 * hinted object is released.
589 */
David Hildenbranddb051a02019-07-18 15:56:56 -0700590static struct memory_block *find_memory_block_by_id(int block_id,
591 struct memory_block *hint)
Robin Holt98383032010-09-29 14:00:55 -0500592{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800593 struct device *hintdev = hint ? &hint->dev : NULL;
594 struct device *dev;
Robin Holt98383032010-09-29 14:00:55 -0500595
Kay Sievers10fbcf42011-12-21 14:48:43 -0800596 dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
597 if (hint)
598 put_device(&hint->dev);
599 if (!dev)
Robin Holt98383032010-09-29 14:00:55 -0500600 return NULL;
Gu Zheng7315f0c2013-08-28 14:38:27 +0800601 return to_memory_block(dev);
Robin Holt98383032010-09-29 14:00:55 -0500602}
603
David Hildenbranddb051a02019-07-18 15:56:56 -0700604struct memory_block *find_memory_block_hinted(struct mem_section *section,
605 struct memory_block *hint)
606{
607 int block_id = base_memory_block_id(__section_nr(section));
608
609 return find_memory_block_by_id(block_id, hint);
610}
611
Dave Hansen3947be12005-10-29 18:16:54 -0700612/*
613 * For now, we have a linear search to go find the appropriate
614 * memory_block corresponding to a particular phys_index. If
615 * this gets to be a real problem, we can always use a radix
616 * tree or something here.
617 *
Kay Sievers10fbcf42011-12-21 14:48:43 -0800618 * This could be made generic for all device subsystems.
Dave Hansen3947be12005-10-29 18:16:54 -0700619 */
Gary Hadec04fc582009-01-06 14:39:14 -0800620struct memory_block *find_memory_block(struct mem_section *section)
Dave Hansen3947be12005-10-29 18:16:54 -0700621{
Robin Holt98383032010-09-29 14:00:55 -0500622 return find_memory_block_hinted(section, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700623}
624
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500625static struct attribute *memory_memblk_attrs[] = {
626 &dev_attr_phys_index.attr,
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500627 &dev_attr_state.attr,
628 &dev_attr_phys_device.attr,
629 &dev_attr_removable.attr,
Zhang Zhened2f2402014-10-09 15:26:31 -0700630#ifdef CONFIG_MEMORY_HOTREMOVE
631 &dev_attr_valid_zones.attr,
632#endif
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500633 NULL
634};
635
636static struct attribute_group memory_memblk_attr_group = {
637 .attrs = memory_memblk_attrs,
638};
639
640static const struct attribute_group *memory_memblk_attr_groups[] = {
641 &memory_memblk_attr_group,
642 NULL,
643};
644
645/*
646 * register_memory - Setup a sysfs device for a memory block
647 */
648static
649int register_memory(struct memory_block *memory)
650{
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530651 int ret;
652
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500653 memory->dev.bus = &memory_subsys;
654 memory->dev.id = memory->start_section_nr / sections_per_block;
655 memory->dev.release = memory_block_release;
656 memory->dev.groups = memory_memblk_attr_groups;
Linus Torvaldsf991fae2013-07-03 14:35:40 -0700657 memory->dev.offline = memory->state == MEM_OFFLINE;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500658
Arvind Yadav085aa2d2018-04-26 21:12:09 +0530659 ret = device_register(&memory->dev);
660 if (ret)
661 put_device(&memory->dev);
662
663 return ret;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500664}
665
David Hildenbrand18115822019-07-18 15:56:46 -0700666static int init_memory_block(struct memory_block **memory, int block_id,
667 unsigned long state)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500668{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600669 struct memory_block *mem;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500670 unsigned long start_pfn;
671 int ret = 0;
672
David Hildenbranddb051a02019-07-18 15:56:56 -0700673 mem = find_memory_block_by_id(block_id, NULL);
674 if (mem) {
675 put_device(&mem->dev);
676 return -EEXIST;
677 }
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600678 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500679 if (!mem)
680 return -ENOMEM;
681
David Hildenbrand18115822019-07-18 15:56:46 -0700682 mem->start_section_nr = block_id * sections_per_block;
Nathan Fontenotd3360162011-01-20 10:44:29 -0600683 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500684 mem->state = state;
Nathan Fontenotd3360162011-01-20 10:44:29 -0600685 start_pfn = section_nr_to_pfn(mem->start_section_nr);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500686 mem->phys_device = arch_get_memory_phys_device(start_pfn);
687
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600688 ret = register_memory(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600689
690 *memory = mem;
691 return ret;
692}
693
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500694static int add_memory_block(int base_section_nr)
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600695{
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500696 struct memory_block *mem;
David Hildenbrand18115822019-07-18 15:56:46 -0700697 int i, ret, section_count = 0;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600698
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500699 for (i = base_section_nr;
Wei Yang3b6fd6f2018-12-28 00:35:33 -0800700 i < base_section_nr + sections_per_block;
David Hildenbrand18115822019-07-18 15:56:46 -0700701 i++)
702 if (present_section_nr(i))
703 section_count++;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600704
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500705 if (section_count == 0)
706 return 0;
David Hildenbrand18115822019-07-18 15:56:46 -0700707 ret = init_memory_block(&mem, base_memory_block_id(base_section_nr),
708 MEM_ONLINE);
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500709 if (ret)
710 return ret;
711 mem->section_count = section_count;
712 return 0;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500713}
714
David Hildenbranddb051a02019-07-18 15:56:56 -0700715static void unregister_memory(struct memory_block *memory)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700716{
David Hildenbranddb051a02019-07-18 15:56:56 -0700717 if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
718 return;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700719
David Hildenbrandcb7b3a32019-05-13 17:21:37 -0700720 /* drop the ref. we got via find_memory_block() */
Seth Jenningsdf2b7172013-08-20 12:12:59 -0500721 put_device(&memory->dev);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700722 device_unregister(&memory->dev);
723}
724
David Hildenbranddb051a02019-07-18 15:56:56 -0700725/*
726 * Create memory block devices for the given memory area. Start and size
727 * have to be aligned to memory block granularity. Memory block devices
728 * will be initialized as offline.
729 */
730int create_memory_block_devices(unsigned long start, unsigned long size)
731{
732 const int start_block_id = pfn_to_block_id(PFN_DOWN(start));
733 int end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
734 struct memory_block *mem;
735 unsigned long block_id;
736 int ret = 0;
737
738 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
739 !IS_ALIGNED(size, memory_block_size_bytes())))
740 return -EINVAL;
741
742 mutex_lock(&mem_sysfs_mutex);
743 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
744 ret = init_memory_block(&mem, block_id, MEM_OFFLINE);
745 if (ret)
746 break;
747 mem->section_count = sections_per_block;
748 }
749 if (ret) {
750 end_block_id = block_id;
751 for (block_id = start_block_id; block_id != end_block_id;
752 block_id++) {
753 mem = find_memory_block_by_id(block_id, NULL);
754 mem->section_count = 0;
755 unregister_memory(mem);
756 }
757 }
758 mutex_unlock(&mem_sysfs_mutex);
759 return ret;
760}
761
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700762/*
763 * Remove memory block devices for the given memory area. Start and size
764 * have to be aligned to memory block granularity. Memory block devices
765 * have to be offline.
766 */
767void remove_memory_block_devices(unsigned long start, unsigned long size)
Dave Hansen3947be12005-10-29 18:16:54 -0700768{
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700769 const int start_block_id = pfn_to_block_id(PFN_DOWN(start));
770 const int end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
Dave Hansen3947be12005-10-29 18:16:54 -0700771 struct memory_block *mem;
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700772 int block_id;
Dave Hansen3947be12005-10-29 18:16:54 -0700773
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700774 if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
775 !IS_ALIGNED(size, memory_block_size_bytes())))
David Hildenbrandcb7b3a32019-05-13 17:21:37 -0700776 return;
777
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500778 mutex_lock(&mem_sysfs_mutex);
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700779 for (block_id = start_block_id; block_id != end_block_id; block_id++) {
780 mem = find_memory_block_by_id(block_id, NULL);
781 if (WARN_ON_ONCE(!mem))
782 continue;
783 mem->section_count = 0;
784 unregister_memory_block_under_nodes(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600785 unregister_memory(mem);
David Hildenbrand4c4b7f92019-07-18 15:57:06 -0700786 }
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500787 mutex_unlock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700788}
789
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -0800790/* return true if the memory block is offlined, otherwise, return false */
791bool is_memblock_offlined(struct memory_block *mem)
792{
793 return mem->state == MEM_OFFLINE;
794}
795
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500796static struct attribute *memory_root_attrs[] = {
797#ifdef CONFIG_ARCH_MEMORY_PROBE
798 &dev_attr_probe.attr,
799#endif
800
801#ifdef CONFIG_MEMORY_FAILURE
802 &dev_attr_soft_offline_page.attr,
803 &dev_attr_hard_offline_page.attr,
804#endif
805
806 &dev_attr_block_size_bytes.attr,
Vitaly Kuznetsov31bc3852016-03-15 14:56:48 -0700807 &dev_attr_auto_online_blocks.attr,
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500808 NULL
809};
810
811static struct attribute_group memory_root_attr_group = {
812 .attrs = memory_root_attrs,
813};
814
815static const struct attribute_group *memory_root_attr_groups[] = {
816 &memory_root_attr_group,
817 NULL,
818};
819
Wen Congyange90bdb72012-10-08 16:34:01 -0700820/*
Dave Hansen3947be12005-10-29 18:16:54 -0700821 * Initialize the sysfs support for memory devices...
822 */
823int __init memory_dev_init(void)
824{
825 unsigned int i;
826 int ret;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800827 int err;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600828 unsigned long block_sz;
Dave Hansen3947be12005-10-29 18:16:54 -0700829
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500830 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800831 if (ret)
832 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700833
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600834 block_sz = get_memory_block_size();
835 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
836
Dave Hansen3947be12005-10-29 18:16:54 -0700837 /*
838 * Create entries for memory sections that were found
839 * during boot and have been initialized
840 */
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500841 mutex_lock(&mem_sysfs_mutex);
Wei Yangbc8755b2018-04-10 16:29:23 -0700842 for (i = 0; i <= __highest_present_section_nr;
843 i += sections_per_block) {
Seth Jenningscb5e39b2013-08-20 12:13:03 -0500844 err = add_memory_block(i);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800845 if (!ret)
846 ret = err;
Dave Hansen3947be12005-10-29 18:16:54 -0700847 }
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500848 mutex_unlock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700849
Andrew Morton28ec24e2006-12-06 20:37:29 -0800850out:
851 if (ret)
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800852 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700853 return ret;
854}