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