memory.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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/kobject.h>
  19. #include <linux/memory_hotplug.h>
  20. #include <linux/mm.h>
  21. #include <linux/mutex.h>
  22. #include <linux/stat.h>
  23. #include <linux/slab.h>
  24. #include <linux/atomic.h>
  25. #include <asm/uaccess.h>
  26. static DEFINE_MUTEX(mem_sysfs_mutex);
  27. #define MEMORY_CLASS_NAME "memory"
  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 struct bus_type memory_subsys = {
  34. .name = MEMORY_CLASS_NAME,
  35. .dev_name = MEMORY_CLASS_NAME,
  36. };
  37. static BLOCKING_NOTIFIER_HEAD(memory_chain);
  38. int register_memory_notifier(struct notifier_block *nb)
  39. {
  40. return blocking_notifier_chain_register(&memory_chain, nb);
  41. }
  42. EXPORT_SYMBOL(register_memory_notifier);
  43. void unregister_memory_notifier(struct notifier_block *nb)
  44. {
  45. blocking_notifier_chain_unregister(&memory_chain, nb);
  46. }
  47. EXPORT_SYMBOL(unregister_memory_notifier);
  48. static ATOMIC_NOTIFIER_HEAD(memory_isolate_chain);
  49. int register_memory_isolate_notifier(struct notifier_block *nb)
  50. {
  51. return atomic_notifier_chain_register(&memory_isolate_chain, nb);
  52. }
  53. EXPORT_SYMBOL(register_memory_isolate_notifier);
  54. void unregister_memory_isolate_notifier(struct notifier_block *nb)
  55. {
  56. atomic_notifier_chain_unregister(&memory_isolate_chain, nb);
  57. }
  58. EXPORT_SYMBOL(unregister_memory_isolate_notifier);
  59. static void memory_block_release(struct device *dev)
  60. {
  61. struct memory_block *mem = container_of(dev, struct memory_block, dev);
  62. kfree(mem);
  63. }
  64. unsigned long __weak memory_block_size_bytes(void)
  65. {
  66. return MIN_MEMORY_BLOCK_SIZE;
  67. }
  68. static unsigned long get_memory_block_size(void)
  69. {
  70. unsigned long block_sz;
  71. block_sz = memory_block_size_bytes();
  72. /* Validate blk_sz is a power of 2 and not less than section size */
  73. if ((block_sz & (block_sz - 1)) || (block_sz < MIN_MEMORY_BLOCK_SIZE)) {
  74. WARN_ON(1);
  75. block_sz = MIN_MEMORY_BLOCK_SIZE;
  76. }
  77. return block_sz;
  78. }
  79. /*
  80. * use this as the physical section index that this memsection
  81. * uses.
  82. */
  83. static ssize_t show_mem_start_phys_index(struct device *dev,
  84. struct device_attribute *attr, char *buf)
  85. {
  86. struct memory_block *mem =
  87. container_of(dev, struct memory_block, dev);
  88. unsigned long phys_index;
  89. phys_index = mem->start_section_nr / sections_per_block;
  90. return sprintf(buf, "%08lx\n", phys_index);
  91. }
  92. static ssize_t show_mem_end_phys_index(struct device *dev,
  93. struct device_attribute *attr, char *buf)
  94. {
  95. struct memory_block *mem =
  96. container_of(dev, struct memory_block, dev);
  97. unsigned long phys_index;
  98. phys_index = mem->end_section_nr / sections_per_block;
  99. return sprintf(buf, "%08lx\n", phys_index);
  100. }
  101. /*
  102. * Show whether the section of memory is likely to be hot-removable
  103. */
  104. static ssize_t show_mem_removable(struct device *dev,
  105. struct device_attribute *attr, char *buf)
  106. {
  107. unsigned long i, pfn;
  108. int ret = 1;
  109. struct memory_block *mem =
  110. container_of(dev, struct memory_block, dev);
  111. for (i = 0; i < sections_per_block; i++) {
  112. pfn = section_nr_to_pfn(mem->start_section_nr + i);
  113. ret &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
  114. }
  115. return sprintf(buf, "%d\n", ret);
  116. }
  117. /*
  118. * online, offline, going offline, etc.
  119. */
  120. static ssize_t show_mem_state(struct device *dev,
  121. struct device_attribute *attr, char *buf)
  122. {
  123. struct memory_block *mem =
  124. container_of(dev, struct 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. int online_type)
  217. {
  218. int ret = 0;
  219. if (mem->state != from_state_req) {
  220. ret = -EINVAL;
  221. goto out;
  222. }
  223. if (to_state == MEM_OFFLINE)
  224. mem->state = MEM_GOING_OFFLINE;
  225. ret = memory_block_action(mem->start_section_nr, to_state, online_type);
  226. if (ret) {
  227. mem->state = from_state_req;
  228. goto out;
  229. }
  230. mem->state = to_state;
  231. switch (mem->state) {
  232. case MEM_OFFLINE:
  233. kobject_uevent(&mem->dev.kobj, KOBJ_OFFLINE);
  234. break;
  235. case MEM_ONLINE:
  236. kobject_uevent(&mem->dev.kobj, KOBJ_ONLINE);
  237. break;
  238. default:
  239. break;
  240. }
  241. out:
  242. return ret;
  243. }
  244. static int memory_block_change_state(struct memory_block *mem,
  245. unsigned long to_state, unsigned long from_state_req,
  246. int online_type)
  247. {
  248. int ret;
  249. mutex_lock(&mem->state_mutex);
  250. ret = __memory_block_change_state(mem, to_state, from_state_req,
  251. online_type);
  252. mutex_unlock(&mem->state_mutex);
  253. return ret;
  254. }
  255. static ssize_t
  256. store_mem_state(struct device *dev,
  257. struct device_attribute *attr, const char *buf, size_t count)
  258. {
  259. struct memory_block *mem;
  260. int ret = -EINVAL;
  261. mem = container_of(dev, struct memory_block, dev);
  262. if (!strncmp(buf, "online_kernel", min_t(int, count, 13)))
  263. ret = memory_block_change_state(mem, MEM_ONLINE,
  264. MEM_OFFLINE, ONLINE_KERNEL);
  265. else if (!strncmp(buf, "online_movable", min_t(int, count, 14)))
  266. ret = memory_block_change_state(mem, MEM_ONLINE,
  267. MEM_OFFLINE, ONLINE_MOVABLE);
  268. else if (!strncmp(buf, "online", min_t(int, count, 6)))
  269. ret = memory_block_change_state(mem, MEM_ONLINE,
  270. MEM_OFFLINE, ONLINE_KEEP);
  271. else if(!strncmp(buf, "offline", min_t(int, count, 7)))
  272. ret = memory_block_change_state(mem, MEM_OFFLINE,
  273. MEM_ONLINE, -1);
  274. if (ret)
  275. return ret;
  276. return count;
  277. }
  278. /*
  279. * phys_device is a bad name for this. What I really want
  280. * is a way to differentiate between memory ranges that
  281. * are part of physical devices that constitute
  282. * a complete removable unit or fru.
  283. * i.e. do these ranges belong to the same physical device,
  284. * s.t. if I offline all of these sections I can then
  285. * remove the physical device?
  286. */
  287. static ssize_t show_phys_device(struct device *dev,
  288. struct device_attribute *attr, char *buf)
  289. {
  290. struct memory_block *mem =
  291. container_of(dev, struct memory_block, dev);
  292. return sprintf(buf, "%d\n", mem->phys_device);
  293. }
  294. static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
  295. static DEVICE_ATTR(end_phys_index, 0444, show_mem_end_phys_index, NULL);
  296. static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state);
  297. static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL);
  298. static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL);
  299. /*
  300. * Block size attribute stuff
  301. */
  302. static ssize_t
  303. print_block_size(struct device *dev, struct device_attribute *attr,
  304. char *buf)
  305. {
  306. return sprintf(buf, "%lx\n", get_memory_block_size());
  307. }
  308. static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL);
  309. /*
  310. * Some architectures will have custom drivers to do this, and
  311. * will not need to do it from userspace. The fake hot-add code
  312. * as well as ppc64 will do all of their discovery in userspace
  313. * and will require this interface.
  314. */
  315. #ifdef CONFIG_ARCH_MEMORY_PROBE
  316. static ssize_t
  317. memory_probe_store(struct device *dev, struct device_attribute *attr,
  318. const char *buf, size_t count)
  319. {
  320. u64 phys_addr;
  321. int nid;
  322. int i, ret;
  323. unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
  324. phys_addr = simple_strtoull(buf, NULL, 0);
  325. if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
  326. return -EINVAL;
  327. for (i = 0; i < sections_per_block; i++) {
  328. nid = memory_add_physaddr_to_nid(phys_addr);
  329. ret = add_memory(nid, phys_addr,
  330. PAGES_PER_SECTION << PAGE_SHIFT);
  331. if (ret)
  332. goto out;
  333. phys_addr += MIN_MEMORY_BLOCK_SIZE;
  334. }
  335. ret = count;
  336. out:
  337. return ret;
  338. }
  339. static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
  340. #endif
  341. #ifdef CONFIG_MEMORY_FAILURE
  342. /*
  343. * Support for offlining pages of memory
  344. */
  345. /* Soft offline a page */
  346. static ssize_t
  347. store_soft_offline_page(struct device *dev,
  348. struct device_attribute *attr,
  349. const char *buf, size_t count)
  350. {
  351. int ret;
  352. u64 pfn;
  353. if (!capable(CAP_SYS_ADMIN))
  354. return -EPERM;
  355. if (strict_strtoull(buf, 0, &pfn) < 0)
  356. return -EINVAL;
  357. pfn >>= PAGE_SHIFT;
  358. if (!pfn_valid(pfn))
  359. return -ENXIO;
  360. ret = soft_offline_page(pfn_to_page(pfn), 0);
  361. return ret == 0 ? count : ret;
  362. }
  363. /* Forcibly offline a page, including killing processes. */
  364. static ssize_t
  365. store_hard_offline_page(struct device *dev,
  366. struct device_attribute *attr,
  367. const char *buf, size_t count)
  368. {
  369. int ret;
  370. u64 pfn;
  371. if (!capable(CAP_SYS_ADMIN))
  372. return -EPERM;
  373. if (strict_strtoull(buf, 0, &pfn) < 0)
  374. return -EINVAL;
  375. pfn >>= PAGE_SHIFT;
  376. ret = memory_failure(pfn, 0, 0);
  377. return ret ? ret : count;
  378. }
  379. static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page);
  380. static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page);
  381. #endif
  382. /*
  383. * Note that phys_device is optional. It is here to allow for
  384. * differentiation between which *physical* devices each
  385. * section belongs to...
  386. */
  387. int __weak arch_get_memory_phys_device(unsigned long start_pfn)
  388. {
  389. return 0;
  390. }
  391. /*
  392. * A reference for the returned object is held and the reference for the
  393. * hinted object is released.
  394. */
  395. struct memory_block *find_memory_block_hinted(struct mem_section *section,
  396. struct memory_block *hint)
  397. {
  398. int block_id = base_memory_block_id(__section_nr(section));
  399. struct device *hintdev = hint ? &hint->dev : NULL;
  400. struct device *dev;
  401. dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
  402. if (hint)
  403. put_device(&hint->dev);
  404. if (!dev)
  405. return NULL;
  406. return container_of(dev, struct memory_block, dev);
  407. }
  408. /*
  409. * For now, we have a linear search to go find the appropriate
  410. * memory_block corresponding to a particular phys_index. If
  411. * this gets to be a real problem, we can always use a radix
  412. * tree or something here.
  413. *
  414. * This could be made generic for all device subsystems.
  415. */
  416. struct memory_block *find_memory_block(struct mem_section *section)
  417. {
  418. return find_memory_block_hinted(section, NULL);
  419. }
  420. static struct attribute *memory_memblk_attrs[] = {
  421. &dev_attr_phys_index.attr,
  422. &dev_attr_end_phys_index.attr,
  423. &dev_attr_state.attr,
  424. &dev_attr_phys_device.attr,
  425. &dev_attr_removable.attr,
  426. NULL
  427. };
  428. static struct attribute_group memory_memblk_attr_group = {
  429. .attrs = memory_memblk_attrs,
  430. };
  431. static const struct attribute_group *memory_memblk_attr_groups[] = {
  432. &memory_memblk_attr_group,
  433. NULL,
  434. };
  435. /*
  436. * register_memory - Setup a sysfs device for a memory block
  437. */
  438. static
  439. int register_memory(struct memory_block *memory)
  440. {
  441. int error;
  442. memory->dev.bus = &memory_subsys;
  443. memory->dev.id = memory->start_section_nr / sections_per_block;
  444. memory->dev.release = memory_block_release;
  445. memory->dev.groups = memory_memblk_attr_groups;
  446. error = device_register(&memory->dev);
  447. return error;
  448. }
  449. static int init_memory_block(struct memory_block **memory,
  450. struct mem_section *section, unsigned long state)
  451. {
  452. struct memory_block *mem;
  453. unsigned long start_pfn;
  454. int scn_nr;
  455. int ret = 0;
  456. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  457. if (!mem)
  458. return -ENOMEM;
  459. scn_nr = __section_nr(section);
  460. mem->start_section_nr =
  461. base_memory_block_id(scn_nr) * sections_per_block;
  462. mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
  463. mem->state = state;
  464. mem->section_count++;
  465. mutex_init(&mem->state_mutex);
  466. start_pfn = section_nr_to_pfn(mem->start_section_nr);
  467. mem->phys_device = arch_get_memory_phys_device(start_pfn);
  468. ret = register_memory(mem);
  469. *memory = mem;
  470. return ret;
  471. }
  472. static int add_memory_section(int nid, struct mem_section *section,
  473. struct memory_block **mem_p,
  474. unsigned long state, enum mem_add_context context)
  475. {
  476. struct memory_block *mem = NULL;
  477. int scn_nr = __section_nr(section);
  478. int ret = 0;
  479. mutex_lock(&mem_sysfs_mutex);
  480. if (context == BOOT) {
  481. /* same memory block ? */
  482. if (mem_p && *mem_p)
  483. if (scn_nr >= (*mem_p)->start_section_nr &&
  484. scn_nr <= (*mem_p)->end_section_nr) {
  485. mem = *mem_p;
  486. kobject_get(&mem->dev.kobj);
  487. }
  488. } else
  489. mem = find_memory_block(section);
  490. if (mem) {
  491. mem->section_count++;
  492. kobject_put(&mem->dev.kobj);
  493. } else {
  494. ret = init_memory_block(&mem, section, state);
  495. /* store memory_block pointer for next loop */
  496. if (!ret && context == BOOT)
  497. if (mem_p)
  498. *mem_p = mem;
  499. }
  500. if (!ret) {
  501. if (context == HOTPLUG &&
  502. mem->section_count == sections_per_block)
  503. ret = register_mem_sect_under_node(mem, nid);
  504. }
  505. mutex_unlock(&mem_sysfs_mutex);
  506. return ret;
  507. }
  508. /*
  509. * need an interface for the VM to add new memory regions,
  510. * but without onlining it.
  511. */
  512. int register_new_memory(int nid, struct mem_section *section)
  513. {
  514. return add_memory_section(nid, section, NULL, MEM_OFFLINE, HOTPLUG);
  515. }
  516. #ifdef CONFIG_MEMORY_HOTREMOVE
  517. static void
  518. unregister_memory(struct memory_block *memory)
  519. {
  520. BUG_ON(memory->dev.bus != &memory_subsys);
  521. /* drop the ref. we got in remove_memory_block() */
  522. kobject_put(&memory->dev.kobj);
  523. device_unregister(&memory->dev);
  524. }
  525. static int remove_memory_block(unsigned long node_id,
  526. struct mem_section *section, int phys_device)
  527. {
  528. struct memory_block *mem;
  529. mutex_lock(&mem_sysfs_mutex);
  530. mem = find_memory_block(section);
  531. unregister_mem_sect_under_nodes(mem, __section_nr(section));
  532. mem->section_count--;
  533. if (mem->section_count == 0)
  534. unregister_memory(mem);
  535. else
  536. kobject_put(&mem->dev.kobj);
  537. mutex_unlock(&mem_sysfs_mutex);
  538. return 0;
  539. }
  540. int unregister_memory_section(struct mem_section *section)
  541. {
  542. if (!present_section(section))
  543. return -EINVAL;
  544. return remove_memory_block(0, section, 0);
  545. }
  546. #endif /* CONFIG_MEMORY_HOTREMOVE */
  547. /*
  548. * offline one memory block. If the memory block has been offlined, do nothing.
  549. */
  550. int offline_memory_block(struct memory_block *mem)
  551. {
  552. int ret = 0;
  553. mutex_lock(&mem->state_mutex);
  554. if (mem->state != MEM_OFFLINE)
  555. ret = __memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE, -1);
  556. mutex_unlock(&mem->state_mutex);
  557. return ret;
  558. }
  559. /* return true if the memory block is offlined, otherwise, return false */
  560. bool is_memblock_offlined(struct memory_block *mem)
  561. {
  562. return mem->state == MEM_OFFLINE;
  563. }
  564. static struct attribute *memory_root_attrs[] = {
  565. #ifdef CONFIG_ARCH_MEMORY_PROBE
  566. &dev_attr_probe.attr,
  567. #endif
  568. #ifdef CONFIG_MEMORY_FAILURE
  569. &dev_attr_soft_offline_page.attr,
  570. &dev_attr_hard_offline_page.attr,
  571. #endif
  572. &dev_attr_block_size_bytes.attr,
  573. NULL
  574. };
  575. static struct attribute_group memory_root_attr_group = {
  576. .attrs = memory_root_attrs,
  577. };
  578. static const struct attribute_group *memory_root_attr_groups[] = {
  579. &memory_root_attr_group,
  580. NULL,
  581. };
  582. /*
  583. * Initialize the sysfs support for memory devices...
  584. */
  585. int __init memory_dev_init(void)
  586. {
  587. unsigned int i;
  588. int ret;
  589. int err;
  590. unsigned long block_sz;
  591. struct memory_block *mem = NULL;
  592. ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
  593. if (ret)
  594. goto out;
  595. block_sz = get_memory_block_size();
  596. sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  597. /*
  598. * Create entries for memory sections that were found
  599. * during boot and have been initialized
  600. */
  601. for (i = 0; i < NR_MEM_SECTIONS; i++) {
  602. if (!present_section_nr(i))
  603. continue;
  604. /* don't need to reuse memory_block if only one per block */
  605. err = add_memory_section(0, __nr_to_section(i),
  606. (sections_per_block == 1) ? NULL : &mem,
  607. MEM_ONLINE,
  608. BOOT);
  609. if (!ret)
  610. ret = err;
  611. }
  612. out:
  613. if (ret)
  614. printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
  615. return ret;
  616. }