Skip to content

Commit 6fb6223

Browse files
Pu Lehuiakpm00
authored andcommitted
selftests/mm: extract read_sysfs and write_sysfs into vm_util
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>
1 parent b36b701 commit 6fb6223

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

tools/testing/selftests/mm/ksm_tests.c

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,40 +58,12 @@ int debug;
5858

5959
static int ksm_write_sysfs(const char *file_path, unsigned long val)
6060
{
61-
FILE *f = fopen(file_path, "w");
62-
63-
if (!f) {
64-
fprintf(stderr, "f %s\n", file_path);
65-
perror("fopen");
66-
return 1;
67-
}
68-
if (fprintf(f, "%lu", val) < 0) {
69-
perror("fprintf");
70-
fclose(f);
71-
return 1;
72-
}
73-
fclose(f);
74-
75-
return 0;
61+
return write_sysfs(file_path, val);
7662
}
7763

7864
static int ksm_read_sysfs(const char *file_path, unsigned long *val)
7965
{
80-
FILE *f = fopen(file_path, "r");
81-
82-
if (!f) {
83-
fprintf(stderr, "f %s\n", file_path);
84-
perror("fopen");
85-
return 1;
86-
}
87-
if (fscanf(f, "%lu", val) != 1) {
88-
perror("fscanf");
89-
fclose(f);
90-
return 1;
91-
}
92-
fclose(f);
93-
94-
return 0;
66+
return read_sysfs(file_path, val);
9567
}
9668

9769
static void ksm_print_sysfs(void)

tools/testing/selftests/mm/thuge-gen.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void show(unsigned long ps)
7777
system(buf);
7878
}
7979

80-
unsigned long read_sysfs(int warn, char *fmt, ...)
80+
unsigned long thuge_read_sysfs(int warn, char *fmt, ...)
8181
{
8282
char *line = NULL;
8383
size_t linelen = 0;
@@ -106,7 +106,7 @@ unsigned long read_sysfs(int warn, char *fmt, ...)
106106

107107
unsigned long read_free(unsigned long ps)
108108
{
109-
return read_sysfs(ps != getpagesize(),
109+
return thuge_read_sysfs(ps != getpagesize(),
110110
"/sys/kernel/mm/hugepages/hugepages-%lukB/free_hugepages",
111111
ps >> 10);
112112
}
@@ -195,7 +195,7 @@ void find_pagesizes(void)
195195
}
196196
globfree(&g);
197197

198-
if (read_sysfs(0, "/proc/sys/kernel/shmmax") < NUM_PAGES * largest)
198+
if (thuge_read_sysfs(0, "/proc/sys/kernel/shmmax") < NUM_PAGES * largest)
199199
ksft_exit_fail_msg("Please do echo %lu > /proc/sys/kernel/shmmax",
200200
largest * NUM_PAGES);
201201

tools/testing/selftests/mm/vm_util.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,41 @@ int close_procmap(struct procmap_fd *procmap)
486486
{
487487
return close(procmap->fd);
488488
}
489+
490+
int write_sysfs(const char *file_path, unsigned long val)
491+
{
492+
FILE *f = fopen(file_path, "w");
493+
494+
if (!f) {
495+
fprintf(stderr, "f %s\n", file_path);
496+
perror("fopen");
497+
return 1;
498+
}
499+
if (fprintf(f, "%lu", val) < 0) {
500+
perror("fprintf");
501+
fclose(f);
502+
return 1;
503+
}
504+
fclose(f);
505+
506+
return 0;
507+
}
508+
509+
int read_sysfs(const char *file_path, unsigned long *val)
510+
{
511+
FILE *f = fopen(file_path, "r");
512+
513+
if (!f) {
514+
fprintf(stderr, "f %s\n", file_path);
515+
perror("fopen");
516+
return 1;
517+
}
518+
if (fscanf(f, "%lu", val) != 1) {
519+
perror("fscanf");
520+
fclose(f);
521+
return 1;
522+
}
523+
fclose(f);
524+
525+
return 0;
526+
}

tools/testing/selftests/mm/vm_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ int open_procmap(pid_t pid, struct procmap_fd *procmap_out);
8888
int query_procmap(struct procmap_fd *procmap);
8989
bool find_vma_procmap(struct procmap_fd *procmap, void *address);
9090
int close_procmap(struct procmap_fd *procmap);
91+
int write_sysfs(const char *file_path, unsigned long val);
92+
int read_sysfs(const char *file_path, unsigned long *val);
9193

9294
static inline int open_self_procmap(struct procmap_fd *procmap_out)
9395
{

0 commit comments

Comments
 (0)