memory.c 18 KB

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