1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2016-2019 Intel Corporation. All rights reserved. */
3 #include <linux/memremap.h>
4 #include <linux/pagemap.h>
5 #include <linux/memory.h>
6 #include <linux/module.h>
7 #include <linux/device.h>
8 #include <linux/pfn_t.h>
9 #include <linux/slab.h>
10 #include <linux/dax.h>
13 #include <linux/mman.h>
14 #include "dax-private.h"
17 /* Memory resource name used for add_memory_driver_managed(). */
18 static const char *kmem_name;
19 /* Set if any memory will remain added when the driver will be unloaded. */
20 static bool any_hotremove_failed;
22 int dev_dax_kmem_probe(struct device *dev)
24 struct dev_dax *dev_dax = to_dev_dax(dev);
25 struct resource *res = &dev_dax->region->res;
26 resource_size_t kmem_start;
27 resource_size_t kmem_size;
28 resource_size_t kmem_end;
29 struct resource *new_res;
30 const char *new_res_name;
35 * Ensure good NUMA information for the persistent memory.
36 * Without this check, there is a risk that slow memory
37 * could be mixed in a node with faster memory, causing
38 * unavoidable performance issues.
40 numa_node = dev_dax->target_node;
42 dev_warn(dev, "rejecting DAX region %pR with invalid node: %d\n",
47 /* Hotplug starting at the beginning of the next block: */
48 kmem_start = ALIGN(res->start, memory_block_size_bytes());
50 kmem_size = resource_size(res);
51 /* Adjust the size down to compensate for moving up kmem_start: */
52 kmem_size -= kmem_start - res->start;
53 /* Align the size down to cover only complete blocks: */
54 kmem_size &= ~(memory_block_size_bytes() - 1);
55 kmem_end = kmem_start + kmem_size;
57 new_res_name = kstrdup(dev_name(dev), GFP_KERNEL);
61 /* Region is permanently reserved if hotremove fails. */
62 new_res = request_mem_region(kmem_start, kmem_size, new_res_name);
64 dev_warn(dev, "could not reserve region [%pa-%pa]\n",
65 &kmem_start, &kmem_end);
71 * Set flags appropriate for System RAM. Leave ..._BUSY clear
72 * so that add_memory() can add a child resource. Do not
73 * inherit flags from the parent since it may set new flags
74 * unknown to us that will break add_memory() below.
76 new_res->flags = IORESOURCE_SYSTEM_RAM;
79 * Ensure that future kexec'd kernels will not treat this as RAM
82 rc = add_memory_driver_managed(numa_node, new_res->start,
83 resource_size(new_res), kmem_name);
85 release_resource(new_res);
90 dev_dax->dax_kmem_res = new_res;
95 #ifdef CONFIG_MEMORY_HOTREMOVE
96 static int dev_dax_kmem_remove(struct device *dev)
98 struct dev_dax *dev_dax = to_dev_dax(dev);
99 struct resource *res = dev_dax->dax_kmem_res;
100 resource_size_t kmem_start = res->start;
101 resource_size_t kmem_size = resource_size(res);
102 const char *res_name = res->name;
106 * We have one shot for removing memory, if some memory blocks were not
107 * offline prior to calling this function remove_memory() will fail, and
108 * there is no way to hotremove this memory until reboot because device
109 * unbind will succeed even if we return failure.
111 rc = remove_memory(dev_dax->target_node, kmem_start, kmem_size);
113 any_hotremove_failed = true;
115 "DAX region %pR cannot be hotremoved until the next reboot\n",
120 /* Release and free dax resources */
121 release_resource(res);
124 dev_dax->dax_kmem_res = NULL;
129 static int dev_dax_kmem_remove(struct device *dev)
132 * Without hotremove purposely leak the request_mem_region() for the
133 * device-dax range and return '0' to ->remove() attempts. The removal
134 * of the device from the driver always succeeds, but the region is
135 * permanently pinned as reserved by the unreleased
136 * request_mem_region().
138 any_hotremove_failed = true;
141 #endif /* CONFIG_MEMORY_HOTREMOVE */
143 static struct dax_device_driver device_dax_kmem_driver = {
145 .probe = dev_dax_kmem_probe,
146 .remove = dev_dax_kmem_remove,
150 static int __init dax_kmem_init(void)
154 /* Resource name is permanently allocated if any hotremove fails. */
155 kmem_name = kstrdup_const("System RAM (kmem)", GFP_KERNEL);
159 rc = dax_driver_register(&device_dax_kmem_driver);
161 kfree_const(kmem_name);
165 static void __exit dax_kmem_exit(void)
167 dax_driver_unregister(&device_dax_kmem_driver);
168 if (!any_hotremove_failed)
169 kfree_const(kmem_name);
172 MODULE_AUTHOR("Intel Corporation");
173 MODULE_LICENSE("GPL v2");
174 module_init(dax_kmem_init);
175 module_exit(dax_kmem_exit);
176 MODULE_ALIAS_DAX_DEVICE(0);