Received: by 2002:a05:6358:51dd:b0:131:369:b2a3 with SMTP id 29csp1095304rwl; Thu, 10 Aug 2023 06:28:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF/9Qr7OzEmd/9+QyakeCKL/mxCYdU+9eZhuMO6O2L/YBlhhmm5kNszDEaQk85dfkrvj0Cx X-Received: by 2002:a17:903:32c9:b0:1bb:98a0:b78a with SMTP id i9-20020a17090332c900b001bb98a0b78amr2570596plr.18.1691674106506; Thu, 10 Aug 2023 06:28:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691674106; cv=none; d=google.com; s=arc-20160816; b=VTGK5s8BFEtH+f3T1xrxljTcrAWcSRjHWh525ccMPAaa0wxGJ+9P5NyWfrr+G4c2EZ lmsTCnJi/JupFPWHBe+7TqaQfSEq6bvgWMVs+ZRLHR4BV1YG2JDVNOsU5dtJNwU4U6Nn 987xwgngto3sSYi+0sg0P77tyNb42LN4FHgEhys/zYmUg5x9wUJQDSLFhbjetT8FzU3E 06ndX3p0eQJjQKeJYD0+aCQGuA28UFiVdOxPvsu/sdKA2KCTUZqwxMdBV0RseiFblsFU qS0mRncPC3h8cHLBMg6gbdeH5y4kP/IRUaSb5Ye7lbSfsYCGLOegnOiI12JjksaMtNZi fF4A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:references:cc:to:from:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=oLK9LGDlC20kprqlDXLL2d+DOs1eVcgviEjDmvnvMC4=; fh=HFIQwVXepKFGkVfs69+i3mNTOeUE8M131Be6GaWU8Mk=; b=fSwNl+Nje2jr8LQ0cRt9ViyAylkOfadcbNlP+7Izs1WyKq5RPGMVHyR5F5MpV5HBDX /GQAlKwZfnlHVWWQT1IB2mmVEwb3d+XOGAQdb5X5t4ZapShXb7ptfWKXZmhpDBdscihk HIcATtllVfIIbGzW5PjRhcmqhiJLA4h4CDoEkzTcXbY6LLQjjrtcP9dKG6gU5hHXmWAZ Jg5yaT3/4GgfFLQlpVrU3llRLx9Kc3OIeh/BHLP7UFB1KIJ06k0tQmlTcM0mThG6RmfZ dVd3tF2ujJx8oLYjO47dKq2tc+/if1kQAhpvboRClhv3nAQfOIDkg/VMQtv4ORKp9R6K dzFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gao6qnNY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i5-20020a170902c94500b001b864d88d85si1497465pla.636.2023.08.10.06.28.13; Thu, 10 Aug 2023 06:28:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=gao6qnNY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235167AbjHJLcy (ORCPT + 99 others); Thu, 10 Aug 2023 07:32:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34280 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235154AbjHJLcx (ORCPT ); Thu, 10 Aug 2023 07:32:53 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 090DB10D for ; Thu, 10 Aug 2023 04:32:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691667126; 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=oLK9LGDlC20kprqlDXLL2d+DOs1eVcgviEjDmvnvMC4=; b=gao6qnNY7zeoArfM78Lsh+sncCFM0WSL3xxCeze+i440kKvOuiSbuDX4KSmTaWfs/6eY7n AGoMAVhXwwMEvRg7zugepJ2HaewNdlxv6sEZ7OqaoYOqLPJaW142ly3V8wxQb0HQe+om7r Cenh32lYUtXvAEwwBw9xJOuZ5DFEsF0= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-497-TlpgSoikNUKVz_txJkIGCQ-1; Thu, 10 Aug 2023 07:32:05 -0400 X-MC-Unique: TlpgSoikNUKVz_txJkIGCQ-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-3fe4f953070so4606665e9.2 for ; Thu, 10 Aug 2023 04:32:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691667124; x=1692271924; 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=oLK9LGDlC20kprqlDXLL2d+DOs1eVcgviEjDmvnvMC4=; b=AJznnv2r+tozuY/385nIYcukSyOe4FP/qZ3H0JzlyKZCRcGmJhsL/djhUgz17Bca7z Usse5TALGs0EslKJrkD5jgqKSx6Rh2ETlDnx9nsY3tRCy/8n+q+HuSRoYti7xKJSPF7K LuYacVVBn1C+45pvu7O29ld4L/1vFW4dfyxmWjIqa3hQVhyvUoofi2akULmVEvG+GASQ MovtrSWGLqueMTYX8IEk+b+ukrA8JzV3n0rMi16DfJbBRx1H37j/K3BslGSXSYdIzr1T a5veqDCzSyMM3XD+Ts698m8M2juzZUwwNfb0ngiieUVoylUfOlf2MwXqVt5WKMp/ke+k Tspg== X-Gm-Message-State: AOJu0Ywdi4oKAs6ppryaNjjfLyOdFjIYbD5VRYOPfs0QSWvbA8TRtxSP d97lgHDjISt93b+5p3+R2KQsho71wW3895Q6NBKk/nE0qzBLW/DZrpOEWWlBNMLhN7AmbH/yG/b q8hLLCqzxgvfDLODRm56jxgOu X-Received: by 2002:a1c:721a:0:b0:3fb:9474:54cd with SMTP id n26-20020a1c721a000000b003fb947454cdmr1581075wmc.19.1691667123841; Thu, 10 Aug 2023 04:32:03 -0700 (PDT) X-Received: by 2002:a1c:721a:0:b0:3fb:9474:54cd with SMTP id n26-20020a1c721a000000b003fb947454cdmr1581050wmc.19.1691667123429; Thu, 10 Aug 2023 04:32:03 -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 y1-20020a05600c364100b003fbb06af219sm1840009wmq.32.2023.08.10.04.32.02 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Aug 2023 04:32:03 -0700 (PDT) Message-ID: <08f066e9-f137-f96c-df63-d5ccc8138470@redhat.com> Date: Thu, 10 Aug 2023 13:32:02 +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 Content-Language: en-US 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: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10.08.23 13:27, David Hildenbrand wrote: > 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; > 0, obviously :) -- Cheers, David / dhildenb