Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2801039rwd; Mon, 15 May 2023 17:36:16 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5fDD5iWd17t398P/ACxOEEkgb7tHFFjQh8T4SSKKnoVBZEsd9k8jo2BgVQKLdoG8NJaRQn X-Received: by 2002:a17:903:22c9:b0:1a5:22a6:4e6a with SMTP id y9-20020a17090322c900b001a522a64e6amr43777093plg.51.1684197375862; Mon, 15 May 2023 17:36:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684197375; cv=none; d=google.com; s=arc-20160816; b=rdA+X0KyhkSxa1nb/zTiAMiOBmpWs1aDcoJnw5irkWhDoq9hIhce/F7GM4jfegEPLy qpc3i6iTyxVCATFCbaIsRqkpFVOwlvvZcFcIo/jWif8sYeTUU877P9LHnwKz9i9CC/yP sVr+wnPwnJIDcP1HJa+K0+nGLTnc4AwIu7obeFssHmTOyOn32Ee0HBHT1jssg8Asi2XX HfP22DQDB0WJdNIqJR8l5I4U5ATd3c6EQ3OxuNTxGyVZT/Mev9698xPxx+VE7//9G3MI ne+QDe7qpn3kGicMvwBltIStNGoKYrQC9Ra/uHEQNIf8ret9rNXeDo++vMdsfSunBrGK rrlg== 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:dkim-signature; bh=T6gh2qYVut9aSylVwiYmVdq2jPzWcix3qnUjMW+9u1Y=; b=SJj2sAXK8PathI7e7sEUf/Ea7kVfeX6T83b1xDLfqcjLndViChXnG2eHJtuSqnfsSH 7PkAoCo7Gf8xuwlnotnfHNArlPwn/SgSlP2M6agCWxSBCN796xV/ks37aEUt3DqaI6AZ AigZjbFbRrCbqcC+a+uFfyUYEnJuBTBfuuYdXwX90M1chBpGnaAi406O48E9Q5rvvqdD WTt+RrSUUL5S+692WQxsNhtrdPU9XnX/bkq0xRyBJZIGgMV5F0B8pbjLHU/gLULGUmMw W3pogxDsTUAPiFi2kdSG1+2qq0K6MMtYvPurcJTZRYT1Gx4ExLGIr/UgivNyUFmcDsRA E1Hg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=4DT4IrQP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u11-20020a170902a60b00b001a986e5bd92si3639285plq.83.2023.05.15.17.35.57; Mon, 15 May 2023 17:36:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=4DT4IrQP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S245369AbjEPAQT (ORCPT + 99 others); Mon, 15 May 2023 20:16:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244409AbjEPAQS (ORCPT ); Mon, 15 May 2023 20:16:18 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A54E75FD1 for ; Mon, 15 May 2023 17:16:15 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1ac65ab7432so785035ad.0 for ; Mon, 15 May 2023 17:16:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684196175; x=1686788175; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=T6gh2qYVut9aSylVwiYmVdq2jPzWcix3qnUjMW+9u1Y=; b=4DT4IrQPyQsz4h+dcRk/i2LEM+ilSfmH36PEG55MInjjTBGxQELkAy2DYSBQDQwIaO 4X9iQxrb0bD6WqOfmbVD1TRc+OSr1/fhNSat4Lh1WFRxQfsuAIolN8800kDElXhNXelP LlqIpSwDtJPlkSMJL3mCFlcx7Mpde4o4iDk7xUzqFS4y8VdInEFgNXsvhb5OVozPBqPC P5nDQrkneFxH9jJ+OustYYE3W7BHQQzUaU7WXrV/bo2+IfYJL0t8d7cDO2VZ7sGKINON QPt9EIBsy+JHePYNsII5qP0NAJTzYMjlpMKd/qeQ7nDio77QEz76JhMEYmYceqmrZ+oQ Ji3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684196175; x=1686788175; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=T6gh2qYVut9aSylVwiYmVdq2jPzWcix3qnUjMW+9u1Y=; b=d38R3hzi+qG83ojJt32yQAx04AZZ9qgZY0wweRfGN0ZO5HR2mG8+FamA3eFps2b/e8 lLgHxxoouHRGXR8E/kCj9tkVrTmmXU2i5FJyjsociJqHPwTgJPX5KFgkD0ws2rWyMHII I5bAq/BjLgz9d4iBBXQ+9aQ6Fy1cuk1auyb06KejoKmEDQXiqH1N6m9UvKlUMqTErSh8 ze2ru7UOZI5fxva18V50yfkDPrp94H4VIEhhJvNSd2AFjUbFqk0HBABEdsqSEeNjn9yg T3VGedaVwcvS0VyEXyjBt4EuGQ84TNiVIz/zmjvF2NsJfwKLjqVfsrTq8DcLYaOZr7qE CoMg== X-Gm-Message-State: AC+VfDzTj4PXtO42ZcsGlUNd6g0hgCOyWybvTogCQLrTOvV2/k3O8PSE MMk1hDWFbgyS+pnQVMHHDCIx0g== X-Received: by 2002:a17:903:2281:b0:1aa:dfdf:9232 with SMTP id b1-20020a170903228100b001aadfdf9232mr30882plh.16.1684196174906; Mon, 15 May 2023 17:16:14 -0700 (PDT) Received: from google.com ([2620:15c:2d3:205:c825:9c0b:b4be:8ee4]) by smtp.gmail.com with ESMTPSA id z21-20020aa791d5000000b006260526cf0csm12286564pfa.116.2023.05.15.17.16.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 17:16:14 -0700 (PDT) Date: Mon, 15 May 2023 17:16:09 -0700 From: Peter Collingbourne To: David Hildenbrand Cc: Catalin Marinas , Qun-wei Lin =?utf-8?B?KOael+e+pOW0tCk=?= , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, "surenb@google.com" , Chinwen Chang =?utf-8?B?KOW8temMpuaWhyk=?= , "kasan-dev@googlegroups.com" , Kuan-Ying Lee =?utf-8?B?KOadjuWGoOepjik=?= , Casper Li =?utf-8?B?KOadjuS4reamrik=?= , "gregkh@linuxfoundation.org" , vincenzo.frascino@arm.com, Alexandru Elisei , will@kernel.org, eugenis@google.com, Steven Price , stable@vger.kernel.org Subject: Re: [PATCH 1/3] mm: Move arch_do_swap_page() call to before swap_free() Message-ID: References: <20230512235755.1589034-1-pcc@google.com> <20230512235755.1589034-2-pcc@google.com> <7471013e-4afb-e445-5985-2441155fc82c@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <7471013e-4afb-e445-5985-2441155fc82c@redhat.com> X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, May 13, 2023 at 05:29:53AM +0200, David Hildenbrand wrote: > On 13.05.23 01:57, Peter Collingbourne wrote: > > Commit c145e0b47c77 ("mm: streamline COW logic in do_swap_page()") moved > > the call to swap_free() before the call to set_pte_at(), which meant that > > the MTE tags could end up being freed before set_pte_at() had a chance > > to restore them. One other possibility was to hook arch_do_swap_page(), > > but this had a number of problems: > > > > - The call to the hook was also after swap_free(). > > > > - The call to the hook was after the call to set_pte_at(), so there was a > > racy window where uninitialized metadata may be exposed to userspace. > > This likely also affects SPARC ADI, which implements this hook to > > restore tags. > > > > - As a result of commit 1eba86c096e3 ("mm: change page type prior to > > adding page table entry"), we were also passing the new PTE as the > > oldpte argument, preventing the hook from knowing the swap index. > > > > Fix all of these problems by moving the arch_do_swap_page() call before > > the call to free_page(), and ensuring that we do not set orig_pte until > > after the call. > > > > Signed-off-by: Peter Collingbourne > > Suggested-by: Catalin Marinas > > Link: https://linux-review.googlesource.com/id/I6470efa669e8bd2f841049b8c61020c510678965 > > Cc: # 6.1 > > Fixes: ca827d55ebaa ("mm, swap: Add infrastructure for saving page metadata on swap") > > Fixes: 1eba86c096e3 ("mm: change page type prior to adding page table entry") > > I'm confused. You say c145e0b47c77 changed something (which was after above > commits), indicate that it fixes two other commits, and indicate "6.1" as > stable which does not apply to any of these commits. Sorry, the situation is indeed a bit confusing. - In order to make the arch_do_swap_page() hook suitable for fixing the bug introduced by c145e0b47c77, patch 1 addresses a number of issues, including fixing bugs introduced by ca827d55ebaa and 1eba86c096e3, but we haven't fixed the c145e0b47c77 bug yet, so there's no Fixes: tag for it yet. - Patch 2, relying on the fixes in patch 1, makes MTE install an arch_do_swap_page() hook (indirectly, by making arch_swap_restore() also hook arch_do_swap_page()), thereby fixing the c145e0b47c77 bug. - 6.1 is the first stable version in which all 3 commits in my Fixes: tags are present, so that is the version that I've indicated in my stable tag for this series. In theory patch 1 could be applied to older kernel versions, but it wouldn't fix any problems that we are facing with MTE (because it only fixes problems relating to the arch_do_swap_page() hook, which older kernel versions don't hook with MTE), and there are some merge conflicts if we go back further anyway. If the SPARC folks (the previous only user of this hook) want to fix these issues with ADI, they can propose their own backport. > > @@ -3959,7 +3960,6 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > > VM_BUG_ON(!folio_test_anon(folio) || > > (pte_write(pte) && !PageAnonExclusive(page))); > > set_pte_at(vma->vm_mm, vmf->address, vmf->pte, pte); > > - arch_do_swap_page(vma->vm_mm, vma, vmf->address, pte, vmf->orig_pte); > > folio_unlock(folio); > > if (folio != swapcache && swapcache) { > > > You are moving the folio_free_swap() call after the folio_ref_count(folio) > == 1 check, which means that such (previously) swapped pages that are > exclusive cannot be detected as exclusive. Ack. I will fix this in v2. Peter