Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp4424671ybv; Tue, 25 Feb 2020 20:00:00 -0800 (PST) X-Google-Smtp-Source: APXvYqy4Z+VDUOWBtP+YLckqh2T0IXilCUakXFKKLuA6MqydAXGFIgBoFYjmNma7JU9z05Yl1hW5 X-Received: by 2002:a9d:58cb:: with SMTP id s11mr1476611oth.55.1582689600601; Tue, 25 Feb 2020 20:00:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582689600; cv=none; d=google.com; s=arc-20160816; b=TZbRL5CFsnSh3QYqxej4AHVe8uW16U+jyptudiEW7APrIGMMLm1jMQyY1HuGJMb3W2 n+UpJ+83oUhyJ6EX+qz5JE72F9KuBr3uezkqxhuFiIeaT7r5NW8K43YpNtW3aFy1hpay Zk2QcdLSSjieSjCh6nRKmhf4E8hAlBNn2p1EA8qtksIXMC5tSWt3MfBQ/aic5h09RwY8 nWE7loDyEJQFW5yuiCRXiBRYhNePU9pEaUva6oBXl95+mAaW0s7Ih1kAO/od1Iw9Cw4H oBQoNkjYbQg+ObE7ggG5rvi2tq64skFRPVQIfH6ZTjq2PaLdAmSLjxRwWOxhIEaPo6B9 QQQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=rqQmLpDggmkX79HmGpvNiduhmKU3MFQ/yb6zXVa9mHE=; b=LLDnqGBXuTTGqOFXzostM35rAPZGJPJnpEfY21C62ue2LbtjfOez/rHwTuNmdtNrlT h9m3+celxCUvTq8K/IOftWioeRNEaulVE+HHCHgTogGFx8/DKwNy44huLY5FWxkmwSkO RouAlQvZNILYbxnZXRecKk0569OVc3T6NLhvC70Zpy393lGIbTShbKtX0kkVSCmej9w3 USlzb94GqxfJOrUQai26Qw1pUARLAWfrxHrk1mvufKrpXcz5XgTEvHP7BI3BOxUtVpbL B5L10VMGPH+v2zzeZGp86Gh8oqvqXdMccaYBHUpsLZ0E2w0aHCCDK4PbXDHOeqEhtKS2 BVRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lca.pw header.s=google header.b=rT6F5zYE; 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 p191si443047oic.257.2020.02.25.19.59.48; Tue, 25 Feb 2020 20:00:00 -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=rT6F5zYE; 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 S1726592AbgBZD6g (ORCPT + 99 others); Tue, 25 Feb 2020 22:58:36 -0500 Received: from mail-qk1-f193.google.com ([209.85.222.193]:34568 "EHLO mail-qk1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725788AbgBZD6g (ORCPT ); Tue, 25 Feb 2020 22:58:36 -0500 Received: by mail-qk1-f193.google.com with SMTP id 11so1434581qkd.1 for ; Tue, 25 Feb 2020 19:58: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:mime-version :content-transfer-encoding; bh=rqQmLpDggmkX79HmGpvNiduhmKU3MFQ/yb6zXVa9mHE=; b=rT6F5zYEOmpDgUzrFDBCMZAaOJhoSxf4o8I4SGInJOOYeGvF761sKRyRCkPu2qxIYM eGIPJMS32c/dSXBGgxX/JnwJSkz59s0zlk3v4G5UjTN2IrOm9RKYv2tCS8fx7ShZ2PFe L6Dzyxqv4esV8AzjCiLSysQ0SM0vtS5CubXECAaSwcoQzrGKMtfgKNqG0t0YTHPFYJDe fjm0lKvXiZNnXN4svOOvoO/IBMc3adHjPaIoraXeIvsTIUVGM5nH8/Bxm2TOENPOzgpX 7it3V/PlJ1yfTBJstjO3O0JbV8DD6Q7FoU20D0v2Lm4yGuk//WeX3mV5kVvNU6k9ozoZ ar7g== 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:mime-version :content-transfer-encoding; bh=rqQmLpDggmkX79HmGpvNiduhmKU3MFQ/yb6zXVa9mHE=; b=lZ5LUmtQFfpuB0lWS1/joel22LijJP3AdjEs8D33LqnoYtddxva5iIeG3elTqtaIdS /+MoKRpS0GnuzClJN+fYPHrrBbr/ebIqu5b+Q89nyvvsjoUsMVbfs8J9+/dGpsw5Ts7W ncxPTopGpUBpJMJZpIwWe8fDK1liJz9ySQRp+b/62Nd/QM+/E+AwMfWXmrQsn1DapCL9 oM1EiIyd+0vG4c1POI3sYNvX3sbLhA56ZxdjU/PTHertkETsnVXItloOHvjrDSOx9HOp yQ/FDFQ/w4GbDyX+TEmiDeu4MIM6Jaj82CovYk9ddZgIYcGa8dss5OlqfyGn56EuJ+E3 ZQ4Q== X-Gm-Message-State: APjAAAW6NJmsqaqLolH3Vas5Zn41+sOusnod5jPER4q1dj+AMlM0rGkA mS6M4/8HNw/Ay4jhJySIL86fCA== X-Received: by 2002:a37:9b91:: with SMTP id d139mr2993478qke.366.1582689515562; Tue, 25 Feb 2020 19:58:35 -0800 (PST) Received: from ovpn-121-122.rdu2.redhat.com (pool-71-184-117-43.bstnma.fios.verizon.net. [71.184.117.43]) by smtp.gmail.com with ESMTPSA id m17sm401595qki.128.2020.02.25.19.58.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Feb 2020 19:58:35 -0800 (PST) From: Qian Cai To: akpm@linux-foundation.org Cc: elver@google.com, willy@infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Qian Cai Subject: [PATCH v2] mm/vmscan: fix data races at kswapd_classzone_idx Date: Tue, 25 Feb 2020 22:58:27 -0500 Message-Id: <20200226035827.1285-1-cai@lca.pw> X-Mailer: git-send-email 2.21.0 (Apple Git-122.2) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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). While at it, also take care of pgdat->kswapd_order and non-kswapd threads in allow_direct_reclaim(). 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 --- v2: use a temp variable and take care of kswapd_order per Matthew. take care of allow_direct_reclaim() as well. mm/vmscan.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/mm/vmscan.c b/mm/vmscan.c index 876370565455..e61cc71b8915 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -3136,8 +3136,9 @@ static bool allow_direct_reclaim(pg_data_t *pgdat) /* kswapd must be awake if processes are being throttled */ if (!wmark_ok && waitqueue_active(&pgdat->kswapd_wait)) { - pgdat->kswapd_classzone_idx = min(pgdat->kswapd_classzone_idx, - (enum zone_type)ZONE_NORMAL); + if (READ_ONCE(pgdat->kswapd_classzone_idx) > ZONE_NORMAL) + WRITE_ONCE(pgdat->kswapd_classzone_idx, ZONE_NORMAL); + wake_up_interruptible(&pgdat->kswapd_wait); } @@ -3953,20 +3954,23 @@ void wakeup_kswapd(struct zone *zone, gfp_t gfp_flags, int order, enum zone_type classzone_idx) { pg_data_t *pgdat; + enum zone_type curr_idx; if (!managed_zone(zone)) return; if (!cpuset_zone_allowed(zone, gfp_flags)) return; + pgdat = zone->zone_pgdat; + curr_idx = READ_ONCE(pgdat->kswapd_classzone_idx); + + if (curr_idx == MAX_NR_ZONES || curr_idx < classzone_idx) + WRITE_ONCE(pgdat->kswapd_classzone_idx, classzone_idx); + + if (READ_ONCE(pgdat->kswapd_order) < order) + WRITE_ONCE(pgdat->kswapd_order, order); - 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); - pgdat->kswapd_order = max(pgdat->kswapd_order, order); if (!waitqueue_active(&pgdat->kswapd_wait)) return; -- 2.21.0 (Apple Git-122.2)