summaryrefslogtreecommitdiffstats
path: root/mm/swapfile.c
diff options
context:
space:
mode:
authorHuang Ying <ying.huang@intel.com>2017-05-03 17:52:49 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2017-05-03 18:52:08 -0400
commit322b8afe4a65906c133102532e63a278775cc5f0 (patch)
tree479940969c31f1bd766de491f62fbdceec70d066 /mm/swapfile.c
parent9a4caf1e9fa4864ce21ba9584a2c336bfbc72740 (diff)
mm, swap: Fix a race in free_swap_and_cache()
Before using cluster lock in free_swap_and_cache(), the swap_info_struct->lock will be held during freeing the swap entry and acquiring page lock, so the page swap count will not change when testing page information later. But after using cluster lock, the cluster lock (or swap_info_struct->lock) will be held only during freeing the swap entry. So before acquiring the page lock, the page swap count may be changed in another thread. If the page swap count is not 0, we should not delete the page from the swap cache. This is fixed via checking page swap count again after acquiring the page lock. I found the race when I review the code, so I didn't trigger the race via a test program. If the race occurs for an anonymous page shared by multiple processes via fork, multiple pages will be allocated and swapped in from the swap device for the previously shared one page. That is, the user-visible runtime effect is more memory will be used and the access latency for the page will be higher, that is, the performance regression. Link: http://lkml.kernel.org/r/20170301143905.12846-1-ying.huang@intel.com Signed-off-by: "Huang, Ying" <ying.huang@intel.com> Cc: Hugh Dickins <hughd@google.com> Cc: Shaohua Li <shli@kernel.org> Cc: Minchan Kim <minchan@kernel.org> Cc: Rik van Riel <riel@redhat.com> Cc: Tim Chen <tim.c.chen@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/swapfile.c')
-rw-r--r--mm/swapfile.c25
1 files changed, 16 insertions, 9 deletions
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 178130880b90..6b6bb1bb6209 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1111,6 +1111,18 @@ int page_swapcount(struct page *page)
1111 return count; 1111 return count;
1112} 1112}
1113 1113
1114static int swap_swapcount(struct swap_info_struct *si, swp_entry_t entry)
1115{
1116 int count = 0;
1117 pgoff_t offset = swp_offset(entry);
1118 struct swap_cluster_info *ci;
1119
1120 ci = lock_cluster_or_swap_info(si, offset);
1121 count = swap_count(si->swap_map[offset]);
1122 unlock_cluster_or_swap_info(si, ci);
1123 return count;
1124}
1125
1114/* 1126/*
1115 * How many references to @entry are currently swapped out? 1127 * How many references to @entry are currently swapped out?
1116 * This does not give an exact answer when swap count is continued, 1128 * This does not give an exact answer when swap count is continued,
@@ -1119,17 +1131,11 @@ int page_swapcount(struct page *page)
1119int __swp_swapcount(swp_entry_t entry) 1131int __swp_swapcount(swp_entry_t entry)
1120{ 1132{
1121 int count = 0; 1133 int count = 0;
1122 pgoff_t offset;
1123 struct swap_info_struct *si; 1134 struct swap_info_struct *si;
1124 struct swap_cluster_info *ci;
1125 1135
1126 si = __swap_info_get(entry); 1136 si = __swap_info_get(entry);
1127 if (si) { 1137 if (si)
1128 offset = swp_offset(entry); 1138 count = swap_swapcount(si, entry);
1129 ci = lock_cluster_or_swap_info(si, offset);
1130 count = swap_count(si->swap_map[offset]);
1131 unlock_cluster_or_swap_info(si, ci);
1132 }
1133 return count; 1139 return count;
1134} 1140}
1135 1141
@@ -1291,7 +1297,8 @@ int free_swap_and_cache(swp_entry_t entry)
1291 * Also recheck PageSwapCache now page is locked (above). 1297 * Also recheck PageSwapCache now page is locked (above).
1292 */ 1298 */
1293 if (PageSwapCache(page) && !PageWriteback(page) && 1299 if (PageSwapCache(page) && !PageWriteback(page) &&
1294 (!page_mapped(page) || mem_cgroup_swap_full(page))) { 1300 (!page_mapped(page) || mem_cgroup_swap_full(page)) &&
1301 !swap_swapcount(p, entry)) {
1295 delete_from_swap_cache(page); 1302 delete_from_swap_cache(page);
1296 SetPageDirty(page); 1303 SetPageDirty(page);
1297 } 1304 }