From: Johannes Weiner <hannes@cmpxchg.org>
To: Joonsoo Kim <js1304@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Linux Memory Management List <linux-mm@kvack.org>,
LKML <linux-kernel@vger.kernel.org>,
Michal Hocko <mhocko@kernel.org>, Hugh Dickins <hughd@google.com>,
Minchan Kim <minchan@kernel.org>,
Vlastimil Babka <vbabka@suse.cz>,
Mel Gorman <mgorman@techsingularity.net>,
kernel-team@lge.com, Joonsoo Kim <iamjoonsoo.kim@lge.com>
Subject: Re: [PATCH v5 05/10] mm/swap: charge the page when adding to the swap cache
Date: Thu, 16 Apr 2020 23:31:16 -0400 [thread overview]
Message-ID: <20200417033116.GJ195132@cmpxchg.org> (raw)
In-Reply-To: <CAAmzW4M5EBXDarx-Xkryrf+BKgdOJqBr7aBaB=4P3gBRN-v30Q@mail.gmail.com>
On Fri, Apr 17, 2020 at 10:38:53AM +0900, Joonsoo Kim wrote:
> 2020년 4월 17일 (금) 오전 1:11, Johannes Weiner <hannes@cmpxchg.org>님이 작성:
> >
> > Hello Joonsoo,
> >
> > On Fri, Apr 03, 2020 at 02:40:43PM +0900, js1304@gmail.com wrote:
> > > @@ -112,7 +112,7 @@ void show_swap_cache_info(void)
> > > * but sets SwapCache flag and private instead of mapping and index.
> > > */
> > > int add_to_swap_cache(struct page *page, swp_entry_t entry,
> > > - gfp_t gfp, void **shadowp)
> > > + struct vm_area_struct *vma, gfp_t gfp, void **shadowp)
> > > {
> > > struct address_space *address_space = swap_address_space(entry);
> > > pgoff_t idx = swp_offset(entry);
> > > @@ -120,14 +120,26 @@ int add_to_swap_cache(struct page *page, swp_entry_t entry,
> > > unsigned long i, nr = compound_nr(page);
> > > unsigned long nrexceptional = 0;
> > > void *old;
> > > + bool compound = !!compound_order(page);
> > > + int error;
> > > + struct mm_struct *mm = vma ? vma->vm_mm : current->mm;
> > > + struct mem_cgroup *memcg;
> > >
> > > VM_BUG_ON_PAGE(!PageLocked(page), page);
> > > VM_BUG_ON_PAGE(PageSwapCache(page), page);
> > > VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
> > >
> > > page_ref_add(page, nr);
> > > + /* PageSwapCache() prevent the page from being re-charged */
> > > SetPageSwapCache(page);
> > >
> > > + error = mem_cgroup_try_charge(page, mm, gfp, &memcg, compound);
> > > + if (error) {
> > > + ClearPageSwapCache(page);
> > > + page_ref_sub(page, nr);
> > > + return error;
> > > + }
> > > +
> > > do {
> > > xas_lock_irq(&xas);
> > > xas_create_range(&xas);
> > > @@ -153,11 +165,16 @@ int add_to_swap_cache(struct page *page, swp_entry_t entry,
> > > xas_unlock_irq(&xas);
> > > } while (xas_nomem(&xas, gfp));
> > >
> > > - if (!xas_error(&xas))
> > > + if (!xas_error(&xas)) {
> > > + mem_cgroup_commit_charge(page, memcg, false, compound);
> >
> > Unfortunately you cannot commit here yet because the rmap isn't set up
> > and that will cause memcg_charge_statistics() to account the page
> > incorrectly as file. And rmap is only set up during a page fault.
>
> I also found this problem a few days ago. In my investigation, what we need for
> anonymous page to make accounting correct is a way to find the type of the page,
> file or anon, since there is no special code to use the rmap. And, I
> think that it
> could be done by checking NULL mapping or something else.
page->mapping is NULL for truncated file pages, file pages before they
are inserted into the page cache, and anon pages before the rmap. It's
not straight-forward to robustly tell those apart inside memcg.
But fundamentally, it's a silly problem to fix. We only need to tell
page types apart to maintain the MEMCG_CACHE and MEMCG_RSS
counters. But these are unnecessary duplicates of the NR_FILE_PAGES
and NR_ANON_MAPPED vmstat counters - counters for which we already
have accounting sites in generic code, and that already exist in the
per-cgroup vmstat arrays. We just need to link them.
So that's what I'm fixing instead: I'm adjusting the charging sequence
slightly so that page->mem_cgroup is stable when the core VM code
accounts pages by type. And then I'm hooking into these places with
mod_lruvec_page_state and friends, and ditching MEMCG_CACHE/MEMCG_RSS.
After that, memcg doesn't have to know about the types of pages at
all. It can focus on maintaining page_counters and page->mem_cgroup,
and leave the vmstat breakdown to generic VM code.
Then we can charge pages right after allocation, regardless of type.
[ Eventually, the memcg accounting interface shouldn't be much more
than GFP_ACCOUNT (with memalloc_use_memcg() for faults, swap etc.),
and the vmstat hooks. My patches don't quite get there, but that's
the direction they're pushing. ]
> Is there anything I missed? And, I cannot find the function,
> memcg_charge_statistics(). Please let me know the file name of this
> function.
The correct name is mem_cgroup_charge_statistics(), my apologies.
> > This needs a bit of a rework of the memcg charging code that appears
> > out of scope for your patches. I'm preparing a series right now to do
> > just that. It'll also fix the swap ownership tracking problem when the
> > swap controller is disabled, so we don't have to choose between
> > charging the wrong cgroup or hampering swap readahead efficiency.
>
> Sound good! I also think that these patches are out of scope of my series.
> I will wait your patches. Could you let me know when your series is submitted?
> I'd like to plan my work schedule based on your patch schedule.
I just finished the first draft of the series a few minutes ago. I'll
polish it a bit, make sure it compiles etc. ;-), and send it out for
review, testing and rebasing, hopefully tomorrow or early next week.
next prev parent reply other threads:[~2020-04-17 3:31 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-03 5:40 [PATCH v5 00/10] workingset protection/detection on the anonymous LRU list js1304
2020-04-03 5:40 ` [PATCH v5 01/10] mm/vmscan: make active/inactive ratio as 1:1 for anon lru js1304
2020-04-03 5:40 ` [PATCH v5 02/10] mm/vmscan: protect the workingset on anonymous LRU js1304
2020-04-03 5:40 ` [PATCH v5 03/10] mm/workingset: extend the workingset detection for anon LRU js1304
2020-04-03 5:40 ` [PATCH v5 04/10] mm/swapcache: support to handle the exceptional entries in swapcache js1304
2020-04-03 5:40 ` [PATCH v5 05/10] mm/swap: charge the page when adding to the swap cache js1304
2020-04-03 18:29 ` Yang Shi
2020-04-06 1:03 ` Joonsoo Kim
2020-04-07 0:22 ` Yang Shi
2020-04-07 1:27 ` Joonsoo Kim
2020-04-16 16:11 ` Johannes Weiner
2020-04-17 1:38 ` Joonsoo Kim
2020-04-17 3:31 ` Johannes Weiner [this message]
2020-04-17 3:57 ` Joonsoo Kim
2020-04-03 5:40 ` [PATCH v5 06/10] mm/swap: implement workingset detection for anonymous LRU js1304
2020-04-03 5:40 ` [PATCH v5 07/10] mm/workingset: support to remember the previous owner of the page js1304
2020-04-03 5:40 ` [PATCH v5 08/10] mm/swap: do not readahead if the previous owner of the swap entry isn't me js1304
2020-04-03 5:40 ` [PATCH v5 09/10] mm/vmscan: restore active/inactive ratio for anonymous LRU js1304
2020-04-03 5:45 ` [PATCH v5 10/10] mm/swap: reinforce the reclaim_stat changed by anon LRU algorithm change js1304
[not found] ` <20200406091814.17256-1-hdanton@sina.com>
2020-04-07 0:40 ` [PATCH v5 02/10] mm/vmscan: protect the workingset on anonymous LRU Joonsoo Kim
[not found] ` <20200406115804.4440-1-hdanton@sina.com>
2020-04-07 0:42 ` [PATCH v5 05/10] mm/swap: charge the page when adding to the swap cache Joonsoo Kim
[not found] ` <20200407022144.11164-1-hdanton@sina.com>
2020-04-09 0:53 ` Joonsoo Kim
2020-04-08 16:55 ` [PATCH v5 00/10] workingset protection/detection on the anonymous LRU list Vlastimil Babka
2020-04-09 0:50 ` Joonsoo Kim
2020-06-03 3:57 ` Suren Baghdasaryan
2020-06-03 5:46 ` Joonsoo Kim
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200417033116.GJ195132@cmpxchg.org \
--to=hannes@cmpxchg.org \
--cc=akpm@linux-foundation.org \
--cc=hughd@google.com \
--cc=iamjoonsoo.kim@lge.com \
--cc=js1304@gmail.com \
--cc=kernel-team@lge.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mgorman@techsingularity.net \
--cc=mhocko@kernel.org \
--cc=minchan@kernel.org \
--cc=vbabka@suse.cz \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).