memory.c 20 KB

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