memory.c 16 KB

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