Skip to content

Commit 3c479b5

Browse files
t-8chakpm00
authored andcommitted
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>
1 parent b2a79f6 commit 3c479b5

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 <linux/fs.h>
@@ -193,13 +194,11 @@ unsigned long rss_anon(void)
193194
return rss_anon;
194195
}
195196

196-
bool __check_huge(void *addr, char *pattern, int nr_hpages,
197-
uint64_t hpage_size)
197+
char *__get_smap_entry(void *addr, const char *pattern, char *buf, size_t len)
198198
{
199-
uint64_t thp = -1;
200199
int ret;
201200
FILE *fp;
202-
char buffer[MAX_LINE_LENGTH];
201+
char *entry = NULL;
203202
char addr_pattern[MAX_LINE_LENGTH];
204203

205204
ret = snprintf(addr_pattern, MAX_LINE_LENGTH, "%08lx-",
@@ -211,23 +210,40 @@ bool __check_huge(void *addr, char *pattern, int nr_hpages,
211210
if (!fp)
212211
ksft_exit_fail_msg("%s: Failed to open file %s\n", __func__, SMAP_FILE_PATH);
213212

214-
if (!check_for_pattern(fp, addr_pattern, buffer, sizeof(buffer)))
213+
if (!check_for_pattern(fp, addr_pattern, buf, len))
215214
goto err_out;
216215

217-
/*
218-
* Fetch the pattern in the same block and check the number of
219-
* hugepages.
220-
*/
221-
if (!check_for_pattern(fp, pattern, buffer, sizeof(buffer)))
216+
/* Fetch the pattern in the same block */
217+
if (!check_for_pattern(fp, pattern, buf, len))
222218
goto err_out;
223219

224-
snprintf(addr_pattern, MAX_LINE_LENGTH, "%s%%9ld kB", pattern);
220+
/* Trim trailing newline */
221+
entry = strchr(buf, '\n');
222+
if (entry)
223+
*entry = '\0';
225224

226-
if (sscanf(buffer, addr_pattern, &thp) != 1)
227-
ksft_exit_fail_msg("Reading smap error\n");
225+
entry = buf + strlen(pattern);
228226

229227
err_out:
230228
fclose(fp);
229+
return entry;
230+
}
231+
232+
bool __check_huge(void *addr, char *pattern, int nr_hpages,
233+
uint64_t hpage_size)
234+
{
235+
char buffer[MAX_LINE_LENGTH];
236+
uint64_t thp = -1;
237+
char *entry;
238+
239+
entry = __get_smap_entry(addr, pattern, buffer, sizeof(buffer));
240+
if (!entry)
241+
goto err_out;
242+
243+
if (sscanf(entry, "%9" SCNu64 " kB", &thp) != 1)
244+
ksft_exit_fail_msg("Reading smap error\n");
245+
246+
err_out:
231247
return thp == (nr_hpages * (hpage_size >> 10));
232248
}
233249

0 commit comments

Comments
 (0)