Skip to content

Commit 710d2f3

Browse files
Chunyu-Huakpm00
authored andcommitted
selftests/mm: move write_file helper to vm_util
thp_settings provides write_file() helper for safely writing to a file and exit when write failure happens. It's a very low level helper and many sub tests need such a helper, not only thp tests. split_huge_page_test also defines a write_file locally. The two have minior differences in return type and used exit api. And there would be conflicts if split_huge_page_test wanted to include thp_settings.h because of different prototype, making it less convenient. It's possisble to merge the two, although some tests don't use the kselftest infrastrucutre for testing. It would also work when using the ksft_exit_msg() to exit in my test, as the counters are all zero. Output will be like: TAP version 13 1..62 Bail out! /proc/sys/vm/drop_caches1 open failed: No such file or directory # Totals: pass:0 fail:0 xfail:0 xpass:0 skip:0 error:0 So here we just keep the version in split_huge_page_test, and move it into the vm_util. This makes it easy to maitain and user could just include one vm_util.h when they don't need thp setting helpers. Keep the prototype of void return as the function will exit on any error, return value is not necessary, and will simply the callers like write_num() and write_string(). Link: https://lore.kernel.org/20260402014543.1671131-4-chuhu@redhat.com Signed-off-by: Chunyu Hu <chuhu@redhat.com> Reviewed-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org> Acked-by: David Hildenbrand (Arm) <david@kernel.org> Reviewed-by: Zi Yan <ziy@nvidia.com> Acked-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Suggested-by: Mike Rapoport <rppt@kernel.org> Cc: Nico Pache <npache@redhat.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 929d5fb commit 710d2f3

5 files changed

Lines changed: 20 additions & 48 deletions

File tree

tools/testing/selftests/mm/split_huge_page_test.c

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -255,21 +255,6 @@ static int check_after_split_folio_orders(char *vaddr_start, size_t len,
255255
return status;
256256
}
257257

258-
static void write_file(const char *path, const char *buf, size_t buflen)
259-
{
260-
int fd;
261-
ssize_t numwritten;
262-
263-
fd = open(path, O_WRONLY);
264-
if (fd == -1)
265-
ksft_exit_fail_msg("%s open failed: %s\n", path, strerror(errno));
266-
267-
numwritten = write(fd, buf, buflen - 1);
268-
close(fd);
269-
if (numwritten < 1)
270-
ksft_exit_fail_msg("Write failed\n");
271-
}
272-
273258
static void write_debugfs(const char *fmt, ...)
274259
{
275260
char input[INPUT_MAX];

tools/testing/selftests/mm/thp_settings.c

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <string.h>
77
#include <unistd.h>
88

9+
#include "vm_util.h"
910
#include "thp_settings.h"
1011

1112
#define THP_SYSFS "/sys/kernel/mm/transparent_hugepage/"
@@ -64,29 +65,6 @@ int read_file(const char *path, char *buf, size_t buflen)
6465
return (unsigned int) numread;
6566
}
6667

67-
int write_file(const char *path, const char *buf, size_t buflen)
68-
{
69-
int fd;
70-
ssize_t numwritten;
71-
72-
fd = open(path, O_WRONLY);
73-
if (fd == -1) {
74-
printf("open(%s)\n", path);
75-
exit(EXIT_FAILURE);
76-
return 0;
77-
}
78-
79-
numwritten = write(fd, buf, buflen - 1);
80-
close(fd);
81-
if (numwritten < 1) {
82-
printf("write(%s)\n", buf);
83-
exit(EXIT_FAILURE);
84-
return 0;
85-
}
86-
87-
return (unsigned int) numwritten;
88-
}
89-
9068
unsigned long read_num(const char *path)
9169
{
9270
char buf[21];
@@ -104,10 +82,7 @@ void write_num(const char *path, unsigned long num)
10482
char buf[21];
10583

10684
sprintf(buf, "%ld", num);
107-
if (!write_file(path, buf, strlen(buf) + 1)) {
108-
perror(path);
109-
exit(EXIT_FAILURE);
110-
}
85+
write_file(path, buf, strlen(buf) + 1);
11186
}
11287

11388
int thp_read_string(const char *name, const char * const strings[])
@@ -165,11 +140,7 @@ void thp_write_string(const char *name, const char *val)
165140
printf("%s: Pathname is too long\n", __func__);
166141
exit(EXIT_FAILURE);
167142
}
168-
169-
if (!write_file(path, val, strlen(val) + 1)) {
170-
perror(path);
171-
exit(EXIT_FAILURE);
172-
}
143+
write_file(path, val, strlen(val) + 1);
173144
}
174145

175146
unsigned long thp_read_num(const char *name)

tools/testing/selftests/mm/thp_settings.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ struct thp_settings {
6363
};
6464

6565
int read_file(const char *path, char *buf, size_t buflen);
66-
int write_file(const char *path, const char *buf, size_t buflen);
6766
unsigned long read_num(const char *path);
6867
void write_num(const char *path, unsigned long num);
6968

tools/testing/selftests/mm/vm_util.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,3 +764,18 @@ int unpoison_memory(unsigned long pfn)
764764

765765
return ret > 0 ? 0 : -errno;
766766
}
767+
768+
void write_file(const char *path, const char *buf, size_t buflen)
769+
{
770+
int fd;
771+
ssize_t numwritten;
772+
773+
fd = open(path, O_WRONLY);
774+
if (fd == -1)
775+
ksft_exit_fail_msg("%s open failed: %s\n", path, strerror(errno));
776+
777+
numwritten = write(fd, buf, buflen - 1);
778+
close(fd);
779+
if (numwritten < 1)
780+
ksft_exit_fail_msg("Write failed\n");
781+
}

tools/testing/selftests/mm/vm_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,5 @@ int unpoison_memory(unsigned long pfn);
166166

167167
#define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0)
168168
#define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1))
169+
170+
void write_file(const char *path, const char *buf, size_t buflen);

0 commit comments

Comments
 (0)