memory.c 21 KB

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