memory.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. 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 = to_memory_block(dev);
  312. return sprintf(buf, "%d\n", mem->phys_device);
  313. }
  314. static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
  315. static DEVICE_ATTR(end_phys_index, 0444, show_mem_end_phys_index, NULL);
  316. static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state);
  317. static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL);
  318. static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL);
  319. /*
  320. * Block size attribute stuff
  321. */
  322. static ssize_t
  323. print_block_size(struct device *dev, struct device_attribute *attr,
  324. char *buf)
  325. {
  326. return sprintf(buf, "%lx\n", get_memory_block_size());
  327. }
  328. static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL);
  329. /*
  330. * Some architectures will have custom drivers to do this, and
  331. * will not need to do it from userspace. The fake hot-add code
  332. * as well as ppc64 will do all of their discovery in userspace
  333. * and will require this interface.
  334. */
  335. #ifdef CONFIG_ARCH_MEMORY_PROBE
  336. static ssize_t
  337. memory_probe_store(struct device *dev, struct device_attribute *attr,
  338. const char *buf, size_t count)
  339. {
  340. u64 phys_addr;
  341. int nid;
  342. int i, ret;
  343. unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
  344. phys_addr = simple_strtoull(buf, NULL, 0);
  345. if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
  346. return -EINVAL;
  347. for (i = 0; i < sections_per_block; i++) {
  348. nid = memory_add_physaddr_to_nid(phys_addr);
  349. ret = add_memory(nid, phys_addr,
  350. PAGES_PER_SECTION << PAGE_SHIFT);
  351. if (ret)
  352. goto out;
  353. phys_addr += MIN_MEMORY_BLOCK_SIZE;
  354. }
  355. ret = count;
  356. out:
  357. return ret;
  358. }
  359. static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
  360. #endif
  361. #ifdef CONFIG_MEMORY_FAILURE
  362. /*
  363. * Support for offlining pages of memory
  364. */
  365. /* Soft offline a page */
  366. static ssize_t
  367. store_soft_offline_page(struct device *dev,
  368. struct device_attribute *attr,
  369. const char *buf, size_t count)
  370. {
  371. int ret;
  372. u64 pfn;
  373. if (!capable(CAP_SYS_ADMIN))
  374. return -EPERM;
  375. if (kstrtoull(buf, 0, &pfn) < 0)
  376. return -EINVAL;
  377. pfn >>= PAGE_SHIFT;
  378. if (!pfn_valid(pfn))
  379. return -ENXIO;
  380. ret = soft_offline_page(pfn_to_page(pfn), 0);
  381. return ret == 0 ? count : ret;
  382. }
  383. /* Forcibly offline a page, including killing processes. */
  384. static ssize_t
  385. store_hard_offline_page(struct device *dev,
  386. struct device_attribute *attr,
  387. const char *buf, size_t count)
  388. {
  389. int ret;
  390. u64 pfn;
  391. if (!capable(CAP_SYS_ADMIN))
  392. return -EPERM;
  393. if (kstrtoull(buf, 0, &pfn) < 0)
  394. return -EINVAL;
  395. pfn >>= PAGE_SHIFT;
  396. ret = memory_failure(pfn, 0, 0);
  397. return ret ? ret : count;
  398. }
  399. static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page);
  400. static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page);
  401. #endif
  402. /*
  403. * Note that phys_device is optional. It is here to allow for
  404. * differentiation between which *physical* devices each
  405. * section belongs to...
  406. */
  407. int __weak arch_get_memory_phys_device(unsigned long start_pfn)
  408. {
  409. return 0;
  410. }
  411. /*
  412. * A reference for the returned object is held and the reference for the
  413. * hinted object is released.
  414. */
  415. struct memory_block *find_memory_block_hinted(struct mem_section *section,
  416. struct memory_block *hint)
  417. {
  418. int block_id = base_memory_block_id(__section_nr(section));
  419. struct device *hintdev = hint ? &hint->dev : NULL;
  420. struct device *dev;
  421. dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
  422. if (hint)
  423. put_device(&hint->dev);
  424. if (!dev)
  425. return NULL;
  426. return to_memory_block(dev);
  427. }
  428. /*
  429. * For now, we have a linear search to go find the appropriate
  430. * memory_block corresponding to a particular phys_index. If
  431. * this gets to be a real problem, we can always use a radix
  432. * tree or something here.
  433. *
  434. * This could be made generic for all device subsystems.
  435. */
  436. struct memory_block *find_memory_block(struct mem_section *section)
  437. {
  438. return find_memory_block_hinted(section, NULL);
  439. }
  440. static struct attribute *memory_memblk_attrs[] = {
  441. &dev_attr_phys_index.attr,
  442. &dev_attr_end_phys_index.attr,
  443. &dev_attr_state.attr,
  444. &dev_attr_phys_device.attr,
  445. &dev_attr_removable.attr,
  446. NULL
  447. };
  448. static struct attribute_group memory_memblk_attr_group = {
  449. .attrs = memory_memblk_attrs,
  450. };
  451. static const struct attribute_group *memory_memblk_attr_groups[] = {
  452. &memory_memblk_attr_group,
  453. NULL,
  454. };
  455. /*
  456. * register_memory - Setup a sysfs device for a memory block
  457. */
  458. static
  459. int register_memory(struct memory_block *memory)
  460. {
  461. memory->dev.bus = &memory_subsys;
  462. memory->dev.id = memory->start_section_nr / sections_per_block;
  463. memory->dev.release = memory_block_release;
  464. memory->dev.groups = memory_memblk_attr_groups;
  465. memory->dev.offline = memory->state == MEM_OFFLINE;
  466. return device_register(&memory->dev);
  467. }
  468. static int init_memory_block(struct memory_block **memory,
  469. struct mem_section *section, unsigned long state)
  470. {
  471. struct memory_block *mem;
  472. unsigned long start_pfn;
  473. int scn_nr;
  474. int ret = 0;
  475. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  476. if (!mem)
  477. return -ENOMEM;
  478. scn_nr = __section_nr(section);
  479. mem->start_section_nr =
  480. base_memory_block_id(scn_nr) * sections_per_block;
  481. mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
  482. mem->state = state;
  483. mem->section_count++;
  484. start_pfn = section_nr_to_pfn(mem->start_section_nr);
  485. mem->phys_device = arch_get_memory_phys_device(start_pfn);
  486. ret = register_memory(mem);
  487. *memory = mem;
  488. return ret;
  489. }
  490. static int add_memory_block(int base_section_nr)
  491. {
  492. struct memory_block *mem;
  493. int i, ret, section_count = 0, section_nr;
  494. for (i = base_section_nr;
  495. (i < base_section_nr + sections_per_block) && i < NR_MEM_SECTIONS;
  496. i++) {
  497. if (!present_section_nr(i))
  498. continue;
  499. if (section_count == 0)
  500. section_nr = i;
  501. section_count++;
  502. }
  503. if (section_count == 0)
  504. return 0;
  505. ret = init_memory_block(&mem, __nr_to_section(section_nr), MEM_ONLINE);
  506. if (ret)
  507. return ret;
  508. mem->section_count = section_count;
  509. return 0;
  510. }
  511. /*
  512. * need an interface for the VM to add new memory regions,
  513. * but without onlining it.
  514. */
  515. int register_new_memory(int nid, struct mem_section *section)
  516. {
  517. int ret = 0;
  518. struct memory_block *mem;
  519. mutex_lock(&mem_sysfs_mutex);
  520. mem = find_memory_block(section);
  521. if (mem) {
  522. mem->section_count++;
  523. put_device(&mem->dev);
  524. } else {
  525. ret = init_memory_block(&mem, section, MEM_OFFLINE);
  526. if (ret)
  527. goto out;
  528. }
  529. if (mem->section_count == sections_per_block)
  530. ret = register_mem_sect_under_node(mem, nid);
  531. out:
  532. mutex_unlock(&mem_sysfs_mutex);
  533. return ret;
  534. }
  535. #ifdef CONFIG_MEMORY_HOTREMOVE
  536. static void
  537. unregister_memory(struct memory_block *memory)
  538. {
  539. BUG_ON(memory->dev.bus != &memory_subsys);
  540. /* drop the ref. we got in remove_memory_block() */
  541. put_device(&memory->dev);
  542. device_unregister(&memory->dev);
  543. }
  544. static int remove_memory_block(unsigned long node_id,
  545. struct mem_section *section, int phys_device)
  546. {
  547. struct memory_block *mem;
  548. mutex_lock(&mem_sysfs_mutex);
  549. mem = find_memory_block(section);
  550. unregister_mem_sect_under_nodes(mem, __section_nr(section));
  551. mem->section_count--;
  552. if (mem->section_count == 0)
  553. unregister_memory(mem);
  554. else
  555. put_device(&mem->dev);
  556. mutex_unlock(&mem_sysfs_mutex);
  557. return 0;
  558. }
  559. int unregister_memory_section(struct mem_section *section)
  560. {
  561. if (!present_section(section))
  562. return -EINVAL;
  563. return remove_memory_block(0, section, 0);
  564. }
  565. #endif /* CONFIG_MEMORY_HOTREMOVE */
  566. /* return true if the memory block is offlined, otherwise, return false */
  567. bool is_memblock_offlined(struct memory_block *mem)
  568. {
  569. return mem->state == MEM_OFFLINE;
  570. }
  571. static struct attribute *memory_root_attrs[] = {
  572. #ifdef CONFIG_ARCH_MEMORY_PROBE
  573. &dev_attr_probe.attr,
  574. #endif
  575. #ifdef CONFIG_MEMORY_FAILURE
  576. &dev_attr_soft_offline_page.attr,
  577. &dev_attr_hard_offline_page.attr,
  578. #endif
  579. &dev_attr_block_size_bytes.attr,
  580. NULL
  581. };
  582. static struct attribute_group memory_root_attr_group = {
  583. .attrs = memory_root_attrs,
  584. };
  585. static const struct attribute_group *memory_root_attr_groups[] = {
  586. &memory_root_attr_group,
  587. NULL,
  588. };
  589. /*
  590. * Initialize the sysfs support for memory devices...
  591. */
  592. int __init memory_dev_init(void)
  593. {
  594. unsigned int i;
  595. int ret;
  596. int err;
  597. unsigned long block_sz;
  598. ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
  599. if (ret)
  600. goto out;
  601. block_sz = get_memory_block_size();
  602. sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  603. /*
  604. * Create entries for memory sections that were found
  605. * during boot and have been initialized
  606. */
  607. mutex_lock(&mem_sysfs_mutex);
  608. for (i = 0; i < NR_MEM_SECTIONS; i += sections_per_block) {
  609. err = add_memory_block(i);
  610. if (!ret)
  611. ret = err;
  612. }
  613. mutex_unlock(&mem_sysfs_mutex);
  614. out:
  615. if (ret)
  616. printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
  617. return ret;
  618. }