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