memory.c 20 KB

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