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; |
| 40 | struct resource *res[]; |
| 41 | }; |
| 42 | |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 43 | static int dev_dax_kmem_probe(struct dev_dax *dev_dax) |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 44 | { |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 45 | struct device *dev = &dev_dax->dev; |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 46 | struct dax_kmem_data *data; |
| 47 | int rc = -ENOMEM; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 48 | int i, mapped = 0; |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 49 | int numa_node; |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | * Ensure good NUMA information for the persistent memory. |
| 53 | * Without this check, there is a risk that slow memory |
| 54 | * could be mixed in a node with faster memory, causing |
| 55 | * unavoidable performance issues. |
| 56 | */ |
| 57 | numa_node = dev_dax->target_node; |
| 58 | if (numa_node < 0) { |
Dan Williams | f5516ec | 2020-10-13 16:49:43 -0700 | [diff] [blame] | 59 | dev_warn(dev, "rejecting DAX region with invalid node: %d\n", |
| 60 | numa_node); |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 61 | return -EINVAL; |
| 62 | } |
| 63 | |
Dan Williams | 7d18dd7 | 2020-12-14 19:04:43 -0800 | [diff] [blame] | 64 | data = kzalloc(struct_size(data, res, dev_dax->nr_range), GFP_KERNEL); |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 65 | if (!data) |
David Hildenbrand | 60858c0 | 2020-05-22 22:22:42 -0700 | [diff] [blame] | 66 | return -ENOMEM; |
| 67 | |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 68 | data->res_name = kstrdup(dev_name(dev), GFP_KERNEL); |
| 69 | if (!data->res_name) |
| 70 | goto err_res_name; |
| 71 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 72 | for (i = 0; i < dev_dax->nr_range; i++) { |
| 73 | struct resource *res; |
| 74 | struct range range; |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 75 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 76 | rc = dax_kmem_range(dev_dax, i, &range); |
| 77 | if (rc) { |
| 78 | dev_info(dev, "mapping%d: %#llx-%#llx too small after alignment\n", |
| 79 | i, range.start, range.end); |
| 80 | continue; |
| 81 | } |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 82 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 83 | /* Region is permanently reserved if hotremove fails. */ |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 84 | res = request_mem_region(range.start, range_len(&range), data->res_name); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 85 | if (!res) { |
| 86 | dev_warn(dev, "mapping%d: %#llx-%#llx could not reserve region\n", |
| 87 | i, range.start, range.end); |
| 88 | /* |
| 89 | * Once some memory has been onlined we can't |
| 90 | * assume that it can be un-onlined safely. |
| 91 | */ |
| 92 | if (mapped) |
| 93 | continue; |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 94 | rc = -EBUSY; |
| 95 | goto err_request_mem; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 96 | } |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 97 | data->res[i] = res; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * Set flags appropriate for System RAM. Leave ..._BUSY clear |
| 101 | * so that add_memory() can add a child resource. Do not |
| 102 | * inherit flags from the parent since it may set new flags |
| 103 | * unknown to us that will break add_memory() below. |
| 104 | */ |
| 105 | res->flags = IORESOURCE_SYSTEM_RAM; |
| 106 | |
| 107 | /* |
| 108 | * Ensure that future kexec'd kernels will not treat |
| 109 | * this as RAM automatically. |
| 110 | */ |
| 111 | rc = add_memory_driver_managed(numa_node, range.start, |
David Hildenbrand | b611719 | 2020-10-15 20:08:44 -0700 | [diff] [blame] | 112 | range_len(&range), kmem_name, MHP_NONE); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 113 | |
| 114 | if (rc) { |
| 115 | dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n", |
| 116 | i, range.start, range.end); |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 117 | release_resource(res); |
| 118 | kfree(res); |
| 119 | data->res[i] = NULL; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 120 | if (mapped) |
| 121 | continue; |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 122 | goto err_request_mem; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 123 | } |
| 124 | mapped++; |
Pavel Tatashin | 31e4ca9 | 2019-07-16 16:30:27 -0700 | [diff] [blame] | 125 | } |
Dan Williams | 7e6b431 | 2020-10-13 16:49:53 -0700 | [diff] [blame] | 126 | |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 127 | dev_set_drvdata(dev, data); |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 128 | |
| 129 | return 0; |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 130 | |
| 131 | err_request_mem: |
| 132 | kfree(data->res_name); |
| 133 | err_res_name: |
| 134 | kfree(data); |
| 135 | return rc; |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 138 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Uwe Kleine-König | 0d519e0 | 2021-02-05 23:28:42 +0100 | [diff] [blame] | 139 | static void dev_dax_kmem_remove(struct dev_dax *dev_dax) |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 140 | { |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 141 | int i, success = 0; |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 142 | struct device *dev = &dev_dax->dev; |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 143 | struct dax_kmem_data *data = dev_get_drvdata(dev); |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 144 | |
| 145 | /* |
| 146 | * We have one shot for removing memory, if some memory blocks were not |
| 147 | * offline prior to calling this function remove_memory() will fail, and |
| 148 | * there is no way to hotremove this memory until reboot because device |
| 149 | * unbind will succeed even if we return failure. |
| 150 | */ |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 151 | for (i = 0; i < dev_dax->nr_range; i++) { |
| 152 | struct range range; |
| 153 | int rc; |
| 154 | |
| 155 | rc = dax_kmem_range(dev_dax, i, &range); |
| 156 | if (rc) |
| 157 | continue; |
| 158 | |
| 159 | rc = remove_memory(dev_dax->target_node, range.start, |
| 160 | range_len(&range)); |
| 161 | if (rc == 0) { |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 162 | release_resource(data->res[i]); |
| 163 | kfree(data->res[i]); |
| 164 | data->res[i] = NULL; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 165 | success++; |
| 166 | continue; |
| 167 | } |
David Hildenbrand | 8a725e4 | 2020-06-04 16:48:48 -0700 | [diff] [blame] | 168 | any_hotremove_failed = true; |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 169 | dev_err(dev, |
| 170 | "mapping%d: %#llx-%#llx cannot be hotremoved until the next reboot\n", |
| 171 | i, range.start, range.end); |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 174 | if (success >= dev_dax->nr_range) { |
Dan Williams | a455aa7 | 2020-10-15 20:04:22 -0700 | [diff] [blame] | 175 | kfree(data->res_name); |
| 176 | kfree(data); |
Dan Williams | 60e93dc | 2020-10-13 16:50:39 -0700 | [diff] [blame] | 177 | dev_set_drvdata(dev, NULL); |
| 178 | } |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 179 | } |
| 180 | #else |
Uwe Kleine-König | 0d519e0 | 2021-02-05 23:28:42 +0100 | [diff] [blame] | 181 | static void dev_dax_kmem_remove(struct dev_dax *dev_dax) |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 182 | { |
| 183 | /* |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 184 | * Without hotremove purposely leak the request_mem_region() for the |
| 185 | * device-dax range and return '0' to ->remove() attempts. The removal |
| 186 | * of the device from the driver always succeeds, but the region is |
| 187 | * permanently pinned as reserved by the unreleased |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 188 | * request_mem_region(). |
| 189 | */ |
David Hildenbrand | 8a725e4 | 2020-06-04 16:48:48 -0700 | [diff] [blame] | 190 | any_hotremove_failed = true; |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 191 | } |
Pavel Tatashin | 9f960da | 2019-07-16 16:30:35 -0700 | [diff] [blame] | 192 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 193 | |
| 194 | static struct dax_device_driver device_dax_kmem_driver = { |
Dan Williams | f11cf81 | 2020-10-13 16:50:08 -0700 | [diff] [blame] | 195 | .probe = dev_dax_kmem_probe, |
| 196 | .remove = dev_dax_kmem_remove, |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | static int __init dax_kmem_init(void) |
| 200 | { |
David Hildenbrand | 8a725e4 | 2020-06-04 16:48:48 -0700 | [diff] [blame] | 201 | int rc; |
| 202 | |
| 203 | /* Resource name is permanently allocated if any hotremove fails. */ |
| 204 | kmem_name = kstrdup_const("System RAM (kmem)", GFP_KERNEL); |
| 205 | if (!kmem_name) |
| 206 | return -ENOMEM; |
| 207 | |
| 208 | rc = dax_driver_register(&device_dax_kmem_driver); |
| 209 | if (rc) |
| 210 | kfree_const(kmem_name); |
| 211 | return rc; |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | static void __exit dax_kmem_exit(void) |
| 215 | { |
| 216 | dax_driver_unregister(&device_dax_kmem_driver); |
David Hildenbrand | 8a725e4 | 2020-06-04 16:48:48 -0700 | [diff] [blame] | 217 | if (!any_hotremove_failed) |
| 218 | kfree_const(kmem_name); |
Dave Hansen | c221c0b | 2019-02-25 10:57:40 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | MODULE_AUTHOR("Intel Corporation"); |
| 222 | MODULE_LICENSE("GPL v2"); |
| 223 | module_init(dax_kmem_init); |
| 224 | module_exit(dax_kmem_exit); |
| 225 | MODULE_ALIAS_DAX_DEVICE(0); |