blob: db68f4fa8ce0e2b25190257c9c407b4803fb5828 [file] [log] [blame]
Dan Williamsab68f262016-05-18 09:15:08 -07001/*
Dan Williams7b6be842017-04-11 09:49:49 -07002 * Copyright(c) 2016 - 2017 Intel Corporation. All rights reserved.
Dan Williamsab68f262016-05-18 09:15:08 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/pagemap.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/pfn_t.h>
Dan Williamsba09c012016-07-24 15:55:42 -070017#include <linux/cdev.h>
Dan Williamsab68f262016-05-18 09:15:08 -070018#include <linux/slab.h>
19#include <linux/dax.h>
20#include <linux/fs.h>
21#include <linux/mm.h>
Dan Williamsccdb07f2016-08-06 16:05:06 -070022#include "dax.h"
Dan Williamsab68f262016-05-18 09:15:08 -070023
Dan Williamsab68f262016-05-18 09:15:08 -070024static struct class *dax_class;
Dan Williamsab68f262016-05-18 09:15:08 -070025
26/**
27 * struct dax_region - mapping infrastructure for dax devices
28 * @id: kernel-wide unique region for a memory range
29 * @base: linear address corresponding to @res
30 * @kref: to pin while other agents have a need to do lookups
31 * @dev: parent device backing this region
32 * @align: allocation and mapping alignment for child dax devices
33 * @res: physical address range of the region
34 * @pfn_flags: identify whether the pfns are paged back or not
35 */
36struct dax_region {
37 int id;
38 struct ida ida;
39 void *base;
40 struct kref kref;
41 struct device *dev;
42 unsigned int align;
43 struct resource res;
44 unsigned long pfn_flags;
45};
46
47/**
Dan Williams5f0694b2017-01-30 21:43:10 -080048 * struct dev_dax - instance data for a subdivision of a dax region
Dan Williamsab68f262016-05-18 09:15:08 -070049 * @region - parent region
Dan Williams7b6be842017-04-11 09:49:49 -070050 * @dax_dev - core dax functionality
51 * @dev - device core
Dan Williamsab68f262016-05-18 09:15:08 -070052 * @id - child id in the region
53 * @num_resources - number of physical address extents in this device
54 * @res - array of physical address ranges
55 */
Dan Williams5f0694b2017-01-30 21:43:10 -080056struct dev_dax {
Dan Williamsab68f262016-05-18 09:15:08 -070057 struct dax_region *region;
Dan Williams7b6be842017-04-11 09:49:49 -070058 struct dax_device *dax_dev;
Dan Williamsebd84d72016-08-11 00:41:51 -070059 struct device dev;
Dan Williamsab68f262016-05-18 09:15:08 -070060 int id;
61 int num_resources;
62 struct resource res[0];
63};
64
Dan Williamsd7fe1a62016-12-17 14:50:04 -080065static ssize_t id_show(struct device *dev,
66 struct device_attribute *attr, char *buf)
67{
68 struct dax_region *dax_region;
69 ssize_t rc = -ENXIO;
70
71 device_lock(dev);
72 dax_region = dev_get_drvdata(dev);
73 if (dax_region)
74 rc = sprintf(buf, "%d\n", dax_region->id);
75 device_unlock(dev);
76
77 return rc;
78}
79static DEVICE_ATTR_RO(id);
80
81static ssize_t region_size_show(struct device *dev,
82 struct device_attribute *attr, char *buf)
83{
84 struct dax_region *dax_region;
85 ssize_t rc = -ENXIO;
86
87 device_lock(dev);
88 dax_region = dev_get_drvdata(dev);
89 if (dax_region)
90 rc = sprintf(buf, "%llu\n", (unsigned long long)
91 resource_size(&dax_region->res));
92 device_unlock(dev);
93
94 return rc;
95}
96static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
97 region_size_show, NULL);
98
99static ssize_t align_show(struct device *dev,
100 struct device_attribute *attr, char *buf)
101{
102 struct dax_region *dax_region;
103 ssize_t rc = -ENXIO;
104
105 device_lock(dev);
106 dax_region = dev_get_drvdata(dev);
107 if (dax_region)
108 rc = sprintf(buf, "%u\n", dax_region->align);
109 device_unlock(dev);
110
111 return rc;
112}
113static DEVICE_ATTR_RO(align);
114
115static struct attribute *dax_region_attributes[] = {
116 &dev_attr_region_size.attr,
117 &dev_attr_align.attr,
118 &dev_attr_id.attr,
119 NULL,
120};
121
122static const struct attribute_group dax_region_attribute_group = {
123 .name = "dax_region",
124 .attrs = dax_region_attributes,
125};
126
127static const struct attribute_group *dax_region_attribute_groups[] = {
128 &dax_region_attribute_group,
129 NULL,
130};
131
Dan Williamsab68f262016-05-18 09:15:08 -0700132static void dax_region_free(struct kref *kref)
133{
134 struct dax_region *dax_region;
135
136 dax_region = container_of(kref, struct dax_region, kref);
137 kfree(dax_region);
138}
139
140void dax_region_put(struct dax_region *dax_region)
141{
142 kref_put(&dax_region->kref, dax_region_free);
143}
144EXPORT_SYMBOL_GPL(dax_region_put);
145
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800146static void dax_region_unregister(void *region)
147{
148 struct dax_region *dax_region = region;
149
150 sysfs_remove_groups(&dax_region->dev->kobj,
151 dax_region_attribute_groups);
152 dax_region_put(dax_region);
153}
154
Dan Williamsab68f262016-05-18 09:15:08 -0700155struct dax_region *alloc_dax_region(struct device *parent, int region_id,
156 struct resource *res, unsigned int align, void *addr,
157 unsigned long pfn_flags)
158{
159 struct dax_region *dax_region;
160
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800161 /*
162 * The DAX core assumes that it can store its private data in
163 * parent->driver_data. This WARN is a reminder / safeguard for
164 * developers of device-dax drivers.
165 */
166 if (dev_get_drvdata(parent)) {
167 dev_WARN(parent, "dax core failed to setup private data\n");
168 return NULL;
169 }
170
Dan Williams9d2d01a2016-07-19 16:17:58 -0700171 if (!IS_ALIGNED(res->start, align)
172 || !IS_ALIGNED(resource_size(res), align))
173 return NULL;
Dan Williamsab68f262016-05-18 09:15:08 -0700174
Dan Williams9d2d01a2016-07-19 16:17:58 -0700175 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
Dan Williamsab68f262016-05-18 09:15:08 -0700176 if (!dax_region)
177 return NULL;
178
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800179 dev_set_drvdata(parent, dax_region);
Dan Williamsab68f262016-05-18 09:15:08 -0700180 memcpy(&dax_region->res, res, sizeof(*res));
181 dax_region->pfn_flags = pfn_flags;
182 kref_init(&dax_region->kref);
183 dax_region->id = region_id;
184 ida_init(&dax_region->ida);
185 dax_region->align = align;
186 dax_region->dev = parent;
187 dax_region->base = addr;
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800188 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
189 kfree(dax_region);
190 return NULL;;
191 }
Dan Williamsab68f262016-05-18 09:15:08 -0700192
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800193 kref_get(&dax_region->kref);
194 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
195 return NULL;
Dan Williamsab68f262016-05-18 09:15:08 -0700196 return dax_region;
197}
198EXPORT_SYMBOL_GPL(alloc_dax_region);
199
Dan Williams5f0694b2017-01-30 21:43:10 -0800200static struct dev_dax *to_dev_dax(struct device *dev)
Dan Williamsebd84d72016-08-11 00:41:51 -0700201{
Dan Williams5f0694b2017-01-30 21:43:10 -0800202 return container_of(dev, struct dev_dax, dev);
Dan Williamsebd84d72016-08-11 00:41:51 -0700203}
204
Dan Williamsab68f262016-05-18 09:15:08 -0700205static ssize_t size_show(struct device *dev,
206 struct device_attribute *attr, char *buf)
207{
Dan Williams5f0694b2017-01-30 21:43:10 -0800208 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williamsab68f262016-05-18 09:15:08 -0700209 unsigned long long size = 0;
210 int i;
211
Dan Williams5f0694b2017-01-30 21:43:10 -0800212 for (i = 0; i < dev_dax->num_resources; i++)
213 size += resource_size(&dev_dax->res[i]);
Dan Williamsab68f262016-05-18 09:15:08 -0700214
215 return sprintf(buf, "%llu\n", size);
216}
217static DEVICE_ATTR_RO(size);
218
Dan Williams5f0694b2017-01-30 21:43:10 -0800219static struct attribute *dev_dax_attributes[] = {
Dan Williamsab68f262016-05-18 09:15:08 -0700220 &dev_attr_size.attr,
221 NULL,
222};
223
Dan Williams5f0694b2017-01-30 21:43:10 -0800224static const struct attribute_group dev_dax_attribute_group = {
225 .attrs = dev_dax_attributes,
Dan Williamsab68f262016-05-18 09:15:08 -0700226};
227
228static const struct attribute_group *dax_attribute_groups[] = {
Dan Williams5f0694b2017-01-30 21:43:10 -0800229 &dev_dax_attribute_group,
Dan Williamsab68f262016-05-18 09:15:08 -0700230 NULL,
231};
232
Dan Williams5f0694b2017-01-30 21:43:10 -0800233static int check_vma(struct dev_dax *dev_dax, struct vm_area_struct *vma,
Dan Williamsdee41072016-05-14 12:20:44 -0700234 const char *func)
235{
Dan Williams5f0694b2017-01-30 21:43:10 -0800236 struct dax_region *dax_region = dev_dax->region;
237 struct device *dev = &dev_dax->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700238 unsigned long mask;
239
Dan Williams7b6be842017-04-11 09:49:49 -0700240 if (!dax_alive(dev_dax->dax_dev))
Dan Williamsdee41072016-05-14 12:20:44 -0700241 return -ENXIO;
242
Dan Williams4cb19352016-11-16 09:00:38 -0800243 /* prevent private mappings from being established */
Dan Williams325896f2016-12-06 17:03:35 -0800244 if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) {
Dan Williamsdee41072016-05-14 12:20:44 -0700245 dev_info(dev, "%s: %s: fail, attempted private mapping\n",
246 current->comm, func);
247 return -EINVAL;
248 }
249
250 mask = dax_region->align - 1;
251 if (vma->vm_start & mask || vma->vm_end & mask) {
252 dev_info(dev, "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n",
253 current->comm, func, vma->vm_start, vma->vm_end,
254 mask);
255 return -EINVAL;
256 }
257
258 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV
259 && (vma->vm_flags & VM_DONTCOPY) == 0) {
260 dev_info(dev, "%s: %s: fail, dax range requires MADV_DONTFORK\n",
261 current->comm, func);
262 return -EINVAL;
263 }
264
265 if (!vma_is_dax(vma)) {
266 dev_info(dev, "%s: %s: fail, vma is not DAX capable\n",
267 current->comm, func);
268 return -EINVAL;
269 }
270
271 return 0;
272}
273
Dan Williams5f0694b2017-01-30 21:43:10 -0800274static phys_addr_t pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff,
Dan Williamsdee41072016-05-14 12:20:44 -0700275 unsigned long size)
276{
277 struct resource *res;
278 phys_addr_t phys;
279 int i;
280
Dan Williams5f0694b2017-01-30 21:43:10 -0800281 for (i = 0; i < dev_dax->num_resources; i++) {
282 res = &dev_dax->res[i];
Dan Williamsdee41072016-05-14 12:20:44 -0700283 phys = pgoff * PAGE_SIZE + res->start;
284 if (phys >= res->start && phys <= res->end)
285 break;
286 pgoff -= PHYS_PFN(resource_size(res));
287 }
288
Dan Williams5f0694b2017-01-30 21:43:10 -0800289 if (i < dev_dax->num_resources) {
290 res = &dev_dax->res[i];
Dan Williamsdee41072016-05-14 12:20:44 -0700291 if (phys + size - 1 <= res->end)
292 return phys;
293 }
294
295 return -1;
296}
297
Dan Williams5f0694b2017-01-30 21:43:10 -0800298static int __dev_dax_pte_fault(struct dev_dax *dev_dax, struct vm_fault *vmf)
Dan Williamsdee41072016-05-14 12:20:44 -0700299{
Dan Williams5f0694b2017-01-30 21:43:10 -0800300 struct device *dev = &dev_dax->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700301 struct dax_region *dax_region;
302 int rc = VM_FAULT_SIGBUS;
303 phys_addr_t phys;
304 pfn_t pfn;
Dave Jiang0134ed42017-03-10 13:24:22 -0700305 unsigned int fault_size = PAGE_SIZE;
Dan Williamsdee41072016-05-14 12:20:44 -0700306
Dan Williams5f0694b2017-01-30 21:43:10 -0800307 if (check_vma(dev_dax, vmf->vma, __func__))
Dan Williamsdee41072016-05-14 12:20:44 -0700308 return VM_FAULT_SIGBUS;
309
Dan Williams5f0694b2017-01-30 21:43:10 -0800310 dax_region = dev_dax->region;
Dan Williamsdee41072016-05-14 12:20:44 -0700311 if (dax_region->align > PAGE_SIZE) {
Oliver O'Halloran76202622017-04-12 01:59:36 +1000312 dev_dbg(dev, "%s: alignment (%#x) > fault size (%#x)\n",
313 __func__, dax_region->align, fault_size);
Dan Williamsdee41072016-05-14 12:20:44 -0700314 return VM_FAULT_SIGBUS;
315 }
316
Dave Jiang0134ed42017-03-10 13:24:22 -0700317 if (fault_size != dax_region->align)
318 return VM_FAULT_SIGBUS;
319
Dan Williams5f0694b2017-01-30 21:43:10 -0800320 phys = pgoff_to_phys(dev_dax, vmf->pgoff, PAGE_SIZE);
Dan Williamsdee41072016-05-14 12:20:44 -0700321 if (phys == -1) {
Dave Jiang52084f82017-03-09 16:56:01 -0700322 dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__,
Dan Williamsdee41072016-05-14 12:20:44 -0700323 vmf->pgoff);
324 return VM_FAULT_SIGBUS;
325 }
326
327 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
328
Dave Jiang11bac802017-02-24 14:56:41 -0800329 rc = vm_insert_mixed(vmf->vma, vmf->address, pfn);
Dan Williamsdee41072016-05-14 12:20:44 -0700330
331 if (rc == -ENOMEM)
332 return VM_FAULT_OOM;
333 if (rc < 0 && rc != -EBUSY)
334 return VM_FAULT_SIGBUS;
335
336 return VM_FAULT_NOPAGE;
337}
338
Dan Williams5f0694b2017-01-30 21:43:10 -0800339static int __dev_dax_pmd_fault(struct dev_dax *dev_dax, struct vm_fault *vmf)
Dan Williamsdee41072016-05-14 12:20:44 -0700340{
Dave Jiangd8a849e2017-02-22 15:40:03 -0800341 unsigned long pmd_addr = vmf->address & PMD_MASK;
Dan Williams5f0694b2017-01-30 21:43:10 -0800342 struct device *dev = &dev_dax->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700343 struct dax_region *dax_region;
344 phys_addr_t phys;
345 pgoff_t pgoff;
346 pfn_t pfn;
Dave Jiang0134ed42017-03-10 13:24:22 -0700347 unsigned int fault_size = PMD_SIZE;
Dan Williamsdee41072016-05-14 12:20:44 -0700348
Dan Williams5f0694b2017-01-30 21:43:10 -0800349 if (check_vma(dev_dax, vmf->vma, __func__))
Dan Williamsdee41072016-05-14 12:20:44 -0700350 return VM_FAULT_SIGBUS;
351
Dan Williams5f0694b2017-01-30 21:43:10 -0800352 dax_region = dev_dax->region;
Dan Williamsdee41072016-05-14 12:20:44 -0700353 if (dax_region->align > PMD_SIZE) {
Oliver O'Halloran76202622017-04-12 01:59:36 +1000354 dev_dbg(dev, "%s: alignment (%#x) > fault size (%#x)\n",
355 __func__, dax_region->align, fault_size);
Dan Williamsdee41072016-05-14 12:20:44 -0700356 return VM_FAULT_SIGBUS;
357 }
358
359 /* dax pmd mappings require pfn_t_devmap() */
360 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
Oliver O'Halloran76202622017-04-12 01:59:36 +1000361 dev_dbg(dev, "%s: region lacks devmap flags\n", __func__);
Dan Williamsdee41072016-05-14 12:20:44 -0700362 return VM_FAULT_SIGBUS;
363 }
364
Dave Jiang0134ed42017-03-10 13:24:22 -0700365 if (fault_size < dax_region->align)
366 return VM_FAULT_SIGBUS;
367 else if (fault_size > dax_region->align)
368 return VM_FAULT_FALLBACK;
369
370 /* if we are outside of the VMA */
371 if (pmd_addr < vmf->vma->vm_start ||
372 (pmd_addr + PMD_SIZE) > vmf->vma->vm_end)
373 return VM_FAULT_SIGBUS;
374
Dave Jiangf4200392017-02-22 15:40:06 -0800375 pgoff = linear_page_index(vmf->vma, pmd_addr);
Dan Williams5f0694b2017-01-30 21:43:10 -0800376 phys = pgoff_to_phys(dev_dax, pgoff, PMD_SIZE);
Dan Williamsdee41072016-05-14 12:20:44 -0700377 if (phys == -1) {
Dave Jiang52084f82017-03-09 16:56:01 -0700378 dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__,
Dan Williamsdee41072016-05-14 12:20:44 -0700379 pgoff);
380 return VM_FAULT_SIGBUS;
381 }
382
383 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
384
Dave Jiangf4200392017-02-22 15:40:06 -0800385 return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd, pfn,
Dave Jiangd8a849e2017-02-22 15:40:03 -0800386 vmf->flags & FAULT_FLAG_WRITE);
Dan Williamsdee41072016-05-14 12:20:44 -0700387}
388
Dave Jiang9557fee2017-02-24 14:57:05 -0800389#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
Dan Williams5f0694b2017-01-30 21:43:10 -0800390static int __dev_dax_pud_fault(struct dev_dax *dev_dax, struct vm_fault *vmf)
Dave Jiang9557fee2017-02-24 14:57:05 -0800391{
392 unsigned long pud_addr = vmf->address & PUD_MASK;
Dan Williams5f0694b2017-01-30 21:43:10 -0800393 struct device *dev = &dev_dax->dev;
Dave Jiang9557fee2017-02-24 14:57:05 -0800394 struct dax_region *dax_region;
395 phys_addr_t phys;
396 pgoff_t pgoff;
397 pfn_t pfn;
Dave Jiang70b085b2017-03-10 13:24:27 -0700398 unsigned int fault_size = PUD_SIZE;
399
Dave Jiang9557fee2017-02-24 14:57:05 -0800400
Dan Williams5f0694b2017-01-30 21:43:10 -0800401 if (check_vma(dev_dax, vmf->vma, __func__))
Dave Jiang9557fee2017-02-24 14:57:05 -0800402 return VM_FAULT_SIGBUS;
403
Dan Williams5f0694b2017-01-30 21:43:10 -0800404 dax_region = dev_dax->region;
Dave Jiang9557fee2017-02-24 14:57:05 -0800405 if (dax_region->align > PUD_SIZE) {
Oliver O'Halloran76202622017-04-12 01:59:36 +1000406 dev_dbg(dev, "%s: alignment (%#x) > fault size (%#x)\n",
407 __func__, dax_region->align, fault_size);
Dave Jiang9557fee2017-02-24 14:57:05 -0800408 return VM_FAULT_SIGBUS;
409 }
410
411 /* dax pud mappings require pfn_t_devmap() */
412 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
Oliver O'Halloran76202622017-04-12 01:59:36 +1000413 dev_dbg(dev, "%s: region lacks devmap flags\n", __func__);
Dave Jiang9557fee2017-02-24 14:57:05 -0800414 return VM_FAULT_SIGBUS;
415 }
416
Dave Jiang70b085b2017-03-10 13:24:27 -0700417 if (fault_size < dax_region->align)
418 return VM_FAULT_SIGBUS;
419 else if (fault_size > dax_region->align)
420 return VM_FAULT_FALLBACK;
421
422 /* if we are outside of the VMA */
423 if (pud_addr < vmf->vma->vm_start ||
424 (pud_addr + PUD_SIZE) > vmf->vma->vm_end)
425 return VM_FAULT_SIGBUS;
426
Dave Jiang9557fee2017-02-24 14:57:05 -0800427 pgoff = linear_page_index(vmf->vma, pud_addr);
Dan Williams5f0694b2017-01-30 21:43:10 -0800428 phys = pgoff_to_phys(dev_dax, pgoff, PUD_SIZE);
Dave Jiang9557fee2017-02-24 14:57:05 -0800429 if (phys == -1) {
Dave Jiang52084f82017-03-09 16:56:01 -0700430 dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__,
Dave Jiang9557fee2017-02-24 14:57:05 -0800431 pgoff);
432 return VM_FAULT_SIGBUS;
433 }
434
435 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
436
437 return vmf_insert_pfn_pud(vmf->vma, vmf->address, vmf->pud, pfn,
438 vmf->flags & FAULT_FLAG_WRITE);
439}
440#else
Dan Williams5f0694b2017-01-30 21:43:10 -0800441static int __dev_dax_pud_fault(struct dev_dax *dev_dax, struct vm_fault *vmf)
Dave Jiang9557fee2017-02-24 14:57:05 -0800442{
443 return VM_FAULT_FALLBACK;
444}
445#endif /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
446
Dan Williams5f0694b2017-01-30 21:43:10 -0800447static int dev_dax_huge_fault(struct vm_fault *vmf,
Dave Jiangc791ace2017-02-24 14:57:08 -0800448 enum page_entry_size pe_size)
Dan Williamsdee41072016-05-14 12:20:44 -0700449{
Dan Williams956a4cd2017-04-07 16:42:08 -0700450 int rc, id;
Dave Jiangf4200392017-02-22 15:40:06 -0800451 struct file *filp = vmf->vma->vm_file;
Dan Williams5f0694b2017-01-30 21:43:10 -0800452 struct dev_dax *dev_dax = filp->private_data;
Dan Williamsdee41072016-05-14 12:20:44 -0700453
Dan Williams5f0694b2017-01-30 21:43:10 -0800454 dev_dbg(&dev_dax->dev, "%s: %s: %s (%#lx - %#lx) size = %d\n", __func__,
Dave Jiangd8a849e2017-02-22 15:40:03 -0800455 current->comm, (vmf->flags & FAULT_FLAG_WRITE)
Dave Jiangf4200392017-02-22 15:40:06 -0800456 ? "write" : "read",
Oliver O'Halloran76202622017-04-12 01:59:36 +1000457 vmf->vma->vm_start, vmf->vma->vm_end, pe_size);
Dan Williamsdee41072016-05-14 12:20:44 -0700458
Dan Williams7b6be842017-04-11 09:49:49 -0700459 id = dax_read_lock();
Dave Jiangc791ace2017-02-24 14:57:08 -0800460 switch (pe_size) {
461 case PE_SIZE_PTE:
Dan Williams5f0694b2017-01-30 21:43:10 -0800462 rc = __dev_dax_pte_fault(dev_dax, vmf);
Dave Jianga2d58162017-02-24 14:56:59 -0800463 break;
Dave Jiangc791ace2017-02-24 14:57:08 -0800464 case PE_SIZE_PMD:
Dan Williams5f0694b2017-01-30 21:43:10 -0800465 rc = __dev_dax_pmd_fault(dev_dax, vmf);
Dave Jianga2d58162017-02-24 14:56:59 -0800466 break;
Dave Jiangc791ace2017-02-24 14:57:08 -0800467 case PE_SIZE_PUD:
Dan Williams5f0694b2017-01-30 21:43:10 -0800468 rc = __dev_dax_pud_fault(dev_dax, vmf);
Dave Jiang9557fee2017-02-24 14:57:05 -0800469 break;
Dave Jianga2d58162017-02-24 14:56:59 -0800470 default:
Pushkar Jambhlekar54eafcc2017-04-11 09:12:25 -0700471 rc = VM_FAULT_SIGBUS;
Dave Jianga2d58162017-02-24 14:56:59 -0800472 }
Dan Williams7b6be842017-04-11 09:49:49 -0700473 dax_read_unlock(id);
Dan Williamsdee41072016-05-14 12:20:44 -0700474
475 return rc;
476}
477
Dan Williams5f0694b2017-01-30 21:43:10 -0800478static int dev_dax_fault(struct vm_fault *vmf)
Dave Jiangc791ace2017-02-24 14:57:08 -0800479{
Dan Williams5f0694b2017-01-30 21:43:10 -0800480 return dev_dax_huge_fault(vmf, PE_SIZE_PTE);
Dave Jiangc791ace2017-02-24 14:57:08 -0800481}
482
Dan Williams5f0694b2017-01-30 21:43:10 -0800483static const struct vm_operations_struct dax_vm_ops = {
484 .fault = dev_dax_fault,
485 .huge_fault = dev_dax_huge_fault,
Dan Williamsdee41072016-05-14 12:20:44 -0700486};
487
Dan Williamsaf69f512016-08-11 00:38:03 -0700488static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
Dan Williamsdee41072016-05-14 12:20:44 -0700489{
Dan Williams5f0694b2017-01-30 21:43:10 -0800490 struct dev_dax *dev_dax = filp->private_data;
Dan Williams7b6be842017-04-11 09:49:49 -0700491 int rc, id;
Dan Williamsdee41072016-05-14 12:20:44 -0700492
Dan Williams5f0694b2017-01-30 21:43:10 -0800493 dev_dbg(&dev_dax->dev, "%s\n", __func__);
Dan Williamsdee41072016-05-14 12:20:44 -0700494
Dan Williams7b6be842017-04-11 09:49:49 -0700495 /*
496 * We lock to check dax_dev liveness and will re-check at
497 * fault time.
498 */
499 id = dax_read_lock();
Dan Williams5f0694b2017-01-30 21:43:10 -0800500 rc = check_vma(dev_dax, vma, __func__);
Dan Williams7b6be842017-04-11 09:49:49 -0700501 dax_read_unlock(id);
Dan Williamsdee41072016-05-14 12:20:44 -0700502 if (rc)
503 return rc;
504
Dan Williams5f0694b2017-01-30 21:43:10 -0800505 vma->vm_ops = &dax_vm_ops;
Dan Williamsdee41072016-05-14 12:20:44 -0700506 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
507 return 0;
Dan Williams043a9252016-08-07 08:23:56 -0700508}
Dan Williamsdee41072016-05-14 12:20:44 -0700509
Dan Williams043a9252016-08-07 08:23:56 -0700510/* return an unmapped area aligned to the dax region specified alignment */
Dan Williamsaf69f512016-08-11 00:38:03 -0700511static unsigned long dax_get_unmapped_area(struct file *filp,
Dan Williams043a9252016-08-07 08:23:56 -0700512 unsigned long addr, unsigned long len, unsigned long pgoff,
513 unsigned long flags)
514{
515 unsigned long off, off_end, off_align, len_align, addr_align, align;
Dan Williams5f0694b2017-01-30 21:43:10 -0800516 struct dev_dax *dev_dax = filp ? filp->private_data : NULL;
Dan Williams043a9252016-08-07 08:23:56 -0700517 struct dax_region *dax_region;
518
Dan Williams5f0694b2017-01-30 21:43:10 -0800519 if (!dev_dax || addr)
Dan Williams043a9252016-08-07 08:23:56 -0700520 goto out;
521
Dan Williams5f0694b2017-01-30 21:43:10 -0800522 dax_region = dev_dax->region;
Dan Williams043a9252016-08-07 08:23:56 -0700523 align = dax_region->align;
524 off = pgoff << PAGE_SHIFT;
525 off_end = off + len;
526 off_align = round_up(off, align);
527
528 if ((off_end <= off_align) || ((off_end - off_align) < align))
529 goto out;
530
531 len_align = len + align;
532 if ((off + len_align) < off)
533 goto out;
534
535 addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
536 pgoff, flags);
537 if (!IS_ERR_VALUE(addr_align)) {
538 addr_align += (off - addr_align) & (align - 1);
539 return addr_align;
540 }
541 out:
542 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
543}
544
Dan Williamsaf69f512016-08-11 00:38:03 -0700545static int dax_open(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700546{
Dan Williams7b6be842017-04-11 09:49:49 -0700547 struct dax_device *dax_dev = inode_dax(inode);
548 struct inode *__dax_inode = dax_inode(dax_dev);
549 struct dev_dax *dev_dax = dax_get_private(dax_dev);
Dan Williams043a9252016-08-07 08:23:56 -0700550
Dan Williams5f0694b2017-01-30 21:43:10 -0800551 dev_dbg(&dev_dax->dev, "%s\n", __func__);
Dan Williams7b6be842017-04-11 09:49:49 -0700552 inode->i_mapping = __dax_inode->i_mapping;
553 inode->i_mapping->host = __dax_inode;
Dan Williams3bc52c42016-07-24 21:55:45 -0700554 filp->f_mapping = inode->i_mapping;
Dan Williams5f0694b2017-01-30 21:43:10 -0800555 filp->private_data = dev_dax;
Dan Williamsebd84d72016-08-11 00:41:51 -0700556 inode->i_flags = S_DAX;
Dan Williams043a9252016-08-07 08:23:56 -0700557
Dan Williams043a9252016-08-07 08:23:56 -0700558 return 0;
559}
560
Dan Williamsaf69f512016-08-11 00:38:03 -0700561static int dax_release(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700562{
Dan Williams5f0694b2017-01-30 21:43:10 -0800563 struct dev_dax *dev_dax = filp->private_data;
Dan Williams043a9252016-08-07 08:23:56 -0700564
Dan Williams5f0694b2017-01-30 21:43:10 -0800565 dev_dbg(&dev_dax->dev, "%s\n", __func__);
Dan Williams043a9252016-08-07 08:23:56 -0700566 return 0;
Dan Williamsdee41072016-05-14 12:20:44 -0700567}
568
Dan Williamsab68f262016-05-18 09:15:08 -0700569static const struct file_operations dax_fops = {
570 .llseek = noop_llseek,
571 .owner = THIS_MODULE,
Dan Williamsaf69f512016-08-11 00:38:03 -0700572 .open = dax_open,
573 .release = dax_release,
574 .get_unmapped_area = dax_get_unmapped_area,
575 .mmap = dax_mmap,
Dan Williamsab68f262016-05-18 09:15:08 -0700576};
577
Dan Williams5f0694b2017-01-30 21:43:10 -0800578static void dev_dax_release(struct device *dev)
Dan Williams043a9252016-08-07 08:23:56 -0700579{
Dan Williams5f0694b2017-01-30 21:43:10 -0800580 struct dev_dax *dev_dax = to_dev_dax(dev);
581 struct dax_region *dax_region = dev_dax->region;
Dan Williams7b6be842017-04-11 09:49:49 -0700582 struct dax_device *dax_dev = dev_dax->dax_dev;
Dan Williams043a9252016-08-07 08:23:56 -0700583
Dan Williams5f0694b2017-01-30 21:43:10 -0800584 ida_simple_remove(&dax_region->ida, dev_dax->id);
Dan Williamsebd84d72016-08-11 00:41:51 -0700585 dax_region_put(dax_region);
Dan Williams7b6be842017-04-11 09:49:49 -0700586 put_dax(dax_dev);
Dan Williams5f0694b2017-01-30 21:43:10 -0800587 kfree(dev_dax);
Dan Williamsebd84d72016-08-11 00:41:51 -0700588}
589
Dan Williams5f0694b2017-01-30 21:43:10 -0800590static void kill_dev_dax(struct dev_dax *dev_dax)
Dan Williamsebd84d72016-08-11 00:41:51 -0700591{
Dan Williams7b6be842017-04-11 09:49:49 -0700592 struct dax_device *dax_dev = dev_dax->dax_dev;
593 struct inode *inode = dax_inode(dax_dev);
594
595 kill_dax(dax_dev);
596 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
Dan Williamsed01e502017-03-17 12:48:09 -0600597}
598
Dan Williams5f0694b2017-01-30 21:43:10 -0800599static void unregister_dev_dax(void *dev)
Dan Williamsed01e502017-03-17 12:48:09 -0600600{
Dan Williams5f0694b2017-01-30 21:43:10 -0800601 struct dev_dax *dev_dax = to_dev_dax(dev);
Dan Williams7b6be842017-04-11 09:49:49 -0700602 struct dax_device *dax_dev = dev_dax->dax_dev;
603 struct inode *inode = dax_inode(dax_dev);
604 struct cdev *cdev = inode->i_cdev;
Dan Williamsed01e502017-03-17 12:48:09 -0600605
606 dev_dbg(dev, "%s\n", __func__);
607
Dan Williams5f0694b2017-01-30 21:43:10 -0800608 kill_dev_dax(dev_dax);
Dan Williams7b6be842017-04-11 09:49:49 -0700609 cdev_device_del(cdev, dev);
Logan Gunthorpe92a3fa02017-03-17 12:48:10 -0600610 put_device(dev);
Dan Williams043a9252016-08-07 08:23:56 -0700611}
612
Dan Williams5f0694b2017-01-30 21:43:10 -0800613struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region,
Dan Williamsd76911e2016-07-19 17:51:40 -0700614 struct resource *res, int count)
Dan Williams043a9252016-08-07 08:23:56 -0700615{
616 struct device *parent = dax_region->dev;
Dan Williams7b6be842017-04-11 09:49:49 -0700617 struct dax_device *dax_dev;
Dan Williams5f0694b2017-01-30 21:43:10 -0800618 struct dev_dax *dev_dax;
Dan Williams7b6be842017-04-11 09:49:49 -0700619 struct inode *inode;
Dan Williams043a9252016-08-07 08:23:56 -0700620 struct device *dev;
Dan Williamsba09c012016-07-24 15:55:42 -0700621 struct cdev *cdev;
Dan Williams7b6be842017-04-11 09:49:49 -0700622 int rc = 0, i;
Dan Williams043a9252016-08-07 08:23:56 -0700623
Dan Williams5f0694b2017-01-30 21:43:10 -0800624 dev_dax = kzalloc(sizeof(*dev_dax) + sizeof(*res) * count, GFP_KERNEL);
625 if (!dev_dax)
Dan Williamsd76911e2016-07-19 17:51:40 -0700626 return ERR_PTR(-ENOMEM);
Dan Williams043a9252016-08-07 08:23:56 -0700627
Dan Williams9d2d01a2016-07-19 16:17:58 -0700628 for (i = 0; i < count; i++) {
629 if (!IS_ALIGNED(res[i].start, dax_region->align)
630 || !IS_ALIGNED(resource_size(&res[i]),
631 dax_region->align)) {
632 rc = -EINVAL;
633 break;
634 }
Dan Williams5f0694b2017-01-30 21:43:10 -0800635 dev_dax->res[i].start = res[i].start;
636 dev_dax->res[i].end = res[i].end;
Dan Williams9d2d01a2016-07-19 16:17:58 -0700637 }
638
639 if (i < count)
640 goto err_id;
641
Dan Williams5f0694b2017-01-30 21:43:10 -0800642 dev_dax->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
643 if (dev_dax->id < 0) {
644 rc = dev_dax->id;
Dan Williams043a9252016-08-07 08:23:56 -0700645 goto err_id;
646 }
647
Dan Williams72058002017-04-19 15:14:31 -0700648 dax_dev = alloc_dax(dev_dax, NULL);
Dan Williams7b6be842017-04-11 09:49:49 -0700649 if (!dax_dev)
650 goto err_dax;
Dan Williams043a9252016-08-07 08:23:56 -0700651
Dan Williams7b6be842017-04-11 09:49:49 -0700652 /* from here on we're committed to teardown via dax_dev_release() */
Dan Williams5f0694b2017-01-30 21:43:10 -0800653 dev = &dev_dax->dev;
Dan Williamsebd84d72016-08-11 00:41:51 -0700654 device_initialize(dev);
Dan Williamsba09c012016-07-24 15:55:42 -0700655
Dan Williams7b6be842017-04-11 09:49:49 -0700656 inode = dax_inode(dax_dev);
657 cdev = inode->i_cdev;
Dan Williamsba09c012016-07-24 15:55:42 -0700658 cdev_init(cdev, &dax_fops);
659 cdev->owner = parent->driver->owner;
Dan Williamsba09c012016-07-24 15:55:42 -0700660
Dan Williams5f0694b2017-01-30 21:43:10 -0800661 dev_dax->num_resources = count;
Dan Williams7b6be842017-04-11 09:49:49 -0700662 dev_dax->dax_dev = dax_dev;
Dan Williams5f0694b2017-01-30 21:43:10 -0800663 dev_dax->region = dax_region;
Dan Williamsba09c012016-07-24 15:55:42 -0700664 kref_get(&dax_region->kref);
665
Dan Williams7b6be842017-04-11 09:49:49 -0700666 dev->devt = inode->i_rdev;
Dan Williamsebd84d72016-08-11 00:41:51 -0700667 dev->class = dax_class;
668 dev->parent = parent;
669 dev->groups = dax_attribute_groups;
Dan Williams5f0694b2017-01-30 21:43:10 -0800670 dev->release = dev_dax_release;
671 dev_set_name(dev, "dax%d.%d", dax_region->id, dev_dax->id);
Logan Gunthorpe92a3fa02017-03-17 12:48:10 -0600672
673 rc = cdev_device_add(cdev, dev);
Dan Williamsebd84d72016-08-11 00:41:51 -0700674 if (rc) {
Dan Williams5f0694b2017-01-30 21:43:10 -0800675 kill_dev_dax(dev_dax);
Dan Williamsebd84d72016-08-11 00:41:51 -0700676 put_device(dev);
Dan Williamsd76911e2016-07-19 17:51:40 -0700677 return ERR_PTR(rc);
Dan Williamsebd84d72016-08-11 00:41:51 -0700678 }
Dan Williams043a9252016-08-07 08:23:56 -0700679
Dan Williams5f0694b2017-01-30 21:43:10 -0800680 rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
Dan Williamsd76911e2016-07-19 17:51:40 -0700681 if (rc)
682 return ERR_PTR(rc);
683
Dan Williams5f0694b2017-01-30 21:43:10 -0800684 return dev_dax;
Dan Williams043a9252016-08-07 08:23:56 -0700685
Dan Williams7b6be842017-04-11 09:49:49 -0700686 err_dax:
Dan Williams5f0694b2017-01-30 21:43:10 -0800687 ida_simple_remove(&dax_region->ida, dev_dax->id);
Dan Williams043a9252016-08-07 08:23:56 -0700688 err_id:
Dan Williams5f0694b2017-01-30 21:43:10 -0800689 kfree(dev_dax);
Dan Williams043a9252016-08-07 08:23:56 -0700690
Dan Williamsd76911e2016-07-19 17:51:40 -0700691 return ERR_PTR(rc);
Dan Williams043a9252016-08-07 08:23:56 -0700692}
Dan Williams5f0694b2017-01-30 21:43:10 -0800693EXPORT_SYMBOL_GPL(devm_create_dev_dax);
Dan Williams043a9252016-08-07 08:23:56 -0700694
Dan Williamsab68f262016-05-18 09:15:08 -0700695static int __init dax_init(void)
696{
Dan Williamsab68f262016-05-18 09:15:08 -0700697 dax_class = class_create(THIS_MODULE, "dax");
Dan Williams7b6be842017-04-11 09:49:49 -0700698 return PTR_ERR_OR_ZERO(dax_class);
Dan Williamsab68f262016-05-18 09:15:08 -0700699}
700
701static void __exit dax_exit(void)
702{
703 class_destroy(dax_class);
Dan Williamsab68f262016-05-18 09:15:08 -0700704}
705
706MODULE_AUTHOR("Intel Corporation");
707MODULE_LICENSE("GPL v2");
708subsys_initcall(dax_init);
709module_exit(dax_exit);