memory.c 19 KB

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