btrfs: raid56: reduce indentation in lock_stripe_add
[linux-2.6-block.git] / mm / 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 16
f6a55e1a
CH
17#ifdef CONFIG_DEV_PAGEMAP_OPS
18DEFINE_STATIC_KEY_FALSE(devmap_managed_key);
19EXPORT_SYMBOL(devmap_managed_key);
20static atomic_t devmap_managed_enable;
21
6f42193f 22static void devmap_managed_enable_put(void)
f6a55e1a
CH
23{
24 if (atomic_dec_and_test(&devmap_managed_enable))
25 static_branch_disable(&devmap_managed_key);
26}
27
6f42193f 28static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
f6a55e1a 29{
24917f6b 30 if (!pgmap->ops || !pgmap->ops->page_free) {
f6a55e1a
CH
31 WARN(1, "Missing page_free method\n");
32 return -EINVAL;
33 }
34
35 if (atomic_inc_return(&devmap_managed_enable) == 1)
36 static_branch_enable(&devmap_managed_key);
6f42193f 37 return 0;
f6a55e1a
CH
38}
39#else
6f42193f 40static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
f6a55e1a
CH
41{
42 return -EINVAL;
43}
6f42193f
CH
44static void devmap_managed_enable_put(void)
45{
46}
f6a55e1a
CH
47#endif /* CONFIG_DEV_PAGEMAP_OPS */
48
bcfa4b72 49static void pgmap_array_delete(struct resource *res)
ab1b597e 50{
bcfa4b72
MW
51 xa_store_range(&pgmap_array, PHYS_PFN(res->start), PHYS_PFN(res->end),
52 NULL, GFP_KERNEL);
ab1b597e 53 synchronize_rcu();
9476df7d
DW
54}
55
e7744aa2 56static unsigned long pfn_first(struct dev_pagemap *pgmap)
5c2c2587 57{
7cc7867f 58 return PHYS_PFN(pgmap->res.start) +
514caf23 59 vmem_altmap_offset(pgmap_altmap(pgmap));
5c2c2587
DW
60}
61
e7744aa2 62static unsigned long pfn_end(struct dev_pagemap *pgmap)
5c2c2587 63{
e7744aa2 64 const struct resource *res = &pgmap->res;
5c2c2587
DW
65
66 return (res->start + resource_size(res)) >> PAGE_SHIFT;
67}
68
949b9325
DW
69static unsigned long pfn_next(unsigned long pfn)
70{
71 if (pfn % 1024 == 0)
72 cond_resched();
73 return pfn + 1;
74}
75
5c2c2587 76#define for_each_device_pfn(pfn, map) \
949b9325 77 for (pfn = pfn_first(map); pfn < pfn_end(map); pfn = pfn_next(pfn))
5c2c2587 78
24917f6b
CH
79static void dev_pagemap_kill(struct dev_pagemap *pgmap)
80{
81 if (pgmap->ops && pgmap->ops->kill)
82 pgmap->ops->kill(pgmap);
83 else
84 percpu_ref_kill(pgmap->ref);
85}
86
87static void dev_pagemap_cleanup(struct dev_pagemap *pgmap)
88{
89 if (pgmap->ops && pgmap->ops->cleanup) {
90 pgmap->ops->cleanup(pgmap);
91 } else {
92 wait_for_completion(&pgmap->done);
93 percpu_ref_exit(pgmap->ref);
94 }
06282373
DW
95 /*
96 * Undo the pgmap ref assignment for the internal case as the
97 * caller may re-enable the same pgmap.
98 */
99 if (pgmap->ref == &pgmap->internal_ref)
100 pgmap->ref = NULL;
24917f6b
CH
101}
102
6869b7b2 103void memunmap_pages(struct dev_pagemap *pgmap)
41e94a85 104{
e7744aa2 105 struct resource *res = &pgmap->res;
77e080e7 106 struct page *first_page;
71389703 107 unsigned long pfn;
2c2a5af6 108 int nid;
71389703 109
24917f6b 110 dev_pagemap_kill(pgmap);
e7744aa2 111 for_each_device_pfn(pfn, pgmap)
71389703 112 put_page(pfn_to_page(pfn));
24917f6b 113 dev_pagemap_cleanup(pgmap);
9476df7d 114
77e080e7
AK
115 /* make sure to access a memmap that was actually initialized */
116 first_page = pfn_to_page(pfn_first(pgmap));
117
41e94a85 118 /* pages are dead and unused, undo the arch mapping */
77e080e7 119 nid = page_to_nid(first_page);
2c2a5af6 120
f931ab47 121 mem_hotplug_begin();
69324b8f 122 if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
77e080e7
AK
123 __remove_pages(page_zone(first_page), PHYS_PFN(res->start),
124 PHYS_PFN(resource_size(res)), NULL);
69324b8f 125 } else {
7cc7867f 126 arch_remove_memory(nid, res->start, resource_size(res),
514caf23 127 pgmap_altmap(pgmap));
7cc7867f 128 kasan_remove_zero_shadow(__va(res->start), resource_size(res));
69324b8f 129 }
f931ab47 130 mem_hotplug_done();
b5d24fda 131
7cc7867f 132 untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
bcfa4b72 133 pgmap_array_delete(res);
fdc029b1 134 WARN_ONCE(pgmap->altmap.alloc, "failed to free all reserved pages\n");
6f42193f 135 devmap_managed_enable_put();
9476df7d 136}
6869b7b2
CH
137EXPORT_SYMBOL_GPL(memunmap_pages);
138
139static void devm_memremap_pages_release(void *data)
140{
141 memunmap_pages(data);
142}
9476df7d 143
24917f6b
CH
144static void dev_pagemap_percpu_release(struct percpu_ref *ref)
145{
146 struct dev_pagemap *pgmap =
147 container_of(ref, struct dev_pagemap, internal_ref);
148
149 complete(&pgmap->done);
150}
151
6869b7b2
CH
152/*
153 * Not device managed version of dev_memremap_pages, undone by
154 * memunmap_pages(). Please use dev_memremap_pages if you have a struct
155 * device available.
4b94ffdc 156 */
6869b7b2 157void *memremap_pages(struct dev_pagemap *pgmap, int nid)
41e94a85 158{
949b9325 159 struct resource *res = &pgmap->res;
966cf44f 160 struct dev_pagemap *conflict_pgmap;
940519f0
MH
161 struct mhp_restrictions restrictions = {
162 /*
163 * We do not want any optional features only our own memmap
7cc7867f 164 */
514caf23 165 .altmap = pgmap_altmap(pgmap),
940519f0 166 };
9049771f 167 pgprot_t pgprot = PAGE_KERNEL;
6869b7b2 168 int error, is_ram;
f6a55e1a 169 bool need_devmap_managed = true;
5f29a77c 170
3ed2dcdf
CH
171 switch (pgmap->type) {
172 case MEMORY_DEVICE_PRIVATE:
173 if (!IS_ENABLED(CONFIG_DEVICE_PRIVATE)) {
174 WARN(1, "Device private memory not supported\n");
175 return ERR_PTR(-EINVAL);
176 }
897e6365
CH
177 if (!pgmap->ops || !pgmap->ops->migrate_to_ram) {
178 WARN(1, "Missing migrate_to_ram method\n");
179 return ERR_PTR(-EINVAL);
180 }
3ed2dcdf
CH
181 break;
182 case MEMORY_DEVICE_FS_DAX:
183 if (!IS_ENABLED(CONFIG_ZONE_DEVICE) ||
184 IS_ENABLED(CONFIG_FS_DAX_LIMITED)) {
185 WARN(1, "File system DAX not supported\n");
186 return ERR_PTR(-EINVAL);
187 }
188 break;
189 case MEMORY_DEVICE_DEVDAX:
190 case MEMORY_DEVICE_PCI_P2PDMA:
f6a55e1a 191 need_devmap_managed = false;
3ed2dcdf
CH
192 break;
193 default:
194 WARN(1, "Invalid pgmap type %d\n", pgmap->type);
195 break;
196 }
197
24917f6b
CH
198 if (!pgmap->ref) {
199 if (pgmap->ops && (pgmap->ops->kill || pgmap->ops->cleanup))
200 return ERR_PTR(-EINVAL);
201
202 init_completion(&pgmap->done);
203 error = percpu_ref_init(&pgmap->internal_ref,
204 dev_pagemap_percpu_release, 0, GFP_KERNEL);
205 if (error)
206 return ERR_PTR(error);
207 pgmap->ref = &pgmap->internal_ref;
208 } else {
209 if (!pgmap->ops || !pgmap->ops->kill || !pgmap->ops->cleanup) {
210 WARN(1, "Missing reference count teardown definition\n");
211 return ERR_PTR(-EINVAL);
212 }
50f44ee7 213 }
a95c90f1 214
f6a55e1a 215 if (need_devmap_managed) {
6f42193f 216 error = devmap_managed_enable_get(pgmap);
f6a55e1a
CH
217 if (error)
218 return ERR_PTR(error);
219 }
220
7cc7867f 221 conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->start), NULL);
15d36fec 222 if (conflict_pgmap) {
6869b7b2 223 WARN(1, "Conflicting mapping in same section\n");
15d36fec 224 put_dev_pagemap(conflict_pgmap);
50f44ee7
DW
225 error = -ENOMEM;
226 goto err_array;
15d36fec
DJ
227 }
228
7cc7867f 229 conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->end), NULL);
15d36fec 230 if (conflict_pgmap) {
6869b7b2 231 WARN(1, "Conflicting mapping in same section\n");
15d36fec 232 put_dev_pagemap(conflict_pgmap);
50f44ee7
DW
233 error = -ENOMEM;
234 goto err_array;
15d36fec
DJ
235 }
236
7cc7867f 237 is_ram = region_intersects(res->start, resource_size(res),
d37a14bb 238 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
41e94a85 239
06489cfb
DW
240 if (is_ram != REGION_DISJOINT) {
241 WARN_ONCE(1, "%s attempted on %s region %pr\n", __func__,
242 is_ram == REGION_MIXED ? "mixed" : "ram", res);
a95c90f1
DW
243 error = -ENXIO;
244 goto err_array;
41e94a85
CH
245 }
246
bcfa4b72
MW
247 error = xa_err(xa_store_range(&pgmap_array, PHYS_PFN(res->start),
248 PHYS_PFN(res->end), pgmap, GFP_KERNEL));
9476df7d 249 if (error)
bcfa4b72 250 goto err_array;
9476df7d 251
41e94a85 252 if (nid < 0)
7eff93b7 253 nid = numa_mem_id();
41e94a85 254
7cc7867f
DW
255 error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(res->start), 0,
256 resource_size(res));
9049771f
DW
257 if (error)
258 goto err_pfn_remap;
259
f931ab47 260 mem_hotplug_begin();
69324b8f
DW
261
262 /*
263 * For device private memory we call add_pages() as we only need to
264 * allocate and initialize struct page for the device memory. More-
265 * over the device memory is un-accessible thus we do not want to
266 * create a linear mapping for the memory like arch_add_memory()
267 * would do.
268 *
269 * For all other device memory types, which are accessible by
270 * the CPU, we do want the linear mapping and thus use
271 * arch_add_memory().
272 */
273 if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
7cc7867f
DW
274 error = add_pages(nid, PHYS_PFN(res->start),
275 PHYS_PFN(resource_size(res)), &restrictions);
69324b8f 276 } else {
7cc7867f 277 error = kasan_add_zero_shadow(__va(res->start), resource_size(res));
69324b8f
DW
278 if (error) {
279 mem_hotplug_done();
280 goto err_kasan;
281 }
282
7cc7867f 283 error = arch_add_memory(nid, res->start, resource_size(res),
940519f0 284 &restrictions);
69324b8f
DW
285 }
286
287 if (!error) {
288 struct zone *zone;
289
290 zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
7cc7867f
DW
291 move_pfn_range_to_zone(zone, PHYS_PFN(res->start),
292 PHYS_PFN(resource_size(res)), restrictions.altmap);
0207df4f
AR
293 }
294
f931ab47 295 mem_hotplug_done();
9476df7d
DW
296 if (error)
297 goto err_add_memory;
41e94a85 298
966cf44f
AD
299 /*
300 * Initialization of the pages has been deferred until now in order
301 * to allow us to do the work while not holding the hotplug lock.
302 */
303 memmap_init_zone_device(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
7cc7867f
DW
304 PHYS_PFN(res->start),
305 PHYS_PFN(resource_size(res)), pgmap);
966cf44f 306 percpu_ref_get_many(pgmap->ref, pfn_end(pgmap) - pfn_first(pgmap));
41e94a85 307 return __va(res->start);
9476df7d
DW
308
309 err_add_memory:
7cc7867f 310 kasan_remove_zero_shadow(__va(res->start), resource_size(res));
0207df4f 311 err_kasan:
7cc7867f 312 untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
9049771f 313 err_pfn_remap:
bcfa4b72
MW
314 pgmap_array_delete(res);
315 err_array:
24917f6b
CH
316 dev_pagemap_kill(pgmap);
317 dev_pagemap_cleanup(pgmap);
6f42193f 318 devmap_managed_enable_put();
9476df7d 319 return ERR_PTR(error);
41e94a85 320}
6869b7b2
CH
321EXPORT_SYMBOL_GPL(memremap_pages);
322
323/**
324 * devm_memremap_pages - remap and provide memmap backing for the given resource
325 * @dev: hosting device for @res
326 * @pgmap: pointer to a struct dev_pagemap
327 *
328 * Notes:
329 * 1/ At a minimum the res and type members of @pgmap must be initialized
330 * by the caller before passing it to this function
331 *
332 * 2/ The altmap field may optionally be initialized, in which case
333 * PGMAP_ALTMAP_VALID must be set in pgmap->flags.
334 *
335 * 3/ The ref field may optionally be provided, in which pgmap->ref must be
336 * 'live' on entry and will be killed and reaped at
337 * devm_memremap_pages_release() time, or if this routine fails.
338 *
339 * 4/ res is expected to be a host memory range that could feasibly be
340 * treated as a "System RAM" range, i.e. not a device mmio range, but
341 * this is not enforced.
342 */
343void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
344{
345 int error;
346 void *ret;
347
348 ret = memremap_pages(pgmap, dev_to_node(dev));
349 if (IS_ERR(ret))
350 return ret;
351
352 error = devm_add_action_or_reset(dev, devm_memremap_pages_release,
353 pgmap);
354 if (error)
355 return ERR_PTR(error);
356 return ret;
357}
808153e1 358EXPORT_SYMBOL_GPL(devm_memremap_pages);
4b94ffdc 359
2e3f139e
DW
360void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap)
361{
362 devm_release_action(dev, devm_memremap_pages_release, pgmap);
363}
364EXPORT_SYMBOL_GPL(devm_memunmap_pages);
365
4b94ffdc
DW
366unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
367{
368 /* number of pfns from base where pfn_to_page() is valid */
514caf23
CH
369 if (altmap)
370 return altmap->reserve + altmap->free;
371 return 0;
4b94ffdc
DW
372}
373
374void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
375{
376 altmap->alloc -= nr_pfns;
377}
378
0822acb8
CH
379/**
380 * get_dev_pagemap() - take a new live reference on the dev_pagemap for @pfn
381 * @pfn: page frame number to lookup page_map
382 * @pgmap: optional known pgmap that already has a reference
383 *
832d7aa0
CH
384 * If @pgmap is non-NULL and covers @pfn it will be returned as-is. If @pgmap
385 * is non-NULL but does not cover @pfn the reference to it will be released.
0822acb8
CH
386 */
387struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
388 struct dev_pagemap *pgmap)
389{
0822acb8
CH
390 resource_size_t phys = PFN_PHYS(pfn);
391
392 /*
832d7aa0 393 * In the cached case we're already holding a live reference.
0822acb8 394 */
832d7aa0 395 if (pgmap) {
e7744aa2 396 if (phys >= pgmap->res.start && phys <= pgmap->res.end)
832d7aa0
CH
397 return pgmap;
398 put_dev_pagemap(pgmap);
0822acb8
CH
399 }
400
401 /* fall back to slow path lookup */
402 rcu_read_lock();
bcfa4b72 403 pgmap = xa_load(&pgmap_array, PHYS_PFN(phys));
0822acb8
CH
404 if (pgmap && !percpu_ref_tryget_live(pgmap->ref))
405 pgmap = NULL;
406 rcu_read_unlock();
407
408 return pgmap;
409}
e7638488 410EXPORT_SYMBOL_GPL(get_dev_pagemap);
7b2d55d2 411
e7638488 412#ifdef CONFIG_DEV_PAGEMAP_OPS
e7638488 413void __put_devmap_managed_page(struct page *page)
7b2d55d2
JG
414{
415 int count = page_ref_dec_return(page);
416
417 /*
418 * If refcount is 1 then page is freed and refcount is stable as nobody
419 * holds a reference on the page.
420 */
421 if (count == 1) {
422 /* Clear Active bit in case of parallel mark_page_accessed */
423 __ClearPageActive(page);
424 __ClearPageWaiters(page);
425
c733a828 426 mem_cgroup_uncharge(page);
7b2d55d2 427
7ab0ad0e
RC
428 /*
429 * When a device_private page is freed, the page->mapping field
430 * may still contain a (stale) mapping value. For example, the
431 * lower bits of page->mapping may still identify the page as
432 * an anonymous page. Ultimately, this entire field is just
433 * stale and wrong, and it will cause errors if not cleared.
434 * One example is:
435 *
436 * migrate_vma_pages()
437 * migrate_vma_insert_page()
438 * page_add_new_anon_rmap()
439 * __page_set_anon_rmap()
440 * ...checks page->mapping, via PageAnon(page) call,
441 * and incorrectly concludes that the page is an
442 * anonymous page. Therefore, it incorrectly,
443 * silently fails to set up the new anon rmap.
444 *
445 * For other types of ZONE_DEVICE pages, migration is either
446 * handled differently or not done at all, so there is no need
447 * to clear page->mapping.
448 */
449 if (is_device_private_page(page))
450 page->mapping = NULL;
451
80a72d0a 452 page->pgmap->ops->page_free(page);
7b2d55d2
JG
453 } else if (!count)
454 __put_page(page);
455}
31c5bda3 456EXPORT_SYMBOL(__put_devmap_managed_page);
e7638488 457#endif /* CONFIG_DEV_PAGEMAP_OPS */