From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3AD98C7EE22 for ; Tue, 16 May 2023 00:16:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=GSWTIZOXXlz86bGVdUvW1+BKG6S3m5RzNh6fD/F3xHQ=; b=XpokOnv20cnN3+ 0qHhjinpsELDspV69igGPaL527V9kjLNn22LspcGE5Ugcw4/TQ7YJiMuIvRNLD5+Rz8lR2uYSa99J csvGNgwYnKH/v5GmUDTdjVtDys/Ghtym5DKxzJr7NW6eexEvSDfseZW5cnjAKJUeuyE8eNaG/lmOV wGh6FDF7ZGLxhKpZdaUz+Xrm+hOFWBGsFmWIwXcFY8HdL4BRCeYNq5Za2jBnwOK1ZE/XExjCEojj5 igpfzRnzn2oq0kpDviRUa2XCT1BUNce1cwLYqArT9t8lUz00oDPdx4xeCI6H0T3nUsy1y+PxfmPAV wgPpfJc99qdk1XW7Z0uA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pyiMW-003vRa-2p; Tue, 16 May 2023 00:16:20 +0000 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pyiMT-003vQa-0Y for linux-arm-kernel@lists.infradead.org; Tue, 16 May 2023 00:16:18 +0000 Received: by mail-pl1-x62d.google.com with SMTP id d9443c01a7336-1aae90f5ebcso759345ad.1 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=AVLBpIivD0oLpX5D8al7ziI0HBKlhcl8oMVv79xvq97XyfRYzDZqfXemtRn7YxCHm+ bDmnRfxkREnAPD519Q20MHAlx9ojnHshuclAS4ZaQtXdb+p8+HgB3ITQ+3e19wGKlMRm 9Uopzy5hQSpEjoM2+jR/CYz5zfO685/1oUUFGW+VP736csHWshsYnkyqPxc2riPL61Lx 7q2ANwcb7lgGRaKlzTPe/DatqZ1J5PYbyZxUu4ugTYAuu+EKiJSjZLyp8HVGRFp70RA5 jcrN9Qt+PEG7lKyTgdiHdrk2/JmIDqG0vVWi1+PggRdmRC0U/CS/WRI+gcVg0NrKVnLH fLNQ== X-Gm-Message-State: AC+VfDw+isNrw/IAQU7R/D7GnoPIVo67kt99FoJxxB6M7/iJI5TCiHx0 ue8qMYdFUjxTml7T/z0lG1pSOQ== X-Google-Smtp-Source: ACHHUZ6F8d7WPH/3QMv++9pQyJNemJ8pH/5jv3HUHQFDdhHoClEdG/sj9VEr8QKBhfu3MpNJ0Ldv3Q== 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-Disposition: inline In-Reply-To: <7471013e-4afb-e445-5985-2441155fc82c@redhat.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230515_171617_228952_7497D453 X-CRM114-Status: GOOD ( 29.96 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.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 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel