mirror of
https://github.com/torvalds/linux.git
synced 2026-07-27 17:47:41 +02:00
selftests/mm: move read_file(), read_num() and write_num() to vm_util
These are useful helpers for writing and reading sysfs and proc files. Make them available to the tests that don't use thp_settings. While on it make write_num() use "%lu" instead of "%ld" to match 'unsigned long num' argument type. Link: https://lore.kernel.org/20260511162840.375890-30-rppt@kernel.org Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Tested-by: Luiz Capitulino <luizcap@redhat.com> Tested-by: Sarthak Sharma <sarthak.sharma@arm.com> Cc: Baolin Wang <baolin.wang@linux.alibaba.com> Cc: Barry Song <baohua@kernel.org> Cc: David Hildenbrand <david@kernel.org> Cc: Dev Jain <dev.jain@arm.com> Cc: Donet Tom <donettom@linux.ibm.com> Cc: Jason Gunthorpe <jgg@ziepe.ca> Cc: John Hubbard <jhubbard@nvidia.com> Cc: Lance Yang <lance.yang@linux.dev> Cc: Leon Romanovsky <leon@kernel.org> Cc: Liam Howlett <liam@infradead.org> Cc: Li Wang <li.wang@linux.dev> Cc: Lorenzo Stoakes <ljs@kernel.org> Cc: Mark Brown <broonie@kernel.org> Cc: Michal Hocko <mhocko@suse.com> Cc: Nico Pache <npache@redhat.com> Cc: Peter Xu <peterx@redhat.com> Cc: Ryan Roberts <ryan.roberts@arm.com> Cc: Shuah Khan <shuah@kernel.org> Cc: Suren Baghdasaryan <surenb@google.com> Cc: Vlastimil Babka <vbabka@kernel.org> Cc: Zi Yan <ziy@nvidia.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
This commit is contained in:
parent
08e51ae56a
commit
505c9605ee
|
|
@ -48,47 +48,6 @@ static const char * const shmem_enabled_strings[] = {
|
|||
NULL
|
||||
};
|
||||
|
||||
int read_file(const char *path, char *buf, size_t buflen)
|
||||
{
|
||||
int fd;
|
||||
ssize_t numread;
|
||||
|
||||
fd = open(path, O_RDONLY);
|
||||
if (fd == -1)
|
||||
return 0;
|
||||
|
||||
numread = read(fd, buf, buflen - 1);
|
||||
if (numread < 1) {
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf[numread] = '\0';
|
||||
close(fd);
|
||||
|
||||
return (unsigned int) numread;
|
||||
}
|
||||
|
||||
unsigned long read_num(const char *path)
|
||||
{
|
||||
char buf[21];
|
||||
|
||||
if (read_file(path, buf, sizeof(buf)) < 0) {
|
||||
perror("read_file()");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return strtoul(buf, NULL, 10);
|
||||
}
|
||||
|
||||
void write_num(const char *path, unsigned long num)
|
||||
{
|
||||
char buf[21];
|
||||
|
||||
sprintf(buf, "%ld", num);
|
||||
write_file(path, buf, strlen(buf) + 1);
|
||||
}
|
||||
|
||||
int thp_read_string(const char *name, const char * const strings[])
|
||||
{
|
||||
char path[PATH_MAX];
|
||||
|
|
|
|||
|
|
@ -66,10 +66,6 @@ struct thp_settings {
|
|||
struct shmem_hugepages_settings shmem_hugepages[NR_ORDERS];
|
||||
};
|
||||
|
||||
int read_file(const char *path, char *buf, size_t buflen);
|
||||
unsigned long read_num(const char *path);
|
||||
void write_num(const char *path, unsigned long num);
|
||||
|
||||
int thp_read_string(const char *name, const char * const strings[]);
|
||||
void thp_write_string(const char *name, const char *val);
|
||||
unsigned long thp_read_num(const char *name);
|
||||
|
|
|
|||
|
|
@ -698,6 +698,27 @@ int unpoison_memory(unsigned long pfn)
|
|||
return ret > 0 ? 0 : -errno;
|
||||
}
|
||||
|
||||
int read_file(const char *path, char *buf, size_t buflen)
|
||||
{
|
||||
int fd;
|
||||
ssize_t numread;
|
||||
|
||||
fd = open(path, O_RDONLY);
|
||||
if (fd == -1)
|
||||
return 0;
|
||||
|
||||
numread = read(fd, buf, buflen - 1);
|
||||
if (numread < 1) {
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf[numread] = '\0';
|
||||
close(fd);
|
||||
|
||||
return (unsigned int) numread;
|
||||
}
|
||||
|
||||
void write_file(const char *path, const char *buf, size_t buflen)
|
||||
{
|
||||
int fd, saved_errno;
|
||||
|
|
@ -721,3 +742,21 @@ void write_file(const char *path, const char *buf, size_t buflen)
|
|||
ksft_exit_fail_msg("%s write(%.*s) is truncated, expected %zu bytes, got %zd bytes\n",
|
||||
path, (int)(buflen - 1), buf, buflen - 1, numwritten);
|
||||
}
|
||||
|
||||
unsigned long read_num(const char *path)
|
||||
{
|
||||
char buf[21];
|
||||
|
||||
if (read_file(path, buf, sizeof(buf)) < 0)
|
||||
ksft_exit_fail_perror("read_file()");
|
||||
|
||||
return strtoul(buf, NULL, 10);
|
||||
}
|
||||
|
||||
void write_num(const char *path, unsigned long num)
|
||||
{
|
||||
char buf[21];
|
||||
|
||||
sprintf(buf, "%lu", num);
|
||||
write_file(path, buf, strlen(buf) + 1);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -165,3 +165,6 @@ int unpoison_memory(unsigned long pfn);
|
|||
#define PAGEMAP_PFN(ent) ((ent) & ((1ull << 55) - 1))
|
||||
|
||||
void write_file(const char *path, const char *buf, size_t buflen);
|
||||
int read_file(const char *path, char *buf, size_t buflen);
|
||||
unsigned long read_num(const char *path);
|
||||
void write_num(const char *path, unsigned long num);
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user