Commit b50da6e9 authored by Zhaoyang Huang's avatar Zhaoyang Huang Committed by Linus Torvalds

mm: fix a race on nr_swap_pages

The scenario on which "Free swap = -4kB" happens in my system, which is caused
by several get_swap_pages racing with each other and show_swap_cache_info
happens simutaniously. No need to add a lock on get_swap_page_of_type as we
remove "Presub/PosAdd" here.

ProcessA			ProcessB			ProcessC
ngoals = 1			ngoals = 1
avail = nr_swap_pages(1)	avail = nr_swap_pages(1)
nr_swap_pages(1) -= ngoals
				nr_swap_pages(0) -= ngoals
								nr_swap_pages = -1

Link: https://lkml.kernel.org/r/1607050340-4535-1-git-send-email-zhaoyang.huang@unisoc.comSigned-off-by: default avatarZhaoyang Huang <zhaoyang.huang@unisoc.com>
Acked-by: default avatarVlastimil Babka <vbabka@suse.cz>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 489e9fea
...@@ -1042,16 +1042,18 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size) ...@@ -1042,16 +1042,18 @@ int get_swap_pages(int n_goal, swp_entry_t swp_entries[], int entry_size)
/* Only single cluster request supported */ /* Only single cluster request supported */
WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER); WARN_ON_ONCE(n_goal > 1 && size == SWAPFILE_CLUSTER);
spin_lock(&swap_avail_lock);
avail_pgs = atomic_long_read(&nr_swap_pages) / size; avail_pgs = atomic_long_read(&nr_swap_pages) / size;
if (avail_pgs <= 0) if (avail_pgs <= 0) {
spin_unlock(&swap_avail_lock);
goto noswap; goto noswap;
}
n_goal = min3((long)n_goal, (long)SWAP_BATCH, avail_pgs); n_goal = min3((long)n_goal, (long)SWAP_BATCH, avail_pgs);
atomic_long_sub(n_goal * size, &nr_swap_pages); atomic_long_sub(n_goal * size, &nr_swap_pages);
spin_lock(&swap_avail_lock);
start_over: start_over:
node = numa_node_id(); node = numa_node_id();
plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) { plist_for_each_entry_safe(si, next, &swap_avail_heads[node], avail_lists[node]) {
...@@ -1125,14 +1127,13 @@ swp_entry_t get_swap_page_of_type(int type) ...@@ -1125,14 +1127,13 @@ swp_entry_t get_swap_page_of_type(int type)
spin_lock(&si->lock); spin_lock(&si->lock);
if (si->flags & SWP_WRITEOK) { if (si->flags & SWP_WRITEOK) {
atomic_long_dec(&nr_swap_pages);
/* This is called for allocating swap entry, not cache */ /* This is called for allocating swap entry, not cache */
offset = scan_swap_map(si, 1); offset = scan_swap_map(si, 1);
if (offset) { if (offset) {
atomic_long_dec(&nr_swap_pages);
spin_unlock(&si->lock); spin_unlock(&si->lock);
return swp_entry(type, offset); return swp_entry(type, offset);
} }
atomic_long_inc(&nr_swap_pages);
} }
spin_unlock(&si->lock); spin_unlock(&si->lock);
fail: fail:
......
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