memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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 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 class *class, char *buf)
  267. {
  268. return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
  269. }
  270. static CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
  271. static int block_size_init(void)
  272. {
  273. return sysfs_create_file(&memory_sysdev_class.kset.kobj,
  274. &class_attr_block_size_bytes.attr);
  275. }
  276. /*
  277. * Some architectures will have custom drivers to do this, and
  278. * will not need to do it from userspace. The fake hot-add code
  279. * as well as ppc64 will do all of their discovery in userspace
  280. * and will require this interface.
  281. */
  282. #ifdef CONFIG_ARCH_MEMORY_PROBE
  283. static ssize_t
  284. memory_probe_store(struct class *class, const char *buf, size_t count)
  285. {
  286. u64 phys_addr;
  287. int nid;
  288. int ret;
  289. phys_addr = simple_strtoull(buf, NULL, 0);
  290. nid = memory_add_physaddr_to_nid(phys_addr);
  291. ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
  292. if (ret)
  293. count = ret;
  294. return count;
  295. }
  296. static CLASS_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
  297. static int memory_probe_init(void)
  298. {
  299. return sysfs_create_file(&memory_sysdev_class.kset.kobj,
  300. &class_attr_probe.attr);
  301. }
  302. #else
  303. static inline int memory_probe_init(void)
  304. {
  305. return 0;
  306. }
  307. #endif
  308. #ifdef CONFIG_MEMORY_FAILURE
  309. /*
  310. * Support for offlining pages of memory
  311. */
  312. /* Soft offline a page */
  313. static ssize_t
  314. store_soft_offline_page(struct class *class, const char *buf, size_t count)
  315. {
  316. int ret;
  317. u64 pfn;
  318. if (!capable(CAP_SYS_ADMIN))
  319. return -EPERM;
  320. if (strict_strtoull(buf, 0, &pfn) < 0)
  321. return -EINVAL;
  322. pfn >>= PAGE_SHIFT;
  323. if (!pfn_valid(pfn))
  324. return -ENXIO;
  325. ret = soft_offline_page(pfn_to_page(pfn), 0);
  326. return ret == 0 ? count : ret;
  327. }
  328. /* Forcibly offline a page, including killing processes. */
  329. static ssize_t
  330. store_hard_offline_page(struct class *class, const char *buf, size_t count)
  331. {
  332. int ret;
  333. u64 pfn;
  334. if (!capable(CAP_SYS_ADMIN))
  335. return -EPERM;
  336. if (strict_strtoull(buf, 0, &pfn) < 0)
  337. return -EINVAL;
  338. pfn >>= PAGE_SHIFT;
  339. ret = __memory_failure(pfn, 0, 0);
  340. return ret ? ret : count;
  341. }
  342. static CLASS_ATTR(soft_offline_page, 0644, NULL, store_soft_offline_page);
  343. static CLASS_ATTR(hard_offline_page, 0644, NULL, store_hard_offline_page);
  344. static __init int memory_fail_init(void)
  345. {
  346. int err;
  347. err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
  348. &class_attr_soft_offline_page.attr);
  349. if (!err)
  350. err = sysfs_create_file(&memory_sysdev_class.kset.kobj,
  351. &class_attr_hard_offline_page.attr);
  352. return err;
  353. }
  354. #else
  355. static inline int memory_fail_init(void)
  356. {
  357. return 0;
  358. }
  359. #endif
  360. /*
  361. * Note that phys_device is optional. It is here to allow for
  362. * differentiation between which *physical* devices each
  363. * section belongs to...
  364. */
  365. static int add_memory_block(int nid, struct mem_section *section,
  366. unsigned long state, int phys_device,
  367. enum mem_add_context context)
  368. {
  369. struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  370. int ret = 0;
  371. if (!mem)
  372. return -ENOMEM;
  373. mem->phys_index = __section_nr(section);
  374. mem->state = state;
  375. mutex_init(&mem->state_mutex);
  376. mem->phys_device = phys_device;
  377. ret = register_memory(mem, section);
  378. if (!ret)
  379. ret = mem_create_simple_file(mem, phys_index);
  380. if (!ret)
  381. ret = mem_create_simple_file(mem, state);
  382. if (!ret)
  383. ret = mem_create_simple_file(mem, phys_device);
  384. if (!ret)
  385. ret = mem_create_simple_file(mem, removable);
  386. if (!ret) {
  387. if (context == HOTPLUG)
  388. ret = register_mem_sect_under_node(mem, nid);
  389. }
  390. return ret;
  391. }
  392. /*
  393. * For now, we have a linear search to go find the appropriate
  394. * memory_block corresponding to a particular phys_index. If
  395. * this gets to be a real problem, we can always use a radix
  396. * tree or something here.
  397. *
  398. * This could be made generic for all sysdev classes.
  399. */
  400. struct memory_block *find_memory_block(struct mem_section *section)
  401. {
  402. struct kobject *kobj;
  403. struct sys_device *sysdev;
  404. struct memory_block *mem;
  405. char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
  406. /*
  407. * This only works because we know that section == sysdev->id
  408. * slightly redundant with sysdev_register()
  409. */
  410. sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
  411. kobj = kset_find_obj(&memory_sysdev_class.kset, name);
  412. if (!kobj)
  413. return NULL;
  414. sysdev = container_of(kobj, struct sys_device, kobj);
  415. mem = container_of(sysdev, struct memory_block, sysdev);
  416. return mem;
  417. }
  418. int remove_memory_block(unsigned long node_id, struct mem_section *section,
  419. int phys_device)
  420. {
  421. struct memory_block *mem;
  422. mem = find_memory_block(section);
  423. unregister_mem_sect_under_nodes(mem);
  424. mem_remove_simple_file(mem, phys_index);
  425. mem_remove_simple_file(mem, state);
  426. mem_remove_simple_file(mem, phys_device);
  427. mem_remove_simple_file(mem, removable);
  428. unregister_memory(mem, section);
  429. return 0;
  430. }
  431. /*
  432. * need an interface for the VM to add new memory regions,
  433. * but without onlining it.
  434. */
  435. int register_new_memory(int nid, struct mem_section *section)
  436. {
  437. return add_memory_block(nid, section, MEM_OFFLINE, 0, HOTPLUG);
  438. }
  439. int unregister_memory_section(struct mem_section *section)
  440. {
  441. if (!present_section(section))
  442. return -EINVAL;
  443. return remove_memory_block(0, section, 0);
  444. }
  445. /*
  446. * Initialize the sysfs support for memory devices...
  447. */
  448. int __init memory_dev_init(void)
  449. {
  450. unsigned int i;
  451. int ret;
  452. int err;
  453. memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
  454. ret = sysdev_class_register(&memory_sysdev_class);
  455. if (ret)
  456. goto out;
  457. /*
  458. * Create entries for memory sections that were found
  459. * during boot and have been initialized
  460. */
  461. for (i = 0; i < NR_MEM_SECTIONS; i++) {
  462. if (!present_section_nr(i))
  463. continue;
  464. err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE,
  465. 0, BOOT);
  466. if (!ret)
  467. ret = err;
  468. }
  469. err = memory_probe_init();
  470. if (!ret)
  471. ret = err;
  472. err = memory_fail_init();
  473. if (!ret)
  474. ret = err;
  475. err = block_size_init();
  476. if (!ret)
  477. ret = err;
  478. out:
  479. if (ret)
  480. printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
  481. return ret;
  482. }