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 067DFC61D97 for ; Fri, 24 Nov 2023 01:35:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 669046B059B; Thu, 23 Nov 2023 20:35:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 616316B05A3; Thu, 23 Nov 2023 20:35:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 490F56B05A7; Thu, 23 Nov 2023 20:35:20 -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 355456B059B for ; Thu, 23 Nov 2023 20:35:20 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 14E6640202 for ; Fri, 24 Nov 2023 01:35:20 +0000 (UTC) X-FDA: 81491130000.07.AFBA236 Received: from mail-ua1-f50.google.com (mail-ua1-f50.google.com [209.85.222.50]) by imf02.hostedemail.com (Postfix) with ESMTP id 4F4948000C for ; Fri, 24 Nov 2023 01:35:18 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Vw4EwJuc; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.50 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=1700789718; 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=wG1ZuaRbCPBv0+UAInoHXoinm1O34IZTOsjDEN9VpmI=; b=xyEjDBDnFz7q8YzFQ3EfBt7LpNmNaIGUcNQjMedIqZTaMLpkuXbrX7RV/dXCcBq2vd6bJb lT2rOXKPgqM35fFXbj5NrB5SmpjNX5yNskcIHg8zUq7w9oYzqp5veaQn2fHABeZCcTFeSZ h8tsRQBM70L3wbI/KxP5rtcIzKcJ4r4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Vw4EwJuc; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf02.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.50 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700789718; a=rsa-sha256; cv=none; b=IWXAbyjCoNNSJMbBvoY+R4IK3cQ9IvSNEya8GI7nwtlE13aUwSG1y/phCUw7N4HqlkHubP Mh4Zh2tidOG57lcO7lwqvlRJfvumeDwpn1qeGnizL74MMXDbUHdzoG/eGdrzra/vgvkEHJ pdtmZb4jRdwN5rcvTBE0DAL8OJXkmNg= Received: by mail-ua1-f50.google.com with SMTP id a1e0cc1a2514c-7be55675503so409646241.1 for ; Thu, 23 Nov 2023 17:35:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700789717; x=1701394517; 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=wG1ZuaRbCPBv0+UAInoHXoinm1O34IZTOsjDEN9VpmI=; b=Vw4EwJuc+wskDTmgZqib9IS97R0i+I8rPwFTXMbRmBT7pxAFMyHquvOgmPSsrDRNjt sUl0dwxrjFUnwNc5QEEcL8flpYPalmNL+OQCTQsSu5FXQWbP+pVnFApPmXN7OIaSGZxW Ijmw4Pp6i8GvdxnnVrqi1/NyiEuElFJMDE6OIwMZEP+h+J3Fttm+ITxmb6SRKvC5zrxJ VDPZakCfAMI5zp9Xi9JwjciK+iDqq2JMb1JA1RKJcHFTS95wHOunCEdlL4gnRoZvPs1V olPFVTSHJl07FeTZeRvxrGMiO2FlwEi8Ny2ih/ly6Q/oKHhyg4X/SksAPdisu9iqt6QZ I0Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700789717; x=1701394517; 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=wG1ZuaRbCPBv0+UAInoHXoinm1O34IZTOsjDEN9VpmI=; b=C/m/HeKugQjVsq/oYeulL23QP9YXz1bbN2Cj4dTwElc+eN86g7zzdlZ9gE3ByQ/Fh5 RsOeR+9je/veVLsTeTbPVkBTvUUFeXppfdYE/F9v2kElAfQX1rKZPMHrhhhAufxNIH3X S0tjhXxmsuZK2Kyc7EjN+eJpFN+nlz1Mc1qO9XPDJ3cQ4Hd+SPEktHusdsdoCEwm6B8H 9S1zg6uQb4nIEaahdProYHcDocsH3Q6JZ0brms62JX5xrVCNaw6OsXwyqBeQKBpp4Ok9 CrgCRoHHtMfyBMoNo3g+DzLCoLiMQ9Mqf4jHGIxHNOwaxAj6ja6geWpOaodE+gatZfHZ 0pXw== X-Gm-Message-State: AOJu0YxxdPJE+I4gFgHS0FOFP+vppFdIeLpAKYXeLxPRnfnnTYImbdkA w+Q4b7o3Q+Qzt+Vy4yQSDQbLm7+SGCox2iQ6TkI= X-Google-Smtp-Source: AGHT+IFrFnFyALVSXBM657DQOsFzWG1PiI+L20o9Ck02MKBjcqSeZVZiCVh2b4opqse0rECIInlYosUnm8ldmV4dA2U= X-Received: by 2002:a67:eb0b:0:b0:45e:8fd7:1ae0 with SMTP id a11-20020a67eb0b000000b0045e8fd71ae0mr1093082vso.8.1700789717296; Thu, 23 Nov 2023 17:35:17 -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: From: Barry Song <21cnbao@gmail.com> Date: Fri, 24 Nov 2023 14:35:06 +1300 Message-ID: Subject: Re: [RFC V3 PATCH] arm64: mm: swap: save and restore mte tags for large folios To: Ryan Roberts Cc: David Hildenbrand , steven.price@arm.com, akpm@linux-foundation.org, 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-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 4F4948000C X-Stat-Signature: b5cc3hrbmzas4anekdagshxf7i1rj8xi X-Rspam-User: X-HE-Tag: 1700789718-9129 X-HE-Meta: U2FsdGVkX19Rre3AfqygM/7kcjPyS3bpGYNpiddsxZ3IklacJzt8+KLG9TBmkpouqvo/FuynhgFnbAAXlLz/x+XLd4k2LEPxyx0bNn/ojiOIhX4xJToIo7ieSmhMgX/59oRuGDy13rWw1bz019SdEhcNkUEKaniQ9cN7exKuRPf57nX8Q5kcB0uO43LHliyPmZQxH83XCTRmiZsEOT8Ts+7PgYUwpbecXNYo9oW/vdXvE2f2WNm9qGO8GYrPEQI0dX0KFJu4x6nf+bTnFQ4aCL6LPhbJDwDTsQWm9mP8SkaGo+nPaVi/JMakCwyR5yC4h7OoIpXOB2EIf6bZ2yyKIJti2J+KeTrN+lKnUre0I1wggxL3BuKEW5vOZ3Peg2D0ItxEUc89+PsHf3e0ylsQSsTZA4bhoAk3nQrM5lbNkQc0sOM/m4xWXiiz25+r8T7IL/PCduHu57AppQYy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=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 Mon, Nov 20, 2023 at 11:57=E2=80=AFPM Ryan Roberts wrote: > > On 20/11/2023 09:11, David Hildenbrand wrote: > > On 17.11.23 19:41, Barry Song wrote: > >> 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.co= m> 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 fo= lios, > >>>>>>>>> then we don't need to split them into base pages for swapping o= ut > >>>>>>>>> on ARM64 SoCs with MTE. > >>>>>>>>> > >>>>>>>>> arch_prepare_to_swap() should take folio rather than page as pa= rameter > >>>>>>>>> because we support THP swap-out as a whole. > >>>>>>>>> > >>>>>>>>> Meanwhile, arch_swap_restore() should use page parameter rather= than > >>>>>>>>> folio as swap-in always works at the granularity of base pages = right > >>>>>>>>> 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-fR06= ijv4Ac68MkhjMDw@mail.gmail.com/ > >>>>>>> https://lore.kernel.org/lkml/CAGsJ_4xmBAcApyK8NgVQeX_Znp5e8D4fbbh= GguOkNzmh1Veocg@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 granul= arity of > >>>>>> base pages right now" comment. > >>>>>> > >>>>>> What you actually wanted to say is "While we always swap in small = folios, we > >>>>>> might refault large folios from the swapcache, and we only want to= restore > >>>>>> 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/= offset are > >>>>>> contiguous. If you know you are faulting on page[1] of the folio w= ith a > >>>>>> given swap offset, you can calculate the swap offset for page[0] s= imply 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 correspo= nding > >>>>>> 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= _page > >>>>> 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= page > >>>>> faults) > >>>>> for this large folio. > >>>>> > >>>>> and still the worst thing is the page fault in the Nth PTE of large= folio > >>>>> might free swap entry as that swap has been in. > >>>>> do_swap_page() > >>>>> { > >>>>> /* > >>>>> * Remove the swap entry and conditionally try to free up the = swapcache. > >>>>> * We're already holding a reference on the page but haven't m= apped it > >>>>> * yet. > >>>>> */ > >>>>> swap_free(entry); > >>>>> } > >>>>> > >>>>> So in the page faults other than N, I mean 0~N-1 and N+1 to 15, you= might > >>>>> 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 fol= io > >>>> is a better choice. > >>> > >>> I think just restoring all tags and remembering for a large folio tha= t > >>> 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 compl= ex and > >> is not worth it for at least three reasons. > > > > Fair enough. > > > >> > >> 1. In multi-thread and particularly multi-processes, we need some lock= s to > >> protect and help know if one process is the first one to restore tags = and if > >> someone else is restoring tags when one process wants to restore. ther= e > >> is not this kind of fine-grained lock at all. > > > > We surely always hold the folio lock on swapin/swapout, no? So when the= se > > functions are called. > > > > So that might just work already -- unless I am missing something import= ant. > > We already have a page flag that we use to mark the page as having had it= s mte > state associated; PG_mte_tagged. This is currently per-page (and IIUC, Ma= tthew > has been working to remove as many per-page flags as possible). Couldn't = we just > make arch_swap_restore() take a folio, restore the tags for *all* the pag= es and > repurpose that flag to be per-folio (so head page only)? It looks like th= e the > mte code already manages all the serialization requirements too. Then > arch_swap_restore() can just exit early if it sees the flag is already se= t on > the folio. > > One (probably nonsense) concern that just sprung to mind about having MTE= work > with large folios in general; is it possible that user space could cause = a large > anon folio to be allocated (THP), then later mark *part* of it to be tagg= ed with > MTE? In this case you would need to apply tags to part of the folio only. > Although I have a vague recollection that any MTE areas have to be marked= at > mmap time and therefore this type of thing is impossible? right, we might need to consider only a part of folio needs to be mapped and restored MTE tags. do_swap_page() can have a chance to hit a large folio but it only needs to fault-in a page. A case can be quite simple as below, 1. anon folio shared by process A and B 2. add_to_swap() as a large folio; 3. try to unmap A and B; 4. after A is unmapped(ptes become swap entries), we do a MADV_DONTNEED on a part of the folio. this can happen very easily as userspace is still working in 4KB level; userspace heap management can free an basepage area by MADV_DONTNEED; madvise(address, MADV_DONTNEED, 4KB); 5. A refault on address + 8KB, we will hit large folio in do_swap_page() but we will only need to map one basepage, we will never need this DONTNEEDed in process A. another more complicated case can be mprotect and munmap a part of large folios. since userspace has no idea of large folios in their mind, they can do all strange things. are we sure in all cases, large folios have been splitted into small folios? > > Thanks, > Ryan > > > > > > >> > >> 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 have > >> to use different ways to remember restoring state for small folios and > >> large folios. > > > > if (folio_test_large(folio)) { > > > > } else { > > > > } > > > > Easy ;) > > > > Seriously, it's not that complicated and/or ugly. > > > >> > >> 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 ot= her > >> after this patch. I would argue what is really wrong is the current ma= inline. > >> > >> 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. > >> > > > > Just making both functions consume folios is much cleaner and also more= future > > proof. > > > > Consuming now a page where we used to consume a folio is a step backwar= ds. > > > Thanks Barry