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 B0FF9C47422 for ; Fri, 26 Jan 2024 23:14:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 214CF6B0081; Fri, 26 Jan 2024 18:14:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1C4B66B0082; Fri, 26 Jan 2024 18:14:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 068DB6B0083; Fri, 26 Jan 2024 18:14:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E52A86B0081 for ; Fri, 26 Jan 2024 18:14:19 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 67F7E1C1A56 for ; Fri, 26 Jan 2024 23:14:18 +0000 (UTC) X-FDA: 81723017796.29.A0C4BB8 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf07.hostedemail.com (Postfix) with ESMTP id 7DE0440015 for ; Fri, 26 Jan 2024 23:14:16 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LVODDONb; spf=pass (imf07.hostedemail.com: domain of chrisl@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=chrisl@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1706310856; 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:dkim-signature; bh=/McHrgO1qLFs6tpnDvi/0GEBPOUGPZx2aB/g3OefdFQ=; b=VeHzUXbBVQTdwS40v/C3Z+M+bf9XH+dhNy8HogAuE3iz6K7BTgEkxea/4/3WxetKVwrE07 97r4I6DMdCu+CjaiLd3a26hR167oUahfrZRLe6Vkv6iepZ6o2hIlhstCgNcSIQgNv7bELE tDta20bBRUJpXYuMwKTjnJlQmpwAykY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1706310856; a=rsa-sha256; cv=none; b=4k0LwqcIw7067oK0IHmABi/JxQD/9G+aI/7n0mweejfkiA2l9mxgepkpNmJgh0yxJbZlFC g9tQ1TYqDXIDWC5zw+nPAu92Aa5gH4Yk7kSrj8VpOLNoa6ZbgLFmSII/8AyCLhoguj7Ss2 yQxWavh3NyTTwQDO7S3oZ4HFKUeygx4= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=LVODDONb; spf=pass (imf07.hostedemail.com: domain of chrisl@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=chrisl@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 495566263A for ; Fri, 26 Jan 2024 23:14:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ABE0DC43141 for ; Fri, 26 Jan 2024 23:14:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1706310854; bh=+oKDDKRyI7OKfps5tA+D5MorjH0m9ZM8s5XpTUsGro0=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=LVODDONbKwu7vS3Qgtbid/id8fJCVdzwnOCvJu2V00gQKgEj3KCMnC0E7qB2yB++v V3kBwMCeYdwCnIkrRhM4PqT3opt1zrhbhaGyd3caeFD3fcWpAbE5J7PE+u2Vk9red8 O9VSYXMNtu0bAxrDZTgjOCh/039zLgRdMQKEWWNGy6aBRWNHTHlFJapyaKQ4M3+74H 4Vf/idtgROLs0sq6GlHXob/EUWrX1ZhcHYANNOUy1N4iE3zvdvUt8rcbw6cOyQDKY7 G2PWPL1zWRX9rA99b0/dbxXDx8E3r1xwJapLe+Bg3Fu1Sc/QpUvoKSjSsbeI4CmVlJ I7SRHZCZd2d4g== Received: by mail-il1-f180.google.com with SMTP id e9e14a558f8ab-362a24b136fso1244445ab.3 for ; Fri, 26 Jan 2024 15:14:14 -0800 (PST) X-Gm-Message-State: AOJu0Yz/kjWdivTd1LLmwh36+/fskpDS+xFfVfHEVCy9+Xtng6VaJJZL 07Qbym6YOVueTVN98RnLpnOhWMGwjLfnqcYtP4oiie0RyZrFuAqukWWhzGY6cXKYZcpks9zPh4A 58i4JXF6uujLUk5YuGs0ujG/jtfHfqYYhEGgo X-Google-Smtp-Source: AGHT+IGA6N1e/CyyIzJme2fJkdHFac4vf9+ga8MksY50ZoNX6thbVbpAbovQQz5SoZ19Vc5J8LFk5U2XoevmRmyTW+Q= X-Received: by 2002:a05:6e02:1523:b0:361:9538:e440 with SMTP id i3-20020a056e02152300b003619538e440mr691723ilu.21.1706310853872; Fri, 26 Jan 2024 15:14:13 -0800 (PST) MIME-Version: 1.0 References: <20231025144546.577640-1-ryan.roberts@arm.com> <20240118111036.72641-1-21cnbao@gmail.com> <20240118111036.72641-2-21cnbao@gmail.com> In-Reply-To: <20240118111036.72641-2-21cnbao@gmail.com> From: Chris Li Date: Fri, 26 Jan 2024 15:14:02 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH RFC 1/6] arm64: mm: swap: support THP_SWAP on hardware with MTE To: Barry Song <21cnbao@gmail.com> Cc: ryan.roberts@arm.com, akpm@linux-foundation.org, david@redhat.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, mhocko@suse.com, shy828301@gmail.com, wangkefeng.wang@huawei.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yuzhao@google.com, surenb@google.com, steven.price@arm.com, Barry Song Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 7DE0440015 X-Rspam-User: X-Stat-Signature: ctyr98hn1m3ans5gspy48ttnpruewu6e X-Rspamd-Server: rspam03 X-HE-Tag: 1706310856-113710 X-HE-Meta: U2FsdGVkX1+vM6Bu01YDDFlTlZfCtl5M26braB/sCYscNQl+sfQqpHpeGegShp2dXGJH7csrcndfF0aZBAY5xRHvXnoIBUQGwtoiEcj/SZUvsPczZ40PpTVReuqZaDbi/6TLqk8EPam3fSLIWlmMg1binn3rmDCPCBO3BUczDEUzKPAlzqt+/MZovHodQUTPqo2Tg/R/9F39qlgcXEsb2AgbSV2U82xCnKmIkWubm7TPZwLBSZu5Wszqj8B07rgbrQTWwOD0FR3tsG+mlq5wPbyByJ2ul+3wCS6pGHxfJiDGsuUKiOvqCrJEuuyKdEGWc3q8oaCstv/T3wBA8Iq7GWUHaW5XPSc1Gs5Y6f/01+2sM3YrM7fC9tQtndKXZCHh13PfBT7xQspkcqsp7sWeJZc1mIjtTxA5htqZl+JW5GWlyY4zYA3OWlEEoNhNUFmebEqw02Cu9gWlIH1v14e32ex3sk29+Ngi1c5u4VcmJ9D1dt1v8pOiPhd27OroMEqOiS/YNTGKXo3Ew3EzzK1xRTnkpJflWULl2N1RP0Jws3daoC36qMdA/JMj9MfX88gwfI8RG0ef2GxW3weztNd7i0QtPBkAvREkCuqsBPsCTXRNNLhiNsnzaUODFMWjEFbuX7zElNQYRLLefmDM/7wupyvqzhpUZ2NIrNba9CfyH5MP1epqydjz/2PDL62iF5REP1a3y61UwGsLovD5rDo+UywUgAWTIOQRcvsVFpY0qhBYJ8TTAz3NxFgSbfaP10Rl532tOhG3fB4FV2RdZJweiHD4kaCdoLoJLfqVAY6l/9oIZAZzIciYojn7mSM4iCEwIfefcRjRVJgc/2uAAgRr1Fu4ay8/kLqAaWSGSvur0AVFEKxgp/0RcIEzeEgviSY1PmqPGpIHXnr+JYiix0SrX+G2urRLd8/8TE3/QuJSYYjFNG8jvAGCny0KxZ+yynMoiK7sBYR70ciCcqtfl71 16Yj3SDp HG7mK6RtPO2Sw17ZJR8nsj8RXtbMZhSPMb4clSo2G0uZUq6G3lrL7mTDV5ijTm/dssPgVy9r4HvY4sdN2+1fEZ7jwy3LsPiINi3EoZwH3TRfCmbkEdnyMPKv2FPjmEek5DKjr/4H/9iK61i5flE6/yK/UgvvODMqgp6Bfxh8iavvPnyhtEgBHCd3JlesdPjuC/pZ3sHArU1JieuBLAI5FVVOYamk6HbMebVcgUSqaZKIaJGzWkAxL50XoNwJD+EUz9nj9fvlrJn0TWtLVW4Tc/+GQrI4t6vz+L9/h76nzk9Hq56vCJhCEa3pPfvTe6iE0RfwtiHhD8NpsgAdxoYId3iDUm16o8hxeEPcT9+AjZ+9Z+92H48Wm2nfrey/6byqAxP5UPZEx+19mE3hgrUlGOqVXO5CjX+PLO87lF0JuzlpPvIUgAchLELGyUjIaOvE4xAfkHZKlJTDAYlZIbzrD8U0yfg== 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: List-Subscribe: List-Unsubscribe: On Thu, Jan 18, 2024 at 3:11=E2=80=AFAM Barry Song <21cnbao@gmail.com> wrot= e: > > From: Barry Song > > Commit d0637c505f8a1 ("arm64: enable THP_SWAP for arm64") brings up > THP_SWAP on ARM64, but it doesn't enable THP_SWP on hardware with > MTE as the MTE code works with the assumption tags save/restore is > always handling a folio with only one page. > > The limitation should be removed as more and more ARM64 SoCs have > this feature. Co-existence of MTE and THP_SWAP becomes more and > more important. > > This patch makes MTE tags saving support large folios, then we don't > need to split large folios into base pages for swapping out on ARM64 > SoCs with MTE any more. > > arch_prepare_to_swap() should take folio rather than page as parameter > because we support THP swap-out as a whole. It saves tags for all > pages in a large folio. > > As now we are restoring tags based-on folio, in arch_swap_restore(), > we may increase some extra loops and early-exitings while refaulting > a large folio which is still in swapcache in do_swap_page(). In case > a large folio has nr pages, do_swap_page() will only set the PTE of > the particular page which is causing the page fault. > Thus do_swap_page() runs nr times, and each time, arch_swap_restore() > will loop nr times for those subpages in the folio. So right now the > algorithmic complexity becomes O(nr^2). > > Once we support mapping large folios in do_swap_page(), extra loops > and early-exitings will decrease while not being completely removed > as a large folio might get partially tagged in corner cases such as, > 1. a large folio in swapcache can be partially unmapped, thus, MTE > tags for the unmapped pages will be invalidated; > 2. users might use mprotect() to set MTEs on a part of a large folio. > > arch_thp_swp_supported() is dropped since ARM64 MTE was the only one > who needed it. > > Reviewed-by: Steven Price > Signed-off-by: Barry Song > --- > arch/arm64/include/asm/pgtable.h | 21 +++------------- > arch/arm64/mm/mteswap.c | 42 ++++++++++++++++++++++++++++++++ > include/linux/huge_mm.h | 12 --------- > include/linux/pgtable.h | 2 +- > mm/page_io.c | 2 +- > mm/swap_slots.c | 2 +- > 6 files changed, 49 insertions(+), 32 deletions(-) > > diff --git a/arch/arm64/include/asm/pgtable.h b/arch/arm64/include/asm/pg= table.h > index 79ce70fbb751..9902395ca426 100644 > --- a/arch/arm64/include/asm/pgtable.h > +++ b/arch/arm64/include/asm/pgtable.h > @@ -45,12 +45,6 @@ > __flush_tlb_range(vma, addr, end, PUD_SIZE, false, 1) > #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ > > -static inline bool arch_thp_swp_supported(void) > -{ > - return !system_supports_mte(); > -} > -#define arch_thp_swp_supported arch_thp_swp_supported > - > /* > * Outside of a few very special situations (e.g. hibernation), we alway= s > * use broadcast TLB invalidation instructions, therefore a spurious pag= e > @@ -1042,12 +1036,8 @@ static inline pmd_t pmdp_establish(struct vm_area_= struct *vma, > #ifdef CONFIG_ARM64_MTE > > #define __HAVE_ARCH_PREPARE_TO_SWAP > -static inline int arch_prepare_to_swap(struct page *page) > -{ > - if (system_supports_mte()) > - return mte_save_tags(page); > - return 0; > -} > +#define arch_prepare_to_swap arch_prepare_to_swap This seems a noop, define "arch_prepare_to_swap" back to itself. What am I missing? I see. Answer my own question, I guess you want to allow someone to overwrite the arch_prepare_to_swap. Wouldn't testing against __HAVE_ARCH_PREPARE_TO_SWAP enough to support tha= t? Maybe I need to understand better how you want others to extend this code to make suggestions. As it is, this looks strange. > +extern int arch_prepare_to_swap(struct folio *folio); > > #define __HAVE_ARCH_SWAP_INVALIDATE > static inline void arch_swap_invalidate_page(int type, pgoff_t offset) > @@ -1063,11 +1053,8 @@ static inline void arch_swap_invalidate_area(int t= ype) > } > > #define __HAVE_ARCH_SWAP_RESTORE > -static inline void arch_swap_restore(swp_entry_t entry, struct folio *fo= lio) > -{ > - if (system_supports_mte()) > - mte_restore_tags(entry, &folio->page); > -} > +#define arch_swap_restore arch_swap_restore Same here. > +extern void arch_swap_restore(swp_entry_t entry, struct folio *folio); > > #endif /* CONFIG_ARM64_MTE */ > > diff --git a/arch/arm64/mm/mteswap.c b/arch/arm64/mm/mteswap.c > index a31833e3ddc5..b9ca1b35902f 100644 > --- a/arch/arm64/mm/mteswap.c > +++ b/arch/arm64/mm/mteswap.c > @@ -68,6 +68,13 @@ void mte_invalidate_tags(int type, pgoff_t offset) > mte_free_tag_storage(tags); > } > > +static inline void __mte_invalidate_tags(struct page *page) > +{ > + swp_entry_t entry =3D page_swap_entry(page); > + > + mte_invalidate_tags(swp_type(entry), swp_offset(entry)); > +} > + > void mte_invalidate_tags_area(int type) > { > swp_entry_t entry =3D swp_entry(type, 0); > @@ -83,3 +90,38 @@ void mte_invalidate_tags_area(int type) > } > xa_unlock(&mte_pages); > } > + > +int arch_prepare_to_swap(struct folio *folio) > +{ > + int err; > + long i; > + > + if (system_supports_mte()) { Very minor nitpick. You can do if (!system_supports_mte()) return 0; Here and the for loop would have less indent. The function looks flatter. > + long nr =3D folio_nr_pages(folio); > + > + for (i =3D 0; i < nr; i++) { > + err =3D mte_save_tags(folio_page(folio, i)); > + if (err) > + goto out; > + } > + } > + return 0; > + > +out: > + while (i--) > + __mte_invalidate_tags(folio_page(folio, i)); > + return err; > +} > + > +void arch_swap_restore(swp_entry_t entry, struct folio *folio) > +{ > + if (system_supports_mte()) { Same here. Looks good otherwise. None of the nitpicks are deal breakers. Acked-by: Chris Li Chris > + long i, nr =3D folio_nr_pages(folio); > + > + entry.val -=3D swp_offset(entry) & (nr - 1); > + for (i =3D 0; i < nr; i++) { > + mte_restore_tags(entry, folio_page(folio, i)); > + entry.val++; > + } > + } > +} > diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h > index 5adb86af35fc..67219d2309dd 100644 > --- a/include/linux/huge_mm.h > +++ b/include/linux/huge_mm.h > @@ -530,16 +530,4 @@ static inline int split_folio(struct folio *folio) > return split_folio_to_list(folio, NULL); > } > > -/* > - * archs that select ARCH_WANTS_THP_SWAP but don't support THP_SWP due t= o > - * limitations in the implementation like arm64 MTE can override this to > - * false > - */ > -#ifndef arch_thp_swp_supported > -static inline bool arch_thp_swp_supported(void) > -{ > - return true; > -} > -#endif > - > #endif /* _LINUX_HUGE_MM_H */ > diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h > index f6d0e3513948..37fe83b0c358 100644 > --- a/include/linux/pgtable.h > +++ b/include/linux/pgtable.h > @@ -925,7 +925,7 @@ static inline int arch_unmap_one(struct mm_struct *mm= , > * prototypes must be defined in the arch-specific asm/pgtable.h file. > */ > #ifndef __HAVE_ARCH_PREPARE_TO_SWAP > -static inline int arch_prepare_to_swap(struct page *page) > +static inline int arch_prepare_to_swap(struct folio *folio) > { > return 0; > } > diff --git a/mm/page_io.c b/mm/page_io.c > index ae2b49055e43..a9a7c236aecc 100644 > --- a/mm/page_io.c > +++ b/mm/page_io.c > @@ -189,7 +189,7 @@ int swap_writepage(struct page *page, struct writebac= k_control *wbc) > * Arch code may have to preserve more data than just the page > * contents, e.g. memory tags. > */ > - ret =3D arch_prepare_to_swap(&folio->page); > + ret =3D arch_prepare_to_swap(folio); > if (ret) { > folio_mark_dirty(folio); > folio_unlock(folio); > diff --git a/mm/swap_slots.c b/mm/swap_slots.c > index 0bec1f705f8e..2325adbb1f19 100644 > --- a/mm/swap_slots.c > +++ b/mm/swap_slots.c > @@ -307,7 +307,7 @@ swp_entry_t folio_alloc_swap(struct folio *folio) > entry.val =3D 0; > > if (folio_test_large(folio)) { > - if (IS_ENABLED(CONFIG_THP_SWAP) && arch_thp_swp_supported= ()) > + if (IS_ENABLED(CONFIG_THP_SWAP)) > get_swap_pages(1, &entry, folio_nr_pages(folio)); > goto out; > } > -- > 2.34.1 > >