memory.c 11 KB

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