Skip to contentSkip to conversation

syscalls/mallinfo01: Add a basic test for mallinfo

PATCH Superseded Yang Xu 4 participants 39 messages Jan 26, 2021 – Feb 23, 2021 Library code

lore
Share & tools
lore

Yang Xu

author
#0 lore raw

PATCH 1/3 syscalls/mallinfo01: Add a basic test for mallinfo

Referring to glibc test tst-mallinfo2.c[1], add a test to test mallinfo. Also check mallinfo in autotools.

[1]https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c

---
 configure.ac                                  |  1 +
 runtest/syscalls                              |  2 +
 testcases/kernel/syscalls/mallinfo/.gitignore |  1 +
 testcases/kernel/syscalls/mallinfo/Makefile   |  8 ++
 .../kernel/syscalls/mallinfo/mallinfo01.c     | 84 +++++++++++++++++++
 5 files changed, 96 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mallinfo/.gitignore
 create mode 100644 testcases/kernel/syscalls/mallinfo/Makefile
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo01.c
configure.ac +1 -0
index 8bdb96300..223900ca1 100644
@@ -94,6 +94,7 @@ AC_CHECK_FUNCS_ONCE([ \
     io_uring_register \
     io_uring_enter \
     kcmp \
+    mallinfo \
     mallopt \
     mkdirat \
     mknodat \
runtest/syscalls +2 -0
index 576eacf83..d0a9a5145 100644
@@ -682,6 +682,8 @@ lstat01_64 lstat01_64
 lstat02 lstat02
 lstat02_64 lstat02_64
 
+mallinfo01 mallinfo01
+
 mallopt01 mallopt01
 
 mbind01 mbind01
testcases/kernel/syscalls/mallinfo/.gitignore new file +1 -0
new file mode 100644
index 000000000..a7e32a637
@@ -0,0 +1 @@
+/mallinfo01
testcases/kernel/syscalls/mallinfo/Makefile new file +8 -0
new file mode 100644
index 000000000..044619fb8
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) International Business Machines  Corp., 2001
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +84 -0

84 lines not on this page · raw message

Signature · 1 line
2.23.0
author
#1 lore raw

PATCH 2/3 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

According mallinfo man-page, hblkhd member represents
"The number of bytes in blocks currently allocated using mmap(2).". For allocations greater than or equal to 128K and that can't be satisfied from the free list, the memory-allocation functions employ mmap(2) instead of increasing the program break using sbrk(2).

In this case, we test 20k size to use sbrk and 128k size to use mmap.

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/mallinfo/.gitignore |  1 +
 .../kernel/syscalls/mallinfo/mallinfo02.c     | 76 +++++++++++++++++++
 3 files changed, 78 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo02.c
runtest/syscalls +1 -0
index d0a9a5145..74ad999e8 100644
@@ -683,6 +683,7 @@ lstat02 lstat02
 lstat02_64 lstat02_64
 
 mallinfo01 mallinfo01
+mallinfo02 mallinfo02
 
 mallopt01 mallopt01
 
testcases/kernel/syscalls/mallinfo/.gitignore +1 -0
index a7e32a637..678ac277e 100644
@@ -1 +1,2 @@
 /mallinfo01
+/mallinfo02
testcases/kernel/syscalls/mallinfo/mallinfo02.c new file +76 -0

76 lines not on this page · raw message

Signature · 1 line
2.23.0
#2 lore raw

PATCH 2/3 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

Hi!

Quoted 6 lines

"The number of bytes in blocks currently allocated using mmap(2).". For allocations greater than or equal to 128K and that can't be satisfied from the free list, the memory-allocation functions employ mmap(2) instead of increasing the program break using sbrk(2).

In this case, we test 20k size to use sbrk and 128k size to use mmap.

The size when glibc uses mmap() instead of heap is libc implementation detail. I'm not sure that we want to have that value hardcoded in a LTP test.

Also glibc documentation says:

The default value is set to `131072' bytes and the threshold is adjusted dynamically to suit the allocation patterns of the program.

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

PATCH 2/3 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

Hi Cyril

Quoted 10 lines

Hi!

Quoted 6 lines

"The number of bytes in blocks currently allocated using mmap(2).". For allocations greater than or equal to 128K and that can't be satisfied from the free list, the memory-allocation functions employ mmap(2) instead of increasing the program break using sbrk(2).

In this case, we test 20k size to use sbrk and 128k size to use mmap.

The size when glibc uses mmap() instead of heap is libc implementation detail. I'm not sure that we want to have that value hardcoded in a LTP test.

Here has some wrong description, I use "MAX(info.fordblks, 131072) + reuse_size" size to test instead of 128K.

Quoted 5 lines

Also glibc documentation says:

The default value is set to `131072' bytes and the threshold is adjusted dynamically to suit the allocation patterns of the program.

IMO, the threshold is adjusted dynamically because of two things if we don't use mallopt with M_MMAP_THRESHOLD
1) fordblks; /* Total free space (bytes) */
2) the previous mmap regin space

From mallopt man-page for M_MMAP_THRESHOLD option, it said
" For allocations greater than or equal to the limit specified (in bytes) by M_MMAP_THRESHOLD that can't be satisfied from the free list, the memory-allocation functions employ mmap(2) instead of increasing the program break using sbrk(2)."

So I use this code "MAX(info.fordblks, 131072)" to get the right value to trigger mmap.

mallopt man-page for M_MMAP_THRESHOLD option also said "On the
other hand, there are some disadvantages to the use of mmap(2):
deallocated space is not placed on the free list for reuse by later allocations; " .

I guess it means mmap area is not counted int info.fordblks(free list ) and can be used for the next sbrk(increase the heap). That is why I add reuse_size when I get the corrcet mmap size. Or, I miss something?

If we can't ensure it , I will remove this patch. Or, other advise?

Best Regards
Yang Xu

Quoted 1 line
author
#4 lore raw

PATCH 2/3 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

Hi Cyril

Quoted 42 lines

Hi Cyril

Quoted 8 lines

Hi!

Quoted 7 lines

"The number of bytes in blocks currently allocated using mmap(2).". For allocations greater than or equal to 128K and that can't be satisfied from
the free list, the memory-allocation functions employ mmap(2) instead of increasing
the program break using sbrk(2).

Quoted 5 lines
Quoted 1 line

In this case, we test 20k size to use sbrk and 128k size to use mmap.

The size when glibc uses mmap() instead of heap is libc implementation detail. I'm not sure that we want to have that value hardcoded in a LTP test.

Here has some wrong description, I use "MAX(info.fordblks, 131072) + reuse_size" size to test instead of 128K.

Quoted 5 lines

Also glibc documentation says:

The default value is set to `131072' bytes and the threshold is adjusted dynamically to suit the allocation patterns of the program.

IMO, the threshold is adjusted dynamically because of two things if we don't use mallopt with M_MMAP_THRESHOLD
1) fordblks; /* Total free space (bytes) */
2) the previous mmap regin space

From mallopt man-page for M_MMAP_THRESHOLD option, it said
" For allocations greater than or equal to the limit specified (in bytes) by M_MMAP_THRESHOLD that can't be satisfied from the free list, the memory-allocation functions employ mmap(2) instead of increasing the program break using sbrk(2)."

So I use this code "MAX(info.fordblks, 131072)" to get the right value to trigger mmap.

mallopt man-page for M_MMAP_THRESHOLD option also said "On the other hand, there are some disadvantages to the use of mmap(2):
deallocated space is not placed on the free list for reuse by later allocations; " .

Quoted 5 lines

I guess it means mmap area is not counted int info.fordblks(free list ) and can be used for the next sbrk(increase the heap). That is why I add reuse_size when I get the corrcet mmap size. Or, I miss something?

If we can't ensure it , I will remove this patch. Or, other advise?

For glibc malloc mmap dynamic threshhold , here[1] has a detailed description.

[1]https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/malloc.c;h=1f4bbd8edf8b97701b779f183475565c7d0a6762;hb=d5c8f98c5e6de207790d3e9edadf5bda4aa2521f#l1043

Quoted 10 lines

Best Regards
Yang Xu

Quoted 1 line
#5 lore raw

PATCH 2/3 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

In reply to Yang Xu

Hi Xu,

Yang Xu <[email protected]> wrote:

...

Quoted 4 lines

So I use this code "MAX(info.fordblks, 131072)" to get the right value to trigger mmap.

My 2 cents:

From what I understand, once we request memory larger than the upper limit DEFAULT_MMAP_THRESHOLD_MAX, Glibc will always make use of mmap() because it will be out of any value it can adjust in dynamical. So that we don't be bothered with this issue anymore.

Or:

Invoking mallopt(M_MMAP_THRESHOLD, ...) function in front of the SAFE_MALLOC helps to fix the threshold. In such a case, the dynamic adjustment of the mmap threshold will be disabled.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/74a8a9ec/attachment.htm>
author
#6 lore raw

PATCH 2/3 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

Hi Li

Quoted 11 lines

Hi Xu,

Yang Xu <[email protected]
<mailto:[email protected]>> wrote:

...

So I use this code "MAX(info.fordblks, 131072)" to get the right value to trigger mmap.

Quoted 6 lines

My 2 cents:

From what I understand, once we request memory larger than the upper
limit DEFAULT_MMAP_THRESHOLD_MAX, Glibc will always make use of mmap() because it will be out of any value it can adjust in dynamical. So that we don't be bothered with this issue anymore.

But in subsequent malloc, the previous mmap area will add into arena and we still will increase the threashold. I use 8M size to test.

Quoted 6 lines

Or:

Invoking mallopt(M_MMAP_THRESHOLD, ...) function in front of the SAFE_MALLOC helps to fix the threshold. In such a case, the dynamic adjustment of the mmap threshold will be disabled.

Yes, it works well. Thanks for your review.

Quoted 4 lines

--
Regards,
Li Wang

author
#7 lore raw

PATCH 3/3 syscalls/mallinfo03: Add an overflow test when setting 2G size

In reply to Yang Xu

Since these members of mallinfo struct use int data type, it will overflow when allocating 2G size. mallinfo() is deprecated and we should use mallinfo2() in the future.

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/mallinfo/.gitignore |  1 +
 .../kernel/syscalls/mallinfo/mallinfo03.c     | 62 +++++++++++++++++++
 3 files changed, 64 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo03.c
runtest/syscalls +1 -0
index 74ad999e8..8fe123f8c 100644
@@ -684,6 +684,7 @@ lstat02_64 lstat02_64
 
 mallinfo01 mallinfo01
 mallinfo02 mallinfo02
+mallinfo03 mallinfo03
 
 mallopt01 mallopt01
 
testcases/kernel/syscalls/mallinfo/.gitignore +1 -0
index 678ac277e..30c315cf2 100644
@@ -1,2 +1,3 @@
 /mallinfo01
 /mallinfo02
+/mallinfo03
testcases/kernel/syscalls/mallinfo/mallinfo03.c new file +62 -0

62 lines not on this page · raw message

Signature · 1 line
2.23.0
#8 lore raw

PATCH 3/3 syscalls/mallinfo03: Add an overflow test when setting 2G size

Hi!

testcases/kernel/syscalls/mallinfo/mallinfo03.c line 44

Quoted 7 lines
testcases/kernel/syscalls/mallinfo/mallinfo03.c new file +7 -0
+void test_mallinfo(void)
+{
+	struct mallinfo info;
+	char *buf;
+	size_t size = 2UL * 1024UL * 1024UL * 1024UL;
+
+	buf = SAFE_MALLOC(size);

If nothing else use of SAFE_MALLOC() here is wrong. The call may potentionally fail and return NULL since we are passing overly large value there.

For example it will fail if memory overcommit is disabled and there is not enough free memory.

So we should, at least, use malloc() instead and skip the test if NULL was returned.

Quoted 26 lines
testcases/kernel/syscalls/mallinfo/mallinfo03.c new file +18 -0
+	info = mallinfo();
+	if (info.hblkhd < 0) {
+		print_mallinfo("Test malloc 2G", &info);
+		tst_res(TFAIL, "The member of struct mallinfo overflow, we should use mallinfo2");
+	} else {
+		/*We will never get here*/
+		tst_res(TPASS, "The member of struct mallinfo doesn't overflow");
+	}
+	free(buf);
+}
+
+static struct tst_test test = {
+	.test_all = test_mallinfo,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
Signature · 7 lines
2.23.0




-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#9 lore raw

PATCH 3/3 syscalls/mallinfo03: Add an overflow test when setting 2G size

Hi Cyril

Quoted 13 lines

Hi!

testcases/kernel/syscalls/mallinfo/mallinfo03.c line 44

Quoted 7 lines
testcases/kernel/syscalls/mallinfo/mallinfo03.c new file +7 -0
+void test_mallinfo(void)
+{
+	struct mallinfo info;
+	char *buf;
+	size_t size = 2UL * 1024UL * 1024UL * 1024UL;
+
+	buf = SAFE_MALLOC(size);

If nothing else use of SAFE_MALLOC() here is wrong. The call may potentionally fail and return NULL since we are passing overly large value there.

Quoted 6 lines

For example it will fail if memory overcommit is disabled and there is not enough free memory.

So we should, at least, use malloc() instead and skip the test if NULL was returned.

Agree. Will do it in v2.

Quoted 27 lines
Quoted 26 lines
testcases/kernel/syscalls/mallinfo/mallinfo03.c new file +18 -1
+	info = mallinfo();
+	if (info.hblkhd<  0) {
+		print_mallinfo("Test malloc 2G",&info);
+		tst_res(TFAIL, "The member of struct mallinfo overflow, we should use mallinfo2");
+	} else {
+		/*We will never get here*/
+		tst_res(TPASS, "The member of struct mallinfo doesn't overflow");
+	}
+	free(buf);
+}
+
+static struct tst_test test = {
+	.test_all = test_mallinfo,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
--

2.23.0

--
Mailing list info: https://lists.linux.it/listinfo/ltp

#10 lore raw

PATCH 3/3 syscalls/mallinfo03: Add an overflow test when setting 2G size

In reply to Yang Xu

Hi Xu,

Quoted 11 lines
testcases/kernel/syscalls/mallinfo/mallinfo03.c new file +11 -0
+void test_mallinfo(void)
+{
+       struct mallinfo info;
+       char *buf;
+       size_t size = 2UL * 1024UL * 1024UL * 1024UL;
+
+       buf = SAFE_MALLOC(size);
+       info = mallinfo();
+       if (info.hblkhd < 0) {
+               print_mallinfo("Test malloc 2G", &info);
+               tst_res(TFAIL, "The member of struct mallinfo overflow, we
Quoted 2 lines

should use mallinfo2");

TPASS?

Quoted 5 lines
testcases/kernel/syscalls/mallinfo/mallinfo03.c new file +3 -0
+       } else {
+               /*We will never get here*/
+               tst_res(TPASS, "The member of struct mallinfo doesn't

overflow");

TFAIL?

Quoted 4 lines
testcases/kernel/syscalls/mallinfo/mallinfo03.c new file +3 -0
+       }
+       free(buf);
+}
Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/101b8c8d/attachment.htm>
author
#11 lore raw

PATCH 3/3 syscalls/mallinfo03: Add an overflow test when setting 2G size

Hi Li

Quoted 22 lines

Hi Xu,

+void test_mallinfo(void)
+{
+ struct mallinfo info;
+ char *buf;
+ size_t size = 2UL * 1024UL * 1024UL * 1024UL;
+
+ buf = SAFE_MALLOC(size);
+ info = mallinfo();
+ if (info.hblkhd < 0) {
+ print_mallinfo("Test malloc 2G", &info);
+ tst_res(TFAIL, "The member of struct mallinfo overflow, we should use mallinfo2");

TPASS?

+ } else {
+ /*We will never get here*/
+ tst_res(TPASS, "The member of struct mallinfo doesn't overflow");

Quoted 5 lines

TFAIL?

+ }
+ free(buf);
+}

It is a "always" fail test. We should use mallinfo2 in the future. I guess failure may attract user's attention than pass.

Quoted 6 lines

--
Regards,
Li Wang

#12 lore raw

PATCH 3/3 syscalls/mallinfo03: Add an overflow test when setting 2G size

Quoted 10 lines
Quoted 10 lines

+ tst_res(TFAIL, "The member of struct mallinfo overflow, we should use mallinfo2");

TPASS?

+ } else {
+ /*We will never get here*/
+ tst_res(TPASS, "The member of struct mallinfo doesn't overflow");

Quoted 9 lines
Quoted 5 lines

TFAIL?

+ }
+ free(buf);
+}

It is a "always" fail test. We should use mallinfo2 in the future. I guess failure may attract user's attention than pass.

Sorry, I don't understand the intention here. I got failures like below, allow it to report FAIL each time in CI/CD system?

# ./mallinfo03
tst_test.c:1263: TINFO: Timeout per run is 0h 05m 00s
tst_mallinfo.c:15: TINFO: Test malloc 2G...
tst_mallinfo.c:17: TINFO: arena: 135168
tst_mallinfo.c:18: TINFO: ordblks: 1
tst_mallinfo.c:19: TINFO: smblks: 0
tst_mallinfo.c:20: TINFO: hblks: 1
tst_mallinfo.c:21: TINFO: hblkhd: -2147479552
tst_mallinfo.c:22: TINFO: usmblks: 0
tst_mallinfo.c:23: TINFO: fsmblks: 0
tst_mallinfo.c:24: TINFO: uordblks: 848
tst_mallinfo.c:25: TINFO: fordblks: 134320
tst_mallinfo.c:26: TINFO: keepcost: 134320
mallinfo03.c:36: TFAIL: The member of struct mallinfo overflow, we should
use mallinfo2

Summary:

passed   0
failed   1
broken   0
skipped  0

warnings 0

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/1680b7bd/attachment.htm>
author
#13 lore raw

PATCH v2 1/5 tst_mallinfo.c: Add a common print helper for mallinfo

In reply to Li Wang

This function can be used for mallopt and subsequent mallinfo/mallinfo2 test.

---
 configure.ac           |  1 +
 include/tst_mallinfo.h | 17 +++++++++++++++++
 lib/tst_mallinfo.c     | 28 ++++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)
 create mode 100644 include/tst_mallinfo.h
 create mode 100644 lib/tst_mallinfo.c
configure.ac +1 -0
index 8bdb96300..223900ca1 100644
@@ -94,6 +94,7 @@ AC_CHECK_FUNCS_ONCE([ \
     io_uring_register \
     io_uring_enter \
     kcmp \
+    mallinfo \
     mallopt \
     mkdirat \
     mknodat \
include/tst_mallinfo.h new file +17 -0
new file mode 100644
index 000000000..bd69d032c
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <[email protected]>
+ */
+
+#ifndef TST_MALLINFO_H__
+#define TST_MALLINFO_H__
+
+#include <malloc.h>
+#include "config.h"
+
+#ifdef HAVE_MALLINFO
+void tst_print_mallinfo(const char *msg, struct mallinfo *m);
+#endif
+
+#endif
lib/tst_mallinfo.c new file +28 -0
new file mode 100644
index 000000000..229e5aa2e
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <[email protected]>
+ */
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+#include "tst_mallinfo.h"
+
+// To DO for mallinfo2
+#ifdef HAVE_MALLINFO
+void tst_print_mallinfo(const char *msg, struct mallinfo *m)
+{
+	tst_res(TINFO, "%s...", msg);
+#define P(f) tst_res(TINFO, "%s: %d", #f, m->f)
+	P(arena);
+	P(ordblks);
+	P(smblks);
+	P(hblks);
+	P(hblkhd);
+	P(usmblks);
+	P(fsmblks);
+	P(uordblks);
+	P(fordblks);
+	P(keepcost);
+}
+#endif
Signature · 1 line
2.23.0
author
#14 lore raw

PATCH v2 2/5 syscalls/mallopt01: Use tst_print_mallinfo api

---
 testcases/kernel/syscalls/mallopt/mallopt01.c | 22 +++----------------
 1 file changed, 3 insertions(+), 19 deletions(-)
testcases/kernel/syscalls/mallopt/mallopt01.c +3 -19
index f799aaf9d..271079c12 100644
@@ -12,10 +12,9 @@
  * Basic mallinfo() and mallopt() testing.
 \*/
 
-#include <malloc.h>
-
 #include "tst_test.h"
 #include "tst_safe_macros.h"
+#include "tst_mallinfo.h"
 
 #ifdef HAVE_MALLOPT
 
@@ -23,21 +22,6 @@
 
 struct mallinfo info;
 
-void print_mallinfo(void)
-{
-	tst_res(TINFO, "mallinfo structure:");
-	tst_res(TINFO, "mallinfo.arena = %d", info.arena);
-	tst_res(TINFO, "mallinfo.ordblks = %d", info.ordblks);
-	tst_res(TINFO, "mallinfo.smblks = %d", info.smblks);
-	tst_res(TINFO, "mallinfo.hblkhd = %d", info.hblkhd);
-	tst_res(TINFO, "mallinfo.hblks = %d", info.hblks);
-	tst_res(TINFO, "mallinfo.usmblks = %d", info.usmblks);
-	tst_res(TINFO, "mallinfo.fsmblks = %d", info.fsmblks);
-	tst_res(TINFO, "mallinfo.uordblks = %d", info.uordblks);
-	tst_res(TINFO, "mallinfo.fordblks = %d", info.fordblks);
-	tst_res(TINFO, "mallinfo.keepcost = %d", info.keepcost);
-}
-
 void test_mallopt(void)
 {
 	char *buf;
@@ -46,11 +30,11 @@ void test_mallopt(void)
 
 	info = mallinfo();
 	if (info.uordblks < 20480) {
-		print_mallinfo();
+		tst_print_mallinfo("Test uordblks", &info);
 		tst_res(TFAIL, "mallinfo() failed: uordblks < 20K");
 	}
 	if (info.smblks != 0) {
-		print_mallinfo();
+		tst_print_mallinfo("Test smblks", &info);
 		tst_res(TFAIL, "mallinfo() failed: smblks != 0");
 	}
 	if (info.uordblks >= 20480 && info.smblks == 0)
Signature · 1 line
2.23.0
author
#15 lore raw

PATCH v2 3/5 syscalls/mallinfo01: Add a basic test for mallinfo

In reply to Yang Xu

Referring to glibc test tst-mallinfo2.c[1], add a test to test mallinfo. Also check mallinfo in autotools.

[1]https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c

---
v1->v2
1.use tst_print_mallinfo api
2.use buf array
3.use 160 multiple instead of 16
4.add free chunk check

 runtest/syscalls                              |  2 +
 testcases/kernel/syscalls/mallinfo/.gitignore |  1 +
 testcases/kernel/syscalls/mallinfo/Makefile   |  8 ++
 .../kernel/syscalls/mallinfo/mallinfo01.c     | 82 +++++++++++++++++++
 4 files changed, 93 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mallinfo/.gitignore
 create mode 100644 testcases/kernel/syscalls/mallinfo/Makefile
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo01.c
runtest/syscalls +2 -0
index 11a1e83c2..753340068 100644
@@ -682,6 +682,8 @@ lstat01_64 lstat01_64
 lstat02 lstat02
 lstat02_64 lstat02_64
 
+mallinfo01 mallinfo01
+
 mallopt01 mallopt01
 
 mbind01 mbind01
testcases/kernel/syscalls/mallinfo/.gitignore new file +1 -0
new file mode 100644
index 000000000..a7e32a637
@@ -0,0 +1 @@
+/mallinfo01
testcases/kernel/syscalls/mallinfo/Makefile new file +8 -0
new file mode 100644
index 000000000..044619fb8
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) International Business Machines  Corp., 2001
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +82 -0

82 lines not on this page · raw message

Signature · 1 line
2.23.0
author
#16 lore raw

PATCH v2 4/5 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

In reply to Yang Xu

According mallinfo man-page, hblkhd member represents
"The number of bytes in blocks currently allocated using mmap(2).". For allocations greater than or equal to 128K and that can't be satisfied from the free list, the memory-allocation functions employ mmap(2) instead of increasing the program break using sbrk(2).

In this case, we test 20k size to use sbrk and 128k size to use mmap.

---
v1->v2:
1.Use mallopt(M_MMAP_THRESHOLD, 131072) to avoid dynamic mmap threshold 2.Use tst_print_malinfo api

 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/mallinfo/.gitignore |  1 +
 .../kernel/syscalls/mallinfo/mallinfo02.c     | 64 +++++++++++++++++++
 3 files changed, 66 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo02.c
runtest/syscalls +1 -0
index 753340068..a8fa3f7bf 100644
@@ -683,6 +683,7 @@ lstat02 lstat02
 lstat02_64 lstat02_64
 
 mallinfo01 mallinfo01
+mallinfo02 mallinfo02
 
 mallopt01 mallopt01
 
testcases/kernel/syscalls/mallinfo/.gitignore +1 -0
index a7e32a637..678ac277e 100644
@@ -1 +1,2 @@
 /mallinfo01
+/mallinfo02
testcases/kernel/syscalls/mallinfo/mallinfo02.c new file +64 -0

64 lines not on this page · raw message

Signature · 1 line
2.23.0
#17 lore raw

PATCH v2 4/5 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

Quoted 58 lines · Yang Xu

According mallinfo man-page, hblkhd member represents
"The number of bytes in blocks currently allocated using mmap(2).". For allocations greater than or equal to 128K and that can't be satisfied from
the free list, the memory-allocation functions employ mmap(2) instead of increasing
the program break using sbrk(2).

In this case, we test 20k size to use sbrk and 128k size to use mmap.

---
v1->v2:
1.Use mallopt(M_MMAP_THRESHOLD, 131072) to avoid dynamic mmap threshold 2.Use tst_print_malinfo api

 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/mallinfo/.gitignore |  1 +
 .../kernel/syscalls/mallinfo/mallinfo02.c     | 64 +++++++++++++++++++
 3 files changed, 66 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo02.c
runtest/syscalls +1 -0
index 753340068..a8fa3f7bf 100644
@@ -683,6 +683,7 @@ lstat02 lstat02
 lstat02_64 lstat02_64
 
 mallinfo01 mallinfo01
+mallinfo02 mallinfo02
 
 mallopt01 mallopt01
 
testcases/kernel/syscalls/mallinfo/.gitignore +1 -0
index a7e32a637..678ac277e 100644
@@ -1 +1,2 @@
 /mallinfo01
+/mallinfo02
testcases/kernel/syscalls/mallinfo/mallinfo02.c new file +9 -0
new file mode 100644
index 000000000..d5bed45a8
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <[email protected]>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *

testcases/kernel/syscalls/mallinfo/mallinfo02.c line 19

Quoted 10 lines
testcases/kernel/syscalls/mallinfo/mallinfo02.c new file +10 -0
+ * Basic mallinfo() test for malloc() using sbrk or mmap.
+ * It size > MMAP_THRESHOLD, it will use mmap. Otherwise, use sbrk.
+\*/
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "tst_mallinfo.h"
+
+#ifdef HAVE_MALLINFO
+void test_mallopt(void)

what about renaming to test_mallinfo(void) ?

Quoted 16 lines
testcases/kernel/syscalls/mallinfo/mallinfo02.c new file +15 -0
+{
+       struct mallinfo info;
+       int size;
+       char *buf;
+
+       buf = SAFE_MALLOC(20480);
+
+       info = mallinfo();
+       if (info.uordblks > 20480 && info.hblkhd == 0) {
+               tst_res(TPASS, "malloc() uses sbrk when size < 128k");
+       } else {
+               tst_res(TFAIL, "malloc() use mmap when size < 128k");
+               tst_print_mallinfo("Test malloc(20480)", &info);
+       }
+       free(buf);

testcases/kernel/syscalls/mallinfo/mallinfo02.c line 44

Quoted 10 lines
testcases/kernel/syscalls/mallinfo/mallinfo02.c new file +10 -0
+
+       info = mallinfo();
+       size = MAX(info.fordblks, 131072);
+
+       buf = SAFE_MALLOC(size);
+       info = mallinfo();
+       if (info.hblkhd > size && info.hblks > 0) {
+               tst_res(TPASS, "malloc() uses mmap when size >= 128k");
+       } else {
+               tst_res(TFAIL, "malloc uses sbrk when size >= 128k");

Why not 'TFAIL | TERRNO' ?

testcases/kernel/syscalls/mallinfo/mallinfo02.c line 54

Quoted 11 lines
testcases/kernel/syscalls/mallinfo/mallinfo02.c new file +10 -0
+               tst_print_mallinfo("Test malloc(1024*128)", &info);
+       }
+
+       free(buf);
+}
+
+static void setup(void)
+{
+       if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)
+               tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");

Here as well.

Quoted 16 lines
testcases/kernel/syscalls/mallinfo/mallinfo02.c new file +10 -1
+}
+
+static struct tst_test test = {
+       .setup = setup,
+       .test_all = test_mallopt,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
--

2.23.0

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/4e0843f5/attachment-0001.htm>
author
#18 lore raw

PATCH v2 4/5 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

Hi Li

Quoted 76 lines

On Thu, Feb 4, 2021 at 8:12 PM Yang Xu <[email protected] <mailto:[email protected]>> wrote:

    According mallinfo man-page, hblkhd member represents
    "The number of bytes in blocks currently allocated using mmap(2).".
    For allocations greater than or equal to 128K and that can't be
    satisfied from
    the free list, the memory-allocation functions employ mmap(2)
    instead of increasing
    the program break using sbrk(2).

    In this case, we test 20k size to use sbrk and 128k size to use mmap.

    Signed-off-by: Yang Xu <[email protected]
    <mailto:[email protected]>>
    ---
    v1->v2:
    1.Use mallopt(M_MMAP_THRESHOLD, 131072) to avoid dynamic mmap threshold
    2.Use tst_print_malinfo api
    runtest/syscalls | 1 +
    testcases/kernel/syscalls/mallinfo/.gitignore | 1 +
    .../kernel/syscalls/mallinfo/mallinfo02.c | 64 +++++++++++++++++++
    3 files changed, 66 insertions(+)
    create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo02.c

    diff --git a/runtest/syscalls b/runtest/syscalls
    index 753340068..a8fa3f7bf 100644
    --- a/runtest/syscalls
    +++ b/runtest/syscalls
    @@ -683,6 +683,7 @@ lstat02 lstat02
    lstat02_64 lstat02_64

    mallinfo01 mallinfo01
    +mallinfo02 mallinfo02

    mallopt01 mallopt01

    diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore
    b/testcases/kernel/syscalls/mallinfo/.gitignore
    index a7e32a637..678ac277e 100644
    --- a/testcases/kernel/syscalls/mallinfo/.gitignore
    +++ b/testcases/kernel/syscalls/mallinfo/.gitignore
    @@ -1 +1,2 @@
    /mallinfo01
    +/mallinfo02
    diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo02.c
    b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
    new file mode 100644
    index 000000000..d5bed45a8
    --- /dev/null
    +++ b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
    @@ -0,0 +1,64 @@
    +// SPDX-License-Identifier: GPL-2.0-or-later
    +/*
    + * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
    + * Author: Yang Xu <[email protected]
    <mailto:[email protected]>>
    + */
    +
    +/*\
    + * [DESCRIPTION]
    + *
    + * Basic mallinfo() test for malloc() using sbrk or mmap.
    + * It size > MMAP_THRESHOLD, it will use mmap. Otherwise, use sbrk.
    +\*/
    +
    +#include "tst_test.h"
    +#include "tst_safe_macros.h"
    +#include "tst_mallinfo.h"
    +
    +#ifdef HAVE_MALLINFO
    +void test_mallopt(void)
Quoted 1 line

what about renaming to test_mallinfo(void) ?

Yes, sorry for typo.

Quoted 28 lines

+{
+ struct mallinfo info;
+ int size;
+ char *buf;
+
+ buf = SAFE_MALLOC(20480);
+
+ info = mallinfo();
+ if (info.uordblks > 20480 && info.hblkhd == 0) {
+ tst_res(TPASS, "malloc() uses sbrk when size < 128k");
+ } else {
+ tst_res(TFAIL, "malloc() use mmap when size < 128k");
+ tst_print_mallinfo("Test malloc(20480)", &info);
+ }
+ free(buf);
+
+ info = mallinfo();
+ size = MAX(info.fordblks, 131072);
+
+ buf = SAFE_MALLOC(size);
+ info = mallinfo();
+ if (info.hblkhd > size && info.hblks > 0) {
+ tst_res(TPASS, "malloc() uses mmap when size >= 128k");
+ } else {
+ tst_res(TFAIL, "malloc uses sbrk when size >= 128k");

Quoted 1 line

Why not 'TFAIL | TERRNO' ?

I don't see mallinfo() will fail and it should always succeed.

Quoted 13 lines

+ tst_print_mallinfo("Test malloc(1024*128)", &info);
+ }
+
+ free(buf);
+}
+
+static void setup(void)
+{
+ if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)
+ tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");

Quoted 1 line

Here as well.

Look mallopt man-pages, it said "
RETURN VALUE
On success, mallopt() returns 1. On error, it returns 0.
ERRORS
On error, errno is not set
"
That is why I don't use TERRNO.

Quoted 21 lines
    +}
    +
    +static struct tst_test test = {
    + .setup = setup,
    + .test_all = test_mallopt,
    +};
    +
    +#else
    +TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
    +#endif
    --
    2.23.0

--
Regards,
Li Wang

author
#19 lore raw

PATCH v2 4/5 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

In reply to Li Wang

Hi Li

I have reply this email, but I don't see it on inbox or patchwork, so again

Quoted 6 lines
    +
    +#ifdef HAVE_MALLINFO
    +void test_mallopt(void)

what about renaming to test_mallinfo(void) ?

Yes, sorry for this typo.

Quoted 28 lines

+{
+ struct mallinfo info;
+ int size;
+ char *buf;
+
+ buf = SAFE_MALLOC(20480);
+
+ info = mallinfo();
+ if (info.uordblks > 20480 && info.hblkhd == 0) {
+ tst_res(TPASS, "malloc() uses sbrk when size < 128k");
+ } else {
+ tst_res(TFAIL, "malloc() use mmap when size < 128k");
+ tst_print_mallinfo("Test malloc(20480)", &info);
+ }
+ free(buf);
+
+ info = mallinfo();
+ size = MAX(info.fordblks, 131072);
+
+ buf = SAFE_MALLOC(size);
+ info = mallinfo();
+ if (info.hblkhd > size && info.hblks > 0) {
+ tst_res(TPASS, "malloc() uses mmap when size >= 128k");
+ } else {
+ tst_res(TFAIL, "malloc uses sbrk when size >= 128k");

Quoted 1 line

Why not 'TFAIL | TERRNO' ?

mallinfo should always succeed.

Quoted 13 lines

+ tst_print_mallinfo("Test malloc(1024*128)", &info);
+ }
+
+ free(buf);
+}
+
+static void setup(void)
+{
+ if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)
+ tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");

Quoted 1 line

Here as well.

mallopt man-pages said even mallopt fails, return 0 and it doesn't set errno.

Quoted 21 lines
    +}
    +
    +static struct tst_test test = {
    + .setup = setup,
    + .test_all = test_mallopt,
    +};
    +
    +#else
    +TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
    +#endif
    --
    2.23.0

--
Regards,
Li Wang

author
#20 lore raw

PATCH v2 4/5 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

In reply to Li Wang

Hi Li

I have reply this email, but I don't see it on inbox or patchwork, so again

Quoted 5 lines

+#ifdef HAVE_MALLINFO
+void test_mallopt(void)

what about renaming to test_mallinfo(void) ?

Yes, sorry for this typo.

Quoted 28 lines

+{
+ struct mallinfo info;
+ int size;
+ char *buf;
+
+ buf = SAFE_MALLOC(20480);
+
+ info = mallinfo();
+ if (info.uordblks > 20480 && info.hblkhd == 0) {
+ tst_res(TPASS, "malloc() uses sbrk when size < 128k");
+ } else {
+ tst_res(TFAIL, "malloc() use mmap when size < 128k");
+ tst_print_mallinfo("Test malloc(20480)", &info);
+ }
+ free(buf);
+
+ info = mallinfo();
+ size = MAX(info.fordblks, 131072);
+
+ buf = SAFE_MALLOC(size);
+ info = mallinfo();
+ if (info.hblkhd > size && info.hblks > 0) {
+ tst_res(TPASS, "malloc() uses mmap when size >= 128k");
+ } else {
+ tst_res(TFAIL, "malloc uses sbrk when size >= 128k");

Quoted 1 line

Why not 'TFAIL | TERRNO' ?

mallinfo should succeed always.

Quoted 13 lines

+ tst_print_mallinfo("Test malloc(1024*128)", &info);
+ }
+
+ free(buf);
+}
+
+static void setup(void)
+{
+ if (mallopt(M_MMAP_THRESHOLD, 131072) == 0)
+ tst_res(TFAIL, "mallopt(M_MMAP_THRESHOLD, 128K) failed");

Quoted 2 lines

Here as well.

From mallopt man-page, even it fail, it doesn't set errno and only return 0

Quoted 20 lines
    +}
    +
    +static struct tst_test test = {
    + .setup = setup,
    + .test_all = test_mallopt,
    +};
    +
    +#else
    +TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
    +#endif
    --
    2.23.0

--
Regards,
Li Wang

#21 lore raw

PATCH v2 4/5 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

Hi Xu,

Yang Xu <[email protected]> wrote:

I have reply this email, but I don't see it on inbox or patchwork, so again

Quoted 1 line

Plz stop replying to this, I have received three same emails so far :).

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210205/e3f21ac7/attachment.htm>
author
#22 lore raw

PATCH v2 5/5 syscalls/mallinfo03: Add an overflow test when setting 2G size

In reply to Yang Xu

Since these members of mallinfo struct use int data type, it will overflow when allocating 2G size. mallinfo() is deprecated and we should use mallinfo2() in the future.

---
v1->v2
1.use tst_print_mallinfo api
2.skip test if we can't malloc 2g space

 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/mallinfo/.gitignore |  1 +
 .../kernel/syscalls/mallinfo/mallinfo03.c     | 51 +++++++++++++++++++
 3 files changed, 53 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo03.c
runtest/syscalls +1 -0
index a8fa3f7bf..c5cf78381 100644
@@ -684,6 +684,7 @@ lstat02_64 lstat02_64
 
 mallinfo01 mallinfo01
 mallinfo02 mallinfo02
+mallinfo03 mallinfo03
 
 mallopt01 mallopt01
 
testcases/kernel/syscalls/mallinfo/.gitignore +1 -0
index 678ac277e..30c315cf2 100644
@@ -1,2 +1,3 @@
 /mallinfo01
 /mallinfo02
+/mallinfo03
testcases/kernel/syscalls/mallinfo/mallinfo03.c new file +51 -0

51 lines not on this page · raw message

Signature · 1 line
2.23.0
#23 lore raw

PATCH v2 1/5 tst_mallinfo.c: Add a common print helper for mallinfo

In reply to Yang Xu

Hi Xu,

Thanks for your work.

--- /dev/null

Quoted 2 lines

+++ b/include/tst_mallinfo.h

Do we really need to export this function into the LTP library? (I assumed no other tests will using this lib function anymore)

So I prefer to create a common header file as:
../syscalls/mallinfo/mallinfo_common.h,
and just use it locally.

If you're hoping mallopt01.c also benefits from it, only need to build something like mallinfo_commo.o via Makefile and include it in mallopt01.c by "../mallinfo/mallinfo_common.h".

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/b556dd12/attachment.htm>
author
#24 lore raw

PATCH v2 1/5 tst_mallinfo.c: Add a common print helper for mallinfo

Hi Li

Quoted 10 lines

Hi Xu,

Thanks for your work.

    --- /dev/null
    +++ b/include/tst_mallinfo.h

Do we really need to export this function into the LTP library? (I assumed no other tests will using this lib function anymore)

Maybe we can add tst_print_mallinfo2 in the future and mallinfo2() case can use this header.

Quoted 8 lines

So I prefer to create a common header file as:
../syscalls/mallinfo/mallinfo_common.h,
and just use it locally.

If you're hoping mallopt01.c also benefits from it, only need to build something like mallinfo_commo.o via Makefile and include it in mallopt01.c by "../mallinfo/mallinfo_common.h".

Just want to avoid random makefile. I am not sure which way is better. Let's listen advise from other maintainers.

Quoted 4 lines

--
Regards,
Li Wang

#25 lore raw

PATCH v2 1/5 tst_mallinfo.c: Add a common print helper for mallinfo

Quoted 5 lines · Yang Xu

Hi Li

Quoted 4 lines

Hi Xu,

Thanks for your work.

Quoted 9 lines
Quoted 6 lines
    --- /dev/null
    +++ b/include/tst_mallinfo.h

Do we really need to export this function into the LTP library? (I assumed no other tests will using this lib function anymore)

Maybe we can add tst_print_mallinfo2 in the future and mallinfo2() case can use this header.

Or put it into the tst_memutils.h, I guess this header file is a general-purpose for providing kinds of memory helpers.

Quoted 6 lines
Quoted 6 lines

So I prefer to create a common header file as:
../syscalls/mallinfo/mallinfo_common.h,
and just use it locally.

Quoted 6 lines
Quoted 3 lines

If you're hoping mallopt01.c also benefits from it, only need to build something like mallinfo_commo.o via Makefile and include it in mallopt01.c by "../mallinfo/mallinfo_common.h".

Just want to avoid random makefile. I am not sure which way is better. Let's listen advise from other maintainers.

Sure, my pleasure to hear more voice~

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210205/c6b2fe84/attachment.htm>
#26 lore raw

PATCH v2 1/5 tst_mallinfo.c: Add a common print helper for mallinfo

Hi Xu, Li,

Quoted 11 lines
Quoted 7 lines
Quoted 5 lines
    --- /dev/null
    +++ b/include/tst_mallinfo.h

Do we really need to export this function into the LTP library? (I assumed no other tests will using this lib function anymore)

Maybe we can add tst_print_mallinfo2 in the future and mallinfo2() case can use this header.

Or put it into the tst_memutils.h, I guess this header file is a general-purpose for providing kinds of memory helpers.

Either ways are ok for me. tst_memutils.h is a bit cleaner approach, but given the fact that not many tests will use it having it in into testcases/kernel/syscalls/mallinfo/ and source it with relative path in mallopt01.c is also ok for me.

Kind regards,
Petr

Quoted 12 lines
Quoted 9 lines
Quoted 7 lines

So I prefer to create a common header file as:
../syscalls/mallinfo/mallinfo_common.h,
and just use it locally.

If you're hoping mallopt01.c also benefits from it, only need to build something like mallinfo_commo.o via Makefile and include it in mallopt01.c by "../mallinfo/mallinfo_common.h".

Just want to avoid random makefile. I am not sure which way is better. Let's listen advise from other maintainers.

Sure, my pleasure to hear more voice~

#27 lore raw

PATCH v2 1/5 tst_mallinfo.c: Add a common print helper for mallinfo

In reply to Li Wang

Hi!

Quoted 2 lines
Quoted 2 lines

Just want to avoid random makefile. I am not sure which way is better. Let's listen advise from other maintainers.

Given that its only one single function I would just put it into a header as a static inline function.

Also mallopt is glibc specific so I guess that it makes sense to keep it in a separate header.

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

PATCH v3 1/4 syscalls/mallinfo01: Add a basic test for mallinfo

Referring to glibc test tst-mallinfo2.c[1], add a test to test mallinfo. Also check mallinfo in autotools.

[1]https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c

---
 configure.ac                                  |  1 +
 runtest/syscalls                              |  2 +
 testcases/kernel/syscalls/mallinfo/.gitignore |  1 +
 testcases/kernel/syscalls/mallinfo/Makefile   |  8 ++
 .../kernel/syscalls/mallinfo/mallinfo01.c     | 80 +++++++++++++++++++
 .../syscalls/mallinfo/mallinfo_common.h       | 31 +++++++
 6 files changed, 123 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mallinfo/.gitignore
 create mode 100644 testcases/kernel/syscalls/mallinfo/Makefile
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo01.c
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo_common.h
configure.ac +1 -0
index d4bef5e45..2e1552cf0 100644
@@ -97,6 +97,7 @@ AC_CHECK_FUNCS_ONCE([ \
     io_uring_register \
     io_uring_enter \
     kcmp \
+    mallinfo \
     mallopt \
     mkdirat \
     mknodat \
runtest/syscalls +2 -0
index ae47a6d5e..b3f4660f4 100644
@@ -685,6 +685,8 @@ lstat01_64 lstat01_64
 lstat02 lstat02
 lstat02_64 lstat02_64
 
+mallinfo01 mallinfo01
+
 mallopt01 mallopt01
 
 mbind01 mbind01
testcases/kernel/syscalls/mallinfo/.gitignore new file +1 -0
new file mode 100644
index 000000000..a7e32a637
@@ -0,0 +1 @@
+/mallinfo01
testcases/kernel/syscalls/mallinfo/Makefile new file +8 -0
new file mode 100644
index 000000000..044619fb8
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) International Business Machines  Corp., 2001
+
+top_srcdir		?= ../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +80 -0

80 lines not on this page · raw message

testcases/kernel/syscalls/mallinfo/mallinfo_common.h new file +31 -0
new file mode 100644
index 000000000..a00cc7a64
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <[email protected]>
+ */
+#ifndef MALLINFO_COMMON_H
+#define MALLINFO_COMMON_H
+
+#include <malloc.h>
+#include "tst_test.h"
+#include "config.h"
+
+#ifdef HAVE_MALLINFO
+static inline void print_mallinfo(const char *msg, struct mallinfo *m)
+{
+	tst_res(TINFO, "%s...", msg);
+#define P(f) tst_res(TINFO, "%s: %d", #f, m->f)
+	P(arena);
+	P(ordblks);
+	P(smblks);
+	P(hblks);
+	P(hblkhd);
+	P(usmblks);
+	P(fsmblks);
+	P(uordblks);
+	P(fordblks);
+	P(keepcost);
+}
+#endif
+
+#endif
Signature · 1 line
2.23.0
author
#29 lore raw

PATCH v3 2/4 syscalls/mallopt01: Use unified print_mallinfo api

---
 testcases/kernel/syscalls/mallopt/mallopt01.c | 22 +++----------------
 1 file changed, 3 insertions(+), 19 deletions(-)
testcases/kernel/syscalls/mallopt/mallopt01.c +3 -19
index f799aaf9d..e270b0bda 100644
@@ -12,9 +12,8 @@
  * Basic mallinfo() and mallopt() testing.
 \*/
 
-#include <malloc.h>
 
-#include "tst_test.h"
+#include "../mallinfo/mallinfo_common.h"
 #include "tst_safe_macros.h"
 
 #ifdef HAVE_MALLOPT
@@ -23,21 +22,6 @@
 
 struct mallinfo info;
 
-void print_mallinfo(void)
-{
-	tst_res(TINFO, "mallinfo structure:");
-	tst_res(TINFO, "mallinfo.arena = %d", info.arena);
-	tst_res(TINFO, "mallinfo.ordblks = %d", info.ordblks);
-	tst_res(TINFO, "mallinfo.smblks = %d", info.smblks);
-	tst_res(TINFO, "mallinfo.hblkhd = %d", info.hblkhd);
-	tst_res(TINFO, "mallinfo.hblks = %d", info.hblks);
-	tst_res(TINFO, "mallinfo.usmblks = %d", info.usmblks);
-	tst_res(TINFO, "mallinfo.fsmblks = %d", info.fsmblks);
-	tst_res(TINFO, "mallinfo.uordblks = %d", info.uordblks);
-	tst_res(TINFO, "mallinfo.fordblks = %d", info.fordblks);
-	tst_res(TINFO, "mallinfo.keepcost = %d", info.keepcost);
-}
-
 void test_mallopt(void)
 {
 	char *buf;
@@ -46,11 +30,11 @@ void test_mallopt(void)
 
 	info = mallinfo();
 	if (info.uordblks < 20480) {
-		print_mallinfo();
+		print_mallinfo("Test uordblks", &info);
 		tst_res(TFAIL, "mallinfo() failed: uordblks < 20K");
 	}
 	if (info.smblks != 0) {
-		print_mallinfo();
+		print_mallinfo("Test smblks", &info);
 		tst_res(TFAIL, "mallinfo() failed: smblks != 0");
 	}
 	if (info.uordblks >= 20480 && info.smblks == 0)
Signature · 1 line
2.23.0
author
#30 lore raw

PATCH v3 3/4 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

In reply to Yang Xu

According mallinfo man-page, hblkhd member represents
"The number of bytes in blocks currently allocated using mmap(2).". For allocations greater than or equal to 128K and that can't be satisfied from the free list, the memory-allocation functions employ mmap(2) instead of increasing the program break using sbrk(2).

In this case, we test 20k size to use sbrk and 128k size to use mmap.

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/mallinfo/.gitignore |  1 +
 .../kernel/syscalls/mallinfo/mallinfo02.c     | 63 +++++++++++++++++++
 3 files changed, 65 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo02.c
runtest/syscalls +1 -0
index b3f4660f4..312544659 100644
@@ -686,6 +686,7 @@ lstat02 lstat02
 lstat02_64 lstat02_64
 
 mallinfo01 mallinfo01
+mallinfo02 mallinfo02
 
 mallopt01 mallopt01
 
testcases/kernel/syscalls/mallinfo/.gitignore +1 -0
index a7e32a637..678ac277e 100644
@@ -1 +1,2 @@
 /mallinfo01
+/mallinfo02
testcases/kernel/syscalls/mallinfo/mallinfo02.c new file +63 -0

63 lines not on this page · raw message

Signature · 1 line
2.23.0
#31 lore raw

PATCH v3 3/4 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

Hi Xu,

For patch 1/4 ~ 3/4, looks good to me.

For 4/4, I slightly think it does not make much sense to test, because it just verifies the info.hblkhd integer overflow at unsuggested usage. Anyway, that's only my thoughts, maybe a one-sided view:).

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210222/182c9e58/attachment.htm>
author
#32 lore raw

PATCH v3 3/4 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

Hi Li

Quoted 8 lines

Hi Xu,

For patch 1/4 ~ 3/4, looks good to me.

Reviewed-by Li Wang <[email protected] <mailto:[email protected]>>

For 4/4, I slightly think it does not make much sense to test, because it just verifies the info.hblkhd integer overflow at unsuggested usage. Anyway, that's only my thoughts, maybe a one-sided view:).

I guess we can just add a valid test for mallinfo2 to test it still can get the correct info when setting 2G size instead of this case.

ps: I need to find a new enough system to have supported mallinfo2.

Best Regards
Yang Xu

Quoted 4 lines

--
Regards,
Li Wang

author
#33 lore raw

PATCH v3 3/4 syscalls/mallinfo02: Add a basic test to check use mmap or sbrk

In reply to Li Wang

Hi Li

Quoted 4 lines

Hi Xu,

For patch 1/4 ~ 3/4, looks good to me.

Reviewed-by Li Wang <[email protected] <mailto:[email protected]>>

Thanks for you review. I have merged this patchset and drop the last one.

Quoted 8 lines

For 4/4, I slightly think it does not make much sense to test, because it just verifies the info.hblkhd integer overflow at unsuggested usage. Anyway, that's only my thoughts, maybe a one-sided view:).

--
Regards,
Li Wang

author
#34 lore raw

PATCH v3 4/4 syscalls/mallinfo03: Add an overflow test when setting 2G size

In reply to Yang Xu

Since these members of mallinfo struct use int type, it will overflow when allocating 2G size. mallinfo() is deprecated and we should use mallinfo2() in the future.

---
 runtest/syscalls                              |  1 +
 testcases/kernel/syscalls/mallinfo/.gitignore |  1 +
 .../kernel/syscalls/mallinfo/mallinfo03.c     | 50 +++++++++++++++++++
 3 files changed, 52 insertions(+)
 create mode 100644 testcases/kernel/syscalls/mallinfo/mallinfo03.c
runtest/syscalls +1 -0
index 312544659..7d6d42072 100644
@@ -687,6 +687,7 @@ lstat02_64 lstat02_64
 
 mallinfo01 mallinfo01
 mallinfo02 mallinfo02
+mallinfo03 mallinfo03
 
 mallopt01 mallopt01
 
testcases/kernel/syscalls/mallinfo/.gitignore +1 -0
index 678ac277e..30c315cf2 100644
@@ -1,2 +1,3 @@
 /mallinfo01
 /mallinfo02
+/mallinfo03
testcases/kernel/syscalls/mallinfo/mallinfo03.c new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
2.23.0
#35 lore raw

PATCH 1/3 syscalls/mallinfo01: Add a basic test for mallinfo

In reply to Yang Xu
Quoted 43 lines
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +37 -0
new file mode 100644
index 000000000..1a2a30af1
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <[email protected]>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Refer to glibc test
+ * https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c
+\*/
+
+#include <malloc.h>
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+
+#ifdef HAVE_MALLINFO
+static char *buf;
+static struct mallinfo info1;
+
+static void
+print_mallinfo(const char *msg, struct mallinfo *m)
+{
+	tst_res(TINFO, "%s...", msg);
+#define P(f) tst_res(TINFO, "%s: %d", #f, m->f)
+	P(arena);
+	P(ordblks);
+	P(smblks);
+	P(hblks);
+	P(hblkhd);
+	P(usmblks);
+	P(fsmblks);
+	P(uordblks);
+	P(fordblks);
+	P(keepcost);

testcases/kernel/syscalls/mallinfo/mallinfo01.c line 47

Quoted 10 lines
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +10 -0
+}
+
+void test_mallinfo(void)
+{
+	struct mallinfo info2;
+	int i;
+	int total = 0;
+
+	for (i = 1; i < 20; i++) {
+		buf = SAFE_MALLOC(160 * i);

The buf here has to be array and we have to free the buffers at the end of this function, otherwise the malloc() will fail sooner or later when the test_mallinfo() function runs in a loop.

testcases/kernel/syscalls/mallinfo/mallinfo01.c line 58

Quoted 11 lines
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +11 -0
+		total += 16 * i;
+	}
+
+	info2 = mallinfo();
+	print_mallinfo("Test", &info2);
+	if (info2.uordblks > info1.uordblks + total)
+		tst_res(TPASS, "mallinfo() passed");
+	else
+		tst_res(TFAIL, "mallinfo() failed");
+
+	info1 = info2;

And then we cannot do this either.

Quoted 34 lines
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +26 -0
+}
+
+static void setup(void)
+{
+	if (sizeof(info1.arena) != sizeof(int))
+		tst_res(TFAIL, "The member of mallinfo struct is not int");
+
+	info1 = mallinfo();
+	print_mallinfo("Start", &info1);
+}
+
+static void cleanup(void)
+{
+	if (buf)
+		free(buf);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = test_mallinfo,
+	.cleanup = cleanup,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
Signature · 7 lines
2.23.0




-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#36 lore raw

PATCH 1/3 syscalls/mallinfo01: Add a basic test for mallinfo

Hi Cyril
Sorry for late reply, I am busy with other thing.

Quoted 49 lines
Quoted 49 lines
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +43 -0
new file mode 100644
index 000000000..1a2a30af1
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu<[email protected]>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Refer to glibc test
+ * https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c
+\*/
+
+#include<malloc.h>
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+
+#ifdef HAVE_MALLINFO
+static char *buf;
+static struct mallinfo info1;
+
+static void
+print_mallinfo(const char *msg, struct mallinfo *m)
+{
+	tst_res(TINFO, "%s...", msg);
+#define P(f) tst_res(TINFO, "%s: %d", #f, m->f)
+	P(arena);
+	P(ordblks);
+	P(smblks);
+	P(hblks);
+	P(hblkhd);
+	P(usmblks);
+	P(fsmblks);
+	P(uordblks);
+	P(fordblks);
+	P(keepcost);
+}
+
+void test_mallinfo(void)
+{
+	struct mallinfo info2;
+	int i;
Quoted 8 lines

testcases/kernel/syscalls/mallinfo/mallinfo01.c line 47

Quoted 4 lines
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +4 -0
+	int total = 0;
+
+	for (i = 1; i<  20; i++) {
+		buf = SAFE_MALLOC(160 * i);

The buf here has to be array and we have to free the buffers at the end of this function, otherwise the malloc() will fail sooner or later when the test_mallinfo() function runs in a loop.

Yes. Will do it in v2.

Quoted 2 lines

testcases/kernel/syscalls/mallinfo/mallinfo01.c line 48

Quoted 2 lines
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +1 -0
+		total += 16 * i;

It looks glibc test uses wrong multiple for total variable, I will correct it (16=>160). Also, I have sent a patch to glibc.

Quoted 4 lines
Quoted 4 lines
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +4 -0
+	}
+
+	info2 = mallinfo();
+	print_mallinfo("Test",&info2);
Quoted 8 lines

testcases/kernel/syscalls/mallinfo/mallinfo01.c line 58

Quoted 6 lines
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +6 -0
+	if (info2.uordblks>  info1.uordblks + total)
+		tst_res(TPASS, "mallinfo() passed");
+	else
+		tst_res(TFAIL, "mallinfo() failed");
+
+	info1 = info2;

And then we cannot do this either.

Yes, Will do it in v2.

Quoted 36 lines
Quoted 35 lines
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +26 -1
+}
+
+static void setup(void)
+{
+	if (sizeof(info1.arena) != sizeof(int))
+		tst_res(TFAIL, "The member of mallinfo struct is not int");
+
+	info1 = mallinfo();
+	print_mallinfo("Start",&info1);
+}
+
+static void cleanup(void)
+{
+	if (buf)
+		free(buf);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = test_mallinfo,
+	.cleanup = cleanup,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
--

2.23.0

--
Mailing list info: https://lists.linux.it/listinfo/ltp

#37 lore raw

PATCH 1/3 syscalls/mallinfo01: Add a basic test for mallinfo

In reply to Yang Xu

Hi Xu,

testcases/kernel/syscalls/mallinfo/mallinfo01.c line 24

Quoted 3 lines
testcases/kernel/syscalls/mallinfo/mallinfo01.c new file +2 -0
+static void
+print_mallinfo(const char *msg, struct mallinfo *m)

What about moving this print_mallinfo() into a mallinfo_common.h file to avoid the duplicated code in each test?

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/04b5897c/attachment.htm>
author
#38 lore raw

PATCH 1/3 syscalls/mallinfo01: Add a basic test for mallinfo

Hi Li

Quoted 8 lines

Hi Xu,

+static void
+print_mallinfo(const char *msg, struct mallinfo *m)

What about moving this print_mallinfo() into a mallinfo_common.h file to avoid the duplicated code in each test?

Good suggestion. Will do it.

Quoted 4 lines

--
Regards,
Li Wang