Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1204873pxb; Fri, 21 Jan 2022 12:11:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJyWJ90i+LgaxtPmYkcBaWh3YipF0a9GCKnZ+ud4ePONvMn5z0gL64AK9RgmX25mVc1KMDJC X-Received: by 2002:a17:903:11cd:b0:149:bf70:2031 with SMTP id q13-20020a17090311cd00b00149bf702031mr5096403plh.40.1642795918198; Fri, 21 Jan 2022 12:11:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642795918; cv=none; d=google.com; s=arc-20160816; b=Xi1jsh4mHoZd0BeQ6frl/mrkFrbcJRN7LF/lD7z72rkADNk9/eHZufkTv/HDZNcujS 1N9cZvkXrA4Cbm6zZ7Dvg48+Fsjhl1F2H7/vOUhY4FWFPFjLNov8o9S2iz2RBR5rgY4A cvo6EmziQT8Nj7ffZLDCheL4gE1Y85iUYNUH2JWH6kIKmAhMyRkGM+ajtgLgsEDSpDmj Vd2Ogni4VPEozloOikfOfGSr/4yh8n7baNucZOS2WNCWw8DpHwVAmMp4MjJckqvjmIL9 h2p1vxascxi2u5qBd5Hf1j6vjjqiSvAbCbj3oWiJakprL7BNQsFqmcMzFN3b2+CNJb10 4WBA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=SAvFOz8ghr5oAZyEvyWgFSjE0cOqBv8pLZgD3az/Alw=; b=aDwgRW+eLlb8Kp2qEve93s44VbwKFH1en0ZrFBI8zxkRWBPEn+WJTIsAbulL8yDSWZ FR4TCJL+Vekec/GdwUhCoaW0M0X3TT5ZepQkvByU09RUk0x5GIWIhE7fWF4EDaFfxbQ+ qpQG8sNMwlonU03jyGIctN3dNMESglFouDr5YXaPhKts+uMQ0GM1gi7JudEThSmVuWIN spDEURj+DHkd81uqnybwEyvNCVLcEQFC4kBo/dRbIoCkJUy1VWKUnnCQbEc9kzLxmixe u+4duTuZbJVuIw7QeWatDUZbCP+sLU6yjaiXRyErcKs1KmaxUOOjbdzdGQZj2Wr3P8Ml xb0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=bsrpLBeE; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v11si6593213plz.366.2022.01.21.12.11.45; Fri, 21 Jan 2022 12:11:58 -0800 (PST) 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; dkim=pass header.i=@google.com header.s=20210112 header.b=bsrpLBeE; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344364AbiASX2q (ORCPT + 99 others); Wed, 19 Jan 2022 18:28:46 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229874AbiASX2p (ORCPT ); Wed, 19 Jan 2022 18:28:45 -0500 Received: from mail-ua1-x932.google.com (mail-ua1-x932.google.com [IPv6:2607:f8b0:4864:20::932]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C5A53C06161C for ; Wed, 19 Jan 2022 15:28:44 -0800 (PST) Received: by mail-ua1-x932.google.com with SMTP id p1so7569007uap.9 for ; Wed, 19 Jan 2022 15:28:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=SAvFOz8ghr5oAZyEvyWgFSjE0cOqBv8pLZgD3az/Alw=; b=bsrpLBeEmGPON4IVnn1J9LS41vfyBHVbscaKWsuCglNOrJmCd5wbVfUTccva0jnF4t 279q+9zbrD+ZWr8qrelwJQTRhNzSYbs5gig4TBG0nyZEqIinSkCPAxiYOvna0hrEiv5D xoeJC9NwShyhujgekVKeZa14J68SMjQo3LSdBUda1rDhWMATtytzbjfPJCNxcthKatxB wt70R5w+8MsTAylz4/5aRRwYbrOpRn5RU8pogPtfenu6omPyh9m0vxuWpD+rg4Rk5VS2 Cm+SF06WzCepcaQQeTQ12imr97K+3TkPsU54aR6k7wXd2b9y4OVXuKL4xdbvd/7zw4B+ P/YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=SAvFOz8ghr5oAZyEvyWgFSjE0cOqBv8pLZgD3az/Alw=; b=Rq90XMzM4N/9FPi32mVM8+LpdEkzljXK0aAk97hR5dtPfBb7xUaIynldgrb1eH9wLk yzaB2qZlTgS2pfHE1FM9BT08u9q3FN4VDq0myfu2tYfPLH0aK3l/qSHwnKdSHMjgDqHI 7AZaIWLYNfq/6ezP9GVostB00YcFPFX+ztqIZs+AOiNNdYj6r7eFRxxb/qoIAYqP/ix+ cRqc73F50dwj+Q52IyvBgGlcUe1PDNgZEoyqfUiWMK3kB5oihBqXQooOkLxLyA4bSYNh 7SKNo6NmZHmEv3ux2ZB1iYgx9XQMAILzSL6TVdrkjP3LmVNPq6lv0V5O/8iQyATKEkcv iMMA== X-Gm-Message-State: AOAM533vafVYEX+jlO6Tf6G7yiAMHc29OfasbQPYH7T9qu/HlPuUHW7j wIPFD/ws3/d78n8bg/FnmT5aYDpzbiDNMYJf2J74Aw== X-Received: by 2002:a67:6587:: with SMTP id z129mr140195vsb.61.1642634923681; Wed, 19 Jan 2022 15:28:43 -0800 (PST) MIME-Version: 1.0 References: <20220118230539.323058-1-pcc@google.com> In-Reply-To: From: Peter Collingbourne Date: Wed, 19 Jan 2022 15:28:32 -0800 Message-ID: Subject: Re: [PATCH] mm/mmzone.c: fix page_cpupid_xchg_last() to READ_ONCE() the page flags To: Peter Zijlstra Cc: Andrey Konovalov , Andrew Morton , Linux Memory Management List , Linux Kernel Mailing List , Mel Gorman , "# 3.4.x" Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jan 19, 2022 at 2:03 AM Peter Zijlstra wrote: > > On Tue, Jan 18, 2022 at 03:05:39PM -0800, Peter Collingbourne wrote: > > After submitting a patch with a compare-exchange loop similar to this > > one to set the KASAN tag in the page flags, Andrey Konovalov pointed > > out that we should be using READ_ONCE() to read the page flags. Fix > > it here. > > What does it actually fix? If it manages to split the read and read > garbage the cmpxchg will fail and we go another round, no harm done. What I wasn't sure about was whether the compiler would be allowed to break this code by hoisting the read of page->flags out of the loop (because nothing in the loop actually writes to page->flags aside from the compare-exchange, and if that succeeds we're *leaving* the loop). That could potentially result in a loop that never terminates if the first compare-exchange fails. This is largely a theoretical problem as far as I know; the assembly produced by clang and gcc on x86_64 and arm64 appears to be doing the expected thing for now, and we're using inline asm for compare-exchange instead of the compiler builtins on those architectures (and on all other architectures it seems? no matches for __atomic_compare_exchange outside of kcsan and the selftests) so the compiler wouldn't be able to look inside it anyway. > > Fixes: 75980e97dacc ("mm: fold page->_last_nid into page->flags where possible") > > As per the above argument, I don't think this rates a Fixes tag, there > is no actual fix. Okay, I'll remove it unless you find the above convincing. > > Signed-off-by: Peter Collingbourne > > Link: https://linux-review.googlesource.com/id/I2e1f5b5b080ac9c4e0eb7f98768dba6fd7821693 > > That's that doing here? I upload my changes to Gerrit and link to them here so that I (and others) can see the progression of the patch via the web UI. > > Cc: stable@vger.kernel.org > > That's massively over-selling things. Fair enough since it isn't causing an actual problem, I'll remove this tag. > > --- > > mm/mmzone.c | 2 +- > > 1 file changed, 1 insertion(+), 1 deletion(-) > > > > diff --git a/mm/mmzone.c b/mm/mmzone.c > > index eb89d6e018e2..f84b84b0d3fc 100644 > > --- a/mm/mmzone.c > > +++ b/mm/mmzone.c > > @@ -90,7 +90,7 @@ int page_cpupid_xchg_last(struct page *page, int cpupid) > > int last_cpupid; > > > > do { > > - old_flags = flags = page->flags; > > + old_flags = flags = READ_ONCE(page->flags); > > last_cpupid = page_cpupid_last(page); > > > > flags &= ~(LAST_CPUPID_MASK << LAST_CPUPID_PGSHIFT); > > I think that if you want to touch that code, something like the below > makes more sense... Yeah, that looks a bit nicer. I'll send a v2 and update the other patch as well. Peter