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 9480AC3DA6E for ; Wed, 10 Jan 2024 10:59:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2E52F6B0092; Wed, 10 Jan 2024 05:59:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 295106B0093; Wed, 10 Jan 2024 05:59:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1850B6B0095; Wed, 10 Jan 2024 05:59:05 -0500 (EST) 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 04CAD6B0092 for ; Wed, 10 Jan 2024 05:59:05 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id BCB40A0B61 for ; Wed, 10 Jan 2024 10:59:04 +0000 (UTC) X-FDA: 81663104208.21.D749B53 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf17.hostedemail.com (Postfix) with ESMTP id 5189840024 for ; Wed, 10 Jan 2024 10:58:55 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf17.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1704884335; 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; bh=Kz+9ikjHj08VH2VORckGukVV68QVYYq88HTOIfFpRco=; b=MZd0Qn+g1pbMXECrq4toE55OuIGNaradsHoVVwrweUeGsZ2ZAXwhtRMBB8AlbIoP7c0nbh hpzmIc/z3G/iCAheSAURohCgIctRVrjWhKoTVn28mA0Qn4QaZAxsjjHNZuy1yESLeFqoLD mL7XhKYGvFNAal8a1G/HZqU3fdD9Ca4= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf17.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1704884335; a=rsa-sha256; cv=none; b=2d0/KANyTOvScDiOwmRZCbPObugvTnrU584IwSpS1a97yYmoUDCRI0Rn41CtoVmRoAkUoL U9HzK2/GBqMLvKZqGLehQEcJLCkfFovr2s26RITA57+6j25KEFktVrcTmGEWrr5PGQe0I5 9Lo10D8aDokU/y8IBsXBAwPrHA6xunE= Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 327D72F4; Wed, 10 Jan 2024 02:59:40 -0800 (PST) Received: from [10.57.76.12] (unknown [10.57.76.12]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id EA3A63F64C; Wed, 10 Jan 2024 02:58:52 -0800 (PST) Message-ID: <8070266d-7cff-40c9-892c-2c7d1ca2e85d@arm.com> Date: Wed, 10 Jan 2024 10:58:51 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH v1] tools/mm: Add thpmaps script to dump THP usage info Content-Language: en-GB To: David Hildenbrand , Barry Song <21cnbao@gmail.com> Cc: John Hubbard , Andrew Morton , Zenghui Yu , Matthew Wilcox , Kefeng Wang , Zi Yan , Alistair Popple , linux-mm@kvack.org References: <20240102153828.1002295-1-ryan.roberts@arm.com> <4e7445a0-acc9-487f-999f-a2b6d03d265e@nvidia.com> <3bd5e4a3-9f67-4483-9a0e-9abb5eb783cd@arm.com> <94ebe62b-5f55-4be9-b464-4105b4692496@arm.com> <68d5ce7e-6587-47c6-bd0f-988adf5d92a4@arm.com> <714438db-b83b-41e7-bf73-b95267fa34c2@redhat.com> From: Ryan Roberts In-Reply-To: <714438db-b83b-41e7-bf73-b95267fa34c2@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 5189840024 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: dhap5kkhnguaqowifp6uk1p4d7sdj5zx X-HE-Tag: 1704884335-83631 X-HE-Meta: U2FsdGVkX18Qr7rFom4i5GnqcQC/l7E/bGvxopW2R1FIGp18LAElbIsLYZ1XMUXMT2vIAbOIKWmKbFJujYXj6BMWAdwKa8FE1l69TfiQSYmZ/LqbUgHbPDRwQA8ThIDA46vNzOYxtHtSBCwysRlX5BTA9ILnmZP/dXVNPQ/D3soZv9JjQAGhfMHzK0VdvDXIfZ0QQb6RImgjLkxL3E6jUdTI0vetkboIUtkJ7fdtSR08KAwaOWHgQn99ONU/h82GF4+d6Niv3HTKMvzOk0iQREBc6RtM5oNy1t6kbYOl+OOPKQxRI1I6BFkUGamtFLPsU6ZGxCFXhMgInDZjUB0Jp1TYabkU7cZQMR0siP5AhTVwK87SaFjI8VFtpndQZNypckaqfbUJVbBRI5Jcs8bKLglr/DQ8tPmEkCjiSx9f5azsNxwdCQcg6vaKjMte9m9e19TmI7OXqnrIHZaS+m7TmUVUCJOOq+W2MiUou0vQxAUNh3e3BEdTJwSpG41T4xWRK6yvhOeqHzuCBvy0JRXA44b3cWXCBSCbXAx2b92YTiTVXpg8LXUq3QsI5KL/hyrR2rgKt5XBuTBAr9ahp/CQfY1YaYOHyTsQDTSKTFLaOdqRcNbDUiU0INiaER21VCQxLi4+SJFMuFL+Z3MYxd0rLvWAayVGlcN5mW2gfkMP8itByBAAGWPwe1LuXGo76Hw+WGHzrtM35TlKpqqAfAT6JWW4QlrPJtFI5M7Yvc0+fvd6e0YUaDZesIVPMx4WZqG643rH5VPbXWmPXkMGV0Pa/vwo9cvEW55ppVt62L9sA/Rc/55huTmSQ9i7tYAw1WVa70JctYg0fLSAHfuln/JR2tn9UGGBOuvi82KVBL0YbezQD7VVoI/0SvvoCvDEYK3Q4LM2R4UpIkVOFcEeqkf3FY99XPUuR516L/hznUefh1AMcfMDnT5JnMpbumEKb62k0VCLa3NhURQNtvSj6kT fo/rteFl fhKD/RMNGrIXi3DOQtEy3BWv+iSSgzo0ZT6NOjqK960PtmnGETHzI/UbiCn4YcDiVSy0nCeY1bXuZWLrJCyXWoVpYW2Euc9mvBPv2N1tNGl5JLRLIGGrS9EmdklQzwiASxOsil5qrdAJYbMJ3j9sXKfr1eK6a2ze6A3/YbkJ+un5H+2Jr6WDNgFLyYaXaD4ueT3vHvFgXkiQGQBTZJ/IBQhFc5ldleAay7JY2 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: List-Subscribe: List-Unsubscribe: On 10/01/2024 10:54, David Hildenbrand wrote: > On 10.01.24 11:48, Barry Song wrote: >> On Wed, Jan 10, 2024 at 6:38 PM Ryan Roberts wrote: >>> >>> On 10/01/2024 10:30, Barry Song wrote: >>>> On Wed, Jan 10, 2024 at 6:23 PM Ryan Roberts wrote: >>>>> >>>>> On 10/01/2024 09:09, Barry Song wrote: >>>>>> On Wed, Jan 10, 2024 at 4:58 PM Ryan Roberts wrote: >>>>>>> >>>>>>> On 10/01/2024 08:02, Barry Song wrote: >>>>>>>> On Wed, Jan 10, 2024 at 12:16 PM John Hubbard wrote: >>>>>>>>> >>>>>>>>> On 1/9/24 19:51, Barry Song wrote: >>>>>>>>>> On Wed, Jan 10, 2024 at 11:35 AM John Hubbard >>>>>>>>>> wrote: >>>>>>>>> ... >>>>>>>>>>> Hi Ryan, >>>>>>>>>>> >>>>>>>>>>> One thing that immediately came up during some recent testing of mTHP >>>>>>>>>>> on arm64: the pid requirement is sometimes a little awkward. I'm running >>>>>>>>>>> tests on a machine at a time for now, inside various containers and >>>>>>>>>>> such, and it would be nice if there were an easy way to get some numbers >>>>>>>>>>> for the mTHPs across the whole machine. >>>>>>> >>>>>>> Just to confirm, you're expecting these "global" stats be truely global >>>>>>> and not >>>>>>> per-container? (asking because you exploicitly mentioned being in a >>>>>>> container). >>>>>>> If you want per-container, then you can probably just create the >>>>>>> container in a >>>>>>> cgroup? >>>>>>> >>>>>>>>>>> >>>>>>>>>>> I'm not sure if that changes anything about thpmaps here. Probably >>>>>>>>>>> this is fine as-is. But I wanted to give some initial reactions from >>>>>>>>>>> just some quick runs: the global state would be convenient. >>>>>>> >>>>>>> Thanks for taking this for a spin! Appreciate the feedback. >>>>>>> >>>>>>>>>> >>>>>>>>>> +1. but this seems to be impossible by scanning pagemap? >>>>>>>>>> so may we add this statistics information in kernel just like >>>>>>>>>> /proc/meminfo or a separate /proc/mthp_info? >>>>>>>>>> >>>>>>>>> >>>>>>>>> Yes. From my perspective, it looks like the global stats are more useful >>>>>>>>> initially, and the more detailed per-pid or per-cgroup stats are the >>>>>>>>> next level of investigation. So feels odd to start with the more >>>>>>>>> detailed stats. >>>>>>>>> >>>>>>>> >>>>>>>> probably because this can be done without the modification of the kernel. >>>>>>> >>>>>>> Yes indeed, as John said in an earlier thread, my previous attempts to >>>>>>> add stats >>>>>>> directly in the kernel got pushback; DavidH was concerned that we don't >>>>>>> really >>>>>>> know exectly how to account mTHPs yet >>>>>>> (whole/partial/aligned/unaligned/per-size/etc) so didn't want to end up >>>>>>> adding >>>>>>> the wrong ABI and having to maintain it forever. There has also been some >>>>>>> pushback regarding adding more values to multi-value files in sysfs, so >>>>>>> David >>>>>>> was suggesting coming up with a whole new scheme at some point (I know >>>>>>> /proc/meminfo isn't sysfs, but the equivalent files for NUMA nodes and >>>>>>> cgroups >>>>>>> do live in sysfs). >>>>>>> >>>>>>> Anyway, this script was my attempt to 1) provide a short term solution to >>>>>>> the >>>>>>> "we need some stats" request and 2) provide a context in which to explore >>>>>>> what >>>>>>> the right stats are - this script can evolve without the ABI problem. >>>>>>> >>>>>>>> The detailed per-pid or per-cgroup is still quite useful to my case in >>>>>>>> which >>>>>>>> we set mTHP enabled/disabled and allowed sizes according to vma types, >>>>>>>> eg. libc_malloc, java heaps etc. >>>>>>>> >>>>>>>> Different vma types can have different anon_name. So I can use the detailed >>>>>>>> info to find out if specific VMAs have gotten mTHP properly and how many >>>>>>>> they have gotten. >>>>>>>> >>>>>>>>> However, Ryan did clearly say, above, "In future we may wish to >>>>>>>>> introduce stats directly into the kernel (e.g. smaps or similar)". And >>>>>>>>> earlier he ran into some pushback on trying to set up /proc or /sys >>>>>>>>> values because this is still such an early feature. >>>>>>>>> >>>>>>>>> I wonder if we could put the global stats in debugfs for now? That's >>>>>>>>> specifically supposed to be a "we promise *not* to keep this ABI stable" >>>>>>>>> location. >>>>>>> >>>>>>> Now that I think about it, I wonder if we can add a --global mode to the >>>>>>> script >>>>>>> (or just infer global when neither --pid nor --cgroup are provided). I >>>>>>> think I >>>>>>> should be able to determine all the physical memory ranges from /proc/iomem, >>>>>>> then grab all the info we need from /proc/kpageflags. We should then be >>>>>>> able to >>>>>>> process it all in much the same way as for --pid/--cgroup and provide the >>>>>>> same >>>>>>> stats, but it will apply globally. What do you think? >>>>> >>>>> Having now thought about this for a few mins (in the shower, if anyone >>>>> wants the >>>>> complete picture :) ), this won't quite work. This approach doesn't have the >>>>> virtual mapping information so the best it can do is tell us "how many of each >>>>> size of THP are allocated?" - it doesn't tell us anything about whether >>>>> they are >>>>> fully or partially mapped or what their alignment is (all necessary if we want >>>>> to know if they are contpte-mapped). So I don't think this approach is >>>>> going to >>>>> be particularly useful. >>>>> >>>>> And this is also the big problem if we want to gather stats inside the kernel; >>>>> if we want something equivalant to /proc/meminfo's >>>>> AnonHugePages/ShmemPmdMapped/FilePmdMapped, we need to consider not just the >>>>> allocation of the THP but also whether it is mapped. That's easy for >>>>> PMD-mappings, because there is only one entry to consider - when you set >>>>> it, you >>>>> increment the number of PMD-mapped THPs, when you clear it, you decrement. But >>>>> for PTE-mappings it's harder; you know the size when you are mapping so its >>>>> easy >>>>> to increment, but you can do a partial unmap, so you would need to scan the >>>>> PTEs >>>>> to figure out if we are unmapping the first page of a previously >>>>> fully-PTE-mapped THP, which is expensive. We would need a cheap mechanism to >>>>> determine "is this folio fully and contiguously mapped in at least one >>>>> process?". >>>> >>>> as OPPO's approach I shared to you before is maintaining two mapcount >>>> 1. entire map >>>> 2. subpage's map >>>> 3. if 1 and 2 both exist, it is DoubleMapped. >>>> >>>> This isn't a problem for us. and everytime if we do a partial unmap, >>>> we have an explicit >>>> cont_pte split which will decrease the entire map and increase the >>>> subpage's mapcount. >>>> >>>> but its downside is that we expose this info to mm-core. >>> >>> OK, but I think we have a slightly more generic situation going on with the >>> upstream; If I've understood correctly, you are using the PTE_CONT bit in the >>> PTE to determne if its fully mapped? That works for your case where you only >>> have 1 size of THP that you care about (contpte-size). But for the upstream, we >>> have multi-size THP so we can't use the PTE_CONT bit to determine if its fully >>> mapped because we can only use that bit if the THP is at least 64K and aligned, >>> and only on arm64. We would need a SW bit for this purpose, and the mm would >>> need to update that SW bit for every PTE one the full -> partial map transition. >> >> My current implementation does use cont_pte but i don't think it is a must-have. >> we don't need a bit in PTE to know if we are partially unmapping a large folio >> at all. >> >> as long as we are unmapping a part of a large folio, we do know what we are >> doing. if a large folio is mapped entirely in a process, we get only >> entire_map +1, >> if we are unmapping a subpage of it, we get entire_map -1 and remained subpage's >> mapcount + 1. if we are only mapping a part of this large folio, we >> only increase >> its subpages' mapcount. > > That doesn't work as soon as you unmap a second subpage. Not to mention that > people ( :) ) are working on removing the subpage mapcounts. Yes, that was my point - Oppo's implementation relies on the bit in the PTE to tell the difference between unmapping the first subpage and unmapping the others. We don't have that luxury here. > > I'm going propose that as a topic for LSF/MM soon, once I get to it. >