xref: /openbmc/linux/drivers/dax/kmem.c (revision 59bc8d10)
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 
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 
22 static struct range dax_kmem_range(struct dev_dax *dev_dax)
23 {
24 	struct range range;
25 
26 	/* memory-block align the hotplug range */
27 	range.start = ALIGN(dev_dax->range.start, memory_block_size_bytes());
28 	range.end = ALIGN_DOWN(dev_dax->range.end + 1, memory_block_size_bytes()) - 1;
29 	return range;
30 }
31 
32 int dev_dax_kmem_probe(struct device *dev)
33 {
34 	struct dev_dax *dev_dax = to_dev_dax(dev);
35 	struct range range = dax_kmem_range(dev_dax);
36 	struct resource *new_res;
37 	const char *new_res_name;
38 	int numa_node;
39 	int rc;
40 
41 	/*
42 	 * Ensure good NUMA information for the persistent memory.
43 	 * Without this check, there is a risk that slow memory
44 	 * could be mixed in a node with faster memory, causing
45 	 * unavoidable performance issues.
46 	 */
47 	numa_node = dev_dax->target_node;
48 	if (numa_node < 0) {
49 		dev_warn(dev, "rejecting DAX region with invalid node: %d\n",
50 				numa_node);
51 		return -EINVAL;
52 	}
53 
54 	new_res_name = kstrdup(dev_name(dev), GFP_KERNEL);
55 	if (!new_res_name)
56 		return -ENOMEM;
57 
58 	/* Region is permanently reserved if hotremove fails. */
59 	new_res = request_mem_region(range.start, range_len(&range), new_res_name);
60 	if (!new_res) {
61 		dev_warn(dev, "could not reserve region [%#llx-%#llx]\n", range.start, range.end);
62 		kfree(new_res_name);
63 		return -EBUSY;
64 	}
65 
66 	/*
67 	 * Set flags appropriate for System RAM.  Leave ..._BUSY clear
68 	 * so that add_memory() can add a child resource.  Do not
69 	 * inherit flags from the parent since it may set new flags
70 	 * unknown to us that will break add_memory() below.
71 	 */
72 	new_res->flags = IORESOURCE_SYSTEM_RAM;
73 
74 	/*
75 	 * Ensure that future kexec'd kernels will not treat this as RAM
76 	 * automatically.
77 	 */
78 	rc = add_memory_driver_managed(numa_node, new_res->start,
79 				       resource_size(new_res), kmem_name);
80 	if (rc) {
81 		release_resource(new_res);
82 		kfree(new_res);
83 		kfree(new_res_name);
84 		return rc;
85 	}
86 	dev_dax->dax_kmem_res = new_res;
87 
88 	return 0;
89 }
90 
91 #ifdef CONFIG_MEMORY_HOTREMOVE
92 static int dev_dax_kmem_remove(struct device *dev)
93 {
94 	struct dev_dax *dev_dax = to_dev_dax(dev);
95 	struct range range = dax_kmem_range(dev_dax);
96 	struct resource *res = dev_dax->dax_kmem_res;
97 	const char *res_name = res->name;
98 	int rc;
99 
100 	/*
101 	 * We have one shot for removing memory, if some memory blocks were not
102 	 * offline prior to calling this function remove_memory() will fail, and
103 	 * there is no way to hotremove this memory until reboot because device
104 	 * unbind will succeed even if we return failure.
105 	 */
106 	rc = remove_memory(dev_dax->target_node, range.start, range_len(&range));
107 	if (rc) {
108 		any_hotremove_failed = true;
109 		dev_err(dev, "%#llx-%#llx cannot be hotremoved until the next reboot\n",
110 				range.start, range.end);
111 		return rc;
112 	}
113 
114 	/* Release and free dax resources */
115 	release_resource(res);
116 	kfree(res);
117 	kfree(res_name);
118 	dev_dax->dax_kmem_res = NULL;
119 
120 	return 0;
121 }
122 #else
123 static int dev_dax_kmem_remove(struct device *dev)
124 {
125 	/*
126 	 * Without hotremove purposely leak the request_mem_region() for the
127 	 * device-dax range and return '0' to ->remove() attempts. The removal
128 	 * of the device from the driver always succeeds, but the region is
129 	 * permanently pinned as reserved by the unreleased
130 	 * request_mem_region().
131 	 */
132 	any_hotremove_failed = true;
133 	return 0;
134 }
135 #endif /* CONFIG_MEMORY_HOTREMOVE */
136 
137 static struct dax_device_driver device_dax_kmem_driver = {
138 	.drv = {
139 		.probe = dev_dax_kmem_probe,
140 		.remove = dev_dax_kmem_remove,
141 	},
142 };
143 
144 static int __init dax_kmem_init(void)
145 {
146 	int rc;
147 
148 	/* Resource name is permanently allocated if any hotremove fails. */
149 	kmem_name = kstrdup_const("System RAM (kmem)", GFP_KERNEL);
150 	if (!kmem_name)
151 		return -ENOMEM;
152 
153 	rc = dax_driver_register(&device_dax_kmem_driver);
154 	if (rc)
155 		kfree_const(kmem_name);
156 	return rc;
157 }
158 
159 static void __exit dax_kmem_exit(void)
160 {
161 	dax_driver_unregister(&device_dax_kmem_driver);
162 	if (!any_hotremove_failed)
163 		kfree_const(kmem_name);
164 }
165 
166 MODULE_AUTHOR("Intel Corporation");
167 MODULE_LICENSE("GPL v2");
168 module_init(dax_kmem_init);
169 module_exit(dax_kmem_exit);
170 MODULE_ALIAS_DAX_DEVICE(0);
171