xref: /openbmc/linux/drivers/dax/kmem.c (revision 7e6b431a)
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 	char *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 	res_name = kstrdup(dev_name(dev), GFP_KERNEL);
55 	if (!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), res_name);
60 	if (!new_res) {
61 		dev_warn(dev, "could not reserve region [%#llx-%#llx]\n", range.start, range.end);
62 		kfree(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(res_name);
84 		return rc;
85 	}
86 
87 	dev_set_drvdata(dev, res_name);
88 	dev_dax->dax_kmem_res = new_res;
89 
90 	return 0;
91 }
92 
93 #ifdef CONFIG_MEMORY_HOTREMOVE
94 static int dev_dax_kmem_remove(struct device *dev)
95 {
96 	struct dev_dax *dev_dax = to_dev_dax(dev);
97 	struct range range = dax_kmem_range(dev_dax);
98 	struct resource *res = dev_dax->dax_kmem_res;
99 	const char *res_name = dev_get_drvdata(dev);
100 	int rc;
101 
102 	/*
103 	 * We have one shot for removing memory, if some memory blocks were not
104 	 * offline prior to calling this function remove_memory() will fail, and
105 	 * there is no way to hotremove this memory until reboot because device
106 	 * unbind will succeed even if we return failure.
107 	 */
108 	rc = remove_memory(dev_dax->target_node, range.start, range_len(&range));
109 	if (rc) {
110 		any_hotremove_failed = true;
111 		dev_err(dev, "%#llx-%#llx cannot be hotremoved until the next reboot\n",
112 				range.start, range.end);
113 		return rc;
114 	}
115 
116 	/* Release and free dax resources */
117 	release_resource(res);
118 	kfree(res);
119 	kfree(res_name);
120 	dev_dax->dax_kmem_res = NULL;
121 
122 	return 0;
123 }
124 #else
125 static int dev_dax_kmem_remove(struct device *dev)
126 {
127 	/*
128 	 * Without hotremove purposely leak the request_mem_region() for the
129 	 * device-dax range and return '0' to ->remove() attempts. The removal
130 	 * of the device from the driver always succeeds, but the region is
131 	 * permanently pinned as reserved by the unreleased
132 	 * request_mem_region().
133 	 */
134 	any_hotremove_failed = true;
135 	return 0;
136 }
137 #endif /* CONFIG_MEMORY_HOTREMOVE */
138 
139 static struct dax_device_driver device_dax_kmem_driver = {
140 	.drv = {
141 		.probe = dev_dax_kmem_probe,
142 		.remove = dev_dax_kmem_remove,
143 	},
144 };
145 
146 static int __init dax_kmem_init(void)
147 {
148 	int rc;
149 
150 	/* Resource name is permanently allocated if any hotremove fails. */
151 	kmem_name = kstrdup_const("System RAM (kmem)", GFP_KERNEL);
152 	if (!kmem_name)
153 		return -ENOMEM;
154 
155 	rc = dax_driver_register(&device_dax_kmem_driver);
156 	if (rc)
157 		kfree_const(kmem_name);
158 	return rc;
159 }
160 
161 static void __exit dax_kmem_exit(void)
162 {
163 	dax_driver_unregister(&device_dax_kmem_driver);
164 	if (!any_hotremove_failed)
165 		kfree_const(kmem_name);
166 }
167 
168 MODULE_AUTHOR("Intel Corporation");
169 MODULE_LICENSE("GPL v2");
170 module_init(dax_kmem_init);
171 module_exit(dax_kmem_exit);
172 MODULE_ALIAS_DAX_DEVICE(0);
173