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 <linux/memory-tiers.h>
15 #include "dax-private.h"
19 * Default abstract distance assigned to the NUMA node onlined
20 * by DAX/kmem if the low level platform driver didn't initialize
21 * one for this NUMA node.
23 #define MEMTIER_DEFAULT_DAX_ADISTANCE (MEMTIER_ADISTANCE_DRAM * 5)
25 /* Memory resource name used for add_memory_driver_managed(). */
26 static const char *kmem_name;
27 /* Set if any memory will remain added when the driver will be unloaded. */
28 static bool any_hotremove_failed;
30 static int dax_kmem_range(struct dev_dax *dev_dax, int i, struct range *r)
32 struct dev_dax_range *dax_range = &dev_dax->ranges[i];
33 struct range *range = &dax_range->range;
35 /* memory-block align the hotplug range */
36 r->start = ALIGN(range->start, memory_block_size_bytes());
37 r->end = ALIGN_DOWN(range->end + 1, memory_block_size_bytes()) - 1;
38 if (r->start >= r->end) {
39 r->start = range->start;
46 struct dax_kmem_data {
49 struct resource *res[];
52 static DEFINE_MUTEX(kmem_memory_type_lock);
53 static LIST_HEAD(kmem_memory_types);
55 static struct memory_dev_type *kmem_find_alloc_memory_type(int adist)
58 struct memory_dev_type *mtype;
60 mutex_lock(&kmem_memory_type_lock);
61 list_for_each_entry(mtype, &kmem_memory_types, list) {
62 if (mtype->adistance == adist) {
68 mtype = alloc_memory_type(adist);
70 list_add(&mtype->list, &kmem_memory_types);
72 mutex_unlock(&kmem_memory_type_lock);
77 static void kmem_put_memory_types(void)
79 struct memory_dev_type *mtype, *mtn;
81 mutex_lock(&kmem_memory_type_lock);
82 list_for_each_entry_safe(mtype, mtn, &kmem_memory_types, list) {
83 list_del(&mtype->list);
84 put_memory_type(mtype);
86 mutex_unlock(&kmem_memory_type_lock);
89 static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
91 struct device *dev = &dev_dax->dev;
92 unsigned long total_len = 0;
93 struct dax_kmem_data *data;
94 struct memory_dev_type *mtype;
95 int i, rc, mapped = 0;
97 int adist = MEMTIER_DEFAULT_DAX_ADISTANCE;
100 * Ensure good NUMA information for the persistent memory.
101 * Without this check, there is a risk that slow memory
102 * could be mixed in a node with faster memory, causing
103 * unavoidable performance issues.
105 numa_node = dev_dax->target_node;
107 dev_warn(dev, "rejecting DAX region with invalid node: %d\n",
112 mt_calc_adistance(numa_node, &adist);
113 mtype = kmem_find_alloc_memory_type(adist);
115 return PTR_ERR(mtype);
117 for (i = 0; i < dev_dax->nr_range; i++) {
120 rc = dax_kmem_range(dev_dax, i, &range);
122 dev_info(dev, "mapping%d: %#llx-%#llx too small after alignment\n",
123 i, range.start, range.end);
126 total_len += range_len(&range);
130 dev_warn(dev, "rejecting DAX region without any memory after alignment\n");
134 init_node_memory_type(numa_node, mtype);
137 data = kzalloc(struct_size(data, res, dev_dax->nr_range), GFP_KERNEL);
139 goto err_dax_kmem_data;
141 data->res_name = kstrdup(dev_name(dev), GFP_KERNEL);
145 rc = memory_group_register_static(numa_node, PFN_UP(total_len));
150 for (i = 0; i < dev_dax->nr_range; i++) {
151 struct resource *res;
154 rc = dax_kmem_range(dev_dax, i, &range);
158 /* Region is permanently reserved if hotremove fails. */
159 res = request_mem_region(range.start, range_len(&range), data->res_name);
161 dev_warn(dev, "mapping%d: %#llx-%#llx could not reserve region\n",
162 i, range.start, range.end);
164 * Once some memory has been onlined we can't
165 * assume that it can be un-onlined safely.
170 goto err_request_mem;
175 * Set flags appropriate for System RAM. Leave ..._BUSY clear
176 * so that add_memory() can add a child resource. Do not
177 * inherit flags from the parent since it may set new flags
178 * unknown to us that will break add_memory() below.
180 res->flags = IORESOURCE_SYSTEM_RAM;
183 * Ensure that future kexec'd kernels will not treat
184 * this as RAM automatically.
186 rc = add_memory_driver_managed(data->mgid, range.start,
187 range_len(&range), kmem_name, MHP_NID_IS_MGID);
190 dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n",
191 i, range.start, range.end);
192 remove_resource(res);
197 goto err_request_mem;
202 dev_set_drvdata(dev, data);
207 memory_group_unregister(data->mgid);
209 kfree(data->res_name);
213 clear_node_memory_type(numa_node, mtype);
217 #ifdef CONFIG_MEMORY_HOTREMOVE
218 static void dev_dax_kmem_remove(struct dev_dax *dev_dax)
221 int node = dev_dax->target_node;
222 struct device *dev = &dev_dax->dev;
223 struct dax_kmem_data *data = dev_get_drvdata(dev);
226 * We have one shot for removing memory, if some memory blocks were not
227 * offline prior to calling this function remove_memory() will fail, and
228 * there is no way to hotremove this memory until reboot because device
229 * unbind will succeed even if we return failure.
231 for (i = 0; i < dev_dax->nr_range; i++) {
235 rc = dax_kmem_range(dev_dax, i, &range);
239 rc = remove_memory(range.start, range_len(&range));
241 remove_resource(data->res[i]);
247 any_hotremove_failed = true;
249 "mapping%d: %#llx-%#llx cannot be hotremoved until the next reboot\n",
250 i, range.start, range.end);
253 if (success >= dev_dax->nr_range) {
254 memory_group_unregister(data->mgid);
255 kfree(data->res_name);
257 dev_set_drvdata(dev, NULL);
259 * Clear the memtype association on successful unplug.
260 * If not, we have memory blocks left which can be
261 * offlined/onlined later. We need to keep memory_dev_type
262 * for that. This implies this reference will be around
265 clear_node_memory_type(node, NULL);
269 static void dev_dax_kmem_remove(struct dev_dax *dev_dax)
272 * Without hotremove purposely leak the request_mem_region() for the
273 * device-dax range and return '0' to ->remove() attempts. The removal
274 * of the device from the driver always succeeds, but the region is
275 * permanently pinned as reserved by the unreleased
276 * request_mem_region().
278 any_hotremove_failed = true;
280 #endif /* CONFIG_MEMORY_HOTREMOVE */
282 static struct dax_device_driver device_dax_kmem_driver = {
283 .probe = dev_dax_kmem_probe,
284 .remove = dev_dax_kmem_remove,
285 .type = DAXDRV_KMEM_TYPE,
288 static int __init dax_kmem_init(void)
292 /* Resource name is permanently allocated if any hotremove fails. */
293 kmem_name = kstrdup_const("System RAM (kmem)", GFP_KERNEL);
297 rc = dax_driver_register(&device_dax_kmem_driver);
299 goto error_dax_driver;
304 kmem_put_memory_types();
305 kfree_const(kmem_name);
309 static void __exit dax_kmem_exit(void)
311 dax_driver_unregister(&device_dax_kmem_driver);
312 if (!any_hotremove_failed)
313 kfree_const(kmem_name);
314 kmem_put_memory_types();
317 MODULE_AUTHOR("Intel Corporation");
318 MODULE_LICENSE("GPL v2");
319 module_init(dax_kmem_init);
320 module_exit(dax_kmem_exit);
321 MODULE_ALIAS_DAX_DEVICE(0);