Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp4294693pxb; Mon, 25 Oct 2021 01:09:56 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzreDgtwoksLrIaPbRq8nyKXlaRaayNuzFYK6w4kwjq1S2eVNiQ7v3atAAQfTp8xuTnF+rP X-Received: by 2002:a50:ce4e:: with SMTP id k14mr24014452edj.269.1635149395952; Mon, 25 Oct 2021 01:09:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635149395; cv=none; d=google.com; s=arc-20160816; b=gi37sCI9Cc9KaUHAPpz4ZRXZ5B4An5xxWKHXDRuTuWbjuUY2k6KCM4rogxT8jZJUrZ OvqbdVkB3PmQR3Ecekf8tyLQCZDh0tvtMUB8VvKs3DpHEVz9QZ2Dd7OZa8fwMsDVgQwh yESoxcEpqyVTGUUO1yBJ8sNIeRF7GLccyaH4Mc8U+SBDsH3yUzOuMNTj4J2b0Cf8tQWx mfqrJngbSO5yKDFvp+LEbzjwyHmIikbiSfpxx3AGQA7JdSTOwgJBLL3iadjJJdJDBIRW 3o0z1rKB8ICBISUw6plOI0kFkbeG9l4YXZ6eARMsQhdNPTbRhZgQk5GmBqvv8v/EtQtf /3nA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date; bh=n8WsGXBPPC/z2sLvPhY7GERps0EF6ixGYkynm2NM6Zs=; b=fR4Ti557rH4oEutr2Ms7E2QimO6hQ77AtyyTLlhVZDM27YbN75iUXjnTerYJ/6YXRJ jd/cjBby6mtkTH4ee/iWQsHDSpcogpxzgWF9DbN0O6ih65+Grk7pJCjfPX72tJ3kbCY3 VVAWgd3HMXz+HksCorZQshkgp1YfX6TQ0N1B8b7dPLKJMqfNsZP4hils6yZ0TnaYQfe/ zfyRfy8Chyhu4/8HCng8n0shukxvmls0AuAfJzb1vof/5eWQoUJUpEYLRJaElTAZKsPL Q8Uf7a25YxMMjpej45zufUvcW6Zyk7a8H5fyLAGh7NLM4Xv2ayMCYxJ9i7hL1L9391DI vFyw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id do13si2824634ejc.718.2021.10.25.01.09.32; Mon, 25 Oct 2021 01:09:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232007AbhJYHxN (ORCPT + 99 others); Mon, 25 Oct 2021 03:53:13 -0400 Received: from vmi485042.contaboserver.net ([161.97.139.209]:34488 "EHLO gentwo.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229491AbhJYHxM (ORCPT ); Mon, 25 Oct 2021 03:53:12 -0400 Received: by gentwo.de (Postfix, from userid 1001) id 783F5B00267; Mon, 25 Oct 2021 09:50:48 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by gentwo.de (Postfix) with ESMTP id 7443EB00100; Mon, 25 Oct 2021 09:50:48 +0200 (CEST) Date: Mon, 25 Oct 2021 09:50:48 +0200 (CEST) From: Christoph Lameter To: Yuanzheng Song 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 In-Reply-To: <20211025070015.553813-1-songyuanzheng@huawei.com> Message-ID: References: <20211025070015.553813-1-songyuanzheng@huawei.com> User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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()?