[LTP] [PATCH 03/13] lib: Add tst_mapping_in_range()
Cyril Hrubis
chrubis@suse.cz
Wed Dec 18 19:45:08 CET 2024
This moves the range_is_mapped() function from
testcases/kernel/mem/lib/mem.c to a top level library and renames the
function to a better fitting name.
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
include/tst_memutils.h | 11 +++++++
lib/tst_memutils.c | 31 ++++++++++++++++++
.../kernel/mem/hugetlb/hugemmap/hugemmap02.c | 4 +--
.../kernel/mem/hugetlb/hugemmap/hugemmap13.c | 4 +--
.../kernel/mem/hugetlb/hugemmap/hugemmap14.c | 6 ++--
.../mem/hugetlb/hugeshmat/hugeshmat04.c | 2 +-
testcases/kernel/mem/include/mem.h | 2 --
testcases/kernel/mem/lib/mem.c | 32 -------------------
8 files changed, 50 insertions(+), 42 deletions(-)
diff --git a/include/tst_memutils.h b/include/tst_memutils.h
index 0dd941ced..e9289a4e9 100644
--- a/include/tst_memutils.h
+++ b/include/tst_memutils.h
@@ -60,4 +60,15 @@ void tst_disable_oom_protection(pid_t pid);
#define TST_PRINT_MEMINFO() safe_print_file(__FILE__, __LINE__, "/proc/meminfo")
+/**
+ * tst_mapping_in_range()
+ *
+ * @low A lower address inside of the processe address space.
+ * @high A higher address inside of the processe address space.
+ *
+ * @return Returns true if there is a mapping between low and high addresses in
+ * the process address space.
+ */
+int tst_mapping_in_range(unsigned long low, unsigned long high);
+
#endif /* TST_MEMUTILS_H__ */
diff --git a/lib/tst_memutils.c b/lib/tst_memutils.c
index e33f19d29..e49684ba1 100644
--- a/lib/tst_memutils.c
+++ b/lib/tst_memutils.c
@@ -14,6 +14,7 @@
#include "tst_test.h"
#include "tst_memutils.h"
#include "tst_capability.h"
+#include "tst_safe_stdio.h"
#include "lapi/syscalls.h"
#define BLOCKSIZE (16 * 1024 * 1024)
@@ -184,3 +185,33 @@ void tst_disable_oom_protection(pid_t pid)
{
set_oom_score_adj(pid, 0);
}
+
+int tst_mapping_in_range(unsigned long low, unsigned long high)
+{
+ FILE *fp;
+
+ fp = SAFE_FOPEN("/proc/self/maps", "r");
+
+ while (!feof(fp)) {
+ unsigned long start, end;
+ int ret;
+
+ ret = fscanf(fp, "%lx-%lx %*[^\n]\n", &start, &end);
+ if (ret != 2) {
+ fclose(fp);
+ tst_brk(TBROK | TERRNO, "Couldn't parse /proc/self/maps line.");
+ }
+
+ if ((start >= low) && (start < high)) {
+ fclose(fp);
+ return 1;
+ }
+ if ((end >= low) && (end < high)) {
+ fclose(fp);
+ return 1;
+ }
+ }
+
+ fclose(fp);
+ return 0;
+}
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c
index b788aeafb..611ae7090 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c
@@ -62,7 +62,7 @@ static void test_hugemmap(void)
addrlist[i] = addr;
}
- while (range_is_mapped(low_addr, low_addr + map_sz) == 1) {
+ while (tst_mapping_in_range(low_addr, low_addr + map_sz) == 1) {
low_addr = low_addr + 0x10000000;
if (low_addr < LOW_ADDR)
@@ -74,7 +74,7 @@ static void test_hugemmap(void)
if (addr == MAP_FAILED)
tst_brk(TBROK | TERRNO, "mmap failed on nfildes");
- while (range_is_mapped(low_addr2, low_addr2 + map_sz) == 1) {
+ while (tst_mapping_in_range(low_addr2, low_addr2 + map_sz) == 1) {
low_addr2 = low_addr2 + 0x10000000;
if (low_addr2 < LOW_ADDR2)
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap13.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap13.c
index 42b80064e..b543c8882 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap13.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap13.c
@@ -45,7 +45,7 @@ static void run_test(void)
below_start = FOURGB;
above_end = 1024ULL*1024*1024*1024;
- if (range_is_mapped(below_start, above_end) == 1) {
+ if (tst_mapping_in_range(below_start, above_end) == 1) {
tst_res(TINFO|TERRNO, "region 4G-IT is not free & "
"mmap() failed expected");
tst_res(TPASS, "Successful but inconclusive");
@@ -70,7 +70,7 @@ static void run_test(void)
below_start = FOURGB - MMAP_GRANULARITY;
above_end = FOURGB;
- if (range_is_mapped(below_start, above_end) == 1) {
+ if (tst_mapping_in_range(below_start, above_end) == 1) {
tst_res(TINFO|TERRNO, "region (4G-MMAP_GRANULARITY)-4G is not free & "
"mmap() failed expected");
tst_res(TPASS, "Successful but inconclusive");
diff --git a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap14.c b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap14.c
index c54a746b3..0a2ae312b 100644
--- a/testcases/kernel/mem/hugetlb/hugemmap/hugemmap14.c
+++ b/testcases/kernel/mem/hugetlb/hugemmap/hugemmap14.c
@@ -50,7 +50,7 @@ static void run_test(void)
below_start = FOURGB - 256ULL*1024*1024;
above_end = FOURGB;
- if (range_is_mapped(below_start, above_end) == 1) {
+ if (tst_mapping_in_range(below_start, above_end) == 1) {
tst_res(TINFO|TERRNO, "region (4G-256M)-4G is not free & "
"mmap() failed expected");
tst_res(TPASS, "Successful but inconclusive");
@@ -73,7 +73,7 @@ static void run_test(void)
below_start = FOURGB;
above_end = FOURGB + page_size;
- if (range_is_mapped(below_start, above_end) == 1) {
+ if (tst_mapping_in_range(below_start, above_end) == 1) {
tst_res(TINFO|TERRNO, "region 4G-(4G+page) is not free & "
"mmap() failed expected");
tst_res(TPASS, "Successful but inconclusive");
@@ -101,7 +101,7 @@ static void run_test(void)
below_start = highaddr;
above_end = highaddr + page_size;
- if (range_is_mapped(below_start, above_end) == 1) {
+ if (tst_mapping_in_range(below_start, above_end) == 1) {
tst_res(TINFO|TERRNO, "region haddr-(haddr+page) not free & "
"mmap() failed unexpected");
tst_res(TPASS, "Successful but inconclusive");
diff --git a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c
index 0d7bbf64f..9f877fd07 100644
--- a/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c
+++ b/testcases/kernel/mem/hugetlb/hugeshmat/hugeshmat04.c
@@ -55,7 +55,7 @@ static void shared_hugepage(void)
tst_brk(TBROK | TERRNO, "shmget");
while (boundary <= BOUNDARY_MAX
- && range_is_mapped(boundary, boundary+SIZE))
+ && tst_mapping_in_range(boundary, boundary+SIZE))
boundary += 128*1024*1024;
if (boundary > BOUNDARY_MAX)
tst_brk(TCONF, "failed to find free unmapped range");
diff --git a/testcases/kernel/mem/include/mem.h b/testcases/kernel/mem/include/mem.h
index 87528fdd0..7251a8839 100644
--- a/testcases/kernel/mem/include/mem.h
+++ b/testcases/kernel/mem/include/mem.h
@@ -73,6 +73,4 @@ unsigned int get_a_numa_node(void);
void update_shm_size(size_t *shm_size);
-/* MMAP */
-int range_is_mapped(unsigned long low, unsigned long high);
#endif
diff --git a/testcases/kernel/mem/lib/mem.c b/testcases/kernel/mem/lib/mem.c
index f293e766e..16eb39cba 100644
--- a/testcases/kernel/mem/lib/mem.c
+++ b/testcases/kernel/mem/lib/mem.c
@@ -636,35 +636,3 @@ void update_shm_size(size_t * shm_size)
*shm_size = shmmax;
}
}
-
-int range_is_mapped(unsigned long low, unsigned long high)
-{
- FILE *fp;
-
- fp = fopen("/proc/self/maps", "r");
- if (fp == NULL)
- tst_brk(TBROK | TERRNO, "Failed to open /proc/self/maps.");
-
- while (!feof(fp)) {
- unsigned long start, end;
- int ret;
-
- ret = fscanf(fp, "%lx-%lx %*[^\n]\n", &start, &end);
- if (ret != 2) {
- fclose(fp);
- tst_brk(TBROK | TERRNO, "Couldn't parse /proc/self/maps line.");
- }
-
- if ((start >= low) && (start < high)) {
- fclose(fp);
- return 1;
- }
- if ((end >= low) && (end < high)) {
- fclose(fp);
- return 1;
- }
- }
-
- fclose(fp);
- return 0;
-}
--
2.45.2
More information about the ltp
mailing list