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 static int dax_kmem_range(struct dev_dax *dev_dax, int i, struct range *r)
24 struct dev_dax_range *dax_range = &dev_dax->ranges[i];
25 struct range *range = &dax_range->range;
27 /* memory-block align the hotplug range */
28 r->start = ALIGN(range->start, memory_block_size_bytes());
29 r->end = ALIGN_DOWN(range->end + 1, memory_block_size_bytes()) - 1;
30 if (r->start >= r->end) {
31 r->start = range->start;
38 struct dax_kmem_data {
40 struct resource *res[];
43 static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
45 struct device *dev = &dev_dax->dev;
46 struct dax_kmem_data *data;
52 * Ensure good NUMA information for the persistent memory.
53 * Without this check, there is a risk that slow memory
54 * could be mixed in a node with faster memory, causing
55 * unavoidable performance issues.
57 numa_node = dev_dax->target_node;
59 dev_warn(dev, "rejecting DAX region with invalid node: %d\n",
64 data = kzalloc(struct_size(data, res, dev_dax->nr_range), GFP_KERNEL);
68 data->res_name = kstrdup(dev_name(dev), GFP_KERNEL);
72 for (i = 0; i < dev_dax->nr_range; i++) {
76 rc = dax_kmem_range(dev_dax, i, &range);
78 dev_info(dev, "mapping%d: %#llx-%#llx too small after alignment\n",
79 i, range.start, range.end);
83 /* Region is permanently reserved if hotremove fails. */
84 res = request_mem_region(range.start, range_len(&range), data->res_name);
86 dev_warn(dev, "mapping%d: %#llx-%#llx could not reserve region\n",
87 i, range.start, range.end);
89 * Once some memory has been onlined we can't
90 * assume that it can be un-onlined safely.
100 * Set flags appropriate for System RAM. Leave ..._BUSY clear
101 * so that add_memory() can add a child resource. Do not
102 * inherit flags from the parent since it may set new flags
103 * unknown to us that will break add_memory() below.
105 res->flags = IORESOURCE_SYSTEM_RAM;
108 * Ensure that future kexec'd kernels will not treat
109 * this as RAM automatically.
111 rc = add_memory_driver_managed(numa_node, range.start,
112 range_len(&range), kmem_name, MHP_NONE);
115 dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n",
116 i, range.start, range.end);
117 release_resource(res);
122 goto err_request_mem;
127 dev_set_drvdata(dev, data);
132 kfree(data->res_name);
138 #ifdef CONFIG_MEMORY_HOTREMOVE
139 static int dev_dax_kmem_remove(struct dev_dax *dev_dax)
142 struct device *dev = &dev_dax->dev;
143 struct dax_kmem_data *data = dev_get_drvdata(dev);
146 * We have one shot for removing memory, if some memory blocks were not
147 * offline prior to calling this function remove_memory() will fail, and
148 * there is no way to hotremove this memory until reboot because device
149 * unbind will succeed even if we return failure.
151 for (i = 0; i < dev_dax->nr_range; i++) {
155 rc = dax_kmem_range(dev_dax, i, &range);
159 rc = remove_memory(dev_dax->target_node, range.start,
162 release_resource(data->res[i]);
168 any_hotremove_failed = true;
170 "mapping%d: %#llx-%#llx cannot be hotremoved until the next reboot\n",
171 i, range.start, range.end);
174 if (success >= dev_dax->nr_range) {
175 kfree(data->res_name);
177 dev_set_drvdata(dev, NULL);
183 static int dev_dax_kmem_remove(struct dev_dax *dev_dax)
186 * Without hotremove purposely leak the request_mem_region() for the
187 * device-dax range and return '0' to ->remove() attempts. The removal
188 * of the device from the driver always succeeds, but the region is
189 * permanently pinned as reserved by the unreleased
190 * request_mem_region().
192 any_hotremove_failed = true;
195 #endif /* CONFIG_MEMORY_HOTREMOVE */
197 static struct dax_device_driver device_dax_kmem_driver = {
198 .probe = dev_dax_kmem_probe,
199 .remove = dev_dax_kmem_remove,
202 static int __init dax_kmem_init(void)
206 /* Resource name is permanently allocated if any hotremove fails. */
207 kmem_name = kstrdup_const("System RAM (kmem)", GFP_KERNEL);
211 rc = dax_driver_register(&device_dax_kmem_driver);
213 kfree_const(kmem_name);
217 static void __exit dax_kmem_exit(void)
219 dax_driver_unregister(&device_dax_kmem_driver);
220 if (!any_hotremove_failed)
221 kfree_const(kmem_name);
224 MODULE_AUTHOR("Intel Corporation");
225 MODULE_LICENSE("GPL v2");
226 module_init(dax_kmem_init);
227 module_exit(dax_kmem_exit);
228 MODULE_ALIAS_DAX_DEVICE(0);