mm: check nr_initialised with PAGES_PER_SECTION directly in defer_init()
[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();
69324b8f
DW
101 if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
102 pfn = align_start >> PAGE_SHIFT;
103 __remove_pages(page_zone(pfn_to_page(pfn)), pfn,
104 align_size >> PAGE_SHIFT, NULL);
105 } else {
106 arch_remove_memory(align_start, align_size,
107 pgmap->altmap_valid ? &pgmap->altmap : NULL);
108 kasan_remove_zero_shadow(__va(align_start), align_size);
109 }
f931ab47 110 mem_hotplug_done();
b5d24fda 111
9049771f 112 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
bcfa4b72 113 pgmap_array_delete(res);
e7744aa2
LG
114 dev_WARN_ONCE(dev, pgmap->altmap.alloc,
115 "%s: failed to free all reserved pages\n", __func__);
9476df7d
DW
116}
117
4b94ffdc
DW
118/**
119 * devm_memremap_pages - remap and provide memmap backing for the given resource
120 * @dev: hosting device for @res
a95c90f1 121 * @pgmap: pointer to a struct dev_pagemap
4b94ffdc 122 *
5c2c2587 123 * Notes:
e8d51348
CH
124 * 1/ At a minimum the res, ref and type members of @pgmap must be initialized
125 * by the caller before passing it to this function
126 *
127 * 2/ The altmap field may optionally be initialized, in which case altmap_valid
128 * must be set to true
129 *
a95c90f1
DW
130 * 3/ pgmap->ref must be 'live' on entry and will be killed at
131 * devm_memremap_pages_release() time, or if this routine fails.
5c2c2587 132 *
e8d51348 133 * 4/ res is expected to be a host memory range that could feasibly be
5c2c2587
DW
134 * treated as a "System RAM" range, i.e. not a device mmio range, but
135 * this is not enforced.
4b94ffdc 136 */
e8d51348 137void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
41e94a85 138{
ab1b597e 139 resource_size_t align_start, align_size, align_end;
e8d51348
CH
140 struct vmem_altmap *altmap = pgmap->altmap_valid ?
141 &pgmap->altmap : NULL;
949b9325 142 struct resource *res = &pgmap->res;
966cf44f 143 struct dev_pagemap *conflict_pgmap;
9049771f 144 pgprot_t pgprot = PAGE_KERNEL;
949b9325 145 int error, nid, is_ram;
5f29a77c 146
a95c90f1
DW
147 if (!pgmap->ref || !pgmap->kill)
148 return ERR_PTR(-EINVAL);
149
5f29a77c
DW
150 align_start = res->start & ~(SECTION_SIZE - 1);
151 align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
152 - align_start;
15d36fec
DJ
153 align_end = align_start + align_size - 1;
154
155 conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_start), NULL);
156 if (conflict_pgmap) {
157 dev_WARN(dev, "Conflicting mapping in same section\n");
158 put_dev_pagemap(conflict_pgmap);
159 return ERR_PTR(-ENOMEM);
160 }
161
162 conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_end), NULL);
163 if (conflict_pgmap) {
164 dev_WARN(dev, "Conflicting mapping in same section\n");
165 put_dev_pagemap(conflict_pgmap);
166 return ERR_PTR(-ENOMEM);
167 }
168
d37a14bb
LT
169 is_ram = region_intersects(align_start, align_size,
170 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
41e94a85 171
06489cfb
DW
172 if (is_ram != REGION_DISJOINT) {
173 WARN_ONCE(1, "%s attempted on %s region %pr\n", __func__,
174 is_ram == REGION_MIXED ? "mixed" : "ram", res);
a95c90f1
DW
175 error = -ENXIO;
176 goto err_array;
41e94a85
CH
177 }
178
4b94ffdc 179 pgmap->dev = dev;
4b94ffdc 180
bcfa4b72
MW
181 error = xa_err(xa_store_range(&pgmap_array, PHYS_PFN(res->start),
182 PHYS_PFN(res->end), pgmap, GFP_KERNEL));
9476df7d 183 if (error)
bcfa4b72 184 goto err_array;
9476df7d 185
41e94a85
CH
186 nid = dev_to_node(dev);
187 if (nid < 0)
7eff93b7 188 nid = numa_mem_id();
41e94a85 189
9049771f
DW
190 error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(align_start), 0,
191 align_size);
192 if (error)
193 goto err_pfn_remap;
194
f931ab47 195 mem_hotplug_begin();
69324b8f
DW
196
197 /*
198 * For device private memory we call add_pages() as we only need to
199 * allocate and initialize struct page for the device memory. More-
200 * over the device memory is un-accessible thus we do not want to
201 * create a linear mapping for the memory like arch_add_memory()
202 * would do.
203 *
204 * For all other device memory types, which are accessible by
205 * the CPU, we do want the linear mapping and thus use
206 * arch_add_memory().
207 */
208 if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
209 error = add_pages(nid, align_start >> PAGE_SHIFT,
210 align_size >> PAGE_SHIFT, NULL, false);
211 } else {
212 error = kasan_add_zero_shadow(__va(align_start), align_size);
213 if (error) {
214 mem_hotplug_done();
215 goto err_kasan;
216 }
217
218 error = arch_add_memory(nid, align_start, align_size, altmap,
219 false);
220 }
221
222 if (!error) {
223 struct zone *zone;
224
225 zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
226 move_pfn_range_to_zone(zone, align_start >> PAGE_SHIFT,
227 align_size >> PAGE_SHIFT, altmap);
0207df4f
AR
228 }
229
f931ab47 230 mem_hotplug_done();
9476df7d
DW
231 if (error)
232 goto err_add_memory;
41e94a85 233
966cf44f
AD
234 /*
235 * Initialization of the pages has been deferred until now in order
236 * to allow us to do the work while not holding the hotplug lock.
237 */
238 memmap_init_zone_device(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
239 align_start >> PAGE_SHIFT,
240 align_size >> PAGE_SHIFT, pgmap);
241 percpu_ref_get_many(pgmap->ref, pfn_end(pgmap) - pfn_first(pgmap));
e8d51348 242
a95c90f1
DW
243 error = devm_add_action_or_reset(dev, devm_memremap_pages_release,
244 pgmap);
245 if (error)
246 return ERR_PTR(error);
e8d51348 247
41e94a85 248 return __va(res->start);
9476df7d
DW
249
250 err_add_memory:
0207df4f
AR
251 kasan_remove_zero_shadow(__va(align_start), align_size);
252 err_kasan:
9049771f
DW
253 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
254 err_pfn_remap:
bcfa4b72
MW
255 pgmap_array_delete(res);
256 err_array:
a95c90f1 257 pgmap->kill(pgmap->ref);
9476df7d 258 return ERR_PTR(error);
41e94a85 259}
808153e1 260EXPORT_SYMBOL_GPL(devm_memremap_pages);
4b94ffdc
DW
261
262unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
263{
264 /* number of pfns from base where pfn_to_page() is valid */
265 return altmap->reserve + altmap->free;
266}
267
268void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
269{
270 altmap->alloc -= nr_pfns;
271}
272
0822acb8
CH
273/**
274 * get_dev_pagemap() - take a new live reference on the dev_pagemap for @pfn
275 * @pfn: page frame number to lookup page_map
276 * @pgmap: optional known pgmap that already has a reference
277 *
832d7aa0
CH
278 * If @pgmap is non-NULL and covers @pfn it will be returned as-is. If @pgmap
279 * is non-NULL but does not cover @pfn the reference to it will be released.
0822acb8
CH
280 */
281struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
282 struct dev_pagemap *pgmap)
283{
0822acb8
CH
284 resource_size_t phys = PFN_PHYS(pfn);
285
286 /*
832d7aa0 287 * In the cached case we're already holding a live reference.
0822acb8 288 */
832d7aa0 289 if (pgmap) {
e7744aa2 290 if (phys >= pgmap->res.start && phys <= pgmap->res.end)
832d7aa0
CH
291 return pgmap;
292 put_dev_pagemap(pgmap);
0822acb8
CH
293 }
294
295 /* fall back to slow path lookup */
296 rcu_read_lock();
bcfa4b72 297 pgmap = xa_load(&pgmap_array, PHYS_PFN(phys));
0822acb8
CH
298 if (pgmap && !percpu_ref_tryget_live(pgmap->ref))
299 pgmap = NULL;
300 rcu_read_unlock();
301
302 return pgmap;
303}
e7638488 304EXPORT_SYMBOL_GPL(get_dev_pagemap);
7b2d55d2 305
e7638488
DW
306#ifdef CONFIG_DEV_PAGEMAP_OPS
307DEFINE_STATIC_KEY_FALSE(devmap_managed_key);
31c5bda3 308EXPORT_SYMBOL(devmap_managed_key);
e7638488
DW
309static atomic_t devmap_enable;
310
311/*
312 * Toggle the static key for ->page_free() callbacks when dev_pagemap
313 * pages go idle.
314 */
315void dev_pagemap_get_ops(void)
316{
317 if (atomic_inc_return(&devmap_enable) == 1)
318 static_branch_enable(&devmap_managed_key);
319}
320EXPORT_SYMBOL_GPL(dev_pagemap_get_ops);
321
322void dev_pagemap_put_ops(void)
323{
324 if (atomic_dec_and_test(&devmap_enable))
325 static_branch_disable(&devmap_managed_key);
326}
327EXPORT_SYMBOL_GPL(dev_pagemap_put_ops);
328
329void __put_devmap_managed_page(struct page *page)
7b2d55d2
JG
330{
331 int count = page_ref_dec_return(page);
332
333 /*
334 * If refcount is 1 then page is freed and refcount is stable as nobody
335 * holds a reference on the page.
336 */
337 if (count == 1) {
338 /* Clear Active bit in case of parallel mark_page_accessed */
339 __ClearPageActive(page);
340 __ClearPageWaiters(page);
341
c733a828 342 mem_cgroup_uncharge(page);
7b2d55d2
JG
343
344 page->pgmap->page_free(page, page->pgmap->data);
345 } else if (!count)
346 __put_page(page);
347}
31c5bda3 348EXPORT_SYMBOL(__put_devmap_managed_page);
e7638488 349#endif /* CONFIG_DEV_PAGEMAP_OPS */