net/mlx4_core: Reduce harmless SRIOV error message to debug level
[linux-2.6-block.git] / drivers / dax / dax.c
1 /*
2  * Copyright(c) 2016 Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  */
13 #include <linux/pagemap.h>
14 #include <linux/module.h>
15 #include <linux/device.h>
16 #include <linux/magic.h>
17 #include <linux/mount.h>
18 #include <linux/pfn_t.h>
19 #include <linux/hash.h>
20 #include <linux/cdev.h>
21 #include <linux/slab.h>
22 #include <linux/dax.h>
23 #include <linux/fs.h>
24 #include <linux/mm.h>
25 #include "dax.h"
26
27 static dev_t dax_devt;
28 DEFINE_STATIC_SRCU(dax_srcu);
29 static struct class *dax_class;
30 static DEFINE_IDA(dax_minor_ida);
31 static int nr_dax = CONFIG_NR_DEV_DAX;
32 module_param(nr_dax, int, S_IRUGO);
33 static struct vfsmount *dax_mnt;
34 static struct kmem_cache *dax_cache __read_mostly;
35 static struct super_block *dax_superblock __read_mostly;
36 MODULE_PARM_DESC(nr_dax, "max number of device-dax instances");
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
63  * @cdev - core chardev data
64  * @alive - !alive + srcu grace period == no new mappings can be established
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;
71         struct inode *inode;
72         struct device dev;
73         struct cdev cdev;
74         bool alive;
75         int id;
76         int num_resources;
77         struct resource res[0];
78 };
79
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
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
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
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
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
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
297         if (!IS_ALIGNED(res->start, align)
298                         || !IS_ALIGNED(resource_size(res), align))
299                 return NULL;
300
301         dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
302         if (!dax_region)
303                 return NULL;
304
305         dev_set_drvdata(parent, dax_region);
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;
314         if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
315                 kfree(dax_region);
316                 return NULL;;
317         }
318
319         kref_get(&dax_region->kref);
320         if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
321                 return NULL;
322         return dax_region;
323 }
324 EXPORT_SYMBOL_GPL(alloc_dax_region);
325
326 static struct dax_dev *to_dax_dev(struct device *dev)
327 {
328         return container_of(dev, struct dax_dev, dev);
329 }
330
331 static ssize_t size_show(struct device *dev,
332                 struct device_attribute *attr, char *buf)
333 {
334         struct dax_dev *dax_dev = to_dax_dev(dev);
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
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;
363         struct device *dev = &dax_dev->dev;
364         unsigned long mask;
365
366         if (!dax_dev->alive)
367                 return -ENXIO;
368
369         /* prevent private mappings from being established */
370         if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) {
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
424 static int __dax_dev_pte_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
425 {
426         struct device *dev = &dax_dev->dev;
427         struct dax_region *dax_region;
428         int rc = VM_FAULT_SIGBUS;
429         phys_addr_t phys;
430         pfn_t pfn;
431         unsigned int fault_size = PAGE_SIZE;
432
433         if (check_vma(dax_dev, vmf->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
442         if (fault_size != dax_region->align)
443                 return VM_FAULT_SIGBUS;
444
445         phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE);
446         if (phys == -1) {
447                 dev_dbg(dev, "%s: pgoff_to_phys(%#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
454         rc = vm_insert_mixed(vmf->vma, vmf->address, pfn);
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
464 static int __dax_dev_pmd_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
465 {
466         unsigned long pmd_addr = vmf->address & PMD_MASK;
467         struct device *dev = &dax_dev->dev;
468         struct dax_region *dax_region;
469         phys_addr_t phys;
470         pgoff_t pgoff;
471         pfn_t pfn;
472         unsigned int fault_size = PMD_SIZE;
473
474         if (check_vma(dax_dev, vmf->vma, __func__))
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
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
499         pgoff = linear_page_index(vmf->vma, pmd_addr);
500         phys = pgoff_to_phys(dax_dev, pgoff, PMD_SIZE);
501         if (phys == -1) {
502                 dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__,
503                                 pgoff);
504                 return VM_FAULT_SIGBUS;
505         }
506
507         pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
508
509         return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd, pfn,
510                         vmf->flags & FAULT_FLAG_WRITE);
511 }
512
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;
522         unsigned int fault_size = PUD_SIZE;
523
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
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
550         pgoff = linear_page_index(vmf->vma, pud_addr);
551         phys = pgoff_to_phys(dax_dev, pgoff, PUD_SIZE);
552         if (phys == -1) {
553                 dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__,
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
570 static int dax_dev_huge_fault(struct vm_fault *vmf,
571                 enum page_entry_size pe_size)
572 {
573         int rc, id;
574         struct file *filp = vmf->vma->vm_file;
575         struct dax_dev *dax_dev = filp->private_data;
576
577         dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
578                         current->comm, (vmf->flags & FAULT_FLAG_WRITE)
579                         ? "write" : "read",
580                         vmf->vma->vm_start, vmf->vma->vm_end);
581
582         id = srcu_read_lock(&dax_srcu);
583         switch (pe_size) {
584         case PE_SIZE_PTE:
585                 rc = __dax_dev_pte_fault(dax_dev, vmf);
586                 break;
587         case PE_SIZE_PMD:
588                 rc = __dax_dev_pmd_fault(dax_dev, vmf);
589                 break;
590         case PE_SIZE_PUD:
591                 rc = __dax_dev_pud_fault(dax_dev, vmf);
592                 break;
593         default:
594                 return VM_FAULT_FALLBACK;
595         }
596         srcu_read_unlock(&dax_srcu, id);
597
598         return rc;
599 }
600
601 static int dax_dev_fault(struct vm_fault *vmf)
602 {
603         return dax_dev_huge_fault(vmf, PE_SIZE_PTE);
604 }
605
606 static const struct vm_operations_struct dax_dev_vm_ops = {
607         .fault = dax_dev_fault,
608         .huge_fault = dax_dev_huge_fault,
609 };
610
611 static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
612 {
613         struct dax_dev *dax_dev = filp->private_data;
614         int rc;
615
616         dev_dbg(&dax_dev->dev, "%s\n", __func__);
617
618         rc = check_vma(dax_dev, vma, __func__);
619         if (rc)
620                 return rc;
621
622         vma->vm_ops = &dax_dev_vm_ops;
623         vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
624         return 0;
625 }
626
627 /* return an unmapped area aligned to the dax region specified alignment */
628 static unsigned long dax_get_unmapped_area(struct file *filp,
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
662 static int dax_open(struct inode *inode, struct file *filp)
663 {
664         struct dax_dev *dax_dev;
665
666         dax_dev = container_of(inode->i_cdev, struct dax_dev, cdev);
667         dev_dbg(&dax_dev->dev, "%s\n", __func__);
668         inode->i_mapping = dax_dev->inode->i_mapping;
669         inode->i_mapping->host = dax_dev->inode;
670         filp->f_mapping = inode->i_mapping;
671         filp->private_data = dax_dev;
672         inode->i_flags = S_DAX;
673
674         return 0;
675 }
676
677 static int dax_release(struct inode *inode, struct file *filp)
678 {
679         struct dax_dev *dax_dev = filp->private_data;
680
681         dev_dbg(&dax_dev->dev, "%s\n", __func__);
682         return 0;
683 }
684
685 static const struct file_operations dax_fops = {
686         .llseek = noop_llseek,
687         .owner = THIS_MODULE,
688         .open = dax_open,
689         .release = dax_release,
690         .get_unmapped_area = dax_get_unmapped_area,
691         .mmap = dax_mmap,
692 };
693
694 static void dax_dev_release(struct device *dev)
695 {
696         struct dax_dev *dax_dev = to_dax_dev(dev);
697         struct dax_region *dax_region = dax_dev->region;
698
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);
702         iput(dax_dev->inode);
703         kfree(dax_dev);
704 }
705
706 static void kill_dax_dev(struct dax_dev *dax_dev)
707 {
708         /*
709          * Note, rcu is not protecting the liveness of dax_dev, rcu is
710          * ensuring that any fault handlers that might have seen
711          * dax_dev->alive == true, have completed.  Any fault handlers
712          * that start after synchronize_srcu() has started will abort
713          * upon seeing dax_dev->alive == false.
714          */
715         dax_dev->alive = false;
716         synchronize_srcu(&dax_srcu);
717         unmap_mapping_range(dax_dev->inode->i_mapping, 0, 0, 1);
718 }
719
720 static void unregister_dax_dev(void *dev)
721 {
722         struct dax_dev *dax_dev = to_dax_dev(dev);
723
724         dev_dbg(dev, "%s\n", __func__);
725
726         kill_dax_dev(dax_dev);
727         cdev_device_del(&dax_dev->cdev, dev);
728         put_device(dev);
729 }
730
731 struct dax_dev *devm_create_dax_dev(struct dax_region *dax_region,
732                 struct resource *res, int count)
733 {
734         struct device *parent = dax_region->dev;
735         struct dax_dev *dax_dev;
736         int rc = 0, minor, i;
737         struct device *dev;
738         struct cdev *cdev;
739         dev_t dev_t;
740
741         dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
742         if (!dax_dev)
743                 return ERR_PTR(-ENOMEM);
744
745         for (i = 0; i < count; i++) {
746                 if (!IS_ALIGNED(res[i].start, dax_region->align)
747                                 || !IS_ALIGNED(resource_size(&res[i]),
748                                         dax_region->align)) {
749                         rc = -EINVAL;
750                         break;
751                 }
752                 dax_dev->res[i].start = res[i].start;
753                 dax_dev->res[i].end = res[i].end;
754         }
755
756         if (i < count)
757                 goto err_id;
758
759         dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
760         if (dax_dev->id < 0) {
761                 rc = dax_dev->id;
762                 goto err_id;
763         }
764
765         minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL);
766         if (minor < 0) {
767                 rc = minor;
768                 goto err_minor;
769         }
770
771         dev_t = MKDEV(MAJOR(dax_devt), minor);
772         dev = &dax_dev->dev;
773         dax_dev->inode = dax_inode_get(&dax_dev->cdev, dev_t);
774         if (!dax_dev->inode) {
775                 rc = -ENOMEM;
776                 goto err_inode;
777         }
778
779         /* from here on we're committed to teardown via dax_dev_release() */
780         device_initialize(dev);
781
782         cdev = &dax_dev->cdev;
783         cdev_init(cdev, &dax_fops);
784         cdev->owner = parent->driver->owner;
785
786         dax_dev->num_resources = count;
787         dax_dev->alive = true;
788         dax_dev->region = dax_region;
789         kref_get(&dax_region->kref);
790
791         dev->devt = dev_t;
792         dev->class = dax_class;
793         dev->parent = parent;
794         dev->groups = dax_attribute_groups;
795         dev->release = dax_dev_release;
796         dev_set_name(dev, "dax%d.%d", dax_region->id, dax_dev->id);
797
798         rc = cdev_device_add(cdev, dev);
799         if (rc) {
800                 kill_dax_dev(dax_dev);
801                 put_device(dev);
802                 return ERR_PTR(rc);
803         }
804
805         rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev);
806         if (rc)
807                 return ERR_PTR(rc);
808
809         return dax_dev;
810
811  err_inode:
812         ida_simple_remove(&dax_minor_ida, minor);
813  err_minor:
814         ida_simple_remove(&dax_region->ida, dax_dev->id);
815  err_id:
816         kfree(dax_dev);
817
818         return ERR_PTR(rc);
819 }
820 EXPORT_SYMBOL_GPL(devm_create_dax_dev);
821
822 static int __init dax_init(void)
823 {
824         int rc;
825
826         rc = dax_inode_init();
827         if (rc)
828                 return rc;
829
830         nr_dax = max(nr_dax, 256);
831         rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
832         if (rc)
833                 goto err_chrdev;
834
835         dax_class = class_create(THIS_MODULE, "dax");
836         if (IS_ERR(dax_class)) {
837                 rc = PTR_ERR(dax_class);
838                 goto err_class;
839         }
840
841         return 0;
842
843  err_class:
844         unregister_chrdev_region(dax_devt, nr_dax);
845  err_chrdev:
846         dax_inode_exit();
847         return rc;
848 }
849
850 static void __exit dax_exit(void)
851 {
852         class_destroy(dax_class);
853         unregister_chrdev_region(dax_devt, nr_dax);
854         ida_destroy(&dax_minor_ida);
855         dax_inode_exit();
856 }
857
858 MODULE_AUTHOR("Intel Corporation");
859 MODULE_LICENSE("GPL v2");
860 subsys_initcall(dax_init);
861 module_exit(dax_exit);