memory.c 14 KB

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