Skip to content

Commit 73444bc

Browse files
gormanmtorvalds
authored andcommitted
mm, page_alloc: do not wake kswapd with zone lock held
syzbot reported the following regression in the latest merge window and it was confirmed by Qian Cai that a similar bug was visible from a different context. ====================================================== WARNING: possible circular locking dependency detected 4.20.0+ torvalds#297 Not tainted ------------------------------------------------------ syz-executor0/8529 is trying to acquire lock: 000000005e7fb829 (&pgdat->kswapd_wait){....}, at: __wake_up_common_lock+0x19e/0x330 kernel/sched/wait.c:120 but task is already holding lock: 000000009bb7bae0 (&(&zone->lock)->rlock){-.-.}, at: spin_lock include/linux/spinlock.h:329 [inline] 000000009bb7bae0 (&(&zone->lock)->rlock){-.-.}, at: rmqueue_bulk mm/page_alloc.c:2548 [inline] 000000009bb7bae0 (&(&zone->lock)->rlock){-.-.}, at: __rmqueue_pcplist mm/page_alloc.c:3021 [inline] 000000009bb7bae0 (&(&zone->lock)->rlock){-.-.}, at: rmqueue_pcplist mm/page_alloc.c:3050 [inline] 000000009bb7bae0 (&(&zone->lock)->rlock){-.-.}, at: rmqueue mm/page_alloc.c:3072 [inline] 000000009bb7bae0 (&(&zone->lock)->rlock){-.-.}, at: get_page_from_freelist+0x1bae/0x52a0 mm/page_alloc.c:3491 It appears to be a false positive in that the only way the lock ordering should be inverted is if kswapd is waking itself and the wakeup allocates debugging objects which should already be allocated if it's kswapd doing the waking. Nevertheless, the possibility exists and so it's best to avoid the problem. This patch flags a zone as needing a kswapd using the, surprisingly, unused zone flag field. The flag is read without the lock held to do the wakeup. It's possible that the flag setting context is not the same as the flag clearing context or for small races to occur. However, each race possibility is harmless and there is no visible degredation in fragmentation treatment. While zone->flag could have continued to be unused, there is potential for moving some existing fields into the flags field instead. Particularly read-mostly ones like zone->initialized and zone->contiguous. Link: http://lkml.kernel.org/r/20190103225712.GJ31517@techsingularity.net Fixes: 1c30844 ("mm: reclaim small amounts of memory when an external fragmentation event occurs") Reported-by: syzbot+93d94a001cfbce9e60e1@syzkaller.appspotmail.com Signed-off-by: Mel Gorman <mgorman@techsingularity.net> Acked-by: Vlastimil Babka <vbabka@suse.cz> Tested-by: Qian Cai <cai@lca.pw> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Michal Hocko <mhocko@suse.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent ddeaab3 commit 73444bc

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

include/linux/mmzone.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,12 @@ enum pgdat_flags {
520520
PGDAT_RECLAIM_LOCKED, /* prevents concurrent reclaim */
521521
};
522522

523+
enum zone_flags {
524+
ZONE_BOOSTED_WATERMARK, /* zone recently boosted watermarks.
525+
* Cleared when kswapd is woken.
526+
*/
527+
};
528+
523529
static inline unsigned long zone_managed_pages(struct zone *zone)
524530
{
525531
return (unsigned long)atomic_long_read(&zone->managed_pages);

mm/page_alloc.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2214,7 +2214,7 @@ static void steal_suitable_fallback(struct zone *zone, struct page *page,
22142214
*/
22152215
boost_watermark(zone);
22162216
if (alloc_flags & ALLOC_KSWAPD)
2217-
wakeup_kswapd(zone, 0, 0, zone_idx(zone));
2217+
set_bit(ZONE_BOOSTED_WATERMARK, &zone->flags);
22182218

22192219
/* We are not allowed to try stealing from the whole block */
22202220
if (!whole_block)
@@ -3102,6 +3102,12 @@ struct page *rmqueue(struct zone *preferred_zone,
31023102
local_irq_restore(flags);
31033103

31043104
out:
3105+
/* Separate test+clear to avoid unnecessary atomics */
3106+
if (test_bit(ZONE_BOOSTED_WATERMARK, &zone->flags)) {
3107+
clear_bit(ZONE_BOOSTED_WATERMARK, &zone->flags);
3108+
wakeup_kswapd(zone, 0, 0, zone_idx(zone));
3109+
}
3110+
31053111
VM_BUG_ON_PAGE(page && bad_range(zone, page), page);
31063112
return page;
31073113

0 commit comments

Comments
 (0)