device.c 12.2 KB
Newer Older
1 2
// SPDX-License-Identifier: GPL-2.0
/* Copyright(c) 2016-2018 Intel Corporation. All rights reserved. */
3 4 5 6
#include <linux/pagemap.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/pfn_t.h>
D
Dan Williams 已提交
7
#include <linux/cdev.h>
8 9 10 11
#include <linux/slab.h>
#include <linux/dax.h>
#include <linux/fs.h>
#include <linux/mm.h>
12
#include <linux/mman.h>
13
#include "dax-private.h"
14
#include "bus.h"
15 16 17

static struct class *dax_class;

18
static int check_vma(struct dev_dax *dev_dax, struct vm_area_struct *vma,
19 20
		const char *func)
{
21 22
	struct dax_region *dax_region = dev_dax->region;
	struct device *dev = &dev_dax->dev;
23 24
	unsigned long mask;

25
	if (!dax_alive(dev_dax->dax_dev))
26 27
		return -ENXIO;

28
	/* prevent private mappings from being established */
29
	if ((vma->vm_flags & VM_MAYSHARE) != VM_MAYSHARE) {
30 31
		dev_info_ratelimited(dev,
				"%s: %s: fail, attempted private mapping\n",
32 33 34 35 36 37
				current->comm, func);
		return -EINVAL;
	}

	mask = dax_region->align - 1;
	if (vma->vm_start & mask || vma->vm_end & mask) {
38 39
		dev_info_ratelimited(dev,
				"%s: %s: fail, unaligned vma (%#lx - %#lx, %#lx)\n",
40 41 42 43 44 45 46
				current->comm, func, vma->vm_start, vma->vm_end,
				mask);
		return -EINVAL;
	}

	if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) == PFN_DEV
			&& (vma->vm_flags & VM_DONTCOPY) == 0) {
47 48
		dev_info_ratelimited(dev,
				"%s: %s: fail, dax range requires MADV_DONTFORK\n",
49 50 51 52 53
				current->comm, func);
		return -EINVAL;
	}

	if (!vma_is_dax(vma)) {
54 55
		dev_info_ratelimited(dev,
				"%s: %s: fail, vma is not DAX capable\n",
56 57 58 59 60 61 62
				current->comm, func);
		return -EINVAL;
	}

	return 0;
}

63
/* see "strong" declaration in tools/testing/nvdimm/dax-dev.c */
64
__weak phys_addr_t dax_pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff,
65 66
		unsigned long size)
{
67 68
	struct resource *res = &dev_dax->region->res;
	phys_addr_t phys;
69

70 71
	phys = pgoff * PAGE_SIZE + res->start;
	if (phys >= res->start && phys <= res->end) {
72 73 74 75 76 77 78
		if (phys + size - 1 <= res->end)
			return phys;
	}

	return -1;
}

79
static vm_fault_t __dev_dax_pte_fault(struct dev_dax *dev_dax,
D
Dan Williams 已提交
80
				struct vm_fault *vmf, pfn_t *pfn)
81
{
82
	struct device *dev = &dev_dax->dev;
83 84
	struct dax_region *dax_region;
	phys_addr_t phys;
85
	unsigned int fault_size = PAGE_SIZE;
86

87
	if (check_vma(dev_dax, vmf->vma, __func__))
88 89
		return VM_FAULT_SIGBUS;

90
	dax_region = dev_dax->region;
91
	if (dax_region->align > PAGE_SIZE) {
92 93
		dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
			dax_region->align, fault_size);
94 95 96
		return VM_FAULT_SIGBUS;
	}

97 98 99
	if (fault_size != dax_region->align)
		return VM_FAULT_SIGBUS;

100
	phys = dax_pgoff_to_phys(dev_dax, vmf->pgoff, PAGE_SIZE);
101
	if (phys == -1) {
102
		dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", vmf->pgoff);
103 104 105
		return VM_FAULT_SIGBUS;
	}

D
Dan Williams 已提交
106
	*pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
107

D
Dan Williams 已提交
108
	return vmf_insert_mixed(vmf->vma, vmf->address, *pfn);
109 110
}

111
static vm_fault_t __dev_dax_pmd_fault(struct dev_dax *dev_dax,
D
Dan Williams 已提交
112
				struct vm_fault *vmf, pfn_t *pfn)
113
{
114
	unsigned long pmd_addr = vmf->address & PMD_MASK;
115
	struct device *dev = &dev_dax->dev;
116 117 118
	struct dax_region *dax_region;
	phys_addr_t phys;
	pgoff_t pgoff;
119
	unsigned int fault_size = PMD_SIZE;
120

121
	if (check_vma(dev_dax, vmf->vma, __func__))
122 123
		return VM_FAULT_SIGBUS;

124
	dax_region = dev_dax->region;
125
	if (dax_region->align > PMD_SIZE) {
126 127
		dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
			dax_region->align, fault_size);
128 129 130 131 132
		return VM_FAULT_SIGBUS;
	}

	/* dax pmd mappings require pfn_t_devmap() */
	if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
133
		dev_dbg(dev, "region lacks devmap flags\n");
134 135 136
		return VM_FAULT_SIGBUS;
	}

137 138 139 140 141 142 143 144 145 146
	if (fault_size < dax_region->align)
		return VM_FAULT_SIGBUS;
	else if (fault_size > dax_region->align)
		return VM_FAULT_FALLBACK;

	/* if we are outside of the VMA */
	if (pmd_addr < vmf->vma->vm_start ||
			(pmd_addr + PMD_SIZE) > vmf->vma->vm_end)
		return VM_FAULT_SIGBUS;

147
	pgoff = linear_page_index(vmf->vma, pmd_addr);
148
	phys = dax_pgoff_to_phys(dev_dax, pgoff, PMD_SIZE);
149
	if (phys == -1) {
150
		dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff);
151 152 153
		return VM_FAULT_SIGBUS;
	}

D
Dan Williams 已提交
154
	*pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
155

156
	return vmf_insert_pfn_pmd(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE);
157 158
}

159
#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
160
static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax,
D
Dan Williams 已提交
161
				struct vm_fault *vmf, pfn_t *pfn)
162 163
{
	unsigned long pud_addr = vmf->address & PUD_MASK;
164
	struct device *dev = &dev_dax->dev;
165 166 167
	struct dax_region *dax_region;
	phys_addr_t phys;
	pgoff_t pgoff;
168 169
	unsigned int fault_size = PUD_SIZE;

170

171
	if (check_vma(dev_dax, vmf->vma, __func__))
172 173
		return VM_FAULT_SIGBUS;

174
	dax_region = dev_dax->region;
175
	if (dax_region->align > PUD_SIZE) {
176 177
		dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
			dax_region->align, fault_size);
178 179 180 181 182
		return VM_FAULT_SIGBUS;
	}

	/* dax pud mappings require pfn_t_devmap() */
	if ((dax_region->pfn_flags & (PFN_DEV|PFN_MAP)) != (PFN_DEV|PFN_MAP)) {
183
		dev_dbg(dev, "region lacks devmap flags\n");
184 185 186
		return VM_FAULT_SIGBUS;
	}

187 188 189 190 191 192 193 194 195 196
	if (fault_size < dax_region->align)
		return VM_FAULT_SIGBUS;
	else if (fault_size > dax_region->align)
		return VM_FAULT_FALLBACK;

	/* if we are outside of the VMA */
	if (pud_addr < vmf->vma->vm_start ||
			(pud_addr + PUD_SIZE) > vmf->vma->vm_end)
		return VM_FAULT_SIGBUS;

197
	pgoff = linear_page_index(vmf->vma, pud_addr);
198
	phys = dax_pgoff_to_phys(dev_dax, pgoff, PUD_SIZE);
199
	if (phys == -1) {
200
		dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff);
201 202 203
		return VM_FAULT_SIGBUS;
	}

D
Dan Williams 已提交
204
	*pfn = phys_to_pfn_t(phys, dax_region->pfn_flags);
205

206
	return vmf_insert_pfn_pud(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE);
207 208
}
#else
209
static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax,
D
Dan Williams 已提交
210
				struct vm_fault *vmf, pfn_t *pfn)
211 212 213 214 215
{
	return VM_FAULT_FALLBACK;
}
#endif /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */

216
static vm_fault_t dev_dax_huge_fault(struct vm_fault *vmf,
217
		enum page_entry_size pe_size)
218
{
219
	struct file *filp = vmf->vma->vm_file;
D
Dan Williams 已提交
220
	unsigned long fault_size;
221 222
	vm_fault_t rc = VM_FAULT_SIGBUS;
	int id;
D
Dan Williams 已提交
223
	pfn_t pfn;
224
	struct dev_dax *dev_dax = filp->private_data;
225

226 227
	dev_dbg(&dev_dax->dev, "%s: %s (%#lx - %#lx) size = %d\n", current->comm,
			(vmf->flags & FAULT_FLAG_WRITE) ? "write" : "read",
228
			vmf->vma->vm_start, vmf->vma->vm_end, pe_size);
229

230
	id = dax_read_lock();
231 232
	switch (pe_size) {
	case PE_SIZE_PTE:
D
Dan Williams 已提交
233 234
		fault_size = PAGE_SIZE;
		rc = __dev_dax_pte_fault(dev_dax, vmf, &pfn);
235
		break;
236
	case PE_SIZE_PMD:
D
Dan Williams 已提交
237 238
		fault_size = PMD_SIZE;
		rc = __dev_dax_pmd_fault(dev_dax, vmf, &pfn);
239
		break;
240
	case PE_SIZE_PUD:
D
Dan Williams 已提交
241 242
		fault_size = PUD_SIZE;
		rc = __dev_dax_pud_fault(dev_dax, vmf, &pfn);
243 244
		break;
	default:
245
		rc = VM_FAULT_SIGBUS;
246
	}
D
Dan Williams 已提交
247 248 249

	if (rc == VM_FAULT_NOPAGE) {
		unsigned long i;
D
Dan Williams 已提交
250
		pgoff_t pgoff;
D
Dan Williams 已提交
251 252 253 254 255 256 257

		/*
		 * In the device-dax case the only possibility for a
		 * VM_FAULT_NOPAGE result is when device-dax capacity is
		 * mapped. No need to consider the zero page, or racing
		 * conflicting mappings.
		 */
D
Dan Williams 已提交
258 259
		pgoff = linear_page_index(vmf->vma, vmf->address
				& ~(fault_size - 1));
D
Dan Williams 已提交
260 261 262 263 264 265 266
		for (i = 0; i < fault_size / PAGE_SIZE; i++) {
			struct page *page;

			page = pfn_to_page(pfn_t_to_pfn(pfn) + i);
			if (page->mapping)
				continue;
			page->mapping = filp->f_mapping;
D
Dan Williams 已提交
267
			page->index = pgoff + i;
D
Dan Williams 已提交
268 269
		}
	}
270
	dax_read_unlock(id);
271 272 273 274

	return rc;
}

275
static vm_fault_t dev_dax_fault(struct vm_fault *vmf)
276
{
277
	return dev_dax_huge_fault(vmf, PE_SIZE_PTE);
278 279
}

280 281 282 283 284 285 286 287 288 289 290
static int dev_dax_split(struct vm_area_struct *vma, unsigned long addr)
{
	struct file *filp = vma->vm_file;
	struct dev_dax *dev_dax = filp->private_data;
	struct dax_region *dax_region = dev_dax->region;

	if (!IS_ALIGNED(addr, dax_region->align))
		return -EINVAL;
	return 0;
}

291 292 293 294 295 296 297 298 299
static unsigned long dev_dax_pagesize(struct vm_area_struct *vma)
{
	struct file *filp = vma->vm_file;
	struct dev_dax *dev_dax = filp->private_data;
	struct dax_region *dax_region = dev_dax->region;

	return dax_region->align;
}

300 301 302
static const struct vm_operations_struct dax_vm_ops = {
	.fault = dev_dax_fault,
	.huge_fault = dev_dax_huge_fault,
303
	.split = dev_dax_split,
304
	.pagesize = dev_dax_pagesize,
305 306
};

307
static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
308
{
309
	struct dev_dax *dev_dax = filp->private_data;
310
	int rc, id;
311

312
	dev_dbg(&dev_dax->dev, "trace\n");
313

314 315 316 317 318
	/*
	 * We lock to check dax_dev liveness and will re-check at
	 * fault time.
	 */
	id = dax_read_lock();
319
	rc = check_vma(dev_dax, vma, __func__);
320
	dax_read_unlock(id);
321 322 323
	if (rc)
		return rc;

324
	vma->vm_ops = &dax_vm_ops;
325
	vma->vm_flags |= VM_HUGEPAGE;
326
	return 0;
327 328 329
}

/* return an unmapped area aligned to the dax region specified alignment */
330
static unsigned long dax_get_unmapped_area(struct file *filp,
331 332 333 334
		unsigned long addr, unsigned long len, unsigned long pgoff,
		unsigned long flags)
{
	unsigned long off, off_end, off_align, len_align, addr_align, align;
335
	struct dev_dax *dev_dax = filp ? filp->private_data : NULL;
336 337
	struct dax_region *dax_region;

338
	if (!dev_dax || addr)
339 340
		goto out;

341
	dax_region = dev_dax->region;
342 343 344 345 346 347 348 349 350 351 352
	align = dax_region->align;
	off = pgoff << PAGE_SHIFT;
	off_end = off + len;
	off_align = round_up(off, align);

	if ((off_end <= off_align) || ((off_end - off_align) < align))
		goto out;

	len_align = len + align;
	if ((off + len_align) < off)
		goto out;
353

354 355 356 357 358 359 360 361 362 363
	addr_align = current->mm->get_unmapped_area(filp, addr, len_align,
			pgoff, flags);
	if (!IS_ERR_VALUE(addr_align)) {
		addr_align += (off - addr_align) & (align - 1);
		return addr_align;
	}
 out:
	return current->mm->get_unmapped_area(filp, addr, len, pgoff, flags);
}

364 365 366 367 368
static const struct address_space_operations dev_dax_aops = {
	.set_page_dirty		= noop_set_page_dirty,
	.invalidatepage		= noop_invalidatepage,
};

369
static int dax_open(struct inode *inode, struct file *filp)
370
{
371 372 373
	struct dax_device *dax_dev = inode_dax(inode);
	struct inode *__dax_inode = dax_inode(dax_dev);
	struct dev_dax *dev_dax = dax_get_private(dax_dev);
374

375
	dev_dbg(&dev_dax->dev, "trace\n");
376 377
	inode->i_mapping = __dax_inode->i_mapping;
	inode->i_mapping->host = __dax_inode;
378
	inode->i_mapping->a_ops = &dev_dax_aops;
379
	filp->f_mapping = inode->i_mapping;
380
	filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
381
	filp->private_data = dev_dax;
382
	inode->i_flags = S_DAX;
383 384 385

	return 0;
}
386

387
static int dax_release(struct inode *inode, struct file *filp)
388
{
389
	struct dev_dax *dev_dax = filp->private_data;
390

391
	dev_dbg(&dev_dax->dev, "trace\n");
392
	return 0;
393 394
}

395 396 397
static const struct file_operations dax_fops = {
	.llseek = noop_llseek,
	.owner = THIS_MODULE,
398 399 400 401
	.open = dax_open,
	.release = dax_release,
	.get_unmapped_area = dax_get_unmapped_area,
	.mmap = dax_mmap,
402
	.mmap_supported_flags = MAP_SYNC,
403 404
};

405
static void dev_dax_release(struct device *dev)
406
{
407 408
	struct dev_dax *dev_dax = to_dev_dax(dev);
	struct dax_region *dax_region = dev_dax->region;
409
	struct dax_device *dax_dev = dev_dax->dax_dev;
410

411
	dax_region_put(dax_region);
412
	put_dax(dax_dev);
413
	kfree(dev_dax);
414 415
}

416
struct dev_dax *devm_create_dev_dax(struct dax_region *dax_region, int id)
417 418
{
	struct device *parent = dax_region->dev;
419
	struct dax_device *dax_dev;
420
	struct dev_dax *dev_dax;
421
	struct inode *inode;
422
	struct device *dev;
D
Dan Williams 已提交
423
	struct cdev *cdev;
424
	int rc;
425

426
	dev_dax = kzalloc(sizeof(*dev_dax), GFP_KERNEL);
427
	if (!dev_dax)
428
		return ERR_PTR(-ENOMEM);
429

D
Dan Williams 已提交
430 431 432 433 434
	/*
	 * No 'host' or dax_operations since there is no access to this
	 * device outside of mmap of the resulting character device.
	 */
	dax_dev = alloc_dax(dev_dax, NULL, NULL);
435 436
	if (!dax_dev) {
		rc = -ENOMEM;
D
Dan Williams 已提交
437
		goto err;
438
	}
439

440
	/* from here on we're committed to teardown via dax_dev_release() */
441
	dev = &dev_dax->dev;
442
	device_initialize(dev);
D
Dan Williams 已提交
443

444 445
	inode = dax_inode(dax_dev);
	cdev = inode->i_cdev;
D
Dan Williams 已提交
446 447 448
	cdev_init(cdev, &dax_fops);
	cdev->owner = parent->driver->owner;

449
	dev_dax->dax_dev = dax_dev;
450
	dev_dax->region = dax_region;
D
Dan Williams 已提交
451 452
	kref_get(&dax_region->kref);

453
	dev->devt = inode->i_rdev;
454 455 456
	dev->class = dax_class;
	dev->parent = parent;
	dev->groups = dax_attribute_groups;
457
	dev->release = dev_dax_release;
458
	dev_set_name(dev, "dax%d.%d", dax_region->id, id);
459 460

	rc = cdev_device_add(cdev, dev);
461
	if (rc) {
462
		kill_dev_dax(dev_dax);
463
		put_device(dev);
464
		return ERR_PTR(rc);
465
	}
466

467
	rc = devm_add_action_or_reset(dax_region->dev, unregister_dev_dax, dev);
468 469 470
	if (rc)
		return ERR_PTR(rc);

471
	return dev_dax;
472

D
Dan Williams 已提交
473
 err:
474
	kfree(dev_dax);
475

476
	return ERR_PTR(rc);
477
}
478
EXPORT_SYMBOL_GPL(devm_create_dev_dax);
479

480 481 482
static int __init dax_init(void)
{
	dax_class = class_create(THIS_MODULE, "dax");
483
	return PTR_ERR_OR_ZERO(dax_class);
484 485 486 487 488 489 490 491 492 493 494
}

static void __exit dax_exit(void)
{
	class_destroy(dax_class);
}

MODULE_AUTHOR("Intel Corporation");
MODULE_LICENSE("GPL v2");
subsys_initcall(dax_init);
module_exit(dax_exit);