memory.c 11 KB

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