blob: c7092bc3c01ecafa98ef8bbfe714eacb4159af72 [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
Dave Hansen3947be12005-10-29 18:16:54 -070085/*
86 * register_memory - Setup a sysfs device for a memory block
87 */
Badari Pulavarty00a41db2008-02-11 09:23:18 -080088static
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -060089int register_memory(struct memory_block *memory)
Dave Hansen3947be12005-10-29 18:16:54 -070090{
91 int error;
92
Kay Sievers10fbcf42011-12-21 14:48:43 -080093 memory->dev.bus = &memory_subsys;
94 memory->dev.id = memory->start_section_nr / sections_per_block;
Yasuaki Ishimatsufa7194e2012-12-11 16:00:44 -080095 memory->dev.release = memory_block_release;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +020096 memory->dev.offline = memory->state == MEM_OFFLINE;
Dave Hansen3947be12005-10-29 18:16:54 -070097
Kay Sievers10fbcf42011-12-21 14:48:43 -080098 error = device_register(&memory->dev);
Dave Hansen3947be12005-10-29 18:16:54 -070099 return error;
100}
101
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600102unsigned long __weak memory_block_size_bytes(void)
103{
104 return MIN_MEMORY_BLOCK_SIZE;
105}
106
107static unsigned long get_memory_block_size(void)
108{
109 unsigned long block_sz;
110
111 block_sz = memory_block_size_bytes();
112
113 /* Validate blk_sz is a power of 2 and not less than section size */
114 if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
115 WARN_ON(1);
116 block_sz = MIN_MEMORY_BLOCK_SIZE;
117 }
118
119 return block_sz;
120}
121
Dave Hansen3947be12005-10-29 18:16:54 -0700122/*
123 * use this as the physical section index that this memsection
124 * uses.
125 */
126
Kay Sievers10fbcf42011-12-21 14:48:43 -0800127static ssize_t show_mem_start_phys_index(struct device *dev,
128 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700129{
130 struct memory_block *mem =
Kay Sievers10fbcf42011-12-21 14:48:43 -0800131 container_of(dev, struct memory_block, dev);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600132 unsigned long phys_index;
133
134 phys_index = mem->start_section_nr / sections_per_block;
135 return sprintf(buf, "%08lx\n", phys_index);
136}
137
Kay Sievers10fbcf42011-12-21 14:48:43 -0800138static ssize_t show_mem_end_phys_index(struct device *dev,
139 struct device_attribute *attr, char *buf)
Nathan Fontenotd3360162011-01-20 10:44:29 -0600140{
141 struct memory_block *mem =
Kay Sievers10fbcf42011-12-21 14:48:43 -0800142 container_of(dev, struct memory_block, dev);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600143 unsigned long phys_index;
144
145 phys_index = mem->end_section_nr / sections_per_block;
146 return sprintf(buf, "%08lx\n", phys_index);
Dave Hansen3947be12005-10-29 18:16:54 -0700147}
148
149/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700150 * Show whether the section of memory is likely to be hot-removable
151 */
Kay Sievers10fbcf42011-12-21 14:48:43 -0800152static ssize_t show_mem_removable(struct device *dev,
153 struct device_attribute *attr, char *buf)
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700154{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600155 unsigned long i, pfn;
156 int ret = 1;
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700157 struct memory_block *mem =
Kay Sievers10fbcf42011-12-21 14:48:43 -0800158 container_of(dev, struct memory_block, dev);
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700159
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600160 for (i = 0; i < sections_per_block; i++) {
Nathan Fontenotd3360162011-01-20 10:44:29 -0600161 pfn = section_nr_to_pfn(mem->start_section_nr + i);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600162 ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
163 }
164
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700165 return sprintf(buf, "%d\n", ret);
166}
167
168/*
Dave Hansen3947be12005-10-29 18:16:54 -0700169 * online, offline, going offline, etc.
170 */
Kay Sievers10fbcf42011-12-21 14:48:43 -0800171static ssize_t show_mem_state(struct device *dev,
172 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700173{
174 struct memory_block *mem =
Kay Sievers10fbcf42011-12-21 14:48:43 -0800175 container_of(dev, struct memory_block, dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700176 ssize_t len = 0;
177
178 /*
179 * We can probably put these states in a nice little array
180 * so that they're not open-coded
181 */
182 switch (mem->state) {
183 case MEM_ONLINE:
184 len = sprintf(buf, "online\n");
185 break;
186 case MEM_OFFLINE:
187 len = sprintf(buf, "offline\n");
188 break;
189 case MEM_GOING_OFFLINE:
190 len = sprintf(buf, "going-offline\n");
191 break;
192 default:
193 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
194 mem->state);
195 WARN_ON(1);
196 break;
197 }
198
199 return len;
200}
201
Yasunori Goto7b78d332007-10-21 16:41:36 -0700202int memory_notify(unsigned long val, void *v)
Dave Hansen3947be12005-10-29 18:16:54 -0700203{
Alan Sterne041c682006-03-27 01:16:30 -0800204 return blocking_notifier_call_chain(&memory_chain, val, v);
Dave Hansen3947be12005-10-29 18:16:54 -0700205}
206
Robert Jennings925cc712009-12-17 14:44:38 +0000207int memory_isolate_notify(unsigned long val, void *v)
208{
209 return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
210}
211
Dave Hansen3947be12005-10-29 18:16:54 -0700212/*
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200213 * The probe routines leave the pages reserved, just as the bootmem code does.
214 * Make sure they're still that way.
215 */
Tang Chen6056d612013-04-29 15:08:40 -0700216static bool pages_correctly_reserved(unsigned long start_pfn)
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200217{
218 int i, j;
219 struct page *page;
220 unsigned long pfn = start_pfn;
221
222 /*
223 * memmap between sections is not contiguous except with
224 * SPARSEMEM_VMEMMAP. We lookup the page once per section
225 * and assume memmap is contiguous within each section
226 */
227 for (i = 0; i < sections_per_block; i++, pfn += PAGES_PER_SECTION) {
228 if (WARN_ON_ONCE(!pfn_valid(pfn)))
229 return false;
230 page = pfn_to_page(pfn);
231
232 for (j = 0; j < PAGES_PER_SECTION; j++) {
233 if (PageReserved(page + j))
234 continue;
235
236 printk(KERN_WARNING "section number %ld page number %d "
237 "not reserved, was it already online?\n",
238 pfn_to_section_nr(pfn), j);
239
240 return false;
241 }
242 }
243
244 return true;
245}
246
247/*
Dave Hansen3947be12005-10-29 18:16:54 -0700248 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
249 * OK to have direct references to sparsemem variables in here.
250 */
251static int
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800252memory_block_action(unsigned long phys_index, unsigned long action, int online_type)
Dave Hansen3947be12005-10-29 18:16:54 -0700253{
Wen Congyanga16cee12012-10-08 16:33:58 -0700254 unsigned long start_pfn;
Anton Blanchard5409d2c2011-05-11 17:25:14 +1000255 unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700256 struct page *first_page;
Dave Hansen3947be12005-10-29 18:16:54 -0700257 int ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700258
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700259 first_page = pfn_to_page(phys_index << PFN_SECTION_SHIFT);
Wen Congyanga16cee12012-10-08 16:33:58 -0700260 start_pfn = page_to_pfn(first_page);
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700261
Dave Hansen3947be12005-10-29 18:16:54 -0700262 switch (action) {
263 case MEM_ONLINE:
Tang Chen6056d612013-04-29 15:08:40 -0700264 if (!pages_correctly_reserved(start_pfn))
Mel Gorman2bbcb8782011-10-17 16:38:20 +0200265 return -EBUSY;
266
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800267 ret = online_pages(start_pfn, nr_pages, online_type);
Dave Hansen3947be12005-10-29 18:16:54 -0700268 break;
269 case MEM_OFFLINE:
Wen Congyanga16cee12012-10-08 16:33:58 -0700270 ret = offline_pages(start_pfn, nr_pages);
Dave Hansen3947be12005-10-29 18:16:54 -0700271 break;
272 default:
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600273 WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
274 "%ld\n", __func__, phys_index, action, action);
Dave Hansen3947be12005-10-29 18:16:54 -0700275 ret = -EINVAL;
276 }
Dave Hansen3947be12005-10-29 18:16:54 -0700277
278 return ret;
279}
280
Wen Congyange90bdb72012-10-08 16:34:01 -0700281static int __memory_block_change_state(struct memory_block *mem,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800282 unsigned long to_state, unsigned long from_state_req,
283 int online_type)
Dave Hansen3947be12005-10-29 18:16:54 -0700284{
Greg Kroah-Hartmande0ed362011-10-18 14:00:57 -0700285 int ret = 0;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600286
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200287 if (mem->state != from_state_req)
288 return -EINVAL;
Dave Hansen3947be12005-10-29 18:16:54 -0700289
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600290 if (to_state == MEM_OFFLINE)
291 mem->state = MEM_GOING_OFFLINE;
292
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800293 ret = memory_block_action(mem->start_section_nr, to_state, online_type);
Rafael J. Wysockib2c064b2013-05-23 10:38:55 +0200294 mem->state = ret ? from_state_req : to_state;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200295 return ret;
296}
Dave Hansen3947be12005-10-29 18:16:54 -0700297
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200298static int memory_subsys_online(struct device *dev)
299{
300 struct memory_block *mem = container_of(dev, struct memory_block, dev);
301 int ret;
302
303 mutex_lock(&mem->state_mutex);
304
305 ret = mem->state == MEM_ONLINE ? 0 :
306 __memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE,
Rafael J. Wysockib2c064b2013-05-23 10:38:55 +0200307 ONLINE_KEEP);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200308
309 mutex_unlock(&mem->state_mutex);
310 return ret;
311}
312
313static int memory_subsys_offline(struct device *dev)
314{
315 struct memory_block *mem = container_of(dev, struct memory_block, dev);
316 int ret;
317
318 mutex_lock(&mem->state_mutex);
319
320 ret = mem->state == MEM_OFFLINE ? 0 :
321 __memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE, -1);
322
323 mutex_unlock(&mem->state_mutex);
324 return ret;
325}
326
327static int __memory_block_change_state_uevent(struct memory_block *mem,
328 unsigned long to_state, unsigned long from_state_req,
329 int online_type)
330{
331 int ret = __memory_block_change_state(mem, to_state, from_state_req,
332 online_type);
333 if (!ret) {
334 switch (mem->state) {
335 case MEM_OFFLINE:
336 kobject_uevent(&mem->dev.kobj, KOBJ_OFFLINE);
337 break;
338 case MEM_ONLINE:
339 kobject_uevent(&mem->dev.kobj, KOBJ_ONLINE);
340 break;
341 default:
342 break;
343 }
Michael Holzheuf5138e42012-01-12 17:20:23 -0800344 }
Dave Hansen3947be12005-10-29 18:16:54 -0700345 return ret;
346}
347
Wen Congyange90bdb72012-10-08 16:34:01 -0700348static int memory_block_change_state(struct memory_block *mem,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800349 unsigned long to_state, unsigned long from_state_req,
350 int online_type)
Wen Congyange90bdb72012-10-08 16:34:01 -0700351{
352 int ret;
353
354 mutex_lock(&mem->state_mutex);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200355 ret = __memory_block_change_state_uevent(mem, to_state, from_state_req,
356 online_type);
Wen Congyange90bdb72012-10-08 16:34:01 -0700357 mutex_unlock(&mem->state_mutex);
358
359 return ret;
360}
Dave Hansen3947be12005-10-29 18:16:54 -0700361static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800362store_mem_state(struct device *dev,
363 struct device_attribute *attr, const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700364{
365 struct memory_block *mem;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200366 bool offline;
Dave Hansen3947be12005-10-29 18:16:54 -0700367 int ret = -EINVAL;
368
Kay Sievers10fbcf42011-12-21 14:48:43 -0800369 mem = container_of(dev, struct memory_block, dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700370
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200371 lock_device_hotplug();
372
373 if (!strncmp(buf, "online_kernel", min_t(int, count, 13))) {
374 offline = false;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800375 ret = memory_block_change_state(mem, MEM_ONLINE,
376 MEM_OFFLINE, ONLINE_KERNEL);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200377 } else if (!strncmp(buf, "online_movable", min_t(int, count, 14))) {
378 offline = false;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800379 ret = memory_block_change_state(mem, MEM_ONLINE,
380 MEM_OFFLINE, ONLINE_MOVABLE);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200381 } else if (!strncmp(buf, "online", min_t(int, count, 6))) {
382 offline = false;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800383 ret = memory_block_change_state(mem, MEM_ONLINE,
384 MEM_OFFLINE, ONLINE_KEEP);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200385 } else if(!strncmp(buf, "offline", min_t(int, count, 7))) {
386 offline = true;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800387 ret = memory_block_change_state(mem, MEM_OFFLINE,
388 MEM_ONLINE, -1);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200389 }
390 if (!ret)
391 dev->offline = offline;
392
393 unlock_device_hotplug();
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600394
Dave Hansen3947be12005-10-29 18:16:54 -0700395 if (ret)
396 return ret;
397 return count;
398}
399
400/*
401 * phys_device is a bad name for this. What I really want
402 * is a way to differentiate between memory ranges that
403 * are part of physical devices that constitute
404 * a complete removable unit or fru.
405 * i.e. do these ranges belong to the same physical device,
406 * s.t. if I offline all of these sections I can then
407 * remove the physical device?
408 */
Kay Sievers10fbcf42011-12-21 14:48:43 -0800409static ssize_t show_phys_device(struct device *dev,
410 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700411{
412 struct memory_block *mem =
Kay Sievers10fbcf42011-12-21 14:48:43 -0800413 container_of(dev, struct memory_block, dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700414 return sprintf(buf, "%d\n", mem->phys_device);
415}
416
Kay Sievers10fbcf42011-12-21 14:48:43 -0800417static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
418static DEVICE_ATTR(end_phys_index, 0444, show_mem_end_phys_index, NULL);
419static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state);
420static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL);
421static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700422
423#define mem_create_simple_file(mem, attr_name) \
Kay Sievers10fbcf42011-12-21 14:48:43 -0800424 device_create_file(&mem->dev, &dev_attr_##attr_name)
Dave Hansen3947be12005-10-29 18:16:54 -0700425#define mem_remove_simple_file(mem, attr_name) \
Kay Sievers10fbcf42011-12-21 14:48:43 -0800426 device_remove_file(&mem->dev, &dev_attr_##attr_name)
Dave Hansen3947be12005-10-29 18:16:54 -0700427
428/*
429 * Block size attribute stuff
430 */
431static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800432print_block_size(struct device *dev, struct device_attribute *attr,
Andi Kleen8564a6c2010-01-05 12:48:06 +0100433 char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700434{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600435 return sprintf(buf, "%lx\n", get_memory_block_size());
Dave Hansen3947be12005-10-29 18:16:54 -0700436}
437
Kay Sievers10fbcf42011-12-21 14:48:43 -0800438static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700439
440static int block_size_init(void)
441{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800442 return device_create_file(memory_subsys.dev_root,
443 &dev_attr_block_size_bytes);
Dave Hansen3947be12005-10-29 18:16:54 -0700444}
445
446/*
447 * Some architectures will have custom drivers to do this, and
448 * will not need to do it from userspace. The fake hot-add code
449 * as well as ppc64 will do all of their discovery in userspace
450 * and will require this interface.
451 */
452#ifdef CONFIG_ARCH_MEMORY_PROBE
453static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800454memory_probe_store(struct device *dev, struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100455 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700456{
457 u64 phys_addr;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700458 int nid;
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600459 int i, ret;
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000460 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700461
462 phys_addr = simple_strtoull(buf, NULL, 0);
463
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000464 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
465 return -EINVAL;
466
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600467 for (i = 0; i < sections_per_block; i++) {
468 nid = memory_add_physaddr_to_nid(phys_addr);
469 ret = add_memory(nid, phys_addr,
470 PAGES_PER_SECTION << PAGE_SHIFT);
471 if (ret)
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530472 goto out;
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600473
474 phys_addr += MIN_MEMORY_BLOCK_SIZE;
475 }
Dave Hansen3947be12005-10-29 18:16:54 -0700476
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530477 ret = count;
478out:
479 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700480}
Kay Sievers10fbcf42011-12-21 14:48:43 -0800481static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
Dave Hansen3947be12005-10-29 18:16:54 -0700482
483static int memory_probe_init(void)
484{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800485 return device_create_file(memory_subsys.dev_root, &dev_attr_probe);
Dave Hansen3947be12005-10-29 18:16:54 -0700486}
487#else
Andrew Morton28ec24e2006-12-06 20:37:29 -0800488static inline int memory_probe_init(void)
489{
490 return 0;
491}
Dave Hansen3947be12005-10-29 18:16:54 -0700492#endif
493
Andi Kleenfacb6012009-12-16 12:20:00 +0100494#ifdef CONFIG_MEMORY_FAILURE
495/*
496 * Support for offlining pages of memory
497 */
498
499/* Soft offline a page */
500static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800501store_soft_offline_page(struct device *dev,
502 struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100503 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100504{
505 int ret;
506 u64 pfn;
507 if (!capable(CAP_SYS_ADMIN))
508 return -EPERM;
509 if (strict_strtoull(buf, 0, &pfn) < 0)
510 return -EINVAL;
511 pfn >>= PAGE_SHIFT;
512 if (!pfn_valid(pfn))
513 return -ENXIO;
514 ret = soft_offline_page(pfn_to_page(pfn), 0);
515 return ret == 0 ? count : ret;
516}
517
518/* Forcibly offline a page, including killing processes. */
519static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800520store_hard_offline_page(struct device *dev,
521 struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100522 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100523{
524 int ret;
525 u64 pfn;
526 if (!capable(CAP_SYS_ADMIN))
527 return -EPERM;
528 if (strict_strtoull(buf, 0, &pfn) < 0)
529 return -EINVAL;
530 pfn >>= PAGE_SHIFT;
Tony Luckcd42f4a2011-12-15 10:48:12 -0800531 ret = memory_failure(pfn, 0, 0);
Andi Kleenfacb6012009-12-16 12:20:00 +0100532 return ret ? ret : count;
533}
534
Felipe Balbi74fef7a2013-02-18 21:09:03 +0200535static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page);
536static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100537
538static __init int memory_fail_init(void)
539{
540 int err;
541
Kay Sievers10fbcf42011-12-21 14:48:43 -0800542 err = device_create_file(memory_subsys.dev_root,
543 &dev_attr_soft_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100544 if (!err)
Kay Sievers10fbcf42011-12-21 14:48:43 -0800545 err = device_create_file(memory_subsys.dev_root,
546 &dev_attr_hard_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100547 return err;
548}
549#else
550static inline int memory_fail_init(void)
551{
552 return 0;
553}
554#endif
555
Dave Hansen3947be12005-10-29 18:16:54 -0700556/*
557 * Note that phys_device is optional. It is here to allow for
558 * differentiation between which *physical* devices each
559 * section belongs to...
560 */
Heiko Carstensbc32df02010-03-15 00:35:03 -0400561int __weak arch_get_memory_phys_device(unsigned long start_pfn)
562{
563 return 0;
564}
Dave Hansen3947be12005-10-29 18:16:54 -0700565
Kay Sievers10fbcf42011-12-21 14:48:43 -0800566/*
567 * A reference for the returned object is held and the reference for the
568 * hinted object is released.
569 */
Robin Holt98383032010-09-29 14:00:55 -0500570struct memory_block *find_memory_block_hinted(struct mem_section *section,
571 struct memory_block *hint)
572{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600573 int block_id = base_memory_block_id(__section_nr(section));
Kay Sievers10fbcf42011-12-21 14:48:43 -0800574 struct device *hintdev = hint ? &hint->dev : NULL;
575 struct device *dev;
Robin Holt98383032010-09-29 14:00:55 -0500576
Kay Sievers10fbcf42011-12-21 14:48:43 -0800577 dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
578 if (hint)
579 put_device(&hint->dev);
580 if (!dev)
Robin Holt98383032010-09-29 14:00:55 -0500581 return NULL;
Kay Sievers10fbcf42011-12-21 14:48:43 -0800582 return container_of(dev, struct memory_block, dev);
Robin Holt98383032010-09-29 14:00:55 -0500583}
584
Dave Hansen3947be12005-10-29 18:16:54 -0700585/*
586 * For now, we have a linear search to go find the appropriate
587 * memory_block corresponding to a particular phys_index. If
588 * this gets to be a real problem, we can always use a radix
589 * tree or something here.
590 *
Kay Sievers10fbcf42011-12-21 14:48:43 -0800591 * This could be made generic for all device subsystems.
Dave Hansen3947be12005-10-29 18:16:54 -0700592 */
Gary Hadec04fc582009-01-06 14:39:14 -0800593struct memory_block *find_memory_block(struct mem_section *section)
Dave Hansen3947be12005-10-29 18:16:54 -0700594{
Robin Holt98383032010-09-29 14:00:55 -0500595 return find_memory_block_hinted(section, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700596}
597
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600598static int init_memory_block(struct memory_block **memory,
599 struct mem_section *section, unsigned long state)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500600{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600601 struct memory_block *mem;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500602 unsigned long start_pfn;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600603 int scn_nr;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500604 int ret = 0;
605
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600606 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500607 if (!mem)
608 return -ENOMEM;
609
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600610 scn_nr = __section_nr(section);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600611 mem->start_section_nr =
612 base_memory_block_id(scn_nr) * sections_per_block;
613 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500614 mem->state = state;
Nathan Fontenot07681212010-10-19 12:46:19 -0500615 mem->section_count++;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500616 mutex_init(&mem->state_mutex);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600617 start_pfn = section_nr_to_pfn(mem->start_section_nr);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500618 mem->phys_device = arch_get_memory_phys_device(start_pfn);
619
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600620 ret = register_memory(mem);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500621 if (!ret)
622 ret = mem_create_simple_file(mem, phys_index);
623 if (!ret)
Nathan Fontenotd3360162011-01-20 10:44:29 -0600624 ret = mem_create_simple_file(mem, end_phys_index);
625 if (!ret)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500626 ret = mem_create_simple_file(mem, state);
627 if (!ret)
628 ret = mem_create_simple_file(mem, phys_device);
629 if (!ret)
630 ret = mem_create_simple_file(mem, removable);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600631
632 *memory = mem;
633 return ret;
634}
635
636static int add_memory_section(int nid, struct mem_section *section,
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800637 struct memory_block **mem_p,
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600638 unsigned long state, enum mem_add_context context)
639{
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800640 struct memory_block *mem = NULL;
641 int scn_nr = __section_nr(section);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600642 int ret = 0;
643
644 mutex_lock(&mem_sysfs_mutex);
645
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800646 if (context == BOOT) {
647 /* same memory block ? */
648 if (mem_p && *mem_p)
649 if (scn_nr >= (*mem_p)->start_section_nr &&
650 scn_nr <= (*mem_p)->end_section_nr) {
651 mem = *mem_p;
652 kobject_get(&mem->dev.kobj);
653 }
654 } else
655 mem = find_memory_block(section);
656
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600657 if (mem) {
658 mem->section_count++;
Kay Sievers10fbcf42011-12-21 14:48:43 -0800659 kobject_put(&mem->dev.kobj);
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800660 } else {
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600661 ret = init_memory_block(&mem, section, state);
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800662 /* store memory_block pointer for next loop */
663 if (!ret && context == BOOT)
664 if (mem_p)
665 *mem_p = mem;
666 }
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600667
Nathan Fontenote4619c82010-10-19 12:44:20 -0500668 if (!ret) {
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600669 if (context == HOTPLUG &&
670 mem->section_count == sections_per_block)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500671 ret = register_mem_sect_under_node(mem, nid);
672 }
673
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500674 mutex_unlock(&mem_sysfs_mutex);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500675 return ret;
676}
677
David Rientjes4edd7ce2013-04-29 15:08:22 -0700678/*
679 * need an interface for the VM to add new memory regions,
680 * but without onlining it.
681 */
682int register_new_memory(int nid, struct mem_section *section)
683{
684 return add_memory_section(nid, section, NULL, MEM_OFFLINE, HOTPLUG);
685}
686
687#ifdef CONFIG_MEMORY_HOTREMOVE
688static void
689unregister_memory(struct memory_block *memory)
690{
691 BUG_ON(memory->dev.bus != &memory_subsys);
692
693 /* drop the ref. we got in remove_memory_block() */
694 kobject_put(&memory->dev.kobj);
695 device_unregister(&memory->dev);
696}
697
698static int remove_memory_block(unsigned long node_id,
699 struct mem_section *section, int phys_device)
Dave Hansen3947be12005-10-29 18:16:54 -0700700{
701 struct memory_block *mem;
702
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500703 mutex_lock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700704 mem = find_memory_block(section);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600705 unregister_mem_sect_under_nodes(mem, __section_nr(section));
Nathan Fontenot07681212010-10-19 12:46:19 -0500706
707 mem->section_count--;
708 if (mem->section_count == 0) {
Nathan Fontenot07681212010-10-19 12:46:19 -0500709 mem_remove_simple_file(mem, phys_index);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600710 mem_remove_simple_file(mem, end_phys_index);
Nathan Fontenot07681212010-10-19 12:46:19 -0500711 mem_remove_simple_file(mem, state);
712 mem_remove_simple_file(mem, phys_device);
713 mem_remove_simple_file(mem, removable);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600714 unregister_memory(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600715 } else
Kay Sievers10fbcf42011-12-21 14:48:43 -0800716 kobject_put(&mem->dev.kobj);
Dave Hansen3947be12005-10-29 18:16:54 -0700717
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500718 mutex_unlock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700719 return 0;
720}
721
Dave Hansen3947be12005-10-29 18:16:54 -0700722int unregister_memory_section(struct mem_section *section)
723{
Andy Whitcroft540557b2007-10-16 01:24:11 -0700724 if (!present_section(section))
Dave Hansen3947be12005-10-29 18:16:54 -0700725 return -EINVAL;
726
727 return remove_memory_block(0, section, 0);
728}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700729#endif /* CONFIG_MEMORY_HOTREMOVE */
Dave Hansen3947be12005-10-29 18:16:54 -0700730
731/*
Wen Congyange90bdb72012-10-08 16:34:01 -0700732 * offline one memory block. If the memory block has been offlined, do nothing.
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200733 *
734 * Call under device_hotplug_lock.
Wen Congyange90bdb72012-10-08 16:34:01 -0700735 */
736int offline_memory_block(struct memory_block *mem)
737{
738 int ret = 0;
739
740 mutex_lock(&mem->state_mutex);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200741 if (mem->state != MEM_OFFLINE) {
742 ret = __memory_block_change_state_uevent(mem, MEM_OFFLINE,
743 MEM_ONLINE, -1);
744 if (!ret)
745 mem->dev.offline = true;
746 }
Wen Congyange90bdb72012-10-08 16:34:01 -0700747 mutex_unlock(&mem->state_mutex);
748
749 return ret;
750}
751
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -0800752/* 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
Wen Congyange90bdb72012-10-08 16:34:01 -0700758/*
Dave Hansen3947be12005-10-29 18:16:54 -0700759 * Initialize the sysfs support for memory devices...
760 */
761int __init memory_dev_init(void)
762{
763 unsigned int i;
764 int ret;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800765 int err;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600766 unsigned long block_sz;
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800767 struct memory_block *mem = NULL;
Dave Hansen3947be12005-10-29 18:16:54 -0700768
Kay Sievers10fbcf42011-12-21 14:48:43 -0800769 ret = subsys_system_register(&memory_subsys, NULL);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800770 if (ret)
771 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700772
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600773 block_sz = get_memory_block_size();
774 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
775
Dave Hansen3947be12005-10-29 18:16:54 -0700776 /*
777 * Create entries for memory sections that were found
778 * during boot and have been initialized
779 */
780 for (i = 0; i < NR_MEM_SECTIONS; i++) {
Andy Whitcroft540557b2007-10-16 01:24:11 -0700781 if (!present_section_nr(i))
Dave Hansen3947be12005-10-29 18:16:54 -0700782 continue;
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800783 /* don't need to reuse memory_block if only one per block */
784 err = add_memory_section(0, __nr_to_section(i),
785 (sections_per_block == 1) ? NULL : &mem,
786 MEM_ONLINE,
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600787 BOOT);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800788 if (!ret)
789 ret = err;
Dave Hansen3947be12005-10-29 18:16:54 -0700790 }
791
Andrew Morton28ec24e2006-12-06 20:37:29 -0800792 err = memory_probe_init();
793 if (!ret)
794 ret = err;
Andi Kleenfacb6012009-12-16 12:20:00 +0100795 err = memory_fail_init();
796 if (!ret)
797 ret = err;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800798 err = block_size_init();
799 if (!ret)
800 ret = err;
801out:
802 if (ret)
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800803 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700804 return ret;
805}