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 57150C001B0 for ; Thu, 10 Aug 2023 11:14:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B326C6B0071; Thu, 10 Aug 2023 07:14:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AE23A6B0074; Thu, 10 Aug 2023 07:14:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 983176B0075; Thu, 10 Aug 2023 07:14:11 -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 891296B0071 for ; Thu, 10 Aug 2023 07:14:11 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 52EB2A0666 for ; Thu, 10 Aug 2023 11:14:11 +0000 (UTC) X-FDA: 81107935902.22.D5644AD Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf30.hostedemail.com (Postfix) with ESMTP id 0FB628001D for ; Thu, 10 Aug 2023 11:14:08 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iPvmb8E0; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of david@redhat.com designates 170.10.129.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=1691666049; 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=DDSK6F5I/+yE0F6q0sunjDYJdBs3tXhIpKsBzqS4+vc=; b=hU7W6kz7tlI6PomuxH9qfX8X3+mahamzELZfWGkN2+p65LlaITeZXZor1RBZ96SNMJPK8R 9UclY3PSDM4RODe/xpAuM3gcdj7aTwMEgNSFxkhQ9lN7lCBjGpTdkM8NfrrAiY1SzfEeee 1v8nQOF0UZCj+xlxbbjaGnbdT52i4c4= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=iPvmb8E0; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf30.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691666049; a=rsa-sha256; cv=none; b=oMTPHMMzoLVUsNBp7/RlBZiZDLD2kHnOEEvYKLkNsI47iIPyTZoEqym/PnToEgWN21a2VL ujLdVr4AdjBgaERiXrFEDeY1HB5j4io3faa5hOth7w72Th4rrMmULZtjriLCtn5AB2b3hb rP49nBysxVAaxW/iZGMUETBK+eVS6uA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691666048; 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=DDSK6F5I/+yE0F6q0sunjDYJdBs3tXhIpKsBzqS4+vc=; b=iPvmb8E0c3SebYQPFcxN2uaV7PtCFiVQtoNFbvmKKY6hS0Kr/i2UhUwWuKlkG/d31uMhaV EoU97GFzIuabppxea/XMmh5OhqHa5AyrH67PT29dblXZ4aGcwwzkjzk+AFFxa4nqZLOU8T IWOvNGMKuamkC2v1E/PVgBROBmP7/Cs= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-237-9CySghRqOaCQqq6zVmMw9A-1; Thu, 10 Aug 2023 07:14:07 -0400 X-MC-Unique: 9CySghRqOaCQqq6zVmMw9A-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-3fe4cca36b7so4936625e9.3 for ; Thu, 10 Aug 2023 04:14:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691666046; x=1692270846; h=content-transfer-encoding:in-reply-to:subject:organization :references:cc:to:from:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=DDSK6F5I/+yE0F6q0sunjDYJdBs3tXhIpKsBzqS4+vc=; b=DTKbkzH2oQZx1i6hK4ZbU8lbmFWaPuUWevQDKU1KusWR9dySFE4v1kQmVxCGFAqk/O vnERWEFZsTEnRoQcOxyWAg8ksvyu6YDJmGm4jIcyBY/iEy47k2/nol95JOUZecQd1/0o gu/IEvsU7LW9BOyeiZjWyoU5IY0jvZIieBrMsLrPzafsACXeH6O0vvo2fY9z5rkx3fTg 5fiRfe0GHYeZl9VMdYsv651qcJcIh+iaEYRB7fAlQaO1w51JXlZ8nA5gudThGirRU7Zh cfO6RuZ+dPhfyz9o9kT8eJE2IFZ1OhznG7+pUxX1gBeEyQThHiEaa60rhcbwb8tG9AWh 5WfA== X-Gm-Message-State: AOJu0YwJ5tjZFUw+2k+LAlUU4XaDnYgOIVCqaAcePk1SLwF7M1EQP5/K v6SSV+cNoNEiqtRBAcmv8KfJeK3dHyeIlkyTEQHmIEEtUYunyw8wYx0MyEsEYTGh6W4l2G3/m9e 039i+IM1MOhk= X-Received: by 2002:a7b:cd95:0:b0:3fb:bc6d:41f1 with SMTP id y21-20020a7bcd95000000b003fbbc6d41f1mr1658539wmj.17.1691666045803; Thu, 10 Aug 2023 04:14:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFuylGB3efGq/sqv4qSbrjfKHFNql3z/CfdBvaVdRJoMwVqPRMC5ETm/VjH7QY7oBwb5r/wdg== X-Received: by 2002:a7b:cd95:0:b0:3fb:bc6d:41f1 with SMTP id y21-20020a7bcd95000000b003fbbc6d41f1mr1658525wmj.17.1691666045367; Thu, 10 Aug 2023 04:14:05 -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 c16-20020a7bc010000000b003fc00212c1esm1824051wmb.28.2023.08.10.04.14.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Aug 2023 04:14:04 -0700 (PDT) Message-ID: Date: Thu, 10 Aug 2023 13:14:03 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 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 Subject: Re: [PATCH mm-unstable v1] mm: add a total mapcount for large folios In-Reply-To: <60b5b2a2-1d1d-661c-d61e-855178fff44d@redhat.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: 0FB628001D X-Stat-Signature: 5zhofgwa5fzf9wwp5tgwxuxhp86rdits X-HE-T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+ncgpEtEGNl2G2LiZEUJRP/8DkKcIHam4DoBMwz9t+l9VOeH/+BMa/GpSyjFDe5Avq7rk9jQvVJkOg4fhytJlg27MVPbbmIgP5oRYwEXpuH4hUZ38bClJRH2sSk7MLIu2U67kOlehpKJKc/K8gkn9uqS/pKRtbzKm8xEwRp6BsyaC+Zp+Xh4baWYgWPdk48EyPeMlNbWLJ1amnQiknA/Vofq2Viccdv9LiEdRzAOGzi9D9RqQJfKfg8vMdKk1ZcjNIXj/flRalQz6lx+3u+A16boN9NND/UGZJBzwzMh6p+DEW0DJOTRwDy/8vy/W4I6CzAozze2mS06xdJ7QPISw3ONL8HvMcvGfGp1ScxvETvUG7sD28wjYdahQt6x3/Lg9iKg9re2bYl74utjBUqhVGs6ll+IzGd+1AbktXIKxQmo14PYbkLqNsvoRaFUcarNR1sO9Fa+8hrRYNlXKWeLr6LFGOJPbAKE/Hysiy+IB66CpU11atb3jwe446R+y9Vtg3mhKCYefLML2emWV06tyRTBK4mbqW5Il9pPBUJBkJ6VCpQuu+4RfeQYA0+H2xZQDw4cIv7PndbnBp4TuEIkxVadnBJqrIxzmQgaMrVPYz/V36RgP720E3wwyOnAP+JIhsT/6jUGjz4x34VLb+zaTVjGO6snGrJfz1yig2aHkcVCh6eWNzTQ41TOqkCarc42Hf1wx8BVxJ+FQ0nRb95E1N 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: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 :) -- Cheers, David / dhildenb