From: Christoph Lameter <cl@gentwo.de>
To: Yuanzheng Song <songyuanzheng@huawei.com>
Cc: 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
Date: Mon, 25 Oct 2021 09:50:48 +0200 (CEST) [thread overview]
Message-ID: <alpine.DEB.2.22.394.2110250943310.2528@gentwo.de> (raw)
In-Reply-To: <20211025070015.553813-1-songyuanzheng@huawei.com>
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.
> 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()?
next prev parent reply other threads:[~2021-10-25 7:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-25 7:00 Yuanzheng Song
2021-10-25 7:50 ` Christoph Lameter [this message]
2021-10-26 2:41 ` Dennis Zhou
2021-10-27 7:12 ` songyuanzheng
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=alpine.DEB.2.22.394.2110250943310.2528@gentwo.de \
--to=cl@gentwo.de \
--cc=akpm@linux-foundation.org \
--cc=dennis@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=songyuanzheng@huawei.com \
--cc=tj@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox