memory.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 <asm/atomic.h>
  23. #include <asm/uaccess.h>
  24. #define MEMORY_CLASS_NAME "memory"
  25. static struct sysdev_class memory_sysdev_class = {
  26. .name = MEMORY_CLASS_NAME,
  27. };
  28. static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)
  29. {
  30. return MEMORY_CLASS_NAME;
  31. }
  32. static int memory_uevent(struct kset *kset, struct kobject *obj, struct kobj_uevent_env *env)
  33. {
  34. int retval = 0;
  35. return retval;
  36. }
  37. static struct kset_uevent_ops memory_uevent_ops = {
  38. .name = memory_uevent_name,
  39. .uevent = memory_uevent,
  40. };
  41. static BLOCKING_NOTIFIER_HEAD(memory_chain);
  42. int register_memory_notifier(struct notifier_block *nb)
  43. {
  44. return blocking_notifier_chain_register(&memory_chain, nb);
  45. }
  46. void unregister_memory_notifier(struct notifier_block *nb)
  47. {
  48. blocking_notifier_chain_unregister(&memory_chain, nb);
  49. }
  50. /*
  51. * register_memory - Setup a sysfs device for a memory block
  52. */
  53. int register_memory(struct memory_block *memory, struct mem_section *section,
  54. struct node *root)
  55. {
  56. int error;
  57. memory->sysdev.cls = &memory_sysdev_class;
  58. memory->sysdev.id = __section_nr(section);
  59. error = sysdev_register(&memory->sysdev);
  60. if (root && !error)
  61. error = sysfs_create_link(&root->sysdev.kobj,
  62. &memory->sysdev.kobj,
  63. kobject_name(&memory->sysdev.kobj));
  64. return error;
  65. }
  66. static void
  67. unregister_memory(struct memory_block *memory, struct mem_section *section,
  68. struct node *root)
  69. {
  70. BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
  71. BUG_ON(memory->sysdev.id != __section_nr(section));
  72. sysdev_unregister(&memory->sysdev);
  73. if (root)
  74. sysfs_remove_link(&root->sysdev.kobj,
  75. kobject_name(&memory->sysdev.kobj));
  76. }
  77. /*
  78. * use this as the physical section index that this memsection
  79. * uses.
  80. */
  81. static ssize_t show_mem_phys_index(struct sys_device *dev, char *buf)
  82. {
  83. struct memory_block *mem =
  84. container_of(dev, struct memory_block, sysdev);
  85. return sprintf(buf, "%08lx\n", mem->phys_index);
  86. }
  87. /*
  88. * online, offline, going offline, etc.
  89. */
  90. static ssize_t show_mem_state(struct sys_device *dev, char *buf)
  91. {
  92. struct memory_block *mem =
  93. container_of(dev, struct memory_block, sysdev);
  94. ssize_t len = 0;
  95. /*
  96. * We can probably put these states in a nice little array
  97. * so that they're not open-coded
  98. */
  99. switch (mem->state) {
  100. case MEM_ONLINE:
  101. len = sprintf(buf, "online\n");
  102. break;
  103. case MEM_OFFLINE:
  104. len = sprintf(buf, "offline\n");
  105. break;
  106. case MEM_GOING_OFFLINE:
  107. len = sprintf(buf, "going-offline\n");
  108. break;
  109. default:
  110. len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
  111. mem->state);
  112. WARN_ON(1);
  113. break;
  114. }
  115. return len;
  116. }
  117. int memory_notify(unsigned long val, void *v)
  118. {
  119. return blocking_notifier_call_chain(&memory_chain, val, v);
  120. }
  121. /*
  122. * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
  123. * OK to have direct references to sparsemem variables in here.
  124. */
  125. static int
  126. memory_block_action(struct memory_block *mem, unsigned long action)
  127. {
  128. int i;
  129. unsigned long psection;
  130. unsigned long start_pfn, start_paddr;
  131. struct page *first_page;
  132. int ret;
  133. int old_state = mem->state;
  134. psection = mem->phys_index;
  135. first_page = pfn_to_page(psection << PFN_SECTION_SHIFT);
  136. /*
  137. * The probe routines leave the pages reserved, just
  138. * as the bootmem code does. Make sure they're still
  139. * that way.
  140. */
  141. if (action == MEM_ONLINE) {
  142. for (i = 0; i < PAGES_PER_SECTION; i++) {
  143. if (PageReserved(first_page+i))
  144. continue;
  145. printk(KERN_WARNING "section number %ld page number %d "
  146. "not reserved, was it already online? \n",
  147. psection, i);
  148. return -EBUSY;
  149. }
  150. }
  151. switch (action) {
  152. case MEM_ONLINE:
  153. start_pfn = page_to_pfn(first_page);
  154. ret = online_pages(start_pfn, PAGES_PER_SECTION);
  155. break;
  156. case MEM_OFFLINE:
  157. mem->state = MEM_GOING_OFFLINE;
  158. start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
  159. ret = remove_memory(start_paddr,
  160. PAGES_PER_SECTION << PAGE_SHIFT);
  161. if (ret) {
  162. mem->state = old_state;
  163. break;
  164. }
  165. break;
  166. default:
  167. printk(KERN_WARNING "%s(%p, %ld) unknown action: %ld\n",
  168. __FUNCTION__, mem, action, action);
  169. WARN_ON(1);
  170. ret = -EINVAL;
  171. }
  172. return ret;
  173. }
  174. static int memory_block_change_state(struct memory_block *mem,
  175. unsigned long to_state, unsigned long from_state_req)
  176. {
  177. int ret = 0;
  178. down(&mem->state_sem);
  179. if (mem->state != from_state_req) {
  180. ret = -EINVAL;
  181. goto out;
  182. }
  183. ret = memory_block_action(mem, to_state);
  184. if (!ret)
  185. mem->state = to_state;
  186. out:
  187. up(&mem->state_sem);
  188. return ret;
  189. }
  190. static ssize_t
  191. store_mem_state(struct sys_device *dev, const char *buf, size_t count)
  192. {
  193. struct memory_block *mem;
  194. unsigned int phys_section_nr;
  195. int ret = -EINVAL;
  196. mem = container_of(dev, struct memory_block, sysdev);
  197. phys_section_nr = mem->phys_index;
  198. if (!present_section_nr(phys_section_nr))
  199. goto out;
  200. if (!strncmp(buf, "online", min((int)count, 6)))
  201. ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
  202. else if(!strncmp(buf, "offline", min((int)count, 7)))
  203. ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
  204. out:
  205. if (ret)
  206. return ret;
  207. return count;
  208. }
  209. /*
  210. * phys_device is a bad name for this. What I really want
  211. * is a way to differentiate between memory ranges that
  212. * are part of physical devices that constitute
  213. * a complete removable unit or fru.
  214. * i.e. do these ranges belong to the same physical device,
  215. * s.t. if I offline all of these sections I can then
  216. * remove the physical device?
  217. */
  218. static ssize_t show_phys_device(struct sys_device *dev, char *buf)
  219. {
  220. struct memory_block *mem =
  221. container_of(dev, struct memory_block, sysdev);
  222. return sprintf(buf, "%d\n", mem->phys_device);
  223. }
  224. static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
  225. static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
  226. static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
  227. #define mem_create_simple_file(mem, attr_name) \
  228. sysdev_create_file(&mem->sysdev, &attr_##attr_name)
  229. #define mem_remove_simple_file(mem, attr_name) \
  230. sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
  231. /*
  232. * Block size attribute stuff
  233. */
  234. static ssize_t
  235. print_block_size(struct class *class, char *buf)
  236. {
  237. return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
  238. }
  239. static CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
  240. static int block_size_init(void)
  241. {
  242. return sysfs_create_file(&memory_sysdev_class.kset.kobj,
  243. &class_attr_block_size_bytes.attr);
  244. }
  245. /*
  246. * Some architectures will have custom drivers to do this, and
  247. * will not need to do it from userspace. The fake hot-add code
  248. * as well as ppc64 will do all of their discovery in userspace
  249. * and will require this interface.
  250. */
  251. #ifdef CONFIG_ARCH_MEMORY_PROBE
  252. static ssize_t
  253. memory_probe_store(struct class *class, const char *buf, size_t count)
  254. {
  255. u64 phys_addr;
  256. int nid;
  257. int ret;
  258. phys_addr = simple_strtoull(buf, NULL, 0);
  259. nid = memory_add_physaddr_to_nid(phys_addr);
  260. ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
  261. if (ret)
  262. count = ret;
  263. return count;
  264. }
  265. static CLASS_ATTR(probe, 0700, NULL, memory_probe_store);
  266. static int memory_probe_init(void)
  267. {
  268. return sysfs_create_file(&memory_sysdev_class.kset.kobj,
  269. &class_attr_probe.attr);
  270. }
  271. #else
  272. static inline int memory_probe_init(void)
  273. {
  274. return 0;
  275. }
  276. #endif
  277. /*
  278. * Note that phys_device is optional. It is here to allow for
  279. * differentiation between which *physical* devices each
  280. * section belongs to...
  281. */
  282. static int add_memory_block(unsigned long node_id, struct mem_section *section,
  283. unsigned long state, int phys_device)
  284. {
  285. struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  286. int ret = 0;
  287. if (!mem)
  288. return -ENOMEM;
  289. mem->phys_index = __section_nr(section);
  290. mem->state = state;
  291. init_MUTEX(&mem->state_sem);
  292. mem->phys_device = phys_device;
  293. ret = register_memory(mem, section, NULL);
  294. if (!ret)
  295. ret = mem_create_simple_file(mem, phys_index);
  296. if (!ret)
  297. ret = mem_create_simple_file(mem, state);
  298. if (!ret)
  299. ret = mem_create_simple_file(mem, phys_device);
  300. return ret;
  301. }
  302. /*
  303. * For now, we have a linear search to go find the appropriate
  304. * memory_block corresponding to a particular phys_index. If
  305. * this gets to be a real problem, we can always use a radix
  306. * tree or something here.
  307. *
  308. * This could be made generic for all sysdev classes.
  309. */
  310. static struct memory_block *find_memory_block(struct mem_section *section)
  311. {
  312. struct kobject *kobj;
  313. struct sys_device *sysdev;
  314. struct memory_block *mem;
  315. char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
  316. /*
  317. * This only works because we know that section == sysdev->id
  318. * slightly redundant with sysdev_register()
  319. */
  320. sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
  321. kobj = kset_find_obj(&memory_sysdev_class.kset, name);
  322. if (!kobj)
  323. return NULL;
  324. sysdev = container_of(kobj, struct sys_device, kobj);
  325. mem = container_of(sysdev, struct memory_block, sysdev);
  326. return mem;
  327. }
  328. int remove_memory_block(unsigned long node_id, struct mem_section *section,
  329. int phys_device)
  330. {
  331. struct memory_block *mem;
  332. mem = find_memory_block(section);
  333. mem_remove_simple_file(mem, phys_index);
  334. mem_remove_simple_file(mem, state);
  335. mem_remove_simple_file(mem, phys_device);
  336. unregister_memory(mem, section, NULL);
  337. return 0;
  338. }
  339. /*
  340. * need an interface for the VM to add new memory regions,
  341. * but without onlining it.
  342. */
  343. int register_new_memory(struct mem_section *section)
  344. {
  345. return add_memory_block(0, section, MEM_OFFLINE, 0);
  346. }
  347. int unregister_memory_section(struct mem_section *section)
  348. {
  349. if (!present_section(section))
  350. return -EINVAL;
  351. return remove_memory_block(0, section, 0);
  352. }
  353. /*
  354. * Initialize the sysfs support for memory devices...
  355. */
  356. int __init memory_dev_init(void)
  357. {
  358. unsigned int i;
  359. int ret;
  360. int err;
  361. memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
  362. ret = sysdev_class_register(&memory_sysdev_class);
  363. if (ret)
  364. goto out;
  365. /*
  366. * Create entries for memory sections that were found
  367. * during boot and have been initialized
  368. */
  369. for (i = 0; i < NR_MEM_SECTIONS; i++) {
  370. if (!present_section_nr(i))
  371. continue;
  372. err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE, 0);
  373. if (!ret)
  374. ret = err;
  375. }
  376. err = memory_probe_init();
  377. if (!ret)
  378. ret = err;
  379. err = block_size_init();
  380. if (!ret)
  381. ret = err;
  382. out:
  383. if (ret)
  384. printk(KERN_ERR "%s() failed: %d\n", __FUNCTION__, ret);
  385. return ret;
  386. }