From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54195C3F68F for ; Mon, 10 Feb 2020 04:28:43 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E831F21775 for ; Mon, 10 Feb 2020 04:28:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="16CmDXbC" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E831F21775 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=linux-foundation.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 758CB6B00AA; Sun, 9 Feb 2020 23:28:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 709FD6B00AB; Sun, 9 Feb 2020 23:28:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F8A76B00AC; Sun, 9 Feb 2020 23:28:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0031.hostedemail.com [216.40.44.31]) by kanga.kvack.org (Postfix) with ESMTP id 445646B00AA for ; Sun, 9 Feb 2020 23:28:42 -0500 (EST) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 083F62C98 for ; Mon, 10 Feb 2020 04:28:42 +0000 (UTC) X-FDA: 76472936484.19.room05_862062e841049 X-HE-Tag: room05_862062e841049 X-Filterd-Recvd-Size: 2978 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf41.hostedemail.com (Postfix) with ESMTP for ; Mon, 10 Feb 2020 04:28:41 +0000 (UTC) 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 7D748214DB; Mon, 10 Feb 2020 04:28:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581308920; bh=l1OeRezZzhe+y0mBNHwN5J5otTAGBbttv+vIF5SGOJk=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=16CmDXbCPWS2GhluQtjfPjZiGcRa6DeS3CtEcGMYB8mhvGi20OtXYMbMkIR31W+2C 5zAWDQKxBkgmbnZRu1EFfvvbU9j9vaofeckw1R3rowBSLuusqh01YIjP8JbNd3cLww 44VYaNI4cEWAIktNi/gYtd6WzPyBlKFY/59jKcLo= Date: Sun, 9 Feb 2020 20:28:40 -0800 From: Andrew Morton To: Qian Cai Cc: hannes@cmpxchg.org, mhocko@kernel.org, vdavydov.dev@gmail.com, cgroups@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm/memcontrol: fix a data race in scan count Message-Id: <20200209202840.2bf97ffcfa811550d733c461@linux-foundation.org> In-Reply-To: <20200206034945.2481-1-cai@lca.pw> References: <20200206034945.2481-1-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 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000058, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Wed, 5 Feb 2020 22:49:45 -0500 Qian Cai wrote: > struct mem_cgroup_per_node mz.lru_zone_size[zone_idx][lru] could be > accessed concurrently as noticed by KCSAN, > > ... > > Reported by Kernel Concurrency Sanitizer on: > CPU: 95 PID: 50964 Comm: cc1 Tainted: G W O L 5.5.0-next-20200204+ #6 > Hardware name: HPE ProLiant DL385 Gen10/ProLiant DL385 Gen10, BIOS A40 07/10/2019 > > The write is under lru_lock, but the read is done as lockless. The scan > count is used to determine how aggressively the anon and file LRU lists > should be scanned. Load tearing could generate an inefficient heuristic, > so fix it by adding READ_ONCE() for the read. > > ... > > --- a/include/linux/memcontrol.h > +++ b/include/linux/memcontrol.h > @@ -533,7 +533,7 @@ unsigned long mem_cgroup_get_zone_lru_size(struct lruvec *lruvec, > struct mem_cgroup_per_node *mz; > > mz = container_of(lruvec, struct mem_cgroup_per_node, lruvec); > - return mz->lru_zone_size[zone_idx][lru]; > + return READ_ONCE(mz->lru_zone_size[zone_idx][lru]); > } I worry about the readability/maintainability of these things. A naive reader who comes upon this code will wonder "why the heck is it using READ_ONCE?". A possibly lengthy trawl through the git history will reveal the reason but that's rather unkind. Wouldn't a simple /* modified under lru_lock, so use READ_ONCE */ improve the situation?