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 DBE95C072A2 for ; Fri, 17 Nov 2023 18:41:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DEA68004C; Fri, 17 Nov 2023 13:41:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1900D8003F; Fri, 17 Nov 2023 13:41:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 057698004C; Fri, 17 Nov 2023 13:41:39 -0500 (EST) 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 EBE3A8003F for ; Fri, 17 Nov 2023 13:41:38 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id BA770A0266 for ; Fri, 17 Nov 2023 18:41:38 +0000 (UTC) X-FDA: 81468314676.18.73E961F Received: from mail-vs1-f46.google.com (mail-vs1-f46.google.com [209.85.217.46]) by imf11.hostedemail.com (Postfix) with ESMTP id DDB5140024 for ; Fri, 17 Nov 2023 18:41:36 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fAcyHTAa; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.217.46 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700246496; a=rsa-sha256; cv=none; b=1W+RRmkGhqUHD5ZYDMLUzP3sM6xEshUU3ZDOx8l/c82IbYXuMP5FczLwxvw2a/p18ONsug GMqbysMonJDsq+0UNin0A0Pjig/yNCEA9KXVXiFsLnaziL4lHctGyBTBHPX4+IFU5LJO7R q9dWQgxTlBQkAVQm52NpGdejmop5HOc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=fAcyHTAa; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf11.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.217.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=1700246496; 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=YxkQtyANA6jEMk/z4doQOumnQ7XYlHV3YMgX4dWRIZY=; b=Z8fFJ0Ni8zd57ZSDmALNyNOXaeVhY1IMI4PkattFTuVqNgDXOfcSFNEE6UH3+Yn/9BHYF7 GXiS3cIUg3GQ7ZWEWJD5AKmmz8dXW+k9xAYIwl9Cn8dyzPKkxR646w3/Cd44mWVtCSxsdz s4BctbONeMUgd4/2BdKmE9oL8VshW9c= Received: by mail-vs1-f46.google.com with SMTP id ada2fe7eead31-45ef8c21e8aso856109137.1 for ; Fri, 17 Nov 2023 10:41:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700246496; x=1700851296; 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=YxkQtyANA6jEMk/z4doQOumnQ7XYlHV3YMgX4dWRIZY=; b=fAcyHTAaTGAA54ZNV4Wj/sTlaZCWa4g0p9BRb2dhRWHRj7evJoqdy4KRFXPf+/IgIa jrJBc0gt7/tG+E3AMQAXdoCljwHo6SwhgdqbmXQBjrLS1Zzna7kPHycFO4/fEj+O9+BW tyhlfiEovGPM+1c6yJcW1hnWE7Bqc5AIlo2xS2LRgHYbpKrZW4N8aN95oByKkqi9jZxO mJ/yBchgKNC/wpIKMrSE5FRUgBJ12i4t3zSGj16SL49jFCOkhfQGQ4ZpTNuRDef0NqRc J+MKWlTeAOHVPQlJAebPmEEjQhzIHmae+CjigEl1ZT5mdvGIUTD7RkOdnp/NZrK6GQ0U dDwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700246496; x=1700851296; 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=YxkQtyANA6jEMk/z4doQOumnQ7XYlHV3YMgX4dWRIZY=; b=aHLurw8e1y17KqwjdHuTrgWEixMEqWgU9P31iI+u8eV2vWBSc60OPOHqWJHmfZAHrX jrnkZVTbXSZ8jmLaROQmfxpahwHOM28SgbUH9etzH1LKtD2hwr8oEFZa+yHYw9hvRj8h OHrS14XluhP8vzNPoWFFEbimpZ1WwbTPC0RRdMeTlAIze6CkXeERru11V31ihG+5bJI5 sg2N9pIVdfYY/XVMyEvnJaagBXUcyQocoPA08BK2fzYZMoFgdR/qsq/VtsdHH9cTcPAm rccE151fM+F612uM3GzBd4IgoVPDLgPam8KBQWcBhJGa8D5uOgxjzEIluybQ3f//cogd OPCg== X-Gm-Message-State: AOJu0YySmDFZ580C75Kf3i7oZQcajd4Cyqu+9ky99N9GIHOXWDVU+HOh 1VmaJxpfkuaG2BzBFb/WSB3wL5oTsgf2EjBIQEM= X-Google-Smtp-Source: AGHT+IE71nMn1BTfvDu89B2Auj7yAh6uPj2dZw/e4xI95okTO3dQsH5oMDqy618Fd/sVOwntoZekMu/D2qr5DD0whGg= X-Received: by 2002:a05:6102:3441:b0:45d:aa3e:a78 with SMTP id o1-20020a056102344100b0045daa3e0a78mr270518vsj.10.1700246495545; Fri, 17 Nov 2023 10:41:35 -0800 (PST) MIME-Version: 1.0 References: <20231114014313.67232-1-v-songbaohua@oppo.com> <864489b3-5d85-4145-b5bb-5d8a74b9b92d@redhat.com> <8c7f1a2f-57d2-4f20-abb2-394c7980008e@redhat.com> In-Reply-To: <8c7f1a2f-57d2-4f20-abb2-394c7980008e@redhat.com> From: Barry Song <21cnbao@gmail.com> Date: Sat, 18 Nov 2023 02:41:23 +0800 Message-ID: Subject: Re: [RFC V3 PATCH] arm64: mm: swap: save and restore mte tags for large folios To: David Hildenbrand Cc: steven.price@arm.com, akpm@linux-foundation.org, ryan.roberts@arm.com, catalin.marinas@arm.com, will@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, mhocko@suse.com, shy828301@gmail.com, v-songbaohua@oppo.com, wangkefeng.wang@huawei.com, willy@infradead.org, xiang@kernel.org, ying.huang@intel.com, yuzhao@google.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: DDB5140024 X-Stat-Signature: 18tzt6oorca8abizeqdu69gidnmrh7ck X-HE-Tag: 1700246496-403445 X-HE-Meta: U2FsdGVkX1/OVSDPIXu65lEudzBZsH9rkWwmVYm/DUs1m1PhC0Vs4xWSN45/+hWdk01HYJeDF22PIWVhHA/P9cbMHFtrLn/0DCjNjrq2m7d5glE8Gy+tD1Wb2Pcm4NKZBVdRSHz/gH8oc5Kht1pWon54qBGWV4ACYCO+A6xa14ByxR27hJAJqL/8omJGUZ2Zse4zXwSgtZqbwuo3r/9WYyy2fexKV2olUgTP5ZGnQfrI1a81uLjyjoPVz7aWbNZwHWTUNWUndZQNH/RrTvKumSa5ykB9zR2u33w6ulPoE95AMYkrs86BfNKvhmyz9XD9LlWSzf0NhvXadHrqbl4W+0ri7xl56H7Pr1SvgQRvw1E4/UPwShGig8HE53IO5M0iAoTRPCFBu67NehLtmdhWiOzUS8hHBGOv8COcgInBxer4RQ/Q8pWL/vtJbX2CZ2MhrRNt+3AeafQy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loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Fri, Nov 17, 2023 at 7:28=E2=80=AFPM David Hildenbrand wrote: > > On 17.11.23 01:15, Barry Song wrote: > > On Fri, Nov 17, 2023 at 7:47=E2=80=AFAM Barry Song <21cnbao@gmail.com> = wrote: > >> > >> On Thu, Nov 16, 2023 at 5:36=E2=80=AFPM David Hildenbrand wrote: > >>> > >>> On 15.11.23 21:49, Barry Song wrote: > >>>> On Wed, Nov 15, 2023 at 11:16=E2=80=AFPM David Hildenbrand wrote: > >>>>> > >>>>> On 14.11.23 02:43, Barry Song wrote: > >>>>>> This patch makes MTE tags saving and restoring support large folio= s, > >>>>>> then we don't need to split them into base pages for swapping out > >>>>>> on ARM64 SoCs with MTE. > >>>>>> > >>>>>> arch_prepare_to_swap() should take folio rather than page as param= eter > >>>>>> because we support THP swap-out as a whole. > >>>>>> > >>>>>> Meanwhile, arch_swap_restore() should use page parameter rather th= an > >>>>>> folio as swap-in always works at the granularity of base pages rig= ht > >>>>>> now. > >>>>> > >>>>> ... but then we always have order-0 folios and can pass a folio, or= what > >>>>> am I missing? > >>>> > >>>> Hi David, > >>>> you missed the discussion here: > >>>> > >>>> https://lore.kernel.org/lkml/CAGsJ_4yXjex8txgEGt7+WMKp4uDQTn-fR06ijv= 4Ac68MkhjMDw@mail.gmail.com/ > >>>> https://lore.kernel.org/lkml/CAGsJ_4xmBAcApyK8NgVQeX_Znp5e8D4fbbhGgu= OkNzmh1Veocg@mail.gmail.com/ > >>> > >>> Okay, so you want to handle the refault-from-swapcache case where you= get a > >>> large folio. > >>> > >>> I was mislead by your "folio as swap-in always works at the granulari= ty of > >>> base pages right now" comment. > >>> > >>> What you actually wanted to say is "While we always swap in small fol= ios, we > >>> might refault large folios from the swapcache, and we only want to re= store > >>> the tags for the page of the large folio we are faulting on." > >>> > >>> But, I do if we can't simply restore the tags for the whole thing at = once > >>> at make the interface page-free? > >>> > >>> Let me elaborate: > >>> > >>> IIRC, if we have a large folio in the swapcache, the swap entries/off= set are > >>> contiguous. If you know you are faulting on page[1] of the folio with= a > >>> given swap offset, you can calculate the swap offset for page[0] simp= ly by > >>> subtracting from the offset. > >>> > >>> See page_swap_entry() on how we perform this calculation. > >>> > >>> > >>> So you can simply pass the large folio and the swap entry correspondi= ng > >>> to the first page of the large folio, and restore all tags at once. > >>> > >>> So the interface would be > >>> > >>> arch_prepare_to_swap(struct folio *folio); > >>> void arch_swap_restore(struct page *folio, swp_entry_t start_entry); > >>> > >>> I'm sorry if that was also already discussed. > >> > >> This has been discussed. Steven, Ryan and I all don't think this is a = good > >> option. in case we have a large folio with 16 basepages, as do_swap_pa= ge > >> can only map one base page for each page fault, that means we have > >> to restore 16(tags we restore in each page fault) * 16(the times of pa= ge faults) > >> for this large folio. > >> > >> and still the worst thing is the page fault in the Nth PTE of large fo= lio > >> might free swap entry as that swap has been in. > >> do_swap_page() > >> { > >> /* > >> * Remove the swap entry and conditionally try to free up the swap= cache. > >> * We're already holding a reference on the page but haven't mappe= d it > >> * yet. > >> */ > >> swap_free(entry); > >> } > >> > >> So in the page faults other than N, I mean 0~N-1 and N+1 to 15, you mi= ght access > >> a freed tag. > > > > And David, one more information is that to keep the parameter of > > arch_swap_restore() unchanged as folio, > > i actually tried an ugly approach in rfc v2: > > > > +void arch_swap_restore(swp_entry_t entry, struct folio *folio) > > +{ > > + if (system_supports_mte()) { > > + /* > > + * We don't support large folios swap in as whole yet, but > > + * we can hit a large folio which is still in swapcache > > + * after those related processes' PTEs have been unmapped > > + * but before the swapcache folio is dropped, in this case, > > + * we need to find the exact page which "entry" is mapping > > + * to. If we are not hitting swapcache, this folio won't be > > + * large > > + */ > > + struct page *page =3D folio_file_page(folio, swp_offset(entry)); > > + mte_restore_tags(entry, page); > > + } > > +} > > > > And obviously everybody in the discussion hated it :-) > > > > I can relate :D > > > i feel the only way to keep API unchanged using folio is that we > > support restoring PTEs > > all together for the whole large folio and we support the swap-in of > > large folios. This is > > in my list to do, I will send a patchset based on Ryan's large anon > > folios series after a > > while. till that is really done, it seems using page rather than folio > > is a better choice. > > I think just restoring all tags and remembering for a large folio that > they have been restored might be the low hanging fruit. But as always, > devil is in the detail :) Hi David, thanks for all your suggestions though my feeling is this is too complex an= d is not worth it for at least three reasons. 1. In multi-thread and particularly multi-processes, we need some locks to protect and help know if one process is the first one to restore tags and i= f someone else is restoring tags when one process wants to restore. there is not this kind of fine-grained lock at all. 2. folios are not always large, in many cases, they have just one base page and there is no tail to remember. and it seems pretty ugly if we turn out h= ave to use different ways to remember restoring state for small folios and large folios. 3. there is nothing wrong to use page to restore tags since right now swap-= in is page. restoring tags and swapping-in become harmonious with each other after this patch. I would argue what is really wrong is the current mainlin= e. If eventually we are able to do_swap_page() for the whole large folio, we move to folios for MTE tags as well. These two behaviours make a new harmonious picture again. > > -- > Cheers, > > David / dhildenb > Thanks Barry