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 0CD1DC47DD9 for ; Mon, 26 Feb 2024 02:59:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6ED986B013F; Sun, 25 Feb 2024 21:59:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 69CA26B0140; Sun, 25 Feb 2024 21:59:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53D796B0141; Sun, 25 Feb 2024 21:59:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 4010D6B013F for ; Sun, 25 Feb 2024 21:59:55 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 049C5C06C6 for ; Mon, 26 Feb 2024 02:59:54 +0000 (UTC) X-FDA: 81832450350.30.0A3BB70 Received: from mail-ua1-f46.google.com (mail-ua1-f46.google.com [209.85.222.46]) by imf09.hostedemail.com (Postfix) with ESMTP id 42F7414000F for ; Mon, 26 Feb 2024 02:59:52 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=L7rWiME6; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.46 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708916392; 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=fxgyv7Hl9XGA+yew2AuEx/r3B9iAfdwbZeCvSOYqb0g=; b=6yBaSQecOus23W9LdKjAZHhuhCAY7NFhCQxvqQFuQ+Pc4v43xMieklV2M9ION/caCfPw5I PsCdkbI3zWu7cMKku1bRr8nvGKBx40xhmferMZquGffwmKqCsQSzFux4hjdegruZJKWq8r zBv1oPb2Dfy00WzKfR9dMrBEbLcnA0c= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=L7rWiME6; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf09.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.46 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708916392; a=rsa-sha256; cv=none; b=Rg1jHGid3VdcRYNlAV7n/4Zr7N28DSmwTzjWDdSvZSI+qpLT0yhu7udRriWzkIBycuW6DK D5SVN/rTGa9o1FpTOlvqF/5/mECk5Z/e3cb17JfnCeuGkVlNLXOQry2LwuriW4PMRhJtqB Fl3hLW1EKskYQf12lppfWYtMIewlwnU= Received: by mail-ua1-f46.google.com with SMTP id a1e0cc1a2514c-7d6024b181bso1162837241.2 for ; Sun, 25 Feb 2024 18:59:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708916391; x=1709521191; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=fxgyv7Hl9XGA+yew2AuEx/r3B9iAfdwbZeCvSOYqb0g=; b=L7rWiME6vbnqp+I/4zouqZmCQ8ZPvGiAaavBiAILzD3g/fmzgSsgeEoPmQHP0xfMrA ZQaLdN0TZVoogixE6TpzPyvA6rJuzMbJaFRb9/Co61dEK2NT3guTOv/9F40em72r0GRK GsN5+9s+UfGn7KdhXFsH89d1yxBSAonuX4NMqpp8HfmPr1a/ft5dPwYzgV0jrvML7rt5 x/qYcysp6GaTPZj37MZGMXEjXskRCVI3COArcYVigk2LxtGSkDR4JaWuLwhskKeQKsjs 7IJRcE39dCJ6ibLhmAaQoxswgpE63k0DPc0SzVdHJRD7MGPMIerxz0VkmX6cBayY1ggB tjzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708916391; x=1709521191; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fxgyv7Hl9XGA+yew2AuEx/r3B9iAfdwbZeCvSOYqb0g=; b=h8mpc1ns3PsVOxj8WwG3TywaHAWEQw24IdgvbKMykd/tjPMdMrDr+HCf1DgzueFW2n Zi2W/AwW6Gw3AAY+9rdEZZyWHw8o5TzmjeZws9OuzkkZ+J9y/Tg718sLGLG43T0JEnPp INhsXgDeWvWhei9K3kPFmUHZ39LzHCEK1Tp3R1RqMn/SN5mobF1V0DN74FgBibAG1ImB fzj1m+1zpXc0aTW507i7AAVol/2w1N872/x3694NgYx9j6F9PywjRLnyv5PymFNYOiyB IQVtsZurwfvF7qgKmNfoIMkSeAaT93KUNp4hMJiZoRiEk7EWrG0W6S5ONaJ6GHsljzZI tHvw== X-Forwarded-Encrypted: i=1; AJvYcCVAsuEFiwfNOxW4Frn5Jo4fvCB7KwkGAlNvC3XkPa6cjaGmFdXUFRT78aulvGGKQmaBAXNQdbVi56tNYc2HBJYqIek= X-Gm-Message-State: AOJu0Yw72fG3U10u5g2lYqswq5Dff2Y+l4N2JUsfi1lCnjTjg21qVXn7 s6/HDLZtE4lVHHYzTg6LqcZAFafFbLf4ZoCVSgNEVzI5Od+OUjNTC7g2s0eE9/FtQkVeI7FOdAF djZRBTbOgnXBoIg/S7jROPWKAHIU= X-Google-Smtp-Source: AGHT+IF7+ggaYaKgeJw+F1HL9Ggy+Zjxt9GGCumvpwnyrkhV9VS3BOVEWt8GVMzYyTN6Ero1/F+4FilHOSKHi3I89Pk= X-Received: by 2002:a1f:4843:0:b0:4c9:98f8:83d0 with SMTP id v64-20020a1f4843000000b004c998f883d0mr2484773vka.0.1708916391205; Sun, 25 Feb 2024 18:59:51 -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: From: Barry Song <21cnbao@gmail.com> Date: Mon, 26 Feb 2024 15:59:40 +1300 Message-ID: Subject: Re: [PATCH RFC 1/6] arm64: mm: swap: support THP_SWAP on hardware with MTE To: Chris Li 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-Rspam-User: X-Stat-Signature: e6ndq9yfp57z6rt7x3iof5d98nhiub3y X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 42F7414000F X-HE-Tag: 1708916392-374593 X-HE-Meta: U2FsdGVkX1+kc8kfSdT1X7SxPVOjgMkH2Cyq3L2Au5SlDzhWeLWmH/cz+IMNYZ6IUshbia+cpk6Hb0WEiWSkaxVqD1rFXTymRROIWWRnRo2llXVZE2ijcDkxLuarUspDLvaZ55K8HrahnE+9Mv1HZrylWYHDgRuym6rYn/MERl61f2idfQttF7eYAfknWk7YfipN2Kkpjq005+k18RPzTG/8L95zBXkfw5fJSK8Sp5qJnnNmrk+1Bwwxj2MAUCdFnMGTtQ1VdzwnwH9rTTN/2Az9S6PhLg2BwqfaGY1VqKP97zkWpyf4fXHtNg6r7W8cpMWtdvtAbUh48qggTqWTBn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sN0+JMi+MVJAP3UZmap+1CvuezJLcbICoGr3GGhIeRtkZ7u0jN9quVyXYTGZciN8TSZsmEaxHIYZ7LQ1u+Z3meqRe1Be8tlFmjTZjOE8WLBG82xM0pmpJ7Z3R7+dry0lIZ0IdnIaVCJMSw7MRBODNK/3v8X4Ie3dfXpipATHPotTxLnGE+UZcOOgL6GfNrc 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: Hi Chris, Thanks for reviewing. sorry for the late reply as I=E2=80=99ve been getting= a lot to do recently. On Sat, Jan 27, 2024 at 12:14=E2=80=AFPM Chris Li wrote= : > > On Thu, Jan 18, 2024 at 3:11=E2=80=AFAM Barry Song <21cnbao@gmail.com> wr= ote: > > > > 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/= pgtable.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 alw= ays > > * use broadcast TLB invalidation instructions, therefore a spurious p= age > > @@ -1042,12 +1036,8 @@ static inline pmd_t pmdp_establish(struct vm_are= a_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 t= hat? you are right. i was blindly copying my previous code static inline bool arch_thp_swp_supported(void) { return !system_supports_mte(); } #define arch_thp_swp_supported arch_thp_swp_supported for arch_thp_swp_supported, there isn't a similar MACRO. so we are dependin= g on arch_thp_swp_supported as a MACRO for itself in include/linux/huge_mm.h. /* * archs that select ARCH_WANTS_THP_SWAP but don't support THP_SWP due to * 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 Now the case is different, we do have __HAVE_ARCH_PREPARE_TO_SWAP instead. > > 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= type) > > } > > > > #define __HAVE_ARCH_SWAP_RESTORE > > -static inline void arch_swap_restore(swp_entry_t entry, struct folio *= folio) > > -{ > > - if (system_supports_mte()) > > - mte_restore_tags(entry, &folio->page); > > -} > > +#define arch_swap_restore arch_swap_restore > > Same here. you are right, again. > > > +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. I agree. > > > + 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 Thanks! > > > 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= to > > - * 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 writeb= ack_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_support= ed()) > > + if (IS_ENABLED(CONFIG_THP_SWAP)) > > get_swap_pages(1, &entry, folio_nr_pages(folio)= ); > > goto out; > > } > > -- > > 2.34.1 > > Best Regards, Barry