blob: a37622060fffa3a5ba821ee90263d7dfbd3db91d [file] [log] [blame]
Dave Hansenc221c0b2019-02-25 10:57:40 -08001// 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 "dax-private.h"
15#include "bus.h"
16
David Hildenbrand8a725e42020-06-04 16:48:48 -070017/* Memory resource name used for add_memory_driver_managed(). */
18static const char *kmem_name;
19/* Set if any memory will remain added when the driver will be unloaded. */
20static bool any_hotremove_failed;
21
Dan Williams60e93dc2020-10-13 16:50:39 -070022static int dax_kmem_range(struct dev_dax *dev_dax, int i, struct range *r)
Dan Williams59bc8d12020-10-13 16:49:48 -070023{
Dan Williams60e93dc2020-10-13 16:50:39 -070024 struct dev_dax_range *dax_range = &dev_dax->ranges[i];
25 struct range *range = &dax_range->range;
Dan Williams59bc8d12020-10-13 16:49:48 -070026
27 /* memory-block align the hotplug range */
Dan Williams60e93dc2020-10-13 16:50:39 -070028 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;
32 r->end = range->end;
33 return -ENOSPC;
34 }
35 return 0;
Dan Williams59bc8d12020-10-13 16:49:48 -070036}
37
Dan Williamsa455aa72020-10-15 20:04:22 -070038struct dax_kmem_data {
39 const char *res_name;
David Hildenbrandeedf6342021-09-07 19:55:37 -070040 int mgid;
Dan Williamsa455aa72020-10-15 20:04:22 -070041 struct resource *res[];
42};
43
Dan Williamsf11cf812020-10-13 16:50:08 -070044static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
Dave Hansenc221c0b2019-02-25 10:57:40 -080045{
Dan Williamsf11cf812020-10-13 16:50:08 -070046 struct device *dev = &dev_dax->dev;
David Hildenbrandeedf6342021-09-07 19:55:37 -070047 unsigned long total_len = 0;
Dan Williamsa455aa72020-10-15 20:04:22 -070048 struct dax_kmem_data *data;
David Hildenbrandeedf6342021-09-07 19:55:37 -070049 int i, rc, mapped = 0;
Dave Hansenc221c0b2019-02-25 10:57:40 -080050 int numa_node;
Dave Hansenc221c0b2019-02-25 10:57:40 -080051
52 /*
53 * Ensure good NUMA information for the persistent memory.
54 * Without this check, there is a risk that slow memory
55 * could be mixed in a node with faster memory, causing
56 * unavoidable performance issues.
57 */
58 numa_node = dev_dax->target_node;
59 if (numa_node < 0) {
Dan Williamsf5516ec2020-10-13 16:49:43 -070060 dev_warn(dev, "rejecting DAX region with invalid node: %d\n",
61 numa_node);
Dave Hansenc221c0b2019-02-25 10:57:40 -080062 return -EINVAL;
63 }
64
Dan Williams60e93dc2020-10-13 16:50:39 -070065 for (i = 0; i < dev_dax->nr_range; i++) {
Dan Williams60e93dc2020-10-13 16:50:39 -070066 struct range range;
Dave Hansenc221c0b2019-02-25 10:57:40 -080067
Dan Williams60e93dc2020-10-13 16:50:39 -070068 rc = dax_kmem_range(dev_dax, i, &range);
69 if (rc) {
70 dev_info(dev, "mapping%d: %#llx-%#llx too small after alignment\n",
71 i, range.start, range.end);
72 continue;
73 }
David Hildenbrandeedf6342021-09-07 19:55:37 -070074 total_len += range_len(&range);
75 }
76
77 if (!total_len) {
78 dev_warn(dev, "rejecting DAX region without any memory after alignment\n");
79 return -EINVAL;
80 }
81
82 data = kzalloc(struct_size(data, res, dev_dax->nr_range), GFP_KERNEL);
83 if (!data)
84 return -ENOMEM;
85
86 rc = -ENOMEM;
87 data->res_name = kstrdup(dev_name(dev), GFP_KERNEL);
88 if (!data->res_name)
89 goto err_res_name;
90
91 rc = memory_group_register_static(numa_node, total_len);
92 if (rc < 0)
93 goto err_reg_mgid;
94 data->mgid = rc;
95
96 for (i = 0; i < dev_dax->nr_range; i++) {
97 struct resource *res;
98 struct range range;
99
100 rc = dax_kmem_range(dev_dax, i, &range);
101 if (rc)
102 continue;
Dave Hansenc221c0b2019-02-25 10:57:40 -0800103
Dan Williams60e93dc2020-10-13 16:50:39 -0700104 /* Region is permanently reserved if hotremove fails. */
Dan Williamsa455aa72020-10-15 20:04:22 -0700105 res = request_mem_region(range.start, range_len(&range), data->res_name);
Dan Williams60e93dc2020-10-13 16:50:39 -0700106 if (!res) {
107 dev_warn(dev, "mapping%d: %#llx-%#llx could not reserve region\n",
108 i, range.start, range.end);
109 /*
110 * Once some memory has been onlined we can't
111 * assume that it can be un-onlined safely.
112 */
113 if (mapped)
114 continue;
Dan Williamsa455aa72020-10-15 20:04:22 -0700115 rc = -EBUSY;
116 goto err_request_mem;
Dan Williams60e93dc2020-10-13 16:50:39 -0700117 }
Dan Williamsa455aa72020-10-15 20:04:22 -0700118 data->res[i] = res;
Dan Williams60e93dc2020-10-13 16:50:39 -0700119
120 /*
121 * Set flags appropriate for System RAM. Leave ..._BUSY clear
122 * so that add_memory() can add a child resource. Do not
123 * inherit flags from the parent since it may set new flags
124 * unknown to us that will break add_memory() below.
125 */
126 res->flags = IORESOURCE_SYSTEM_RAM;
127
128 /*
129 * Ensure that future kexec'd kernels will not treat
130 * this as RAM automatically.
131 */
David Hildenbrandeedf6342021-09-07 19:55:37 -0700132 rc = add_memory_driver_managed(data->mgid, range.start,
133 range_len(&range), kmem_name, MHP_NID_IS_MGID);
Dan Williams60e93dc2020-10-13 16:50:39 -0700134
135 if (rc) {
136 dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n",
137 i, range.start, range.end);
Dan Williamsa455aa72020-10-15 20:04:22 -0700138 release_resource(res);
139 kfree(res);
140 data->res[i] = NULL;
Dan Williams60e93dc2020-10-13 16:50:39 -0700141 if (mapped)
142 continue;
Dan Williamsa455aa72020-10-15 20:04:22 -0700143 goto err_request_mem;
Dan Williams60e93dc2020-10-13 16:50:39 -0700144 }
145 mapped++;
Pavel Tatashin31e4ca92019-07-16 16:30:27 -0700146 }
Dan Williams7e6b4312020-10-13 16:49:53 -0700147
Dan Williamsa455aa72020-10-15 20:04:22 -0700148 dev_set_drvdata(dev, data);
Dave Hansenc221c0b2019-02-25 10:57:40 -0800149
150 return 0;
Dan Williamsa455aa72020-10-15 20:04:22 -0700151
152err_request_mem:
David Hildenbrandeedf6342021-09-07 19:55:37 -0700153 memory_group_unregister(data->mgid);
154err_reg_mgid:
Dan Williamsa455aa72020-10-15 20:04:22 -0700155 kfree(data->res_name);
156err_res_name:
157 kfree(data);
158 return rc;
Dave Hansenc221c0b2019-02-25 10:57:40 -0800159}
160
Pavel Tatashin9f960da2019-07-16 16:30:35 -0700161#ifdef CONFIG_MEMORY_HOTREMOVE
Uwe Kleine-König0d519e02021-02-05 23:28:42 +0100162static void dev_dax_kmem_remove(struct dev_dax *dev_dax)
Pavel Tatashin9f960da2019-07-16 16:30:35 -0700163{
Dan Williams60e93dc2020-10-13 16:50:39 -0700164 int i, success = 0;
Dan Williamsf11cf812020-10-13 16:50:08 -0700165 struct device *dev = &dev_dax->dev;
Dan Williamsa455aa72020-10-15 20:04:22 -0700166 struct dax_kmem_data *data = dev_get_drvdata(dev);
Pavel Tatashin9f960da2019-07-16 16:30:35 -0700167
168 /*
169 * We have one shot for removing memory, if some memory blocks were not
170 * offline prior to calling this function remove_memory() will fail, and
171 * there is no way to hotremove this memory until reboot because device
172 * unbind will succeed even if we return failure.
173 */
Dan Williams60e93dc2020-10-13 16:50:39 -0700174 for (i = 0; i < dev_dax->nr_range; i++) {
175 struct range range;
176 int rc;
177
178 rc = dax_kmem_range(dev_dax, i, &range);
179 if (rc)
180 continue;
181
David Hildenbrande1c158e2021-09-07 19:55:09 -0700182 rc = remove_memory(range.start, range_len(&range));
Dan Williams60e93dc2020-10-13 16:50:39 -0700183 if (rc == 0) {
Dan Williamsa455aa72020-10-15 20:04:22 -0700184 release_resource(data->res[i]);
185 kfree(data->res[i]);
186 data->res[i] = NULL;
Dan Williams60e93dc2020-10-13 16:50:39 -0700187 success++;
188 continue;
189 }
David Hildenbrand8a725e42020-06-04 16:48:48 -0700190 any_hotremove_failed = true;
Dan Williams60e93dc2020-10-13 16:50:39 -0700191 dev_err(dev,
192 "mapping%d: %#llx-%#llx cannot be hotremoved until the next reboot\n",
193 i, range.start, range.end);
Pavel Tatashin9f960da2019-07-16 16:30:35 -0700194 }
195
Dan Williams60e93dc2020-10-13 16:50:39 -0700196 if (success >= dev_dax->nr_range) {
David Hildenbrandeedf6342021-09-07 19:55:37 -0700197 memory_group_unregister(data->mgid);
Dan Williamsa455aa72020-10-15 20:04:22 -0700198 kfree(data->res_name);
199 kfree(data);
Dan Williams60e93dc2020-10-13 16:50:39 -0700200 dev_set_drvdata(dev, NULL);
201 }
Pavel Tatashin9f960da2019-07-16 16:30:35 -0700202}
203#else
Uwe Kleine-König0d519e02021-02-05 23:28:42 +0100204static void dev_dax_kmem_remove(struct dev_dax *dev_dax)
Dave Hansenc221c0b2019-02-25 10:57:40 -0800205{
206 /*
Pavel Tatashin9f960da2019-07-16 16:30:35 -0700207 * Without hotremove purposely leak the request_mem_region() for the
208 * device-dax range and return '0' to ->remove() attempts. The removal
209 * of the device from the driver always succeeds, but the region is
210 * permanently pinned as reserved by the unreleased
Dave Hansenc221c0b2019-02-25 10:57:40 -0800211 * request_mem_region().
212 */
David Hildenbrand8a725e42020-06-04 16:48:48 -0700213 any_hotremove_failed = true;
Dave Hansenc221c0b2019-02-25 10:57:40 -0800214}
Pavel Tatashin9f960da2019-07-16 16:30:35 -0700215#endif /* CONFIG_MEMORY_HOTREMOVE */
Dave Hansenc221c0b2019-02-25 10:57:40 -0800216
217static struct dax_device_driver device_dax_kmem_driver = {
Dan Williamsf11cf812020-10-13 16:50:08 -0700218 .probe = dev_dax_kmem_probe,
219 .remove = dev_dax_kmem_remove,
Dave Hansenc221c0b2019-02-25 10:57:40 -0800220};
221
222static int __init dax_kmem_init(void)
223{
David Hildenbrand8a725e42020-06-04 16:48:48 -0700224 int rc;
225
226 /* Resource name is permanently allocated if any hotremove fails. */
227 kmem_name = kstrdup_const("System RAM (kmem)", GFP_KERNEL);
228 if (!kmem_name)
229 return -ENOMEM;
230
231 rc = dax_driver_register(&device_dax_kmem_driver);
232 if (rc)
233 kfree_const(kmem_name);
234 return rc;
Dave Hansenc221c0b2019-02-25 10:57:40 -0800235}
236
237static void __exit dax_kmem_exit(void)
238{
239 dax_driver_unregister(&device_dax_kmem_driver);
David Hildenbrand8a725e42020-06-04 16:48:48 -0700240 if (!any_hotremove_failed)
241 kfree_const(kmem_name);
Dave Hansenc221c0b2019-02-25 10:57:40 -0800242}
243
244MODULE_AUTHOR("Intel Corporation");
245MODULE_LICENSE("GPL v2");
246module_init(dax_kmem_init);
247module_exit(dax_kmem_exit);
248MODULE_ALIAS_DAX_DEVICE(0);