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>
13 #include <linux/mman.h>
14 #include "dax-private.h"
17 int dev_dax_kmem_probe(struct device
*dev
)
19 struct dev_dax
*dev_dax
= to_dev_dax(dev
);
20 struct resource
*res
= &dev_dax
->region
->res
;
21 resource_size_t kmem_start
;
22 resource_size_t kmem_size
;
23 resource_size_t kmem_end
;
24 struct resource
*new_res
;
29 * Ensure good NUMA information for the persistent memory.
30 * Without this check, there is a risk that slow memory
31 * could be mixed in a node with faster memory, causing
32 * unavoidable performance issues.
34 numa_node
= dev_dax
->target_node
;
36 dev_warn(dev
, "rejecting DAX region %pR with invalid node: %d\n",
41 /* Hotplug starting at the beginning of the next block: */
42 kmem_start
= ALIGN(res
->start
, memory_block_size_bytes());
44 kmem_size
= resource_size(res
);
45 /* Adjust the size down to compensate for moving up kmem_start: */
46 kmem_size
-= kmem_start
- res
->start
;
47 /* Align the size down to cover only complete blocks: */
48 kmem_size
&= ~(memory_block_size_bytes() - 1);
49 kmem_end
= kmem_start
+ kmem_size
;
51 /* Region is permanently reserved. Hot-remove not yet implemented. */
52 new_res
= request_mem_region(kmem_start
, kmem_size
, dev_name(dev
));
54 dev_warn(dev
, "could not reserve region [%pa-%pa]\n",
55 &kmem_start
, &kmem_end
);
60 * Set flags appropriate for System RAM. Leave ..._BUSY clear
61 * so that add_memory() can add a child resource. Do not
62 * inherit flags from the parent since it may set new flags
63 * unknown to us that will break add_memory() below.
65 new_res
->flags
= IORESOURCE_SYSTEM_RAM
;
66 new_res
->name
= dev_name(dev
);
68 rc
= add_memory(numa_node
, new_res
->start
, resource_size(new_res
));
70 release_resource(new_res
);
74 dev_dax
->dax_kmem_res
= new_res
;
79 #ifdef CONFIG_MEMORY_HOTREMOVE
80 static int dev_dax_kmem_remove(struct device
*dev
)
82 struct dev_dax
*dev_dax
= to_dev_dax(dev
);
83 struct resource
*res
= dev_dax
->dax_kmem_res
;
84 resource_size_t kmem_start
= res
->start
;
85 resource_size_t kmem_size
= resource_size(res
);
89 * We have one shot for removing memory, if some memory blocks were not
90 * offline prior to calling this function remove_memory() will fail, and
91 * there is no way to hotremove this memory until reboot because device
92 * unbind will succeed even if we return failure.
94 rc
= remove_memory(dev_dax
->target_node
, kmem_start
, kmem_size
);
97 "DAX region %pR cannot be hotremoved until the next reboot\n",
102 /* Release and free dax resources */
103 release_resource(res
);
105 dev_dax
->dax_kmem_res
= NULL
;
110 static int dev_dax_kmem_remove(struct device
*dev
)
113 * Without hotremove purposely leak the request_mem_region() for the
114 * device-dax range and return '0' to ->remove() attempts. The removal
115 * of the device from the driver always succeeds, but the region is
116 * permanently pinned as reserved by the unreleased
117 * request_mem_region().
121 #endif /* CONFIG_MEMORY_HOTREMOVE */
123 static struct dax_device_driver device_dax_kmem_driver
= {
125 .probe
= dev_dax_kmem_probe
,
126 .remove
= dev_dax_kmem_remove
,
130 static int __init
dax_kmem_init(void)
132 return dax_driver_register(&device_dax_kmem_driver
);
135 static void __exit
dax_kmem_exit(void)
137 dax_driver_unregister(&device_dax_kmem_driver
);
140 MODULE_AUTHOR("Intel Corporation");
141 MODULE_LICENSE("GPL v2");
142 module_init(dax_kmem_init
);
143 module_exit(dax_kmem_exit
);
144 MODULE_ALIAS_DAX_DEVICE(0);