blob: 37a9604133f621b8f0e68f320523fd14fc4f5512 [file] [log] [blame]
Dan Williams59816902018-03-29 19:07:13 -07001/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright(c) 2015 Intel Corporation. All rights reserved. */
Dan Williams9476df72016-01-15 16:56:19 -08003#include <linux/radix-tree.h>
Christoph Hellwig7d3dcf22015-08-10 23:07:07 -04004#include <linux/device.h>
Dan Williams92281dee2015-08-10 23:07:06 -04005#include <linux/types.h>
Dan Williams34c0fd52016-01-15 16:56:14 -08006#include <linux/pfn_t.h>
Dan Williams92281dee2015-08-10 23:07:06 -04007#include <linux/io.h>
8#include <linux/mm.h>
Christoph Hellwig41e94a82015-08-17 16:00:35 +02009#include <linux/memory_hotplug.h>
Jérôme Glisse5042db42017-09-08 16:11:43 -070010#include <linux/swap.h>
11#include <linux/swapops.h>
Dan Williams92281dee2015-08-10 23:07:06 -040012
Dan Williams9476df72016-01-15 16:56:19 -080013static DEFINE_MUTEX(pgmap_lock);
14static RADIX_TREE(pgmap_radix, GFP_KERNEL);
15#define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1)
16#define SECTION_SIZE (1UL << PA_SECTION_SHIFT)
17
Dan Williamsab1b5972017-09-06 16:24:13 -070018static 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
23 nr_pages = PHYS_PFN(resource_size(res));
24 if (nr_pages == pgoff)
25 return ULONG_MAX;
26
27 /*
28 * What is the largest aligned power-of-2 range available from
29 * this resource pgoff to the end of the resource range,
30 * considering the alignment of the current pgoff?
31 */
32 mask = phys_pgoff | rounddown_pow_of_two(nr_pages - pgoff);
33 if (!mask)
34 return ULONG_MAX;
35
36 return find_first_bit(&mask, BITS_PER_LONG);
37}
38
39#define foreach_order_pgoff(res, order, pgoff) \
40 for (pgoff = 0, order = order_at((res), pgoff); order < ULONG_MAX; \
41 pgoff += 1UL << order, order = order_at((res), pgoff))
42
Jérôme Glisse5042db42017-09-08 16:11:43 -070043#if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
44int device_private_entry_fault(struct vm_area_struct *vma,
45 unsigned long addr,
46 swp_entry_t entry,
47 unsigned int flags,
48 pmd_t *pmdp)
49{
50 struct page *page = device_private_entry_to_page(entry);
51
52 /*
53 * The page_fault() callback must migrate page back to system memory
54 * so that CPU can access it. This might fail for various reasons
55 * (device issue, device was unsafely unplugged, ...). When such
56 * error conditions happen, the callback must return VM_FAULT_SIGBUS.
57 *
58 * Note that because memory cgroup charges are accounted to the device
59 * memory, this should never fail because of memory restrictions (but
60 * allocation of regular system page might still fail because we are
61 * out of memory).
62 *
63 * There is a more in-depth description of what that callback can and
64 * cannot do, in include/linux/memremap.h
65 */
66 return page->pgmap->page_fault(vma, addr, page, flags, pmdp);
67}
68EXPORT_SYMBOL(device_private_entry_fault);
69#endif /* CONFIG_DEVICE_PRIVATE */
70
Jan H. Schönherr77dd66a2018-01-19 16:26:33 -080071static void pgmap_radix_release(struct resource *res, unsigned long end_pgoff)
Christoph Hellwig41e94a82015-08-17 16:00:35 +020072{
Dan Williamsab1b5972017-09-06 16:24:13 -070073 unsigned long pgoff, order;
Dan Williams9476df72016-01-15 16:56:19 -080074
75 mutex_lock(&pgmap_lock);
Jan H. Schönherr77dd66a2018-01-19 16:26:33 -080076 foreach_order_pgoff(res, order, pgoff) {
77 if (pgoff >= end_pgoff)
78 break;
Dan Williamsab1b5972017-09-06 16:24:13 -070079 radix_tree_delete(&pgmap_radix, PHYS_PFN(res->start) + pgoff);
Jan H. Schönherr77dd66a2018-01-19 16:26:33 -080080 }
Dan Williams9476df72016-01-15 16:56:19 -080081 mutex_unlock(&pgmap_lock);
Dan Williamsab1b5972017-09-06 16:24:13 -070082
83 synchronize_rcu();
Dan Williams9476df72016-01-15 16:56:19 -080084}
85
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010086static unsigned long pfn_first(struct dev_pagemap *pgmap)
Dan Williams5c2c2582016-01-15 16:56:49 -080087{
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010088 const struct resource *res = &pgmap->res;
89 struct vmem_altmap *altmap = &pgmap->altmap;
Dan Williams5c2c2582016-01-15 16:56:49 -080090 unsigned long pfn;
91
92 pfn = res->start >> PAGE_SHIFT;
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010093 if (pgmap->altmap_valid)
Dan Williams5c2c2582016-01-15 16:56:49 -080094 pfn += vmem_altmap_offset(altmap);
95 return pfn;
96}
97
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010098static unsigned long pfn_end(struct dev_pagemap *pgmap)
Dan Williams5c2c2582016-01-15 16:56:49 -080099{
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100100 const struct resource *res = &pgmap->res;
Dan Williams5c2c2582016-01-15 16:56:49 -0800101
102 return (res->start + resource_size(res)) >> PAGE_SHIFT;
103}
104
Dan Williams949b93252018-02-06 19:34:11 -0800105static unsigned long pfn_next(unsigned long pfn)
106{
107 if (pfn % 1024 == 0)
108 cond_resched();
109 return pfn + 1;
110}
111
Dan Williams5c2c2582016-01-15 16:56:49 -0800112#define for_each_device_pfn(pfn, map) \
Dan Williams949b93252018-02-06 19:34:11 -0800113 for (pfn = pfn_first(map); pfn < pfn_end(map); pfn = pfn_next(pfn))
Dan Williams5c2c2582016-01-15 16:56:49 -0800114
Christoph Hellwige8d51342017-12-29 08:54:05 +0100115static void devm_memremap_pages_release(void *data)
Dan Williams9476df72016-01-15 16:56:19 -0800116{
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100117 struct dev_pagemap *pgmap = data;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100118 struct device *dev = pgmap->dev;
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100119 struct resource *res = &pgmap->res;
Dan Williams9476df72016-01-15 16:56:19 -0800120 resource_size_t align_start, align_size;
Dan Williams71389702017-04-28 10:23:37 -0700121 unsigned long pfn;
122
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100123 for_each_device_pfn(pfn, pgmap)
Dan Williams71389702017-04-28 10:23:37 -0700124 put_page(pfn_to_page(pfn));
Dan Williams9476df72016-01-15 16:56:19 -0800125
Dan Williams5c2c2582016-01-15 16:56:49 -0800126 if (percpu_ref_tryget_live(pgmap->ref)) {
127 dev_WARN(dev, "%s: page mapping is still live!\n", __func__);
128 percpu_ref_put(pgmap->ref);
129 }
130
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200131 /* pages are dead and unused, undo the arch mapping */
Dan Williams9476df72016-01-15 16:56:19 -0800132 align_start = res->start & ~(SECTION_SIZE - 1);
Jan H. Schönherr10a0cd62018-01-19 16:27:54 -0800133 align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
134 - align_start;
Dan Williamsb5d24fd2017-02-24 14:55:45 -0800135
Dan Williamsf931ab42017-01-10 16:57:36 -0800136 mem_hotplug_begin();
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100137 arch_remove_memory(align_start, align_size, pgmap->altmap_valid ?
138 &pgmap->altmap : NULL);
Dan Williamsf931ab42017-01-10 16:57:36 -0800139 mem_hotplug_done();
Dan Williamsb5d24fd2017-02-24 14:55:45 -0800140
Dan Williams90497712016-09-07 08:51:21 -0700141 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
Jan H. Schönherr77dd66a2018-01-19 16:26:33 -0800142 pgmap_radix_release(res, -1);
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100143 dev_WARN_ONCE(dev, pgmap->altmap.alloc,
144 "%s: failed to free all reserved pages\n", __func__);
Dan Williams9476df72016-01-15 16:56:19 -0800145}
146
Dan Williams4b94ffd2016-01-15 16:56:22 -0800147/**
148 * devm_memremap_pages - remap and provide memmap backing for the given resource
149 * @dev: hosting device for @res
Christoph Hellwige8d51342017-12-29 08:54:05 +0100150 * @pgmap: pointer to a struct dev_pgmap
Dan Williams4b94ffd2016-01-15 16:56:22 -0800151 *
Dan Williams5c2c2582016-01-15 16:56:49 -0800152 * Notes:
Christoph Hellwige8d51342017-12-29 08:54:05 +0100153 * 1/ At a minimum the res, ref and type members of @pgmap must be initialized
154 * by the caller before passing it to this function
155 *
156 * 2/ The altmap field may optionally be initialized, in which case altmap_valid
157 * must be set to true
158 *
159 * 3/ pgmap.ref must be 'live' on entry and 'dead' before devm_memunmap_pages()
160 * time (or devm release event). The expected order of events is that ref has
Dan Williams71389702017-04-28 10:23:37 -0700161 * been through percpu_ref_kill() before devm_memremap_pages_release(). The
162 * wait for the completion of all references being dropped and
163 * percpu_ref_exit() must occur after devm_memremap_pages_release().
Dan Williams5c2c2582016-01-15 16:56:49 -0800164 *
Christoph Hellwige8d51342017-12-29 08:54:05 +0100165 * 4/ res is expected to be a host memory range that could feasibly be
Dan Williams5c2c2582016-01-15 16:56:49 -0800166 * treated as a "System RAM" range, i.e. not a device mmio range, but
167 * this is not enforced.
Dan Williams4b94ffd2016-01-15 16:56:22 -0800168 */
Christoph Hellwige8d51342017-12-29 08:54:05 +0100169void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200170{
Dan Williamsab1b5972017-09-06 16:24:13 -0700171 resource_size_t align_start, align_size, align_end;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100172 struct vmem_altmap *altmap = pgmap->altmap_valid ?
173 &pgmap->altmap : NULL;
Dan Williams949b93252018-02-06 19:34:11 -0800174 struct resource *res = &pgmap->res;
Dan Williamsab1b5972017-09-06 16:24:13 -0700175 unsigned long pfn, pgoff, order;
Dan Williams90497712016-09-07 08:51:21 -0700176 pgprot_t pgprot = PAGE_KERNEL;
Dan Williams949b93252018-02-06 19:34:11 -0800177 int error, nid, is_ram;
Dan Williams5f29a772016-03-09 14:08:13 -0800178
179 align_start = res->start & ~(SECTION_SIZE - 1);
180 align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
181 - align_start;
Linus Torvaldsd37a14bb2016-03-14 15:15:51 -0700182 is_ram = region_intersects(align_start, align_size,
183 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200184
185 if (is_ram == REGION_MIXED) {
186 WARN_ONCE(1, "%s attempted on mixed region %pr\n",
187 __func__, res);
188 return ERR_PTR(-ENXIO);
189 }
190
191 if (is_ram == REGION_INTERSECTS)
192 return __va(res->start);
193
Christoph Hellwige8d51342017-12-29 08:54:05 +0100194 if (!pgmap->ref)
Dan Williams5c2c2582016-01-15 16:56:49 -0800195 return ERR_PTR(-EINVAL);
196
Dan Williams4b94ffd2016-01-15 16:56:22 -0800197 pgmap->dev = dev;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800198
Dan Williams9476df72016-01-15 16:56:19 -0800199 mutex_lock(&pgmap_lock);
200 error = 0;
Dan Williamseb7d78c2016-01-29 21:48:34 -0800201 align_end = align_start + align_size - 1;
Dan Williamsab1b5972017-09-06 16:24:13 -0700202
203 foreach_order_pgoff(res, order, pgoff) {
Dan Williamsab1b5972017-09-06 16:24:13 -0700204 error = __radix_tree_insert(&pgmap_radix,
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100205 PHYS_PFN(res->start) + pgoff, order, pgmap);
Dan Williams9476df72016-01-15 16:56:19 -0800206 if (error) {
207 dev_err(dev, "%s: failed: %d\n", __func__, error);
208 break;
209 }
210 }
211 mutex_unlock(&pgmap_lock);
212 if (error)
213 goto err_radix;
214
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200215 nid = dev_to_node(dev);
216 if (nid < 0)
Dan Williams7eff93b2015-10-05 20:35:55 -0400217 nid = numa_mem_id();
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200218
Dan Williams90497712016-09-07 08:51:21 -0700219 error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(align_start), 0,
220 align_size);
221 if (error)
222 goto err_pfn_remap;
223
Dan Williamsf931ab42017-01-10 16:57:36 -0800224 mem_hotplug_begin();
Christoph Hellwig24e6d5a2017-12-29 08:53:53 +0100225 error = arch_add_memory(nid, align_start, align_size, altmap, false);
Michal Hockof1dd2cd2017-07-06 15:38:11 -0700226 if (!error)
227 move_pfn_range_to_zone(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
228 align_start >> PAGE_SHIFT,
Christoph Hellwiga99583e2017-12-29 08:53:57 +0100229 align_size >> PAGE_SHIFT, altmap);
Dan Williamsf931ab42017-01-10 16:57:36 -0800230 mem_hotplug_done();
Dan Williams9476df72016-01-15 16:56:19 -0800231 if (error)
232 goto err_add_memory;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200233
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100234 for_each_device_pfn(pfn, pgmap) {
Dan Williams5c2c2582016-01-15 16:56:49 -0800235 struct page *page = pfn_to_page(pfn);
236
Dan Williamsd77a1172016-03-09 14:08:10 -0800237 /*
238 * ZONE_DEVICE pages union ->lru with a ->pgmap back
239 * pointer. It is a bug if a ZONE_DEVICE page is ever
240 * freed or placed on a driver-private list. Seed the
241 * storage with LIST_POISON* values.
242 */
243 list_del(&page->lru);
Dan Williams5c2c2582016-01-15 16:56:49 -0800244 page->pgmap = pgmap;
Christoph Hellwige8d51342017-12-29 08:54:05 +0100245 percpu_ref_get(pgmap->ref);
Dan Williams5c2c2582016-01-15 16:56:49 -0800246 }
Christoph Hellwige8d51342017-12-29 08:54:05 +0100247
248 devm_add_action(dev, devm_memremap_pages_release, pgmap);
249
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200250 return __va(res->start);
Dan Williams9476df72016-01-15 16:56:19 -0800251
252 err_add_memory:
Dan Williams90497712016-09-07 08:51:21 -0700253 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
254 err_pfn_remap:
Dan Williams9476df72016-01-15 16:56:19 -0800255 err_radix:
Jan H. Schönherr77dd66a2018-01-19 16:26:33 -0800256 pgmap_radix_release(res, pgoff);
Dan Williams9476df72016-01-15 16:56:19 -0800257 return ERR_PTR(error);
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200258}
259EXPORT_SYMBOL(devm_memremap_pages);
Dan Williams4b94ffd2016-01-15 16:56:22 -0800260
261unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
262{
263 /* number of pfns from base where pfn_to_page() is valid */
264 return altmap->reserve + altmap->free;
265}
266
267void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
268{
269 altmap->alloc -= nr_pfns;
270}
271
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100272/**
273 * get_dev_pagemap() - take a new live reference on the dev_pagemap for @pfn
274 * @pfn: page frame number to lookup page_map
275 * @pgmap: optional known pgmap that already has a reference
276 *
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100277 * If @pgmap is non-NULL and covers @pfn it will be returned as-is. If @pgmap
278 * is non-NULL but does not cover @pfn the reference to it will be released.
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100279 */
280struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
281 struct dev_pagemap *pgmap)
282{
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100283 resource_size_t phys = PFN_PHYS(pfn);
284
285 /*
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100286 * In the cached case we're already holding a live reference.
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100287 */
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100288 if (pgmap) {
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100289 if (phys >= pgmap->res.start && phys <= pgmap->res.end)
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100290 return pgmap;
291 put_dev_pagemap(pgmap);
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100292 }
293
294 /* fall back to slow path lookup */
295 rcu_read_lock();
Christoph Hellwige697c5b2017-12-29 08:54:06 +0100296 pgmap = radix_tree_lookup(&pgmap_radix, PHYS_PFN(phys));
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100297 if (pgmap && !percpu_ref_tryget_live(pgmap->ref))
298 pgmap = NULL;
299 rcu_read_unlock();
300
301 return pgmap;
302}
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700303
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700304#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) || IS_ENABLED(CONFIG_DEVICE_PUBLIC)
305void put_zone_device_private_or_public_page(struct page *page)
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700306{
307 int count = page_ref_dec_return(page);
308
309 /*
310 * If refcount is 1 then page is freed and refcount is stable as nobody
311 * holds a reference on the page.
312 */
313 if (count == 1) {
314 /* Clear Active bit in case of parallel mark_page_accessed */
315 __ClearPageActive(page);
316 __ClearPageWaiters(page);
317
318 page->mapping = NULL;
Jérôme Glissec733a822017-09-08 16:11:54 -0700319 mem_cgroup_uncharge(page);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700320
321 page->pgmap->page_free(page, page->pgmap->data);
322 } else if (!count)
323 __put_page(page);
324}
Jérôme Glissedf6ad692017-09-08 16:12:24 -0700325EXPORT_SYMBOL(put_zone_device_private_or_public_page);
326#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */