blob: bea6f887adad100f7419efcb71c589578c5f6a9e [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
24static void devmap_managed_enable_put(void *data)
25{
26 if (atomic_dec_and_test(&devmap_managed_enable))
27 static_branch_disable(&devmap_managed_key);
28}
29
30static int devmap_managed_enable_get(struct device *dev, struct dev_pagemap *pgmap)
31{
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);
39 return devm_add_action_or_reset(dev, devmap_managed_enable_put, NULL);
40}
41#else
42static int devmap_managed_enable_get(struct device *dev, struct dev_pagemap *pgmap)
43{
44 return -EINVAL;
45}
46#endif /* CONFIG_DEV_PAGEMAP_OPS */
47
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -040048static void pgmap_array_delete(struct resource *res)
Christoph Hellwig41e94a82015-08-17 16:00:35 +020049{
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -040050 xa_store_range(&pgmap_array, PHYS_PFN(res->start), PHYS_PFN(res->end),
51 NULL, GFP_KERNEL);
Dan Williamsab1b5972017-09-06 16:24:13 -070052 synchronize_rcu();
Dan Williams9476df72016-01-15 16:56:19 -080053}
54
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010055static unsigned long pfn_first(struct dev_pagemap *pgmap)
Dan Williams5c2c2582016-01-15 16:56:49 -080056{
Christoph Hellwig514caf22019-06-26 14:27:13 +020057 return (pgmap->res.start >> PAGE_SHIFT) +
58 vmem_altmap_offset(pgmap_altmap(pgmap));
Dan Williams5c2c2582016-01-15 16:56:49 -080059}
60
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010061static unsigned long pfn_end(struct dev_pagemap *pgmap)
Dan Williams5c2c2582016-01-15 16:56:49 -080062{
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010063 const struct resource *res = &pgmap->res;
Dan Williams5c2c2582016-01-15 16:56:49 -080064
65 return (res->start + resource_size(res)) >> PAGE_SHIFT;
66}
67
Dan Williams949b93252018-02-06 19:34:11 -080068static unsigned long pfn_next(unsigned long pfn)
69{
70 if (pfn % 1024 == 0)
71 cond_resched();
72 return pfn + 1;
73}
74
Dan Williams5c2c2582016-01-15 16:56:49 -080075#define for_each_device_pfn(pfn, map) \
Dan Williams949b93252018-02-06 19:34:11 -080076 for (pfn = pfn_first(map); pfn < pfn_end(map); pfn = pfn_next(pfn))
Dan Williams5c2c2582016-01-15 16:56:49 -080077
Christoph Hellwig24917f62019-06-26 14:27:14 +020078static void dev_pagemap_kill(struct dev_pagemap *pgmap)
79{
80 if (pgmap->ops && pgmap->ops->kill)
81 pgmap->ops->kill(pgmap);
82 else
83 percpu_ref_kill(pgmap->ref);
84}
85
86static void dev_pagemap_cleanup(struct dev_pagemap *pgmap)
87{
88 if (pgmap->ops && pgmap->ops->cleanup) {
89 pgmap->ops->cleanup(pgmap);
90 } else {
91 wait_for_completion(&pgmap->done);
92 percpu_ref_exit(pgmap->ref);
93 }
94}
95
Christoph Hellwige8d51342017-12-29 08:54:05 +010096static void devm_memremap_pages_release(void *data)
Dan Williams9476df72016-01-15 16:56:19 -080097{
Logan Gunthorpee7744aa2017-12-29 08:54:04 +010098 struct dev_pagemap *pgmap = data;
Christoph Hellwige8d51342017-12-29 08:54:05 +010099 struct device *dev = pgmap->dev;
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100100 struct resource *res = &pgmap->res;
Dan Williams9476df72016-01-15 16:56:19 -0800101 resource_size_t align_start, align_size;
Dan Williams71389702017-04-28 10:23:37 -0700102 unsigned long pfn;
Oscar Salvador2c2a5af2018-12-28 00:36:22 -0800103 int nid;
Dan Williams71389702017-04-28 10:23:37 -0700104
Christoph Hellwig24917f62019-06-26 14:27:14 +0200105 dev_pagemap_kill(pgmap);
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100106 for_each_device_pfn(pfn, pgmap)
Dan Williams71389702017-04-28 10:23:37 -0700107 put_page(pfn_to_page(pfn));
Christoph Hellwig24917f62019-06-26 14:27:14 +0200108 dev_pagemap_cleanup(pgmap);
Dan Williams9476df72016-01-15 16:56:19 -0800109
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200110 /* pages are dead and unused, undo the arch mapping */
Dan Williams9476df72016-01-15 16:56:19 -0800111 align_start = res->start & ~(SECTION_SIZE - 1);
Jan H. Schönherr10a0cd62018-01-19 16:27:54 -0800112 align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
113 - align_start;
Dan Williamsb5d24fd2017-02-24 14:55:45 -0800114
Oscar Salvador2c2a5af2018-12-28 00:36:22 -0800115 nid = page_to_nid(pfn_to_page(align_start >> PAGE_SHIFT));
116
Dan Williamsf931ab42017-01-10 16:57:36 -0800117 mem_hotplug_begin();
Dan Williams69324b82018-12-28 00:35:01 -0800118 if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
119 pfn = align_start >> PAGE_SHIFT;
120 __remove_pages(page_zone(pfn_to_page(pfn)), pfn,
121 align_size >> PAGE_SHIFT, NULL);
122 } else {
Oscar Salvador2c2a5af2018-12-28 00:36:22 -0800123 arch_remove_memory(nid, align_start, align_size,
Christoph Hellwig514caf22019-06-26 14:27:13 +0200124 pgmap_altmap(pgmap));
Dan Williams69324b82018-12-28 00:35:01 -0800125 kasan_remove_zero_shadow(__va(align_start), align_size);
126 }
Dan Williamsf931ab42017-01-10 16:57:36 -0800127 mem_hotplug_done();
Dan Williamsb5d24fd2017-02-24 14:55:45 -0800128
Dan Williams90497712016-09-07 08:51:21 -0700129 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400130 pgmap_array_delete(res);
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100131 dev_WARN_ONCE(dev, pgmap->altmap.alloc,
132 "%s: failed to free all reserved pages\n", __func__);
Dan Williams9476df72016-01-15 16:56:19 -0800133}
134
Christoph Hellwig24917f62019-06-26 14:27:14 +0200135static void dev_pagemap_percpu_release(struct percpu_ref *ref)
136{
137 struct dev_pagemap *pgmap =
138 container_of(ref, struct dev_pagemap, internal_ref);
139
140 complete(&pgmap->done);
141}
142
Dan Williams4b94ffd2016-01-15 16:56:22 -0800143/**
144 * devm_memremap_pages - remap and provide memmap backing for the given resource
145 * @dev: hosting device for @res
Dan Williamsa95c90f2018-12-28 00:34:57 -0800146 * @pgmap: pointer to a struct dev_pagemap
Dan Williams4b94ffd2016-01-15 16:56:22 -0800147 *
Dan Williams5c2c2582016-01-15 16:56:49 -0800148 * Notes:
Christoph Hellwig24917f62019-06-26 14:27:14 +0200149 * 1/ At a minimum the res and type members of @pgmap must be initialized
150 * by the caller before passing it to this function
Christoph Hellwige8d51342017-12-29 08:54:05 +0100151 *
Christoph Hellwig514caf22019-06-26 14:27:13 +0200152 * 2/ The altmap field may optionally be initialized, in which case
153 * PGMAP_ALTMAP_VALID must be set in pgmap->flags.
Christoph Hellwige8d51342017-12-29 08:54:05 +0100154 *
Christoph Hellwig24917f62019-06-26 14:27:14 +0200155 * 3/ The ref field may optionally be provided, in which pgmap->ref must be
156 * 'live' on entry and will be killed and reaped at
157 * devm_memremap_pages_release() time, or if this routine fails.
Dan Williams5c2c2582016-01-15 16:56:49 -0800158 *
Christoph Hellwige8d51342017-12-29 08:54:05 +0100159 * 4/ res is expected to be a host memory range that could feasibly be
Dan Williams5c2c2582016-01-15 16:56:49 -0800160 * treated as a "System RAM" range, i.e. not a device mmio range, but
161 * this is not enforced.
Dan Williams4b94ffd2016-01-15 16:56:22 -0800162 */
Christoph Hellwige8d51342017-12-29 08:54:05 +0100163void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200164{
Dan Williamsab1b5972017-09-06 16:24:13 -0700165 resource_size_t align_start, align_size, align_end;
Dan Williams949b93252018-02-06 19:34:11 -0800166 struct resource *res = &pgmap->res;
Dave Jiang15d36fe2018-07-26 16:37:15 -0700167 struct dev_pagemap *conflict_pgmap;
Michal Hocko940519f2019-05-13 17:21:26 -0700168 struct mhp_restrictions restrictions = {
169 /*
170 * We do not want any optional features only our own memmap
171 */
Christoph Hellwig514caf22019-06-26 14:27:13 +0200172 .altmap = pgmap_altmap(pgmap),
Michal Hocko940519f2019-05-13 17:21:26 -0700173 };
Alexander Duyck966cf442018-10-26 15:07:52 -0700174 pgprot_t pgprot = PAGE_KERNEL;
Alexander Duyck966cf442018-10-26 15:07:52 -0700175 int error, nid, is_ram;
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200176 bool need_devmap_managed = true;
Dan Williams5f29a772016-03-09 14:08:13 -0800177
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +0200178 switch (pgmap->type) {
179 case MEMORY_DEVICE_PRIVATE:
180 if (!IS_ENABLED(CONFIG_DEVICE_PRIVATE)) {
181 WARN(1, "Device private memory not supported\n");
182 return ERR_PTR(-EINVAL);
183 }
Christoph Hellwig897e6362019-06-26 14:27:11 +0200184 if (!pgmap->ops || !pgmap->ops->migrate_to_ram) {
185 WARN(1, "Missing migrate_to_ram method\n");
186 return ERR_PTR(-EINVAL);
187 }
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +0200188 break;
189 case MEMORY_DEVICE_FS_DAX:
190 if (!IS_ENABLED(CONFIG_ZONE_DEVICE) ||
191 IS_ENABLED(CONFIG_FS_DAX_LIMITED)) {
192 WARN(1, "File system DAX not supported\n");
193 return ERR_PTR(-EINVAL);
194 }
195 break;
196 case MEMORY_DEVICE_DEVDAX:
197 case MEMORY_DEVICE_PCI_P2PDMA:
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200198 need_devmap_managed = false;
Christoph Hellwig3ed2dcd2019-06-26 14:27:07 +0200199 break;
200 default:
201 WARN(1, "Invalid pgmap type %d\n", pgmap->type);
202 break;
203 }
204
Christoph Hellwig24917f62019-06-26 14:27:14 +0200205 if (!pgmap->ref) {
206 if (pgmap->ops && (pgmap->ops->kill || pgmap->ops->cleanup))
207 return ERR_PTR(-EINVAL);
208
209 init_completion(&pgmap->done);
210 error = percpu_ref_init(&pgmap->internal_ref,
211 dev_pagemap_percpu_release, 0, GFP_KERNEL);
212 if (error)
213 return ERR_PTR(error);
214 pgmap->ref = &pgmap->internal_ref;
215 } else {
216 if (!pgmap->ops || !pgmap->ops->kill || !pgmap->ops->cleanup) {
217 WARN(1, "Missing reference count teardown definition\n");
218 return ERR_PTR(-EINVAL);
219 }
Dan Williams50f44ee2019-06-13 15:56:33 -0700220 }
Dan Williamsa95c90f2018-12-28 00:34:57 -0800221
Christoph Hellwigf6a55e12019-06-26 14:27:10 +0200222 if (need_devmap_managed) {
223 error = devmap_managed_enable_get(dev, pgmap);
224 if (error)
225 return ERR_PTR(error);
226 }
227
Dan Williams5f29a772016-03-09 14:08:13 -0800228 align_start = res->start & ~(SECTION_SIZE - 1);
229 align_size = ALIGN(res->start + resource_size(res), SECTION_SIZE)
230 - align_start;
Dave Jiang15d36fe2018-07-26 16:37:15 -0700231 align_end = align_start + align_size - 1;
232
233 conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_start), NULL);
234 if (conflict_pgmap) {
235 dev_WARN(dev, "Conflicting mapping in same section\n");
236 put_dev_pagemap(conflict_pgmap);
Dan Williams50f44ee2019-06-13 15:56:33 -0700237 error = -ENOMEM;
238 goto err_array;
Dave Jiang15d36fe2018-07-26 16:37:15 -0700239 }
240
241 conflict_pgmap = get_dev_pagemap(PHYS_PFN(align_end), NULL);
242 if (conflict_pgmap) {
243 dev_WARN(dev, "Conflicting mapping in same section\n");
244 put_dev_pagemap(conflict_pgmap);
Dan Williams50f44ee2019-06-13 15:56:33 -0700245 error = -ENOMEM;
246 goto err_array;
Dave Jiang15d36fe2018-07-26 16:37:15 -0700247 }
248
Linus Torvaldsd37a14bb2016-03-14 15:15:51 -0700249 is_ram = region_intersects(align_start, align_size,
250 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200251
Dan Williams06489cf2018-12-28 00:34:54 -0800252 if (is_ram != REGION_DISJOINT) {
253 WARN_ONCE(1, "%s attempted on %s region %pr\n", __func__,
254 is_ram == REGION_MIXED ? "mixed" : "ram", res);
Dan Williamsa95c90f2018-12-28 00:34:57 -0800255 error = -ENXIO;
256 goto err_array;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200257 }
258
Dan Williams4b94ffd2016-01-15 16:56:22 -0800259 pgmap->dev = dev;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800260
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400261 error = xa_err(xa_store_range(&pgmap_array, PHYS_PFN(res->start),
262 PHYS_PFN(res->end), pgmap, GFP_KERNEL));
Dan Williams9476df72016-01-15 16:56:19 -0800263 if (error)
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400264 goto err_array;
Dan Williams9476df72016-01-15 16:56:19 -0800265
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200266 nid = dev_to_node(dev);
267 if (nid < 0)
Dan Williams7eff93b2015-10-05 20:35:55 -0400268 nid = numa_mem_id();
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200269
Dan Williams90497712016-09-07 08:51:21 -0700270 error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(align_start), 0,
271 align_size);
272 if (error)
273 goto err_pfn_remap;
274
Dan Williamsf931ab42017-01-10 16:57:36 -0800275 mem_hotplug_begin();
Dan Williams69324b82018-12-28 00:35:01 -0800276
277 /*
278 * For device private memory we call add_pages() as we only need to
279 * allocate and initialize struct page for the device memory. More-
280 * over the device memory is un-accessible thus we do not want to
281 * create a linear mapping for the memory like arch_add_memory()
282 * would do.
283 *
284 * For all other device memory types, which are accessible by
285 * the CPU, we do want the linear mapping and thus use
286 * arch_add_memory().
287 */
288 if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
289 error = add_pages(nid, align_start >> PAGE_SHIFT,
Michal Hocko940519f2019-05-13 17:21:26 -0700290 align_size >> PAGE_SHIFT, &restrictions);
Dan Williams69324b82018-12-28 00:35:01 -0800291 } else {
292 error = kasan_add_zero_shadow(__va(align_start), align_size);
293 if (error) {
294 mem_hotplug_done();
295 goto err_kasan;
296 }
297
Michal Hocko940519f2019-05-13 17:21:26 -0700298 error = arch_add_memory(nid, align_start, align_size,
299 &restrictions);
Andrey Ryabinin0207df42018-08-17 15:47:04 -0700300 }
301
Dan Williams69324b82018-12-28 00:35:01 -0800302 if (!error) {
303 struct zone *zone;
304
305 zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
306 move_pfn_range_to_zone(zone, align_start >> PAGE_SHIFT,
Christoph Hellwig514caf22019-06-26 14:27:13 +0200307 align_size >> PAGE_SHIFT, pgmap_altmap(pgmap));
Dan Williams69324b82018-12-28 00:35:01 -0800308 }
309
Dan Williamsf931ab42017-01-10 16:57:36 -0800310 mem_hotplug_done();
Dan Williams9476df72016-01-15 16:56:19 -0800311 if (error)
312 goto err_add_memory;
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200313
Alexander Duyck966cf442018-10-26 15:07:52 -0700314 /*
315 * Initialization of the pages has been deferred until now in order
316 * to allow us to do the work while not holding the hotplug lock.
317 */
318 memmap_init_zone_device(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
319 align_start >> PAGE_SHIFT,
320 align_size >> PAGE_SHIFT, pgmap);
321 percpu_ref_get_many(pgmap->ref, pfn_end(pgmap) - pfn_first(pgmap));
Christoph Hellwige8d51342017-12-29 08:54:05 +0100322
Dan Williamsa95c90f2018-12-28 00:34:57 -0800323 error = devm_add_action_or_reset(dev, devm_memremap_pages_release,
324 pgmap);
325 if (error)
326 return ERR_PTR(error);
Christoph Hellwige8d51342017-12-29 08:54:05 +0100327
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200328 return __va(res->start);
Dan Williams9476df72016-01-15 16:56:19 -0800329
330 err_add_memory:
Andrey Ryabinin0207df42018-08-17 15:47:04 -0700331 kasan_remove_zero_shadow(__va(align_start), align_size);
332 err_kasan:
Dan Williams90497712016-09-07 08:51:21 -0700333 untrack_pfn(NULL, PHYS_PFN(align_start), align_size);
334 err_pfn_remap:
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400335 pgmap_array_delete(res);
336 err_array:
Christoph Hellwig24917f62019-06-26 14:27:14 +0200337 dev_pagemap_kill(pgmap);
338 dev_pagemap_cleanup(pgmap);
Dan Williams9476df72016-01-15 16:56:19 -0800339 return ERR_PTR(error);
Christoph Hellwig41e94a82015-08-17 16:00:35 +0200340}
Dan Williams808153e2018-12-28 00:34:50 -0800341EXPORT_SYMBOL_GPL(devm_memremap_pages);
Dan Williams4b94ffd2016-01-15 16:56:22 -0800342
Dan Williams2e3f1392019-06-13 15:56:21 -0700343void devm_memunmap_pages(struct device *dev, struct dev_pagemap *pgmap)
344{
345 devm_release_action(dev, devm_memremap_pages_release, pgmap);
346}
347EXPORT_SYMBOL_GPL(devm_memunmap_pages);
348
Dan Williams4b94ffd2016-01-15 16:56:22 -0800349unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
350{
351 /* number of pfns from base where pfn_to_page() is valid */
Christoph Hellwig514caf22019-06-26 14:27:13 +0200352 if (altmap)
353 return altmap->reserve + altmap->free;
354 return 0;
Dan Williams4b94ffd2016-01-15 16:56:22 -0800355}
356
357void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
358{
359 altmap->alloc -= nr_pfns;
360}
361
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100362/**
363 * get_dev_pagemap() - take a new live reference on the dev_pagemap for @pfn
364 * @pfn: page frame number to lookup page_map
365 * @pgmap: optional known pgmap that already has a reference
366 *
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100367 * If @pgmap is non-NULL and covers @pfn it will be returned as-is. If @pgmap
368 * is non-NULL but does not cover @pfn the reference to it will be released.
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100369 */
370struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
371 struct dev_pagemap *pgmap)
372{
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100373 resource_size_t phys = PFN_PHYS(pfn);
374
375 /*
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100376 * In the cached case we're already holding a live reference.
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100377 */
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100378 if (pgmap) {
Logan Gunthorpee7744aa2017-12-29 08:54:04 +0100379 if (phys >= pgmap->res.start && phys <= pgmap->res.end)
Christoph Hellwig832d7aa2017-12-29 08:54:01 +0100380 return pgmap;
381 put_dev_pagemap(pgmap);
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100382 }
383
384 /* fall back to slow path lookup */
385 rcu_read_lock();
Matthew Wilcoxbcfa4b72018-08-15 14:22:16 -0400386 pgmap = xa_load(&pgmap_array, PHYS_PFN(phys));
Christoph Hellwig0822acb2017-12-29 08:54:00 +0100387 if (pgmap && !percpu_ref_tryget_live(pgmap->ref))
388 pgmap = NULL;
389 rcu_read_unlock();
390
391 return pgmap;
392}
Dan Williamse76384882018-05-16 11:46:08 -0700393EXPORT_SYMBOL_GPL(get_dev_pagemap);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700394
Dan Williamse76384882018-05-16 11:46:08 -0700395#ifdef CONFIG_DEV_PAGEMAP_OPS
Dan Williamse76384882018-05-16 11:46:08 -0700396void __put_devmap_managed_page(struct page *page)
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700397{
398 int count = page_ref_dec_return(page);
399
400 /*
401 * If refcount is 1 then page is freed and refcount is stable as nobody
402 * holds a reference on the page.
403 */
404 if (count == 1) {
405 /* Clear Active bit in case of parallel mark_page_accessed */
406 __ClearPageActive(page);
407 __ClearPageWaiters(page);
408
Jérôme Glissec733a822017-09-08 16:11:54 -0700409 mem_cgroup_uncharge(page);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700410
Christoph Hellwig80a72d02019-06-26 14:27:12 +0200411 page->pgmap->ops->page_free(page);
Jérôme Glisse7b2d55d22017-09-08 16:11:46 -0700412 } else if (!count)
413 __put_page(page);
414}
Dan Williams31c5bda2018-07-26 16:37:22 -0700415EXPORT_SYMBOL(__put_devmap_managed_page);
Dan Williamse76384882018-05-16 11:46:08 -0700416#endif /* CONFIG_DEV_PAGEMAP_OPS */