memory.c 18 KB

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