blob: 996d68ff992a88bf63d9f6ca1a6a617a83b5be6d [file] [log] [blame]
Dan Williams51cf7842017-07-12 17:58:21 -07001// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 2016-2018 Intel Corporation. All rights reserved. */
Dan Williams89ec9f22018-10-29 15:52:42 -07003#include <linux/memremap.h>
Dan Williamsab68f262016-05-18 09:15:08 -07004#include <linux/pagemap.h>
5#include <linux/module.h>
6#include <linux/device.h>
7#include <linux/pfn_t.h>
Dan Williamsba09c012016-07-24 15:55:42 -07008#include <linux/cdev.h>
Dan Williamsab68f262016-05-18 09:15:08 -07009#include <linux/slab.h>
10#include <linux/dax.h>
11#include <linux/fs.h>
12#include <linux/mm.h>
Dave Jiangef842302018-04-19 13:39:43 -070013#include <linux/mman.h>
Dan Williams73616362017-05-04 23:38:43 -070014#include "dax-private.h"
Dan Williams51cf7842017-07-12 17:58:21 -070015#include "bus.h"
Dan Williamsab68f262016-05-18 09:15:08 -070016
Dan Williams89ec9f22018-10-29 15:52:42 -070017static struct dev_dax *ref_to_dev_dax(struct percpu_ref *ref)
18{
19 return container_of(ref, struct dev_dax, ref);
20}
21
22static 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
30static 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
40static 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 Williams5f0694b2017-01-30 21:43:10 -080049static int check_vma(struct dev_dax *dev_dax, struct vm_area_struct *vma,
Dan Williamsdee41072016-05-14 12:20:44 -070050 const char *func)
51{
Dan Williams5f0694b2017-01-30 21:43:10 -080052 struct dax_region *dax_region = dev_dax->region;
53 struct device *dev = &dev_dax->dev;
Dan Williamsdee41072016-05-14 12:20:44 -070054 unsigned long mask;
55
Dan Williams7b6be842017-04-11 09:49:49 -070056 if (!dax_alive(dev_dax->dax_dev))
Dan Williamsdee41072016-05-14 12:20:44 -070057 return -ENXIO;
58
Dan Williams4cb19352016-11-16 09:00:38 -080059 /* prevent private mappings from being established */
Dan Williams325896f2016-12-06 17:03:35 -080060 if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) {
Jeff Moyer5a14e912018-06-27 11:43:58 -040061 dev_info_ratelimited(dev,
62 "%s: %s: fail, attempted private mapping\n",
Dan Williamsdee41072016-05-14 12:20:44 -070063 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 Moyer5a14e912018-06-27 11:43:58 -040069 dev_info_ratelimited(dev,
70 "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n",
Dan Williamsdee41072016-05-14 12:20:44 -070071 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 Moyer5a14e912018-06-27 11:43:58 -040078 dev_info_ratelimited(dev,
79 "%s: %s: fail, dax range requires MADV_DONTFORK\n",
Dan Williamsdee41072016-05-14 12:20:44 -070080 current->comm, func);
81 return -EINVAL;
82 }
83
84 if (!vma_is_dax(vma)) {
Jeff Moyer5a14e912018-06-27 11:43:58 -040085 dev_info_ratelimited(dev,
86 "%s: %s: fail, vma is not DAX capable\n",
Dan Williamsdee41072016-05-14 12:20:44 -070087 current->comm, func);
88 return -EINVAL;
89 }
90
91 return 0;
92}
93
Dan Williams73616362017-05-04 23:38:43 -070094/* 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 Williamsdee41072016-05-14 12:20:44 -070096 unsigned long size)
97{
Dan Williams753a0852017-07-14 13:54:50 -070098 struct resource *res = &dev_dax->region->res;
99 phys_addr_t phys;
Dan Williamsdee41072016-05-14 12:20:44 -0700100
Dan Williams753a0852017-07-14 13:54:50 -0700101 phys = pgoff * PAGE_SIZE + res->start;
102 if (phys >= res->start && phys <= res->end) {
Dan Williamsdee41072016-05-14 12:20:44 -0700103 if (phys + size - 1 <= res->end)
104 return phys;
105 }
106
107 return -1;
108}
109
Dan Williams226ab562018-07-13 21:49:34 -0700110static vm_fault_t __dev_dax_pte_fault(struct dev_dax *dev_dax,
Dan Williams2232c632018-07-13 21:49:40 -0700111 struct vm_fault *vmf, pfn_t *pfn)
Dan Williamsdee41072016-05-14 12:20:44 -0700112{
Dan Williams5f0694b2017-01-30 21:43:10 -0800113 struct device *dev = &dev_dax->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700114 struct dax_region *dax_region;
Dan Williamsdee41072016-05-14 12:20:44 -0700115 phys_addr_t phys;
Dave Jiang0134ed42017-03-10 13:24:22 -0700116 unsigned int fault_size = PAGE_SIZE;
Dan Williamsdee41072016-05-14 12:20:44 -0700117
Dan Williams5f0694b2017-01-30 21:43:10 -0800118 if (check_vma(dev_dax, vmf->vma, __func__))
Dan Williamsdee41072016-05-14 12:20:44 -0700119 return VM_FAULT_SIGBUS;
120
Dan Williams5f0694b2017-01-30 21:43:10 -0800121 dax_region = dev_dax->region;
Dan Williamsdee41072016-05-14 12:20:44 -0700122 if (dax_region->align > PAGE_SIZE) {
Dan Williams6daaca52018-03-05 16:40:05 -0800123 dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
124 dax_region->align, fault_size);
Dan Williamsdee41072016-05-14 12:20:44 -0700125 return VM_FAULT_SIGBUS;
126 }
127
Dave Jiang0134ed42017-03-10 13:24:22 -0700128 if (fault_size != dax_region->align)
129 return VM_FAULT_SIGBUS;
130
Dan Williams73616362017-05-04 23:38:43 -0700131 phys = dax_pgoff_to_phys(dev_dax, vmf->pgoff, PAGE_SIZE);
Dan Williamsdee41072016-05-14 12:20:44 -0700132 if (phys == -1) {
Dan Williams6daaca52018-03-05 16:40:05 -0800133 dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", vmf->pgoff);
Dan Williamsdee41072016-05-14 12:20:44 -0700134 return VM_FAULT_SIGBUS;
135 }
136
Dan Williams2232c632018-07-13 21:49:40 -0700137 *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
Dan Williamsdee41072016-05-14 12:20:44 -0700138
Dan Williams2232c632018-07-13 21:49:40 -0700139 return vmf_insert_mixed(vmf->vma, vmf->address, *pfn);
Dan Williamsdee41072016-05-14 12:20:44 -0700140}
141
Dan Williams226ab562018-07-13 21:49:34 -0700142static vm_fault_t __dev_dax_pmd_fault(struct dev_dax *dev_dax,
Dan Williams2232c632018-07-13 21:49:40 -0700143 struct vm_fault *vmf, pfn_t *pfn)
Dan Williamsdee41072016-05-14 12:20:44 -0700144{
Dave Jiangd8a849e2017-02-22 15:40:03 -0800145 unsigned long pmd_addr = vmf->address & PMD_MASK;
Dan Williams5f0694b2017-01-30 21:43:10 -0800146 struct device *dev = &dev_dax->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700147 struct dax_region *dax_region;
148 phys_addr_t phys;
149 pgoff_t pgoff;
Dave Jiang0134ed42017-03-10 13:24:22 -0700150 unsigned int fault_size = PMD_SIZE;
Dan Williamsdee41072016-05-14 12:20:44 -0700151
Dan Williams5f0694b2017-01-30 21:43:10 -0800152 if (check_vma(dev_dax, vmf->vma, __func__))
Dan Williamsdee41072016-05-14 12:20:44 -0700153 return VM_FAULT_SIGBUS;
154
Dan Williams5f0694b2017-01-30 21:43:10 -0800155 dax_region = dev_dax->region;
Dan Williamsdee41072016-05-14 12:20:44 -0700156 if (dax_region->align > PMD_SIZE) {
Dan Williams6daaca52018-03-05 16:40:05 -0800157 dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
158 dax_region->align, fault_size);
Dan Williamsdee41072016-05-14 12:20:44 -0700159 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 Williams6daaca52018-03-05 16:40:05 -0800164 dev_dbg(dev, "region lacks devmap flags\n");
Dan Williamsdee41072016-05-14 12:20:44 -0700165 return VM_FAULT_SIGBUS;
166 }
167
Dave Jiang0134ed42017-03-10 13:24:22 -0700168 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 Jiangf4200392017-02-22 15:40:06 -0800178 pgoff = linear_page_index(vmf->vma, pmd_addr);
Dan Williams73616362017-05-04 23:38:43 -0700179 phys = dax_pgoff_to_phys(dev_dax, pgoff, PMD_SIZE);
Dan Williamsdee41072016-05-14 12:20:44 -0700180 if (phys == -1) {
Dan Williams6daaca52018-03-05 16:40:05 -0800181 dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff);
Dan Williamsdee41072016-05-14 12:20:44 -0700182 return VM_FAULT_SIGBUS;
183 }
184
Dan Williams2232c632018-07-13 21:49:40 -0700185 *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
Dan Williamsdee41072016-05-14 12:20:44 -0700186
Dan Williamsfce86ff2019-05-13 17:15:33 -0700187 return vmf_insert_pfn_pmd(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE);
Dan Williamsdee41072016-05-14 12:20:44 -0700188}
189
Dave Jiang9557fee2017-02-24 14:57:05 -0800190#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Dan Williams226ab562018-07-13 21:49:34 -0700191static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax,
Dan Williams2232c632018-07-13 21:49:40 -0700192 struct vm_fault *vmf, pfn_t *pfn)
Dave Jiang9557fee2017-02-24 14:57:05 -0800193{
194 unsigned long pud_addr = vmf->address & PUD_MASK;
Dan Williams5f0694b2017-01-30 21:43:10 -0800195 struct device *dev = &dev_dax->dev;
Dave Jiang9557fee2017-02-24 14:57:05 -0800196 struct dax_region *dax_region;
197 phys_addr_t phys;
198 pgoff_t pgoff;
Dave Jiang70b085b2017-03-10 13:24:27 -0700199 unsigned int fault_size = PUD_SIZE;
200
Dave Jiang9557fee2017-02-24 14:57:05 -0800201
Dan Williams5f0694b2017-01-30 21:43:10 -0800202 if (check_vma(dev_dax, vmf->vma, __func__))
Dave Jiang9557fee2017-02-24 14:57:05 -0800203 return VM_FAULT_SIGBUS;
204
Dan Williams5f0694b2017-01-30 21:43:10 -0800205 dax_region = dev_dax->region;
Dave Jiang9557fee2017-02-24 14:57:05 -0800206 if (dax_region->align > PUD_SIZE) {
Dan Williams6daaca52018-03-05 16:40:05 -0800207 dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
208 dax_region->align, fault_size);
Dave Jiang9557fee2017-02-24 14:57:05 -0800209 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 Williams6daaca52018-03-05 16:40:05 -0800214 dev_dbg(dev, "region lacks devmap flags\n");
Dave Jiang9557fee2017-02-24 14:57:05 -0800215 return VM_FAULT_SIGBUS;
216 }
217
Dave Jiang70b085b2017-03-10 13:24:27 -0700218 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 Jiang9557fee2017-02-24 14:57:05 -0800228 pgoff = linear_page_index(vmf->vma, pud_addr);
Dan Williams73616362017-05-04 23:38:43 -0700229 phys = dax_pgoff_to_phys(dev_dax, pgoff, PUD_SIZE);
Dave Jiang9557fee2017-02-24 14:57:05 -0800230 if (phys == -1) {
Dan Williams6daaca52018-03-05 16:40:05 -0800231 dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff);
Dave Jiang9557fee2017-02-24 14:57:05 -0800232 return VM_FAULT_SIGBUS;
233 }
234
Dan Williams2232c632018-07-13 21:49:40 -0700235 *pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
Dave Jiang9557fee2017-02-24 14:57:05 -0800236
Dan Williamsfce86ff2019-05-13 17:15:33 -0700237 return vmf_insert_pfn_pud(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE);
Dave Jiang9557fee2017-02-24 14:57:05 -0800238}
239#else
Dan Williams226ab562018-07-13 21:49:34 -0700240static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax,
Dan Williams2232c632018-07-13 21:49:40 -0700241 struct vm_fault *vmf, pfn_t *pfn)
Dave Jiang9557fee2017-02-24 14:57:05 -0800242{
243 return VM_FAULT_FALLBACK;
244}
245#endif /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
246
Dan Williams226ab562018-07-13 21:49:34 -0700247static vm_fault_t dev_dax_huge_fault(struct vm_fault *vmf,
Dave Jiangc791ace2017-02-24 14:57:08 -0800248 enum page_entry_size pe_size)
Dan Williamsdee41072016-05-14 12:20:44 -0700249{
Dave Jiangf4200392017-02-22 15:40:06 -0800250 struct file *filp = vmf->vma->vm_file;
Dan Williams2232c632018-07-13 21:49:40 -0700251 unsigned long fault_size;
Souptick Joarder36bdac12018-09-04 15:46:26 -0700252 vm_fault_t rc = VM_FAULT_SIGBUS;
253 int id;
Dan Williams2232c632018-07-13 21:49:40 -0700254 pfn_t pfn;
Dan Williams5f0694b2017-01-30 21:43:10 -0800255 struct dev_dax *dev_dax = filp->private_data;
Dan Williamsdee41072016-05-14 12:20:44 -0700256
Dan Williams6daaca52018-03-05 16:40:05 -0800257 dev_dbg(&dev_dax->dev, "%s: %s (%#lx - %#lx) size = %d\n", current->comm,
258 (vmf->flags & FAULT_FLAG_WRITE) ? "write" : "read",
Oliver O'Halloran76202622017-04-12 01:59:36 +1000259 vmf->vma->vm_start, vmf->vma->vm_end, pe_size);
Dan Williamsdee41072016-05-14 12:20:44 -0700260
Dan Williams7b6be842017-04-11 09:49:49 -0700261 id = dax_read_lock();
Dave Jiangc791ace2017-02-24 14:57:08 -0800262 switch (pe_size) {
263 case PE_SIZE_PTE:
Dan Williams2232c632018-07-13 21:49:40 -0700264 fault_size = PAGE_SIZE;
265 rc = __dev_dax_pte_fault(dev_dax, vmf, &pfn);
Dave Jianga2d58162017-02-24 14:56:59 -0800266 break;
Dave Jiangc791ace2017-02-24 14:57:08 -0800267 case PE_SIZE_PMD:
Dan Williams2232c632018-07-13 21:49:40 -0700268 fault_size = PMD_SIZE;
269 rc = __dev_dax_pmd_fault(dev_dax, vmf, &pfn);
Dave Jianga2d58162017-02-24 14:56:59 -0800270 break;
Dave Jiangc791ace2017-02-24 14:57:08 -0800271 case PE_SIZE_PUD:
Dan Williams2232c632018-07-13 21:49:40 -0700272 fault_size = PUD_SIZE;
273 rc = __dev_dax_pud_fault(dev_dax, vmf, &pfn);
Dave Jiang9557fee2017-02-24 14:57:05 -0800274 break;
Dave Jianga2d58162017-02-24 14:56:59 -0800275 default:
Pushkar Jambhlekar54eafcc2017-04-11 09:12:25 -0700276 rc = VM_FAULT_SIGBUS;
Dave Jianga2d58162017-02-24 14:56:59 -0800277 }
Dan Williams2232c632018-07-13 21:49:40 -0700278
279 if (rc == VM_FAULT_NOPAGE) {
280 unsigned long i;
Dan Williams35de2992018-07-13 21:49:45 -0700281 pgoff_t pgoff;
Dan Williams2232c632018-07-13 21:49:40 -0700282
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 Williams35de2992018-07-13 21:49:45 -0700289 pgoff = linear_page_index(vmf->vma, vmf->address
290 & ~(fault_size - 1));
Dan Williams2232c632018-07-13 21:49:40 -0700291 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 Williams35de2992018-07-13 21:49:45 -0700298 page->index = pgoff + i;
Dan Williams2232c632018-07-13 21:49:40 -0700299 }
300 }
Dan Williams7b6be842017-04-11 09:49:49 -0700301 dax_read_unlock(id);
Dan Williamsdee41072016-05-14 12:20:44 -0700302
303 return rc;
304}
305
Dan Williams226ab562018-07-13 21:49:34 -0700306static vm_fault_t dev_dax_fault(struct vm_fault *vmf)
Dave Jiangc791ace2017-02-24 14:57:08 -0800307{
Dan Williams5f0694b2017-01-30 21:43:10 -0800308 return dev_dax_huge_fault(vmf, PE_SIZE_PTE);
Dave Jiangc791ace2017-02-24 14:57:08 -0800309}
310
Dan Williams9702cff2017-11-29 16:10:32 -0800311static 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 Williamsc1d53b92018-04-05 16:24:28 -0700322static 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 Williams5f0694b2017-01-30 21:43:10 -0800331static const struct vm_operations_struct dax_vm_ops = {
332 .fault = dev_dax_fault,
333 .huge_fault = dev_dax_huge_fault,
Dan Williams9702cff2017-11-29 16:10:32 -0800334 .split = dev_dax_split,
Dan Williamsc1d53b92018-04-05 16:24:28 -0700335 .pagesize = dev_dax_pagesize,
Dan Williamsdee41072016-05-14 12:20:44 -0700336};
337
Dan Williamsaf69f512016-08-11 00:38:03 -0700338static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
Dan Williamsdee41072016-05-14 12:20:44 -0700339{
Dan Williams5f0694b2017-01-30 21:43:10 -0800340 struct dev_dax *dev_dax = filp->private_data;
Dan Williams7b6be842017-04-11 09:49:49 -0700341 int rc, id;
Dan Williamsdee41072016-05-14 12:20:44 -0700342
Dan Williams6daaca52018-03-05 16:40:05 -0800343 dev_dbg(&dev_dax->dev, "trace\n");
Dan Williamsdee41072016-05-14 12:20:44 -0700344
Dan Williams7b6be842017-04-11 09:49:49 -0700345 /*
346 * We lock to check dax_dev liveness and will re-check at
347 * fault time.
348 */
349 id = dax_read_lock();
Dan Williams5f0694b2017-01-30 21:43:10 -0800350 rc = check_vma(dev_dax, vma, __func__);
Dan Williams7b6be842017-04-11 09:49:49 -0700351 dax_read_unlock(id);
Dan Williamsdee41072016-05-14 12:20:44 -0700352 if (rc)
353 return rc;
354
Dan Williams5f0694b2017-01-30 21:43:10 -0800355 vma->vm_ops = &dax_vm_ops;
Dave Jiange1fb4a02018-08-17 15:43:40 -0700356 vma->vm_flags |= VM_HUGEPAGE;
Dan Williamsdee41072016-05-14 12:20:44 -0700357 return 0;
Dan Williams043a9252016-08-07 08:23:56 -0700358}
Dan Williamsdee41072016-05-14 12:20:44 -0700359
Dan Williams043a9252016-08-07 08:23:56 -0700360/* return an unmapped area aligned to the dax region specified alignment */
Dan Williamsaf69f512016-08-11 00:38:03 -0700361static unsigned long dax_get_unmapped_area(struct file *filp,
Dan Williams043a9252016-08-07 08:23:56 -0700362 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 Williams5f0694b2017-01-30 21:43:10 -0800366 struct dev_dax *dev_dax = filp ? filp->private_data : NULL;
Dan Williams043a9252016-08-07 08:23:56 -0700367 struct dax_region *dax_region;
368
Dan Williams5f0694b2017-01-30 21:43:10 -0800369 if (!dev_dax || addr)
Dan Williams043a9252016-08-07 08:23:56 -0700370 goto out;
371
Dan Williams5f0694b2017-01-30 21:43:10 -0800372 dax_region = dev_dax->region;
Dan Williams043a9252016-08-07 08:23:56 -0700373 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 Jiang41c9b1b2018-09-10 16:18:29 -0700395static const struct address_space_operations dev_dax_aops = {
396 .set_page_dirty = noop_set_page_dirty,
397 .invalidatepage = noop_invalidatepage,
398};
399
Dan Williamsaf69f512016-08-11 00:38:03 -0700400static int dax_open(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700401{
Dan Williams7b6be842017-04-11 09:49:49 -0700402 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 Williams043a9252016-08-07 08:23:56 -0700405
Dan Williams6daaca52018-03-05 16:40:05 -0800406 dev_dbg(&dev_dax->dev, "trace\n");
Dan Williams7b6be842017-04-11 09:49:49 -0700407 inode->i_mapping = __dax_inode->i_mapping;
408 inode->i_mapping->host = __dax_inode;
Dave Jiang41c9b1b2018-09-10 16:18:29 -0700409 inode->i_mapping->a_ops = &dev_dax_aops;
Dan Williams3bc52c42016-07-24 21:55:45 -0700410 filp->f_mapping = inode->i_mapping;
Jeff Layton5660e132017-07-06 07:02:25 -0400411 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
Dan Williams5f0694b2017-01-30 21:43:10 -0800412 filp->private_data = dev_dax;
Dan Williamsebd84d72016-08-11 00:41:51 -0700413 inode->i_flags = S_DAX;
Dan Williams043a9252016-08-07 08:23:56 -0700414
Dan Williams043a9252016-08-07 08:23:56 -0700415 return 0;
416}
417
Dan Williamsaf69f512016-08-11 00:38:03 -0700418static int dax_release(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700419{
Dan Williams5f0694b2017-01-30 21:43:10 -0800420 struct dev_dax *dev_dax = filp->private_data;
Dan Williams043a9252016-08-07 08:23:56 -0700421
Dan Williams6daaca52018-03-05 16:40:05 -0800422 dev_dbg(&dev_dax->dev, "trace\n");
Dan Williams043a9252016-08-07 08:23:56 -0700423 return 0;
Dan Williamsdee41072016-05-14 12:20:44 -0700424}
425
Dan Williamsab68f262016-05-18 09:15:08 -0700426static const struct file_operations dax_fops = {
427 .llseek = noop_llseek,
428 .owner = THIS_MODULE,
Dan Williamsaf69f512016-08-11 00:38:03 -0700429 .open = dax_open,
430 .release = dax_release,
431 .get_unmapped_area = dax_get_unmapped_area,
432 .mmap = dax_mmap,
Dave Jiangef842302018-04-19 13:39:43 -0700433 .mmap_supported_flags = MAP_SYNC,
Dan Williamsab68f262016-05-18 09:15:08 -0700434};
435
Dan Williams9567da02017-07-12 17:58:21 -0700436static void dev_dax_cdev_del(void *cdev)
Dan Williams043a9252016-08-07 08:23:56 -0700437{
Dan Williams9567da02017-07-12 17:58:21 -0700438 cdev_del(cdev);
Dan Williamsebd84d72016-08-11 00:41:51 -0700439}
440
Dan Williams9567da02017-07-12 17:58:21 -0700441static void dev_dax_kill(void *dev_dax)
Dan Williams043a9252016-08-07 08:23:56 -0700442{
Dan Williams9567da02017-07-12 17:58:21 -0700443 kill_dev_dax(dev_dax);
444}
445
Dan Williams730926c2017-07-16 13:51:53 -0700446int dev_dax_probe(struct device *dev)
Dan Williams9567da02017-07-12 17:58:21 -0700447{
448 struct dev_dax *dev_dax = to_dev_dax(dev);
449 struct dax_device *dax_dev = dev_dax->dax_dev;
Dan Williams89ec9f22018-10-29 15:52:42 -0700450 struct resource *res = &dev_dax->region->res;
Dan Williams7b6be842017-04-11 09:49:49 -0700451 struct inode *inode;
Dan Williamsba09c012016-07-24 15:55:42 -0700452 struct cdev *cdev;
Dan Williams89ec9f22018-10-29 15:52:42 -0700453 void *addr;
Dan Williams753a0852017-07-14 13:54:50 -0700454 int rc;
Dan Williams43fe51e2017-07-12 13:42:37 -0700455
Dan Williams89ec9f22018-10-29 15:52:42 -0700456 /* 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 Williams7b6be842017-04-11 09:49:49 -0700482 inode = dax_inode(dax_dev);
483 cdev = inode->i_cdev;
Dan Williamsba09c012016-07-24 15:55:42 -0700484 cdev_init(cdev, &dax_fops);
Dan Williams730926c2017-07-16 13:51:53 -0700485 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 Williams9567da02017-07-12 17:58:21 -0700490 cdev_set_parent(cdev, &dev->kobj);
491 rc = cdev_add(cdev, dev->devt, 1);
Dan Williamsd76911e2016-07-19 17:51:40 -0700492 if (rc)
Dan Williams9567da02017-07-12 17:58:21 -0700493 return rc;
Dan Williamsd76911e2016-07-19 17:51:40 -0700494
Dan Williams9567da02017-07-12 17:58:21 -0700495 rc = devm_add_action_or_reset(dev, dev_dax_cdev_del, cdev);
496 if (rc)
497 return rc;
Dan Williams043a9252016-08-07 08:23:56 -0700498
Dan Williams9567da02017-07-12 17:58:21 -0700499 run_dax(dax_dev);
500 return devm_add_action_or_reset(dev, dev_dax_kill, dev_dax);
Dan Williams043a9252016-08-07 08:23:56 -0700501}
Dan Williams730926c2017-07-16 13:51:53 -0700502EXPORT_SYMBOL_GPL(dev_dax_probe);
Dan Williams9567da02017-07-12 17:58:21 -0700503
504static int dev_dax_remove(struct device *dev)
505{
506 /* all probe actions are unwound by devm */
507 return 0;
508}
509
Dan Williamsd2007812018-11-07 15:31:23 -0800510static 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 Williams9567da02017-07-12 17:58:21 -0700516};
Dan Williams043a9252016-08-07 08:23:56 -0700517
Dan Williamsab68f262016-05-18 09:15:08 -0700518static int __init dax_init(void)
519{
Dan Williams9567da02017-07-12 17:58:21 -0700520 return dax_driver_register(&device_dax_driver);
Dan Williamsab68f262016-05-18 09:15:08 -0700521}
522
523static void __exit dax_exit(void)
524{
Dan Williamsd2007812018-11-07 15:31:23 -0800525 dax_driver_unregister(&device_dax_driver);
Dan Williamsab68f262016-05-18 09:15:08 -0700526}
527
528MODULE_AUTHOR("Intel Corporation");
529MODULE_LICENSE("GPL v2");
Dan Williams9567da02017-07-12 17:58:21 -0700530module_init(dax_init);
Dan Williamsab68f262016-05-18 09:15:08 -0700531module_exit(dax_exit);
Dan Williams9567da02017-07-12 17:58:21 -0700532MODULE_ALIAS_DAX_DEVICE(0);