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