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