inet: frag: enforce memory limits earlier
[linux/fpc-iii.git] / drivers / dax / pmem.c
blobeebb35720398bf6280b0b61a1a85d43de41bfcac
1 /*
2 * Copyright(c) 2016 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #include <linux/percpu-refcount.h>
14 #include <linux/memremap.h>
15 #include <linux/module.h>
16 #include <linux/pfn_t.h>
17 #include "../nvdimm/pfn.h"
18 #include "../nvdimm/nd.h"
19 #include "dax.h"
21 struct dax_pmem {
22 struct device *dev;
23 struct percpu_ref ref;
24 struct completion cmp;
27 static struct dax_pmem *to_dax_pmem(struct percpu_ref *ref)
29 return container_of(ref, struct dax_pmem, ref);
32 static void dax_pmem_percpu_release(struct percpu_ref *ref)
34 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
36 dev_dbg(dax_pmem->dev, "%s\n", __func__);
37 complete(&dax_pmem->cmp);
40 static void dax_pmem_percpu_exit(void *data)
42 struct percpu_ref *ref = data;
43 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
45 dev_dbg(dax_pmem->dev, "%s\n", __func__);
46 percpu_ref_exit(ref);
49 static void dax_pmem_percpu_kill(void *data)
51 struct percpu_ref *ref = data;
52 struct dax_pmem *dax_pmem = to_dax_pmem(ref);
54 dev_dbg(dax_pmem->dev, "%s\n", __func__);
55 percpu_ref_kill(ref);
56 wait_for_completion(&dax_pmem->cmp);
59 static int dax_pmem_probe(struct device *dev)
61 void *addr;
62 struct resource res;
63 struct dax_dev *dax_dev;
64 int rc, id, region_id;
65 struct nd_pfn_sb *pfn_sb;
66 struct dax_pmem *dax_pmem;
67 struct nd_namespace_io *nsio;
68 struct dax_region *dax_region;
69 struct nd_namespace_common *ndns;
70 struct nd_dax *nd_dax = to_nd_dax(dev);
71 struct nd_pfn *nd_pfn = &nd_dax->nd_pfn;
72 struct vmem_altmap __altmap, *altmap = NULL;
74 ndns = nvdimm_namespace_common_probe(dev);
75 if (IS_ERR(ndns))
76 return PTR_ERR(ndns);
77 nsio = to_nd_namespace_io(&ndns->dev);
79 /* parse the 'pfn' info block via ->rw_bytes */
80 rc = devm_nsio_enable(dev, nsio);
81 if (rc)
82 return rc;
83 altmap = nvdimm_setup_pfn(nd_pfn, &res, &__altmap);
84 if (IS_ERR(altmap))
85 return PTR_ERR(altmap);
86 devm_nsio_disable(dev, nsio);
88 pfn_sb = nd_pfn->pfn_sb;
90 if (!devm_request_mem_region(dev, nsio->res.start,
91 resource_size(&nsio->res), dev_name(dev))) {
92 dev_warn(dev, "could not reserve region %pR\n", &nsio->res);
93 return -EBUSY;
96 dax_pmem = devm_kzalloc(dev, sizeof(*dax_pmem), GFP_KERNEL);
97 if (!dax_pmem)
98 return -ENOMEM;
100 dax_pmem->dev = dev;
101 init_completion(&dax_pmem->cmp);
102 rc = percpu_ref_init(&dax_pmem->ref, dax_pmem_percpu_release, 0,
103 GFP_KERNEL);
104 if (rc)
105 return rc;
107 rc = devm_add_action_or_reset(dev, dax_pmem_percpu_exit,
108 &dax_pmem->ref);
109 if (rc)
110 return rc;
112 addr = devm_memremap_pages(dev, &res, &dax_pmem->ref, altmap);
113 if (IS_ERR(addr))
114 return PTR_ERR(addr);
116 rc = devm_add_action_or_reset(dev, dax_pmem_percpu_kill,
117 &dax_pmem->ref);
118 if (rc)
119 return rc;
121 /* adjust the dax_region resource to the start of data */
122 res.start += le64_to_cpu(pfn_sb->dataoff);
124 rc = sscanf(dev_name(&ndns->dev), "namespace%d.%d", &region_id, &id);
125 if (rc != 2)
126 return -EINVAL;
128 dax_region = alloc_dax_region(dev, region_id, &res,
129 le32_to_cpu(pfn_sb->align), addr, PFN_DEV|PFN_MAP);
130 if (!dax_region)
131 return -ENOMEM;
133 /* TODO: support for subdividing a dax region... */
134 dax_dev = devm_create_dax_dev(dax_region, id, &res, 1);
136 /* child dax_dev instances now own the lifetime of the dax_region */
137 dax_region_put(dax_region);
139 return PTR_ERR_OR_ZERO(dax_dev);
142 static struct nd_device_driver dax_pmem_driver = {
143 .probe = dax_pmem_probe,
144 .drv = {
145 .name = "dax_pmem",
147 .type = ND_DRIVER_DAX_PMEM,
150 static int __init dax_pmem_init(void)
152 return nd_driver_register(&dax_pmem_driver);
154 module_init(dax_pmem_init);
156 static void __exit dax_pmem_exit(void)
158 driver_unregister(&dax_pmem_driver.drv);
160 module_exit(dax_pmem_exit);
162 MODULE_LICENSE("GPL v2");
163 MODULE_AUTHOR("Intel Corporation");
164 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_DAX_PMEM);