blob: c51c6bd2fe3425f0fc00dae12f29dcec5d395a42 [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
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020017#ifdef CONFIG_DEV_PAGEMAP_OPS
18DEFINE_STATIC_KEY_FALSE(devmap_managed_key);
19EXPORT_SYMBOL(devmap_managed_key);
20static atomic_t devmap_managed_enable;
21
Christoph Hellwig6f421932019-08-18 11:05:56 +020022static void devmap_managed_enable_put(void)
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020023{
24 if (atomic_dec_and_test(&devmap_managed_enable))
25 static_branch_disable(&devmap_managed_key);
26}
27
Christoph Hellwig6f421932019-08-18 11:05:56 +020028static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020029{
Christoph Hellwig24917f62019-06-26 14:27:14 +020030 if (!pgmap->ops || !pgmap->ops->page_free) {
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020031 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);
Christoph Hellwig6f421932019-08-18 11:05:56 +020037 return 0;
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020038}
39#else
Christoph Hellwig6f421932019-08-18 11:05:56 +020040static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020041{
42 return -EINVAL;
43}
Christoph Hellwig6f421932019-08-18 11:05:56 +020044static void devmap_managed_enable_put(void)
45{
46}
Christoph Hellwigf6a55e12019-06-26 14:27:10 +020047#endif /* CONFIG_DEV_PAGEMAP_OPS */
48
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -040049static void pgmap_array_delete(struct resource *res)
Christoph Hellwig41e94a82015-08-17 16:00:35 +020050{
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -040051 xa_store_range(&pgmap_array, PHYS_PFN(res->start), PHYS_PFN(res->end),
52 NULL, GFP_KERNEL);
Dan Williamsab1b5972017-09-06 16:24:13 -070053 synchronize_rcu();
Dan Williams9476df72016-01-15 16:56:19 -080054}
55
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010056static unsigned long pfn_first(struct dev_pagemap *pgmap)
Dan Williams5c2c2582016-01-15 16:56:49 -080057{
Dan Williams7cc78672019-07-18 15:58:33 -070058 return PHYS_PFN(pgmap->res.start) +
Christoph Hellwig514caf22019-06-26 14:27:13 +020059 vmem_altmap_offset(pgmap_altmap(pgmap));
Dan Williams5c2c2582016-01-15 16:56:49 -080060}
61
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010062static unsigned long pfn_end(struct dev_pagemap *pgmap)
Dan Williams5c2c2582016-01-15 16:56:49 -080063{
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010064 const struct resource *res = &pgmap->res;
Dan Williams5c2c2582016-01-15 16:56:49 -080065
66 return (res->start + resource_size(res)) >> PAGE_SHIFT;
67}
68
Dan Williams949b93252018-02-06 19:34:11 -080069static unsigned long pfn_next(unsigned long pfn)
70{
71 if (pfn % 1024 == 0)
72 cond_resched();
73 return pfn + 1;
74}
75
Dan Williams5c2c2582016-01-15 16:56:49 -080076#define for_each_device_pfn(pfn, map) \
Dan Williams949b93252018-02-06 19:34:11 -080077 for (pfn = pfn_first(map); pfn < pfn_end(map); pfn = pfn_next(pfn))
Dan Williams5c2c2582016-01-15 16:56:49 -080078
Christoph Hellwig24917f62019-06-26 14:27:14 +020079static 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 }
Dan Williams062823732019-08-08 14:43:49 -070095 /*
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;
Christoph Hellwig24917f62019-06-26 14:27:14 +0200101}
102
Christoph Hellwig6869b7b2019-08-18 11:05:57 +0200103void memunmap_pages(struct dev_pagemap *pgmap)
Dan Williams9476df72016-01-15 16:56:19 -0800104{
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100105 struct resource *res = &pgmap->res;
Aneesh Kumar K.V77e080e2019-10-18 20:19:39 -0700106 struct page *first_page;
Dan Williams71389702017-04-28 10:23:37 -0700107 unsigned long pfn;
Oscar Salvador2c2a5af2018-12-28 00:36:22 -0800108 int nid;
Dan Williams71389702017-04-28 10:23:37 -0700109
Christoph Hellwig24917f62019-06-26 14:27:14 +0200110 dev_pagemap_kill(pgmap);
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100111 for_each_device_pfn(pfn, pgmap)
Dan Williams71389702017-04-28 10:23:37 -0700112 put_page(pfn_to_page(pfn));
Christoph Hellwig24917f62019-06-26 14:27:14 +0200113 dev_pagemap_cleanup(pgmap);
Dan Williams9476df72016-01-15 16:56:19 -0800114
Aneesh Kumar K.V77e080e2019-10-18 20:19:39 -0700115 /* make sure to access a memmap that was actually initialized */
116 first_page = pfn_to_page(pfn_first(pgmap));
117
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200118 /* pages are dead and unused, undo the arch mapping */
Aneesh Kumar K.V77e080e2019-10-18 20:19:39 -0700119 nid = page_to_nid(first_page);
Oscar Salvador2c2a5af2018-12-28 00:36:22 -0800120
Dan Williamsf931ab42017-01-10 16:57:36 -0800121 mem_hotplug_begin();
Dan Williams69324b82018-12-28 00:35:01 -0800122 if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
David Hildenbrande84c5b72020-01-04 12:59:33 -0800123 __remove_pages(PHYS_PFN(res->start),
Aneesh Kumar K.V77e080e2019-10-18 20:19:39 -0700124 PHYS_PFN(resource_size(res)), NULL);
Dan Williams69324b82018-12-28 00:35:01 -0800125 } else {
Dan Williams7cc78672019-07-18 15:58:33 -0700126 arch_remove_memory(nid, res->start, resource_size(res),
Christoph Hellwig514caf22019-06-26 14:27:13 +0200127 pgmap_altmap(pgmap));
Dan Williams7cc78672019-07-18 15:58:33 -0700128 kasan_remove_zero_shadow(__va(res->start), resource_size(res));
Dan Williams69324b82018-12-28 00:35:01 -0800129 }
Dan Williamsf931ab42017-01-10 16:57:36 -0800130 mem_hotplug_done();
Dan Williamsb5d24fd2017-02-24 14:55:45 -0800131
Dan Williams7cc78672019-07-18 15:58:33 -0700132 untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400133 pgmap_array_delete(res);
Christoph Hellwigfdc029b2019-08-18 11:05:55 +0200134 WARN_ONCE(pgmap->altmap.alloc, "failed to free all reserved pages\n");
Christoph Hellwig6f421932019-08-18 11:05:56 +0200135 devmap_managed_enable_put();
Dan Williams9476df72016-01-15 16:56:19 -0800136}
Christoph Hellwig6869b7b2019-08-18 11:05:57 +0200137EXPORT_SYMBOL_GPL(memunmap_pages);
138
139static void devm_memremap_pages_release(void *data)
140{
141 memunmap_pages(data);
142}
Dan Williams9476df72016-01-15 16:56:19 -0800143
Christoph Hellwig24917f62019-06-26 14:27:14 +0200144static 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
Christoph Hellwig6869b7b2019-08-18 11:05:57 +0200152/*
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.
Dan Williams4b94ffd2016-01-15 16:56:22 -0800156 */
Christoph Hellwig6869b7b2019-08-18 11:05:57 +0200157void *memremap_pages(struct dev_pagemap *pgmap, int nid)
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200158{
Dan Williams949b93252018-02-06 19:34:11 -0800159 struct resource *res = &pgmap->res;
Dave Jiang15d36fe2018-07-26 16:37:15 -0700160 struct dev_pagemap *conflict_pgmap;
Michal Hocko940519f2019-05-13 17:21:26 -0700161 struct mhp_restrictions restrictions = {
162 /*
163 * We do not want any optional features only our own memmap
Dan Williams7cc78672019-07-18 15:58:33 -0700164 */
Christoph Hellwig514caf22019-06-26 14:27:13 +0200165 .altmap = pgmap_altmap(pgmap),
Michal Hocko940519f2019-05-13 17:21:26 -0700166 };
Alexander Duyck966cf442018-10-26 15:07:52 -0700167 pgprot_t pgprot = PAGE_KERNEL;
Christoph Hellwig6869b7b2019-08-18 11:05:57 +0200168 int error, is_ram;
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200169 bool need_devmap_managed = true;
Dan Williams5f29a772016-03-09 14:08:13 -0800170
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +0200171 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 }
Christoph Hellwig897e6362019-06-26 14:27:11 +0200177 if (!pgmap->ops || !pgmap->ops->migrate_to_ram) {
178 WARN(1, "Missing migrate_to_ram method\n");
179 return ERR_PTR(-EINVAL);
180 }
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +0200181 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:
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200191 need_devmap_managed = false;
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +0200192 break;
193 default:
194 WARN(1, "Invalid pgmap type %d\n", pgmap->type);
195 break;
196 }
197
Christoph Hellwig24917f62019-06-26 14:27:14 +0200198 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 }
Dan Williams50f44ee2019-06-13 15:56:33 -0700213 }
Dan Williamsa95c90f2018-12-28 00:34:57 -0800214
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200215 if (need_devmap_managed) {
Christoph Hellwig6f421932019-08-18 11:05:56 +0200216 error = devmap_managed_enable_get(pgmap);
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200217 if (error)
218 return ERR_PTR(error);
219 }
220
Dan Williams7cc78672019-07-18 15:58:33 -0700221 conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->start), NULL);
Dave Jiang15d36fe2018-07-26 16:37:15 -0700222 if (conflict_pgmap) {
Christoph Hellwig6869b7b2019-08-18 11:05:57 +0200223 WARN(1, "Conflicting mapping in same section\n");
Dave Jiang15d36fe2018-07-26 16:37:15 -0700224 put_dev_pagemap(conflict_pgmap);
Dan Williams50f44ee2019-06-13 15:56:33 -0700225 error = -ENOMEM;
226 goto err_array;
Dave Jiang15d36fe2018-07-26 16:37:15 -0700227 }
228
Dan Williams7cc78672019-07-18 15:58:33 -0700229 conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->end), NULL);
Dave Jiang15d36fe2018-07-26 16:37:15 -0700230 if (conflict_pgmap) {
Christoph Hellwig6869b7b2019-08-18 11:05:57 +0200231 WARN(1, "Conflicting mapping in same section\n");
Dave Jiang15d36fe2018-07-26 16:37:15 -0700232 put_dev_pagemap(conflict_pgmap);
Dan Williams50f44ee2019-06-13 15:56:33 -0700233 error = -ENOMEM;
234 goto err_array;
Dave Jiang15d36fe2018-07-26 16:37:15 -0700235 }
236
Dan Williams7cc78672019-07-18 15:58:33 -0700237 is_ram = region_intersects(res->start, resource_size(res),
Linus Torvaldsd37a14bb2016-03-14 15:15:51 -0700238 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200239
Dan Williams06489cf2018-12-28 00:34:54 -0800240 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);
Dan Williamsa95c90f2018-12-28 00:34:57 -0800243 error = -ENXIO;
244 goto err_array;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200245 }
246
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400247 error = xa_err(xa_store_range(&pgmap_array, PHYS_PFN(res->start),
248 PHYS_PFN(res->end), pgmap, GFP_KERNEL));
Dan Williams9476df72016-01-15 16:56:19 -0800249 if (error)
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400250 goto err_array;
Dan Williams9476df72016-01-15 16:56:19 -0800251
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200252 if (nid < 0)
Dan Williams7eff93b2015-10-05 20:35:55 -0400253 nid = numa_mem_id();
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200254
Dan Williams7cc78672019-07-18 15:58:33 -0700255 error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(res->start), 0,
256 resource_size(res));
Dan Williams90497712016-09-07 08:51:21 -0700257 if (error)
258 goto err_pfn_remap;
259
Dan Williamsf931ab42017-01-10 16:57:36 -0800260 mem_hotplug_begin();
Dan Williams69324b82018-12-28 00:35:01 -0800261
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) {
Dan Williams7cc78672019-07-18 15:58:33 -0700274 error = add_pages(nid, PHYS_PFN(res->start),
275 PHYS_PFN(resource_size(res)), &restrictions);
Dan Williams69324b82018-12-28 00:35:01 -0800276 } else {
Dan Williams7cc78672019-07-18 15:58:33 -0700277 error = kasan_add_zero_shadow(__va(res->start), resource_size(res));
Dan Williams69324b82018-12-28 00:35:01 -0800278 if (error) {
279 mem_hotplug_done();
280 goto err_kasan;
281 }
282
Dan Williams7cc78672019-07-18 15:58:33 -0700283 error = arch_add_memory(nid, res->start, resource_size(res),
Michal Hocko940519f2019-05-13 17:21:26 -0700284 &restrictions);
Andrey Ryabinin0207df42018-08-17 15:47:04 -0700285 }
286
Dan Williams69324b82018-12-28 00:35:01 -0800287 if (!error) {
288 struct zone *zone;
289
290 zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
Dan Williams7cc78672019-07-18 15:58:33 -0700291 move_pfn_range_to_zone(zone, PHYS_PFN(res->start),
292 PHYS_PFN(resource_size(res)), restrictions.altmap);
Dan Williams69324b82018-12-28 00:35:01 -0800293 }
294
Dan Williamsf931ab42017-01-10 16:57:36 -0800295 mem_hotplug_done();
Dan Williams9476df72016-01-15 16:56:19 -0800296 if (error)
297 goto err_add_memory;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200298
Alexander Duyck966cf442018-10-26 15:07:52 -0700299 /*
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],
Dan Williams7cc78672019-07-18 15:58:33 -0700304 PHYS_PFN(res->start),
305 PHYS_PFN(resource_size(res)), pgmap);
Alexander Duyck966cf442018-10-26 15:07:52 -0700306 percpu_ref_get_many(pgmap->ref, pfn_end(pgmap) - pfn_first(pgmap));
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200307 return __va(res->start);
Dan Williams9476df72016-01-15 16:56:19 -0800308
309 err_add_memory:
Dan Williams7cc78672019-07-18 15:58:33 -0700310 kasan_remove_zero_shadow(__va(res->start), resource_size(res));
Andrey Ryabinin0207df42018-08-17 15:47:04 -0700311 err_kasan:
Dan Williams7cc78672019-07-18 15:58:33 -0700312 untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
Dan Williams90497712016-09-07 08:51:21 -0700313 err_pfn_remap:
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400314 pgmap_array_delete(res);
315 err_array:
Christoph Hellwig24917f62019-06-26 14:27:14 +0200316 dev_pagemap_kill(pgmap);
317 dev_pagemap_cleanup(pgmap);
Christoph Hellwig6f421932019-08-18 11:05:56 +0200318 devmap_managed_enable_put();
Dan Williams9476df72016-01-15 16:56:19 -0800319 return ERR_PTR(error);
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200320}
Christoph Hellwig6869b7b2019-08-18 11:05:57 +0200321EXPORT_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}
Dan Williams808153e2018-12-28 00:34:50 -0800358EXPORT_SYMBOL_GPL(devm_memremap_pages);
Dan Williams4b94ffd2016-01-15 16:56:22 -0800359
Dan Williams2e3f1392019-06-13 15:56:21 -0700360void 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
Dan Williams4b94ffd2016-01-15 16:56:22 -0800366unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
367{
368 /* number of pfns from base where pfn_to_page() is valid */
Christoph Hellwig514caf22019-06-26 14:27:13 +0200369 if (altmap)
370 return altmap->reserve + altmap->free;
371 return 0;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800372}
373
374void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
375{
376 altmap->alloc -= nr_pfns;
377}
378
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100379/**
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 *
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100384 * 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.
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100386 */
387struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
388 struct dev_pagemap *pgmap)
389{
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100390 resource_size_t phys = PFN_PHYS(pfn);
391
392 /*
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100393 * In the cached case we're already holding a live reference.
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100394 */
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100395 if (pgmap) {
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100396 if (phys >= pgmap->res.start && phys <= pgmap->res.end)
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100397 return pgmap;
398 put_dev_pagemap(pgmap);
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100399 }
400
401 /* fall back to slow path lookup */
402 rcu_read_lock();
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400403 pgmap = xa_load(&pgmap_array, PHYS_PFN(phys));
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100404 if (pgmap && !percpu_ref_tryget_live(pgmap->ref))
405 pgmap = NULL;
406 rcu_read_unlock();
407
408 return pgmap;
409}
Dan Williamse76384882018-05-16 11:46:08 -0700410EXPORT_SYMBOL_GPL(get_dev_pagemap);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700411
Dan Williamse76384882018-05-16 11:46:08 -0700412#ifdef CONFIG_DEV_PAGEMAP_OPS
Dan Williamse76384882018-05-16 11:46:08 -0700413void __put_devmap_managed_page(struct page *page)
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700414{
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
Jérôme Glissec733a822017-09-08 16:11:54 -0700426 mem_cgroup_uncharge(page);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700427
Ralph Campbell7ab0ad02019-08-13 15:37:07 -0700428 /*
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
Christoph Hellwig80a72d02019-06-26 14:27:12 +0200452 page->pgmap->ops->page_free(page);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700453 } else if (!count)
454 __put_page(page);
455}
Dan Williams31c5bda2018-07-26 16:37:22 -0700456EXPORT_SYMBOL(__put_devmap_managed_page);
Dan Williamse76384882018-05-16 11:46:08 -0700457#endif /* CONFIG_DEV_PAGEMAP_OPS */