memory.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  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/stat.h>
  22. #include <linux/slab.h>
  23. #include <linux/xarray.h>
  24. #include <linux/atomic.h>
  25. #include <linux/uaccess.h>
  26. #define MEMORY_CLASS_NAME "memory"
  27. static const char *const online_type_to_str[] = {
  28. [MMOP_OFFLINE] = "offline",
  29. [MMOP_ONLINE] = "online",
  30. [MMOP_ONLINE_KERNEL] = "online_kernel",
  31. [MMOP_ONLINE_MOVABLE] = "online_movable",
  32. };
  33. int mhp_online_type_from_str(const char *str)
  34. {
  35. int i;
  36. for (i = 0; i < ARRAY_SIZE(online_type_to_str); i++) {
  37. if (sysfs_streq(str, online_type_to_str[i]))
  38. return i;
  39. }
  40. return -EINVAL;
  41. }
  42. #define to_memory_block(dev) container_of(dev, struct memory_block, dev)
  43. static int sections_per_block;
  44. static inline unsigned long memory_block_id(unsigned long section_nr)
  45. {
  46. return section_nr / sections_per_block;
  47. }
  48. static inline unsigned long pfn_to_block_id(unsigned long pfn)
  49. {
  50. return memory_block_id(pfn_to_section_nr(pfn));
  51. }
  52. static inline unsigned long phys_to_block_id(unsigned long phys)
  53. {
  54. return pfn_to_block_id(PFN_DOWN(phys));
  55. }
  56. static int memory_subsys_online(struct device *dev);
  57. static int memory_subsys_offline(struct device *dev);
  58. static const struct bus_type memory_subsys = {
  59. .name = MEMORY_CLASS_NAME,
  60. .dev_name = MEMORY_CLASS_NAME,
  61. .online = memory_subsys_online,
  62. .offline = memory_subsys_offline,
  63. };
  64. /*
  65. * Memory blocks are cached in a local radix tree to avoid
  66. * a costly linear search for the corresponding device on
  67. * the subsystem bus.
  68. */
  69. static DEFINE_XARRAY(memory_blocks);
  70. /*
  71. * Memory groups, indexed by memory group id (mgid).
  72. */
  73. static DEFINE_XARRAY_FLAGS(memory_groups, XA_FLAGS_ALLOC);
  74. #define MEMORY_GROUP_MARK_DYNAMIC XA_MARK_1
  75. static BLOCKING_NOTIFIER_HEAD(memory_chain);
  76. int register_memory_notifier(struct notifier_block *nb)
  77. {
  78. return blocking_notifier_chain_register(&memory_chain, nb);
  79. }
  80. EXPORT_SYMBOL(register_memory_notifier);
  81. void unregister_memory_notifier(struct notifier_block *nb)
  82. {
  83. blocking_notifier_chain_unregister(&memory_chain, nb);
  84. }
  85. EXPORT_SYMBOL(unregister_memory_notifier);
  86. static void memory_block_release(struct device *dev)
  87. {
  88. struct memory_block *mem = to_memory_block(dev);
  89. /* Verify that the altmap is freed */
  90. WARN_ON(mem->altmap);
  91. kfree(mem);
  92. }
  93. unsigned long __weak memory_block_size_bytes(void)
  94. {
  95. return MIN_MEMORY_BLOCK_SIZE;
  96. }
  97. EXPORT_SYMBOL_GPL(memory_block_size_bytes);
  98. /* Show the memory block ID, relative to the memory block size */
  99. static ssize_t phys_index_show(struct device *dev,
  100. struct device_attribute *attr, char *buf)
  101. {
  102. struct memory_block *mem = to_memory_block(dev);
  103. return sysfs_emit(buf, "%08lx\n", memory_block_id(mem->start_section_nr));
  104. }
  105. /*
  106. * Legacy interface that we cannot remove. Always indicate "removable"
  107. * with CONFIG_MEMORY_HOTREMOVE - bad heuristic.
  108. */
  109. static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
  110. char *buf)
  111. {
  112. return sysfs_emit(buf, "%d\n", (int)IS_ENABLED(CONFIG_MEMORY_HOTREMOVE));
  113. }
  114. /*
  115. * online, offline, going offline, etc.
  116. */
  117. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  118. char *buf)
  119. {
  120. struct memory_block *mem = to_memory_block(dev);
  121. const char *output;
  122. /*
  123. * We can probably put these states in a nice little array
  124. * so that they're not open-coded
  125. */
  126. switch (mem->state) {
  127. case MEM_ONLINE:
  128. output = "online";
  129. break;
  130. case MEM_OFFLINE:
  131. output = "offline";
  132. break;
  133. case MEM_GOING_OFFLINE:
  134. output = "going-offline";
  135. break;
  136. default:
  137. WARN_ON(1);
  138. return sysfs_emit(buf, "ERROR-UNKNOWN-%ld\n", mem->state);
  139. }
  140. return sysfs_emit(buf, "%s\n", output);
  141. }
  142. int memory_notify(unsigned long val, void *v)
  143. {
  144. return blocking_notifier_call_chain(&memory_chain, val, v);
  145. }
  146. #if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_MEMORY_HOTPLUG)
  147. static unsigned long memblk_nr_poison(struct memory_block *mem);
  148. #else
  149. static inline unsigned long memblk_nr_poison(struct memory_block *mem)
  150. {
  151. return 0;
  152. }
  153. #endif
  154. /*
  155. * Must acquire mem_hotplug_lock in write mode.
  156. */
  157. static int memory_block_online(struct memory_block *mem)
  158. {
  159. unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
  160. unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
  161. unsigned long nr_vmemmap_pages = 0;
  162. struct memory_notify arg;
  163. struct zone *zone;
  164. int ret;
  165. if (memblk_nr_poison(mem))
  166. return -EHWPOISON;
  167. zone = zone_for_pfn_range(mem->online_type, mem->nid, mem->group,
  168. start_pfn, nr_pages);
  169. /*
  170. * Although vmemmap pages have a different lifecycle than the pages
  171. * they describe (they remain until the memory is unplugged), doing
  172. * their initialization and accounting at memory onlining/offlining
  173. * stage helps to keep accounting easier to follow - e.g vmemmaps
  174. * belong to the same zone as the memory they backed.
  175. */
  176. if (mem->altmap)
  177. nr_vmemmap_pages = mem->altmap->free;
  178. arg.altmap_start_pfn = start_pfn;
  179. arg.altmap_nr_pages = nr_vmemmap_pages;
  180. arg.start_pfn = start_pfn + nr_vmemmap_pages;
  181. arg.nr_pages = nr_pages - nr_vmemmap_pages;
  182. mem_hotplug_begin();
  183. ret = memory_notify(MEM_PREPARE_ONLINE, &arg);
  184. ret = notifier_to_errno(ret);
  185. if (ret)
  186. goto out_notifier;
  187. if (nr_vmemmap_pages) {
  188. ret = mhp_init_memmap_on_memory(start_pfn, nr_vmemmap_pages,
  189. zone, mem->altmap->inaccessible);
  190. if (ret)
  191. goto out;
  192. }
  193. ret = online_pages(start_pfn + nr_vmemmap_pages,
  194. nr_pages - nr_vmemmap_pages, zone, mem->group);
  195. if (ret) {
  196. if (nr_vmemmap_pages)
  197. mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages);
  198. goto out;
  199. }
  200. /*
  201. * Account once onlining succeeded. If the zone was unpopulated, it is
  202. * now already properly populated.
  203. */
  204. if (nr_vmemmap_pages)
  205. adjust_present_page_count(pfn_to_page(start_pfn), mem->group,
  206. nr_vmemmap_pages);
  207. mem->zone = zone;
  208. mem_hotplug_done();
  209. return ret;
  210. out:
  211. memory_notify(MEM_FINISH_OFFLINE, &arg);
  212. out_notifier:
  213. mem_hotplug_done();
  214. return ret;
  215. }
  216. /*
  217. * Must acquire mem_hotplug_lock in write mode.
  218. */
  219. static int memory_block_offline(struct memory_block *mem)
  220. {
  221. unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
  222. unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
  223. unsigned long nr_vmemmap_pages = 0;
  224. struct memory_notify arg;
  225. int ret;
  226. if (!mem->zone)
  227. return -EINVAL;
  228. /*
  229. * Unaccount before offlining, such that unpopulated zone and kthreads
  230. * can properly be torn down in offline_pages().
  231. */
  232. if (mem->altmap)
  233. nr_vmemmap_pages = mem->altmap->free;
  234. mem_hotplug_begin();
  235. if (nr_vmemmap_pages)
  236. adjust_present_page_count(pfn_to_page(start_pfn), mem->group,
  237. -nr_vmemmap_pages);
  238. ret = offline_pages(start_pfn + nr_vmemmap_pages,
  239. nr_pages - nr_vmemmap_pages, mem->zone, mem->group);
  240. if (ret) {
  241. /* offline_pages() failed. Account back. */
  242. if (nr_vmemmap_pages)
  243. adjust_present_page_count(pfn_to_page(start_pfn),
  244. mem->group, nr_vmemmap_pages);
  245. goto out;
  246. }
  247. if (nr_vmemmap_pages)
  248. mhp_deinit_memmap_on_memory(start_pfn, nr_vmemmap_pages);
  249. mem->zone = NULL;
  250. arg.altmap_start_pfn = start_pfn;
  251. arg.altmap_nr_pages = nr_vmemmap_pages;
  252. arg.start_pfn = start_pfn + nr_vmemmap_pages;
  253. arg.nr_pages = nr_pages - nr_vmemmap_pages;
  254. memory_notify(MEM_FINISH_OFFLINE, &arg);
  255. out:
  256. mem_hotplug_done();
  257. return ret;
  258. }
  259. /*
  260. * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
  261. * OK to have direct references to sparsemem variables in here.
  262. */
  263. static int
  264. memory_block_action(struct memory_block *mem, unsigned long action)
  265. {
  266. int ret;
  267. switch (action) {
  268. case MEM_ONLINE:
  269. ret = memory_block_online(mem);
  270. break;
  271. case MEM_OFFLINE:
  272. ret = memory_block_offline(mem);
  273. break;
  274. default:
  275. WARN(1, KERN_WARNING "%s(%ld, %ld) unknown action: "
  276. "%ld\n", __func__, mem->start_section_nr, action, action);
  277. ret = -EINVAL;
  278. }
  279. return ret;
  280. }
  281. static int memory_block_change_state(struct memory_block *mem,
  282. unsigned long to_state, unsigned long from_state_req)
  283. {
  284. int ret = 0;
  285. if (mem->state != from_state_req)
  286. return -EINVAL;
  287. if (to_state == MEM_OFFLINE)
  288. mem->state = MEM_GOING_OFFLINE;
  289. ret = memory_block_action(mem, to_state);
  290. mem->state = ret ? from_state_req : to_state;
  291. return ret;
  292. }
  293. /* The device lock serializes operations on memory_subsys_[online|offline] */
  294. static int memory_subsys_online(struct device *dev)
  295. {
  296. struct memory_block *mem = to_memory_block(dev);
  297. int ret;
  298. if (mem->state == MEM_ONLINE)
  299. return 0;
  300. /*
  301. * When called via device_online() without configuring the online_type,
  302. * we want to default to MMOP_ONLINE.
  303. */
  304. if (mem->online_type == MMOP_OFFLINE)
  305. mem->online_type = MMOP_ONLINE;
  306. ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
  307. mem->online_type = MMOP_OFFLINE;
  308. return ret;
  309. }
  310. static int memory_subsys_offline(struct device *dev)
  311. {
  312. struct memory_block *mem = to_memory_block(dev);
  313. if (mem->state == MEM_OFFLINE)
  314. return 0;
  315. return memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
  316. }
  317. static ssize_t state_store(struct device *dev, struct device_attribute *attr,
  318. const char *buf, size_t count)
  319. {
  320. const int online_type = mhp_online_type_from_str(buf);
  321. struct memory_block *mem = to_memory_block(dev);
  322. int ret;
  323. if (online_type < 0)
  324. return -EINVAL;
  325. ret = lock_device_hotplug_sysfs();
  326. if (ret)
  327. return ret;
  328. switch (online_type) {
  329. case MMOP_ONLINE_KERNEL:
  330. case MMOP_ONLINE_MOVABLE:
  331. case MMOP_ONLINE:
  332. /* mem->online_type is protected by device_hotplug_lock */
  333. mem->online_type = online_type;
  334. ret = device_online(&mem->dev);
  335. break;
  336. case MMOP_OFFLINE:
  337. ret = device_offline(&mem->dev);
  338. break;
  339. default:
  340. ret = -EINVAL; /* should never happen */
  341. }
  342. unlock_device_hotplug();
  343. if (ret < 0)
  344. return ret;
  345. if (ret)
  346. return -EINVAL;
  347. return count;
  348. }
  349. /*
  350. * Legacy interface that we cannot remove: s390x exposes the storage increment
  351. * covered by a memory block, allowing for identifying which memory blocks
  352. * comprise a storage increment. Since a memory block spans complete
  353. * storage increments nowadays, this interface is basically unused. Other
  354. * archs never exposed != 0.
  355. */
  356. static ssize_t phys_device_show(struct device *dev,
  357. struct device_attribute *attr, char *buf)
  358. {
  359. struct memory_block *mem = to_memory_block(dev);
  360. unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
  361. return sysfs_emit(buf, "%d\n",
  362. arch_get_memory_phys_device(start_pfn));
  363. }
  364. #ifdef CONFIG_MEMORY_HOTREMOVE
  365. static int print_allowed_zone(char *buf, int len, int nid,
  366. struct memory_group *group,
  367. unsigned long start_pfn, unsigned long nr_pages,
  368. int online_type, struct zone *default_zone)
  369. {
  370. struct zone *zone;
  371. zone = zone_for_pfn_range(online_type, nid, group, start_pfn, nr_pages);
  372. if (zone == default_zone)
  373. return 0;
  374. return sysfs_emit_at(buf, len, " %s", zone->name);
  375. }
  376. static ssize_t valid_zones_show(struct device *dev,
  377. struct device_attribute *attr, char *buf)
  378. {
  379. struct memory_block *mem = to_memory_block(dev);
  380. unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
  381. unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
  382. struct memory_group *group = mem->group;
  383. struct zone *default_zone;
  384. int nid = mem->nid;
  385. int len = 0;
  386. /*
  387. * Check the existing zone. Make sure that we do that only on the
  388. * online nodes otherwise the page_zone is not reliable
  389. */
  390. if (mem->state == MEM_ONLINE) {
  391. /*
  392. * If !mem->zone, the memory block spans multiple zones and
  393. * cannot get offlined.
  394. */
  395. default_zone = mem->zone;
  396. if (!default_zone)
  397. return sysfs_emit(buf, "%s\n", "none");
  398. len += sysfs_emit_at(buf, len, "%s", default_zone->name);
  399. goto out;
  400. }
  401. default_zone = zone_for_pfn_range(MMOP_ONLINE, nid, group,
  402. start_pfn, nr_pages);
  403. len += sysfs_emit_at(buf, len, "%s", default_zone->name);
  404. len += print_allowed_zone(buf, len, nid, group, start_pfn, nr_pages,
  405. MMOP_ONLINE_KERNEL, default_zone);
  406. len += print_allowed_zone(buf, len, nid, group, start_pfn, nr_pages,
  407. MMOP_ONLINE_MOVABLE, default_zone);
  408. out:
  409. len += sysfs_emit_at(buf, len, "\n");
  410. return len;
  411. }
  412. static DEVICE_ATTR_RO(valid_zones);
  413. #endif
  414. static DEVICE_ATTR_RO(phys_index);
  415. static DEVICE_ATTR_RW(state);
  416. static DEVICE_ATTR_RO(phys_device);
  417. static DEVICE_ATTR_RO(removable);
  418. /*
  419. * Show the memory block size (shared by all memory blocks).
  420. */
  421. static ssize_t block_size_bytes_show(struct device *dev,
  422. struct device_attribute *attr, char *buf)
  423. {
  424. return sysfs_emit(buf, "%lx\n", memory_block_size_bytes());
  425. }
  426. static DEVICE_ATTR_RO(block_size_bytes);
  427. /*
  428. * Memory auto online policy.
  429. */
  430. static ssize_t auto_online_blocks_show(struct device *dev,
  431. struct device_attribute *attr, char *buf)
  432. {
  433. return sysfs_emit(buf, "%s\n",
  434. online_type_to_str[mhp_default_online_type]);
  435. }
  436. static ssize_t auto_online_blocks_store(struct device *dev,
  437. struct device_attribute *attr,
  438. const char *buf, size_t count)
  439. {
  440. const int online_type = mhp_online_type_from_str(buf);
  441. if (online_type < 0)
  442. return -EINVAL;
  443. mhp_default_online_type = online_type;
  444. return count;
  445. }
  446. static DEVICE_ATTR_RW(auto_online_blocks);
  447. #ifdef CONFIG_CRASH_HOTPLUG
  448. #include <linux/kexec.h>
  449. static ssize_t crash_hotplug_show(struct device *dev,
  450. struct device_attribute *attr, char *buf)
  451. {
  452. return sysfs_emit(buf, "%d\n", crash_check_hotplug_support());
  453. }
  454. static DEVICE_ATTR_RO(crash_hotplug);
  455. #endif
  456. /*
  457. * Some architectures will have custom drivers to do this, and
  458. * will not need to do it from userspace. The fake hot-add code
  459. * as well as ppc64 will do all of their discovery in userspace
  460. * and will require this interface.
  461. */
  462. #ifdef CONFIG_ARCH_MEMORY_PROBE
  463. static ssize_t probe_store(struct device *dev, struct device_attribute *attr,
  464. const char *buf, size_t count)
  465. {
  466. u64 phys_addr;
  467. int nid, ret;
  468. unsigned long pages_per_block = PAGES_PER_SECTION * sections_per_block;
  469. ret = kstrtoull(buf, 0, &phys_addr);
  470. if (ret)
  471. return ret;
  472. if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1))
  473. return -EINVAL;
  474. ret = lock_device_hotplug_sysfs();
  475. if (ret)
  476. return ret;
  477. nid = memory_add_physaddr_to_nid(phys_addr);
  478. ret = __add_memory(nid, phys_addr,
  479. MIN_MEMORY_BLOCK_SIZE * sections_per_block,
  480. MHP_NONE);
  481. if (ret)
  482. goto out;
  483. ret = count;
  484. out:
  485. unlock_device_hotplug();
  486. return ret;
  487. }
  488. static DEVICE_ATTR_WO(probe);
  489. #endif
  490. #ifdef CONFIG_MEMORY_FAILURE
  491. /*
  492. * Support for offlining pages of memory
  493. */
  494. /* Soft offline a page */
  495. static ssize_t soft_offline_page_store(struct device *dev,
  496. struct device_attribute *attr,
  497. const char *buf, size_t count)
  498. {
  499. int ret;
  500. u64 pfn;
  501. if (!capable(CAP_SYS_ADMIN))
  502. return -EPERM;
  503. if (kstrtoull(buf, 0, &pfn) < 0)
  504. return -EINVAL;
  505. pfn >>= PAGE_SHIFT;
  506. ret = soft_offline_page(pfn, 0);
  507. return ret == 0 ? count : ret;
  508. }
  509. /* Forcibly offline a page, including killing processes. */
  510. static ssize_t hard_offline_page_store(struct device *dev,
  511. struct device_attribute *attr,
  512. const char *buf, size_t count)
  513. {
  514. int ret;
  515. u64 pfn;
  516. if (!capable(CAP_SYS_ADMIN))
  517. return -EPERM;
  518. if (kstrtoull(buf, 0, &pfn) < 0)
  519. return -EINVAL;
  520. pfn >>= PAGE_SHIFT;
  521. ret = memory_failure(pfn, MF_SW_SIMULATED);
  522. if (ret == -EOPNOTSUPP)
  523. ret = 0;
  524. return ret ? ret : count;
  525. }
  526. static DEVICE_ATTR_WO(soft_offline_page);
  527. static DEVICE_ATTR_WO(hard_offline_page);
  528. #endif
  529. /* See phys_device_show(). */
  530. int __weak arch_get_memory_phys_device(unsigned long start_pfn)
  531. {
  532. return 0;
  533. }
  534. /*
  535. * A reference for the returned memory block device is acquired.
  536. *
  537. * Called under device_hotplug_lock.
  538. */
  539. static struct memory_block *find_memory_block_by_id(unsigned long block_id)
  540. {
  541. struct memory_block *mem;
  542. mem = xa_load(&memory_blocks, block_id);
  543. if (mem)
  544. get_device(&mem->dev);
  545. return mem;
  546. }
  547. /*
  548. * Called under device_hotplug_lock.
  549. */
  550. struct memory_block *find_memory_block(unsigned long section_nr)
  551. {
  552. unsigned long block_id = memory_block_id(section_nr);
  553. return find_memory_block_by_id(block_id);
  554. }
  555. static struct attribute *memory_memblk_attrs[] = {
  556. &dev_attr_phys_index.attr,
  557. &dev_attr_state.attr,
  558. &dev_attr_phys_device.attr,
  559. &dev_attr_removable.attr,
  560. #ifdef CONFIG_MEMORY_HOTREMOVE
  561. &dev_attr_valid_zones.attr,
  562. #endif
  563. NULL
  564. };
  565. static const struct attribute_group memory_memblk_attr_group = {
  566. .attrs = memory_memblk_attrs,
  567. };
  568. static const struct attribute_group *memory_memblk_attr_groups[] = {
  569. &memory_memblk_attr_group,
  570. NULL,
  571. };
  572. static int __add_memory_block(struct memory_block *memory)
  573. {
  574. int ret;
  575. memory->dev.bus = &memory_subsys;
  576. memory->dev.id = memory->start_section_nr / sections_per_block;
  577. memory->dev.release = memory_block_release;
  578. memory->dev.groups = memory_memblk_attr_groups;
  579. memory->dev.offline = memory->state == MEM_OFFLINE;
  580. ret = device_register(&memory->dev);
  581. if (ret) {
  582. put_device(&memory->dev);
  583. return ret;
  584. }
  585. ret = xa_err(xa_store(&memory_blocks, memory->dev.id, memory,
  586. GFP_KERNEL));
  587. if (ret)
  588. device_unregister(&memory->dev);
  589. return ret;
  590. }
  591. static struct zone *early_node_zone_for_memory_block(struct memory_block *mem,
  592. int nid)
  593. {
  594. const unsigned long start_pfn = section_nr_to_pfn(mem->start_section_nr);
  595. const unsigned long nr_pages = PAGES_PER_SECTION * sections_per_block;
  596. struct zone *zone, *matching_zone = NULL;
  597. pg_data_t *pgdat = NODE_DATA(nid);
  598. int i;
  599. /*
  600. * This logic only works for early memory, when the applicable zones
  601. * already span the memory block. We don't expect overlapping zones on
  602. * a single node for early memory. So if we're told that some PFNs
  603. * of a node fall into this memory block, we can assume that all node
  604. * zones that intersect with the memory block are actually applicable.
  605. * No need to look at the memmap.
  606. */
  607. for (i = 0; i < MAX_NR_ZONES; i++) {
  608. zone = pgdat->node_zones + i;
  609. if (!populated_zone(zone))
  610. continue;
  611. if (!zone_intersects(zone, start_pfn, nr_pages))
  612. continue;
  613. if (!matching_zone) {
  614. matching_zone = zone;
  615. continue;
  616. }
  617. /* Spans multiple zones ... */
  618. matching_zone = NULL;
  619. break;
  620. }
  621. return matching_zone;
  622. }
  623. #ifdef CONFIG_NUMA
  624. /**
  625. * memory_block_add_nid() - Indicate that system RAM falling into this memory
  626. * block device (partially) belongs to the given node.
  627. * @mem: The memory block device.
  628. * @nid: The node id.
  629. * @context: The memory initialization context.
  630. *
  631. * Indicate that system RAM falling into this memory block (partially) belongs
  632. * to the given node. If the context indicates ("early") that we are adding the
  633. * node during node device subsystem initialization, this will also properly
  634. * set/adjust mem->zone based on the zone ranges of the given node.
  635. */
  636. void memory_block_add_nid(struct memory_block *mem, int nid,
  637. enum meminit_context context)
  638. {
  639. if (context == MEMINIT_EARLY && mem->nid != nid) {
  640. /*
  641. * For early memory we have to determine the zone when setting
  642. * the node id and handle multiple nodes spanning a single
  643. * memory block by indicate via zone == NULL that we're not
  644. * dealing with a single zone. So if we're setting the node id
  645. * the first time, determine if there is a single zone. If we're
  646. * setting the node id a second time to a different node,
  647. * invalidate the single detected zone.
  648. */
  649. if (mem->nid == NUMA_NO_NODE)
  650. mem->zone = early_node_zone_for_memory_block(mem, nid);
  651. else
  652. mem->zone = NULL;
  653. }
  654. /*
  655. * If this memory block spans multiple nodes, we only indicate
  656. * the last processed node. If we span multiple nodes (not applicable
  657. * to hotplugged memory), zone == NULL will prohibit memory offlining
  658. * and consequently unplug.
  659. */
  660. mem->nid = nid;
  661. }
  662. #endif
  663. static int add_memory_block(unsigned long block_id, unsigned long state,
  664. struct vmem_altmap *altmap,
  665. struct memory_group *group)
  666. {
  667. struct memory_block *mem;
  668. int ret = 0;
  669. mem = find_memory_block_by_id(block_id);
  670. if (mem) {
  671. put_device(&mem->dev);
  672. return -EEXIST;
  673. }
  674. mem = kzalloc(sizeof(*mem), GFP_KERNEL);
  675. if (!mem)
  676. return -ENOMEM;
  677. mem->start_section_nr = block_id * sections_per_block;
  678. mem->state = state;
  679. mem->nid = NUMA_NO_NODE;
  680. mem->altmap = altmap;
  681. INIT_LIST_HEAD(&mem->group_next);
  682. #ifndef CONFIG_NUMA
  683. if (state == MEM_ONLINE)
  684. /*
  685. * MEM_ONLINE at this point implies early memory. With NUMA,
  686. * we'll determine the zone when setting the node id via
  687. * memory_block_add_nid(). Memory hotplug updated the zone
  688. * manually when memory onlining/offlining succeeds.
  689. */
  690. mem->zone = early_node_zone_for_memory_block(mem, NUMA_NO_NODE);
  691. #endif /* CONFIG_NUMA */
  692. ret = __add_memory_block(mem);
  693. if (ret)
  694. return ret;
  695. if (group) {
  696. mem->group = group;
  697. list_add(&mem->group_next, &group->memory_blocks);
  698. }
  699. return 0;
  700. }
  701. static int __init add_boot_memory_block(unsigned long base_section_nr)
  702. {
  703. int section_count = 0;
  704. unsigned long nr;
  705. for (nr = base_section_nr; nr < base_section_nr + sections_per_block;
  706. nr++)
  707. if (present_section_nr(nr))
  708. section_count++;
  709. if (section_count == 0)
  710. return 0;
  711. return add_memory_block(memory_block_id(base_section_nr),
  712. MEM_ONLINE, NULL, NULL);
  713. }
  714. static int add_hotplug_memory_block(unsigned long block_id,
  715. struct vmem_altmap *altmap,
  716. struct memory_group *group)
  717. {
  718. return add_memory_block(block_id, MEM_OFFLINE, altmap, group);
  719. }
  720. static void remove_memory_block(struct memory_block *memory)
  721. {
  722. if (WARN_ON_ONCE(memory->dev.bus != &memory_subsys))
  723. return;
  724. WARN_ON(xa_erase(&memory_blocks, memory->dev.id) == NULL);
  725. if (memory->group) {
  726. list_del(&memory->group_next);
  727. memory->group = NULL;
  728. }
  729. /* drop the ref. we got via find_memory_block() */
  730. put_device(&memory->dev);
  731. device_unregister(&memory->dev);
  732. }
  733. /*
  734. * Create memory block devices for the given memory area. Start and size
  735. * have to be aligned to memory block granularity. Memory block devices
  736. * will be initialized as offline.
  737. *
  738. * Called under device_hotplug_lock.
  739. */
  740. int create_memory_block_devices(unsigned long start, unsigned long size,
  741. struct vmem_altmap *altmap,
  742. struct memory_group *group)
  743. {
  744. const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
  745. unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
  746. struct memory_block *mem;
  747. unsigned long block_id;
  748. int ret = 0;
  749. if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
  750. !IS_ALIGNED(size, memory_block_size_bytes())))
  751. return -EINVAL;
  752. for (block_id = start_block_id; block_id != end_block_id; block_id++) {
  753. ret = add_hotplug_memory_block(block_id, altmap, group);
  754. if (ret)
  755. break;
  756. }
  757. if (ret) {
  758. end_block_id = block_id;
  759. for (block_id = start_block_id; block_id != end_block_id;
  760. block_id++) {
  761. mem = find_memory_block_by_id(block_id);
  762. if (WARN_ON_ONCE(!mem))
  763. continue;
  764. remove_memory_block(mem);
  765. }
  766. }
  767. return ret;
  768. }
  769. /*
  770. * Remove memory block devices for the given memory area. Start and size
  771. * have to be aligned to memory block granularity. Memory block devices
  772. * have to be offline.
  773. *
  774. * Called under device_hotplug_lock.
  775. */
  776. void remove_memory_block_devices(unsigned long start, unsigned long size)
  777. {
  778. const unsigned long start_block_id = pfn_to_block_id(PFN_DOWN(start));
  779. const unsigned long end_block_id = pfn_to_block_id(PFN_DOWN(start + size));
  780. struct memory_block *mem;
  781. unsigned long block_id;
  782. if (WARN_ON_ONCE(!IS_ALIGNED(start, memory_block_size_bytes()) ||
  783. !IS_ALIGNED(size, memory_block_size_bytes())))
  784. return;
  785. for (block_id = start_block_id; block_id != end_block_id; block_id++) {
  786. mem = find_memory_block_by_id(block_id);
  787. if (WARN_ON_ONCE(!mem))
  788. continue;
  789. num_poisoned_pages_sub(-1UL, memblk_nr_poison(mem));
  790. unregister_memory_block_under_nodes(mem);
  791. remove_memory_block(mem);
  792. }
  793. }
  794. static struct attribute *memory_root_attrs[] = {
  795. #ifdef CONFIG_ARCH_MEMORY_PROBE
  796. &dev_attr_probe.attr,
  797. #endif
  798. #ifdef CONFIG_MEMORY_FAILURE
  799. &dev_attr_soft_offline_page.attr,
  800. &dev_attr_hard_offline_page.attr,
  801. #endif
  802. &dev_attr_block_size_bytes.attr,
  803. &dev_attr_auto_online_blocks.attr,
  804. #ifdef CONFIG_CRASH_HOTPLUG
  805. &dev_attr_crash_hotplug.attr,
  806. #endif
  807. NULL
  808. };
  809. static const struct attribute_group memory_root_attr_group = {
  810. .attrs = memory_root_attrs,
  811. };
  812. static const struct attribute_group *memory_root_attr_groups[] = {
  813. &memory_root_attr_group,
  814. NULL,
  815. };
  816. /*
  817. * Initialize the sysfs support for memory devices. At the time this function
  818. * is called, we cannot have concurrent creation/deletion of memory block
  819. * devices, the device_hotplug_lock is not needed.
  820. */
  821. void __init memory_dev_init(void)
  822. {
  823. int ret;
  824. unsigned long block_sz, nr;
  825. /* Validate the configured memory block size */
  826. block_sz = memory_block_size_bytes();
  827. if (!is_power_of_2(block_sz) || block_sz < MIN_MEMORY_BLOCK_SIZE)
  828. panic("Memory block size not suitable: 0x%lx\n", block_sz);
  829. sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
  830. ret = subsys_system_register(&memory_subsys, memory_root_attr_groups);
  831. if (ret)
  832. panic("%s() failed to register subsystem: %d\n", __func__, ret);
  833. /*
  834. * Create entries for memory sections that were found
  835. * during boot and have been initialized
  836. */
  837. for (nr = 0; nr <= __highest_present_section_nr;
  838. nr += sections_per_block) {
  839. ret = add_boot_memory_block(nr);
  840. if (ret)
  841. panic("%s() failed to add memory block: %d\n", __func__,
  842. ret);
  843. }
  844. }
  845. /**
  846. * walk_memory_blocks - walk through all present memory blocks overlapped
  847. * by the range [start, start + size)
  848. *
  849. * @start: start address of the memory range
  850. * @size: size of the memory range
  851. * @arg: argument passed to func
  852. * @func: callback for each memory section walked
  853. *
  854. * This function walks through all present memory blocks overlapped by the
  855. * range [start, start + size), calling func on each memory block.
  856. *
  857. * In case func() returns an error, walking is aborted and the error is
  858. * returned.
  859. *
  860. * Called under device_hotplug_lock.
  861. */
  862. int walk_memory_blocks(unsigned long start, unsigned long size,
  863. void *arg, walk_memory_blocks_func_t func)
  864. {
  865. const unsigned long start_block_id = phys_to_block_id(start);
  866. const unsigned long end_block_id = phys_to_block_id(start + size - 1);
  867. struct memory_block *mem;
  868. unsigned long block_id;
  869. int ret = 0;
  870. if (!size)
  871. return 0;
  872. for (block_id = start_block_id; block_id <= end_block_id; block_id++) {
  873. mem = find_memory_block_by_id(block_id);
  874. if (!mem)
  875. continue;
  876. ret = func(mem, arg);
  877. put_device(&mem->dev);
  878. if (ret)
  879. break;
  880. }
  881. return ret;
  882. }
  883. struct for_each_memory_block_cb_data {
  884. walk_memory_blocks_func_t func;
  885. void *arg;
  886. };
  887. static int for_each_memory_block_cb(struct device *dev, void *data)
  888. {
  889. struct memory_block *mem = to_memory_block(dev);
  890. struct for_each_memory_block_cb_data *cb_data = data;
  891. return cb_data->func(mem, cb_data->arg);
  892. }
  893. /**
  894. * for_each_memory_block - walk through all present memory blocks
  895. *
  896. * @arg: argument passed to func
  897. * @func: callback for each memory block walked
  898. *
  899. * This function walks through all present memory blocks, calling func on
  900. * each memory block.
  901. *
  902. * In case func() returns an error, walking is aborted and the error is
  903. * returned.
  904. */
  905. int for_each_memory_block(void *arg, walk_memory_blocks_func_t func)
  906. {
  907. struct for_each_memory_block_cb_data cb_data = {
  908. .func = func,
  909. .arg = arg,
  910. };
  911. return bus_for_each_dev(&memory_subsys, NULL, &cb_data,
  912. for_each_memory_block_cb);
  913. }
  914. /*
  915. * This is an internal helper to unify allocation and initialization of
  916. * memory groups. Note that the passed memory group will be copied to a
  917. * dynamically allocated memory group. After this call, the passed
  918. * memory group should no longer be used.
  919. */
  920. static int memory_group_register(struct memory_group group)
  921. {
  922. struct memory_group *new_group;
  923. uint32_t mgid;
  924. int ret;
  925. if (!node_possible(group.nid))
  926. return -EINVAL;
  927. new_group = kzalloc(sizeof(group), GFP_KERNEL);
  928. if (!new_group)
  929. return -ENOMEM;
  930. *new_group = group;
  931. INIT_LIST_HEAD(&new_group->memory_blocks);
  932. ret = xa_alloc(&memory_groups, &mgid, new_group, xa_limit_31b,
  933. GFP_KERNEL);
  934. if (ret) {
  935. kfree(new_group);
  936. return ret;
  937. } else if (group.is_dynamic) {
  938. xa_set_mark(&memory_groups, mgid, MEMORY_GROUP_MARK_DYNAMIC);
  939. }
  940. return mgid;
  941. }
  942. /**
  943. * memory_group_register_static() - Register a static memory group.
  944. * @nid: The node id.
  945. * @max_pages: The maximum number of pages we'll have in this static memory
  946. * group.
  947. *
  948. * Register a new static memory group and return the memory group id.
  949. * All memory in the group belongs to a single unit, such as a DIMM. All
  950. * memory belonging to a static memory group is added in one go to be removed
  951. * in one go -- it's static.
  952. *
  953. * Returns an error if out of memory, if the node id is invalid, if no new
  954. * memory groups can be registered, or if max_pages is invalid (0). Otherwise,
  955. * returns the new memory group id.
  956. */
  957. int memory_group_register_static(int nid, unsigned long max_pages)
  958. {
  959. struct memory_group group = {
  960. .nid = nid,
  961. .s = {
  962. .max_pages = max_pages,
  963. },
  964. };
  965. if (!max_pages)
  966. return -EINVAL;
  967. return memory_group_register(group);
  968. }
  969. EXPORT_SYMBOL_GPL(memory_group_register_static);
  970. /**
  971. * memory_group_register_dynamic() - Register a dynamic memory group.
  972. * @nid: The node id.
  973. * @unit_pages: Unit in pages in which is memory added/removed in this dynamic
  974. * memory group.
  975. *
  976. * Register a new dynamic memory group and return the memory group id.
  977. * Memory within a dynamic memory group is added/removed dynamically
  978. * in unit_pages.
  979. *
  980. * Returns an error if out of memory, if the node id is invalid, if no new
  981. * memory groups can be registered, or if unit_pages is invalid (0, not a
  982. * power of two, smaller than a single memory block). Otherwise, returns the
  983. * new memory group id.
  984. */
  985. int memory_group_register_dynamic(int nid, unsigned long unit_pages)
  986. {
  987. struct memory_group group = {
  988. .nid = nid,
  989. .is_dynamic = true,
  990. .d = {
  991. .unit_pages = unit_pages,
  992. },
  993. };
  994. if (!unit_pages || !is_power_of_2(unit_pages) ||
  995. unit_pages < PHYS_PFN(memory_block_size_bytes()))
  996. return -EINVAL;
  997. return memory_group_register(group);
  998. }
  999. EXPORT_SYMBOL_GPL(memory_group_register_dynamic);
  1000. /**
  1001. * memory_group_unregister() - Unregister a memory group.
  1002. * @mgid: the memory group id
  1003. *
  1004. * Unregister a memory group. If any memory block still belongs to this
  1005. * memory group, unregistering will fail.
  1006. *
  1007. * Returns -EINVAL if the memory group id is invalid, returns -EBUSY if some
  1008. * memory blocks still belong to this memory group and returns 0 if
  1009. * unregistering succeeded.
  1010. */
  1011. int memory_group_unregister(int mgid)
  1012. {
  1013. struct memory_group *group;
  1014. if (mgid < 0)
  1015. return -EINVAL;
  1016. group = xa_load(&memory_groups, mgid);
  1017. if (!group)
  1018. return -EINVAL;
  1019. if (!list_empty(&group->memory_blocks))
  1020. return -EBUSY;
  1021. xa_erase(&memory_groups, mgid);
  1022. kfree(group);
  1023. return 0;
  1024. }
  1025. EXPORT_SYMBOL_GPL(memory_group_unregister);
  1026. /*
  1027. * This is an internal helper only to be used in core memory hotplug code to
  1028. * lookup a memory group. We don't care about locking, as we don't expect a
  1029. * memory group to get unregistered while adding memory to it -- because
  1030. * the group and the memory is managed by the same driver.
  1031. */
  1032. struct memory_group *memory_group_find_by_id(int mgid)
  1033. {
  1034. return xa_load(&memory_groups, mgid);
  1035. }
  1036. /*
  1037. * This is an internal helper only to be used in core memory hotplug code to
  1038. * walk all dynamic memory groups excluding a given memory group, either
  1039. * belonging to a specific node, or belonging to any node.
  1040. */
  1041. int walk_dynamic_memory_groups(int nid, walk_memory_groups_func_t func,
  1042. struct memory_group *excluded, void *arg)
  1043. {
  1044. struct memory_group *group;
  1045. unsigned long index;
  1046. int ret = 0;
  1047. xa_for_each_marked(&memory_groups, index, group,
  1048. MEMORY_GROUP_MARK_DYNAMIC) {
  1049. if (group == excluded)
  1050. continue;
  1051. #ifdef CONFIG_NUMA
  1052. if (nid != NUMA_NO_NODE && group->nid != nid)
  1053. continue;
  1054. #endif /* CONFIG_NUMA */
  1055. ret = func(group, arg);
  1056. if (ret)
  1057. break;
  1058. }
  1059. return ret;
  1060. }
  1061. #if defined(CONFIG_MEMORY_FAILURE) && defined(CONFIG_MEMORY_HOTPLUG)
  1062. void memblk_nr_poison_inc(unsigned long pfn)
  1063. {
  1064. const unsigned long block_id = pfn_to_block_id(pfn);
  1065. struct memory_block *mem = find_memory_block_by_id(block_id);
  1066. if (mem)
  1067. atomic_long_inc(&mem->nr_hwpoison);
  1068. }
  1069. void memblk_nr_poison_sub(unsigned long pfn, long i)
  1070. {
  1071. const unsigned long block_id = pfn_to_block_id(pfn);
  1072. struct memory_block *mem = find_memory_block_by_id(block_id);
  1073. if (mem)
  1074. atomic_long_sub(i, &mem->nr_hwpoison);
  1075. }
  1076. static unsigned long memblk_nr_poison(struct memory_block *mem)
  1077. {
  1078. return atomic_long_read(&mem->nr_hwpoison);
  1079. }
  1080. #endif