memory.c 16 KB

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