blob: e97519bc3d067a9b4f48352aa6c1cc376a5e1a56 [file] [log] [blame]
Dave Hansen3947be12005-10-29 18:16:54 -07001/*
Kay Sievers10fbcf42011-12-21 14:48:43 -08002 * Memory subsystem support
Dave Hansen3947be12005-10-29 18:16:54 -07003 *
4 * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
5 * Dave Hansen <haveblue@us.ibm.com>
6 *
7 * This file provides the necessary infrastructure to represent
8 * a SPARSEMEM-memory-model system's physical memory in /sysfs.
9 * All arch-independent code that assumes MEMORY_HOTPLUG requires
10 * SPARSEMEM should be contained here, or in mm/memory_hotplug.c.
11 */
12
Dave Hansen3947be12005-10-29 18:16:54 -070013#include <linux/module.h>
14#include <linux/init.h>
Dave Hansen3947be12005-10-29 18:16:54 -070015#include <linux/topology.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080016#include <linux/capability.h>
Dave Hansen3947be12005-10-29 18:16:54 -070017#include <linux/device.h>
18#include <linux/memory.h>
19#include <linux/kobject.h>
20#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>
Dave Hansen3947be12005-10-29 18:16:54 -070027#include <asm/uaccess.h>
28
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
33static int sections_per_block;
34
35static inline int base_memory_block_id(int section_nr)
36{
37 return section_nr / sections_per_block;
38}
Dave Hansen3947be12005-10-29 18:16:54 -070039
Rafael J. Wysocki4960e052013-05-08 14:18:37 +020040static int memory_subsys_online(struct device *dev);
41static int memory_subsys_offline(struct device *dev);
42
Kay Sievers10fbcf42011-12-21 14:48:43 -080043static struct bus_type memory_subsys = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +010044 .name = MEMORY_CLASS_NAME,
Kay Sievers10fbcf42011-12-21 14:48:43 -080045 .dev_name = MEMORY_CLASS_NAME,
Rafael J. Wysocki4960e052013-05-08 14:18:37 +020046 .online = memory_subsys_online,
47 .offline = memory_subsys_offline,
Dave Hansen3947be12005-10-29 18:16:54 -070048};
49
Alan Sterne041c682006-03-27 01:16:30 -080050static BLOCKING_NOTIFIER_HEAD(memory_chain);
Dave Hansen3947be12005-10-29 18:16:54 -070051
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080052int register_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070053{
Alan Sterne041c682006-03-27 01:16:30 -080054 return blocking_notifier_chain_register(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070055}
Hannes Hering3c82c302008-05-07 14:43:01 +020056EXPORT_SYMBOL(register_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070057
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080058void unregister_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070059{
Alan Sterne041c682006-03-27 01:16:30 -080060 blocking_notifier_chain_unregister(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070061}
Hannes Hering3c82c302008-05-07 14:43:01 +020062EXPORT_SYMBOL(unregister_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070063
Robert Jennings925cc712009-12-17 14:44:38 +000064static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
65
66int register_memory_isolate_notifier(struct notifier_block *nb)
67{
68 return atomic_notifier_chain_register(&memory_isolate_chain, nb);
69}
70EXPORT_SYMBOL(register_memory_isolate_notifier);
71
72void unregister_memory_isolate_notifier(struct notifier_block *nb)
73{
74 atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
75}
76EXPORT_SYMBOL(unregister_memory_isolate_notifier);
77
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -080078static void memory_block_release(struct device *dev)
79{
80 struct memory_block *mem = container_of(dev, struct memory_block, dev);
81
82 kfree(mem);
83}
84
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060085unsigned long __weak memory_block_size_bytes(void)
86{
87 return MIN_MEMORY_BLOCK_SIZE;
88}
89
90static unsigned long get_memory_block_size(void)
91{
92 unsigned long block_sz;
93
94 block_sz = memory_block_size_bytes();
95
96 /* Validate blk_sz is a power of 2 and not less than section size */
97 if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
98 WARN_ON(1);
99 block_sz = MIN_MEMORY_BLOCK_SIZE;
100 }
101
102 return block_sz;
103}
104
Dave Hansen3947be12005-10-29 18:16:54 -0700105/*
106 * use this as the physical section index that this memsection
107 * uses.
108 */
109
Kay Sievers10fbcf42011-12-21 14:48:43 -0800110static ssize_t show_mem_start_phys_index(struct device *dev,
111 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700112{
113 struct memory_block *mem =
Kay Sievers10fbcf42011-12-21 14:48:43 -0800114 container_of(dev, struct memory_block, dev);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600115 unsigned long phys_index;
116
117 phys_index = mem->start_section_nr / sections_per_block;
118 return sprintf(buf, "%08lx\n", phys_index);
119}
120
Kay Sievers10fbcf42011-12-21 14:48:43 -0800121static ssize_t show_mem_end_phys_index(struct device *dev,
122 struct device_attribute *attr, char *buf)
Nathan Fontenotd3360162011-01-20 10:44:29 -0600123{
124 struct memory_block *mem =
Kay Sievers10fbcf42011-12-21 14:48:43 -0800125 container_of(dev, struct memory_block, dev);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600126 unsigned long phys_index;
127
128 phys_index = mem->end_section_nr / sections_per_block;
129 return sprintf(buf, "%08lx\n", phys_index);
Dave Hansen3947be12005-10-29 18:16:54 -0700130}
131
132/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700133 * Show whether the section of memory is likely to be hot-removable
134 */
Kay Sievers10fbcf42011-12-21 14:48:43 -0800135static ssize_t show_mem_removable(struct device *dev,
136 struct device_attribute *attr, char *buf)
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700137{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600138 unsigned long i, pfn;
139 int ret = 1;
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700140 struct memory_block *mem =
Kay Sievers10fbcf42011-12-21 14:48:43 -0800141 container_of(dev, struct memory_block, dev);
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700142
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600143 for (i = 0; i < sections_per_block; i++) {
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
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700148 return sprintf(buf, "%d\n", ret);
149}
150
151/*
Dave Hansen3947be12005-10-29 18:16:54 -0700152 * online, offline, going offline, etc.
153 */
Kay Sievers10fbcf42011-12-21 14:48:43 -0800154static ssize_t show_mem_state(struct device *dev,
155 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700156{
157 struct memory_block *mem =
Kay Sievers10fbcf42011-12-21 14:48:43 -0800158 container_of(dev, struct 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) {
166 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;
180 }
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/*
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200196 * The probe routines leave the pages reserved, just as the bootmem code does.
197 * Make sure they're still that way.
198 */
Tang Chen6056d612013-04-29 15:08:40 -0700199static bool pages_correctly_reserved(unsigned long start_pfn)
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200200{
201 int i, j;
202 struct page *page;
203 unsigned long pfn = start_pfn;
204
205 /*
206 * memmap between sections is not contiguous except with
207 * SPARSEMEM_VMEMMAP. We lookup the page once per section
208 * and assume memmap is contiguous within each section
209 */
210 for (i = 0; i < sections_per_block; i++, pfn += PAGES_PER_SECTION) {
211 if (WARN_ON_ONCE(!pfn_valid(pfn)))
212 return false;
213 page = pfn_to_page(pfn);
214
215 for (j = 0; j < PAGES_PER_SECTION; j++) {
216 if (PageReserved(page + j))
217 continue;
218
219 printk(KERN_WARNING "section number %ld page number %d "
220 "not reserved, was it already online?\n",
221 pfn_to_section_nr(pfn), j);
222
223 return false;
224 }
225 }
226
227 return true;
228}
229
230/*
Dave Hansen3947be12005-10-29 18:16:54 -0700231 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
232 * OK to have direct references to sparsemem variables in here.
233 */
234static int
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800235memory_block_action(unsigned long phys_index, unsigned long action, int online_type)
Dave Hansen3947be12005-10-29 18:16:54 -0700236{
Wen Congyanga16cee12012-10-08 16:33:58 -0700237 unsigned long start_pfn;
Anton Blanchard5409d2c2011-05-11 17:25:14 +1000238 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700239 struct page *first_page;
Dave Hansen3947be12005-10-29 18:16:54 -0700240 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700241
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700242 first_page = pfn_to_page(phys_index << PFN_SECTION_SHIFT);
Wen Congyanga16cee12012-10-08 16:33:58 -0700243 start_pfn = page_to_pfn(first_page);
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700244
Dave Hansen3947be12005-10-29 18:16:54 -0700245 switch (action) {
246 case MEM_ONLINE:
Tang Chen6056d612013-04-29 15:08:40 -0700247 if (!pages_correctly_reserved(start_pfn))
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200248 return -EBUSY;
249
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800250 ret = online_pages(start_pfn, nr_pages, online_type);
Dave Hansen3947be12005-10-29 18:16:54 -0700251 break;
252 case MEM_OFFLINE:
Wen Congyanga16cee12012-10-08 16:33:58 -0700253 ret = offline_pages(start_pfn, nr_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700254 break;
255 default:
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600256 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
257 "%ld\n", __func__, phys_index, action, action);
Dave Hansen3947be12005-10-29 18:16:54 -0700258 ret = -EINVAL;
259 }
Dave Hansen3947be12005-10-29 18:16:54 -0700260
261 return ret;
262}
263
Wen Congyange90bdb72012-10-08 16:34:01 -0700264static int __memory_block_change_state(struct memory_block *mem,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800265 unsigned long to_state, unsigned long from_state_req,
266 int online_type)
Dave Hansen3947be12005-10-29 18:16:54 -0700267{
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700268 int ret = 0;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600269
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200270 if (mem->state != from_state_req)
271 return -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700272
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600273 if (to_state == MEM_OFFLINE)
274 mem->state = MEM_GOING_OFFLINE;
275
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800276 ret = memory_block_action(mem->start_section_nr, to_state, online_type);
Rafael J. Wysockib2c064b2013-05-23 10:38:55 +0200277 mem->state = ret ? from_state_req : to_state;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200278 return ret;
279}
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600280
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200281static int memory_subsys_online(struct device *dev)
282{
283 struct memory_block *mem = container_of(dev, struct memory_block, dev);
284 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700285
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200286 mutex_lock(&mem->state_mutex);
287
288 ret = mem->state == MEM_ONLINE ? 0 :
289 __memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE,
Rafael J. Wysockib2c064b2013-05-23 10:38:55 +0200290 ONLINE_KEEP);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200291
292 mutex_unlock(&mem->state_mutex);
293 return ret;
294}
295
296static int memory_subsys_offline(struct device *dev)
297{
298 struct memory_block *mem = container_of(dev, struct memory_block, dev);
299 int ret;
300
301 mutex_lock(&mem->state_mutex);
302
303 ret = mem->state == MEM_OFFLINE ? 0 :
304 __memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE, -1);
305
306 mutex_unlock(&mem->state_mutex);
307 return ret;
308}
309
310static int __memory_block_change_state_uevent(struct memory_block *mem,
311 unsigned long to_state, unsigned long from_state_req,
312 int online_type)
313{
314 int ret = __memory_block_change_state(mem, to_state, from_state_req,
315 online_type);
316 if (!ret) {
317 switch (mem->state) {
318 case MEM_OFFLINE:
319 kobject_uevent(&mem->dev.kobj, KOBJ_OFFLINE);
320 break;
321 case MEM_ONLINE:
322 kobject_uevent(&mem->dev.kobj, KOBJ_ONLINE);
323 break;
324 default:
325 break;
326 }
Michael Holzheuf5138e42012-01-12 17:20:23 -0800327 }
Dave Hansen3947be12005-10-29 18:16:54 -0700328 return ret;
329}
330
Wen Congyange90bdb72012-10-08 16:34:01 -0700331static int memory_block_change_state(struct memory_block *mem,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800332 unsigned long to_state, unsigned long from_state_req,
333 int online_type)
Wen Congyange90bdb72012-10-08 16:34:01 -0700334{
335 int ret;
336
337 mutex_lock(&mem->state_mutex);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200338 ret = __memory_block_change_state_uevent(mem, to_state, from_state_req,
339 online_type);
Wen Congyange90bdb72012-10-08 16:34:01 -0700340 mutex_unlock(&mem->state_mutex);
341
342 return ret;
343}
Dave Hansen3947be12005-10-29 18:16:54 -0700344static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800345store_mem_state(struct device *dev,
346 struct device_attribute *attr, const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700347{
348 struct memory_block *mem;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200349 bool offline;
Dave Hansen3947be12005-10-29 18:16:54 -0700350 int ret = -EINVAL;
351
Kay Sievers10fbcf42011-12-21 14:48:43 -0800352 mem = container_of(dev, struct memory_block, dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700353
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200354 lock_device_hotplug();
355
356 if (!strncmp(buf, "online_kernel", min_t(int, count, 13))) {
357 offline = false;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800358 ret = memory_block_change_state(mem, MEM_ONLINE,
359 MEM_OFFLINE, ONLINE_KERNEL);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200360 } else if (!strncmp(buf, "online_movable", min_t(int, count, 14))) {
361 offline = false;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800362 ret = memory_block_change_state(mem, MEM_ONLINE,
363 MEM_OFFLINE, ONLINE_MOVABLE);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200364 } else if (!strncmp(buf, "online", min_t(int, count, 6))) {
365 offline = false;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800366 ret = memory_block_change_state(mem, MEM_ONLINE,
367 MEM_OFFLINE, ONLINE_KEEP);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200368 } else if(!strncmp(buf, "offline", min_t(int, count, 7))) {
369 offline = true;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800370 ret = memory_block_change_state(mem, MEM_OFFLINE,
371 MEM_ONLINE, -1);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200372 }
373 if (!ret)
374 dev->offline = offline;
375
376 unlock_device_hotplug();
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600377
Dave Hansen3947be12005-10-29 18:16:54 -0700378 if (ret)
379 return ret;
380 return count;
381}
382
383/*
384 * phys_device is a bad name for this. What I really want
385 * is a way to differentiate between memory ranges that
386 * are part of physical devices that constitute
387 * a complete removable unit or fru.
388 * i.e. do these ranges belong to the same physical device,
389 * s.t. if I offline all of these sections I can then
390 * remove the physical device?
391 */
Kay Sievers10fbcf42011-12-21 14:48:43 -0800392static ssize_t show_phys_device(struct device *dev,
393 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700394{
395 struct memory_block *mem =
Kay Sievers10fbcf42011-12-21 14:48:43 -0800396 container_of(dev, struct memory_block, dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700397 return sprintf(buf, "%d\n", mem->phys_device);
398}
399
Kay Sievers10fbcf42011-12-21 14:48:43 -0800400static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
401static DEVICE_ATTR(end_phys_index, 0444, show_mem_end_phys_index, NULL);
402static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state);
403static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL);
404static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700405
Dave Hansen3947be12005-10-29 18:16:54 -0700406/*
407 * Block size attribute stuff
408 */
409static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800410print_block_size(struct device *dev, struct device_attribute *attr,
Andi Kleen8564a6c2010-01-05 12:48:06 +0100411 char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700412{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600413 return sprintf(buf, "%lx\n", get_memory_block_size());
Dave Hansen3947be12005-10-29 18:16:54 -0700414}
415
Kay Sievers10fbcf42011-12-21 14:48:43 -0800416static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700417
Dave Hansen3947be12005-10-29 18:16:54 -0700418/*
419 * Some architectures will have custom drivers to do this, and
420 * will not need to do it from userspace. The fake hot-add code
421 * as well as ppc64 will do all of their discovery in userspace
422 * and will require this interface.
423 */
424#ifdef CONFIG_ARCH_MEMORY_PROBE
425static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800426memory_probe_store(struct device *dev, struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100427 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700428{
429 u64 phys_addr;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700430 int nid;
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600431 int i, ret;
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000432 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700433
434 phys_addr = simple_strtoull(buf, NULL, 0);
435
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000436 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
437 return -EINVAL;
438
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600439 for (i = 0; i < sections_per_block; i++) {
440 nid = memory_add_physaddr_to_nid(phys_addr);
441 ret = add_memory(nid, phys_addr,
442 PAGES_PER_SECTION << PAGE_SHIFT);
443 if (ret)
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530444 goto out;
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600445
446 phys_addr += MIN_MEMORY_BLOCK_SIZE;
447 }
Dave Hansen3947be12005-10-29 18:16:54 -0700448
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530449 ret = count;
450out:
451 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700452}
Dave Hansen3947be12005-10-29 18:16:54 -0700453
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500454static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
Dave Hansen3947be12005-10-29 18:16:54 -0700455#endif
456
Andi Kleenfacb6012009-12-16 12:20:00 +0100457#ifdef CONFIG_MEMORY_FAILURE
458/*
459 * Support for offlining pages of memory
460 */
461
462/* Soft offline a page */
463static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800464store_soft_offline_page(struct device *dev,
465 struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100466 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100467{
468 int ret;
469 u64 pfn;
470 if (!capable(CAP_SYS_ADMIN))
471 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900472 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100473 return -EINVAL;
474 pfn >>= PAGE_SHIFT;
475 if (!pfn_valid(pfn))
476 return -ENXIO;
477 ret = soft_offline_page(pfn_to_page(pfn), 0);
478 return ret == 0 ? count : ret;
479}
480
481/* Forcibly offline a page, including killing processes. */
482static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800483store_hard_offline_page(struct device *dev,
484 struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100485 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100486{
487 int ret;
488 u64 pfn;
489 if (!capable(CAP_SYS_ADMIN))
490 return -EPERM;
Jingoo Han34da5e62013-07-26 13:10:22 +0900491 if (kstrtoull(buf, 0, &pfn) < 0)
Andi Kleenfacb6012009-12-16 12:20:00 +0100492 return -EINVAL;
493 pfn >>= PAGE_SHIFT;
Tony Luckcd42f4a2011-12-15 10:48:12 -0800494 ret = memory_failure(pfn, 0, 0);
Andi Kleenfacb6012009-12-16 12:20:00 +0100495 return ret ? ret : count;
496}
497
Felipe Balbi74fef7a2013-02-18 21:09:03 +0200498static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page);
499static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100500#endif
501
Dave Hansen3947be12005-10-29 18:16:54 -0700502/*
503 * Note that phys_device is optional. It is here to allow for
504 * differentiation between which *physical* devices each
505 * section belongs to...
506 */
Heiko Carstensbc32df02010-03-15 00:35:03 -0400507int __weak arch_get_memory_phys_device(unsigned long start_pfn)
508{
509 return 0;
510}
Dave Hansen3947be12005-10-29 18:16:54 -0700511
Kay Sievers10fbcf42011-12-21 14:48:43 -0800512/*
513 * A reference for the returned object is held and the reference for the
514 * hinted object is released.
515 */
Robin Holt98383032010-09-29 14:00:55 -0500516struct memory_block *find_memory_block_hinted(struct mem_section *section,
517 struct memory_block *hint)
518{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600519 int block_id = base_memory_block_id(__section_nr(section));
Kay Sievers10fbcf42011-12-21 14:48:43 -0800520 struct device *hintdev = hint ? &hint->dev : NULL;
521 struct device *dev;
Robin Holt98383032010-09-29 14:00:55 -0500522
Kay Sievers10fbcf42011-12-21 14:48:43 -0800523 dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
524 if (hint)
525 put_device(&hint->dev);
526 if (!dev)
Robin Holt98383032010-09-29 14:00:55 -0500527 return NULL;
Kay Sievers10fbcf42011-12-21 14:48:43 -0800528 return container_of(dev, struct memory_block, dev);
Robin Holt98383032010-09-29 14:00:55 -0500529}
530
Dave Hansen3947be12005-10-29 18:16:54 -0700531/*
532 * For now, we have a linear search to go find the appropriate
533 * memory_block corresponding to a particular phys_index. If
534 * this gets to be a real problem, we can always use a radix
535 * tree or something here.
536 *
Kay Sievers10fbcf42011-12-21 14:48:43 -0800537 * This could be made generic for all device subsystems.
Dave Hansen3947be12005-10-29 18:16:54 -0700538 */
Gary Hadec04fc582009-01-06 14:39:14 -0800539struct memory_block *find_memory_block(struct mem_section *section)
Dave Hansen3947be12005-10-29 18:16:54 -0700540{
Robin Holt98383032010-09-29 14:00:55 -0500541 return find_memory_block_hinted(section, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700542}
543
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500544static struct attribute *memory_memblk_attrs[] = {
545 &dev_attr_phys_index.attr,
546 &dev_attr_end_phys_index.attr,
547 &dev_attr_state.attr,
548 &dev_attr_phys_device.attr,
549 &dev_attr_removable.attr,
550 NULL
551};
552
553static struct attribute_group memory_memblk_attr_group = {
554 .attrs = memory_memblk_attrs,
555};
556
557static const struct attribute_group *memory_memblk_attr_groups[] = {
558 &memory_memblk_attr_group,
559 NULL,
560};
561
562/*
563 * register_memory - Setup a sysfs device for a memory block
564 */
565static
566int register_memory(struct memory_block *memory)
567{
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500568 memory->dev.bus = &memory_subsys;
569 memory->dev.id = memory->start_section_nr / sections_per_block;
570 memory->dev.release = memory_block_release;
571 memory->dev.groups = memory_memblk_attr_groups;
Linus Torvaldsf991fae2013-07-03 14:35:40 -0700572 memory->dev.offline = memory->state == MEM_OFFLINE;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500573
Seth Jennings879f1be2013-08-20 12:12:58 -0500574 return device_register(&memory->dev);
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500575}
576
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600577static int init_memory_block(struct memory_block **memory,
578 struct mem_section *section, unsigned long state)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500579{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600580 struct memory_block *mem;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500581 unsigned long start_pfn;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600582 int scn_nr;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500583 int ret = 0;
584
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600585 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500586 if (!mem)
587 return -ENOMEM;
588
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600589 scn_nr = __section_nr(section);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600590 mem->start_section_nr =
591 base_memory_block_id(scn_nr) * sections_per_block;
592 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500593 mem->state = state;
Nathan Fontenot07681212010-10-19 12:46:19 -0500594 mem->section_count++;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500595 mutex_init(&mem->state_mutex);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600596 start_pfn = section_nr_to_pfn(mem->start_section_nr);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500597 mem->phys_device = arch_get_memory_phys_device(start_pfn);
598
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600599 ret = register_memory(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600600
601 *memory = mem;
602 return ret;
603}
604
605static int add_memory_section(int nid, struct mem_section *section,
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800606 struct memory_block **mem_p,
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600607 unsigned long state, enum mem_add_context context)
608{
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800609 struct memory_block *mem = NULL;
610 int scn_nr = __section_nr(section);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600611 int ret = 0;
612
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800613 if (context == BOOT) {
614 /* same memory block ? */
615 if (mem_p && *mem_p)
616 if (scn_nr >= (*mem_p)->start_section_nr &&
617 scn_nr <= (*mem_p)->end_section_nr) {
618 mem = *mem_p;
Seth Jenningsdf2b7172013-08-20 12:12:59 -0500619 get_device(&mem->dev);
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800620 }
621 } else
622 mem = find_memory_block(section);
623
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600624 if (mem) {
625 mem->section_count++;
Seth Jenningsdf2b7172013-08-20 12:12:59 -0500626 put_device(&mem->dev);
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800627 } else {
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600628 ret = init_memory_block(&mem, section, state);
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800629 /* store memory_block pointer for next loop */
630 if (!ret && context == BOOT)
631 if (mem_p)
632 *mem_p = mem;
633 }
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600634
Nathan Fontenote4619c82010-10-19 12:44:20 -0500635 if (!ret) {
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600636 if (context == HOTPLUG &&
637 mem->section_count == sections_per_block)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500638 ret = register_mem_sect_under_node(mem, nid);
639 }
640
641 return ret;
642}
643
David Rientjes4edd7ce2013-04-29 15:08:22 -0700644/*
645 * need an interface for the VM to add new memory regions,
646 * but without onlining it.
647 */
648int register_new_memory(int nid, struct mem_section *section)
649{
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500650 int ret;
651
652 mutex_lock(&mem_sysfs_mutex);
653 ret = add_memory_section(nid, section, NULL, MEM_OFFLINE, HOTPLUG);
654 mutex_unlock(&mem_sysfs_mutex);
655
656 return ret;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700657}
658
659#ifdef CONFIG_MEMORY_HOTREMOVE
660static void
661unregister_memory(struct memory_block *memory)
662{
663 BUG_ON(memory->dev.bus != &memory_subsys);
664
665 /* drop the ref. we got in remove_memory_block() */
Seth Jenningsdf2b7172013-08-20 12:12:59 -0500666 put_device(&memory->dev);
David Rientjes4edd7ce2013-04-29 15:08:22 -0700667 device_unregister(&memory->dev);
668}
669
670static int remove_memory_block(unsigned long node_id,
671 struct mem_section *section, int phys_device)
Dave Hansen3947be12005-10-29 18:16:54 -0700672{
673 struct memory_block *mem;
674
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500675 mutex_lock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700676 mem = find_memory_block(section);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600677 unregister_mem_sect_under_nodes(mem, __section_nr(section));
Nathan Fontenot07681212010-10-19 12:46:19 -0500678
679 mem->section_count--;
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500680 if (mem->section_count == 0)
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600681 unregister_memory(mem);
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500682 else
Seth Jenningsdf2b7172013-08-20 12:12:59 -0500683 put_device(&mem->dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700684
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500685 mutex_unlock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700686 return 0;
687}
688
Dave Hansen3947be12005-10-29 18:16:54 -0700689int unregister_memory_section(struct mem_section *section)
690{
Andy Whitcroft540557b2007-10-16 01:24:11 -0700691 if (!present_section(section))
Dave Hansen3947be12005-10-29 18:16:54 -0700692 return -EINVAL;
693
694 return remove_memory_block(0, section, 0);
695}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700696#endif /* CONFIG_MEMORY_HOTREMOVE */
Dave Hansen3947be12005-10-29 18:16:54 -0700697
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -0800698/* return true if the memory block is offlined, otherwise, return false */
699bool is_memblock_offlined(struct memory_block *mem)
700{
701 return mem->state == MEM_OFFLINE;
702}
703
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500704static struct attribute *memory_root_attrs[] = {
705#ifdef CONFIG_ARCH_MEMORY_PROBE
706 &dev_attr_probe.attr,
707#endif
708
709#ifdef CONFIG_MEMORY_FAILURE
710 &dev_attr_soft_offline_page.attr,
711 &dev_attr_hard_offline_page.attr,
712#endif
713
714 &dev_attr_block_size_bytes.attr,
715 NULL
716};
717
718static struct attribute_group memory_root_attr_group = {
719 .attrs = memory_root_attrs,
720};
721
722static const struct attribute_group *memory_root_attr_groups[] = {
723 &memory_root_attr_group,
724 NULL,
725};
726
Wen Congyange90bdb72012-10-08 16:34:01 -0700727/*
Dave Hansen3947be12005-10-29 18:16:54 -0700728 * Initialize the sysfs support for memory devices...
729 */
730int __init memory_dev_init(void)
731{
732 unsigned int i;
733 int ret;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800734 int err;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600735 unsigned long block_sz;
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800736 struct memory_block *mem = NULL;
Dave Hansen3947be12005-10-29 18:16:54 -0700737
Nathan Fontenot96b2c0f2013-06-04 14:42:28 -0500738 ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800739 if (ret)
740 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700741
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600742 block_sz = get_memory_block_size();
743 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
744
Dave Hansen3947be12005-10-29 18:16:54 -0700745 /*
746 * Create entries for memory sections that were found
747 * during boot and have been initialized
748 */
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500749 mutex_lock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700750 for (i = 0; i < NR_MEM_SECTIONS; i++) {
Andy Whitcroft540557b2007-10-16 01:24:11 -0700751 if (!present_section_nr(i))
Dave Hansen3947be12005-10-29 18:16:54 -0700752 continue;
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800753 /* don't need to reuse memory_block if only one per block */
754 err = add_memory_section(0, __nr_to_section(i),
755 (sections_per_block == 1) ? NULL : &mem,
756 MEM_ONLINE,
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600757 BOOT);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800758 if (!ret)
759 ret = err;
Dave Hansen3947be12005-10-29 18:16:54 -0700760 }
Seth Jenningsb1eaef32013-08-20 12:12:57 -0500761 mutex_unlock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700762
Andrew Morton28ec24e2006-12-06 20:37:29 -0800763out:
764 if (ret)
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800765 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700766 return ret;
767}