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> |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 16 | #include <linux/mount.h> |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 17 | #include <linux/pfn_t.h> |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 18 | #include <linux/hash.h> |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 19 | #include <linux/cdev.h> |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/dax.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/mm.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 | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 26 | static dev_t dax_devt; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 27 | static struct class *dax_class; |
| 28 | static DEFINE_IDA(dax_minor_ida); |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 29 | static int nr_dax = CONFIG_NR_DEV_DAX; |
| 30 | module_param(nr_dax, int, S_IRUGO); |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 31 | static struct vfsmount *dax_mnt; |
| 32 | static struct kmem_cache *dax_cache __read_mostly; |
| 33 | static struct super_block *dax_superblock __read_mostly; |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 34 | MODULE_PARM_DESC(nr_dax, "max number of device-dax instances"); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * struct dax_region - mapping infrastructure for dax devices |
| 38 | * @id: kernel-wide unique region for a memory range |
| 39 | * @base: linear address corresponding to @res |
| 40 | * @kref: to pin while other agents have a need to do lookups |
| 41 | * @dev: parent device backing this region |
| 42 | * @align: allocation and mapping alignment for child dax devices |
| 43 | * @res: physical address range of the region |
| 44 | * @pfn_flags: identify whether the pfns are paged back or not |
| 45 | */ |
| 46 | struct dax_region { |
| 47 | int id; |
| 48 | struct ida ida; |
| 49 | void *base; |
| 50 | struct kref kref; |
| 51 | struct device *dev; |
| 52 | unsigned int align; |
| 53 | struct resource res; |
| 54 | unsigned long pfn_flags; |
| 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * struct dax_dev - subdivision of a dax region |
| 59 | * @region - parent region |
| 60 | * @dev - device backing the character device |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 61 | * @cdev - core chardev data |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 62 | * @alive - !alive + rcu grace period == no new mappings can be established |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 63 | * @id - child id in the region |
| 64 | * @num_resources - number of physical address extents in this device |
| 65 | * @res - array of physical address ranges |
| 66 | */ |
| 67 | struct dax_dev { |
| 68 | struct dax_region *region; |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 69 | struct inode *inode; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 70 | struct device dev; |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 71 | struct cdev cdev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 72 | bool alive; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 73 | int id; |
| 74 | int num_resources; |
| 75 | struct resource res[0]; |
| 76 | }; |
| 77 | |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 78 | static struct inode *dax_alloc_inode(struct super_block *sb) |
| 79 | { |
| 80 | return kmem_cache_alloc(dax_cache, GFP_KERNEL); |
| 81 | } |
| 82 | |
| 83 | static void dax_i_callback(struct rcu_head *head) |
| 84 | { |
| 85 | struct inode *inode = container_of(head, struct inode, i_rcu); |
| 86 | |
| 87 | kmem_cache_free(dax_cache, inode); |
| 88 | } |
| 89 | |
| 90 | static void dax_destroy_inode(struct inode *inode) |
| 91 | { |
| 92 | call_rcu(&inode->i_rcu, dax_i_callback); |
| 93 | } |
| 94 | |
| 95 | static const struct super_operations dax_sops = { |
| 96 | .statfs = simple_statfs, |
| 97 | .alloc_inode = dax_alloc_inode, |
| 98 | .destroy_inode = dax_destroy_inode, |
| 99 | .drop_inode = generic_delete_inode, |
| 100 | }; |
| 101 | |
| 102 | static struct dentry *dax_mount(struct file_system_type *fs_type, |
| 103 | int flags, const char *dev_name, void *data) |
| 104 | { |
| 105 | return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC); |
| 106 | } |
| 107 | |
| 108 | static struct file_system_type dax_type = { |
| 109 | .name = "dax", |
| 110 | .mount = dax_mount, |
| 111 | .kill_sb = kill_anon_super, |
| 112 | }; |
| 113 | |
| 114 | static int dax_test(struct inode *inode, void *data) |
| 115 | { |
| 116 | return inode->i_cdev == data; |
| 117 | } |
| 118 | |
| 119 | static int dax_set(struct inode *inode, void *data) |
| 120 | { |
| 121 | inode->i_cdev = data; |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static struct inode *dax_inode_get(struct cdev *cdev, dev_t devt) |
| 126 | { |
| 127 | struct inode *inode; |
| 128 | |
| 129 | inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31), |
| 130 | dax_test, dax_set, cdev); |
| 131 | |
| 132 | if (!inode) |
| 133 | return NULL; |
| 134 | |
| 135 | if (inode->i_state & I_NEW) { |
| 136 | inode->i_mode = S_IFCHR; |
| 137 | inode->i_flags = S_DAX; |
| 138 | inode->i_rdev = devt; |
| 139 | mapping_set_gfp_mask(&inode->i_data, GFP_USER); |
| 140 | unlock_new_inode(inode); |
| 141 | } |
| 142 | return inode; |
| 143 | } |
| 144 | |
| 145 | static void init_once(void *inode) |
| 146 | { |
| 147 | inode_init_once(inode); |
| 148 | } |
| 149 | |
| 150 | static int dax_inode_init(void) |
| 151 | { |
| 152 | int rc; |
| 153 | |
| 154 | dax_cache = kmem_cache_create("dax_cache", sizeof(struct inode), 0, |
| 155 | (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT| |
| 156 | SLAB_MEM_SPREAD|SLAB_ACCOUNT), |
| 157 | init_once); |
| 158 | if (!dax_cache) |
| 159 | return -ENOMEM; |
| 160 | |
| 161 | rc = register_filesystem(&dax_type); |
| 162 | if (rc) |
| 163 | goto err_register_fs; |
| 164 | |
| 165 | dax_mnt = kern_mount(&dax_type); |
| 166 | if (IS_ERR(dax_mnt)) { |
| 167 | rc = PTR_ERR(dax_mnt); |
| 168 | goto err_mount; |
| 169 | } |
| 170 | dax_superblock = dax_mnt->mnt_sb; |
| 171 | |
| 172 | return 0; |
| 173 | |
| 174 | err_mount: |
| 175 | unregister_filesystem(&dax_type); |
| 176 | err_register_fs: |
| 177 | kmem_cache_destroy(dax_cache); |
| 178 | |
| 179 | return rc; |
| 180 | } |
| 181 | |
| 182 | static void dax_inode_exit(void) |
| 183 | { |
| 184 | kern_unmount(dax_mnt); |
| 185 | unregister_filesystem(&dax_type); |
| 186 | kmem_cache_destroy(dax_cache); |
| 187 | } |
| 188 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 189 | static void dax_region_free(struct kref *kref) |
| 190 | { |
| 191 | struct dax_region *dax_region; |
| 192 | |
| 193 | dax_region = container_of(kref, struct dax_region, kref); |
| 194 | kfree(dax_region); |
| 195 | } |
| 196 | |
| 197 | void dax_region_put(struct dax_region *dax_region) |
| 198 | { |
| 199 | kref_put(&dax_region->kref, dax_region_free); |
| 200 | } |
| 201 | EXPORT_SYMBOL_GPL(dax_region_put); |
| 202 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 203 | struct dax_region *alloc_dax_region(struct device *parent, int region_id, |
| 204 | struct resource *res, unsigned int align, void *addr, |
| 205 | unsigned long pfn_flags) |
| 206 | { |
| 207 | struct dax_region *dax_region; |
| 208 | |
Dan Williams | 9d2d01a | 2016-07-19 16:17:58 -0700 | [diff] [blame] | 209 | if (!IS_ALIGNED(res->start, align) |
| 210 | || !IS_ALIGNED(resource_size(res), align)) |
| 211 | return NULL; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 212 | |
Dan Williams | 9d2d01a | 2016-07-19 16:17:58 -0700 | [diff] [blame] | 213 | dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 214 | if (!dax_region) |
| 215 | return NULL; |
| 216 | |
| 217 | memcpy(&dax_region->res, res, sizeof(*res)); |
| 218 | dax_region->pfn_flags = pfn_flags; |
| 219 | kref_init(&dax_region->kref); |
| 220 | dax_region->id = region_id; |
| 221 | ida_init(&dax_region->ida); |
| 222 | dax_region->align = align; |
| 223 | dax_region->dev = parent; |
| 224 | dax_region->base = addr; |
| 225 | |
| 226 | return dax_region; |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(alloc_dax_region); |
| 229 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 230 | static struct dax_dev *to_dax_dev(struct device *dev) |
| 231 | { |
| 232 | return container_of(dev, struct dax_dev, dev); |
| 233 | } |
| 234 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 235 | static ssize_t size_show(struct device *dev, |
| 236 | struct device_attribute *attr, char *buf) |
| 237 | { |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 238 | struct dax_dev *dax_dev = to_dax_dev(dev); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 239 | unsigned long long size = 0; |
| 240 | int i; |
| 241 | |
| 242 | for (i = 0; i < dax_dev->num_resources; i++) |
| 243 | size += resource_size(&dax_dev->res[i]); |
| 244 | |
| 245 | return sprintf(buf, "%llu\n", size); |
| 246 | } |
| 247 | static DEVICE_ATTR_RO(size); |
| 248 | |
| 249 | static struct attribute *dax_device_attributes[] = { |
| 250 | &dev_attr_size.attr, |
| 251 | NULL, |
| 252 | }; |
| 253 | |
| 254 | static const struct attribute_group dax_device_attribute_group = { |
| 255 | .attrs = dax_device_attributes, |
| 256 | }; |
| 257 | |
| 258 | static const struct attribute_group *dax_attribute_groups[] = { |
| 259 | &dax_device_attribute_group, |
| 260 | NULL, |
| 261 | }; |
| 262 | |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 263 | static int check_vma(struct dax_dev *dax_dev, struct vm_area_struct *vma, |
| 264 | const char *func) |
| 265 | { |
| 266 | struct dax_region *dax_region = dax_dev->region; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 267 | struct device *dev = &dax_dev->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 268 | unsigned long mask; |
| 269 | |
| 270 | if (!dax_dev->alive) |
| 271 | return -ENXIO; |
| 272 | |
| 273 | /* prevent private / writable mappings from being established */ |
| 274 | if ((vma->vm_flags & (VM_NORESERVE|VM_SHARED|VM_WRITE)) == VM_WRITE) { |
| 275 | dev_info(dev, "%s: %s: fail, attempted private mapping\n", |
| 276 | current->comm, func); |
| 277 | return -EINVAL; |
| 278 | } |
| 279 | |
| 280 | mask = dax_region->align - 1; |
| 281 | if (vma->vm_start & mask || vma->vm_end & mask) { |
| 282 | dev_info(dev, "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n", |
| 283 | current->comm, func, vma->vm_start, vma->vm_end, |
| 284 | mask); |
| 285 | return -EINVAL; |
| 286 | } |
| 287 | |
| 288 | if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV |
| 289 | && (vma->vm_flags & VM_DONTCOPY) == 0) { |
| 290 | dev_info(dev, "%s: %s: fail, dax range requires MADV_DONTFORK\n", |
| 291 | current->comm, func); |
| 292 | return -EINVAL; |
| 293 | } |
| 294 | |
| 295 | if (!vma_is_dax(vma)) { |
| 296 | dev_info(dev, "%s: %s: fail, vma is not DAX capable\n", |
| 297 | current->comm, func); |
| 298 | return -EINVAL; |
| 299 | } |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static phys_addr_t pgoff_to_phys(struct dax_dev *dax_dev, pgoff_t pgoff, |
| 305 | unsigned long size) |
| 306 | { |
| 307 | struct resource *res; |
| 308 | phys_addr_t phys; |
| 309 | int i; |
| 310 | |
| 311 | for (i = 0; i < dax_dev->num_resources; i++) { |
| 312 | res = &dax_dev->res[i]; |
| 313 | phys = pgoff * PAGE_SIZE + res->start; |
| 314 | if (phys >= res->start && phys <= res->end) |
| 315 | break; |
| 316 | pgoff -= PHYS_PFN(resource_size(res)); |
| 317 | } |
| 318 | |
| 319 | if (i < dax_dev->num_resources) { |
| 320 | res = &dax_dev->res[i]; |
| 321 | if (phys + size - 1 <= res->end) |
| 322 | return phys; |
| 323 | } |
| 324 | |
| 325 | return -1; |
| 326 | } |
| 327 | |
| 328 | static int __dax_dev_fault(struct dax_dev *dax_dev, struct vm_area_struct *vma, |
| 329 | struct vm_fault *vmf) |
| 330 | { |
| 331 | unsigned long vaddr = (unsigned long) vmf->virtual_address; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 332 | struct device *dev = &dax_dev->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 333 | struct dax_region *dax_region; |
| 334 | int rc = VM_FAULT_SIGBUS; |
| 335 | phys_addr_t phys; |
| 336 | pfn_t pfn; |
| 337 | |
| 338 | if (check_vma(dax_dev, vma, __func__)) |
| 339 | return VM_FAULT_SIGBUS; |
| 340 | |
| 341 | dax_region = dax_dev->region; |
| 342 | if (dax_region->align > PAGE_SIZE) { |
| 343 | dev_dbg(dev, "%s: alignment > fault size\n", __func__); |
| 344 | return VM_FAULT_SIGBUS; |
| 345 | } |
| 346 | |
| 347 | phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE); |
| 348 | if (phys == -1) { |
| 349 | dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__, |
| 350 | vmf->pgoff); |
| 351 | return VM_FAULT_SIGBUS; |
| 352 | } |
| 353 | |
| 354 | pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
| 355 | |
| 356 | rc = vm_insert_mixed(vma, vaddr, pfn); |
| 357 | |
| 358 | if (rc == -ENOMEM) |
| 359 | return VM_FAULT_OOM; |
| 360 | if (rc < 0 && rc != -EBUSY) |
| 361 | return VM_FAULT_SIGBUS; |
| 362 | |
| 363 | return VM_FAULT_NOPAGE; |
| 364 | } |
| 365 | |
| 366 | static int dax_dev_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 367 | { |
| 368 | int rc; |
| 369 | struct file *filp = vma->vm_file; |
| 370 | struct dax_dev *dax_dev = filp->private_data; |
| 371 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 372 | dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 373 | current->comm, (vmf->flags & FAULT_FLAG_WRITE) |
| 374 | ? "write" : "read", vma->vm_start, vma->vm_end); |
| 375 | rcu_read_lock(); |
| 376 | rc = __dax_dev_fault(dax_dev, vma, vmf); |
| 377 | rcu_read_unlock(); |
| 378 | |
| 379 | return rc; |
| 380 | } |
| 381 | |
| 382 | static int __dax_dev_pmd_fault(struct dax_dev *dax_dev, |
| 383 | struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd, |
| 384 | unsigned int flags) |
| 385 | { |
| 386 | unsigned long pmd_addr = addr & PMD_MASK; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 387 | struct device *dev = &dax_dev->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 388 | struct dax_region *dax_region; |
| 389 | phys_addr_t phys; |
| 390 | pgoff_t pgoff; |
| 391 | pfn_t pfn; |
| 392 | |
| 393 | if (check_vma(dax_dev, vma, __func__)) |
| 394 | return VM_FAULT_SIGBUS; |
| 395 | |
| 396 | dax_region = dax_dev->region; |
| 397 | if (dax_region->align > PMD_SIZE) { |
| 398 | dev_dbg(dev, "%s: alignment > fault size\n", __func__); |
| 399 | return VM_FAULT_SIGBUS; |
| 400 | } |
| 401 | |
| 402 | /* dax pmd mappings require pfn_t_devmap() */ |
| 403 | if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) { |
| 404 | dev_dbg(dev, "%s: alignment > fault size\n", __func__); |
| 405 | return VM_FAULT_SIGBUS; |
| 406 | } |
| 407 | |
| 408 | pgoff = linear_page_index(vma, pmd_addr); |
Dan Williams | 4c3cb6e | 2016-09-03 10:36:00 -0700 | [diff] [blame] | 409 | phys = pgoff_to_phys(dax_dev, pgoff, PMD_SIZE); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 410 | if (phys == -1) { |
| 411 | dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__, |
| 412 | pgoff); |
| 413 | return VM_FAULT_SIGBUS; |
| 414 | } |
| 415 | |
| 416 | pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
| 417 | |
| 418 | return vmf_insert_pfn_pmd(vma, addr, pmd, pfn, |
| 419 | flags & FAULT_FLAG_WRITE); |
| 420 | } |
| 421 | |
| 422 | static int dax_dev_pmd_fault(struct vm_area_struct *vma, unsigned long addr, |
| 423 | pmd_t *pmd, unsigned int flags) |
| 424 | { |
| 425 | int rc; |
| 426 | struct file *filp = vma->vm_file; |
| 427 | struct dax_dev *dax_dev = filp->private_data; |
| 428 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 429 | dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 430 | current->comm, (flags & FAULT_FLAG_WRITE) |
| 431 | ? "write" : "read", vma->vm_start, vma->vm_end); |
| 432 | |
| 433 | rcu_read_lock(); |
| 434 | rc = __dax_dev_pmd_fault(dax_dev, vma, addr, pmd, flags); |
| 435 | rcu_read_unlock(); |
| 436 | |
| 437 | return rc; |
| 438 | } |
| 439 | |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 440 | static const struct vm_operations_struct dax_dev_vm_ops = { |
| 441 | .fault = dax_dev_fault, |
| 442 | .pmd_fault = dax_dev_pmd_fault, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 443 | }; |
| 444 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 445 | static int dax_mmap(struct file *filp, struct vm_area_struct *vma) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 446 | { |
| 447 | struct dax_dev *dax_dev = filp->private_data; |
| 448 | int rc; |
| 449 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 450 | dev_dbg(&dax_dev->dev, "%s\n", __func__); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 451 | |
| 452 | rc = check_vma(dax_dev, vma, __func__); |
| 453 | if (rc) |
| 454 | return rc; |
| 455 | |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 456 | vma->vm_ops = &dax_dev_vm_ops; |
| 457 | vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE; |
| 458 | return 0; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 459 | } |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 460 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 461 | /* return an unmapped area aligned to the dax region specified alignment */ |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 462 | static unsigned long dax_get_unmapped_area(struct file *filp, |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 463 | unsigned long addr, unsigned long len, unsigned long pgoff, |
| 464 | unsigned long flags) |
| 465 | { |
| 466 | unsigned long off, off_end, off_align, len_align, addr_align, align; |
| 467 | struct dax_dev *dax_dev = filp ? filp->private_data : NULL; |
| 468 | struct dax_region *dax_region; |
| 469 | |
| 470 | if (!dax_dev || addr) |
| 471 | goto out; |
| 472 | |
| 473 | dax_region = dax_dev->region; |
| 474 | align = dax_region->align; |
| 475 | off = pgoff << PAGE_SHIFT; |
| 476 | off_end = off + len; |
| 477 | off_align = round_up(off, align); |
| 478 | |
| 479 | if ((off_end <= off_align) || ((off_end - off_align) < align)) |
| 480 | goto out; |
| 481 | |
| 482 | len_align = len + align; |
| 483 | if ((off + len_align) < off) |
| 484 | goto out; |
| 485 | |
| 486 | addr_align = current->mm->get_unmapped_area(filp, addr, len_align, |
| 487 | pgoff, flags); |
| 488 | if (!IS_ERR_VALUE(addr_align)) { |
| 489 | addr_align += (off - addr_align) & (align - 1); |
| 490 | return addr_align; |
| 491 | } |
| 492 | out: |
| 493 | return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags); |
| 494 | } |
| 495 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 496 | static int dax_open(struct inode *inode, struct file *filp) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 497 | { |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 498 | struct dax_dev *dax_dev; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 499 | |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 500 | dax_dev = container_of(inode->i_cdev, struct dax_dev, cdev); |
| 501 | dev_dbg(&dax_dev->dev, "%s\n", __func__); |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 502 | inode->i_mapping = dax_dev->inode->i_mapping; |
| 503 | inode->i_mapping->host = dax_dev->inode; |
| 504 | filp->f_mapping = inode->i_mapping; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 505 | filp->private_data = dax_dev; |
| 506 | inode->i_flags = S_DAX; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 507 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 508 | return 0; |
| 509 | } |
| 510 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 511 | static int dax_release(struct inode *inode, struct file *filp) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 512 | { |
| 513 | struct dax_dev *dax_dev = filp->private_data; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 514 | |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 515 | dev_dbg(&dax_dev->dev, "%s\n", __func__); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 516 | return 0; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 519 | static const struct file_operations dax_fops = { |
| 520 | .llseek = noop_llseek, |
| 521 | .owner = THIS_MODULE, |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 522 | .open = dax_open, |
| 523 | .release = dax_release, |
| 524 | .get_unmapped_area = dax_get_unmapped_area, |
| 525 | .mmap = dax_mmap, |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 526 | }; |
| 527 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 528 | static void dax_dev_release(struct device *dev) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 529 | { |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 530 | struct dax_dev *dax_dev = to_dax_dev(dev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 531 | struct dax_region *dax_region = dax_dev->region; |
| 532 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 533 | ida_simple_remove(&dax_region->ida, dax_dev->id); |
| 534 | ida_simple_remove(&dax_minor_ida, MINOR(dev->devt)); |
| 535 | dax_region_put(dax_region); |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 536 | iput(dax_dev->inode); |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 537 | kfree(dax_dev); |
| 538 | } |
| 539 | |
| 540 | static void unregister_dax_dev(void *dev) |
| 541 | { |
| 542 | struct dax_dev *dax_dev = to_dax_dev(dev); |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 543 | struct cdev *cdev = &dax_dev->cdev; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 544 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 545 | dev_dbg(dev, "%s\n", __func__); |
| 546 | |
| 547 | /* |
| 548 | * Note, rcu is not protecting the liveness of dax_dev, rcu is |
| 549 | * ensuring that any fault handlers that might have seen |
| 550 | * dax_dev->alive == true, have completed. Any fault handlers |
| 551 | * that start after synchronize_rcu() has started will abort |
| 552 | * upon seeing dax_dev->alive == false. |
| 553 | */ |
| 554 | dax_dev->alive = false; |
| 555 | synchronize_rcu(); |
Dan Williams | 9dc1e49 | 2016-08-04 16:53:50 -0700 | [diff] [blame] | 556 | unmap_mapping_range(dax_dev->inode->i_mapping, 0, 0, 1); |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 557 | cdev_del(cdev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 558 | device_unregister(dev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 561 | struct dax_dev *devm_create_dax_dev(struct dax_region *dax_region, |
| 562 | struct resource *res, int count) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 563 | { |
| 564 | struct device *parent = dax_region->dev; |
| 565 | struct dax_dev *dax_dev; |
Dan Williams | 9d2d01a | 2016-07-19 16:17:58 -0700 | [diff] [blame] | 566 | int rc = 0, minor, i; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 567 | struct device *dev; |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 568 | struct cdev *cdev; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 569 | dev_t dev_t; |
| 570 | |
| 571 | dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL); |
| 572 | if (!dax_dev) |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 573 | return ERR_PTR(-ENOMEM); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 574 | |
Dan Williams | 9d2d01a | 2016-07-19 16:17:58 -0700 | [diff] [blame] | 575 | for (i = 0; i < count; i++) { |
| 576 | if (!IS_ALIGNED(res[i].start, dax_region->align) |
| 577 | || !IS_ALIGNED(resource_size(&res[i]), |
| 578 | dax_region->align)) { |
| 579 | rc = -EINVAL; |
| 580 | break; |
| 581 | } |
| 582 | dax_dev->res[i].start = res[i].start; |
| 583 | dax_dev->res[i].end = res[i].end; |
| 584 | } |
| 585 | |
| 586 | if (i < count) |
| 587 | goto err_id; |
| 588 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 589 | dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL); |
| 590 | if (dax_dev->id < 0) { |
| 591 | rc = dax_dev->id; |
| 592 | goto err_id; |
| 593 | } |
| 594 | |
| 595 | minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL); |
| 596 | if (minor < 0) { |
| 597 | rc = minor; |
| 598 | goto err_minor; |
| 599 | } |
| 600 | |
Arnd Bergmann | bc0a0fe | 2016-09-08 15:53:28 +0200 | [diff] [blame] | 601 | dev_t = MKDEV(MAJOR(dax_devt), minor); |
| 602 | dev = &dax_dev->dev; |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 603 | dax_dev->inode = dax_inode_get(&dax_dev->cdev, dev_t); |
| 604 | if (!dax_dev->inode) { |
| 605 | rc = -ENOMEM; |
| 606 | goto err_inode; |
| 607 | } |
| 608 | |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 609 | /* device_initialize() so cdev can reference kobj parent */ |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 610 | device_initialize(dev); |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 611 | |
| 612 | cdev = &dax_dev->cdev; |
| 613 | cdev_init(cdev, &dax_fops); |
| 614 | cdev->owner = parent->driver->owner; |
| 615 | cdev->kobj.parent = &dev->kobj; |
| 616 | rc = cdev_add(&dax_dev->cdev, dev_t, 1); |
| 617 | if (rc) |
| 618 | goto err_cdev; |
| 619 | |
| 620 | /* from here on we're committed to teardown via dax_dev_release() */ |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 621 | dax_dev->num_resources = count; |
| 622 | dax_dev->alive = true; |
| 623 | dax_dev->region = dax_region; |
| 624 | kref_get(&dax_region->kref); |
| 625 | |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 626 | dev->devt = dev_t; |
| 627 | dev->class = dax_class; |
| 628 | dev->parent = parent; |
| 629 | dev->groups = dax_attribute_groups; |
| 630 | dev->release = dax_dev_release; |
| 631 | dev_set_name(dev, "dax%d.%d", dax_region->id, dax_dev->id); |
| 632 | rc = device_add(dev); |
| 633 | if (rc) { |
| 634 | put_device(dev); |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 635 | return ERR_PTR(rc); |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 636 | } |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 637 | |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 638 | rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev); |
| 639 | if (rc) |
| 640 | return ERR_PTR(rc); |
| 641 | |
| 642 | return dax_dev; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 643 | |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 644 | err_cdev: |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 645 | iput(dax_dev->inode); |
| 646 | err_inode: |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 647 | ida_simple_remove(&dax_minor_ida, minor); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 648 | err_minor: |
| 649 | ida_simple_remove(&dax_region->ida, dax_dev->id); |
| 650 | err_id: |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 651 | kfree(dax_dev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 652 | |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 653 | return ERR_PTR(rc); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 654 | } |
| 655 | EXPORT_SYMBOL_GPL(devm_create_dax_dev); |
| 656 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 657 | static int __init dax_init(void) |
| 658 | { |
| 659 | int rc; |
| 660 | |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 661 | rc = dax_inode_init(); |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 662 | if (rc) |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 663 | return rc; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 664 | |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 665 | nr_dax = max(nr_dax, 256); |
| 666 | rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax"); |
| 667 | if (rc) |
| 668 | goto err_chrdev; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 669 | |
| 670 | dax_class = class_create(THIS_MODULE, "dax"); |
| 671 | if (IS_ERR(dax_class)) { |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 672 | rc = PTR_ERR(dax_class); |
| 673 | goto err_class; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | return 0; |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 677 | |
| 678 | err_class: |
| 679 | unregister_chrdev_region(dax_devt, nr_dax); |
| 680 | err_chrdev: |
| 681 | dax_inode_exit(); |
| 682 | return rc; |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static void __exit dax_exit(void) |
| 686 | { |
| 687 | class_destroy(dax_class); |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 688 | unregister_chrdev_region(dax_devt, nr_dax); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 689 | ida_destroy(&dax_minor_ida); |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 690 | dax_inode_exit(); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | MODULE_AUTHOR("Intel Corporation"); |
| 694 | MODULE_LICENSE("GPL v2"); |
| 695 | subsys_initcall(dax_init); |
| 696 | module_exit(dax_exit); |