Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3897194ybv; Tue, 25 Feb 2020 09:14:05 -0800 (PST) X-Google-Smtp-Source: APXvYqziC1YTmzkT1/0n9+ZwXnOWZZ67/GKGx6G6Y8aZgnigEzQu6hPGsXPmdShVFjesf2COqNqh X-Received: by 2002:a05:6830:c9:: with SMTP id x9mr45738290oto.345.1582650845559; Tue, 25 Feb 2020 09:14:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582650845; cv=none; d=google.com; s=arc-20160816; b=a0XTmrADEWYFTDvOb/2REC4IYYh7U5OlhzfM1DeDcym+v2gSsKiouEB2Ipejgh13XL 1xovQ+Hbocmq6shnBY6ZyU6MkfADyuxMQEPQ61Tgul7TRDhthtVJVoQCZISuS6MvCj8i gVs2i91DBMPpD9+0Y+DToe6z0wffTEavLm9NGZWeNIu8lB+XQVw4jSfkvG9q37raI/ng UqWlBrYd2mV9xWgAvgzYuD8BqZPHPFhaHe+Jnmq/fOZ6LQ6aClC0KUY6AKMx26YgeDUS Xd3uTHzrEqy2pbyqGksbLtqFeWmqxmnK0YZM/+8PtXFtqOz2+N7G6+5F7hETUtVpj75q 13Ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature; bh=eI480rA4BDfxWyGgJwW8aPuYxCTO0HnuLE75vGul/Xs=; b=WZ4dykE40Sounualhfk20lfrQCwrAS+oeXbnvPyr112+9p8DkBAtNv65BUQkHIcRqs B0j6Hm0Kb9iBTFjmp6xINSubJLLlANgLS1Ev0U1vmP73OAVWZSCUa/HtBDIZufIyGOb8 kCPLaMSXE9Hdpm/j3PJWOcEd3djmE0Lhxkgm1Ivzu2iwogpe47GnNiOQLtUyhsQ7UCyy bpXlBcN7tjfhwxfq32zYcPHsi3XEsXs3wC+ClGfGNASQWzxbb818pp+gWafKEY83mDVC QrC7n2O4ChZhz9BuOEAGmRUeqPr82EGyM8N0Z20LhO+woHfWHlEj5fUiOejs+6nnRCpz dmSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=P184IGeA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o7si8692216otk.185.2020.02.25.09.13.53; Tue, 25 Feb 2020 09:14:05 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=P184IGeA; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730900AbgBYQzh (ORCPT + 99 others); Tue, 25 Feb 2020 11:55:37 -0500 Received: from mail-qt1-f194.google.com ([209.85.160.194]:37473 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727983AbgBYQzg (ORCPT ); Tue, 25 Feb 2020 11:55:36 -0500 Received: by mail-qt1-f194.google.com with SMTP id j34so130376qtk.4 for ; Tue, 25 Feb 2020 08:55:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=lca.pw; s=google; h=from:to:cc:subject:date:message-id; bh=eI480rA4BDfxWyGgJwW8aPuYxCTO0HnuLE75vGul/Xs=; b=P184IGeAqAXRM6R0OEhOJISQRtMWwb9pZ+9c1oAjp2YYR6Dp+uD6ARuWbBtHGOZr2J vK6M4D6AzZC47x9J9h3G0MuW3tXDh+CWptXiLd+VP81+pxlCGywMqrhqyhm9rPX9LImG mNZaRu+T1/sHzEsctNZT4TQZ34JL0gnGDh60xKlsJpFyfyvfVU1n/MqSHFQdDVCOV8lc UNFv8f7ymvtvvvb101XFzxusbA2Oed3oEMx+3QjgkoJJeL5yyx7fpX6DCmCoYicsBAEK IEjLhNX56qFDHYUOi43NZuecSd1k0uDwfEaebDtDS06hVuWSB66CVTtRZhS/44GYjC9u /FcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=eI480rA4BDfxWyGgJwW8aPuYxCTO0HnuLE75vGul/Xs=; b=HeHIlK4VTJwWlX9VXW2BOcSyZsy2dSdEGFstcjSWUir/6mqK6JvAHlfHdpZBRgcvzF sBb/e7MVHeuaoDbU5zD6Q5Wh/xbCiSxJoULAlGTOs1nh3JpQwj+MdmJu4rD7PvMZ2nbi FSsZC02iVEHWOEiTw90VkQtLmqT1fwGY5AlanUq+S6zintJGvb+1LzkEK4M3dFWqf+6u zuZrzOWekUIwkqcMcn6etH8qJpOIK3OqWr5phDG58SJy45V1u9lYPUHBWhZq2IRqslQc 4ag7Vf0zOaohljLIKFokEE8A3tNQ5//P8ATedqZco6PugfKLpwGvBK5q0Gg9Kc8YePub oTMA== X-Gm-Message-State: APjAAAWSxh7EXY43WcFx+rN0moaNpbHxEtFftsaH6aNTq9iEaEZE4nll xn0R+aUwuCJF2rfcsHTlBY1prA== X-Received: by 2002:ac8:6046:: with SMTP id k6mr56995736qtm.357.1582649735740; Tue, 25 Feb 2020 08:55:35 -0800 (PST) Received: from qcai.nay.com (nat-pool-bos-t.redhat.com. [66.187.233.206]) by smtp.gmail.com with ESMTPSA id 131sm2743238qkl.86.2020.02.25.08.55.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Feb 2020 08:55:35 -0800 (PST) From: Qian Cai To: akpm@linux-foundation.org Cc: elver@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qian Cai Subject: [PATCH] mm/vmscan: fix data races at kswapd_classzone_idx Date: Tue, 25 Feb 2020 11:55:26 -0500 Message-Id: <1582649726-15474-1-git-send-email-cai@lca.pw> X-Mailer: git-send-email 1.8.3.1 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org pgdat->kswapd_classzone_idx could be accessed concurrently in wakeup_kswapd(). Plain writes and reads without any lock protection result in data races. Fix them by adding a pair of READ|WRITE_ONCE() as well as saving a branch (compilers might well optimize the original code in an unintentional way anyway). The data races were reported by KCSAN, BUG: KCSAN: data-race in wakeup_kswapd / wakeup_kswapd write to 0xffff9f427ffff2dc of 4 bytes by task 7454 on cpu 13: wakeup_kswapd+0xf1/0x400 wakeup_kswapd at mm/vmscan.c:3967 wake_all_kswapds+0x59/0xc0 wake_all_kswapds at mm/page_alloc.c:4241 __alloc_pages_slowpath+0xdcc/0x1290 __alloc_pages_slowpath at mm/page_alloc.c:4512 __alloc_pages_nodemask+0x3bb/0x450 alloc_pages_vma+0x8a/0x2c0 do_anonymous_page+0x16e/0x6f0 __handle_mm_fault+0xcd5/0xd40 handle_mm_fault+0xfc/0x2f0 do_page_fault+0x263/0x6f9 page_fault+0x34/0x40 1 lock held by mtest01/7454: #0: ffff9f425afe8808 (&mm->mmap_sem#2){++++}, at: do_page_fault+0x143/0x6f9 do_user_addr_fault at arch/x86/mm/fault.c:1405 (inlined by) do_page_fault at arch/x86/mm/fault.c:1539 irq event stamp: 6944085 count_memcg_event_mm+0x1a6/0x270 count_memcg_event_mm+0x119/0x270 __do_softirq+0x34c/0x57c irq_exit+0xa2/0xc0 read to 0xffff9f427ffff2dc of 4 bytes by task 7472 on cpu 38: wakeup_kswapd+0xc8/0x400 wake_all_kswapds+0x59/0xc0 __alloc_pages_slowpath+0xdcc/0x1290 __alloc_pages_nodemask+0x3bb/0x450 alloc_pages_vma+0x8a/0x2c0 do_anonymous_page+0x16e/0x6f0 __handle_mm_fault+0xcd5/0xd40 handle_mm_fault+0xfc/0x2f0 do_page_fault+0x263/0x6f9 page_fault+0x34/0x40 1 lock held by mtest01/7472: #0: ffff9f425a9ac148 (&mm->mmap_sem#2){++++}, at: do_page_fault+0x143/0x6f9 irq event stamp: 6793561 count_memcg_event_mm+0x1a6/0x270 count_memcg_event_mm+0x119/0x270 __do_softirq+0x34c/0x57c irq_exit+0xa2/0xc0 Signed-off-by: Qian Cai --- mm/vmscan.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 876370565455..400950734e5a 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3961,11 +3961,10 @@ void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, return; pgdat = zone->zone_pgdat; - if (pgdat->kswapd_classzone_idx == MAX_NR_ZONES) - pgdat->kswapd_classzone_idx = classzone_idx; - else - pgdat->kswapd_classzone_idx = max(pgdat->kswapd_classzone_idx, - classzone_idx); + if (READ_ONCE(pgdat->kswapd_classzone_idx) == MAX_NR_ZONES || + READ_ONCE(pgdat->kswapd_classzone_idx) < classzone_idx) + WRITE_ONCE(pgdat->kswapd_classzone_idx, classzone_idx); + pgdat->kswapd_order = max(pgdat->kswapd_order, order); if (!waitqueue_active(&pgdat->kswapd_wait)) return; -- 1.8.3.1