memory.c 16 KB

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