Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 311838
b: refs/heads/master
c: d189922
h: refs/heads/master
v: v3
  • Loading branch information
Hugh Dickins authored and Linus Torvalds committed Jul 11, 2012
1 parent 02bf2ac commit 84345c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: f21f8062201fc6361f65de92e758a76375ba8c59
refs/heads/master: d189922862e03ce6c7adc1e99d3b94e632dc8e89
41 changes: 29 additions & 12 deletions trunk/mm/shmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ static int shmem_radix_tree_replace(struct address_space *mapping,
return 0;
}

/*
* Sometimes, before we decide whether to proceed or to fail, we must check
* that an entry was not already brought back from swap by a racing thread.
*
* Checking page is not enough: by the time a SwapCache page is locked, it
* might be reused, and again be SwapCache, using the same swap as before.
*/
static bool shmem_confirm_swap(struct address_space *mapping,
pgoff_t index, swp_entry_t swap)
{
void *item;

rcu_read_lock();
item = radix_tree_lookup(&mapping->page_tree, index);
rcu_read_unlock();
return item == swp_to_radix_entry(swap);
}

/*
* Like add_to_page_cache_locked, but error if expected item has gone.
*/
Expand Down Expand Up @@ -1124,9 +1142,9 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
/* We have to do this with page locked to prevent races */
lock_page(page);
if (!PageSwapCache(page) || page_private(page) != swap.val ||
page->mapping) {
!shmem_confirm_swap(mapping, index, swap)) {
error = -EEXIST; /* try again */
goto failed;
goto unlock;
}
if (!PageUptodate(page)) {
error = -EIO;
Expand All @@ -1142,9 +1160,12 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,

error = mem_cgroup_cache_charge(page, current->mm,
gfp & GFP_RECLAIM_MASK);
if (!error)
if (!error) {
error = shmem_add_to_page_cache(page, mapping, index,
gfp, swp_to_radix_entry(swap));
/* We already confirmed swap, and make no allocation */
VM_BUG_ON(error);
}
if (error)
goto failed;

Expand Down Expand Up @@ -1245,14 +1266,10 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
unacct:
shmem_unacct_blocks(info->flags, 1);
failed:
if (swap.val && error != -EINVAL) {
struct page *test = find_get_page(mapping, index);
if (test && !radix_tree_exceptional_entry(test))
page_cache_release(test);
/* Have another try if the entry has changed */
if (test != swp_to_radix_entry(swap))
error = -EEXIST;
}
if (swap.val && error != -EINVAL &&
!shmem_confirm_swap(mapping, index, swap))
error = -EEXIST;
unlock:
if (page) {
unlock_page(page);
page_cache_release(page);
Expand All @@ -1264,7 +1281,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
spin_unlock(&info->lock);
goto repeat;
}
if (error == -EEXIST)
if (error == -EEXIST) /* from above or from radix_tree_insert */
goto repeat;
return error;
}
Expand Down

0 comments on commit 84345c7

Please sign in to comment.