^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright(c) 2016-2019 Intel Corporation. All rights reserved. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/memremap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/pfn_t.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/dax.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "dax-private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "bus.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /* Memory resource name used for add_memory_driver_managed(). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static const char *kmem_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* Set if any memory will remain added when the driver will be unloaded. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static bool any_hotremove_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int dax_kmem_range(struct dev_dax *dev_dax, int i, struct range *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct dev_dax_range *dax_range = &dev_dax->ranges[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct range *range = &dax_range->range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) /* memory-block align the hotplug range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) r->start = ALIGN(range->start, memory_block_size_bytes());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) r->end = ALIGN_DOWN(range->end + 1, memory_block_size_bytes()) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (r->start >= r->end) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) r->start = range->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) r->end = range->end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct dax_kmem_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) const char *res_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct resource *res[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static int dev_dax_kmem_probe(struct dev_dax *dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct device *dev = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct dax_kmem_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) int i, mapped = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int numa_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * Ensure good NUMA information for the persistent memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * Without this check, there is a risk that slow memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * could be mixed in a node with faster memory, causing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * unavoidable performance issues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) numa_node = dev_dax->target_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (numa_node < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) dev_warn(dev, "rejecting DAX region with invalid node: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) numa_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) data = kzalloc(sizeof(*data) + sizeof(struct resource *) * dev_dax->nr_range, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) data->res_name = kstrdup(dev_name(dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (!data->res_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) goto err_res_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) for (i = 0; i < dev_dax->nr_range; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) rc = dax_kmem_range(dev_dax, i, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) dev_info(dev, "mapping%d: %#llx-%#llx too small after alignment\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) i, range.start, range.end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* Region is permanently reserved if hotremove fails. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) res = request_mem_region(range.start, range_len(&range), data->res_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) dev_warn(dev, "mapping%d: %#llx-%#llx could not reserve region\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) i, range.start, range.end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * Once some memory has been onlined we can't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * assume that it can be un-onlined safely.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (mapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) goto err_request_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) data->res[i] = res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * Set flags appropriate for System RAM. Leave ..._BUSY clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * so that add_memory() can add a child resource. Do not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * inherit flags from the parent since it may set new flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * unknown to us that will break add_memory() below.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) res->flags = IORESOURCE_SYSTEM_RAM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * Ensure that future kexec'd kernels will not treat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * this as RAM automatically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) rc = add_memory_driver_managed(numa_node, range.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) range_len(&range), kmem_name, MHP_NONE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) dev_warn(dev, "mapping%d: %#llx-%#llx memory add failed\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) i, range.start, range.end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) release_resource(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) kfree(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) data->res[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (mapped)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto err_request_mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) mapped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) dev_set_drvdata(dev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) err_request_mem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) kfree(data->res_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) err_res_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) #ifdef CONFIG_MEMORY_HOTREMOVE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int dev_dax_kmem_remove(struct dev_dax *dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int i, success = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct device *dev = &dev_dax->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) struct dax_kmem_data *data = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * We have one shot for removing memory, if some memory blocks were not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * offline prior to calling this function remove_memory() will fail, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * there is no way to hotremove this memory until reboot because device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * unbind will succeed even if we return failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) for (i = 0; i < dev_dax->nr_range; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct range range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) rc = dax_kmem_range(dev_dax, i, &range);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) rc = remove_memory(dev_dax->target_node, range.start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) range_len(&range));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (rc == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) release_resource(data->res[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) kfree(data->res[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) data->res[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) success++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) any_hotremove_failed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) "mapping%d: %#llx-%#llx cannot be hotremoved until the next reboot\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) i, range.start, range.end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (success >= dev_dax->nr_range) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) kfree(data->res_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) dev_set_drvdata(dev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int dev_dax_kmem_remove(struct dev_dax *dev_dax)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * Without hotremove purposely leak the request_mem_region() for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * device-dax range and return '0' to ->remove() attempts. The removal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * of the device from the driver always succeeds, but the region is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * permanently pinned as reserved by the unreleased
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * request_mem_region().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) any_hotremove_failed = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) #endif /* CONFIG_MEMORY_HOTREMOVE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static struct dax_device_driver device_dax_kmem_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) .probe = dev_dax_kmem_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) .remove = dev_dax_kmem_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int __init dax_kmem_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /* Resource name is permanently allocated if any hotremove fails. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) kmem_name = kstrdup_const("System RAM (kmem)", GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (!kmem_name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) rc = dax_driver_register(&device_dax_kmem_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) kfree_const(kmem_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) static void __exit dax_kmem_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) dax_driver_unregister(&device_dax_kmem_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (!any_hotremove_failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) kfree_const(kmem_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) MODULE_AUTHOR("Intel Corporation");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) module_init(dax_kmem_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) module_exit(dax_kmem_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) MODULE_ALIAS_DAX_DEVICE(0);