blob: 806f180c80d816b313319f960479a7ad2848c672 [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 Williams956a4cd2017-04-07 16:42:08 -070028DEFINE_STATIC_SRCU(dax_srcu);
Dan Williamsab68f262016-05-18 09:15:08 -070029static struct class *dax_class;
30static DEFINE_IDA(dax_minor_ida);
Dan Williamsba09c012016-07-24 15:55:42 -070031static int nr_dax = CONFIG_NR_DEV_DAX;
32module_param(nr_dax, int, S_IRUGO);
Dan Williams3bc52c42016-07-24 21:55:45 -070033static struct vfsmount *dax_mnt;
34static struct kmem_cache *dax_cache __read_mostly;
35static struct super_block *dax_superblock __read_mostly;
Dan Williamsba09c012016-07-24 15:55:42 -070036MODULE_PARM_DESC(nr_dax, "max number of device-dax instances");
Dan Williamsab68f262016-05-18 09:15:08 -070037
38/**
39 * struct dax_region - mapping infrastructure for dax devices
40 * @id: kernel-wide unique region for a memory range
41 * @base: linear address corresponding to @res
42 * @kref: to pin while other agents have a need to do lookups
43 * @dev: parent device backing this region
44 * @align: allocation and mapping alignment for child dax devices
45 * @res: physical address range of the region
46 * @pfn_flags: identify whether the pfns are paged back or not
47 */
48struct dax_region {
49 int id;
50 struct ida ida;
51 void *base;
52 struct kref kref;
53 struct device *dev;
54 unsigned int align;
55 struct resource res;
56 unsigned long pfn_flags;
57};
58
59/**
60 * struct dax_dev - subdivision of a dax region
61 * @region - parent region
62 * @dev - device backing the character device
Dan Williamsba09c012016-07-24 15:55:42 -070063 * @cdev - core chardev data
Dan Williams956a4cd2017-04-07 16:42:08 -070064 * @alive - !alive + srcu grace period == no new mappings can be established
Dan Williamsab68f262016-05-18 09:15:08 -070065 * @id - child id in the region
66 * @num_resources - number of physical address extents in this device
67 * @res - array of physical address ranges
68 */
69struct dax_dev {
70 struct dax_region *region;
Dan Williams3bc52c42016-07-24 21:55:45 -070071 struct inode *inode;
Dan Williamsebd84d72016-08-11 00:41:51 -070072 struct device dev;
Dan Williamsba09c012016-07-24 15:55:42 -070073 struct cdev cdev;
Dan Williamsdee41072016-05-14 12:20:44 -070074 bool alive;
Dan Williamsab68f262016-05-18 09:15:08 -070075 int id;
76 int num_resources;
77 struct resource res[0];
78};
79
Dan Williamsd7fe1a62016-12-17 14:50:04 -080080static ssize_t id_show(struct device *dev,
81 struct device_attribute *attr, char *buf)
82{
83 struct dax_region *dax_region;
84 ssize_t rc = -ENXIO;
85
86 device_lock(dev);
87 dax_region = dev_get_drvdata(dev);
88 if (dax_region)
89 rc = sprintf(buf, "%d\n", dax_region->id);
90 device_unlock(dev);
91
92 return rc;
93}
94static DEVICE_ATTR_RO(id);
95
96static ssize_t region_size_show(struct device *dev,
97 struct device_attribute *attr, char *buf)
98{
99 struct dax_region *dax_region;
100 ssize_t rc = -ENXIO;
101
102 device_lock(dev);
103 dax_region = dev_get_drvdata(dev);
104 if (dax_region)
105 rc = sprintf(buf, "%llu\n", (unsigned long long)
106 resource_size(&dax_region->res));
107 device_unlock(dev);
108
109 return rc;
110}
111static struct device_attribute dev_attr_region_size = __ATTR(size, 0444,
112 region_size_show, NULL);
113
114static ssize_t align_show(struct device *dev,
115 struct device_attribute *attr, char *buf)
116{
117 struct dax_region *dax_region;
118 ssize_t rc = -ENXIO;
119
120 device_lock(dev);
121 dax_region = dev_get_drvdata(dev);
122 if (dax_region)
123 rc = sprintf(buf, "%u\n", dax_region->align);
124 device_unlock(dev);
125
126 return rc;
127}
128static DEVICE_ATTR_RO(align);
129
130static struct attribute *dax_region_attributes[] = {
131 &dev_attr_region_size.attr,
132 &dev_attr_align.attr,
133 &dev_attr_id.attr,
134 NULL,
135};
136
137static const struct attribute_group dax_region_attribute_group = {
138 .name = "dax_region",
139 .attrs = dax_region_attributes,
140};
141
142static const struct attribute_group *dax_region_attribute_groups[] = {
143 &dax_region_attribute_group,
144 NULL,
145};
146
Dan Williams3bc52c42016-07-24 21:55:45 -0700147static struct inode *dax_alloc_inode(struct super_block *sb)
148{
149 return kmem_cache_alloc(dax_cache, GFP_KERNEL);
150}
151
152static void dax_i_callback(struct rcu_head *head)
153{
154 struct inode *inode = container_of(head, struct inode, i_rcu);
155
156 kmem_cache_free(dax_cache, inode);
157}
158
159static void dax_destroy_inode(struct inode *inode)
160{
161 call_rcu(&inode->i_rcu, dax_i_callback);
162}
163
164static const struct super_operations dax_sops = {
165 .statfs = simple_statfs,
166 .alloc_inode = dax_alloc_inode,
167 .destroy_inode = dax_destroy_inode,
168 .drop_inode = generic_delete_inode,
169};
170
171static struct dentry *dax_mount(struct file_system_type *fs_type,
172 int flags, const char *dev_name, void *data)
173{
174 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
175}
176
177static struct file_system_type dax_type = {
178 .name = "dax",
179 .mount = dax_mount,
180 .kill_sb = kill_anon_super,
181};
182
183static int dax_test(struct inode *inode, void *data)
184{
185 return inode->i_cdev == data;
186}
187
188static int dax_set(struct inode *inode, void *data)
189{
190 inode->i_cdev = data;
191 return 0;
192}
193
194static struct inode *dax_inode_get(struct cdev *cdev, dev_t devt)
195{
196 struct inode *inode;
197
198 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
199 dax_test, dax_set, cdev);
200
201 if (!inode)
202 return NULL;
203
204 if (inode->i_state & I_NEW) {
205 inode->i_mode = S_IFCHR;
206 inode->i_flags = S_DAX;
207 inode->i_rdev = devt;
208 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
209 unlock_new_inode(inode);
210 }
211 return inode;
212}
213
214static void init_once(void *inode)
215{
216 inode_init_once(inode);
217}
218
219static int dax_inode_init(void)
220{
221 int rc;
222
223 dax_cache = kmem_cache_create("dax_cache", sizeof(struct inode), 0,
224 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
225 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
226 init_once);
227 if (!dax_cache)
228 return -ENOMEM;
229
230 rc = register_filesystem(&dax_type);
231 if (rc)
232 goto err_register_fs;
233
234 dax_mnt = kern_mount(&dax_type);
235 if (IS_ERR(dax_mnt)) {
236 rc = PTR_ERR(dax_mnt);
237 goto err_mount;
238 }
239 dax_superblock = dax_mnt->mnt_sb;
240
241 return 0;
242
243 err_mount:
244 unregister_filesystem(&dax_type);
245 err_register_fs:
246 kmem_cache_destroy(dax_cache);
247
248 return rc;
249}
250
251static void dax_inode_exit(void)
252{
253 kern_unmount(dax_mnt);
254 unregister_filesystem(&dax_type);
255 kmem_cache_destroy(dax_cache);
256}
257
Dan Williamsab68f262016-05-18 09:15:08 -0700258static void dax_region_free(struct kref *kref)
259{
260 struct dax_region *dax_region;
261
262 dax_region = container_of(kref, struct dax_region, kref);
263 kfree(dax_region);
264}
265
266void dax_region_put(struct dax_region *dax_region)
267{
268 kref_put(&dax_region->kref, dax_region_free);
269}
270EXPORT_SYMBOL_GPL(dax_region_put);
271
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800272static void dax_region_unregister(void *region)
273{
274 struct dax_region *dax_region = region;
275
276 sysfs_remove_groups(&dax_region->dev->kobj,
277 dax_region_attribute_groups);
278 dax_region_put(dax_region);
279}
280
Dan Williamsab68f262016-05-18 09:15:08 -0700281struct dax_region *alloc_dax_region(struct device *parent, int region_id,
282 struct resource *res, unsigned int align, void *addr,
283 unsigned long pfn_flags)
284{
285 struct dax_region *dax_region;
286
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800287 /*
288 * The DAX core assumes that it can store its private data in
289 * parent->driver_data. This WARN is a reminder / safeguard for
290 * developers of device-dax drivers.
291 */
292 if (dev_get_drvdata(parent)) {
293 dev_WARN(parent, "dax core failed to setup private data\n");
294 return NULL;
295 }
296
Dan Williams9d2d01a2016-07-19 16:17:58 -0700297 if (!IS_ALIGNED(res->start, align)
298 || !IS_ALIGNED(resource_size(res), align))
299 return NULL;
Dan Williamsab68f262016-05-18 09:15:08 -0700300
Dan Williams9d2d01a2016-07-19 16:17:58 -0700301 dax_region = kzalloc(sizeof(*dax_region), GFP_KERNEL);
Dan Williamsab68f262016-05-18 09:15:08 -0700302 if (!dax_region)
303 return NULL;
304
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800305 dev_set_drvdata(parent, dax_region);
Dan Williamsab68f262016-05-18 09:15:08 -0700306 memcpy(&dax_region->res, res, sizeof(*res));
307 dax_region->pfn_flags = pfn_flags;
308 kref_init(&dax_region->kref);
309 dax_region->id = region_id;
310 ida_init(&dax_region->ida);
311 dax_region->align = align;
312 dax_region->dev = parent;
313 dax_region->base = addr;
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800314 if (sysfs_create_groups(&parent->kobj, dax_region_attribute_groups)) {
315 kfree(dax_region);
316 return NULL;;
317 }
Dan Williamsab68f262016-05-18 09:15:08 -0700318
Dan Williamsd7fe1a62016-12-17 14:50:04 -0800319 kref_get(&dax_region->kref);
320 if (devm_add_action_or_reset(parent, dax_region_unregister, dax_region))
321 return NULL;
Dan Williamsab68f262016-05-18 09:15:08 -0700322 return dax_region;
323}
324EXPORT_SYMBOL_GPL(alloc_dax_region);
325
Dan Williamsebd84d72016-08-11 00:41:51 -0700326static struct dax_dev *to_dax_dev(struct device *dev)
327{
328 return container_of(dev, struct dax_dev, dev);
329}
330
Dan Williamsab68f262016-05-18 09:15:08 -0700331static ssize_t size_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
333{
Dan Williamsebd84d72016-08-11 00:41:51 -0700334 struct dax_dev *dax_dev = to_dax_dev(dev);
Dan Williamsab68f262016-05-18 09:15:08 -0700335 unsigned long long size = 0;
336 int i;
337
338 for (i = 0; i < dax_dev->num_resources; i++)
339 size += resource_size(&dax_dev->res[i]);
340
341 return sprintf(buf, "%llu\n", size);
342}
343static DEVICE_ATTR_RO(size);
344
345static struct attribute *dax_device_attributes[] = {
346 &dev_attr_size.attr,
347 NULL,
348};
349
350static const struct attribute_group dax_device_attribute_group = {
351 .attrs = dax_device_attributes,
352};
353
354static const struct attribute_group *dax_attribute_groups[] = {
355 &dax_device_attribute_group,
356 NULL,
357};
358
Dan Williamsdee41072016-05-14 12:20:44 -0700359static int check_vma(struct dax_dev *dax_dev, struct vm_area_struct *vma,
360 const char *func)
361{
362 struct dax_region *dax_region = dax_dev->region;
Dan Williamsebd84d72016-08-11 00:41:51 -0700363 struct device *dev = &dax_dev->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700364 unsigned long mask;
365
366 if (!dax_dev->alive)
367 return -ENXIO;
368
Dan Williams4cb19352016-11-16 09:00:38 -0800369 /* prevent private mappings from being established */
Dan Williams325896f2016-12-06 17:03:35 -0800370 if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) {
Dan Williamsdee41072016-05-14 12:20:44 -0700371 dev_info(dev, "%s: %s: fail, attempted private mapping\n",
372 current->comm, func);
373 return -EINVAL;
374 }
375
376 mask = dax_region->align - 1;
377 if (vma->vm_start & mask || vma->vm_end & mask) {
378 dev_info(dev, "%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n",
379 current->comm, func, vma->vm_start, vma->vm_end,
380 mask);
381 return -EINVAL;
382 }
383
384 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV
385 && (vma->vm_flags & VM_DONTCOPY) == 0) {
386 dev_info(dev, "%s: %s: fail, dax range requires MADV_DONTFORK\n",
387 current->comm, func);
388 return -EINVAL;
389 }
390
391 if (!vma_is_dax(vma)) {
392 dev_info(dev, "%s: %s: fail, vma is not DAX capable\n",
393 current->comm, func);
394 return -EINVAL;
395 }
396
397 return 0;
398}
399
400static phys_addr_t pgoff_to_phys(struct dax_dev *dax_dev, pgoff_t pgoff,
401 unsigned long size)
402{
403 struct resource *res;
404 phys_addr_t phys;
405 int i;
406
407 for (i = 0; i < dax_dev->num_resources; i++) {
408 res = &dax_dev->res[i];
409 phys = pgoff * PAGE_SIZE + res->start;
410 if (phys >= res->start && phys <= res->end)
411 break;
412 pgoff -= PHYS_PFN(resource_size(res));
413 }
414
415 if (i < dax_dev->num_resources) {
416 res = &dax_dev->res[i];
417 if (phys + size - 1 <= res->end)
418 return phys;
419 }
420
421 return -1;
422}
423
Dave Jianga2d58162017-02-24 14:56:59 -0800424static int __dax_dev_pte_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
Dan Williamsdee41072016-05-14 12:20:44 -0700425{
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;
Dave Jiang0134ed42017-03-10 13:24:22 -0700431 unsigned int fault_size = PAGE_SIZE;
Dan Williamsdee41072016-05-14 12:20:44 -0700432
Dave Jiang11bac802017-02-24 14:56:41 -0800433 if (check_vma(dax_dev, vmf->vma, __func__))
Dan Williamsdee41072016-05-14 12:20:44 -0700434 return VM_FAULT_SIGBUS;
435
436 dax_region = dax_dev->region;
437 if (dax_region->align > PAGE_SIZE) {
438 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
439 return VM_FAULT_SIGBUS;
440 }
441
Dave Jiang0134ed42017-03-10 13:24:22 -0700442 if (fault_size != dax_region->align)
443 return VM_FAULT_SIGBUS;
444
Dan Williamsdee41072016-05-14 12:20:44 -0700445 phys = pgoff_to_phys(dax_dev, vmf->pgoff, PAGE_SIZE);
446 if (phys == -1) {
Dave Jiang52084f82017-03-09 16:56:01 -0700447 dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__,
Dan Williamsdee41072016-05-14 12:20:44 -0700448 vmf->pgoff);
449 return VM_FAULT_SIGBUS;
450 }
451
452 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
453
Dave Jiang11bac802017-02-24 14:56:41 -0800454 rc = vm_insert_mixed(vmf->vma, vmf->address, pfn);
Dan Williamsdee41072016-05-14 12:20:44 -0700455
456 if (rc == -ENOMEM)
457 return VM_FAULT_OOM;
458 if (rc < 0 && rc != -EBUSY)
459 return VM_FAULT_SIGBUS;
460
461 return VM_FAULT_NOPAGE;
462}
463
Dave Jiangf4200392017-02-22 15:40:06 -0800464static int __dax_dev_pmd_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
Dan Williamsdee41072016-05-14 12:20:44 -0700465{
Dave Jiangd8a849e2017-02-22 15:40:03 -0800466 unsigned long pmd_addr = vmf->address & PMD_MASK;
Dan Williamsebd84d72016-08-11 00:41:51 -0700467 struct device *dev = &dax_dev->dev;
Dan Williamsdee41072016-05-14 12:20:44 -0700468 struct dax_region *dax_region;
469 phys_addr_t phys;
470 pgoff_t pgoff;
471 pfn_t pfn;
Dave Jiang0134ed42017-03-10 13:24:22 -0700472 unsigned int fault_size = PMD_SIZE;
Dan Williamsdee41072016-05-14 12:20:44 -0700473
Dave Jiangf4200392017-02-22 15:40:06 -0800474 if (check_vma(dax_dev, vmf->vma, __func__))
Dan Williamsdee41072016-05-14 12:20:44 -0700475 return VM_FAULT_SIGBUS;
476
477 dax_region = dax_dev->region;
478 if (dax_region->align > PMD_SIZE) {
479 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
480 return VM_FAULT_SIGBUS;
481 }
482
483 /* dax pmd mappings require pfn_t_devmap() */
484 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
485 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
486 return VM_FAULT_SIGBUS;
487 }
488
Dave Jiang0134ed42017-03-10 13:24:22 -0700489 if (fault_size < dax_region->align)
490 return VM_FAULT_SIGBUS;
491 else if (fault_size > dax_region->align)
492 return VM_FAULT_FALLBACK;
493
494 /* if we are outside of the VMA */
495 if (pmd_addr < vmf->vma->vm_start ||
496 (pmd_addr + PMD_SIZE) > vmf->vma->vm_end)
497 return VM_FAULT_SIGBUS;
498
Dave Jiangf4200392017-02-22 15:40:06 -0800499 pgoff = linear_page_index(vmf->vma, pmd_addr);
Dan Williams4c3cb6e2016-09-03 10:36:00 -0700500 phys = pgoff_to_phys(dax_dev, pgoff, PMD_SIZE);
Dan Williamsdee41072016-05-14 12:20:44 -0700501 if (phys == -1) {
Dave Jiang52084f82017-03-09 16:56:01 -0700502 dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__,
Dan Williamsdee41072016-05-14 12:20:44 -0700503 pgoff);
504 return VM_FAULT_SIGBUS;
505 }
506
507 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
508
Dave Jiangf4200392017-02-22 15:40:06 -0800509 return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd, pfn,
Dave Jiangd8a849e2017-02-22 15:40:03 -0800510 vmf->flags & FAULT_FLAG_WRITE);
Dan Williamsdee41072016-05-14 12:20:44 -0700511}
512
Dave Jiang9557fee2017-02-24 14:57:05 -0800513#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
514static int __dax_dev_pud_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
515{
516 unsigned long pud_addr = vmf->address & PUD_MASK;
517 struct device *dev = &dax_dev->dev;
518 struct dax_region *dax_region;
519 phys_addr_t phys;
520 pgoff_t pgoff;
521 pfn_t pfn;
Dave Jiang70b085b2017-03-10 13:24:27 -0700522 unsigned int fault_size = PUD_SIZE;
523
Dave Jiang9557fee2017-02-24 14:57:05 -0800524
525 if (check_vma(dax_dev, vmf->vma, __func__))
526 return VM_FAULT_SIGBUS;
527
528 dax_region = dax_dev->region;
529 if (dax_region->align > PUD_SIZE) {
530 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
531 return VM_FAULT_SIGBUS;
532 }
533
534 /* dax pud mappings require pfn_t_devmap() */
535 if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
536 dev_dbg(dev, "%s: alignment > fault size\n", __func__);
537 return VM_FAULT_SIGBUS;
538 }
539
Dave Jiang70b085b2017-03-10 13:24:27 -0700540 if (fault_size < dax_region->align)
541 return VM_FAULT_SIGBUS;
542 else if (fault_size > dax_region->align)
543 return VM_FAULT_FALLBACK;
544
545 /* if we are outside of the VMA */
546 if (pud_addr < vmf->vma->vm_start ||
547 (pud_addr + PUD_SIZE) > vmf->vma->vm_end)
548 return VM_FAULT_SIGBUS;
549
Dave Jiang9557fee2017-02-24 14:57:05 -0800550 pgoff = linear_page_index(vmf->vma, pud_addr);
551 phys = pgoff_to_phys(dax_dev, pgoff, PUD_SIZE);
552 if (phys == -1) {
Dave Jiang52084f82017-03-09 16:56:01 -0700553 dev_dbg(dev, "%s: pgoff_to_phys(%#lx) failed\n", __func__,
Dave Jiang9557fee2017-02-24 14:57:05 -0800554 pgoff);
555 return VM_FAULT_SIGBUS;
556 }
557
558 pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
559
560 return vmf_insert_pfn_pud(vmf->vma, vmf->address, vmf->pud, pfn,
561 vmf->flags & FAULT_FLAG_WRITE);
562}
563#else
564static int __dax_dev_pud_fault(struct dax_dev *dax_dev, struct vm_fault *vmf)
565{
566 return VM_FAULT_FALLBACK;
567}
568#endif /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */
569
Dave Jiangc791ace2017-02-24 14:57:08 -0800570static int dax_dev_huge_fault(struct vm_fault *vmf,
571 enum page_entry_size pe_size)
Dan Williamsdee41072016-05-14 12:20:44 -0700572{
Dan Williams956a4cd2017-04-07 16:42:08 -0700573 int rc, id;
Dave Jiangf4200392017-02-22 15:40:06 -0800574 struct file *filp = vmf->vma->vm_file;
Dan Williamsdee41072016-05-14 12:20:44 -0700575 struct dax_dev *dax_dev = filp->private_data;
576
Dan Williamsebd84d72016-08-11 00:41:51 -0700577 dev_dbg(&dax_dev->dev, "%s: %s: %s (%#lx - %#lx)\n", __func__,
Dave Jiangd8a849e2017-02-22 15:40:03 -0800578 current->comm, (vmf->flags & FAULT_FLAG_WRITE)
Dave Jiangf4200392017-02-22 15:40:06 -0800579 ? "write" : "read",
580 vmf->vma->vm_start, vmf->vma->vm_end);
Dan Williamsdee41072016-05-14 12:20:44 -0700581
Dan Williams956a4cd2017-04-07 16:42:08 -0700582 id = srcu_read_lock(&dax_srcu);
Dave Jiangc791ace2017-02-24 14:57:08 -0800583 switch (pe_size) {
584 case PE_SIZE_PTE:
Dave Jianga2d58162017-02-24 14:56:59 -0800585 rc = __dax_dev_pte_fault(dax_dev, vmf);
586 break;
Dave Jiangc791ace2017-02-24 14:57:08 -0800587 case PE_SIZE_PMD:
Dave Jianga2d58162017-02-24 14:56:59 -0800588 rc = __dax_dev_pmd_fault(dax_dev, vmf);
589 break;
Dave Jiangc791ace2017-02-24 14:57:08 -0800590 case PE_SIZE_PUD:
Dave Jiang9557fee2017-02-24 14:57:05 -0800591 rc = __dax_dev_pud_fault(dax_dev, vmf);
592 break;
Dave Jianga2d58162017-02-24 14:56:59 -0800593 default:
594 return VM_FAULT_FALLBACK;
595 }
Dan Williams956a4cd2017-04-07 16:42:08 -0700596 srcu_read_unlock(&dax_srcu, id);
Dan Williamsdee41072016-05-14 12:20:44 -0700597
598 return rc;
599}
600
Dave Jiangc791ace2017-02-24 14:57:08 -0800601static int dax_dev_fault(struct vm_fault *vmf)
602{
603 return dax_dev_huge_fault(vmf, PE_SIZE_PTE);
604}
605
Dan Williamsdee41072016-05-14 12:20:44 -0700606static const struct vm_operations_struct dax_dev_vm_ops = {
607 .fault = dax_dev_fault,
Dave Jiangc791ace2017-02-24 14:57:08 -0800608 .huge_fault = dax_dev_huge_fault,
Dan Williamsdee41072016-05-14 12:20:44 -0700609};
610
Dan Williamsaf69f512016-08-11 00:38:03 -0700611static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
Dan Williamsdee41072016-05-14 12:20:44 -0700612{
613 struct dax_dev *dax_dev = filp->private_data;
614 int rc;
615
Dan Williamsebd84d72016-08-11 00:41:51 -0700616 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williamsdee41072016-05-14 12:20:44 -0700617
618 rc = check_vma(dax_dev, vma, __func__);
619 if (rc)
620 return rc;
621
Dan Williamsdee41072016-05-14 12:20:44 -0700622 vma->vm_ops = &dax_dev_vm_ops;
623 vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE;
624 return 0;
Dan Williams043a9252016-08-07 08:23:56 -0700625}
Dan Williamsdee41072016-05-14 12:20:44 -0700626
Dan Williams043a9252016-08-07 08:23:56 -0700627/* return an unmapped area aligned to the dax region specified alignment */
Dan Williamsaf69f512016-08-11 00:38:03 -0700628static unsigned long dax_get_unmapped_area(struct file *filp,
Dan Williams043a9252016-08-07 08:23:56 -0700629 unsigned long addr, unsigned long len, unsigned long pgoff,
630 unsigned long flags)
631{
632 unsigned long off, off_end, off_align, len_align, addr_align, align;
633 struct dax_dev *dax_dev = filp ? filp->private_data : NULL;
634 struct dax_region *dax_region;
635
636 if (!dax_dev || addr)
637 goto out;
638
639 dax_region = dax_dev->region;
640 align = dax_region->align;
641 off = pgoff << PAGE_SHIFT;
642 off_end = off + len;
643 off_align = round_up(off, align);
644
645 if ((off_end <= off_align) || ((off_end - off_align) < align))
646 goto out;
647
648 len_align = len + align;
649 if ((off + len_align) < off)
650 goto out;
651
652 addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
653 pgoff, flags);
654 if (!IS_ERR_VALUE(addr_align)) {
655 addr_align += (off - addr_align) & (align - 1);
656 return addr_align;
657 }
658 out:
659 return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
660}
661
Dan Williamsaf69f512016-08-11 00:38:03 -0700662static int dax_open(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700663{
Dan Williamsba09c012016-07-24 15:55:42 -0700664 struct dax_dev *dax_dev;
Dan Williams043a9252016-08-07 08:23:56 -0700665
Dan Williamsba09c012016-07-24 15:55:42 -0700666 dax_dev = container_of(inode->i_cdev, struct dax_dev, cdev);
667 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williams3bc52c42016-07-24 21:55:45 -0700668 inode->i_mapping = dax_dev->inode->i_mapping;
669 inode->i_mapping->host = dax_dev->inode;
670 filp->f_mapping = inode->i_mapping;
Dan Williamsebd84d72016-08-11 00:41:51 -0700671 filp->private_data = dax_dev;
672 inode->i_flags = S_DAX;
Dan Williams043a9252016-08-07 08:23:56 -0700673
Dan Williams043a9252016-08-07 08:23:56 -0700674 return 0;
675}
676
Dan Williamsaf69f512016-08-11 00:38:03 -0700677static int dax_release(struct inode *inode, struct file *filp)
Dan Williams043a9252016-08-07 08:23:56 -0700678{
679 struct dax_dev *dax_dev = filp->private_data;
Dan Williams043a9252016-08-07 08:23:56 -0700680
Dan Williamsba09c012016-07-24 15:55:42 -0700681 dev_dbg(&dax_dev->dev, "%s\n", __func__);
Dan Williams043a9252016-08-07 08:23:56 -0700682 return 0;
Dan Williamsdee41072016-05-14 12:20:44 -0700683}
684
Dan Williamsab68f262016-05-18 09:15:08 -0700685static const struct file_operations dax_fops = {
686 .llseek = noop_llseek,
687 .owner = THIS_MODULE,
Dan Williamsaf69f512016-08-11 00:38:03 -0700688 .open = dax_open,
689 .release = dax_release,
690 .get_unmapped_area = dax_get_unmapped_area,
691 .mmap = dax_mmap,
Dan Williamsab68f262016-05-18 09:15:08 -0700692};
693
Dan Williamsebd84d72016-08-11 00:41:51 -0700694static void dax_dev_release(struct device *dev)
Dan Williams043a9252016-08-07 08:23:56 -0700695{
Dan Williamsebd84d72016-08-11 00:41:51 -0700696 struct dax_dev *dax_dev = to_dax_dev(dev);
Dan Williams043a9252016-08-07 08:23:56 -0700697 struct dax_region *dax_region = dax_dev->region;
698
Dan Williamsebd84d72016-08-11 00:41:51 -0700699 ida_simple_remove(&dax_region->ida, dax_dev->id);
700 ida_simple_remove(&dax_minor_ida, MINOR(dev->devt));
701 dax_region_put(dax_region);
Dan Williams3bc52c42016-07-24 21:55:45 -0700702 iput(dax_dev->inode);
Dan Williamsebd84d72016-08-11 00:41:51 -0700703 kfree(dax_dev);
704}
705
706static void unregister_dax_dev(void *dev)
707{
708 struct dax_dev *dax_dev = to_dax_dev(dev);
Dan Williamsba09c012016-07-24 15:55:42 -0700709 struct cdev *cdev = &dax_dev->cdev;
Dan Williamsebd84d72016-08-11 00:41:51 -0700710
Dan Williams043a9252016-08-07 08:23:56 -0700711 dev_dbg(dev, "%s\n", __func__);
712
713 /*
714 * Note, rcu is not protecting the liveness of dax_dev, rcu is
715 * ensuring that any fault handlers that might have seen
716 * dax_dev->alive == true, have completed. Any fault handlers
Dan Williams956a4cd2017-04-07 16:42:08 -0700717 * that start after synchronize_srcu() has started will abort
Dan Williams043a9252016-08-07 08:23:56 -0700718 * upon seeing dax_dev->alive == false.
719 */
720 dax_dev->alive = false;
Dan Williams956a4cd2017-04-07 16:42:08 -0700721 synchronize_srcu(&dax_srcu);
Dan Williams9dc1e492016-08-04 16:53:50 -0700722 unmap_mapping_range(dax_dev->inode->i_mapping, 0, 0, 1);
Dan Williamsba09c012016-07-24 15:55:42 -0700723 cdev_del(cdev);
Dan Williams043a9252016-08-07 08:23:56 -0700724 device_unregister(dev);
Dan Williams043a9252016-08-07 08:23:56 -0700725}
726
Dan Williamsd76911e2016-07-19 17:51:40 -0700727struct dax_dev *devm_create_dax_dev(struct dax_region *dax_region,
728 struct resource *res, int count)
Dan Williams043a9252016-08-07 08:23:56 -0700729{
730 struct device *parent = dax_region->dev;
731 struct dax_dev *dax_dev;
Dan Williams9d2d01a2016-07-19 16:17:58 -0700732 int rc = 0, minor, i;
Dan Williams043a9252016-08-07 08:23:56 -0700733 struct device *dev;
Dan Williamsba09c012016-07-24 15:55:42 -0700734 struct cdev *cdev;
Dan Williams043a9252016-08-07 08:23:56 -0700735 dev_t dev_t;
736
737 dax_dev = kzalloc(sizeof(*dax_dev) + sizeof(*res) * count, GFP_KERNEL);
738 if (!dax_dev)
Dan Williamsd76911e2016-07-19 17:51:40 -0700739 return ERR_PTR(-ENOMEM);
Dan Williams043a9252016-08-07 08:23:56 -0700740
Dan Williams9d2d01a2016-07-19 16:17:58 -0700741 for (i = 0; i < count; i++) {
742 if (!IS_ALIGNED(res[i].start, dax_region->align)
743 || !IS_ALIGNED(resource_size(&res[i]),
744 dax_region->align)) {
745 rc = -EINVAL;
746 break;
747 }
748 dax_dev->res[i].start = res[i].start;
749 dax_dev->res[i].end = res[i].end;
750 }
751
752 if (i < count)
753 goto err_id;
754
Dan Williams043a9252016-08-07 08:23:56 -0700755 dax_dev->id = ida_simple_get(&dax_region->ida, 0, 0, GFP_KERNEL);
756 if (dax_dev->id < 0) {
757 rc = dax_dev->id;
758 goto err_id;
759 }
760
761 minor = ida_simple_get(&dax_minor_ida, 0, 0, GFP_KERNEL);
762 if (minor < 0) {
763 rc = minor;
764 goto err_minor;
765 }
766
Arnd Bergmannbc0a0fe2016-09-08 15:53:28 +0200767 dev_t = MKDEV(MAJOR(dax_devt), minor);
768 dev = &dax_dev->dev;
Dan Williams3bc52c42016-07-24 21:55:45 -0700769 dax_dev->inode = dax_inode_get(&dax_dev->cdev, dev_t);
770 if (!dax_dev->inode) {
771 rc = -ENOMEM;
772 goto err_inode;
773 }
774
Dan Williamsba09c012016-07-24 15:55:42 -0700775 /* device_initialize() so cdev can reference kobj parent */
Dan Williamsebd84d72016-08-11 00:41:51 -0700776 device_initialize(dev);
Dan Williamsba09c012016-07-24 15:55:42 -0700777
778 cdev = &dax_dev->cdev;
779 cdev_init(cdev, &dax_fops);
780 cdev->owner = parent->driver->owner;
781 cdev->kobj.parent = &dev->kobj;
782 rc = cdev_add(&dax_dev->cdev, dev_t, 1);
783 if (rc)
784 goto err_cdev;
785
786 /* from here on we're committed to teardown via dax_dev_release() */
Dan Williamsba09c012016-07-24 15:55:42 -0700787 dax_dev->num_resources = count;
788 dax_dev->alive = true;
789 dax_dev->region = dax_region;
790 kref_get(&dax_region->kref);
791
Dan Williamsebd84d72016-08-11 00:41:51 -0700792 dev->devt = dev_t;
793 dev->class = dax_class;
794 dev->parent = parent;
795 dev->groups = dax_attribute_groups;
796 dev->release = dax_dev_release;
797 dev_set_name(dev, "dax%d.%d", dax_region->id, dax_dev->id);
798 rc = device_add(dev);
799 if (rc) {
800 put_device(dev);
Dan Williamsd76911e2016-07-19 17:51:40 -0700801 return ERR_PTR(rc);
Dan Williamsebd84d72016-08-11 00:41:51 -0700802 }
Dan Williams043a9252016-08-07 08:23:56 -0700803
Dan Williamsd76911e2016-07-19 17:51:40 -0700804 rc = devm_add_action_or_reset(dax_region->dev, unregister_dax_dev, dev);
805 if (rc)
806 return ERR_PTR(rc);
807
808 return dax_dev;
Dan Williams043a9252016-08-07 08:23:56 -0700809
Dan Williamsba09c012016-07-24 15:55:42 -0700810 err_cdev:
Dan Williams3bc52c42016-07-24 21:55:45 -0700811 iput(dax_dev->inode);
812 err_inode:
Dan Williamsba09c012016-07-24 15:55:42 -0700813 ida_simple_remove(&dax_minor_ida, minor);
Dan Williams043a9252016-08-07 08:23:56 -0700814 err_minor:
815 ida_simple_remove(&dax_region->ida, dax_dev->id);
816 err_id:
Dan Williamsebd84d72016-08-11 00:41:51 -0700817 kfree(dax_dev);
Dan Williams043a9252016-08-07 08:23:56 -0700818
Dan Williamsd76911e2016-07-19 17:51:40 -0700819 return ERR_PTR(rc);
Dan Williams043a9252016-08-07 08:23:56 -0700820}
821EXPORT_SYMBOL_GPL(devm_create_dax_dev);
822
Dan Williamsab68f262016-05-18 09:15:08 -0700823static int __init dax_init(void)
824{
825 int rc;
826
Dan Williams3bc52c42016-07-24 21:55:45 -0700827 rc = dax_inode_init();
Dan Williamsba09c012016-07-24 15:55:42 -0700828 if (rc)
Dan Williamsab68f262016-05-18 09:15:08 -0700829 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700830
Dan Williams3bc52c42016-07-24 21:55:45 -0700831 nr_dax = max(nr_dax, 256);
832 rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
833 if (rc)
834 goto err_chrdev;
Dan Williamsab68f262016-05-18 09:15:08 -0700835
836 dax_class = class_create(THIS_MODULE, "dax");
837 if (IS_ERR(dax_class)) {
Dan Williams3bc52c42016-07-24 21:55:45 -0700838 rc = PTR_ERR(dax_class);
839 goto err_class;
Dan Williamsab68f262016-05-18 09:15:08 -0700840 }
841
842 return 0;
Dan Williams3bc52c42016-07-24 21:55:45 -0700843
844 err_class:
845 unregister_chrdev_region(dax_devt, nr_dax);
846 err_chrdev:
847 dax_inode_exit();
848 return rc;
Dan Williamsab68f262016-05-18 09:15:08 -0700849}
850
851static void __exit dax_exit(void)
852{
853 class_destroy(dax_class);
Dan Williamsba09c012016-07-24 15:55:42 -0700854 unregister_chrdev_region(dax_devt, nr_dax);
Dan Williamsab68f262016-05-18 09:15:08 -0700855 ida_destroy(&dax_minor_ida);
Dan Williams3bc52c42016-07-24 21:55:45 -0700856 dax_inode_exit();
Dan Williamsab68f262016-05-18 09:15:08 -0700857}
858
859MODULE_AUTHOR("Intel Corporation");
860MODULE_LICENSE("GPL v2");
861subsys_initcall(dax_init);
862module_exit(dax_exit);