Skip to content

Commit 6dbaf22

Browse files
hnaztorvalds
authored andcommitted
mm: shmem: save one radix tree lookup when truncating swapped pages
Page cache radix tree slots are usually stabilized by the page lock, but shmem's swap cookies have no such thing. Because the overall truncation loop is lockless, the swap entry is currently confirmed by a tree lookup and then deleted by another tree lookup under the same tree lock region. Use radix_tree_delete_item() instead, which does the verification and deletion with only one lookup. This also allows removing the delete-only special case from shmem_radix_tree_replace(). Signed-off-by: Johannes Weiner <[email protected]> Reviewed-by: Minchan Kim <[email protected]> Reviewed-by: Rik van Riel <[email protected]> Acked-by: Mel Gorman <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Bob Liu <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Dave Chinner <[email protected]> Cc: Greg Thelen <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Jan Kara <[email protected]> Cc: KOSAKI Motohiro <[email protected]> Cc: Luigi Semenzato <[email protected]> Cc: Metin Doslu <[email protected]> Cc: Michel Lespinasse <[email protected]> Cc: Ozgun Erdogan <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Roman Gushchin <[email protected]> Cc: Ryan Mallon <[email protected]> Cc: Tejun Heo <[email protected]> Cc: Vlastimil Babka <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 53c59f2 commit 6dbaf22

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

mm/shmem.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,17 @@ static int shmem_radix_tree_replace(struct address_space *mapping,
242242
pgoff_t index, void *expected, void *replacement)
243243
{
244244
void **pslot;
245-
void *item = NULL;
245+
void *item;
246246

247247
VM_BUG_ON(!expected);
248+
VM_BUG_ON(!replacement);
248249
pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
249-
if (pslot)
250-
item = radix_tree_deref_slot_protected(pslot,
251-
&mapping->tree_lock);
250+
if (!pslot)
251+
return -ENOENT;
252+
item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
252253
if (item != expected)
253254
return -ENOENT;
254-
if (replacement)
255-
radix_tree_replace_slot(pslot, replacement);
256-
else
257-
radix_tree_delete(&mapping->page_tree, index);
255+
radix_tree_replace_slot(pslot, replacement);
258256
return 0;
259257
}
260258

@@ -386,14 +384,15 @@ static unsigned shmem_find_get_pages_and_swap(struct address_space *mapping,
386384
static int shmem_free_swap(struct address_space *mapping,
387385
pgoff_t index, void *radswap)
388386
{
389-
int error;
387+
void *old;
390388

391389
spin_lock_irq(&mapping->tree_lock);
392-
error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
390+
old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
393391
spin_unlock_irq(&mapping->tree_lock);
394-
if (!error)
395-
free_swap_and_cache(radix_to_swp_entry(radswap));
396-
return error;
392+
if (old != radswap)
393+
return -ENOENT;
394+
free_swap_and_cache(radix_to_swp_entry(radswap));
395+
return 0;
397396
}
398397

399398
/*

0 commit comments

Comments
 (0)