From: "Kirill A. Shutemov" <kirill@shutemov.name>
To: Hugh Dickins <hughd@google.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Matthew Wilcox <willy@infradead.org>,
David Hildenbrand <david@redhat.com>,
Vlastimil Babka <vbabka@suse.cz>, Peter Xu <peterx@redhat.com>,
Yang Shi <shy828301@gmail.com>,
John Hubbard <jhubbard@nvidia.com>,
Mike Kravetz <mike.kravetz@oracle.com>,
Sidhartha Kumar <sidhartha.kumar@oracle.com>,
Muchun Song <songmuchun@bytedance.com>,
Miaohe Lin <linmiaohe@huawei.com>,
Naoya Horiguchi <naoya.horiguchi@linux.dev>,
Mina Almasry <almasrymina@google.com>,
James Houghton <jthoughton@google.com>,
Zach O'Keefe <zokeefe@google.com>,
linux-kernel@vger.kernel.org, linux-mm@kvack.org
Subject: Re: [PATCH 2/3] mm,thp,rmap: simplify compound page mapcount handling
Date: Sat, 5 Nov 2022 22:51:15 +0300 [thread overview]
Message-ID: <20221105195115.2d5yvvepdjsqjmmv@box> (raw)
In-Reply-To: <47ad693-717-79c8-e1ba-46c3a6602e48@google.com>
On Wed, Nov 02, 2022 at 06:51:38PM -0700, Hugh Dickins wrote:
> Compound page (folio) mapcount calculations have been different for
> anon and file (or shmem) THPs, and involved the obscure PageDoubleMap
> flag. And each huge mapping and unmapping of a file (or shmem) THP
> involved atomically incrementing and decrementing the mapcount of every
> subpage of that huge page, dirtying many struct page cachelines.
>
> Add subpages_mapcount field to the struct folio and first tail page,
> so that the total of subpage mapcounts is available in one place near
> the head: then page_mapcount() and total_mapcount() and page_mapped(),
> and their folio equivalents, are so quick that anon and file and hugetlb
> don't need to be optimized differently. Delete the unloved PageDoubleMap.
>
> page_add and page_remove rmap functions must now maintain the
> subpages_mapcount as well as the subpage _mapcount, when dealing with
> pte mappings of huge pages; and correct maintenance of NR_ANON_MAPPED
> and NR_FILE_MAPPED statistics still needs reading through the subpages,
> using nr_subpages_unmapped() - but only when first or last pmd mapping
> finds subpages_mapcount raised (double-map case, not the common case).
>
> But are those counts (used to decide when to split an anon THP, and
> in vmscan's pagecache_reclaimable heuristic) correctly maintained?
> Not quite: since page_remove_rmap() (and also split_huge_pmd()) is
> often called without page lock, there can be races when a subpage pte
> mapcount 0<->1 while compound pmd mapcount 0<->1 is scanning - races
> which the previous implementation had prevented. The statistics might
> become inaccurate, and even drift down until they underflow through 0.
> That is not good enough, but is better dealt with in a followup patch.
>
> Update a few comments on first and second tail page overlaid fields.
> hugepage_add_new_anon_rmap() has to "increment" compound_mapcount, but
> subpages_mapcount and compound_pincount are already correctly at 0,
> so delete its reinitialization of compound_pincount.
>
> A simple 100 X munmap(mmap(2GB, MAP_SHARED|MAP_POPULATE, tmpfs), 2GB)
> took 18 seconds on small pages, and used to take 1 second on huge pages,
> but now takes 119 milliseconds on huge pages. Mapping by pmds a second
> time used to take 860ms and now takes 92ms; mapping by pmds after mapping
> by ptes (when the scan is needed) used to take 870ms and now takes 495ms.
> But there might be some benchmarks which would show a slowdown, because
> tail struct pages now fall out of cache until final freeing checks them.
>
> Signed-off-by: Hugh Dickins <hughd@google.com>
Thanks for doing this!
Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
And sorry again for PageDoubleMap() :/
Minor nitpick and a question below.
> @@ -829,12 +829,20 @@ static inline int folio_entire_mapcount(struct folio *folio)
>
> /*
> * Mapcount of compound page as a whole, does not include mapped sub-pages.
> - *
> - * Must be called only for compound pages.
> + * Must be called only on head of compound page.
> */
> -static inline int compound_mapcount(struct page *page)
> +static inline int head_compound_mapcount(struct page *head)
> {
> - return folio_entire_mapcount(page_folio(page));
> + return atomic_read(compound_mapcount_ptr(head)) + 1;
> +}
> +
> +/*
> + * Sum of mapcounts of sub-pages, does not include compound mapcount.
> + * Must be called only on head of compound page.
> + */
> +static inline int head_subpages_mapcount(struct page *head)
> +{
> + return atomic_read(subpages_mapcount_ptr(head));
> }
>
> /*
Any particular reason these two do not take struct folio as an input?
It would guarantee that it is non-tail page. It will not guarantee
large-folio, but it is something.
> @@ -1265,8 +1288,6 @@ void page_add_new_anon_rmap(struct page *page,
> VM_BUG_ON_PAGE(!PageTransHuge(page), page);
> /* increment count (starts at -1) */
> atomic_set(compound_mapcount_ptr(page), 0);
> - atomic_set(compound_pincount_ptr(page), 0);
> -
It has to be initialized to 0 on allocation, right?
> __mod_lruvec_page_state(page, NR_ANON_THPS, nr);
> } else {
> /* increment count (starts at -1) */
--
Kiryl Shutsemau / Kirill A. Shutemov
next prev parent reply other threads:[~2022-11-05 19:51 UTC|newest]
Thread overview: 54+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-03 1:44 [PATCH 0/3] mm,huge,rmap: unify and speed up compound mapcounts Hugh Dickins
2022-11-03 1:48 ` [PATCH 1/3] mm,hugetlb: use folio fields in second tail page Hugh Dickins
2022-11-03 21:18 ` Sidhartha Kumar
2022-11-04 4:29 ` Hugh Dickins
2022-11-10 0:11 ` Sidhartha Kumar
2022-11-10 2:10 ` Hugh Dickins
2022-11-10 2:13 ` [PATCH 1/3 fix] mm,hugetlb: use folio fields in second tail page: fix Hugh Dickins
2022-11-05 19:13 ` [PATCH 1/3] mm,hugetlb: use folio fields in second tail page Kirill A. Shutemov
2022-11-10 1:58 ` Hugh Dickins
2022-11-03 1:51 ` [PATCH 2/3] mm,thp,rmap: simplify compound page mapcount handling Hugh Dickins
2022-11-05 19:51 ` Kirill A. Shutemov [this message]
2022-11-10 2:49 ` Hugh Dickins
2022-11-03 1:53 ` [PATCH 3/3] mm,thp,rmap: lock_compound_mapcounts() on THP mapcounts Hugh Dickins
2022-11-05 20:06 ` Kirill A. Shutemov
2022-11-10 3:31 ` Hugh Dickins
2022-11-10 2:18 ` [PATCH 4/3] mm,thp,rmap: handle the normal !PageCompound case first Hugh Dickins
2022-11-10 3:23 ` Linus Torvalds
2022-11-10 4:21 ` Hugh Dickins
2022-11-10 16:31 ` Matthew Wilcox
2022-11-10 16:58 ` Linus Torvalds
2022-11-18 9:08 ` [PATCH 0/3] mm,thp,rmap: rework the use of subpages_mapcount Hugh Dickins
2022-11-18 9:12 ` [PATCH 1/3] mm,thp,rmap: subpages_mapcount of PTE-mapped subpages Hugh Dickins
2022-11-19 0:12 ` Yu Zhao
2022-11-19 0:37 ` Hugh Dickins
2022-11-19 1:35 ` [PATCH 1/3 fix] mm,thp,rmap: subpages_mapcount of PTE-mapped subpages: fix Hugh Dickins
2022-11-21 12:38 ` Kirill A. Shutemov
2022-11-22 9:13 ` Hugh Dickins
2022-11-21 12:36 ` [PATCH 1/3] mm,thp,rmap: subpages_mapcount of PTE-mapped subpages Kirill A. Shutemov
2022-11-22 9:03 ` Hugh Dickins
2022-11-18 9:14 ` [PATCH 2/3] mm,thp,rmap: subpages_mapcount COMPOUND_MAPPED if PMD-mapped Hugh Dickins
2022-11-21 13:09 ` Kirill A. Shutemov
2022-11-22 9:33 ` Hugh Dickins
2022-11-18 9:16 ` [PATCH 3/3] mm,thp,rmap: clean up the end of __split_huge_pmd_locked() Hugh Dickins
2022-11-21 13:24 ` Kirill A. Shutemov
2022-11-18 20:18 ` [PATCH 0/3] mm,thp,rmap: rework the use of subpages_mapcount Linus Torvalds
2022-11-18 20:42 ` Johannes Weiner
2022-11-18 20:51 ` Hugh Dickins
2022-11-18 22:03 ` Andrew Morton
2022-11-18 22:07 ` Linus Torvalds
2022-11-18 22:10 ` Hugh Dickins
2022-11-18 22:23 ` Andrew Morton
2022-11-21 16:59 ` Shakeel Butt
2022-11-21 17:16 ` Linus Torvalds
2022-11-22 16:27 ` Shakeel Butt
2022-11-21 18:52 ` Johannes Weiner
2022-11-22 1:32 ` Hugh Dickins
2022-11-22 5:57 ` Matthew Wilcox
2022-11-22 6:55 ` Johannes Weiner
2022-11-22 16:30 ` Shakeel Butt
2022-11-22 9:38 ` [PATCH v2 " Hugh Dickins
2022-11-22 9:42 ` [PATCH v2 1/3] mm,thp,rmap: subpages_mapcount of PTE-mapped subpages Hugh Dickins
2022-11-22 9:49 ` [PATCH v2 2/3] mm,thp,rmap: subpages_mapcount COMPOUND_MAPPED if PMD-mapped Hugh Dickins
2022-11-22 9:51 ` [PATCH v2 3/3] mm,thp,rmap: clean up the end of __split_huge_pmd_locked() Hugh Dickins
2022-12-05 1:38 ` Hugh Dickins
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=20221105195115.2d5yvvepdjsqjmmv@box \
--to=kirill@shutemov.name \
--cc=akpm@linux-foundation.org \
--cc=almasrymina@google.com \
--cc=david@redhat.com \
--cc=hughd@google.com \
--cc=jhubbard@nvidia.com \
--cc=jthoughton@google.com \
--cc=linmiaohe@huawei.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mike.kravetz@oracle.com \
--cc=naoya.horiguchi@linux.dev \
--cc=peterx@redhat.com \
--cc=shy828301@gmail.com \
--cc=sidhartha.kumar@oracle.com \
--cc=songmuchun@bytedance.com \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
--cc=zokeefe@google.com \
/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