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 B767BC0015E for ; Wed, 9 Aug 2023 19:17:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 070546B007B; Wed, 9 Aug 2023 15:17:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F3BAC6B007D; Wed, 9 Aug 2023 15:17:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D8F0C6B007E; Wed, 9 Aug 2023 15:17:39 -0400 (EDT) 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 C301A6B007B for ; Wed, 9 Aug 2023 15:17:39 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 91368B29E2 for ; Wed, 9 Aug 2023 19:17:39 +0000 (UTC) X-FDA: 81105525438.26.637A2E7 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf06.hostedemail.com (Postfix) with ESMTP id 502B818002A for ; Wed, 9 Aug 2023 19:17:37 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eEv4iBFJ; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691608657; 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=vP7trY6UjzQ/nSDjPzqV0b/9egqHQIgzSwV3d83W41k=; b=och3rTzqglOLGFzy+/3cSy3NXxc9aeoP4yi+plCByPPTAiqGmMAtxODMU+tKsf4PTVmGMO WrcaWvy4FbSOAhsrTC4voAr7Zey+O5NFUQQydVMXb2ttkBsXo4E9m9CRO8GpwSrpVnkUaR md4nmtHFdDliDJ8dEf1hr3nxTMKoMcU= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eEv4iBFJ; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf06.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691608657; a=rsa-sha256; cv=none; b=pqS2Ontvxs04Q+Qh9rzljwse5cDoBRFjQlzp0zivx3AyfNUMgyK7W7iMk4v+NgBtN1XM2R ZWa4ws2UYznaia8lLXqiRH/lb2djSjw80DUXmDxIHdNSMv1WSMpjfCFLbTCiQgrdo6SC/J iftUVFYQ0zhLAoTr9agxxDyn+mt1TzU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691608656; h=from:from: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; bh=vP7trY6UjzQ/nSDjPzqV0b/9egqHQIgzSwV3d83W41k=; b=eEv4iBFJsfSJ5r+iqZRT1ocfq2hX/vcgMq89kkEdffNo3k6of3YrOiPyueDxvWpjLEVWTk bHFDc16Y7n32ki/p3wbM27OqMXYi0QkjRITjI7Gp+K/4waAKbN7z4S06/yifoYvHSnJIlm Fun/Y8blnGCMBnA0Y1X3REWBelJ2Q7E= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-516-oq1Kr-QLOMaH5vJsJ8cSBw-1; Wed, 09 Aug 2023 15:17:35 -0400 X-MC-Unique: oq1Kr-QLOMaH5vJsJ8cSBw-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-3fe5b94bd62so662395e9.2 for ; Wed, 09 Aug 2023 12:17:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691608654; x=1692213454; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vP7trY6UjzQ/nSDjPzqV0b/9egqHQIgzSwV3d83W41k=; b=R3cxmi13g5R8EM+DjL3ecaHFFX64hITsveBgSwNVOQqG52b+LdbAvFZ1Rq0qfboLIk Knamx2BIJ61W6HJqmfB5DhJCXkxvLGG1kOjCPVrWtq+U/JUVTJdZINa8hEiPel++d9uZ BtkbKrGjAsRh0YNJa5ESsFRfoR3S3KhxKSgBjwmmRWdqic7YLXjB2p5nP5AFe/uejslW iWZmOOXNxwVm1zwZ/1zwSmLuhObclHiFXngeTmF+E9UHsOD6I/D1GZeMT7dvuXoVCXBh NrHETlFg9BxwGKxBBxCblppoXf8KLbcIz4tYSK+fUuqibpfDMsJWqKuNHf4eRxoh1X/M xUhg== X-Gm-Message-State: AOJu0YxdUMdHXOcnEskmTkxOW054YnVS3AxhEymiO+kv4i4oXoI8sFu4 MQF27lE25NuP7/qJtbT2AD3AlBoGs3DfYPRqlSIJWX3Xo1iPTgOoeRHCBpL1MAfX3dJHmSTzKEx PutoaLpxaCw4= X-Received: by 2002:a7b:ce88:0:b0:3fe:1deb:86 with SMTP id q8-20020a7bce88000000b003fe1deb0086mr50989wmj.28.1691608653946; Wed, 09 Aug 2023 12:17:33 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHCxDqts0CmhqrwLmrMtleFWF2PhsEkZ7JxUEb8gf0izo63cH/p0Wr8KfJ4AI/Ke9wrCOW2uQ== X-Received: by 2002:a7b:ce88:0:b0:3fe:1deb:86 with SMTP id q8-20020a7bce88000000b003fe1deb0086mr50976wmj.28.1691608653526; Wed, 09 Aug 2023 12:17:33 -0700 (PDT) Received: from ?IPV6:2003:cb:c70e:6800:9933:28db:f83a:ef5? (p200300cbc70e6800993328dbf83a0ef5.dip0.t-ipconnect.de. [2003:cb:c70e:6800:9933:28db:f83a:ef5]) by smtp.gmail.com with ESMTPSA id a14-20020adfed0e000000b003177f57e79esm17588407wro.88.2023.08.09.12.17.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 09 Aug 2023 12:17:32 -0700 (PDT) Message-ID: <60b5b2a2-1d1d-661c-d61e-855178fff44d@redhat.com> Date: Wed, 9 Aug 2023 21:17:31 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 To: Ryan Roberts , linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, Andrew Morton , Jonathan Corbet , Mike Kravetz , Hugh Dickins , "Matthew Wilcox (Oracle)" , Yin Fengwei , Yang Shi , Zi Yan References: <20230809083256.699513-1-david@redhat.com> <181fcc79-b1c6-412f-9ca1-d1f21ef33e32@arm.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH mm-unstable v1] mm: add a total mapcount for large folios In-Reply-To: <181fcc79-b1c6-412f-9ca1-d1f21ef33e32@arm.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 502B818002A X-Stat-Signature: 6zkyrnue9dn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Q0BIvv8b0FNZVG0A03ohSg9ZZ2PnV0mG9XGpvoLZR/kaiDqHJSoSfxNZzRrE/wSgnvDbFjiazo/2Ior7kvk++EhVO7DK49u3qy8Ha7t8NXeAb2Da7QwSoynnP0W90wR0ku7CbKf+YJAFm2iQMgFwFPoMENUufiGgCZNldlY71BL/Dm0B2NZJ1ydPRB29KjruY9AhRamREh80KV3pxstAptQy7ny/DhtCUsRczdrXuBLe0RrloLgwIzm+x+ej4jceMy68WdabZc6CTW3pGZJHj5OZvP5Ua0T5zlcRCnMJ4b3GlFLKkdHCE5e4mrwh6kdQH/7L/aAYnkFb63gJfjw34rrXOzZ/2q26FnCrK6bBvByPsmtnlnYwnG+83vT5XnclQtCW/nSM5UeE1bt3Cr7WxC1m4gFJMod9Va/vg2/WRZ71rW355sJbkPjrmYWR7CH5zF9AsDKHdcUG/GJGIS+aqdzjk2pTJlQn3qZ4Za25Pz2DYl9e+B+/695N/bqDV+G34ix7z6ZgDlGOG7R0F6tw1InrTKHUS/7qR6W9VbwcOs2KJuujKVGp/mJo4Y/ejNSkzXQQQisyrx179VpxIItTPoIiYq8xFrLPX3cfat/ruWEah+/xTgGnAdiN6cGnGCobYVOfXAg0d8feO+Vcbq1vI8OxeA3YQgTFWByodSDgHFZabqKUaMqfJkNU+65u5/2YuJhfkLa69wZa//oJ 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: On 09.08.23 21:07, Ryan Roberts wrote: > On 09/08/2023 09:32, David Hildenbrand wrote: >> Let's track the total mapcount for all large folios in the first subpage. >> >> The total mapcount is what we actually want to know in folio_mapcount() >> and it is also sufficient for implementing folio_mapped(). This also >> gets rid of any "raceiness" concerns as expressed in >> folio_total_mapcount(). >> >> With sub-PMD THP becoming more important and things looking promising >> that we will soon get support for such anon THP, we want to avoid looping >> over all pages of a folio just to calculate the total mapcount. Further, >> we may soon want to use the total mapcount in other context more >> frequently, so prepare for reading it efficiently and atomically. >> >> Make room for the total mapcount in page[1] of the folio by moving >> _nr_pages_mapped to page[2] of the folio: it is not applicable to hugetlb >> -- and with the total mapcount in place probably also not desirable even >> if PMD-mappable hugetlb pages could get PTE-mapped at some point -- so we >> can overlay the hugetlb fields. >> >> Note that we currently don't expect any order-1 compound pages / THP in >> rmap code, and that such support is not planned. If ever desired, we could >> move the compound mapcount to another page, because it only applies to >> PMD-mappable folios that are definitely larger. Let's avoid consuming >> more space elsewhere for now -- we might need more space for a different >> purpose in some subpages soon. >> >> Maintain the total mapcount also for hugetlb pages. Use the total mapcount >> to implement folio_mapcount(), total_mapcount(), folio_mapped() and >> page_mapped(). >> >> We can now get rid of folio_total_mapcount() and >> folio_large_is_mapped(), by just inlining reading of the total mapcount. >> >> _nr_pages_mapped is now only used in rmap code, so not accidentially >> externally where it might be used on arbitrary order-1 pages. The remaining >> usage is: >> >> (1) Detect how to adjust stats: NR_ANON_MAPPED and NR_FILE_MAPPED >> -> If we would account the total folio as mapped when mapping a >> page (based on the total mapcount), we could remove that usage. >> >> (2) Detect when to add a folio to the deferred split queue >> -> If we would apply a different heuristic, or scan using the rmap on >> the memory reclaim path for partially mapped anon folios to >> split them, we could remove that usage as well. >> >> So maybe, we can simplify things in the future and remove >> _nr_pages_mapped. For now, leave these things as they are, they need more >> thought. Hugh really did a nice job implementing that precise tracking >> after all. >> >> Note: Not adding order-1 sanity checks to the file_rmap functions for >> now. For anon pages, they are certainly not required right now. >> >> Cc: Andrew Morton >> Cc: Jonathan Corbet >> Cc: Mike Kravetz >> Cc: Hugh Dickins >> Cc: "Matthew Wilcox (Oracle)" >> Cc: Ryan Roberts >> Cc: Yin Fengwei >> Cc: Yang Shi >> Cc: Zi Yan >> Signed-off-by: David Hildenbrand > > Other than the nits and query on zeroing _total_mapcount below, LGTM. If zeroing > is correct: > > Reviewed-by: Ryan Roberts Thanks for the review! [...] >> >> static inline int total_mapcount(struct page *page) > > nit: couldn't total_mapcount() just be implemented as a wrapper around > folio_mapcount()? What's the benefit of PageCompound() check instead of just > getting the folio and checking if it's large? i.e: Good point, let me take a look tomorrow if the compiler can optimize in both cases equally well. [...] >> >> diff --git a/mm/hugetlb.c b/mm/hugetlb.c >> index 5f498e8025cc..6a614c559ccf 100644 >> --- a/mm/hugetlb.c >> +++ b/mm/hugetlb.c >> @@ -1479,7 +1479,7 @@ static void __destroy_compound_gigantic_folio(struct folio *folio, >> struct page *p; >> >> atomic_set(&folio->_entire_mapcount, 0); >> - atomic_set(&folio->_nr_pages_mapped, 0); >> + atomic_set(&folio->_total_mapcount, 0); > > Just checking this is definitely what you intended? _total_mapcount is -1 when > it means "no pages mapped", so 0 means 1 page mapped? I was blindly doing what _entire_mapcount is doing: zeroing out the values. ;) But let's look into the details: in __destroy_compound_gigantic_folio(), we're manually dissolving the whole compound page. So instead of actually returning a compound page to the buddy (where we would make sure the mapcounts are -1, to then zero them out !), we simply zero-out the fields we use and then dissolve the compound page: to be left with a bunch of order-0 pages where the memmap is in a clean state. (the buddy doesn't handle that page order, so we have to do things manually to get to order-0 pages we can reuse or free) -- Cheers, David / dhildenb