blob: 32c79b51af8678bead9702a1447b7def15050ad9 [file] [log] [blame]
Dan Williams59816902018-03-29 19:07:13 -07001/* SPDX-License-Identifier: GPL-2.0 */
2/* Copyright(c) 2015 Intel Corporation. All rights reserved. */
Christoph Hellwig7d3dcf22015-08-10 23:07:07 -04003#include <linux/device.h>
Dan Williams92281dee2015-08-10 23:07:06 -04004#include <linux/io.h>
Andrey Ryabinin0207df42018-08-17 15:47:04 -07005#include <linux/kasan.h>
Christoph Hellwig41e94a82015-08-17 16:00:35 +02006#include <linux/memory_hotplug.h>
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -04007#include <linux/mm.h>
8#include <linux/pfn_t.h>
Jérôme Glisse5042db42017-09-08 16:11:43 -07009#include <linux/swap.h>
10#include <linux/swapops.h>
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -040011#include <linux/types.h>
Dan Williamse76384882018-05-16 11:46:08 -070012#include <linux/wait_bit.h>
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -040013#include <linux/xarray.h>
Dan Williams92281dee2015-08-10 23:07:06 -040014
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -040015static DEFINE_XARRAY(pgmap_array);
Dan Williams9476df72016-01-15 16:56:19 -080016#define SECTION_MASK ~((1UL << PA_SECTION_SHIFT) - 1)
17#define SECTION_SIZE (1UL << PA_SECTION_SHIFT)
18
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020019#ifdef CONFIG_DEV_PAGEMAP_OPS
20DEFINE_STATIC_KEY_FALSE(devmap_managed_key);
21EXPORT_SYMBOL(devmap_managed_key);
22static atomic_t devmap_managed_enable;
23
Christoph Hellwig6f421932019-08-18 11:05:56 +020024static void devmap_managed_enable_put(void)
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020025{
26 if (atomic_dec_and_test(&devmap_managed_enable))
27 static_branch_disable(&devmap_managed_key);
28}
29
Christoph Hellwig6f421932019-08-18 11:05:56 +020030static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020031{
Christoph Hellwig24917f62019-06-26 14:27:14 +020032 if (!pgmap->ops || !pgmap->ops->page_free) {
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020033 WARN(1, "Missing page_free method\n");
34 return -EINVAL;
35 }
36
37 if (atomic_inc_return(&devmap_managed_enable) == 1)
38 static_branch_enable(&devmap_managed_key);
Christoph Hellwig6f421932019-08-18 11:05:56 +020039 return 0;
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020040}
41#else
Christoph Hellwig6f421932019-08-18 11:05:56 +020042static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020043{
44 return -EINVAL;
45}
Christoph Hellwig6f421932019-08-18 11:05:56 +020046static void devmap_managed_enable_put(void)
47{
48}
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020049#endif /* CONFIG_DEV_PAGEMAP_OPS */
50
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -040051static void pgmap_array_delete(struct resource *res)
Christoph Hellwig41e94a82015-08-17 16:00:35 +020052{
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -040053 xa_store_range(&pgmap_array, PHYS_PFN(res->start), PHYS_PFN(res->end),
54 NULL, GFP_KERNEL);
Dan Williamsab1b5972017-09-06 16:24:13 -070055 synchronize_rcu();
Dan Williams9476df72016-01-15 16:56:19 -080056}
57
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010058static unsigned long pfn_first(struct dev_pagemap *pgmap)
Dan Williams5c2c2582016-01-15 16:56:49 -080059{
Dan Williams7cc78672019-07-18 15:58:33 -070060 return PHYS_PFN(pgmap->res.start) +
Christoph Hellwig514caf22019-06-26 14:27:13 +020061 vmem_altmap_offset(pgmap_altmap(pgmap));
Dan Williams5c2c2582016-01-15 16:56:49 -080062}
63
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010064static unsigned long pfn_end(struct dev_pagemap *pgmap)
Dan Williams5c2c2582016-01-15 16:56:49 -080065{
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010066 const struct resource *res = &pgmap->res;
Dan Williams5c2c2582016-01-15 16:56:49 -080067
68 return (res->start + resource_size(res)) >> PAGE_SHIFT;
69}
70
Dan Williams949b93252018-02-06 19:34:11 -080071static unsigned long pfn_next(unsigned long pfn)
72{
73 if (pfn % 1024 == 0)
74 cond_resched();
75 return pfn + 1;
76}
77
Dan Williams5c2c2582016-01-15 16:56:49 -080078#define for_each_device_pfn(pfn, map) \
Dan Williams949b93252018-02-06 19:34:11 -080079 for (pfn = pfn_first(map); pfn < pfn_end(map); pfn = pfn_next(pfn))
Dan Williams5c2c2582016-01-15 16:56:49 -080080
Christoph Hellwig24917f62019-06-26 14:27:14 +020081static void dev_pagemap_kill(struct dev_pagemap *pgmap)
82{
83 if (pgmap->ops && pgmap->ops->kill)
84 pgmap->ops->kill(pgmap);
85 else
86 percpu_ref_kill(pgmap->ref);
87}
88
89static void dev_pagemap_cleanup(struct dev_pagemap *pgmap)
90{
91 if (pgmap->ops && pgmap->ops->cleanup) {
92 pgmap->ops->cleanup(pgmap);
93 } else {
94 wait_for_completion(&pgmap->done);
95 percpu_ref_exit(pgmap->ref);
96 }
Dan Williams062823732019-08-08 14:43:49 -070097 /*
98 * Undo the pgmap ref assignment for the internal case as the
99 * caller may re-enable the same pgmap.
100 */
101 if (pgmap->ref == &pgmap->internal_ref)
102 pgmap->ref = NULL;
Christoph Hellwig24917f62019-06-26 14:27:14 +0200103}
104
Christoph Hellwig6869b7b22019-08-18 11:05:57 +0200105void memunmap_pages(struct dev_pagemap *pgmap)
Dan Williams9476df72016-01-15 16:56:19 -0800106{
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100107 struct resource *res = &pgmap->res;
Dan Williams71389702017-04-28 10:23:37 -0700108 unsigned long pfn;
Oscar Salvador2c2a5af2018-12-28 00:36:22 -0800109 int nid;
Dan Williams71389702017-04-28 10:23:37 -0700110
Christoph Hellwig24917f62019-06-26 14:27:14 +0200111 dev_pagemap_kill(pgmap);
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100112 for_each_device_pfn(pfn, pgmap)
Dan Williams71389702017-04-28 10:23:37 -0700113 put_page(pfn_to_page(pfn));
Christoph Hellwig24917f62019-06-26 14:27:14 +0200114 dev_pagemap_cleanup(pgmap);
Dan Williams9476df72016-01-15 16:56:19 -0800115
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200116 /* pages are dead and unused, undo the arch mapping */
Dan Williams7cc78672019-07-18 15:58:33 -0700117 nid = page_to_nid(pfn_to_page(PHYS_PFN(res->start)));
Oscar Salvador2c2a5af2018-12-28 00:36:22 -0800118
Dan Williamsf931ab42017-01-10 16:57:36 -0800119 mem_hotplug_begin();
Dan Williams69324b82018-12-28 00:35:01 -0800120 if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
Dan Williams7cc78672019-07-18 15:58:33 -0700121 pfn = PHYS_PFN(res->start);
Dan Williams69324b82018-12-28 00:35:01 -0800122 __remove_pages(page_zone(pfn_to_page(pfn)), pfn,
Dan Williams7cc78672019-07-18 15:58:33 -0700123 PHYS_PFN(resource_size(res)), NULL);
Dan Williams69324b82018-12-28 00:35:01 -0800124 } else {
Dan Williams7cc78672019-07-18 15:58:33 -0700125 arch_remove_memory(nid, res->start, resource_size(res),
Christoph Hellwig514caf22019-06-26 14:27:13 +0200126 pgmap_altmap(pgmap));
Dan Williams7cc78672019-07-18 15:58:33 -0700127 kasan_remove_zero_shadow(__va(res->start), resource_size(res));
Dan Williams69324b82018-12-28 00:35:01 -0800128 }
Dan Williamsf931ab42017-01-10 16:57:36 -0800129 mem_hotplug_done();
Dan Williamsb5d24fd2017-02-24 14:55:45 -0800130
Dan Williams7cc78672019-07-18 15:58:33 -0700131 untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400132 pgmap_array_delete(res);
Christoph Hellwigfdc029b2019-08-18 11:05:55 +0200133 WARN_ONCE(pgmap->altmap.alloc, "failed to free all reserved pages\n");
Christoph Hellwig6f421932019-08-18 11:05:56 +0200134 devmap_managed_enable_put();
Dan Williams9476df72016-01-15 16:56:19 -0800135}
Christoph Hellwig6869b7b22019-08-18 11:05:57 +0200136EXPORT_SYMBOL_GPL(memunmap_pages);
137
138static void devm_memremap_pages_release(void *data)
139{
140 memunmap_pages(data);
141}
Dan Williams9476df72016-01-15 16:56:19 -0800142
Christoph Hellwig24917f62019-06-26 14:27:14 +0200143static void dev_pagemap_percpu_release(struct percpu_ref *ref)
144{
145 struct dev_pagemap *pgmap =
146 container_of(ref, struct dev_pagemap, internal_ref);
147
148 complete(&pgmap->done);
149}
150
Christoph Hellwig6869b7b22019-08-18 11:05:57 +0200151/*
152 * Not device managed version of dev_memremap_pages, undone by
153 * memunmap_pages(). Please use dev_memremap_pages if you have a struct
154 * device available.
Dan Williams4b94ffd2016-01-15 16:56:22 -0800155 */
Christoph Hellwig6869b7b22019-08-18 11:05:57 +0200156void *memremap_pages(struct dev_pagemap *pgmap, int nid)
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200157{
Dan Williams949b93252018-02-06 19:34:11 -0800158 struct resource *res = &pgmap->res;
Dave Jiang15d36fe2018-07-26 16:37:15 -0700159 struct dev_pagemap *conflict_pgmap;
Michal Hocko940519f2019-05-13 17:21:26 -0700160 struct mhp_restrictions restrictions = {
161 /*
162 * We do not want any optional features only our own memmap
Dan Williams7cc78672019-07-18 15:58:33 -0700163 */
Christoph Hellwig514caf22019-06-26 14:27:13 +0200164 .altmap = pgmap_altmap(pgmap),
Michal Hocko940519f2019-05-13 17:21:26 -0700165 };
Alexander Duyck966cf442018-10-26 15:07:52 -0700166 pgprot_t pgprot = PAGE_KERNEL;
Christoph Hellwig6869b7b22019-08-18 11:05:57 +0200167 int error, is_ram;
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200168 bool need_devmap_managed = true;
Dan Williams5f29a772016-03-09 14:08:13 -0800169
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +0200170 switch (pgmap->type) {
171 case MEMORY_DEVICE_PRIVATE:
172 if (!IS_ENABLED(CONFIG_DEVICE_PRIVATE)) {
173 WARN(1, "Device private memory not supported\n");
174 return ERR_PTR(-EINVAL);
175 }
Christoph Hellwig897e6362019-06-26 14:27:11 +0200176 if (!pgmap->ops || !pgmap->ops->migrate_to_ram) {
177 WARN(1, "Missing migrate_to_ram method\n");
178 return ERR_PTR(-EINVAL);
179 }
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +0200180 break;
181 case MEMORY_DEVICE_FS_DAX:
182 if (!IS_ENABLED(CONFIG_ZONE_DEVICE) ||
183 IS_ENABLED(CONFIG_FS_DAX_LIMITED)) {
184 WARN(1, "File system DAX not supported\n");
185 return ERR_PTR(-EINVAL);
186 }
187 break;
188 case MEMORY_DEVICE_DEVDAX:
189 case MEMORY_DEVICE_PCI_P2PDMA:
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200190 need_devmap_managed = false;
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +0200191 break;
192 default:
193 WARN(1, "Invalid pgmap type %d\n", pgmap->type);
194 break;
195 }
196
Christoph Hellwig24917f62019-06-26 14:27:14 +0200197 if (!pgmap->ref) {
198 if (pgmap->ops && (pgmap->ops->kill || pgmap->ops->cleanup))
199 return ERR_PTR(-EINVAL);
200
201 init_completion(&pgmap->done);
202 error = percpu_ref_init(&pgmap->internal_ref,
203 dev_pagemap_percpu_release, 0, GFP_KERNEL);
204 if (error)
205 return ERR_PTR(error);
206 pgmap->ref = &pgmap->internal_ref;
207 } else {
208 if (!pgmap->ops || !pgmap->ops->kill || !pgmap->ops->cleanup) {
209 WARN(1, "Missing reference count teardown definition\n");
210 return ERR_PTR(-EINVAL);
211 }
Dan Williams50f44ee2019-06-13 15:56:33 -0700212 }
Dan Williamsa95c90f2018-12-28 00:34:57 -0800213
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200214 if (need_devmap_managed) {
Christoph Hellwig6f421932019-08-18 11:05:56 +0200215 error = devmap_managed_enable_get(pgmap);
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200216 if (error)
217 return ERR_PTR(error);
218 }
219
Dan Williams7cc78672019-07-18 15:58:33 -0700220 conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->start), NULL);
Dave Jiang15d36fe2018-07-26 16:37:15 -0700221 if (conflict_pgmap) {
Christoph Hellwig6869b7b22019-08-18 11:05:57 +0200222 WARN(1, "Conflicting mapping in same section\n");
Dave Jiang15d36fe2018-07-26 16:37:15 -0700223 put_dev_pagemap(conflict_pgmap);
Dan Williams50f44ee2019-06-13 15:56:33 -0700224 error = -ENOMEM;
225 goto err_array;
Dave Jiang15d36fe2018-07-26 16:37:15 -0700226 }
227
Dan Williams7cc78672019-07-18 15:58:33 -0700228 conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->end), NULL);
Dave Jiang15d36fe2018-07-26 16:37:15 -0700229 if (conflict_pgmap) {
Christoph Hellwig6869b7b22019-08-18 11:05:57 +0200230 WARN(1, "Conflicting mapping in same section\n");
Dave Jiang15d36fe2018-07-26 16:37:15 -0700231 put_dev_pagemap(conflict_pgmap);
Dan Williams50f44ee2019-06-13 15:56:33 -0700232 error = -ENOMEM;
233 goto err_array;
Dave Jiang15d36fe2018-07-26 16:37:15 -0700234 }
235
Dan Williams7cc78672019-07-18 15:58:33 -0700236 is_ram = region_intersects(res->start, resource_size(res),
Linus Torvaldsd37a14bb2016-03-14 15:15:51 -0700237 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200238
Dan Williams06489cf2018-12-28 00:34:54 -0800239 if (is_ram != REGION_DISJOINT) {
240 WARN_ONCE(1, "%s attempted on %s region %pr\n", __func__,
241 is_ram == REGION_MIXED ? "mixed" : "ram", res);
Dan Williamsa95c90f2018-12-28 00:34:57 -0800242 error = -ENXIO;
243 goto err_array;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200244 }
245
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400246 error = xa_err(xa_store_range(&pgmap_array, PHYS_PFN(res->start),
247 PHYS_PFN(res->end), pgmap, GFP_KERNEL));
Dan Williams9476df72016-01-15 16:56:19 -0800248 if (error)
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400249 goto err_array;
Dan Williams9476df72016-01-15 16:56:19 -0800250
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200251 if (nid < 0)
Dan Williams7eff93b2015-10-05 20:35:55 -0400252 nid = numa_mem_id();
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200253
Dan Williams7cc78672019-07-18 15:58:33 -0700254 error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(res->start), 0,
255 resource_size(res));
Dan Williams90497712016-09-07 08:51:21 -0700256 if (error)
257 goto err_pfn_remap;
258
Dan Williamsf931ab42017-01-10 16:57:36 -0800259 mem_hotplug_begin();
Dan Williams69324b82018-12-28 00:35:01 -0800260
261 /*
262 * For device private memory we call add_pages() as we only need to
263 * allocate and initialize struct page for the device memory. More-
264 * over the device memory is un-accessible thus we do not want to
265 * create a linear mapping for the memory like arch_add_memory()
266 * would do.
267 *
268 * For all other device memory types, which are accessible by
269 * the CPU, we do want the linear mapping and thus use
270 * arch_add_memory().
271 */
272 if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
Dan Williams7cc78672019-07-18 15:58:33 -0700273 error = add_pages(nid, PHYS_PFN(res->start),
274 PHYS_PFN(resource_size(res)), &restrictions);
Dan Williams69324b82018-12-28 00:35:01 -0800275 } else {
Dan Williams7cc78672019-07-18 15:58:33 -0700276 error = kasan_add_zero_shadow(__va(res->start), resource_size(res));
Dan Williams69324b82018-12-28 00:35:01 -0800277 if (error) {
278 mem_hotplug_done();
279 goto err_kasan;
280 }
281
Dan Williams7cc78672019-07-18 15:58:33 -0700282 error = arch_add_memory(nid, res->start, resource_size(res),
Michal Hocko940519f2019-05-13 17:21:26 -0700283 &restrictions);
Andrey Ryabinin0207df42018-08-17 15:47:04 -0700284 }
285
Dan Williams69324b82018-12-28 00:35:01 -0800286 if (!error) {
287 struct zone *zone;
288
289 zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
Dan Williams7cc78672019-07-18 15:58:33 -0700290 move_pfn_range_to_zone(zone, PHYS_PFN(res->start),
291 PHYS_PFN(resource_size(res)), restrictions.altmap);
Dan Williams69324b82018-12-28 00:35:01 -0800292 }
293
Dan Williamsf931ab42017-01-10 16:57:36 -0800294 mem_hotplug_done();
Dan Williams9476df72016-01-15 16:56:19 -0800295 if (error)
296 goto err_add_memory;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200297
Alexander Duyck966cf442018-10-26 15:07:52 -0700298 /*
299 * Initialization of the pages has been deferred until now in order
300 * to allow us to do the work while not holding the hotplug lock.
301 */
302 memmap_init_zone_device(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
Dan Williams7cc78672019-07-18 15:58:33 -0700303 PHYS_PFN(res->start),
304 PHYS_PFN(resource_size(res)), pgmap);
Alexander Duyck966cf442018-10-26 15:07:52 -0700305 percpu_ref_get_many(pgmap->ref, pfn_end(pgmap) - pfn_first(pgmap));
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200306 return __va(res->start);
Dan Williams9476df72016-01-15 16:56:19 -0800307
308 err_add_memory:
Dan Williams7cc78672019-07-18 15:58:33 -0700309 kasan_remove_zero_shadow(__va(res->start), resource_size(res));
Andrey Ryabinin0207df42018-08-17 15:47:04 -0700310 err_kasan:
Dan Williams7cc78672019-07-18 15:58:33 -0700311 untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
Dan Williams90497712016-09-07 08:51:21 -0700312 err_pfn_remap:
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400313 pgmap_array_delete(res);
314 err_array:
Christoph Hellwig24917f62019-06-26 14:27:14 +0200315 dev_pagemap_kill(pgmap);
316 dev_pagemap_cleanup(pgmap);
Christoph Hellwig6f421932019-08-18 11:05:56 +0200317 devmap_managed_enable_put();
Dan Williams9476df72016-01-15 16:56:19 -0800318 return ERR_PTR(error);
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200319}
Christoph Hellwig6869b7b22019-08-18 11:05:57 +0200320EXPORT_SYMBOL_GPL(memremap_pages);
321
322/**
323 * devm_memremap_pages - remap and provide memmap backing for the given resource
324 * @dev: hosting device for @res
325 * @pgmap: pointer to a struct dev_pagemap
326 *
327 * Notes:
328 * 1/ At a minimum the res and type members of @pgmap must be initialized
329 * by the caller before passing it to this function
330 *
331 * 2/ The altmap field may optionally be initialized, in which case
332 * PGMAP_ALTMAP_VALID must be set in pgmap->flags.
333 *
334 * 3/ The ref field may optionally be provided, in which pgmap->ref must be
335 * 'live' on entry and will be killed and reaped at
336 * devm_memremap_pages_release() time, or if this routine fails.
337 *
338 * 4/ res is expected to be a host memory range that could feasibly be
339 * treated as a "System RAM" range, i.e. not a device mmio range, but
340 * this is not enforced.
341 */
342void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
343{
344 int error;
345 void *ret;
346
347 ret = memremap_pages(pgmap, dev_to_node(dev));
348 if (IS_ERR(ret))
349 return ret;
350
351 error = devm_add_action_or_reset(dev, devm_memremap_pages_release,
352 pgmap);
353 if (error)
354 return ERR_PTR(error);
355 return ret;
356}
Dan Williams808153e2018-12-28 00:34:50 -0800357EXPORT_SYMBOL_GPL(devm_memremap_pages);
Dan Williams4b94ffd2016-01-15 16:56:22 -0800358
Dan Williams2e3f1392019-06-13 15:56:21 -0700359void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap)
360{
361 devm_release_action(dev, devm_memremap_pages_release, pgmap);
362}
363EXPORT_SYMBOL_GPL(devm_memunmap_pages);
364
Dan Williams4b94ffd2016-01-15 16:56:22 -0800365unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
366{
367 /* number of pfns from base where pfn_to_page() is valid */
Christoph Hellwig514caf22019-06-26 14:27:13 +0200368 if (altmap)
369 return altmap->reserve + altmap->free;
370 return 0;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800371}
372
373void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
374{
375 altmap->alloc -= nr_pfns;
376}
377
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100378/**
379 * get_dev_pagemap() - take a new live reference on the dev_pagemap for @pfn
380 * @pfn: page frame number to lookup page_map
381 * @pgmap: optional known pgmap that already has a reference
382 *
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100383 * If @pgmap is non-NULL and covers @pfn it will be returned as-is. If @pgmap
384 * is non-NULL but does not cover @pfn the reference to it will be released.
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100385 */
386struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
387 struct dev_pagemap *pgmap)
388{
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100389 resource_size_t phys = PFN_PHYS(pfn);
390
391 /*
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100392 * In the cached case we're already holding a live reference.
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100393 */
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100394 if (pgmap) {
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100395 if (phys >= pgmap->res.start && phys <= pgmap->res.end)
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100396 return pgmap;
397 put_dev_pagemap(pgmap);
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100398 }
399
400 /* fall back to slow path lookup */
401 rcu_read_lock();
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400402 pgmap = xa_load(&pgmap_array, PHYS_PFN(phys));
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100403 if (pgmap && !percpu_ref_tryget_live(pgmap->ref))
404 pgmap = NULL;
405 rcu_read_unlock();
406
407 return pgmap;
408}
Dan Williamse76384882018-05-16 11:46:08 -0700409EXPORT_SYMBOL_GPL(get_dev_pagemap);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700410
Dan Williamse76384882018-05-16 11:46:08 -0700411#ifdef CONFIG_DEV_PAGEMAP_OPS
Dan Williamse76384882018-05-16 11:46:08 -0700412void __put_devmap_managed_page(struct page *page)
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700413{
414 int count = page_ref_dec_return(page);
415
416 /*
417 * If refcount is 1 then page is freed and refcount is stable as nobody
418 * holds a reference on the page.
419 */
420 if (count == 1) {
421 /* Clear Active bit in case of parallel mark_page_accessed */
422 __ClearPageActive(page);
423 __ClearPageWaiters(page);
424
Jérôme Glissec733a822017-09-08 16:11:54 -0700425 mem_cgroup_uncharge(page);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700426
Ralph Campbell7ab0ad02019-08-13 15:37:07 -0700427 /*
428 * When a device_private page is freed, the page->mapping field
429 * may still contain a (stale) mapping value. For example, the
430 * lower bits of page->mapping may still identify the page as
431 * an anonymous page. Ultimately, this entire field is just
432 * stale and wrong, and it will cause errors if not cleared.
433 * One example is:
434 *
435 * migrate_vma_pages()
436 * migrate_vma_insert_page()
437 * page_add_new_anon_rmap()
438 * __page_set_anon_rmap()
439 * ...checks page->mapping, via PageAnon(page) call,
440 * and incorrectly concludes that the page is an
441 * anonymous page. Therefore, it incorrectly,
442 * silently fails to set up the new anon rmap.
443 *
444 * For other types of ZONE_DEVICE pages, migration is either
445 * handled differently or not done at all, so there is no need
446 * to clear page->mapping.
447 */
448 if (is_device_private_page(page))
449 page->mapping = NULL;
450
Christoph Hellwig80a72d02019-06-26 14:27:12 +0200451 page->pgmap->ops->page_free(page);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700452 } else if (!count)
453 __put_page(page);
454}
Dan Williams31c5bda2018-07-26 16:37:22 -0700455EXPORT_SYMBOL(__put_devmap_managed_page);
Dan Williamse76384882018-05-16 11:46:08 -0700456#endif /* CONFIG_DEV_PAGEMAP_OPS */