Skip to content

Commit 5a2796b

Browse files
committed
selftests/mm: vm_util: split up /proc/self/smaps parsing
JIRA: https://issues.redhat.com/browse/RHEL-88165 commit 3c479b5 Author: Thomas Weißschuh <thomas.weissschuh@linutronix.de> Date: Tue Jan 14 17:06:47 2025 +0100 selftests/mm: vm_util: split up /proc/self/smaps parsing Upcoming changes want to reuse the /proc/self/smaps parsing logic to parse the VmFlags field. As that works differently from the currently parsed HugePage counters, split up the logic so common functionality can be shared. While reworking this code, also use the correct sscanf placeholder for the "uint64_t thp" variable. Link: https://lkml.kernel.org/r/20250114-virtual_address_range-tests-v4-3-6fd7269934a5@linutronix.de Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de> Acked-by: David Hildenbrand <david@redhat.com> Cc: Anshuman Khandual <khandual@linux.vnet.ibm.com> Cc: Dev Jain <dev.jain@arm.com> Cc: kernel test robot <oliver.sang@intel.com> Cc: Shuah Khan (Samsung OSG) <shuah@kernel.org> Cc: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Li Wang <liwang@redhat.com>
1 parent 1b8ec45 commit 5a2796b

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

tools/testing/selftests/mm/vm_util.c

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string.h>
33
#include <fcntl.h>
44
#include <dirent.h>
5+
#include <inttypes.h>
56
#include <sys/ioctl.h>
67
#include <linux/userfaultfd.h>
78
#include <sys/syscall.h>
@@ -119,13 +120,11 @@ unsigned long rss_anon(void)
119120
return rss_anon;
120121
}
121122

122-
bool __check_huge(void *addr, char *pattern, int nr_hpages,
123-
uint64_t hpage_size)
123+
char *__get_smap_entry(void *addr, const char *pattern, char *buf, size_t len)
124124
{
125-
uint64_t thp = -1;
126125
int ret;
127126
FILE *fp;
128-
char buffer[MAX_LINE_LENGTH];
127+
char *entry = NULL;
129128
char addr_pattern[MAX_LINE_LENGTH];
130129

131130
ret = snprintf(addr_pattern, MAX_LINE_LENGTH, "%08lx-",
@@ -137,23 +136,40 @@ bool __check_huge(void *addr, char *pattern, int nr_hpages,
137136
if (!fp)
138137
ksft_exit_fail_msg("%s: Failed to open file %s\n", __func__, SMAP_FILE_PATH);
139138

140-
if (!check_for_pattern(fp, addr_pattern, buffer, sizeof(buffer)))
139+
if (!check_for_pattern(fp, addr_pattern, buf, len))
141140
goto err_out;
142141

143-
/*
144-
* Fetch the pattern in the same block and check the number of
145-
* hugepages.
146-
*/
147-
if (!check_for_pattern(fp, pattern, buffer, sizeof(buffer)))
142+
/* Fetch the pattern in the same block */
143+
if (!check_for_pattern(fp, pattern, buf, len))
148144
goto err_out;
149145

150-
snprintf(addr_pattern, MAX_LINE_LENGTH, "%s%%9ld kB", pattern);
146+
/* Trim trailing newline */
147+
entry = strchr(buf, '\n');
148+
if (entry)
149+
*entry = '\0';
151150

152-
if (sscanf(buffer, addr_pattern, &thp) != 1)
153-
ksft_exit_fail_msg("Reading smap error\n");
151+
entry = buf + strlen(pattern);
154152

155153
err_out:
156154
fclose(fp);
155+
return entry;
156+
}
157+
158+
bool __check_huge(void *addr, char *pattern, int nr_hpages,
159+
uint64_t hpage_size)
160+
{
161+
char buffer[MAX_LINE_LENGTH];
162+
uint64_t thp = -1;
163+
char *entry;
164+
165+
entry = __get_smap_entry(addr, pattern, buffer, sizeof(buffer));
166+
if (!entry)
167+
goto err_out;
168+
169+
if (sscanf(entry, "%9" SCNu64 " kB", &thp) != 1)
170+
ksft_exit_fail_msg("Reading smap error\n");
171+
172+
err_out:
157173
return thp == (nr_hpages * (hpage_size >> 10));
158174
}
159175

0 commit comments

Comments
 (0)