2 * Copyright(c) 2016 Intel Corporation. All rights reserved.
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.
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.
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>
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");
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
56 unsigned long pfn_flags;
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
70 struct dax_region *region;
77 struct resource res[0];
80 static ssize_t id_show(struct device *dev,
81 struct device_attribute *attr, char *buf)
83 struct dax_region *dax_region;
87 dax_region = dev_get_drvdata(dev);
89 rc = sprintf(buf, "%d\n", dax_region->id);
94 static DEVICE_ATTR_RO(id);
96 static ssize_t region_size_show(struct device *dev,
97 struct device_attribute *attr, char *buf)
99 struct dax_region *dax_region;
103 dax_region = dev_get_drvdata(dev);
105 rc = sprintf(buf, "%llu\n", (unsigned long long)
106 resource_size(&dax_region->res));
111 static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
112 region_size_show, NULL);
114 static ssize_t align_show(struct device *dev,
115 struct device_attribute *attr, char *buf)
117 struct dax_region *dax_region;
121 dax_region = dev_get_drvdata(dev);
123 rc = sprintf(buf, "%u\n", dax_region->align);
128 static DEVICE_ATTR_RO(align);
130 static struct attribute *dax_region_attributes[] = {
131 &dev_attr_region_size.attr,
132 &dev_attr_align.attr,
137 static const struct attribute_group dax_region_attribute_group = {
138 .name = "dax_region",
139 .attrs = dax_region_attributes,
142 static const struct attribute_group *dax_region_attribute_groups[] = {
143 &dax_region_attribute_group,
147 static struct inode *dax_alloc_inode(struct super_block *sb)
149 return kmem_cache_alloc(dax_cache, GFP_KERNEL);
152 static void dax_i_callback(struct rcu_head *head)
154 struct inode *inode = container_of(head, struct inode, i_rcu);
156 kmem_cache_free(dax_cache, inode);
159 static void dax_destroy_inode(struct inode *inode)
161 call_rcu(&inode->i_rcu, dax_i_callback);
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,
171 static struct dentry *dax_mount(struct file_system_type *fs_type,
172 int flags, const char *dev_name, void *data)
174 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
177 static struct file_system_type dax_type = {
180 .kill_sb = kill_anon_super,
183 static int dax_test(struct inode *inode, void *data)
185 return inode->i_cdev == data;
188 static int dax_set(struct inode *inode, void *data)
190 inode->i_cdev = data;
194 static struct inode *dax_inode_get(struct cdev *cdev, dev_t devt)
198 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
199 dax_test, dax_set, cdev);
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);
214 static void init_once(void *inode)
216 inode_init_once(inode);
219 static int dax_inode_init(void)
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),
230 rc = register_filesystem(&dax_type);
232 goto err_register_fs;
234 dax_mnt = kern_mount(&dax_type);
235 if (IS_ERR(dax_mnt)) {
236 rc = PTR_ERR(dax_mnt);
239 dax_superblock = dax_mnt->mnt_sb;
244 unregister_filesystem(&dax_type);
246 kmem_cache_destroy(dax_cache);
251 static void dax_inode_exit(void)
253 kern_unmount(dax_mnt);
254 unregister_filesystem(&dax_type);
255 kmem_cache_destroy(dax_cache);
258 static void dax_region_free(struct kref *kref)
260 struct dax_region *dax_region;
262 dax_region = container_of(kref, struct dax_region, kref);
266 void dax_region_put(struct dax_region *dax_region)
268 kref_put(&dax_region->kref, dax_region_free);
270 EXPORT_SYMBOL_GPL(dax_region_put);
272 static void dax_region_unregister(void *region)
274 struct dax_region *dax_region = region;
276 sysfs_remove_groups(&dax_region->dev->kobj,
277 dax_region_attribute_groups);
278 dax_region_put(dax_region);
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)
285 struct dax_region *dax_region;
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.
292 if (dev_get_drvdata(parent)) {
293 dev_WARN(parent, "dax core failed to setup private data\n");
297 if (!IS_ALIGNED(res->start, align)
298 || !IS_ALIGNED(resource_size(res), align))
301 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
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)) {
319 kref_get(&dax_region->kref);
320 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
324 EXPORT_SYMBOL_GPL(alloc_dax_region);
326 static struct dax_dev *to_dax_dev(struct device *dev)
328 return container_of(dev, struct dax_dev, dev);
331 static ssize_t size_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
334 struct dax_dev *dax_dev = to_dax_dev(dev);
335 unsigned long long size = 0;
338 for (i = 0; i < dax_dev->num_resources; i++)
339 size += resource_size(&dax_dev->res[i]);
341 return sprintf(buf, "%llu\n", size);
343 static DEVICE_ATTR_RO(size);
345 static struct attribute *dax_device_attributes[] = {
350 static const struct attribute_group dax_device_attribute_group = {
351 .attrs = dax_device_attributes,
354 static const struct attribute_group *dax_attribute_groups[] = {
355 &dax_device_attribute_group,
359 static int check_vma(struct dax_dev *dax_dev, struct vm_area_struct *vma,
362 struct dax_region *dax_region = dax_dev->region;
363 struct device *dev = &dax_dev->dev;
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);
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,
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);
391 if (!vma_is_dax(vma)) {
392 dev_info(dev, "%s: %s: fail, vma is not DAX capable\n",
393 current->comm, func);
400 static phys_addr_t pgoff_to_phys(struct dax_dev *dax_dev, pgoff_t pgoff,
403 struct resource *res;
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)
412 pgoff -= PHYS_PFN(resource_size(res));
415 if (i < dax_dev->num_resources) {
416 res = &dax_dev->res[i];
417 if (phys + size - 1 <= res->end)
424 static int __dax_dev_pte_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
426 struct device *dev = &dax_dev->dev;
427 struct dax_region *dax_region;
428 int rc = VM_FAULT_SIGBUS;
431 unsigned int fault_size = PAGE_SIZE;
433 if (check_vma(dax_dev, vmf->vma, __func__))
434 return VM_FAULT_SIGBUS;
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;
442 if (fault_size != dax_region->align)
443 return VM_FAULT_SIGBUS;
445 phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE);
447 dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__,
449 return VM_FAULT_SIGBUS;
452 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
454 rc = vm_insert_mixed(vmf->vma, vmf->address, pfn);
458 if (rc < 0 && rc != -EBUSY)
459 return VM_FAULT_SIGBUS;
461 return VM_FAULT_NOPAGE;
464 static int __dax_dev_pmd_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
466 unsigned long pmd_addr = vmf->address & PMD_MASK;
467 struct device *dev = &dax_dev->dev;
468 struct dax_region *dax_region;
472 unsigned int fault_size = PMD_SIZE;
474 if (check_vma(dax_dev, vmf->vma, __func__))
475 return VM_FAULT_SIGBUS;
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;
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;
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;
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;
499 pgoff = linear_page_index(vmf->vma, pmd_addr);
500 phys = pgoff_to_phys(dax_dev, pgoff, PMD_SIZE);
502 dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__,
504 return VM_FAULT_SIGBUS;
507 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
509 return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd, pfn,
510 vmf->flags & FAULT_FLAG_WRITE);
513 #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
514 static int __dax_dev_pud_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
516 unsigned long pud_addr = vmf->address & PUD_MASK;
517 struct device *dev = &dax_dev->dev;
518 struct dax_region *dax_region;
522 unsigned int fault_size = PUD_SIZE;
525 if (check_vma(dax_dev, vmf->vma, __func__))
526 return VM_FAULT_SIGBUS;
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;
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;
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;
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;
550 pgoff = linear_page_index(vmf->vma, pud_addr);
551 phys = pgoff_to_phys(dax_dev, pgoff, PUD_SIZE);
553 dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__,
555 return VM_FAULT_SIGBUS;
558 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
560 return vmf_insert_pfn_pud(vmf->vma, vmf->address, vmf->pud, pfn,
561 vmf->flags & FAULT_FLAG_WRITE);
564 static int __dax_dev_pud_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
566 return VM_FAULT_FALLBACK;
568 #endif /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
570 static int dax_dev_huge_fault(struct vm_fault *vmf,
571 enum page_entry_size pe_size)
574 struct file *filp = vmf->vma->vm_file;
575 struct dax_dev *dax_dev = filp->private_data;
577 dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
578 current->comm, (vmf->flags & FAULT_FLAG_WRITE)
580 vmf->vma->vm_start, vmf->vma->vm_end);
582 id = srcu_read_lock(&dax_srcu);
585 rc = __dax_dev_pte_fault(dax_dev, vmf);
588 rc = __dax_dev_pmd_fault(dax_dev, vmf);
591 rc = __dax_dev_pud_fault(dax_dev, vmf);
594 return VM_FAULT_FALLBACK;
596 srcu_read_unlock(&dax_srcu, id);
601 static int dax_dev_fault(struct vm_fault *vmf)
603 return dax_dev_huge_fault(vmf, PE_SIZE_PTE);
606 static const struct vm_operations_struct dax_dev_vm_ops = {
607 .fault = dax_dev_fault,
608 .huge_fault = dax_dev_huge_fault,
611 static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
613 struct dax_dev *dax_dev = filp->private_data;
616 dev_dbg(&dax_dev->dev, "%s\n", __func__);
618 rc = check_vma(dax_dev, vma, __func__);
622 vma->vm_ops = &dax_dev_vm_ops;
623 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
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,
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;
636 if (!dax_dev || addr)
639 dax_region = dax_dev->region;
640 align = dax_region->align;
641 off = pgoff << PAGE_SHIFT;
643 off_align = round_up(off, align);
645 if ((off_end <= off_align) || ((off_end - off_align) < align))
648 len_align = len + align;
649 if ((off + len_align) < off)
652 addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
654 if (!IS_ERR_VALUE(addr_align)) {
655 addr_align += (off - addr_align) & (align - 1);
659 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
662 static int dax_open(struct inode *inode, struct file *filp)
664 struct dax_dev *dax_dev;
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;
677 static int dax_release(struct inode *inode, struct file *filp)
679 struct dax_dev *dax_dev = filp->private_data;
681 dev_dbg(&dax_dev->dev, "%s\n", __func__);
685 static const struct file_operations dax_fops = {
686 .llseek = noop_llseek,
687 .owner = THIS_MODULE,
689 .release = dax_release,
690 .get_unmapped_area = dax_get_unmapped_area,
694 static void dax_dev_release(struct device *dev)
696 struct dax_dev *dax_dev = to_dax_dev(dev);
697 struct dax_region *dax_region = dax_dev->region;
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);
706 static void unregister_dax_dev(void *dev)
708 struct dax_dev *dax_dev = to_dax_dev(dev);
709 struct cdev *cdev = &dax_dev->cdev;
711 dev_dbg(dev, "%s\n", __func__);
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
717 * that start after synchronize_srcu() has started will abort
718 * upon seeing dax_dev->alive == false.
720 dax_dev->alive = false;
721 synchronize_srcu(&dax_srcu);
722 unmap_mapping_range(dax_dev->inode->i_mapping, 0, 0, 1);
724 device_unregister(dev);
727 struct dax_dev *devm_create_dax_dev(struct dax_region *dax_region,
728 struct resource *res, int count)
730 struct device *parent = dax_region->dev;
731 struct dax_dev *dax_dev;
732 int rc = 0, minor, i;
737 dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
739 return ERR_PTR(-ENOMEM);
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)) {
748 dax_dev->res[i].start = res[i].start;
749 dax_dev->res[i].end = res[i].end;
755 dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
756 if (dax_dev->id < 0) {
761 minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL);
767 dev_t = MKDEV(MAJOR(dax_devt), minor);
769 dax_dev->inode = dax_inode_get(&dax_dev->cdev, dev_t);
770 if (!dax_dev->inode) {
775 /* device_initialize() so cdev can reference kobj parent */
776 device_initialize(dev);
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);
786 /* from here on we're committed to teardown via dax_dev_release() */
787 dax_dev->num_resources = count;
788 dax_dev->alive = true;
789 dax_dev->region = dax_region;
790 kref_get(&dax_region->kref);
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);
804 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev);
811 iput(dax_dev->inode);
813 ida_simple_remove(&dax_minor_ida, minor);
815 ida_simple_remove(&dax_region->ida, dax_dev->id);
821 EXPORT_SYMBOL_GPL(devm_create_dax_dev);
823 static int __init dax_init(void)
827 rc = dax_inode_init();
831 nr_dax = max(nr_dax, 256);
832 rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
836 dax_class = class_create(THIS_MODULE, "dax");
837 if (IS_ERR(dax_class)) {
838 rc = PTR_ERR(dax_class);
845 unregister_chrdev_region(dax_devt, nr_dax);
851 static void __exit dax_exit(void)
853 class_destroy(dax_class);
854 unregister_chrdev_region(dax_devt, nr_dax);
855 ida_destroy(&dax_minor_ida);
859 MODULE_AUTHOR("Intel Corporation");
860 MODULE_LICENSE("GPL v2");
861 subsys_initcall(dax_init);
862 module_exit(dax_exit);