]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/dax/dax.c
dax: support for transparent PUD pages for device DAX
[mirror_ubuntu-artful-kernel.git] / drivers / dax / dax.c
CommitLineData
ab68f262
DW
1/*
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>
3bc52c45 16#include <linux/mount.h>
ab68f262 17#include <linux/pfn_t.h>
3bc52c45 18#include <linux/hash.h>
ba09c01d 19#include <linux/cdev.h>
ab68f262
DW
20#include <linux/slab.h>
21#include <linux/dax.h>
22#include <linux/fs.h>
23#include <linux/mm.h>
ccdb07f6 24#include "dax.h"
ab68f262 25
ba09c01d 26static dev_t dax_devt;
ab68f262
DW
27static struct class *dax_class;
28static DEFINE_IDA(dax_minor_ida);
ba09c01d
DW
29static int nr_dax = CONFIG_NR_DEV_DAX;
30module_param(nr_dax, int, S_IRUGO);
3bc52c45
DW
31static struct vfsmount *dax_mnt;
32static struct kmem_cache *dax_cache __read_mostly;
33static struct super_block *dax_superblock __read_mostly;
ba09c01d 34MODULE_PARM_DESC(nr_dax, "max number of device-dax instances");
ab68f262
DW
35
36/**
37 * struct dax_region - mapping infrastructure for dax devices
38 * @id: kernel-wide unique region for a memory range
39 * @base: linear address corresponding to @res
40 * @kref: to pin while other agents have a need to do lookups
41 * @dev: parent device backing this region
42 * @align: allocation and mapping alignment for child dax devices
43 * @res: physical address range of the region
44 * @pfn_flags: identify whether the pfns are paged back or not
45 */
46struct dax_region {
47 int id;
48 struct ida ida;
49 void *base;
50 struct kref kref;
51 struct device *dev;
52 unsigned int align;
53 struct resource res;
54 unsigned long pfn_flags;
55};
56
57/**
58 * struct dax_dev - subdivision of a dax region
59 * @region - parent region
60 * @dev - device backing the character device
ba09c01d 61 * @cdev - core chardev data
dee41079 62 * @alive - !alive + rcu grace period == no new mappings can be established
ab68f262
DW
63 * @id - child id in the region
64 * @num_resources - number of physical address extents in this device
65 * @res - array of physical address ranges
66 */
67struct dax_dev {
68 struct dax_region *region;
3bc52c45 69 struct inode *inode;
ebd84d72 70 struct device dev;
ba09c01d 71 struct cdev cdev;
dee41079 72 bool alive;
ab68f262
DW
73 int id;
74 int num_resources;
75 struct resource res[0];
76};
77
d7fe1a67
DW
78static ssize_t id_show(struct device *dev,
79 struct device_attribute *attr, char *buf)
80{
81 struct dax_region *dax_region;
82 ssize_t rc = -ENXIO;
83
84 device_lock(dev);
85 dax_region = dev_get_drvdata(dev);
86 if (dax_region)
87 rc = sprintf(buf, "%d\n", dax_region->id);
88 device_unlock(dev);
89
90 return rc;
91}
92static DEVICE_ATTR_RO(id);
93
94static ssize_t region_size_show(struct device *dev,
95 struct device_attribute *attr, char *buf)
96{
97 struct dax_region *dax_region;
98 ssize_t rc = -ENXIO;
99
100 device_lock(dev);
101 dax_region = dev_get_drvdata(dev);
102 if (dax_region)
103 rc = sprintf(buf, "%llu\n", (unsigned long long)
104 resource_size(&dax_region->res));
105 device_unlock(dev);
106
107 return rc;
108}
109static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
110 region_size_show, NULL);
111
112static ssize_t align_show(struct device *dev,
113 struct device_attribute *attr, char *buf)
114{
115 struct dax_region *dax_region;
116 ssize_t rc = -ENXIO;
117
118 device_lock(dev);
119 dax_region = dev_get_drvdata(dev);
120 if (dax_region)
121 rc = sprintf(buf, "%u\n", dax_region->align);
122 device_unlock(dev);
123
124 return rc;
125}
126static DEVICE_ATTR_RO(align);
127
128static struct attribute *dax_region_attributes[] = {
129 &dev_attr_region_size.attr,
130 &dev_attr_align.attr,
131 &dev_attr_id.attr,
132 NULL,
133};
134
135static const struct attribute_group dax_region_attribute_group = {
136 .name = "dax_region",
137 .attrs = dax_region_attributes,
138};
139
140static const struct attribute_group *dax_region_attribute_groups[] = {
141 &dax_region_attribute_group,
142 NULL,
143};
144
3bc52c45 145static struct inode *dax_alloc_inode(struct super_block *sb)
ab68f262 146{
3bc52c45
DW
147 return kmem_cache_alloc(dax_cache, GFP_KERNEL);
148}
ab68f262 149
3bc52c45
DW
150static void dax_i_callback(struct rcu_head *head)
151{
152 struct inode *inode = container_of(head, struct inode, i_rcu);
153
154 kmem_cache_free(dax_cache, inode);
ab68f262
DW
155}
156
3bc52c45 157static void dax_destroy_inode(struct inode *inode)
ab68f262 158{
3bc52c45 159 call_rcu(&inode->i_rcu, dax_i_callback);
ab68f262 160}
ab68f262 161
3bc52c45
DW
162static const struct super_operations dax_sops = {
163 .statfs = simple_statfs,
164 .alloc_inode = dax_alloc_inode,
165 .destroy_inode = dax_destroy_inode,
166 .drop_inode = generic_delete_inode,
167};
168
169static struct dentry *dax_mount(struct file_system_type *fs_type,
170 int flags, const char *dev_name, void *data)
ab68f262 171{
3bc52c45
DW
172 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
173}
ab68f262 174
3bc52c45
DW
175static struct file_system_type dax_type = {
176 .name = "dax",
177 .mount = dax_mount,
178 .kill_sb = kill_anon_super,
179};
180
181static int dax_test(struct inode *inode, void *data)
182{
183 return inode->i_cdev == data;
184}
185
186static int dax_set(struct inode *inode, void *data)
187{
188 inode->i_cdev = data;
189 return 0;
190}
191
192static struct inode *dax_inode_get(struct cdev *cdev, dev_t devt)
193{
194 struct inode *inode;
195
196 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
197 dax_test, dax_set, cdev);
198
199 if (!inode)
200 return NULL;
201
202 if (inode->i_state & I_NEW) {
203 inode->i_mode = S_IFCHR;
204 inode->i_flags = S_DAX;
205 inode->i_rdev = devt;
206 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
207 unlock_new_inode(inode);
208 }
209 return inode;
210}
211
212static void init_once(void *inode)
213{
214 inode_init_once(inode);
215}
216
217static int dax_inode_init(void)
218{
219 int rc;
220
221 dax_cache = kmem_cache_create("dax_cache", sizeof(struct inode), 0,
222 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
223 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
224 init_once);
225 if (!dax_cache)
226 return -ENOMEM;
227
228 rc = register_filesystem(&dax_type);
229 if (rc)
230 goto err_register_fs;
231
232 dax_mnt = kern_mount(&dax_type);
233 if (IS_ERR(dax_mnt)) {
234 rc = PTR_ERR(dax_mnt);
235 goto err_mount;
236 }
237 dax_superblock = dax_mnt->mnt_sb;
238
239 return 0;
240
241 err_mount:
242 unregister_filesystem(&dax_type);
243 err_register_fs:
244 kmem_cache_destroy(dax_cache);
245
246 return rc;
ab68f262
DW
247}
248
3bc52c45
DW
249static void dax_inode_exit(void)
250{
251 kern_unmount(dax_mnt);
252 unregister_filesystem(&dax_type);
253 kmem_cache_destroy(dax_cache);
254}
255
ab68f262
DW
256static void dax_region_free(struct kref *kref)
257{
258 struct dax_region *dax_region;
259
260 dax_region = container_of(kref, struct dax_region, kref);
261 kfree(dax_region);
262}
263
264void dax_region_put(struct dax_region *dax_region)
ab68f262 265{
ab68f262 266 kref_put(&dax_region->kref, dax_region_free);
ab68f262 267}
ab68f262 268EXPORT_SYMBOL_GPL(dax_region_put);
ab68f262 269
d7fe1a67
DW
270static void dax_region_unregister(void *region)
271{
272 struct dax_region *dax_region = region;
273
274 sysfs_remove_groups(&dax_region->dev->kobj,
275 dax_region_attribute_groups);
276 dax_region_put(dax_region);
277}
278
ab68f262
DW
279struct dax_region *alloc_dax_region(struct device *parent, int region_id,
280 struct resource *res, unsigned int align, void *addr,
281 unsigned long pfn_flags)
282{
283 struct dax_region *dax_region;
284
d7fe1a67
DW
285 /*
286 * The DAX core assumes that it can store its private data in
287 * parent->driver_data. This WARN is a reminder / safeguard for
288 * developers of device-dax drivers.
289 */
290 if (dev_get_drvdata(parent)) {
291 dev_WARN(parent, "dax core failed to setup private data\n");
292 return NULL;
293 }
294
9d2d01a0
DW
295 if (!IS_ALIGNED(res->start, align)
296 || !IS_ALIGNED(resource_size(res), align))
297 return NULL;
ab68f262 298
9d2d01a0 299 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
ab68f262
DW
300 if (!dax_region)
301 return NULL;
302
d7fe1a67 303 dev_set_drvdata(parent, dax_region);
ab68f262
DW
304 memcpy(&dax_region->res, res, sizeof(*res));
305 dax_region->pfn_flags = pfn_flags;
306 kref_init(&dax_region->kref);
307 dax_region->id = region_id;
308 ida_init(&dax_region->ida);
309 dax_region->align = align;
310 dax_region->dev = parent;
311 dax_region->base = addr;
d7fe1a67
DW
312 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
313 kfree(dax_region);
314 return NULL;;
315 }
ab68f262 316
d7fe1a67
DW
317 kref_get(&dax_region->kref);
318 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
319 return NULL;
ab68f262
DW
320 return dax_region;
321}
322EXPORT_SYMBOL_GPL(alloc_dax_region);
323
ebd84d72
DW
324static struct dax_dev *to_dax_dev(struct device *dev)
325{
326 return container_of(dev, struct dax_dev, dev);
327}
328
ab68f262
DW
329static ssize_t size_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
331{
ebd84d72 332 struct dax_dev *dax_dev = to_dax_dev(dev);
ab68f262
DW
333 unsigned long long size = 0;
334 int i;
335
336 for (i = 0; i < dax_dev->num_resources; i++)
337 size += resource_size(&dax_dev->res[i]);
338
339 return sprintf(buf, "%llu\n", size);
340}
341static DEVICE_ATTR_RO(size);
342
343static struct attribute *dax_device_attributes[] = {
344 &dev_attr_size.attr,
345 NULL,
346};
347
348static const struct attribute_group dax_device_attribute_group = {
349 .attrs = dax_device_attributes,
350};
351
352static const struct attribute_group *dax_attribute_groups[] = {
353 &dax_device_attribute_group,
354 NULL,
355};
356
dee41079
DW
357static int check_vma(struct dax_dev *dax_dev, struct vm_area_struct *vma,
358 const char *func)
359{
360 struct dax_region *dax_region = dax_dev->region;
ebd84d72 361 struct device *dev = &dax_dev->dev;
dee41079
DW
362 unsigned long mask;
363
364 if (!dax_dev->alive)
365 return -ENXIO;
366
4cb19355 367 /* prevent private mappings from being established */
325896ff 368 if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) {
dee41079
DW
369 dev_info(dev, "%s: %s: fail, attempted private mapping\n",
370 current->comm, func);
371 return -EINVAL;
372 }
373
374 mask = dax_region->align - 1;
375 if (vma->vm_start & mask || vma->vm_end & mask) {
376 dev_info(dev, "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n",
377 current->comm, func, vma->vm_start, vma->vm_end,
378 mask);
379 return -EINVAL;
380 }
381
382 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV
383 && (vma->vm_flags & VM_DONTCOPY) == 0) {
384 dev_info(dev, "%s: %s: fail, dax range requires MADV_DONTFORK\n",
385 current->comm, func);
386 return -EINVAL;
387 }
388
389 if (!vma_is_dax(vma)) {
390 dev_info(dev, "%s: %s: fail, vma is not DAX capable\n",
391 current->comm, func);
392 return -EINVAL;
393 }
394
395 return 0;
396}
397
398static phys_addr_t pgoff_to_phys(struct dax_dev *dax_dev, pgoff_t pgoff,
399 unsigned long size)
400{
401 struct resource *res;
402 phys_addr_t phys;
403 int i;
404
405 for (i = 0; i < dax_dev->num_resources; i++) {
406 res = &dax_dev->res[i];
407 phys = pgoff * PAGE_SIZE + res->start;
408 if (phys >= res->start && phys <= res->end)
409 break;
410 pgoff -= PHYS_PFN(resource_size(res));
411 }
412
413 if (i < dax_dev->num_resources) {
414 res = &dax_dev->res[i];
415 if (phys + size - 1 <= res->end)
416 return phys;
417 }
418
419 return -1;
420}
421
a2d58167 422static int __dax_dev_pte_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
dee41079 423{
ebd84d72 424 struct device *dev = &dax_dev->dev;
dee41079
DW
425 struct dax_region *dax_region;
426 int rc = VM_FAULT_SIGBUS;
427 phys_addr_t phys;
428 pfn_t pfn;
429
11bac800 430 if (check_vma(dax_dev, vmf->vma, __func__))
dee41079
DW
431 return VM_FAULT_SIGBUS;
432
433 dax_region = dax_dev->region;
434 if (dax_region->align > PAGE_SIZE) {
435 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
436 return VM_FAULT_SIGBUS;
437 }
438
439 phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE);
440 if (phys == -1) {
441 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
442 vmf->pgoff);
443 return VM_FAULT_SIGBUS;
444 }
445
446 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
447
11bac800 448 rc = vm_insert_mixed(vmf->vma, vmf->address, pfn);
dee41079
DW
449
450 if (rc == -ENOMEM)
451 return VM_FAULT_OOM;
452 if (rc < 0 && rc != -EBUSY)
453 return VM_FAULT_SIGBUS;
454
455 return VM_FAULT_NOPAGE;
456}
457
f4200391 458static int __dax_dev_pmd_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
dee41079 459{
d8a849e1 460 unsigned long pmd_addr = vmf->address & PMD_MASK;
ebd84d72 461 struct device *dev = &dax_dev->dev;
dee41079
DW
462 struct dax_region *dax_region;
463 phys_addr_t phys;
464 pgoff_t pgoff;
465 pfn_t pfn;
466
f4200391 467 if (check_vma(dax_dev, vmf->vma, __func__))
dee41079
DW
468 return VM_FAULT_SIGBUS;
469
470 dax_region = dax_dev->region;
471 if (dax_region->align > PMD_SIZE) {
472 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
473 return VM_FAULT_SIGBUS;
474 }
475
476 /* dax pmd mappings require pfn_t_devmap() */
477 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
478 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
479 return VM_FAULT_SIGBUS;
480 }
481
f4200391 482 pgoff = linear_page_index(vmf->vma, pmd_addr);
4c3cb6e9 483 phys = pgoff_to_phys(dax_dev, pgoff, PMD_SIZE);
dee41079
DW
484 if (phys == -1) {
485 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
486 pgoff);
487 return VM_FAULT_SIGBUS;
488 }
489
490 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
491
f4200391 492 return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd, pfn,
d8a849e1 493 vmf->flags & FAULT_FLAG_WRITE);
dee41079
DW
494}
495
9557feee
DJ
496#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
497static int __dax_dev_pud_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
498{
499 unsigned long pud_addr = vmf->address & PUD_MASK;
500 struct device *dev = &dax_dev->dev;
501 struct dax_region *dax_region;
502 phys_addr_t phys;
503 pgoff_t pgoff;
504 pfn_t pfn;
505
506 if (check_vma(dax_dev, vmf->vma, __func__))
507 return VM_FAULT_SIGBUS;
508
509 dax_region = dax_dev->region;
510 if (dax_region->align > PUD_SIZE) {
511 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
512 return VM_FAULT_SIGBUS;
513 }
514
515 /* dax pud mappings require pfn_t_devmap() */
516 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
517 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
518 return VM_FAULT_SIGBUS;
519 }
520
521 pgoff = linear_page_index(vmf->vma, pud_addr);
522 phys = pgoff_to_phys(dax_dev, pgoff, PUD_SIZE);
523 if (phys == -1) {
524 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
525 pgoff);
526 return VM_FAULT_SIGBUS;
527 }
528
529 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
530
531 return vmf_insert_pfn_pud(vmf->vma, vmf->address, vmf->pud, pfn,
532 vmf->flags & FAULT_FLAG_WRITE);
533}
534#else
535static int __dax_dev_pud_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
536{
537 return VM_FAULT_FALLBACK;
538}
539#endif /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
540
a2d58167 541static int dax_dev_fault(struct vm_fault *vmf)
dee41079
DW
542{
543 int rc;
f4200391 544 struct file *filp = vmf->vma->vm_file;
dee41079
DW
545 struct dax_dev *dax_dev = filp->private_data;
546
ebd84d72 547 dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
d8a849e1 548 current->comm, (vmf->flags & FAULT_FLAG_WRITE)
f4200391
DJ
549 ? "write" : "read",
550 vmf->vma->vm_start, vmf->vma->vm_end);
dee41079
DW
551
552 rcu_read_lock();
a2d58167
DJ
553 switch (vmf->flags & FAULT_FLAG_SIZE_MASK) {
554 case FAULT_FLAG_SIZE_PTE:
555 rc = __dax_dev_pte_fault(dax_dev, vmf);
556 break;
557 case FAULT_FLAG_SIZE_PMD:
558 rc = __dax_dev_pmd_fault(dax_dev, vmf);
9557feee
DJ
559 break;
560 case FAULT_FLAG_SIZE_PUD:
561 rc = __dax_dev_pud_fault(dax_dev, vmf);
a2d58167
DJ
562 break;
563 default:
564 return VM_FAULT_FALLBACK;
565 }
dee41079
DW
566 rcu_read_unlock();
567
568 return rc;
569}
570
dee41079
DW
571static const struct vm_operations_struct dax_dev_vm_ops = {
572 .fault = dax_dev_fault,
a2d58167 573 .huge_fault = dax_dev_fault,
dee41079
DW
574};
575
af69f51e 576static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
dee41079
DW
577{
578 struct dax_dev *dax_dev = filp->private_data;
579 int rc;
580
ebd84d72 581 dev_dbg(&dax_dev->dev, "%s\n", __func__);
dee41079
DW
582
583 rc = check_vma(dax_dev, vma, __func__);
584 if (rc)
585 return rc;
586
dee41079
DW
587 vma->vm_ops = &dax_dev_vm_ops;
588 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
589 return 0;
043a9255
DW
590}
591
592/* return an unmapped area aligned to the dax region specified alignment */
af69f51e 593static unsigned long dax_get_unmapped_area(struct file *filp,
043a9255
DW
594 unsigned long addr, unsigned long len, unsigned long pgoff,
595 unsigned long flags)
596{
597 unsigned long off, off_end, off_align, len_align, addr_align, align;
598 struct dax_dev *dax_dev = filp ? filp->private_data : NULL;
599 struct dax_region *dax_region;
600
601 if (!dax_dev || addr)
602 goto out;
603
604 dax_region = dax_dev->region;
605 align = dax_region->align;
606 off = pgoff << PAGE_SHIFT;
607 off_end = off + len;
608 off_align = round_up(off, align);
609
610 if ((off_end <= off_align) || ((off_end - off_align) < align))
611 goto out;
612
613 len_align = len + align;
614 if ((off + len_align) < off)
615 goto out;
dee41079 616
043a9255
DW
617 addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
618 pgoff, flags);
619 if (!IS_ERR_VALUE(addr_align)) {
620 addr_align += (off - addr_align) & (align - 1);
621 return addr_align;
622 }
623 out:
624 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
625}
626
af69f51e 627static int dax_open(struct inode *inode, struct file *filp)
043a9255 628{
ba09c01d 629 struct dax_dev *dax_dev;
043a9255 630
ba09c01d
DW
631 dax_dev = container_of(inode->i_cdev, struct dax_dev, cdev);
632 dev_dbg(&dax_dev->dev, "%s\n", __func__);
3bc52c45
DW
633 inode->i_mapping = dax_dev->inode->i_mapping;
634 inode->i_mapping->host = dax_dev->inode;
635 filp->f_mapping = inode->i_mapping;
ebd84d72
DW
636 filp->private_data = dax_dev;
637 inode->i_flags = S_DAX;
043a9255 638
043a9255
DW
639 return 0;
640}
dee41079 641
af69f51e 642static int dax_release(struct inode *inode, struct file *filp)
043a9255
DW
643{
644 struct dax_dev *dax_dev = filp->private_data;
043a9255 645
ba09c01d 646 dev_dbg(&dax_dev->dev, "%s\n", __func__);
043a9255 647 return 0;
dee41079
DW
648}
649
ab68f262
DW
650static const struct file_operations dax_fops = {
651 .llseek = noop_llseek,
652 .owner = THIS_MODULE,
af69f51e
DW
653 .open = dax_open,
654 .release = dax_release,
655 .get_unmapped_area = dax_get_unmapped_area,
656 .mmap = dax_mmap,
ab68f262
DW
657};
658
ebd84d72 659static void dax_dev_release(struct device *dev)
043a9255 660{
ebd84d72 661 struct dax_dev *dax_dev = to_dax_dev(dev);
043a9255
DW
662 struct dax_region *dax_region = dax_dev->region;
663
ebd84d72
DW
664 ida_simple_remove(&dax_region->ida, dax_dev->id);
665 ida_simple_remove(&dax_minor_ida, MINOR(dev->devt));
666 dax_region_put(dax_region);
3bc52c45 667 iput(dax_dev->inode);
ebd84d72
DW
668 kfree(dax_dev);
669}
670
671static void unregister_dax_dev(void *dev)
672{
673 struct dax_dev *dax_dev = to_dax_dev(dev);
ba09c01d 674 struct cdev *cdev = &dax_dev->cdev;
ebd84d72 675
043a9255
DW
676 dev_dbg(dev, "%s\n", __func__);
677
678 /*
679 * Note, rcu is not protecting the liveness of dax_dev, rcu is
680 * ensuring that any fault handlers that might have seen
681 * dax_dev->alive == true, have completed. Any fault handlers
682 * that start after synchronize_rcu() has started will abort
683 * upon seeing dax_dev->alive == false.
684 */
685 dax_dev->alive = false;
686 synchronize_rcu();
9dc1e492 687 unmap_mapping_range(dax_dev->inode->i_mapping, 0, 0, 1);
ba09c01d 688 cdev_del(cdev);
043a9255 689 device_unregister(dev);
043a9255
DW
690}
691
d76911ee
DW
692struct dax_dev *devm_create_dax_dev(struct dax_region *dax_region,
693 struct resource *res, int count)
043a9255
DW
694{
695 struct device *parent = dax_region->dev;
696 struct dax_dev *dax_dev;
9d2d01a0 697 int rc = 0, minor, i;
043a9255 698 struct device *dev;
ba09c01d 699 struct cdev *cdev;
043a9255
DW
700 dev_t dev_t;
701
702 dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
703 if (!dax_dev)
d76911ee 704 return ERR_PTR(-ENOMEM);
043a9255 705
9d2d01a0
DW
706 for (i = 0; i < count; i++) {
707 if (!IS_ALIGNED(res[i].start, dax_region->align)
708 || !IS_ALIGNED(resource_size(&res[i]),
709 dax_region->align)) {
710 rc = -EINVAL;
711 break;
712 }
713 dax_dev->res[i].start = res[i].start;
714 dax_dev->res[i].end = res[i].end;
715 }
716
717 if (i < count)
718 goto err_id;
719
043a9255
DW
720 dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
721 if (dax_dev->id < 0) {
722 rc = dax_dev->id;
723 goto err_id;
724 }
725
726 minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL);
727 if (minor < 0) {
728 rc = minor;
729 goto err_minor;
730 }
731
bc0a0fe9
AB
732 dev_t = MKDEV(MAJOR(dax_devt), minor);
733 dev = &dax_dev->dev;
3bc52c45
DW
734 dax_dev->inode = dax_inode_get(&dax_dev->cdev, dev_t);
735 if (!dax_dev->inode) {
736 rc = -ENOMEM;
737 goto err_inode;
738 }
739
ba09c01d 740 /* device_initialize() so cdev can reference kobj parent */
ebd84d72 741 device_initialize(dev);
ba09c01d
DW
742
743 cdev = &dax_dev->cdev;
744 cdev_init(cdev, &dax_fops);
745 cdev->owner = parent->driver->owner;
746 cdev->kobj.parent = &dev->kobj;
747 rc = cdev_add(&dax_dev->cdev, dev_t, 1);
748 if (rc)
749 goto err_cdev;
750
751 /* from here on we're committed to teardown via dax_dev_release() */
ba09c01d
DW
752 dax_dev->num_resources = count;
753 dax_dev->alive = true;
754 dax_dev->region = dax_region;
755 kref_get(&dax_region->kref);
756
ebd84d72
DW
757 dev->devt = dev_t;
758 dev->class = dax_class;
759 dev->parent = parent;
760 dev->groups = dax_attribute_groups;
761 dev->release = dax_dev_release;
762 dev_set_name(dev, "dax%d.%d", dax_region->id, dax_dev->id);
763 rc = device_add(dev);
764 if (rc) {
765 put_device(dev);
d76911ee 766 return ERR_PTR(rc);
ebd84d72 767 }
043a9255 768
d76911ee
DW
769 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev);
770 if (rc)
771 return ERR_PTR(rc);
772
773 return dax_dev;
043a9255 774
ba09c01d 775 err_cdev:
3bc52c45
DW
776 iput(dax_dev->inode);
777 err_inode:
ba09c01d 778 ida_simple_remove(&dax_minor_ida, minor);
043a9255
DW
779 err_minor:
780 ida_simple_remove(&dax_region->ida, dax_dev->id);
781 err_id:
ebd84d72 782 kfree(dax_dev);
043a9255 783
d76911ee 784 return ERR_PTR(rc);
043a9255
DW
785}
786EXPORT_SYMBOL_GPL(devm_create_dax_dev);
787
ab68f262
DW
788static int __init dax_init(void)
789{
790 int rc;
791
3bc52c45
DW
792 rc = dax_inode_init();
793 if (rc)
ab68f262 794 return rc;
3bc52c45 795
ba09c01d
DW
796 nr_dax = max(nr_dax, 256);
797 rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
798 if (rc)
3bc52c45 799 goto err_chrdev;
ab68f262
DW
800
801 dax_class = class_create(THIS_MODULE, "dax");
802 if (IS_ERR(dax_class)) {
3bc52c45
DW
803 rc = PTR_ERR(dax_class);
804 goto err_class;
ab68f262
DW
805 }
806
807 return 0;
3bc52c45
DW
808
809 err_class:
810 unregister_chrdev_region(dax_devt, nr_dax);
811 err_chrdev:
812 dax_inode_exit();
813 return rc;
ab68f262
DW
814}
815
816static void __exit dax_exit(void)
817{
818 class_destroy(dax_class);
ba09c01d 819 unregister_chrdev_region(dax_devt, nr_dax);
ab68f262 820 ida_destroy(&dax_minor_ida);
3bc52c45 821 dax_inode_exit();
ab68f262
DW
822}
823
824MODULE_AUTHOR("Intel Corporation");
825MODULE_LICENSE("GPL v2");
826subsys_initcall(dax_init);
827module_exit(dax_exit);