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