blob: 736c03830fd032bb757cdd507328fe1b735eb56f [file] [log] [blame]
Dan Williamsab68f262016-05-18 09:15:08 -07001/*
2 * Copyright(c) 2016 Intel Corporation. All rights reserved.
3 *
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>
17#include <linux/slab.h>
18#include <linux/dax.h>
19#include <linux/fs.h>
20#include <linux/mm.h>
Dan Williamsccdb07f2016-08-06 16:05:06 -070021#include "dax.h"
Dan Williamsab68f262016-05-18 09:15:08 -070022
23static int dax_major;
24static struct class *dax_class;
25static DEFINE_IDA(dax_minor_ida);
26
27/**
28 * struct dax_region - mapping infrastructure for dax devices
29 * @id: kernel-wide unique region for a memory range
30 * @base: linear address corresponding to @res
31 * @kref: to pin while other agents have a need to do lookups
32 * @dev: parent device backing this region
33 * @align: allocation and mapping alignment for child dax devices
34 * @res: physical address range of the region
35 * @pfn_flags: identify whether the pfns are paged back or not
36 */
37struct dax_region {
38 int id;
39 struct ida ida;
40 void *base;
41 struct kref kref;
42 struct device *dev;
43 unsigned int align;
44 struct resource res;
45 unsigned long pfn_flags;
46};
47
48/**
49 * struct dax_dev - subdivision of a dax region
50 * @region - parent region
51 * @dev - device backing the character device
52 * @kref - enable this data to be tracked in filp->private_data
Dan Williamsdee41072016-05-14 12:20:44 -070053 * @alive - !alive + rcu grace period == no new mappings can be established
Dan Williamsab68f262016-05-18 09:15:08 -070054 * @id - child id in the region
55 * @num_resources - number of physical address extents in this device
56 * @res - array of physical address ranges
57 */
58struct dax_dev {
59 struct dax_region *region;
60 struct device *dev;
61 struct kref kref;
Dan Williamsdee41072016-05-14 12:20:44 -070062 bool alive;
Dan Williamsab68f262016-05-18 09:15:08 -070063 int id;
64 int num_resources;
65 struct resource res[0];
66};
67
68static void dax_region_free(struct kref *kref)
69{
70 struct dax_region *dax_region;
71
72 dax_region = container_of(kref, struct dax_region, kref);
73 kfree(dax_region);
74}
75
76void dax_region_put(struct dax_region *dax_region)
77{
78 kref_put(&dax_region->kref, dax_region_free);
79}
80EXPORT_SYMBOL_GPL(dax_region_put);
81
82static void dax_dev_free(struct kref *kref)
83{
84 struct dax_dev *dax_dev;
85
86 dax_dev = container_of(kref, struct dax_dev, kref);
87 dax_region_put(dax_dev->region);
88 kfree(dax_dev);
89}
90
91static void dax_dev_put(struct dax_dev *dax_dev)
92{
93 kref_put(&dax_dev->kref, dax_dev_free);
94}
95
96struct dax_region *alloc_dax_region(struct device *parent, int region_id,
97 struct resource *res, unsigned int align, void *addr,
98 unsigned long pfn_flags)
99{
100 struct dax_region *dax_region;
101
102 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
103
104 if (!dax_region)
105 return NULL;
106
107 memcpy(&dax_region->res, res, sizeof(*res));
108 dax_region->pfn_flags = pfn_flags;
109 kref_init(&dax_region->kref);
110 dax_region->id = region_id;
111 ida_init(&dax_region->ida);
112 dax_region->align = align;
113 dax_region->dev = parent;
114 dax_region->base = addr;
115
116 return dax_region;
117}
118EXPORT_SYMBOL_GPL(alloc_dax_region);
119
120static ssize_t size_show(struct device *dev,
121 struct device_attribute *attr, char *buf)
122{
123 struct dax_dev *dax_dev = dev_get_drvdata(dev);
124 unsigned long long size = 0;
125 int i;
126
127 for (i = 0; i < dax_dev->num_resources; i++)
128 size += resource_size(&dax_dev->res[i]);
129
130 return sprintf(buf, "%llu\n", size);
131}
132static DEVICE_ATTR_RO(size);
133
134static struct attribute *dax_device_attributes[] = {
135 &dev_attr_size.attr,
136 NULL,
137};
138
139static const struct attribute_group dax_device_attribute_group = {
140 .attrs = dax_device_attributes,
141};
142
143static const struct attribute_group *dax_attribute_groups[] = {
144 &dax_device_attribute_group,
145 NULL,
146};
147
148static void unregister_dax_dev(void *_dev)
149{
150 struct device *dev = _dev;
151 struct dax_dev *dax_dev = dev_get_drvdata(dev);
152 struct dax_region *dax_region = dax_dev->region;
153
154 dev_dbg(dev, "%s\n", __func__);
155
Dan Williamsdee41072016-05-14 12:20:44 -0700156 /*
157 * Note, rcu is not protecting the liveness of dax_dev, rcu is
158 * ensuring that any fault handlers that might have seen
159 * dax_dev->alive == true, have completed. Any fault handlers
160 * that start after synchronize_rcu() has started will abort
161 * upon seeing dax_dev->alive == false.
162 */
163 dax_dev->alive = false;
164 synchronize_rcu();
165
Dan Williamsab68f262016-05-18 09:15:08 -0700166 get_device(dev);
167 device_unregister(dev);
168 ida_simple_remove(&dax_region->ida, dax_dev->id);
169 ida_simple_remove(&dax_minor_ida, MINOR(dev->devt));
170 put_device(dev);
171 dax_dev_put(dax_dev);
172}
173
174int devm_create_dax_dev(struct dax_region *dax_region, struct resource *res,
175 int count)
176{
177 struct device *parent = dax_region->dev;
178 struct dax_dev *dax_dev;
179 struct device *dev;
180 int rc, minor;
181 dev_t dev_t;
182
183 dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
184 if (!dax_dev)
185 return -ENOMEM;
186 memcpy(dax_dev->res, res, sizeof(*res) * count);
187 dax_dev->num_resources = count;
188 kref_init(&dax_dev->kref);
Dan Williamsdee41072016-05-14 12:20:44 -0700189 dax_dev->alive = true;
Dan Williamsab68f262016-05-18 09:15:08 -0700190 dax_dev->region = dax_region;
191 kref_get(&dax_region->kref);
192
193 dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
194 if (dax_dev->id < 0) {
195 rc = dax_dev->id;
196 goto err_id;
197 }
198
199 minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL);
200 if (minor < 0) {
201 rc = minor;
202 goto err_minor;
203 }
204
205 dev_t = MKDEV(dax_major, minor);
206 dev = device_create_with_groups(dax_class, parent, dev_t, dax_dev,
207 dax_attribute_groups, "dax%d.%d", dax_region->id,
208 dax_dev->id);
209 if (IS_ERR(dev)) {
210 rc = PTR_ERR(dev);
211 goto err_create;
212 }
213 dax_dev->dev = dev;
214
Sajjan, Vikas Cd1c8e0c2016-07-05 11:20:07 +0530215 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev);
216 if (rc)
Dan Williamsab68f262016-05-18 09:15:08 -0700217 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700218
219 return 0;
220
221 err_create:
222 ida_simple_remove(&dax_minor_ida, minor);
223 err_minor:
224 ida_simple_remove(&dax_region->ida, dax_dev->id);
225 err_id:
226 dax_dev_put(dax_dev);
227
228 return rc;
229}
230EXPORT_SYMBOL_GPL(devm_create_dax_dev);
231
Dan Williamsdee41072016-05-14 12:20:44 -0700232/* return an unmapped area aligned to the dax region specified alignment */
233static unsigned long dax_dev_get_unmapped_area(struct file *filp,
234 unsigned long addr, unsigned long len, unsigned long pgoff,
235 unsigned long flags)
236{
237 unsigned long off, off_end, off_align, len_align, addr_align, align;
238 struct dax_dev *dax_dev = filp ? filp->private_data : NULL;
239 struct dax_region *dax_region;
240
241 if (!dax_dev || addr)
242 goto out;
243
244 dax_region = dax_dev->region;
245 align = dax_region->align;
246 off = pgoff << PAGE_SHIFT;
247 off_end = off + len;
248 off_align = round_up(off, align);
249
250 if ((off_end <= off_align) || ((off_end - off_align) < align))
251 goto out;
252
253 len_align = len + align;
254 if ((off + len_align) < off)
255 goto out;
256
257 addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
258 pgoff, flags);
259 if (!IS_ERR_VALUE(addr_align)) {
260 addr_align += (off - addr_align) & (align - 1);
261 return addr_align;
262 }
263 out:
264 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
265}
266
267static int __match_devt(struct device *dev, const void *data)
268{
269 const dev_t *devt = data;
270
271 return dev->devt == *devt;
272}
273
274static struct device *dax_dev_find(dev_t dev_t)
275{
276 return class_find_device(dax_class, NULL, &dev_t, __match_devt);
277}
278
279static int dax_dev_open(struct inode *inode, struct file *filp)
280{
281 struct dax_dev *dax_dev = NULL;
282 struct device *dev;
283
284 dev = dax_dev_find(inode->i_rdev);
285 if (!dev)
286 return -ENXIO;
287
288 device_lock(dev);
289 dax_dev = dev_get_drvdata(dev);
290 if (dax_dev) {
291 dev_dbg(dev, "%s\n", __func__);
292 filp->private_data = dax_dev;
293 kref_get(&dax_dev->kref);
294 inode->i_flags = S_DAX;
295 }
296 device_unlock(dev);
297
298 if (!dax_dev) {
299 put_device(dev);
300 return -ENXIO;
301 }
302 return 0;
303}
304
305static int dax_dev_release(struct inode *inode, struct file *filp)
306{
307 struct dax_dev *dax_dev = filp->private_data;
308 struct device *dev = dax_dev->dev;
309
310 dev_dbg(dax_dev->dev, "%s\n", __func__);
311 dax_dev_put(dax_dev);
312 put_device(dev);
313
314 return 0;
315}
316
317static int check_vma(struct dax_dev *dax_dev, struct vm_area_struct *vma,
318 const char *func)
319{
320 struct dax_region *dax_region = dax_dev->region;
321 struct device *dev = dax_dev->dev;
322 unsigned long mask;
323
324 if (!dax_dev->alive)
325 return -ENXIO;
326
327 /* prevent private / writable mappings from being established */
328 if ((vma->vm_flags & (VM_NORESERVE|VM_SHARED|VM_WRITE)) == VM_WRITE) {
329 dev_info(dev, "%s: %s: fail, attempted private mapping\n",
330 current->comm, func);
331 return -EINVAL;
332 }
333
334 mask = dax_region->align - 1;
335 if (vma->vm_start & mask || vma->vm_end & mask) {
336 dev_info(dev, "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n",
337 current->comm, func, vma->vm_start, vma->vm_end,
338 mask);
339 return -EINVAL;
340 }
341
342 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV
343 && (vma->vm_flags & VM_DONTCOPY) == 0) {
344 dev_info(dev, "%s: %s: fail, dax range requires MADV_DONTFORK\n",
345 current->comm, func);
346 return -EINVAL;
347 }
348
349 if (!vma_is_dax(vma)) {
350 dev_info(dev, "%s: %s: fail, vma is not DAX capable\n",
351 current->comm, func);
352 return -EINVAL;
353 }
354
355 return 0;
356}
357
358static phys_addr_t pgoff_to_phys(struct dax_dev *dax_dev, pgoff_t pgoff,
359 unsigned long size)
360{
361 struct resource *res;
362 phys_addr_t phys;
363 int i;
364
365 for (i = 0; i < dax_dev->num_resources; i++) {
366 res = &dax_dev->res[i];
367 phys = pgoff * PAGE_SIZE + res->start;
368 if (phys >= res->start && phys <= res->end)
369 break;
370 pgoff -= PHYS_PFN(resource_size(res));
371 }
372
373 if (i < dax_dev->num_resources) {
374 res = &dax_dev->res[i];
375 if (phys + size - 1 <= res->end)
376 return phys;
377 }
378
379 return -1;
380}
381
382static int __dax_dev_fault(struct dax_dev *dax_dev, struct vm_area_struct *vma,
383 struct vm_fault *vmf)
384{
385 unsigned long vaddr = (unsigned long) vmf->virtual_address;
386 struct device *dev = dax_dev->dev;
387 struct dax_region *dax_region;
388 int rc = VM_FAULT_SIGBUS;
389 phys_addr_t phys;
390 pfn_t pfn;
391
392 if (check_vma(dax_dev, vma, __func__))
393 return VM_FAULT_SIGBUS;
394
395 dax_region = dax_dev->region;
396 if (dax_region->align > PAGE_SIZE) {
397 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
398 return VM_FAULT_SIGBUS;
399 }
400
401 phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE);
402 if (phys == -1) {
403 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
404 vmf->pgoff);
405 return VM_FAULT_SIGBUS;
406 }
407
408 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
409
410 rc = vm_insert_mixed(vma, vaddr, pfn);
411
412 if (rc == -ENOMEM)
413 return VM_FAULT_OOM;
414 if (rc < 0 && rc != -EBUSY)
415 return VM_FAULT_SIGBUS;
416
417 return VM_FAULT_NOPAGE;
418}
419
420static int dax_dev_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
421{
422 int rc;
423 struct file *filp = vma->vm_file;
424 struct dax_dev *dax_dev = filp->private_data;
425
426 dev_dbg(dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
427 current->comm, (vmf->flags & FAULT_FLAG_WRITE)
428 ? "write" : "read", vma->vm_start, vma->vm_end);
429 rcu_read_lock();
430 rc = __dax_dev_fault(dax_dev, vma, vmf);
431 rcu_read_unlock();
432
433 return rc;
434}
435
436static int __dax_dev_pmd_fault(struct dax_dev *dax_dev,
437 struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd,
438 unsigned int flags)
439{
440 unsigned long pmd_addr = addr & PMD_MASK;
441 struct device *dev = dax_dev->dev;
442 struct dax_region *dax_region;
443 phys_addr_t phys;
444 pgoff_t pgoff;
445 pfn_t pfn;
446
447 if (check_vma(dax_dev, vma, __func__))
448 return VM_FAULT_SIGBUS;
449
450 dax_region = dax_dev->region;
451 if (dax_region->align > PMD_SIZE) {
452 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
453 return VM_FAULT_SIGBUS;
454 }
455
456 /* dax pmd mappings require pfn_t_devmap() */
457 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
458 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
459 return VM_FAULT_SIGBUS;
460 }
461
462 pgoff = linear_page_index(vma, pmd_addr);
463 phys = pgoff_to_phys(dax_dev, pgoff, PAGE_SIZE);
464 if (phys == -1) {
465 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
466 pgoff);
467 return VM_FAULT_SIGBUS;
468 }
469
470 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
471
472 return vmf_insert_pfn_pmd(vma, addr, pmd, pfn,
473 flags & FAULT_FLAG_WRITE);
474}
475
476static int dax_dev_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
477 pmd_t *pmd, unsigned int flags)
478{
479 int rc;
480 struct file *filp = vma->vm_file;
481 struct dax_dev *dax_dev = filp->private_data;
482
483 dev_dbg(dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
484 current->comm, (flags & FAULT_FLAG_WRITE)
485 ? "write" : "read", vma->vm_start, vma->vm_end);
486
487 rcu_read_lock();
488 rc = __dax_dev_pmd_fault(dax_dev, vma, addr, pmd, flags);
489 rcu_read_unlock();
490
491 return rc;
492}
493
494static void dax_dev_vm_open(struct vm_area_struct *vma)
495{
496 struct file *filp = vma->vm_file;
497 struct dax_dev *dax_dev = filp->private_data;
498
499 dev_dbg(dax_dev->dev, "%s\n", __func__);
500 kref_get(&dax_dev->kref);
501}
502
503static void dax_dev_vm_close(struct vm_area_struct *vma)
504{
505 struct file *filp = vma->vm_file;
506 struct dax_dev *dax_dev = filp->private_data;
507
508 dev_dbg(dax_dev->dev, "%s\n", __func__);
509 dax_dev_put(dax_dev);
510}
511
512static const struct vm_operations_struct dax_dev_vm_ops = {
513 .fault = dax_dev_fault,
514 .pmd_fault = dax_dev_pmd_fault,
515 .open = dax_dev_vm_open,
516 .close = dax_dev_vm_close,
517};
518
519static int dax_dev_mmap(struct file *filp, struct vm_area_struct *vma)
520{
521 struct dax_dev *dax_dev = filp->private_data;
522 int rc;
523
524 dev_dbg(dax_dev->dev, "%s\n", __func__);
525
526 rc = check_vma(dax_dev, vma, __func__);
527 if (rc)
528 return rc;
529
530 kref_get(&dax_dev->kref);
531 vma->vm_ops = &dax_dev_vm_ops;
532 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
533 return 0;
534
535}
536
Dan Williamsab68f262016-05-18 09:15:08 -0700537static const struct file_operations dax_fops = {
538 .llseek = noop_llseek,
539 .owner = THIS_MODULE,
Dan Williamsdee41072016-05-14 12:20:44 -0700540 .open = dax_dev_open,
541 .release = dax_dev_release,
542 .get_unmapped_area = dax_dev_get_unmapped_area,
543 .mmap = dax_dev_mmap,
Dan Williamsab68f262016-05-18 09:15:08 -0700544};
545
546static int __init dax_init(void)
547{
548 int rc;
549
550 rc = register_chrdev(0, "dax", &dax_fops);
551 if (rc < 0)
552 return rc;
553 dax_major = rc;
554
555 dax_class = class_create(THIS_MODULE, "dax");
556 if (IS_ERR(dax_class)) {
557 unregister_chrdev(dax_major, "dax");
558 return PTR_ERR(dax_class);
559 }
560
561 return 0;
562}
563
564static void __exit dax_exit(void)
565{
566 class_destroy(dax_class);
567 unregister_chrdev(dax_major, "dax");
568 ida_destroy(&dax_minor_ida);
569}
570
571MODULE_AUTHOR("Intel Corporation");
572MODULE_LICENSE("GPL v2");
573subsys_initcall(dax_init);
574module_exit(dax_exit);