mm: fix false-positive OVERCOMMIT_GUESS failures
[linux-2.6-microblaze.git] / kernel / memremap.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright(c) 2015 Intel Corporation. All rights reserved. */
3 #include <linux/device.h>
4 #include <linux/io.h>
5 #include <linux/kasan.h>
6 #include <linux/memory_hotplug.h>
7 #include <linux/mm.h>
8 #include <linux/pfn_t.h>
9 #include <linux/swap.h>
10 #include <linux/swapops.h>
11 #include <linux/types.h>
12 #include <linux/wait_bit.h>
13 #include <linux/xarray.h>
14 #include <linux/hmm.h>
15
16 static DEFINE_XARRAY(pgmap_array);
17 #define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1)
18 #define SECTION_SIZE (1UL << PA_SECTION_SHIFT)
19
20 #if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
21 vm_fault_t device_private_entry_fault(struct vm_area_struct *vma,
22                        unsigned long addr,
23                        swp_entry_t entry,
24                        unsigned int flags,
25                        pmd_t *pmdp)
26 {
27         struct page *page = device_private_entry_to_page(entry);
28         struct hmm_devmem *devmem;
29
30         devmem = container_of(page->pgmap, typeof(*devmem), pagemap);
31
32         /*
33          * The page_fault() callback must migrate page back to system memory
34          * so that CPU can access it. This might fail for various reasons
35          * (device issue, device was unsafely unplugged, ...). When such
36          * error conditions happen, the callback must return VM_FAULT_SIGBUS.
37          *
38          * Note that because memory cgroup charges are accounted to the device
39          * memory, this should never fail because of memory restrictions (but
40          * allocation of regular system page might still fail because we are
41          * out of memory).
42          *
43          * There is a more in-depth description of what that callback can and
44          * cannot do, in include/linux/memremap.h
45          */
46         return devmem->page_fault(vma, addr, page, flags, pmdp);
47 }
48 EXPORT_SYMBOL(device_private_entry_fault);
49 #endif /* CONFIG_DEVICE_PRIVATE */
50
51 static void pgmap_array_delete(struct resource *res)
52 {
53         xa_store_range(&pgmap_array, PHYS_PFN(res->start), PHYS_PFN(res->end),
54                         NULL, GFP_KERNEL);
55         synchronize_rcu();
56 }
57
58 static unsigned long pfn_first(struct dev_pagemap *pgmap)
59 {
60         const struct resource *res = &pgmap->res;
61         struct vmem_altmap *altmap = &pgmap->altmap;
62         unsigned long pfn;
63
64         pfn = res->start >> PAGE_SHIFT;
65         if (pgmap->altmap_valid)
66                 pfn += vmem_altmap_offset(altmap);
67         return pfn;
68 }
69
70 static unsigned long pfn_end(struct dev_pagemap *pgmap)
71 {
72         const struct resource *res = &pgmap->res;
73
74         return (res->start + resource_size(res)) >> PAGE_SHIFT;
75 }
76
77 static unsigned long pfn_next(unsigned long pfn)
78 {
79         if (pfn % 1024 == 0)
80                 cond_resched();
81         return pfn + 1;
82 }
83
84 #define for_each_device_pfn(pfn, map) \
85         for (pfn = pfn_first(map); pfn < pfn_end(map); pfn = pfn_next(pfn))
86
87 static void devm_memremap_pages_release(void *data)
88 {
89         struct dev_pagemap *pgmap = data;
90         struct device *dev = pgmap->dev;
91         struct resource *res = &pgmap->res;
92         resource_size_t align_start, align_size;
93         unsigned long pfn;
94         int nid;
95
96         pgmap->kill(pgmap->ref);
97         for_each_device_pfn(pfn, pgmap)
98                 put_page(pfn_to_page(pfn));
99
100         /* pages are dead and unused, undo the arch mapping */
101         align_start = res->start & ~(SECTION_SIZE - 1);
102         align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
103                 - align_start;
104
105         nid = page_to_nid(pfn_to_page(align_start >> PAGE_SHIFT));
106
107         mem_hotplug_begin();
108         if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
109                 pfn = align_start >> PAGE_SHIFT;
110                 __remove_pages(page_zone(pfn_to_page(pfn)), pfn,
111                                 align_size >> PAGE_SHIFT, NULL);
112         } else {
113                 arch_remove_memory(nid, align_start, align_size,
114                                 pgmap->altmap_valid ? &pgmap->altmap : NULL);
115                 kasan_remove_zero_shadow(__va(align_start), align_size);
116         }
117         mem_hotplug_done();
118
119         untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
120         pgmap_array_delete(res);
121         dev_WARN_ONCE(dev, pgmap->altmap.alloc,
122                       "%s: failed to free all reserved pages\n", __func__);
123 }
124
125 /**
126  * devm_memremap_pages - remap and provide memmap backing for the given resource
127  * @dev: hosting device for @res
128  * @pgmap: pointer to a struct dev_pagemap
129  *
130  * Notes:
131  * 1/ At a minimum the res, ref and type members of @pgmap must be initialized
132  *    by the caller before passing it to this function
133  *
134  * 2/ The altmap field may optionally be initialized, in which case altmap_valid
135  *    must be set to true
136  *
137  * 3/ pgmap->ref must be 'live' on entry and will be killed at
138  *    devm_memremap_pages_release() time, or if this routine fails.
139  *
140  * 4/ res is expected to be a host memory range that could feasibly be
141  *    treated as a "System RAM" range, i.e. not a device mmio range, but
142  *    this is not enforced.
143  */
144 void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
145 {
146         resource_size_t align_start, align_size, align_end;
147         struct vmem_altmap *altmap = pgmap->altmap_valid ?
148                         &pgmap->altmap : NULL;
149         struct resource *res = &pgmap->res;
150         struct dev_pagemap *conflict_pgmap;
151         struct mhp_restrictions restrictions = {
152                 /*
153                  * We do not want any optional features only our own memmap
154                 */
155                 .altmap = altmap,
156         };
157         pgprot_t pgprot = PAGE_KERNEL;
158         int error, nid, is_ram;
159
160         if (!pgmap->ref || !pgmap->kill)
161                 return ERR_PTR(-EINVAL);
162
163         align_start = res->start & ~(SECTION_SIZE - 1);
164         align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
165                 - align_start;
166         align_end = align_start + align_size - 1;
167
168         conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_start), NULL);
169         if (conflict_pgmap) {
170                 dev_WARN(dev, "Conflicting mapping in same section\n");
171                 put_dev_pagemap(conflict_pgmap);
172                 return ERR_PTR(-ENOMEM);
173         }
174
175         conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_end), NULL);
176         if (conflict_pgmap) {
177                 dev_WARN(dev, "Conflicting mapping in same section\n");
178                 put_dev_pagemap(conflict_pgmap);
179                 return ERR_PTR(-ENOMEM);
180         }
181
182         is_ram = region_intersects(align_start, align_size,
183                 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
184
185         if (is_ram != REGION_DISJOINT) {
186                 WARN_ONCE(1, "%s attempted on %s region %pr\n", __func__,
187                                 is_ram == REGION_MIXED ? "mixed" : "ram", res);
188                 error = -ENXIO;
189                 goto err_array;
190         }
191
192         pgmap->dev = dev;
193
194         error = xa_err(xa_store_range(&pgmap_array, PHYS_PFN(res->start),
195                                 PHYS_PFN(res->end), pgmap, GFP_KERNEL));
196         if (error)
197                 goto err_array;
198
199         nid = dev_to_node(dev);
200         if (nid < 0)
201                 nid = numa_mem_id();
202
203         error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(align_start), 0,
204                         align_size);
205         if (error)
206                 goto err_pfn_remap;
207
208         mem_hotplug_begin();
209
210         /*
211          * For device private memory we call add_pages() as we only need to
212          * allocate and initialize struct page for the device memory. More-
213          * over the device memory is un-accessible thus we do not want to
214          * create a linear mapping for the memory like arch_add_memory()
215          * would do.
216          *
217          * For all other device memory types, which are accessible by
218          * the CPU, we do want the linear mapping and thus use
219          * arch_add_memory().
220          */
221         if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
222                 error = add_pages(nid, align_start >> PAGE_SHIFT,
223                                 align_size >> PAGE_SHIFT, &restrictions);
224         } else {
225                 error = kasan_add_zero_shadow(__va(align_start), align_size);
226                 if (error) {
227                         mem_hotplug_done();
228                         goto err_kasan;
229                 }
230
231                 error = arch_add_memory(nid, align_start, align_size,
232                                         &restrictions);
233         }
234
235         if (!error) {
236                 struct zone *zone;
237
238                 zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
239                 move_pfn_range_to_zone(zone, align_start >> PAGE_SHIFT,
240                                 align_size >> PAGE_SHIFT, altmap);
241         }
242
243         mem_hotplug_done();
244         if (error)
245                 goto err_add_memory;
246
247         /*
248          * Initialization of the pages has been deferred until now in order
249          * to allow us to do the work while not holding the hotplug lock.
250          */
251         memmap_init_zone_device(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
252                                 align_start >> PAGE_SHIFT,
253                                 align_size >> PAGE_SHIFT, pgmap);
254         percpu_ref_get_many(pgmap->ref, pfn_end(pgmap) - pfn_first(pgmap));
255
256         error = devm_add_action_or_reset(dev, devm_memremap_pages_release,
257                         pgmap);
258         if (error)
259                 return ERR_PTR(error);
260
261         return __va(res->start);
262
263  err_add_memory:
264         kasan_remove_zero_shadow(__va(align_start), align_size);
265  err_kasan:
266         untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
267  err_pfn_remap:
268         pgmap_array_delete(res);
269  err_array:
270         pgmap->kill(pgmap->ref);
271         return ERR_PTR(error);
272 }
273 EXPORT_SYMBOL_GPL(devm_memremap_pages);
274
275 unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
276 {
277         /* number of pfns from base where pfn_to_page() is valid */
278         return altmap->reserve + altmap->free;
279 }
280
281 void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
282 {
283         altmap->alloc -= nr_pfns;
284 }
285
286 /**
287  * get_dev_pagemap() - take a new live reference on the dev_pagemap for @pfn
288  * @pfn: page frame number to lookup page_map
289  * @pgmap: optional known pgmap that already has a reference
290  *
291  * If @pgmap is non-NULL and covers @pfn it will be returned as-is.  If @pgmap
292  * is non-NULL but does not cover @pfn the reference to it will be released.
293  */
294 struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
295                 struct dev_pagemap *pgmap)
296 {
297         resource_size_t phys = PFN_PHYS(pfn);
298
299         /*
300          * In the cached case we're already holding a live reference.
301          */
302         if (pgmap) {
303                 if (phys >= pgmap->res.start && phys <= pgmap->res.end)
304                         return pgmap;
305                 put_dev_pagemap(pgmap);
306         }
307
308         /* fall back to slow path lookup */
309         rcu_read_lock();
310         pgmap = xa_load(&pgmap_array, PHYS_PFN(phys));
311         if (pgmap && !percpu_ref_tryget_live(pgmap->ref))
312                 pgmap = NULL;
313         rcu_read_unlock();
314
315         return pgmap;
316 }
317 EXPORT_SYMBOL_GPL(get_dev_pagemap);
318
319 #ifdef CONFIG_DEV_PAGEMAP_OPS
320 DEFINE_STATIC_KEY_FALSE(devmap_managed_key);
321 EXPORT_SYMBOL(devmap_managed_key);
322 static atomic_t devmap_enable;
323
324 /*
325  * Toggle the static key for ->page_free() callbacks when dev_pagemap
326  * pages go idle.
327  */
328 void dev_pagemap_get_ops(void)
329 {
330         if (atomic_inc_return(&devmap_enable) == 1)
331                 static_branch_enable(&devmap_managed_key);
332 }
333 EXPORT_SYMBOL_GPL(dev_pagemap_get_ops);
334
335 void dev_pagemap_put_ops(void)
336 {
337         if (atomic_dec_and_test(&devmap_enable))
338                 static_branch_disable(&devmap_managed_key);
339 }
340 EXPORT_SYMBOL_GPL(dev_pagemap_put_ops);
341
342 void __put_devmap_managed_page(struct page *page)
343 {
344         int count = page_ref_dec_return(page);
345
346         /*
347          * If refcount is 1 then page is freed and refcount is stable as nobody
348          * holds a reference on the page.
349          */
350         if (count == 1) {
351                 /* Clear Active bit in case of parallel mark_page_accessed */
352                 __ClearPageActive(page);
353                 __ClearPageWaiters(page);
354
355                 mem_cgroup_uncharge(page);
356
357                 page->pgmap->page_free(page, page->pgmap->data);
358         } else if (!count)
359                 __put_page(page);
360 }
361 EXPORT_SYMBOL(__put_devmap_managed_page);
362 #endif /* CONFIG_DEV_PAGEMAP_OPS */