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