Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 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 "dax-private.h" |
| 15 | #include "bus.h" |
| 16 | |
David Hildenbrand | 8a725e4 | 2020-06-04 16:48:48 -0700 | [diff] [blame] | 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; |
| 21 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 22 | static int dax_kmem_range(struct dev_dax *dev_dax, int i, struct range *r) |
Dan Williams | 59bc8d1 | 2020-10-13 16:49:48 -0700 | [diff] [blame] | 23 | { |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 24 | struct dev_dax_range *dax_range = &dev_dax->ranges[i]; |
| 25 | struct range *range = &dax_range->range; |
Dan Williams | 59bc8d1 | 2020-10-13 16:49:48 -0700 | [diff] [blame] | 26 | |
| 27 | /* memory-block align the hotplug range */ |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 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; |
| 32 | r->end = range->end; |
| 33 | return -ENOSPC; |
| 34 | } |
| 35 | return 0; |
Dan Williams | 59bc8d1 | 2020-10-13 16:49:48 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 38 | struct dax_kmem_data { |
| 39 | const char *res_name; |
David Hildenbrand | eedf634 | 2021-09-07 19:55:37 -0700 | [diff] [blame] | 40 | int mgid; |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 41 | struct resource *res[]; |
| 42 | }; |
| 43 | |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 44 | static int dev_dax_kmem_probe(struct dev_dax *dev_dax) |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 45 | { |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 46 | struct device *dev = &dev_dax->dev; |
David Hildenbrand | eedf634 | 2021-09-07 19:55:37 -0700 | [diff] [blame] | 47 | unsigned long total_len = 0; |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 48 | struct dax_kmem_data *data; |
David Hildenbrand | eedf634 | 2021-09-07 19:55:37 -0700 | [diff] [blame] | 49 | int i, rc, mapped = 0; |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 50 | int numa_node; |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 51 | |
| 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 Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 60 | dev_warn(dev, "rejecting DAX region with invalid node: %d\n", |
| 61 | numa_node); |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 62 | return -EINVAL; |
| 63 | } |
| 64 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 65 | for (i = 0; i < dev_dax->nr_range; i++) { |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 66 | struct range range; |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 67 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 68 | 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 Hildenbrand | eedf634 | 2021-09-07 19:55:37 -0700 | [diff] [blame] | 74 | 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 Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 103 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 104 | /* Region is permanently reserved if hotremove fails. */ |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 105 | res = request_mem_region(range.start, range_len(&range), data->res_name); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 106 | 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 Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 115 | rc = -EBUSY; |
| 116 | goto err_request_mem; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 117 | } |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 118 | data->res[i] = res; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 119 | |
| 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 Hildenbrand | eedf634 | 2021-09-07 19:55:37 -0700 | [diff] [blame] | 132 | rc = add_memory_driver_managed(data->mgid, range.start, |
| 133 | range_len(&range), kmem_name, MHP_NID_IS_MGID); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 134 | |
| 135 | if (rc) { |
| 136 | dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n", |
| 137 | i, range.start, range.end); |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 138 | release_resource(res); |
| 139 | kfree(res); |
| 140 | data->res[i] = NULL; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 141 | if (mapped) |
| 142 | continue; |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 143 | goto err_request_mem; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 144 | } |
| 145 | mapped++; |
Pavel Tatashin | 31e4ca9 | 2019-07-16 16:30:27 -0700 | [diff] [blame] | 146 | } |
Dan Williams | 7e6b431 | 2020-10-13 16:49:53 -0700 | [diff] [blame] | 147 | |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 148 | dev_set_drvdata(dev, data); |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 149 | |
| 150 | return 0; |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 151 | |
| 152 | err_request_mem: |
David Hildenbrand | eedf634 | 2021-09-07 19:55:37 -0700 | [diff] [blame] | 153 | memory_group_unregister(data->mgid); |
| 154 | err_reg_mgid: |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 155 | kfree(data->res_name); |
| 156 | err_res_name: |
| 157 | kfree(data); |
| 158 | return rc; |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 161 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Uwe Kleine-König | 0d519e0 | 2021-02-05 23:28:42 +0100 | [diff] [blame] | 162 | static void dev_dax_kmem_remove(struct dev_dax *dev_dax) |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 163 | { |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 164 | int i, success = 0; |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 165 | struct device *dev = &dev_dax->dev; |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 166 | struct dax_kmem_data *data = dev_get_drvdata(dev); |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 167 | |
| 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 Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 174 | 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 Hildenbrand | e1c158e | 2021-09-07 19:55:09 -0700 | [diff] [blame] | 182 | rc = remove_memory(range.start, range_len(&range)); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 183 | if (rc == 0) { |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 184 | release_resource(data->res[i]); |
| 185 | kfree(data->res[i]); |
| 186 | data->res[i] = NULL; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 187 | success++; |
| 188 | continue; |
| 189 | } |
David Hildenbrand | 8a725e4 | 2020-06-04 16:48:48 -0700 | [diff] [blame] | 190 | any_hotremove_failed = true; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 191 | dev_err(dev, |
| 192 | "mapping%d: %#llx-%#llx cannot be hotremoved until the next reboot\n", |
| 193 | i, range.start, range.end); |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 196 | if (success >= dev_dax->nr_range) { |
David Hildenbrand | eedf634 | 2021-09-07 19:55:37 -0700 | [diff] [blame] | 197 | memory_group_unregister(data->mgid); |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 198 | kfree(data->res_name); |
| 199 | kfree(data); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 200 | dev_set_drvdata(dev, NULL); |
| 201 | } |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 202 | } |
| 203 | #else |
Uwe Kleine-König | 0d519e0 | 2021-02-05 23:28:42 +0100 | [diff] [blame] | 204 | static void dev_dax_kmem_remove(struct dev_dax *dev_dax) |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 205 | { |
| 206 | /* |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 207 | * 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 Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 211 | * request_mem_region(). |
| 212 | */ |
David Hildenbrand | 8a725e4 | 2020-06-04 16:48:48 -0700 | [diff] [blame] | 213 | any_hotremove_failed = true; |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 214 | } |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 215 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 216 | |
| 217 | static struct dax_device_driver device_dax_kmem_driver = { |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 218 | .probe = dev_dax_kmem_probe, |
| 219 | .remove = dev_dax_kmem_remove, |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | static int __init dax_kmem_init(void) |
| 223 | { |
David Hildenbrand | 8a725e4 | 2020-06-04 16:48:48 -0700 | [diff] [blame] | 224 | 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 Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static void __exit dax_kmem_exit(void) |
| 238 | { |
| 239 | dax_driver_unregister(&device_dax_kmem_driver); |
David Hildenbrand | 8a725e4 | 2020-06-04 16:48:48 -0700 | [diff] [blame] | 240 | if (!any_hotremove_failed) |
| 241 | kfree_const(kmem_name); |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | MODULE_AUTHOR("Intel Corporation"); |
| 245 | MODULE_LICENSE("GPL v2"); |
| 246 | module_init(dax_kmem_init); |
| 247 | module_exit(dax_kmem_exit); |
| 248 | MODULE_ALIAS_DAX_DEVICE(0); |