xref: /linux/drivers/base/memory.c (revision 52cf25d0ab7f78eeecc59ac652ed5090f69b619e)
1 /*
2  * drivers/base/memory.c - basic Memory class support
3  *
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 
13 #include <linux/sysdev.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/topology.h>
17 #include <linux/capability.h>
18 #include <linux/device.h>
19 #include <linux/memory.h>
20 #include <linux/kobject.h>
21 #include <linux/memory_hotplug.h>
22 #include <linux/mm.h>
23 #include <linux/mutex.h>
24 #include <linux/stat.h>
25 
26 #include <asm/atomic.h>
27 #include <asm/uaccess.h>
28 
29 #define MEMORY_CLASS_NAME	"memory"
30 
31 static struct sysdev_class memory_sysdev_class = {
32 	.name = MEMORY_CLASS_NAME,
33 };
34 
35 static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)
36 {
37 	return MEMORY_CLASS_NAME;
38 }
39 
40 static int memory_uevent(struct kset *kset, struct kobject *obj, struct kobj_uevent_env *env)
41 {
42 	int retval = 0;
43 
44 	return retval;
45 }
46 
47 static const struct kset_uevent_ops memory_uevent_ops = {
48 	.name		= memory_uevent_name,
49 	.uevent		= memory_uevent,
50 };
51 
52 static BLOCKING_NOTIFIER_HEAD(memory_chain);
53 
54 int register_memory_notifier(struct notifier_block *nb)
55 {
56         return blocking_notifier_chain_register(&memory_chain, nb);
57 }
58 EXPORT_SYMBOL(register_memory_notifier);
59 
60 void unregister_memory_notifier(struct notifier_block *nb)
61 {
62         blocking_notifier_chain_unregister(&memory_chain, nb);
63 }
64 EXPORT_SYMBOL(unregister_memory_notifier);
65 
66 static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
67 
68 int register_memory_isolate_notifier(struct notifier_block *nb)
69 {
70 	return atomic_notifier_chain_register(&memory_isolate_chain, nb);
71 }
72 EXPORT_SYMBOL(register_memory_isolate_notifier);
73 
74 void unregister_memory_isolate_notifier(struct notifier_block *nb)
75 {
76 	atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
77 }
78 EXPORT_SYMBOL(unregister_memory_isolate_notifier);
79 
80 /*
81  * register_memory - Setup a sysfs device for a memory block
82  */
83 static
84 int register_memory(struct memory_block *memory, struct mem_section *section)
85 {
86 	int error;
87 
88 	memory->sysdev.cls = &memory_sysdev_class;
89 	memory->sysdev.id = __section_nr(section);
90 
91 	error = sysdev_register(&memory->sysdev);
92 	return error;
93 }
94 
95 static void
96 unregister_memory(struct memory_block *memory, struct mem_section *section)
97 {
98 	BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
99 	BUG_ON(memory->sysdev.id != __section_nr(section));
100 
101 	/* drop the ref. we got in remove_memory_block() */
102 	kobject_put(&memory->sysdev.kobj);
103 	sysdev_unregister(&memory->sysdev);
104 }
105 
106 /*
107  * use this as the physical section index that this memsection
108  * uses.
109  */
110 
111 static ssize_t show_mem_phys_index(struct sys_device *dev,
112 			struct sysdev_attribute *attr, char *buf)
113 {
114 	struct memory_block *mem =
115 		container_of(dev, struct memory_block, sysdev);
116 	return sprintf(buf, "%08lx\n", mem->phys_index);
117 }
118 
119 /*
120  * Show whether the section of memory is likely to be hot-removable
121  */
122 static ssize_t show_mem_removable(struct sys_device *dev,
123 			struct sysdev_attribute *attr, char *buf)
124 {
125 	unsigned long start_pfn;
126 	int ret;
127 	struct memory_block *mem =
128 		container_of(dev, struct memory_block, sysdev);
129 
130 	start_pfn = section_nr_to_pfn(mem->phys_index);
131 	ret = is_mem_section_removable(start_pfn, PAGES_PER_SECTION);
132 	return sprintf(buf, "%d\n", ret);
133 }
134 
135 /*
136  * online, offline, going offline, etc.
137  */
138 static ssize_t show_mem_state(struct sys_device *dev,
139 			struct sysdev_attribute *attr, char *buf)
140 {
141 	struct memory_block *mem =
142 		container_of(dev, struct memory_block, sysdev);
143 	ssize_t len = 0;
144 
145 	/*
146 	 * We can probably put these states in a nice little array
147 	 * so that they're not open-coded
148 	 */
149 	switch (mem->state) {
150 		case MEM_ONLINE:
151 			len = sprintf(buf, "online\n");
152 			break;
153 		case MEM_OFFLINE:
154 			len = sprintf(buf, "offline\n");
155 			break;
156 		case MEM_GOING_OFFLINE:
157 			len = sprintf(buf, "going-offline\n");
158 			break;
159 		default:
160 			len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
161 					mem->state);
162 			WARN_ON(1);
163 			break;
164 	}
165 
166 	return len;
167 }
168 
169 int memory_notify(unsigned long val, void *v)
170 {
171 	return blocking_notifier_call_chain(&memory_chain, val, v);
172 }
173 
174 int memory_isolate_notify(unsigned long val, void *v)
175 {
176 	return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
177 }
178 
179 /*
180  * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
181  * OK to have direct references to sparsemem variables in here.
182  */
183 static int
184 memory_block_action(struct memory_block *mem, unsigned long action)
185 {
186 	int i;
187 	unsigned long psection;
188 	unsigned long start_pfn, start_paddr;
189 	struct page *first_page;
190 	int ret;
191 	int old_state = mem->state;
192 
193 	psection = mem->phys_index;
194 	first_page = pfn_to_page(psection << PFN_SECTION_SHIFT);
195 
196 	/*
197 	 * The probe routines leave the pages reserved, just
198 	 * as the bootmem code does.  Make sure they're still
199 	 * that way.
200 	 */
201 	if (action == MEM_ONLINE) {
202 		for (i = 0; i < PAGES_PER_SECTION; i++) {
203 			if (PageReserved(first_page+i))
204 				continue;
205 
206 			printk(KERN_WARNING "section number %ld page number %d "
207 				"not reserved, was it already online? \n",
208 				psection, i);
209 			return -EBUSY;
210 		}
211 	}
212 
213 	switch (action) {
214 		case MEM_ONLINE:
215 			start_pfn = page_to_pfn(first_page);
216 			ret = online_pages(start_pfn, PAGES_PER_SECTION);
217 			break;
218 		case MEM_OFFLINE:
219 			mem->state = MEM_GOING_OFFLINE;
220 			start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
221 			ret = remove_memory(start_paddr,
222 					    PAGES_PER_SECTION << PAGE_SHIFT);
223 			if (ret) {
224 				mem->state = old_state;
225 				break;
226 			}
227 			break;
228 		default:
229 			WARN(1, KERN_WARNING "%s(%p, %ld) unknown action: %ld\n",
230 					__func__, mem, action, action);
231 			ret = -EINVAL;
232 	}
233 
234 	return ret;
235 }
236 
237 static int memory_block_change_state(struct memory_block *mem,
238 		unsigned long to_state, unsigned long from_state_req)
239 {
240 	int ret = 0;
241 	mutex_lock(&mem->state_mutex);
242 
243 	if (mem->state != from_state_req) {
244 		ret = -EINVAL;
245 		goto out;
246 	}
247 
248 	ret = memory_block_action(mem, to_state);
249 	if (!ret)
250 		mem->state = to_state;
251 
252 out:
253 	mutex_unlock(&mem->state_mutex);
254 	return ret;
255 }
256 
257 static ssize_t
258 store_mem_state(struct sys_device *dev,
259 		struct sysdev_attribute *attr, const char *buf, size_t count)
260 {
261 	struct memory_block *mem;
262 	unsigned int phys_section_nr;
263 	int ret = -EINVAL;
264 
265 	mem = container_of(dev, struct memory_block, sysdev);
266 	phys_section_nr = mem->phys_index;
267 
268 	if (!present_section_nr(phys_section_nr))
269 		goto out;
270 
271 	if (!strncmp(buf, "online", min((int)count, 6)))
272 		ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
273 	else if(!strncmp(buf, "offline", min((int)count, 7)))
274 		ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
275 out:
276 	if (ret)
277 		return ret;
278 	return count;
279 }
280 
281 /*
282  * phys_device is a bad name for this.  What I really want
283  * is a way to differentiate between memory ranges that
284  * are part of physical devices that constitute
285  * a complete removable unit or fru.
286  * i.e. do these ranges belong to the same physical device,
287  * s.t. if I offline all of these sections I can then
288  * remove the physical device?
289  */
290 static ssize_t show_phys_device(struct sys_device *dev,
291 				struct sysdev_attribute *attr, char *buf)
292 {
293 	struct memory_block *mem =
294 		container_of(dev, struct memory_block, sysdev);
295 	return sprintf(buf, "%d\n", mem->phys_device);
296 }
297 
298 static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
299 static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
300 static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
301 static SYSDEV_ATTR(removable, 0444, show_mem_removable, NULL);
302 
303 #define mem_create_simple_file(mem, attr_name)	\
304 	sysdev_create_file(&mem->sysdev, &attr_##attr_name)
305 #define mem_remove_simple_file(mem, attr_name)	\
306 	sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
307 
308 /*
309  * Block size attribute stuff
310  */
311 static ssize_t
312 print_block_size(struct sysdev_class *class, struct sysdev_class_attribute *attr,
313 		 char *buf)
314 {
315 	return sprintf(buf, "%#lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
316 }
317 
318 static SYSDEV_CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
319 
320 static int block_size_init(void)
321 {
322 	return sysfs_create_file(&memory_sysdev_class.kset.kobj,
323 				&attr_block_size_bytes.attr);
324 }
325 
326 /*
327  * Some architectures will have custom drivers to do this, and
328  * will not need to do it from userspace.  The fake hot-add code
329  * as well as ppc64 will do all of their discovery in userspace
330  * and will require this interface.
331  */
332 #ifdef CONFIG_ARCH_MEMORY_PROBE
333 static ssize_t
334 memory_probe_store(struct class *class, struct class_attribute *attr,
335 		   const char *buf, size_t count)
336 {
337 	u64 phys_addr;
338 	int nid;
339 	int ret;
340 
341 	phys_addr = simple_strtoull(buf, NULL, 0);
342 
343 	nid = memory_add_physaddr_to_nid(phys_addr);
344 	ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
345 
346 	if (ret)
347 		count = ret;
348 
349 	return count;
350 }
351 static CLASS_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
352 
353 static int memory_probe_init(void)
354 {
355 	return sysfs_create_file(&memory_sysdev_class.kset.kobj,
356 				&class_attr_probe.attr);
357 }
358 #else
359 static inline int memory_probe_init(void)
360 {
361 	return 0;
362 }
363 #endif
364 
365 #ifdef CONFIG_MEMORY_FAILURE
366 /*
367  * Support for offlining pages of memory
368  */
369 
370 /* Soft offline a page */
371 static ssize_t
372 store_soft_offline_page(struct class *class,
373 			struct class_attribute *attr,
374 			const char *buf, size_t count)
375 {
376 	int ret;
377 	u64 pfn;
378 	if (!capable(CAP_SYS_ADMIN))
379 		return -EPERM;
380 	if (strict_strtoull(buf, 0, &pfn) < 0)
381 		return -EINVAL;
382 	pfn >>= PAGE_SHIFT;
383 	if (!pfn_valid(pfn))
384 		return -ENXIO;
385 	ret = soft_offline_page(pfn_to_page(pfn), 0);
386 	return ret == 0 ? count : ret;
387 }
388 
389 /* Forcibly offline a page, including killing processes. */
390 static ssize_t
391 store_hard_offline_page(struct class *class,
392 			struct class_attribute *attr,
393 			const char *buf, size_t count)
394 {
395 	int ret;
396 	u64 pfn;
397 	if (!capable(CAP_SYS_ADMIN))
398 		return -EPERM;
399 	if (strict_strtoull(buf, 0, &pfn) < 0)
400 		return -EINVAL;
401 	pfn >>= PAGE_SHIFT;
402 	ret = __memory_failure(pfn, 0, 0);
403 	return ret ? ret : count;
404 }
405 
406 static CLASS_ATTR(soft_offline_page, 0644, NULL, store_soft_offline_page);
407 static CLASS_ATTR(hard_offline_page, 0644, NULL, store_hard_offline_page);
408 
409 static __init int memory_fail_init(void)
410 {
411 	int err;
412 
413 	err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
414 				&class_attr_soft_offline_page.attr);
415 	if (!err)
416 		err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
417 				&class_attr_hard_offline_page.attr);
418 	return err;
419 }
420 #else
421 static inline int memory_fail_init(void)
422 {
423 	return 0;
424 }
425 #endif
426 
427 /*
428  * Note that phys_device is optional.  It is here to allow for
429  * differentiation between which *physical* devices each
430  * section belongs to...
431  */
432 
433 static int add_memory_block(int nid, struct mem_section *section,
434 			unsigned long state, int phys_device,
435 			enum mem_add_context context)
436 {
437 	struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
438 	int ret = 0;
439 
440 	if (!mem)
441 		return -ENOMEM;
442 
443 	mem->phys_index = __section_nr(section);
444 	mem->state = state;
445 	mutex_init(&mem->state_mutex);
446 	mem->phys_device = phys_device;
447 
448 	ret = register_memory(mem, section);
449 	if (!ret)
450 		ret = mem_create_simple_file(mem, phys_index);
451 	if (!ret)
452 		ret = mem_create_simple_file(mem, state);
453 	if (!ret)
454 		ret = mem_create_simple_file(mem, phys_device);
455 	if (!ret)
456 		ret = mem_create_simple_file(mem, removable);
457 	if (!ret) {
458 		if (context == HOTPLUG)
459 			ret = register_mem_sect_under_node(mem, nid);
460 	}
461 
462 	return ret;
463 }
464 
465 /*
466  * For now, we have a linear search to go find the appropriate
467  * memory_block corresponding to a particular phys_index. If
468  * this gets to be a real problem, we can always use a radix
469  * tree or something here.
470  *
471  * This could be made generic for all sysdev classes.
472  */
473 struct memory_block *find_memory_block(struct mem_section *section)
474 {
475 	struct kobject *kobj;
476 	struct sys_device *sysdev;
477 	struct memory_block *mem;
478 	char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
479 
480 	/*
481 	 * This only works because we know that section == sysdev->id
482 	 * slightly redundant with sysdev_register()
483 	 */
484 	sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
485 
486 	kobj = kset_find_obj(&memory_sysdev_class.kset, name);
487 	if (!kobj)
488 		return NULL;
489 
490 	sysdev = container_of(kobj, struct sys_device, kobj);
491 	mem = container_of(sysdev, struct memory_block, sysdev);
492 
493 	return mem;
494 }
495 
496 int remove_memory_block(unsigned long node_id, struct mem_section *section,
497 		int phys_device)
498 {
499 	struct memory_block *mem;
500 
501 	mem = find_memory_block(section);
502 	unregister_mem_sect_under_nodes(mem);
503 	mem_remove_simple_file(mem, phys_index);
504 	mem_remove_simple_file(mem, state);
505 	mem_remove_simple_file(mem, phys_device);
506 	mem_remove_simple_file(mem, removable);
507 	unregister_memory(mem, section);
508 
509 	return 0;
510 }
511 
512 /*
513  * need an interface for the VM to add new memory regions,
514  * but without onlining it.
515  */
516 int register_new_memory(int nid, struct mem_section *section)
517 {
518 	return add_memory_block(nid, section, MEM_OFFLINE, 0, HOTPLUG);
519 }
520 
521 int unregister_memory_section(struct mem_section *section)
522 {
523 	if (!present_section(section))
524 		return -EINVAL;
525 
526 	return remove_memory_block(0, section, 0);
527 }
528 
529 /*
530  * Initialize the sysfs support for memory devices...
531  */
532 int __init memory_dev_init(void)
533 {
534 	unsigned int i;
535 	int ret;
536 	int err;
537 
538 	memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
539 	ret = sysdev_class_register(&memory_sysdev_class);
540 	if (ret)
541 		goto out;
542 
543 	/*
544 	 * Create entries for memory sections that were found
545 	 * during boot and have been initialized
546 	 */
547 	for (i = 0; i < NR_MEM_SECTIONS; i++) {
548 		if (!present_section_nr(i))
549 			continue;
550 		err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE,
551 					0, BOOT);
552 		if (!ret)
553 			ret = err;
554 	}
555 
556 	err = memory_probe_init();
557 	if (!ret)
558 		ret = err;
559 	err = memory_fail_init();
560 	if (!ret)
561 		ret = err;
562 	err = block_size_init();
563 	if (!ret)
564 		ret = err;
565 out:
566 	if (ret)
567 		printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
568 	return ret;
569 }
570