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