1
2
3#include <linux/device.h>
4#include <linux/io.h>
5#include <linux/kasan.h>
6#include <linux/memory_hotplug.h>
7#include <linux/mm.h>
8#include <linux/pfn_t.h>
9#include <linux/swap.h>
10#include <linux/swapops.h>
11#include <linux/types.h>
12#include <linux/wait_bit.h>
13#include <linux/xarray.h>
14
15static DEFINE_XARRAY(pgmap_array);
16
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
22static void devmap_managed_enable_put(void)
23{
24 if (atomic_dec_and_test(&devmap_managed_enable))
25 static_branch_disable(&devmap_managed_key);
26}
27
28static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
29{
30 if (!pgmap->ops || !pgmap->ops->page_free) {
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);
37 return 0;
38}
39#else
40static int devmap_managed_enable_get(struct dev_pagemap *pgmap)
41{
42 return -EINVAL;
43}
44static void devmap_managed_enable_put(void)
45{
46}
47#endif
48
49static void pgmap_array_delete(struct resource *res)
50{
51 xa_store_range(&pgmap_array, PHYS_PFN(res->start), PHYS_PFN(res->end),
52 NULL, GFP_KERNEL);
53 synchronize_rcu();
54}
55
56static unsigned long pfn_first(struct dev_pagemap *pgmap)
57{
58 return PHYS_PFN(pgmap->res.start) +
59 vmem_altmap_offset(pgmap_altmap(pgmap));
60}
61
62static unsigned long pfn_end(struct dev_pagemap *pgmap)
63{
64 const struct resource *res = &pgmap->res;
65
66 return (res->start + resource_size(res)) >> PAGE_SHIFT;
67}
68
69static unsigned long pfn_next(unsigned long pfn)
70{
71 if (pfn % 1024 == 0)
72 cond_resched();
73 return pfn + 1;
74}
75
76#define for_each_device_pfn(pfn, map) \
77 for (pfn = pfn_first(map); pfn < pfn_end(map); pfn = pfn_next(pfn))
78
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 }
95
96
97
98
99 if (pgmap->ref == &pgmap->internal_ref)
100 pgmap->ref = NULL;
101}
102
103void memunmap_pages(struct dev_pagemap *pgmap)
104{
105 struct resource *res = &pgmap->res;
106 struct page *first_page;
107 unsigned long pfn;
108 int nid;
109
110 dev_pagemap_kill(pgmap);
111 for_each_device_pfn(pfn, pgmap)
112 put_page(pfn_to_page(pfn));
113 dev_pagemap_cleanup(pgmap);
114
115
116 first_page = pfn_to_page(pfn_first(pgmap));
117
118
119 nid = page_to_nid(first_page);
120
121 mem_hotplug_begin();
122 if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
123 __remove_pages(page_zone(first_page), PHYS_PFN(res->start),
124 PHYS_PFN(resource_size(res)), NULL);
125 } else {
126 arch_remove_memory(nid, res->start, resource_size(res),
127 pgmap_altmap(pgmap));
128 kasan_remove_zero_shadow(__va(res->start), resource_size(res));
129 }
130 mem_hotplug_done();
131
132 untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
133 pgmap_array_delete(res);
134 WARN_ONCE(pgmap->altmap.alloc, "failed to free all reserved pages\n");
135 devmap_managed_enable_put();
136}
137EXPORT_SYMBOL_GPL(memunmap_pages);
138
139static void devm_memremap_pages_release(void *data)
140{
141 memunmap_pages(data);
142}
143
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
152
153
154
155
156
157void *memremap_pages(struct dev_pagemap *pgmap, int nid)
158{
159 struct resource *res = &pgmap->res;
160 struct dev_pagemap *conflict_pgmap;
161 struct mhp_restrictions restrictions = {
162
163
164
165 .altmap = pgmap_altmap(pgmap),
166 };
167 pgprot_t pgprot = PAGE_KERNEL;
168 int error, is_ram;
169 bool need_devmap_managed = true;
170
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 }
177 if (!pgmap->ops || !pgmap->ops->migrate_to_ram) {
178 WARN(1, "Missing migrate_to_ram method\n");
179 return ERR_PTR(-EINVAL);
180 }
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:
191 need_devmap_managed = false;
192 break;
193 default:
194 WARN(1, "Invalid pgmap type %d\n", pgmap->type);
195 break;
196 }
197
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 }
213 }
214
215 if (need_devmap_managed) {
216 error = devmap_managed_enable_get(pgmap);
217 if (error)
218 return ERR_PTR(error);
219 }
220
221 conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->start), NULL);
222 if (conflict_pgmap) {
223 WARN(1, "Conflicting mapping in same section\n");
224 put_dev_pagemap(conflict_pgmap);
225 error = -ENOMEM;
226 goto err_array;
227 }
228
229 conflict_pgmap = get_dev_pagemap(PHYS_PFN(res->end), NULL);
230 if (conflict_pgmap) {
231 WARN(1, "Conflicting mapping in same section\n");
232 put_dev_pagemap(conflict_pgmap);
233 error = -ENOMEM;
234 goto err_array;
235 }
236
237 is_ram = region_intersects(res->start, resource_size(res),
238 IORESOURCE_SYSTEM_RAM, IORES_DESC_NONE);
239
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);
243 error = -ENXIO;
244 goto err_array;
245 }
246
247 error = xa_err(xa_store_range(&pgmap_array, PHYS_PFN(res->start),
248 PHYS_PFN(res->end), pgmap, GFP_KERNEL));
249 if (error)
250 goto err_array;
251
252 if (nid < 0)
253 nid = numa_mem_id();
254
255 error = track_pfn_remap(NULL, &pgprot, PHYS_PFN(res->start), 0,
256 resource_size(res));
257 if (error)
258 goto err_pfn_remap;
259
260 mem_hotplug_begin();
261
262
263
264
265
266
267
268
269
270
271
272
273 if (pgmap->type == MEMORY_DEVICE_PRIVATE) {
274 error = add_pages(nid, PHYS_PFN(res->start),
275 PHYS_PFN(resource_size(res)), &restrictions);
276 } else {
277 error = kasan_add_zero_shadow(__va(res->start), resource_size(res));
278 if (error) {
279 mem_hotplug_done();
280 goto err_kasan;
281 }
282
283 error = arch_add_memory(nid, res->start, resource_size(res),
284 &restrictions);
285 }
286
287 if (!error) {
288 struct zone *zone;
289
290 zone = &NODE_DATA(nid)->node_zones[ZONE_DEVICE];
291 move_pfn_range_to_zone(zone, PHYS_PFN(res->start),
292 PHYS_PFN(resource_size(res)), restrictions.altmap);
293 }
294
295 mem_hotplug_done();
296 if (error)
297 goto err_add_memory;
298
299
300
301
302
303 memmap_init_zone_device(&NODE_DATA(nid)->node_zones[ZONE_DEVICE],
304 PHYS_PFN(res->start),
305 PHYS_PFN(resource_size(res)), pgmap);
306 percpu_ref_get_many(pgmap->ref, pfn_end(pgmap) - pfn_first(pgmap));
307 return __va(res->start);
308
309 err_add_memory:
310 kasan_remove_zero_shadow(__va(res->start), resource_size(res));
311 err_kasan:
312 untrack_pfn(NULL, PHYS_PFN(res->start), resource_size(res));
313 err_pfn_remap:
314 pgmap_array_delete(res);
315 err_array:
316 dev_pagemap_kill(pgmap);
317 dev_pagemap_cleanup(pgmap);
318 devmap_managed_enable_put();
319 return ERR_PTR(error);
320}
321EXPORT_SYMBOL_GPL(memremap_pages);
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
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}
358EXPORT_SYMBOL_GPL(devm_memremap_pages);
359
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
366unsigned long vmem_altmap_offset(struct vmem_altmap *altmap)
367{
368
369 if (altmap)
370 return altmap->reserve + altmap->free;
371 return 0;
372}
373
374void vmem_altmap_free(struct vmem_altmap *altmap, unsigned long nr_pfns)
375{
376 altmap->alloc -= nr_pfns;
377}
378
379
380
381
382
383
384
385
386
387struct dev_pagemap *get_dev_pagemap(unsigned long pfn,
388 struct dev_pagemap *pgmap)
389{
390 resource_size_t phys = PFN_PHYS(pfn);
391
392
393
394
395 if (pgmap) {
396 if (phys >= pgmap->res.start && phys <= pgmap->res.end)
397 return pgmap;
398 put_dev_pagemap(pgmap);
399 }
400
401
402 rcu_read_lock();
403 pgmap = xa_load(&pgmap_array, PHYS_PFN(phys));
404 if (pgmap && !percpu_ref_tryget_live(pgmap->ref))
405 pgmap = NULL;
406 rcu_read_unlock();
407
408 return pgmap;
409}
410EXPORT_SYMBOL_GPL(get_dev_pagemap);
411
412#ifdef CONFIG_DEV_PAGEMAP_OPS
413void __put_devmap_managed_page(struct page *page)
414{
415 int count = page_ref_dec_return(page);
416
417
418
419
420
421 if (count == 1) {
422
423 __ClearPageActive(page);
424 __ClearPageWaiters(page);
425
426 mem_cgroup_uncharge(page);
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449 if (is_device_private_page(page))
450 page->mapping = NULL;
451
452 page->pgmap->ops->page_free(page);
453 } else if (!count)
454 __put_page(page);
455}
456EXPORT_SYMBOL(__put_devmap_managed_page);
457#endif
458