memory.c 20 KB

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