memory.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * Memory subsystem 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/module.h>
  13. #include <linux/init.h>
  14. #include <linux/topology.h>
  15. #include <linux/capability.h>
  16. #include <linux/device.h>
  17. #include <linux/memory.h>
  18. #include <linux/memory_hotplug.h>
  19. #include <linux/mm.h>
  20. #include <linux/mutex.h>
  21. #include <linux/stat.h>
  22. #include <linux/slab.h>
  23. #include <linux/atomic.h>
  24. #include <asm/uaccess.h>
  25. static DEFINE_MUTEX(mem_sysfs_mutex);
  26. #define MEMORY_CLASS_NAME "memory"
  27. static int sections_per_block;
  28. static inline int base_memory_block_id(int section_nr)
  29. {
  30. return section_nr / sections_per_block;
  31. }
  32. static int memory_subsys_online(struct device *dev);
  33. static int memory_subsys_offline(struct device *dev);
  34. static struct bus_type memory_subsys = {
  35. .name = MEMORY_CLASS_NAME,
  36. .dev_name = MEMORY_CLASS_NAME,
  37. .online = memory_subsys_online,
  38. .offline = memory_subsys_offline,
  39. };
  40. static BLOCKING_NOTIFIER_HEAD(memory_chain);
  41. int register_memory_notifier(struct notifier_block *nb)
  42. {
  43. return blocking_notifier_chain_register(&memory_chain, nb);
  44. }
  45. EXPORT_SYMBOL(register_memory_notifier);
  46. void unregister_memory_notifier(struct notifier_block *nb)
  47. {
  48. blocking_notifier_chain_unregister(&memory_chain, nb);
  49. }
  50. EXPORT_SYMBOL(unregister_memory_notifier);
  51. static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
  52. int register_memory_isolate_notifier(struct notifier_block *nb)
  53. {
  54. return atomic_notifier_chain_register(&memory_isolate_chain, nb);
  55. }
  56. EXPORT_SYMBOL(register_memory_isolate_notifier);
  57. void unregister_memory_isolate_notifier(struct notifier_block *nb)
  58. {
  59. atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
  60. }
  61. EXPORT_SYMBOL(unregister_memory_isolate_notifier);
  62. static void memory_block_release(struct device *dev)
  63. {
  64. struct memory_block *mem = container_of(dev, struct memory_block, dev);
  65. kfree(mem);
  66. }
  67. unsigned long __weak memory_block_size_bytes(void)
  68. {
  69. return MIN_MEMORY_BLOCK_SIZE;
  70. }
  71. static unsigned long get_memory_block_size(void)
  72. {
  73. unsigned long block_sz;
  74. block_sz = memory_block_size_bytes();
  75. /* Validate blk_sz is a power of 2 and not less than section size */
  76. if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
  77. WARN_ON(1);
  78. block_sz = MIN_MEMORY_BLOCK_SIZE;
  79. }
  80. return block_sz;
  81. }
  82. /*
  83. * use this as the physical section index that this memsection
  84. * uses.
  85. */
  86. static ssize_t show_mem_start_phys_index(struct device *dev,
  87. struct device_attribute *attr, char *buf)
  88. {
  89. struct memory_block *mem =
  90. container_of(dev, struct memory_block, dev);
  91. unsigned long phys_index;
  92. phys_index = mem->start_section_nr / sections_per_block;
  93. return sprintf(buf, "%08lx\n", phys_index);
  94. }
  95. static ssize_t show_mem_end_phys_index(struct device *dev,
  96. struct device_attribute *attr, char *buf)
  97. {
  98. struct memory_block *mem =
  99. container_of(dev, struct memory_block, dev);
  100. unsigned long phys_index;
  101. phys_index = mem->end_section_nr / sections_per_block;
  102. return sprintf(buf, "%08lx\n", phys_index);
  103. }
  104. /*
  105. * Show whether the section of memory is likely to be hot-removable
  106. */
  107. static ssize_t show_mem_removable(struct device *dev,
  108. struct device_attribute *attr, char *buf)
  109. {
  110. unsigned long i, pfn;
  111. int ret = 1;
  112. struct memory_block *mem =
  113. container_of(dev, struct memory_block, dev);
  114. for (i = 0; i < sections_per_block; i++) {
  115. pfn = section_nr_to_pfn(mem->start_section_nr + i);
  116. ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
  117. }
  118. return sprintf(buf, "%d\n", ret);
  119. }
  120. /*
  121. * online, offline, going offline, etc.
  122. */
  123. static ssize_t show_mem_state(struct device *dev,
  124. struct device_attribute *attr, char *buf)
  125. {
  126. struct memory_block *mem =
  127. container_of(dev, struct memory_block, dev);
  128. ssize_t len = 0;
  129. /*
  130. * We can probably put these states in a nice little array
  131. * so that they're not open-coded
  132. */
  133. switch (mem->state) {
  134. case MEM_ONLINE:
  135. len = sprintf(buf, "online\n");
  136. break;
  137. case MEM_OFFLINE:
  138. len = sprintf(buf, "offline\n");
  139. break;
  140. case MEM_GOING_OFFLINE:
  141. len = sprintf(buf, "going-offline\n");
  142. break;
  143. default:
  144. len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
  145. mem->state);
  146. WARN_ON(1);
  147. break;
  148. }
  149. return len;
  150. }
  151. int memory_notify(unsigned long val, void *v)
  152. {
  153. return blocking_notifier_call_chain(&memory_chain, val, v);
  154. }
  155. int memory_isolate_notify(unsigned long val, void *v)
  156. {
  157. return atomic_notifier_call_chain(&memory_isolate_chain, val, v);
  158. }
  159. /*
  160. * The probe routines leave the pages reserved, just as the bootmem code does.
  161. * Make sure they're still that way.
  162. */
  163. static bool pages_correctly_reserved(unsigned long start_pfn)
  164. {
  165. int i, j;
  166. struct page *page;
  167. unsigned long pfn = start_pfn;
  168. /*
  169. * memmap between sections is not contiguous except with
  170. * SPARSEMEM_VMEMMAP. We lookup the page once per section
  171. * and assume memmap is contiguous within each section
  172. */
  173. for (i = 0; i < sections_per_block; i++, pfn += PAGES_PER_SECTION) {
  174. if (WARN_ON_ONCE(!pfn_valid(pfn)))
  175. return false;
  176. page = pfn_to_page(pfn);
  177. for (j = 0; j < PAGES_PER_SECTION; j++) {
  178. if (PageReserved(page + j))
  179. continue;
  180. printk(KERN_WARNING "section number %ld page number %d "
  181. "not reserved, was it already online?\n",
  182. pfn_to_section_nr(pfn), j);
  183. return false;
  184. }
  185. }
  186. return true;
  187. }
  188. /*
  189. * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
  190. * OK to have direct references to sparsemem variables in here.
  191. */
  192. static int
  193. memory_block_action(unsigned long phys_index, unsigned long action, int online_type)
  194. {
  195. unsigned long start_pfn;
  196. unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
  197. struct page *first_page;
  198. int ret;
  199. first_page = pfn_to_page(phys_index << PFN_SECTION_SHIFT);
  200. start_pfn = page_to_pfn(first_page);
  201. switch (action) {
  202. case MEM_ONLINE:
  203. if (!pages_correctly_reserved(start_pfn))
  204. return -EBUSY;
  205. ret = online_pages(start_pfn, nr_pages, online_type);
  206. break;
  207. case MEM_OFFLINE:
  208. ret = offline_pages(start_pfn, nr_pages);
  209. break;
  210. default:
  211. WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
  212. "%ld\n", __func__, phys_index, action, action);
  213. ret = -EINVAL;
  214. }
  215. return ret;
  216. }
  217. static int memory_block_change_state(struct memory_block *mem,
  218. unsigned long to_state, unsigned long from_state_req)
  219. {
  220. int ret = 0;
  221. if (mem->state != from_state_req)
  222. return -EINVAL;
  223. if (to_state == MEM_OFFLINE)
  224. mem->state = MEM_GOING_OFFLINE;
  225. ret = memory_block_action(mem->start_section_nr, to_state,
  226. mem->online_type);
  227. mem->state = ret ? from_state_req : to_state;
  228. return ret;
  229. }
  230. /* The device lock serializes operations on memory_subsys_[online|offline] */
  231. static int memory_subsys_online(struct device *dev)
  232. {
  233. struct memory_block *mem = container_of(dev, struct memory_block, dev);
  234. int ret;
  235. if (mem->state == MEM_ONLINE)
  236. return 0;
  237. /*
  238. * If we are called from store_mem_state(), online_type will be
  239. * set >= 0 Otherwise we were called from the device online
  240. * attribute and need to set the online_type.
  241. */
  242. if (mem->online_type < 0)
  243. mem->online_type = ONLINE_KEEP;
  244. ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
  245. /* clear online_type */
  246. mem->online_type = -1;
  247. return ret;
  248. }
  249. static int memory_subsys_offline(struct device *dev)
  250. {
  251. struct memory_block *mem = container_of(dev, struct memory_block, dev);
  252. if (mem->state == MEM_OFFLINE)
  253. return 0;
  254. return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
  255. }
  256. static ssize_t
  257. store_mem_state(struct device *dev,
  258. struct device_attribute *attr, const char *buf, size_t count)
  259. {
  260. struct memory_block *mem;
  261. int ret, online_type;
  262. mem = container_of(dev, struct memory_block, dev);
  263. lock_device_hotplug();
  264. if (!strncmp(buf, "online_kernel", min_t(int, count, 13)))
  265. online_type = ONLINE_KERNEL;
  266. else if (!strncmp(buf, "online_movable", min_t(int, count, 14)))
  267. online_type = ONLINE_MOVABLE;
  268. else if (!strncmp(buf, "online", min_t(int, count, 6)))
  269. online_type = ONLINE_KEEP;
  270. else if (!strncmp(buf, "offline", min_t(int, count, 7)))
  271. online_type = -1;
  272. else
  273. return -EINVAL;
  274. switch (online_type) {
  275. case ONLINE_KERNEL:
  276. case ONLINE_MOVABLE:
  277. case ONLINE_KEEP:
  278. /*
  279. * mem->online_type is not protected so there can be a
  280. * race here. However, when racing online, the first
  281. * will succeed and the second will just return as the
  282. * block will already be online. The online type
  283. * could be either one, but that is expected.
  284. */
  285. mem->online_type = online_type;
  286. ret = device_online(&mem->dev);
  287. break;
  288. case -1:
  289. ret = device_offline(&mem->dev);
  290. break;
  291. default:
  292. ret = -EINVAL; /* should never happen */
  293. }
  294. unlock_device_hotplug();
  295. if (ret)
  296. return ret;
  297. return count;
  298. }
  299. /*
  300. * phys_device is a bad name for this. What I really want
  301. * is a way to differentiate between memory ranges that
  302. * are part of physical devices that constitute
  303. * a complete removable unit or fru.
  304. * i.e. do these ranges belong to the same physical device,
  305. * s.t. if I offline all of these sections I can then
  306. * remove the physical device?
  307. */
  308. static ssize_t show_phys_device(struct device *dev,
  309. struct device_attribute *attr, char *buf)
  310. {
  311. struct memory_block *mem =
  312. container_of(dev, struct memory_block, dev);
  313. return sprintf(buf, "%d\n", mem->phys_device);
  314. }
  315. static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
  316. static DEVICE_ATTR(end_phys_index, 0444, show_mem_end_phys_index, NULL);
  317. static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state);
  318. static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL);
  319. static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL);
  320. /*
  321. * Block size attribute stuff
  322. */
  323. static ssize_t
  324. print_block_size(struct device *dev, struct device_attribute *attr,
  325. char *buf)
  326. {
  327. return sprintf(buf, "%lx\n", get_memory_block_size());
  328. }
  329. static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL);
  330. /*
  331. * Some architectures will have custom drivers to do this, and
  332. * will not need to do it from userspace. The fake hot-add code
  333. * as well as ppc64 will do all of their discovery in userspace
  334. * and will require this interface.
  335. */
  336. #ifdef CONFIG_ARCH_MEMORY_PROBE
  337. static ssize_t
  338. memory_probe_store(struct device *dev, struct device_attribute *attr,
  339. const char *buf, size_t count)
  340. {
  341. u64 phys_addr;
  342. int nid;
  343. int i, ret;
  344. unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
  345. phys_addr = simple_strtoull(buf, NULL, 0);
  346. if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
  347. return -EINVAL;
  348. for (i = 0; i < sections_per_block; i++) {
  349. nid = memory_add_physaddr_to_nid(phys_addr);
  350. ret = add_memory(nid, phys_addr,
  351. PAGES_PER_SECTION << PAGE_SHIFT);
  352. if (ret)
  353. goto out;
  354. phys_addr += MIN_MEMORY_BLOCK_SIZE;
  355. }
  356. ret = count;
  357. out:
  358. return ret;
  359. }
  360. static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
  361. #endif
  362. #ifdef CONFIG_MEMORY_FAILURE
  363. /*
  364. * Support for offlining pages of memory
  365. */
  366. /* Soft offline a page */
  367. static ssize_t
  368. store_soft_offline_page(struct device *dev,
  369. struct device_attribute *attr,
  370. const char *buf, size_t count)
  371. {
  372. int ret;
  373. u64 pfn;
  374. if (!capable(CAP_SYS_ADMIN))
  375. return -EPERM;
  376. if (kstrtoull(buf, 0, &pfn) < 0)
  377. return -EINVAL;
  378. pfn >>= PAGE_SHIFT;
  379. if (!pfn_valid(pfn))
  380. return -ENXIO;
  381. ret = soft_offline_page(pfn_to_page(pfn), 0);
  382. return ret == 0 ? count : ret;
  383. }
  384. /* Forcibly offline a page, including killing processes. */
  385. static ssize_t
  386. store_hard_offline_page(struct device *dev,
  387. struct device_attribute *attr,
  388. const char *buf, size_t count)
  389. {
  390. int ret;
  391. u64 pfn;
  392. if (!capable(CAP_SYS_ADMIN))
  393. return -EPERM;
  394. if (kstrtoull(buf, 0, &pfn) < 0)
  395. return -EINVAL;
  396. pfn >>= PAGE_SHIFT;
  397. ret = memory_failure(pfn, 0, 0);
  398. return ret ? ret : count;
  399. }
  400. static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page);
  401. static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page);
  402. #endif
  403. /*
  404. * Note that phys_device is optional. It is here to allow for
  405. * differentiation between which *physical* devices each
  406. * section belongs to...
  407. */
  408. int __weak arch_get_memory_phys_device(unsigned long start_pfn)
  409. {
  410. return 0;
  411. }
  412. /*
  413. * A reference for the returned object is held and the reference for the
  414. * hinted object is released.
  415. */
  416. struct memory_block *find_memory_block_hinted(struct mem_section *section,
  417. struct memory_block *hint)
  418. {
  419. int block_id = base_memory_block_id(__section_nr(section));
  420. struct device *hintdev = hint ? &hint->dev : NULL;
  421. struct device *dev;
  422. dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
  423. if (hint)
  424. put_device(&hint->dev);
  425. if (!dev)
  426. return NULL;
  427. return container_of(dev, struct memory_block, dev);
  428. }
  429. /*
  430. * For now, we have a linear search to go find the appropriate
  431. * memory_block corresponding to a particular phys_index. If
  432. * this gets to be a real problem, we can always use a radix
  433. * tree or something here.
  434. *
  435. * This could be made generic for all device subsystems.
  436. */
  437. struct memory_block *find_memory_block(struct mem_section *section)
  438. {
  439. return find_memory_block_hinted(section, NULL);
  440. }
  441. static struct attribute *memory_memblk_attrs[] = {
  442. &dev_attr_phys_index.attr,
  443. &dev_attr_end_phys_index.attr,
  444. &dev_attr_state.attr,
  445. &dev_attr_phys_device.attr,
  446. &dev_attr_removable.attr,
  447. NULL
  448. };
  449. static struct attribute_group memory_memblk_attr_group = {
  450. .attrs = memory_memblk_attrs,
  451. };
  452. static const struct attribute_group *memory_memblk_attr_groups[] = {
  453. &memory_memblk_attr_group,
  454. NULL,
  455. };
  456. /*
  457. * register_memory - Setup a sysfs device for a memory block
  458. */
  459. static
  460. int register_memory(struct memory_block *memory)
  461. {
  462. memory->dev.bus = &memory_subsys;
  463. memory->dev.id = memory->start_section_nr / sections_per_block;
  464. memory->dev.release = memory_block_release;
  465. memory->dev.groups = memory_memblk_attr_groups;
  466. memory->dev.offline = memory->state == MEM_OFFLINE;
  467. return device_register(&memory->dev);
  468. }
  469. static int init_memory_block(struct memory_block **memory,
  470. struct mem_section *section, unsigned long state)
  471. {
  472. struct memory_block *mem;
  473. unsigned long start_pfn;
  474. int scn_nr;
  475. int ret = 0;
  476. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  477. if (!mem)
  478. return -ENOMEM;
  479. scn_nr = __section_nr(section);
  480. mem->start_section_nr =
  481. base_memory_block_id(scn_nr) * sections_per_block;
  482. mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
  483. mem->state = state;
  484. mem->section_count++;
  485. start_pfn = section_nr_to_pfn(mem->start_section_nr);
  486. mem->phys_device = arch_get_memory_phys_device(start_pfn);
  487. ret = register_memory(mem);
  488. *memory = mem;
  489. return ret;
  490. }
  491. static int add_memory_block(int base_section_nr)
  492. {
  493. struct memory_block *mem;
  494. int i, ret, section_count = 0, section_nr;
  495. for (i = base_section_nr;
  496. (i < base_section_nr + sections_per_block) && i < NR_MEM_SECTIONS;
  497. i++) {
  498. if (!present_section_nr(i))
  499. continue;
  500. if (section_count == 0)
  501. section_nr = i;
  502. section_count++;
  503. }
  504. if (section_count == 0)
  505. return 0;
  506. ret = init_memory_block(&mem, __nr_to_section(section_nr), MEM_ONLINE);
  507. if (ret)
  508. return ret;
  509. mem->section_count = section_count;
  510. return 0;
  511. }
  512. /*
  513. * need an interface for the VM to add new memory regions,
  514. * but without onlining it.
  515. */
  516. int register_new_memory(int nid, struct mem_section *section)
  517. {
  518. int ret = 0;
  519. struct memory_block *mem;
  520. mutex_lock(&mem_sysfs_mutex);
  521. mem = find_memory_block(section);
  522. if (mem) {
  523. mem->section_count++;
  524. put_device(&mem->dev);
  525. } else {
  526. ret = init_memory_block(&mem, section, MEM_OFFLINE);
  527. if (ret)
  528. goto out;
  529. }
  530. if (mem->section_count == sections_per_block)
  531. ret = register_mem_sect_under_node(mem, nid);
  532. out:
  533. mutex_unlock(&mem_sysfs_mutex);
  534. return ret;
  535. }
  536. #ifdef CONFIG_MEMORY_HOTREMOVE
  537. static void
  538. unregister_memory(struct memory_block *memory)
  539. {
  540. BUG_ON(memory->dev.bus != &memory_subsys);
  541. /* drop the ref. we got in remove_memory_block() */
  542. put_device(&memory->dev);
  543. device_unregister(&memory->dev);
  544. }
  545. static int remove_memory_block(unsigned long node_id,
  546. struct mem_section *section, int phys_device)
  547. {
  548. struct memory_block *mem;
  549. mutex_lock(&mem_sysfs_mutex);
  550. mem = find_memory_block(section);
  551. unregister_mem_sect_under_nodes(mem, __section_nr(section));
  552. mem->section_count--;
  553. if (mem->section_count == 0)
  554. unregister_memory(mem);
  555. else
  556. put_device(&mem->dev);
  557. mutex_unlock(&mem_sysfs_mutex);
  558. return 0;
  559. }
  560. int unregister_memory_section(struct mem_section *section)
  561. {
  562. if (!present_section(section))
  563. return -EINVAL;
  564. return remove_memory_block(0, section, 0);
  565. }
  566. #endif /* CONFIG_MEMORY_HOTREMOVE */
  567. /* return true if the memory block is offlined, otherwise, return false */
  568. bool is_memblock_offlined(struct memory_block *mem)
  569. {
  570. return mem->state == MEM_OFFLINE;
  571. }
  572. static struct attribute *memory_root_attrs[] = {
  573. #ifdef CONFIG_ARCH_MEMORY_PROBE
  574. &dev_attr_probe.attr,
  575. #endif
  576. #ifdef CONFIG_MEMORY_FAILURE
  577. &dev_attr_soft_offline_page.attr,
  578. &dev_attr_hard_offline_page.attr,
  579. #endif
  580. &dev_attr_block_size_bytes.attr,
  581. NULL
  582. };
  583. static struct attribute_group memory_root_attr_group = {
  584. .attrs = memory_root_attrs,
  585. };
  586. static const struct attribute_group *memory_root_attr_groups[] = {
  587. &memory_root_attr_group,
  588. NULL,
  589. };
  590. /*
  591. * Initialize the sysfs support for memory devices...
  592. */
  593. int __init memory_dev_init(void)
  594. {
  595. unsigned int i;
  596. int ret;
  597. int err;
  598. unsigned long block_sz;
  599. ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
  600. if (ret)
  601. goto out;
  602. block_sz = get_memory_block_size();
  603. sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  604. /*
  605. * Create entries for memory sections that were found
  606. * during boot and have been initialized
  607. */
  608. mutex_lock(&mem_sysfs_mutex);
  609. for (i = 0; i < NR_MEM_SECTIONS; i += sections_per_block) {
  610. err = add_memory_block(i);
  611. if (!ret)
  612. ret = err;
  613. }
  614. mutex_unlock(&mem_sysfs_mutex);
  615. out:
  616. if (ret)
  617. printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
  618. return ret;
  619. }