memory.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. */
  193. static int
  194. memory_block_action(unsigned long phys_index, unsigned long action, int online_type)
  195. {
  196. unsigned long start_pfn;
  197. unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
  198. int ret;
  199. start_pfn = section_nr_to_pfn(phys_index);
  200. switch (action) {
  201. case MEM_ONLINE:
  202. if (!pages_correctly_probed(start_pfn))
  203. return -EBUSY;
  204. ret = online_pages(start_pfn, nr_pages, online_type);
  205. break;
  206. case MEM_OFFLINE:
  207. ret = offline_pages(start_pfn, nr_pages);
  208. break;
  209. default:
  210. WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
  211. "%ld\n", __func__, phys_index, action, action);
  212. ret = -EINVAL;
  213. }
  214. return ret;
  215. }
  216. static int memory_block_change_state(struct memory_block *mem,
  217. unsigned long to_state, unsigned long from_state_req)
  218. {
  219. int ret = 0;
  220. if (mem->state != from_state_req)
  221. return -EINVAL;
  222. if (to_state == MEM_OFFLINE)
  223. mem->state = MEM_GOING_OFFLINE;
  224. ret = memory_block_action(mem->start_section_nr, to_state,
  225. mem->online_type);
  226. mem->state = ret ? from_state_req : to_state;
  227. return ret;
  228. }
  229. /* The device lock serializes operations on memory_subsys_[online|offline] */
  230. static int memory_subsys_online(struct device *dev)
  231. {
  232. struct memory_block *mem = to_memory_block(dev);
  233. int ret;
  234. if (mem->state == MEM_ONLINE)
  235. return 0;
  236. /*
  237. * If we are called from store_mem_state(), online_type will be
  238. * set >= 0 Otherwise we were called from the device online
  239. * attribute and need to set the online_type.
  240. */
  241. if (mem->online_type < 0)
  242. mem->online_type = MMOP_ONLINE_KEEP;
  243. ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
  244. /* clear online_type */
  245. mem->online_type = -1;
  246. return ret;
  247. }
  248. static int memory_subsys_offline(struct device *dev)
  249. {
  250. struct memory_block *mem = to_memory_block(dev);
  251. if (mem->state == MEM_OFFLINE)
  252. return 0;
  253. /* Can't offline block with non-present sections */
  254. if (mem->section_count != sections_per_block)
  255. return -EINVAL;
  256. return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
  257. }
  258. static ssize_t
  259. store_mem_state(struct device *dev,
  260. struct device_attribute *attr, const char *buf, size_t count)
  261. {
  262. struct memory_block *mem = to_memory_block(dev);
  263. int ret, online_type;
  264. ret = lock_device_hotplug_sysfs();
  265. if (ret)
  266. return ret;
  267. if (sysfs_streq(buf, "online_kernel"))
  268. online_type = MMOP_ONLINE_KERNEL;
  269. else if (sysfs_streq(buf, "online_movable"))
  270. online_type = MMOP_ONLINE_MOVABLE;
  271. else if (sysfs_streq(buf, "online"))
  272. online_type = MMOP_ONLINE_KEEP;
  273. else if (sysfs_streq(buf, "offline"))
  274. online_type = MMOP_OFFLINE;
  275. else {
  276. ret = -EINVAL;
  277. goto err;
  278. }
  279. switch (online_type) {
  280. case MMOP_ONLINE_KERNEL:
  281. case MMOP_ONLINE_MOVABLE:
  282. case MMOP_ONLINE_KEEP:
  283. /* mem->online_type is protected by device_hotplug_lock */
  284. mem->online_type = online_type;
  285. ret = device_online(&mem->dev);
  286. break;
  287. case MMOP_OFFLINE:
  288. ret = device_offline(&mem->dev);
  289. break;
  290. default:
  291. ret = -EINVAL; /* should never happen */
  292. }
  293. err:
  294. unlock_device_hotplug();
  295. if (ret < 0)
  296. return ret;
  297. if (ret)
  298. return -EINVAL;
  299. return count;
  300. }
  301. /*
  302. * phys_device is a bad name for this. What I really want
  303. * is a way to differentiate between memory ranges that
  304. * are part of physical devices that constitute
  305. * a complete removable unit or fru.
  306. * i.e. do these ranges belong to the same physical device,
  307. * s.t. if I offline all of these sections I can then
  308. * remove the physical device?
  309. */
  310. static ssize_t show_phys_device(struct device *dev,
  311. struct device_attribute *attr, char *buf)
  312. {
  313. struct memory_block *mem = to_memory_block(dev);
  314. return sprintf(buf, "%d\n", mem->phys_device);
  315. }
  316. #ifdef CONFIG_MEMORY_HOTREMOVE
  317. static void print_allowed_zone(char *buf, int nid, unsigned long start_pfn,
  318. unsigned long nr_pages, int online_type,
  319. struct zone *default_zone)
  320. {
  321. struct zone *zone;
  322. zone = zone_for_pfn_range(online_type, nid, start_pfn, nr_pages);
  323. if (zone != default_zone) {
  324. strcat(buf, " ");
  325. strcat(buf, zone->name);
  326. }
  327. }
  328. static ssize_t show_valid_zones(struct device *dev,
  329. struct device_attribute *attr, char *buf)
  330. {
  331. struct memory_block *mem = to_memory_block(dev);
  332. unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
  333. unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
  334. unsigned long valid_start_pfn, valid_end_pfn;
  335. struct zone *default_zone;
  336. int nid;
  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. /*
  343. * The block contains more than one zone can not be offlined.
  344. * This can happen e.g. for ZONE_DMA and ZONE_DMA32
  345. */
  346. if (!test_pages_in_a_zone(start_pfn, start_pfn + nr_pages,
  347. &valid_start_pfn, &valid_end_pfn))
  348. return sprintf(buf, "none\n");
  349. start_pfn = valid_start_pfn;
  350. strcat(buf, page_zone(pfn_to_page(start_pfn))->name);
  351. goto out;
  352. }
  353. nid = mem->nid;
  354. default_zone = zone_for_pfn_range(MMOP_ONLINE_KEEP, nid, start_pfn, nr_pages);
  355. strcat(buf, default_zone->name);
  356. print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_KERNEL,
  357. default_zone);
  358. print_allowed_zone(buf, nid, start_pfn, nr_pages, MMOP_ONLINE_MOVABLE,
  359. default_zone);
  360. out:
  361. strcat(buf, "\n");
  362. return strlen(buf);
  363. }
  364. static DEVICE_ATTR(valid_zones, 0444, show_valid_zones, NULL);
  365. #endif
  366. static DEVICE_ATTR(phys_index, 0444, show_mem_start_phys_index, NULL);
  367. static DEVICE_ATTR(state, 0644, show_mem_state, store_mem_state);
  368. static DEVICE_ATTR(phys_device, 0444, show_phys_device, NULL);
  369. static DEVICE_ATTR(removable, 0444, show_mem_removable, NULL);
  370. /*
  371. * Block size attribute stuff
  372. */
  373. static ssize_t
  374. print_block_size(struct device *dev, struct device_attribute *attr,
  375. char *buf)
  376. {
  377. return sprintf(buf, "%lx\n", get_memory_block_size());
  378. }
  379. static DEVICE_ATTR(block_size_bytes, 0444, print_block_size, NULL);
  380. /*
  381. * Memory auto online policy.
  382. */
  383. static ssize_t
  384. show_auto_online_blocks(struct device *dev, struct device_attribute *attr,
  385. char *buf)
  386. {
  387. if (memhp_auto_online)
  388. return sprintf(buf, "online\n");
  389. else
  390. return sprintf(buf, "offline\n");
  391. }
  392. static ssize_t
  393. store_auto_online_blocks(struct device *dev, struct device_attribute *attr,
  394. const char *buf, size_t count)
  395. {
  396. if (sysfs_streq(buf, "online"))
  397. memhp_auto_online = true;
  398. else if (sysfs_streq(buf, "offline"))
  399. memhp_auto_online = false;
  400. else
  401. return -EINVAL;
  402. return count;
  403. }
  404. static DEVICE_ATTR(auto_online_blocks, 0644, show_auto_online_blocks,
  405. store_auto_online_blocks);
  406. /*
  407. * Some architectures will have custom drivers to do this, and
  408. * will not need to do it from userspace. The fake hot-add code
  409. * as well as ppc64 will do all of their discovery in userspace
  410. * and will require this interface.
  411. */
  412. #ifdef CONFIG_ARCH_MEMORY_PROBE
  413. static ssize_t
  414. memory_probe_store(struct device *dev, struct device_attribute *attr,
  415. const char *buf, size_t count)
  416. {
  417. u64 phys_addr;
  418. int nid, ret;
  419. unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
  420. ret = kstrtoull(buf, 0, &phys_addr);
  421. if (ret)
  422. return ret;
  423. if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
  424. return -EINVAL;
  425. ret = lock_device_hotplug_sysfs();
  426. if (ret)
  427. goto out;
  428. nid = memory_add_physaddr_to_nid(phys_addr);
  429. ret = __add_memory(nid, phys_addr,
  430. MIN_MEMORY_BLOCK_SIZE * sections_per_block);
  431. if (ret)
  432. goto out;
  433. ret = count;
  434. out:
  435. unlock_device_hotplug();
  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. int ret;
  543. memory->dev.bus = &memory_subsys;
  544. memory->dev.id = memory->start_section_nr / sections_per_block;
  545. memory->dev.release = memory_block_release;
  546. memory->dev.groups = memory_memblk_attr_groups;
  547. memory->dev.offline = memory->state == MEM_OFFLINE;
  548. ret = device_register(&memory->dev);
  549. if (ret)
  550. put_device(&memory->dev);
  551. return ret;
  552. }
  553. static int init_memory_block(struct memory_block **memory,
  554. struct mem_section *section, unsigned long state)
  555. {
  556. struct memory_block *mem;
  557. unsigned long start_pfn;
  558. int scn_nr;
  559. int ret = 0;
  560. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  561. if (!mem)
  562. return -ENOMEM;
  563. scn_nr = __section_nr(section);
  564. mem->start_section_nr =
  565. base_memory_block_id(scn_nr) * sections_per_block;
  566. mem->end_section_nr = mem->start_section_nr + sections_per_block - 1;
  567. mem->state = state;
  568. start_pfn = section_nr_to_pfn(mem->start_section_nr);
  569. mem->phys_device = arch_get_memory_phys_device(start_pfn);
  570. ret = register_memory(mem);
  571. *memory = mem;
  572. return ret;
  573. }
  574. static int add_memory_block(int base_section_nr)
  575. {
  576. struct memory_block *mem;
  577. int i, ret, section_count = 0, section_nr;
  578. for (i = base_section_nr;
  579. (i < base_section_nr + sections_per_block) && i < NR_MEM_SECTIONS;
  580. i++) {
  581. if (!present_section_nr(i))
  582. continue;
  583. if (section_count == 0)
  584. section_nr = i;
  585. section_count++;
  586. }
  587. if (section_count == 0)
  588. return 0;
  589. ret = init_memory_block(&mem, __nr_to_section(section_nr), MEM_ONLINE);
  590. if (ret)
  591. return ret;
  592. mem->section_count = section_count;
  593. return 0;
  594. }
  595. /*
  596. * need an interface for the VM to add new memory regions,
  597. * but without onlining it.
  598. */
  599. int hotplug_memory_register(int nid, struct mem_section *section)
  600. {
  601. int ret = 0;
  602. struct memory_block *mem;
  603. mutex_lock(&mem_sysfs_mutex);
  604. mem = find_memory_block(section);
  605. if (mem) {
  606. mem->section_count++;
  607. put_device(&mem->dev);
  608. } else {
  609. ret = init_memory_block(&mem, section, MEM_OFFLINE);
  610. if (ret)
  611. goto out;
  612. mem->section_count++;
  613. }
  614. out:
  615. mutex_unlock(&mem_sysfs_mutex);
  616. return ret;
  617. }
  618. #ifdef CONFIG_MEMORY_HOTREMOVE
  619. static void
  620. unregister_memory(struct memory_block *memory)
  621. {
  622. BUG_ON(memory->dev.bus != &memory_subsys);
  623. /* drop the ref. we got in remove_memory_block() */
  624. put_device(&memory->dev);
  625. device_unregister(&memory->dev);
  626. }
  627. static int remove_memory_section(unsigned long node_id,
  628. struct mem_section *section, int phys_device)
  629. {
  630. struct memory_block *mem;
  631. mutex_lock(&mem_sysfs_mutex);
  632. /*
  633. * Some users of the memory hotplug do not want/need memblock to
  634. * track all sections. Skip over those.
  635. */
  636. mem = find_memory_block(section);
  637. if (!mem)
  638. goto out_unlock;
  639. unregister_mem_sect_under_nodes(mem, __section_nr(section));
  640. mem->section_count--;
  641. if (mem->section_count == 0)
  642. unregister_memory(mem);
  643. else
  644. put_device(&mem->dev);
  645. out_unlock:
  646. mutex_unlock(&mem_sysfs_mutex);
  647. return 0;
  648. }
  649. int unregister_memory_section(struct mem_section *section)
  650. {
  651. if (!present_section(section))
  652. return -EINVAL;
  653. return remove_memory_section(0, section, 0);
  654. }
  655. #endif /* CONFIG_MEMORY_HOTREMOVE */
  656. /* return true if the memory block is offlined, otherwise, return false */
  657. bool is_memblock_offlined(struct memory_block *mem)
  658. {
  659. return mem->state == MEM_OFFLINE;
  660. }
  661. static struct attribute *memory_root_attrs[] = {
  662. #ifdef CONFIG_ARCH_MEMORY_PROBE
  663. &dev_attr_probe.attr,
  664. #endif
  665. #ifdef CONFIG_MEMORY_FAILURE
  666. &dev_attr_soft_offline_page.attr,
  667. &dev_attr_hard_offline_page.attr,
  668. #endif
  669. &dev_attr_block_size_bytes.attr,
  670. &dev_attr_auto_online_blocks.attr,
  671. NULL
  672. };
  673. static struct attribute_group memory_root_attr_group = {
  674. .attrs = memory_root_attrs,
  675. };
  676. static const struct attribute_group *memory_root_attr_groups[] = {
  677. &memory_root_attr_group,
  678. NULL,
  679. };
  680. /*
  681. * Initialize the sysfs support for memory devices...
  682. */
  683. int __init memory_dev_init(void)
  684. {
  685. unsigned int i;
  686. int ret;
  687. int err;
  688. unsigned long block_sz;
  689. ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
  690. if (ret)
  691. goto out;
  692. block_sz = get_memory_block_size();
  693. sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  694. /*
  695. * Create entries for memory sections that were found
  696. * during boot and have been initialized
  697. */
  698. mutex_lock(&mem_sysfs_mutex);
  699. for (i = 0; i <= __highest_present_section_nr;
  700. i += sections_per_block) {
  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. }