Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp4341429ybv; Tue, 25 Feb 2020 18:11:36 -0800 (PST) X-Google-Smtp-Source: APXvYqznnd9w50cLLWADGDC6RowOk/WTVjJEG/yc+vdFBJAYuFaj64mId8Av81Y5swgsF2p7FfRj X-Received: by 2002:a9d:7593:: with SMTP id s19mr1150337otk.219.1582683096638; Tue, 25 Feb 2020 18:11:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582683096; cv=none; d=google.com; s=arc-20160816; b=QVglI8HZIWA5V0ryfznOfMKVFhWsWsTx6LirwaoK8zuhHMfO6hZL8NTqBPh6hGE3zv c0Pgm2pHEYCMHWgQXZQ12k8840hjbR68Bj/y8+gBgZyTswEG89kBtFEcD7MyM5KY5I+x wM6uKZL+brxAZmNdwj7O0TPNzvcIE8CUig3fTJ8iONeeFB3HovmZhj9OG8T1sTDZdiKP RsLdTFhRkKo0WA5g4/MeFm7h0HNntONyguGrNeejOcB1a/bKlym/6BXzuSq9xo3Anqiv Dtxll60Mt+EDdFyX/1Ye7up9R8wW/y1srPTGbJaAQpG4eM8JdjDCGAjHMEXA1E7AV1Q6 mLQQ== 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 :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=5WDy3wMlzc8Rcub/QOkJaK51bDA9071qCVM48yW/hSQ=; b=Ccdb9WeArAq9V3nHrbEhHuZ/7C/GXWwikfPcmAuvyZs3HY4x0zFVh0OX5Xam4tznQv oSan+uLakAnDs8G8naKgpO7r1+PSTuXOpgGUJPcQ4bDr4LrzVE9b0/YCwMeUSa5Y6Rou 0aYDKSgHsJLrdpi1ujPOzMeVQNmHzCILUWuaEcMIFrTaW1/PRftBWl2lZlEC3jNbLTCf 55bSntHvNoat2KRqxCZLtYJzUX2wFCreTZ0pwTlOvxS2JBeAi17fVmzwA10UFese1ZIR y20p+2tBD7Wc0Teaz8Q/vWMWmkYGLSWJjqdemMQa74bNeM+4CHAFMxyDyTcMNGWLnyFh BliA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KMWf8lCx; 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 a13si409565otq.201.2020.02.25.18.11.24; Tue, 25 Feb 2020 18:11:36 -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=@kernel.org header.s=default header.b=KMWf8lCx; 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 S1729504AbgBZCLE (ORCPT + 99 others); Tue, 25 Feb 2020 21:11:04 -0500 Received: from mail.kernel.org ([198.145.29.99]:39998 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727809AbgBZCLD (ORCPT ); Tue, 25 Feb 2020 21:11:03 -0500 Received: from localhost.localdomain (c-73-231-172-41.hsd1.ca.comcast.net [73.231.172.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 7A4CE21744; Wed, 26 Feb 2020 02:11:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1582683061; bh=ZEaedp/t+OQSF3UOsAPhZYZgIEc+LWUwL3y+/EEtQhA=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=KMWf8lCx4d8eRF5v4CGpKVE8EnPQo0CFGrnGVlu8kncXipEwtwpCsQIpjm3SDeyfh Ah2b+mGlHmB6PKYzocKhQKhMaQh4tpOOlYNqsoGPseZs6xOMpS40LCsUSuhfVIziRi 3asgSyZdZbKMPwQoczZ0RDkU9rp/VUe/B27qFS/w= Date: Tue, 25 Feb 2020 18:11:01 -0800 From: Andrew Morton To: Qian Cai Cc: elver@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/vmscan: fix data races at kswapd_classzone_idx Message-Id: <20200225181101.eca053d3201a6ac68e543572@linux-foundation.org> In-Reply-To: <1582649726-15474-1-git-send-email-cai@lca.pw> References: <1582649726-15474-1-git-send-email-cai@lca.pw> X-Mailer: Sylpheed 3.5.1 (GTK+ 2.24.31; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 25 Feb 2020 11:55:26 -0500 Qian Cai wrote: > 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, > > ... > > --- 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; This is very partial, isn't it? The above code itself is racy against other code which manipulates ->kswapd_classzone_idx and the manipulation in allow_direct_reclaim() is performed by threads other than kswapd and so need the READ_ONCE treatment and is still racy with that? I guess occasional races here don't really matter, but a grossly wrong read from load tearing might matter. In which case shouldn't we be defending against them in all cases where non-kswapd threads read this field?