All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo
@ 2021-01-26  9:55 Yang Xu
  2021-01-26  9:55 ` [LTP] [PATCH v1 2/3] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
                   ` (3 more replies)
  0 siblings, 4 replies; 39+ messages in thread
From: Yang Xu @ 2021-01-26  9:55 UTC (permalink / raw)
  To: ltp

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

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 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

diff --git a/configure.ac b/configure.ac
index 8bdb96300..223900ca1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -94,6 +94,7 @@ AC_CHECK_FUNCS_ONCE([ \
     io_uring_register \
     io_uring_enter \
     kcmp \
+    mallinfo \
     mallopt \
     mkdirat \
     mknodat \
diff --git a/runtest/syscalls b/runtest/syscalls
index 576eacf83..d0a9a5145 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -682,6 +682,8 @@ lstat01_64 lstat01_64
 lstat02 lstat02
 lstat02_64 lstat02_64
 
+mallinfo01 mallinfo01
+
 mallopt01 mallopt01
 
 mbind01 mbind01
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
new file mode 100644
index 000000000..a7e32a637
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -0,0 +1 @@
+/mallinfo01
diff --git a/testcases/kernel/syscalls/mallinfo/Makefile b/testcases/kernel/syscalls/mallinfo/Makefile
new file mode 100644
index 000000000..044619fb8
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/Makefile
@@ -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
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo01.c b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
new file mode 100644
index 000000000..1a2a30af1
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
@@ -0,0 +1,84 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [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;
+	int total = 0;
+
+	for (i = 1; i < 20; i++) {
+		buf = SAFE_MALLOC(160 * i);
+		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;
+}
+
+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




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 2/3] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-01-26  9:55 [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo Yang Xu
@ 2021-01-26  9:55 ` Yang Xu
  2021-01-28 15:37   ` Cyril Hrubis
  2021-01-26  9:55 ` [LTP] [PATCH v1 3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 39+ messages in thread
From: Yang Xu @ 2021-01-26  9:55 UTC (permalink / raw)
  To: ltp

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 <xuyang2018.jy@cn.fujitsu.com>
---
 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

diff --git a/runtest/syscalls b/runtest/syscalls
index d0a9a5145..74ad999e8 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..f0c9cb99c
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test for malloc() using sbrk or mmap.
+ * It size > 128k, it will use mmap and deallocated space is not placed on
+ * the free list for reuse by later allocations.
+\*/
+
+#include <malloc.h>
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+
+#ifdef HAVE_MALLINFO
+static int reuse_size;
+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_mallopt(void)
+{
+	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");
+		print_mallinfo("Test malloc(20480)", &info);
+	}
+	free(buf);
+
+	info = mallinfo();
+	size = MAX(info.fordblks, 131072) + reuse_size;
+	buf = SAFE_MALLOC(size);
+	info = mallinfo();
+	if (info.hblkhd > size) {
+		tst_res(TPASS, "malloc() uses mmap when size >= 128k");
+	} else {
+		tst_res(TFAIL, "malloc uses sbrk when size >= 128k");
+		print_mallinfo("Test malloc(1024*128)", &info);
+	}
+
+	reuse_size = info.hblkhd;
+	free(buf);
+}
+
+static struct tst_test test = {
+	.test_all = test_mallopt,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
-- 
2.23.0




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size
  2021-01-26  9:55 [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo Yang Xu
  2021-01-26  9:55 ` [LTP] [PATCH v1 2/3] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
@ 2021-01-26  9:55 ` Yang Xu
  2021-01-28 15:42   ` Cyril Hrubis
  2021-02-04  8:54   ` Li Wang
  2021-01-28 15:50 ` [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo Cyril Hrubis
  2021-02-04  6:42 ` Li Wang
  3 siblings, 2 replies; 39+ messages in thread
From: Yang Xu @ 2021-01-26  9:55 UTC (permalink / raw)
  To: ltp

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.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 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

diff --git a/runtest/syscalls b/runtest/syscalls
index 74ad999e8..8fe123f8c 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -684,6 +684,7 @@ lstat02_64 lstat02_64
 
 mallinfo01 mallinfo01
 mallinfo02 mallinfo02
+mallinfo03 mallinfo03
 
 mallopt01 mallopt01
 
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
index 678ac277e..30c315cf2 100644
--- a/testcases/kernel/syscalls/mallinfo/.gitignore
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -1,2 +1,3 @@
 /mallinfo01
 /mallinfo02
+/mallinfo03
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo03.c b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
new file mode 100644
index 000000000..e8dc35e42
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Test the member of struct mallinfo
+ * whether overflow when setting 2G size. mallinfo() is deprecated
+ * since the type used for the fields is too small. We should use
+ * mallinfo2() and it was added since glibc 2.33.
+\*/
+
+#include <malloc.h>
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+
+#ifdef HAVE_MALLINFO
+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 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");
+	} 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




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 2/3] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-01-26  9:55 ` [LTP] [PATCH v1 2/3] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
@ 2021-01-28 15:37   ` Cyril Hrubis
  2021-02-03  9:47     ` Yang Xu
  0 siblings, 1 reply; 39+ messages in thread
From: Cyril Hrubis @ 2021-01-28 15:37 UTC (permalink / raw)
  To: ltp

Hi!
> "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.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size
  2021-01-26  9:55 ` [LTP] [PATCH v1 3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
@ 2021-01-28 15:42   ` Cyril Hrubis
  2021-02-03  9:49     ` Yang Xu
  2021-02-04  8:54   ` Li Wang
  1 sibling, 1 reply; 39+ messages in thread
From: Cyril Hrubis @ 2021-01-28 15:42 UTC (permalink / raw)
  To: ltp

Hi!
> +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.

> +	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

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo
  2021-01-26  9:55 [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo Yang Xu
  2021-01-26  9:55 ` [LTP] [PATCH v1 2/3] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
  2021-01-26  9:55 ` [LTP] [PATCH v1 3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
@ 2021-01-28 15:50 ` Cyril Hrubis
  2021-02-03  6:03   ` Yang Xu
  2021-02-04  6:42 ` Li Wang
  3 siblings, 1 reply; 39+ messages in thread
From: Cyril Hrubis @ 2021-01-28 15:50 UTC (permalink / raw)
  To: ltp

> diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo01.c b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
> new file mode 100644
> index 000000000..1a2a30af1
> --- /dev/null
> +++ b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
> @@ -0,0 +1,84 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
> + * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
> + */
> +
> +/*\
> + * [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;
> +	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.

> +		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.

> +}
> +
> +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

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo
  2021-01-28 15:50 ` [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo Cyril Hrubis
@ 2021-02-03  6:03   ` Yang Xu
  0 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-03  6:03 UTC (permalink / raw)
  To: ltp

Hi Cyril
Sorry for late reply, I am busy with other thing.
>> diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo01.c b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
>> new file mode 100644
>> index 000000000..1a2a30af1
>> --- /dev/null
>> +++ b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
>> @@ -0,0 +1,84 @@
>> +// SPDX-License-Identifier: GPL-2.0-or-later
>> +/*
>> + * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
>> + * Author: Yang Xu<xuyang2018.jy@cn.fujitsu.com>
>> + */
>> +
>> +/*\
>> + * [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;
>> +	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.
>
>> +		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.
>> +	}
>> +
>> +	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.
Yes, Will do it in v2.
>
>> +}
>> +
>> +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
>




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 2/3] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-01-28 15:37   ` Cyril Hrubis
@ 2021-02-03  9:47     ` Yang Xu
  2021-02-03 10:46       ` Yang Xu
  2021-02-04  6:08       ` Li Wang
  0 siblings, 2 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-03  9:47 UTC (permalink / raw)
  To: ltp

Hi Cyril
> Hi!
>> "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.
>
> 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

>




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size
  2021-01-28 15:42   ` Cyril Hrubis
@ 2021-02-03  9:49     ` Yang Xu
  0 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-03  9:49 UTC (permalink / raw)
  To: ltp

Hi Cyril
> Hi!
>> +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.
>
Agree. Will do it in v2.
>> +	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
>




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 2/3] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-03  9:47     ` Yang Xu
@ 2021-02-03 10:46       ` Yang Xu
  2021-02-04  6:08       ` Li Wang
  1 sibling, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-03 10:46 UTC (permalink / raw)
  To: ltp

Hi Cyril
> Hi Cyril
>> Hi!
>>> "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.
>>
>> 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?
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
>
>
> Best Regards
> Yang Xu
>
>>
>
>
>
>




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 2/3] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-03  9:47     ` Yang Xu
  2021-02-03 10:46       ` Yang Xu
@ 2021-02-04  6:08       ` Li Wang
  2021-02-04  9:53         ` Yang Xu
  1 sibling, 1 reply; 39+ messages in thread
From: Li Wang @ 2021-02-04  6:08 UTC (permalink / raw)
  To: ltp

Hi Xu,

Yang Xu <xuyang2018.jy@cn.fujitsu.com> wrote:

...
>
> 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.

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/74a8a9ec/attachment.htm>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo
  2021-01-26  9:55 [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo Yang Xu
                   ` (2 preceding siblings ...)
  2021-01-28 15:50 ` [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo Cyril Hrubis
@ 2021-02-04  6:42 ` Li Wang
  2021-02-04  9:54   ` Yang Xu
  3 siblings, 1 reply; 39+ messages in thread
From: Li Wang @ 2021-02-04  6:42 UTC (permalink / raw)
  To: ltp

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?

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/04b5897c/attachment.htm>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size
  2021-01-26  9:55 ` [LTP] [PATCH v1 3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
  2021-01-28 15:42   ` Cyril Hrubis
@ 2021-02-04  8:54   ` Li Wang
  2021-02-04 10:01     ` Yang Xu
  2021-02-04 12:12     ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
  1 sibling, 2 replies; 39+ messages in thread
From: Li Wang @ 2021-02-04  8:54 UTC (permalink / raw)
  To: ltp

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");
>

TFAIL?



> +       }
> +       free(buf);
> +}
>


-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/101b8c8d/attachment.htm>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 2/3] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-04  6:08       ` Li Wang
@ 2021-02-04  9:53         ` Yang Xu
  0 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-04  9:53 UTC (permalink / raw)
  To: ltp

Hi Li
> Hi Xu,
>
> Yang Xu <xuyang2018.jy@cn.fujitsu.com
> <mailto:xuyang2018.jy@cn.fujitsu.com>> wrote:
>
>     ...
>
>     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.
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.
>
> 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.
>
> --
> Regards,
> Li Wang




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo
  2021-02-04  6:42 ` Li Wang
@ 2021-02-04  9:54   ` Yang Xu
  0 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-04  9:54 UTC (permalink / raw)
  To: ltp

Hi Li
> 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.
>
> --
> Regards,
> Li Wang




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size
  2021-02-04  8:54   ` Li Wang
@ 2021-02-04 10:01     ` Yang Xu
  2021-02-04 13:56       ` Li Wang
  2021-02-04 12:12     ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
  1 sibling, 1 reply; 39+ messages in thread
From: Yang Xu @ 2021-02-04 10:01 UTC (permalink / raw)
  To: ltp

Hi Li
> 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");
>
>
> 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.

>
>
>
> --
> Regards,
> Li Wang




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo
  2021-02-04  8:54   ` Li Wang
  2021-02-04 10:01     ` Yang Xu
@ 2021-02-04 12:12     ` Yang Xu
  2021-02-04 12:12       ` [LTP] [PATCH v2 2/5] syscalls/mallopt01: Use tst_print_mallinfo api Yang Xu
                         ` (4 more replies)
  1 sibling, 5 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-04 12:12 UTC (permalink / raw)
  To: ltp

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

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 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

diff --git a/configure.ac b/configure.ac
index 8bdb96300..223900ca1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -94,6 +94,7 @@ AC_CHECK_FUNCS_ONCE([ \
     io_uring_register \
     io_uring_enter \
     kcmp \
+    mallinfo \
     mallopt \
     mkdirat \
     mknodat \
diff --git a/include/tst_mallinfo.h b/include/tst_mallinfo.h
new file mode 100644
index 000000000..bd69d032c
--- /dev/null
+++ b/include/tst_mallinfo.h
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+#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
diff --git a/lib/tst_mallinfo.c b/lib/tst_mallinfo.c
new file mode 100644
index 000000000..229e5aa2e
--- /dev/null
+++ b/lib/tst_mallinfo.c
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+#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
-- 
2.23.0




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 2/5] syscalls/mallopt01: Use tst_print_mallinfo api
  2021-02-04 12:12     ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
@ 2021-02-04 12:12       ` Yang Xu
  2021-02-04 12:12       ` [LTP] [PATCH v2 3/5] syscalls/mallinfo01: Add a basic test for mallinfo Yang Xu
                         ` (3 subsequent siblings)
  4 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-04 12:12 UTC (permalink / raw)
  To: ltp

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mallopt/mallopt01.c | 22 +++----------------
 1 file changed, 3 insertions(+), 19 deletions(-)

diff --git a/testcases/kernel/syscalls/mallopt/mallopt01.c b/testcases/kernel/syscalls/mallopt/mallopt01.c
index f799aaf9d..271079c12 100644
--- a/testcases/kernel/syscalls/mallopt/mallopt01.c
+++ b/testcases/kernel/syscalls/mallopt/mallopt01.c
@@ -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)
-- 
2.23.0




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 3/5] syscalls/mallinfo01: Add a basic test for mallinfo
  2021-02-04 12:12     ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
  2021-02-04 12:12       ` [LTP] [PATCH v2 2/5] syscalls/mallopt01: Use tst_print_mallinfo api Yang Xu
@ 2021-02-04 12:12       ` Yang Xu
  2021-02-04 12:12       ` [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
                         ` (2 subsequent siblings)
  4 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-04 12:12 UTC (permalink / raw)
  To: ltp

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

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
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

diff --git a/runtest/syscalls b/runtest/syscalls
index 11a1e83c2..753340068 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -682,6 +682,8 @@ lstat01_64 lstat01_64
 lstat02 lstat02
 lstat02_64 lstat02_64
 
+mallinfo01 mallinfo01
+
 mallopt01 mallopt01
 
 mbind01 mbind01
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
new file mode 100644
index 000000000..a7e32a637
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -0,0 +1 @@
+/mallinfo01
diff --git a/testcases/kernel/syscalls/mallinfo/Makefile b/testcases/kernel/syscalls/mallinfo/Makefile
new file mode 100644
index 000000000..044619fb8
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/Makefile
@@ -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
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo01.c b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
new file mode 100644
index 000000000..add3c0df3
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
@@ -0,0 +1,82 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Refer to glibc test mallinfo2 test
+ * https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c
+\*/
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "tst_mallinfo.h"
+
+#ifdef HAVE_MALLINFO
+#define M_NUM 20
+static struct mallinfo info1;
+static void *buf[M_NUM + 1];
+
+static void cleanup(void)
+{
+	int i;
+
+	for (i = M_NUM; i > 0; i--) {
+		if (buf[i]) {
+			free(buf[i]);
+			buf[i] = NULL;
+		}
+	}
+}
+
+void test_mallinfo(void)
+{
+	int i;
+	int total = 0;
+	struct mallinfo info2;
+
+	for (i = 1; i <= M_NUM; i++) {
+		buf[i] = SAFE_MALLOC(160 * i);
+		total += 160 * i;
+	}
+	info2 = mallinfo();
+	tst_print_mallinfo("Test uordblks", &info2);
+	if (info2.uordblks > info1.uordblks + total)
+		tst_res(TPASS, "mallinfo() uordblks passed");
+	else
+		tst_res(TFAIL, "mallinfo() uordblks failed");
+
+	//Create another free chunk
+	free(buf[M_NUM/2]);
+	buf[M_NUM/2] = NULL;
+	info2 = mallinfo();
+	tst_print_mallinfo("Test ordblks", &info2);
+	if (info2.ordblks == info1.ordblks + 1)
+		tst_res(TPASS, "mallinfo() ordblks passed");
+	else
+		tst_res(TFAIL, "mallinfo() ordblks failed");
+
+	cleanup();
+}
+
+static void setup(void)
+{
+	if (sizeof(info1.arena) != sizeof(int))
+		tst_res(TFAIL, "The member of mallinfo struct is not int");
+
+	info1 = mallinfo();
+	tst_print_mallinfo("Start", &info1);
+}
+
+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




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-04 12:12     ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
  2021-02-04 12:12       ` [LTP] [PATCH v2 2/5] syscalls/mallopt01: Use tst_print_mallinfo api Yang Xu
  2021-02-04 12:12       ` [LTP] [PATCH v2 3/5] syscalls/mallinfo01: Add a basic test for mallinfo Yang Xu
@ 2021-02-04 12:12       ` Yang Xu
  2021-02-04 13:51         ` Li Wang
  2021-02-04 12:12       ` [LTP] [PATCH v2 5/5] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
  2021-02-04 13:29       ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Li Wang
  4 siblings, 1 reply; 39+ messages in thread
From: Yang Xu @ 2021-02-04 12:12 UTC (permalink / raw)
  To: ltp

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 <xuyang2018.jy@cn.fujitsu.com>
---
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 <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [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)
+{
+	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");
+		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");
+}
+
+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




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 5/5] syscalls/mallinfo03: Add an overflow test when setting 2G size
  2021-02-04 12:12     ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
                         ` (2 preceding siblings ...)
  2021-02-04 12:12       ` [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
@ 2021-02-04 12:12       ` Yang Xu
  2021-02-04 13:29       ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Li Wang
  4 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-04 12:12 UTC (permalink / raw)
  To: ltp

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.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
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

diff --git a/runtest/syscalls b/runtest/syscalls
index a8fa3f7bf..c5cf78381 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -684,6 +684,7 @@ lstat02_64 lstat02_64
 
 mallinfo01 mallinfo01
 mallinfo02 mallinfo02
+mallinfo03 mallinfo03
 
 mallopt01 mallopt01
 
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
index 678ac277e..30c315cf2 100644
--- a/testcases/kernel/syscalls/mallinfo/.gitignore
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -1,2 +1,3 @@
 /mallinfo01
 /mallinfo02
+/mallinfo03
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo03.c b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
new file mode 100644
index 000000000..36d9e5d85
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
@@ -0,0 +1,51 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Test the member of struct mallinfo
+ * whether overflow when setting 2G size. mallinfo() is deprecated
+ * since the type used for the fields is too small. We should use
+ * mallinfo2() and it was added since glibc 2.33.
+\*/
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "tst_mallinfo.h"
+
+#ifdef HAVE_MALLINFO
+
+void test_mallinfo(void)
+{
+	struct mallinfo info;
+	char *buf;
+	size_t size = 2UL * 1024UL * 1024UL * 1024UL;
+
+	buf = malloc(size);
+	if (!buf) {
+		tst_res(TCONF, "Current system can not malloc 2G space, skip it");
+		return;
+	}
+	info = mallinfo();
+	if (info.hblkhd < 0) {
+		tst_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




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo
  2021-02-04 12:12     ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
                         ` (3 preceding siblings ...)
  2021-02-04 12:12       ` [LTP] [PATCH v2 5/5] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
@ 2021-02-04 13:29       ` Li Wang
  2021-02-05  7:44         ` Yang Xu
  4 siblings, 1 reply; 39+ messages in thread
From: Li Wang @ 2021-02-04 13:29 UTC (permalink / raw)
  To: ltp

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)

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".

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/b556dd12/attachment.htm>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-04 12:12       ` [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
@ 2021-02-04 13:51         ` Li Wang
  2021-02-05  7:51           ` Yang Xu
                             ` (2 more replies)
  0 siblings, 3 replies; 39+ messages in thread
From: Li Wang @ 2021-02-04 13:51 UTC (permalink / raw)
  To: ltp

On Thu, Feb 4, 2021 at 8:12 PM Yang Xu <xuyang2018.jy@cn.fujitsu.com> 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 <xuyang2018.jy@cn.fujitsu.com>
> ---
> 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 <xuyang2018.jy@cn.fujitsu.com>
> + */
> +
> +/*\
> + * [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)
>

what about renaming to test_mallinfo(void) ?



> +{
> +       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");
>

Why not 'TFAIL | TERRNO' ?


> +               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.


> +}
> +
> +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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/4e0843f5/attachment-0001.htm>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v1 3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size
  2021-02-04 10:01     ` Yang Xu
@ 2021-02-04 13:56       ` Li Wang
  0 siblings, 0 replies; 39+ messages in thread
From: Li Wang @ 2021-02-04 13:56 UTC (permalink / raw)
  To: ltp

> >     + 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");
> >
> >
> > 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

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210204/1680b7bd/attachment.htm>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo
  2021-02-04 13:29       ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Li Wang
@ 2021-02-05  7:44         ` Yang Xu
  2021-02-05  8:00           ` Li Wang
  0 siblings, 1 reply; 39+ messages in thread
From: Yang Xu @ 2021-02-05  7:44 UTC (permalink / raw)
  To: ltp

Hi Li
> 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.

>
> 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.
>
> --
> Regards,
> Li Wang




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-04 13:51         ` Li Wang
@ 2021-02-05  7:51           ` Yang Xu
  2021-02-05  8:15           ` Yang Xu
  2021-02-05  9:00           ` Yang Xu
  2 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-05  7:51 UTC (permalink / raw)
  To: ltp

Hi Li
>
>
> On Thu, Feb 4, 2021 at 8:12 PM Yang Xu <xuyang2018.jy@cn.fujitsu.com
> <mailto:xuyang2018.jy@cn.fujitsu.com>> 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 <xuyang2018.jy@cn.fujitsu.com
>     <mailto:xuyang2018.jy@cn.fujitsu.com>>
>     ---
>     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 <xuyang2018.jy@cn.fujitsu.com
>     <mailto:xuyang2018.jy@cn.fujitsu.com>>
>     + */
>     +
>     +/*\
>     + * [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)
>
>
> what about renaming to test_mallinfo(void) ?
Yes, sorry for typo.
>
>     +{
>     + 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");
>
>
> Why not 'TFAIL | TERRNO' ?
I don't see mallinfo() will fail and it should always succeed.
>
>     + 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.
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.

>
>     +}
>     +
>     +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




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo
  2021-02-05  7:44         ` Yang Xu
@ 2021-02-05  8:00           ` Li Wang
  2021-02-05 11:21             ` Petr Vorel
  2021-02-08 15:30             ` Cyril Hrubis
  0 siblings, 2 replies; 39+ messages in thread
From: Li Wang @ 2021-02-05  8:00 UTC (permalink / raw)
  To: ltp

On Fri, Feb 5, 2021 at 3:44 PM Yang Xu <xuyang2018.jy@cn.fujitsu.com> wrote:

> Hi Li
> > 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.
>

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



>
> >
> > 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~

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210205/c6b2fe84/attachment.htm>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-04 13:51         ` Li Wang
  2021-02-05  7:51           ` Yang Xu
@ 2021-02-05  8:15           ` Yang Xu
  2021-02-05  9:00           ` Yang Xu
  2 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-05  8:15 UTC (permalink / raw)
  To: ltp

Hi Li

I have reply this email, but I don't see it on inbox or patchwork, so again
>     +
>     +#ifdef HAVE_MALLINFO
>     +void test_mallopt(void)
>
>
> what about renaming to test_mallinfo(void) ?
Yes, sorry for this typo.
>
>     +{
>     + 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");
>
>
> Why not 'TFAIL | TERRNO' ?
mallinfo should always succeed.
>
>     + 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.
mallopt man-pages said even mallopt fails, return 0 and it doesn't set 
errno.
>
>     +}
>     +
>     +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




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-04 13:51         ` Li Wang
  2021-02-05  7:51           ` Yang Xu
  2021-02-05  8:15           ` Yang Xu
@ 2021-02-05  9:00           ` Yang Xu
  2021-02-05  9:20             ` Li Wang
  2 siblings, 1 reply; 39+ messages in thread
From: Yang Xu @ 2021-02-05  9:00 UTC (permalink / raw)
  To: ltp

Hi Li

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

>     +#ifdef HAVE_MALLINFO
>     +void test_mallopt(void)
>
>
> what about renaming to test_mallinfo(void) ?
Yes, sorry for this typo.
>
>     +{
>     + 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");
>
>
> Why not 'TFAIL | TERRNO' ?
mallinfo should succeed always.
>
>     + 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.
>
 From mallopt man-page, even it fail, it doesn't set errno and only return 0
>     +}
>     +
>     +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




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-05  9:00           ` Yang Xu
@ 2021-02-05  9:20             ` Li Wang
  0 siblings, 0 replies; 39+ messages in thread
From: Li Wang @ 2021-02-05  9:20 UTC (permalink / raw)
  To: ltp

Hi Xu,

Yang Xu <xuyang2018.jy@cn.fujitsu.com> wrote:

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

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

-- 
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210205/e3f21ac7/attachment.htm>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo
  2021-02-05  8:00           ` Li Wang
@ 2021-02-05 11:21             ` Petr Vorel
  2021-02-08 15:30             ` Cyril Hrubis
  1 sibling, 0 replies; 39+ messages in thread
From: Petr Vorel @ 2021-02-05 11:21 UTC (permalink / raw)
  To: ltp

Hi Xu, Li,

> > >     --- /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

> > > 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~

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo
  2021-02-05  8:00           ` Li Wang
  2021-02-05 11:21             ` Petr Vorel
@ 2021-02-08 15:30             ` Cyril Hrubis
  2021-02-18  5:52               ` [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test " Yang Xu
  1 sibling, 1 reply; 39+ messages in thread
From: Cyril Hrubis @ 2021-02-08 15:30 UTC (permalink / raw)
  To: ltp

Hi!
> > 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.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test for mallinfo
  2021-02-08 15:30             ` Cyril Hrubis
@ 2021-02-18  5:52               ` Yang Xu
  2021-02-18  5:52                 ` [LTP] [PATCH v3 2/4] syscalls/mallopt01: Use unified print_mallinfo api Yang Xu
                                   ` (2 more replies)
  0 siblings, 3 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-18  5:52 UTC (permalink / raw)
  To: ltp

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

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 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

diff --git a/configure.ac b/configure.ac
index d4bef5e45..2e1552cf0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -97,6 +97,7 @@ AC_CHECK_FUNCS_ONCE([ \
     io_uring_register \
     io_uring_enter \
     kcmp \
+    mallinfo \
     mallopt \
     mkdirat \
     mknodat \
diff --git a/runtest/syscalls b/runtest/syscalls
index ae47a6d5e..b3f4660f4 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -685,6 +685,8 @@ lstat01_64 lstat01_64
 lstat02 lstat02
 lstat02_64 lstat02_64
 
+mallinfo01 mallinfo01
+
 mallopt01 mallopt01
 
 mbind01 mbind01
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
new file mode 100644
index 000000000..a7e32a637
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -0,0 +1 @@
+/mallinfo01
diff --git a/testcases/kernel/syscalls/mallinfo/Makefile b/testcases/kernel/syscalls/mallinfo/Makefile
new file mode 100644
index 000000000..044619fb8
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/Makefile
@@ -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
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo01.c b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
new file mode 100644
index 000000000..ba5366896
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo01.c
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Refer to glibc test mallinfo2 test
+ * https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/tst-mallinfo2.c
+\*/
+#include "mallinfo_common.h"
+#include "tst_safe_macros.h"
+
+#ifdef HAVE_MALLINFO
+#define M_NUM 20
+static struct mallinfo info1;
+static void *buf[M_NUM + 1];
+
+static void cleanup(void)
+{
+	int i;
+
+	for (i = M_NUM; i > 0; i--) {
+		if (buf[i]) {
+			free(buf[i]);
+			buf[i] = NULL;
+		}
+	}
+}
+
+void test_mallinfo(void)
+{
+	int i;
+	int total = 0;
+	struct mallinfo info2;
+
+	for (i = 1; i <= M_NUM; i++) {
+		buf[i] = SAFE_MALLOC(160 * i);
+		total += 160 * i;
+	}
+	info2 = mallinfo();
+	print_mallinfo("Test uordblks", &info2);
+	if (info2.uordblks >= info1.uordblks + total)
+		tst_res(TPASS, "mallinfo() uordblks passed");
+	else
+		tst_res(TFAIL, "mallinfo() uordblks failed");
+
+	//Create another free chunk
+	free(buf[M_NUM/2]);
+	buf[M_NUM/2] = NULL;
+	info2 = mallinfo();
+	print_mallinfo("Test ordblks", &info2);
+	if (info2.ordblks == info1.ordblks + 1)
+		tst_res(TPASS, "mallinfo() ordblks passed");
+	else
+		tst_res(TFAIL, "mallinfo() ordblks failed");
+
+	cleanup();
+}
+
+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 struct tst_test test = {
+	.setup = setup,
+	.test_all = test_mallinfo,
+	.cleanup = cleanup,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo_common.h b/testcases/kernel/syscalls/mallinfo/mallinfo_common.h
new file mode 100644
index 000000000..a00cc7a64
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo_common.h
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+#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
-- 
2.23.0




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v3 2/4] syscalls/mallopt01: Use unified print_mallinfo api
  2021-02-18  5:52               ` [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test " Yang Xu
@ 2021-02-18  5:52                 ` Yang Xu
  2021-02-18  5:52                 ` [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
  2021-02-18  5:52                 ` [LTP] [PATCH v3 4/4] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
  2 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-18  5:52 UTC (permalink / raw)
  To: ltp

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/mallopt/mallopt01.c | 22 +++----------------
 1 file changed, 3 insertions(+), 19 deletions(-)

diff --git a/testcases/kernel/syscalls/mallopt/mallopt01.c b/testcases/kernel/syscalls/mallopt/mallopt01.c
index f799aaf9d..e270b0bda 100644
--- a/testcases/kernel/syscalls/mallopt/mallopt01.c
+++ b/testcases/kernel/syscalls/mallopt/mallopt01.c
@@ -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)
-- 
2.23.0




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-18  5:52               ` [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test " Yang Xu
  2021-02-18  5:52                 ` [LTP] [PATCH v3 2/4] syscalls/mallopt01: Use unified print_mallinfo api Yang Xu
@ 2021-02-18  5:52                 ` Yang Xu
  2021-02-22  6:32                   ` Li Wang
  2021-02-18  5:52                 ` [LTP] [PATCH v3 4/4] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
  2 siblings, 1 reply; 39+ messages in thread
From: Yang Xu @ 2021-02-18  5:52 UTC (permalink / raw)
  To: ltp

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 <xuyang2018.jy@cn.fujitsu.com>
---
 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

diff --git a/runtest/syscalls b/runtest/syscalls
index b3f4660f4..312544659 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -686,6 +686,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..945d3227c
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo02.c
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test for malloc() using sbrk or mmap.
+ * It size > MMAP_THRESHOLD, it will use mmap. Otherwise, use sbrk.
+\*/
+
+#include "mallinfo_common.h"
+#include "tst_safe_macros.h"
+
+#ifdef HAVE_MALLINFO
+void test_mallinfo(void)
+{
+	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");
+		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");
+		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");
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.test_all = test_mallinfo,
+};
+
+#else
+TST_TEST_TCONF("system doesn't implement non-POSIX mallinfo()");
+#endif
-- 
2.23.0




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v3 4/4] syscalls/mallinfo03: Add an overflow test when setting 2G size
  2021-02-18  5:52               ` [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test " Yang Xu
  2021-02-18  5:52                 ` [LTP] [PATCH v3 2/4] syscalls/mallopt01: Use unified print_mallinfo api Yang Xu
  2021-02-18  5:52                 ` [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
@ 2021-02-18  5:52                 ` Yang Xu
  2 siblings, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-18  5:52 UTC (permalink / raw)
  To: ltp

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.

Signed-off-by: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
---
 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

diff --git a/runtest/syscalls b/runtest/syscalls
index 312544659..7d6d42072 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -687,6 +687,7 @@ lstat02_64 lstat02_64
 
 mallinfo01 mallinfo01
 mallinfo02 mallinfo02
+mallinfo03 mallinfo03
 
 mallopt01 mallopt01
 
diff --git a/testcases/kernel/syscalls/mallinfo/.gitignore b/testcases/kernel/syscalls/mallinfo/.gitignore
index 678ac277e..30c315cf2 100644
--- a/testcases/kernel/syscalls/mallinfo/.gitignore
+++ b/testcases/kernel/syscalls/mallinfo/.gitignore
@@ -1,2 +1,3 @@
 /mallinfo01
 /mallinfo02
+/mallinfo03
diff --git a/testcases/kernel/syscalls/mallinfo/mallinfo03.c b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
new file mode 100644
index 000000000..d3c6dd52d
--- /dev/null
+++ b/testcases/kernel/syscalls/mallinfo/mallinfo03.c
@@ -0,0 +1,50 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
+ */
+
+/*\
+ * [DESCRIPTION]
+ *
+ * Basic mallinfo() test. Test the member of struct mallinfo
+ * whether overflow when setting 2G size. mallinfo() is deprecated
+ * since the type used for the fields is too small. We should use
+ * mallinfo2() and it was added since glibc 2.33.
+\*/
+
+#include "mallinfo_common.h"
+#include "tst_safe_macros.h"
+
+#ifdef HAVE_MALLINFO
+
+void test_mallinfo(void)
+{
+	struct mallinfo info;
+	char *buf;
+	size_t size = 2UL * 1024UL * 1024UL * 1024UL;
+
+	buf = malloc(size);
+	if (!buf) {
+		tst_res(TCONF, "Current system can not malloc 2G space, skip it");
+		return;
+	}
+	info = mallinfo();
+	if (info.hblkhd < 0) {
+		print_mallinfo("Test malloc 2G", &info);
+		tst_res(TPASS, "The member of struct mallinfo overflow, we should use mallinfo2");
+	} else {
+		/*We will never get here*/
+		tst_res(TFAIL, "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




^ permalink raw reply related	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-18  5:52                 ` [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
@ 2021-02-22  6:32                   ` Li Wang
  2021-02-22  6:40                     ` Yang Xu
  2021-02-23  1:45                     ` Yang Xu
  0 siblings, 2 replies; 39+ messages in thread
From: Li Wang @ 2021-02-22  6:32 UTC (permalink / raw)
  To: ltp

Hi Xu,

For patch 1/4 ~ 3/4, looks good to me.
Reviewed-by: Li Wang <liwang@redhat.com>

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20210222/182c9e58/attachment.htm>

^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-22  6:32                   ` Li Wang
@ 2021-02-22  6:40                     ` Yang Xu
  2021-02-23  1:45                     ` Yang Xu
  1 sibling, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-22  6:40 UTC (permalink / raw)
  To: ltp

Hi Li
> Hi Xu,
>
> For patch 1/4 ~ 3/4, looks good to me.
> Reviewed-by: Li Wang <liwang@redhat.com <mailto:liwang@redhat.com>>
>
> 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
>
> --
> Regards,
> Li Wang




^ permalink raw reply	[flat|nested] 39+ messages in thread

* [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk
  2021-02-22  6:32                   ` Li Wang
  2021-02-22  6:40                     ` Yang Xu
@ 2021-02-23  1:45                     ` Yang Xu
  1 sibling, 0 replies; 39+ messages in thread
From: Yang Xu @ 2021-02-23  1:45 UTC (permalink / raw)
  To: ltp

Hi Li
> Hi Xu,
>
> For patch 1/4 ~ 3/4, looks good to me.
> Reviewed-by: Li Wang <liwang@redhat.com <mailto:liwang@redhat.com>>
Thanks for you review. I have merged this patchset and drop the last one.
>
> 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




^ permalink raw reply	[flat|nested] 39+ messages in thread

end of thread, other threads:[~2021-02-23  1:45 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-26  9:55 [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo Yang Xu
2021-01-26  9:55 ` [LTP] [PATCH v1 2/3] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
2021-01-28 15:37   ` Cyril Hrubis
2021-02-03  9:47     ` Yang Xu
2021-02-03 10:46       ` Yang Xu
2021-02-04  6:08       ` Li Wang
2021-02-04  9:53         ` Yang Xu
2021-01-26  9:55 ` [LTP] [PATCH v1 3/3] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
2021-01-28 15:42   ` Cyril Hrubis
2021-02-03  9:49     ` Yang Xu
2021-02-04  8:54   ` Li Wang
2021-02-04 10:01     ` Yang Xu
2021-02-04 13:56       ` Li Wang
2021-02-04 12:12     ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Yang Xu
2021-02-04 12:12       ` [LTP] [PATCH v2 2/5] syscalls/mallopt01: Use tst_print_mallinfo api Yang Xu
2021-02-04 12:12       ` [LTP] [PATCH v2 3/5] syscalls/mallinfo01: Add a basic test for mallinfo Yang Xu
2021-02-04 12:12       ` [LTP] [PATCH v2 4/5] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
2021-02-04 13:51         ` Li Wang
2021-02-05  7:51           ` Yang Xu
2021-02-05  8:15           ` Yang Xu
2021-02-05  9:00           ` Yang Xu
2021-02-05  9:20             ` Li Wang
2021-02-04 12:12       ` [LTP] [PATCH v2 5/5] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
2021-02-04 13:29       ` [LTP] [PATCH v2 1/5] tst_mallinfo.c: Add a common print helper for mallinfo Li Wang
2021-02-05  7:44         ` Yang Xu
2021-02-05  8:00           ` Li Wang
2021-02-05 11:21             ` Petr Vorel
2021-02-08 15:30             ` Cyril Hrubis
2021-02-18  5:52               ` [LTP] [PATCH v3 1/4] syscalls/mallinfo01: Add a basic test " Yang Xu
2021-02-18  5:52                 ` [LTP] [PATCH v3 2/4] syscalls/mallopt01: Use unified print_mallinfo api Yang Xu
2021-02-18  5:52                 ` [LTP] [PATCH v3 3/4] syscalls/mallinfo02: Add a basic test to check use mmap or sbrk Yang Xu
2021-02-22  6:32                   ` Li Wang
2021-02-22  6:40                     ` Yang Xu
2021-02-23  1:45                     ` Yang Xu
2021-02-18  5:52                 ` [LTP] [PATCH v3 4/4] syscalls/mallinfo03: Add an overflow test when setting 2G size Yang Xu
2021-01-28 15:50 ` [LTP] [PATCH v1 1/3] syscalls/mallinfo01: Add a basic test for mallinfo Cyril Hrubis
2021-02-03  6:03   ` Yang Xu
2021-02-04  6:42 ` Li Wang
2021-02-04  9:54   ` Yang Xu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.