Skip to content

Commit c6dcf52

Browse files
jankaradjbw
authored andcommitted
mm: Invalidate DAX radix tree entries only if appropriate
Currently invalidate_inode_pages2_range() and invalidate_mapping_pages() just delete all exceptional radix tree entries they find. For DAX this is not desirable as we track cache dirtiness in these entries and when they are evicted, we may not flush caches although it is necessary. This can for example manifest when we write to the same block both via mmap and via write(2) (to different offsets) and fsync(2) then does not properly flush CPU caches when modification via write(2) was the last one. Create appropriate DAX functions to handle invalidation of DAX entries for invalidate_inode_pages2_range() and invalidate_mapping_pages() and wire them up into the corresponding mm functions. Acked-by: Johannes Weiner <[email protected]> Reviewed-by: Ross Zwisler <[email protected]> Signed-off-by: Jan Kara <[email protected]> Signed-off-by: Dan Williams <[email protected]>
1 parent e568df6 commit c6dcf52

File tree

3 files changed

+125
-24
lines changed

3 files changed

+125
-24
lines changed

fs/dax.c

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -451,33 +451,84 @@ void dax_wake_mapping_entry_waiter(struct address_space *mapping,
451451
__wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
452452
}
453453

454+
static int __dax_invalidate_mapping_entry(struct address_space *mapping,
455+
pgoff_t index, bool trunc)
456+
{
457+
int ret = 0;
458+
void *entry;
459+
struct radix_tree_root *page_tree = &mapping->page_tree;
460+
461+
spin_lock_irq(&mapping->tree_lock);
462+
entry = get_unlocked_mapping_entry(mapping, index, NULL);
463+
if (!entry || !radix_tree_exceptional_entry(entry))
464+
goto out;
465+
if (!trunc &&
466+
(radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
467+
radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)))
468+
goto out;
469+
radix_tree_delete(page_tree, index);
470+
mapping->nrexceptional--;
471+
ret = 1;
472+
out:
473+
put_unlocked_mapping_entry(mapping, index, entry);
474+
spin_unlock_irq(&mapping->tree_lock);
475+
return ret;
476+
}
454477
/*
455478
* Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
456479
* entry to get unlocked before deleting it.
457480
*/
458481
int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
459482
{
460-
void *entry;
483+
int ret = __dax_invalidate_mapping_entry(mapping, index, true);
461484

462-
spin_lock_irq(&mapping->tree_lock);
463-
entry = get_unlocked_mapping_entry(mapping, index, NULL);
464485
/*
465486
* This gets called from truncate / punch_hole path. As such, the caller
466487
* must hold locks protecting against concurrent modifications of the
467488
* radix tree (usually fs-private i_mmap_sem for writing). Since the
468489
* caller has seen exceptional entry for this index, we better find it
469490
* at that index as well...
470491
*/
471-
if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry))) {
472-
spin_unlock_irq(&mapping->tree_lock);
473-
return 0;
474-
}
475-
radix_tree_delete(&mapping->page_tree, index);
492+
WARN_ON_ONCE(!ret);
493+
return ret;
494+
}
495+
496+
/*
497+
* Invalidate exceptional DAX entry if easily possible. This handles DAX
498+
* entries for invalidate_inode_pages() so we evict the entry only if we can
499+
* do so without blocking.
500+
*/
501+
int dax_invalidate_mapping_entry(struct address_space *mapping, pgoff_t index)
502+
{
503+
int ret = 0;
504+
void *entry, **slot;
505+
struct radix_tree_root *page_tree = &mapping->page_tree;
506+
507+
spin_lock_irq(&mapping->tree_lock);
508+
entry = __radix_tree_lookup(page_tree, index, NULL, &slot);
509+
if (!entry || !radix_tree_exceptional_entry(entry) ||
510+
slot_locked(mapping, slot))
511+
goto out;
512+
if (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
513+
radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
514+
goto out;
515+
radix_tree_delete(page_tree, index);
476516
mapping->nrexceptional--;
517+
ret = 1;
518+
out:
477519
spin_unlock_irq(&mapping->tree_lock);
478-
dax_wake_mapping_entry_waiter(mapping, index, entry, true);
520+
if (ret)
521+
dax_wake_mapping_entry_waiter(mapping, index, entry, true);
522+
return ret;
523+
}
479524

480-
return 1;
525+
/*
526+
* Invalidate exceptional DAX entry if it is clean.
527+
*/
528+
int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
529+
pgoff_t index)
530+
{
531+
return __dax_invalidate_mapping_entry(mapping, index, false);
481532
}
482533

483534
/*

include/linux/dax.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ ssize_t dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
4141
int dax_iomap_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
4242
struct iomap_ops *ops);
4343
int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index);
44+
int dax_invalidate_mapping_entry(struct address_space *mapping, pgoff_t index);
45+
int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
46+
pgoff_t index);
4447
void dax_wake_mapping_entry_waiter(struct address_space *mapping,
4548
pgoff_t index, void *entry, bool wake_all);
4649

mm/truncate.c

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,12 @@
2424
#include <linux/rmap.h>
2525
#include "internal.h"
2626

27-
static void clear_exceptional_entry(struct address_space *mapping,
28-
pgoff_t index, void *entry)
27+
static void clear_shadow_entry(struct address_space *mapping, pgoff_t index,
28+
void *entry)
2929
{
3030
struct radix_tree_node *node;
3131
void **slot;
3232

33-
/* Handled by shmem itself */
34-
if (shmem_mapping(mapping))
35-
return;
36-
37-
if (dax_mapping(mapping)) {
38-
dax_delete_mapping_entry(mapping, index);
39-
return;
40-
}
4133
spin_lock_irq(&mapping->tree_lock);
4234
/*
4335
* Regular page slots are stabilized by the page lock even
@@ -55,6 +47,56 @@ static void clear_exceptional_entry(struct address_space *mapping,
5547
spin_unlock_irq(&mapping->tree_lock);
5648
}
5749

50+
/*
51+
* Unconditionally remove exceptional entry. Usually called from truncate path.
52+
*/
53+
static void truncate_exceptional_entry(struct address_space *mapping,
54+
pgoff_t index, void *entry)
55+
{
56+
/* Handled by shmem itself */
57+
if (shmem_mapping(mapping))
58+
return;
59+
60+
if (dax_mapping(mapping)) {
61+
dax_delete_mapping_entry(mapping, index);
62+
return;
63+
}
64+
clear_shadow_entry(mapping, index, entry);
65+
}
66+
67+
/*
68+
* Invalidate exceptional entry if easily possible. This handles exceptional
69+
* entries for invalidate_inode_pages() so for DAX it evicts only unlocked and
70+
* clean entries.
71+
*/
72+
static int invalidate_exceptional_entry(struct address_space *mapping,
73+
pgoff_t index, void *entry)
74+
{
75+
/* Handled by shmem itself */
76+
if (shmem_mapping(mapping))
77+
return 1;
78+
if (dax_mapping(mapping))
79+
return dax_invalidate_mapping_entry(mapping, index);
80+
clear_shadow_entry(mapping, index, entry);
81+
return 1;
82+
}
83+
84+
/*
85+
* Invalidate exceptional entry if clean. This handles exceptional entries for
86+
* invalidate_inode_pages2() so for DAX it evicts only clean entries.
87+
*/
88+
static int invalidate_exceptional_entry2(struct address_space *mapping,
89+
pgoff_t index, void *entry)
90+
{
91+
/* Handled by shmem itself */
92+
if (shmem_mapping(mapping))
93+
return 1;
94+
if (dax_mapping(mapping))
95+
return dax_invalidate_mapping_entry_sync(mapping, index);
96+
clear_shadow_entry(mapping, index, entry);
97+
return 1;
98+
}
99+
58100
/**
59101
* do_invalidatepage - invalidate part or all of a page
60102
* @page: the page which is affected
@@ -262,7 +304,8 @@ void truncate_inode_pages_range(struct address_space *mapping,
262304
break;
263305

264306
if (radix_tree_exceptional_entry(page)) {
265-
clear_exceptional_entry(mapping, index, page);
307+
truncate_exceptional_entry(mapping, index,
308+
page);
266309
continue;
267310
}
268311

@@ -351,7 +394,8 @@ void truncate_inode_pages_range(struct address_space *mapping,
351394
}
352395

353396
if (radix_tree_exceptional_entry(page)) {
354-
clear_exceptional_entry(mapping, index, page);
397+
truncate_exceptional_entry(mapping, index,
398+
page);
355399
continue;
356400
}
357401

@@ -470,7 +514,8 @@ unsigned long invalidate_mapping_pages(struct address_space *mapping,
470514
break;
471515

472516
if (radix_tree_exceptional_entry(page)) {
473-
clear_exceptional_entry(mapping, index, page);
517+
invalidate_exceptional_entry(mapping, index,
518+
page);
474519
continue;
475520
}
476521

@@ -592,7 +637,9 @@ int invalidate_inode_pages2_range(struct address_space *mapping,
592637
break;
593638

594639
if (radix_tree_exceptional_entry(page)) {
595-
clear_exceptional_entry(mapping, index, page);
640+
if (!invalidate_exceptional_entry2(mapping,
641+
index, page))
642+
ret = -EBUSY;
596643
continue;
597644
}
598645

0 commit comments

Comments
 (0)