memory.c 11 KB

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