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