Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2485193rwl; Thu, 30 Mar 2023 10:57:58 -0700 (PDT) X-Google-Smtp-Source: AKy350Y0rH09m7/6HMqVQ/YuhaUzxAPs6Q3ShJMcRf8BpVtL+GM9QxXiyMlKDO5g//pELMKOFK6C X-Received: by 2002:a05:6402:5107:b0:502:ffd:74a0 with SMTP id m7-20020a056402510700b005020ffd74a0mr3567170edd.2.1680199078157; Thu, 30 Mar 2023 10:57:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680199078; cv=none; d=google.com; s=arc-20160816; b=AzHagQbNp6c27qsHh+qjOpVC9AmsQesc3tl+2MXl3EOPSD6tJ5rLWxL6a6Ljp3XPbd kMjloOhDFUHx7RKREoES5b6FFjHwl+VHFXBL8SvpdULh1FWLCimuJAQYPD9a8NTQHX9v BpmZGhn0mzVbq2TZ0/6SqLG0JaQwVmeqPx6hsvul7YGO6oP8+GMUjA3JqUaJzRqqh8Nt B579qfZi8mbgkUnBdMXvZWFaeNvS1XPp8OHru207jgD8Qme6PH5zRI7T5jU0+aI4A8xo vpN37lyQ9QMDVSx+wzXTwBES//Y1PDueVKcz1esDckZTR93uzpd5MbVsUz+jUZgfn8dJ f4cQ== 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-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=FBtNuWw+wF4D9IE00grL+pO/d/DypJsFApa227hRub0=; b=NWDVWWzMpOjP6suCLfAtJ3+dnj+MCEQsiHK3yR0ZaOH+nwv3FMhad2/FtYD0EdHfCv Uj4r93nzNmvCJgdFDCgItCMafSHssucyxkzQjeyTXS8yywMZRLynVTNZ1hAMC1q9KjMt yxYRG7JPjkFaowBgsoxyJ/JFk85uiLopewZ/gSU3rDNwoPhHKKuBFCMmkIuCbIDBrWHk 2Nh4kbI5zDU0DNHq0WfPK4gK9QLzZMvo38LtUOeUuNdg/e0LNBSXYucJ2tIq3qm8Xfg3 zA8hhQxgs3tlTB4dRRubeJq14342inQQn9LZc9Kjh2fFcBczvj1JT0GrDuek+bhA4i8V rhJQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o18-20020aa7dd52000000b005024d8cc847si313665edw.22.2023.03.30.10.57.32; Thu, 30 Mar 2023 10:57:58 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232531AbjC3RgQ (ORCPT + 99 others); Thu, 30 Mar 2023 13:36:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43714 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231867AbjC3RgO (ORCPT ); Thu, 30 Mar 2023 13:36:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC27C6A47 for ; Thu, 30 Mar 2023 10:36:13 -0700 (PDT) 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 dfw.source.kernel.org (Postfix) with ESMTPS id 7658662150 for ; Thu, 30 Mar 2023 17:36:13 +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-Spam-Status: No, score=-4.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_HI,SPF_HELO_NONE,SPF_PASS 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 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