[07/29] selftests/mm: Merge default_huge_page_size() into one

Message ID 20230330160705.3106966-1-peterx@redhat.com
State New
Headers
Series selftests/mm: Split / Refactor userfault test |

Commit Message

Peter Xu March 30, 2023, 4:07 p.m. UTC
  There're already 3 same definitions of the three functions.  Move it into
vm_util.[ch].

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 tools/testing/selftests/mm/hugetlb-madvise.c | 25 +-------------------
 tools/testing/selftests/mm/thuge-gen.c       | 19 +--------------
 tools/testing/selftests/mm/userfaultfd.c     | 24 -------------------
 tools/testing/selftests/mm/vm_util.c         | 21 ++++++++++++++++
 tools/testing/selftests/mm/vm_util.h         |  1 +
 5 files changed, 24 insertions(+), 66 deletions(-)
  

Comments

Axel Rasmussen March 30, 2023, 8:30 p.m. UTC | #1
On Thu, Mar 30, 2023 at 9:07 AM Peter Xu <peterx@redhat.com> wrote:
>
> There're already 3 same definitions of the three functions.  Move it into
> vm_util.[ch].
>
> Signed-off-by: Peter Xu <peterx@redhat.com>

Reviewed-by: Axel Rasmussen <axelrasmussen@google.com>

> ---
>  tools/testing/selftests/mm/hugetlb-madvise.c | 25 +-------------------
>  tools/testing/selftests/mm/thuge-gen.c       | 19 +--------------
>  tools/testing/selftests/mm/userfaultfd.c     | 24 -------------------
>  tools/testing/selftests/mm/vm_util.c         | 21 ++++++++++++++++
>  tools/testing/selftests/mm/vm_util.h         |  1 +
>  5 files changed, 24 insertions(+), 66 deletions(-)
>
> diff --git a/tools/testing/selftests/mm/hugetlb-madvise.c b/tools/testing/selftests/mm/hugetlb-madvise.c
> index 9a127a8fe176..28426e30d9bc 100644
> --- a/tools/testing/selftests/mm/hugetlb-madvise.c
> +++ b/tools/testing/selftests/mm/hugetlb-madvise.c
> @@ -18,6 +18,7 @@
>  #include <unistd.h>
>  #include <sys/mman.h>
>  #include <fcntl.h>
> +#include "vm_util.h"
>
>  #define MIN_FREE_PAGES 20
>  #define NR_HUGE_PAGES  10      /* common number of pages to map/allocate */
> @@ -35,30 +36,6 @@
>  unsigned long huge_page_size;
>  unsigned long base_page_size;
>
> -/*
> - * default_huge_page_size copied from mlock2-tests.c
> - */
> -unsigned long default_huge_page_size(void)
> -{
> -       unsigned long hps = 0;
> -       char *line = NULL;
> -       size_t linelen = 0;
> -       FILE *f = fopen("/proc/meminfo", "r");
> -
> -       if (!f)
> -               return 0;
> -       while (getline(&line, &linelen, f) > 0) {
> -               if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
> -                       hps <<= 10;
> -                       break;
> -               }
> -       }
> -
> -       free(line);
> -       fclose(f);
> -       return hps;
> -}
> -
>  unsigned long get_free_hugepages(void)
>  {
>         unsigned long fhp = 0;
> diff --git a/tools/testing/selftests/mm/thuge-gen.c b/tools/testing/selftests/mm/thuge-gen.c
> index 361ef7192cc6..380ab5f0a534 100644
> --- a/tools/testing/selftests/mm/thuge-gen.c
> +++ b/tools/testing/selftests/mm/thuge-gen.c
> @@ -24,6 +24,7 @@
>  #include <unistd.h>
>  #include <stdarg.h>
>  #include <string.h>
> +#include "vm_util.h"
>
>  #define err(x) perror(x), exit(1)
>
> @@ -74,24 +75,6 @@ void find_pagesizes(void)
>         globfree(&g);
>  }
>
> -unsigned long default_huge_page_size(void)
> -{
> -       unsigned long hps = 0;
> -       char *line = NULL;
> -       size_t linelen = 0;
> -       FILE *f = fopen("/proc/meminfo", "r");
> -       if (!f)
> -               return 0;
> -       while (getline(&line, &linelen, f) > 0) {
> -               if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
> -                       hps <<= 10;
> -                       break;
> -               }
> -       }
> -       free(line);
> -       return hps;
> -}
> -
>  void show(unsigned long ps)
>  {
>         char buf[100];
> diff --git a/tools/testing/selftests/mm/userfaultfd.c b/tools/testing/selftests/mm/userfaultfd.c
> index a96d126cb40e..4cc80a0e8955 100644
> --- a/tools/testing/selftests/mm/userfaultfd.c
> +++ b/tools/testing/selftests/mm/userfaultfd.c
> @@ -1703,30 +1703,6 @@ static int userfaultfd_stress(void)
>                 || userfaultfd_events_test() || userfaultfd_minor_test();
>  }
>
> -/*
> - * Copied from mlock2-tests.c
> - */
> -unsigned long default_huge_page_size(void)
> -{
> -       unsigned long hps = 0;
> -       char *line = NULL;
> -       size_t linelen = 0;
> -       FILE *f = fopen("/proc/meminfo", "r");
> -
> -       if (!f)
> -               return 0;
> -       while (getline(&line, &linelen, f) > 0) {
> -               if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
> -                       hps <<= 10;
> -                       break;
> -               }
> -       }
> -
> -       free(line);
> -       fclose(f);
> -       return hps;
> -}
> -
>  static void set_test_type(const char *type)
>  {
>         if (!strcmp(type, "anon")) {
> diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
> index 0204c469be43..12dc654b5be3 100644
> --- a/tools/testing/selftests/mm/vm_util.c
> +++ b/tools/testing/selftests/mm/vm_util.c
> @@ -180,3 +180,24 @@ int64_t allocate_transhuge(void *ptr, int pagemap_fd)
>
>         return -1;
>  }
> +
> +unsigned long default_huge_page_size(void)
> +{
> +       unsigned long hps = 0;
> +       char *line = NULL;
> +       size_t linelen = 0;
> +       FILE *f = fopen("/proc/meminfo", "r");
> +
> +       if (!f)
> +               return 0;
> +       while (getline(&line, &linelen, f) > 0) {
> +               if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
> +                       hps <<= 10;
> +                       break;
> +               }
> +       }
> +
> +       free(line);
> +       fclose(f);
> +       return hps;
> +}
> diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
> index 6edeb531afc6..d7163fff8fb7 100644
> --- a/tools/testing/selftests/mm/vm_util.h
> +++ b/tools/testing/selftests/mm/vm_util.h
> @@ -35,6 +35,7 @@ bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);
>  bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size);
>  bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);
>  int64_t allocate_transhuge(void *ptr, int pagemap_fd);
> +unsigned long default_huge_page_size(void);
>
>  /*
>   * On ppc64 this will only work with radix 2M hugepage size
> --
> 2.39.1
>
  
Mike Kravetz March 31, 2023, 6:15 p.m. UTC | #2
On 03/30/23 12:07, Peter Xu wrote:
> There're already 3 same definitions of the three functions.  Move it into
> vm_util.[ch].
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---
>  tools/testing/selftests/mm/hugetlb-madvise.c | 25 +-------------------
>  tools/testing/selftests/mm/thuge-gen.c       | 19 +--------------
>  tools/testing/selftests/mm/userfaultfd.c     | 24 -------------------
>  tools/testing/selftests/mm/vm_util.c         | 21 ++++++++++++++++
>  tools/testing/selftests/mm/vm_util.h         |  1 +
>  5 files changed, 24 insertions(+), 66 deletions(-)

Thanks!

Some of those copies were make by me.  Sorry.

Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>

BTW - The same code (even with '... copied from mlock2-tests.c) resides
in the selftests/memfd directory.  I did that as well. :( Suspect it is OK
to leave the copy there.  But, it does make me wonder why memfd is not in
the mm directory?
  
David Hildenbrand April 3, 2023, 7:53 a.m. UTC | #3
On 30.03.23 18:07, Peter Xu wrote:
> There're already 3 same definitions of the three functions.  Move it into
> vm_util.[ch].
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>
> ---

Reviewed-by: David Hildenbrand <david@redhat.com>
  
Peter Xu April 3, 2023, 3:16 p.m. UTC | #4
On Fri, Mar 31, 2023 at 11:15:35AM -0700, Mike Kravetz wrote:
> On 03/30/23 12:07, Peter Xu wrote:
> > There're already 3 same definitions of the three functions.  Move it into
> > vm_util.[ch].
> > 
> > Signed-off-by: Peter Xu <peterx@redhat.com>
> > ---
> >  tools/testing/selftests/mm/hugetlb-madvise.c | 25 +-------------------
> >  tools/testing/selftests/mm/thuge-gen.c       | 19 +--------------
> >  tools/testing/selftests/mm/userfaultfd.c     | 24 -------------------
> >  tools/testing/selftests/mm/vm_util.c         | 21 ++++++++++++++++
> >  tools/testing/selftests/mm/vm_util.h         |  1 +
> >  5 files changed, 24 insertions(+), 66 deletions(-)
> 
> Thanks!
> 
> Some of those copies were make by me.  Sorry.
> 
> Reviewed-by: Mike Kravetz <mike.kravetz@oracle.com>

Thanks!

> 
> BTW - The same code (even with '... copied from mlock2-tests.c) resides
> in the selftests/memfd directory.  I did that as well. :( Suspect it is OK
> to leave the copy there.  But, it does make me wonder why memfd is not in
> the mm directory?

I don't know either, I suspect it was just a personal preference when it
was firstly introduced in:

        commit 4f5ce5e8d7e2da3c714df8a7fa42edb9f992fc52
        Author: David Herrmann <dh.herrmann@gmail.com>
        Date:   Fri Aug 8 14:25:32 2014 -0700

        selftests: add memfd_create() + sealing tests

So I left all those alone for now. Logically they should be merged into mm/
indeed, perhaps in some future cleanups.
  
Mike Rapoport April 7, 2023, 9:24 a.m. UTC | #5
On Thu, Mar 30, 2023 at 12:07:05PM -0400, Peter Xu wrote:
> There're already 3 same definitions of the three functions.  Move it into
> vm_util.[ch].
> 
> Signed-off-by: Peter Xu <peterx@redhat.com>

Reviewed-by: Mike Rapoport (IBM) <rppt@kernel.org>

> ---
>  tools/testing/selftests/mm/hugetlb-madvise.c | 25 +-------------------
>  tools/testing/selftests/mm/thuge-gen.c       | 19 +--------------
>  tools/testing/selftests/mm/userfaultfd.c     | 24 -------------------
>  tools/testing/selftests/mm/vm_util.c         | 21 ++++++++++++++++
>  tools/testing/selftests/mm/vm_util.h         |  1 +
>  5 files changed, 24 insertions(+), 66 deletions(-)
> 
> diff --git a/tools/testing/selftests/mm/hugetlb-madvise.c b/tools/testing/selftests/mm/hugetlb-madvise.c
> index 9a127a8fe176..28426e30d9bc 100644
> --- a/tools/testing/selftests/mm/hugetlb-madvise.c
> +++ b/tools/testing/selftests/mm/hugetlb-madvise.c
> @@ -18,6 +18,7 @@
>  #include <unistd.h>
>  #include <sys/mman.h>
>  #include <fcntl.h>
> +#include "vm_util.h"
> 
>  #define MIN_FREE_PAGES	20
>  #define NR_HUGE_PAGES	10	/* common number of pages to map/allocate */
> @@ -35,30 +36,6 @@
>  unsigned long huge_page_size;
>  unsigned long base_page_size;
> 
> -/*
> - * default_huge_page_size copied from mlock2-tests.c
> - */
> -unsigned long default_huge_page_size(void)
> -{
> -	unsigned long hps = 0;
> -	char *line = NULL;
> -	size_t linelen = 0;
> -	FILE *f = fopen("/proc/meminfo", "r");
> -
> -	if (!f)
> -		return 0;
> -	while (getline(&line, &linelen, f) > 0) {
> -		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
> -			hps <<= 10;
> -			break;
> -		}
> -	}
> -
> -	free(line);
> -	fclose(f);
> -	return hps;
> -}
> -
>  unsigned long get_free_hugepages(void)
>  {
>  	unsigned long fhp = 0;
> diff --git a/tools/testing/selftests/mm/thuge-gen.c b/tools/testing/selftests/mm/thuge-gen.c
> index 361ef7192cc6..380ab5f0a534 100644
> --- a/tools/testing/selftests/mm/thuge-gen.c
> +++ b/tools/testing/selftests/mm/thuge-gen.c
> @@ -24,6 +24,7 @@
>  #include <unistd.h>
>  #include <stdarg.h>
>  #include <string.h>
> +#include "vm_util.h"
> 
>  #define err(x) perror(x), exit(1)
> 
> @@ -74,24 +75,6 @@ void find_pagesizes(void)
>  	globfree(&g);
>  }
> 
> -unsigned long default_huge_page_size(void)
> -{
> -	unsigned long hps = 0;
> -	char *line = NULL;
> -	size_t linelen = 0;
> -	FILE *f = fopen("/proc/meminfo", "r");
> -	if (!f)
> -		return 0;
> -	while (getline(&line, &linelen, f) > 0) {
> -		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
> -			hps <<= 10;
> -			break;
> -		}
> -	}
> -	free(line);
> -	return hps;
> -}
> -
>  void show(unsigned long ps)
>  {
>  	char buf[100];
> diff --git a/tools/testing/selftests/mm/userfaultfd.c b/tools/testing/selftests/mm/userfaultfd.c
> index a96d126cb40e..4cc80a0e8955 100644
> --- a/tools/testing/selftests/mm/userfaultfd.c
> +++ b/tools/testing/selftests/mm/userfaultfd.c
> @@ -1703,30 +1703,6 @@ static int userfaultfd_stress(void)
>  		|| userfaultfd_events_test() || userfaultfd_minor_test();
>  }
> 
> -/*
> - * Copied from mlock2-tests.c
> - */
> -unsigned long default_huge_page_size(void)
> -{
> -	unsigned long hps = 0;
> -	char *line = NULL;
> -	size_t linelen = 0;
> -	FILE *f = fopen("/proc/meminfo", "r");
> -
> -	if (!f)
> -		return 0;
> -	while (getline(&line, &linelen, f) > 0) {
> -		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
> -			hps <<= 10;
> -			break;
> -		}
> -	}
> -
> -	free(line);
> -	fclose(f);
> -	return hps;
> -}
> -
>  static void set_test_type(const char *type)
>  {
>  	if (!strcmp(type, "anon")) {
> diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
> index 0204c469be43..12dc654b5be3 100644
> --- a/tools/testing/selftests/mm/vm_util.c
> +++ b/tools/testing/selftests/mm/vm_util.c
> @@ -180,3 +180,24 @@ int64_t allocate_transhuge(void *ptr, int pagemap_fd)
> 
>  	return -1;
>  }
> +
> +unsigned long default_huge_page_size(void)
> +{
> +	unsigned long hps = 0;
> +	char *line = NULL;
> +	size_t linelen = 0;
> +	FILE *f = fopen("/proc/meminfo", "r");
> +
> +	if (!f)
> +		return 0;
> +	while (getline(&line, &linelen, f) > 0) {
> +		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
> +			hps <<= 10;
> +			break;
> +		}
> +	}
> +
> +	free(line);
> +	fclose(f);
> +	return hps;
> +}
> diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
> index 6edeb531afc6..d7163fff8fb7 100644
> --- a/tools/testing/selftests/mm/vm_util.h
> +++ b/tools/testing/selftests/mm/vm_util.h
> @@ -35,6 +35,7 @@ bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);
>  bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size);
>  bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);
>  int64_t allocate_transhuge(void *ptr, int pagemap_fd);
> +unsigned long default_huge_page_size(void);
> 
>  /*
>   * On ppc64 this will only work with radix 2M hugepage size
> -- 
> 2.39.1
>
  

Patch

diff --git a/tools/testing/selftests/mm/hugetlb-madvise.c b/tools/testing/selftests/mm/hugetlb-madvise.c
index 9a127a8fe176..28426e30d9bc 100644
--- a/tools/testing/selftests/mm/hugetlb-madvise.c
+++ b/tools/testing/selftests/mm/hugetlb-madvise.c
@@ -18,6 +18,7 @@ 
 #include <unistd.h>
 #include <sys/mman.h>
 #include <fcntl.h>
+#include "vm_util.h"
 
 #define MIN_FREE_PAGES	20
 #define NR_HUGE_PAGES	10	/* common number of pages to map/allocate */
@@ -35,30 +36,6 @@ 
 unsigned long huge_page_size;
 unsigned long base_page_size;
 
-/*
- * default_huge_page_size copied from mlock2-tests.c
- */
-unsigned long default_huge_page_size(void)
-{
-	unsigned long hps = 0;
-	char *line = NULL;
-	size_t linelen = 0;
-	FILE *f = fopen("/proc/meminfo", "r");
-
-	if (!f)
-		return 0;
-	while (getline(&line, &linelen, f) > 0) {
-		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
-			hps <<= 10;
-			break;
-		}
-	}
-
-	free(line);
-	fclose(f);
-	return hps;
-}
-
 unsigned long get_free_hugepages(void)
 {
 	unsigned long fhp = 0;
diff --git a/tools/testing/selftests/mm/thuge-gen.c b/tools/testing/selftests/mm/thuge-gen.c
index 361ef7192cc6..380ab5f0a534 100644
--- a/tools/testing/selftests/mm/thuge-gen.c
+++ b/tools/testing/selftests/mm/thuge-gen.c
@@ -24,6 +24,7 @@ 
 #include <unistd.h>
 #include <stdarg.h>
 #include <string.h>
+#include "vm_util.h"
 
 #define err(x) perror(x), exit(1)
 
@@ -74,24 +75,6 @@  void find_pagesizes(void)
 	globfree(&g);
 }
 
-unsigned long default_huge_page_size(void)
-{
-	unsigned long hps = 0;
-	char *line = NULL;
-	size_t linelen = 0;
-	FILE *f = fopen("/proc/meminfo", "r");
-	if (!f)
-		return 0;
-	while (getline(&line, &linelen, f) > 0) {
-		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
-			hps <<= 10;
-			break;
-		}
-	}
-	free(line);
-	return hps;
-}
-
 void show(unsigned long ps)
 {
 	char buf[100];
diff --git a/tools/testing/selftests/mm/userfaultfd.c b/tools/testing/selftests/mm/userfaultfd.c
index a96d126cb40e..4cc80a0e8955 100644
--- a/tools/testing/selftests/mm/userfaultfd.c
+++ b/tools/testing/selftests/mm/userfaultfd.c
@@ -1703,30 +1703,6 @@  static int userfaultfd_stress(void)
 		|| userfaultfd_events_test() || userfaultfd_minor_test();
 }
 
-/*
- * Copied from mlock2-tests.c
- */
-unsigned long default_huge_page_size(void)
-{
-	unsigned long hps = 0;
-	char *line = NULL;
-	size_t linelen = 0;
-	FILE *f = fopen("/proc/meminfo", "r");
-
-	if (!f)
-		return 0;
-	while (getline(&line, &linelen, f) > 0) {
-		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
-			hps <<= 10;
-			break;
-		}
-	}
-
-	free(line);
-	fclose(f);
-	return hps;
-}
-
 static void set_test_type(const char *type)
 {
 	if (!strcmp(type, "anon")) {
diff --git a/tools/testing/selftests/mm/vm_util.c b/tools/testing/selftests/mm/vm_util.c
index 0204c469be43..12dc654b5be3 100644
--- a/tools/testing/selftests/mm/vm_util.c
+++ b/tools/testing/selftests/mm/vm_util.c
@@ -180,3 +180,24 @@  int64_t allocate_transhuge(void *ptr, int pagemap_fd)
 
 	return -1;
 }
+
+unsigned long default_huge_page_size(void)
+{
+	unsigned long hps = 0;
+	char *line = NULL;
+	size_t linelen = 0;
+	FILE *f = fopen("/proc/meminfo", "r");
+
+	if (!f)
+		return 0;
+	while (getline(&line, &linelen, f) > 0) {
+		if (sscanf(line, "Hugepagesize:       %lu kB", &hps) == 1) {
+			hps <<= 10;
+			break;
+		}
+	}
+
+	free(line);
+	fclose(f);
+	return hps;
+}
diff --git a/tools/testing/selftests/mm/vm_util.h b/tools/testing/selftests/mm/vm_util.h
index 6edeb531afc6..d7163fff8fb7 100644
--- a/tools/testing/selftests/mm/vm_util.h
+++ b/tools/testing/selftests/mm/vm_util.h
@@ -35,6 +35,7 @@  bool check_huge_anon(void *addr, int nr_hpages, uint64_t hpage_size);
 bool check_huge_file(void *addr, int nr_hpages, uint64_t hpage_size);
 bool check_huge_shmem(void *addr, int nr_hpages, uint64_t hpage_size);
 int64_t allocate_transhuge(void *ptr, int pagemap_fd);
+unsigned long default_huge_page_size(void);
 
 /*
  * On ppc64 this will only work with radix 2M hugepage size