memory.c 19 KB

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