Skip to content

Commit b568457

Browse files
committed
Merge branch 'for-4.18/dax' into libnvdimm-for-next
2 parents 808c340 + cc4a90a commit b568457

21 files changed

Lines changed: 545 additions & 295 deletions

File tree

‎drivers/dax/super.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ int __bdev_dax_supported(struct super_block *sb, int blocksize)
8686
{
8787
struct block_device *bdev = sb->s_bdev;
8888
struct dax_device *dax_dev;
89+
bool dax_enabled = false;
8990
pgoff_t pgoff;
9091
int err, id;
9192
void *kaddr;
@@ -134,14 +135,21 @@ int __bdev_dax_supported(struct super_block *sb, int blocksize)
134135
* on being able to do (page_address(pfn_to_page())).
135136
*/
136137
WARN_ON(IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API));
138+
dax_enabled = true;
137139
} else if (pfn_t_devmap(pfn)) {
138-
/* pass */;
139-
} else {
140+
struct dev_pagemap *pgmap;
141+
142+
pgmap = get_dev_pagemap(pfn_t_to_pfn(pfn), NULL);
143+
if (pgmap && pgmap->type == MEMORY_DEVICE_FS_DAX)
144+
dax_enabled = true;
145+
put_dev_pagemap(pgmap);
146+
}
147+
148+
if (!dax_enabled) {
140149
pr_debug("VFS (%s): error: dax support not enabled\n",
141150
sb->s_id);
142151
return -EOPNOTSUPP;
143152
}
144-
145153
return 0;
146154
}
147155
EXPORT_SYMBOL_GPL(__bdev_dax_supported);

‎drivers/nvdimm/pfn_devs.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,6 @@ static int __nvdimm_setup_pfn(struct nd_pfn *nd_pfn, struct dev_pagemap *pgmap)
561561
res->start += start_pad;
562562
res->end -= end_trunc;
563563

564-
pgmap->type = MEMORY_DEVICE_HOST;
565-
566564
if (nd_pfn->mode == PFN_MODE_RAM) {
567565
if (offset < SZ_8K)
568566
return -EINVAL;

‎drivers/nvdimm/pmem.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,27 @@ static void pmem_release_disk(void *__pmem)
289289
put_disk(pmem->disk);
290290
}
291291

292+
static void pmem_release_pgmap_ops(void *__pgmap)
293+
{
294+
dev_pagemap_put_ops();
295+
}
296+
297+
static void fsdax_pagefree(struct page *page, void *data)
298+
{
299+
wake_up_var(&page->_refcount);
300+
}
301+
302+
static int setup_pagemap_fsdax(struct device *dev, struct dev_pagemap *pgmap)
303+
{
304+
dev_pagemap_get_ops();
305+
if (devm_add_action_or_reset(dev, pmem_release_pgmap_ops, pgmap))
306+
return -ENOMEM;
307+
pgmap->type = MEMORY_DEVICE_FS_DAX;
308+
pgmap->page_free = fsdax_pagefree;
309+
310+
return 0;
311+
}
312+
292313
static int pmem_attach_disk(struct device *dev,
293314
struct nd_namespace_common *ndns)
294315
{
@@ -347,6 +368,8 @@ static int pmem_attach_disk(struct device *dev,
347368
pmem->pfn_flags = PFN_DEV;
348369
pmem->pgmap.ref = &q->q_usage_counter;
349370
if (is_nd_pfn(dev)) {
371+
if (setup_pagemap_fsdax(dev, &pmem->pgmap))
372+
return -ENOMEM;
350373
addr = devm_memremap_pages(dev, &pmem->pgmap);
351374
pfn_sb = nd_pfn->pfn_sb;
352375
pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
@@ -358,6 +381,8 @@ static int pmem_attach_disk(struct device *dev,
358381
} else if (pmem_should_map_pages(dev)) {
359382
memcpy(&pmem->pgmap.res, &nsio->res, sizeof(pmem->pgmap.res));
360383
pmem->pgmap.altmap_valid = false;
384+
if (setup_pagemap_fsdax(dev, &pmem->pgmap))
385+
return -ENOMEM;
361386
addr = devm_memremap_pages(dev, &pmem->pgmap);
362387
pmem->pfn_flags |= PFN_MAP;
363388
memcpy(&bb_res, &pmem->pgmap.res, sizeof(bb_res));

‎fs/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ config FS_DAX
3838
bool "Direct Access (DAX) support"
3939
depends on MMU
4040
depends on !(ARM || MIPS || SPARC)
41+
select DEV_PAGEMAP_OPS if (ZONE_DEVICE && !FS_DAX_LIMITED)
4142
select FS_IOMAP
4243
select DAX
4344
help

‎fs/dax.c‎

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,19 @@ static void dax_disassociate_entry(void *entry, struct address_space *mapping,
351351
}
352352
}
353353

354+
static struct page *dax_busy_page(void *entry)
355+
{
356+
unsigned long pfn;
357+
358+
for_each_mapped_pfn(entry, pfn) {
359+
struct page *page = pfn_to_page(pfn);
360+
361+
if (page_ref_count(page) > 1)
362+
return page;
363+
}
364+
return NULL;
365+
}
366+
354367
/*
355368
* Find radix tree entry at given index. If it points to an exceptional entry,
356369
* return it with the radix tree entry locked. If the radix tree doesn't
@@ -492,6 +505,90 @@ static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
492505
return entry;
493506
}
494507

508+
/**
509+
* dax_layout_busy_page - find first pinned page in @mapping
510+
* @mapping: address space to scan for a page with ref count > 1
511+
*
512+
* DAX requires ZONE_DEVICE mapped pages. These pages are never
513+
* 'onlined' to the page allocator so they are considered idle when
514+
* page->count == 1. A filesystem uses this interface to determine if
515+
* any page in the mapping is busy, i.e. for DMA, or other
516+
* get_user_pages() usages.
517+
*
518+
* It is expected that the filesystem is holding locks to block the
519+
* establishment of new mappings in this address_space. I.e. it expects
520+
* to be able to run unmap_mapping_range() and subsequently not race
521+
* mapping_mapped() becoming true.
522+
*/
523+
struct page *dax_layout_busy_page(struct address_space *mapping)
524+
{
525+
pgoff_t indices[PAGEVEC_SIZE];
526+
struct page *page = NULL;
527+
struct pagevec pvec;
528+
pgoff_t index, end;
529+
unsigned i;
530+
531+
/*
532+
* In the 'limited' case get_user_pages() for dax is disabled.
533+
*/
534+
if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
535+
return NULL;
536+
537+
if (!dax_mapping(mapping) || !mapping_mapped(mapping))
538+
return NULL;
539+
540+
pagevec_init(&pvec);
541+
index = 0;
542+
end = -1;
543+
544+
/*
545+
* If we race get_user_pages_fast() here either we'll see the
546+
* elevated page count in the pagevec_lookup and wait, or
547+
* get_user_pages_fast() will see that the page it took a reference
548+
* against is no longer mapped in the page tables and bail to the
549+
* get_user_pages() slow path. The slow path is protected by
550+
* pte_lock() and pmd_lock(). New references are not taken without
551+
* holding those locks, and unmap_mapping_range() will not zero the
552+
* pte or pmd without holding the respective lock, so we are
553+
* guaranteed to either see new references or prevent new
554+
* references from being established.
555+
*/
556+
unmap_mapping_range(mapping, 0, 0, 1);
557+
558+
while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
559+
min(end - index, (pgoff_t)PAGEVEC_SIZE),
560+
indices)) {
561+
for (i = 0; i < pagevec_count(&pvec); i++) {
562+
struct page *pvec_ent = pvec.pages[i];
563+
void *entry;
564+
565+
index = indices[i];
566+
if (index >= end)
567+
break;
568+
569+
if (!radix_tree_exceptional_entry(pvec_ent))
570+
continue;
571+
572+
xa_lock_irq(&mapping->i_pages);
573+
entry = get_unlocked_mapping_entry(mapping, index, NULL);
574+
if (entry)
575+
page = dax_busy_page(entry);
576+
put_unlocked_mapping_entry(mapping, index, entry);
577+
xa_unlock_irq(&mapping->i_pages);
578+
if (page)
579+
break;
580+
}
581+
pagevec_remove_exceptionals(&pvec);
582+
pagevec_release(&pvec);
583+
index++;
584+
585+
if (page)
586+
break;
587+
}
588+
return page;
589+
}
590+
EXPORT_SYMBOL_GPL(dax_layout_busy_page);
591+
495592
static int __dax_invalidate_mapping_entry(struct address_space *mapping,
496593
pgoff_t index, bool trunc)
497594
{
@@ -912,7 +1009,6 @@ static int dax_load_hole(struct address_space *mapping, void *entry,
9121009
unsigned long vaddr = vmf->address;
9131010
int ret = VM_FAULT_NOPAGE;
9141011
struct page *zero_page;
915-
void *entry2;
9161012
pfn_t pfn;
9171013

9181014
zero_page = ZERO_PAGE(0);
@@ -922,13 +1018,8 @@ static int dax_load_hole(struct address_space *mapping, void *entry,
9221018
}
9231019

9241020
pfn = page_to_pfn_t(zero_page);
925-
entry2 = dax_insert_mapping_entry(mapping, vmf, entry, pfn,
926-
RADIX_DAX_ZERO_PAGE, false);
927-
if (IS_ERR(entry2)) {
928-
ret = VM_FAULT_SIGBUS;
929-
goto out;
930-
}
931-
1021+
dax_insert_mapping_entry(mapping, vmf, entry, pfn, RADIX_DAX_ZERO_PAGE,
1022+
false);
9321023
vm_insert_mixed(vmf->vma, vaddr, pfn);
9331024
out:
9341025
trace_dax_load_hole(inode, vmf, ret);
@@ -1240,10 +1331,6 @@ static int dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
12401331

12411332
entry = dax_insert_mapping_entry(mapping, vmf, entry, pfn,
12421333
0, write && !sync);
1243-
if (IS_ERR(entry)) {
1244-
error = PTR_ERR(entry);
1245-
goto error_finish_iomap;
1246-
}
12471334

12481335
/*
12491336
* If we are doing synchronous page fault and inode needs fsync,
@@ -1327,8 +1414,6 @@ static int dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
13271414
pfn = page_to_pfn_t(zero_page);
13281415
ret = dax_insert_mapping_entry(mapping, vmf, entry, pfn,
13291416
RADIX_DAX_PMD | RADIX_DAX_ZERO_PAGE, false);
1330-
if (IS_ERR(ret))
1331-
goto fallback;
13321417

13331418
ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
13341419
if (!pmd_none(*(vmf->pmd))) {
@@ -1450,8 +1535,6 @@ static int dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
14501535

14511536
entry = dax_insert_mapping_entry(mapping, vmf, entry, pfn,
14521537
RADIX_DAX_PMD, write && !sync);
1453-
if (IS_ERR(entry))
1454-
goto finish_iomap;
14551538

14561539
/*
14571540
* If we are doing synchronous page fault and inode needs fsync,

‎fs/xfs/xfs_file.c‎

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ xfs_file_aio_write_checks(
312312
if (error <= 0)
313313
return error;
314314

315-
error = xfs_break_layouts(inode, iolock);
315+
error = xfs_break_layouts(inode, iolock, BREAK_WRITE);
316316
if (error)
317317
return error;
318318

@@ -718,6 +718,69 @@ xfs_file_write_iter(
718718
return ret;
719719
}
720720

721+
static void
722+
xfs_wait_dax_page(
723+
struct inode *inode,
724+
bool *did_unlock)
725+
{
726+
struct xfs_inode *ip = XFS_I(inode);
727+
728+
*did_unlock = true;
729+
xfs_iunlock(ip, XFS_MMAPLOCK_EXCL);
730+
schedule();
731+
xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
732+
}
733+
734+
static int
735+
xfs_break_dax_layouts(
736+
struct inode *inode,
737+
uint iolock,
738+
bool *did_unlock)
739+
{
740+
struct page *page;
741+
742+
ASSERT(xfs_isilocked(XFS_I(inode), XFS_MMAPLOCK_EXCL));
743+
744+
page = dax_layout_busy_page(inode->i_mapping);
745+
if (!page)
746+
return 0;
747+
748+
return ___wait_var_event(&page->_refcount,
749+
atomic_read(&page->_refcount) == 1, TASK_INTERRUPTIBLE,
750+
0, 0, xfs_wait_dax_page(inode, did_unlock));
751+
}
752+
753+
int
754+
xfs_break_layouts(
755+
struct inode *inode,
756+
uint *iolock,
757+
enum layout_break_reason reason)
758+
{
759+
bool retry;
760+
int error;
761+
762+
ASSERT(xfs_isilocked(XFS_I(inode), XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL));
763+
764+
do {
765+
retry = false;
766+
switch (reason) {
767+
case BREAK_UNMAP:
768+
error = xfs_break_dax_layouts(inode, *iolock, &retry);
769+
if (error || retry)
770+
break;
771+
/* fall through */
772+
case BREAK_WRITE:
773+
error = xfs_break_leased_layouts(inode, iolock, &retry);
774+
break;
775+
default:
776+
WARN_ON_ONCE(1);
777+
error = -EINVAL;
778+
}
779+
} while (error == 0 && retry);
780+
781+
return error;
782+
}
783+
721784
#define XFS_FALLOC_FL_SUPPORTED \
722785
(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
723786
FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \
@@ -734,7 +797,7 @@ xfs_file_fallocate(
734797
struct xfs_inode *ip = XFS_I(inode);
735798
long error;
736799
enum xfs_prealloc_flags flags = 0;
737-
uint iolock = XFS_IOLOCK_EXCL;
800+
uint iolock = XFS_IOLOCK_EXCL | XFS_MMAPLOCK_EXCL;
738801
loff_t new_size = 0;
739802
bool do_file_insert = false;
740803

@@ -744,13 +807,10 @@ xfs_file_fallocate(
744807
return -EOPNOTSUPP;
745808

746809
xfs_ilock(ip, iolock);
747-
error = xfs_break_layouts(inode, &iolock);
810+
error = xfs_break_layouts(inode, &iolock, BREAK_UNMAP);
748811
if (error)
749812
goto out_unlock;
750813

751-
xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
752-
iolock |= XFS_MMAPLOCK_EXCL;
753-
754814
if (mode & FALLOC_FL_PUNCH_HOLE) {
755815
error = xfs_free_file_space(ip, offset, len);
756816
if (error)

‎fs/xfs/xfs_inode.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,20 @@ static inline void xfs_ifunlock(struct xfs_inode *ip)
378378
#define XFS_ILOCK_DEP(flags) (((flags) & XFS_ILOCK_DEP_MASK) \
379379
>> XFS_ILOCK_SHIFT)
380380

381+
/*
382+
* Layouts are broken in the BREAK_WRITE case to ensure that
383+
* layout-holders do not collide with local writes. Additionally,
384+
* layouts are broken in the BREAK_UNMAP case to make sure the
385+
* layout-holder has a consistent view of the file's extent map. While
386+
* BREAK_WRITE breaks can be satisfied by recalling FL_LAYOUT leases,
387+
* BREAK_UNMAP breaks additionally require waiting for busy dax-pages to
388+
* go idle.
389+
*/
390+
enum layout_break_reason {
391+
BREAK_WRITE,
392+
BREAK_UNMAP,
393+
};
394+
381395
/*
382396
* For multiple groups support: if S_ISGID bit is set in the parent
383397
* directory, group of new file is set to that of the parent, and
@@ -443,6 +457,8 @@ enum xfs_prealloc_flags {
443457

444458
int xfs_update_prealloc_flags(struct xfs_inode *ip,
445459
enum xfs_prealloc_flags flags);
460+
int xfs_break_layouts(struct inode *inode, uint *iolock,
461+
enum layout_break_reason reason);
446462

447463
/* from xfs_iops.c */
448464
extern void xfs_setup_inode(struct xfs_inode *ip);

0 commit comments

Comments
 (0)