memory.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. ret = lock_device_hotplug_sysfs();
  430. if (ret)
  431. return ret;
  432. nid = memory_add_physaddr_to_nid(phys_addr);
  433. ret = __add_memory(nid, phys_addr,
  434. MIN_MEMORY_BLOCK_SIZE * sections_per_block);
  435. if (ret)
  436. goto out;
  437. ret = count;
  438. out:
  439. unlock_device_hotplug();
  440. return ret;
  441. }
  442. static DEVICE_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
  443. #endif
  444. #ifdef CONFIG_MEMORY_FAILURE
  445. /*
  446. * Support for offlining pages of memory
  447. */
  448. /* Soft offline a page */
  449. static ssize_t
  450. store_soft_offline_page(struct device *dev,
  451. struct device_attribute *attr,
  452. const char *buf, size_t count)
  453. {
  454. int ret;
  455. u64 pfn;
  456. if (!capable(CAP_SYS_ADMIN))
  457. return -EPERM;
  458. if (kstrtoull(buf, 0, &pfn) < 0)
  459. return -EINVAL;
  460. pfn >>= PAGE_SHIFT;
  461. if (!pfn_valid(pfn))
  462. return -ENXIO;
  463. /* Only online pages can be soft-offlined (esp., not ZONE_DEVICE). */
  464. if (!pfn_to_online_page(pfn))
  465. return -EIO;
  466. ret = soft_offline_page(pfn_to_page(pfn), 0);
  467. return ret == 0 ? count : ret;
  468. }
  469. /* Forcibly offline a page, including killing processes. */
  470. static ssize_t
  471. store_hard_offline_page(struct device *dev,
  472. struct device_attribute *attr,
  473. const char *buf, size_t count)
  474. {
  475. int ret;
  476. u64 pfn;
  477. if (!capable(CAP_SYS_ADMIN))
  478. return -EPERM;
  479. if (kstrtoull(buf, 0, &pfn) < 0)
  480. return -EINVAL;
  481. pfn >>= PAGE_SHIFT;
  482. ret = memory_failure(pfn, 0, 0);
  483. return ret ? ret : count;
  484. }
  485. static DEVICE_ATTR(soft_offline_page, S_IWUSR, NULL, store_soft_offline_page);
  486. static DEVICE_ATTR(hard_offline_page, S_IWUSR, NULL, store_hard_offline_page);
  487. #endif
  488. /*
  489. * Note that phys_device is optional. It is here to allow for
  490. * differentiation between which *physical* devices each
  491. * section belongs to...
  492. */
  493. int __weak arch_get_memory_phys_device(unsigned long start_pfn)
  494. {
  495. return 0;
  496. }
  497. /*
  498. * A reference for the returned object is held and the reference for the
  499. * hinted object is released.
  500. */
  501. struct memory_block *find_memory_block_hinted(struct mem_section *section,
  502. struct memory_block *hint)
  503. {
  504. int block_id = base_memory_block_id(__section_nr(section));
  505. struct device *hintdev = hint ? &hint->dev : NULL;
  506. struct device *dev;
  507. dev = subsys_find_device_by_id(&memory_subsys, block_id, hintdev);
  508. if (hint)
  509. put_device(&hint->dev);
  510. if (!dev)
  511. return NULL;
  512. return to_memory_block(dev);
  513. }
  514. /*
  515. * For now, we have a linear search to go find the appropriate
  516. * memory_block corresponding to a particular phys_index. If
  517. * this gets to be a real problem, we can always use a radix
  518. * tree or something here.
  519. *
  520. * This could be made generic for all device subsystems.
  521. */
  522. struct memory_block *find_memory_block(struct mem_section *section)
  523. {
  524. return find_memory_block_hinted(section, NULL);
  525. }
  526. static struct attribute *memory_memblk_attrs[] = {
  527. &dev_attr_phys_index.attr,
  528. &dev_attr_state.attr,
  529. &dev_attr_phys_device.attr,
  530. &dev_attr_removable.attr,
  531. #ifdef CONFIG_MEMORY_HOTREMOVE
  532. &dev_attr_valid_zones.attr,
  533. #endif
  534. NULL
  535. };
  536. static struct attribute_group memory_memblk_attr_group = {
  537. .attrs = memory_memblk_attrs,
  538. };
  539. static const struct attribute_group *memory_memblk_attr_groups[] = {
  540. &memory_memblk_attr_group,
  541. NULL,
  542. };
  543. /*
  544. * register_memory - Setup a sysfs device for a memory block
  545. */
  546. static
  547. int register_memory(struct memory_block *memory)
  548. {
  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. return device_register(&memory->dev);
  555. }
  556. static int init_memory_block(struct memory_block **memory,
  557. struct mem_section *section, unsigned long state)
  558. {
  559. struct memory_block *mem;
  560. unsigned long start_pfn;
  561. int scn_nr;
  562. int ret = 0;
  563. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  564. if (!mem)
  565. return -ENOMEM;
  566. scn_nr = __section_nr(section);
  567. mem->start_section_nr =
  568. base_memory_block_id(scn_nr) * sections_per_block;
  569. mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
  570. mem->state = state;
  571. start_pfn = section_nr_to_pfn(mem->start_section_nr);
  572. mem->phys_device = arch_get_memory_phys_device(start_pfn);
  573. ret = register_memory(mem);
  574. *memory = mem;
  575. return ret;
  576. }
  577. static int add_memory_block(int base_section_nr)
  578. {
  579. struct memory_block *mem;
  580. int i, ret, section_count = 0, section_nr;
  581. for (i = base_section_nr;
  582. (i < base_section_nr + sections_per_block) && i < NR_MEM_SECTIONS;
  583. i++) {
  584. if (!present_section_nr(i))
  585. continue;
  586. if (section_count == 0)
  587. section_nr = i;
  588. section_count++;
  589. }
  590. if (section_count == 0)
  591. return 0;
  592. ret = init_memory_block(&mem, __nr_to_section(section_nr), MEM_ONLINE);
  593. if (ret)
  594. return ret;
  595. mem->section_count = section_count;
  596. return 0;
  597. }
  598. /*
  599. * need an interface for the VM to add new memory regions,
  600. * but without onlining it.
  601. */
  602. int register_new_memory(int nid, struct mem_section *section)
  603. {
  604. int ret = 0;
  605. struct memory_block *mem;
  606. mutex_lock(&mem_sysfs_mutex);
  607. mem = find_memory_block(section);
  608. if (mem) {
  609. mem->section_count++;
  610. put_device(&mem->dev);
  611. } else {
  612. ret = init_memory_block(&mem, section, MEM_OFFLINE);
  613. if (ret)
  614. goto out;
  615. mem->section_count++;
  616. }
  617. if (mem->section_count == sections_per_block)
  618. ret = register_mem_sect_under_node(mem, nid);
  619. out:
  620. mutex_unlock(&mem_sysfs_mutex);
  621. return ret;
  622. }
  623. #ifdef CONFIG_MEMORY_HOTREMOVE
  624. static void
  625. unregister_memory(struct memory_block *memory)
  626. {
  627. BUG_ON(memory->dev.bus != &memory_subsys);
  628. /* drop the ref. we got in remove_memory_block() */
  629. put_device(&memory->dev);
  630. device_unregister(&memory->dev);
  631. }
  632. static int remove_memory_section(unsigned long node_id,
  633. struct mem_section *section, int phys_device)
  634. {
  635. struct memory_block *mem;
  636. mutex_lock(&mem_sysfs_mutex);
  637. /*
  638. * Some users of the memory hotplug do not want/need memblock to
  639. * track all sections. Skip over those.
  640. */
  641. mem = find_memory_block(section);
  642. if (!mem)
  643. goto out_unlock;
  644. unregister_mem_sect_under_nodes(mem, __section_nr(section));
  645. mem->section_count--;
  646. if (mem->section_count == 0)
  647. unregister_memory(mem);
  648. else
  649. put_device(&mem->dev);
  650. out_unlock:
  651. mutex_unlock(&mem_sysfs_mutex);
  652. return 0;
  653. }
  654. int unregister_memory_section(struct mem_section *section)
  655. {
  656. if (!present_section(section))
  657. return -EINVAL;
  658. return remove_memory_section(0, section, 0);
  659. }
  660. #endif /* CONFIG_MEMORY_HOTREMOVE */
  661. /* return true if the memory block is offlined, otherwise, return false */
  662. bool is_memblock_offlined(struct memory_block *mem)
  663. {
  664. return mem->state == MEM_OFFLINE;
  665. }
  666. static struct attribute *memory_root_attrs[] = {
  667. #ifdef CONFIG_ARCH_MEMORY_PROBE
  668. &dev_attr_probe.attr,
  669. #endif
  670. #ifdef CONFIG_MEMORY_FAILURE
  671. &dev_attr_soft_offline_page.attr,
  672. &dev_attr_hard_offline_page.attr,
  673. #endif
  674. &dev_attr_block_size_bytes.attr,
  675. &dev_attr_auto_online_blocks.attr,
  676. NULL
  677. };
  678. static struct attribute_group memory_root_attr_group = {
  679. .attrs = memory_root_attrs,
  680. };
  681. static const struct attribute_group *memory_root_attr_groups[] = {
  682. &memory_root_attr_group,
  683. NULL,
  684. };
  685. /*
  686. * Initialize the sysfs support for memory devices...
  687. */
  688. int __init memory_dev_init(void)
  689. {
  690. unsigned int i;
  691. int ret;
  692. int err;
  693. unsigned long block_sz;
  694. ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
  695. if (ret)
  696. goto out;
  697. block_sz = get_memory_block_size();
  698. sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  699. /*
  700. * Create entries for memory sections that were found
  701. * during boot and have been initialized
  702. */
  703. mutex_lock(&mem_sysfs_mutex);
  704. for (i = 0; i < NR_MEM_SECTIONS; i += sections_per_block) {
  705. /* Don't iterate over sections we know are !present: */
  706. if (i > __highest_present_section_nr)
  707. break;
  708. err = add_memory_block(i);
  709. if (!ret)
  710. ret = err;
  711. }
  712. mutex_unlock(&mem_sysfs_mutex);
  713. out:
  714. if (ret)
  715. printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
  716. return ret;
  717. }