Skip to content

Commit 60858c0

Browse files
davidhildenbrandtorvalds
authored andcommitted
device-dax: don't leak kernel memory to user space after unloading kmem
Assume we have kmem configured and loaded: [root@localhost ~]# cat /proc/iomem ... 140000000-33fffffff : Persistent Memory$ 140000000-1481fffff : namespace0.0 150000000-33fffffff : dax0.0 150000000-33fffffff : System RAM Assume we try to unload kmem. This force-unloading will work, even if memory cannot get removed from the system. [root@localhost ~]# rmmod kmem [ 86.380228] removing memory fails, because memory [0x0000000150000000-0x0000000157ffffff] is onlined ... [ 86.431225] kmem dax0.0: DAX region [mem 0x150000000-0x33fffffff] cannot be hotremoved until the next reboot Now, we can reconfigure the namespace: [root@localhost ~]# ndctl create-namespace --force --reconfig=namespace0.0 --mode=devdax [ 131.409351] nd_pmem namespace0.0: could not reserve region [mem 0x140000000-0x33fffffff]dax [ 131.410147] nd_pmem: probe of namespace0.0 failed with error -16namespace0.0 --mode=devdax ... This fails as expected due to the busy memory resource, and the memory cannot be used. However, the dax0.0 device is removed, and along its name. The name of the memory resource now points at freed memory (name of the device): [root@localhost ~]# cat /proc/iomem ... 140000000-33fffffff : Persistent Memory 140000000-1481fffff : namespace0.0 150000000-33fffffff : �_�^7_��/_��wR��WQ���^��� ... 150000000-33fffffff : System RAM We have to make sure to duplicate the string. While at it, remove the superfluous setting of the name and fixup a stale comment. Fixes: 9f960da ("device-dax: "Hotremove" persistent memory that is used like normal RAM") Signed-off-by: David Hildenbrand <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Cc: Dan Williams <[email protected]> Cc: Vishal Verma <[email protected]> Cc: Dave Jiang <[email protected]> Cc: Pavel Tatashin <[email protected]> Cc: Andrew Morton <[email protected]> Cc: <[email protected]> [5.3] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4445656 commit 60858c0

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

drivers/dax/kmem.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ int dev_dax_kmem_probe(struct device *dev)
2222
resource_size_t kmem_size;
2323
resource_size_t kmem_end;
2424
struct resource *new_res;
25+
const char *new_res_name;
2526
int numa_node;
2627
int rc;
2728

@@ -48,11 +49,16 @@ int dev_dax_kmem_probe(struct device *dev)
4849
kmem_size &= ~(memory_block_size_bytes() - 1);
4950
kmem_end = kmem_start + kmem_size;
5051

51-
/* Region is permanently reserved. Hot-remove not yet implemented. */
52-
new_res = request_mem_region(kmem_start, kmem_size, dev_name(dev));
52+
new_res_name = kstrdup(dev_name(dev), GFP_KERNEL);
53+
if (!new_res_name)
54+
return -ENOMEM;
55+
56+
/* Region is permanently reserved if hotremove fails. */
57+
new_res = request_mem_region(kmem_start, kmem_size, new_res_name);
5358
if (!new_res) {
5459
dev_warn(dev, "could not reserve region [%pa-%pa]\n",
5560
&kmem_start, &kmem_end);
61+
kfree(new_res_name);
5662
return -EBUSY;
5763
}
5864

@@ -63,12 +69,12 @@ int dev_dax_kmem_probe(struct device *dev)
6369
* unknown to us that will break add_memory() below.
6470
*/
6571
new_res->flags = IORESOURCE_SYSTEM_RAM;
66-
new_res->name = dev_name(dev);
6772

6873
rc = add_memory(numa_node, new_res->start, resource_size(new_res));
6974
if (rc) {
7075
release_resource(new_res);
7176
kfree(new_res);
77+
kfree(new_res_name);
7278
return rc;
7379
}
7480
dev_dax->dax_kmem_res = new_res;
@@ -83,6 +89,7 @@ static int dev_dax_kmem_remove(struct device *dev)
8389
struct resource *res = dev_dax->dax_kmem_res;
8490
resource_size_t kmem_start = res->start;
8591
resource_size_t kmem_size = resource_size(res);
92+
const char *res_name = res->name;
8693
int rc;
8794

8895
/*
@@ -102,6 +109,7 @@ static int dev_dax_kmem_remove(struct device *dev)
102109
/* Release and free dax resources */
103110
release_resource(res);
104111
kfree(res);
112+
kfree(res_name);
105113
dev_dax->dax_kmem_res = NULL;
106114

107115
return 0;

0 commit comments

Comments
 (0)