memory.c 20 KB

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