Re: [v2 PATCH -mm] mm: account deferred split THPs into MemAvailable
From: Kirill A. Shutemov
Date: Tue Aug 27 2019 - 07:02:13 EST
On Tue, Aug 27, 2019 at 08:01:39AM +0200, Michal Hocko wrote:
> On Mon 26-08-19 16:15:38, Kirill A. Shutemov wrote:
> > On Mon, Aug 26, 2019 at 09:40:35AM +0200, Michal Hocko wrote:
> > > On Thu 22-08-19 18:29:34, Kirill A. Shutemov wrote:
> > > > On Thu, Aug 22, 2019 at 02:56:56PM +0200, Vlastimil Babka wrote:
> > > > > On 8/22/19 10:04 AM, Michal Hocko wrote:
> > > > > > On Thu 22-08-19 01:55:25, Yang Shi wrote:
> > > > > >> Available memory is one of the most important metrics for memory
> > > > > >> pressure.
> > > > > >
> > > > > > I would disagree with this statement. It is a rough estimate that tells
> > > > > > how much memory you can allocate before going into a more expensive
> > > > > > reclaim (mostly swapping). Allocating that amount still might result in
> > > > > > direct reclaim induced stalls. I do realize that this is simple metric
> > > > > > that is attractive to use and works in many cases though.
> > > > > >
> > > > > >> Currently, the deferred split THPs are not accounted into
> > > > > >> available memory, but they are reclaimable actually, like reclaimable
> > > > > >> slabs.
> > > > > >>
> > > > > >> And, they seems very common with the common workloads when THP is
> > > > > >> enabled. A simple run with MariaDB test of mmtest with THP enabled as
> > > > > >> always shows it could generate over fifteen thousand deferred split THPs
> > > > > >> (accumulated around 30G in one hour run, 75% of 40G memory for my VM).
> > > > > >> It looks worth accounting in MemAvailable.
> > > > > >
> > > > > > OK, this makes sense. But your above numbers are really worrying.
> > > > > > Accumulating such a large amount of pages that are likely not going to
> > > > > > be used is really bad. They are essentially blocking any higher order
> > > > > > allocations and also push the system towards more memory pressure.
> > > > > >
> > > > > > IIUC deferred splitting is mostly a workaround for nasty locking issues
> > > > > > during splitting, right? This is not really an optimization to cache
> > > > > > THPs for reuse or something like that. What is the reason this is not
> > > > > > done from a worker context? At least THPs which would be freed
> > > > > > completely sound like a good candidate for kworker tear down, no?
> > > > >
> > > > > Agreed that it's a good question. For Kirill :) Maybe with kworker approach we
> > > > > also wouldn't need the cgroup awareness?
> > > >
> > > > I don't remember a particular locking issue, but I cannot say there's
> > > > none :P
> > > >
> > > > It's artifact from decoupling PMD split from compound page split: the same
> > > > page can be mapped multiple times with combination of PMDs and PTEs. Split
> > > > of one PMD doesn't need to trigger split of all PMDs and underlying
> > > > compound page.
> > > >
> > > > Other consideration is the fact that page split can fail and we need to
> > > > have fallback for this case.
> > > >
> > > > Also in most cases THP split would be just waste of time if we would do
> > > > them at the spot. If you don't have memory pressure it's better to wait
> > > > until process termination: less pages on LRU is still beneficial.
> > >
> > > This might be true but the reality shows that a lot of THPs might be
> > > waiting for the memory pressure that is essentially freeable on the
> > > spot. So I am not really convinced that "less pages on LRUs" is really a
> > > plausible justification. Can we free at least those THPs which are
> > > unmapped completely without any pte mappings?
> >
> > Unmapped completely pages will be freed with current code. Deferred split
> > only applies to partly mapped THPs: at least on 4k of the THP is still
> > mapped somewhere.
>
> Hmm, I am probably misreading the code but at least current Linus' tree
> reads page_remove_rmap -> [page_remove_anon_compound_rmap ->\ deferred_split_huge_page even
> for fully mapped THP.
Well, you read correctly, but it was not intended. I screwed it up at some
point.
See the patch below. It should make it work as intened.
It's not bug as such, but inefficientcy. We add page to the queue where
it's not needed.
> > > > Main source of partly mapped THPs comes from exit path. When PMD mapping
> > > > of THP got split across multiple VMAs (for instance due to mprotect()),
> > > > in exit path we unmap PTEs belonging to one VMA just before unmapping the
> > > > rest of the page. It would be total waste of time to split the page in
> > > > this scenario.
> > > >
> > > > The whole deferred split thing still looks as a reasonable compromise
> > > > to me.
> > >
> > > Even when it leads to all other problems mentioned in this and memcg
> > > deferred reclaim series?
> >
> > Yes.
> >
> > You would still need deferred split even if you *try* to split the page on
> > the spot. split_huge_page() can fail (due to pin on the page) and you will
> > need to have a way to try again later.
> >
> > You'll not win anything in complexity by trying split_huge_page()
> > immediately. I would ague you'll create much more complexity.
>
> I am not arguing for in place split. I am arguing to do it ASAP rather
> than to wait for memory pressure which might be in an unbound amount of
> time. So let me ask again. Why cannot we do that in the worker context?
> Essentially schedure the work item right away?
Let me look into it.
diff --git a/mm/rmap.c b/mm/rmap.c
index 003377e24232..45388f1bf317 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1271,12 +1271,20 @@ static void page_remove_anon_compound_rmap(struct page *page)
if (TestClearPageDoubleMap(page)) {
/*
* Subpages can be mapped with PTEs too. Check how many of
- * themi are still mapped.
+ * them are still mapped.
*/
for (i = 0, nr = 0; i < HPAGE_PMD_NR; i++) {
if (atomic_add_negative(-1, &page[i]._mapcount))
nr++;
}
+
+ /*
+ * Queue the page for deferred split if at least one small
+ * page of the compound page is unmapped, but at least one
+ * small page is still mapped.
+ */
+ if (nr && nr < HPAGE_PMD_NR)
+ deferred_split_huge_page(page);
} else {
nr = HPAGE_PMD_NR;
}
@@ -1284,10 +1292,8 @@ static void page_remove_anon_compound_rmap(struct page *page)
if (unlikely(PageMlocked(page)))
clear_page_mlock(page);
- if (nr) {
+ if (nr)
__mod_node_page_state(page_pgdat(page), NR_ANON_MAPPED, -nr);
- deferred_split_huge_page(page);
- }
}
/**
--
Kirill A. Shutemov