linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Wei Yang <richardw.yang@linux.intel.com>
To: viro@zeniv.linux.org.uk
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Wei Yang <richardw.yang@linux.intel.com>
Subject: [PATCH 3/3] fs/userfaultfd.c: wrap cheching huge page alignment into a helper
Date: Fri, 13 Sep 2019 05:31:10 +0800	[thread overview]
Message-ID: <20190912213110.3691-3-richardw.yang@linux.intel.com> (raw)
In-Reply-To: <20190912213110.3691-1-richardw.yang@linux.intel.com>

There are three places checking whether one address is huge page
aligned.

This patch just makes a helper function to wrap it up.

Signed-off-by: Wei Yang <richardw.yang@linux.intel.com>
---
 fs/userfaultfd.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
index 70c0e0ef01d7..d8665ffdd576 100644
--- a/fs/userfaultfd.c
+++ b/fs/userfaultfd.c
@@ -1296,6 +1296,16 @@ static inline bool vma_can_userfault(struct vm_area_struct *vma)
 		vma_is_shmem(vma);
 }
 
+static inline bool addr_huge_page_aligned(unsigned long addr,
+					  struct vm_area_struct *vma)
+{
+	unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
+
+	if (addr & (vma_hpagesize - 1))
+		return false;
+	return true;
+}
+
 static int userfaultfd_register(struct userfaultfd_ctx *ctx,
 				unsigned long arg)
 {
@@ -1363,12 +1373,8 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
 	 * If the first vma contains huge pages, make sure start address
 	 * is aligned to huge page size.
 	 */
-	if (is_vm_hugetlb_page(vma)) {
-		unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
-
-		if (start & (vma_hpagesize - 1))
-			goto out_unlock;
-	}
+	if (is_vm_hugetlb_page(vma) && !addr_huge_page_aligned(start, vma))
+		goto out_unlock;
 
 	/*
 	 * Search for not compatible vmas.
@@ -1403,11 +1409,9 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
 		 * check alignment.
 		 */
 		if (end <= cur->vm_end && is_vm_hugetlb_page(cur)) {
-			unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
-
 			ret = -EINVAL;
 
-			if (end & (vma_hpagesize - 1))
+			if (!addr_huge_page_aligned(end, cur))
 				goto out_unlock;
 		}
 
@@ -1551,12 +1555,8 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx,
 	 * If the first vma contains huge pages, make sure start address
 	 * is aligned to huge page size.
 	 */
-	if (is_vm_hugetlb_page(vma)) {
-		unsigned long vma_hpagesize = vma_kernel_pagesize(vma);
-
-		if (start & (vma_hpagesize - 1))
-			goto out_unlock;
-	}
+	if (is_vm_hugetlb_page(vma) && !addr_huge_page_aligned(start, vma))
+		goto out_unlock;
 
 	/*
 	 * Search for not compatible vmas.
-- 
2.17.1


  parent reply	other threads:[~2019-09-12 21:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-12 21:31 [PATCH 1/3] fs/userfaultfd.c: remove a redundant check on end Wei Yang
2019-09-12 21:31 ` [PATCH 2/3] fs/userfaultfd.c: reorder the if check to reduce some computation Wei Yang
2019-09-12 21:31 ` Wei Yang [this message]
2019-10-12  9:16 ` [PATCH 1/3] fs/userfaultfd.c: remove a redundant check on end Wei Yang
2019-11-09  1:29 ` Wei Yang

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=20190912213110.3691-3-richardw.yang@linux.intel.com \
    --to=richardw.yang@linux.intel.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /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).