Skip to contentSkip to conversation

mm: Exclude HugeTLB pages from THP page_mapped logic

PATCH In review Steve Capper 3 participants 5 messages Mar 29, 2016 – Apr 1, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: Exclude HugeTLB pages from THP page_mapped logic

HugeTLB pages cannot be split, thus use the compound_mapcount to track rmaps.

Currently the page_mapped function will check the compound_mapcount, but will also go through the constituent pages of a THP compound page and query the individual _mapcount's too.

Unfortunately, the page_mapped function does not distinguish between HugeTLB and THP compound pages and assumes that a compound page always needs to have HPAGE_PMD_NR pages querying.

For most cases when dealing with HugeTLB this is just inefficient, but for scenarios where the HugeTLB page size is less than the pmd block size (e.g. when using contiguous bit on ARM) this can lead to crashes.

This patch adjusts the page_mapped function such that we skip the unnecessary THP reference checks for HugeTLB pages.

Fixes e1534ae95004 ("mm: differentiate page_mapped() from page_mapcount() for compound pages")
Cc Kirill A. Shutemov <[email protected]>

---

Hi,

This patch is my approach to fixing a problem that unearthed with HugeTLB pages on arm64. We ran with PAGE_SIZE=64KB and placed down 32 contiguous ptes to create 2MB HugeTLB pages. (We can provide hints to the MMU that page table entries are contiguous thus larger TLB entries can be used to represent them).

The PMD_SIZE was 512MB thus the old version of page_mapped would read through too many struct pages and lead to BUGs.

Original problem reported here:
http://lists.infradead.org/pipermail/linux-arm-kernel/2016-March/414657.html

Having examined the HugeTLB code, I understand that only the compound_mapcount_ptr is used to track rmap presence so going through the individual _mapcounts for HugeTLB pages is superfluous? Or should I instead post a patch that changes hpage_nr_pages to use the compound order?

Also, for the sake of readability, would it be worth changing the definition of PageTransHuge to refer to only THPs (not both HugeTLB and THP)?

(I misinterpreted PageTransHuge in hpage_nr_pages initially which is one reason this problem took me longer than normal to pin down this issue).

Cheers,

Signature · 5 lines
Steve

---
 include/linux/mm.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/mm.h +2 -0
index ed6407d..4b223dc 100644
@@ -1031,6 +1031,8 @@ static inline bool page_mapped(struct page *page)
 	page = compound_head(page);
 	if (atomic_read(compound_mapcount_ptr(page)) >= 0)
 		return true;
+	if (PageHuge(page))
+		return false;
 	for (i = 0; i < hpage_nr_pages(page); i++) {
 		if (atomic_read(&page[i]._mapcount) >= 0)
 			return true;
Signature · 7 lines
2.1.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Acked-by on v1 1/1
#1 lore raw
Quoted 15 lines · Steve Capper

HugeTLB pages cannot be split, thus use the compound_mapcount to track rmaps.

Currently the page_mapped function will check the compound_mapcount, but will also go through the constituent pages of a THP compound page and query the individual _mapcount's too.

Unfortunately, the page_mapped function does not distinguish between HugeTLB and THP compound pages and assumes that a compound page always needs to have HPAGE_PMD_NR pages querying.

For most cases when dealing with HugeTLB this is just inefficient, but for scenarios where the HugeTLB page size is less than the pmd block size (e.g. when using contiguous bit on ARM) this can lead to crashes.

Quoted 6 lines

This patch adjusts the page_mapped function such that we skip the unnecessary THP reference checks for HugeTLB pages.

Fixes e1534ae95004 ("mm: differentiate page_mapped() from page_mapcount() for compound pages")
Cc Kirill A. Shutemov <[email protected]>
Quoted 13 lines

---

Hi,

This patch is my approach to fixing a problem that unearthed with HugeTLB pages on arm64. We ran with PAGE_SIZE=64KB and placed down 32 contiguous ptes to create 2MB HugeTLB pages. (We can provide hints to the MMU that page table entries are contiguous thus larger TLB entries can be used to represent them).

The PMD_SIZE was 512MB thus the old version of page_mapped would read through too many struct pages and lead to BUGs.

Quoted 8 lines

Original problem reported here:
http://lists.infradead.org/pipermail/linux-arm-kernel/2016-March/414657.html

Having examined the HugeTLB code, I understand that only the compound_mapcount_ptr is used to track rmap presence so going through the individual _mapcounts for HugeTLB pages is superfluous? Or should I instead post a patch that changes hpage_nr_pages to use the compound order?

I would not touch hpage_nr_page().

We probably need to introduce compound_nr_pages() or something to replace (1 << compound_order(page)) to be used independetely from thp/hugetlb pages.

Quoted 3 lines

Also, for the sake of readability, would it be worth changing the definition of PageTransHuge to refer to only THPs (not both HugeTLB and THP)?

I don't think so.

That would have overhead, since we wound need to do function call inside PageTransHuge(). HugeTLB() is not inlinable.

hugetlb deverges from rest of mm pretty early, so thp vs. hugetlb confusion is not that ofter. We just don't share enough codepath.

Quoted 32 lines

(I misinterpreted PageTransHuge in hpage_nr_pages initially which is one reason this problem took me longer than normal to pin down this issue).

Cheers,

Signature · 5 lines
Steve

---
 include/linux/mm.h | 2 ++
 1 file changed, 2 insertions(+)
include/linux/mm.h +2 -0
index ed6407d..4b223dc 100644
@@ -1031,6 +1031,8 @@ static inline bool page_mapped(struct page *page)
 	page = compound_head(page);
 	if (atomic_read(compound_mapcount_ptr(page)) >= 0)
 		return true;
+	if (PageHuge(page))
+		return false;
 	for (i = 0; i < hpage_nr_pages(page); i++) {
 		if (atomic_read(&page[i]._mapcount) >= 0)
 			return true;
Signature · 7 lines
2.1.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Signature · 1 line
 Kirill A. Shutemov
#2 lore raw
Quoted 16 lines · Kirill A. Shutemov
Quoted 15 lines · Steve Capper

HugeTLB pages cannot be split, thus use the compound_mapcount to track rmaps.

Currently the page_mapped function will check the compound_mapcount, but will also go through the constituent pages of a THP compound page and query the individual _mapcount's too.

Unfortunately, the page_mapped function does not distinguish between HugeTLB and THP compound pages and assumes that a compound page always needs to have HPAGE_PMD_NR pages querying.

For most cases when dealing with HugeTLB this is just inefficient, but for scenarios where the HugeTLB page size is less than the pmd block size (e.g. when using contiguous bit on ARM) this can lead to crashes.

Quoted 8 lines
Quoted 6 lines

This patch adjusts the page_mapped function such that we skip the unnecessary THP reference checks for HugeTLB pages.

Fixes e1534ae95004 ("mm: differentiate page_mapped() from page_mapcount() for compound pages")
Cc Kirill A. Shutemov <[email protected]>

Thanks!

Quoted 23 lines
Quoted 14 lines

---

Hi,

This patch is my approach to fixing a problem that unearthed with HugeTLB pages on arm64. We ran with PAGE_SIZE=64KB and placed down 32 contiguous ptes to create 2MB HugeTLB pages. (We can provide hints to the MMU that page table entries are contiguous thus larger TLB entries can be used to represent them).

The PMD_SIZE was 512MB thus the old version of page_mapped would read through too many struct pages and lead to BUGs.

Quoted 8 lines

Original problem reported here:
http://lists.infradead.org/pipermail/linux-arm-kernel/2016-March/414657.html

Having examined the HugeTLB code, I understand that only the compound_mapcount_ptr is used to track rmap presence so going through the individual _mapcounts for HugeTLB pages is superfluous? Or should I instead post a patch that changes hpage_nr_pages to use the compound order?

Quoted 5 lines

I would not touch hpage_nr_page().

We probably need to introduce compound_nr_pages() or something to replace (1 << compound_order(page)) to be used independetely from thp/hugetlb pages.

Okay, I will stick with the approach in this patch. With HugeTLB we also have hstate information to use.

Quoted 9 lines
Quoted 4 lines

Also, for the sake of readability, would it be worth changing the definition of PageTransHuge to refer to only THPs (not both HugeTLB and THP)?

I don't think so.

That would have overhead, since we wound need to do function call inside PageTransHuge(). HugeTLB() is not inlinable.

Ahh, I hadn't considered that...

Quoted 3 lines

hugetlb deverges from rest of mm pretty early, so thp vs. hugetlb confusion is not that ofter. We just don't share enough codepath.

Thanks Kirill, agreed.

Cheers,

Signature · 7 lines
Steve

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#3 lore raw
In reply to Steve Capper
Quoted 4 lines · On Tue, 29 Mar 2016 17:39:41 +0100 Steve Capper <[email protected]> wrote:

HugeTLB pages cannot be split, thus use the compound_mapcount to track rmaps.

Currently the page_mapped function will check the compound_mapcount, but

s/the page_mapped function/page_mapped()/. It's so much simpler!

Quoted 19 lines

will also go through the constituent pages of a THP compound page and query the individual _mapcount's too.

Unfortunately, the page_mapped function does not distinguish between HugeTLB and THP compound pages and assumes that a compound page always needs to have HPAGE_PMD_NR pages querying.

For most cases when dealing with HugeTLB this is just inefficient, but for scenarios where the HugeTLB page size is less than the pmd block size (e.g. when using contiguous bit on ARM) this can lead to crashes.

This patch adjusts the page_mapped function such that we skip the unnecessary THP reference checks for HugeTLB pages.

Fixes e1534ae95004 ("mm: differentiate page_mapped() from page_mapcount() for compound pages")
Cc Kirill A. Shutemov <[email protected]>

---

Quoted 7 lines

Hi,

This patch is my approach to fixing a problem that unearthed with HugeTLB pages on arm64. We ran with PAGE_SIZE=64KB and placed down 32 contiguous ptes to create 2MB HugeTLB pages. (We can provide hints to the MMU that page table entries are contiguous thus larger TLB entries can be used to represent them).

So which kernel version(s) need this patch? I think both 4.4 and 4.5 will crash in this manner? Should we backport the fix into 4.4.x and 4.5.x?

Quoted 6 lines

...

include/linux/mm.h static inline bool page_mapped(struct page *page) · line 1038

Quoted 8 lines
include/linux/mm.h +2 -0
@@ -1031,6 +1031,8 @@ static inline bool page_mapped(struct page *page)
 	page = compound_head(page);
 	if (atomic_read(compound_mapcount_ptr(page)) >= 0)
 		return true;
+	if (PageHuge(page))
+		return false;
 	for (i = 0; i < hpage_nr_pages(page); i++) {
 		if (atomic_read(&page[i]._mapcount) >= 0)
 			return true;

page_mapped() is moronically huge. Uninlining it saves 206 bytes per callsite. It has 40+ callsites.

btw, is anyone else seeing this `make M=' breakage?

akpm3:/usr/src/25> make M=mm
Makefile:679: Cannot use CONFIG_KCOV: -fsanitize-coverage=trace-pc is not supported by compiler

WARNING: Symbol version dump ./Module.symvers
is missing; modules will have no dependencies and modversions.

make[1]: *** No rule to make target mm/filemap.o', needed by mm/built-in.o'. Stop.
make: *** [_module_mm] Error 2

It's a post-4.5 thing.

From: Andrew Morton <[email protected]>
Subject: mm: uninline page_mapped()

It's huge. Uninlining it saves 206 bytes per callsite. Shaves 4924 bytes from the x86_64 allmodconfig vmlinux.

Cc Steve Capper <[email protected]>
Cc Kirill A. Shutemov <[email protected]>

---

 include/linux/mm.h |   21 +--------------------
 mm/util.c          |   22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 20 deletions(-)
include/linux/mm.h renamed from include/linux/mm.h~mm-uninline-page_mapped +1 -20
diff -puN include/linux/mm.h~mm-uninline-page_mapped include/linux/mm.h
@@ -1019,26 +1019,7 @@ static inline pgoff_t page_file_index(st
 	return page->index;
 }
 
-/*
- * Return true if this page is mapped into pagetables.
- * For compound page it returns true if any subpage of compound page is mapped.
- */
-static inline bool page_mapped(struct page *page)
-{
-	int i;
-	if (likely(!PageCompound(page)))
-		return atomic_read(&page->_mapcount) >= 0;
-	page = compound_head(page);
-	if (atomic_read(compound_mapcount_ptr(page)) >= 0)
-		return true;
-	if (PageHuge(page))
-		return false;
-	for (i = 0; i < hpage_nr_pages(page); i++) {
-		if (atomic_read(&page[i]._mapcount) >= 0)
-			return true;
-	}
-	return false;
-}
+bool page_mapped(struct page *page);
 
 /*
  * Return true only if the page has been allocated with
mm/util.c renamed from mm/util.c~mm-uninline-page_mapped +22 -0
diff -puN mm/util.c~mm-uninline-page_mapped mm/util.c
@@ -346,6 +346,28 @@ void *page_rmapping(struct page *page)
 	return __page_rmapping(page);
 }
 
+/*
+ * Return true if this page is mapped into pagetables.
+ * For compound page it returns true if any subpage of compound page is mapped.
+ */
+bool page_mapped(struct page *page)
+{
+	int i;
+	if (likely(!PageCompound(page)))
+		return atomic_read(&page->_mapcount) >= 0;
+	page = compound_head(page);
+	if (atomic_read(compound_mapcount_ptr(page)) >= 0)
+		return true;
+	if (PageHuge(page))
+		return false;
+	for (i = 0; i < hpage_nr_pages(page); i++) {
+		if (atomic_read(&page[i]._mapcount) >= 0)
+			return true;
+	}
+	return false;
+}
+EXPORT_SYMBOL(page_mapped);
+
 struct anon_vma *page_anon_vma(struct page *page)
 {
 	unsigned long mapping;

_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw

Hi Andrew,

Quoted 8 lines · Andrew Morton
Quoted 4 lines · On Tue, 29 Mar 2016 17:39:41 +0100 Steve Capper <[email protected]> wrote:

HugeTLB pages cannot be split, thus use the compound_mapcount to track rmaps.

Currently the page_mapped function will check the compound_mapcount, but

s/the page_mapped function/page_mapped()/. It's so much simpler!

Thanks, agreed :-).

Quoted 28 lines
Quoted 20 lines

will also go through the constituent pages of a THP compound page and query the individual _mapcount's too.

Unfortunately, the page_mapped function does not distinguish between HugeTLB and THP compound pages and assumes that a compound page always needs to have HPAGE_PMD_NR pages querying.

For most cases when dealing with HugeTLB this is just inefficient, but for scenarios where the HugeTLB page size is less than the pmd block size (e.g. when using contiguous bit on ARM) this can lead to crashes.

This patch adjusts the page_mapped function such that we skip the unnecessary THP reference checks for HugeTLB pages.

Fixes e1534ae95004 ("mm: differentiate page_mapped() from page_mapcount() for compound pages")
Cc Kirill A. Shutemov <[email protected]>

---

Quoted 7 lines

Hi,

This patch is my approach to fixing a problem that unearthed with HugeTLB pages on arm64. We ran with PAGE_SIZE=64KB and placed down 32 contiguous ptes to create 2MB HugeTLB pages. (We can provide hints to the MMU that page table entries are contiguous thus larger TLB entries can be used to represent them).

Quoted 3 lines

So which kernel version(s) need this patch? I think both 4.4 and 4.5 will crash in this manner? Should we backport the fix into 4.4.x and 4.5.x?

We de-activated the contiguous hint support just before 4.5 (as we ran into the problem too late). So no kernels are currently crashing due to this. If this goes in, we can then re-enable contiguous hint on ARM.

Quoted 27 lines
Quoted 7 lines

...

include/linux/mm.h static inline bool page_mapped(struct page *page) · line 1038

Quoted 8 lines
include/linux/mm.h +2 -0
@@ -1031,6 +1031,8 @@ static inline bool page_mapped(struct page *page)
 	page = compound_head(page);
 	if (atomic_read(compound_mapcount_ptr(page)) >= 0)
 		return true;
+	if (PageHuge(page))
+		return false;
 	for (i = 0; i < hpage_nr_pages(page); i++) {
 		if (atomic_read(&page[i]._mapcount) >= 0)
 			return true;

page_mapped() is moronically huge. Uninlining it saves 206 bytes per callsite. It has 40+ callsites.

btw, is anyone else seeing this `make M=' breakage?

akpm3:/usr/src/25> make M=mm
Makefile:679: Cannot use CONFIG_KCOV: -fsanitize-coverage=trace-pc is not supported by compiler

Quoted 7 lines

WARNING: Symbol version dump ./Module.symvers
is missing; modules will have no dependencies and modversions.

make[1]: *** No rule to make target mm/filemap.o', needed by mm/built-in.o'. Stop.
make: *** [_module_mm] Error 2

It's a post-4.5 thing.

Sorry I have not yet tried out KCOV.

Quoted 6 lines

From: Andrew Morton <[email protected]>
Subject: mm: uninline page_mapped()

Quoted 7 lines

It's huge. Uninlining it saves 206 bytes per callsite. Shaves 4924 bytes from the x86_64 allmodconfig vmlinux.

Cc Steve Capper <[email protected]>
Cc Kirill A. Shutemov <[email protected]>

---

The below looks reasonable to me, I don't have any benchmarks handy to test for a performance regression on this though.

Quoted 68 lines
 include/linux/mm.h |   21 +--------------------
 mm/util.c          |   22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+), 20 deletions(-)

diff -puN include/linux/mm.h~mm-uninline-page_mapped include/linux/mm.h

include/linux/mm.h renamed from include/linux/mm.h~mm-uninline-page_mapped +1 -20
@@ -1019,26 +1019,7 @@ static inline pgoff_t page_file_index(st
 	return page->index;
 }
 
-/*
- * Return true if this page is mapped into pagetables.
- * For compound page it returns true if any subpage of compound page is mapped.
- */
-static inline bool page_mapped(struct page *page)
-{
-	int i;
-	if (likely(!PageCompound(page)))
-		return atomic_read(&page->_mapcount) >= 0;
-	page = compound_head(page);
-	if (atomic_read(compound_mapcount_ptr(page)) >= 0)
-		return true;
-	if (PageHuge(page))
-		return false;
-	for (i = 0; i < hpage_nr_pages(page); i++) {
-		if (atomic_read(&page[i]._mapcount) >= 0)
-			return true;
-	}
-	return false;
-}
+bool page_mapped(struct page *page);
 
 /*
  * Return true only if the page has been allocated with

diff -puN mm/util.c~mm-uninline-page_mapped mm/util.c

mm/util.c renamed from mm/util.c~mm-uninline-page_mapped +22 -0
@@ -346,6 +346,28 @@ void *page_rmapping(struct page *page)
 	return __page_rmapping(page);
 }
 
+/*
+ * Return true if this page is mapped into pagetables.
+ * For compound page it returns true if any subpage of compound page is mapped.
+ */
+bool page_mapped(struct page *page)
+{
+	int i;
+	if (likely(!PageCompound(page)))
+		return atomic_read(&page->_mapcount) >= 0;
+	page = compound_head(page);
+	if (atomic_read(compound_mapcount_ptr(page)) >= 0)
+		return true;
+	if (PageHuge(page))
+		return false;
+	for (i = 0; i < hpage_nr_pages(page); i++) {
+		if (atomic_read(&page[i]._mapcount) >= 0)
+			return true;
+	}
+	return false;
+}
+EXPORT_SYMBOL(page_mapped);
+
 struct anon_vma *page_anon_vma(struct page *page)
 {
 	unsigned long mapping;
Quoted 2 lines

_

Cheers,

Signature · 7 lines
Steve

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>