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