Skip to content

Commit 9f53f9f

Browse files
committed
libnvdimm, pmem: add libnvdimm support to the pmem driver
nd_pmem attaches to persistent memory regions and namespaces emitted by the libnvdimm subsystem, and, same as the original pmem driver, presents the system-physical-address range as a block device. The existing e820-type-12 to pmem setup is converted to an nvdimm_bus that emits an nd_namespace_io device. Note that the X in 'pmemX' is now derived from the parent region. This provides some stability to the pmem devices names from boot-to-boot. The minor numbers are also more predictable by passing 0 to alloc_disk(). Cc: Andy Lutomirski <[email protected]> Cc: Boaz Harrosh <[email protected]> Cc: H. Peter Anvin <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Christoph Hellwig <[email protected]> Signed-off-by: Ross Zwisler <[email protected]> Acked-by: Christoph Hellwig <[email protected]> Tested-by: Toshi Kani <[email protected]> Signed-off-by: Dan Williams <[email protected]>
1 parent 18da2c9 commit 9f53f9f

File tree

3 files changed

+96
-67
lines changed

3 files changed

+96
-67
lines changed

arch/x86/Kconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,6 +1424,9 @@ source "mm/Kconfig"
14241424

14251425
config X86_PMEM_LEGACY
14261426
bool "Support non-standard NVDIMMs and ADR protected memory"
1427+
depends on PHYS_ADDR_T_64BIT
1428+
depends on BLK_DEV
1429+
select LIBNVDIMM
14271430
help
14281431
Treat memory marked using the non-standard e820 type of 12 as used
14291432
by the Intel Sandy Bridge-EP reference BIOS as protected memory.

arch/x86/kernel/pmem.c

Lines changed: 60 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,81 @@
11
/*
22
* Copyright (c) 2015, Christoph Hellwig.
3+
* Copyright (c) 2015, Intel Corporation.
34
*/
4-
#include <linux/memblock.h>
55
#include <linux/platform_device.h>
6-
#include <linux/slab.h>
6+
#include <linux/libnvdimm.h>
7+
#include <linux/module.h>
78
#include <asm/e820.h>
8-
#include <asm/page_types.h>
9-
#include <asm/setup.h>
109

11-
static __init void register_pmem_device(struct resource *res)
10+
static void e820_pmem_release(struct device *dev)
1211
{
13-
struct platform_device *pdev;
14-
int error;
12+
struct nvdimm_bus *nvdimm_bus = dev->platform_data;
1513

16-
pdev = platform_device_alloc("pmem", PLATFORM_DEVID_AUTO);
17-
if (!pdev)
18-
return;
14+
if (nvdimm_bus)
15+
nvdimm_bus_unregister(nvdimm_bus);
16+
}
1917

20-
error = platform_device_add_resources(pdev, res, 1);
21-
if (error)
22-
goto out_put_pdev;
18+
static struct platform_device e820_pmem = {
19+
.name = "e820_pmem",
20+
.id = -1,
21+
.dev = {
22+
.release = e820_pmem_release,
23+
},
24+
};
2325

24-
error = platform_device_add(pdev);
25-
if (error)
26-
goto out_put_pdev;
27-
return;
26+
static const struct attribute_group *e820_pmem_attribute_groups[] = {
27+
&nvdimm_bus_attribute_group,
28+
NULL,
29+
};
2830

29-
out_put_pdev:
30-
dev_warn(&pdev->dev, "failed to add 'pmem' (persistent memory) device!\n");
31-
platform_device_put(pdev);
32-
}
31+
static const struct attribute_group *e820_pmem_region_attribute_groups[] = {
32+
&nd_region_attribute_group,
33+
&nd_device_attribute_group,
34+
NULL,
35+
};
3336

34-
static __init int register_pmem_devices(void)
37+
static __init int register_e820_pmem(void)
3538
{
36-
int i;
39+
static struct nvdimm_bus_descriptor nd_desc;
40+
struct device *dev = &e820_pmem.dev;
41+
struct nvdimm_bus *nvdimm_bus;
42+
int rc, i;
43+
44+
rc = platform_device_register(&e820_pmem);
45+
if (rc)
46+
return rc;
47+
48+
nd_desc.attr_groups = e820_pmem_attribute_groups;
49+
nd_desc.provider_name = "e820";
50+
nvdimm_bus = nvdimm_bus_register(dev, &nd_desc);
51+
if (!nvdimm_bus)
52+
goto err;
53+
dev->platform_data = nvdimm_bus;
3754

3855
for (i = 0; i < e820.nr_map; i++) {
3956
struct e820entry *ei = &e820.map[i];
57+
struct resource res = {
58+
.flags = IORESOURCE_MEM,
59+
.start = ei->addr,
60+
.end = ei->addr + ei->size - 1,
61+
};
62+
struct nd_region_desc ndr_desc;
63+
64+
if (ei->type != E820_PRAM)
65+
continue;
4066

41-
if (ei->type == E820_PRAM) {
42-
struct resource res = {
43-
.flags = IORESOURCE_MEM,
44-
.start = ei->addr,
45-
.end = ei->addr + ei->size - 1,
46-
};
47-
register_pmem_device(&res);
48-
}
67+
memset(&ndr_desc, 0, sizeof(ndr_desc));
68+
ndr_desc.res = &res;
69+
ndr_desc.attr_groups = e820_pmem_region_attribute_groups;
70+
if (!nvdimm_pmem_region_create(nvdimm_bus, &ndr_desc))
71+
goto err;
4972
}
5073

5174
return 0;
75+
76+
err:
77+
dev_err(dev, "failed to register legacy persistent memory ranges\n");
78+
platform_device_unregister(&e820_pmem);
79+
return -ENXIO;
5280
}
53-
device_initcall(register_pmem_devices);
81+
device_initcall(register_e820_pmem);

drivers/nvdimm/pmem.c

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Persistent Memory Driver
33
*
4-
* Copyright (c) 2014, Intel Corporation.
4+
* Copyright (c) 2014-2015, Intel Corporation.
55
* Copyright (c) 2015, Christoph Hellwig <[email protected]>.
66
* Copyright (c) 2015, Boaz Harrosh <[email protected]>.
77
*
@@ -23,8 +23,8 @@
2323
#include <linux/module.h>
2424
#include <linux/moduleparam.h>
2525
#include <linux/slab.h>
26-
27-
#define PMEM_MINORS 16
26+
#include <linux/nd.h>
27+
#include "nd.h"
2828

2929
struct pmem_device {
3030
struct request_queue *pmem_queue;
@@ -37,7 +37,6 @@ struct pmem_device {
3737
};
3838

3939
static int pmem_major;
40-
static atomic_t pmem_index;
4140

4241
static void pmem_do_bvec(struct pmem_device *pmem, struct page *page,
4342
unsigned int len, unsigned int off, int rw,
@@ -118,11 +117,12 @@ static const struct block_device_operations pmem_fops = {
118117
.direct_access = pmem_direct_access,
119118
};
120119

121-
static struct pmem_device *pmem_alloc(struct device *dev, struct resource *res)
120+
static struct pmem_device *pmem_alloc(struct device *dev,
121+
struct resource *res, int id)
122122
{
123123
struct pmem_device *pmem;
124124
struct gendisk *disk;
125-
int idx, err;
125+
int err;
126126

127127
err = -ENOMEM;
128128
pmem = kzalloc(sizeof(*pmem), GFP_KERNEL);
@@ -134,7 +134,8 @@ static struct pmem_device *pmem_alloc(struct device *dev, struct resource *res)
134134

135135
err = -EINVAL;
136136
if (!request_mem_region(pmem->phys_addr, pmem->size, "pmem")) {
137-
dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n", &pmem->phys_addr, pmem->size);
137+
dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n",
138+
&pmem->phys_addr, pmem->size);
138139
goto out_free_dev;
139140
}
140141

@@ -155,19 +156,17 @@ static struct pmem_device *pmem_alloc(struct device *dev, struct resource *res)
155156
blk_queue_max_hw_sectors(pmem->pmem_queue, 1024);
156157
blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY);
157158

158-
disk = alloc_disk(PMEM_MINORS);
159+
disk = alloc_disk(0);
159160
if (!disk)
160161
goto out_free_queue;
161162

162-
idx = atomic_inc_return(&pmem_index) - 1;
163-
164163
disk->major = pmem_major;
165-
disk->first_minor = PMEM_MINORS * idx;
164+
disk->first_minor = 0;
166165
disk->fops = &pmem_fops;
167166
disk->private_data = pmem;
168167
disk->queue = pmem->pmem_queue;
169168
disk->flags = GENHD_FL_EXT_DEVT;
170-
sprintf(disk->disk_name, "pmem%d", idx);
169+
sprintf(disk->disk_name, "pmem%d", id);
171170
disk->driverfs_dev = dev;
172171
set_capacity(disk, pmem->size >> 9);
173172
pmem->pmem_disk = disk;
@@ -198,42 +197,38 @@ static void pmem_free(struct pmem_device *pmem)
198197
kfree(pmem);
199198
}
200199

201-
static int pmem_probe(struct platform_device *pdev)
200+
static int nd_pmem_probe(struct device *dev)
202201
{
202+
struct nd_region *nd_region = to_nd_region(dev->parent);
203+
struct nd_namespace_io *nsio = to_nd_namespace_io(dev);
203204
struct pmem_device *pmem;
204-
struct resource *res;
205-
206-
if (WARN_ON(pdev->num_resources > 1))
207-
return -ENXIO;
208-
209-
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
210-
if (!res)
211-
return -ENXIO;
212205

213-
pmem = pmem_alloc(&pdev->dev, res);
206+
pmem = pmem_alloc(dev, &nsio->res, nd_region->id);
214207
if (IS_ERR(pmem))
215208
return PTR_ERR(pmem);
216209

217-
platform_set_drvdata(pdev, pmem);
210+
dev_set_drvdata(dev, pmem);
218211

219212
return 0;
220213
}
221214

222-
static int pmem_remove(struct platform_device *pdev)
215+
static int nd_pmem_remove(struct device *dev)
223216
{
224-
struct pmem_device *pmem = platform_get_drvdata(pdev);
217+
struct pmem_device *pmem = dev_get_drvdata(dev);
225218

226219
pmem_free(pmem);
227220
return 0;
228221
}
229222

230-
static struct platform_driver pmem_driver = {
231-
.probe = pmem_probe,
232-
.remove = pmem_remove,
233-
.driver = {
234-
.owner = THIS_MODULE,
235-
.name = "pmem",
223+
MODULE_ALIAS("pmem");
224+
MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
225+
static struct nd_device_driver nd_pmem_driver = {
226+
.probe = nd_pmem_probe,
227+
.remove = nd_pmem_remove,
228+
.drv = {
229+
.name = "nd_pmem",
236230
},
231+
.type = ND_DRIVER_NAMESPACE_IO,
237232
};
238233

239234
static int __init pmem_init(void)
@@ -244,16 +239,19 @@ static int __init pmem_init(void)
244239
if (pmem_major < 0)
245240
return pmem_major;
246241

247-
error = platform_driver_register(&pmem_driver);
248-
if (error)
242+
error = nd_driver_register(&nd_pmem_driver);
243+
if (error) {
249244
unregister_blkdev(pmem_major, "pmem");
250-
return error;
245+
return error;
246+
}
247+
248+
return 0;
251249
}
252250
module_init(pmem_init);
253251

254252
static void pmem_exit(void)
255253
{
256-
platform_driver_unregister(&pmem_driver);
254+
driver_unregister(&nd_pmem_driver.drv);
257255
unregister_blkdev(pmem_major, "pmem");
258256
}
259257
module_exit(pmem_exit);

0 commit comments

Comments
 (0)