Commit 6dbaf22c authored by Johannes Weiner's avatar Johannes Weiner Committed by Linus Torvalds

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: default avatarJohannes Weiner <>
Reviewed-by: default avatarMinchan Kim <>
Reviewed-by: default avatarRik van Riel <>
Acked-by: default avatarMel Gorman <>
Cc: Andrea Arcangeli <>
Cc: Bob Liu <>
Cc: Christoph Hellwig <>
Cc: Dave Chinner <>
Cc: Greg Thelen <>
Cc: Hugh Dickins <>
Cc: Jan Kara <>
Cc: KOSAKI Motohiro <>
Cc: Luigi Semenzato <>
Cc: Metin Doslu <>
Cc: Michel Lespinasse <>
Cc: Ozgun Erdogan <>
Cc: Peter Zijlstra <>
Cc: Roman Gushchin <>
Cc: Ryan Mallon <>
Cc: Tejun Heo <>
Cc: Vlastimil Babka <>
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarLinus Torvalds <>
parent 53c59f26
......@@ -242,19 +242,17 @@ static int shmem_radix_tree_replace(struct address_space *mapping,
pgoff_t index, void *expected, void *replacement)
void **pslot;
void *item = NULL;
void *item;
pslot = radix_tree_lookup_slot(&mapping->page_tree, index);
if (pslot)
item = radix_tree_deref_slot_protected(pslot,
if (!pslot)
return -ENOENT;
item = radix_tree_deref_slot_protected(pslot, &mapping->tree_lock);
if (item != expected)
return -ENOENT;
if (replacement)
radix_tree_replace_slot(pslot, replacement);
radix_tree_delete(&mapping->page_tree, index);
radix_tree_replace_slot(pslot, replacement);
return 0;
......@@ -386,14 +384,15 @@ export:
static int shmem_free_swap(struct address_space *mapping,
pgoff_t index, void *radswap)
int error;
void *old;
error = shmem_radix_tree_replace(mapping, index, radswap, NULL);
old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
if (!error)
return error;
if (old != radswap)
return -ENOENT;
return 0;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment