From patchwork Wed May 17 02:21:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 13244075 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 AFF78C77B7A for ; Wed, 17 May 2023 02:21:54 +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:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=tX4mhEM8mCkfMsL7aF/PQg0kKH4eje80eOKDrjMGHr8=; b=AtvIVr+KTM+2xTWB0uF3PbtGlu rQOP/byeFSijE1AOhlrGEIjwvK3tdhSiIll1qOv0Ll0IaJp2ClZR2nDe3l5E+jEOYFityCkZPC016 NHaoXbbC7zuYP9ur6RriEfdRL6zBUxzeU/CxCOYpqucGO7xDpdXP18PYpETEnPGu2FNzdkh48b8/g 9RmQGzPYCM9KVo4UDi0ubEzjalnIz2PKwIDYBceFjxMx9hcDtTzYvXLVQbJUkZAXd7Ne09VzSXKUF 79UpajHlFVsMSB/eriNQ8JsLmWI+vVgvuISeN3kB1uTwk9pVc+2GigDvBD2mGuIU27Ja02oC/6gNK FrtRGESg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pz6nG-007oKk-2O; Wed, 17 May 2023 02:21:34 +0000 Received: from mail-yb1-xb49.google.com ([2607:f8b0:4864:20::b49]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pz6n9-007oG4-1f for linux-arm-kernel@lists.infradead.org; Wed, 17 May 2023 02:21:28 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-ba7831dfe95so226957276.2 for ; Tue, 16 May 2023 19:21:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684290085; x=1686882085; h=content-transfer-encoding:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:from:to:cc:subject:date :message-id:reply-to; bh=icy+EhZ3UBgzX+LkBKZmwy9I7D5yLVOL3MyBuUnW0II=; b=kU3u/jVPNbJOPkDSOv1M9xE9gA7eqoTFhSdZQD4qt7SNgcYJYLu77i/2peyYkaoM+E 1CklaeTAYSmTwv8Km5dGKi7C0WCfutgqDI2sGNHRkPp+sdYNdJ963Ikhl8N6lfTWY03r Jc0A1tNgoUDHR5FiwBDbsHV8XCGmQ1kAqZSfPSuC5DautA1wUknPrMKD4FBEPRXJM1cI zDV47+hqam9u7nttpZOONvGIAgOZpyS9w/3N1M0W8GoFsxZc0Tu51VmRu5U30nbsUdLv KRfdPlHe57ebRvFOgibo1B8s53lcdE03PGn3ebRw8zfKd/8XLAR3q4VID38yQeldPd39 cHzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684290085; x=1686882085; h=content-transfer-encoding:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=icy+EhZ3UBgzX+LkBKZmwy9I7D5yLVOL3MyBuUnW0II=; b=XPQo3oqMpWTbHXovhi3glHSugi88rkr3urLTmsKo6VyS5vPVdth+Gm4bwHKNrc9Fd7 27xNDpCfN7F1cC1B/0MO8dBo9BtO3icpdvxl+GqGYa8sjmOtVdMd0YWIXAOiK7jB0Af/ 4t5UL+wkDUNr8WUjs/I8+kua+OB3EE+d3GWhqI4WqAGZIhJZQRdaZp85ixP+Q7fhO0KC nnQ4xnmbVb1lIe9FgXLq/Z0q8gELz7+OokXlSaeU25LDMp+nEJ0pRI9FBpUaZXNSy6pN i9KML0FsLV5/C5lLrSs+sDjLGYP+b2iOg3l2Op8FFAvEU1JNZ0QXaGB3wcGl5DAvhDXa yO5w== X-Gm-Message-State: AC+VfDxg5QLrKh29veJ7APTQLvasJVjpU2zYspa8EHdkyruA3WLroHor aZOJ7aHokaL9b5gbMwraT2QO70k= X-Google-Smtp-Source: ACHHUZ6HltqTMi815u6+g2acsl5ZG5UZd+Y+2Rbyffv/trwosDfbc8rLxY0NJ38GbW8keQ8pFoFL1ds= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2d3:205:b3a7:7c59:b96b:adaa]) (user=pcc job=sendgmr) by 2002:a5b:309:0:b0:ba6:a54d:1cae with SMTP id j9-20020a5b0309000000b00ba6a54d1caemr8869621ybp.0.1684290085477; Tue, 16 May 2023 19:21:25 -0700 (PDT) Date: Tue, 16 May 2023 19:21:11 -0700 In-Reply-To: <20230517022115.3033604-1-pcc@google.com> Message-Id: <20230517022115.3033604-2-pcc@google.com> Mime-Version: 1.0 References: <20230517022115.3033604-1-pcc@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog Subject: [PATCH v3 1/3] mm: Call arch_swap_restore() from do_swap_page() From: Peter Collingbourne To: Catalin Marinas Cc: Peter Collingbourne , " =?utf-8?b?UXVuLXdlaSBMaW4gKA==?= =?utf-8?b?5p6X576k5bS0KQ==?= " , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, "surenb@google.com" , "david@redhat.com" , " =?utf-8?b?Q2hpbndlbiBDaGFuZyAo?= =?utf-8?b?5by16Yym5paHKQ==?= " , "kasan-dev@googlegroups.com" , " =?utf-8?b?S3Vhbi1ZaW5nIExlZSAo5p2O5Yag56mOKQ==?= " , " =?utf-8?b?Q2FzcGVyIExpICjmnY7kuK3mpq4p?= " , "gregkh@linuxfoundation.org" , vincenzo.frascino@arm.com, Alexandru Elisei , will@kernel.org, eugenis@google.com, Steven Price , stable@vger.kernel.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230516_192127_549714_85128F9B X-CRM114-Status: GOOD ( 10.72 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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. Fix it by adding a call to the arch_swap_restore() hook before the call to swap_free(). Signed-off-by: Peter Collingbourne Link: https://linux-review.googlesource.com/id/I6470efa669e8bd2f841049b8c61020c510678965 Cc: # 6.1 Fixes: c145e0b47c77 ("mm: streamline COW logic in do_swap_page()") Reported-by: Qun-wei Lin (林群崴) Closes: https://lore.kernel.org/all/5050805753ac469e8d727c797c2218a9d780d434.camel@mediatek.com/ Acked-by: "Huang, Ying" Acked-by: David Hildenbrand Reviewed-by: Steven Price Acked-by: Catalin Marinas --- v2: - Call arch_swap_restore() directly instead of via arch_do_swap_page() mm/memory.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mm/memory.c b/mm/memory.c index f69fbc251198..fc25764016b3 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -3932,6 +3932,13 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) } } + /* + * Some architectures may have to restore extra metadata to the page + * when reading from swap. This metadata may be indexed by swap entry + * so this must be called before swap_free(). + */ + arch_swap_restore(entry, folio); + /* * Remove the swap entry and conditionally try to free up the swapcache. * We're already holding a reference on the page but haven't mapped it From patchwork Wed May 17 02:21:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 13244077 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 2C578C77B7A for ; Wed, 17 May 2023 02:22:04 +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:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=gc+YVDMflBHvPcaXnm9DlVfMKmB0Nxr7jZAV66Oq9Fo=; b=ke0RAb+EiMLET9NiDTnDcQWnF8 d57+pb4BUnwulKp8gErT1oB308oc8wnkhzY3XstWu9282DAq0TBIvd4wI8NeYkSO/AH5va4mj1VBt OJ9ErYP8nUPYbyuXMbX3xt0r7+L3opyEHnu38bG4NIEJgMS2heddNgHlR8txwS2d4DDefVBkFPBgt tUKBDY4bUwHXTvA6PBfWKJXnIx/WrUMbBctjUZ+JjR524ZNWXtoWt6cY95hKAnFxd9lq9KvH5A6MJ /D+CcjBBUdx08Wa1BlYLj/L8+WPqxqD0/SG423jvtcdSgkyo3dj39lNkniEAcz6Vye9Gc+GKOEk+Y AKYDEcHA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pz6nM-007oN3-31; Wed, 17 May 2023 02:21:40 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pz6nI-007oLG-2M for linux-arm-kernel@bombadil.infradead.org; Wed, 17 May 2023 02:21:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:Cc:To:From:Subject: References:Mime-Version:Message-Id:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=DmZ7qJu2QJbmubDEgRMJhswdry7Qhukww11ZPOJRgvs=; b=hFtI8STOOhMnK8MeBragv6P7wD HepdtQYM6nMPiPomJr9PEhWqn8ijOQVWHWzkLFqg8LvSrqhgHO92DqViMFbvuwoysPhIYAw9Zmv6L un7KuL79rwemsS9vLEAOg//PfywXGaO45fjPCEPc2D2J35wPHXmp07BJ5cR632CTL5JJRCqZgrRRP 8ox6AYs/gSIS0e/eyNt7J1H05cX9hPZnMlnXI5T3eaYqC2rojf51iTWuukjbVZ2wwl5OlFHI0QAKG D36RJ5zfuKPiGwcxAViA1ZicP2kOUa+d9xmtlDIVzeOgxAewr4Nv6Pd9ulfVB/ptqLO9KIqpKf+08 RGkD+y0g==; Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pz6nD-00CqdI-2w for linux-arm-kernel@lists.infradead.org; Wed, 17 May 2023 02:21:35 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-ba8217b3d30so145544276.2 for ; Tue, 16 May 2023 19:21:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684290087; x=1686882087; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=DmZ7qJu2QJbmubDEgRMJhswdry7Qhukww11ZPOJRgvs=; b=pXVJppsksfHh4I2n3d8xVcufhKZZlJjnCny+rmQ4mjQKI0AW/cCK2hASK83KfSiYpL SwH5TVvYpvNqffEsNvDPgC+nhgLYFM3KaqHCuYbPaIFzls2lqfx0JYU50+6NVWzrME9W 1mfRz5hwNYEvKHUFnlT4dknNheNNzedfdZLSkBZlu7QlWmiIOdq/l+RcVRDobfHZBP0c 7otPYVh11OT4odfN5rNmMB6Xfe/sUmOnKNfozY8i+NNWwTSKo6jWbGtluyHsGeqrt/Um gaSjHg7Sh7LXhrO/UUQMLAjulMUlLvxOhNHHmrBn6+ZKABwqhlZs4QhdwbdE4TaSFaoL o8HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684290087; x=1686882087; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DmZ7qJu2QJbmubDEgRMJhswdry7Qhukww11ZPOJRgvs=; b=Jvx+4tfsguhs1rS48sJM9MQbkRUPvV3K+XoXdtmgMnuMOv2L3gxDIMQRlbnmVMrim4 f31VcypZN8DI2yANtAH+LSyIiTGudVoS5070Jj55hqGkNi041jaJaIpJBcKtioswD5pq HvTd8qIRL6VWTS7bl11jd+T/Kf90xd4QwaSB1L3tF7Vs0aPK3jX7Q1iJTa9fYmhY/v64 RL/4z631OJmdrlPlbo2oSme1pTDk9daKnj55P2KeGFaDFr7pcdqcv2lsVrWmqmwiGoII Q9q0WgvQzXCH6LBoUeH2p/UD+YUztKrzD8luVE6lOBRYA/Maeq/BECnhjDPtXjLLRZeD EhvA== X-Gm-Message-State: AC+VfDzqucJNM9xLlayWR8YotvVRBeSPBRe9B9QPw/v+eDgu9SGRryp3 Rgq1Q3swyuv+BtuMnWmbUAA4P/A= X-Google-Smtp-Source: ACHHUZ5CS7mk6mAM2fYvrtpJa+GCdOe26eXVwKAHAWQiAcvcFXtzYZLVtM0rm9Ojto/ISeUmGRcWqts= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2d3:205:b3a7:7c59:b96b:adaa]) (user=pcc job=sendgmr) by 2002:a25:8407:0:b0:ba8:4b22:4e8a with SMTP id u7-20020a258407000000b00ba84b224e8amr1065312ybk.0.1684290087741; Tue, 16 May 2023 19:21:27 -0700 (PDT) Date: Tue, 16 May 2023 19:21:12 -0700 In-Reply-To: <20230517022115.3033604-1-pcc@google.com> Message-Id: <20230517022115.3033604-3-pcc@google.com> Mime-Version: 1.0 References: <20230517022115.3033604-1-pcc@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog Subject: [PATCH v3 2/3] mm: Call arch_swap_restore() from unuse_pte() From: Peter Collingbourne To: Catalin Marinas Cc: Peter Collingbourne , " =?utf-8?b?UXVuLXdlaSBMaW4gKA==?= =?utf-8?b?5p6X576k5bS0KQ==?= " , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, "surenb@google.com" , "david@redhat.com" , " =?utf-8?b?Q2hpbndlbiBDaGFuZyAo?= =?utf-8?b?5by16Yym5paHKQ==?= " , "kasan-dev@googlegroups.com" , " =?utf-8?b?S3Vhbi1ZaW5nIExlZSAo5p2O5Yag56mOKQ==?= " , " =?utf-8?b?Q2FzcGVyIExpICjmnY7kuK3mpq4p?= " , "gregkh@linuxfoundation.org" , vincenzo.frascino@arm.com, Alexandru Elisei , will@kernel.org, eugenis@google.com, Steven Price X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230517_032132_928071_BDF26BD9 X-CRM114-Status: GOOD ( 10.33 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org We would like to move away from requiring architectures to restore metadata from swap in the set_pte_at() implementation, as this is not only error-prone but adds complexity to the arch-specific code. This requires us to call arch_swap_restore() before calling swap_free() whenever pages are restored from swap. We are currently doing so everywhere except in unuse_pte(); do so there as well. Signed-off-by: Peter Collingbourne Link: https://linux-review.googlesource.com/id/I68276653e612d64cde271ce1b5a99ae05d6bbc4f Acked-by: David Hildenbrand Reviewed-by: Steven Price Acked-by: Catalin Marinas --- mm/swapfile.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mm/swapfile.c b/mm/swapfile.c index 274bbf797480..e9843fadecd6 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -1794,6 +1794,13 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, goto setpte; } + /* + * Some architectures may have to restore extra metadata to the page + * when reading from swap. This metadata may be indexed by swap entry + * so this must be called before swap_free(). + */ + arch_swap_restore(entry, page_folio(page)); + /* See do_swap_page() */ BUG_ON(!PageAnon(page) && PageMappedToDisk(page)); BUG_ON(PageAnon(page) && PageAnonExclusive(page)); From patchwork Wed May 17 02:21:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 13244076 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 2F77DC7EE24 for ; Wed, 17 May 2023 02:21:59 +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:Cc:To:From:Subject:References: Mime-Version:Message-Id:In-Reply-To:Date:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=j6Yj6mcmZV4wEMN6iKocrJmdO+j+/6ce+OlY8R6gXAQ=; b=sqUkCVL7d0K6JFSdn2a7Aa+DUy Ettp+VIVMVHX5q5mMemrGBUvAk817blZbqgxTm04OkObhLWi9i1NA57RKFscskWYRrRJMVMUJKyU+ h0NitLL1xqrjNp4aOeiveUGci2H6AM5MAzN7WK+sdrJrPedRrnGZogtSwwFO5Em2XLENcizw6t/uB lLJTPBvb//qc03uztZL5IWIdm28rYc7sU4YqWhcIZ20PyVXaD+iAVyuPf6W2/6WyJ7dFbLb+eGQ5b 7eWXmShZX6oT3QArzCFEoD/cmOiIFgfni0AzZ2YpJ38GwQCfdn0Fnsoqg8rbVU8x25gUyZfKPWZkM Ihdvau8w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pz6nM-007oMf-1K; Wed, 17 May 2023 02:21:40 +0000 Received: from mail-yw1-x1149.google.com ([2607:f8b0:4864:20::1149]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pz6nE-007oIV-1j for linux-arm-kernel@lists.infradead.org; Wed, 17 May 2023 02:21:34 +0000 Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5611211f767so2888557b3.0 for ; Tue, 16 May 2023 19:21:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684290090; x=1686882090; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=rrlZpn5lxQz0wYWidXhNS5k4qYdBDD7AOOGwfPiDsdQ=; b=nS7ih6tlhFuvsMZlYwq74HbaDFXcOaTcEpDv9JgzLfbOcJeYHe55FxK9/JT/+cQbri Mn6JAGOT1VNNKqihdxteunOmx5PruJg2/fGmmxtN3oJJTqr4S4WMMQ0nGGzH9yNCitD0 Kqebqjin+DuMmuu1ErU226+1fL8do4fC3XsOIDYwtzvXNYfy2fVo4kDBR9Dx2K31khL6 UbHaRwmvEWWPO7TJUQ+CmLPQ8R7iMGPQ8aPuRy4ci2qpyyMDXCg6i23WhA35RidSAMDh 2IbzmeSuiKtMSP2SW/XYy9SByGadBaD56U2heVCkkevaE6uaQskw46S6Abvm/vT+UB+O 0DLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684290090; x=1686882090; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rrlZpn5lxQz0wYWidXhNS5k4qYdBDD7AOOGwfPiDsdQ=; b=A1f/7dslkfyat7CvlNUolWp7VPB4S0qIAPCch+iYwtUJB+R9HY4mIce9Rxfv6j1Kwn 8dzFJRBJ5FgWyM0q24q0rU3iH6u6XYHnaHNYceqxs2QCLbDnHqU7XFtT4lioesYkHKBY X7u8GQLbRRGYejhHp4kd/DykjvopI9M42Rp+e1NAruowZdwLVVz+h2moyg5KZq26gCIS 2yVeuCyyt4OmObAFGLOAn28JtJGbeoA5aK6BZNBkoSHY8+C0W6iX1VT70e4bkOdOvUue ACbeQVX5tqXjGH2kHJ0btRMfiSVJWAAKcrh7UGuaAnXLOe8X/7brQQmJdNKje+ktZN2y Z1Ag== X-Gm-Message-State: AC+VfDyQZ/ITm6P3BalkZV/CKjdv7Va1QLSgIb/MLjGBGk+dphXuLRHx VURVJTdwakNAsH8TK36NXt7QSlk= X-Google-Smtp-Source: ACHHUZ7bLZybv9LVeL4X5M5xX1h96wUcd50pNUsrXoFbzJBGVtFUeHzy5iTZQluDbjMVvEYQTCD10jE= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2d3:205:b3a7:7c59:b96b:adaa]) (user=pcc job=sendgmr) by 2002:a81:ae54:0:b0:559:f89f:bc81 with SMTP id g20-20020a81ae54000000b00559f89fbc81mr23166501ywk.6.1684290089941; Tue, 16 May 2023 19:21:29 -0700 (PDT) Date: Tue, 16 May 2023 19:21:13 -0700 In-Reply-To: <20230517022115.3033604-1-pcc@google.com> Message-Id: <20230517022115.3033604-4-pcc@google.com> Mime-Version: 1.0 References: <20230517022115.3033604-1-pcc@google.com> X-Mailer: git-send-email 2.40.1.606.ga4b1b128d6-goog Subject: [PATCH v3 3/3] arm64: mte: Simplify swap tag restoration logic From: Peter Collingbourne To: Catalin Marinas Cc: Peter Collingbourne , " =?utf-8?b?UXVuLXdlaSBMaW4gKA==?= =?utf-8?b?5p6X576k5bS0KQ==?= " , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, "surenb@google.com" , "david@redhat.com" , " =?utf-8?b?Q2hpbndlbiBDaGFuZyAo?= =?utf-8?b?5by16Yym5paHKQ==?= " , "kasan-dev@googlegroups.com" , " =?utf-8?b?S3Vhbi1ZaW5nIExlZSAo5p2O5Yag56mOKQ==?= " , " =?utf-8?b?Q2FzcGVyIExpICjmnY7kuK3mpq4p?= " , "gregkh@linuxfoundation.org" , vincenzo.frascino@arm.com, Alexandru Elisei , will@kernel.org, eugenis@google.com, Steven Price X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230516_192132_579660_5524330F X-CRM114-Status: GOOD ( 21.30 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org As a result of the previous two patches, there are no circumstances in which a swapped-in page is installed in a page table without first having arch_swap_restore() called on it. Therefore, we no longer need the logic in set_pte_at() that restores the tags, so remove it. Because we can now rely on the page being locked, we no longer need to handle the case where a page is having its tags restored by multiple tasks concurrently, so we can slightly simplify the logic in mte_restore_tags(). Signed-off-by: Peter Collingbourne Link: https://linux-review.googlesource.com/id/I8ad54476f3b2d0144ccd8ce0c1d7a2963e5ff6f3 Reviewed-by: Steven Price Reviewed-by: Catalin Marinas --- v3: - Rebased onto arm64/for-next/fixes, which already has a fix for the issue previously tagged, therefore removed Fixes: tag arch/arm64/include/asm/mte.h | 4 ++-- arch/arm64/include/asm/pgtable.h | 14 ++---------- arch/arm64/kernel/mte.c | 37 ++++++-------------------------- arch/arm64/mm/mteswap.c | 7 +++--- 4 files changed, 14 insertions(+), 48 deletions(-) diff --git a/arch/arm64/include/asm/mte.h b/arch/arm64/include/asm/mte.h index c028afb1cd0b..4cedbaa16f41 100644 --- a/arch/arm64/include/asm/mte.h +++ b/arch/arm64/include/asm/mte.h @@ -90,7 +90,7 @@ static inline bool try_page_mte_tagging(struct page *page) } void mte_zero_clear_page_tags(void *addr); -void mte_sync_tags(pte_t old_pte, pte_t pte); +void mte_sync_tags(pte_t pte); void mte_copy_page_tags(void *kto, const void *kfrom); void mte_thread_init_user(void); void mte_thread_switch(struct task_struct *next); @@ -122,7 +122,7 @@ static inline bool try_page_mte_tagging(struct page *page) static inline void mte_zero_clear_page_tags(void *addr) { } -static inline void mte_sync_tags(pte_t old_pte, pte_t pte) +static inline void mte_sync_tags(pte_t pte) { } static inline void mte_copy_page_tags(void *kto, const void *kfrom) diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pgtable.h index 0bd18de9fd97..e8a252e62b12 100644 --- a/arch/arm64/include/asm/pgtable.h +++ b/arch/arm64/include/asm/pgtable.h @@ -337,18 +337,8 @@ static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr, * don't expose tags (instruction fetches don't check tags). */ if (system_supports_mte() && pte_access_permitted(pte, false) && - !pte_special(pte)) { - pte_t old_pte = READ_ONCE(*ptep); - /* - * We only need to synchronise if the new PTE has tags enabled - * or if swapping in (in which case another mapping may have - * set tags in the past even if this PTE isn't tagged). - * (!pte_none() && !pte_present()) is an open coded version of - * is_swap_pte() - */ - if (pte_tagged(pte) || (!pte_none(old_pte) && !pte_present(old_pte))) - mte_sync_tags(old_pte, pte); - } + !pte_special(pte) && pte_tagged(pte)) + mte_sync_tags(pte); __check_safe_pte_update(mm, ptep, pte); diff --git a/arch/arm64/kernel/mte.c b/arch/arm64/kernel/mte.c index 7e89968bd282..c40728046fed 100644 --- a/arch/arm64/kernel/mte.c +++ b/arch/arm64/kernel/mte.c @@ -35,41 +35,18 @@ DEFINE_STATIC_KEY_FALSE(mte_async_or_asymm_mode); EXPORT_SYMBOL_GPL(mte_async_or_asymm_mode); #endif -static void mte_sync_page_tags(struct page *page, pte_t old_pte, - bool check_swap, bool pte_is_tagged) -{ - if (check_swap && is_swap_pte(old_pte)) { - swp_entry_t entry = pte_to_swp_entry(old_pte); - - if (!non_swap_entry(entry)) - mte_restore_tags(entry, page); - } - - if (!pte_is_tagged) - return; - - if (try_page_mte_tagging(page)) { - mte_clear_page_tags(page_address(page)); - set_page_mte_tagged(page); - } -} - -void mte_sync_tags(pte_t old_pte, pte_t pte) +void mte_sync_tags(pte_t pte) { struct page *page = pte_page(pte); long i, nr_pages = compound_nr(page); - bool check_swap = nr_pages == 1; - bool pte_is_tagged = pte_tagged(pte); - - /* Early out if there's nothing to do */ - if (!check_swap && !pte_is_tagged) - return; /* if PG_mte_tagged is set, tags have already been initialised */ - for (i = 0; i < nr_pages; i++, page++) - if (!page_mte_tagged(page)) - mte_sync_page_tags(page, old_pte, check_swap, - pte_is_tagged); + for (i = 0; i < nr_pages; i++, page++) { + if (try_page_mte_tagging(page)) { + mte_clear_page_tags(page_address(page)); + set_page_mte_tagged(page); + } + } /* ensure the tags are visible before the PTE is set */ smp_wmb(); diff --git a/arch/arm64/mm/mteswap.c b/arch/arm64/mm/mteswap.c index cd508ba80ab1..3a78bf1b1364 100644 --- a/arch/arm64/mm/mteswap.c +++ b/arch/arm64/mm/mteswap.c @@ -53,10 +53,9 @@ void mte_restore_tags(swp_entry_t entry, struct page *page) if (!tags) return; - if (try_page_mte_tagging(page)) { - mte_restore_page_tags(page_address(page), tags); - set_page_mte_tagged(page); - } + WARN_ON_ONCE(!try_page_mte_tagging(page)); + mte_restore_page_tags(page_address(page), tags); + set_page_mte_tagged(page); } void mte_invalidate_tags(int type, pgoff_t offset)