memremap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * Copyright(c) 2015 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of version 2 of the GNU General Public License as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. */
  13. #include <linux/radix-tree.h>
  14. #include <linux/device.h>
  15. #include <linux/types.h>
  16. #include <linux/pfn_t.h>
  17. #include <linux/io.h>
  18. #include <linux/mm.h>
  19. #include <linux/memory_hotplug.h>
  20. #include <linux/swap.h>
  21. #include <linux/swapops.h>
  22. #ifndef ioremap_cache
  23. /* temporary while we convert existing ioremap_cache users to memremap */
  24. __weak void __iomem *ioremap_cache(resource_size_t offset, unsigned long size)
  25. {
  26. return ioremap(offset, size);
  27. }
  28. #endif
  29. #ifndef arch_memremap_wb
  30. static void *arch_memremap_wb(resource_size_t offset, unsigned long size)
  31. {
  32. return (__force void *)ioremap_cache(offset, size);
  33. }
  34. #endif
  35. #ifndef arch_memremap_can_ram_remap
  36. static bool arch_memremap_can_ram_remap(resource_size_t offset, size_t size,
  37. unsigned long flags)
  38. {
  39. return true;
  40. }
  41. #endif
  42. static void *try_ram_remap(resource_size_t offset, size_t size,
  43. unsigned long flags)
  44. {
  45. unsigned long pfn = PHYS_PFN(offset);
  46. /* In the simple case just return the existing linear address */
  47. if (pfn_valid(pfn) && !PageHighMem(pfn_to_page(pfn)) &&
  48. arch_memremap_can_ram_remap(offset, size, flags))
  49. return __va(offset);
  50. return NULL; /* fallback to arch_memremap_wb */
  51. }
  52. /**
  53. * memremap() - remap an iomem_resource as cacheable memory
  54. * @offset: iomem resource start address
  55. * @size: size of remap
  56. * @flags: any of MEMREMAP_WB, MEMREMAP_WT, MEMREMAP_WC,
  57. * MEMREMAP_ENC, MEMREMAP_DEC
  58. *
  59. * memremap() is "ioremap" for cases where it is known that the resource
  60. * being mapped does not have i/o side effects and the __iomem
  61. * annotation is not applicable. In the case of multiple flags, the different
  62. * mapping types will be attempted in the order listed below until one of
  63. * them succeeds.
  64. *
  65. * MEMREMAP_WB - matches the default mapping for System RAM on
  66. * the architecture. This is usually a read-allocate write-back cache.
  67. * Morever, if MEMREMAP_WB is specified and the requested remap region is RAM
  68. * memremap() will bypass establishing a new mapping and instead return
  69. * a pointer into the direct map.
  70. *
  71. * MEMREMAP_WT - establish a mapping whereby writes either bypass the
  72. * cache or are written through to memory and never exist in a
  73. * cache-dirty state with respect to program visibility. Attempts to
  74. * map System RAM with this mapping type will fail.
  75. *
  76. * MEMREMAP_WC - establish a writecombine mapping, whereby writes may
  77. * be coalesced together (e.g. in the CPU's write buffers), but is otherwise
  78. * uncached. Attempts to map System RAM with this mapping type will fail.
  79. */
  80. void *memremap(resource_size_t offset, size_t size, unsigned long flags)
  81. {
  82. int is_ram = region_intersects(offset, size,
  83. IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
  84. void *addr = NULL;
  85. if (!flags)
  86. return NULL;
  87. if (is_ram == REGION_MIXED) {
  88. WARN_ONCE(1, "memremap attempted on mixed range %pa size: %#lx\n",
  89. &offset, (unsigned long) size);
  90. return NULL;
  91. }
  92. /* Try all mapping types requested until one returns non-NULL */
  93. if (flags & MEMREMAP_WB) {
  94. /*
  95. * MEMREMAP_WB is special in that it can be satisifed
  96. * from the direct map. Some archs depend on the
  97. * capability of memremap() to autodetect cases where
  98. * the requested range is potentially in System RAM.
  99. */
  100. if (is_ram == REGION_INTERSECTS)
  101. addr = try_ram_remap(offset, size, flags);
  102. if (!addr)
  103. addr = arch_memremap_wb(offset, size);
  104. }
  105. /*
  106. * If we don't have a mapping yet and other request flags are
  107. * present then we will be attempting to establish a new virtual
  108. * address mapping. Enforce that this mapping is not aliasing
  109. * System RAM.
  110. */
  111. if (!addr && is_ram == REGION_INTERSECTS && flags != MEMREMAP_WB) {
  112. WARN_ONCE(1, "memremap attempted on ram %pa size: %#lx\n",
  113. &offset, (unsigned long) size);
  114. return NULL;
  115. }
  116. if (!addr && (flags & MEMREMAP_WT))
  117. addr = ioremap_wt(offset, size);
  118. if (!addr && (flags & MEMREMAP_WC))
  119. addr = ioremap_wc(offset, size);
  120. return addr;
  121. }
  122. EXPORT_SYMBOL(memremap);
  123. void memunmap(void *addr)
  124. {
  125. if (is_vmalloc_addr(addr))
  126. iounmap((void __iomem *) addr);
  127. }
  128. EXPORT_SYMBOL(memunmap);
  129. static void devm_memremap_release(struct device *dev, void *res)
  130. {
  131. memunmap(*(void **)res);
  132. }
  133. static int devm_memremap_match(struct device *dev, void *res, void *match_data)
  134. {
  135. return *(void **)res == match_data;
  136. }
  137. void *devm_memremap(struct device *dev, resource_size_t offset,
  138. size_t size, unsigned long flags)
  139. {
  140. void **ptr, *addr;
  141. ptr = devres_alloc_node(devm_memremap_release, sizeof(*ptr), GFP_KERNEL,
  142. dev_to_node(dev));
  143. if (!ptr)
  144. return ERR_PTR(-ENOMEM);
  145. addr = memremap(offset, size, flags);
  146. if (addr) {
  147. *ptr = addr;
  148. devres_add(dev, ptr);
  149. } else {
  150. devres_free(ptr);
  151. return ERR_PTR(-ENXIO);
  152. }
  153. return addr;
  154. }
  155. EXPORT_SYMBOL(devm_memremap);
  156. void devm_memunmap(struct device *dev, void *addr)
  157. {
  158. WARN_ON(devres_release(dev, devm_memremap_release,
  159. devm_memremap_match, addr));
  160. }
  161. EXPORT_SYMBOL(devm_memunmap);
  162. #ifdef CONFIG_ZONE_DEVICE
  163. static DEFINE_MUTEX(pgmap_lock);
  164. static RADIX_TREE(pgmap_radix, GFP_KERNEL);
  165. #define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1)
  166. #define SECTION_SIZE (1UL << PA_SECTION_SHIFT)
  167. struct page_map {
  168. struct resource res;
  169. struct percpu_ref *ref;
  170. struct dev_pagemap pgmap;
  171. struct vmem_altmap altmap;
  172. };
  173. static unsigned long order_at(struct resource *res, unsigned long pgoff)
  174. {
  175. unsigned long phys_pgoff = PHYS_PFN(res->start) + pgoff;
  176. unsigned long nr_pages, mask;
  177. nr_pages = PHYS_PFN(resource_size(res));
  178. if (nr_pages == pgoff)
  179. return ULONG_MAX;
  180. /*
  181. * What is the largest aligned power-of-2 range available from
  182. * this resource pgoff to the end of the resource range,
  183. * considering the alignment of the current pgoff?
  184. */
  185. mask = phys_pgoff | rounddown_pow_of_two(nr_pages - pgoff);
  186. if (!mask)
  187. return ULONG_MAX;
  188. return find_first_bit(&mask, BITS_PER_LONG);
  189. }
  190. #define foreach_order_pgoff(res, order, pgoff) \
  191. for (pgoff = 0, order = order_at((res), pgoff); order < ULONG_MAX; \
  192. pgoff += 1UL << order, order = order_at((res), pgoff))
  193. #if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
  194. int device_private_entry_fault(struct vm_area_struct *vma,
  195. unsigned long addr,
  196. swp_entry_t entry,
  197. unsigned int flags,
  198. pmd_t *pmdp)
  199. {
  200. struct page *page = device_private_entry_to_page(entry);
  201. /*
  202. * The page_fault() callback must migrate page back to system memory
  203. * so that CPU can access it. This might fail for various reasons
  204. * (device issue, device was unsafely unplugged, ...). When such
  205. * error conditions happen, the callback must return VM_FAULT_SIGBUS.
  206. *
  207. * Note that because memory cgroup charges are accounted to the device
  208. * memory, this should never fail because of memory restrictions (but
  209. * allocation of regular system page might still fail because we are
  210. * out of memory).
  211. *
  212. * There is a more in-depth description of what that callback can and
  213. * cannot do, in include/linux/memremap.h
  214. */
  215. return page->pgmap->page_fault(vma, addr, page, flags, pmdp);
  216. }
  217. EXPORT_SYMBOL(device_private_entry_fault);
  218. #endif /* CONFIG_DEVICE_PRIVATE */
  219. static void pgmap_radix_release(struct resource *res)
  220. {
  221. unsigned long pgoff, order;
  222. mutex_lock(&pgmap_lock);
  223. foreach_order_pgoff(res, order, pgoff)
  224. radix_tree_delete(&pgmap_radix, PHYS_PFN(res->start) + pgoff);
  225. mutex_unlock(&pgmap_lock);
  226. synchronize_rcu();
  227. }
  228. static unsigned long pfn_first(struct page_map *page_map)
  229. {
  230. struct dev_pagemap *pgmap = &page_map->pgmap;
  231. const struct resource *res = &page_map->res;
  232. struct vmem_altmap *altmap = pgmap->altmap;
  233. unsigned long pfn;
  234. pfn = res->start >> PAGE_SHIFT;
  235. if (altmap)
  236. pfn += vmem_altmap_offset(altmap);
  237. return pfn;
  238. }
  239. static unsigned long pfn_end(struct page_map *page_map)
  240. {
  241. const struct resource *res = &page_map->res;
  242. return (res->start + resource_size(res)) >> PAGE_SHIFT;
  243. }
  244. #define for_each_device_pfn(pfn, map) \
  245. for (pfn = pfn_first(map); pfn < pfn_end(map); pfn++)
  246. static void devm_memremap_pages_release(struct device *dev, void *data)
  247. {
  248. struct page_map *page_map = data;
  249. struct resource *res = &page_map->res;
  250. resource_size_t align_start, align_size;
  251. struct dev_pagemap *pgmap = &page_map->pgmap;
  252. unsigned long pfn;
  253. for_each_device_pfn(pfn, page_map)
  254. put_page(pfn_to_page(pfn));
  255. if (percpu_ref_tryget_live(pgmap->ref)) {
  256. dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
  257. percpu_ref_put(pgmap->ref);
  258. }
  259. /* pages are dead and unused, undo the arch mapping */
  260. align_start = res->start & ~(SECTION_SIZE - 1);
  261. align_size = ALIGN(resource_size(res), SECTION_SIZE);
  262. mem_hotplug_begin();
  263. arch_remove_memory(align_start, align_size);
  264. mem_hotplug_done();
  265. untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
  266. pgmap_radix_release(res);
  267. dev_WARN_ONCE(dev, pgmap->altmap && pgmap->altmap->alloc,
  268. "%s: failed to free all reserved pages\n", __func__);
  269. }
  270. /* assumes rcu_read_lock() held at entry */
  271. struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
  272. {
  273. struct page_map *page_map;
  274. WARN_ON_ONCE(!rcu_read_lock_held());
  275. page_map = radix_tree_lookup(&pgmap_radix, PHYS_PFN(phys));
  276. return page_map ? &page_map->pgmap : NULL;
  277. }
  278. /**
  279. * devm_memremap_pages - remap and provide memmap backing for the given resource
  280. * @dev: hosting device for @res
  281. * @res: "host memory" address range
  282. * @ref: a live per-cpu reference count
  283. * @altmap: optional descriptor for allocating the memmap from @res
  284. *
  285. * Notes:
  286. * 1/ @ref must be 'live' on entry and 'dead' before devm_memunmap_pages() time
  287. * (or devm release event). The expected order of events is that @ref has
  288. * been through percpu_ref_kill() before devm_memremap_pages_release(). The
  289. * wait for the completion of all references being dropped and
  290. * percpu_ref_exit() must occur after devm_memremap_pages_release().
  291. *
  292. * 2/ @res is expected to be a host memory range that could feasibly be
  293. * treated as a "System RAM" range, i.e. not a device mmio range, but
  294. * this is not enforced.
  295. */
  296. void *devm_memremap_pages(struct device *dev, struct resource *res,
  297. struct percpu_ref *ref, struct vmem_altmap *altmap)
  298. {
  299. resource_size_t align_start, align_size, align_end;
  300. unsigned long pfn, pgoff, order;
  301. pgprot_t pgprot = PAGE_KERNEL;
  302. struct dev_pagemap *pgmap;
  303. struct page_map *page_map;
  304. int error, nid, is_ram;
  305. align_start = res->start & ~(SECTION_SIZE - 1);
  306. align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
  307. - align_start;
  308. is_ram = region_intersects(align_start, align_size,
  309. IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
  310. if (is_ram == REGION_MIXED) {
  311. WARN_ONCE(1, "%s attempted on mixed region %pr\n",
  312. __func__, res);
  313. return ERR_PTR(-ENXIO);
  314. }
  315. if (is_ram == REGION_INTERSECTS)
  316. return __va(res->start);
  317. if (!ref)
  318. return ERR_PTR(-EINVAL);
  319. page_map = devres_alloc_node(devm_memremap_pages_release,
  320. sizeof(*page_map), GFP_KERNEL, dev_to_node(dev));
  321. if (!page_map)
  322. return ERR_PTR(-ENOMEM);
  323. pgmap = &page_map->pgmap;
  324. memcpy(&page_map->res, res, sizeof(*res));
  325. pgmap->dev = dev;
  326. if (altmap) {
  327. memcpy(&page_map->altmap, altmap, sizeof(*altmap));
  328. pgmap->altmap = &page_map->altmap;
  329. }
  330. pgmap->ref = ref;
  331. pgmap->res = &page_map->res;
  332. pgmap->type = MEMORY_DEVICE_HOST;
  333. pgmap->page_fault = NULL;
  334. pgmap->page_free = NULL;
  335. pgmap->data = NULL;
  336. mutex_lock(&pgmap_lock);
  337. error = 0;
  338. align_end = align_start + align_size - 1;
  339. foreach_order_pgoff(res, order, pgoff) {
  340. struct dev_pagemap *dup;
  341. rcu_read_lock();
  342. dup = find_dev_pagemap(res->start + PFN_PHYS(pgoff));
  343. rcu_read_unlock();
  344. if (dup) {
  345. dev_err(dev, "%s: %pr collides with mapping for %s\n",
  346. __func__, res, dev_name(dup->dev));
  347. error = -EBUSY;
  348. break;
  349. }
  350. error = __radix_tree_insert(&pgmap_radix,
  351. PHYS_PFN(res->start) + pgoff, order, page_map);
  352. if (error) {
  353. dev_err(dev, "%s: failed: %d\n", __func__, error);
  354. break;
  355. }
  356. }
  357. mutex_unlock(&pgmap_lock);
  358. if (error)
  359. goto err_radix;
  360. nid = dev_to_node(dev);
  361. if (nid < 0)
  362. nid = numa_mem_id();
  363. error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(align_start), 0,
  364. align_size);
  365. if (error)
  366. goto err_pfn_remap;
  367. mem_hotplug_begin();
  368. error = arch_add_memory(nid, align_start, align_size, false);
  369. if (!error)
  370. move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
  371. align_start >> PAGE_SHIFT,
  372. align_size >> PAGE_SHIFT);
  373. mem_hotplug_done();
  374. if (error)
  375. goto err_add_memory;
  376. for_each_device_pfn(pfn, page_map) {
  377. struct page *page = pfn_to_page(pfn);
  378. /*
  379. * ZONE_DEVICE pages union ->lru with a ->pgmap back
  380. * pointer. It is a bug if a ZONE_DEVICE page is ever
  381. * freed or placed on a driver-private list. Seed the
  382. * storage with LIST_POISON* values.
  383. */
  384. list_del(&page->lru);
  385. page->pgmap = pgmap;
  386. percpu_ref_get(ref);
  387. }
  388. devres_add(dev, page_map);
  389. return __va(res->start);
  390. err_add_memory:
  391. untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
  392. err_pfn_remap:
  393. err_radix:
  394. pgmap_radix_release(res);
  395. devres_free(page_map);
  396. return ERR_PTR(error);
  397. }
  398. EXPORT_SYMBOL(devm_memremap_pages);
  399. unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
  400. {
  401. /* number of pfns from base where pfn_to_page() is valid */
  402. return altmap->reserve + altmap->free;
  403. }
  404. void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
  405. {
  406. altmap->alloc -= nr_pfns;
  407. }
  408. struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
  409. {
  410. /*
  411. * 'memmap_start' is the virtual address for the first "struct
  412. * page" in this range of the vmemmap array. In the case of
  413. * CONFIG_SPARSEMEM_VMEMMAP a page_to_pfn conversion is simple
  414. * pointer arithmetic, so we can perform this to_vmem_altmap()
  415. * conversion without concern for the initialization state of
  416. * the struct page fields.
  417. */
  418. struct page *page = (struct page *) memmap_start;
  419. struct dev_pagemap *pgmap;
  420. /*
  421. * Unconditionally retrieve a dev_pagemap associated with the
  422. * given physical address, this is only for use in the
  423. * arch_{add|remove}_memory() for setting up and tearing down
  424. * the memmap.
  425. */
  426. rcu_read_lock();
  427. pgmap = find_dev_pagemap(__pfn_to_phys(page_to_pfn(page)));
  428. rcu_read_unlock();
  429. return pgmap ? pgmap->altmap : NULL;
  430. }
  431. #endif /* CONFIG_ZONE_DEVICE */
  432. #if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
  433. void put_zone_device_private_or_public_page(struct page *page)
  434. {
  435. int count = page_ref_dec_return(page);
  436. /*
  437. * If refcount is 1 then page is freed and refcount is stable as nobody
  438. * holds a reference on the page.
  439. */
  440. if (count == 1) {
  441. /* Clear Active bit in case of parallel mark_page_accessed */
  442. __ClearPageActive(page);
  443. __ClearPageWaiters(page);
  444. page->mapping = NULL;
  445. mem_cgroup_uncharge(page);
  446. page->pgmap->page_free(page, page->pgmap->data);
  447. } else if (!count)
  448. __put_page(page);
  449. }
  450. EXPORT_SYMBOL(put_zone_device_private_or_public_page);
  451. #endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */