blob: 473b44c008dd3c1a5997f2b6d348bb50aa5b1a33 [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 Williamsc36eaa62017-04-07 16:42:08 -070027DEFINE_STATIC_SRCU(dax_srcu);
Dan Williamsab68f262016-05-18 09:15:08 -070028static struct class *dax_class;
29static DEFINE_IDA(dax_minor_ida);
Dan Williamsba09c012016-07-24 15:55:42 -070030static int nr_dax = CONFIG_NR_DEV_DAX;
31module_param(nr_dax, int, S_IRUGO);
Dan Williams3bc52c42016-07-24 21:55:45 -070032static struct vfsmount *dax_mnt;
33static struct kmem_cache *dax_cache __read_mostly;
34static struct super_block *dax_superblock __read_mostly;
Dan Williamsba09c012016-07-24 15:55:42 -070035MODULE_PARM_DESC(nr_dax, "max number of device-dax instances");
Dan Williamsab68f262016-05-18 09:15:08 -070036
37/**
38 * struct dax_region - mapping infrastructure for dax devices
39 * @id: kernel-wide unique region for a memory range
40 * @base: linear address corresponding to @res
41 * @kref: to pin while other agents have a need to do lookups
42 * @dev: parent device backing this region
43 * @align: allocation and mapping alignment for child dax devices
44 * @res: physical address range of the region
45 * @pfn_flags: identify whether the pfns are paged back or not
46 */
47struct dax_region {
48 int id;
49 struct ida ida;
50 void *base;
51 struct kref kref;
52 struct device *dev;
53 unsigned int align;
54 struct resource res;
55 unsigned long pfn_flags;
56};
57
58/**
59 * struct dax_dev - subdivision of a dax region
60 * @region - parent region
61 * @dev - device backing the character device
Dan Williamsba09c012016-07-24 15:55:42 -070062 * @cdev - core chardev data
Dan Williamsc36eaa62017-04-07 16:42:08 -070063 * @alive - !alive + srcu grace period == no new mappings can be established
Dan Williamsab68f262016-05-18 09:15:08 -070064 * @id - child id in the region
65 * @num_resources - number of physical address extents in this device
66 * @res - array of physical address ranges
67 */
68struct dax_dev {
69 struct dax_region *region;
Dan Williams3bc52c42016-07-24 21:55:45 -070070 struct inode *inode;
Dan Williamsebd84d72016-08-11 00:41:51 -070071 struct device dev;
Dan Williamsba09c012016-07-24 15:55:42 -070072 struct cdev cdev;
Dan Williamsdee41072016-05-14 12:20:44 -070073 bool alive;
Dan Williamsab68f262016-05-18 09:15:08 -070074 int id;
75 int num_resources;
76 struct resource res[0];
77};
78
Dan Williams3bc52c42016-07-24 21:55:45 -070079static struct inode *dax_alloc_inode(struct super_block *sb)
80{
81 return kmem_cache_alloc(dax_cache, GFP_KERNEL);
82}
83
84static void dax_i_callback(struct rcu_head *head)
85{
86 struct inode *inode = container_of(head, struct inode, i_rcu);
87
88 kmem_cache_free(dax_cache, inode);
89}
90
91static void dax_destroy_inode(struct inode *inode)
92{
93 call_rcu(&inode->i_rcu, dax_i_callback);
94}
95
96static const struct super_operations dax_sops = {
97 .statfs = simple_statfs,
98 .alloc_inode = dax_alloc_inode,
99 .destroy_inode = dax_destroy_inode,
100 .drop_inode = generic_delete_inode,
101};
102
103static struct dentry *dax_mount(struct file_system_type *fs_type,
104 int flags, const char *dev_name, void *data)
105{
106 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
107}
108
109static struct file_system_type dax_type = {
110 .name = "dax",
111 .mount = dax_mount,
112 .kill_sb = kill_anon_super,
113};
114
115static int dax_test(struct inode *inode, void *data)
116{
117 return inode->i_cdev == data;
118}
119
120static int dax_set(struct inode *inode, void *data)
121{
122 inode->i_cdev = data;
123 return 0;
124}
125
126static struct inode *dax_inode_get(struct cdev *cdev, dev_t devt)
127{
128 struct inode *inode;
129
130 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
131 dax_test, dax_set, cdev);
132
133 if (!inode)
134 return NULL;
135
136 if (inode->i_state & I_NEW) {
137 inode->i_mode = S_IFCHR;
138 inode->i_flags = S_DAX;
139 inode->i_rdev = devt;
140 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
141 unlock_new_inode(inode);
142 }
143 return inode;
144}
145
146static void init_once(void *inode)
147{
148 inode_init_once(inode);
149}
150
151static int dax_inode_init(void)
152{
153 int rc;
154
155 dax_cache = kmem_cache_create("dax_cache", sizeof(struct inode), 0,
156 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
157 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
158 init_once);
159 if (!dax_cache)
160 return -ENOMEM;
161
162 rc = register_filesystem(&dax_type);
163 if (rc)
164 goto err_register_fs;
165
166 dax_mnt = kern_mount(&dax_type);
167 if (IS_ERR(dax_mnt)) {
168 rc = PTR_ERR(dax_mnt);
169 goto err_mount;
170 }
171 dax_superblock = dax_mnt->mnt_sb;
172
173 return 0;
174
175 err_mount:
176 unregister_filesystem(&dax_type);
177 err_register_fs:
178 kmem_cache_destroy(dax_cache);
179
180 return rc;
181}
182
183static void dax_inode_exit(void)
184{
185 kern_unmount(dax_mnt);
186 unregister_filesystem(&dax_type);
187 kmem_cache_destroy(dax_cache);
188}
189
Dan Williamsab68f262016-05-18 09:15:08 -0700190static void dax_region_free(struct kref *kref)
191{
192 struct dax_region *dax_region;
193
194 dax_region = container_of(kref, struct dax_region, kref);
195 kfree(dax_region);
196}
197
198void dax_region_put(struct dax_region *dax_region)
199{
200 kref_put(&dax_region->kref, dax_region_free);
201}
202EXPORT_SYMBOL_GPL(dax_region_put);
203
Dan Williamsab68f262016-05-18 09:15:08 -0700204struct dax_region *alloc_dax_region(struct device *parent, int region_id,
205 struct resource *res, unsigned int align, void *addr,
206 unsigned long pfn_flags)
207{
208 struct dax_region *dax_region;
209
Dan Williams9d2d01a2016-07-19 16:17:58 -0700210 if (!IS_ALIGNED(res->start, align)
211 || !IS_ALIGNED(resource_size(res), align))
212 return NULL;
Dan Williamsab68f262016-05-18 09:15:08 -0700213
Dan Williams9d2d01a2016-07-19 16:17:58 -0700214 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
Dan Williamsab68f262016-05-18 09:15:08 -0700215 if (!dax_region)
216 return NULL;
217
218 memcpy(&dax_region->res, res, sizeof(*res));
219 dax_region->pfn_flags = pfn_flags;
220 kref_init(&dax_region->kref);
221 dax_region->id = region_id;
222 ida_init(&dax_region->ida);
223 dax_region->align = align;
224 dax_region->dev = parent;
225 dax_region->base = addr;
226
227 return dax_region;
228}
229EXPORT_SYMBOL_GPL(alloc_dax_region);
230
Dan Williamsebd84d72016-08-11 00:41:51 -0700231static struct dax_dev *to_dax_dev(struct device *dev)
232{
233 return container_of(dev, struct dax_dev, dev);
234}
235
Dan Williamsab68f262016-05-18 09:15:08 -0700236static ssize_t size_show(struct device *dev,
237 struct device_attribute *attr, char *buf)
238{
Dan Williamsebd84d72016-08-11 00:41:51 -0700239 struct dax_dev *dax_dev = to_dax_dev(dev);
Dan Williamsab68f262016-05-18 09:15:08 -0700240 unsigned long long size = 0;
241 int i;
242
243 for (i = 0; i < dax_dev->num_resources; i++)
244 size += resource_size(&dax_dev->res[i]);
245
246 return sprintf(buf, "%llu\n", size);
247}
248static DEVICE_ATTR_RO(size);
249
250static struct attribute *dax_device_attributes[] = {
251 &dev_attr_size.attr,
252 NULL,
253};
254
255static const struct attribute_group dax_device_attribute_group = {
256 .attrs = dax_device_attributes,
257};
258
259static const struct attribute_group *dax_attribute_groups[] = {
260 &dax_device_attribute_group,
261 NULL,
262};
263
Dan Williamsdee41072016-05-14 12:20:44 -0700264static int check_vma(struct dax_dev *dax_dev, struct vm_area_struct *vma,
265 const char *func)
266{
267 struct dax_region *dax_region = dax_dev->region;
Dan Williamsebd84d72016-08-11 00:41:51 -0700268 struct device *dev = &dax_dev->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700269 unsigned long mask;
270
271 if (!dax_dev->alive)
272 return -ENXIO;
273
Dan Williams4cb19352016-11-16 09:00:38 -0800274 /* prevent private mappings from being established */
Dan Williams325896f2016-12-06 17:03:35 -0800275 if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) {
Dan Williamsdee41072016-05-14 12:20:44 -0700276 dev_info(dev, "%s: %s: fail, attempted private mapping\n",
277 current->comm, func);
278 return -EINVAL;
279 }
280
281 mask = dax_region->align - 1;
282 if (vma->vm_start & mask || vma->vm_end & mask) {
283 dev_info(dev, "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n",
284 current->comm, func, vma->vm_start, vma->vm_end,
285 mask);
286 return -EINVAL;
287 }
288
289 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV
290 && (vma->vm_flags & VM_DONTCOPY) == 0) {
291 dev_info(dev, "%s: %s: fail, dax range requires MADV_DONTFORK\n",
292 current->comm, func);
293 return -EINVAL;
294 }
295
296 if (!vma_is_dax(vma)) {
297 dev_info(dev, "%s: %s: fail, vma is not DAX capable\n",
298 current->comm, func);
299 return -EINVAL;
300 }
301
302 return 0;
303}
304
305static phys_addr_t pgoff_to_phys(struct dax_dev *dax_dev, pgoff_t pgoff,
306 unsigned long size)
307{
308 struct resource *res;
309 phys_addr_t phys;
310 int i;
311
312 for (i = 0; i < dax_dev->num_resources; i++) {
313 res = &dax_dev->res[i];
314 phys = pgoff * PAGE_SIZE + res->start;
315 if (phys >= res->start && phys <= res->end)
316 break;
317 pgoff -= PHYS_PFN(resource_size(res));
318 }
319
320 if (i < dax_dev->num_resources) {
321 res = &dax_dev->res[i];
322 if (phys + size - 1 <= res->end)
323 return phys;
324 }
325
326 return -1;
327}
328
329static int __dax_dev_fault(struct dax_dev *dax_dev, struct vm_area_struct *vma,
330 struct vm_fault *vmf)
331{
332 unsigned long vaddr = (unsigned long) vmf->virtual_address;
Dan Williamsebd84d72016-08-11 00:41:51 -0700333 struct device *dev = &dax_dev->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700334 struct dax_region *dax_region;
335 int rc = VM_FAULT_SIGBUS;
336 phys_addr_t phys;
337 pfn_t pfn;
Dave Jiangeae72462017-03-10 13:24:22 -0700338 unsigned int fault_size = PAGE_SIZE;
Dan Williamsdee41072016-05-14 12:20:44 -0700339
340 if (check_vma(dax_dev, vma, __func__))
341 return VM_FAULT_SIGBUS;
342
343 dax_region = dax_dev->region;
344 if (dax_region->align > PAGE_SIZE) {
345 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
346 return VM_FAULT_SIGBUS;
347 }
348
Dave Jiangeae72462017-03-10 13:24:22 -0700349 if (fault_size != dax_region->align)
350 return VM_FAULT_SIGBUS;
351
Dan Williamsdee41072016-05-14 12:20:44 -0700352 phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE);
353 if (phys == -1) {
354 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
355 vmf->pgoff);
356 return VM_FAULT_SIGBUS;
357 }
358
359 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
360
361 rc = vm_insert_mixed(vma, vaddr, pfn);
362
363 if (rc == -ENOMEM)
364 return VM_FAULT_OOM;
365 if (rc < 0 && rc != -EBUSY)
366 return VM_FAULT_SIGBUS;
367
368 return VM_FAULT_NOPAGE;
369}
370
371static int dax_dev_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
372{
373 int rc;
374 struct file *filp = vma->vm_file;
375 struct dax_dev *dax_dev = filp->private_data;
376
Dan Williamsebd84d72016-08-11 00:41:51 -0700377 dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
Dan Williamsdee41072016-05-14 12:20:44 -0700378 current->comm, (vmf->flags & FAULT_FLAG_WRITE)
379 ? "write" : "read", vma->vm_start, vma->vm_end);
380 rcu_read_lock();
381 rc = __dax_dev_fault(dax_dev, vma, vmf);
382 rcu_read_unlock();
383
384 return rc;
385}
386
387static int __dax_dev_pmd_fault(struct dax_dev *dax_dev,
388 struct vm_area_struct *vma, unsigned long addr, pmd_t *pmd,
389 unsigned int flags)
390{
391 unsigned long pmd_addr = addr & PMD_MASK;
Dan Williamsebd84d72016-08-11 00:41:51 -0700392 struct device *dev = &dax_dev->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700393 struct dax_region *dax_region;
394 phys_addr_t phys;
395 pgoff_t pgoff;
396 pfn_t pfn;
Dave Jiangeae72462017-03-10 13:24:22 -0700397 unsigned int fault_size = PMD_SIZE;
Dan Williamsdee41072016-05-14 12:20:44 -0700398
399 if (check_vma(dax_dev, vma, __func__))
400 return VM_FAULT_SIGBUS;
401
402 dax_region = dax_dev->region;
403 if (dax_region->align > PMD_SIZE) {
404 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
405 return VM_FAULT_SIGBUS;
406 }
407
408 /* dax pmd mappings require pfn_t_devmap() */
409 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
410 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
411 return VM_FAULT_SIGBUS;
412 }
413
Dave Jiangeae72462017-03-10 13:24:22 -0700414 if (fault_size < dax_region->align)
415 return VM_FAULT_SIGBUS;
416 else if (fault_size > dax_region->align)
417 return VM_FAULT_FALLBACK;
418
419 /* if we are outside of the VMA */
420 if (pmd_addr < vma->vm_start ||
421 (pmd_addr + PMD_SIZE) > vma->vm_end)
422 return VM_FAULT_SIGBUS;
423
Dan Williamsdee41072016-05-14 12:20:44 -0700424 pgoff = linear_page_index(vma, pmd_addr);
Dan Williams4c3cb6e2016-09-03 10:36:00 -0700425 phys = pgoff_to_phys(dax_dev, pgoff, PMD_SIZE);
Dan Williamsdee41072016-05-14 12:20:44 -0700426 if (phys == -1) {
427 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
428 pgoff);
429 return VM_FAULT_SIGBUS;
430 }
431
432 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
433
434 return vmf_insert_pfn_pmd(vma, addr, pmd, pfn,
435 flags & FAULT_FLAG_WRITE);
436}
437
438static int dax_dev_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
439 pmd_t *pmd, unsigned int flags)
440{
Dan Williamsc36eaa62017-04-07 16:42:08 -0700441 int rc, id;
Dan Williamsdee41072016-05-14 12:20:44 -0700442 struct file *filp = vma->vm_file;
443 struct dax_dev *dax_dev = filp->private_data;
444
Dan Williamsebd84d72016-08-11 00:41:51 -0700445 dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
Dan Williamsdee41072016-05-14 12:20:44 -0700446 current->comm, (flags & FAULT_FLAG_WRITE)
447 ? "write" : "read", vma->vm_start, vma->vm_end);
448
Dan Williamsc36eaa62017-04-07 16:42:08 -0700449 id = srcu_read_lock(&dax_srcu);
Dan Williamsdee41072016-05-14 12:20:44 -0700450 rc = __dax_dev_pmd_fault(dax_dev, vma, addr, pmd, flags);
Dan Williamsc36eaa62017-04-07 16:42:08 -0700451 srcu_read_unlock(&dax_srcu, id);
Dan Williamsdee41072016-05-14 12:20:44 -0700452
453 return rc;
454}
455
Dan Williamsbe387592018-02-23 14:05:43 -0800456static int dax_dev_split(struct vm_area_struct *vma, unsigned long addr)
457{
458 struct file *filp = vma->vm_file;
459 struct dax_dev *dax_dev = filp->private_data;
460 struct dax_region *dax_region = dax_dev->region;
461
462 if (!IS_ALIGNED(addr, dax_region->align))
463 return -EINVAL;
464 return 0;
465}
466
Dan Williamsdee41072016-05-14 12:20:44 -0700467static const struct vm_operations_struct dax_dev_vm_ops = {
468 .fault = dax_dev_fault,
469 .pmd_fault = dax_dev_pmd_fault,
Dan Williamsbe387592018-02-23 14:05:43 -0800470 .split = dax_dev_split,
Dan Williamsdee41072016-05-14 12:20:44 -0700471};
472
Dan Williamsaf69f512016-08-11 00:38:03 -0700473static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
Dan Williamsdee41072016-05-14 12:20:44 -0700474{
475 struct dax_dev *dax_dev = filp->private_data;
476 int rc;
477
Dan Williamsebd84d72016-08-11 00:41:51 -0700478 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williamsdee41072016-05-14 12:20:44 -0700479
480 rc = check_vma(dax_dev, vma, __func__);
481 if (rc)
482 return rc;
483
Dan Williamsdee41072016-05-14 12:20:44 -0700484 vma->vm_ops = &dax_dev_vm_ops;
485 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
486 return 0;
Dan Williams043a9252016-08-07 08:23:56 -0700487}
Dan Williamsdee41072016-05-14 12:20:44 -0700488
Dan Williams043a9252016-08-07 08:23:56 -0700489/* return an unmapped area aligned to the dax region specified alignment */
Dan Williamsaf69f512016-08-11 00:38:03 -0700490static unsigned long dax_get_unmapped_area(struct file *filp,
Dan Williams043a9252016-08-07 08:23:56 -0700491 unsigned long addr, unsigned long len, unsigned long pgoff,
492 unsigned long flags)
493{
494 unsigned long off, off_end, off_align, len_align, addr_align, align;
495 struct dax_dev *dax_dev = filp ? filp->private_data : NULL;
496 struct dax_region *dax_region;
497
498 if (!dax_dev || addr)
499 goto out;
500
501 dax_region = dax_dev->region;
502 align = dax_region->align;
503 off = pgoff << PAGE_SHIFT;
504 off_end = off + len;
505 off_align = round_up(off, align);
506
507 if ((off_end <= off_align) || ((off_end - off_align) < align))
508 goto out;
509
510 len_align = len + align;
511 if ((off + len_align) < off)
512 goto out;
513
514 addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
515 pgoff, flags);
516 if (!IS_ERR_VALUE(addr_align)) {
517 addr_align += (off - addr_align) & (align - 1);
518 return addr_align;
519 }
520 out:
521 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
522}
523
Dan Williamsaf69f512016-08-11 00:38:03 -0700524static int dax_open(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700525{
Dan Williamsba09c012016-07-24 15:55:42 -0700526 struct dax_dev *dax_dev;
Dan Williams043a9252016-08-07 08:23:56 -0700527
Dan Williamsba09c012016-07-24 15:55:42 -0700528 dax_dev = container_of(inode->i_cdev, struct dax_dev, cdev);
529 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williams3bc52c42016-07-24 21:55:45 -0700530 inode->i_mapping = dax_dev->inode->i_mapping;
531 inode->i_mapping->host = dax_dev->inode;
532 filp->f_mapping = inode->i_mapping;
Dan Williamsebd84d72016-08-11 00:41:51 -0700533 filp->private_data = dax_dev;
534 inode->i_flags = S_DAX;
Dan Williams043a9252016-08-07 08:23:56 -0700535
Dan Williams043a9252016-08-07 08:23:56 -0700536 return 0;
537}
538
Dan Williamsaf69f512016-08-11 00:38:03 -0700539static int dax_release(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700540{
541 struct dax_dev *dax_dev = filp->private_data;
Dan Williams043a9252016-08-07 08:23:56 -0700542
Dan Williamsba09c012016-07-24 15:55:42 -0700543 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williams043a9252016-08-07 08:23:56 -0700544 return 0;
Dan Williamsdee41072016-05-14 12:20:44 -0700545}
546
Dan Williamsab68f262016-05-18 09:15:08 -0700547static const struct file_operations dax_fops = {
548 .llseek = noop_llseek,
549 .owner = THIS_MODULE,
Dan Williamsaf69f512016-08-11 00:38:03 -0700550 .open = dax_open,
551 .release = dax_release,
552 .get_unmapped_area = dax_get_unmapped_area,
553 .mmap = dax_mmap,
Dan Williamsab68f262016-05-18 09:15:08 -0700554};
555
Dan Williamsebd84d72016-08-11 00:41:51 -0700556static void dax_dev_release(struct device *dev)
Dan Williams043a9252016-08-07 08:23:56 -0700557{
Dan Williamsebd84d72016-08-11 00:41:51 -0700558 struct dax_dev *dax_dev = to_dax_dev(dev);
Dan Williams043a9252016-08-07 08:23:56 -0700559 struct dax_region *dax_region = dax_dev->region;
560
Dan Williamsa3ff4602017-07-18 17:49:14 -0700561 if (dax_dev->id >= 0)
562 ida_simple_remove(&dax_region->ida, dax_dev->id);
Dan Williamsebd84d72016-08-11 00:41:51 -0700563 ida_simple_remove(&dax_minor_ida, MINOR(dev->devt));
564 dax_region_put(dax_region);
Dan Williams3bc52c42016-07-24 21:55:45 -0700565 iput(dax_dev->inode);
Dan Williamsebd84d72016-08-11 00:41:51 -0700566 kfree(dax_dev);
567}
568
Dan Williams8dd114e2017-03-17 12:48:09 -0600569static void kill_dax_dev(struct dax_dev *dax_dev)
Dan Williamsebd84d72016-08-11 00:41:51 -0700570{
Dan Williamsba09c012016-07-24 15:55:42 -0700571 struct cdev *cdev = &dax_dev->cdev;
Dan Williamsebd84d72016-08-11 00:41:51 -0700572
Dan Williams043a9252016-08-07 08:23:56 -0700573 /*
574 * Note, rcu is not protecting the liveness of dax_dev, rcu is
575 * ensuring that any fault handlers that might have seen
576 * dax_dev->alive == true, have completed. Any fault handlers
Dan Williamsc36eaa62017-04-07 16:42:08 -0700577 * that start after synchronize_srcu() has started will abort
Dan Williams043a9252016-08-07 08:23:56 -0700578 * upon seeing dax_dev->alive == false.
579 */
580 dax_dev->alive = false;
Dan Williamsc36eaa62017-04-07 16:42:08 -0700581 synchronize_srcu(&dax_srcu);
Dan Williams9dc1e492016-08-04 16:53:50 -0700582 unmap_mapping_range(dax_dev->inode->i_mapping, 0, 0, 1);
Dan Williamsba09c012016-07-24 15:55:42 -0700583 cdev_del(cdev);
Dan Williams8dd114e2017-03-17 12:48:09 -0600584}
585
586static void unregister_dax_dev(void *dev)
587{
588 struct dax_dev *dax_dev = to_dax_dev(dev);
589
590 dev_dbg(dev, "%s\n", __func__);
591
592 kill_dax_dev(dax_dev);
Dan Williams043a9252016-08-07 08:23:56 -0700593 device_unregister(dev);
Dan Williams043a9252016-08-07 08:23:56 -0700594}
595
Dan Williamsd76911e2016-07-19 17:51:40 -0700596struct dax_dev *devm_create_dax_dev(struct dax_region *dax_region,
Dan Williamsa3ff4602017-07-18 17:49:14 -0700597 int id, struct resource *res, int count)
Dan Williams043a9252016-08-07 08:23:56 -0700598{
599 struct device *parent = dax_region->dev;
600 struct dax_dev *dax_dev;
Dan Williams9d2d01a2016-07-19 16:17:58 -0700601 int rc = 0, minor, i;
Dan Williams043a9252016-08-07 08:23:56 -0700602 struct device *dev;
Dan Williamsba09c012016-07-24 15:55:42 -0700603 struct cdev *cdev;
Dan Williams043a9252016-08-07 08:23:56 -0700604 dev_t dev_t;
605
606 dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
607 if (!dax_dev)
Dan Williamsd76911e2016-07-19 17:51:40 -0700608 return ERR_PTR(-ENOMEM);
Dan Williams043a9252016-08-07 08:23:56 -0700609
Dan Williams9d2d01a2016-07-19 16:17:58 -0700610 for (i = 0; i < count; i++) {
611 if (!IS_ALIGNED(res[i].start, dax_region->align)
612 || !IS_ALIGNED(resource_size(&res[i]),
613 dax_region->align)) {
614 rc = -EINVAL;
615 break;
616 }
617 dax_dev->res[i].start = res[i].start;
618 dax_dev->res[i].end = res[i].end;
619 }
620
621 if (i < count)
622 goto err_id;
623
Dan Williamsa3ff4602017-07-18 17:49:14 -0700624 if (id < 0) {
625 id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
626 dax_dev->id = id;
627 if (id < 0) {
628 rc = id;
629 goto err_id;
630 }
631 } else {
632 /* region provider owns @id lifetime */
633 dax_dev->id = -1;
Dan Williams043a9252016-08-07 08:23:56 -0700634 }
635
636 minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL);
637 if (minor < 0) {
638 rc = minor;
639 goto err_minor;
640 }
641
Arnd Bergmannbc0a0fe2016-09-08 15:53:28 +0200642 dev_t = MKDEV(MAJOR(dax_devt), minor);
643 dev = &dax_dev->dev;
Dan Williams3bc52c42016-07-24 21:55:45 -0700644 dax_dev->inode = dax_inode_get(&dax_dev->cdev, dev_t);
645 if (!dax_dev->inode) {
646 rc = -ENOMEM;
647 goto err_inode;
648 }
649
Dan Williamsba09c012016-07-24 15:55:42 -0700650 /* device_initialize() so cdev can reference kobj parent */
Dan Williamsebd84d72016-08-11 00:41:51 -0700651 device_initialize(dev);
Dan Williamsba09c012016-07-24 15:55:42 -0700652
653 cdev = &dax_dev->cdev;
654 cdev_init(cdev, &dax_fops);
655 cdev->owner = parent->driver->owner;
656 cdev->kobj.parent = &dev->kobj;
657 rc = cdev_add(&dax_dev->cdev, dev_t, 1);
658 if (rc)
659 goto err_cdev;
660
661 /* from here on we're committed to teardown via dax_dev_release() */
Dan Williamsba09c012016-07-24 15:55:42 -0700662 dax_dev->num_resources = count;
663 dax_dev->alive = true;
664 dax_dev->region = dax_region;
665 kref_get(&dax_region->kref);
666
Dan Williamsebd84d72016-08-11 00:41:51 -0700667 dev->devt = dev_t;
668 dev->class = dax_class;
669 dev->parent = parent;
670 dev->groups = dax_attribute_groups;
671 dev->release = dax_dev_release;
Dan Williamsa3ff4602017-07-18 17:49:14 -0700672 dev_set_name(dev, "dax%d.%d", dax_region->id, id);
Dan Williamsebd84d72016-08-11 00:41:51 -0700673 rc = device_add(dev);
674 if (rc) {
Dan Williams8dd114e2017-03-17 12:48:09 -0600675 kill_dax_dev(dax_dev);
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 Williamsd76911e2016-07-19 17:51:40 -0700680 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev);
681 if (rc)
682 return ERR_PTR(rc);
683
684 return dax_dev;
Dan Williams043a9252016-08-07 08:23:56 -0700685
Dan Williamsba09c012016-07-24 15:55:42 -0700686 err_cdev:
Dan Williams3bc52c42016-07-24 21:55:45 -0700687 iput(dax_dev->inode);
688 err_inode:
Dan Williamsba09c012016-07-24 15:55:42 -0700689 ida_simple_remove(&dax_minor_ida, minor);
Dan Williams043a9252016-08-07 08:23:56 -0700690 err_minor:
Dan Williamsa3ff4602017-07-18 17:49:14 -0700691 if (dax_dev->id >= 0)
692 ida_simple_remove(&dax_region->ida, dax_dev->id);
Dan Williams043a9252016-08-07 08:23:56 -0700693 err_id:
Dan Williamsebd84d72016-08-11 00:41:51 -0700694 kfree(dax_dev);
Dan Williams043a9252016-08-07 08:23:56 -0700695
Dan Williamsd76911e2016-07-19 17:51:40 -0700696 return ERR_PTR(rc);
Dan Williams043a9252016-08-07 08:23:56 -0700697}
698EXPORT_SYMBOL_GPL(devm_create_dax_dev);
699
Dan Williamsab68f262016-05-18 09:15:08 -0700700static int __init dax_init(void)
701{
702 int rc;
703
Dan Williams3bc52c42016-07-24 21:55:45 -0700704 rc = dax_inode_init();
Dan Williamsba09c012016-07-24 15:55:42 -0700705 if (rc)
Dan Williamsab68f262016-05-18 09:15:08 -0700706 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700707
Dan Williams3bc52c42016-07-24 21:55:45 -0700708 nr_dax = max(nr_dax, 256);
709 rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
710 if (rc)
711 goto err_chrdev;
Dan Williamsab68f262016-05-18 09:15:08 -0700712
713 dax_class = class_create(THIS_MODULE, "dax");
714 if (IS_ERR(dax_class)) {
Dan Williams3bc52c42016-07-24 21:55:45 -0700715 rc = PTR_ERR(dax_class);
716 goto err_class;
Dan Williamsab68f262016-05-18 09:15:08 -0700717 }
718
719 return 0;
Dan Williams3bc52c42016-07-24 21:55:45 -0700720
721 err_class:
722 unregister_chrdev_region(dax_devt, nr_dax);
723 err_chrdev:
724 dax_inode_exit();
725 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700726}
727
728static void __exit dax_exit(void)
729{
730 class_destroy(dax_class);
Dan Williamsba09c012016-07-24 15:55:42 -0700731 unregister_chrdev_region(dax_devt, nr_dax);
Dan Williamsab68f262016-05-18 09:15:08 -0700732 ida_destroy(&dax_minor_ida);
Dan Williams3bc52c42016-07-24 21:55:45 -0700733 dax_inode_exit();
Dan Williamsab68f262016-05-18 09:15:08 -0700734}
735
736MODULE_AUTHOR("Intel Corporation");
737MODULE_LICENSE("GPL v2");
738subsys_initcall(dax_init);
739module_exit(dax_exit);