Skip to contentSkip to conversation

mem/hugetlb: shift an empty region to use in hugemmap02.c

PATCH Superseded Li Wang 3 participants 8 messages Oct 12, 2015 – Oct 13, 2015

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH v2 1/1 mem/hugetlb: shift an empty region to use in hugemmap02.c

v1 --> v2
1. add a function 'range_is_mapped()' in the mem.c
2. choice the mmap address region dynamically

the testcase 'hugemmap02' always failed on s390x as:
----------

hugemmap02    0  TINFO  :  set nr_hugepages to 128
hugemmap02    1  TPASS  :  huge mmap succeeded (64-bit)

./hugemmap02: relocation error: ./hugemmap02: symbol , version GLIBC_2.2 not defined in file libc.so.6 with link time reference
----------

it is caused by mapping in zero where libc.so.6 was loaded and resulting in the dynamic loader finding no information about versions for the library.

running the program, we could obviously see the region area:
---------------
program address:

80000000-80001000 r--s 00000000 00:05 2644                               /dev/zero
80001000-80012000 r-xp 00001000 fd:00 35121302                           /mnt/tests/kernel/distribution/ltp/generic/ltp-full-20150903/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02
80012000-80013000 r--p 00011000 fd:00 35121302                           /mnt/tests/kernel/distribution/ltp/generic/ltp-full-20150903/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02
80013000-80014000 rw-p 00012000 fd:00 35121302                           /mnt/tests/kernel/distribution/ltp/generic/ltp-full-20150903/testcases/kernel/mem/hugetlb/hugemmap/hugemmap02
80014000-80017000 rw-p 00000000 00:00 0
90000000-90200000 rw-s 00000000 00:29 75950                              /tmp/hugme85Xf/mmapfile41071
9c14f000-9c170000 rw-p 00000000 00:00 0                                  [heap]
3ffad342000-3ffbd342000 r--s 00000000 00:05 2644                         /dev/zero
3ffbd342000-3ffcd342000 r--s 00000000 00:05 2644                         /dev/zero
3ffcd342000-3ffdd342000 r--s 00000000 00:05 2644                         /dev/zero
3ffdd342000-3ffed342000 r--s 00000000 00:05 2644                         /dev/zero
3ffed342000-3fffd342000 r--s 00000000 00:05 2644                         /dev/zero
3fffd342000-3fffd344000 rw-p 00000000 00:00 0
3fffd344000-3fffd4db000 r-xp 00000000 fd:00 5438                         /usr/lib64/libc-2.17.so
3fffd4db000-3fffd4df000 r--p 00196000 fd:00 5438                         /usr/lib64/libc-2.17.so
3fffd4df000-3fffd4e1000 rw-p 0019a000 fd:00 5438                         /usr/lib64/libc-2.17.so
3fffd4e1000-3fffd4e6000 rw-p 00000000 00:00 0
3fffd4e6000-3fffd4fe000 r-xp 00000000 fd:00 5464                         /usr/lib64/libpthread-2.17.so
3fffd4fe000-3fffd4ff000 r--p 00017000 fd:00 5464                         /usr/lib64/libpthread-2.17.so
3fffd4ff000-3fffd500000 rw-p 00018000 fd:00 5464                         /usr/lib64/libpthread-2.17.so
3fffd500000-3fffd504000 rw-p 00000000 00:00 0
3fffd50c000-3fffd510000 rw-p 00000000 00:00 0
3fffd510000-3fffd512000 r-xp 00000000 00:00 0                            [vdso]
3fffd512000-3fffd534000 r-xp 00000000 fd:00 5431                         /usr/lib64/ld-2.17.so
3fffd534000-3fffd535000 r--p 00022000 fd:00 5431                         /usr/lib64/ld-2.17.so
3fffd535000-3fffd536000 rw-p 00023000 fd:00 5431                         /usr/lib64/ld-2.17.so

3fffd536000-3fffd537000 rw-p 00000000 00:00 0
3ffffaaf000-3ffffad0000 rw-p 00000000 00:00 0 [stack]
-------------

---
 testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c | 24 ++++++++++--
 testcases/kernel/mem/include/mem.h                 |  2 +
 testcases/kernel/mem/lib/mem.c                     | 43 ++++++++++++++++++++++
 3 files changed, 65 insertions(+), 4 deletions(-)
testcases/kernel/mem/hugetlb/hugemmap/hugemmap02.c +20 -4
index cfdcb3c..a4fbff9 100644
@@ -56,8 +56,8 @@
 #include "safe_macros.h"
 #include "mem.h"
 
-#define LOW_ADDR       (void *)(0x80000000)
-#define LOW_ADDR2      (void *)(0x90000000)
+#define LOW_ADDR       0x80000000
+#define LOW_ADDR2      0x90000000
 
 static char TEMPFILE[MAXPATHLEN];
 
@@ -65,6 +65,8 @@ char *TCID = "hugemmap02";
 int TST_TOTAL = 1;
 static unsigned long *addr;
 static unsigned long *addr2;
+static unsigned long low_addr = LOW_ADDR;
+static unsigned long low_addr2 = LOW_ADDR2;
 static unsigned long *addrlist[5];
 static int i;
 static int fildes;
@@ -127,15 +129,29 @@ int main(int ac, char **av)
 			addrlist[i] = addr;
 		}
 
+		while (range_is_mapped(low_addr, low_addr + 0x1000) == 1) {
+			low_addr = low_addr + 0x10000000;
+
+			if (low_addr < LOW_ADDR)
+				tst_brkm(TBROK | TERRNO, cleanup,
+						"no empty region to use");
+		}
 		/* mmap using normal pages and a low memory address */
-		addr = mmap(LOW_ADDR, page_sz, PROT_READ,
+		addr = mmap((void *)low_addr, page_sz, PROT_READ,
 			    MAP_SHARED | MAP_FIXED, nfildes, 0);
 		if (addr == MAP_FAILED)
 			tst_brkm(TBROK | TERRNO, cleanup,
 				 "mmap failed on nfildes");
 
+		while (range_is_mapped(low_addr2, low_addr2 + 0x1000) == 1) {
+			low_addr2 = low_addr2 + 0x10000000;
+
+			if (low_addr2 < LOW_ADDR2)
+				tst_brkm(TBROK | TERRNO, cleanup,
+						"no empty region to use");
+		}
 		/* Attempt to mmap a huge page into a low memory address */
-		addr2 = mmap(LOW_ADDR2, map_sz, PROT_READ | PROT_WRITE,
+		addr2 = mmap((void *)low_addr2, map_sz, PROT_READ | PROT_WRITE,
 			     MAP_SHARED, fildes, 0);
 #if __WORDSIZE == 64		/* 64-bit process */
 		if (addr2 == MAP_FAILED) {
testcases/kernel/mem/include/mem.h +2 -0
index fd1879c..7ed3835 100644
@@ -100,4 +100,6 @@ void setup(void);
 
 void update_shm_size(size_t *shm_size);
 
+/* MMAP */
+int range_is_mapped(unsigned long low, unsigned long high)
 #endif
testcases/kernel/mem/lib/mem.c +43 -0

46 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 1/1 mem/hugetlb: shift an empty region to use in hugemmap02.c

Hi!
Generall idea looks good, a few comments below.

Quoted 12 lines
testcases/kernel/mem/include/mem.h +2 -0
+/* MMAP */
+int range_is_mapped(unsigned long low, unsigned long high)
 #endif
testcases/kernel/mem/lib/mem.c +1 -0
index 118b6c0..e0e1bdf 100644
@@ -1113,3 +1113,46 @@ void update_shm_size(size_t * shm_size)
 		*shm_size = shmmax;
 	}
 }
+

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1126

Quoted 10 lines
testcases/kernel/mem/lib/mem.c +10 -0
+int range_is_mapped(unsigned long low, unsigned long high)
+{
+	FILE *f;
+	int MAPS_BUF_SZ = 4096;
+	char line[MAPS_BUF_SZ];
+	char *tmp;
+
+	f = fopen("/proc/self/maps", "r");
+	if (!f) {
+		printf("Failed to open /proc/self/maps.\n");
                ^
		should rather be tst_resm(TWARN, );
Quoted 5 lines
testcases/kernel/mem/lib/mem.c +5 -0
+		return -1;
+	}
+
+	while (1) {
+		unsigned long start, end;

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1141

Quoted 10 lines
testcases/kernel/mem/lib/mem.c +10 -0
+		int ret;
+
+		tmp = fgets(line, MAPS_BUF_SZ, f);
+		if (!tmp)
+			break;
+
+		ret = sscanf(line, "%lx-%lx", &start, &end);
+		if (ret != 2) {
+			printf("Couldn't parse /proc/self/maps line: %s\n",
+					line);

^
here as well

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1144

Quoted 3 lines
testcases/kernel/mem/lib/mem.c +3 -0
+			fclose(f);
+			return -1;
+		}

Why don't you just use fscanf()? It doesn't make sense to read the line into the buffer and then sscanf() the values.

Quoted 13 lines
testcases/kernel/mem/lib/mem.c +13 -0
+		if ((start >= low) && (start < high)) {
+			fclose(f);
+			return 1;
+		}
+		if ((end >= low) && (end < high)) {
+			fclose(f);
+			return 1;
+		}
+	}
+
+	fclose(f);
+	return 0;
+}
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

PATCH v2 1/1 mem/hugetlb: shift an empty region to use in hugemmap02.c

Hi,

Quoted 20 lines · Cyril Hrubis

Hi!
Generall idea looks good, a few comments below.

Review

Quoted 4 lines
testcases/kernel/mem/include/mem.h +2 -0
+/* MMAP */
+int range_is_mapped(unsigned long low, unsigned long high)
 #endif
(quoted diff) +0 -0

b/testcases/kernel/mem/lib/mem.c

Quoted 12 lines

index 118b6c0..e0e1bdf 100644

testcases/kernel/mem/lib/mem.c +5 -0
@@ -1113,3 +1113,46 @@ void update_shm_size(size_t * shm_size)
              *shm_size = shmmax;
      }
 }
+
+int range_is_mapped(unsigned long low, unsigned long high)
+{
+     FILE *f;
+     int MAPS_BUF_SZ = 4096;
Quoted 9 lines

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1126

Quoted 6 lines
testcases/kernel/mem/lib/mem.c +6 -0
+     char line[MAPS_BUF_SZ];
+     char *tmp;
+
+     f = fopen("/proc/self/maps", "r");
+     if (!f) {
+             printf("Failed to open /proc/self/maps.\n");
                ^
                should rather be tst_resm(TWARN, );

ok. looks like using

    FILE * f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");

is better than above.

Quoted 9 lines
Quoted 9 lines
testcases/kernel/mem/lib/mem.c +9 -0
+             return -1;
+     }
+
+     while (1) {
+             unsigned long start, end;
+             int ret;
+
+             tmp = fgets(line, MAPS_BUF_SZ, f);
+             if (!tmp)
Quoted 9 lines

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1141

Quoted 6 lines
testcases/kernel/mem/lib/mem.c +6 -0
+                     break;
+
+             ret = sscanf(line, "%lx-%lx", &start, &end);
+             if (ret != 2) {
+                     printf("Couldn't parse /proc/self/maps line: %s\n",
+                                     line);

^
here as well

ok.

Quoted 7 lines

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1144

Quoted 3 lines
testcases/kernel/mem/lib/mem.c +3 -0
+                     fclose(f);
+                     return -1;
+             }

Why don't you just use fscanf()? It doesn't make sense to read the line into the buffer and then sscanf() the values.

hmm, we need to read the '/proc/self/maps' line by line, It easy to use fgets() to get each line in and then sscanf() that.

fscanf() will never read the newline at the end of the first line. So when it is called the second time, it will fail (returning 0, not EOF) and read nothing, leaving the [start, end] unchanged.
maybe I don't know if fscanf() could achieve that also?

so what about something like:
--------------------------------------
int range_is_mapped(unsigned long low, unsigned long high)

{
    FILE *f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");

    int MAPS_BUF_SZ = 4096;
    char line[MAPS_BUF_SZ];

    while (fgets(line, MAPS_BUF_SZ, f)) {
        unsigned long start, end;
        int ret;

        ret = sscanf(line, "%lx-%lx", &start, &end);
        if (ret != 2) {
            tst_resm(TWARN | TERRNO, "Couldn't parse /proc/self/maps
line.");
            SAFE_FCLOSE(NULL, f);
        }

        if ((start >= low) && (start < high)) {
            SAFE_FCLOSE(NULL, f);
            return 1;
        }
        if ((end >= low) && (end < high)) {
            SAFE_FCLOSE(NULL, f);
            return 1;
        }
    }

    SAFE_FCLOSE(NULL, f);
    return 0;
}
Quoted 18 lines

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1158

Quoted 13 lines
testcases/kernel/mem/lib/mem.c +13 -0
+             if ((start >= low) && (start < high)) {
+                     fclose(f);
+                     return 1;
+             }
+             if ((end >= low) && (end < high)) {
+                     fclose(f);
+                     return 1;
+             }
+     }
+
+     fclose(f);
+     return 0;
+}

--
Cyril Hrubis
[email protected]

Signature · 6 lines
Regards,
Li Wang
Email: [email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20151013/95af04a5/attachment-0001.html>
author
#3 lore raw

PATCH v2 1/1 mem/hugetlb: shift an empty region to use in hugemmap02.c

Hi,

Quoted 54 lines · Li Wang

Hi,

Quoted 20 lines · Cyril Hrubis

Hi!
Generall idea looks good, a few comments below.

Review

Quoted 4 lines
testcases/kernel/mem/include/mem.h +2 -0
+/* MMAP */
+int range_is_mapped(unsigned long low, unsigned long high)
 #endif
(quoted diff) +0 -0

b/testcases/kernel/mem/lib/mem.c

Quoted 12 lines

index 118b6c0..e0e1bdf 100644

testcases/kernel/mem/lib/mem.c +5 -0
@@ -1113,3 +1113,46 @@ void update_shm_size(size_t * shm_size)
              *shm_size = shmmax;
      }
 }
+
+int range_is_mapped(unsigned long low, unsigned long high)
+{
+     FILE *f;
+     int MAPS_BUF_SZ = 4096;
Quoted 9 lines

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1126

Quoted 6 lines
testcases/kernel/mem/lib/mem.c +6 -0
+     char line[MAPS_BUF_SZ];
+     char *tmp;
+
+     f = fopen("/proc/self/maps", "r");
+     if (!f) {
+             printf("Failed to open /proc/self/maps.\n");
                ^
                should rather be tst_resm(TWARN, );

ok. looks like using

    FILE * f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");

is better than above.

Quoted 15 lines
Quoted 4 lines
testcases/kernel/mem/lib/mem.c +4 -0
+             return -1;
+     }
+
+     while (1) {

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size)

Quoted 10 lines
testcases/kernel/mem/lib/mem.c +10 -0
+             unsigned long start, end;
+             int ret;
+
+             tmp = fgets(line, MAPS_BUF_SZ, f);
+             if (!tmp)
+                     break;
+
+             ret = sscanf(line, "%lx-%lx", &start, &end);
+             if (ret != 2) {
+                     printf("Couldn't parse /proc/self/maps line:

%s\n",

Quoted 16 lines
Quoted 4 lines

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1141

Quoted 1 line
testcases/kernel/mem/lib/mem.c +1 -0
+                                     line);

^
here as well

ok.

Quoted 7 lines

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1144

Quoted 3 lines
testcases/kernel/mem/lib/mem.c +3 -0
+                     fclose(f);
+                     return -1;
+             }

Why don't you just use fscanf()? It doesn't make sense to read the line into the buffer and then sscanf() the values.

okay, seems you are right. I tried and achieved another one, following code test pass on my s390x system.
-----------------------------------------
int range_is_mapped(unsigned long low, unsigned long high)

{
    FILE *f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");

    char buf[512];

    while (!feof(f)) {
        unsigned long start, end;
        int ret;

        ret = fscanf(f, "%lx-%lx %[^\n]%*c]", &start, &end, buf);
        if (ret != 3) {
            tst_resm(TWARN | TERRNO, "Couldn't parse /proc/self/maps
line.");
            SAFE_FCLOSE(NULL, f);
        }

        if ((start >= low) && (start < high)) {
            SAFE_FCLOSE(NULL, f);
            return 1;
        }
        if ((end >= low) && (end < high)) {
            SAFE_FCLOSE(NULL, f);
            return 1;
        }
    }

    SAFE_FCLOSE(NULL, f);
    return 0;
}

so, which one is better? I will format patch V3 when getting some proposals.

cc' jstancek@ if has some good idea.

Quoted 70 lines

hmm, we need to read the '/proc/self/maps' line by line, It easy to use fgets() to get each line in and then sscanf() that.

fscanf() will never read the newline at the end of the first line. So when it is called the second time, it will fail (returning 0, not EOF) and read nothing, leaving the [start, end] unchanged.
maybe I don't know if fscanf() could achieve that also?

so what about something like:
--------------------------------------
int range_is_mapped(unsigned long low, unsigned long high)

{
    FILE *f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");

    int MAPS_BUF_SZ = 4096;
    char line[MAPS_BUF_SZ];

    while (fgets(line, MAPS_BUF_SZ, f)) {
        unsigned long start, end;
        int ret;

        ret = sscanf(line, "%lx-%lx", &start, &end);
        if (ret != 2) {
            tst_resm(TWARN | TERRNO, "Couldn't parse /proc/self/maps
line.");
            SAFE_FCLOSE(NULL, f);
        }

        if ((start >= low) && (start < high)) {
            SAFE_FCLOSE(NULL, f);
            return 1;
        }
        if ((end >= low) && (end < high)) {
            SAFE_FCLOSE(NULL, f);
            return 1;
        }
    }

    SAFE_FCLOSE(NULL, f);
    return 0;
}
Quoted 9 lines
Quoted 9 lines
testcases/kernel/mem/lib/mem.c +9 -0
+             if ((start >= low) && (start < high)) {
+                     fclose(f);
+                     return 1;
+             }
+             if ((end >= low) && (end < high)) {
+                     fclose(f);
+                     return 1;
+             }
+     }
Quoted 9 lines

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1158

Quoted 4 lines
testcases/kernel/mem/lib/mem.c +4 -0
+
+     fclose(f);
+     return 0;
+}

--
Cyril Hrubis
[email protected]

--
Regards,
Li Wang
Email: [email protected]

Signature · 6 lines
Regards,
Li Wang
Email: [email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20151013/6ec80a51/attachment.html>
#4 lore raw

PATCH v2 1/1 mem/hugetlb: shift an empty region to use in hugemmap02.c

Hi!

Quoted 6 lines

okay, seems you are right. I tried and achieved another one, following code test pass on my s390x system.
-----------------------------------------
int range_is_mapped(unsigned long low, unsigned long high)

{
    FILE *f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");

If we start to use SAFE_MACROS() here we have to pass the cleanup callback to the range_is_mapped() as a first parameter so that we can pass correct cleanup callback to the SAFE_MACROS().

Quoted 7 lines
    char buf[512];

    while (!feof(f)) {
        unsigned long start, end;
        int ret;

        ret = fscanf(f, "%lx-%lx %[^\n]%*c]", &start, &end, buf);

You can do even better with:

                ret = fscanf(f, "%lx-%lx %*[^\n]\n", &start, &end);

Which says read two long ints and then ignore everything until the end of line.

You could have ommited the last '\n' in the format string, but then you would need to exit the loop when the fscanf() returns -1 since the feof() would not exit the loop since there would still be the last newline in the buffer after we read the last line...

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#5 lore raw

PATCH v2 1/1 mem/hugetlb: shift an empty region to use in hugemmap02.c

Hi,

Quoted 8 lines · Cyril Hrubis

Hi!

Quoted 6 lines

okay, seems you are right. I tried and achieved another one, following code test pass on my s390x system.
-----------------------------------------
int range_is_mapped(unsigned long low, unsigned long high)

{
    FILE *f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");
Quoted 4 lines

If we start to use SAFE_MACROS() here we have to pass the cleanup callback to the range_is_mapped() as a first parameter so that we can pass correct cleanup callback to the SAFE_MACROS().

ok, now I prefer to use fopen()/fclose() to keep simple.

Quoted 7 lines
Quoted 7 lines
    char buf[512];

    while (!feof(f)) {
        unsigned long start, end;
        int ret;
Quoted 6 lines
Quoted 1 line
        ret = fscanf(f, "%lx-%lx %[^\n]%*c]", &start, &end, buf);

You can do even better with:

                ret = fscanf(f, "%lx-%lx %*[^\n]\n", &start, &end);

great!

Quoted 9 lines

Which says read two long ints and then ignore everything until the end of line.

You could have ommited the last '\n' in the format string, but then you would need to exit the loop when the fscanf() returns -1 since the feof() would not exit the loop since there would still be the last newline in the buffer after we read the last line...

ok, agreed. I will sent V3.

thanks for reviewing the patch.

Signature · 6 lines
Regards,
Li Wang
Email: [email protected]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20151013/d3351495/attachment.html>
#6 lore raw

PATCH v2 1/1 mem/hugetlb: shift an empty region to use in hugemmap02.c

In reply to Li Wang

----- Original Message -----

Quoted 114 lines

From: "Li Wang" <[email protected]>
To: "Cyril Hrubis" <[email protected]>, "Jan Stancek" <[email protected]>
Cc: [email protected]
Sent: Tuesday, 13 October, 2015 12:15:23 PM
Subject: Re: [LTP] [PATCH v2] mem/hugetlb: shift an empty region to use in hugemmap02.c

Hi,

Quoted 54 lines · Li Wang

Hi,

Quoted 20 lines · Cyril Hrubis

Hi!
Generall idea looks good, a few comments below.

Review

Quoted 4 lines
testcases/kernel/mem/include/mem.h +2 -0
+/* MMAP */
+int range_is_mapped(unsigned long low, unsigned long high)
 #endif
(quoted diff) +0 -0

b/testcases/kernel/mem/lib/mem.c

Quoted 12 lines

index 118b6c0..e0e1bdf 100644

testcases/kernel/mem/lib/mem.c +5 -0
@@ -1113,3 +1113,46 @@ void update_shm_size(size_t * shm_size)
              *shm_size = shmmax;
      }
 }
+
+int range_is_mapped(unsigned long low, unsigned long high)
+{
+     FILE *f;
+     int MAPS_BUF_SZ = 4096;
Quoted 9 lines

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1126

Quoted 6 lines
testcases/kernel/mem/lib/mem.c +6 -0
+     char line[MAPS_BUF_SZ];
+     char *tmp;
+
+     f = fopen("/proc/self/maps", "r");
+     if (!f) {
+             printf("Failed to open /proc/self/maps.\n");
                ^
                should rather be tst_resm(TWARN, );

ok. looks like using

    FILE * f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");

is better than above.

Quoted 15 lines
Quoted 4 lines
testcases/kernel/mem/lib/mem.c +4 -0
+             return -1;
+     }
+
+     while (1) {

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size)

Quoted 10 lines
testcases/kernel/mem/lib/mem.c +10 -0
+             unsigned long start, end;
+             int ret;
+
+             tmp = fgets(line, MAPS_BUF_SZ, f);
+             if (!tmp)
+                     break;
+
+             ret = sscanf(line, "%lx-%lx", &start, &end);
+             if (ret != 2) {
+                     printf("Couldn't parse /proc/self/maps line:

%s\n",

Quoted 16 lines
Quoted 4 lines

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1141

Quoted 1 line
testcases/kernel/mem/lib/mem.c +1 -0
+                                     line);

^
here as well

ok.

Quoted 7 lines

testcases/kernel/mem/lib/mem.c void update_shm_size(size_t * shm_size) · line 1144

Quoted 3 lines
testcases/kernel/mem/lib/mem.c +3 -0
+                     fclose(f);
+                     return -1;
+             }

Why don't you just use fscanf()? It doesn't make sense to read the line into the buffer and then sscanf() the values.

okay, seems you are right. I tried and achieved another one, following code test pass on my s390x system.
-----------------------------------------
int range_is_mapped(unsigned long low, unsigned long high)

{
    FILE *f = SAFE_FOPEN(NULL, "/proc/self/maps", "r");

    char buf[512];

    while (!feof(f)) {
        unsigned long start, end;
        int ret;

        ret = fscanf(f, "%lx-%lx %[^\n]%*c]", &start, &end, buf);
        if (ret != 3) {
            tst_resm(TWARN | TERRNO, "Couldn't parse /proc/self/maps
line.");
            SAFE_FCLOSE(NULL, f);
        }

        if ((start >= low) && (start < high)) {
            SAFE_FCLOSE(NULL, f);
            return 1;
        }
        if ((end >= low) && (end < high)) {
            SAFE_FCLOSE(NULL, f);
            return 1;
        }
    }

    SAFE_FCLOSE(NULL, f);
    return 0;
}
Quoted 5 lines

so, which one is better? I will format patch V3 when getting some proposals.

cc' jstancek@ if has some good idea.

The approach above should work, but it also stores data to buf, that we don't care about. You could do fscanf + "while (fgetc(f) != '\n');" to forward to next line, if you want to avoid using buf.

Anyway, I'd go with fgets() + sscanf(), it wastes couple bytes on stack, but it's the simplest.

Regards,
Jan

#7 lore raw

PATCH v2 1/1 mem/hugetlb: shift an empty region to use in hugemmap02.c

----- Original Message -----

Quoted 10 lines

From: "Jan Stancek" <[email protected]>
To: "Li Wang" <[email protected]>, "Cyril Hrubis" <[email protected]>
Cc: [email protected]
Sent: Tuesday, 13 October, 2015 12:54:46 PM
Subject: Re: [LTP] [PATCH v2] mem/hugetlb: shift an empty region to use in hugemmap02.c

The approach above should work, but it also stores data to buf, that we don't care about. You could do fscanf + "while (fgetc(f) != '\n');" to forward to next line, if you want to avoid using buf.

OK, I take that back, Cyril's version is prettier:

Quoted 2 lines
                ret = fscanf(f, "%lx-%lx %*[^\n]\n", &start, &end);