blob: 369fccda3b1b04c33ac603a218aa4b30dc518962 [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>
Dan Williams3bc52c42016-07-24 21:55:45 -070016#include <linux/mount.h>
Dan Williamsab68f262016-05-18 09:15:08 -070017#include <linux/pfn_t.h>
Dan Williams3bc52c42016-07-24 21:55:45 -070018#include <linux/hash.h>
Dan Williamsba09c012016-07-24 15:55:42 -070019#include <linux/cdev.h>
Dan Williamsab68f262016-05-18 09:15:08 -070020#include <linux/slab.h>
21#include <linux/dax.h>
22#include <linux/fs.h>
23#include <linux/mm.h>
Dan Williamsccdb07f2016-08-06 16:05:06 -070024#include "dax.h"
Dan Williamsab68f262016-05-18 09:15:08 -070025
Dan Williamsba09c012016-07-24 15:55:42 -070026static dev_t dax_devt;
Dan Williamsab68f262016-05-18 09:15:08 -070027static struct class *dax_class;
28static DEFINE_IDA(dax_minor_ida);
Dan Williamsba09c012016-07-24 15:55:42 -070029static int nr_dax = CONFIG_NR_DEV_DAX;
30module_param(nr_dax, int, S_IRUGO);
Dan Williams3bc52c42016-07-24 21:55:45 -070031static struct vfsmount *dax_mnt;
32static struct kmem_cache *dax_cache __read_mostly;
33static struct super_block *dax_superblock __read_mostly;
Dan Williamsba09c012016-07-24 15:55:42 -070034MODULE_PARM_DESC(nr_dax, "max number of device-dax instances");
Dan Williamsab68f262016-05-18 09:15:08 -070035
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
Dan Williamsba09c012016-07-24 15:55:42 -070061 * @cdev - core chardev data
Dan Williamsdee41072016-05-14 12:20:44 -070062 * @alive - !alive + rcu grace period == no new mappings can be established
Dan Williamsab68f262016-05-18 09:15:08 -070063 * @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;
Dan Williams3bc52c42016-07-24 21:55:45 -070069 struct inode *inode;
Dan Williamsebd84d72016-08-11 00:41:51 -070070 struct device dev;
Dan Williamsba09c012016-07-24 15:55:42 -070071 struct cdev cdev;
Dan Williamsdee41072016-05-14 12:20:44 -070072 bool alive;
Dan Williamsab68f262016-05-18 09:15:08 -070073 int id;
74 int num_resources;
75 struct resource res[0];
76};
77
Dan Williamsd7fe1a62016-12-17 14:50:04 -080078static 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
Dan Williams3bc52c42016-07-24 21:55:45 -0700145static struct inode *dax_alloc_inode(struct super_block *sb)
146{
147 return kmem_cache_alloc(dax_cache, GFP_KERNEL);
148}
149
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);
155}
156
157static void dax_destroy_inode(struct inode *inode)
158{
159 call_rcu(&inode->i_rcu, dax_i_callback);
160}
161
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)
171{
172 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
173}
174
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;
247}
248
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
Dan Williamsab68f262016-05-18 09:15:08 -0700256static 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)
265{
266 kref_put(&dax_region->kref, dax_region_free);
267}
268EXPORT_SYMBOL_GPL(dax_region_put);
269
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800270static 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
Dan Williamsab68f262016-05-18 09:15:08 -0700279struct 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
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800285 /*
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
Dan Williams9d2d01a2016-07-19 16:17:58 -0700295 if (!IS_ALIGNED(res->start, align)
296 || !IS_ALIGNED(resource_size(res), align))
297 return NULL;
Dan Williamsab68f262016-05-18 09:15:08 -0700298
Dan Williams9d2d01a2016-07-19 16:17:58 -0700299 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
Dan Williamsab68f262016-05-18 09:15:08 -0700300 if (!dax_region)
301 return NULL;
302
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800303 dev_set_drvdata(parent, dax_region);
Dan Williamsab68f262016-05-18 09:15:08 -0700304 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;
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800312 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
313 kfree(dax_region);
314 return NULL;;
315 }
Dan Williamsab68f262016-05-18 09:15:08 -0700316
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800317 kref_get(&dax_region->kref);
318 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
319 return NULL;
Dan Williamsab68f262016-05-18 09:15:08 -0700320 return dax_region;
321}
322EXPORT_SYMBOL_GPL(alloc_dax_region);
323
Dan Williamsebd84d72016-08-11 00:41:51 -0700324static struct dax_dev *to_dax_dev(struct device *dev)
325{
326 return container_of(dev, struct dax_dev, dev);
327}
328
Dan Williamsab68f262016-05-18 09:15:08 -0700329static ssize_t size_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
331{
Dan Williamsebd84d72016-08-11 00:41:51 -0700332 struct dax_dev *dax_dev = to_dax_dev(dev);
Dan Williamsab68f262016-05-18 09:15:08 -0700333 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
Dan Williamsdee41072016-05-14 12:20:44 -0700357static 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;
Dan Williamsebd84d72016-08-11 00:41:51 -0700361 struct device *dev = &dax_dev->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700362 unsigned long mask;
363
364 if (!dax_dev->alive)
365 return -ENXIO;
366
367 /* prevent private / writable mappings from being established */
368 if ((vma->vm_flags & (VM_NORESERVE|VM_SHARED|VM_WRITE)) == VM_WRITE) {
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
422static int __dax_dev_fault(struct dax_dev *dax_dev, struct vm_area_struct *vma,
423 struct vm_fault *vmf)
424{
425 unsigned long vaddr = (unsigned long) vmf->virtual_address;
Dan Williamsebd84d72016-08-11 00:41:51 -0700426 struct device *dev = &dax_dev->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700427 struct dax_region *dax_region;
428 int rc = VM_FAULT_SIGBUS;
429 phys_addr_t phys;
430 pfn_t pfn;
431
432 if (check_vma(dax_dev, vma, __func__))
433 return VM_FAULT_SIGBUS;
434
435 dax_region = dax_dev->region;
436 if (dax_region->align > PAGE_SIZE) {
437 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
438 return VM_FAULT_SIGBUS;
439 }
440
441 phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE);
442 if (phys == -1) {
443 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
444 vmf->pgoff);
445 return VM_FAULT_SIGBUS;
446 }
447
448 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
449
450 rc = vm_insert_mixed(vma, vaddr, pfn);
451
452 if (rc == -ENOMEM)
453 return VM_FAULT_OOM;
454 if (rc < 0 && rc != -EBUSY)
455 return VM_FAULT_SIGBUS;
456
457 return VM_FAULT_NOPAGE;
458}
459
460static int dax_dev_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
461{
462 int rc;
463 struct file *filp = vma->vm_file;
464 struct dax_dev *dax_dev = filp->private_data;
465
Dan Williamsebd84d72016-08-11 00:41:51 -0700466 dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
Dan Williamsdee41072016-05-14 12:20:44 -0700467 current->comm, (vmf->flags & FAULT_FLAG_WRITE)
468 ? "write" : "read", vma->vm_start, vma->vm_end);
469 rcu_read_lock();
470 rc = __dax_dev_fault(dax_dev, vma, vmf);
471 rcu_read_unlock();
472
473 return rc;
474}
475
476static int __dax_dev_pmd_fault(struct dax_dev *dax_dev,
477 struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd,
478 unsigned int flags)
479{
480 unsigned long pmd_addr = addr & PMD_MASK;
Dan Williamsebd84d72016-08-11 00:41:51 -0700481 struct device *dev = &dax_dev->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700482 struct dax_region *dax_region;
483 phys_addr_t phys;
484 pgoff_t pgoff;
485 pfn_t pfn;
486
487 if (check_vma(dax_dev, vma, __func__))
488 return VM_FAULT_SIGBUS;
489
490 dax_region = dax_dev->region;
491 if (dax_region->align > PMD_SIZE) {
492 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
493 return VM_FAULT_SIGBUS;
494 }
495
496 /* dax pmd mappings require pfn_t_devmap() */
497 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
498 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
499 return VM_FAULT_SIGBUS;
500 }
501
502 pgoff = linear_page_index(vma, pmd_addr);
Dan Williams4c3cb6e2016-09-03 10:36:00 -0700503 phys = pgoff_to_phys(dax_dev, pgoff, PMD_SIZE);
Dan Williamsdee41072016-05-14 12:20:44 -0700504 if (phys == -1) {
505 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
506 pgoff);
507 return VM_FAULT_SIGBUS;
508 }
509
510 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
511
512 return vmf_insert_pfn_pmd(vma, addr, pmd, pfn,
513 flags & FAULT_FLAG_WRITE);
514}
515
516static int dax_dev_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
517 pmd_t *pmd, unsigned int flags)
518{
519 int rc;
520 struct file *filp = vma->vm_file;
521 struct dax_dev *dax_dev = filp->private_data;
522
Dan Williamsebd84d72016-08-11 00:41:51 -0700523 dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
Dan Williamsdee41072016-05-14 12:20:44 -0700524 current->comm, (flags & FAULT_FLAG_WRITE)
525 ? "write" : "read", vma->vm_start, vma->vm_end);
526
527 rcu_read_lock();
528 rc = __dax_dev_pmd_fault(dax_dev, vma, addr, pmd, flags);
529 rcu_read_unlock();
530
531 return rc;
532}
533
Dan Williamsdee41072016-05-14 12:20:44 -0700534static const struct vm_operations_struct dax_dev_vm_ops = {
535 .fault = dax_dev_fault,
536 .pmd_fault = dax_dev_pmd_fault,
Dan Williamsdee41072016-05-14 12:20:44 -0700537};
538
Dan Williamsaf69f512016-08-11 00:38:03 -0700539static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
Dan Williamsdee41072016-05-14 12:20:44 -0700540{
541 struct dax_dev *dax_dev = filp->private_data;
542 int rc;
543
Dan Williamsebd84d72016-08-11 00:41:51 -0700544 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williamsdee41072016-05-14 12:20:44 -0700545
546 rc = check_vma(dax_dev, vma, __func__);
547 if (rc)
548 return rc;
549
Dan Williamsdee41072016-05-14 12:20:44 -0700550 vma->vm_ops = &dax_dev_vm_ops;
551 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
552 return 0;
Dan Williams043a9252016-08-07 08:23:56 -0700553}
Dan Williamsdee41072016-05-14 12:20:44 -0700554
Dan Williams043a9252016-08-07 08:23:56 -0700555/* return an unmapped area aligned to the dax region specified alignment */
Dan Williamsaf69f512016-08-11 00:38:03 -0700556static unsigned long dax_get_unmapped_area(struct file *filp,
Dan Williams043a9252016-08-07 08:23:56 -0700557 unsigned long addr, unsigned long len, unsigned long pgoff,
558 unsigned long flags)
559{
560 unsigned long off, off_end, off_align, len_align, addr_align, align;
561 struct dax_dev *dax_dev = filp ? filp->private_data : NULL;
562 struct dax_region *dax_region;
563
564 if (!dax_dev || addr)
565 goto out;
566
567 dax_region = dax_dev->region;
568 align = dax_region->align;
569 off = pgoff << PAGE_SHIFT;
570 off_end = off + len;
571 off_align = round_up(off, align);
572
573 if ((off_end <= off_align) || ((off_end - off_align) < align))
574 goto out;
575
576 len_align = len + align;
577 if ((off + len_align) < off)
578 goto out;
579
580 addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
581 pgoff, flags);
582 if (!IS_ERR_VALUE(addr_align)) {
583 addr_align += (off - addr_align) & (align - 1);
584 return addr_align;
585 }
586 out:
587 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
588}
589
Dan Williamsaf69f512016-08-11 00:38:03 -0700590static int dax_open(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700591{
Dan Williamsba09c012016-07-24 15:55:42 -0700592 struct dax_dev *dax_dev;
Dan Williams043a9252016-08-07 08:23:56 -0700593
Dan Williamsba09c012016-07-24 15:55:42 -0700594 dax_dev = container_of(inode->i_cdev, struct dax_dev, cdev);
595 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williams3bc52c42016-07-24 21:55:45 -0700596 inode->i_mapping = dax_dev->inode->i_mapping;
597 inode->i_mapping->host = dax_dev->inode;
598 filp->f_mapping = inode->i_mapping;
Dan Williamsebd84d72016-08-11 00:41:51 -0700599 filp->private_data = dax_dev;
600 inode->i_flags = S_DAX;
Dan Williams043a9252016-08-07 08:23:56 -0700601
Dan Williams043a9252016-08-07 08:23:56 -0700602 return 0;
603}
604
Dan Williamsaf69f512016-08-11 00:38:03 -0700605static int dax_release(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700606{
607 struct dax_dev *dax_dev = filp->private_data;
Dan Williams043a9252016-08-07 08:23:56 -0700608
Dan Williamsba09c012016-07-24 15:55:42 -0700609 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williams043a9252016-08-07 08:23:56 -0700610 return 0;
Dan Williamsdee41072016-05-14 12:20:44 -0700611}
612
Dan Williamsab68f262016-05-18 09:15:08 -0700613static const struct file_operations dax_fops = {
614 .llseek = noop_llseek,
615 .owner = THIS_MODULE,
Dan Williamsaf69f512016-08-11 00:38:03 -0700616 .open = dax_open,
617 .release = dax_release,
618 .get_unmapped_area = dax_get_unmapped_area,
619 .mmap = dax_mmap,
Dan Williamsab68f262016-05-18 09:15:08 -0700620};
621
Dan Williamsebd84d72016-08-11 00:41:51 -0700622static void dax_dev_release(struct device *dev)
Dan Williams043a9252016-08-07 08:23:56 -0700623{
Dan Williamsebd84d72016-08-11 00:41:51 -0700624 struct dax_dev *dax_dev = to_dax_dev(dev);
Dan Williams043a9252016-08-07 08:23:56 -0700625 struct dax_region *dax_region = dax_dev->region;
626
Dan Williamsebd84d72016-08-11 00:41:51 -0700627 ida_simple_remove(&dax_region->ida, dax_dev->id);
628 ida_simple_remove(&dax_minor_ida, MINOR(dev->devt));
629 dax_region_put(dax_region);
Dan Williams3bc52c42016-07-24 21:55:45 -0700630 iput(dax_dev->inode);
Dan Williamsebd84d72016-08-11 00:41:51 -0700631 kfree(dax_dev);
632}
633
634static void unregister_dax_dev(void *dev)
635{
636 struct dax_dev *dax_dev = to_dax_dev(dev);
Dan Williamsba09c012016-07-24 15:55:42 -0700637 struct cdev *cdev = &dax_dev->cdev;
Dan Williamsebd84d72016-08-11 00:41:51 -0700638
Dan Williams043a9252016-08-07 08:23:56 -0700639 dev_dbg(dev, "%s\n", __func__);
640
641 /*
642 * Note, rcu is not protecting the liveness of dax_dev, rcu is
643 * ensuring that any fault handlers that might have seen
644 * dax_dev->alive == true, have completed. Any fault handlers
645 * that start after synchronize_rcu() has started will abort
646 * upon seeing dax_dev->alive == false.
647 */
648 dax_dev->alive = false;
649 synchronize_rcu();
Dan Williams9dc1e492016-08-04 16:53:50 -0700650 unmap_mapping_range(dax_dev->inode->i_mapping, 0, 0, 1);
Dan Williamsba09c012016-07-24 15:55:42 -0700651 cdev_del(cdev);
Dan Williams043a9252016-08-07 08:23:56 -0700652 device_unregister(dev);
Dan Williams043a9252016-08-07 08:23:56 -0700653}
654
Dan Williamsd76911e2016-07-19 17:51:40 -0700655struct dax_dev *devm_create_dax_dev(struct dax_region *dax_region,
656 struct resource *res, int count)
Dan Williams043a9252016-08-07 08:23:56 -0700657{
658 struct device *parent = dax_region->dev;
659 struct dax_dev *dax_dev;
Dan Williams9d2d01a2016-07-19 16:17:58 -0700660 int rc = 0, minor, i;
Dan Williams043a9252016-08-07 08:23:56 -0700661 struct device *dev;
Dan Williamsba09c012016-07-24 15:55:42 -0700662 struct cdev *cdev;
Dan Williams043a9252016-08-07 08:23:56 -0700663 dev_t dev_t;
664
665 dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
666 if (!dax_dev)
Dan Williamsd76911e2016-07-19 17:51:40 -0700667 return ERR_PTR(-ENOMEM);
Dan Williams043a9252016-08-07 08:23:56 -0700668
Dan Williams9d2d01a2016-07-19 16:17:58 -0700669 for (i = 0; i < count; i++) {
670 if (!IS_ALIGNED(res[i].start, dax_region->align)
671 || !IS_ALIGNED(resource_size(&res[i]),
672 dax_region->align)) {
673 rc = -EINVAL;
674 break;
675 }
676 dax_dev->res[i].start = res[i].start;
677 dax_dev->res[i].end = res[i].end;
678 }
679
680 if (i < count)
681 goto err_id;
682
Dan Williams043a9252016-08-07 08:23:56 -0700683 dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
684 if (dax_dev->id < 0) {
685 rc = dax_dev->id;
686 goto err_id;
687 }
688
689 minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL);
690 if (minor < 0) {
691 rc = minor;
692 goto err_minor;
693 }
694
Arnd Bergmannbc0a0fe2016-09-08 15:53:28 +0200695 dev_t = MKDEV(MAJOR(dax_devt), minor);
696 dev = &dax_dev->dev;
Dan Williams3bc52c42016-07-24 21:55:45 -0700697 dax_dev->inode = dax_inode_get(&dax_dev->cdev, dev_t);
698 if (!dax_dev->inode) {
699 rc = -ENOMEM;
700 goto err_inode;
701 }
702
Dan Williamsba09c012016-07-24 15:55:42 -0700703 /* device_initialize() so cdev can reference kobj parent */
Dan Williamsebd84d72016-08-11 00:41:51 -0700704 device_initialize(dev);
Dan Williamsba09c012016-07-24 15:55:42 -0700705
706 cdev = &dax_dev->cdev;
707 cdev_init(cdev, &dax_fops);
708 cdev->owner = parent->driver->owner;
709 cdev->kobj.parent = &dev->kobj;
710 rc = cdev_add(&dax_dev->cdev, dev_t, 1);
711 if (rc)
712 goto err_cdev;
713
714 /* from here on we're committed to teardown via dax_dev_release() */
Dan Williamsba09c012016-07-24 15:55:42 -0700715 dax_dev->num_resources = count;
716 dax_dev->alive = true;
717 dax_dev->region = dax_region;
718 kref_get(&dax_region->kref);
719
Dan Williamsebd84d72016-08-11 00:41:51 -0700720 dev->devt = dev_t;
721 dev->class = dax_class;
722 dev->parent = parent;
723 dev->groups = dax_attribute_groups;
724 dev->release = dax_dev_release;
725 dev_set_name(dev, "dax%d.%d", dax_region->id, dax_dev->id);
726 rc = device_add(dev);
727 if (rc) {
728 put_device(dev);
Dan Williamsd76911e2016-07-19 17:51:40 -0700729 return ERR_PTR(rc);
Dan Williamsebd84d72016-08-11 00:41:51 -0700730 }
Dan Williams043a9252016-08-07 08:23:56 -0700731
Dan Williamsd76911e2016-07-19 17:51:40 -0700732 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev);
733 if (rc)
734 return ERR_PTR(rc);
735
736 return dax_dev;
Dan Williams043a9252016-08-07 08:23:56 -0700737
Dan Williamsba09c012016-07-24 15:55:42 -0700738 err_cdev:
Dan Williams3bc52c42016-07-24 21:55:45 -0700739 iput(dax_dev->inode);
740 err_inode:
Dan Williamsba09c012016-07-24 15:55:42 -0700741 ida_simple_remove(&dax_minor_ida, minor);
Dan Williams043a9252016-08-07 08:23:56 -0700742 err_minor:
743 ida_simple_remove(&dax_region->ida, dax_dev->id);
744 err_id:
Dan Williamsebd84d72016-08-11 00:41:51 -0700745 kfree(dax_dev);
Dan Williams043a9252016-08-07 08:23:56 -0700746
Dan Williamsd76911e2016-07-19 17:51:40 -0700747 return ERR_PTR(rc);
Dan Williams043a9252016-08-07 08:23:56 -0700748}
749EXPORT_SYMBOL_GPL(devm_create_dax_dev);
750
Dan Williamsab68f262016-05-18 09:15:08 -0700751static int __init dax_init(void)
752{
753 int rc;
754
Dan Williams3bc52c42016-07-24 21:55:45 -0700755 rc = dax_inode_init();
Dan Williamsba09c012016-07-24 15:55:42 -0700756 if (rc)
Dan Williamsab68f262016-05-18 09:15:08 -0700757 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700758
Dan Williams3bc52c42016-07-24 21:55:45 -0700759 nr_dax = max(nr_dax, 256);
760 rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
761 if (rc)
762 goto err_chrdev;
Dan Williamsab68f262016-05-18 09:15:08 -0700763
764 dax_class = class_create(THIS_MODULE, "dax");
765 if (IS_ERR(dax_class)) {
Dan Williams3bc52c42016-07-24 21:55:45 -0700766 rc = PTR_ERR(dax_class);
767 goto err_class;
Dan Williamsab68f262016-05-18 09:15:08 -0700768 }
769
770 return 0;
Dan Williams3bc52c42016-07-24 21:55:45 -0700771
772 err_class:
773 unregister_chrdev_region(dax_devt, nr_dax);
774 err_chrdev:
775 dax_inode_exit();
776 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700777}
778
779static void __exit dax_exit(void)
780{
781 class_destroy(dax_class);
Dan Williamsba09c012016-07-24 15:55:42 -0700782 unregister_chrdev_region(dax_devt, nr_dax);
Dan Williamsab68f262016-05-18 09:15:08 -0700783 ida_destroy(&dax_minor_ida);
Dan Williams3bc52c42016-07-24 21:55:45 -0700784 dax_inode_exit();
Dan Williamsab68f262016-05-18 09:15:08 -0700785}
786
787MODULE_AUTHOR("Intel Corporation");
788MODULE_LICENSE("GPL v2");
789subsys_initcall(dax_init);
790module_exit(dax_exit);