All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 0/6] mm: split underused THPs
@ 2024-08-19  2:30 Usama Arif
  2024-08-19  2:30 ` [PATCH v4 1/6] mm: free zapped tail pages when splitting isolated thp Usama Arif
                   ` (5 more replies)
  0 siblings, 6 replies; 23+ messages in thread
From: Usama Arif @ 2024-08-19  2:30 UTC (permalink / raw)
  To: akpm, linux-mm
  Cc: hannes, riel, shakeel.butt, roman.gushchin, yuzhao, david, baohua,
	ryan.roberts, rppt, willy, cerasuolodomenico, ryncsn, corbet,
	linux-kernel, linux-doc, kernel-team, Usama Arif

The current upstream default policy for THP is always. However, Meta
uses madvise in production as the current THP=always policy vastly
overprovisions THPs in sparsely accessed memory areas, resulting in
excessive memory pressure and premature OOM killing.
Using madvise + relying on khugepaged has certain drawbacks over
THP=always. Using madvise hints mean THPs aren't "transparent" and
require userspace changes. Waiting for khugepaged to scan memory and
collapse pages into THP can be slow and unpredictable in terms of performance
(i.e. you dont know when the collapse will happen), while production
environments require predictable performance. If there is enough memory
available, its better for both performance and predictability to have
a THP from fault time, i.e. THP=always rather than wait for khugepaged
to collapse it, and deal with sparsely populated THPs when the system is
running out of memory.

This patch-series is an attempt to mitigate the issue of running out of
memory when THP is always enabled. During runtime whenever a THP is being
faulted in or collapsed by khugepaged, the THP is added to a list.
Whenever memory reclaim happens, the kernel runs the deferred_split
shrinker which goes through the list and checks if the THP was underused,
i.e. how many of the base 4K pages of the entire THP were zero-filled.
If this number goes above a certain threshold, the shrinker will attempt
to split that THP. Then at remap time, the pages that were zero-filled are
mapped to the shared zeropage, hence saving memory. This method avoids the
downside of wasting memory in areas where THP is sparsely filled when THP
is always enabled, while still providing the upside THPs like reduced TLB
misses without having to use madvise.

Meta production workloads that were CPU bound (>99% CPU utilzation) were
tested with THP shrinker. The results after 2 hours are as follows:

                            | THP=madvise |  THP=always   | THP=always
                            |             |               | + shrinker series
                            |             |               | + max_ptes_none=409
-----------------------------------------------------------------------------
Performance improvement     |      -      |    +1.8%      |     +1.7%
(over THP=madvise)          |             |               |
-----------------------------------------------------------------------------
Memory usage                |    54.6G    | 58.8G (+7.7%) |   55.9G (+2.4%)
-----------------------------------------------------------------------------
max_ptes_none=409 means that any THP that has more than 409 out of 512
(80%) zero filled filled pages will be split.

To test out the patches, the below commands without the shrinker will
invoke OOM killer immediately and kill stress, but will not fail with
the shrinker:

echo 450 > /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none
mkdir /sys/fs/cgroup/test
echo $$ > /sys/fs/cgroup/test/cgroup.procs
echo 20M > /sys/fs/cgroup/test/memory.max
echo 0 > /sys/fs/cgroup/test/memory.swap.max
# allocate twice memory.max for each stress worker and touch 40/512 of
# each THP, i.e. vm-stride 50K.
# With the shrinker, max_ptes_none of 470 and below won't invoke OOM
# killer.
# Without the shrinker, OOM killer is invoked immediately irrespective
# of max_ptes_none value and kills stress.
stress --vm 1 --vm-bytes 40M --vm-stride 50K

v3 -> v4:
- do not clear partially_mapped flag on hugeTLB folios (Yu Zhao).
- fix condition for calling deferred_folio_split in partially mapped case
  and count for partially mapped vm events (Barry Song).
- use non-atomic versions of set/clear partially_mapped flags
  (David Hildenbrand)
- use PG_partially_mapped = PG_reclaim (Matthew Wilcox)
- delete folio from lru list and folio_batch_add "new_folio" instead
  of folio in __split_huge_page. (Kairui Song)
- fix deadlock in deferred_split_scan by not doing folio_put while
  holding split_queue_lock (Hugh Dickins)
- underutilized to underused and thp_low_util_shrinker to shrink_underused
  (Hugh Dickins)

v2 -> v3:
- Use my_zero_pfn instead of page_to_pfn(ZERO_PAGE(..)) (Johannes)
- Use flags argument instead of bools in remove_migration_ptes (Johannes)
- Use a new flag in folio->_flags_1 instead of folio->_partially_mapped
  (David Hildenbrand).
- Split out the last patch of v2 into 3, one for introducing the flag,
  one for splitting underutilized THPs on _deferred_list and one for adding
  sysfs entry to disable splitting (David Hildenbrand).

v1 -> v2:
- Turn page checks and operations to folio versions in __split_huge_page.
  This means patches 1 and 2 from v1 are no longer needed.
  (David Hildenbrand)
- Map to shared zeropage in all cases if the base page is zero-filled.
  The uffd selftest was removed.
  (David Hildenbrand).
- rename 'dirty' to 'contains_data' in try_to_map_unused_to_zeropage
  (Rik van Riel).
- Use unsigned long instead of uint64_t (kernel test robot).

Alexander Zhu (1):
  mm: selftest to verify zero-filled pages are mapped to zeropage

Usama Arif (3):
  mm: Introduce a pageflag for partially mapped folios
  mm: split underused THPs
  mm: add sysfs entry to disable splitting underused THPs

Yu Zhao (2):
  mm: free zapped tail pages when splitting isolated thp
  mm: remap unused subpages to shared zeropage when splitting isolated
    thp

 Documentation/admin-guide/mm/transhuge.rst    |  16 ++
 include/linux/huge_mm.h                       |   4 +-
 include/linux/khugepaged.h                    |   1 +
 include/linux/page-flags.h                    |  11 ++
 include/linux/rmap.h                          |   7 +-
 include/linux/vm_event_item.h                 |   1 +
 mm/huge_memory.c                              | 145 ++++++++++++++++--
 mm/internal.h                                 |   4 +-
 mm/khugepaged.c                               |   3 +-
 mm/memcontrol.c                               |   3 +-
 mm/migrate.c                                  |  75 +++++++--
 mm/migrate_device.c                           |   4 +-
 mm/page_alloc.c                               |   5 +-
 mm/rmap.c                                     |   5 +-
 mm/vmscan.c                                   |   3 +-
 mm/vmstat.c                                   |   1 +
 .../selftests/mm/split_huge_page_test.c       |  71 +++++++++
 tools/testing/selftests/mm/vm_util.c          |  22 +++
 tools/testing/selftests/mm/vm_util.h          |   1 +
 19 files changed, 346 insertions(+), 36 deletions(-)

-- 
2.43.5


^ permalink raw reply	[flat|nested] 23+ messages in thread
* Re: [PATCH v4 3/6] mm: selftest to verify zero-filled pages are mapped to zeropage
@ 2024-08-21  7:09 kernel test robot
  0 siblings, 0 replies; 23+ messages in thread
From: kernel test robot @ 2024-08-21  7:09 UTC (permalink / raw)
  To: oe-kbuild; +Cc: lkp

:::::: 
:::::: Manual check reason: "low confidence bisect report"
:::::: 

BCC: lkp@intel.com
CC: oe-kbuild-all@lists.linux.dev
In-Reply-To: <20240819023145.2415299-4-usamaarif642@gmail.com>
References: <20240819023145.2415299-4-usamaarif642@gmail.com>
TO: Usama Arif <usamaarif642@gmail.com>

Hi Usama,

kernel test robot noticed the following build errors:

[auto build test ERROR on akpm-mm/mm-everything]

url:    https://github.com/intel-lab-lkp/linux/commits/Usama-Arif/mm-free-zapped-tail-pages-when-splitting-isolated-thp/20240819-103336
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20240819023145.2415299-4-usamaarif642%40gmail.com
patch subject: [PATCH v4 3/6] mm: selftest to verify zero-filled pages are mapped to zeropage
:::::: branch date: 2 days ago
:::::: commit date: 2 days ago
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240821/202408211408.L2BFUig9-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/r/202408211408.L2BFUig9-lkp@intel.com/

All errors (new ones prefixed by >>):

>> vm_util.c:175:15: error: conflicting types for 'rss_anon'; have 'long unsigned int(void)'
     175 | unsigned long rss_anon(void)
         |               ^~~~~~~~
   In file included from vm_util.c:11:
   vm_util.h:42:10: note: previous declaration of 'rss_anon' with type 'uint64_t(void)' {aka 'long long unsigned int(void)'}
      42 | uint64_t rss_anon(void);
         |          ^~~~~~~~


vim +175 tools/testing/selftests/mm/vm_util.c

642bc52aed9c99e tools/testing/selftests/vm/vm_util.c Muhammad Usama Anjum 2022-04-28  174  
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19 @175  unsigned long rss_anon(void)
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  176  {
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  177  	unsigned long rss_anon = 0;
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  178  	FILE *fp;
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  179  	char buffer[MAX_LINE_LENGTH];
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  180  
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  181  	fp = fopen(STATUS_FILE_PATH, "r");
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  182  	if (!fp)
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  183  		ksft_exit_fail_msg("%s: Failed to open file %s\n", __func__, STATUS_FILE_PATH);
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  184  
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  185  	if (!check_for_pattern(fp, "RssAnon:", buffer, sizeof(buffer)))
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  186  		goto err_out;
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  187  
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  188  	if (sscanf(buffer, "RssAnon:%10lu kB", &rss_anon) != 1)
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  189  		ksft_exit_fail_msg("Reading status error\n");
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  190  
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  191  err_out:
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  192  	fclose(fp);
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  193  	return rss_anon;
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  194  }
10bf5ecc7e9d437 tools/testing/selftests/mm/vm_util.c Alexander Zhu        2024-08-19  195  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2024-09-02 10:09 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-19  2:30 [PATCH v4 0/6] mm: split underused THPs Usama Arif
2024-08-19  2:30 ` [PATCH v4 1/6] mm: free zapped tail pages when splitting isolated thp Usama Arif
2024-08-19  2:30 ` [PATCH v4 2/6] mm: remap unused subpages to shared zeropage " Usama Arif
2024-08-19  2:30 ` [PATCH v4 3/6] mm: selftest to verify zero-filled pages are mapped to zeropage Usama Arif
2024-08-21 19:09   ` Usama Arif
2024-08-23  4:01   ` kernel test robot
2024-08-19  2:30 ` [PATCH v4 4/6] mm: Introduce a pageflag for partially mapped folios Usama Arif
2024-08-19  8:29   ` Barry Song
2024-08-19  8:30     ` Barry Song
2024-08-19 14:17     ` Usama Arif
2024-08-19 19:00       ` Barry Song
2024-08-19 20:16         ` Usama Arif
2024-08-19 21:34           ` Barry Song
2024-08-19 21:55             ` Barry Song
2024-08-20 19:35               ` Usama Arif
2024-08-20 21:30                 ` Barry Song
2024-08-21 19:04                   ` Usama Arif
2024-08-21 21:25                     ` Barry Song
2024-09-01 12:48                     ` Bang Li
2024-09-02 10:09                       ` Usama Arif
2024-08-19  2:30 ` [PATCH v4 5/6] mm: split underused THPs Usama Arif
2024-08-19  2:30 ` [PATCH v4 6/6] mm: add sysfs entry to disable splitting " Usama Arif
  -- strict thread matches above, loose matches on Subject: below --
2024-08-21  7:09 [PATCH v4 3/6] mm: selftest to verify zero-filled pages are mapped to zeropage kernel test robot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.