linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Ryan Roberts <ryan.roberts@arm.com>
To: Barry Song <21cnbao@gmail.com>
Cc: David Hildenbrand <david@redhat.com>,
	John Hubbard <jhubbard@nvidia.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Zenghui Yu <yuzenghui@huawei.com>,
	Matthew Wilcox <willy@infradead.org>,
	Kefeng Wang <wangkefeng.wang@huawei.com>, Zi Yan <ziy@nvidia.com>,
	Alistair Popple <apopple@nvidia.com>,
	linux-mm@kvack.org
Subject: Re: [RFC PATCH v1] tools/mm: Add thpmaps script to dump THP usage info
Date: Fri, 12 Jan 2024 10:25:54 +0000	[thread overview]
Message-ID: <ccacc8d1-9157-4548-8c8a-04c9e67d3d5c@arm.com> (raw)
In-Reply-To: <CAGsJ_4zzm44SLzvQXQkxoiE8SUXeODBTgXTS+3LpPg5UvLANwQ@mail.gmail.com>

On 11/01/2024 20:45, Barry Song wrote:
> On Fri, Jan 12, 2024 at 1:25 AM Ryan Roberts <ryan.roberts@arm.com> wrote:
>>
>> On 10/01/2024 22:14, Barry Song wrote:
>>> On Wed, Jan 10, 2024 at 7:59 PM Ryan Roberts <ryan.roberts@arm.com> wrote:
>>>>
>>>> On 10/01/2024 11:38, Barry Song wrote:
>>>>> On Wed, Jan 10, 2024 at 7:21 PM Ryan Roberts <ryan.roberts@arm.com> wrote:
>>>>>>
>>>>>> On 10/01/2024 11:00, David Hildenbrand wrote:
>>>>>>> On 10.01.24 11:55, Ryan Roberts wrote:
>>>>>>>> On 10/01/2024 10:42, David Hildenbrand wrote:
>>>>>>>>> On 10.01.24 11:38, Ryan Roberts wrote:
>>>>>>>>>> On 10/01/2024 10:30, Barry Song wrote:
>>>>>>>>>>> On Wed, Jan 10, 2024 at 6:23 PM Ryan Roberts <ryan.roberts@arm.com> wrote:
>>>>>>>>>>>>
>>>>>>>>>>>> On 10/01/2024 09:09, Barry Song wrote:
>>>>>>>>>>>>> On Wed, Jan 10, 2024 at 4:58 PM Ryan Roberts <ryan.roberts@arm.com> wrote:
>>>>>>>>>>>>>>
>>>>>>>>>>>>>> On 10/01/2024 08:02, Barry Song wrote:
>>>>>>>>>>>>>>> On Wed, Jan 10, 2024 at 12:16 PM John Hubbard <jhubbard@nvidia.com> wrote:
>>>>>>>>>>>>>>>>
>>>>>>>>>>>>>>>> On 1/9/24 19:51, Barry Song wrote:
>>>>>>>>>>>>>>>>> On Wed, Jan 10, 2024 at 11:35 AM John Hubbard <jhubbard@nvidia.com>
>>>>>>>>>>>>>>>>> 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.
>>>>>>>>>
>>>>>>>>> Oh no. Let's not make everything more complicated for the purpose of some stats.
>>>>>>>>>
>>>>>>>>
>>>>>>>> Indeed, I was intending to argue *against* doing it this way. Fundamentally, if
>>>>>>>> we want to know what's fully mapped and what's not, then I don't see any way
>>>>>>>> other than by scanning the page tables and we might as well do that in user
>>>>>>>> space with this script.
>>>>>>>>
>>>>>>>> Although, I expect you will shortly make a proposal that is simple to implement
>>>>>>>> and prove me wrong ;-)
>>>>>>>
>>>>>>> Unlikely :) As you said, once you have multiple folio sizes, it stops really
>>>>>>> making sense.
>>>>>>>
>>>>>>> Assume you have a 128 kiB pageache folio, and half of that is mapped. You can
>>>>>>> set cont-pte bits on that half and all is fine. Or AMD can benefit from it's
>>>>>>> optimizations without the cont-pte bit and everything is fine.
>>>>>>
>>>>>> Yes, but for debug and optimization, its useful to know when THPs are
>>>>>> fully/partially mapped, when they are unaligned etc. Anyway, the script does
>>>>>> that for us, and I think we are tending towards agreement that there are
>>>>>> unlikely to be any cost benefits by moving it into the kernel.
>>>>>
>>>>> frequent partial unmap can defeat all purpose for us to use large folios.
>>>>> just imagine a large folio can soon be splitted after it is formed. we lose
>>>>> the performance gain and might get regression instead.
>>>>
>>>> nit: just because a THP gets partially unmapped in a process doesn't mean it
>>>> gets split into order-0 pages. If the folio still has all its pages mapped at
>>>> least once then no further action is taken. If the page being unmapped was the
>>>> last mapping of that page, then the THP is put on the deferred split queue, so
>>>> that it can be split in future if needed.
>>>>>
>>>>> and this can be very frequent, for example, one userspace heap management
>>>>> is releasing memory page by page.
>>>>>
>>>>> In our real product deployment, we might not care about the second partial
>>>>> unmapped,  we do care about the first partial unmapped as we can use this
>>>>> to know if split has ever happened on this large folios. an partial unmapped
>>>>> subpage can be unlikely re-mapped back.
>>>>>
>>>>> so i guess 1st unmap is probably enough, at least for my product. I mean we
>>>>> care about if partial unmap has ever happened on a large folio more than how
>>>>> they are exactly partially unmapped :-)
>>>>
>>>> I'm not sure what you are suggesting here? A global boolean that tells you if
>>>> any folio in the system has ever been partially unmapped? That will almost
>>>> certainly always be true, even for a very well tuned system.
>>>
>>> not a global boolean but a per-folio boolean. in case userspace maps a region
>>> and has no userspace management, then we are fine as it is unlikely to have
>>> partial unmap/map things; in case userspace maps a region, but manages it
>>> by itself, such as heap things, we might result in lots of partial map/unmap,
>>> which can lead to 3 problems:
>>> 1. potential memory footprint increase, for example, while userspace releases
>>> some pages in a folio, we might still keep it as frequent splitting folio into
>>> basepages and releasing the unmapped subpage might be too expensive.
>>> 2. if cont-pte is involved, frequent dropping cont-pte/tlb shootdown
>>> might happen.
>>> 3. other maintenance overhead such as splitting large folios etc.
>>>
>>> We'd like to know how serious partial map things are happening. so either
>>> we will disable mTHP in this kind of VMAs, or optimize userspace to do
>>> some alignment according to the size of large folios.
>>>
>>> in android phones, we detect lots of apps, and also found some apps might
>>> do things like
>>> 1. mprotect on some pages within a large folio
>>> 2. mlock on some pages within a large folio
>>> 3. madv_free on some pages within a large folio
>>> 4. madv_pageout on some pages within a large folio.
>>>
>>> it would be good if we have a per-folio boolean to know how serious userspace
>>> is breaking the large folios. for example, if more than 50% folios in a vma has
>>> this problem, we can find it out and take some action.
>>
>> The high level value of these stats seems clear - I agree we need to be able to
>> get these insights. I think the issues are more around the implementation
>> though. I'm struggling to understand exactly how we could implement a lot of
>> these things cheaply (either in the kernel or in user space).
>>
>> Let me try to work though what I think you are suggesting:
>>
>>  - every THP is initially fully mapped
>>  - when an operation causes a partial unmap, mark the folio as having at least
>>    one partial mapping
>>  - on transition from "no partial mappings" to "at least one partial mapping"
>>    increment a "anon-partial-<size>kB" (one for each supported folio size)
>>    counter by the folio size
>>  - on transition from "at least one partial mapping" to "fully unampped
>>    everywhere" decrement the counter by the folio size
>>
>> I think the issue with this is that a folio that is fully mapped in a process
>> that gets forked, then is partially unmapped in 1 process, will be accounted as
>> partially mapped even after the process that partially unmapped it exits, even
>> though that folio is now fully mapped in all processes that map it. Is that a
>> problem, perhaps not? I'm not sure.
> 
> I don't think this is a problem as what we really care about is if some "bad"
> behaviour has ever happened in userspace. Though the "bad" guy has exited
> or been killed, we still need the record to find out.
> 
> except the global count, if we can reflect the partially mapped count in  vma
> such as smaps, this will help even more to locate the problematic userspace
> code.

Right. Although note that smaps is already scanning the page table, so for the
smaps case we could do it precisely - it's already slow. The thpmaps script
already gives a precise account of partially mapped THPs, FYI.

> 
> Thanks
> Barry



  reply	other threads:[~2024-01-12 10:26 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-02 15:38 Ryan Roberts
2024-01-03  6:44 ` Barry Song
2024-01-03  8:07   ` William Kucharski
2024-01-03  8:24     ` Ryan Roberts
2024-01-03  9:16       ` Barry Song
2024-01-03  9:35         ` Ryan Roberts
2024-01-03 10:09           ` William Kucharski
2024-01-03 10:20             ` Ryan Roberts
2024-01-04 22:48               ` John Hubbard
2024-01-05  8:35                 ` Ryan Roberts
2024-01-05 11:30                   ` William Kucharski
2024-01-05 23:07                     ` John Hubbard
2024-01-05 23:18                   ` John Hubbard
2024-01-10  8:43                     ` Ryan Roberts
2024-01-05  8:40 ` Ryan Roberts
2024-01-10  3:34 ` John Hubbard
2024-01-10  3:51   ` Barry Song
2024-01-10  4:15     ` John Hubbard
2024-01-10  8:02       ` Barry Song
2024-01-10  8:58         ` Ryan Roberts
2024-01-10  9:09           ` Barry Song
2024-01-10  9:20             ` Ryan Roberts
2024-01-10 10:23             ` Ryan Roberts
2024-01-10 10:30               ` Barry Song
2024-01-10 10:38                 ` Ryan Roberts
2024-01-10 10:42                   ` David Hildenbrand
2024-01-10 10:55                     ` Ryan Roberts
2024-01-10 11:00                       ` David Hildenbrand
2024-01-10 11:20                         ` Ryan Roberts
2024-01-10 11:24                           ` David Hildenbrand
2024-01-10 11:38                           ` Barry Song
2024-01-10 11:59                             ` Ryan Roberts
2024-01-10 12:05                               ` Barry Song
2024-01-10 12:12                                 ` David Hildenbrand
2024-01-10 15:19                                   ` Zi Yan
2024-01-10 15:27                                     ` David Hildenbrand
2024-01-10 22:14                               ` Barry Song
2024-01-11 12:25                                 ` Ryan Roberts
2024-01-11 13:18                                   ` David Hildenbrand
2024-01-11 20:21                                     ` Barry Song
2024-01-11 20:28                                       ` David Hildenbrand
2024-01-12  6:03                                         ` Barry Song
2024-01-12 10:44                                           ` Ryan Roberts
2024-01-12 10:18                                     ` Ryan Roberts
2024-01-17 15:49                                       ` David Hildenbrand
2024-01-11 20:45                                   ` Barry Song
2024-01-12 10:25                                     ` Ryan Roberts [this message]
2024-01-10 23:34                           ` Barry Song
2024-01-10 10:48                   ` Barry Song
2024-01-10 10:54                     ` David Hildenbrand
2024-01-10 10:58                       ` Ryan Roberts
2024-01-10 11:02                         ` David Hildenbrand
2024-01-10 11:07                         ` Barry Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ccacc8d1-9157-4548-8c8a-04c9e67d3d5c@arm.com \
    --to=ryan.roberts@arm.com \
    --cc=21cnbao@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=david@redhat.com \
    --cc=jhubbard@nvidia.com \
    --cc=linux-mm@kvack.org \
    --cc=wangkefeng.wang@huawei.com \
    --cc=willy@infradead.org \
    --cc=yuzenghui@huawei.com \
    --cc=ziy@nvidia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox