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