unpopulated-alloc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/errno.h>
  3. #include <linux/gfp.h>
  4. #include <linux/kernel.h>
  5. #include <linux/mm.h>
  6. #include <linux/memremap.h>
  7. #include <linux/slab.h>
  8. #include <asm/page.h>
  9. #include <xen/balloon.h>
  10. #include <xen/page.h>
  11. #include <xen/xen.h>
  12. static DEFINE_MUTEX(list_lock);
  13. static struct page *page_list;
  14. static unsigned int list_count;
  15. static struct resource *target_resource;
  16. /* Pages to subtract from the memory count when setting balloon target. */
  17. unsigned long xen_unpopulated_pages __initdata;
  18. /*
  19. * If arch is not happy with system "iomem_resource" being used for
  20. * the region allocation it can provide it's own view by creating specific
  21. * Xen resource with unused regions of guest physical address space provided
  22. * by the hypervisor.
  23. */
  24. int __weak __init arch_xen_unpopulated_init(struct resource **res)
  25. {
  26. *res = &iomem_resource;
  27. return 0;
  28. }
  29. static int fill_list(unsigned int nr_pages)
  30. {
  31. struct dev_pagemap *pgmap;
  32. struct resource *res, *tmp_res = NULL;
  33. void *vaddr;
  34. unsigned int i, alloc_pages = round_up(nr_pages, PAGES_PER_SECTION);
  35. struct range mhp_range;
  36. int ret;
  37. res = kzalloc_obj(*res);
  38. if (!res)
  39. return -ENOMEM;
  40. res->name = "Xen scratch";
  41. res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  42. mhp_range = mhp_get_pluggable_range(true);
  43. ret = allocate_resource(target_resource, res,
  44. alloc_pages * PAGE_SIZE, mhp_range.start, mhp_range.end,
  45. PAGES_PER_SECTION * PAGE_SIZE, NULL, NULL);
  46. if (ret < 0) {
  47. pr_err("Cannot allocate new IOMEM resource\n");
  48. goto err_resource;
  49. }
  50. /*
  51. * Reserve the region previously allocated from Xen resource to avoid
  52. * re-using it by someone else.
  53. */
  54. if (target_resource != &iomem_resource) {
  55. tmp_res = kzalloc_obj(*tmp_res);
  56. if (!tmp_res) {
  57. ret = -ENOMEM;
  58. goto err_insert;
  59. }
  60. tmp_res->name = res->name;
  61. tmp_res->start = res->start;
  62. tmp_res->end = res->end;
  63. tmp_res->flags = res->flags;
  64. ret = request_resource(&iomem_resource, tmp_res);
  65. if (ret < 0) {
  66. pr_err("Cannot request resource %pR (%d)\n", tmp_res, ret);
  67. kfree(tmp_res);
  68. goto err_insert;
  69. }
  70. }
  71. pgmap = kzalloc_obj(*pgmap);
  72. if (!pgmap) {
  73. ret = -ENOMEM;
  74. goto err_pgmap;
  75. }
  76. pgmap->type = MEMORY_DEVICE_GENERIC;
  77. pgmap->range = (struct range) {
  78. .start = res->start,
  79. .end = res->end,
  80. };
  81. pgmap->nr_range = 1;
  82. pgmap->owner = res;
  83. #ifdef CONFIG_XEN_HAVE_PVMMU
  84. /*
  85. * memremap will build page tables for the new memory so
  86. * the p2m must contain invalid entries so the correct
  87. * non-present PTEs will be written.
  88. *
  89. * If a failure occurs, the original (identity) p2m entries
  90. * are not restored since this region is now known not to
  91. * conflict with any devices.
  92. */
  93. if (xen_pv_domain()) {
  94. xen_pfn_t pfn = PFN_DOWN(res->start);
  95. for (i = 0; i < alloc_pages; i++) {
  96. if (!set_phys_to_machine(pfn + i, INVALID_P2M_ENTRY)) {
  97. pr_warn("set_phys_to_machine() failed, no memory added\n");
  98. ret = -ENOMEM;
  99. goto err_memremap;
  100. }
  101. }
  102. }
  103. #endif
  104. vaddr = memremap_pages(pgmap, NUMA_NO_NODE);
  105. if (IS_ERR(vaddr)) {
  106. pr_err("Cannot remap memory range\n");
  107. ret = PTR_ERR(vaddr);
  108. goto err_memremap;
  109. }
  110. for (i = 0; i < alloc_pages; i++) {
  111. struct page *pg = virt_to_page(vaddr + PAGE_SIZE * i);
  112. pg->zone_device_data = page_list;
  113. page_list = pg;
  114. list_count++;
  115. }
  116. return 0;
  117. err_memremap:
  118. kfree(pgmap);
  119. err_pgmap:
  120. if (tmp_res) {
  121. release_resource(tmp_res);
  122. kfree(tmp_res);
  123. }
  124. err_insert:
  125. release_resource(res);
  126. err_resource:
  127. kfree(res);
  128. return ret;
  129. }
  130. /**
  131. * xen_alloc_unpopulated_pages - alloc unpopulated pages
  132. * @nr_pages: Number of pages
  133. * @pages: pages returned
  134. * @return 0 on success, error otherwise
  135. */
  136. int xen_alloc_unpopulated_pages(unsigned int nr_pages, struct page **pages)
  137. {
  138. unsigned int i;
  139. int ret = 0;
  140. /*
  141. * Fallback to default behavior if we do not have any suitable resource
  142. * to allocate required region from and as the result we won't be able to
  143. * construct pages.
  144. */
  145. if (!target_resource)
  146. return xen_alloc_ballooned_pages(nr_pages, pages);
  147. mutex_lock(&list_lock);
  148. if (list_count < nr_pages) {
  149. ret = fill_list(nr_pages - list_count);
  150. if (ret)
  151. goto out;
  152. }
  153. for (i = 0; i < nr_pages; i++) {
  154. struct page *pg = page_list;
  155. BUG_ON(!pg);
  156. page_list = pg->zone_device_data;
  157. list_count--;
  158. pages[i] = pg;
  159. #ifdef CONFIG_XEN_HAVE_PVMMU
  160. if (xen_pv_domain()) {
  161. ret = xen_alloc_p2m_entry(page_to_pfn(pg));
  162. if (ret < 0) {
  163. unsigned int j;
  164. for (j = 0; j <= i; j++) {
  165. pages[j]->zone_device_data = page_list;
  166. page_list = pages[j];
  167. list_count++;
  168. }
  169. goto out;
  170. }
  171. }
  172. #endif
  173. }
  174. out:
  175. mutex_unlock(&list_lock);
  176. return ret;
  177. }
  178. EXPORT_SYMBOL(xen_alloc_unpopulated_pages);
  179. /**
  180. * xen_free_unpopulated_pages - return unpopulated pages
  181. * @nr_pages: Number of pages
  182. * @pages: pages to return
  183. */
  184. void xen_free_unpopulated_pages(unsigned int nr_pages, struct page **pages)
  185. {
  186. unsigned int i;
  187. if (!target_resource) {
  188. xen_free_ballooned_pages(nr_pages, pages);
  189. return;
  190. }
  191. mutex_lock(&list_lock);
  192. for (i = 0; i < nr_pages; i++) {
  193. pages[i]->zone_device_data = page_list;
  194. page_list = pages[i];
  195. list_count++;
  196. }
  197. mutex_unlock(&list_lock);
  198. }
  199. EXPORT_SYMBOL(xen_free_unpopulated_pages);
  200. static int __init unpopulated_init(void)
  201. {
  202. int ret;
  203. if (!xen_domain())
  204. return -ENODEV;
  205. ret = arch_xen_unpopulated_init(&target_resource);
  206. if (ret) {
  207. pr_err("xen:unpopulated: Cannot initialize target resource\n");
  208. target_resource = NULL;
  209. }
  210. return ret;
  211. }
  212. early_initcall(unpopulated_init);