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 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 638FFC433F5 for ; Wed, 27 Oct 2021 07:12:23 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E45E66103B for ; Wed, 27 Oct 2021 07:12:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E45E66103B Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 81ECA940008; Wed, 27 Oct 2021 03:12:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7CF12940007; Wed, 27 Oct 2021 03:12:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6BDD1940008; Wed, 27 Oct 2021 03:12:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0199.hostedemail.com [216.40.44.199]) by kanga.kvack.org (Postfix) with ESMTP id 5B313940007 for ; Wed, 27 Oct 2021 03:12:22 -0400 (EDT) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 1911F8249980 for ; Wed, 27 Oct 2021 07:12:22 +0000 (UTC) X-FDA: 78741348924.16.2F4AE09 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by imf04.hostedemail.com (Postfix) with ESMTP id 8BF0950000BD for ; Wed, 27 Oct 2021 07:12:14 +0000 (UTC) Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4HfKb01nR2zZcLl; Wed, 27 Oct 2021 15:10:20 +0800 (CST) Received: from dggpeml500025.china.huawei.com (7.185.36.35) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.15; Wed, 27 Oct 2021 15:12:08 +0800 Received: from dggpeml500026.china.huawei.com (7.185.36.106) by dggpeml500025.china.huawei.com (7.185.36.35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.15; Wed, 27 Oct 2021 15:12:06 +0800 Received: from dggpeml500026.china.huawei.com ([7.185.36.106]) by dggpeml500026.china.huawei.com ([7.185.36.106]) with mapi id 15.01.2308.015; Wed, 27 Oct 2021 15:12:06 +0800 From: songyuanzheng To: Dennis Zhou , Christoph Lameter CC: "tj@kernel.org" , "akpm@linux-foundation.org" , "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" Subject: RE: [PATCH -next] mm/percpu: fix data-race with pcpu_nr_empty_pop_pages Thread-Topic: [PATCH -next] mm/percpu: fix data-race with pcpu_nr_empty_pop_pages Thread-Index: AQHXyXUG8JGkGq6nOUaljbOjl/I/yqvkDQiAgAJjj4A= Date: Wed, 27 Oct 2021 07:12:06 +0000 Message-ID: <4be3bce19c1d44c4a04bb411dfa26182@huawei.com> References: <20211025070015.553813-1-songyuanzheng@huawei.com> In-Reply-To: Accept-Language: zh-CN, en-US Content-Language: zh-CN X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.174.179.110] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-CFilter-Loop: Reflected X-Stat-Signature: pdtdzar51999icz485p59idyhh5h8kn8 Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=huawei.com; spf=pass (imf04.hostedemail.com: domain of songyuanzheng@huawei.com designates 45.249.212.187 as permitted sender) smtp.mailfrom=songyuanzheng@huawei.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 8BF0950000BD X-HE-Tag: 1635318734-490198 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Hello, Thanks for the advice, Dennis Zhou and Christoph Lameter.=20 I really appreciate it. I edited this patch by changing the pcpu_nr_empty_pop_pages to atomic_t var= iable. Here is the v2 patch: https://patchwork.kernel.org/project/linux-mm/patch/2= 0211026084312.2138852-1-songyuanzheng@huawei.com/. Would you mind reviewing it again? Thanks, Yuanzheng Song -----Original Message----- From: Dennis Zhou [mailto:dennis@kernel.org]=20 Sent: Tuesday, October 26, 2021 10:42 AM To: Christoph Lameter Cc: songyuanzheng ; dennis@kernel.org; tj@kernel.= org; akpm@linux-foundation.org; linux-mm@kvack.org; linux-kernel@vger.kerne= l.org Subject: Re: [PATCH -next] mm/percpu: fix data-race with pcpu_nr_empty_pop_= pages Hello, On Mon, Oct 25, 2021 at 09:50:48AM +0200, Christoph Lameter wrote: > On Mon, 25 Oct 2021, Yuanzheng Song wrote: >=20 > > When reading the pcpu_nr_empty_pop_pages in pcpu_alloc() and writing=20 > > the pcpu_nr_empty_pop_pages in > > pcpu_update_empty_pages() at the same time, the data-race occurs. >=20 > Looks like a use case for the atomic RMV instructions. >=20 Yeah. I see 2 options. Switch the variable over to an atomic or we can move= the read behind pcpu_lock. All the writes are already behind it othewise t= hat would actually be problematic. In this particular case, reading a wrong= # of empty pages isn't a big deal as eventually the background work will g= et scheduled. Thanks, Dennis > > To fix this issue, use READ_ONCE() and WRITE_ONCE() to read and=20 > > write the pcpu_nr_empty_pop_pages. >=20 > Never thought that READ_ONCE and WRITE_ONCE can fix races like this.=20 > Really? >=20 > > diff --git a/mm/percpu.c b/mm/percpu.c index=20 > > 293009cc03ef..e8ef92e698ab 100644 > > --- a/mm/percpu.c > > +++ b/mm/percpu.c > > @@ -574,7 +574,9 @@ static void pcpu_isolate_chunk(struct pcpu_chunk=20 > > *chunk) > > > > if (!chunk->isolated) { > > chunk->isolated =3D true; > > - pcpu_nr_empty_pop_pages -=3D chunk->nr_empty_pop_pages; > > + WRITE_ONCE(pcpu_nr_empty_pop_pages, > > + READ_ONCE(pcpu_nr_empty_pop_pages) - > > + chunk->nr_empty_pop_pages); >=20 > atomic_sub()? >=20 > > } > > list_move(&chunk->list,=20 > > &pcpu_chunk_lists[pcpu_to_depopulate_slot]); > > } > > @@ -585,7 +587,9 @@ static void pcpu_reintegrate_chunk(struct=20 > > pcpu_chunk *chunk) > > > > if (chunk->isolated) { > > chunk->isolated =3D false; > > - pcpu_nr_empty_pop_pages +=3D chunk->nr_empty_pop_pages; > > + WRITE_ONCE(pcpu_nr_empty_pop_pages, > > + READ_ONCE(pcpu_nr_empty_pop_pages) + > > + chunk->nr_empty_pop_pages); >=20 > atomic_add()? >=20