All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/9] Free user PTE page table pages
@ 2021-08-19  3:18 Qi Zheng
  2021-08-19  3:18 ` [PATCH v2 1/9] mm: introduce pmd_install() helper Qi Zheng
                   ` (9 more replies)
  0 siblings, 10 replies; 39+ messages in thread
From: Qi Zheng @ 2021-08-19  3:18 UTC (permalink / raw)
  To: akpm, tglx, hannes, mhocko, vdavydov.dev, kirill.shutemov,
	mika.penttila, david
  Cc: linux-doc, linux-kernel, linux-mm, songmuchun, Qi Zheng

Hi,

This patch series aims to free user PTE page table pages when all PTE entries
are empty.

The beginning of this story is that some malloc libraries(e.g. jemalloc or
tcmalloc) usually allocate the amount of VAs by mmap() and do not unmap those VAs.
They will use madvise(MADV_DONTNEED) to free physical memory if they want.
But the page tables do not be freed by madvise(), so it can produce many
page tables when the process touches an enormous virtual address space.

The following figures are a memory usage snapshot of one process which actually
happened on our server:

	VIRT:  55t
	RES:   590g
	VmPTE: 110g

As we can see, the PTE page tables size is 110g, while the RES is 590g. In
theory, the process only need 1.2g PTE page tables to map those physical
memory. The reason why PTE page tables occupy a lot of memory is that
madvise(MADV_DONTNEED) only empty the PTE and free physical memory but
doesn't free the PTE page table pages. So we can free those empty PTE page
tables to save memory. In the above cases, we can save memory about 108g(best
case). And the larger the difference between the size of VIRT and RES, the
more memory we save.

In this patch series, we add a pte_refcount field to the struct page of page
table to track how many users of PTE page table. Similar to the mechanism of
page refcount, the user of PTE page table should hold a refcount to it before
accessing. The PTE page table page will be freed when the last refcount is
dropped.

Testing:

The following code snippet can show the effect of optimization:

	mmap 50G
	while (1) {
		for (; i < 1024 * 25; i++) {
			touch 2M memory
			madvise MADV_DONTNEED 2M
		}
	}

As we can see, the memory usage of VmPTE is reduced:

			before		                after
VIRT		       50.0 GB			      50.0 GB
RES		        3.1 MB			       3.6 MB
VmPTE		     102640 kB			       248 kB

I also have tested the stability by LTP[1] for several weeks. I have not seen
any crash so far.

The performance of page fault can be affected because of the allocation/freeing
of PTE page table pages. The following is the test result by using a micro
benchmark[2]:

root@~# perf stat -e page-faults --repeat 5 ./multi-fault $threads:

threads         before (pf/min)                     after (pf/min)
    1                32,085,255                         31,880,833 (-0.64%)
    8               101,674,967                        100,588,311 (-1.17%)
   16               113,207,000                        112,801,832 (-0.36%)

(The "pfn/min" means how many page faults in one minute.)

The performance of page fault is ~1% slower than before.

This series is based on next-20210812.

Comments and suggestions are welcome.

Thanks,
Qi.

[1] https://github.com/linux-test-project/ltp
[2] https://lore.kernel.org/patchwork/comment/296794/

Changelog in v1 -> v2:
 - Change pte_install() to pmd_install().
 - Fix some typo and code style problems.
 - Split [PATCH v1 5/7] into [PATCH v2 4/9], [PATCH v2 5/9],[PATCH v2 6/9]
   and [PATCH v2 7/9].

Qi Zheng (9):
  mm: introduce pmd_install() helper
  mm: remove redundant smp_wmb()
  mm: rework the parameter of lock_page_or_retry()
  mm: move pte_alloc{,_map,_map_lock}() to a separate file
  mm: pte_refcount infrastructure
  mm: free user PTE page table pages
  mm: add THP support for pte_ref
  mm: free PTE page table by using rcu mechanism
  mm: use mmu_gather to free PTE page table

 arch/arm/mm/pgd.c             |   1 +
 arch/arm64/mm/hugetlbpage.c   |   1 +
 arch/ia64/mm/hugetlbpage.c    |   1 +
 arch/parisc/mm/hugetlbpage.c  |   1 +
 arch/powerpc/mm/hugetlbpage.c |   1 +
 arch/s390/mm/gmap.c           |   1 +
 arch/s390/mm/pgtable.c        |   1 +
 arch/sh/mm/hugetlbpage.c      |   1 +
 arch/sparc/mm/hugetlbpage.c   |   1 +
 arch/x86/Kconfig              |   2 +-
 fs/proc/task_mmu.c            |  22 +++-
 fs/userfaultfd.c              |   2 +
 include/linux/mm.h            |  12 +-
 include/linux/mm_types.h      |   8 +-
 include/linux/pagemap.h       |   8 +-
 include/linux/pgtable.h       |   3 +-
 include/linux/pte_ref.h       | 300 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/rmap.h          |   4 +-
 kernel/events/uprobes.c       |   3 +
 mm/Kconfig                    |   4 +
 mm/Makefile                   |   3 +-
 mm/filemap.c                  |  57 ++++----
 mm/gup.c                      |   7 +
 mm/hmm.c                      |   4 +
 mm/internal.h                 |   2 +
 mm/khugepaged.c               |   9 ++
 mm/ksm.c                      |   4 +
 mm/madvise.c                  |  18 ++-
 mm/memcontrol.c               |  11 +-
 mm/memory.c                   | 276 ++++++++++++++++++++++++--------------
 mm/mempolicy.c                |   4 +-
 mm/migrate.c                  |  18 +--
 mm/mincore.c                  |   5 +-
 mm/mlock.c                    |   1 +
 mm/mmu_gather.c               |  40 +++---
 mm/mprotect.c                 |  10 +-
 mm/mremap.c                   |  12 +-
 mm/page_vma_mapped.c          |   4 +
 mm/pagewalk.c                 |  16 ++-
 mm/pgtable-generic.c          |   2 +
 mm/pte_ref.c                  | 143 ++++++++++++++++++++
 mm/rmap.c                     |  13 ++
 mm/sparse-vmemmap.c           |   2 +-
 mm/swapfile.c                 |   3 +-
 mm/userfaultfd.c              |  18 ++-
 45 files changed, 849 insertions(+), 210 deletions(-)
 create mode 100644 include/linux/pte_ref.h
 create mode 100644 mm/pte_ref.c

-- 
2.11.0


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

end of thread, other threads:[~2021-09-16  8:42 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-19  3:18 [PATCH v2 0/9] Free user PTE page table pages Qi Zheng
2021-08-19  3:18 ` [PATCH v2 1/9] mm: introduce pmd_install() helper Qi Zheng
2021-08-24 16:26   ` David Hildenbrand
2021-08-25 16:20     ` Qi Zheng
2021-08-25 16:32       ` David Hildenbrand
2021-08-26  3:04         ` Qi Zheng
2021-08-19  3:18 ` [PATCH v2 2/9] mm: remove redundant smp_wmb() Qi Zheng
2021-08-19  3:18 ` [PATCH v2 3/9] mm: rework the parameter of lock_page_or_retry() Qi Zheng
2021-08-19  3:18 ` [PATCH v2 4/9] mm: move pte_alloc{,_map,_map_lock}() to a separate file Qi Zheng
2021-08-19  3:18 ` [PATCH v2 5/9] mm: pte_refcount infrastructure Qi Zheng
2021-08-19  3:18 ` [PATCH v2 6/9] mm: free user PTE page table pages Qi Zheng
2021-08-19  7:01   ` David Hildenbrand
2021-08-19 10:18     ` [External] " Qi Zheng
2021-09-01 13:53   ` Jason Gunthorpe
2021-09-01 13:57     ` David Hildenbrand
2021-09-01 15:32       ` Jason Gunthorpe
2021-09-01 16:13         ` David Hildenbrand
2021-09-01 16:16           ` Jason Gunthorpe
2021-09-01 16:19             ` David Hildenbrand
2021-09-01 17:10               ` Jason Gunthorpe
2021-09-01 17:49                 ` David Hildenbrand
2021-09-01 17:55                   ` Jason Gunthorpe
2021-09-01 17:58                     ` David Hildenbrand
2021-09-01 18:09                       ` Jason Gunthorpe
2021-09-01 18:10                         ` David Hildenbrand
2021-09-02  7:04                     ` Qi Zheng
2021-09-02  6:53         ` Qi Zheng
2021-08-19  3:18 ` [PATCH v2 7/9] mm: add THP support for pte_ref Qi Zheng
2021-08-19  3:18 ` [PATCH v2 8/9] mm: free PTE page table by using rcu mechanism Qi Zheng
2021-08-19  3:18 ` [PATCH v2 9/9] mm: use mmu_gather to free PTE page table Qi Zheng
2021-09-01 12:32 ` [PATCH v2 0/9] Free user PTE page table pages David Hildenbrand
2021-09-01 16:07   ` Jason Gunthorpe
2021-09-01 16:10     ` David Hildenbrand
2021-09-02  3:37   ` Qi Zheng
2021-09-15 14:52   ` Qi Zheng
2021-09-15 14:59     ` Jason Gunthorpe
2021-09-16  5:32       ` Qi Zheng
2021-09-16  8:30         ` David Hildenbrand
2021-09-16  8:41           ` Qi Zheng

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.