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