Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright(c) 2016-2018 Intel Corporation. All rights reserved. */ |
Dan Williams | 89ec9f2 | 2018-10-29 15:52:42 -0700 | [diff] [blame] | 3 | #include <linux/memremap.h> |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 4 | #include <linux/pagemap.h> |
| 5 | #include <linux/module.h> |
| 6 | #include <linux/device.h> |
| 7 | #include <linux/pfn_t.h> |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 8 | #include <linux/cdev.h> |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 9 | #include <linux/slab.h> |
| 10 | #include <linux/dax.h> |
| 11 | #include <linux/fs.h> |
| 12 | #include <linux/mm.h> |
Dave Jiang | ef84230 | 2018-04-19 13:39:43 -0700 | [diff] [blame] | 13 | #include <linux/mman.h> |
Dan Williams | 7361636 | 2017-05-04 23:38:43 -0700 | [diff] [blame] | 14 | #include "dax-private.h" |
Dan Williams | 51cf784 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 15 | #include "bus.h" |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 16 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 17 | static int check_vma(struct dev_dax *dev_dax, struct vm_area_struct *vma, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 18 | const char *func) |
| 19 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 20 | struct dax_region *dax_region = dev_dax->region; |
| 21 | struct device *dev = &dev_dax->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 22 | unsigned long mask; |
| 23 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 24 | if (!dax_alive(dev_dax->dax_dev)) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 25 | return -ENXIO; |
| 26 | |
Dan Williams | 4cb1935 | 2016-11-16 09:00:38 -0800 | [diff] [blame] | 27 | /* prevent private mappings from being established */ |
Dan Williams | 325896f | 2016-12-06 17:03:35 -0800 | [diff] [blame] | 28 | if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) { |
Jeff Moyer | 5a14e91 | 2018-06-27 11:43:58 -0400 | [diff] [blame] | 29 | dev_info_ratelimited(dev, |
| 30 | "%s: %s: fail, attempted private mapping\n", |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 31 | current->comm, func); |
| 32 | return -EINVAL; |
| 33 | } |
| 34 | |
| 35 | mask = dax_region->align - 1; |
| 36 | if (vma->vm_start & mask || vma->vm_end & mask) { |
Jeff Moyer | 5a14e91 | 2018-06-27 11:43:58 -0400 | [diff] [blame] | 37 | dev_info_ratelimited(dev, |
| 38 | "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n", |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 39 | current->comm, func, vma->vm_start, vma->vm_end, |
| 40 | mask); |
| 41 | return -EINVAL; |
| 42 | } |
| 43 | |
| 44 | if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV |
| 45 | && (vma->vm_flags & VM_DONTCOPY) == 0) { |
Jeff Moyer | 5a14e91 | 2018-06-27 11:43:58 -0400 | [diff] [blame] | 46 | dev_info_ratelimited(dev, |
| 47 | "%s: %s: fail, dax range requires MADV_DONTFORK\n", |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 48 | current->comm, func); |
| 49 | return -EINVAL; |
| 50 | } |
| 51 | |
| 52 | if (!vma_is_dax(vma)) { |
Jeff Moyer | 5a14e91 | 2018-06-27 11:43:58 -0400 | [diff] [blame] | 53 | dev_info_ratelimited(dev, |
| 54 | "%s: %s: fail, vma is not DAX capable\n", |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 55 | current->comm, func); |
| 56 | return -EINVAL; |
| 57 | } |
| 58 | |
| 59 | return 0; |
| 60 | } |
| 61 | |
Dan Williams | 7361636 | 2017-05-04 23:38:43 -0700 | [diff] [blame] | 62 | /* see "strong" declaration in tools/testing/nvdimm/dax-dev.c */ |
| 63 | __weak phys_addr_t dax_pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 64 | unsigned long size) |
| 65 | { |
Dan Williams | 753a085 | 2017-07-14 13:54:50 -0700 | [diff] [blame] | 66 | struct resource *res = &dev_dax->region->res; |
| 67 | phys_addr_t phys; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 68 | |
Dan Williams | 753a085 | 2017-07-14 13:54:50 -0700 | [diff] [blame] | 69 | phys = pgoff * PAGE_SIZE + res->start; |
| 70 | if (phys >= res->start && phys <= res->end) { |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 71 | if (phys + size - 1 <= res->end) |
| 72 | return phys; |
| 73 | } |
| 74 | |
| 75 | return -1; |
| 76 | } |
| 77 | |
Dan Williams | 226ab56 | 2018-07-13 21:49:34 -0700 | [diff] [blame] | 78 | static vm_fault_t __dev_dax_pte_fault(struct dev_dax *dev_dax, |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 79 | struct vm_fault *vmf, pfn_t *pfn) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 80 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 81 | struct device *dev = &dev_dax->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 82 | struct dax_region *dax_region; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 83 | phys_addr_t phys; |
Dave Jiang | 0134ed4 | 2017-03-10 13:24:22 -0700 | [diff] [blame] | 84 | unsigned int fault_size = PAGE_SIZE; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 85 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 86 | if (check_vma(dev_dax, vmf->vma, __func__)) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 87 | return VM_FAULT_SIGBUS; |
| 88 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 89 | dax_region = dev_dax->region; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 90 | if (dax_region->align > PAGE_SIZE) { |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 91 | dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n", |
| 92 | dax_region->align, fault_size); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 93 | return VM_FAULT_SIGBUS; |
| 94 | } |
| 95 | |
Dave Jiang | 0134ed4 | 2017-03-10 13:24:22 -0700 | [diff] [blame] | 96 | if (fault_size != dax_region->align) |
| 97 | return VM_FAULT_SIGBUS; |
| 98 | |
Dan Williams | 7361636 | 2017-05-04 23:38:43 -0700 | [diff] [blame] | 99 | phys = dax_pgoff_to_phys(dev_dax, vmf->pgoff, PAGE_SIZE); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 100 | if (phys == -1) { |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 101 | dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", vmf->pgoff); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 102 | return VM_FAULT_SIGBUS; |
| 103 | } |
| 104 | |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 105 | *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 106 | |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 107 | return vmf_insert_mixed(vmf->vma, vmf->address, *pfn); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Dan Williams | 226ab56 | 2018-07-13 21:49:34 -0700 | [diff] [blame] | 110 | static vm_fault_t __dev_dax_pmd_fault(struct dev_dax *dev_dax, |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 111 | struct vm_fault *vmf, pfn_t *pfn) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 112 | { |
Dave Jiang | d8a849e | 2017-02-22 15:40:03 -0800 | [diff] [blame] | 113 | unsigned long pmd_addr = vmf->address & PMD_MASK; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 114 | struct device *dev = &dev_dax->dev; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 115 | struct dax_region *dax_region; |
| 116 | phys_addr_t phys; |
| 117 | pgoff_t pgoff; |
Dave Jiang | 0134ed4 | 2017-03-10 13:24:22 -0700 | [diff] [blame] | 118 | unsigned int fault_size = PMD_SIZE; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 119 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 120 | if (check_vma(dev_dax, vmf->vma, __func__)) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 121 | return VM_FAULT_SIGBUS; |
| 122 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 123 | dax_region = dev_dax->region; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 124 | if (dax_region->align > PMD_SIZE) { |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 125 | dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n", |
| 126 | dax_region->align, fault_size); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 127 | return VM_FAULT_SIGBUS; |
| 128 | } |
| 129 | |
| 130 | /* dax pmd mappings require pfn_t_devmap() */ |
| 131 | if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) { |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 132 | dev_dbg(dev, "region lacks devmap flags\n"); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 133 | return VM_FAULT_SIGBUS; |
| 134 | } |
| 135 | |
Dave Jiang | 0134ed4 | 2017-03-10 13:24:22 -0700 | [diff] [blame] | 136 | if (fault_size < dax_region->align) |
| 137 | return VM_FAULT_SIGBUS; |
| 138 | else if (fault_size > dax_region->align) |
| 139 | return VM_FAULT_FALLBACK; |
| 140 | |
| 141 | /* if we are outside of the VMA */ |
| 142 | if (pmd_addr < vmf->vma->vm_start || |
| 143 | (pmd_addr + PMD_SIZE) > vmf->vma->vm_end) |
| 144 | return VM_FAULT_SIGBUS; |
| 145 | |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 146 | pgoff = linear_page_index(vmf->vma, pmd_addr); |
Dan Williams | 7361636 | 2017-05-04 23:38:43 -0700 | [diff] [blame] | 147 | phys = dax_pgoff_to_phys(dev_dax, pgoff, PMD_SIZE); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 148 | if (phys == -1) { |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 149 | dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 150 | return VM_FAULT_SIGBUS; |
| 151 | } |
| 152 | |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 153 | *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 154 | |
Dan Williams | fce86ff | 2019-05-13 17:15:33 -0700 | [diff] [blame] | 155 | return vmf_insert_pfn_pmd(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 158 | #ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD |
Dan Williams | 226ab56 | 2018-07-13 21:49:34 -0700 | [diff] [blame] | 159 | static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax, |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 160 | struct vm_fault *vmf, pfn_t *pfn) |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 161 | { |
| 162 | unsigned long pud_addr = vmf->address & PUD_MASK; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 163 | struct device *dev = &dev_dax->dev; |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 164 | struct dax_region *dax_region; |
| 165 | phys_addr_t phys; |
| 166 | pgoff_t pgoff; |
Dave Jiang | 70b085b | 2017-03-10 13:24:27 -0700 | [diff] [blame] | 167 | unsigned int fault_size = PUD_SIZE; |
| 168 | |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 169 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 170 | if (check_vma(dev_dax, vmf->vma, __func__)) |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 171 | return VM_FAULT_SIGBUS; |
| 172 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 173 | dax_region = dev_dax->region; |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 174 | if (dax_region->align > PUD_SIZE) { |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 175 | dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n", |
| 176 | dax_region->align, fault_size); |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 177 | return VM_FAULT_SIGBUS; |
| 178 | } |
| 179 | |
| 180 | /* dax pud mappings require pfn_t_devmap() */ |
| 181 | if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) { |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 182 | dev_dbg(dev, "region lacks devmap flags\n"); |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 183 | return VM_FAULT_SIGBUS; |
| 184 | } |
| 185 | |
Dave Jiang | 70b085b | 2017-03-10 13:24:27 -0700 | [diff] [blame] | 186 | if (fault_size < dax_region->align) |
| 187 | return VM_FAULT_SIGBUS; |
| 188 | else if (fault_size > dax_region->align) |
| 189 | return VM_FAULT_FALLBACK; |
| 190 | |
| 191 | /* if we are outside of the VMA */ |
| 192 | if (pud_addr < vmf->vma->vm_start || |
| 193 | (pud_addr + PUD_SIZE) > vmf->vma->vm_end) |
| 194 | return VM_FAULT_SIGBUS; |
| 195 | |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 196 | pgoff = linear_page_index(vmf->vma, pud_addr); |
Dan Williams | 7361636 | 2017-05-04 23:38:43 -0700 | [diff] [blame] | 197 | phys = dax_pgoff_to_phys(dev_dax, pgoff, PUD_SIZE); |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 198 | if (phys == -1) { |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 199 | dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff); |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 200 | return VM_FAULT_SIGBUS; |
| 201 | } |
| 202 | |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 203 | *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags); |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 204 | |
Dan Williams | fce86ff | 2019-05-13 17:15:33 -0700 | [diff] [blame] | 205 | return vmf_insert_pfn_pud(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE); |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 206 | } |
| 207 | #else |
Dan Williams | 226ab56 | 2018-07-13 21:49:34 -0700 | [diff] [blame] | 208 | static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax, |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 209 | struct vm_fault *vmf, pfn_t *pfn) |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 210 | { |
| 211 | return VM_FAULT_FALLBACK; |
| 212 | } |
| 213 | #endif /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */ |
| 214 | |
Dan Williams | 226ab56 | 2018-07-13 21:49:34 -0700 | [diff] [blame] | 215 | static vm_fault_t dev_dax_huge_fault(struct vm_fault *vmf, |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 216 | enum page_entry_size pe_size) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 217 | { |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 218 | struct file *filp = vmf->vma->vm_file; |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 219 | unsigned long fault_size; |
Souptick Joarder | 36bdac1 | 2018-09-04 15:46:26 -0700 | [diff] [blame] | 220 | vm_fault_t rc = VM_FAULT_SIGBUS; |
| 221 | int id; |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 222 | pfn_t pfn; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 223 | struct dev_dax *dev_dax = filp->private_data; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 224 | |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 225 | dev_dbg(&dev_dax->dev, "%s: %s (%#lx - %#lx) size = %d\n", current->comm, |
| 226 | (vmf->flags & FAULT_FLAG_WRITE) ? "write" : "read", |
Oliver O'Halloran | 7620262 | 2017-04-12 01:59:36 +1000 | [diff] [blame] | 227 | vmf->vma->vm_start, vmf->vma->vm_end, pe_size); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 228 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 229 | id = dax_read_lock(); |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 230 | switch (pe_size) { |
| 231 | case PE_SIZE_PTE: |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 232 | fault_size = PAGE_SIZE; |
| 233 | rc = __dev_dax_pte_fault(dev_dax, vmf, &pfn); |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 234 | break; |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 235 | case PE_SIZE_PMD: |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 236 | fault_size = PMD_SIZE; |
| 237 | rc = __dev_dax_pmd_fault(dev_dax, vmf, &pfn); |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 238 | break; |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 239 | case PE_SIZE_PUD: |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 240 | fault_size = PUD_SIZE; |
| 241 | rc = __dev_dax_pud_fault(dev_dax, vmf, &pfn); |
Dave Jiang | 9557fee | 2017-02-24 14:57:05 -0800 | [diff] [blame] | 242 | break; |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 243 | default: |
Pushkar Jambhlekar | 54eafcc | 2017-04-11 09:12:25 -0700 | [diff] [blame] | 244 | rc = VM_FAULT_SIGBUS; |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 245 | } |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 246 | |
| 247 | if (rc == VM_FAULT_NOPAGE) { |
| 248 | unsigned long i; |
Dan Williams | 35de299 | 2018-07-13 21:49:45 -0700 | [diff] [blame] | 249 | pgoff_t pgoff; |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 250 | |
| 251 | /* |
| 252 | * In the device-dax case the only possibility for a |
| 253 | * VM_FAULT_NOPAGE result is when device-dax capacity is |
| 254 | * mapped. No need to consider the zero page, or racing |
| 255 | * conflicting mappings. |
| 256 | */ |
Dan Williams | 35de299 | 2018-07-13 21:49:45 -0700 | [diff] [blame] | 257 | pgoff = linear_page_index(vmf->vma, vmf->address |
| 258 | & ~(fault_size - 1)); |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 259 | for (i = 0; i < fault_size / PAGE_SIZE; i++) { |
| 260 | struct page *page; |
| 261 | |
| 262 | page = pfn_to_page(pfn_t_to_pfn(pfn) + i); |
| 263 | if (page->mapping) |
| 264 | continue; |
| 265 | page->mapping = filp->f_mapping; |
Dan Williams | 35de299 | 2018-07-13 21:49:45 -0700 | [diff] [blame] | 266 | page->index = pgoff + i; |
Dan Williams | 2232c63 | 2018-07-13 21:49:40 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 269 | dax_read_unlock(id); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 270 | |
| 271 | return rc; |
| 272 | } |
| 273 | |
Dan Williams | 226ab56 | 2018-07-13 21:49:34 -0700 | [diff] [blame] | 274 | static vm_fault_t dev_dax_fault(struct vm_fault *vmf) |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 275 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 276 | return dev_dax_huge_fault(vmf, PE_SIZE_PTE); |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Dan Williams | 9702cff | 2017-11-29 16:10:32 -0800 | [diff] [blame] | 279 | static int dev_dax_split(struct vm_area_struct *vma, unsigned long addr) |
| 280 | { |
| 281 | struct file *filp = vma->vm_file; |
| 282 | struct dev_dax *dev_dax = filp->private_data; |
| 283 | struct dax_region *dax_region = dev_dax->region; |
| 284 | |
| 285 | if (!IS_ALIGNED(addr, dax_region->align)) |
| 286 | return -EINVAL; |
| 287 | return 0; |
| 288 | } |
| 289 | |
Dan Williams | c1d53b9 | 2018-04-05 16:24:28 -0700 | [diff] [blame] | 290 | static unsigned long dev_dax_pagesize(struct vm_area_struct *vma) |
| 291 | { |
| 292 | struct file *filp = vma->vm_file; |
| 293 | struct dev_dax *dev_dax = filp->private_data; |
| 294 | struct dax_region *dax_region = dev_dax->region; |
| 295 | |
| 296 | return dax_region->align; |
| 297 | } |
| 298 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 299 | static const struct vm_operations_struct dax_vm_ops = { |
| 300 | .fault = dev_dax_fault, |
| 301 | .huge_fault = dev_dax_huge_fault, |
Dan Williams | 9702cff | 2017-11-29 16:10:32 -0800 | [diff] [blame] | 302 | .split = dev_dax_split, |
Dan Williams | c1d53b9 | 2018-04-05 16:24:28 -0700 | [diff] [blame] | 303 | .pagesize = dev_dax_pagesize, |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 304 | }; |
| 305 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 306 | static int dax_mmap(struct file *filp, struct vm_area_struct *vma) |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 307 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 308 | struct dev_dax *dev_dax = filp->private_data; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 309 | int rc, id; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 310 | |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 311 | dev_dbg(&dev_dax->dev, "trace\n"); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 312 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 313 | /* |
| 314 | * We lock to check dax_dev liveness and will re-check at |
| 315 | * fault time. |
| 316 | */ |
| 317 | id = dax_read_lock(); |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 318 | rc = check_vma(dev_dax, vma, __func__); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 319 | dax_read_unlock(id); |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 320 | if (rc) |
| 321 | return rc; |
| 322 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 323 | vma->vm_ops = &dax_vm_ops; |
Dave Jiang | e1fb4a0 | 2018-08-17 15:43:40 -0700 | [diff] [blame] | 324 | vma->vm_flags |= VM_HUGEPAGE; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 325 | return 0; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 326 | } |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 327 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 328 | /* return an unmapped area aligned to the dax region specified alignment */ |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 329 | static unsigned long dax_get_unmapped_area(struct file *filp, |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 330 | unsigned long addr, unsigned long len, unsigned long pgoff, |
| 331 | unsigned long flags) |
| 332 | { |
| 333 | unsigned long off, off_end, off_align, len_align, addr_align, align; |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 334 | struct dev_dax *dev_dax = filp ? filp->private_data : NULL; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 335 | struct dax_region *dax_region; |
| 336 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 337 | if (!dev_dax || addr) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 338 | goto out; |
| 339 | |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 340 | dax_region = dev_dax->region; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 341 | align = dax_region->align; |
| 342 | off = pgoff << PAGE_SHIFT; |
| 343 | off_end = off + len; |
| 344 | off_align = round_up(off, align); |
| 345 | |
| 346 | if ((off_end <= off_align) || ((off_end - off_align) < align)) |
| 347 | goto out; |
| 348 | |
| 349 | len_align = len + align; |
| 350 | if ((off + len_align) < off) |
| 351 | goto out; |
| 352 | |
| 353 | addr_align = current->mm->get_unmapped_area(filp, addr, len_align, |
| 354 | pgoff, flags); |
| 355 | if (!IS_ERR_VALUE(addr_align)) { |
| 356 | addr_align += (off - addr_align) & (align - 1); |
| 357 | return addr_align; |
| 358 | } |
| 359 | out: |
| 360 | return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags); |
| 361 | } |
| 362 | |
Dave Jiang | 41c9b1b | 2018-09-10 16:18:29 -0700 | [diff] [blame] | 363 | static const struct address_space_operations dev_dax_aops = { |
| 364 | .set_page_dirty = noop_set_page_dirty, |
| 365 | .invalidatepage = noop_invalidatepage, |
| 366 | }; |
| 367 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 368 | static int dax_open(struct inode *inode, struct file *filp) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 369 | { |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 370 | struct dax_device *dax_dev = inode_dax(inode); |
| 371 | struct inode *__dax_inode = dax_inode(dax_dev); |
| 372 | struct dev_dax *dev_dax = dax_get_private(dax_dev); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 373 | |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 374 | dev_dbg(&dev_dax->dev, "trace\n"); |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 375 | inode->i_mapping = __dax_inode->i_mapping; |
| 376 | inode->i_mapping->host = __dax_inode; |
Dave Jiang | 41c9b1b | 2018-09-10 16:18:29 -0700 | [diff] [blame] | 377 | inode->i_mapping->a_ops = &dev_dax_aops; |
Dan Williams | 3bc52c4 | 2016-07-24 21:55:45 -0700 | [diff] [blame] | 378 | filp->f_mapping = inode->i_mapping; |
Jeff Layton | 5660e13 | 2017-07-06 07:02:25 -0400 | [diff] [blame] | 379 | filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping); |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 380 | filp->private_data = dev_dax; |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 381 | inode->i_flags = S_DAX; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 382 | |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 383 | return 0; |
| 384 | } |
| 385 | |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 386 | static int dax_release(struct inode *inode, struct file *filp) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 387 | { |
Dan Williams | 5f0694b | 2017-01-30 21:43:10 -0800 | [diff] [blame] | 388 | struct dev_dax *dev_dax = filp->private_data; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 389 | |
Dan Williams | 6daaca5 | 2018-03-05 16:40:05 -0800 | [diff] [blame] | 390 | dev_dbg(&dev_dax->dev, "trace\n"); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 391 | return 0; |
Dan Williams | dee4107 | 2016-05-14 12:20:44 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 394 | static const struct file_operations dax_fops = { |
| 395 | .llseek = noop_llseek, |
| 396 | .owner = THIS_MODULE, |
Dan Williams | af69f51 | 2016-08-11 00:38:03 -0700 | [diff] [blame] | 397 | .open = dax_open, |
| 398 | .release = dax_release, |
| 399 | .get_unmapped_area = dax_get_unmapped_area, |
| 400 | .mmap = dax_mmap, |
Dave Jiang | ef84230 | 2018-04-19 13:39:43 -0700 | [diff] [blame] | 401 | .mmap_supported_flags = MAP_SYNC, |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 402 | }; |
| 403 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 404 | static void dev_dax_cdev_del(void *cdev) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 405 | { |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 406 | cdev_del(cdev); |
Dan Williams | ebd84d7 | 2016-08-11 00:41:51 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 409 | static void dev_dax_kill(void *dev_dax) |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 410 | { |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 411 | kill_dev_dax(dev_dax); |
| 412 | } |
| 413 | |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 414 | int dev_dax_probe(struct device *dev) |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 415 | { |
| 416 | struct dev_dax *dev_dax = to_dev_dax(dev); |
| 417 | struct dax_device *dax_dev = dev_dax->dax_dev; |
Dan Williams | 89ec9f2 | 2018-10-29 15:52:42 -0700 | [diff] [blame] | 418 | struct resource *res = &dev_dax->region->res; |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 419 | struct inode *inode; |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 420 | struct cdev *cdev; |
Dan Williams | 89ec9f2 | 2018-10-29 15:52:42 -0700 | [diff] [blame] | 421 | void *addr; |
Dan Williams | 753a085 | 2017-07-14 13:54:50 -0700 | [diff] [blame] | 422 | int rc; |
Dan Williams | 43fe51e | 2017-07-12 13:42:37 -0700 | [diff] [blame] | 423 | |
Dan Williams | 89ec9f2 | 2018-10-29 15:52:42 -0700 | [diff] [blame] | 424 | /* 1:1 map region resource range to device-dax instance range */ |
| 425 | if (!devm_request_mem_region(dev, res->start, resource_size(res), |
| 426 | dev_name(dev))) { |
| 427 | dev_warn(dev, "could not reserve region %pR\n", res); |
| 428 | return -EBUSY; |
| 429 | } |
| 430 | |
Christoph Hellwig | 3ed2dcd | 2019-06-26 14:27:07 +0200 | [diff] [blame] | 431 | dev_dax->pgmap.type = MEMORY_DEVICE_DEVDAX; |
Dan Williams | 89ec9f2 | 2018-10-29 15:52:42 -0700 | [diff] [blame] | 432 | addr = devm_memremap_pages(dev, &dev_dax->pgmap); |
Dan Williams | 50f44ee | 2019-06-13 15:56:33 -0700 | [diff] [blame] | 433 | if (IS_ERR(addr)) |
Dan Williams | 89ec9f2 | 2018-10-29 15:52:42 -0700 | [diff] [blame] | 434 | return PTR_ERR(addr); |
Dan Williams | 89ec9f2 | 2018-10-29 15:52:42 -0700 | [diff] [blame] | 435 | |
Dan Williams | 7b6be84 | 2017-04-11 09:49:49 -0700 | [diff] [blame] | 436 | inode = dax_inode(dax_dev); |
| 437 | cdev = inode->i_cdev; |
Dan Williams | ba09c01 | 2016-07-24 15:55:42 -0700 | [diff] [blame] | 438 | cdev_init(cdev, &dax_fops); |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 439 | if (dev->class) { |
| 440 | /* for the CONFIG_DEV_DAX_PMEM_COMPAT case */ |
| 441 | cdev->owner = dev->parent->driver->owner; |
| 442 | } else |
| 443 | cdev->owner = dev->driver->owner; |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 444 | cdev_set_parent(cdev, &dev->kobj); |
| 445 | rc = cdev_add(cdev, dev->devt, 1); |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 446 | if (rc) |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 447 | return rc; |
Dan Williams | d76911e | 2016-07-19 17:51:40 -0700 | [diff] [blame] | 448 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 449 | rc = devm_add_action_or_reset(dev, dev_dax_cdev_del, cdev); |
| 450 | if (rc) |
| 451 | return rc; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 452 | |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 453 | run_dax(dax_dev); |
| 454 | return devm_add_action_or_reset(dev, dev_dax_kill, dev_dax); |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 455 | } |
Dan Williams | 730926c | 2017-07-16 13:51:53 -0700 | [diff] [blame] | 456 | EXPORT_SYMBOL_GPL(dev_dax_probe); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 457 | |
| 458 | static int dev_dax_remove(struct device *dev) |
| 459 | { |
| 460 | /* all probe actions are unwound by devm */ |
| 461 | return 0; |
| 462 | } |
| 463 | |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 464 | static struct dax_device_driver device_dax_driver = { |
| 465 | .drv = { |
| 466 | .probe = dev_dax_probe, |
| 467 | .remove = dev_dax_remove, |
| 468 | }, |
| 469 | .match_always = 1, |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 470 | }; |
Dan Williams | 043a925 | 2016-08-07 08:23:56 -0700 | [diff] [blame] | 471 | |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 472 | static int __init dax_init(void) |
| 473 | { |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 474 | return dax_driver_register(&device_dax_driver); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static void __exit dax_exit(void) |
| 478 | { |
Dan Williams | d200781 | 2018-11-07 15:31:23 -0800 | [diff] [blame] | 479 | dax_driver_unregister(&device_dax_driver); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | MODULE_AUTHOR("Intel Corporation"); |
| 483 | MODULE_LICENSE("GPL v2"); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 484 | module_init(dax_init); |
Dan Williams | ab68f26 | 2016-05-18 09:15:08 -0700 | [diff] [blame] | 485 | module_exit(dax_exit); |
Dan Williams | 9567da0 | 2017-07-12 17:58:21 -0700 | [diff] [blame] | 486 | MODULE_ALIAS_DAX_DEVICE(0); |