blob: a5ed59a5a968c1f39bd0985d9fc1def911a5eee4 [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>
Ingo Molnar50d34392017-02-05 16:03:58 +010016#include <linux/magic.h>
Dan Williams3bc52c42016-07-24 21:55:45 -070017#include <linux/mount.h>
Dan Williamsab68f262016-05-18 09:15:08 -070018#include <linux/pfn_t.h>
Dan Williams3bc52c42016-07-24 21:55:45 -070019#include <linux/hash.h>
Dan Williamsba09c012016-07-24 15:55:42 -070020#include <linux/cdev.h>
Dan Williamsab68f262016-05-18 09:15:08 -070021#include <linux/slab.h>
22#include <linux/dax.h>
23#include <linux/fs.h>
24#include <linux/mm.h>
Dan Williamsccdb07f2016-08-06 16:05:06 -070025#include "dax.h"
Dan Williamsab68f262016-05-18 09:15:08 -070026
Dan Williamsba09c012016-07-24 15:55:42 -070027static dev_t dax_devt;
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 Williamsdee41072016-05-14 12:20:44 -070063 * @alive - !alive + rcu 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 Williamsd7fe1a62016-12-17 14:50:04 -080079static ssize_t id_show(struct device *dev,
80 struct device_attribute *attr, char *buf)
81{
82 struct dax_region *dax_region;
83 ssize_t rc = -ENXIO;
84
85 device_lock(dev);
86 dax_region = dev_get_drvdata(dev);
87 if (dax_region)
88 rc = sprintf(buf, "%d\n", dax_region->id);
89 device_unlock(dev);
90
91 return rc;
92}
93static DEVICE_ATTR_RO(id);
94
95static ssize_t region_size_show(struct device *dev,
96 struct device_attribute *attr, char *buf)
97{
98 struct dax_region *dax_region;
99 ssize_t rc = -ENXIO;
100
101 device_lock(dev);
102 dax_region = dev_get_drvdata(dev);
103 if (dax_region)
104 rc = sprintf(buf, "%llu\n", (unsigned long long)
105 resource_size(&dax_region->res));
106 device_unlock(dev);
107
108 return rc;
109}
110static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
111 region_size_show, NULL);
112
113static ssize_t align_show(struct device *dev,
114 struct device_attribute *attr, char *buf)
115{
116 struct dax_region *dax_region;
117 ssize_t rc = -ENXIO;
118
119 device_lock(dev);
120 dax_region = dev_get_drvdata(dev);
121 if (dax_region)
122 rc = sprintf(buf, "%u\n", dax_region->align);
123 device_unlock(dev);
124
125 return rc;
126}
127static DEVICE_ATTR_RO(align);
128
129static struct attribute *dax_region_attributes[] = {
130 &dev_attr_region_size.attr,
131 &dev_attr_align.attr,
132 &dev_attr_id.attr,
133 NULL,
134};
135
136static const struct attribute_group dax_region_attribute_group = {
137 .name = "dax_region",
138 .attrs = dax_region_attributes,
139};
140
141static const struct attribute_group *dax_region_attribute_groups[] = {
142 &dax_region_attribute_group,
143 NULL,
144};
145
Dan Williams3bc52c42016-07-24 21:55:45 -0700146static struct inode *dax_alloc_inode(struct super_block *sb)
147{
148 return kmem_cache_alloc(dax_cache, GFP_KERNEL);
149}
150
151static void dax_i_callback(struct rcu_head *head)
152{
153 struct inode *inode = container_of(head, struct inode, i_rcu);
154
155 kmem_cache_free(dax_cache, inode);
156}
157
158static void dax_destroy_inode(struct inode *inode)
159{
160 call_rcu(&inode->i_rcu, dax_i_callback);
161}
162
163static const struct super_operations dax_sops = {
164 .statfs = simple_statfs,
165 .alloc_inode = dax_alloc_inode,
166 .destroy_inode = dax_destroy_inode,
167 .drop_inode = generic_delete_inode,
168};
169
170static struct dentry *dax_mount(struct file_system_type *fs_type,
171 int flags, const char *dev_name, void *data)
172{
173 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
174}
175
176static struct file_system_type dax_type = {
177 .name = "dax",
178 .mount = dax_mount,
179 .kill_sb = kill_anon_super,
180};
181
182static int dax_test(struct inode *inode, void *data)
183{
184 return inode->i_cdev == data;
185}
186
187static int dax_set(struct inode *inode, void *data)
188{
189 inode->i_cdev = data;
190 return 0;
191}
192
193static struct inode *dax_inode_get(struct cdev *cdev, dev_t devt)
194{
195 struct inode *inode;
196
197 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
198 dax_test, dax_set, cdev);
199
200 if (!inode)
201 return NULL;
202
203 if (inode->i_state & I_NEW) {
204 inode->i_mode = S_IFCHR;
205 inode->i_flags = S_DAX;
206 inode->i_rdev = devt;
207 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
208 unlock_new_inode(inode);
209 }
210 return inode;
211}
212
213static void init_once(void *inode)
214{
215 inode_init_once(inode);
216}
217
218static int dax_inode_init(void)
219{
220 int rc;
221
222 dax_cache = kmem_cache_create("dax_cache", sizeof(struct inode), 0,
223 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
224 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
225 init_once);
226 if (!dax_cache)
227 return -ENOMEM;
228
229 rc = register_filesystem(&dax_type);
230 if (rc)
231 goto err_register_fs;
232
233 dax_mnt = kern_mount(&dax_type);
234 if (IS_ERR(dax_mnt)) {
235 rc = PTR_ERR(dax_mnt);
236 goto err_mount;
237 }
238 dax_superblock = dax_mnt->mnt_sb;
239
240 return 0;
241
242 err_mount:
243 unregister_filesystem(&dax_type);
244 err_register_fs:
245 kmem_cache_destroy(dax_cache);
246
247 return rc;
248}
249
250static void dax_inode_exit(void)
251{
252 kern_unmount(dax_mnt);
253 unregister_filesystem(&dax_type);
254 kmem_cache_destroy(dax_cache);
255}
256
Dan Williamsab68f262016-05-18 09:15:08 -0700257static void dax_region_free(struct kref *kref)
258{
259 struct dax_region *dax_region;
260
261 dax_region = container_of(kref, struct dax_region, kref);
262 kfree(dax_region);
263}
264
265void dax_region_put(struct dax_region *dax_region)
266{
267 kref_put(&dax_region->kref, dax_region_free);
268}
269EXPORT_SYMBOL_GPL(dax_region_put);
270
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800271static void dax_region_unregister(void *region)
272{
273 struct dax_region *dax_region = region;
274
275 sysfs_remove_groups(&dax_region->dev->kobj,
276 dax_region_attribute_groups);
277 dax_region_put(dax_region);
278}
279
Dan Williamsab68f262016-05-18 09:15:08 -0700280struct dax_region *alloc_dax_region(struct device *parent, int region_id,
281 struct resource *res, unsigned int align, void *addr,
282 unsigned long pfn_flags)
283{
284 struct dax_region *dax_region;
285
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800286 /*
287 * The DAX core assumes that it can store its private data in
288 * parent->driver_data. This WARN is a reminder / safeguard for
289 * developers of device-dax drivers.
290 */
291 if (dev_get_drvdata(parent)) {
292 dev_WARN(parent, "dax core failed to setup private data\n");
293 return NULL;
294 }
295
Dan Williams9d2d01a2016-07-19 16:17:58 -0700296 if (!IS_ALIGNED(res->start, align)
297 || !IS_ALIGNED(resource_size(res), align))
298 return NULL;
Dan Williamsab68f262016-05-18 09:15:08 -0700299
Dan Williams9d2d01a2016-07-19 16:17:58 -0700300 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
Dan Williamsab68f262016-05-18 09:15:08 -0700301 if (!dax_region)
302 return NULL;
303
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800304 dev_set_drvdata(parent, dax_region);
Dan Williamsab68f262016-05-18 09:15:08 -0700305 memcpy(&dax_region->res, res, sizeof(*res));
306 dax_region->pfn_flags = pfn_flags;
307 kref_init(&dax_region->kref);
308 dax_region->id = region_id;
309 ida_init(&dax_region->ida);
310 dax_region->align = align;
311 dax_region->dev = parent;
312 dax_region->base = addr;
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800313 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
314 kfree(dax_region);
315 return NULL;;
316 }
Dan Williamsab68f262016-05-18 09:15:08 -0700317
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800318 kref_get(&dax_region->kref);
319 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
320 return NULL;
Dan Williamsab68f262016-05-18 09:15:08 -0700321 return dax_region;
322}
323EXPORT_SYMBOL_GPL(alloc_dax_region);
324
Dan Williamsebd84d72016-08-11 00:41:51 -0700325static struct dax_dev *to_dax_dev(struct device *dev)
326{
327 return container_of(dev, struct dax_dev, dev);
328}
329
Dan Williamsab68f262016-05-18 09:15:08 -0700330static ssize_t size_show(struct device *dev,
331 struct device_attribute *attr, char *buf)
332{
Dan Williamsebd84d72016-08-11 00:41:51 -0700333 struct dax_dev *dax_dev = to_dax_dev(dev);
Dan Williamsab68f262016-05-18 09:15:08 -0700334 unsigned long long size = 0;
335 int i;
336
337 for (i = 0; i < dax_dev->num_resources; i++)
338 size += resource_size(&dax_dev->res[i]);
339
340 return sprintf(buf, "%llu\n", size);
341}
342static DEVICE_ATTR_RO(size);
343
344static struct attribute *dax_device_attributes[] = {
345 &dev_attr_size.attr,
346 NULL,
347};
348
349static const struct attribute_group dax_device_attribute_group = {
350 .attrs = dax_device_attributes,
351};
352
353static const struct attribute_group *dax_attribute_groups[] = {
354 &dax_device_attribute_group,
355 NULL,
356};
357
Dan Williamsdee41072016-05-14 12:20:44 -0700358static int check_vma(struct dax_dev *dax_dev, struct vm_area_struct *vma,
359 const char *func)
360{
361 struct dax_region *dax_region = dax_dev->region;
Dan Williamsebd84d72016-08-11 00:41:51 -0700362 struct device *dev = &dax_dev->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700363 unsigned long mask;
364
365 if (!dax_dev->alive)
366 return -ENXIO;
367
Dan Williams4cb19352016-11-16 09:00:38 -0800368 /* prevent private mappings from being established */
Dan Williams325896f2016-12-06 17:03:35 -0800369 if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) {
Dan Williamsdee41072016-05-14 12:20:44 -0700370 dev_info(dev, "%s: %s: fail, attempted private mapping\n",
371 current->comm, func);
372 return -EINVAL;
373 }
374
375 mask = dax_region->align - 1;
376 if (vma->vm_start & mask || vma->vm_end & mask) {
377 dev_info(dev, "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n",
378 current->comm, func, vma->vm_start, vma->vm_end,
379 mask);
380 return -EINVAL;
381 }
382
383 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV
384 && (vma->vm_flags & VM_DONTCOPY) == 0) {
385 dev_info(dev, "%s: %s: fail, dax range requires MADV_DONTFORK\n",
386 current->comm, func);
387 return -EINVAL;
388 }
389
390 if (!vma_is_dax(vma)) {
391 dev_info(dev, "%s: %s: fail, vma is not DAX capable\n",
392 current->comm, func);
393 return -EINVAL;
394 }
395
396 return 0;
397}
398
399static phys_addr_t pgoff_to_phys(struct dax_dev *dax_dev, pgoff_t pgoff,
400 unsigned long size)
401{
402 struct resource *res;
403 phys_addr_t phys;
404 int i;
405
406 for (i = 0; i < dax_dev->num_resources; i++) {
407 res = &dax_dev->res[i];
408 phys = pgoff * PAGE_SIZE + res->start;
409 if (phys >= res->start && phys <= res->end)
410 break;
411 pgoff -= PHYS_PFN(resource_size(res));
412 }
413
414 if (i < dax_dev->num_resources) {
415 res = &dax_dev->res[i];
416 if (phys + size - 1 <= res->end)
417 return phys;
418 }
419
420 return -1;
421}
422
Dave Jianga2d58162017-02-24 14:56:59 -0800423static int __dax_dev_pte_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
Dan Williamsdee41072016-05-14 12:20:44 -0700424{
Dan Williamsebd84d72016-08-11 00:41:51 -0700425 struct device *dev = &dax_dev->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700426 struct dax_region *dax_region;
427 int rc = VM_FAULT_SIGBUS;
428 phys_addr_t phys;
429 pfn_t pfn;
430
Dave Jiang11bac802017-02-24 14:56:41 -0800431 if (check_vma(dax_dev, vmf->vma, __func__))
Dan Williamsdee41072016-05-14 12:20:44 -0700432 return VM_FAULT_SIGBUS;
433
434 dax_region = dax_dev->region;
435 if (dax_region->align > PAGE_SIZE) {
436 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
437 return VM_FAULT_SIGBUS;
438 }
439
440 phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE);
441 if (phys == -1) {
442 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
443 vmf->pgoff);
444 return VM_FAULT_SIGBUS;
445 }
446
447 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
448
Dave Jiang11bac802017-02-24 14:56:41 -0800449 rc = vm_insert_mixed(vmf->vma, vmf->address, pfn);
Dan Williamsdee41072016-05-14 12:20:44 -0700450
451 if (rc == -ENOMEM)
452 return VM_FAULT_OOM;
453 if (rc < 0 && rc != -EBUSY)
454 return VM_FAULT_SIGBUS;
455
456 return VM_FAULT_NOPAGE;
457}
458
Dave Jiangf4200392017-02-22 15:40:06 -0800459static int __dax_dev_pmd_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
Dan Williamsdee41072016-05-14 12:20:44 -0700460{
Dave Jiangd8a849e2017-02-22 15:40:03 -0800461 unsigned long pmd_addr = vmf->address & PMD_MASK;
Dan Williamsebd84d72016-08-11 00:41:51 -0700462 struct device *dev = &dax_dev->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700463 struct dax_region *dax_region;
464 phys_addr_t phys;
465 pgoff_t pgoff;
466 pfn_t pfn;
467
Dave Jiangf4200392017-02-22 15:40:06 -0800468 if (check_vma(dax_dev, vmf->vma, __func__))
Dan Williamsdee41072016-05-14 12:20:44 -0700469 return VM_FAULT_SIGBUS;
470
471 dax_region = dax_dev->region;
472 if (dax_region->align > PMD_SIZE) {
473 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
474 return VM_FAULT_SIGBUS;
475 }
476
477 /* dax pmd mappings require pfn_t_devmap() */
478 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
479 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
480 return VM_FAULT_SIGBUS;
481 }
482
Dave Jiangf4200392017-02-22 15:40:06 -0800483 pgoff = linear_page_index(vmf->vma, pmd_addr);
Dan Williams4c3cb6e2016-09-03 10:36:00 -0700484 phys = pgoff_to_phys(dax_dev, pgoff, PMD_SIZE);
Dan Williamsdee41072016-05-14 12:20:44 -0700485 if (phys == -1) {
486 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
487 pgoff);
488 return VM_FAULT_SIGBUS;
489 }
490
491 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
492
Dave Jiangf4200392017-02-22 15:40:06 -0800493 return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd, pfn,
Dave Jiangd8a849e2017-02-22 15:40:03 -0800494 vmf->flags & FAULT_FLAG_WRITE);
Dan Williamsdee41072016-05-14 12:20:44 -0700495}
496
Dave Jiang9557fee2017-02-24 14:57:05 -0800497#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
498static int __dax_dev_pud_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
499{
500 unsigned long pud_addr = vmf->address & PUD_MASK;
501 struct device *dev = &dax_dev->dev;
502 struct dax_region *dax_region;
503 phys_addr_t phys;
504 pgoff_t pgoff;
505 pfn_t pfn;
506
507 if (check_vma(dax_dev, vmf->vma, __func__))
508 return VM_FAULT_SIGBUS;
509
510 dax_region = dax_dev->region;
511 if (dax_region->align > PUD_SIZE) {
512 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
513 return VM_FAULT_SIGBUS;
514 }
515
516 /* dax pud mappings require pfn_t_devmap() */
517 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
518 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
519 return VM_FAULT_SIGBUS;
520 }
521
522 pgoff = linear_page_index(vmf->vma, pud_addr);
523 phys = pgoff_to_phys(dax_dev, pgoff, PUD_SIZE);
524 if (phys == -1) {
525 dev_dbg(dev, "%s: phys_to_pgoff(%#lx) failed\n", __func__,
526 pgoff);
527 return VM_FAULT_SIGBUS;
528 }
529
530 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
531
532 return vmf_insert_pfn_pud(vmf->vma, vmf->address, vmf->pud, pfn,
533 vmf->flags & FAULT_FLAG_WRITE);
534}
535#else
536static int __dax_dev_pud_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
537{
538 return VM_FAULT_FALLBACK;
539}
540#endif /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
541
Dave Jiangc791ace2017-02-24 14:57:08 -0800542static int dax_dev_huge_fault(struct vm_fault *vmf,
543 enum page_entry_size pe_size)
Dan Williamsdee41072016-05-14 12:20:44 -0700544{
545 int rc;
Dave Jiangf4200392017-02-22 15:40:06 -0800546 struct file *filp = vmf->vma->vm_file;
Dan Williamsdee41072016-05-14 12:20:44 -0700547 struct dax_dev *dax_dev = filp->private_data;
548
Dan Williamsebd84d72016-08-11 00:41:51 -0700549 dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
Dave Jiangd8a849e2017-02-22 15:40:03 -0800550 current->comm, (vmf->flags & FAULT_FLAG_WRITE)
Dave Jiangf4200392017-02-22 15:40:06 -0800551 ? "write" : "read",
552 vmf->vma->vm_start, vmf->vma->vm_end);
Dan Williamsdee41072016-05-14 12:20:44 -0700553
554 rcu_read_lock();
Dave Jiangc791ace2017-02-24 14:57:08 -0800555 switch (pe_size) {
556 case PE_SIZE_PTE:
Dave Jianga2d58162017-02-24 14:56:59 -0800557 rc = __dax_dev_pte_fault(dax_dev, vmf);
558 break;
Dave Jiangc791ace2017-02-24 14:57:08 -0800559 case PE_SIZE_PMD:
Dave Jianga2d58162017-02-24 14:56:59 -0800560 rc = __dax_dev_pmd_fault(dax_dev, vmf);
561 break;
Dave Jiangc791ace2017-02-24 14:57:08 -0800562 case PE_SIZE_PUD:
Dave Jiang9557fee2017-02-24 14:57:05 -0800563 rc = __dax_dev_pud_fault(dax_dev, vmf);
564 break;
Dave Jianga2d58162017-02-24 14:56:59 -0800565 default:
566 return VM_FAULT_FALLBACK;
567 }
Dan Williamsdee41072016-05-14 12:20:44 -0700568 rcu_read_unlock();
569
570 return rc;
571}
572
Dave Jiangc791ace2017-02-24 14:57:08 -0800573static int dax_dev_fault(struct vm_fault *vmf)
574{
575 return dax_dev_huge_fault(vmf, PE_SIZE_PTE);
576}
577
Dan Williamsdee41072016-05-14 12:20:44 -0700578static const struct vm_operations_struct dax_dev_vm_ops = {
579 .fault = dax_dev_fault,
Dave Jiangc791ace2017-02-24 14:57:08 -0800580 .huge_fault = dax_dev_huge_fault,
Dan Williamsdee41072016-05-14 12:20:44 -0700581};
582
Dan Williamsaf69f512016-08-11 00:38:03 -0700583static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
Dan Williamsdee41072016-05-14 12:20:44 -0700584{
585 struct dax_dev *dax_dev = filp->private_data;
586 int rc;
587
Dan Williamsebd84d72016-08-11 00:41:51 -0700588 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williamsdee41072016-05-14 12:20:44 -0700589
590 rc = check_vma(dax_dev, vma, __func__);
591 if (rc)
592 return rc;
593
Dan Williamsdee41072016-05-14 12:20:44 -0700594 vma->vm_ops = &dax_dev_vm_ops;
595 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
596 return 0;
Dan Williams043a9252016-08-07 08:23:56 -0700597}
Dan Williamsdee41072016-05-14 12:20:44 -0700598
Dan Williams043a9252016-08-07 08:23:56 -0700599/* return an unmapped area aligned to the dax region specified alignment */
Dan Williamsaf69f512016-08-11 00:38:03 -0700600static unsigned long dax_get_unmapped_area(struct file *filp,
Dan Williams043a9252016-08-07 08:23:56 -0700601 unsigned long addr, unsigned long len, unsigned long pgoff,
602 unsigned long flags)
603{
604 unsigned long off, off_end, off_align, len_align, addr_align, align;
605 struct dax_dev *dax_dev = filp ? filp->private_data : NULL;
606 struct dax_region *dax_region;
607
608 if (!dax_dev || addr)
609 goto out;
610
611 dax_region = dax_dev->region;
612 align = dax_region->align;
613 off = pgoff << PAGE_SHIFT;
614 off_end = off + len;
615 off_align = round_up(off, align);
616
617 if ((off_end <= off_align) || ((off_end - off_align) < align))
618 goto out;
619
620 len_align = len + align;
621 if ((off + len_align) < off)
622 goto out;
623
624 addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
625 pgoff, flags);
626 if (!IS_ERR_VALUE(addr_align)) {
627 addr_align += (off - addr_align) & (align - 1);
628 return addr_align;
629 }
630 out:
631 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
632}
633
Dan Williamsaf69f512016-08-11 00:38:03 -0700634static int dax_open(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700635{
Dan Williamsba09c012016-07-24 15:55:42 -0700636 struct dax_dev *dax_dev;
Dan Williams043a9252016-08-07 08:23:56 -0700637
Dan Williamsba09c012016-07-24 15:55:42 -0700638 dax_dev = container_of(inode->i_cdev, struct dax_dev, cdev);
639 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williams3bc52c42016-07-24 21:55:45 -0700640 inode->i_mapping = dax_dev->inode->i_mapping;
641 inode->i_mapping->host = dax_dev->inode;
642 filp->f_mapping = inode->i_mapping;
Dan Williamsebd84d72016-08-11 00:41:51 -0700643 filp->private_data = dax_dev;
644 inode->i_flags = S_DAX;
Dan Williams043a9252016-08-07 08:23:56 -0700645
Dan Williams043a9252016-08-07 08:23:56 -0700646 return 0;
647}
648
Dan Williamsaf69f512016-08-11 00:38:03 -0700649static int dax_release(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700650{
651 struct dax_dev *dax_dev = filp->private_data;
Dan Williams043a9252016-08-07 08:23:56 -0700652
Dan Williamsba09c012016-07-24 15:55:42 -0700653 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williams043a9252016-08-07 08:23:56 -0700654 return 0;
Dan Williamsdee41072016-05-14 12:20:44 -0700655}
656
Dan Williamsab68f262016-05-18 09:15:08 -0700657static const struct file_operations dax_fops = {
658 .llseek = noop_llseek,
659 .owner = THIS_MODULE,
Dan Williamsaf69f512016-08-11 00:38:03 -0700660 .open = dax_open,
661 .release = dax_release,
662 .get_unmapped_area = dax_get_unmapped_area,
663 .mmap = dax_mmap,
Dan Williamsab68f262016-05-18 09:15:08 -0700664};
665
Dan Williamsebd84d72016-08-11 00:41:51 -0700666static void dax_dev_release(struct device *dev)
Dan Williams043a9252016-08-07 08:23:56 -0700667{
Dan Williamsebd84d72016-08-11 00:41:51 -0700668 struct dax_dev *dax_dev = to_dax_dev(dev);
Dan Williams043a9252016-08-07 08:23:56 -0700669 struct dax_region *dax_region = dax_dev->region;
670
Dan Williamsebd84d72016-08-11 00:41:51 -0700671 ida_simple_remove(&dax_region->ida, dax_dev->id);
672 ida_simple_remove(&dax_minor_ida, MINOR(dev->devt));
673 dax_region_put(dax_region);
Dan Williams3bc52c42016-07-24 21:55:45 -0700674 iput(dax_dev->inode);
Dan Williamsebd84d72016-08-11 00:41:51 -0700675 kfree(dax_dev);
676}
677
Dan Williamsed01e502017-03-17 12:48:09 -0600678static void kill_dax_dev(struct dax_dev *dax_dev)
Dan Williamsebd84d72016-08-11 00:41:51 -0700679{
Dan Williamsba09c012016-07-24 15:55:42 -0700680 struct cdev *cdev = &dax_dev->cdev;
Dan Williamsebd84d72016-08-11 00:41:51 -0700681
Dan Williams043a9252016-08-07 08:23:56 -0700682 /*
683 * Note, rcu is not protecting the liveness of dax_dev, rcu is
684 * ensuring that any fault handlers that might have seen
685 * dax_dev->alive == true, have completed. Any fault handlers
686 * that start after synchronize_rcu() has started will abort
687 * upon seeing dax_dev->alive == false.
688 */
689 dax_dev->alive = false;
690 synchronize_rcu();
Dan Williams9dc1e492016-08-04 16:53:50 -0700691 unmap_mapping_range(dax_dev->inode->i_mapping, 0, 0, 1);
Dan Williamsba09c012016-07-24 15:55:42 -0700692 cdev_del(cdev);
Dan Williamsed01e502017-03-17 12:48:09 -0600693}
694
695static void unregister_dax_dev(void *dev)
696{
697 struct dax_dev *dax_dev = to_dax_dev(dev);
698
699 dev_dbg(dev, "%s\n", __func__);
700
701 kill_dax_dev(dax_dev);
Dan Williams043a9252016-08-07 08:23:56 -0700702 device_unregister(dev);
Dan Williams043a9252016-08-07 08:23:56 -0700703}
704
Dan Williamsd76911e2016-07-19 17:51:40 -0700705struct dax_dev *devm_create_dax_dev(struct dax_region *dax_region,
706 struct resource *res, int count)
Dan Williams043a9252016-08-07 08:23:56 -0700707{
708 struct device *parent = dax_region->dev;
709 struct dax_dev *dax_dev;
Dan Williams9d2d01a2016-07-19 16:17:58 -0700710 int rc = 0, minor, i;
Dan Williams043a9252016-08-07 08:23:56 -0700711 struct device *dev;
Dan Williamsba09c012016-07-24 15:55:42 -0700712 struct cdev *cdev;
Dan Williams043a9252016-08-07 08:23:56 -0700713 dev_t dev_t;
714
715 dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
716 if (!dax_dev)
Dan Williamsd76911e2016-07-19 17:51:40 -0700717 return ERR_PTR(-ENOMEM);
Dan Williams043a9252016-08-07 08:23:56 -0700718
Dan Williams9d2d01a2016-07-19 16:17:58 -0700719 for (i = 0; i < count; i++) {
720 if (!IS_ALIGNED(res[i].start, dax_region->align)
721 || !IS_ALIGNED(resource_size(&res[i]),
722 dax_region->align)) {
723 rc = -EINVAL;
724 break;
725 }
726 dax_dev->res[i].start = res[i].start;
727 dax_dev->res[i].end = res[i].end;
728 }
729
730 if (i < count)
731 goto err_id;
732
Dan Williams043a9252016-08-07 08:23:56 -0700733 dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
734 if (dax_dev->id < 0) {
735 rc = dax_dev->id;
736 goto err_id;
737 }
738
739 minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL);
740 if (minor < 0) {
741 rc = minor;
742 goto err_minor;
743 }
744
Arnd Bergmannbc0a0fe2016-09-08 15:53:28 +0200745 dev_t = MKDEV(MAJOR(dax_devt), minor);
746 dev = &dax_dev->dev;
Dan Williams3bc52c42016-07-24 21:55:45 -0700747 dax_dev->inode = dax_inode_get(&dax_dev->cdev, dev_t);
748 if (!dax_dev->inode) {
749 rc = -ENOMEM;
750 goto err_inode;
751 }
752
Dan Williamsba09c012016-07-24 15:55:42 -0700753 /* device_initialize() so cdev can reference kobj parent */
Dan Williamsebd84d72016-08-11 00:41:51 -0700754 device_initialize(dev);
Dan Williamsba09c012016-07-24 15:55:42 -0700755
756 cdev = &dax_dev->cdev;
757 cdev_init(cdev, &dax_fops);
758 cdev->owner = parent->driver->owner;
759 cdev->kobj.parent = &dev->kobj;
760 rc = cdev_add(&dax_dev->cdev, dev_t, 1);
761 if (rc)
762 goto err_cdev;
763
764 /* from here on we're committed to teardown via dax_dev_release() */
Dan Williamsba09c012016-07-24 15:55:42 -0700765 dax_dev->num_resources = count;
766 dax_dev->alive = true;
767 dax_dev->region = dax_region;
768 kref_get(&dax_region->kref);
769
Dan Williamsebd84d72016-08-11 00:41:51 -0700770 dev->devt = dev_t;
771 dev->class = dax_class;
772 dev->parent = parent;
773 dev->groups = dax_attribute_groups;
774 dev->release = dax_dev_release;
775 dev_set_name(dev, "dax%d.%d", dax_region->id, dax_dev->id);
776 rc = device_add(dev);
777 if (rc) {
Dan Williamsed01e502017-03-17 12:48:09 -0600778 kill_dax_dev(dax_dev);
Dan Williamsebd84d72016-08-11 00:41:51 -0700779 put_device(dev);
Dan Williamsd76911e2016-07-19 17:51:40 -0700780 return ERR_PTR(rc);
Dan Williamsebd84d72016-08-11 00:41:51 -0700781 }
Dan Williams043a9252016-08-07 08:23:56 -0700782
Dan Williamsd76911e2016-07-19 17:51:40 -0700783 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev);
784 if (rc)
785 return ERR_PTR(rc);
786
787 return dax_dev;
Dan Williams043a9252016-08-07 08:23:56 -0700788
Dan Williamsba09c012016-07-24 15:55:42 -0700789 err_cdev:
Dan Williams3bc52c42016-07-24 21:55:45 -0700790 iput(dax_dev->inode);
791 err_inode:
Dan Williamsba09c012016-07-24 15:55:42 -0700792 ida_simple_remove(&dax_minor_ida, minor);
Dan Williams043a9252016-08-07 08:23:56 -0700793 err_minor:
794 ida_simple_remove(&dax_region->ida, dax_dev->id);
795 err_id:
Dan Williamsebd84d72016-08-11 00:41:51 -0700796 kfree(dax_dev);
Dan Williams043a9252016-08-07 08:23:56 -0700797
Dan Williamsd76911e2016-07-19 17:51:40 -0700798 return ERR_PTR(rc);
Dan Williams043a9252016-08-07 08:23:56 -0700799}
800EXPORT_SYMBOL_GPL(devm_create_dax_dev);
801
Dan Williamsab68f262016-05-18 09:15:08 -0700802static int __init dax_init(void)
803{
804 int rc;
805
Dan Williams3bc52c42016-07-24 21:55:45 -0700806 rc = dax_inode_init();
Dan Williamsba09c012016-07-24 15:55:42 -0700807 if (rc)
Dan Williamsab68f262016-05-18 09:15:08 -0700808 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700809
Dan Williams3bc52c42016-07-24 21:55:45 -0700810 nr_dax = max(nr_dax, 256);
811 rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
812 if (rc)
813 goto err_chrdev;
Dan Williamsab68f262016-05-18 09:15:08 -0700814
815 dax_class = class_create(THIS_MODULE, "dax");
816 if (IS_ERR(dax_class)) {
Dan Williams3bc52c42016-07-24 21:55:45 -0700817 rc = PTR_ERR(dax_class);
818 goto err_class;
Dan Williamsab68f262016-05-18 09:15:08 -0700819 }
820
821 return 0;
Dan Williams3bc52c42016-07-24 21:55:45 -0700822
823 err_class:
824 unregister_chrdev_region(dax_devt, nr_dax);
825 err_chrdev:
826 dax_inode_exit();
827 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700828}
829
830static void __exit dax_exit(void)
831{
832 class_destroy(dax_class);
Dan Williamsba09c012016-07-24 15:55:42 -0700833 unregister_chrdev_region(dax_devt, nr_dax);
Dan Williamsab68f262016-05-18 09:15:08 -0700834 ida_destroy(&dax_minor_ida);
Dan Williams3bc52c42016-07-24 21:55:45 -0700835 dax_inode_exit();
Dan Williamsab68f262016-05-18 09:15:08 -0700836}
837
838MODULE_AUTHOR("Intel Corporation");
839MODULE_LICENSE("GPL v2");
840subsys_initcall(dax_init);
841module_exit(dax_exit);