| .. | .. |
|---|
| 1 | | -/* |
|---|
| 2 | | - * Copyright(c) 2016 - 2017 Intel Corporation. All rights reserved. |
|---|
| 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 | | - */ |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
|---|
| 2 | +/* Copyright(c) 2016-2018 Intel Corporation. All rights reserved. */ |
|---|
| 3 | +#include <linux/memremap.h> |
|---|
| 13 | 4 | #include <linux/pagemap.h> |
|---|
| 14 | 5 | #include <linux/module.h> |
|---|
| 15 | 6 | #include <linux/device.h> |
|---|
| .. | .. |
|---|
| 21 | 12 | #include <linux/mm.h> |
|---|
| 22 | 13 | #include <linux/mman.h> |
|---|
| 23 | 14 | #include "dax-private.h" |
|---|
| 24 | | -#include "dax.h" |
|---|
| 25 | | - |
|---|
| 26 | | -static struct class *dax_class; |
|---|
| 27 | | - |
|---|
| 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. |
|---|
| 32 | | - */ |
|---|
| 33 | | -static ssize_t id_show(struct device *dev, |
|---|
| 34 | | - struct device_attribute *attr, char *buf) |
|---|
| 35 | | -{ |
|---|
| 36 | | - struct dax_region *dax_region = dev_get_drvdata(dev); |
|---|
| 37 | | - |
|---|
| 38 | | - return sprintf(buf, "%d\n", dax_region->id); |
|---|
| 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 | | -{ |
|---|
| 45 | | - struct dax_region *dax_region = dev_get_drvdata(dev); |
|---|
| 46 | | - |
|---|
| 47 | | - return sprintf(buf, "%llu\n", (unsigned long long) |
|---|
| 48 | | - resource_size(&dax_region->res)); |
|---|
| 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 | | -{ |
|---|
| 56 | | - struct dax_region *dax_region = dev_get_drvdata(dev); |
|---|
| 57 | | - |
|---|
| 58 | | - return sprintf(buf, "%u\n", dax_region->align); |
|---|
| 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 | | - |
|---|
| 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 | | - |
|---|
| 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 | | - |
|---|
| 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 | | - |
|---|
| 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 | | - |
|---|
| 118 | | - if (!IS_ALIGNED(res->start, align) |
|---|
| 119 | | - || !IS_ALIGNED(resource_size(res), align)) |
|---|
| 120 | | - return NULL; |
|---|
| 121 | | - |
|---|
| 122 | | - dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL); |
|---|
| 123 | | - if (!dax_region) |
|---|
| 124 | | - return NULL; |
|---|
| 125 | | - |
|---|
| 126 | | - dev_set_drvdata(parent, dax_region); |
|---|
| 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; |
|---|
| 135 | | - if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) { |
|---|
| 136 | | - kfree(dax_region); |
|---|
| 137 | | - return NULL; |
|---|
| 138 | | - } |
|---|
| 139 | | - |
|---|
| 140 | | - kref_get(&dax_region->kref); |
|---|
| 141 | | - if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region)) |
|---|
| 142 | | - return NULL; |
|---|
| 143 | | - return dax_region; |
|---|
| 144 | | -} |
|---|
| 145 | | -EXPORT_SYMBOL_GPL(alloc_dax_region); |
|---|
| 146 | | - |
|---|
| 147 | | -static struct dev_dax *to_dev_dax(struct device *dev) |
|---|
| 148 | | -{ |
|---|
| 149 | | - return container_of(dev, struct dev_dax, dev); |
|---|
| 150 | | -} |
|---|
| 151 | | - |
|---|
| 152 | | -static ssize_t size_show(struct device *dev, |
|---|
| 153 | | - struct device_attribute *attr, char *buf) |
|---|
| 154 | | -{ |
|---|
| 155 | | - struct dev_dax *dev_dax = to_dev_dax(dev); |
|---|
| 156 | | - unsigned long long size = 0; |
|---|
| 157 | | - int i; |
|---|
| 158 | | - |
|---|
| 159 | | - for (i = 0; i < dev_dax->num_resources; i++) |
|---|
| 160 | | - size += resource_size(&dev_dax->res[i]); |
|---|
| 161 | | - |
|---|
| 162 | | - return sprintf(buf, "%llu\n", size); |
|---|
| 163 | | -} |
|---|
| 164 | | -static DEVICE_ATTR_RO(size); |
|---|
| 165 | | - |
|---|
| 166 | | -static struct attribute *dev_dax_attributes[] = { |
|---|
| 167 | | - &dev_attr_size.attr, |
|---|
| 168 | | - NULL, |
|---|
| 169 | | -}; |
|---|
| 170 | | - |
|---|
| 171 | | -static const struct attribute_group dev_dax_attribute_group = { |
|---|
| 172 | | - .attrs = dev_dax_attributes, |
|---|
| 173 | | -}; |
|---|
| 174 | | - |
|---|
| 175 | | -static const struct attribute_group *dax_attribute_groups[] = { |
|---|
| 176 | | - &dev_dax_attribute_group, |
|---|
| 177 | | - NULL, |
|---|
| 178 | | -}; |
|---|
| 15 | +#include "bus.h" |
|---|
| 179 | 16 | |
|---|
| 180 | 17 | static int check_vma(struct dev_dax *dev_dax, struct vm_area_struct *vma, |
|---|
| 181 | 18 | const char *func) |
|---|
| 182 | 19 | { |
|---|
| 183 | | - struct dax_region *dax_region = dev_dax->region; |
|---|
| 184 | 20 | struct device *dev = &dev_dax->dev; |
|---|
| 185 | 21 | unsigned long mask; |
|---|
| 186 | 22 | |
|---|
| .. | .. |
|---|
| 195 | 31 | return -EINVAL; |
|---|
| 196 | 32 | } |
|---|
| 197 | 33 | |
|---|
| 198 | | - mask = dax_region->align - 1; |
|---|
| 34 | + mask = dev_dax->align - 1; |
|---|
| 199 | 35 | if (vma->vm_start & mask || vma->vm_end & mask) { |
|---|
| 200 | 36 | dev_info_ratelimited(dev, |
|---|
| 201 | 37 | "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n", |
|---|
| 202 | 38 | current->comm, func, vma->vm_start, vma->vm_end, |
|---|
| 203 | 39 | 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) { |
|---|
| 209 | | - dev_info_ratelimited(dev, |
|---|
| 210 | | - "%s: %s: fail, dax range requires MADV_DONTFORK\n", |
|---|
| 211 | | - current->comm, func); |
|---|
| 212 | 40 | return -EINVAL; |
|---|
| 213 | 41 | } |
|---|
| 214 | 42 | |
|---|
| .. | .. |
|---|
| 226 | 54 | __weak phys_addr_t dax_pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff, |
|---|
| 227 | 55 | unsigned long size) |
|---|
| 228 | 56 | { |
|---|
| 229 | | - struct resource *res; |
|---|
| 230 | | - /* gcc-4.6.3-nolibc for i386 complains that this is uninitialized */ |
|---|
| 231 | | - phys_addr_t uninitialized_var(phys); |
|---|
| 232 | 57 | int i; |
|---|
| 233 | 58 | |
|---|
| 234 | | - for (i = 0; i < dev_dax->num_resources; i++) { |
|---|
| 235 | | - res = &dev_dax->res[i]; |
|---|
| 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 | | - } |
|---|
| 59 | + for (i = 0; i < dev_dax->nr_range; i++) { |
|---|
| 60 | + struct dev_dax_range *dax_range = &dev_dax->ranges[i]; |
|---|
| 61 | + struct range *range = &dax_range->range; |
|---|
| 62 | + unsigned long long pgoff_end; |
|---|
| 63 | + phys_addr_t phys; |
|---|
| 241 | 64 | |
|---|
| 242 | | - if (i < dev_dax->num_resources) { |
|---|
| 243 | | - res = &dev_dax->res[i]; |
|---|
| 244 | | - if (phys + size - 1 <= res->end) |
|---|
| 65 | + pgoff_end = dax_range->pgoff + PHYS_PFN(range_len(range)) - 1; |
|---|
| 66 | + if (pgoff < dax_range->pgoff || pgoff > pgoff_end) |
|---|
| 67 | + continue; |
|---|
| 68 | + phys = PFN_PHYS(pgoff - dax_range->pgoff) + range->start; |
|---|
| 69 | + if (phys + size - 1 <= range->end) |
|---|
| 245 | 70 | return phys; |
|---|
| 71 | + break; |
|---|
| 246 | 72 | } |
|---|
| 247 | | - |
|---|
| 248 | 73 | return -1; |
|---|
| 249 | 74 | } |
|---|
| 250 | 75 | |
|---|
| .. | .. |
|---|
| 252 | 77 | struct vm_fault *vmf, pfn_t *pfn) |
|---|
| 253 | 78 | { |
|---|
| 254 | 79 | struct device *dev = &dev_dax->dev; |
|---|
| 255 | | - struct dax_region *dax_region; |
|---|
| 256 | 80 | phys_addr_t phys; |
|---|
| 257 | 81 | unsigned int fault_size = PAGE_SIZE; |
|---|
| 258 | 82 | |
|---|
| 259 | 83 | if (check_vma(dev_dax, vmf->vma, __func__)) |
|---|
| 260 | 84 | return VM_FAULT_SIGBUS; |
|---|
| 261 | 85 | |
|---|
| 262 | | - dax_region = dev_dax->region; |
|---|
| 263 | | - if (dax_region->align > PAGE_SIZE) { |
|---|
| 86 | + if (dev_dax->align > PAGE_SIZE) { |
|---|
| 264 | 87 | dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n", |
|---|
| 265 | | - dax_region->align, fault_size); |
|---|
| 88 | + dev_dax->align, fault_size); |
|---|
| 266 | 89 | return VM_FAULT_SIGBUS; |
|---|
| 267 | 90 | } |
|---|
| 268 | 91 | |
|---|
| 269 | | - if (fault_size != dax_region->align) |
|---|
| 92 | + if (fault_size != dev_dax->align) |
|---|
| 270 | 93 | return VM_FAULT_SIGBUS; |
|---|
| 271 | 94 | |
|---|
| 272 | 95 | phys = dax_pgoff_to_phys(dev_dax, vmf->pgoff, PAGE_SIZE); |
|---|
| .. | .. |
|---|
| 275 | 98 | return VM_FAULT_SIGBUS; |
|---|
| 276 | 99 | } |
|---|
| 277 | 100 | |
|---|
| 278 | | - *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
|---|
| 101 | + *pfn = phys_to_pfn_t(phys, PFN_DEV|PFN_MAP); |
|---|
| 279 | 102 | |
|---|
| 280 | 103 | return vmf_insert_mixed(vmf->vma, vmf->address, *pfn); |
|---|
| 281 | 104 | } |
|---|
| .. | .. |
|---|
| 285 | 108 | { |
|---|
| 286 | 109 | unsigned long pmd_addr = vmf->address & PMD_MASK; |
|---|
| 287 | 110 | struct device *dev = &dev_dax->dev; |
|---|
| 288 | | - struct dax_region *dax_region; |
|---|
| 289 | 111 | phys_addr_t phys; |
|---|
| 290 | 112 | pgoff_t pgoff; |
|---|
| 291 | 113 | unsigned int fault_size = PMD_SIZE; |
|---|
| .. | .. |
|---|
| 293 | 115 | if (check_vma(dev_dax, vmf->vma, __func__)) |
|---|
| 294 | 116 | return VM_FAULT_SIGBUS; |
|---|
| 295 | 117 | |
|---|
| 296 | | - dax_region = dev_dax->region; |
|---|
| 297 | | - if (dax_region->align > PMD_SIZE) { |
|---|
| 118 | + if (dev_dax->align > PMD_SIZE) { |
|---|
| 298 | 119 | dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n", |
|---|
| 299 | | - dax_region->align, fault_size); |
|---|
| 120 | + dev_dax->align, fault_size); |
|---|
| 300 | 121 | return VM_FAULT_SIGBUS; |
|---|
| 301 | 122 | } |
|---|
| 302 | 123 | |
|---|
| 303 | | - /* dax pmd mappings require pfn_t_devmap() */ |
|---|
| 304 | | - if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) { |
|---|
| 305 | | - dev_dbg(dev, "region lacks devmap flags\n"); |
|---|
| 124 | + if (fault_size < dev_dax->align) |
|---|
| 306 | 125 | return VM_FAULT_SIGBUS; |
|---|
| 307 | | - } |
|---|
| 308 | | - |
|---|
| 309 | | - if (fault_size < dax_region->align) |
|---|
| 310 | | - return VM_FAULT_SIGBUS; |
|---|
| 311 | | - else if (fault_size > dax_region->align) |
|---|
| 126 | + else if (fault_size > dev_dax->align) |
|---|
| 312 | 127 | return VM_FAULT_FALLBACK; |
|---|
| 313 | 128 | |
|---|
| 314 | 129 | /* if we are outside of the VMA */ |
|---|
| .. | .. |
|---|
| 323 | 138 | return VM_FAULT_SIGBUS; |
|---|
| 324 | 139 | } |
|---|
| 325 | 140 | |
|---|
| 326 | | - *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
|---|
| 141 | + *pfn = phys_to_pfn_t(phys, PFN_DEV|PFN_MAP); |
|---|
| 327 | 142 | |
|---|
| 328 | 143 | return vmf_insert_pfn_pmd(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE); |
|---|
| 329 | 144 | } |
|---|
| .. | .. |
|---|
| 334 | 149 | { |
|---|
| 335 | 150 | unsigned long pud_addr = vmf->address & PUD_MASK; |
|---|
| 336 | 151 | struct device *dev = &dev_dax->dev; |
|---|
| 337 | | - struct dax_region *dax_region; |
|---|
| 338 | 152 | phys_addr_t phys; |
|---|
| 339 | 153 | pgoff_t pgoff; |
|---|
| 340 | 154 | unsigned int fault_size = PUD_SIZE; |
|---|
| .. | .. |
|---|
| 343 | 157 | if (check_vma(dev_dax, vmf->vma, __func__)) |
|---|
| 344 | 158 | return VM_FAULT_SIGBUS; |
|---|
| 345 | 159 | |
|---|
| 346 | | - dax_region = dev_dax->region; |
|---|
| 347 | | - if (dax_region->align > PUD_SIZE) { |
|---|
| 160 | + if (dev_dax->align > PUD_SIZE) { |
|---|
| 348 | 161 | dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n", |
|---|
| 349 | | - dax_region->align, fault_size); |
|---|
| 162 | + dev_dax->align, fault_size); |
|---|
| 350 | 163 | return VM_FAULT_SIGBUS; |
|---|
| 351 | 164 | } |
|---|
| 352 | 165 | |
|---|
| 353 | | - /* dax pud mappings require pfn_t_devmap() */ |
|---|
| 354 | | - if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) { |
|---|
| 355 | | - dev_dbg(dev, "region lacks devmap flags\n"); |
|---|
| 166 | + if (fault_size < dev_dax->align) |
|---|
| 356 | 167 | return VM_FAULT_SIGBUS; |
|---|
| 357 | | - } |
|---|
| 358 | | - |
|---|
| 359 | | - if (fault_size < dax_region->align) |
|---|
| 360 | | - return VM_FAULT_SIGBUS; |
|---|
| 361 | | - else if (fault_size > dax_region->align) |
|---|
| 168 | + else if (fault_size > dev_dax->align) |
|---|
| 362 | 169 | return VM_FAULT_FALLBACK; |
|---|
| 363 | 170 | |
|---|
| 364 | 171 | /* if we are outside of the VMA */ |
|---|
| .. | .. |
|---|
| 373 | 180 | return VM_FAULT_SIGBUS; |
|---|
| 374 | 181 | } |
|---|
| 375 | 182 | |
|---|
| 376 | | - *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
|---|
| 183 | + *pfn = phys_to_pfn_t(phys, PFN_DEV|PFN_MAP); |
|---|
| 377 | 184 | |
|---|
| 378 | 185 | return vmf_insert_pfn_pud(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE); |
|---|
| 379 | 186 | } |
|---|
| .. | .. |
|---|
| 453 | 260 | { |
|---|
| 454 | 261 | struct file *filp = vma->vm_file; |
|---|
| 455 | 262 | struct dev_dax *dev_dax = filp->private_data; |
|---|
| 456 | | - struct dax_region *dax_region = dev_dax->region; |
|---|
| 457 | 263 | |
|---|
| 458 | | - if (!IS_ALIGNED(addr, dax_region->align)) |
|---|
| 264 | + if (!IS_ALIGNED(addr, dev_dax->align)) |
|---|
| 459 | 265 | return -EINVAL; |
|---|
| 460 | 266 | return 0; |
|---|
| 461 | 267 | } |
|---|
| .. | .. |
|---|
| 464 | 270 | { |
|---|
| 465 | 271 | struct file *filp = vma->vm_file; |
|---|
| 466 | 272 | struct dev_dax *dev_dax = filp->private_data; |
|---|
| 467 | | - struct dax_region *dax_region = dev_dax->region; |
|---|
| 468 | 273 | |
|---|
| 469 | | - return dax_region->align; |
|---|
| 274 | + return dev_dax->align; |
|---|
| 470 | 275 | } |
|---|
| 471 | 276 | |
|---|
| 472 | 277 | static const struct vm_operations_struct dax_vm_ops = { |
|---|
| .. | .. |
|---|
| 505 | 310 | { |
|---|
| 506 | 311 | unsigned long off, off_end, off_align, len_align, addr_align, align; |
|---|
| 507 | 312 | struct dev_dax *dev_dax = filp ? filp->private_data : NULL; |
|---|
| 508 | | - struct dax_region *dax_region; |
|---|
| 509 | 313 | |
|---|
| 510 | 314 | if (!dev_dax || addr) |
|---|
| 511 | 315 | goto out; |
|---|
| 512 | 316 | |
|---|
| 513 | | - dax_region = dev_dax->region; |
|---|
| 514 | | - align = dax_region->align; |
|---|
| 317 | + align = dev_dax->align; |
|---|
| 515 | 318 | off = pgoff << PAGE_SHIFT; |
|---|
| 516 | 319 | off_end = off + len; |
|---|
| 517 | 320 | off_align = round_up(off, align); |
|---|
| .. | .. |
|---|
| 550 | 353 | inode->i_mapping->a_ops = &dev_dax_aops; |
|---|
| 551 | 354 | filp->f_mapping = inode->i_mapping; |
|---|
| 552 | 355 | filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping); |
|---|
| 356 | + filp->f_sb_err = file_sample_sb_err(filp); |
|---|
| 553 | 357 | filp->private_data = dev_dax; |
|---|
| 554 | 358 | inode->i_flags = S_DAX; |
|---|
| 555 | 359 | |
|---|
| .. | .. |
|---|
| 574 | 378 | .mmap_supported_flags = MAP_SYNC, |
|---|
| 575 | 379 | }; |
|---|
| 576 | 380 | |
|---|
| 577 | | -static void dev_dax_release(struct device *dev) |
|---|
| 381 | +static void dev_dax_cdev_del(void *cdev) |
|---|
| 578 | 382 | { |
|---|
| 579 | | - struct dev_dax *dev_dax = to_dev_dax(dev); |
|---|
| 580 | | - struct dax_region *dax_region = dev_dax->region; |
|---|
| 581 | | - struct dax_device *dax_dev = dev_dax->dax_dev; |
|---|
| 582 | | - |
|---|
| 583 | | - if (dev_dax->id >= 0) |
|---|
| 584 | | - ida_simple_remove(&dax_region->ida, dev_dax->id); |
|---|
| 585 | | - dax_region_put(dax_region); |
|---|
| 586 | | - put_dax(dax_dev); |
|---|
| 587 | | - kfree(dev_dax); |
|---|
| 383 | + cdev_del(cdev); |
|---|
| 588 | 384 | } |
|---|
| 589 | 385 | |
|---|
| 590 | | -static void kill_dev_dax(struct dev_dax *dev_dax) |
|---|
| 386 | +static void dev_dax_kill(void *dev_dax) |
|---|
| 591 | 387 | { |
|---|
| 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); |
|---|
| 597 | | -} |
|---|
| 598 | | - |
|---|
| 599 | | -static void unregister_dev_dax(void *dev) |
|---|
| 600 | | -{ |
|---|
| 601 | | - struct dev_dax *dev_dax = to_dev_dax(dev); |
|---|
| 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; |
|---|
| 605 | | - |
|---|
| 606 | | - dev_dbg(dev, "trace\n"); |
|---|
| 607 | | - |
|---|
| 608 | 388 | kill_dev_dax(dev_dax); |
|---|
| 609 | | - cdev_device_del(cdev, dev); |
|---|
| 610 | | - put_device(dev); |
|---|
| 611 | 389 | } |
|---|
| 612 | 390 | |
|---|
| 613 | | -struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region, |
|---|
| 614 | | - int id, struct resource *res, int count) |
|---|
| 391 | +int dev_dax_probe(struct dev_dax *dev_dax) |
|---|
| 615 | 392 | { |
|---|
| 616 | | - struct device *parent = dax_region->dev; |
|---|
| 617 | | - struct dax_device *dax_dev; |
|---|
| 618 | | - struct dev_dax *dev_dax; |
|---|
| 393 | + struct dax_device *dax_dev = dev_dax->dax_dev; |
|---|
| 394 | + struct device *dev = &dev_dax->dev; |
|---|
| 395 | + struct dev_pagemap *pgmap; |
|---|
| 619 | 396 | struct inode *inode; |
|---|
| 620 | | - struct device *dev; |
|---|
| 621 | 397 | struct cdev *cdev; |
|---|
| 398 | + void *addr; |
|---|
| 622 | 399 | int rc, i; |
|---|
| 623 | 400 | |
|---|
| 624 | | - if (!count) |
|---|
| 625 | | - return ERR_PTR(-EINVAL); |
|---|
| 401 | + pgmap = dev_dax->pgmap; |
|---|
| 402 | + if (dev_WARN_ONCE(dev, pgmap && dev_dax->nr_range > 1, |
|---|
| 403 | + "static pgmap / multi-range device conflict\n")) |
|---|
| 404 | + return -EINVAL; |
|---|
| 626 | 405 | |
|---|
| 627 | | - dev_dax = kzalloc(struct_size(dev_dax, res, count), GFP_KERNEL); |
|---|
| 628 | | - if (!dev_dax) |
|---|
| 629 | | - return ERR_PTR(-ENOMEM); |
|---|
| 406 | + if (!pgmap) { |
|---|
| 407 | + pgmap = devm_kzalloc(dev, sizeof(*pgmap) + sizeof(struct range) |
|---|
| 408 | + * (dev_dax->nr_range - 1), GFP_KERNEL); |
|---|
| 409 | + if (!pgmap) |
|---|
| 410 | + return -ENOMEM; |
|---|
| 411 | + pgmap->nr_range = dev_dax->nr_range; |
|---|
| 412 | + } |
|---|
| 630 | 413 | |
|---|
| 631 | | - for (i = 0; i < count; i++) { |
|---|
| 632 | | - if (!IS_ALIGNED(res[i].start, dax_region->align) |
|---|
| 633 | | - || !IS_ALIGNED(resource_size(&res[i]), |
|---|
| 634 | | - dax_region->align)) { |
|---|
| 635 | | - rc = -EINVAL; |
|---|
| 636 | | - break; |
|---|
| 414 | + for (i = 0; i < dev_dax->nr_range; i++) { |
|---|
| 415 | + struct range *range = &dev_dax->ranges[i].range; |
|---|
| 416 | + |
|---|
| 417 | + if (!devm_request_mem_region(dev, range->start, |
|---|
| 418 | + range_len(range), dev_name(dev))) { |
|---|
| 419 | + dev_warn(dev, "mapping%d: %#llx-%#llx could not reserve range\n", |
|---|
| 420 | + i, range->start, range->end); |
|---|
| 421 | + return -EBUSY; |
|---|
| 637 | 422 | } |
|---|
| 638 | | - dev_dax->res[i].start = res[i].start; |
|---|
| 639 | | - dev_dax->res[i].end = res[i].end; |
|---|
| 423 | + /* don't update the range for static pgmap */ |
|---|
| 424 | + if (!dev_dax->pgmap) |
|---|
| 425 | + pgmap->ranges[i] = *range; |
|---|
| 640 | 426 | } |
|---|
| 641 | 427 | |
|---|
| 642 | | - if (i < count) |
|---|
| 643 | | - goto err_id; |
|---|
| 644 | | - |
|---|
| 645 | | - if (id < 0) { |
|---|
| 646 | | - id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL); |
|---|
| 647 | | - dev_dax->id = id; |
|---|
| 648 | | - if (id < 0) { |
|---|
| 649 | | - rc = id; |
|---|
| 650 | | - goto err_id; |
|---|
| 651 | | - } |
|---|
| 652 | | - } else { |
|---|
| 653 | | - /* region provider owns @id lifetime */ |
|---|
| 654 | | - dev_dax->id = -1; |
|---|
| 655 | | - } |
|---|
| 656 | | - |
|---|
| 657 | | - /* |
|---|
| 658 | | - * No 'host' or dax_operations since there is no access to this |
|---|
| 659 | | - * device outside of mmap of the resulting character device. |
|---|
| 660 | | - */ |
|---|
| 661 | | - dax_dev = alloc_dax(dev_dax, NULL, NULL); |
|---|
| 662 | | - if (!dax_dev) { |
|---|
| 663 | | - rc = -ENOMEM; |
|---|
| 664 | | - goto err_dax; |
|---|
| 665 | | - } |
|---|
| 666 | | - |
|---|
| 667 | | - /* from here on we're committed to teardown via dax_dev_release() */ |
|---|
| 668 | | - dev = &dev_dax->dev; |
|---|
| 669 | | - device_initialize(dev); |
|---|
| 428 | + pgmap->type = MEMORY_DEVICE_GENERIC; |
|---|
| 429 | + addr = devm_memremap_pages(dev, pgmap); |
|---|
| 430 | + if (IS_ERR(addr)) |
|---|
| 431 | + return PTR_ERR(addr); |
|---|
| 670 | 432 | |
|---|
| 671 | 433 | inode = dax_inode(dax_dev); |
|---|
| 672 | 434 | cdev = inode->i_cdev; |
|---|
| 673 | 435 | cdev_init(cdev, &dax_fops); |
|---|
| 674 | | - cdev->owner = parent->driver->owner; |
|---|
| 675 | | - |
|---|
| 676 | | - dev_dax->num_resources = count; |
|---|
| 677 | | - dev_dax->dax_dev = dax_dev; |
|---|
| 678 | | - dev_dax->region = dax_region; |
|---|
| 679 | | - kref_get(&dax_region->kref); |
|---|
| 680 | | - |
|---|
| 681 | | - dev->devt = inode->i_rdev; |
|---|
| 682 | | - dev->class = dax_class; |
|---|
| 683 | | - dev->parent = parent; |
|---|
| 684 | | - dev->groups = dax_attribute_groups; |
|---|
| 685 | | - dev->release = dev_dax_release; |
|---|
| 686 | | - dev_set_name(dev, "dax%d.%d", dax_region->id, id); |
|---|
| 687 | | - |
|---|
| 688 | | - rc = cdev_device_add(cdev, dev); |
|---|
| 689 | | - if (rc) { |
|---|
| 690 | | - kill_dev_dax(dev_dax); |
|---|
| 691 | | - put_device(dev); |
|---|
| 692 | | - return ERR_PTR(rc); |
|---|
| 693 | | - } |
|---|
| 694 | | - |
|---|
| 695 | | - rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev); |
|---|
| 436 | + if (dev->class) { |
|---|
| 437 | + /* for the CONFIG_DEV_DAX_PMEM_COMPAT case */ |
|---|
| 438 | + cdev->owner = dev->parent->driver->owner; |
|---|
| 439 | + } else |
|---|
| 440 | + cdev->owner = dev->driver->owner; |
|---|
| 441 | + cdev_set_parent(cdev, &dev->kobj); |
|---|
| 442 | + rc = cdev_add(cdev, dev->devt, 1); |
|---|
| 696 | 443 | if (rc) |
|---|
| 697 | | - return ERR_PTR(rc); |
|---|
| 444 | + return rc; |
|---|
| 698 | 445 | |
|---|
| 699 | | - return dev_dax; |
|---|
| 446 | + rc = devm_add_action_or_reset(dev, dev_dax_cdev_del, cdev); |
|---|
| 447 | + if (rc) |
|---|
| 448 | + return rc; |
|---|
| 700 | 449 | |
|---|
| 701 | | - err_dax: |
|---|
| 702 | | - if (dev_dax->id >= 0) |
|---|
| 703 | | - ida_simple_remove(&dax_region->ida, dev_dax->id); |
|---|
| 704 | | - err_id: |
|---|
| 705 | | - kfree(dev_dax); |
|---|
| 706 | | - |
|---|
| 707 | | - return ERR_PTR(rc); |
|---|
| 450 | + run_dax(dax_dev); |
|---|
| 451 | + return devm_add_action_or_reset(dev, dev_dax_kill, dev_dax); |
|---|
| 708 | 452 | } |
|---|
| 709 | | -EXPORT_SYMBOL_GPL(devm_create_dev_dax); |
|---|
| 453 | +EXPORT_SYMBOL_GPL(dev_dax_probe); |
|---|
| 454 | + |
|---|
| 455 | +static int dev_dax_remove(struct dev_dax *dev_dax) |
|---|
| 456 | +{ |
|---|
| 457 | + /* all probe actions are unwound by devm */ |
|---|
| 458 | + return 0; |
|---|
| 459 | +} |
|---|
| 460 | + |
|---|
| 461 | +static struct dax_device_driver device_dax_driver = { |
|---|
| 462 | + .probe = dev_dax_probe, |
|---|
| 463 | + .remove = dev_dax_remove, |
|---|
| 464 | + .match_always = 1, |
|---|
| 465 | +}; |
|---|
| 710 | 466 | |
|---|
| 711 | 467 | static int __init dax_init(void) |
|---|
| 712 | 468 | { |
|---|
| 713 | | - dax_class = class_create(THIS_MODULE, "dax"); |
|---|
| 714 | | - return PTR_ERR_OR_ZERO(dax_class); |
|---|
| 469 | + return dax_driver_register(&device_dax_driver); |
|---|
| 715 | 470 | } |
|---|
| 716 | 471 | |
|---|
| 717 | 472 | static void __exit dax_exit(void) |
|---|
| 718 | 473 | { |
|---|
| 719 | | - class_destroy(dax_class); |
|---|
| 474 | + dax_driver_unregister(&device_dax_driver); |
|---|
| 720 | 475 | } |
|---|
| 721 | 476 | |
|---|
| 722 | 477 | MODULE_AUTHOR("Intel Corporation"); |
|---|
| 723 | 478 | MODULE_LICENSE("GPL v2"); |
|---|
| 724 | | -subsys_initcall(dax_init); |
|---|
| 479 | +module_init(dax_init); |
|---|
| 725 | 480 | module_exit(dax_exit); |
|---|
| 481 | +MODULE_ALIAS_DAX_DEVICE(0); |
|---|