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 BBEF6C433EF for ; Tue, 26 Oct 2021 02:41:55 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5FD606105A for ; Tue, 26 Oct 2021 02:41:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 5FD606105A Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id C944380007; Mon, 25 Oct 2021 22:41:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C1B16940007; Mon, 25 Oct 2021 22:41:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ABBB080007; Mon, 25 Oct 2021 22:41:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0220.hostedemail.com [216.40.44.220]) by kanga.kvack.org (Postfix) with ESMTP id 9592B940007 for ; Mon, 25 Oct 2021 22:41:54 -0400 (EDT) Received: from smtpin32.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 53D1D183CB1F4 for ; Tue, 26 Oct 2021 02:41:54 +0000 (UTC) X-FDA: 78737038548.32.02D68B7 Received: from mail-qk1-f170.google.com (mail-qk1-f170.google.com [209.85.222.170]) by imf20.hostedemail.com (Postfix) with ESMTP id 9ACCED0000A7 for ; Tue, 26 Oct 2021 02:41:48 +0000 (UTC) Received: by mail-qk1-f170.google.com with SMTP id bp7so13675632qkb.12 for ; Mon, 25 Oct 2021 19:41:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=NIMQhTSA9FAdcOXPvrauG7Z+Po3zyqmjnlqLeXJDFUw=; b=vB0hlqNXBWvF/WSgq2W2i8WLfSHJDF/m4449OwyXb/eSRRVPCAdr6ZtM3gtXzb3JhR OT3W3Ma/X3az7ypMQP1jewo3trjhdIzZq/PSYTb7AfpR1a7RVFj+R3kw8dxzOSwvaUQ8 iglGK0T+eP9SoPvj+Cr/6AucTNj8NSSiBC12OiRGpgpdhBo4eUwI3PLLfQb4yE8J6GJd zBSfX5LE85qZAEaYF96frApO+o87qUrMmRxpwfSHbrHb2qQ0M4MP+0QvKcQBJPrR/pUd RbgGf6AJ9iKjcFwgnZAIm0OUPplPwTlCV4MSXIYWqwQyqBrtdJXHi05Mu4ln5leTs9Dr wr1w== X-Gm-Message-State: AOAM532lHPNc4TsfSL1UcvJROxySY4qacH2S8jI8pSi8hefEzS+0zYGQ 37Bu8fOExwpq/CySr6Lc/Fg= X-Google-Smtp-Source: ABdhPJxJVgPz78GJZ1ef2Z+ptkjs7fvTeMbYL3OD4kSiMlssIJbRFWu6ZkVyfzuv1PtQYW2gYYa9wQ== X-Received: by 2002:a05:620a:d93:: with SMTP id q19mr1536754qkl.9.1635216113331; Mon, 25 Oct 2021 19:41:53 -0700 (PDT) Received: from fedora (pool-173-68-57-129.nycmny.fios.verizon.net. [173.68.57.129]) by smtp.gmail.com with ESMTPSA id i21sm4318352qkn.15.2021.10.25.19.41.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Oct 2021 19:41:52 -0700 (PDT) Date: Mon, 25 Oct 2021 22:41:51 -0400 From: Dennis Zhou To: Christoph Lameter Cc: Yuanzheng Song , dennis@kernel.org, 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 Message-ID: References: <20211025070015.553813-1-songyuanzheng@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Stat-Signature: mry8ia776wjf8fjiq7xu5zoczada1nbe X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 9ACCED0000A7 Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none); spf=pass (imf20.hostedemail.com: domain of dennisszhou@gmail.com designates 209.85.222.170 as permitted sender) smtp.mailfrom=dennisszhou@gmail.com X-HE-Tag: 1635216108-134321 X-Bogosity: Ham, tests=bogofilter, spamicity=0.001843, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Hello, On Mon, Oct 25, 2021 at 09:50:48AM +0200, Christoph Lameter wrote: > On Mon, 25 Oct 2021, Yuanzheng Song wrote: > > > When reading the pcpu_nr_empty_pop_pages in pcpu_alloc() > > and writing the pcpu_nr_empty_pop_pages in > > pcpu_update_empty_pages() at the same time, > > the data-race occurs. > > Looks like a use case for the atomic RMV instructions. > 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 that 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 get scheduled. Thanks, Dennis > > To fix this issue, use READ_ONCE() and WRITE_ONCE() to > > read and write the pcpu_nr_empty_pop_pages. > > Never thought that READ_ONCE and WRITE_ONCE can fix races like > this. Really? > > > diff --git a/mm/percpu.c b/mm/percpu.c > > index 293009cc03ef..e8ef92e698ab 100644 > > --- a/mm/percpu.c > > +++ b/mm/percpu.c > > @@ -574,7 +574,9 @@ static void pcpu_isolate_chunk(struct pcpu_chunk *chunk) > > > > if (!chunk->isolated) { > > chunk->isolated = true; > > - pcpu_nr_empty_pop_pages -= chunk->nr_empty_pop_pages; > > + WRITE_ONCE(pcpu_nr_empty_pop_pages, > > + READ_ONCE(pcpu_nr_empty_pop_pages) - > > + chunk->nr_empty_pop_pages); > > atomic_sub()? > > > } > > list_move(&chunk->list, &pcpu_chunk_lists[pcpu_to_depopulate_slot]); > > } > > @@ -585,7 +587,9 @@ static void pcpu_reintegrate_chunk(struct pcpu_chunk *chunk) > > > > if (chunk->isolated) { > > chunk->isolated = false; > > - pcpu_nr_empty_pop_pages += chunk->nr_empty_pop_pages; > > + WRITE_ONCE(pcpu_nr_empty_pop_pages, > > + READ_ONCE(pcpu_nr_empty_pop_pages) + > > + chunk->nr_empty_pop_pages); > > atomic_add()? >