Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp966710pxb; Mon, 25 Oct 2021 23:44:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxo4Xsd8mBTP8tiXqTAkLbuSzkpxnQRID/euHsSTTYJs/2pu9ZkTJLHdy+dvVmjeU0+tsOd X-Received: by 2002:a17:90b:4f90:: with SMTP id qe16mr27352909pjb.137.1635230647774; Mon, 25 Oct 2021 23:44:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635230647; cv=none; d=google.com; s=arc-20160816; b=ua9P/WIKOzn6zjeW+nsu00qSnoS/D2klhYNFE5CPxLWYVY6ustW4f43kqgeeWVMeHC zadmla0O8eBf+1MtKddiL05tf9wwZXg7Sy19nP/+VheLCBFmJDdtcSU6chEWSlm84tzW ivb5SEAyvNGNtH955SwLBxpj05D2dxHBOzJHB3DVFJk5mQo6+dzQ2WN8SuN01RqPsUZv iRFCrTRdy62NHVZjVlhEcr9nTjgHB89EMgFBxTZtpE6cF0hsE0V/Obljv300mDo7zmV+ 3ccu+Eesmon1KTkLOf2Mt/c/we8aaOWBbZX6Lq4ljLcOBMnLXay3FAshEiAMRagNkZsk P/zQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=NIMQhTSA9FAdcOXPvrauG7Z+Po3zyqmjnlqLeXJDFUw=; b=t08IBaHcTQ4ufRfo8IWajIDsIyxENvipbb/2L/qNlXEyz/7V9+GAb0LyA+ZBn3pMUt rlc8cYr5839NFYrQaJw/KUJWG6gdwY1KoF3pssM/LndYNiitbMyVkTxaXPfzy2jWhQpb kJJ9jbTnbk/gYRcBGHqiXlGqNdomK5ucMr5iGmcL5mw+ke4Hh7WFVG20Kb+PSPf+7W8I fsI6fWduFbTdHj4HpXDKIr0nZjfXX10DYv0DEQPlM6c44HTK+bIDIQEvB8lX6KLX6Fi2 XyxF8jmbiwAlENd8I+sgWhO2WxIwAUktimRuPHjhwvI4zNJAXUBz0+MUG6U+/8shG2on J5eg== 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id kv20si9825291pjb.161.2021.10.25.23.43.55; Mon, 25 Oct 2021 23:44:07 -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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234357AbhJZCoT (ORCPT + 99 others); Mon, 25 Oct 2021 22:44:19 -0400 Received: from mail-qk1-f170.google.com ([209.85.222.170]:40557 "EHLO mail-qk1-f170.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230111AbhJZCoQ (ORCPT ); Mon, 25 Oct 2021 22:44:16 -0400 Received: by mail-qk1-f170.google.com with SMTP id x123so13809140qke.7 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=p1Bt4GccmF6lVcHqSEcyMQPFZ0bg90fm9W1WQm6nLnp6DnlKSpz7AenMe1D5Rn1bCf NMorX4pQZzKAEvVM57F3C9/TPl0TfXJPi37cwczjpIUl43M+WcyCLA92cwXym3QV36AC ZnDNMuCGU+FI5edgm1dEnoftcYlTQAMRsS3fU55b5fvBVeAcudXTzQ6SN6DqC6+QNMfC DnVLnJlpekaR2H/HiE7542sWOYP1s+a35kpfRLoMVrRR7d9XBB0+qa6Dh7JYhXCilvfS LEYZfn6xeZ38VXzF3uSBtXGQjuAv1OvpxJKmG7mm1DGi+Cf6A2B/1TBPywFnuOb9i2G7 tO9A== X-Gm-Message-State: AOAM531OumAODTkOpWkoaNBc+E67bp0P7LpaDUvemRT0Pyr09EDARXGq r0bYV2/IJFC+uaX3uhnwRPM= 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: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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()? >