memory.c 19 KB

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