From patchwork Fri Feb 7 22:31:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qian Cai X-Patchwork-Id: 11371331 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E463F109A for ; Fri, 7 Feb 2020 22:31:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A2B03217BA for ; Fri, 7 Feb 2020 22:31:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lca.pw header.i=@lca.pw header.b="Tv72J/MJ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A2B03217BA Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=lca.pw Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id DC6B86B0007; Fri, 7 Feb 2020 17:31:28 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D78146B0008; Fri, 7 Feb 2020 17:31:28 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C66736B000A; Fri, 7 Feb 2020 17:31:28 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0054.hostedemail.com [216.40.44.54]) by kanga.kvack.org (Postfix) with ESMTP id AA4026B0007 for ; Fri, 7 Feb 2020 17:31:28 -0500 (EST) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 634DC4DB6 for ; Fri, 7 Feb 2020 22:31:28 +0000 (UTC) X-FDA: 76464778656.28.coat41_641f9d6b5be26 X-Spam-Summary: 2,0,0,0e3acc80d4bfc80d,d41d8cd98f00b204,cai@lca.pw,:akpm@linux-foundation.org:elver@google.com::linux-kernel@vger.kernel.org:cai@lca.pw,RULES_HIT:41:355:379:421:541:800:960:966:973:988:989:1260:1345:1437:1534:1542:1711:1730:1747:1777:1792:2194:2196:2198:2199:2200:2201:2393:2559:2562:2918:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3870:3872:4321:4385:4605:5007:6261:6653:7903:7904:8603:8784:8957:9163:10004:11026:11657:11658:11914:12043:12296:12297:12438:12517:12519:12555:12679:12740:12895:12986:14018:14096:14104:14181:14394:14721:21067:21080:21220:21444:21524:21627:21990:30029:30054:30056,0,RBL:209.85.160.193:@lca.pw:.lbl8.mailshell.net-62.2.0.100 66.100.201.201,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:45,LUA_SUMMARY:none X-HE-Tag: coat41_641f9d6b5be26 X-Filterd-Recvd-Size: 4834 Received: from mail-qt1-f193.google.com (mail-qt1-f193.google.com [209.85.160.193]) by imf14.hostedemail.com (Postfix) with ESMTP for ; Fri, 7 Feb 2020 22:31:27 +0000 (UTC) Received: by mail-qt1-f193.google.com with SMTP id v25so626697qto.7 for ; Fri, 07 Feb 2020 14:31:27 -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=f6HrDmuf4aCREtfI6DUuWK8pfybhsb7D5BH5pspKyPk=; b=Tv72J/MJwujqp0CyIrTDNsoH+54HegKcOn+RHnsC5TlvgsLRf4jCsB5leiSZHJg7Cr VSDA8UkPPGh4zcciKum6uTLWtePioMS8S+rfazU0YCz2Lqbm14j2cse5sYkvzrcqhcex fbiW/C6Ww/JdxnpVMAKF5tNtdyWxPYj52irA52Sbq4Sn9u0MtAWOWx8BctuUAWaUoH0F zzr3Gu2/9i0H8cDaB4zFd82BFB/OvhfCE1BTi4F6L2Ov4BJ6y8PGSE/PZSonkJd2Cbqa qGg0RKOgw6G+/CEnjGYXRBkqU2iELJoXrS84zIgV9jeH18mb9i/vnwD5l7k9+UbOwa2J mtPw== 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=f6HrDmuf4aCREtfI6DUuWK8pfybhsb7D5BH5pspKyPk=; b=aRzqAW6iiVmEUY1n1A+jjFgmjn1aewdmd/4c9738d1WtMmq4DERStdFz5oQ+kkZSi6 r6uZls2poB1N19CkDc35c0vamcPtExw1MKrplhZHL9kin8iZkyqxvByFa9JxkczZU1YN i3d6e2rFqYmBSYrmIUyOgBpiOtg84CZpMkR02rTMQpN2pzNCJDnL9gws2fEVqoYqBnd2 T/B/UQ4pGDesok90ct53xuU/mV2VBZpvMt7Ok8lgrU7kAZbl40eftt351c6nxVBcwBmw kxMqLpHHs4lSy1V650NMTzb9Lg9dP2v0dBBHtnxEvoF23RSkEngezbx/5XYUSsdT6Gvt xiwQ== X-Gm-Message-State: APjAAAULYAkAZ6DvoJA+VKvrrGoZu7a3Of3Cg4A4kdpwb/r/RI57rYtn wFT7MdAgkd9q8FbVpHVbyXbJFw== X-Google-Smtp-Source: APXvYqxKnwnODtpetfHQiWE90sgaIt6GjS/EYYakURQKqcM+dQhiT3VbONwdjO2Lsj5yRqnLB1dE9w== X-Received: by 2002:aed:2bc2:: with SMTP id e60mr564770qtd.115.1581114687351; Fri, 07 Feb 2020 14:31:27 -0800 (PST) Received: from qcai.nay.com (nat-pool-bos-t.redhat.com. [66.187.233.206]) by smtp.gmail.com with ESMTPSA id x4sm1994873qkx.33.2020.02.07.14.31.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 07 Feb 2020 14:31:26 -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/list_lru: fix a data race in list_lru_count_one Date: Fri, 7 Feb 2020 17:31:19 -0500 Message-Id: <1581114679-5488-1-git-send-email-cai@lca.pw> X-Mailer: git-send-email 1.8.3.1 X-Bogosity: Ham, tests=bogofilter, spamicity=0.002259, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: struct list_lru_one l.nr_items could be accessed concurrently as noticed by KCSAN, BUG: KCSAN: data-race in list_lru_count_one / list_lru_isolate_move write to 0xffffa102789c4510 of 8 bytes by task 823 on cpu 39: list_lru_isolate_move+0xf9/0x130 list_lru_isolate_move at mm/list_lru.c:180 inode_lru_isolate+0x12b/0x2a0 __list_lru_walk_one+0x122/0x3d0 list_lru_walk_one+0x75/0xa0 prune_icache_sb+0x8b/0xc0 super_cache_scan+0x1b8/0x250 do_shrink_slab+0x256/0x6d0 shrink_slab+0x41b/0x4a0 shrink_node+0x35c/0xd80 balance_pgdat+0x652/0xd90 kswapd+0x396/0x8d0 kthread+0x1e0/0x200 ret_from_fork+0x27/0x50 read to 0xffffa102789c4510 of 8 bytes by task 6345 on cpu 56: list_lru_count_one+0x116/0x2f0 list_lru_count_one at mm/list_lru.c:193 super_cache_count+0xe8/0x170 do_shrink_slab+0x95/0x6d0 shrink_slab+0x41b/0x4a0 shrink_node+0x35c/0xd80 do_try_to_free_pages+0x1f7/0xa10 try_to_free_pages+0x26c/0x5e0 __alloc_pages_slowpath+0x458/0x1290 __alloc_pages_nodemask+0x3bb/0x450 alloc_pages_vma+0x8a/0x2c0 do_anonymous_page+0x170/0x700 __handle_mm_fault+0xc9f/0xd00 handle_mm_fault+0xfc/0x2f0 do_page_fault+0x263/0x6f9 page_fault+0x34/0x40 Reported by Kernel Concurrency Sanitizer on: CPU: 56 PID: 6345 Comm: oom01 Tainted: G W L 5.5.0-next-20200205+ #4 Hardware name: HPE ProLiant DL385 Gen10/ProLiant DL385 Gen10, BIOS A40 07/10/2019 A shattered l.nr_items could affect the shrinker behaviour due to a data race. Fix it by adding READ_ONCE() for the read. Since the writes are aligned and up to word-size, assume those are safe from data races to avoid readability issues of writing WRITE_ONCE(var, var + val). Signed-off-by: Qian Cai --- mm/list_lru.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/list_lru.c b/mm/list_lru.c index 0f1f6b06b7f3..249468d06b9c 100644 --- a/mm/list_lru.c +++ b/mm/list_lru.c @@ -190,7 +190,7 @@ unsigned long list_lru_count_one(struct list_lru *lru, rcu_read_lock(); l = list_lru_from_memcg_idx(nlru, memcg_cache_id(memcg)); - count = l->nr_items; + count = READ_ONCE(l->nr_items); rcu_read_unlock(); return count;