LKML Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH 1/3] fs/userfaultfd.c: remove a redundant check on end
@ 2019-09-12 21:31 Wei Yang
  2019-09-12 21:31 ` [PATCH 2/3] fs/userfaultfd.c: reorder the if check to reduce some computation Wei Yang
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Wei Yang @ 2019-09-12 21:31 UTC (permalink / raw)
  To: viro; +Cc: linux-fsdevel, linux-kernel, Wei Yang

For the ending vma, there is a check to make sure the end is huge page
aligned.

The *if* check makes sure vm_start < end <= vm_end. While the first half
is not necessary, because the *for* clause makes sure vm_start < end.

This patch just removes it.

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

diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
index 653d8f7c453c..9ce09ac619a2 100644
--- a/fs/userfaultfd.c
+++ b/fs/userfaultfd.c
@@ -1402,8 +1402,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
 		 * If this vma contains ending address, and huge pages
 		 * check alignment.
 		 */
-		if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
-		    end > cur->vm_start) {
+		if (is_vm_hugetlb_page(cur) && end <= cur->vm_end) {
 			unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
 
 			ret = -EINVAL;
-- 
2.17.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] fs/userfaultfd.c: reorder the if check to reduce some computation
  2019-09-12 21:31 [PATCH 1/3] fs/userfaultfd.c: remove a redundant check on end Wei Yang
@ 2019-09-12 21:31 ` Wei Yang
  2019-09-12 21:31 ` [PATCH 3/3] fs/userfaultfd.c: wrap cheching huge page alignment into a helper Wei Yang
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Wei Yang @ 2019-09-12 21:31 UTC (permalink / raw)
  To: viro; +Cc: linux-fsdevel, linux-kernel, Wei Yang

When there are several condition check in *if* clause, the check will
stop at the first false one.

Since the for loop iterates vma list, we are sure only the last vma
meets the condition "end <= vm_end". Reorder the check sequence to
reduce some computation.

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

diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
index 9ce09ac619a2..70c0e0ef01d7 100644
--- a/fs/userfaultfd.c
+++ b/fs/userfaultfd.c
@@ -1402,7 +1402,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
 		 * If this vma contains ending address, and huge pages
 		 * check alignment.
 		 */
-		if (is_vm_hugetlb_page(cur) && end <= cur->vm_end) {
+		if (end <= cur->vm_end && is_vm_hugetlb_page(cur)) {
 			unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
 
 			ret = -EINVAL;
-- 
2.17.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] fs/userfaultfd.c: wrap cheching huge page alignment into a helper
  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
  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
  3 siblings, 0 replies; 5+ messages in thread
From: Wei Yang @ 2019-09-12 21:31 UTC (permalink / raw)
  To: viro; +Cc: linux-fsdevel, linux-kernel, Wei Yang

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] fs/userfaultfd.c: remove a redundant check on end
  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 ` [PATCH 3/3] fs/userfaultfd.c: wrap cheching huge page alignment into a helper Wei Yang
@ 2019-10-12  9:16 ` Wei Yang
  2019-11-09  1:29 ` Wei Yang
  3 siblings, 0 replies; 5+ messages in thread
From: Wei Yang @ 2019-10-12  9:16 UTC (permalink / raw)
  To: Wei Yang; +Cc: viro, linux-fsdevel, linux-kernel

Ping for comment :-)

On Fri, Sep 13, 2019 at 05:31:08AM +0800, Wei Yang wrote:
>For the ending vma, there is a check to make sure the end is huge page
>aligned.
>
>The *if* check makes sure vm_start < end <= vm_end. While the first half
>is not necessary, because the *for* clause makes sure vm_start < end.
>
>This patch just removes it.
>
>Signed-off-by: Wei Yang <richardw.yang@linux.intel.com>
>---
> fs/userfaultfd.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
>diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
>index 653d8f7c453c..9ce09ac619a2 100644
>--- a/fs/userfaultfd.c
>+++ b/fs/userfaultfd.c
>@@ -1402,8 +1402,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
> 		 * If this vma contains ending address, and huge pages
> 		 * check alignment.
> 		 */
>-		if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
>-		    end > cur->vm_start) {
>+		if (is_vm_hugetlb_page(cur) && end <= cur->vm_end) {
> 			unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
> 
> 			ret = -EINVAL;
>-- 
>2.17.1

-- 
Wei Yang
Help you, Help me

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] fs/userfaultfd.c: remove a redundant check on end
  2019-09-12 21:31 [PATCH 1/3] fs/userfaultfd.c: remove a redundant check on end Wei Yang
                   ` (2 preceding siblings ...)
  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
  3 siblings, 0 replies; 5+ messages in thread
From: Wei Yang @ 2019-11-09  1:29 UTC (permalink / raw)
  To: Wei Yang; +Cc: viro, linux-fsdevel, linux-kernel

On Fri, Sep 13, 2019 at 05:31:08AM +0800, Wei Yang wrote:
>For the ending vma, there is a check to make sure the end is huge page
>aligned.
>
>The *if* check makes sure vm_start < end <= vm_end. While the first half
>is not necessary, because the *for* clause makes sure vm_start < end.
>
>This patch just removes it.
>

Does this one look good?

>Signed-off-by: Wei Yang <richardw.yang@linux.intel.com>
>---
> fs/userfaultfd.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
>diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
>index 653d8f7c453c..9ce09ac619a2 100644
>--- a/fs/userfaultfd.c
>+++ b/fs/userfaultfd.c
>@@ -1402,8 +1402,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx,
> 		 * If this vma contains ending address, and huge pages
> 		 * check alignment.
> 		 */
>-		if (is_vm_hugetlb_page(cur) && end <= cur->vm_end &&
>-		    end > cur->vm_start) {
>+		if (is_vm_hugetlb_page(cur) && end <= cur->vm_end) {
> 			unsigned long vma_hpagesize = vma_kernel_pagesize(cur);
> 
> 			ret = -EINVAL;
>-- 
>2.17.1

-- 
Wei Yang
Help you, Help me

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, back to index

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 3/3] fs/userfaultfd.c: wrap cheching huge page alignment into a helper Wei Yang
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

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git
	git clone --mirror https://lore.kernel.org/lkml/7 lkml/git/7.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org
	public-inbox-index lkml

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git