From patchwork Tue May 23 00:43:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 13251310 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 F0DFAC77B73 for ; Tue, 23 May 2023 00:44:19 +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=rFSJPhWvkatH0kAMHxefMg5MzDLm385q6joqSRwPBzc=; b=epaO1UpZsEgSN3yvVsbUyBWti9 j5/0tAHohFCg6SBD5Nu0fvPREK3G1o8WAhl6Qe5bje7NWLyrlW/SHwLyHhBoYWaYxn6XuwLxAYyTu MRuDb6APNLUtTjwJFMwA72L/x1NMIZmlgGsiqTXORmNxdMlv+pdGiGBNjgfbtt0HF8dtIBj7ecwn/ xKgsa9qO7C5aZZlZYzHWy/+03kUrWUK1Wvgb+Y9URQ5FT9D3EzeuRkTz3u1GqF4yaQ/Pn5pG0CMT5 AQzLakwpRREVrkGEfeNu7cLpH8yc98UJ3CW8vEBVHfJuTHq6zXgLkzN2PV1Gopj3xTZaUwVqg19n/ tF53cCiw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q1G7c-008VCE-1t; Tue, 23 May 2023 00:43:28 +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 1q1G7V-008V89-1q for linux-arm-kernel@lists.infradead.org; Tue, 23 May 2023 00:43:22 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-babb7aaa605so5322207276.3 for ; Mon, 22 May 2023 17:43:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684802597; x=1687394597; 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=msTm3oYdeXIT0hSlyDwQQkvc5wY+Q8yhko13bmJatr8=; b=GVCKc+6rFVLitBZRgjiG8Xp6MHuRDVhZtsHNX/VKvhjNUXI2g+oF9FkWUEYw2KzSxD jp4IhTdFiglLXHuJvOecRUNblTjQqoij5ls5a85sMq/WccA2cWXYiEuTGZE2QPc25z1P I8fzTiWeaDqvG0LuwqBGkTuunNP3dF5yHaQZQWU03AGRfLddIWVSlV0GDW2IfHKUwGIa kJWuZ1I6IkldXrYkOwPS0n6CmraBvOXwhnVIajQXxhcVGPXShTLQ3KiXtaS4Kg9Pbdet qc17JRlFojhHHmnUA1aiDUv9PQDzUO/0c5Sa9DD8d5/h5mOmX7eaqslAsS0IaFcRV2+j ohtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684802597; x=1687394597; 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=msTm3oYdeXIT0hSlyDwQQkvc5wY+Q8yhko13bmJatr8=; b=lhjcZZ+usCCjvhKcM8inHL7GsotIDtC/Tw3dSVnghhTIXYOoobNtvdO2cOprLUWila osdooEmmXjs5P3pBlxqyfuBco7zxCS1LbvyCy8Em3Ov11OyEDbTFUGLej+57Bl8N0Rlk Ubsy7rWSKeDDoG3Ll/jLE7jHC/7rT3maHqM5ie5lC+XB/Mm9PDJqJE0WwnK6qFEIXX56 r5uwCR+39+rLYsYjqXA4IYixSxofXPMpWqpqaA2Q6hjii61WkcorFzlrJdcWvHo1P3Jp JdpSar7HMXTEwfYZ0j6/KYIWG+YNOlLTLD3eTJ9aonCfQ6VGnnkijxwzGXuRRSZwPp2i 9apA== X-Gm-Message-State: AC+VfDzcyNKP3FuFf7kGVEA4ddeB1v6gw2mOuYBsEDlDBpMwKpQyoLMo PyrSA4W5zTbTfnNV4TsDJTFnjeg= X-Google-Smtp-Source: ACHHUZ6kHjMTCHPdYe9Jo2a981mU1PnCLbxJJT05Im440Cs+gDDAq2rqaJTbIgDZ3JZeNb3/fmzcm8U= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2d3:205:3d33:90fe:6f02:afdd]) (user=pcc job=sendgmr) by 2002:a25:10d4:0:b0:ba8:181b:2558 with SMTP id 203-20020a2510d4000000b00ba8181b2558mr7332911ybq.4.1684802597761; Mon, 22 May 2023 17:43:17 -0700 (PDT) Date: Mon, 22 May 2023 17:43:08 -0700 In-Reply-To: <20230523004312.1807357-1-pcc@google.com> Message-Id: <20230523004312.1807357-2-pcc@google.com> Mime-Version: 1.0 References: <20230523004312.1807357-1-pcc@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Subject: [PATCH v4 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-20230522_174321_609351_517494CE X-CRM114-Status: GOOD ( 11.24 ) 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: David Hildenbrand Acked-by: "Huang, Ying" 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 Tue May 23 00:43:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 13251312 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 00324C7EE2A for ; Tue, 23 May 2023 00:44:23 +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=MeVSuMWNMA8OHYrr15m834Rj6dFyXohLr6YPiKJDB+4=; b=yXjXwJi8zmhRxng/9vyHuwyUG9 sPBUWLwvggafEoDAQT3SYEAcHUmy7klshbb5B1J6p5WL3kKTRicXc0xBH8gYoc9Yy/l/Lb8NXwLyq Dd26bKdBJhXBddrZWc8wUks3gCD5SiYaUlyYeUubf8lfB/zcdcaZRg+f+svr9A0d/Wo2wO34rXMB0 kOhDEwHbZLqNA3esBdlJ1trDi4DBE5kQwBW2zth7qZXAadOAlV1aHhLsXqkV/C4bdVmjr4B9DdWa4 fTdVmeKaVpSAjuTiWOFCe/h5oIw8CM4SCTUNXjKrlhLCxnTmbYHBIRXPUtTgoLmlOto+sSP9Euhwt gjkiJwIA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q1G7d-008VCb-0B; Tue, 23 May 2023 00:43:29 +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 1q1G7Y-008V8d-2m for linux-arm-kernel@lists.infradead.org; Tue, 23 May 2023 00:43:26 +0000 Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-ba8b0fc0d35so9301945276.0 for ; Mon, 22 May 2023 17:43:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684802600; x=1687394600; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bX87CylYnN+5c55ItUvtBMTDdL52LhDBMQG45ADuUIs=; b=bdogDu/LWKIXK0xhj/mHEXmPSLZDQ4wTkCFpl5MS9xXgMH/lrIKDBgMm2OVW0088Ob UUXOUas2bD7bxDVYmP+pP6qAeeNNcNNzpyrJngzal1hujSvkuKEu7+oTZMvffng5FsBp gwlthK4wI9AiO+Fdzy5ned4Ve4n2qyJlWIgHIhgxohsY1Qw8A0XJY+vCXRNI9cCOvTZk GipdqA6ZJmCkCppBtgmsiSQUIm6BRaEmUEy2YBmPt0Mrmuq/dUUS0r0t46hxsqR6HbX9 qaAwcxK6iWLJ/y3GzsvNuhSXXmpx/eIa9uHJFpCvIlk4HbNQJ1mX497oATiIBKnqBGwO uixA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684802600; x=1687394600; 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=bX87CylYnN+5c55ItUvtBMTDdL52LhDBMQG45ADuUIs=; b=Eqyjp5TLsSg0iHNyuGKQgMFU6/IXfVCknu9SBfdKyqeR/mZpfn0hf41JovUjJVzthJ mV0D94ctqW8KOrqaAjWeiOgepD1H/cyZMG9VBF++WrOgJWQc4Nmj2VXyA7UrmGmI41L1 Xr0GeOBSVZmRmOQZYgr7vdSfryRI3U1Hp9GmhiX2Djv3k3VBoiE+qcZ7/ri+NoC0QV9l FHEVaXx4o2HZYZYK+sEsrj5PBvJOxo04770G7f9vbmvfCuxilJr3G2GW8g12z1uIfoJ6 MGTL3ioYzet4A4r5i/Es6oJu2pFpSrX31eYxYBbKk3sZ2BLY3qZoW0vXWTz3WhruG2vd 8PgQ== X-Gm-Message-State: AC+VfDx+nkqGIYhB5J+pkHkmGGMXtf82nk/AqD9qPryW+j/oSLtyoQTB sirv6yth1Bt8SOVvIA3IyThzLbE= X-Google-Smtp-Source: ACHHUZ7h2YrRQhx18dwlYMT7aHixdC7I8JgE0ax2zPvocyh8XVLX5phmGOcIHrUfvAqU9QnxAXjxEvs= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2d3:205:3d33:90fe:6f02:afdd]) (user=pcc job=sendgmr) by 2002:a25:d55:0:b0:ba8:9455:85b9 with SMTP id 82-20020a250d55000000b00ba8945585b9mr5274091ybn.4.1684802600447; Mon, 22 May 2023 17:43:20 -0700 (PDT) Date: Mon, 22 May 2023 17:43:09 -0700 In-Reply-To: <20230523004312.1807357-1-pcc@google.com> Message-Id: <20230523004312.1807357-3-pcc@google.com> Mime-Version: 1.0 References: <20230523004312.1807357-1-pcc@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Subject: [PATCH v4 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-20230522_174324_895781_B640E301 X-CRM114-Status: GOOD ( 11.00 ) 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 Suggested-by: David Hildenbrand Acked-by: David Hildenbrand Acked-by: "Huang, Ying" 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 Tue May 23 00:43:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Collingbourne X-Patchwork-Id: 13251311 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 0935CC77B73 for ; Tue, 23 May 2023 00:44:23 +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=VF8mD05OK/9dmniHlRtMmumqOYXM+SsCN8xyympKhmU=; b=P4udac/BHMuJjKP8n4GXqfaq/i AAFU78iB4Kd5fL1SPVhmjVCrLJ26s9hMeVofRqlOEHgYHjgVzZZJiCHKETUlfSLHENZyVISHvmbro OBL7UB/0l3drvg2Au0lauwX+kUSWV3whWIQfNmHK+tsCNLdwj0WAwa12FRWkBWlVRLey7Gc/Hkw3o hopcp41UPm4G/SkXCa5g9BdeYzhSAbpQUF6a+Bv4OtFWOkBLuXag+xLH+3vlYigSDeGq8rU7SNqbH DhnXlhdi/CzP4a8cI0J+CJaCLUAzjvbU7MdWPDDe8tLxs1rBXffiHTYCZGcIuARkCLRhvuKuawNSG OMlDBmTw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q1G7d-008VCz-20; Tue, 23 May 2023 00:43:29 +0000 Received: from mail-yb1-xb4a.google.com ([2607:f8b0:4864:20::b4a]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q1G7Y-008V9v-2x for linux-arm-kernel@lists.infradead.org; Tue, 23 May 2023 00:43:26 +0000 Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-ba81b37d9d2so12214280276.3 for ; Mon, 22 May 2023 17:43:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1684802603; x=1687394603; h=cc:to:from:subject:references:mime-version:message-id:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=EOtAXf5wuxpOrHzLJXv+xvDvcqfWad6caZwbz93V+KE=; b=XY4gva+EMnJWXF+00EDsdNDkxlpkR1226CoKPz6GQhTDY3567XIkv/ajSmvDAyOo+4 Jiou7Fwlc1Fc0otUxmqXU52VFuoJpP67ftFJSgZR71Nr4aEEjXMn3N70kQVwxP2IiBty Ft+9Kcmt21IpAznRpT2tBREMHZy8CkXxRpYN/nRYwK0AQffNTuo3WgTPGwyieVsopPY7 LfJwzSI9v2nyw2s5dDstd4fKEgqImzwUpT4HJZBRIW/+uH9pllKQq/N3OUcDKi+t/fWF afpbXeNQPvaPfbjUxLNGDhhVz87f6a1X4WkEQO6CvDL/WbTGb9WvwVqJATiDfSU5wg40 +GqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684802603; x=1687394603; 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=EOtAXf5wuxpOrHzLJXv+xvDvcqfWad6caZwbz93V+KE=; b=Kio8cpdjtY4m3aXUSKUZy7c6TIeH9p35VysLAGDz8WQC3fLHFeomijZ8IHK4gJud2R XS6o1KRrBO5WyiAim3ifXPDQGCzlqCahi9J8OZcfTsfVq4LT/LtoJqKfd/xJlN07wLQx 8EoUZHQaTkdZ63GSGbiZmMY+emjuTBHhVnkDeBjYkghq7mxb4mNxX97HM4jzo8UjY6tu n2ceJFInnDVgTldtSzgW8IJiOtH4PGNtHay+q9NTwc50ru/zYqFr8usqLMuTULB0EsgJ EF4bZFS11X0f+IqyvP1RAEghO7G9hq8SiuOiC22xMSwKNb87g9HBb/J8dUyqsKOjDemZ 9Gmg== X-Gm-Message-State: AC+VfDyd0cttOzN4/6/oMWLgGV4FhtXi/GdO7Tita0odJdcQJJFyp7zm /fY8+mdhtrErOLY+K4796XXfa2s= X-Google-Smtp-Source: ACHHUZ4LYT/h0488/LNagDCbE/4d9JbfsPfBfwGCaVQAcbHO+6coMUy+IC8/AtpQAjPzk27JRmlqI1E= X-Received: from pcc-desktop.svl.corp.google.com ([2620:15c:2d3:205:3d33:90fe:6f02:afdd]) (user=pcc job=sendgmr) by 2002:a25:fe08:0:b0:ba7:8099:c5f2 with SMTP id k8-20020a25fe08000000b00ba78099c5f2mr5220235ybe.8.1684802603205; Mon, 22 May 2023 17:43:23 -0700 (PDT) Date: Mon, 22 May 2023 17:43:10 -0700 In-Reply-To: <20230523004312.1807357-1-pcc@google.com> Message-Id: <20230523004312.1807357-4-pcc@google.com> Mime-Version: 1.0 References: <20230523004312.1807357-1-pcc@google.com> X-Mailer: git-send-email 2.40.1.698.g37aff9b760-goog Subject: [PATCH v4 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-20230522_174324_958360_2250672F X-CRM114-Status: GOOD ( 20.82 ) 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. Signed-off-by: Peter Collingbourne Link: https://linux-review.googlesource.com/id/I8ad54476f3b2d0144ccd8ce0c1d7a2963e5ff6f3 Reviewed-by: Steven Price Reviewed-by: Catalin Marinas --- v4: - Rebased onto v6.4-rc3 - Reverted change to arch/arm64/mm/mteswap.c; this change was not valid because swapcache pages can have arch_swap_restore() called on them multiple times 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 ++++++-------------------------- 3 files changed, 11 insertions(+), 44 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();