]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
selftests/mm: extract read_sysfs and write_sysfs into vm_util
authorPu Lehui <pulehui@huawei.com>
Thu, 29 May 2025 15:56:49 +0000 (15:56 +0000)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 6 Jun 2025 04:55:42 +0000 (21:55 -0700)
Extract read_sysfs and write_sysfs into vm_util.  Meanwhile, rename the
function in thuge-gen that has the same name as read_sysfs.

Link: https://lkml.kernel.org/r/20250529155650.4017699-4-pulehui@huaweicloud.com
Signed-off-by: Pu Lehui <pulehui@huawei.com>
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Jann Horn <jannh@google.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: "Masami Hiramatsu (Google)" <mhiramat@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Vlastimil Babka <vbabka@suse.cz>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
tools/testing/selftests/mm/ksm_tests.c
tools/testing/selftests/mm/thuge-gen.c
tools/testing/selftests/mm/vm_util.c
tools/testing/selftests/mm/vm_util.h

index dcdd5bb20f3d8c9bb00a8362ced323b2acbe18fb..e80deac1436be29dff4a442393da9e1570726f47 100644 (file)
@@ -58,40 +58,12 @@ int debug;
 
 static int ksm_write_sysfs(const char *file_path, unsigned long val)
 {
-       FILE *f = fopen(file_path, "w");
-
-       if (!f) {
-               fprintf(stderr, "f %s\n", file_path);
-               perror("fopen");
-               return 1;
-       }
-       if (fprintf(f, "%lu", val) < 0) {
-               perror("fprintf");
-               fclose(f);
-               return 1;
-       }
-       fclose(f);
-
-       return 0;
+       return write_sysfs(file_path, val);
 }
 
 static int ksm_read_sysfs(const char *file_path, unsigned long *val)
 {
-       FILE *f = fopen(file_path, "r");
-
-       if (!f) {
-               fprintf(stderr, "f %s\n", file_path);
-               perror("fopen");
-               return 1;
-       }
-       if (fscanf(f, "%lu", val) != 1) {
-               perror("fscanf");
-               fclose(f);
-               return 1;
-       }
-       fclose(f);
-
-       return 0;
+       return read_sysfs(file_path, val);
 }
 
 static void ksm_print_sysfs(void)
index a41bc1234b375dae2f7d0c6329d6cf72e290523a..95b6f043a3cbd3a66655e899e9acdfc4ef50ec9e 100644 (file)
@@ -77,7 +77,7 @@ void show(unsigned long ps)
        system(buf);
 }
 
-unsigned long read_sysfs(int warn, char *fmt, ...)
+unsigned long thuge_read_sysfs(int warn, char *fmt, ...)
 {
        char *line = NULL;
        size_t linelen = 0;
@@ -106,7 +106,7 @@ unsigned long read_sysfs(int warn, char *fmt, ...)
 
 unsigned long read_free(unsigned long ps)
 {
-       return read_sysfs(ps != getpagesize(),
+       return thuge_read_sysfs(ps != getpagesize(),
                          "/sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
                          ps >> 10);
 }
@@ -195,7 +195,7 @@ void find_pagesizes(void)
        }
        globfree(&g);
 
-       if (read_sysfs(0, "/proc/sys/kernel/shmmax") < NUM_PAGES * largest)
+       if (thuge_read_sysfs(0, "/proc/sys/kernel/shmmax") < NUM_PAGES * largest)
                ksft_exit_fail_msg("Please do echo %lu > /proc/sys/kernel/shmmax",
                                   largest * NUM_PAGES);
 
index 61d7bf1f8c62ffd6b195b88c0aaee6907c639764..5492e3f784dfc39125feda0088d483da1e8c9cbd 100644 (file)
@@ -486,3 +486,41 @@ int close_procmap(struct procmap_fd *procmap)
 {
        return close(procmap->fd);
 }
+
+int write_sysfs(const char *file_path, unsigned long val)
+{
+       FILE *f = fopen(file_path, "w");
+
+       if (!f) {
+               fprintf(stderr, "f %s\n", file_path);
+               perror("fopen");
+               return 1;
+       }
+       if (fprintf(f, "%lu", val) < 0) {
+               perror("fprintf");
+               fclose(f);
+               return 1;
+       }
+       fclose(f);
+
+       return 0;
+}
+
+int read_sysfs(const char *file_path, unsigned long *val)
+{
+       FILE *f = fopen(file_path, "r");
+
+       if (!f) {
+               fprintf(stderr, "f %s\n", file_path);
+               perror("fopen");
+               return 1;
+       }
+       if (fscanf(f, "%lu", val) != 1) {
+               perror("fscanf");
+               fclose(f);
+               return 1;
+       }
+       fclose(f);
+
+       return 0;
+}
index adb5d294a2206cf4ecf07fdc9482333b879e202f..b8136d12a0f88597f8f84fc9529130c5323891bd 100644 (file)
@@ -88,6 +88,8 @@ int open_procmap(pid_t pid, struct procmap_fd *procmap_out);
 int query_procmap(struct procmap_fd *procmap);
 bool find_vma_procmap(struct procmap_fd *procmap, void *address);
 int close_procmap(struct procmap_fd *procmap);
+int write_sysfs(const char *file_path, unsigned long val);
+int read_sysfs(const char *file_path, unsigned long *val);
 
 static inline int open_self_procmap(struct procmap_fd *procmap_out)
 {