blob: c8f3b63fcacd56cf4460d20dfa505066fd51d5e8 [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);
Michael Holzheuf5138e42012-01-12 17:20:23 -0800294 if (ret) {
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600295 mem->state = from_state_req;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200296 } else {
297 mem->state = to_state;
298 if (to_state == MEM_ONLINE)
299 mem->last_online = online_type;
Michael Holzheuf5138e42012-01-12 17:20:23 -0800300 }
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200301 return ret;
302}
Dave Hansen3947be12005-10-29 18:16:54 -0700303
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200304static int memory_subsys_online(struct device *dev)
305{
306 struct memory_block *mem = container_of(dev, struct memory_block, dev);
307 int ret;
308
309 mutex_lock(&mem->state_mutex);
310
311 ret = mem->state == MEM_ONLINE ? 0 :
312 __memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE,
313 mem->last_online);
314
315 mutex_unlock(&mem->state_mutex);
316 return ret;
317}
318
319static int memory_subsys_offline(struct device *dev)
320{
321 struct memory_block *mem = container_of(dev, struct memory_block, dev);
322 int ret;
323
324 mutex_lock(&mem->state_mutex);
325
326 ret = mem->state == MEM_OFFLINE ? 0 :
327 __memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE, -1);
328
329 mutex_unlock(&mem->state_mutex);
330 return ret;
331}
332
333static int __memory_block_change_state_uevent(struct memory_block *mem,
334 unsigned long to_state, unsigned long from_state_req,
335 int online_type)
336{
337 int ret = __memory_block_change_state(mem, to_state, from_state_req,
338 online_type);
339 if (!ret) {
340 switch (mem->state) {
341 case MEM_OFFLINE:
342 kobject_uevent(&mem->dev.kobj, KOBJ_OFFLINE);
343 break;
344 case MEM_ONLINE:
345 kobject_uevent(&mem->dev.kobj, KOBJ_ONLINE);
346 break;
347 default:
348 break;
349 }
Michael Holzheuf5138e42012-01-12 17:20:23 -0800350 }
Dave Hansen3947be12005-10-29 18:16:54 -0700351 return ret;
352}
353
Wen Congyange90bdb72012-10-08 16:34:01 -0700354static int memory_block_change_state(struct memory_block *mem,
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800355 unsigned long to_state, unsigned long from_state_req,
356 int online_type)
Wen Congyange90bdb72012-10-08 16:34:01 -0700357{
358 int ret;
359
360 mutex_lock(&mem->state_mutex);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200361 ret = __memory_block_change_state_uevent(mem, to_state, from_state_req,
362 online_type);
Wen Congyange90bdb72012-10-08 16:34:01 -0700363 mutex_unlock(&mem->state_mutex);
364
365 return ret;
366}
Dave Hansen3947be12005-10-29 18:16:54 -0700367static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800368store_mem_state(struct device *dev,
369 struct device_attribute *attr, const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700370{
371 struct memory_block *mem;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200372 bool offline;
Dave Hansen3947be12005-10-29 18:16:54 -0700373 int ret = -EINVAL;
374
Kay Sievers10fbcf42011-12-21 14:48:43 -0800375 mem = container_of(dev, struct memory_block, dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700376
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200377 lock_device_hotplug();
378
379 if (!strncmp(buf, "online_kernel", min_t(int, count, 13))) {
380 offline = false;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800381 ret = memory_block_change_state(mem, MEM_ONLINE,
382 MEM_OFFLINE, ONLINE_KERNEL);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200383 } else if (!strncmp(buf, "online_movable", min_t(int, count, 14))) {
384 offline = false;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800385 ret = memory_block_change_state(mem, MEM_ONLINE,
386 MEM_OFFLINE, ONLINE_MOVABLE);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200387 } else if (!strncmp(buf, "online", min_t(int, count, 6))) {
388 offline = false;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800389 ret = memory_block_change_state(mem, MEM_ONLINE,
390 MEM_OFFLINE, ONLINE_KEEP);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200391 } else if(!strncmp(buf, "offline", min_t(int, count, 7))) {
392 offline = true;
Lai Jiangshan511c2ab2012-12-11 16:03:16 -0800393 ret = memory_block_change_state(mem, MEM_OFFLINE,
394 MEM_ONLINE, -1);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200395 }
396 if (!ret)
397 dev->offline = offline;
398
399 unlock_device_hotplug();
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600400
Dave Hansen3947be12005-10-29 18:16:54 -0700401 if (ret)
402 return ret;
403 return count;
404}
405
406/*
407 * phys_device is a bad name for this. What I really want
408 * is a way to differentiate between memory ranges that
409 * are part of physical devices that constitute
410 * a complete removable unit or fru.
411 * i.e. do these ranges belong to the same physical device,
412 * s.t. if I offline all of these sections I can then
413 * remove the physical device?
414 */
Kay Sievers10fbcf42011-12-21 14:48:43 -0800415static ssize_t show_phys_device(struct device *dev,
416 struct device_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700417{
418 struct memory_block *mem =
Kay Sievers10fbcf42011-12-21 14:48:43 -0800419 container_of(dev, struct memory_block, dev);
Dave Hansen3947be12005-10-29 18:16:54 -0700420 return sprintf(buf, "%d\n", mem->phys_device);
421}
422
Kay Sievers10fbcf42011-12-21 14:48:43 -0800423static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
424static DEVICE_ATTR(end_phys_index, 0444, show_mem_end_phys_index, NULL);
425static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state);
426static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL);
427static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700428
429#define mem_create_simple_file(mem, attr_name) \
Kay Sievers10fbcf42011-12-21 14:48:43 -0800430 device_create_file(&mem->dev, &dev_attr_##attr_name)
Dave Hansen3947be12005-10-29 18:16:54 -0700431#define mem_remove_simple_file(mem, attr_name) \
Kay Sievers10fbcf42011-12-21 14:48:43 -0800432 device_remove_file(&mem->dev, &dev_attr_##attr_name)
Dave Hansen3947be12005-10-29 18:16:54 -0700433
434/*
435 * Block size attribute stuff
436 */
437static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800438print_block_size(struct device *dev, struct device_attribute *attr,
Andi Kleen8564a6c2010-01-05 12:48:06 +0100439 char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700440{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600441 return sprintf(buf, "%lx\n", get_memory_block_size());
Dave Hansen3947be12005-10-29 18:16:54 -0700442}
443
Kay Sievers10fbcf42011-12-21 14:48:43 -0800444static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700445
446static int block_size_init(void)
447{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800448 return device_create_file(memory_subsys.dev_root,
449 &dev_attr_block_size_bytes);
Dave Hansen3947be12005-10-29 18:16:54 -0700450}
451
452/*
453 * Some architectures will have custom drivers to do this, and
454 * will not need to do it from userspace. The fake hot-add code
455 * as well as ppc64 will do all of their discovery in userspace
456 * and will require this interface.
457 */
458#ifdef CONFIG_ARCH_MEMORY_PROBE
459static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800460memory_probe_store(struct device *dev, struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100461 const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700462{
463 u64 phys_addr;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700464 int nid;
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600465 int i, ret;
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000466 unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
Dave Hansen3947be12005-10-29 18:16:54 -0700467
468 phys_addr = simple_strtoull(buf, NULL, 0);
469
Anton Blanchard61b94fe2011-09-15 06:26:15 +1000470 if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
471 return -EINVAL;
472
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600473 for (i = 0; i < sections_per_block; i++) {
474 nid = memory_add_physaddr_to_nid(phys_addr);
475 ret = add_memory(nid, phys_addr,
476 PAGES_PER_SECTION << PAGE_SHIFT);
477 if (ret)
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530478 goto out;
Nathan Fontenot6add7cd2011-01-31 10:55:23 -0600479
480 phys_addr += MIN_MEMORY_BLOCK_SIZE;
481 }
Dave Hansen3947be12005-10-29 18:16:54 -0700482
Nikanth Karthikesan9f0af692011-03-24 11:46:18 +0530483 ret = count;
484out:
485 return ret;
Dave Hansen3947be12005-10-29 18:16:54 -0700486}
Kay Sievers10fbcf42011-12-21 14:48:43 -0800487static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
Dave Hansen3947be12005-10-29 18:16:54 -0700488
489static int memory_probe_init(void)
490{
Kay Sievers10fbcf42011-12-21 14:48:43 -0800491 return device_create_file(memory_subsys.dev_root, &dev_attr_probe);
Dave Hansen3947be12005-10-29 18:16:54 -0700492}
493#else
Andrew Morton28ec24e2006-12-06 20:37:29 -0800494static inline int memory_probe_init(void)
495{
496 return 0;
497}
Dave Hansen3947be12005-10-29 18:16:54 -0700498#endif
499
Andi Kleenfacb6012009-12-16 12:20:00 +0100500#ifdef CONFIG_MEMORY_FAILURE
501/*
502 * Support for offlining pages of memory
503 */
504
505/* Soft offline a page */
506static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800507store_soft_offline_page(struct device *dev,
508 struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100509 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100510{
511 int ret;
512 u64 pfn;
513 if (!capable(CAP_SYS_ADMIN))
514 return -EPERM;
515 if (strict_strtoull(buf, 0, &pfn) < 0)
516 return -EINVAL;
517 pfn >>= PAGE_SHIFT;
518 if (!pfn_valid(pfn))
519 return -ENXIO;
520 ret = soft_offline_page(pfn_to_page(pfn), 0);
521 return ret == 0 ? count : ret;
522}
523
524/* Forcibly offline a page, including killing processes. */
525static ssize_t
Kay Sievers10fbcf42011-12-21 14:48:43 -0800526store_hard_offline_page(struct device *dev,
527 struct device_attribute *attr,
Andi Kleen28812fe2010-01-05 12:48:07 +0100528 const char *buf, size_t count)
Andi Kleenfacb6012009-12-16 12:20:00 +0100529{
530 int ret;
531 u64 pfn;
532 if (!capable(CAP_SYS_ADMIN))
533 return -EPERM;
534 if (strict_strtoull(buf, 0, &pfn) < 0)
535 return -EINVAL;
536 pfn >>= PAGE_SHIFT;
Tony Luckcd42f4a2011-12-15 10:48:12 -0800537 ret = memory_failure(pfn, 0, 0);
Andi Kleenfacb6012009-12-16 12:20:00 +0100538 return ret ? ret : count;
539}
540
Felipe Balbi74fef7a2013-02-18 21:09:03 +0200541static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page);
542static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100543
544static __init int memory_fail_init(void)
545{
546 int err;
547
Kay Sievers10fbcf42011-12-21 14:48:43 -0800548 err = device_create_file(memory_subsys.dev_root,
549 &dev_attr_soft_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100550 if (!err)
Kay Sievers10fbcf42011-12-21 14:48:43 -0800551 err = device_create_file(memory_subsys.dev_root,
552 &dev_attr_hard_offline_page);
Andi Kleenfacb6012009-12-16 12:20:00 +0100553 return err;
554}
555#else
556static inline int memory_fail_init(void)
557{
558 return 0;
559}
560#endif
561
Dave Hansen3947be12005-10-29 18:16:54 -0700562/*
563 * Note that phys_device is optional. It is here to allow for
564 * differentiation between which *physical* devices each
565 * section belongs to...
566 */
Heiko Carstensbc32df02010-03-15 00:35:03 -0400567int __weak arch_get_memory_phys_device(unsigned long start_pfn)
568{
569 return 0;
570}
Dave Hansen3947be12005-10-29 18:16:54 -0700571
Kay Sievers10fbcf42011-12-21 14:48:43 -0800572/*
573 * A reference for the returned object is held and the reference for the
574 * hinted object is released.
575 */
Robin Holt98383032010-09-29 14:00:55 -0500576struct memory_block *find_memory_block_hinted(struct mem_section *section,
577 struct memory_block *hint)
578{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600579 int block_id = base_memory_block_id(__section_nr(section));
Kay Sievers10fbcf42011-12-21 14:48:43 -0800580 struct device *hintdev = hint ? &hint->dev : NULL;
581 struct device *dev;
Robin Holt98383032010-09-29 14:00:55 -0500582
Kay Sievers10fbcf42011-12-21 14:48:43 -0800583 dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
584 if (hint)
585 put_device(&hint->dev);
586 if (!dev)
Robin Holt98383032010-09-29 14:00:55 -0500587 return NULL;
Kay Sievers10fbcf42011-12-21 14:48:43 -0800588 return container_of(dev, struct memory_block, dev);
Robin Holt98383032010-09-29 14:00:55 -0500589}
590
Dave Hansen3947be12005-10-29 18:16:54 -0700591/*
592 * For now, we have a linear search to go find the appropriate
593 * memory_block corresponding to a particular phys_index. If
594 * this gets to be a real problem, we can always use a radix
595 * tree or something here.
596 *
Kay Sievers10fbcf42011-12-21 14:48:43 -0800597 * This could be made generic for all device subsystems.
Dave Hansen3947be12005-10-29 18:16:54 -0700598 */
Gary Hadec04fc582009-01-06 14:39:14 -0800599struct memory_block *find_memory_block(struct mem_section *section)
Dave Hansen3947be12005-10-29 18:16:54 -0700600{
Robin Holt98383032010-09-29 14:00:55 -0500601 return find_memory_block_hinted(section, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700602}
603
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600604static int init_memory_block(struct memory_block **memory,
605 struct mem_section *section, unsigned long state)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500606{
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600607 struct memory_block *mem;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500608 unsigned long start_pfn;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600609 int scn_nr;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500610 int ret = 0;
611
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600612 mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500613 if (!mem)
614 return -ENOMEM;
615
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600616 scn_nr = __section_nr(section);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600617 mem->start_section_nr =
618 base_memory_block_id(scn_nr) * sections_per_block;
619 mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500620 mem->state = state;
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200621 mem->last_online = ONLINE_KEEP;
Nathan Fontenot07681212010-10-19 12:46:19 -0500622 mem->section_count++;
Nathan Fontenote4619c82010-10-19 12:44:20 -0500623 mutex_init(&mem->state_mutex);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600624 start_pfn = section_nr_to_pfn(mem->start_section_nr);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500625 mem->phys_device = arch_get_memory_phys_device(start_pfn);
626
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600627 ret = register_memory(mem);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500628 if (!ret)
629 ret = mem_create_simple_file(mem, phys_index);
630 if (!ret)
Nathan Fontenotd3360162011-01-20 10:44:29 -0600631 ret = mem_create_simple_file(mem, end_phys_index);
632 if (!ret)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500633 ret = mem_create_simple_file(mem, state);
634 if (!ret)
635 ret = mem_create_simple_file(mem, phys_device);
636 if (!ret)
637 ret = mem_create_simple_file(mem, removable);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600638
639 *memory = mem;
640 return ret;
641}
642
643static int add_memory_section(int nid, struct mem_section *section,
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800644 struct memory_block **mem_p,
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600645 unsigned long state, enum mem_add_context context)
646{
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800647 struct memory_block *mem = NULL;
648 int scn_nr = __section_nr(section);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600649 int ret = 0;
650
651 mutex_lock(&mem_sysfs_mutex);
652
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800653 if (context == BOOT) {
654 /* same memory block ? */
655 if (mem_p && *mem_p)
656 if (scn_nr >= (*mem_p)->start_section_nr &&
657 scn_nr <= (*mem_p)->end_section_nr) {
658 mem = *mem_p;
659 kobject_get(&mem->dev.kobj);
660 }
661 } else
662 mem = find_memory_block(section);
663
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600664 if (mem) {
665 mem->section_count++;
Kay Sievers10fbcf42011-12-21 14:48:43 -0800666 kobject_put(&mem->dev.kobj);
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800667 } else {
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600668 ret = init_memory_block(&mem, section, state);
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800669 /* store memory_block pointer for next loop */
670 if (!ret && context == BOOT)
671 if (mem_p)
672 *mem_p = mem;
673 }
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600674
Nathan Fontenote4619c82010-10-19 12:44:20 -0500675 if (!ret) {
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600676 if (context == HOTPLUG &&
677 mem->section_count == sections_per_block)
Nathan Fontenote4619c82010-10-19 12:44:20 -0500678 ret = register_mem_sect_under_node(mem, nid);
679 }
680
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500681 mutex_unlock(&mem_sysfs_mutex);
Nathan Fontenote4619c82010-10-19 12:44:20 -0500682 return ret;
683}
684
David Rientjes4edd7ce2013-04-29 15:08:22 -0700685/*
686 * need an interface for the VM to add new memory regions,
687 * but without onlining it.
688 */
689int register_new_memory(int nid, struct mem_section *section)
690{
691 return add_memory_section(nid, section, NULL, MEM_OFFLINE, HOTPLUG);
692}
693
694#ifdef CONFIG_MEMORY_HOTREMOVE
695static void
696unregister_memory(struct memory_block *memory)
697{
698 BUG_ON(memory->dev.bus != &memory_subsys);
699
700 /* drop the ref. we got in remove_memory_block() */
701 kobject_put(&memory->dev.kobj);
702 device_unregister(&memory->dev);
703}
704
705static int remove_memory_block(unsigned long node_id,
706 struct mem_section *section, int phys_device)
Dave Hansen3947be12005-10-29 18:16:54 -0700707{
708 struct memory_block *mem;
709
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500710 mutex_lock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700711 mem = find_memory_block(section);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600712 unregister_mem_sect_under_nodes(mem, __section_nr(section));
Nathan Fontenot07681212010-10-19 12:46:19 -0500713
714 mem->section_count--;
715 if (mem->section_count == 0) {
Nathan Fontenot07681212010-10-19 12:46:19 -0500716 mem_remove_simple_file(mem, phys_index);
Nathan Fontenotd3360162011-01-20 10:44:29 -0600717 mem_remove_simple_file(mem, end_phys_index);
Nathan Fontenot07681212010-10-19 12:46:19 -0500718 mem_remove_simple_file(mem, state);
719 mem_remove_simple_file(mem, phys_device);
720 mem_remove_simple_file(mem, removable);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600721 unregister_memory(mem);
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600722 } else
Kay Sievers10fbcf42011-12-21 14:48:43 -0800723 kobject_put(&mem->dev.kobj);
Dave Hansen3947be12005-10-29 18:16:54 -0700724
Nathan Fontenot2938ffb2010-10-19 12:45:24 -0500725 mutex_unlock(&mem_sysfs_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700726 return 0;
727}
728
Dave Hansen3947be12005-10-29 18:16:54 -0700729int unregister_memory_section(struct mem_section *section)
730{
Andy Whitcroft540557b2007-10-16 01:24:11 -0700731 if (!present_section(section))
Dave Hansen3947be12005-10-29 18:16:54 -0700732 return -EINVAL;
733
734 return remove_memory_block(0, section, 0);
735}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700736#endif /* CONFIG_MEMORY_HOTREMOVE */
Dave Hansen3947be12005-10-29 18:16:54 -0700737
738/*
Wen Congyange90bdb72012-10-08 16:34:01 -0700739 * offline one memory block. If the memory block has been offlined, do nothing.
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200740 *
741 * Call under device_hotplug_lock.
Wen Congyange90bdb72012-10-08 16:34:01 -0700742 */
743int offline_memory_block(struct memory_block *mem)
744{
745 int ret = 0;
746
747 mutex_lock(&mem->state_mutex);
Rafael J. Wysocki4960e052013-05-08 14:18:37 +0200748 if (mem->state != MEM_OFFLINE) {
749 ret = __memory_block_change_state_uevent(mem, MEM_OFFLINE,
750 MEM_ONLINE, -1);
751 if (!ret)
752 mem->dev.offline = true;
753 }
Wen Congyange90bdb72012-10-08 16:34:01 -0700754 mutex_unlock(&mem->state_mutex);
755
756 return ret;
757}
758
Yasuaki Ishimatsu6677e3e2013-02-22 16:32:52 -0800759/* return true if the memory block is offlined, otherwise, return false */
760bool is_memblock_offlined(struct memory_block *mem)
761{
762 return mem->state == MEM_OFFLINE;
763}
764
Wen Congyange90bdb72012-10-08 16:34:01 -0700765/*
Dave Hansen3947be12005-10-29 18:16:54 -0700766 * Initialize the sysfs support for memory devices...
767 */
768int __init memory_dev_init(void)
769{
770 unsigned int i;
771 int ret;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800772 int err;
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600773 unsigned long block_sz;
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800774 struct memory_block *mem = NULL;
Dave Hansen3947be12005-10-29 18:16:54 -0700775
Kay Sievers10fbcf42011-12-21 14:48:43 -0800776 ret = subsys_system_register(&memory_subsys, NULL);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800777 if (ret)
778 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700779
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600780 block_sz = get_memory_block_size();
781 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
782
Dave Hansen3947be12005-10-29 18:16:54 -0700783 /*
784 * Create entries for memory sections that were found
785 * during boot and have been initialized
786 */
787 for (i = 0; i < NR_MEM_SECTIONS; i++) {
Andy Whitcroft540557b2007-10-16 01:24:11 -0700788 if (!present_section_nr(i))
Dave Hansen3947be12005-10-29 18:16:54 -0700789 continue;
Yinghai Lu321bf4e2012-01-30 13:57:12 -0800790 /* don't need to reuse memory_block if only one per block */
791 err = add_memory_section(0, __nr_to_section(i),
792 (sections_per_block == 1) ? NULL : &mem,
793 MEM_ONLINE,
Nathan Fontenot0c2c99b2011-01-20 10:43:34 -0600794 BOOT);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800795 if (!ret)
796 ret = err;
Dave Hansen3947be12005-10-29 18:16:54 -0700797 }
798
Andrew Morton28ec24e2006-12-06 20:37:29 -0800799 err = memory_probe_init();
800 if (!ret)
801 ret = err;
Andi Kleenfacb6012009-12-16 12:20:00 +0100802 err = memory_fail_init();
803 if (!ret)
804 ret = err;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800805 err = block_size_init();
806 if (!ret)
807 ret = err;
808out:
809 if (ret)
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800810 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700811 return ret;
812}