• Barry Song's avatar
    mm: count the number of anonymous THPs per size · 5d65c8d7
    Barry Song authored
    Patch series "mm: count the number of anonymous THPs per size", v4.
    
    Knowing the number of transparent anon THPs in the system is crucial
    for performance analysis. It helps in understanding the ratio and
    distribution of THPs versus small folios throughout the system.
    
    Additionally, partial unmapping by userspace can lead to significant waste
    of THPs over time and increase memory reclamation pressure. We need this
    information for comprehensive system tuning.
    
    
    This patch (of 2):
    
    Let's track for each anonymous THP size, how many of them are currently
    allocated.  We'll track the complete lifespan of an anon THP, starting
    when it becomes an anon THP ("large anon folio") (->mapping gets set),
    until it gets freed (->mapping gets cleared).
    
    Introduce a new "nr_anon" counter per THP size and adjust the
    corresponding counter in the following cases:
    * We allocate a new THP and call folio_add_new_anon_rmap() to map
       it the first time and turn it into an anon THP.
    * We split an anon THP into multiple smaller ones.
    * We migrate an anon THP, when we prepare the destination.
    * We free an anon THP back to the buddy.
    
    Note that AnonPages in /proc/meminfo currently tracks the total number of
    *mapped* anonymous *pages*, and therefore has slightly different
    semantics.  In the future, we might also want to track "nr_anon_mapped"
    for each THP size, which might be helpful when comparing it to the number
    of allocated anon THPs (long-term pinning, stuck in swapcache, memory
    leaks, ...).
    
    Further note that for now, we only track anon THPs after they got their
    ->mapping set, for example via folio_add_new_anon_rmap().  If we would
    allocate some in the swapcache, they will only show up in the statistics
    for now after they have been mapped to user space the first time, where we
    call folio_add_new_anon_rmap().
    
    [akpm@linux-foundation.org: documentation fixups, per David]
      Link: https://lkml.kernel.org/r/3e8add35-e26b-443b-8a04-1078f4bc78f6@redhat.com
    Link: https://lkml.kernel.org/r/20240824010441.21308-1-21cnbao@gmail.com
    Link: https://lkml.kernel.org/r/20240824010441.21308-2-21cnbao@gmail.comSigned-off-by: default avatarBarry Song <v-songbaohua@oppo.com>
    Acked-by: default avatarDavid Hildenbrand <david@redhat.com>
    Cc: Baolin Wang <baolin.wang@linux.alibaba.com>
    Cc: Chris Li <chrisl@kernel.org>
    Cc: Chuanhua Han <hanchuanhua@oppo.com>
    Cc: Kairui Song <kasong@tencent.com>
    Cc: Kalesh Singh <kaleshsingh@google.com>
    Cc: Lance Yang <ioworker0@gmail.com>
    Cc: Ryan Roberts <ryan.roberts@arm.com>
    Cc: Shuai Yuan <yuanshuai@oppo.com>
    Cc: Usama Arif <usamaarif642@gmail.com>
    Cc: Zi Yan <ziy@nvidia.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    5d65c8d7
migrate.c 72.1 KB