memremap.c 16 KB

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