Re: [PATCH] mm: split thp synchronously on MADV_DONTNEED

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 22.11.21 19:40, Shakeel Butt wrote:
> On Mon, Nov 22, 2021 at 12:32 AM David Hildenbrand <david@xxxxxxxxxx> wrote:
>>
>> On 20.11.21 21:12, Shakeel Butt wrote:
>>> Many applications do sophisticated management of their heap memory for
>>> better performance but with low cost. We have a bunch of such
>>> applications running on our production and examples include caching and
>>> data storage services. These applications keep their hot data on the
>>> THPs for better performance and release the cold data through
>>> MADV_DONTNEED to keep the memory cost low.
>>>
>>> The kernel defers the split and release of THPs until there is memory
>>> pressure. This causes complicates the memory management of these
>>> sophisticated applications which then needs to look into low level
>>> kernel handling of THPs to better gauge their headroom for expansion.
>>
>> Can you elaborate a bit on that point? What exactly does such an
>> application do? I would have assumed that it's mostly transparent for
>> applications.
>>
> 
> The application monitors its cgroup usage to decide if it can expand
> the memory footprint or release some (unneeded/cold) buffer. It
> releases madvise(MADV_DONTNEED) to release the memory which basically
> puts the THP into defer list. These deferred THPs are still charged to
> the cgroup which leads to bloated usage read by the application and
> making wrong decisions. Internally we added a cgroup interface to
> trigger the split of deferred THPs for that cgroup but this is hacky
> and exposing kernel internals to users. I want to solve this problem
> in a more general way for the users.

Thanks for the details, that makes sense to me. It's essentially like
another kernel buffer charged to the process, only reclaimed on memory
reclaim.

(can we add that to the patch description?)

> 
>>> In
>>> addition these applications are very latency sensitive and would prefer
>>> to not face memory reclaim due to non-deterministic nature of reclaim.
>>
>> That makes sense.
>>
>>>
>>> This patch let such applications not worry about the low level handling
>>> of THPs in the kernel and splits the THPs synchronously on
>>> MADV_DONTNEED.
>>
>> The main user I'm concerned about is virtio-balloon, which ends up
>> discarding VM memory via MADV_DONTNEED when inflating the balloon in the
>> guest in 4k granularity, but also during "free page reporting"
>> continuously when e.g., a 2MiB page becomes free in the guest. We want
>> both activities to be fast, and especially during "free page reporting",
>> to defer any heavy work.
> 
> Thanks for the info. What is the source virtio-balloon used for free pages?

So inside the guest (driver/virtio/virtio_balloon.c), we can see:

1. Balloon inflation. We essentially allocate a 4k page and send the PFN
to the hypervisor. The hypervisor will MADV_DONTNEED that memory.

2. Free page reporting. We pull some free higher-order (e.g., 2 MB on
x86-64) pages from the page allocator and report the PFNs to the
hypervisor. The hypervisor will MADV_DONTNEED the regions. Once
reported, we putback the free pages to the free page list in the page
allocator, from where it can be re-allocated eventually immediately.

On some archs (especially aarch64), we can see free page reporting
report sub-THP granularity. But usually we're dealing with THP granularity.

> 
>>
>> Do we have a performance evaluation how much overhead is added e.g., for
>> a single 4k MADV_DONTNEED call on a THP or on a MADV_DONTNEED call that
>> covers the whole THP?
> 
> I did a simple benchmark of madvise(MADV_DONTNEED) on 10000 THPs on
> x86 for both settings you suggested. I don't see any statistically
> significant difference with and without the patch. Let me know if you
> want me to try something else.

Awesome, thanks for benchmarking. I did not check, but I assume on
re-access, we won't actually re-use pages from the underlying, partially
unmapped, THP, correct? So after MADV_DONTNEED, the zapped sub-pages are
essentially lost until reclaimed by splitting the THP? If they could get
reused, there would be value in the deferred split when partially
unmapping a THP.


I do wonder which purpose the deferred split serves nowadays at all.
Fortunately, there is documentation: Documentation/vm/transhuge.rst:

"
Unmapping part of THP (with munmap() or other way) is not going to free
memory immediately. Instead, we detect that a subpage of THP is not in
use in page_remove_rmap() and queue the THP for splitting if memory
pressure comes. Splitting will free up unused subpages.

Splitting the page right away is not an option due to locking context in
the place where we can detect partial unmap. It also might be
counterproductive since in many cases partial unmap happens during
exit(2) if a THP crosses a VMA boundary.

The function deferred_split_huge_page() is used to queue a page for
splitting. The splitting itself will happen when we get memory pressure
via shrinker interface.
"

I do wonder which these locking contexts are exactly, and if we could
also do the same thing on ordinary munmap -- because I assume it can be
similarly problematic for some applications. The "exit()" case might
indeed be interesting, but I really do wonder if this is even observable
in actual number: I'm not so sure about the "many cases" but I might be
wrong, of course.

-- 
Thanks,

David / dhildenb





[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux