mbox series

[RFC,0/3] asynchronously scan and free empty user PTE pages

Message ID cover.1718267194.git.zhengqi.arch@bytedance.com (mailing list archive)
Headers show
Series asynchronously scan and free empty user PTE pages | expand

Message

Qi Zheng June 13, 2024, 8:38 a.m. UTC
Hi all,

This series aims to asynchronously scan and free empty user PTE pages.

1. Background
=============

We often find huge user PTE memory usage on our servers, such as the following:

        VIRT:  55t
        RES:   590g
        VmPTE: 110g
        
The root cause is that these processes use some high-performance mmeory
allocators (such as jemalloc, tcmalloc, etc). These memory allocators use
madvise(MADV_DONTNEED or MADV_FREE) to release physical memory, but neither
MADV_DONTNEED nor MADV_FREE will release page table memory, which may cause
huge page table memory usage.

This issue has been discussed on LSFMM 2022 (led by David Hildenbrand):

        topic link: https://lore.kernel.org/linux-mm/7b908208-02f8-6fde-4dfc-13d5e00310a6@redhat.com/
        youtube link: https://www.youtube.com/watch?v=naO_BRhcU68
        
In the past, I have tried to introduce refcount for PTE pages to solve this
problem, but these methods [1][2][3] introduced too much complexity.

[1]. https://lore.kernel.org/lkml/20211110105428.32458-1-zhengqi.arch@bytedance.com/
[2]. https://lore.kernel.org/lkml/20220429133552.33768-1-zhengqi.arch@bytedance.com/
[3]. https://lore.kernel.org/lkml/20220825101037.96517-1-zhengqi.arch@bytedance.com/

2. Infrastructure
=================

Later, in order to freeing retracted page table, Hugh Dickins added a lot of
PTE-related infrastructure[4][5][6]:

    - allow pte_offset_map_lock() etc to fail
    - make PTE pages can be removed without mmap or rmap locks
      (see collapse_pte_mapped_thp() and retract_page_tables())
    - make PTE pages can be freed by RCU (via pte_free_defer())
    - etc
    
These are all beneficial to freeing empty PTE pages.

[4]. https://lore.kernel.org/all/a4963be9-7aa6-350-66d0-2ba843e1af44@google.com/
[5]. https://lore.kernel.org/all/c1c9a74a-bc5b-15ea-e5d2-8ec34bc921d@google.com/
[6]. https://lore.kernel.org/all/7cd843a9-aa80-14f-5eb2-33427363c20@google.com/

3. Implementation
=================

For empty user PTE pages, we don't actually need to free it immediately, nor do
we need to free all of it.

Therefore, in this patchset, we register a task_work for the user tasks to
asyncronously scan and free empty PTE pages when they return to user space.
(The scanning time interval and address space size can be adjusted.)

When scanning, we can filter out some unsuitable vmas:

    - VM_HUGETLB vma
    - VM_UFFD_WP vma
    - etc
    
And for some PTE pages that spans multiple vmas, we can also skip.

For locking:

    - use the mmap read lock to traverse the vma tree and pgtable
    - use pmd lock for clearing pmd entry
    - use pte lock for checking empty PTE page, and release it after clearing
      pmd entry, then we can capture the changed pmd in pte_offset_map_lock()
      etc after holding this pte lock. Thanks to this, we don't need to hold the
      rmap-related locks.
    - users of pte_offset_map_lock() etc all expect the PTE page to be stable by
      using rcu lock, so use pte_free_defer() to free PTE pages.
      
For the path that will also free PTE pages in THP, we need to recheck whether the
content of pmd entry is valid after holding pmd lock or pte lock.

4. TODO
=======

Some applications may be concerned about the overhead of scanning and rebuilding
page tables, so the following features are considered for implementation in the
future:

    - add per-process switch (via prctl)
    - add a madvise option (like THP)
    - add MM_PGTABLE_SCAN_DELAY/MM_PGTABLE_SCAN_SIZE control (via procfs file)
    
Perhaps we can add the refcount to PTE pages in the future as well, which would
help improve the scanning speed.

This series is based on next-20240612.

Comments and suggestions are welcome!

Thanks,
Qi

Qi Zheng (3):
  mm: pgtable: move pte_free_defer() out of CONFIG_TRANSPARENT_HUGEPAGE
  mm: pgtable: make pte_offset_map_nolock() return pmdval
  mm: free empty user PTE pages

 Documentation/mm/split_page_table_lock.rst |   3 +-
 arch/arm/mm/fault-armv.c                   |   2 +-
 arch/powerpc/mm/pgtable-frag.c             |   2 -
 arch/powerpc/mm/pgtable.c                  |   2 +-
 arch/s390/mm/pgalloc.c                     |   2 -
 arch/sparc/mm/init_64.c                    |   2 +-
 include/linux/mm.h                         |   4 +-
 include/linux/mm_types.h                   |   4 +
 include/linux/pgtable.h                    |  14 ++
 include/linux/sched.h                      |   1 +
 kernel/sched/core.c                        |   1 +
 kernel/sched/fair.c                        |   2 +
 mm/Makefile                                |   2 +-
 mm/filemap.c                               |   2 +-
 mm/freept.c                                | 180 +++++++++++++++++++++
 mm/khugepaged.c                            |  20 ++-
 mm/memory.c                                |   4 +-
 mm/mremap.c                                |   2 +-
 mm/page_vma_mapped.c                       |   2 +-
 mm/pgtable-generic.c                       |  23 +--
 mm/userfaultfd.c                           |   4 +-
 mm/vmscan.c                                |   2 +-
 22 files changed, 249 insertions(+), 31 deletions(-)
 create mode 100644 mm/freept.c

Comments

David Hildenbrand June 13, 2024, 9:04 a.m. UTC | #1
On 13.06.24 10:38, Qi Zheng wrote:
> Hi all,
> 
> This series aims to asynchronously scan and free empty user PTE pages.
> 
> 1. Background
> =============
> 
> We often find huge user PTE memory usage on our servers, such as the following:
> 
>          VIRT:  55t
>          RES:   590g
>          VmPTE: 110g
>          
> The root cause is that these processes use some high-performance mmeory
> allocators (such as jemalloc, tcmalloc, etc). These memory allocators use
> madvise(MADV_DONTNEED or MADV_FREE) to release physical memory, but neither
> MADV_DONTNEED nor MADV_FREE will release page table memory, which may cause
> huge page table memory usage.
> 
> This issue has been discussed on LSFMM 2022 (led by David Hildenbrand):
> 
>          topic link: https://lore.kernel.org/linux-mm/7b908208-02f8-6fde-4dfc-13d5e00310a6@redhat.com/
>          youtube link: https://www.youtube.com/watch?v=naO_BRhcU68
>          
> In the past, I have tried to introduce refcount for PTE pages to solve this
> problem, but these methods [1][2][3] introduced too much complexity.
> 
> [1]. https://lore.kernel.org/lkml/20211110105428.32458-1-zhengqi.arch@bytedance.com/
> [2]. https://lore.kernel.org/lkml/20220429133552.33768-1-zhengqi.arch@bytedance.com/
> [3]. https://lore.kernel.org/lkml/20220825101037.96517-1-zhengqi.arch@bytedance.com/
> 
> 2. Infrastructure
> =================
> 
> Later, in order to freeing retracted page table, Hugh Dickins added a lot of
> PTE-related infrastructure[4][5][6]:
> 
>      - allow pte_offset_map_lock() etc to fail
>      - make PTE pages can be removed without mmap or rmap locks
>        (see collapse_pte_mapped_thp() and retract_page_tables())
>      - make PTE pages can be freed by RCU (via pte_free_defer())
>      - etc
>      
> These are all beneficial to freeing empty PTE pages.
> 
> [4]. https://lore.kernel.org/all/a4963be9-7aa6-350-66d0-2ba843e1af44@google.com/
> [5]. https://lore.kernel.org/all/c1c9a74a-bc5b-15ea-e5d2-8ec34bc921d@google.com/
> [6]. https://lore.kernel.org/all/7cd843a9-aa80-14f-5eb2-33427363c20@google.com/
> 

I'm a big fan for virtio-mem.


> 3. Implementation
> =================
> 
> For empty user PTE pages, we don't actually need to free it immediately, nor do
> we need to free all of it.
> 
> Therefore, in this patchset, we register a task_work for the user tasks to
> asyncronously scan and free empty PTE pages when they return to user space.
> (The scanning time interval and address space size can be adjusted.)

The question is, if we really have to scan asynchronously, or if would 
be reasonable for most use cases to trigger a madvise(MADV_PT_RECLAIM) 
every now and then. For virtio-mem, and likely most memory allocators, 
that might be feasible, and valuable independent of system-wide 
automatic scanning.

> 
> When scanning, we can filter out some unsuitable vmas:
> 
>      - VM_HUGETLB vma
>      - VM_UFFD_WP vma

Why is UFFD_WP unsuitable? It should be suitable as long as you make 
sure to really only remove page tables that are all pte_none().

>      - etc
>      
> And for some PTE pages that spans multiple vmas, we can also skip.
> 
> For locking:
> 
>      - use the mmap read lock to traverse the vma tree and pgtable
>      - use pmd lock for clearing pmd entry
>      - use pte lock for checking empty PTE page, and release it after clearing
>        pmd entry, then we can capture the changed pmd in pte_offset_map_lock()
>        etc after holding this pte lock. Thanks to this, we don't need to hold the
>        rmap-related locks.
>      - users of pte_offset_map_lock() etc all expect the PTE page to be stable by
>        using rcu lock, so use pte_free_defer() to free PTE pages.
>     

I once had a protoype that would scan similar to GUP-fast, using the 
mmap lock in read mode and disabling local IRQs and then walking the 
page table locklessly (no PTLs). Only when identifying an empty page and 
ripping out the page table, it would have to do more heavy locking (back 
when we required the mmap lock in write mode and other things).

I can try digging up that patch if you're interested.

We'll have to double check whether all anon memory cases can *properly* 
handle pte_offset_map_lock() failing (not just handling it, but doing 
the right thing; most of that anon-only code didn't ever run into that 
issue so far, so these code paths were likely never triggered).


> For the path that will also free PTE pages in THP, we need to recheck whether the
> content of pmd entry is valid after holding pmd lock or pte lock.
> 
> 4. TODO
> =======
> 
> Some applications may be concerned about the overhead of scanning and rebuilding
> page tables, so the following features are considered for implementation in the
> future:
> 
>      - add per-process switch (via prctl)
>      - add a madvise option (like THP)
>      - add MM_PGTABLE_SCAN_DELAY/MM_PGTABLE_SCAN_SIZE control (via procfs file)
>      
> Perhaps we can add the refcount to PTE pages in the future as well, which would
> help improve the scanning speed.

I didn't like the added complexity last time, and the problem of 
handling situations where we squeeze multiple page tables into a single 
"struct page".
Qi Zheng June 13, 2024, 9:32 a.m. UTC | #2
Hi David,

Thanks for such a quick reply!

On 2024/6/13 17:04, David Hildenbrand wrote:
> On 13.06.24 10:38, Qi Zheng wrote:
>> Hi all,

[...]

> 
> 
>> 3. Implementation
>> =================
>>
>> For empty user PTE pages, we don't actually need to free it 
>> immediately, nor do
>> we need to free all of it.
>>
>> Therefore, in this patchset, we register a task_work for the user 
>> tasks to
>> asyncronously scan and free empty PTE pages when they return to user 
>> space.
>> (The scanning time interval and address space size can be adjusted.)
> 
> The question is, if we really have to scan asynchronously, or if would 
> be reasonable for most use cases to trigger a madvise(MADV_PT_RECLAIM) 
> every now and then. For virtio-mem, and likely most memory allocators, 
> that might be feasible, and valuable independent of system-wide 
> automatic scanning.

Agree, I also think it is possible to add always && madvise modes
simliar to THP.

> 
>>
>> When scanning, we can filter out some unsuitable vmas:
>>
>>      - VM_HUGETLB vma
>>      - VM_UFFD_WP vma
> 
> Why is UFFD_WP unsuitable? It should be suitable as long as you make 
> sure to really only remove page tables that are all pte_none().

Got it, I mistakenly thought pte_none() covered pte marker case until
I saw pte_none_mostly().

> 
>>      - etc
>> And for some PTE pages that spans multiple vmas, we can also skip.
>>
>> For locking:
>>
>>      - use the mmap read lock to traverse the vma tree and pgtable
>>      - use pmd lock for clearing pmd entry
>>      - use pte lock for checking empty PTE page, and release it after 
>> clearing
>>        pmd entry, then we can capture the changed pmd in 
>> pte_offset_map_lock()
>>        etc after holding this pte lock. Thanks to this, we don't need 
>> to hold the
>>        rmap-related locks.
>>      - users of pte_offset_map_lock() etc all expect the PTE page to 
>> be stable by
>>        using rcu lock, so use pte_free_defer() to free PTE pages.
> 
> I once had a protoype that would scan similar to GUP-fast, using the 
> mmap lock in read mode and disabling local IRQs and then walking the 
> page table locklessly (no PTLs). Only when identifying an empty page and 
> ripping out the page table, it would have to do more heavy locking (back 
> when we required the mmap lock in write mode and other things).

Maybe mmap write lock is not necessary, we can protect it using pmd lock
&& pte lock as above.

> 
> I can try digging up that patch if you're interested.

Yes, that would be better, maybe it can provide more inspiration!

> 
> We'll have to double check whether all anon memory cases can *properly* 
> handle pte_offset_map_lock() failing (not just handling it, but doing 
> the right thing; most of that anon-only code didn't ever run into that 
> issue so far, so these code paths were likely never triggered).

Yeah, I'll keep checking this out too.

> 
> 
>> For the path that will also free PTE pages in THP, we need to recheck 
>> whether the
>> content of pmd entry is valid after holding pmd lock or pte lock.
>>
>> 4. TODO
>> =======
>>
>> Some applications may be concerned about the overhead of scanning and 
>> rebuilding
>> page tables, so the following features are considered for 
>> implementation in the
>> future:
>>
>>      - add per-process switch (via prctl)
>>      - add a madvise option (like THP)
>>      - add MM_PGTABLE_SCAN_DELAY/MM_PGTABLE_SCAN_SIZE control (via 
>> procfs file)
>> Perhaps we can add the refcount to PTE pages in the future as well, 
>> which would
>> help improve the scanning speed.
> 
> I didn't like the added complexity last time, and the problem of 
> handling situations where we squeeze multiple page tables into a single 
> "struct page".

OK, except for refcount, do you think the other three todos above are 
still worth doing?

Thanks,
Qi

>
David Hildenbrand June 13, 2024, 10:25 a.m. UTC | #3
On 13.06.24 11:32, Qi Zheng wrote:
> Hi David,
> 
> Thanks for such a quick reply!

I appreciate you working on this :)

> 
> On 2024/6/13 17:04, David Hildenbrand wrote:
>> On 13.06.24 10:38, Qi Zheng wrote:
>>> Hi all,
> 
> [...]
> 
>>
>>
>>> 3. Implementation
>>> =================
>>>
>>> For empty user PTE pages, we don't actually need to free it
>>> immediately, nor do
>>> we need to free all of it.
>>>
>>> Therefore, in this patchset, we register a task_work for the user
>>> tasks to
>>> asyncronously scan and free empty PTE pages when they return to user
>>> space.
>>> (The scanning time interval and address space size can be adjusted.)
>>
>> The question is, if we really have to scan asynchronously, or if would
>> be reasonable for most use cases to trigger a madvise(MADV_PT_RECLAIM)
>> every now and then. For virtio-mem, and likely most memory allocators,
>> that might be feasible, and valuable independent of system-wide
>> automatic scanning.
> 
> Agree, I also think it is possible to add always && madvise modes
> simliar to THP.

My thinking is, we start with a madvise(MADV_PT_RECLAIM) that will
synchronously try to reclaim page tables without any asynchronous work.

Similar to MADV_COLLAPSE that only does synchronous work. Of course,
if we don't need any heavy locking for reclaim, we might also just
try reclaiming during MADV_DONTNEED when spanning a complete page
table. That won't sort out all cases where reclaim is possible, but
with both approaches we could cover quite a lot that were discovered
to really result in a lot of emprt page tables.

On top, we might implement some asynchronous scanning later, This is,
of course, TBD. Maybe we could wire up other page table scanners
(khugepaged ?) to simply reclaim empty page tables it finds as well?

> 
>>
>>>
>>> When scanning, we can filter out some unsuitable vmas:
>>>
>>>       - VM_HUGETLB vma
>>>       - VM_UFFD_WP vma
>>
>> Why is UFFD_WP unsuitable? It should be suitable as long as you make
>> sure to really only remove page tables that are all pte_none().
> 
> Got it, I mistakenly thought pte_none() covered pte marker case until
> I saw pte_none_mostly().

I *think* there is one nasty detail, and we might need an arch callback
to test if a pte is *really* can be reclaimed: for example, s390x might
require us keeping some !pte_none() page tables.

While a PTE might be none, the s390x PGSTE (think of it as another
8byte per PTE entry stored right next to the actual page table
entries) might hold data we might have to preserve for our KVM guest.

But that should be easy to wire up.

> 
>>
>>>       - etc
>>> And for some PTE pages that spans multiple vmas, we can also skip.
>>>
>>> For locking:
>>>
>>>       - use the mmap read lock to traverse the vma tree and pgtable
>>>       - use pmd lock for clearing pmd entry
>>>       - use pte lock for checking empty PTE page, and release it after
>>> clearing
>>>         pmd entry, then we can capture the changed pmd in
>>> pte_offset_map_lock()
>>>         etc after holding this pte lock. Thanks to this, we don't need
>>> to hold the
>>>         rmap-related locks.
>>>       - users of pte_offset_map_lock() etc all expect the PTE page to
>>> be stable by
>>>         using rcu lock, so use pte_free_defer() to free PTE pages.
>>
>> I once had a protoype that would scan similar to GUP-fast, using the
>> mmap lock in read mode and disabling local IRQs and then walking the
>> page table locklessly (no PTLs). Only when identifying an empty page and
>> ripping out the page table, it would have to do more heavy locking (back
>> when we required the mmap lock in write mode and other things).
> 
> Maybe mmap write lock is not necessary, we can protect it using pmd lock
> && pte lock as above.

Yes, I'm hoping we can do that, that will solve a lot of possible issues.

> 
>>
>> I can try digging up that patch if you're interested.
> 
> Yes, that would be better, maybe it can provide more inspiration!

I pushed it to
	https://github.com/davidhildenbrand/linux/tree/page_table_reclaim

I suspect it's a non-working version (and I assume the locking is broken, there
are no VMA checks, etc), it's an old prototype. Just to give you an idea about the
lockless scanning and how I started by triggering reclaim only when kicked-off by
user space.

> 
>>
>> We'll have to double check whether all anon memory cases can *properly*
>> handle pte_offset_map_lock() failing (not just handling it, but doing
>> the right thing; most of that anon-only code didn't ever run into that
>> issue so far, so these code paths were likely never triggered).
> 
> Yeah, I'll keep checking this out too.
> 
>>
>>
>>> For the path that will also free PTE pages in THP, we need to recheck
>>> whether the
>>> content of pmd entry is valid after holding pmd lock or pte lock.
>>>
>>> 4. TODO
>>> =======
>>>
>>> Some applications may be concerned about the overhead of scanning and
>>> rebuilding
>>> page tables, so the following features are considered for
>>> implementation in the
>>> future:
>>>
>>>       - add per-process switch (via prctl)
>>>       - add a madvise option (like THP)
>>>       - add MM_PGTABLE_SCAN_DELAY/MM_PGTABLE_SCAN_SIZE control (via
>>> procfs file)
>>> Perhaps we can add the refcount to PTE pages in the future as well,
>>> which would
>>> help improve the scanning speed.
>>
>> I didn't like the added complexity last time, and the problem of
>> handling situations where we squeeze multiple page tables into a single
>> "struct page".
> 
> OK, except for refcount, do you think the other three todos above are
> still worth doing?

I think the question is from where we start: for example, only synchronous
reclaim vs. asynchonous reclaim. Synchronous reclaim won't really affect
workloads that do not actively trigger it, so it raises a lot less eyebrows. ...
and some user space might have a good idea where it makes sense to try to
reclaim, and when.

So the other things you note here rather affect asynchronous reclaim, and
might be reasonable in that context. But not sure if we should start with doing
things asynchronously.
Qi Zheng June 13, 2024, 11:59 a.m. UTC | #4
Hi,

On 2024/6/13 18:25, David Hildenbrand wrote:
> On 13.06.24 11:32, Qi Zheng wrote:
>> Hi David,
>>
>> Thanks for such a quick reply!
> 
> I appreciate you working on this :)
> 
>>
>> On 2024/6/13 17:04, David Hildenbrand wrote:
>>> On 13.06.24 10:38, Qi Zheng wrote:
>>>> Hi all,
>>
>> [...]
>>
>>>
>>>
>>>> 3. Implementation
>>>> =================
>>>>
>>>> For empty user PTE pages, we don't actually need to free it
>>>> immediately, nor do
>>>> we need to free all of it.
>>>>
>>>> Therefore, in this patchset, we register a task_work for the user
>>>> tasks to
>>>> asyncronously scan and free empty PTE pages when they return to user
>>>> space.
>>>> (The scanning time interval and address space size can be adjusted.)
>>>
>>> The question is, if we really have to scan asynchronously, or if would
>>> be reasonable for most use cases to trigger a madvise(MADV_PT_RECLAIM)
>>> every now and then. For virtio-mem, and likely most memory allocators,
>>> that might be feasible, and valuable independent of system-wide
>>> automatic scanning.
>>
>> Agree, I also think it is possible to add always && madvise modes
>> simliar to THP.
> 
> My thinking is, we start with a madvise(MADV_PT_RECLAIM) that will
> synchronously try to reclaim page tables without any asynchronous work.
> 
> Similar to MADV_COLLAPSE that only does synchronous work. Of course,

This is feasible, but I worry that some user-mode programs may not be 
able to determine when to call it.

My previous idea was to do something similar to madvise(MADV_HUGEPAGE),
just mark the vma as being able to reclaim the pgtable, and then hand
it over to the background thread for asynchronous reclaim.

> if we don't need any heavy locking for reclaim, we might also just
> try reclaiming during MADV_DONTNEED when spanning a complete page

I think the lock held by the current solution is not too heavy and
should be acceptable.

But for MADV_FREE case, it still needs to be handled by
madvise(MADV_PT_RECLAIM) or asynchronous work.

> table. That won't sort out all cases where reclaim is possible, but
> with both approaches we could cover quite a lot that were discovered
> to really result in a lot of emprt page tables.

Yes, agree.

> 
> On top, we might implement some asynchronous scanning later, This is,
> of course, TBD. Maybe we could wire up other page table scanners
> (khugepaged ?) to simply reclaim empty page tables it finds as well?

This is also an idea. Another option may be some pgtable scanning paths,
such as MGLRU.

> 
>>
>>>
>>>>
>>>> When scanning, we can filter out some unsuitable vmas:
>>>>
>>>>       - VM_HUGETLB vma
>>>>       - VM_UFFD_WP vma
>>>
>>> Why is UFFD_WP unsuitable? It should be suitable as long as you make
>>> sure to really only remove page tables that are all pte_none().
>>
>> Got it, I mistakenly thought pte_none() covered pte marker case until
>> I saw pte_none_mostly().
> 
> I *think* there is one nasty detail, and we might need an arch callback
> to test if a pte is *really* can be reclaimed: for example, s390x might
> require us keeping some !pte_none() page tables.
> 
> While a PTE might be none, the s390x PGSTE (think of it as another
> 8byte per PTE entry stored right next to the actual page table
> entries) might hold data we might have to preserve for our KVM guest.

Oh, thanks for adding this background information!

> 
> But that should be easy to wire up.

That's good!

> 
>>
>>>
>>>>       - etc
>>>> And for some PTE pages that spans multiple vmas, we can also skip.
>>>>
>>>> For locking:
>>>>
>>>>       - use the mmap read lock to traverse the vma tree and pgtable
>>>>       - use pmd lock for clearing pmd entry
>>>>       - use pte lock for checking empty PTE page, and release it after
>>>> clearing
>>>>         pmd entry, then we can capture the changed pmd in
>>>> pte_offset_map_lock()
>>>>         etc after holding this pte lock. Thanks to this, we don't need
>>>> to hold the
>>>>         rmap-related locks.
>>>>       - users of pte_offset_map_lock() etc all expect the PTE page to
>>>> be stable by
>>>>         using rcu lock, so use pte_free_defer() to free PTE pages.
>>>
>>> I once had a protoype that would scan similar to GUP-fast, using the
>>> mmap lock in read mode and disabling local IRQs and then walking the
>>> page table locklessly (no PTLs). Only when identifying an empty page and
>>> ripping out the page table, it would have to do more heavy locking (back
>>> when we required the mmap lock in write mode and other things).
>>
>> Maybe mmap write lock is not necessary, we can protect it using pmd lock
>> && pte lock as above.
> 
> Yes, I'm hoping we can do that, that will solve a lot of possible issues.

Yes, I think the protection provided by the locks above is enough. Of
course, it would be better if more people could double-check it.

> 
>>
>>>
>>> I can try digging up that patch if you're interested.
>>
>> Yes, that would be better, maybe it can provide more inspiration!
> 
> I pushed it to
>      https://github.com/davidhildenbrand/linux/tree/page_table_reclaim
> 
> I suspect it's a non-working version (and I assume the locking is 
> broken, there
> are no VMA checks, etc), it's an old prototype. Just to give you an idea 
> about the
> lockless scanning and how I started by triggering reclaim only when 
> kicked-off by
> user space.

Many thanks! But I'm worried that on some platforms disbaling the IRQ
might be more expensive than holding the lock, such as arm64? Not sure.

> 
>>
>>>
>>> We'll have to double check whether all anon memory cases can *properly*
>>> handle pte_offset_map_lock() failing (not just handling it, but doing
>>> the right thing; most of that anon-only code didn't ever run into that
>>> issue so far, so these code paths were likely never triggered).
>>
>> Yeah, I'll keep checking this out too.
>>
>>>
>>>
>>>> For the path that will also free PTE pages in THP, we need to recheck
>>>> whether the
>>>> content of pmd entry is valid after holding pmd lock or pte lock.
>>>>
>>>> 4. TODO
>>>> =======
>>>>
>>>> Some applications may be concerned about the overhead of scanning and
>>>> rebuilding
>>>> page tables, so the following features are considered for
>>>> implementation in the
>>>> future:
>>>>
>>>>       - add per-process switch (via prctl)
>>>>       - add a madvise option (like THP)
>>>>       - add MM_PGTABLE_SCAN_DELAY/MM_PGTABLE_SCAN_SIZE control (via
>>>> procfs file)
>>>> Perhaps we can add the refcount to PTE pages in the future as well,
>>>> which would
>>>> help improve the scanning speed.
>>>
>>> I didn't like the added complexity last time, and the problem of
>>> handling situations where we squeeze multiple page tables into a single
>>> "struct page".
>>
>> OK, except for refcount, do you think the other three todos above are
>> still worth doing?
> 
> I think the question is from where we start: for example, only synchronous
> reclaim vs. asynchonous reclaim. Synchronous reclaim won't really affect
> workloads that do not actively trigger it, so it raises a lot less 
> eyebrows. ...
> and some user space might have a good idea where it makes sense to try to
> reclaim, and when.
> 
> So the other things you note here rather affect asynchronous reclaim, and
> might be reasonable in that context. But not sure if we should start 
> with doing
> things asynchronously.

I think synchronous and asynchronous have their own advantages and
disadvantages, and are complementary. Perhaps they can be implemented at
the same time?

Thanks,
Qi

>
Qi Zheng June 14, 2024, 3:32 a.m. UTC | #5
Hi David,

How about starting with this:

a. for MADV_DONTNEED case, try synchronous reclaim as you said
b. for MADV_FREE case, add a madvise(MADV_PT_RECLAIM) option to mark
    this vma, then add its corresponding mm to a global list, and then
    traverse the list and reclaim it when the memory is tight and enters
    the system reclaim path.

    (If this option is for synchronous reclaim as you said, then the
     user-mode program may need to start a thread to make a cyclic call.
     I'm not sure if this usage makes sense. If so, I can also implement
     such an option.)
c. for s390 case you mentioned, maybe we can set a CONFIG_FREE_PT first,
    and then s390 will not select this config until the problem is solved.
d. for lockless scan, we can use pte_offset_map_nolock() instead of
    disabling IRQ to scan, because we hold RCU read lock at this time,
    which can also ensure that the PTE page is not freed.

Thanks,
Qi

On 2024/6/13 19:59, Qi Zheng wrote:
> Hi,
> 
> On 2024/6/13 18:25, David Hildenbrand wrote:
>> On 13.06.24 11:32, Qi Zheng wrote:
>>> Hi David,
>>>
>>> Thanks for such a quick reply!
>>
>> I appreciate you working on this :)
>>
>>>
>>> On 2024/6/13 17:04, David Hildenbrand wrote:
>>>> On 13.06.24 10:38, Qi Zheng wrote:
>>>>> Hi all,
>>>
>>> [...]
>>>
>>>>
>>>>
>>>>> 3. Implementation
>>>>> =================
>>>>>
>>>>> For empty user PTE pages, we don't actually need to free it
>>>>> immediately, nor do
>>>>> we need to free all of it.
>>>>>
>>>>> Therefore, in this patchset, we register a task_work for the user
>>>>> tasks to
>>>>> asyncronously scan and free empty PTE pages when they return to user
>>>>> space.
>>>>> (The scanning time interval and address space size can be adjusted.)
>>>>
>>>> The question is, if we really have to scan asynchronously, or if would
>>>> be reasonable for most use cases to trigger a madvise(MADV_PT_RECLAIM)
>>>> every now and then. For virtio-mem, and likely most memory allocators,
>>>> that might be feasible, and valuable independent of system-wide
>>>> automatic scanning.
>>>
>>> Agree, I also think it is possible to add always && madvise modes
>>> simliar to THP.
>>
>> My thinking is, we start with a madvise(MADV_PT_RECLAIM) that will
>> synchronously try to reclaim page tables without any asynchronous work.
>>
>> Similar to MADV_COLLAPSE that only does synchronous work. Of course,
> 
> This is feasible, but I worry that some user-mode programs may not be 
> able to determine when to call it.
> 
> My previous idea was to do something similar to madvise(MADV_HUGEPAGE),
> just mark the vma as being able to reclaim the pgtable, and then hand
> it over to the background thread for asynchronous reclaim.
> 
>> if we don't need any heavy locking for reclaim, we might also just
>> try reclaiming during MADV_DONTNEED when spanning a complete page
> 
> I think the lock held by the current solution is not too heavy and
> should be acceptable.
> 
> But for MADV_FREE case, it still needs to be handled by
> madvise(MADV_PT_RECLAIM) or asynchronous work.
> 
>> table. That won't sort out all cases where reclaim is possible, but
>> with both approaches we could cover quite a lot that were discovered
>> to really result in a lot of emprt page tables.
> 
> Yes, agree.
> 
>>
>> On top, we might implement some asynchronous scanning later, This is,
>> of course, TBD. Maybe we could wire up other page table scanners
>> (khugepaged ?) to simply reclaim empty page tables it finds as well?
> 
> This is also an idea. Another option may be some pgtable scanning paths,
> such as MGLRU.
> 
>>
>>>
>>>>
>>>>>
>>>>> When scanning, we can filter out some unsuitable vmas:
>>>>>
>>>>>       - VM_HUGETLB vma
>>>>>       - VM_UFFD_WP vma
>>>>
>>>> Why is UFFD_WP unsuitable? It should be suitable as long as you make
>>>> sure to really only remove page tables that are all pte_none().
>>>
>>> Got it, I mistakenly thought pte_none() covered pte marker case until
>>> I saw pte_none_mostly().
>>
>> I *think* there is one nasty detail, and we might need an arch callback
>> to test if a pte is *really* can be reclaimed: for example, s390x might
>> require us keeping some !pte_none() page tables.
>>
>> While a PTE might be none, the s390x PGSTE (think of it as another
>> 8byte per PTE entry stored right next to the actual page table
>> entries) might hold data we might have to preserve for our KVM guest.
> 
> Oh, thanks for adding this background information!
> 
>>
>> But that should be easy to wire up.
> 
> That's good!
> 
>>
>>>
>>>>
>>>>>       - etc
>>>>> And for some PTE pages that spans multiple vmas, we can also skip.
>>>>>
>>>>> For locking:
>>>>>
>>>>>       - use the mmap read lock to traverse the vma tree and pgtable
>>>>>       - use pmd lock for clearing pmd entry
>>>>>       - use pte lock for checking empty PTE page, and release it after
>>>>> clearing
>>>>>         pmd entry, then we can capture the changed pmd in
>>>>> pte_offset_map_lock()
>>>>>         etc after holding this pte lock. Thanks to this, we don't need
>>>>> to hold the
>>>>>         rmap-related locks.
>>>>>       - users of pte_offset_map_lock() etc all expect the PTE page to
>>>>> be stable by
>>>>>         using rcu lock, so use pte_free_defer() to free PTE pages.
>>>>
>>>> I once had a protoype that would scan similar to GUP-fast, using the
>>>> mmap lock in read mode and disabling local IRQs and then walking the
>>>> page table locklessly (no PTLs). Only when identifying an empty page 
>>>> and
>>>> ripping out the page table, it would have to do more heavy locking 
>>>> (back
>>>> when we required the mmap lock in write mode and other things).
>>>
>>> Maybe mmap write lock is not necessary, we can protect it using pmd lock
>>> && pte lock as above.
>>
>> Yes, I'm hoping we can do that, that will solve a lot of possible issues.
> 
> Yes, I think the protection provided by the locks above is enough. Of
> course, it would be better if more people could double-check it.
> 
>>
>>>
>>>>
>>>> I can try digging up that patch if you're interested.
>>>
>>> Yes, that would be better, maybe it can provide more inspiration!
>>
>> I pushed it to
>>      https://github.com/davidhildenbrand/linux/tree/page_table_reclaim
>>
>> I suspect it's a non-working version (and I assume the locking is 
>> broken, there
>> are no VMA checks, etc), it's an old prototype. Just to give you an 
>> idea about the
>> lockless scanning and how I started by triggering reclaim only when 
>> kicked-off by
>> user space.
> 
> Many thanks! But I'm worried that on some platforms disbaling the IRQ
> might be more expensive than holding the lock, such as arm64? Not sure.
> 
>>
>>>
>>>>
>>>> We'll have to double check whether all anon memory cases can *properly*
>>>> handle pte_offset_map_lock() failing (not just handling it, but doing
>>>> the right thing; most of that anon-only code didn't ever run into that
>>>> issue so far, so these code paths were likely never triggered).
>>>
>>> Yeah, I'll keep checking this out too.
>>>
>>>>
>>>>
>>>>> For the path that will also free PTE pages in THP, we need to recheck
>>>>> whether the
>>>>> content of pmd entry is valid after holding pmd lock or pte lock.
>>>>>
>>>>> 4. TODO
>>>>> =======
>>>>>
>>>>> Some applications may be concerned about the overhead of scanning and
>>>>> rebuilding
>>>>> page tables, so the following features are considered for
>>>>> implementation in the
>>>>> future:
>>>>>
>>>>>       - add per-process switch (via prctl)
>>>>>       - add a madvise option (like THP)
>>>>>       - add MM_PGTABLE_SCAN_DELAY/MM_PGTABLE_SCAN_SIZE control (via
>>>>> procfs file)
>>>>> Perhaps we can add the refcount to PTE pages in the future as well,
>>>>> which would
>>>>> help improve the scanning speed.
>>>>
>>>> I didn't like the added complexity last time, and the problem of
>>>> handling situations where we squeeze multiple page tables into a single
>>>> "struct page".
>>>
>>> OK, except for refcount, do you think the other three todos above are
>>> still worth doing?
>>
>> I think the question is from where we start: for example, only 
>> synchronous
>> reclaim vs. asynchonous reclaim. Synchronous reclaim won't really affect
>> workloads that do not actively trigger it, so it raises a lot less 
>> eyebrows. ...
>> and some user space might have a good idea where it makes sense to try to
>> reclaim, and when.
>>
>> So the other things you note here rather affect asynchronous reclaim, and
>> might be reasonable in that context. But not sure if we should start 
>> with doing
>> things asynchronously.
> 
> I think synchronous and asynchronous have their own advantages and
> disadvantages, and are complementary. Perhaps they can be implemented at
> the same time?
> 
> Thanks,
> Qi
> 
>>
David Hildenbrand June 14, 2024, 7:53 a.m. UTC | #6
>> My thinking is, we start with a madvise(MADV_PT_RECLAIM) that will
>> synchronously try to reclaim page tables without any asynchronous work.
>>
>> Similar to MADV_COLLAPSE that only does synchronous work. Of course,
> 
> This is feasible, but I worry that some user-mode programs may not be
> able to determine when to call it.

Some yes, but others clearly :) Meaning, it's one step into the right 
direction without having to worry about asynchronous work in the kernel 
for now. That doesn't mean that asynchronous option is off the table.

> 
> My previous idea was to do something similar to madvise(MADV_HUGEPAGE),
> just mark the vma as being able to reclaim the pgtable, and then hand
> it over to the background thread for asynchronous reclaim.

That's one option, although there might be workloads where you really 
don't have to scan asynchronously and possibly repeatedly.

For example, after virtio-mem discarded some memory it hotunplugged from 
a VM using MADV_DONTNEED (in a sequence of multiple steps), it could 
just setup a timer to free up page tables after a while exactly once. No 
need to scan repeatedly / multiple times if virtio-mem didn't remove any 
memory from a VM.

For memory allocators it could be similar: trigger it once (from another 
thread?) on a range after sufficient freeing happened. If the workload 
is mostly idle, there might not be a need to free up memory.

(mostly focused on anonymous memory + shmem for now. With file-backed 
memory it might be different, but that has so far not been the biggest 
consumer we saw regarding page tables.)

Of course, for real asynchronous/automatic scanning in the kernel, one 
could try finding clues when scanning is reasonable: for example, mark 
page tables that have been scanned and there was nothing to reclaim, and 
mark page tables when modifying them. But such optimizations are rather 
future work I guess, because devil is in the detail.

> 
>> if we don't need any heavy locking for reclaim, we might also just
>> try reclaiming during MADV_DONTNEED when spanning a complete page
> 
> I think the lock held by the current solution is not too heavy and
> should be acceptable.
> 
> But for MADV_FREE case, it still needs to be handled by
> madvise(MADV_PT_RECLAIM) or asynchronous work.

Yes. Interestingly, reclaim code might be able to do that scanning + 
reclaim if locking is cheap.

> 
>> table. That won't sort out all cases where reclaim is possible, but
>> with both approaches we could cover quite a lot that were discovered
>> to really result in a lot of emprt page tables.
> 
> Yes, agree.
> 
>>
>> On top, we might implement some asynchronous scanning later, This is,
>> of course, TBD. Maybe we could wire up other page table scanners
>> (khugepaged ?) to simply reclaim empty page tables it finds as well?
> 
> This is also an idea. Another option may be some pgtable scanning paths,
> such as MGLRU.
> 

Exactly.

>>
>>>
>>>>
>>>>>
>>>>> When scanning, we can filter out some unsuitable vmas:
>>>>>
>>>>>        - VM_HUGETLB vma
>>>>>        - VM_UFFD_WP vma
>>>>
>>>> Why is UFFD_WP unsuitable? It should be suitable as long as you make
>>>> sure to really only remove page tables that are all pte_none().
>>>
>>> Got it, I mistakenly thought pte_none() covered pte marker case until
>>> I saw pte_none_mostly().
>>
>> I *think* there is one nasty detail, and we might need an arch callback
>> to test if a pte is *really* can be reclaimed: for example, s390x might
>> require us keeping some !pte_none() page tables.
>>
>> While a PTE might be none, the s390x PGSTE (think of it as another
>> 8byte per PTE entry stored right next to the actual page table
>> entries) might hold data we might have to preserve for our KVM guest.
> 
> Oh, thanks for adding this background information!
> 
>>
>> But that should be easy to wire up.
> 
> That's good!
> 
>>
>>>
>>>>
>>>>>        - etc
>>>>> And for some PTE pages that spans multiple vmas, we can also skip.
>>>>>
>>>>> For locking:
>>>>>
>>>>>        - use the mmap read lock to traverse the vma tree and pgtable
>>>>>        - use pmd lock for clearing pmd entry
>>>>>        - use pte lock for checking empty PTE page, and release it after
>>>>> clearing
>>>>>          pmd entry, then we can capture the changed pmd in
>>>>> pte_offset_map_lock()
>>>>>          etc after holding this pte lock. Thanks to this, we don't need
>>>>> to hold the
>>>>>          rmap-related locks.
>>>>>        - users of pte_offset_map_lock() etc all expect the PTE page to
>>>>> be stable by
>>>>>          using rcu lock, so use pte_free_defer() to free PTE pages.
>>>>
>>>> I once had a protoype that would scan similar to GUP-fast, using the
>>>> mmap lock in read mode and disabling local IRQs and then walking the
>>>> page table locklessly (no PTLs). Only when identifying an empty page and
>>>> ripping out the page table, it would have to do more heavy locking (back
>>>> when we required the mmap lock in write mode and other things).
>>>
>>> Maybe mmap write lock is not necessary, we can protect it using pmd lock
>>> && pte lock as above.
>>
>> Yes, I'm hoping we can do that, that will solve a lot of possible issues.
> 
> Yes, I think the protection provided by the locks above is enough. Of
> course, it would be better if more people could double-check it.
> 
>>
>>>
>>>>
>>>> I can try digging up that patch if you're interested.
>>>
>>> Yes, that would be better, maybe it can provide more inspiration!
>>
>> I pushed it to
>>       https://github.com/davidhildenbrand/linux/tree/page_table_reclaim
>>
>> I suspect it's a non-working version (and I assume the locking is
>> broken, there
>> are no VMA checks, etc), it's an old prototype. Just to give you an idea
>> about the
>> lockless scanning and how I started by triggering reclaim only when
>> kicked-off by
>> user space.
> 
> Many thanks! But I'm worried that on some platforms disbaling the IRQ
> might be more expensive than holding the lock, such as arm64? Not sure.

Scanning completely lockless (no mmap lock, not PT locks), means that -- 
as long as there is not much to reclaim (for most workloads the common 
case!) -- you would not affect the workload at all.

Take a look at the khugepaged logic that does mmap_read_trylock(mm) and 
makes sure to drop the mmap lock frequently due to 
khugepaged_pages_to_scan, to not affect the workload too much while 
scanning.

> 
>>
>>>
>>>>
>>>> We'll have to double check whether all anon memory cases can *properly*
>>>> handle pte_offset_map_lock() failing (not just handling it, but doing
>>>> the right thing; most of that anon-only code didn't ever run into that
>>>> issue so far, so these code paths were likely never triggered).
>>>
>>> Yeah, I'll keep checking this out too.
>>>
>>>>
>>>>
>>>>> For the path that will also free PTE pages in THP, we need to recheck
>>>>> whether the
>>>>> content of pmd entry is valid after holding pmd lock or pte lock.
>>>>>
>>>>> 4. TODO
>>>>> =======
>>>>>
>>>>> Some applications may be concerned about the overhead of scanning and
>>>>> rebuilding
>>>>> page tables, so the following features are considered for
>>>>> implementation in the
>>>>> future:
>>>>>
>>>>>        - add per-process switch (via prctl)
>>>>>        - add a madvise option (like THP)
>>>>>        - add MM_PGTABLE_SCAN_DELAY/MM_PGTABLE_SCAN_SIZE control (via
>>>>> procfs file)
>>>>> Perhaps we can add the refcount to PTE pages in the future as well,
>>>>> which would
>>>>> help improve the scanning speed.
>>>>
>>>> I didn't like the added complexity last time, and the problem of
>>>> handling situations where we squeeze multiple page tables into a single
>>>> "struct page".
>>>
>>> OK, except for refcount, do you think the other three todos above are
>>> still worth doing?
>>
>> I think the question is from where we start: for example, only synchronous
>> reclaim vs. asynchonous reclaim. Synchronous reclaim won't really affect
>> workloads that do not actively trigger it, so it raises a lot less
>> eyebrows. ...
>> and some user space might have a good idea where it makes sense to try to
>> reclaim, and when.
>>
>> So the other things you note here rather affect asynchronous reclaim, and
>> might be reasonable in that context. But not sure if we should start
>> with doing
>> things asynchronously.
> 
> I think synchronous and asynchronous have their own advantages and
> disadvantages, and are complementary. Perhaps they can be implemented at
> the same time?


No strong opinion, something synchronous sounds to me like the 
low-hanging fruit, that could add the infrastructure to be used by 
something more advanced/synchronously :)
Qi Zheng June 14, 2024, 10:49 a.m. UTC | #7
Hi David,

On 2024/6/14 15:53, David Hildenbrand wrote:
>>> My thinking is, we start with a madvise(MADV_PT_RECLAIM) that will
>>> synchronously try to reclaim page tables without any asynchronous work.
>>>
>>> Similar to MADV_COLLAPSE that only does synchronous work. Of course,
>>
>> This is feasible, but I worry that some user-mode programs may not be
>> able to determine when to call it.
> 
> Some yes, but others clearly :) Meaning, it's one step into the right 
> direction without having to worry about asynchronous work in the kernel 
> for now. That doesn't mean that asynchronous option is off the table.

Got it. I will try to implement a synchronous madvise option in the
next version.

> 
>>
>> My previous idea was to do something similar to madvise(MADV_HUGEPAGE),
>> just mark the vma as being able to reclaim the pgtable, and then hand
>> it over to the background thread for asynchronous reclaim.
> 
> That's one option, although there might be workloads where you really 
> don't have to scan asynchronously and possibly repeatedly.
> 
> For example, after virtio-mem discarded some memory it hotunplugged from 
> a VM using MADV_DONTNEED (in a sequence of multiple steps), it could 
> just setup a timer to free up page tables after a while exactly once. No 
> need to scan repeatedly / multiple times if virtio-mem didn't remove any 
> memory from a VM.
> 
> For memory allocators it could be similar: trigger it once (from another 
> thread?) on a range after sufficient freeing happened. If the workload 
> is mostly idle, there might not be a need to free up memory.

Thanks for adding this information!

> 
> (mostly focused on anonymous memory + shmem for now. With file-backed 
> memory it might be different, but that has so far not been the biggest 
> consumer we saw regarding page tables.)

OK.

> 
> Of course, for real asynchronous/automatic scanning in the kernel, one 
> could try finding clues when scanning is reasonable: for example, mark 
> page tables that have been scanned and there was nothing to reclaim, and 
> mark page tables when modifying them. But such optimizations are rather 
> future work I guess, because devil is in the detail.

Yes, we can optimize it step by step.

> 
>>
>>> if we don't need any heavy locking for reclaim, we might also just
>>> try reclaiming during MADV_DONTNEED when spanning a complete page
>>
>> I think the lock held by the current solution is not too heavy and
>> should be acceptable.
>>
>> But for MADV_FREE case, it still needs to be handled by
>> madvise(MADV_PT_RECLAIM) or asynchronous work.
> 
> Yes. Interestingly, reclaim code might be able to do that scanning + 
> reclaim if locking is cheap.

Yes, I am also considering implementing another madvise option in the
next version:

    mark the vma, then add its corresponding mm to a global list, and
    then traverse the list and reclaim it when the memory is tight and
    enters the system reclaim path.


> 
>>
>>> table. That won't sort out all cases where reclaim is possible, but
>>> with both approaches we could cover quite a lot that were discovered
>>> to really result in a lot of emprt page tables.
>>

[...]

>>>
>>> I pushed it to
>>>       https://github.com/davidhildenbrand/linux/tree/page_table_reclaim
>>>
>>> I suspect it's a non-working version (and I assume the locking is
>>> broken, there
>>> are no VMA checks, etc), it's an old prototype. Just to give you an idea
>>> about the
>>> lockless scanning and how I started by triggering reclaim only when
>>> kicked-off by
>>> user space.
>>
>> Many thanks! But I'm worried that on some platforms disbaling the IRQ
>> might be more expensive than holding the lock, such as arm64? Not sure.
> 
> Scanning completely lockless (no mmap lock, not PT locks), means that -- 
> as long as there is not much to reclaim (for most workloads the common 
> case!) -- you would not affect the workload at all.
> 
> Take a look at the khugepaged logic that does mmap_read_trylock(mm) and 
> makes sure to drop the mmap lock frequently due to 
> khugepaged_pages_to_scan, to not affect the workload too much while 
> scanning.
> 

OK, I will take a look.

>>
>>>
>>>>
>>>>>
>>>>> We'll have to double check whether all anon memory cases can 
>>>>> *properly*
>>>>> handle pte_offset_map_lock() failing (not just handling it, but doing
>>>>> the right thing; most of that anon-only code didn't ever run into that
>>>>> issue so far, so these code paths were likely never triggered).
>>>>
>>>> Yeah, I'll keep checking this out too.
>>>>
>>>>>
>>>>>
>>>>>> For the path that will also free PTE pages in THP, we need to recheck
>>>>>> whether the
>>>>>> content of pmd entry is valid after holding pmd lock or pte lock.
>>>>>>
>>>>>> 4. TODO
>>>>>> =======
>>>>>>
>>>>>> Some applications may be concerned about the overhead of scanning and
>>>>>> rebuilding
>>>>>> page tables, so the following features are considered for
>>>>>> implementation in the
>>>>>> future:
>>>>>>
>>>>>>        - add per-process switch (via prctl)
>>>>>>        - add a madvise option (like THP)
>>>>>>        - add MM_PGTABLE_SCAN_DELAY/MM_PGTABLE_SCAN_SIZE control (via
>>>>>> procfs file)
>>>>>> Perhaps we can add the refcount to PTE pages in the future as well,
>>>>>> which would
>>>>>> help improve the scanning speed.
>>>>>
>>>>> I didn't like the added complexity last time, and the problem of
>>>>> handling situations where we squeeze multiple page tables into a 
>>>>> single
>>>>> "struct page".
>>>>
>>>> OK, except for refcount, do you think the other three todos above are
>>>> still worth doing?
>>>
>>> I think the question is from where we start: for example, only 
>>> synchronous
>>> reclaim vs. asynchonous reclaim. Synchronous reclaim won't really affect
>>> workloads that do not actively trigger it, so it raises a lot less
>>> eyebrows. ...
>>> and some user space might have a good idea where it makes sense to 
>>> try to
>>> reclaim, and when.
>>>
>>> So the other things you note here rather affect asynchronous reclaim, 
>>> and
>>> might be reasonable in that context. But not sure if we should start
>>> with doing
>>> things asynchronously.
>>
>> I think synchronous and asynchronous have their own advantages and
>> disadvantages, and are complementary. Perhaps they can be implemented at
>> the same time?
> 
> 
> No strong opinion, something synchronous sounds to me like the 
> low-hanging fruit, that could add the infrastructure to be used by 
> something more advanced/synchronously :)

Got it, I will try to do the following in the next version.

a. for MADV_DONTNEED case, try synchronous reclaim as you said

b. for MADV_FREE case:

	- add a madvise option for synchronous reclaim

	- add another madvise option to mark the vma, then add its
           corresponding mm to a global list, and then traverse
           the list and reclaim it when the memory is tight and
           enters the system reclaim path.
           (maybe there is an option to unmark)

c. for s390 case you mentioned, create a CONFIG_FREE_PT first, and
    then s390 will not select this config until the problem is solved.

d. for lockless scan, try using disabling IRQ or (mmap read lock + 
pte_offset_map_nolock).

Thanks,
Qi

>
David Hildenbrand June 17, 2024, 5:49 p.m. UTC | #8
>>
>> No strong opinion, something synchronous sounds to me like the
>> low-hanging fruit, that could add the infrastructure to be used by
>> something more advanced/synchronously :)
> 
> Got it, I will try to do the following in the next version.
> 
> a. for MADV_DONTNEED case, try synchronous reclaim as you said
> 

I think that really is the low hanging fruit that would cover quite some 
cases already: (1) reclaim when MADV_DONTNEED spans the complete page table.

Then, there is (2) reclaim when MADV_DONTNEED spans only part of the 
page table (e.g., single PTE), but my best guess is that it's better to 
scan for that asynchronously than making possibly each MADV_DONTNEED 
sycall invocation slower.

(1) would already help a lot and showcase how the locking/machinery 
would work.


> b. for MADV_FREE case:
> 
> 	- add a madvise option for synchronous reclaim
> 
> 	- add another madvise option to mark the vma, then add its
>             corresponding mm to a global list, and then traverse
>             the list and reclaim it when the memory is tight and
>             enters the system reclaim path.
>             (maybe there is an option to unmark)
> 
> c. for s390 case you mentioned, create a CONFIG_FREE_PT first, and
>      then s390 will not select this config until the problem is solved.
> 
> d. for lockless scan, try using disabling IRQ or (mmap read lock +
> pte_offset_map_nolock).

Although d) really only is desired when scanning asynchronously I think. 
During (1) above, we know that the table will be very likely empty 
(unless weird race).
David Hildenbrand June 17, 2024, 5:51 p.m. UTC | #9
On 14.06.24 05:32, Qi Zheng wrote:
> Hi David,
> 
> How about starting with this:
> 
> a. for MADV_DONTNEED case, try synchronous reclaim as you said
> b. for MADV_FREE case, add a madvise(MADV_PT_RECLAIM) option to mark
>      this vma, then add its corresponding mm to a global list, and then
>      traverse the list and reclaim it when the memory is tight and enters
>      the system reclaim path.
> 
>      (If this option is for synchronous reclaim as you said, then the
>       user-mode program may need to start a thread to make a cyclic call.
>       I'm not sure if this usage makes sense. If so, I can also implement
>       such an option.)
> c. for s390 case you mentioned, maybe we can set a CONFIG_FREE_PT first,
>      and then s390 will not select this config until the problem is solved.

CONFIG_PT_RECLAIM or sth. like that, that would depend on 
CONFIG_ARCH_SUPPORTS_PT_RECLAIM.

Then we can start with what we know works and was tested (e.g., x86).
Qi Zheng June 18, 2024, 7:51 a.m. UTC | #10
Hi David,

On 2024/6/18 01:49, David Hildenbrand wrote:
> 
>>>
>>> No strong opinion, something synchronous sounds to me like the
>>> low-hanging fruit, that could add the infrastructure to be used by
>>> something more advanced/synchronously :)
>>
>> Got it, I will try to do the following in the next version.
>>
>> a. for MADV_DONTNEED case, try synchronous reclaim as you said
>>
> 
> I think that really is the low hanging fruit that would cover quite some 
> cases already: (1) reclaim when MADV_DONTNEED spans the complete page 
> table.

I will check and free the PTE page in the zap_pte_range() if the
(end - addr >= PMD_SIZE) condition is met.

> 
> Then, there is (2) reclaim when MADV_DONTNEED spans only part of the 
> page table (e.g., single PTE), but my best guess is that it's better to 
> scan for that asynchronously than making possibly each MADV_DONTNEED 
> sycall invocation slower.

Maybe just mark the vma, and then scan it in the system reclaim path.

I also plan to do this in the MADV_FREE case, instead of adding an
asynchronous madvise option first.

> 
> (1) would already help a lot and showcase how the locking/machinery 
> would work.
> 
> 
>> b. for MADV_FREE case:
>>
>>     - add a madvise option for synchronous reclaim
>>
>>     - add another madvise option to mark the vma, then add its
>>             corresponding mm to a global list, and then traverse
>>             the list and reclaim it when the memory is tight and
>>             enters the system reclaim path.
>>             (maybe there is an option to unmark)
>>
>> c. for s390 case you mentioned, create a CONFIG_FREE_PT first, and
>>      then s390 will not select this config until the problem is solved.
>>
>> d. for lockless scan, try using disabling IRQ or (mmap read lock +
>> pte_offset_map_nolock).
> 
> Although d) really only is desired when scanning asynchronously I think. 
> During (1) above, we know that the table will be very likely empty 
> (unless weird race).

Agree.

Thanks,
Qi

>
Qi Zheng June 18, 2024, 7:52 a.m. UTC | #11
On 2024/6/18 01:51, David Hildenbrand wrote:
> On 14.06.24 05:32, Qi Zheng wrote:
>> Hi David,
>>
>> How about starting with this:
>>
>> a. for MADV_DONTNEED case, try synchronous reclaim as you said
>> b. for MADV_FREE case, add a madvise(MADV_PT_RECLAIM) option to mark
>>      this vma, then add its corresponding mm to a global list, and then
>>      traverse the list and reclaim it when the memory is tight and enters
>>      the system reclaim path.
>>
>>      (If this option is for synchronous reclaim as you said, then the
>>       user-mode program may need to start a thread to make a cyclic call.
>>       I'm not sure if this usage makes sense. If so, I can also implement
>>       such an option.)
>> c. for s390 case you mentioned, maybe we can set a CONFIG_FREE_PT first,
>>      and then s390 will not select this config until the problem is 
>> solved.
> 
> CONFIG_PT_RECLAIM or sth. like that, that would depend on 
> CONFIG_ARCH_SUPPORTS_PT_RECLAIM.
> 
> Then we can start with what we know works and was tested (e.g., x86).

OK, will do.

>
David Hildenbrand June 18, 2024, 9:40 a.m. UTC | #12
On 18.06.24 09:51, Qi Zheng wrote:
> Hi David,
> 
> On 2024/6/18 01:49, David Hildenbrand wrote:
>>
>>>>
>>>> No strong opinion, something synchronous sounds to me like the
>>>> low-hanging fruit, that could add the infrastructure to be used by
>>>> something more advanced/synchronously :)
>>>
>>> Got it, I will try to do the following in the next version.
>>>
>>> a. for MADV_DONTNEED case, try synchronous reclaim as you said
>>>
>>
>> I think that really is the low hanging fruit that would cover quite some
>> cases already: (1) reclaim when MADV_DONTNEED spans the complete page
>> table.
> 
> I will check and free the PTE page in the zap_pte_range() if the
> (end - addr >= PMD_SIZE) condition is met.
> 
>>
>> Then, there is (2) reclaim when MADV_DONTNEED spans only part of the
>> page table (e.g., single PTE), but my best guess is that it's better to
>> scan for that asynchronously than making possibly each MADV_DONTNEED
>> sycall invocation slower.
> 
> Maybe just mark the vma, and then scan it in the system reclaim path.
> 
> I also plan to do this in the MADV_FREE case, instead of adding an
> asynchronous madvise option first.
> 
>>
>> (1) would already help a lot and showcase how the locking/machinery
>> would work.
>>
>>
>>> b. for MADV_FREE case:
>>>
>>>      - add a madvise option for synchronous reclaim
>>>
>>>      - add another madvise option to mark the vma, then add its
>>>              corresponding mm to a global list, and then traverse
>>>              the list and reclaim it when the memory is tight and
>>>              enters the system reclaim path.
>>>              (maybe there is an option to unmark)
>>>
>>> c. for s390 case you mentioned, create a CONFIG_FREE_PT first, and
>>>       then s390 will not select this config until the problem is solved.
>>>
>>> d. for lockless scan, try using disabling IRQ or (mmap read lock +
>>> pte_offset_map_nolock).
>>
>> Although d) really only is desired when scanning asynchronously I think.
>> During (1) above, we know that the table will be very likely empty
>> (unless weird race).
> 
> Agree.

Again, thanks for working on this. Let me know (can also do privately) 
if you run into any issues or think I can be of help. :)
Qi Zheng June 18, 2024, 9:55 a.m. UTC | #13
On 2024/6/18 17:40, David Hildenbrand wrote:
> On 18.06.24 09:51, Qi Zheng wrote:
>> Hi David,
>>
>> On 2024/6/18 01:49, David Hildenbrand wrote:
>>>
>>>>>
>>>>> No strong opinion, something synchronous sounds to me like the
>>>>> low-hanging fruit, that could add the infrastructure to be used by
>>>>> something more advanced/synchronously :)
>>>>
>>>> Got it, I will try to do the following in the next version.
>>>>
>>>> a. for MADV_DONTNEED case, try synchronous reclaim as you said
>>>>
>>>
>>> I think that really is the low hanging fruit that would cover quite some
>>> cases already: (1) reclaim when MADV_DONTNEED spans the complete page
>>> table.
>>
>> I will check and free the PTE page in the zap_pte_range() if the
>> (end - addr >= PMD_SIZE) condition is met.
>>
>>>
>>> Then, there is (2) reclaim when MADV_DONTNEED spans only part of the
>>> page table (e.g., single PTE), but my best guess is that it's better to
>>> scan for that asynchronously than making possibly each MADV_DONTNEED
>>> sycall invocation slower.
>>
>> Maybe just mark the vma, and then scan it in the system reclaim path.
>>
>> I also plan to do this in the MADV_FREE case, instead of adding an
>> asynchronous madvise option first.
>>
>>>
>>> (1) would already help a lot and showcase how the locking/machinery
>>> would work.
>>>
>>>
>>>> b. for MADV_FREE case:
>>>>
>>>>      - add a madvise option for synchronous reclaim
>>>>
>>>>      - add another madvise option to mark the vma, then add its
>>>>              corresponding mm to a global list, and then traverse
>>>>              the list and reclaim it when the memory is tight and
>>>>              enters the system reclaim path.
>>>>              (maybe there is an option to unmark)
>>>>
>>>> c. for s390 case you mentioned, create a CONFIG_FREE_PT first, and
>>>>       then s390 will not select this config until the problem is 
>>>> solved.
>>>>
>>>> d. for lockless scan, try using disabling IRQ or (mmap read lock +
>>>> pte_offset_map_nolock).
>>>
>>> Although d) really only is desired when scanning asynchronously I think.
>>> During (1) above, we know that the table will be very likely empty
>>> (unless weird race).
>>
>> Agree.
> 
> Again, thanks for working on this. Let me know (can also do privately) 
> if you run into any issues or think I can be of help. :)

That's great, thank you very much!

>