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