memory.c 13 KB

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