memory.c 14 KB

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