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

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

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

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

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

	if (!vma_is_dax(vma)) {
45 46
		dev_info_ratelimited(dev,
				"%s: %s: fail, vma is not DAX capable\n",
47 48 49 50 51 52 53
				current->comm, func);
		return -EINVAL;
	}

	return 0;
}

54
/* see "strong" declaration in tools/testing/nvdimm/dax-dev.c */
55
__weak phys_addr_t dax_pgoff_to_phys(struct dev_dax *dev_dax, pgoff_t pgoff,
56 57
		unsigned long size)
{
58
	struct range *range = &dev_dax->range;
59
	phys_addr_t phys;
60

61 62 63
	phys = pgoff * PAGE_SIZE + range->start;
	if (phys >= range->start && phys <= range->end) {
		if (phys + size - 1 <= range->end)
64 65 66 67 68 69
			return phys;
	}

	return -1;
}

70
static vm_fault_t __dev_dax_pte_fault(struct dev_dax *dev_dax,
D
Dan Williams 已提交
71
				struct vm_fault *vmf, pfn_t *pfn)
72
{
73
	struct device *dev = &dev_dax->dev;
74 75
	struct dax_region *dax_region;
	phys_addr_t phys;
76
	unsigned int fault_size = PAGE_SIZE;
77

78
	if (check_vma(dev_dax, vmf->vma, __func__))
79 80
		return VM_FAULT_SIGBUS;

81
	dax_region = dev_dax->region;
82
	if (dax_region->align > PAGE_SIZE) {
83 84
		dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
			dax_region->align, fault_size);
85 86 87
		return VM_FAULT_SIGBUS;
	}

88 89 90
	if (fault_size != dax_region->align)
		return VM_FAULT_SIGBUS;

91
	phys = dax_pgoff_to_phys(dev_dax, vmf->pgoff, PAGE_SIZE);
92
	if (phys == -1) {
93
		dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", vmf->pgoff);
94 95 96
		return VM_FAULT_SIGBUS;
	}

97
	*pfn = phys_to_pfn_t(phys, PFN_DEV|PFN_MAP);
98

D
Dan Williams 已提交
99
	return vmf_insert_mixed(vmf->vma, vmf->address, *pfn);
100 101
}

102
static vm_fault_t __dev_dax_pmd_fault(struct dev_dax *dev_dax,
D
Dan Williams 已提交
103
				struct vm_fault *vmf, pfn_t *pfn)
104
{
105
	unsigned long pmd_addr = vmf->address & PMD_MASK;
106
	struct device *dev = &dev_dax->dev;
107 108 109
	struct dax_region *dax_region;
	phys_addr_t phys;
	pgoff_t pgoff;
110
	unsigned int fault_size = PMD_SIZE;
111

112
	if (check_vma(dev_dax, vmf->vma, __func__))
113 114
		return VM_FAULT_SIGBUS;

115
	dax_region = dev_dax->region;
116
	if (dax_region->align > PMD_SIZE) {
117 118
		dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
			dax_region->align, fault_size);
119 120 121
		return VM_FAULT_SIGBUS;
	}

122 123 124 125 126 127 128 129 130 131
	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;

132
	pgoff = linear_page_index(vmf->vma, pmd_addr);
133
	phys = dax_pgoff_to_phys(dev_dax, pgoff, PMD_SIZE);
134
	if (phys == -1) {
135
		dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff);
136 137 138
		return VM_FAULT_SIGBUS;
	}

139
	*pfn = phys_to_pfn_t(phys, PFN_DEV|PFN_MAP);
140

141
	return vmf_insert_pfn_pmd(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE);
142 143
}

144
#ifdef CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD
145
static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax,
D
Dan Williams 已提交
146
				struct vm_fault *vmf, pfn_t *pfn)
147 148
{
	unsigned long pud_addr = vmf->address & PUD_MASK;
149
	struct device *dev = &dev_dax->dev;
150 151 152
	struct dax_region *dax_region;
	phys_addr_t phys;
	pgoff_t pgoff;
153 154
	unsigned int fault_size = PUD_SIZE;

155

156
	if (check_vma(dev_dax, vmf->vma, __func__))
157 158
		return VM_FAULT_SIGBUS;

159
	dax_region = dev_dax->region;
160
	if (dax_region->align > PUD_SIZE) {
161 162
		dev_dbg(dev, "alignment (%#x) > fault size (%#x)\n",
			dax_region->align, fault_size);
163 164 165
		return VM_FAULT_SIGBUS;
	}

166 167 168 169 170 171 172 173 174 175
	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;

176
	pgoff = linear_page_index(vmf->vma, pud_addr);
177
	phys = dax_pgoff_to_phys(dev_dax, pgoff, PUD_SIZE);
178
	if (phys == -1) {
179
		dev_dbg(dev, "pgoff_to_phys(%#lx) failed\n", pgoff);
180 181 182
		return VM_FAULT_SIGBUS;
	}

183
	*pfn = phys_to_pfn_t(phys, PFN_DEV|PFN_MAP);
184

185
	return vmf_insert_pfn_pud(vmf, *pfn, vmf->flags & FAULT_FLAG_WRITE);
186 187
}
#else
188
static vm_fault_t __dev_dax_pud_fault(struct dev_dax *dev_dax,
D
Dan Williams 已提交
189
				struct vm_fault *vmf, pfn_t *pfn)
190 191 192 193 194
{
	return VM_FAULT_FALLBACK;
}
#endif /* !CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD */

195
static vm_fault_t dev_dax_huge_fault(struct vm_fault *vmf,
196
		enum page_entry_size pe_size)
197
{
198
	struct file *filp = vmf->vma->vm_file;
D
Dan Williams 已提交
199
	unsigned long fault_size;
200 201
	vm_fault_t rc = VM_FAULT_SIGBUS;
	int id;
D
Dan Williams 已提交
202
	pfn_t pfn;
203
	struct dev_dax *dev_dax = filp->private_data;
204

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

209
	id = dax_read_lock();
210 211
	switch (pe_size) {
	case PE_SIZE_PTE:
D
Dan Williams 已提交
212 213
		fault_size = PAGE_SIZE;
		rc = __dev_dax_pte_fault(dev_dax, vmf, &pfn);
214
		break;
215
	case PE_SIZE_PMD:
D
Dan Williams 已提交
216 217
		fault_size = PMD_SIZE;
		rc = __dev_dax_pmd_fault(dev_dax, vmf, &pfn);
218
		break;
219
	case PE_SIZE_PUD:
D
Dan Williams 已提交
220 221
		fault_size = PUD_SIZE;
		rc = __dev_dax_pud_fault(dev_dax, vmf, &pfn);
222 223
		break;
	default:
224
		rc = VM_FAULT_SIGBUS;
225
	}
D
Dan Williams 已提交
226 227 228

	if (rc == VM_FAULT_NOPAGE) {
		unsigned long i;
D
Dan Williams 已提交
229
		pgoff_t pgoff;
D
Dan Williams 已提交
230 231 232 233 234 235 236

		/*
		 * 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 已提交
237 238
		pgoff = linear_page_index(vmf->vma, vmf->address
				& ~(fault_size - 1));
D
Dan Williams 已提交
239 240 241 242 243 244 245
		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 已提交
246
			page->index = pgoff + i;
D
Dan Williams 已提交
247 248
		}
	}
249
	dax_read_unlock(id);
250 251 252 253

	return rc;
}

254
static vm_fault_t dev_dax_fault(struct vm_fault *vmf)
255
{
256
	return dev_dax_huge_fault(vmf, PE_SIZE_PTE);
257 258
}

259 260 261 262 263 264 265 266 267 268 269
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;
}

270 271 272 273 274 275 276 277 278
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;
}

279 280 281
static const struct vm_operations_struct dax_vm_ops = {
	.fault = dev_dax_fault,
	.huge_fault = dev_dax_huge_fault,
282
	.split = dev_dax_split,
283
	.pagesize = dev_dax_pagesize,
284 285
};

286
static int dax_mmap(struct file *filp, struct vm_area_struct *vma)
287
{
288
	struct dev_dax *dev_dax = filp->private_data;
289
	int rc, id;
290

291
	dev_dbg(&dev_dax->dev, "trace\n");
292

293 294 295 296 297
	/*
	 * We lock to check dax_dev liveness and will re-check at
	 * fault time.
	 */
	id = dax_read_lock();
298
	rc = check_vma(dev_dax, vma, __func__);
299
	dax_read_unlock(id);
300 301 302
	if (rc)
		return rc;

303
	vma->vm_ops = &dax_vm_ops;
304
	vma->vm_flags |= VM_HUGEPAGE;
305
	return 0;
306 307 308
}

/* return an unmapped area aligned to the dax region specified alignment */
309
static unsigned long dax_get_unmapped_area(struct file *filp,
310 311 312 313
		unsigned long addr, unsigned long len, unsigned long pgoff,
		unsigned long flags)
{
	unsigned long off, off_end, off_align, len_align, addr_align, align;
314
	struct dev_dax *dev_dax = filp ? filp->private_data : NULL;
315 316
	struct dax_region *dax_region;

317
	if (!dev_dax || addr)
318 319
		goto out;

320
	dax_region = dev_dax->region;
321 322 323 324 325 326 327 328 329 330 331
	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;
332

333 334 335 336 337 338 339 340 341 342
	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);
}

343 344 345 346 347
static const struct address_space_operations dev_dax_aops = {
	.set_page_dirty		= noop_set_page_dirty,
	.invalidatepage		= noop_invalidatepage,
};

348
static int dax_open(struct inode *inode, struct file *filp)
349
{
350 351 352
	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);
353

354
	dev_dbg(&dev_dax->dev, "trace\n");
355 356
	inode->i_mapping = __dax_inode->i_mapping;
	inode->i_mapping->host = __dax_inode;
357
	inode->i_mapping->a_ops = &dev_dax_aops;
358
	filp->f_mapping = inode->i_mapping;
359
	filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
360
	filp->f_sb_err = file_sample_sb_err(filp);
361
	filp->private_data = dev_dax;
362
	inode->i_flags = S_DAX;
363 364 365

	return 0;
}
366

367
static int dax_release(struct inode *inode, struct file *filp)
368
{
369
	struct dev_dax *dev_dax = filp->private_data;
370

371
	dev_dbg(&dev_dax->dev, "trace\n");
372
	return 0;
373 374
}

375 376 377
static const struct file_operations dax_fops = {
	.llseek = noop_llseek,
	.owner = THIS_MODULE,
378 379 380 381
	.open = dax_open,
	.release = dax_release,
	.get_unmapped_area = dax_get_unmapped_area,
	.mmap = dax_mmap,
382
	.mmap_supported_flags = MAP_SYNC,
383 384
};

385
static void dev_dax_cdev_del(void *cdev)
386
{
387 388
	cdev_del(cdev);
}
389

390 391 392
static void dev_dax_kill(void *dev_dax)
{
	kill_dev_dax(dev_dax);
393 394
}

395
int dev_dax_probe(struct dev_dax *dev_dax)
396
{
397
	struct dax_device *dax_dev = dev_dax->dax_dev;
398
	struct range *range = &dev_dax->range;
399
	struct device *dev = &dev_dax->dev;
400
	struct dev_pagemap *pgmap;
401
	struct inode *inode;
D
Dan Williams 已提交
402
	struct cdev *cdev;
403
	void *addr;
404
	int rc;
405

406
	/* 1:1 map region resource range to device-dax instance range */
407
	if (!devm_request_mem_region(dev, range->start, range_len(range),
408
				dev_name(dev))) {
409 410
		dev_warn(dev, "could not reserve range: %#llx - %#llx\n",
				range->start, range->end);
411 412 413
		return -EBUSY;
	}

414 415 416 417 418
	pgmap = dev_dax->pgmap;
	if (!pgmap) {
		pgmap = devm_kzalloc(dev, sizeof(*pgmap), GFP_KERNEL);
		if (!pgmap)
			return -ENOMEM;
419
		pgmap->range = *range;
420
		pgmap->nr_range = 1;
421 422 423
	}
	pgmap->type = MEMORY_DEVICE_GENERIC;
	addr = devm_memremap_pages(dev, pgmap);
424
	if (IS_ERR(addr))
425 426
		return PTR_ERR(addr);

427 428
	inode = dax_inode(dax_dev);
	cdev = inode->i_cdev;
D
Dan Williams 已提交
429
	cdev_init(cdev, &dax_fops);
430 431 432 433 434
	if (dev->class) {
		/* for the CONFIG_DEV_DAX_PMEM_COMPAT case */
		cdev->owner = dev->parent->driver->owner;
	} else
		cdev->owner = dev->driver->owner;
435 436
	cdev_set_parent(cdev, &dev->kobj);
	rc = cdev_add(cdev, dev->devt, 1);
437
	if (rc)
438
		return rc;
439

440 441 442
	rc = devm_add_action_or_reset(dev, dev_dax_cdev_del, cdev);
	if (rc)
		return rc;
443

444 445 446
	run_dax(dax_dev);
	return devm_add_action_or_reset(dev, dev_dax_kill, dev_dax);
}
447
EXPORT_SYMBOL_GPL(dev_dax_probe);
448

449
static int dev_dax_remove(struct dev_dax *dev_dax)
450 451 452
{
	/* all probe actions are unwound by devm */
	return 0;
453
}
454

455
static struct dax_device_driver device_dax_driver = {
456 457
	.probe = dev_dax_probe,
	.remove = dev_dax_remove,
458
	.match_always = 1,
459
};
460

461 462
static int __init dax_init(void)
{
463
	return dax_driver_register(&device_dax_driver);
464 465 466 467
}

static void __exit dax_exit(void)
{
468
	dax_driver_unregister(&device_dax_driver);
469 470 471 472
}

MODULE_AUTHOR("Intel Corporation");
MODULE_LICENSE("GPL v2");
473
module_init(dax_init);
474
module_exit(dax_exit);
475
MODULE_ALIAS_DAX_DEVICE(0);