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 CEA17C001E0 for ; Thu, 10 Aug 2023 11:27:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C15D6B0071; Thu, 10 Aug 2023 07:27:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3982B6B0074; Thu, 10 Aug 2023 07:27:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 261066B0075; Thu, 10 Aug 2023 07:27:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 1789C6B0071 for ; Thu, 10 Aug 2023 07:27:18 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D71BC1CA10D for ; Thu, 10 Aug 2023 11:27:17 +0000 (UTC) X-FDA: 81107968914.03.56207A9 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf25.hostedemail.com (Postfix) with ESMTP id A78D2A000B for ; Thu, 10 Aug 2023 11:27:15 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AV6uBVNy; spf=pass (imf25.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691666835; 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=I2OjAiQnSuJM6TSFJoQJwHA6tLVGrg/ayWUjxPCTLSk=; b=uKWQm+hKlpHMGiUAWwaL3AoXP6LbgKFDzTZvQ/27toqxnQcLwNJ469WRAmFQ0e+DRfLRlK VhqE5fYPwQQHph+862mnRr394Ceur0vRSPSlkaT2PBdnRCpgLxqwt1P9j1wsriBAi7QKVO mwHtR6OxszhomQACxxO1fQuOhdZjnHU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691666835; a=rsa-sha256; cv=none; b=U276U260bZ2d7z/MrJZByNi/ZSigU1SBHW6YydV0DBLTvKfcyZ6ZUwhnJdv/XffRzeWDUq 29/zbvSXzshaDS8xJ0MgTIUX+ydSeDUNEKMdz6tRsZNgYGZfSal0+Vxi6PDSmWjR+9XiMk Uvdf+NgogeT4QmpKZTYAduJreaEYWHE= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AV6uBVNy; spf=pass (imf25.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691666834; 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=I2OjAiQnSuJM6TSFJoQJwHA6tLVGrg/ayWUjxPCTLSk=; b=AV6uBVNyhkC/nIjlUYDEoPdY0E98FjVBdxC2nVAArsxiSqCPLh6WSmUcSSJTOpWz0d12ts JFrruvIPPn9S96R7+bnexjSkIAAVWm1XGORTfiBP/OH03gOjslrGTlRi5FZWojRRY6gVfI /FmybGkHpVQIFoeqhoRnCTWhn4cJS/s= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-634-MZytvxTzPDSYnIlRmHeoxA-1; Thu, 10 Aug 2023 07:27:13 -0400 X-MC-Unique: MZytvxTzPDSYnIlRmHeoxA-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-3fe175fc1f8so5462485e9.1 for ; Thu, 10 Aug 2023 04:27:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691666832; x=1692271632; h=content-transfer-encoding:in-reply-to:organization:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=I2OjAiQnSuJM6TSFJoQJwHA6tLVGrg/ayWUjxPCTLSk=; b=YwP/wlLByv8HXlGmCS/3DRRlP3XdU7KMegexeYYo5mktM7kfwCHcPLoYJsQRIJ6rWb xY2BhGI+aiQO70Gz6qRQtai2hEfIj5oUEa8UKwo2422Ux/zMszKHO/fqeymUu1CdsgZs JuVeuj8Yl5AMcE3GcObpJJ2h1t78dvZkCUoWnj9gqMem+YiD6RRRGXP7uxmsvF3iCPJN R0jryzqUqaNq+9edIWhHdvETq8gnuY+0oc+l5sg3+3A/WQ8B6qY9bJ7nqf5UK5fwJ4kT YCV3420jWFgKMFJ6OLwIv74d7Os2uLTM2fUmqT4Kc3bJVnv3uwPE9+D5rGQocauip7sE QWPw== X-Gm-Message-State: AOJu0YynJPrBOXGYsKpIWY8mhpLcBizQzLQMOzdjPigweqmr6tppbKK+ zD2/UKY/b5p6CXBkXWICPXxq3rmCYPYwOXQd7QGGzEb0bvFpX80VcjEv+ehSZOGKlHG+cr49L3H 2VHm7UWZCzk0= X-Received: by 2002:a1c:7408:0:b0:3fc:80a:cf63 with SMTP id p8-20020a1c7408000000b003fc080acf63mr1846134wmc.38.1691666832384; Thu, 10 Aug 2023 04:27:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE2WJdZfFdUEZJQtqF9CK6w/8jN28ZFc4IFZpYmMX7/vVskOkzrD85FlZkCqxOvtfloTGT42g== X-Received: by 2002:a1c:7408:0:b0:3fc:80a:cf63 with SMTP id p8-20020a1c7408000000b003fc080acf63mr1846110wmc.38.1691666831896; Thu, 10 Aug 2023 04:27:11 -0700 (PDT) Received: from ?IPV6:2a09:80c0:192:0:5dac:bf3d:c41:c3e7? ([2a09:80c0:192:0:5dac:bf3d:c41:c3e7]) by smtp.gmail.com with ESMTPSA id m8-20020a7bca48000000b003fa96fe2bd9sm4826821wml.22.2023.08.10.04.27.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Aug 2023 04:27:11 -0700 (PDT) Message-ID: Date: Thu, 10 Aug 2023 13:27:10 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH mm-unstable v1] mm: add a total mapcount for large folios From: David Hildenbrand 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> <60b5b2a2-1d1d-661c-d61e-855178fff44d@redhat.com> Organization: Red Hat In-Reply-To: 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-Stat-Signature: nsj9zdtrkuz6h1tpfb6j1xi5cun4zour X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: A78D2A000B X-Rspam-User: X-HE-Tag: 1691666835-640285 X-HE-Meta: U2FsdGVkX1/3loHzdxV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/Tkm1y3hHeRdSC9pIrC3Rtae01vKHUTbHiHsbJKOqjoH72vJxB+VdmUGYpSPDecI6VesjsoWbUz9iTDHFosLnabnnL0YtmlSjWyt/mGqgrm6DvEaP4XIknUbokOXCwlwtMTREqCLmL5ZWMWzZxvED0tL+qwv2uuIJcdh3D/eQ9+KtjyMxyadM0gsiRG4hDXzO0SZSSqy7YPFLFDPdN6dNz9ATg5DBnGT6Uocb1ClSAYKWCH26NzbNp7x3CgcjIy1D0YnPsqTjPvYxLwPSUKmqAx+oaYKD241nxYHWoycIElmacvvThTM2F4C8uyFQ5sxfI3dahIAjUxI79eagjcVxGouC8A05hVmZQsXz+pbX+sT32YJrRd5cEu9I0JhF0onrNvLRR88YDS4koHrMRnQF/pukfbp8aYAOZ4WfAIGT9z/wRDZD9JVtdqzzpHPvo9+hwNQKHL0QcJmzxJiVKkWb3fNS46NyON24NLBDz/titMUrzqhnt6zjwr0fpjJX/Ts2Wki1l+7ULxJrquHPp/7jvqTv06Uj/ecpnVTdyxS3Szl3vE7rkBW23Lha8APkmEJ++g5eXZPH0KmoSiQEnyYZy4aiwwzybbBtgowpiUDfcv7qXECt6Y8+ENGtxccXJ5+HBsmuGUKCzE1DyHXxjGgdbfG26jN5B1GW 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 10.08.23 13:14, David Hildenbrand wrote: > On 09.08.23 21:17, David Hildenbrand wrote: >> 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. > > I checked by adjusting total_mapcount(): > > Before: > > if (PageTransHuge(page) && total_mapcount(page) > 1) > ffffffff81411931: 4c 89 e7 mov %r12,%rdi > ffffffff81411934: e8 f7 b1 ff ff call ffffffff8140cb30 > ffffffff81411939: 85 c0 test %eax,%eax > ffffffff8141193b: 74 29 je ffffffff81411966 > ffffffff8141193d: 49 8b 04 24 mov (%r12),%rax > return test_bit(PG_head, &page->flags) || > ffffffff81411941: a9 00 00 01 00 test $0x10000,%eax > ffffffff81411946: 0f 85 1f 01 00 00 jne ffffffff81411a6b > READ_ONCE(page->compound_head) & 1; > ffffffff8141194c: 49 8b 44 24 08 mov 0x8(%r12),%rax > return test_bit(PG_head, &page->flags) || > ffffffff81411951: a8 01 test $0x1,%al > ffffffff81411953: 0f 85 12 01 00 00 jne ffffffff81411a6b > ffffffff81411959: 41 8b 44 24 30 mov 0x30(%r12),%eax > return atomic_read(&page->_mapcount) + 1; > ffffffff8141195e: 83 c0 01 add $0x1,%eax > ffffffff81411961: 83 f8 01 cmp $0x1,%eax > ffffffff81411964: 7f 77 jg ffffffff814119dd > > So a total of 10 instructions after handling the mov/call/test/je for PageTransHuge(). > > After: > > if (PageTransHuge(page) && total_mapcount(page) > 1) > ffffffff81411931: 4c 89 e7 mov %r12,%rdi > ffffffff81411934: e8 f7 b1 ff ff call ffffffff8140cb30 > ffffffff81411939: 85 c0 test %eax,%eax > ffffffff8141193b: 74 2f je ffffffff8141196c > unsigned long head = READ_ONCE(page->compound_head); > ffffffff8141193d: 49 8b 44 24 08 mov 0x8(%r12),%rax > if (unlikely(head & 1)) > ffffffff81411942: a8 01 test $0x1,%al > ffffffff81411944: 0f 85 fc 05 00 00 jne ffffffff81411f46 > ffffffff8141194a: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) > return page; > ffffffff8141194f: 4c 89 e0 mov %r12,%rax > ffffffff81411952: 48 8b 10 mov (%rax),%rdx > if (likely(!folio_test_large(folio))) > ffffffff81411955: f7 c2 00 00 01 00 test $0x10000,%edx > ffffffff8141195b: 0f 85 da 05 00 00 jne ffffffff81411f3b > ffffffff81411961: 8b 40 30 mov 0x30(%rax),%eax > return atomic_read(&folio->_mapcount) + 1; > ffffffff81411964: 83 c0 01 add $0x1,%eax > ffffffff81411967: 83 f8 01 cmp $0x1,%eax > ffffffff8141196a: 7f 77 jg ffffffff814119e3 > > So a total of 11 (+ 1 NOP) instructions after handling the mov/call/test/je for PageTransHuge(). > > Essentially one more MOV instruction. > > I guess nobody really cares :) > Also, let's simply do: static inline bool folio_mapped(struct folio *folio) { folio_mapcount(folio) > 1; } So this all cleans up quite a bit. -- Cheers, David / dhildenb