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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06E56C6FD1D for ; Thu, 30 Mar 2023 17:36:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DCC86B0071; Thu, 30 Mar 2023 13:36:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 48D836B0072; Thu, 30 Mar 2023 13:36:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 354806B0074; Thu, 30 Mar 2023 13:36:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 253B56B0071 for ; Thu, 30 Mar 2023 13:36:18 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id EC8AFAC785 for ; Thu, 30 Mar 2023 17:36:17 +0000 (UTC) X-FDA: 80626268394.20.507E68C Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf21.hostedemail.com (Postfix) with ESMTP id E02B11C0014 for ; Thu, 30 Mar 2023 17:36:15 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf21.hostedemail.com: domain of cmarinas@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=cmarinas@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680197776; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FBtNuWw+wF4D9IE00grL+pO/d/DypJsFApa227hRub0=; b=Dx+jzG1/6/5UyFZUDv1HtA2iEe8PzEfhHXQdNdabzps0nDcUY8REY5LTdksGQoGsccIjZ/ lY6gsAT3vkErpo00OPvIK09TbagkTEPa1gg+aF2oZOBt6NmhwPa04UosbuZNbKYRBJrX0p hQADWxv9IUydhsCCggMraQcOlp9u4Os= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=arm.com (policy=none); spf=pass (imf21.hostedemail.com: domain of cmarinas@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=cmarinas@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680197776; a=rsa-sha256; cv=none; b=oy28Xk1tPMgqqdACJBXP9dyHJHeNel8tk9gcMMkYkXuAVTgxgFad5WBUwtbdCxXvTFwAVm cQBvp7nBW9ofjJbekajgfsxtLOS2aHRTai9g1g2zZYBq+uAp//h0pLW/GVbHtO7zUVyHY6 ME1U2V94A1PcPS5KGmGIKvnjTwyCAWU= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 16125B8233F; Thu, 30 Mar 2023 17:36:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id D232BC433EF; Thu, 30 Mar 2023 17:36:10 +0000 (UTC) Date: Thu, 30 Mar 2023 18:36:08 +0100 From: Catalin Marinas To: Steven Price Cc: 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" , "david@redhat.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" Subject: Re: [BUG] Usersapce MTE error with allocation tag 0 when low on memory Message-ID: References: <5050805753ac469e8d727c797c2218a9d780d434.camel@mediatek.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: E02B11C0014 X-Stat-Signature: jpbmbag35y5z3jahpcnxhzon9brxziyn X-HE-Tag: 1680197775-405279 X-HE-Meta: U2FsdGVkX19vvfKO4AHbHoBZBBH6iLfYiTq4OGTtlYrk79Pjx+ZXMUvOfgRcsxpFz0mOw4jnZQjyefmjjnTdJbKpbcDBswBUPVEtgKifrnuadl8aMeQWuMCGx5c3cFCKWkC//wfuyrzn1N8XfBzQStkHbn9zvoyrF0fVnG3osjYjbi5nizSP7ACrdeyyU2AVLkFOgKIIYLis2pACU9YCzeYalcisNacRGmgrm4wnycskG9q5P+eObLcLxj+0WGCzwp+BimKyBZ3A3cv+oyXzQsMvT7hDYCOUAqzrMfnwXw2XNARHrHt2H8pKs/E1x6jHURNaMH/U23de+w5is6v8/JQtO7FPt0mFA8OFuLB3nJvuxUc64wAwuJMarW3hzRDUBeoZKcKjm6lVc/OAre0egJDWqpePyy8fYKAFt20ZUdYxxM+xCFcyWvmGietKDgYKiOc7DnHEiZFar0xsYcSNKcpsyXui3RhwF7tQVYoSsMuCPjNs30etmwPcv+aynxLWKQ/ADcfANODqNdL886rJ/AroSohReX872XWhvlQqo4FiUYgoT3gdCP7II6VzpoZvm1CNB29CfRYIaiSQZYdVop3feWYU2q7Bz2sk7PPYnfVZEch6ETA/z7JerD/FGY/6VejiwvYOV5F8ZNk66JkDSE/ArT4NkYOI6ZyfzU1YhamopPf7lG6qWUWxcKYWypdn8mMr0Rl2pL0qdUF3FL1Hc+bEEoqlXvo8vcE2WNK02SuLTnaaJ465geXH5Y/pr0OzHvVDxgMXt08sEngNILaplbC9uIb3obH9w3Q6nTIGYmm1y9O2QPomi+LZVJVWSpJjQmPtFMzqnhSfiFR7gacufwjfE3uk+7cx2lS4EQAsq/ZS+ZIq/tPbXhYvF1GIRDXwmMeMe6XuZ4/r+dfKs5J5iuduHWa6cQ9tOn/7HU369vm0Wv/BKOwscSVWz+SuGrU90ZpB+KHpV+QgwvoOzsG CzK03xVn SYtaUOK69CVBqgTWyQYpsSR0lMV1S6r9+H/UQYBUXiC7kJTOMPKOHxpHwGAxBRZ0b1migAV/BPdfQvsIfj6MjjNUyILrmZEZbhVri9QjrMEyn5BXC1+WDVzyJCiynpotgeJzbfKyrhBilkqpp2djUs3hQMlKYeWu3Mk7PbqQ/e9/52McTPlzlksVMjW1QspC7hgT12uQIu/C0Ikr+SXiR3AECujXSW7xHmz6EdWI76qGTdimucEu/p8U67mv6EIf96oC6k3gW9oMym+rcgbrjorFHjBz/mKex5MjtzYtac+YVhNV4slfLGUR59Pjwpnca/0pvAb5JX91eehXj+QPk68kXqZZiY55AszkHnGZ/hGUMO5Buakb3QNBcMw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Mar 30, 2023 at 02:56:50PM +0100, Steven Price wrote: > > On Wed, Mar 29, 2023 at 02:55:49AM +0000, Qun-wei Lin (林群崴) wrote: > >> Having compared the differences between Kernel-5.15 and Kernel-6.1, > >> We found the order of swap_free() and set_pte_at() is changed in > >> do_swap_page(). > >> > >> When fault in, do_swap_page() will call swap_free() first: > >> do_swap_page() -> swap_free() -> __swap_entry_free() -> > >> free_swap_slot() -> swapcache_free_entries() -> swap_entry_free() -> > >> swap_range_free() -> arch_swap_invalidate_page() -> > >> mte_invalidate_tags_area() -> mte_invalidate_tags() -> xa_erase() > >> > >> and then call set_pte_at(): > >> do_swap_page() -> set_pte_at() -> __set_pte_at() -> mte_sync_tags() -> > >> mte_sync_page_tags() -> mte_restore_tags() -> xa_load() > >> > >> This means that the swap slot is invalidated before pte mapping, and > >> this will cause the mte tag in XArray to be released before tag > >> restore. > > This analysis looks correct to me. The MTE swap code works on the > assumption that the set_pte_at() will restore the tags to the page > before the swap entry is removed. The reordering which has happened > since has broken this assumption and as you observed can cause the tags > to be unavailable by the time set_pte_at() is called. > > >> After I moved swap_free() to the next line of set_pte_at(), the problem > >> is disappeared. > >> > >> We suspect that the following patches, which have changed the order, do > >> not consider the mte tag restoring in page fault flow: > >> https://lore.kernel.org/all/20220131162940.210846-5-david@redhat.com/ > > I'm not sure I entirely follow the reasoning in this patch, so I'm not > sure whether it's safe to just move swap_free() down to below > set_pte_at() or if that reintroduces the information leak. > > I also wonder if sparc has a similar issue as the arch_do_swap() > callback is located next to set_pte_at(). SPARC has a potential race here since the page is made visible to the user but the tags are not restored yet (I raised this before). But even ignoring this small window, arch_do_swap() needs to have the metadata available. > >> Any suggestion is appreciated. > > The other possibility is to add a(nother) callback for MTE in > arch_do_swap() that calls mte_restore_tags() on the page before the > swap_free() call rather than depending on the hook in set_pte_at(). I think we should move arch_do_swap_page() earlier before swap_free() and in arm64 we copy the tags to pte_page(pte). I don't think SPARC would have any issues with this change (and it also fixes their race). -- Catalin