Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 1 | /* |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 2 | * Copyright(c) 2016 - 2017 Intel Corporation. All rights reserved. |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 3 | * |
| 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. |
| 7 | * |
| 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. |
| 12 | */ |
| 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/pfn_t.h> |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 17 | #include <linux/cdev.h> |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 18 | #include <linux/slab.h> |
| 19 | #include <linux/dax.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/mm.h> |
Dan Williams | ccdb07f | 2016-08-06 16:05:06 -0700 | [diff] [blame] | 22 | #include "dax.h" |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 23 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 24 | static struct class *dax_class; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * struct dax_region - mapping infrastructure for dax devices |
| 28 | * @id: kernel-wide unique region for a memory range |
| 29 | * @base: linear address corresponding to @res |
| 30 | * @kref: to pin while other agents have a need to do lookups |
| 31 | * @dev: parent device backing this region |
| 32 | * @align: allocation and mapping alignment for child dax devices |
| 33 | * @res: physical address range of the region |
| 34 | * @pfn_flags: identify whether the pfns are paged back or not |
| 35 | */ |
| 36 | struct dax_region { |
| 37 | int id; |
| 38 | struct ida ida; |
| 39 | void *base; |
| 40 | struct kref kref; |
| 41 | struct device *dev; |
| 42 | unsigned int align; |
| 43 | struct resource res; |
| 44 | unsigned long pfn_flags; |
| 45 | }; |
| 46 | |
| 47 | /** |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 48 | * struct dev_dax - instance data for a subdivision of a dax region |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 49 | * @region - parent region |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 50 | * @dax_dev - core dax functionality |
| 51 | * @dev - device core |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 52 | * @id - child id in the region |
| 53 | * @num_resources - number of physical address extents in this device |
| 54 | * @res - array of physical address ranges |
| 55 | */ |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 56 | struct dev_dax { |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 57 | struct dax_region *region; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 58 | struct dax_device *dax_dev; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 59 | struct device dev; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 60 | int id; |
| 61 | int num_resources; |
| 62 | struct resource res[0]; |
| 63 | }; |
| 64 | |
Dan Williams | d7fe1a6 | 2016-12-17 14:50:04 -0800 | [diff] [blame] | 65 | static ssize_t id_show(struct device *dev, |
| 66 | struct device_attribute *attr, char *buf) |
| 67 | { |
| 68 | struct dax_region *dax_region; |
| 69 | ssize_t rc = -ENXIO; |
| 70 | |
| 71 | device_lock(dev); |
| 72 | dax_region = dev_get_drvdata(dev); |
| 73 | if (dax_region) |
| 74 | rc = sprintf(buf, "%d\n", dax_region->id); |
| 75 | device_unlock(dev); |
| 76 | |
| 77 | return rc; |
| 78 | } |
| 79 | static DEVICE_ATTR_RO(id); |
| 80 | |
| 81 | static ssize_t region_size_show(struct device *dev, |
| 82 | struct device_attribute *attr, char *buf) |
| 83 | { |
| 84 | struct dax_region *dax_region; |
| 85 | ssize_t rc = -ENXIO; |
| 86 | |
| 87 | device_lock(dev); |
| 88 | dax_region = dev_get_drvdata(dev); |
| 89 | if (dax_region) |
| 90 | rc = sprintf(buf, "%llu\n", (unsigned long long) |
| 91 | resource_size(&dax_region->res)); |
| 92 | device_unlock(dev); |
| 93 | |
| 94 | return rc; |
| 95 | } |
| 96 | static struct device_attribute dev_attr_region_size = __ATTR(size, 0444, |
| 97 | region_size_show, NULL); |
| 98 | |
| 99 | static ssize_t align_show(struct device *dev, |
| 100 | struct device_attribute *attr, char *buf) |
| 101 | { |
| 102 | struct dax_region *dax_region; |
| 103 | ssize_t rc = -ENXIO; |
| 104 | |
| 105 | device_lock(dev); |
| 106 | dax_region = dev_get_drvdata(dev); |
| 107 | if (dax_region) |
| 108 | rc = sprintf(buf, "%u\n", dax_region->align); |
| 109 | device_unlock(dev); |
| 110 | |
| 111 | return rc; |
| 112 | } |
| 113 | static DEVICE_ATTR_RO(align); |
| 114 | |
| 115 | static struct attribute *dax_region_attributes[] = { |
| 116 | &dev_attr_region_size.attr, |
| 117 | &dev_attr_align.attr, |
| 118 | &dev_attr_id.attr, |
| 119 | NULL, |
| 120 | }; |
| 121 | |
| 122 | static const struct attribute_group dax_region_attribute_group = { |
| 123 | .name = "dax_region", |
| 124 | .attrs = dax_region_attributes, |
| 125 | }; |
| 126 | |
| 127 | static const struct attribute_group *dax_region_attribute_groups[] = { |
| 128 | &dax_region_attribute_group, |
| 129 | NULL, |
| 130 | }; |
| 131 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 132 | static void dax_region_free(struct kref *kref) |
| 133 | { |
| 134 | struct dax_region *dax_region; |
| 135 | |
| 136 | dax_region = container_of(kref, struct dax_region, kref); |
| 137 | kfree(dax_region); |
| 138 | } |
| 139 | |
| 140 | void dax_region_put(struct dax_region *dax_region) |
| 141 | { |
| 142 | kref_put(&dax_region->kref, dax_region_free); |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(dax_region_put); |
| 145 | |
Dan Williams | d7fe1a6 | 2016-12-17 14:50:04 -0800 | [diff] [blame] | 146 | static void dax_region_unregister(void *region) |
| 147 | { |
| 148 | struct dax_region *dax_region = region; |
| 149 | |
| 150 | sysfs_remove_groups(&dax_region->dev->kobj, |
| 151 | dax_region_attribute_groups); |
| 152 | dax_region_put(dax_region); |
| 153 | } |
| 154 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 155 | struct dax_region *alloc_dax_region(struct device *parent, int region_id, |
| 156 | struct resource *res, unsigned int align, void *addr, |
| 157 | unsigned long pfn_flags) |
| 158 | { |
| 159 | struct dax_region *dax_region; |
| 160 | |
Dan Williams | d7fe1a6 | 2016-12-17 14:50:04 -0800 | [diff] [blame] | 161 | /* |
| 162 | * The DAX core assumes that it can store its private data in |
| 163 | * parent->driver_data. This WARN is a reminder / safeguard for |
| 164 | * developers of device-dax drivers. |
| 165 | */ |
| 166 | if (dev_get_drvdata(parent)) { |
| 167 | dev_WARN(parent, "dax core failed to setup private data\n"); |
| 168 | return NULL; |
| 169 | } |
| 170 | |
Dan Williams | 9d2d01a | 2016-07-19 16:17:58 -0700 | [diff] [blame] | 171 | if (!IS_ALIGNED(res->start, align) |
| 172 | || !IS_ALIGNED(resource_size(res), align)) |
| 173 | return NULL; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 174 | |
Dan Williams | 9d2d01a | 2016-07-19 16:17:58 -0700 | [diff] [blame] | 175 | dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 176 | if (!dax_region) |
| 177 | return NULL; |
| 178 | |
Dan Williams | d7fe1a6 | 2016-12-17 14:50:04 -0800 | [diff] [blame] | 179 | dev_set_drvdata(parent, dax_region); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 180 | memcpy(&dax_region->res, res, sizeof(*res)); |
| 181 | dax_region->pfn_flags = pfn_flags; |
| 182 | kref_init(&dax_region->kref); |
| 183 | dax_region->id = region_id; |
| 184 | ida_init(&dax_region->ida); |
| 185 | dax_region->align = align; |
| 186 | dax_region->dev = parent; |
| 187 | dax_region->base = addr; |
Dan Williams | d7fe1a6 | 2016-12-17 14:50:04 -0800 | [diff] [blame] | 188 | if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) { |
| 189 | kfree(dax_region); |
| 190 | return NULL;; |
| 191 | } |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 192 | |
Dan Williams | d7fe1a6 | 2016-12-17 14:50:04 -0800 | [diff] [blame] | 193 | kref_get(&dax_region->kref); |
| 194 | if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region)) |
| 195 | return NULL; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 196 | return dax_region; |
| 197 | } |
| 198 | EXPORT_SYMBOL_GPL(alloc_dax_region); |
| 199 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 200 | static struct dev_dax *to_dev_dax(struct device *dev) |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 201 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 202 | return container_of(dev, struct dev_dax, dev); |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 205 | static ssize_t size_show(struct device *dev, |
| 206 | struct device_attribute *attr, char *buf) |
| 207 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 208 | struct dev_dax *dev_dax = to_dev_dax(dev); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 209 | unsigned long long size = 0; |
| 210 | int i; |
| 211 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 212 | for (i = 0; i < dev_dax->num_resources; i++) |
| 213 | size += resource_size(&dev_dax->res[i]); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 214 | |
| 215 | return sprintf(buf, "%llu\n", size); |
| 216 | } |
| 217 | static DEVICE_ATTR_RO(size); |
| 218 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 219 | static struct attribute *dev_dax_attributes[] = { |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 220 | &dev_attr_size.attr, |
| 221 | NULL, |
| 222 | }; |
| 223 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 224 | static const struct attribute_group dev_dax_attribute_group = { |
| 225 | .attrs = dev_dax_attributes, |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | static const struct attribute_group *dax_attribute_groups[] = { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 229 | &dev_dax_attribute_group, |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 230 | NULL, |
| 231 | }; |
| 232 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 233 | static int check_vma(struct dev_dax *dev_dax, struct vm_area_struct *vma, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 234 | const char *func) |
| 235 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 236 | struct dax_region *dax_region = dev_dax->region; |
| 237 | struct device *dev = &dev_dax->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 238 | unsigned long mask; |
| 239 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 240 | if (!dax_alive(dev_dax->dax_dev)) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 241 | return -ENXIO; |
| 242 | |
Dan Williams | 4cb1935 | 2016-11-16 09:00:38 -0800 | [diff] [blame] | 243 | /* prevent private mappings from being established */ |
Dan Williams | 325896f | 2016-12-06 17:03:35 -0800 | [diff] [blame] | 244 | if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) { |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 245 | dev_info(dev, "%s: %s: fail, attempted private mapping\n", |
| 246 | current->comm, func); |
| 247 | return -EINVAL; |
| 248 | } |
| 249 | |
| 250 | mask = dax_region->align - 1; |
| 251 | if (vma->vm_start & mask || vma->vm_end & mask) { |
| 252 | dev_info(dev, "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n", |
| 253 | current->comm, func, vma->vm_start, vma->vm_end, |
| 254 | mask); |
| 255 | return -EINVAL; |
| 256 | } |
| 257 | |
| 258 | if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV |
| 259 | && (vma->vm_flags & VM_DONTCOPY) == 0) { |
| 260 | dev_info(dev, "%s: %s: fail, dax range requires MADV_DONTFORK\n", |
| 261 | current->comm, func); |
| 262 | return -EINVAL; |
| 263 | } |
| 264 | |
| 265 | if (!vma_is_dax(vma)) { |
| 266 | dev_info(dev, "%s: %s: fail, vma is not DAX capable\n", |
| 267 | current->comm, func); |
| 268 | return -EINVAL; |
| 269 | } |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 274 | static phys_addr_t pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 275 | unsigned long size) |
| 276 | { |
| 277 | struct resource *res; |
| 278 | phys_addr_t phys; |
| 279 | int i; |
| 280 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 281 | for (i = 0; i < dev_dax->num_resources; i++) { |
| 282 | res = &dev_dax->res[i]; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 283 | phys = pgoff * PAGE_SIZE + res->start; |
| 284 | if (phys >= res->start && phys <= res->end) |
| 285 | break; |
| 286 | pgoff -= PHYS_PFN(resource_size(res)); |
| 287 | } |
| 288 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 289 | if (i < dev_dax->num_resources) { |
| 290 | res = &dev_dax->res[i]; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 291 | if (phys + size - 1 <= res->end) |
| 292 | return phys; |
| 293 | } |
| 294 | |
| 295 | return -1; |
| 296 | } |
| 297 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 298 | static int __dev_dax_pte_fault(struct dev_dax *dev_dax, struct vm_fault *vmf) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 299 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 300 | struct device *dev = &dev_dax->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 301 | struct dax_region *dax_region; |
| 302 | int rc = VM_FAULT_SIGBUS; |
| 303 | phys_addr_t phys; |
| 304 | pfn_t pfn; |
Dave Jiang | 0134ed4 | 2017-03-10 13:24:22 -0700 | [diff] [blame] | 305 | unsigned int fault_size = PAGE_SIZE; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 306 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 307 | if (check_vma(dev_dax, vmf->vma, __func__)) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 308 | return VM_FAULT_SIGBUS; |
| 309 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 310 | dax_region = dev_dax->region; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 311 | if (dax_region->align > PAGE_SIZE) { |
Oliver O'Halloran | 7620262 | 2017-04-12 01:59:36 +1000 | [diff] [blame] | 312 | dev_dbg(dev, "%s: alignment (%#x) > fault size (%#x)\n", |
| 313 | __func__, dax_region->align, fault_size); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 314 | return VM_FAULT_SIGBUS; |
| 315 | } |
| 316 | |
Dave Jiang | 0134ed4 | 2017-03-10 13:24:22 -0700 | [diff] [blame] | 317 | if (fault_size != dax_region->align) |
| 318 | return VM_FAULT_SIGBUS; |
| 319 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 320 | phys = pgoff_to_phys(dev_dax, vmf->pgoff, PAGE_SIZE); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 321 | if (phys == -1) { |
Dave Jiang | 52084f8 | 2017-03-09 16:56:01 -0700 | [diff] [blame] | 322 | dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 323 | vmf->pgoff); |
| 324 | return VM_FAULT_SIGBUS; |
| 325 | } |
| 326 | |
| 327 | pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
| 328 | |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 329 | rc = vm_insert_mixed(vmf->vma, vmf->address, pfn); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 330 | |
| 331 | if (rc == -ENOMEM) |
| 332 | return VM_FAULT_OOM; |
| 333 | if (rc < 0 && rc != -EBUSY) |
| 334 | return VM_FAULT_SIGBUS; |
| 335 | |
| 336 | return VM_FAULT_NOPAGE; |
| 337 | } |
| 338 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 339 | static int __dev_dax_pmd_fault(struct dev_dax *dev_dax, struct vm_fault *vmf) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 340 | { |
Dave Jiang | d8a849e | 2017-02-22 15:40:03 -0800 | [diff] [blame] | 341 | unsigned long pmd_addr = vmf->address & PMD_MASK; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 342 | struct device *dev = &dev_dax->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 343 | struct dax_region *dax_region; |
| 344 | phys_addr_t phys; |
| 345 | pgoff_t pgoff; |
| 346 | pfn_t pfn; |
Dave Jiang | 0134ed4 | 2017-03-10 13:24:22 -0700 | [diff] [blame] | 347 | unsigned int fault_size = PMD_SIZE; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 348 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 349 | if (check_vma(dev_dax, vmf->vma, __func__)) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 350 | return VM_FAULT_SIGBUS; |
| 351 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 352 | dax_region = dev_dax->region; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 353 | if (dax_region->align > PMD_SIZE) { |
Oliver O'Halloran | 7620262 | 2017-04-12 01:59:36 +1000 | [diff] [blame] | 354 | dev_dbg(dev, "%s: alignment (%#x) > fault size (%#x)\n", |
| 355 | __func__, dax_region->align, fault_size); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 356 | return VM_FAULT_SIGBUS; |
| 357 | } |
| 358 | |
| 359 | /* dax pmd mappings require pfn_t_devmap() */ |
| 360 | if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) { |
Oliver O'Halloran | 7620262 | 2017-04-12 01:59:36 +1000 | [diff] [blame] | 361 | dev_dbg(dev, "%s: region lacks devmap flags\n", __func__); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 362 | return VM_FAULT_SIGBUS; |
| 363 | } |
| 364 | |
Dave Jiang | 0134ed4 | 2017-03-10 13:24:22 -0700 | [diff] [blame] | 365 | if (fault_size < dax_region->align) |
| 366 | return VM_FAULT_SIGBUS; |
| 367 | else if (fault_size > dax_region->align) |
| 368 | return VM_FAULT_FALLBACK; |
| 369 | |
| 370 | /* if we are outside of the VMA */ |
| 371 | if (pmd_addr < vmf->vma->vm_start || |
| 372 | (pmd_addr + PMD_SIZE) > vmf->vma->vm_end) |
| 373 | return VM_FAULT_SIGBUS; |
| 374 | |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 375 | pgoff = linear_page_index(vmf->vma, pmd_addr); |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 376 | phys = pgoff_to_phys(dev_dax, pgoff, PMD_SIZE); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 377 | if (phys == -1) { |
Dave Jiang | 52084f8 | 2017-03-09 16:56:01 -0700 | [diff] [blame] | 378 | dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 379 | pgoff); |
| 380 | return VM_FAULT_SIGBUS; |
| 381 | } |
| 382 | |
| 383 | pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
| 384 | |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 385 | return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd, pfn, |
Dave Jiang | d8a849e | 2017-02-22 15:40:03 -0800 | [diff] [blame] | 386 | vmf->flags & FAULT_FLAG_WRITE); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 389 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 390 | static int __dev_dax_pud_fault(struct dev_dax *dev_dax, struct vm_fault *vmf) |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 391 | { |
| 392 | unsigned long pud_addr = vmf->address & PUD_MASK; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 393 | struct device *dev = &dev_dax->dev; |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 394 | struct dax_region *dax_region; |
| 395 | phys_addr_t phys; |
| 396 | pgoff_t pgoff; |
| 397 | pfn_t pfn; |
Dave Jiang | 70b085b | 2017-03-10 13:24:27 -0700 | [diff] [blame] | 398 | unsigned int fault_size = PUD_SIZE; |
| 399 | |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 400 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 401 | if (check_vma(dev_dax, vmf->vma, __func__)) |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 402 | return VM_FAULT_SIGBUS; |
| 403 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 404 | dax_region = dev_dax->region; |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 405 | if (dax_region->align > PUD_SIZE) { |
Oliver O'Halloran | 7620262 | 2017-04-12 01:59:36 +1000 | [diff] [blame] | 406 | dev_dbg(dev, "%s: alignment (%#x) > fault size (%#x)\n", |
| 407 | __func__, dax_region->align, fault_size); |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 408 | return VM_FAULT_SIGBUS; |
| 409 | } |
| 410 | |
| 411 | /* dax pud mappings require pfn_t_devmap() */ |
| 412 | if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) { |
Oliver O'Halloran | 7620262 | 2017-04-12 01:59:36 +1000 | [diff] [blame] | 413 | dev_dbg(dev, "%s: region lacks devmap flags\n", __func__); |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 414 | return VM_FAULT_SIGBUS; |
| 415 | } |
| 416 | |
Dave Jiang | 70b085b | 2017-03-10 13:24:27 -0700 | [diff] [blame] | 417 | if (fault_size < dax_region->align) |
| 418 | return VM_FAULT_SIGBUS; |
| 419 | else if (fault_size > dax_region->align) |
| 420 | return VM_FAULT_FALLBACK; |
| 421 | |
| 422 | /* if we are outside of the VMA */ |
| 423 | if (pud_addr < vmf->vma->vm_start || |
| 424 | (pud_addr + PUD_SIZE) > vmf->vma->vm_end) |
| 425 | return VM_FAULT_SIGBUS; |
| 426 | |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 427 | pgoff = linear_page_index(vmf->vma, pud_addr); |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 428 | phys = pgoff_to_phys(dev_dax, pgoff, PUD_SIZE); |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 429 | if (phys == -1) { |
Dave Jiang | 52084f8 | 2017-03-09 16:56:01 -0700 | [diff] [blame] | 430 | dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__, |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 431 | pgoff); |
| 432 | return VM_FAULT_SIGBUS; |
| 433 | } |
| 434 | |
| 435 | pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
| 436 | |
| 437 | return vmf_insert_pfn_pud(vmf->vma, vmf->address, vmf->pud, pfn, |
| 438 | vmf->flags & FAULT_FLAG_WRITE); |
| 439 | } |
| 440 | #else |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 441 | static int __dev_dax_pud_fault(struct dev_dax *dev_dax, struct vm_fault *vmf) |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 442 | { |
| 443 | return VM_FAULT_FALLBACK; |
| 444 | } |
| 445 | #endif /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 446 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 447 | static int dev_dax_huge_fault(struct vm_fault *vmf, |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 448 | enum page_entry_size pe_size) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 449 | { |
Dan Williams | 956a4cd | 2017-04-07 16:42:08 -0700 | [diff] [blame] | 450 | int rc, id; |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 451 | struct file *filp = vmf->vma->vm_file; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 452 | struct dev_dax *dev_dax = filp->private_data; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 453 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 454 | dev_dbg(&dev_dax->dev, "%s: %s: %s (%#lx - %#lx) size = %d\n", __func__, |
Dave Jiang | d8a849e | 2017-02-22 15:40:03 -0800 | [diff] [blame] | 455 | current->comm, (vmf->flags & FAULT_FLAG_WRITE) |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 456 | ? "write" : "read", |
Oliver O'Halloran | 7620262 | 2017-04-12 01:59:36 +1000 | [diff] [blame] | 457 | vmf->vma->vm_start, vmf->vma->vm_end, pe_size); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 458 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 459 | id = dax_read_lock(); |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 460 | switch (pe_size) { |
| 461 | case PE_SIZE_PTE: |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 462 | rc = __dev_dax_pte_fault(dev_dax, vmf); |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 463 | break; |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 464 | case PE_SIZE_PMD: |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 465 | rc = __dev_dax_pmd_fault(dev_dax, vmf); |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 466 | break; |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 467 | case PE_SIZE_PUD: |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 468 | rc = __dev_dax_pud_fault(dev_dax, vmf); |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 469 | break; |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 470 | default: |
Pushkar Jambhlekar | 54eafcc | 2017-04-11 09:12:25 -0700 | [diff] [blame] | 471 | rc = VM_FAULT_SIGBUS; |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 472 | } |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 473 | dax_read_unlock(id); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 474 | |
| 475 | return rc; |
| 476 | } |
| 477 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 478 | static int dev_dax_fault(struct vm_fault *vmf) |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 479 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 480 | return dev_dax_huge_fault(vmf, PE_SIZE_PTE); |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 483 | static const struct vm_operations_struct dax_vm_ops = { |
| 484 | .fault = dev_dax_fault, |
| 485 | .huge_fault = dev_dax_huge_fault, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 486 | }; |
| 487 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 488 | static int dax_mmap(struct file *filp, struct vm_area_struct *vma) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 489 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 490 | struct dev_dax *dev_dax = filp->private_data; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 491 | int rc, id; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 492 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 493 | dev_dbg(&dev_dax->dev, "%s\n", __func__); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 494 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 495 | /* |
| 496 | * We lock to check dax_dev liveness and will re-check at |
| 497 | * fault time. |
| 498 | */ |
| 499 | id = dax_read_lock(); |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 500 | rc = check_vma(dev_dax, vma, __func__); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 501 | dax_read_unlock(id); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 502 | if (rc) |
| 503 | return rc; |
| 504 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 505 | vma->vm_ops = &dax_vm_ops; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 506 | vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE; |
| 507 | return 0; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 508 | } |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 509 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 510 | /* return an unmapped area aligned to the dax region specified alignment */ |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 511 | static unsigned long dax_get_unmapped_area(struct file *filp, |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 512 | unsigned long addr, unsigned long len, unsigned long pgoff, |
| 513 | unsigned long flags) |
| 514 | { |
| 515 | unsigned long off, off_end, off_align, len_align, addr_align, align; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 516 | struct dev_dax *dev_dax = filp ? filp->private_data : NULL; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 517 | struct dax_region *dax_region; |
| 518 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 519 | if (!dev_dax || addr) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 520 | goto out; |
| 521 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 522 | dax_region = dev_dax->region; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 523 | align = dax_region->align; |
| 524 | off = pgoff << PAGE_SHIFT; |
| 525 | off_end = off + len; |
| 526 | off_align = round_up(off, align); |
| 527 | |
| 528 | if ((off_end <= off_align) || ((off_end - off_align) < align)) |
| 529 | goto out; |
| 530 | |
| 531 | len_align = len + align; |
| 532 | if ((off + len_align) < off) |
| 533 | goto out; |
| 534 | |
| 535 | addr_align = current->mm->get_unmapped_area(filp, addr, len_align, |
| 536 | pgoff, flags); |
| 537 | if (!IS_ERR_VALUE(addr_align)) { |
| 538 | addr_align += (off - addr_align) & (align - 1); |
| 539 | return addr_align; |
| 540 | } |
| 541 | out: |
| 542 | return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags); |
| 543 | } |
| 544 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 545 | static int dax_open(struct inode *inode, struct file *filp) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 546 | { |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 547 | struct dax_device *dax_dev = inode_dax(inode); |
| 548 | struct inode *__dax_inode = dax_inode(dax_dev); |
| 549 | struct dev_dax *dev_dax = dax_get_private(dax_dev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 550 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 551 | dev_dbg(&dev_dax->dev, "%s\n", __func__); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 552 | inode->i_mapping = __dax_inode->i_mapping; |
| 553 | inode->i_mapping->host = __dax_inode; |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 554 | filp->f_mapping = inode->i_mapping; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 555 | filp->private_data = dev_dax; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 556 | inode->i_flags = S_DAX; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 557 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 558 | return 0; |
| 559 | } |
| 560 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 561 | static int dax_release(struct inode *inode, struct file *filp) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 562 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 563 | struct dev_dax *dev_dax = filp->private_data; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 564 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 565 | dev_dbg(&dev_dax->dev, "%s\n", __func__); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 566 | return 0; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 569 | static const struct file_operations dax_fops = { |
| 570 | .llseek = noop_llseek, |
| 571 | .owner = THIS_MODULE, |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 572 | .open = dax_open, |
| 573 | .release = dax_release, |
| 574 | .get_unmapped_area = dax_get_unmapped_area, |
| 575 | .mmap = dax_mmap, |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 576 | }; |
| 577 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 578 | static void dev_dax_release(struct device *dev) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 579 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 580 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 581 | struct dax_region *dax_region = dev_dax->region; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 582 | struct dax_device *dax_dev = dev_dax->dax_dev; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 583 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 584 | ida_simple_remove(&dax_region->ida, dev_dax->id); |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 585 | dax_region_put(dax_region); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 586 | put_dax(dax_dev); |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 587 | kfree(dev_dax); |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 590 | static void kill_dev_dax(struct dev_dax *dev_dax) |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 591 | { |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 592 | struct dax_device *dax_dev = dev_dax->dax_dev; |
| 593 | struct inode *inode = dax_inode(dax_dev); |
| 594 | |
| 595 | kill_dax(dax_dev); |
| 596 | unmap_mapping_range(inode->i_mapping, 0, 0, 1); |
Dan Williams | ed01e50 | 2017-03-17 12:48:09 -0600 | [diff] [blame] | 597 | } |
| 598 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 599 | static void unregister_dev_dax(void *dev) |
Dan Williams | ed01e50 | 2017-03-17 12:48:09 -0600 | [diff] [blame] | 600 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 601 | struct dev_dax *dev_dax = to_dev_dax(dev); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 602 | struct dax_device *dax_dev = dev_dax->dax_dev; |
| 603 | struct inode *inode = dax_inode(dax_dev); |
| 604 | struct cdev *cdev = inode->i_cdev; |
Dan Williams | ed01e50 | 2017-03-17 12:48:09 -0600 | [diff] [blame] | 605 | |
| 606 | dev_dbg(dev, "%s\n", __func__); |
| 607 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 608 | kill_dev_dax(dev_dax); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 609 | cdev_device_del(cdev, dev); |
Logan Gunthorpe | 92a3fa0 | 2017-03-17 12:48:10 -0600 | [diff] [blame] | 610 | put_device(dev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 613 | struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region, |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 614 | struct resource *res, int count) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 615 | { |
| 616 | struct device *parent = dax_region->dev; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 617 | struct dax_device *dax_dev; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 618 | struct dev_dax *dev_dax; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 619 | struct inode *inode; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 620 | struct device *dev; |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 621 | struct cdev *cdev; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 622 | int rc = 0, i; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 623 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 624 | dev_dax = kzalloc(sizeof(*dev_dax) + sizeof(*res) * count, GFP_KERNEL); |
| 625 | if (!dev_dax) |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 626 | return ERR_PTR(-ENOMEM); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 627 | |
Dan Williams | 9d2d01a | 2016-07-19 16:17:58 -0700 | [diff] [blame] | 628 | for (i = 0; i < count; i++) { |
| 629 | if (!IS_ALIGNED(res[i].start, dax_region->align) |
| 630 | || !IS_ALIGNED(resource_size(&res[i]), |
| 631 | dax_region->align)) { |
| 632 | rc = -EINVAL; |
| 633 | break; |
| 634 | } |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 635 | dev_dax->res[i].start = res[i].start; |
| 636 | dev_dax->res[i].end = res[i].end; |
Dan Williams | 9d2d01a | 2016-07-19 16:17:58 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | if (i < count) |
| 640 | goto err_id; |
| 641 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 642 | dev_dax->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL); |
| 643 | if (dev_dax->id < 0) { |
| 644 | rc = dev_dax->id; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 645 | goto err_id; |
| 646 | } |
| 647 | |
Dan Williams | 7205800 | 2017-04-19 15:14:31 -0700 | [diff] [blame^] | 648 | dax_dev = alloc_dax(dev_dax, NULL); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 649 | if (!dax_dev) |
| 650 | goto err_dax; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 651 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 652 | /* from here on we're committed to teardown via dax_dev_release() */ |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 653 | dev = &dev_dax->dev; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 654 | device_initialize(dev); |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 655 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 656 | inode = dax_inode(dax_dev); |
| 657 | cdev = inode->i_cdev; |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 658 | cdev_init(cdev, &dax_fops); |
| 659 | cdev->owner = parent->driver->owner; |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 660 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 661 | dev_dax->num_resources = count; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 662 | dev_dax->dax_dev = dax_dev; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 663 | dev_dax->region = dax_region; |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 664 | kref_get(&dax_region->kref); |
| 665 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 666 | dev->devt = inode->i_rdev; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 667 | dev->class = dax_class; |
| 668 | dev->parent = parent; |
| 669 | dev->groups = dax_attribute_groups; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 670 | dev->release = dev_dax_release; |
| 671 | dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id); |
Logan Gunthorpe | 92a3fa0 | 2017-03-17 12:48:10 -0600 | [diff] [blame] | 672 | |
| 673 | rc = cdev_device_add(cdev, dev); |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 674 | if (rc) { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 675 | kill_dev_dax(dev_dax); |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 676 | put_device(dev); |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 677 | return ERR_PTR(rc); |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 678 | } |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 679 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 680 | rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev); |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 681 | if (rc) |
| 682 | return ERR_PTR(rc); |
| 683 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 684 | return dev_dax; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 685 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 686 | err_dax: |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 687 | ida_simple_remove(&dax_region->ida, dev_dax->id); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 688 | err_id: |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 689 | kfree(dev_dax); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 690 | |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 691 | return ERR_PTR(rc); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 692 | } |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 693 | EXPORT_SYMBOL_GPL(devm_create_dev_dax); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 694 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 695 | static int __init dax_init(void) |
| 696 | { |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 697 | dax_class = class_create(THIS_MODULE, "dax"); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 698 | return PTR_ERR_OR_ZERO(dax_class); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | static void __exit dax_exit(void) |
| 702 | { |
| 703 | class_destroy(dax_class); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | MODULE_AUTHOR("Intel Corporation"); |
| 707 | MODULE_LICENSE("GPL v2"); |
| 708 | subsys_initcall(dax_init); |
| 709 | module_exit(dax_exit); |