Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Chunyu Hu and committed by
Andrew Morton
710d2f30 929d5fbf

+20 -48
-15
tools/testing/selftests/mm/split_huge_page_test.c
··· 255 255 return status; 256 256 } 257 257 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 - 273 258 static void write_debugfs(const char *fmt, ...) 274 259 { 275 260 char input[INPUT_MAX];
+3 -32
tools/testing/selftests/mm/thp_settings.c
··· 6 6 #include <string.h> 7 7 #include <unistd.h> 8 8 9 + #include "vm_util.h" 9 10 #include "thp_settings.h" 10 11 11 12 #define THP_SYSFS "/sys/kernel/mm/transparent_hugepage/" ··· 65 64 return (unsigned int) numread; 66 65 } 67 66 68 - int write_file(const char *path, const char *buf, size_t buflen) 69 - { 70 - int fd; 71 - ssize_t numwritten; 72 - 73 - fd = open(path, O_WRONLY); 74 - if (fd == -1) { 75 - printf("open(%s)\n", path); 76 - exit(EXIT_FAILURE); 77 - return 0; 78 - } 79 - 80 - numwritten = write(fd, buf, buflen - 1); 81 - close(fd); 82 - if (numwritten < 1) { 83 - printf("write(%s)\n", buf); 84 - exit(EXIT_FAILURE); 85 - return 0; 86 - } 87 - 88 - return (unsigned int) numwritten; 89 - } 90 - 91 67 unsigned long read_num(const char *path) 92 68 { 93 69 char buf[21]; ··· 82 104 char buf[21]; 83 105 84 106 sprintf(buf, "%ld", num); 85 - if (!write_file(path, buf, strlen(buf) + 1)) { 86 - perror(path); 87 - exit(EXIT_FAILURE); 88 - } 107 + write_file(path, buf, strlen(buf) + 1); 89 108 } 90 109 91 110 int thp_read_string(const char *name, const char * const strings[]) ··· 140 165 printf("%s: Pathname is too long\n", __func__); 141 166 exit(EXIT_FAILURE); 142 167 } 143 - 144 - if (!write_file(path, val, strlen(val) + 1)) { 145 - perror(path); 146 - exit(EXIT_FAILURE); 147 - } 168 + write_file(path, val, strlen(val) + 1); 148 169 } 149 170 150 171 unsigned long thp_read_num(const char *name)
-1
tools/testing/selftests/mm/thp_settings.h
··· 63 63 }; 64 64 65 65 int read_file(const char *path, char *buf, size_t buflen); 66 - int write_file(const char *path, const char *buf, size_t buflen); 67 66 unsigned long read_num(const char *path); 68 67 void write_num(const char *path, unsigned long num); 69 68
+15
tools/testing/selftests/mm/vm_util.c
··· 764 764 765 765 return ret > 0 ? 0 : -errno; 766 766 } 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 + }
+2
tools/testing/selftests/mm/vm_util.h
··· 166 166 167 167 #define PAGEMAP_PRESENT(ent) (((ent) & (1ull << 63)) != 0) 168 168 #define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1)) 169 + 170 + void write_file(const char *path, const char *buf, size_t buflen);