xref: /linux/drivers/dax/kmem.c (revision 00c010e130e58301db2ea0cec1eadc931e1cb8cf)
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>
11 #include <linux/fs.h>
12 #include <linux/mm.h>
13 #include <linux/mman.h>
14 #include <linux/memory-tiers.h>
15 #include <linux/memory_hotplug.h>
16 #include <linux/string_helpers.h>
17 #include "dax-private.h"
18 #include "bus.h"
19 
20 /*
21  * Default abstract distance assigned to the NUMA node onlined
22  * by DAX/kmem if the low level platform driver didn't initialize
23  * one for this NUMA node.
24  */
25 #define MEMTIER_DEFAULT_DAX_ADISTANCE	(MEMTIER_ADISTANCE_DRAM * 5)
26 
27 /* Memory resource name used for add_memory_driver_managed(). */
28 static const char *kmem_name;
29 /* Set if any memory will remain added when the driver will be unloaded. */
30 static bool any_hotremove_failed;
31 
dax_kmem_range(struct dev_dax * dev_dax,int i,struct range * r)32 static int dax_kmem_range(struct dev_dax *dev_dax, int i, struct range *r)
33 {
34 	struct dev_dax_range *dax_range = &dev_dax->ranges[i];
35 	struct range *range = &dax_range->range;
36 
37 	/* memory-block align the hotplug range */
38 	r->start = ALIGN(range->start, memory_block_size_bytes());
39 	r->end = ALIGN_DOWN(range->end + 1, memory_block_size_bytes()) - 1;
40 	if (r->start >= r->end) {
41 		r->start = range->start;
42 		r->end = range->end;
43 		return -ENOSPC;
44 	}
45 	return 0;
46 }
47 
48 struct dax_kmem_data {
49 	const char *res_name;
50 	int mgid;
51 	struct resource *res[];
52 };
53 
54 static DEFINE_MUTEX(kmem_memory_type_lock);
55 static LIST_HEAD(kmem_memory_types);
56 
kmem_find_alloc_memory_type(int adist)57 static struct memory_dev_type *kmem_find_alloc_memory_type(int adist)
58 {
59 	guard(mutex)(&kmem_memory_type_lock);
60 	return mt_find_alloc_memory_type(adist, &kmem_memory_types);
61 }
62 
kmem_put_memory_types(void)63 static void kmem_put_memory_types(void)
64 {
65 	guard(mutex)(&kmem_memory_type_lock);
66 	mt_put_memory_types(&kmem_memory_types);
67 }
68 
dev_dax_kmem_probe(struct dev_dax * dev_dax)69 static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
70 {
71 	struct device *dev = &dev_dax->dev;
72 	unsigned long total_len = 0, orig_len = 0;
73 	struct dax_kmem_data *data;
74 	struct memory_dev_type *mtype;
75 	int i, rc, mapped = 0;
76 	mhp_t mhp_flags;
77 	int numa_node;
78 	int adist = MEMTIER_DEFAULT_DAX_ADISTANCE;
79 
80 	/*
81 	 * Ensure good NUMA information for the persistent memory.
82 	 * Without this check, there is a risk that slow memory
83 	 * could be mixed in a node with faster memory, causing
84 	 * unavoidable performance issues.
85 	 */
86 	numa_node = dev_dax->target_node;
87 	if (numa_node < 0) {
88 		dev_warn(dev, "rejecting DAX region with invalid node: %d\n",
89 				numa_node);
90 		return -EINVAL;
91 	}
92 
93 	mt_calc_adistance(numa_node, &adist);
94 	mtype = kmem_find_alloc_memory_type(adist);
95 	if (IS_ERR(mtype))
96 		return PTR_ERR(mtype);
97 
98 	for (i = 0; i < dev_dax->nr_range; i++) {
99 		struct range range;
100 
101 		orig_len += range_len(&dev_dax->ranges[i].range);
102 		rc = dax_kmem_range(dev_dax, i, &range);
103 		if (rc) {
104 			dev_info(dev, "mapping%d: %#llx-%#llx too small after alignment\n",
105 					i, range.start, range.end);
106 			continue;
107 		}
108 		total_len += range_len(&range);
109 	}
110 
111 	if (!total_len) {
112 		dev_warn(dev, "rejecting DAX region without any memory after alignment\n");
113 		return -EINVAL;
114 	} else if (total_len != orig_len) {
115 		char buf[16];
116 
117 		string_get_size(orig_len - total_len, 1, STRING_UNITS_2,
118 				buf, sizeof(buf));
119 		dev_warn(dev, "DAX region truncated by %s due to alignment\n", buf);
120 	}
121 
122 	init_node_memory_type(numa_node, mtype);
123 
124 	rc = -ENOMEM;
125 	data = kzalloc(struct_size(data, res, dev_dax->nr_range), GFP_KERNEL);
126 	if (!data)
127 		goto err_dax_kmem_data;
128 
129 	data->res_name = kstrdup(dev_name(dev), GFP_KERNEL);
130 	if (!data->res_name)
131 		goto err_res_name;
132 
133 	rc = memory_group_register_static(numa_node, PFN_UP(total_len));
134 	if (rc < 0)
135 		goto err_reg_mgid;
136 	data->mgid = rc;
137 
138 	for (i = 0; i < dev_dax->nr_range; i++) {
139 		struct resource *res;
140 		struct range range;
141 
142 		rc = dax_kmem_range(dev_dax, i, &range);
143 		if (rc)
144 			continue;
145 
146 		/* Region is permanently reserved if hotremove fails. */
147 		res = request_mem_region(range.start, range_len(&range), data->res_name);
148 		if (!res) {
149 			dev_warn(dev, "mapping%d: %#llx-%#llx could not reserve region\n",
150 					i, range.start, range.end);
151 			/*
152 			 * Once some memory has been onlined we can't
153 			 * assume that it can be un-onlined safely.
154 			 */
155 			if (mapped)
156 				continue;
157 			rc = -EBUSY;
158 			goto err_request_mem;
159 		}
160 		data->res[i] = res;
161 
162 		/*
163 		 * Set flags appropriate for System RAM.  Leave ..._BUSY clear
164 		 * so that add_memory() can add a child resource.  Do not
165 		 * inherit flags from the parent since it may set new flags
166 		 * unknown to us that will break add_memory() below.
167 		 */
168 		res->flags = IORESOURCE_SYSTEM_RAM;
169 
170 		mhp_flags = MHP_NID_IS_MGID;
171 		if (dev_dax->memmap_on_memory)
172 			mhp_flags |= MHP_MEMMAP_ON_MEMORY;
173 
174 		/*
175 		 * Ensure that future kexec'd kernels will not treat
176 		 * this as RAM automatically.
177 		 */
178 		rc = add_memory_driver_managed(data->mgid, range.start,
179 				range_len(&range), kmem_name, mhp_flags);
180 
181 		if (rc) {
182 			dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n",
183 					i, range.start, range.end);
184 			remove_resource(res);
185 			kfree(res);
186 			data->res[i] = NULL;
187 			if (mapped)
188 				continue;
189 			goto err_request_mem;
190 		}
191 		mapped++;
192 	}
193 
194 	dev_set_drvdata(dev, data);
195 
196 	return 0;
197 
198 err_request_mem:
199 	memory_group_unregister(data->mgid);
200 err_reg_mgid:
201 	kfree(data->res_name);
202 err_res_name:
203 	kfree(data);
204 err_dax_kmem_data:
205 	clear_node_memory_type(numa_node, mtype);
206 	return rc;
207 }
208 
209 #ifdef CONFIG_MEMORY_HOTREMOVE
dev_dax_kmem_remove(struct dev_dax * dev_dax)210 static void dev_dax_kmem_remove(struct dev_dax *dev_dax)
211 {
212 	int i, success = 0;
213 	int node = dev_dax->target_node;
214 	struct device *dev = &dev_dax->dev;
215 	struct dax_kmem_data *data = dev_get_drvdata(dev);
216 
217 	/*
218 	 * We have one shot for removing memory, if some memory blocks were not
219 	 * offline prior to calling this function remove_memory() will fail, and
220 	 * there is no way to hotremove this memory until reboot because device
221 	 * unbind will succeed even if we return failure.
222 	 */
223 	for (i = 0; i < dev_dax->nr_range; i++) {
224 		struct range range;
225 		int rc;
226 
227 		rc = dax_kmem_range(dev_dax, i, &range);
228 		if (rc)
229 			continue;
230 
231 		rc = remove_memory(range.start, range_len(&range));
232 		if (rc == 0) {
233 			remove_resource(data->res[i]);
234 			kfree(data->res[i]);
235 			data->res[i] = NULL;
236 			success++;
237 			continue;
238 		}
239 		any_hotremove_failed = true;
240 		dev_err(dev,
241 			"mapping%d: %#llx-%#llx cannot be hotremoved until the next reboot\n",
242 				i, range.start, range.end);
243 	}
244 
245 	if (success >= dev_dax->nr_range) {
246 		memory_group_unregister(data->mgid);
247 		kfree(data->res_name);
248 		kfree(data);
249 		dev_set_drvdata(dev, NULL);
250 		/*
251 		 * Clear the memtype association on successful unplug.
252 		 * If not, we have memory blocks left which can be
253 		 * offlined/onlined later. We need to keep memory_dev_type
254 		 * for that. This implies this reference will be around
255 		 * till next reboot.
256 		 */
257 		clear_node_memory_type(node, NULL);
258 	}
259 }
260 #else
dev_dax_kmem_remove(struct dev_dax * dev_dax)261 static void dev_dax_kmem_remove(struct dev_dax *dev_dax)
262 {
263 	/*
264 	 * Without hotremove purposely leak the request_mem_region() for the
265 	 * device-dax range and return '0' to ->remove() attempts. The removal
266 	 * of the device from the driver always succeeds, but the region is
267 	 * permanently pinned as reserved by the unreleased
268 	 * request_mem_region().
269 	 */
270 	any_hotremove_failed = true;
271 }
272 #endif /* CONFIG_MEMORY_HOTREMOVE */
273 
274 static struct dax_device_driver device_dax_kmem_driver = {
275 	.probe = dev_dax_kmem_probe,
276 	.remove = dev_dax_kmem_remove,
277 	.type = DAXDRV_KMEM_TYPE,
278 };
279 
dax_kmem_init(void)280 static int __init dax_kmem_init(void)
281 {
282 	int rc;
283 
284 	/* Resource name is permanently allocated if any hotremove fails. */
285 	kmem_name = kstrdup_const("System RAM (kmem)", GFP_KERNEL);
286 	if (!kmem_name)
287 		return -ENOMEM;
288 
289 	rc = dax_driver_register(&device_dax_kmem_driver);
290 	if (rc)
291 		goto error_dax_driver;
292 
293 	return rc;
294 
295 error_dax_driver:
296 	kmem_put_memory_types();
297 	kfree_const(kmem_name);
298 	return rc;
299 }
300 
dax_kmem_exit(void)301 static void __exit dax_kmem_exit(void)
302 {
303 	dax_driver_unregister(&device_dax_kmem_driver);
304 	if (!any_hotremove_failed)
305 		kfree_const(kmem_name);
306 	kmem_put_memory_types();
307 }
308 
309 MODULE_AUTHOR("Intel Corporation");
310 MODULE_DESCRIPTION("KMEM DAX: map dax-devices as System-RAM");
311 MODULE_LICENSE("GPL v2");
312 module_init(dax_kmem_init);
313 module_exit(dax_kmem_exit);
314 MODULE_ALIAS_DAX_DEVICE(0);
315