memremap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright(c) 2015 Intel Corporation. All rights reserved. */
  3. #include <linux/radix-tree.h>
  4. #include <linux/device.h>
  5. #include <linux/types.h>
  6. #include <linux/pfn_t.h>
  7. #include <linux/io.h>
  8. #include <linux/kasan.h>
  9. #include <linux/mm.h>
  10. #include <linux/memory_hotplug.h>
  11. #include <linux/swap.h>
  12. #include <linux/swapops.h>
  13. #include <linux/wait_bit.h>
  14. static DEFINE_MUTEX(pgmap_lock);
  15. static RADIX_TREE(pgmap_radix, GFP_KERNEL);
  16. #define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1)
  17. #define SECTION_SIZE (1UL << PA_SECTION_SHIFT)
  18. static unsigned long order_at(struct resource *res, unsigned long pgoff)
  19. {
  20. unsigned long phys_pgoff = PHYS_PFN(res->start) + pgoff;
  21. unsigned long nr_pages, mask;
  22. nr_pages = PHYS_PFN(resource_size(res));
  23. if (nr_pages == pgoff)
  24. return ULONG_MAX;
  25. /*
  26. * What is the largest aligned power-of-2 range available from
  27. * this resource pgoff to the end of the resource range,
  28. * considering the alignment of the current pgoff?
  29. */
  30. mask = phys_pgoff | rounddown_pow_of_two(nr_pages - pgoff);
  31. if (!mask)
  32. return ULONG_MAX;
  33. return find_first_bit(&mask, BITS_PER_LONG);
  34. }
  35. #define foreach_order_pgoff(res, order, pgoff) \
  36. for (pgoff = 0, order = order_at((res), pgoff); order < ULONG_MAX; \
  37. pgoff += 1UL << order, order = order_at((res), pgoff))
  38. #if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
  39. vm_fault_t device_private_entry_fault(struct vm_area_struct *vma,
  40. unsigned long addr,
  41. swp_entry_t entry,
  42. unsigned int flags,
  43. pmd_t *pmdp)
  44. {
  45. struct page *page = device_private_entry_to_page(entry);
  46. /*
  47. * The page_fault() callback must migrate page back to system memory
  48. * so that CPU can access it. This might fail for various reasons
  49. * (device issue, device was unsafely unplugged, ...). When such
  50. * error conditions happen, the callback must return VM_FAULT_SIGBUS.
  51. *
  52. * Note that because memory cgroup charges are accounted to the device
  53. * memory, this should never fail because of memory restrictions (but
  54. * allocation of regular system page might still fail because we are
  55. * out of memory).
  56. *
  57. * There is a more in-depth description of what that callback can and
  58. * cannot do, in include/linux/memremap.h
  59. */
  60. return page->pgmap->page_fault(vma, addr, page, flags, pmdp);
  61. }
  62. EXPORT_SYMBOL(device_private_entry_fault);
  63. #endif /* CONFIG_DEVICE_PRIVATE */
  64. static void pgmap_radix_release(struct resource *res, unsigned long end_pgoff)
  65. {
  66. unsigned long pgoff, order;
  67. mutex_lock(&pgmap_lock);
  68. foreach_order_pgoff(res, order, pgoff) {
  69. if (pgoff >= end_pgoff)
  70. break;
  71. radix_tree_delete(&pgmap_radix, PHYS_PFN(res->start) + pgoff);
  72. }
  73. mutex_unlock(&pgmap_lock);
  74. synchronize_rcu();
  75. }
  76. static unsigned long pfn_first(struct dev_pagemap *pgmap)
  77. {
  78. const struct resource *res = &pgmap->res;
  79. struct vmem_altmap *altmap = &pgmap->altmap;
  80. unsigned long pfn;
  81. pfn = res->start >> PAGE_SHIFT;
  82. if (pgmap->altmap_valid)
  83. pfn += vmem_altmap_offset(altmap);
  84. return pfn;
  85. }
  86. static unsigned long pfn_end(struct dev_pagemap *pgmap)
  87. {
  88. const struct resource *res = &pgmap->res;
  89. return (res->start + resource_size(res)) >> PAGE_SHIFT;
  90. }
  91. static unsigned long pfn_next(unsigned long pfn)
  92. {
  93. if (pfn % 1024 == 0)
  94. cond_resched();
  95. return pfn + 1;
  96. }
  97. #define for_each_device_pfn(pfn, map) \
  98. for (pfn = pfn_first(map); pfn < pfn_end(map); pfn = pfn_next(pfn))
  99. static void devm_memremap_pages_release(void *data)
  100. {
  101. struct dev_pagemap *pgmap = data;
  102. struct device *dev = pgmap->dev;
  103. struct resource *res = &pgmap->res;
  104. resource_size_t align_start, align_size;
  105. struct page *first_page;
  106. unsigned long pfn;
  107. int nid;
  108. pgmap->kill(pgmap->ref);
  109. for_each_device_pfn(pfn, pgmap)
  110. put_page(pfn_to_page(pfn));
  111. /* pages are dead and unused, undo the arch mapping */
  112. align_start = res->start & ~(SECTION_SIZE - 1);
  113. align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
  114. - align_start;
  115. /* make sure to access a memmap that was actually initialized */
  116. first_page = pfn_to_page(pfn_first(pgmap));
  117. nid = page_to_nid(first_page);
  118. mem_hotplug_begin();
  119. if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
  120. pfn = align_start >> PAGE_SHIFT;
  121. __remove_pages(pfn, align_size >> PAGE_SHIFT, NULL);
  122. } else {
  123. arch_remove_memory(nid, align_start, align_size,
  124. pgmap->altmap_valid ? &pgmap->altmap : NULL);
  125. kasan_remove_zero_shadow(__va(align_start), align_size);
  126. }
  127. mem_hotplug_done();
  128. untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
  129. pgmap_radix_release(res, -1);
  130. dev_WARN_ONCE(dev, pgmap->altmap.alloc,
  131. "%s: failed to free all reserved pages\n", __func__);
  132. }
  133. /**
  134. * devm_memremap_pages - remap and provide memmap backing for the given resource
  135. * @dev: hosting device for @res
  136. * @pgmap: pointer to a struct dev_pagemap
  137. *
  138. * Notes:
  139. * 1/ At a minimum the res, ref and type members of @pgmap must be initialized
  140. * by the caller before passing it to this function
  141. *
  142. * 2/ The altmap field may optionally be initialized, in which case altmap_valid
  143. * must be set to true
  144. *
  145. * 3/ pgmap->ref must be 'live' on entry and will be killed at
  146. * devm_memremap_pages_release() time, or if this routine fails.
  147. *
  148. * 4/ res is expected to be a host memory range that could feasibly be
  149. * treated as a "System RAM" range, i.e. not a device mmio range, but
  150. * this is not enforced.
  151. */
  152. void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
  153. {
  154. resource_size_t align_start, align_size, align_end;
  155. struct vmem_altmap *altmap = pgmap->altmap_valid ?
  156. &pgmap->altmap : NULL;
  157. struct resource *res = &pgmap->res;
  158. unsigned long pfn, pgoff, order;
  159. pgprot_t pgprot = PAGE_KERNEL;
  160. int error, nid, is_ram;
  161. struct dev_pagemap *conflict_pgmap;
  162. if (!pgmap->ref || !pgmap->kill)
  163. return ERR_PTR(-EINVAL);
  164. align_start = res->start & ~(SECTION_SIZE - 1);
  165. align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
  166. - align_start;
  167. align_end = align_start + align_size - 1;
  168. conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_start), NULL);
  169. if (conflict_pgmap) {
  170. dev_WARN(dev, "Conflicting mapping in same section\n");
  171. put_dev_pagemap(conflict_pgmap);
  172. return ERR_PTR(-ENOMEM);
  173. }
  174. conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_end), NULL);
  175. if (conflict_pgmap) {
  176. dev_WARN(dev, "Conflicting mapping in same section\n");
  177. put_dev_pagemap(conflict_pgmap);
  178. return ERR_PTR(-ENOMEM);
  179. }
  180. is_ram = region_intersects(align_start, align_size,
  181. IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
  182. if (is_ram != REGION_DISJOINT) {
  183. WARN_ONCE(1, "%s attempted on %s region %pr\n", __func__,
  184. is_ram == REGION_MIXED ? "mixed" : "ram", res);
  185. error = -ENXIO;
  186. goto err_array;
  187. }
  188. pgmap->dev = dev;
  189. mutex_lock(&pgmap_lock);
  190. error = 0;
  191. foreach_order_pgoff(res, order, pgoff) {
  192. error = __radix_tree_insert(&pgmap_radix,
  193. PHYS_PFN(res->start) + pgoff, order, pgmap);
  194. if (error) {
  195. dev_err(dev, "%s: failed: %d\n", __func__, error);
  196. break;
  197. }
  198. }
  199. mutex_unlock(&pgmap_lock);
  200. if (error)
  201. goto err_radix;
  202. nid = dev_to_node(dev);
  203. if (nid < 0)
  204. nid = numa_mem_id();
  205. error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(align_start), 0,
  206. align_size);
  207. if (error)
  208. goto err_pfn_remap;
  209. mem_hotplug_begin();
  210. /*
  211. * For device private memory we call add_pages() as we only need to
  212. * allocate and initialize struct page for the device memory. More-
  213. * over the device memory is un-accessible thus we do not want to
  214. * create a linear mapping for the memory like arch_add_memory()
  215. * would do.
  216. *
  217. * For all other device memory types, which are accessible by
  218. * the CPU, we do want the linear mapping and thus use
  219. * arch_add_memory().
  220. */
  221. if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
  222. error = add_pages(nid, align_start >> PAGE_SHIFT,
  223. align_size >> PAGE_SHIFT, NULL, false);
  224. } else {
  225. error = kasan_add_zero_shadow(__va(align_start), align_size);
  226. if (error) {
  227. mem_hotplug_done();
  228. goto err_kasan;
  229. }
  230. error = arch_add_memory(nid, align_start, align_size, altmap,
  231. false);
  232. }
  233. if (!error) {
  234. struct zone *zone;
  235. zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
  236. move_pfn_range_to_zone(zone, align_start >> PAGE_SHIFT,
  237. align_size >> PAGE_SHIFT, altmap);
  238. }
  239. mem_hotplug_done();
  240. if (error)
  241. goto err_add_memory;
  242. for_each_device_pfn(pfn, pgmap) {
  243. struct page *page = pfn_to_page(pfn);
  244. /*
  245. * ZONE_DEVICE pages union ->lru with a ->pgmap back
  246. * pointer. It is a bug if a ZONE_DEVICE page is ever
  247. * freed or placed on a driver-private list. Seed the
  248. * storage with LIST_POISON* values.
  249. */
  250. list_del(&page->lru);
  251. page->pgmap = pgmap;
  252. percpu_ref_get(pgmap->ref);
  253. }
  254. error = devm_add_action_or_reset(dev, devm_memremap_pages_release,
  255. pgmap);
  256. if (error)
  257. return ERR_PTR(error);
  258. return __va(res->start);
  259. err_add_memory:
  260. kasan_remove_zero_shadow(__va(align_start), align_size);
  261. err_kasan:
  262. untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
  263. err_pfn_remap:
  264. err_radix:
  265. pgmap_radix_release(res, pgoff);
  266. err_array:
  267. pgmap->kill(pgmap->ref);
  268. return ERR_PTR(error);
  269. }
  270. EXPORT_SYMBOL_GPL(devm_memremap_pages);
  271. unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
  272. {
  273. /* number of pfns from base where pfn_to_page() is valid */
  274. return altmap->reserve + altmap->free;
  275. }
  276. void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
  277. {
  278. altmap->alloc -= nr_pfns;
  279. }
  280. /**
  281. * get_dev_pagemap() - take a new live reference on the dev_pagemap for @pfn
  282. * @pfn: page frame number to lookup page_map
  283. * @pgmap: optional known pgmap that already has a reference
  284. *
  285. * If @pgmap is non-NULL and covers @pfn it will be returned as-is. If @pgmap
  286. * is non-NULL but does not cover @pfn the reference to it will be released.
  287. */
  288. struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
  289. struct dev_pagemap *pgmap)
  290. {
  291. resource_size_t phys = PFN_PHYS(pfn);
  292. /*
  293. * In the cached case we're already holding a live reference.
  294. */
  295. if (pgmap) {
  296. if (phys >= pgmap->res.start && phys <= pgmap->res.end)
  297. return pgmap;
  298. put_dev_pagemap(pgmap);
  299. }
  300. /* fall back to slow path lookup */
  301. rcu_read_lock();
  302. pgmap = radix_tree_lookup(&pgmap_radix, PHYS_PFN(phys));
  303. if (pgmap && !percpu_ref_tryget_live(pgmap->ref))
  304. pgmap = NULL;
  305. rcu_read_unlock();
  306. return pgmap;
  307. }
  308. EXPORT_SYMBOL_GPL(get_dev_pagemap);
  309. #ifdef CONFIG_DEV_PAGEMAP_OPS
  310. DEFINE_STATIC_KEY_FALSE(devmap_managed_key);
  311. EXPORT_SYMBOL(devmap_managed_key);
  312. static atomic_t devmap_enable;
  313. /*
  314. * Toggle the static key for ->page_free() callbacks when dev_pagemap
  315. * pages go idle.
  316. */
  317. void dev_pagemap_get_ops(void)
  318. {
  319. if (atomic_inc_return(&devmap_enable) == 1)
  320. static_branch_enable(&devmap_managed_key);
  321. }
  322. EXPORT_SYMBOL_GPL(dev_pagemap_get_ops);
  323. void dev_pagemap_put_ops(void)
  324. {
  325. if (atomic_dec_and_test(&devmap_enable))
  326. static_branch_disable(&devmap_managed_key);
  327. }
  328. EXPORT_SYMBOL_GPL(dev_pagemap_put_ops);
  329. void __put_devmap_managed_page(struct page *page)
  330. {
  331. int count = page_ref_dec_return(page);
  332. /*
  333. * If refcount is 1 then page is freed and refcount is stable as nobody
  334. * holds a reference on the page.
  335. */
  336. if (count == 1) {
  337. /* Clear Active bit in case of parallel mark_page_accessed */
  338. __ClearPageActive(page);
  339. __ClearPageWaiters(page);
  340. mem_cgroup_uncharge(page);
  341. page->pgmap->page_free(page, page->pgmap->data);
  342. } else if (!count)
  343. __put_page(page);
  344. }
  345. EXPORT_SYMBOL(__put_devmap_managed_page);
  346. #endif /* CONFIG_DEV_PAGEMAP_OPS */