linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Xu <peterx@redhat.com>
To: linux-mm@kvack.org, linux-kernel@vger.kernel.org
Cc: Brian Geffon <bgeffon@google.com>,
	Pavel Emelyanov <xemul@virtuozzo.com>,
	Mike Kravetz <mike.kravetz@oracle.com>,
	David Hildenbrand <david@redhat.com>,
	peterx@redhat.com, Martin Cracauer <cracauer@cons.org>,
	Andrea Arcangeli <aarcange@redhat.com>,
	Mel Gorman <mgorman@suse.de>,
	Bobby Powers <bobbypowers@gmail.com>,
	Mike Rapoport <rppt@linux.vnet.ibm.com>,
	"Kirill A . Shutemov" <kirill@shutemov.name>,
	Maya Gokhale <gokhale2@llnl.gov>,
	Johannes Weiner <hannes@cmpxchg.org>,
	Marty McFadden <mcfadden8@llnl.gov>,
	Denis Plotnikov <dplotnikov@virtuozzo.com>,
	Hugh Dickins <hughd@google.com>,
	"Dr . David Alan Gilbert" <dgilbert@redhat.com>,
	Jerome Glisse <jglisse@redhat.com>
Subject: [PATCH v6 11/19] khugepaged: skip collapse if uffd-wp detected
Date: Thu, 20 Feb 2020 11:31:04 -0500	[thread overview]
Message-ID: <20200220163112.11409-12-peterx@redhat.com> (raw)
In-Reply-To: <20200220163112.11409-1-peterx@redhat.com>

Don't collapse the huge PMD if there is any userfault write protected
small PTEs.  The problem is that the write protection is in small page
granularity and there's no way to keep all these write protection
information if the small pages are going to be merged into a huge PMD.

The same thing needs to be considered for swap entries and migration
entries.  So do the check as well disregarding khugepaged_max_ptes_swap.

Reviewed-by: Jerome Glisse <jglisse@redhat.com>
Reviewed-by: Mike Rapoport <rppt@linux.vnet.ibm.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 include/trace/events/huge_memory.h |  1 +
 mm/khugepaged.c                    | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/trace/events/huge_memory.h b/include/trace/events/huge_memory.h
index d82a0f4e824d..70e32ff096ec 100644
--- a/include/trace/events/huge_memory.h
+++ b/include/trace/events/huge_memory.h
@@ -13,6 +13,7 @@
 	EM( SCAN_PMD_NULL,		"pmd_null")			\
 	EM( SCAN_EXCEED_NONE_PTE,	"exceed_none_pte")		\
 	EM( SCAN_PTE_NON_PRESENT,	"pte_non_present")		\
+	EM( SCAN_PTE_UFFD_WP,		"pte_uffd_wp")			\
 	EM( SCAN_PAGE_RO,		"no_writable_page")		\
 	EM( SCAN_LACK_REFERENCED_PAGE,	"lack_referenced_page")		\
 	EM( SCAN_PAGE_NULL,		"page_null")			\
diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index b679908743cb..789485cc9387 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -29,6 +29,7 @@ enum scan_result {
 	SCAN_PMD_NULL,
 	SCAN_EXCEED_NONE_PTE,
 	SCAN_PTE_NON_PRESENT,
+	SCAN_PTE_UFFD_WP,
 	SCAN_PAGE_RO,
 	SCAN_LACK_REFERENCED_PAGE,
 	SCAN_PAGE_NULL,
@@ -1141,6 +1142,15 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
 		pte_t pteval = *_pte;
 		if (is_swap_pte(pteval)) {
 			if (++unmapped <= khugepaged_max_ptes_swap) {
+				/*
+				 * Always be strict with uffd-wp
+				 * enabled swap entries.  Please see
+				 * comment below for pte_uffd_wp().
+				 */
+				if (pte_swp_uffd_wp(pteval)) {
+					result = SCAN_PTE_UFFD_WP;
+					goto out_unmap;
+				}
 				continue;
 			} else {
 				result = SCAN_EXCEED_SWAP_PTE;
@@ -1160,6 +1170,19 @@ static int khugepaged_scan_pmd(struct mm_struct *mm,
 			result = SCAN_PTE_NON_PRESENT;
 			goto out_unmap;
 		}
+		if (pte_uffd_wp(pteval)) {
+			/*
+			 * Don't collapse the page if any of the small
+			 * PTEs are armed with uffd write protection.
+			 * Here we can also mark the new huge pmd as
+			 * write protected if any of the small ones is
+			 * marked but that could bring uknown
+			 * userfault messages that falls outside of
+			 * the registered range.  So, just be simple.
+			 */
+			result = SCAN_PTE_UFFD_WP;
+			goto out_unmap;
+		}
 		if (pte_write(pteval))
 			writable = true;
 
-- 
2.24.1


  parent reply	other threads:[~2020-02-20 16:31 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-20 16:30 [PATCH v6 00/19] userfaultfd: write protection support Peter Xu
2020-02-20 16:30 ` [PATCH v6 01/19] userfaultfd: wp: add helper for writeprotect check Peter Xu
2020-02-20 16:30 ` [PATCH v6 02/19] userfaultfd: wp: hook userfault handler to write protection fault Peter Xu
2020-02-20 16:30 ` [PATCH v6 03/19] userfaultfd: wp: add WP pagetable tracking to x86 Peter Xu
2020-02-20 16:30 ` [PATCH v6 04/19] userfaultfd: wp: userfaultfd_pte/huge_pmd_wp() helpers Peter Xu
2020-02-20 16:30 ` [PATCH v6 05/19] userfaultfd: wp: add UFFDIO_COPY_MODE_WP Peter Xu
2020-02-20 16:30 ` [PATCH v6 06/19] mm: merge parameters for change_protection() Peter Xu
2020-02-20 16:31 ` [PATCH v6 07/19] userfaultfd: wp: apply _PAGE_UFFD_WP bit Peter Xu
2020-02-20 16:31 ` [PATCH v6 08/19] userfaultfd: wp: drop _PAGE_UFFD_WP properly when fork Peter Xu
2020-02-20 16:31 ` [PATCH v6 09/19] userfaultfd: wp: add pmd_swp_*uffd_wp() helpers Peter Xu
2020-02-20 16:31 ` [PATCH v6 10/19] userfaultfd: wp: support swap and page migration Peter Xu
2020-02-20 16:31 ` Peter Xu [this message]
2020-02-20 16:31 ` [PATCH v6 12/19] userfaultfd: wp: support write protection for userfault vma range Peter Xu
2020-02-20 16:31 ` [PATCH v6 13/19] userfaultfd: wp: add the writeprotect API to userfaultfd ioctl Peter Xu
2020-02-20 16:31 ` [PATCH v6 14/19] userfaultfd: wp: enabled write protection in userfaultfd API Peter Xu
2020-02-20 16:31 ` [PATCH v6 15/19] userfaultfd: wp: don't wake up when doing write protect Peter Xu
2020-02-20 16:31 ` [PATCH v6 16/19] userfaultfd: wp: UFFDIO_REGISTER_MODE_WP documentation update Peter Xu
2020-02-20 16:31 ` [PATCH v6 17/19] userfaultfd: wp: declare _UFFDIO_WRITEPROTECT conditionally Peter Xu
2020-02-20 16:31 ` [PATCH v6 18/19] userfaultfd: selftests: refactor statistics Peter Xu
2020-02-20 16:31 ` [PATCH v6 19/19] userfaultfd: selftests: add write-protect test Peter Xu

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=20200220163112.11409-12-peterx@redhat.com \
    --to=peterx@redhat.com \
    --cc=aarcange@redhat.com \
    --cc=bgeffon@google.com \
    --cc=bobbypowers@gmail.com \
    --cc=cracauer@cons.org \
    --cc=david@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=dplotnikov@virtuozzo.com \
    --cc=gokhale2@llnl.gov \
    --cc=hannes@cmpxchg.org \
    --cc=hughd@google.com \
    --cc=jglisse@redhat.com \
    --cc=kirill@shutemov.name \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mcfadden8@llnl.gov \
    --cc=mgorman@suse.de \
    --cc=mike.kravetz@oracle.com \
    --cc=rppt@linux.vnet.ibm.com \
    --cc=xemul@virtuozzo.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;
as well as URLs for NNTP newsgroup(s).