Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright(c) 2016 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 | */ |
| 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/pfn_t.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/dax.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/mm.h> |
Dan Williams | ccdb07f | 2016-08-06 16:05:06 -0700 | [diff] [blame] | 21 | #include "dax.h" |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 22 | |
| 23 | static int dax_major; |
| 24 | static struct class *dax_class; |
| 25 | static DEFINE_IDA(dax_minor_ida); |
| 26 | |
| 27 | /** |
| 28 | * struct dax_region - mapping infrastructure for dax devices |
| 29 | * @id: kernel-wide unique region for a memory range |
| 30 | * @base: linear address corresponding to @res |
| 31 | * @kref: to pin while other agents have a need to do lookups |
| 32 | * @dev: parent device backing this region |
| 33 | * @align: allocation and mapping alignment for child dax devices |
| 34 | * @res: physical address range of the region |
| 35 | * @pfn_flags: identify whether the pfns are paged back or not |
| 36 | */ |
| 37 | struct dax_region { |
| 38 | int id; |
| 39 | struct ida ida; |
| 40 | void *base; |
| 41 | struct kref kref; |
| 42 | struct device *dev; |
| 43 | unsigned int align; |
| 44 | struct resource res; |
| 45 | unsigned long pfn_flags; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * struct dax_dev - subdivision of a dax region |
| 50 | * @region - parent region |
| 51 | * @dev - device backing the character device |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 52 | * @alive - !alive + rcu grace period == no new mappings can be established |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 53 | * @id - child id in the region |
| 54 | * @num_resources - number of physical address extents in this device |
| 55 | * @res - array of physical address ranges |
| 56 | */ |
| 57 | struct dax_dev { |
| 58 | struct dax_region *region; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 59 | struct device dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 60 | bool alive; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 61 | int id; |
| 62 | int num_resources; |
| 63 | struct resource res[0]; |
| 64 | }; |
| 65 | |
| 66 | static void dax_region_free(struct kref *kref) |
| 67 | { |
| 68 | struct dax_region *dax_region; |
| 69 | |
| 70 | dax_region = container_of(kref, struct dax_region, kref); |
| 71 | kfree(dax_region); |
| 72 | } |
| 73 | |
| 74 | void dax_region_put(struct dax_region *dax_region) |
| 75 | { |
| 76 | kref_put(&dax_region->kref, dax_region_free); |
| 77 | } |
| 78 | EXPORT_SYMBOL_GPL(dax_region_put); |
| 79 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 80 | struct dax_region *alloc_dax_region(struct device *parent, int region_id, |
| 81 | struct resource *res, unsigned int align, void *addr, |
| 82 | unsigned long pfn_flags) |
| 83 | { |
| 84 | struct dax_region *dax_region; |
| 85 | |
| 86 | dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL); |
| 87 | |
| 88 | if (!dax_region) |
| 89 | return NULL; |
| 90 | |
| 91 | memcpy(&dax_region->res, res, sizeof(*res)); |
| 92 | dax_region->pfn_flags = pfn_flags; |
| 93 | kref_init(&dax_region->kref); |
| 94 | dax_region->id = region_id; |
| 95 | ida_init(&dax_region->ida); |
| 96 | dax_region->align = align; |
| 97 | dax_region->dev = parent; |
| 98 | dax_region->base = addr; |
| 99 | |
| 100 | return dax_region; |
| 101 | } |
| 102 | EXPORT_SYMBOL_GPL(alloc_dax_region); |
| 103 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 104 | static struct dax_dev *to_dax_dev(struct device *dev) |
| 105 | { |
| 106 | return container_of(dev, struct dax_dev, dev); |
| 107 | } |
| 108 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 109 | static ssize_t size_show(struct device *dev, |
| 110 | struct device_attribute *attr, char *buf) |
| 111 | { |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 112 | struct dax_dev *dax_dev = to_dax_dev(dev); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 113 | unsigned long long size = 0; |
| 114 | int i; |
| 115 | |
| 116 | for (i = 0; i < dax_dev->num_resources; i++) |
| 117 | size += resource_size(&dax_dev->res[i]); |
| 118 | |
| 119 | return sprintf(buf, "%llu\n", size); |
| 120 | } |
| 121 | static DEVICE_ATTR_RO(size); |
| 122 | |
| 123 | static struct attribute *dax_device_attributes[] = { |
| 124 | &dev_attr_size.attr, |
| 125 | NULL, |
| 126 | }; |
| 127 | |
| 128 | static const struct attribute_group dax_device_attribute_group = { |
| 129 | .attrs = dax_device_attributes, |
| 130 | }; |
| 131 | |
| 132 | static const struct attribute_group *dax_attribute_groups[] = { |
| 133 | &dax_device_attribute_group, |
| 134 | NULL, |
| 135 | }; |
| 136 | |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 137 | static int check_vma(struct dax_dev *dax_dev, struct vm_area_struct *vma, |
| 138 | const char *func) |
| 139 | { |
| 140 | struct dax_region *dax_region = dax_dev->region; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 141 | struct device *dev = &dax_dev->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 142 | unsigned long mask; |
| 143 | |
| 144 | if (!dax_dev->alive) |
| 145 | return -ENXIO; |
| 146 | |
| 147 | /* prevent private / writable mappings from being established */ |
| 148 | if ((vma->vm_flags & (VM_NORESERVE|VM_SHARED|VM_WRITE)) == VM_WRITE) { |
| 149 | dev_info(dev, "%s: %s: fail, attempted private mapping\n", |
| 150 | current->comm, func); |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | mask = dax_region->align - 1; |
| 155 | if (vma->vm_start & mask || vma->vm_end & mask) { |
| 156 | dev_info(dev, "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n", |
| 157 | current->comm, func, vma->vm_start, vma->vm_end, |
| 158 | mask); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | |
| 162 | if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV |
| 163 | && (vma->vm_flags & VM_DONTCOPY) == 0) { |
| 164 | dev_info(dev, "%s: %s: fail, dax range requires MADV_DONTFORK\n", |
| 165 | current->comm, func); |
| 166 | return -EINVAL; |
| 167 | } |
| 168 | |
| 169 | if (!vma_is_dax(vma)) { |
| 170 | dev_info(dev, "%s: %s: fail, vma is not DAX capable\n", |
| 171 | current->comm, func); |
| 172 | return -EINVAL; |
| 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static phys_addr_t pgoff_to_phys(struct dax_dev *dax_dev, pgoff_t pgoff, |
| 179 | unsigned long size) |
| 180 | { |
| 181 | struct resource *res; |
| 182 | phys_addr_t phys; |
| 183 | int i; |
| 184 | |
| 185 | for (i = 0; i < dax_dev->num_resources; i++) { |
| 186 | res = &dax_dev->res[i]; |
| 187 | phys = pgoff * PAGE_SIZE + res->start; |
| 188 | if (phys >= res->start && phys <= res->end) |
| 189 | break; |
| 190 | pgoff -= PHYS_PFN(resource_size(res)); |
| 191 | } |
| 192 | |
| 193 | if (i < dax_dev->num_resources) { |
| 194 | res = &dax_dev->res[i]; |
| 195 | if (phys + size - 1 <= res->end) |
| 196 | return phys; |
| 197 | } |
| 198 | |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | static int __dax_dev_fault(struct dax_dev *dax_dev, struct vm_area_struct *vma, |
| 203 | struct vm_fault *vmf) |
| 204 | { |
| 205 | unsigned long vaddr = (unsigned long) vmf->virtual_address; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 206 | struct device *dev = &dax_dev->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 207 | struct dax_region *dax_region; |
| 208 | int rc = VM_FAULT_SIGBUS; |
| 209 | phys_addr_t phys; |
| 210 | pfn_t pfn; |
| 211 | |
| 212 | if (check_vma(dax_dev, vma, __func__)) |
| 213 | return VM_FAULT_SIGBUS; |
| 214 | |
| 215 | dax_region = dax_dev->region; |
| 216 | if (dax_region->align > PAGE_SIZE) { |
| 217 | dev_dbg(dev, "%s: alignment > fault size\n", __func__); |
| 218 | return VM_FAULT_SIGBUS; |
| 219 | } |
| 220 | |
| 221 | phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE); |
| 222 | if (phys == -1) { |
| 223 | dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__, |
| 224 | vmf->pgoff); |
| 225 | return VM_FAULT_SIGBUS; |
| 226 | } |
| 227 | |
| 228 | pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
| 229 | |
| 230 | rc = vm_insert_mixed(vma, vaddr, pfn); |
| 231 | |
| 232 | if (rc == -ENOMEM) |
| 233 | return VM_FAULT_OOM; |
| 234 | if (rc < 0 && rc != -EBUSY) |
| 235 | return VM_FAULT_SIGBUS; |
| 236 | |
| 237 | return VM_FAULT_NOPAGE; |
| 238 | } |
| 239 | |
| 240 | static int dax_dev_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 241 | { |
| 242 | int rc; |
| 243 | struct file *filp = vma->vm_file; |
| 244 | struct dax_dev *dax_dev = filp->private_data; |
| 245 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 246 | dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 247 | current->comm, (vmf->flags & FAULT_FLAG_WRITE) |
| 248 | ? "write" : "read", vma->vm_start, vma->vm_end); |
| 249 | rcu_read_lock(); |
| 250 | rc = __dax_dev_fault(dax_dev, vma, vmf); |
| 251 | rcu_read_unlock(); |
| 252 | |
| 253 | return rc; |
| 254 | } |
| 255 | |
| 256 | static int __dax_dev_pmd_fault(struct dax_dev *dax_dev, |
| 257 | struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd, |
| 258 | unsigned int flags) |
| 259 | { |
| 260 | unsigned long pmd_addr = addr & PMD_MASK; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 261 | struct device *dev = &dax_dev->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 262 | struct dax_region *dax_region; |
| 263 | phys_addr_t phys; |
| 264 | pgoff_t pgoff; |
| 265 | pfn_t pfn; |
| 266 | |
| 267 | if (check_vma(dax_dev, vma, __func__)) |
| 268 | return VM_FAULT_SIGBUS; |
| 269 | |
| 270 | dax_region = dax_dev->region; |
| 271 | if (dax_region->align > PMD_SIZE) { |
| 272 | dev_dbg(dev, "%s: alignment > fault size\n", __func__); |
| 273 | return VM_FAULT_SIGBUS; |
| 274 | } |
| 275 | |
| 276 | /* dax pmd mappings require pfn_t_devmap() */ |
| 277 | if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) { |
| 278 | dev_dbg(dev, "%s: alignment > fault size\n", __func__); |
| 279 | return VM_FAULT_SIGBUS; |
| 280 | } |
| 281 | |
| 282 | pgoff = linear_page_index(vma, pmd_addr); |
| 283 | phys = pgoff_to_phys(dax_dev, pgoff, PAGE_SIZE); |
| 284 | if (phys == -1) { |
| 285 | dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__, |
| 286 | pgoff); |
| 287 | return VM_FAULT_SIGBUS; |
| 288 | } |
| 289 | |
| 290 | pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
| 291 | |
| 292 | return vmf_insert_pfn_pmd(vma, addr, pmd, pfn, |
| 293 | flags & FAULT_FLAG_WRITE); |
| 294 | } |
| 295 | |
| 296 | static int dax_dev_pmd_fault(struct vm_area_struct *vma, unsigned long addr, |
| 297 | pmd_t *pmd, unsigned int flags) |
| 298 | { |
| 299 | int rc; |
| 300 | struct file *filp = vma->vm_file; |
| 301 | struct dax_dev *dax_dev = filp->private_data; |
| 302 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 303 | dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 304 | current->comm, (flags & FAULT_FLAG_WRITE) |
| 305 | ? "write" : "read", vma->vm_start, vma->vm_end); |
| 306 | |
| 307 | rcu_read_lock(); |
| 308 | rc = __dax_dev_pmd_fault(dax_dev, vma, addr, pmd, flags); |
| 309 | rcu_read_unlock(); |
| 310 | |
| 311 | return rc; |
| 312 | } |
| 313 | |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 314 | static const struct vm_operations_struct dax_dev_vm_ops = { |
| 315 | .fault = dax_dev_fault, |
| 316 | .pmd_fault = dax_dev_pmd_fault, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 317 | }; |
| 318 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 319 | static int dax_mmap(struct file *filp, struct vm_area_struct *vma) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 320 | { |
| 321 | struct dax_dev *dax_dev = filp->private_data; |
| 322 | int rc; |
| 323 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 324 | dev_dbg(&dax_dev->dev, "%s\n", __func__); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 325 | |
| 326 | rc = check_vma(dax_dev, vma, __func__); |
| 327 | if (rc) |
| 328 | return rc; |
| 329 | |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 330 | vma->vm_ops = &dax_dev_vm_ops; |
| 331 | vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE; |
| 332 | return 0; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 333 | } |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 334 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 335 | /* return an unmapped area aligned to the dax region specified alignment */ |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 336 | static unsigned long dax_get_unmapped_area(struct file *filp, |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 337 | unsigned long addr, unsigned long len, unsigned long pgoff, |
| 338 | unsigned long flags) |
| 339 | { |
| 340 | unsigned long off, off_end, off_align, len_align, addr_align, align; |
| 341 | struct dax_dev *dax_dev = filp ? filp->private_data : NULL; |
| 342 | struct dax_region *dax_region; |
| 343 | |
| 344 | if (!dax_dev || addr) |
| 345 | goto out; |
| 346 | |
| 347 | dax_region = dax_dev->region; |
| 348 | align = dax_region->align; |
| 349 | off = pgoff << PAGE_SHIFT; |
| 350 | off_end = off + len; |
| 351 | off_align = round_up(off, align); |
| 352 | |
| 353 | if ((off_end <= off_align) || ((off_end - off_align) < align)) |
| 354 | goto out; |
| 355 | |
| 356 | len_align = len + align; |
| 357 | if ((off + len_align) < off) |
| 358 | goto out; |
| 359 | |
| 360 | addr_align = current->mm->get_unmapped_area(filp, addr, len_align, |
| 361 | pgoff, flags); |
| 362 | if (!IS_ERR_VALUE(addr_align)) { |
| 363 | addr_align += (off - addr_align) & (align - 1); |
| 364 | return addr_align; |
| 365 | } |
| 366 | out: |
| 367 | return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags); |
| 368 | } |
| 369 | |
| 370 | static int __match_devt(struct device *dev, const void *data) |
| 371 | { |
| 372 | const dev_t *devt = data; |
| 373 | |
| 374 | return dev->devt == *devt; |
| 375 | } |
| 376 | |
| 377 | static struct device *dax_dev_find(dev_t dev_t) |
| 378 | { |
| 379 | return class_find_device(dax_class, NULL, &dev_t, __match_devt); |
| 380 | } |
| 381 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 382 | static int dax_open(struct inode *inode, struct file *filp) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 383 | { |
| 384 | struct dax_dev *dax_dev = NULL; |
| 385 | struct device *dev; |
| 386 | |
| 387 | dev = dax_dev_find(inode->i_rdev); |
| 388 | if (!dev) |
| 389 | return -ENXIO; |
| 390 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 391 | dax_dev = to_dax_dev(dev); |
| 392 | dev_dbg(dev, "%s\n", __func__); |
| 393 | filp->private_data = dax_dev; |
| 394 | inode->i_flags = S_DAX; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 395 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 399 | static int dax_release(struct inode *inode, struct file *filp) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 400 | { |
| 401 | struct dax_dev *dax_dev = filp->private_data; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 402 | struct device *dev = &dax_dev->dev; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 403 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 404 | dev_dbg(dev, "%s\n", __func__); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 405 | put_device(dev); |
| 406 | |
| 407 | return 0; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 410 | static const struct file_operations dax_fops = { |
| 411 | .llseek = noop_llseek, |
| 412 | .owner = THIS_MODULE, |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 413 | .open = dax_open, |
| 414 | .release = dax_release, |
| 415 | .get_unmapped_area = dax_get_unmapped_area, |
| 416 | .mmap = dax_mmap, |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 417 | }; |
| 418 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 419 | static void dax_dev_release(struct device *dev) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 420 | { |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 421 | struct dax_dev *dax_dev = to_dax_dev(dev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 422 | struct dax_region *dax_region = dax_dev->region; |
| 423 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 424 | ida_simple_remove(&dax_region->ida, dax_dev->id); |
| 425 | ida_simple_remove(&dax_minor_ida, MINOR(dev->devt)); |
| 426 | dax_region_put(dax_region); |
| 427 | kfree(dax_dev); |
| 428 | } |
| 429 | |
| 430 | static void unregister_dax_dev(void *dev) |
| 431 | { |
| 432 | struct dax_dev *dax_dev = to_dax_dev(dev); |
| 433 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 434 | dev_dbg(dev, "%s\n", __func__); |
| 435 | |
| 436 | /* |
| 437 | * Note, rcu is not protecting the liveness of dax_dev, rcu is |
| 438 | * ensuring that any fault handlers that might have seen |
| 439 | * dax_dev->alive == true, have completed. Any fault handlers |
| 440 | * that start after synchronize_rcu() has started will abort |
| 441 | * upon seeing dax_dev->alive == false. |
| 442 | */ |
| 443 | dax_dev->alive = false; |
| 444 | synchronize_rcu(); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 445 | device_unregister(dev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | int devm_create_dax_dev(struct dax_region *dax_region, struct resource *res, |
| 449 | int count) |
| 450 | { |
| 451 | struct device *parent = dax_region->dev; |
| 452 | struct dax_dev *dax_dev; |
| 453 | struct device *dev; |
| 454 | int rc, minor; |
| 455 | dev_t dev_t; |
| 456 | |
| 457 | dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL); |
| 458 | if (!dax_dev) |
| 459 | return -ENOMEM; |
| 460 | memcpy(dax_dev->res, res, sizeof(*res) * count); |
| 461 | dax_dev->num_resources = count; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 462 | dax_dev->alive = true; |
| 463 | dax_dev->region = dax_region; |
| 464 | kref_get(&dax_region->kref); |
| 465 | |
| 466 | dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL); |
| 467 | if (dax_dev->id < 0) { |
| 468 | rc = dax_dev->id; |
| 469 | goto err_id; |
| 470 | } |
| 471 | |
| 472 | minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL); |
| 473 | if (minor < 0) { |
| 474 | rc = minor; |
| 475 | goto err_minor; |
| 476 | } |
| 477 | |
| 478 | dev_t = MKDEV(dax_major, minor); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 479 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 480 | dev = &dax_dev->dev; |
| 481 | device_initialize(dev); |
| 482 | dev->devt = dev_t; |
| 483 | dev->class = dax_class; |
| 484 | dev->parent = parent; |
| 485 | dev->groups = dax_attribute_groups; |
| 486 | dev->release = dax_dev_release; |
| 487 | dev_set_name(dev, "dax%d.%d", dax_region->id, dax_dev->id); |
| 488 | rc = device_add(dev); |
| 489 | if (rc) { |
| 490 | put_device(dev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 491 | return rc; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 492 | } |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 493 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 494 | return devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 495 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 496 | err_minor: |
| 497 | ida_simple_remove(&dax_region->ida, dax_dev->id); |
| 498 | err_id: |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame^] | 499 | kfree(dax_dev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 500 | |
| 501 | return rc; |
| 502 | } |
| 503 | EXPORT_SYMBOL_GPL(devm_create_dax_dev); |
| 504 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 505 | static int __init dax_init(void) |
| 506 | { |
| 507 | int rc; |
| 508 | |
| 509 | rc = register_chrdev(0, "dax", &dax_fops); |
| 510 | if (rc < 0) |
| 511 | return rc; |
| 512 | dax_major = rc; |
| 513 | |
| 514 | dax_class = class_create(THIS_MODULE, "dax"); |
| 515 | if (IS_ERR(dax_class)) { |
| 516 | unregister_chrdev(dax_major, "dax"); |
| 517 | return PTR_ERR(dax_class); |
| 518 | } |
| 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static void __exit dax_exit(void) |
| 524 | { |
| 525 | class_destroy(dax_class); |
| 526 | unregister_chrdev(dax_major, "dax"); |
| 527 | ida_destroy(&dax_minor_ida); |
| 528 | } |
| 529 | |
| 530 | MODULE_AUTHOR("Intel Corporation"); |
| 531 | MODULE_LICENSE("GPL v2"); |
| 532 | subsys_initcall(dax_init); |
| 533 | module_exit(dax_exit); |