Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm/swap_slots.c: fix race conditions in swap_slots cache init

Memory allocations can happen before the swap_slots cache initialization
is completed during cpu bring up. If we are low on memory, we could
call get_swap_page() and access swap_slots_cache before it is fully
initialized.

Add a check in get_swap_page() for initialized swap_slots_cache to
prevent this condition. Similar check already exists in free_swap_slot.
Also annotate the checks to indicate the likely condition.

We also added a memory barrier to make sure that the locks
initialization are done before the assignment of cache->slots and
cache->slots_ret pointers. This ensures the assumption that it is safe
to acquire the slots cache locks and use the slots cache when the
corresponding cache->slots or cache->slots_ret pointers are non null.

[akpm@linux-foundation.org: tidy up comment]
[akpm@linux-foundation.org: fix spello in comment]
Link: http://lkml.kernel.org/r/65a9d0f133f63e66bba37b53b2fd0464b7cae771.1500677066.git.tim.c.chen@linux.intel.com
Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com>
Reported-by: Wenwei Tao <wenwei.tww@alibaba-inc.com>
Acked-by: Ying Huang <ying.huang@intel.com>
Cc: Minchan Kim <minchan@kernel.org>
Cc: Rik van Riel <riel@redhat.com>
Cc: Andrea Arcangeli <aarcange@redhat.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Michal Hocko <mhocko@kernel.org>
Cc: Hillf Danton <hdanton@sina.com>
Cc: Hugh Dickins <hughd@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Tim Chen and committed by
Linus Torvalds
a2e16731 3a50d14d

+9 -2
+9 -2
mm/swap_slots.c
··· 149 149 cache->nr = 0; 150 150 cache->cur = 0; 151 151 cache->n_ret = 0; 152 + /* 153 + * We initialized alloc_lock and free_lock earlier. We use 154 + * !cache->slots or !cache->slots_ret to know if it is safe to acquire 155 + * the corresponding lock and use the cache. Memory barrier below 156 + * ensures the assumption. 157 + */ 158 + mb(); 152 159 cache->slots = slots; 153 160 slots = NULL; 154 161 cache->slots_ret = slots_ret; ··· 282 275 struct swap_slots_cache *cache; 283 276 284 277 cache = raw_cpu_ptr(&swp_slots); 285 - if (use_swap_slot_cache && cache->slots_ret) { 278 + if (likely(use_swap_slot_cache && cache->slots_ret)) { 286 279 spin_lock_irq(&cache->free_lock); 287 280 /* Swap slots cache may be deactivated before acquiring lock */ 288 281 if (!use_swap_slot_cache || !cache->slots_ret) { ··· 333 326 */ 334 327 cache = raw_cpu_ptr(&swp_slots); 335 328 336 - if (check_cache_active()) { 329 + if (likely(check_cache_active() && cache->slots)) { 337 330 mutex_lock(&cache->alloc_lock); 338 331 if (cache->slots) { 339 332 repeat: