Skip to contentSkip to conversation

secretmem: optimize page_is_secretmem()

PATCH In review Mike Rapoport 3 participants 6 messages Apr 20, 2021 – May 8, 2021

Subsystems (8)
lore
Share & tools
lore

Mike Rapoport

author maintainer
#0 lore raw

v3 cover secretmem: optimize page_is_secretmem()

From: Mike Rapoport <[email protected]>

Hi,

This is an updated version of page_is_secretmem() changes. This is based on v5.12-rc7-mmots-2021-04-15-16-28.

@Andrew, please let me know if you'd like me to rebase it differently or resend the entire set.

v3:
* add missing put_compound_head() if we are to return NULL from gup_page_range(), thanks David.
* add unlikely() to test for page_is_secretmem.

v2:
* move the check for secretmem page in gup_pte_range after we get a reference to the page, per Matthew.

Mike Rapoport (2):
secretmem/gup: don't check if page is secretmem without reference
secretmem: optimize page_is_secretmem()

 include/linux/secretmem.h | 26 +++++++++++++++++++++++++-
 mm/gup.c                  |  6 +++---
 mm/secretmem.c            | 12 +-----------
 3 files changed, 29 insertions(+), 15 deletions(-)
Signature · 14 lines
2.28.0


Mike Rapoport (2):
  secretmem/gup: don't check if page is secretmem without reference
  secretmem: optimize page_is_secretmem()

 include/linux/secretmem.h | 26 +++++++++++++++++++++++++-
 mm/gup.c                  |  8 +++++---
 mm/secretmem.c            | 12 +-----------
 3 files changed, 31 insertions(+), 15 deletions(-)

-- 
2.28.0
author maintainer
#1 lore raw

PATCH v3 1/2 secretmem/gup: don't check if page is secretmem without reference

From: Mike Rapoport <[email protected]>

The check in gup_pte_range() whether a page belongs to a secretmem mapping is performed before grabbing the page reference.

To avoid potential race move the check after try_grab_compound_head().

---
 mm/gup.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
mm/gup.c +5 -3
index c3a17b189064..6515f82b0f32 100644
@@ -2080,13 +2080,15 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
 		VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
 		page = pte_page(pte);
 
-		if (page_is_secretmem(page))
-			goto pte_unmap;
-
 		head = try_grab_compound_head(page, 1, flags);
 		if (!head)
 			goto pte_unmap;
 
+		if (unlikely(page_is_secretmem(page))) {
+			put_compound_head(head, 1, flags);
+			goto pte_unmap;
+		}
+
 		if (unlikely(pte_val(pte) != pte_val(*ptep))) {
 			put_compound_head(head, 1, flags);
 			goto pte_unmap;
Signature · 4 lines
2.28.0
_______________________________________________
Linux-nvdimm mailing list -- [email protected]
To unsubscribe send an email to [email protected]
author maintainer
#3 lore raw

PATCH v3 2/2 secretmem: optimize page_is_secretmem()

In reply to Mike Rapoport

From: Mike Rapoport <[email protected]>

Kernel test robot reported -4.2% regression of will-it-scale.per_thread_ops due to commit "mm: introduce memfd_secret system call to create "secret" memory areas".

The perf profile of the test indicated that the regression is caused by page_is_secretmem() called from gup_pte_range() (inlined by gup_pgd_range):

 27.76  +2.5  30.23       perf-profile.children.cycles-pp.gup_pgd_range
  0.00  +3.2   3.19 ± 2%  perf-profile.children.cycles-pp.page_mapping
  0.00  +3.7   3.66 ± 2%  perf-profile.children.cycles-pp.page_is_secretmem

Further analysis showed that the slow down happens because neither page_is_secretmem() nor page_mapping() are not inline and moreover, multiple page flags checks in page_mapping() involve calling compound_head() several times for the same page.

Make page_is_secretmem() inline and replace page_mapping() with page flag checks that do not imply page-to-head conversion.

---
 include/linux/secretmem.h | 26 +++++++++++++++++++++++++-
 mm/secretmem.c            | 12 +-----------
 2 files changed, 26 insertions(+), 12 deletions(-)
include/linux/secretmem.h +25 -1
index 907a6734059c..b842b38cbeb1 100644
@@ -4,8 +4,32 @@
 
 #ifdef CONFIG_SECRETMEM
 
+extern const struct address_space_operations secretmem_aops;
+
+static inline bool page_is_secretmem(struct page *page)
+{
+	struct address_space *mapping;
+
+	/*
+	 * Using page_mapping() is quite slow because of the actual call
+	 * instruction and repeated compound_head(page) inside the
+	 * page_mapping() function.
+	 * We know that secretmem pages are not compound and LRU so we can
+	 * save a couple of cycles here.
+	 */
+	if (PageCompound(page) || !PageLRU(page))
+		return false;
+
+	mapping = (struct address_space *)
+		((unsigned long)page->mapping & ~PAGE_MAPPING_FLAGS);
+
+	if (mapping != page->mapping)
+		return false;
+
+	return page->mapping->a_ops == &secretmem_aops;
+}
+
 bool vma_is_secretmem(struct vm_area_struct *vma);
-bool page_is_secretmem(struct page *page);
 bool secretmem_active(void);
 
 #else
mm/secretmem.c +1 -11
index 3b1ba3991964..0bcd15e1b549 100644
@@ -151,22 +151,12 @@ static void secretmem_freepage(struct page *page)
 	clear_highpage(page);
 }
 
-static const struct address_space_operations secretmem_aops = {
+const struct address_space_operations secretmem_aops = {
 	.freepage	= secretmem_freepage,
 	.migratepage	= secretmem_migratepage,
 	.isolate_page	= secretmem_isolate_page,
 };
 
-bool page_is_secretmem(struct page *page)
-{
-	struct address_space *mapping = page_mapping(page);
-
-	if (!mapping)
-		return false;
-
-	return mapping->a_ops == &secretmem_aops;
-}
-
 static struct vfsmount *secretmem_mnt;
 
 static struct file *secretmem_file_create(unsigned long flags)
Signature · 1 line
2.28.0
Reviewed-by on v3 1/2
#2 lore raw

Re: [PATCH v3 1/2] secretmem/gup: don't check if page is secretmem without reference

In reply to Mike Rapoport
Quoted 26 lines · Mike Rapoport

From: Mike Rapoport <[email protected]>

The check in gup_pte_range() whether a page belongs to a secretmem mapping is performed before grabbing the page reference.

To avoid potential race move the check after try_grab_compound_head().

---
  mm/gup.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
mm/gup.c +0 -3
index c3a17b189064..6515f82b0f32 100644
@@ -2080,13 +2080,15 @@ static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end,
  		VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  		page = pte_page(pte);
 
-		if (page_is_secretmem(page))
-			goto pte_unmap;
-
  		head = try_grab_compound_head(page, 1, flags);
  		if (!head)

mm/gup.c static int gup_pte_range(pmd_t pmd, unsigned long addr, unsigned long end, · line 2094

Quoted 10 lines
mm/gup.c +5 -0
  			goto pte_unmap;
 
+		if (unlikely(page_is_secretmem(page))) {
+			put_compound_head(head, 1, flags);
+			goto pte_unmap;
+		}
+
  		if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  			put_compound_head(head, 1, flags);
  			goto pte_unmap;
Signature · 3 lines
Thanks,

David / dhildenb
#4 lore raw
In reply to Mike Rapoport

include/linux/secretmem.h line 29

Quoted 7 lines · Mike Rapoport
include/linux/secretmem.h +7 -0
+	mapping = (struct address_space *)
+		((unsigned long)page->mapping & ~PAGE_MAPPING_FLAGS);
+
+	if (mapping != page->mapping)
+		return false;
+
+	return page->mapping->a_ops == &secretmem_aops;

... why do you go back to page->mapping here?

	return mapping->a_ops == &secretmem_aops

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

author maintainer
#5 lore raw

<[email protected]>,Shuah Khan <[email protected]>,Thomas Gleixner <[email protected]>,Tycho Andersen <[email protected]>,Will Deacon <[email protected]>,Yury Norov <[email protected]>,[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],kernel test robot <[email protected]>
From: Mike Rapoport <[email protected]>
Message-ID: <[email protected]>

Quoted 11 lines · Matthew Wilcox

include/linux/secretmem.h line 29

Quoted 7 lines · Mike Rapoport
include/linux/secretmem.h +7 -0
+	mapping = (struct address_space *)
+		((unsigned long)page->mapping & ~PAGE_MAPPING_FLAGS);
+
+	if (mapping != page->mapping)
+		return false;
+
+	return page->mapping->a_ops == &secretmem_aops;

... why do you go back to page->mapping here?

Quoted 1 line
	return mapping->a_ops == &secretmem_aops

Ok

Signature · 2 lines
Sincerely yours,
Mike