Skip to contentSkip to conversation

CGroup API rewrite

PATCH Superseded Richard Palethorpe 4 participants 22 messages Apr 12, 2021 – Apr 27, 2021 Library code

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v3 cover CGroup API rewrite

Hello,

This is a complete rewrite of the CGroups API. It has big changes even compared to V2 of the patchset. To understand why this is so complicated, please see the commments in tst_cgroup.h and tst_cgroup.c.

V3:

* Replaced the object API with a string based lookup.

* Replaced tst_cgroup_css struct and name mapping functions with an item info tree.

* Merged the header files again as there is no longer much seperation between the core and item parts of the library.

* Rename some variables and functions to make them more consistent.

V2:

* Created an object (Item) API which looks a bit like the unified V2 hierarchy. The implementation is quite verbose, but not complicated IMO.

* Add the ability to extend the LTP CGroup hierarchy with child groups. We already have a reproducer that requires such a hierarchy, but I have not had chance to turn it into a test case yet.

* Add documentation for the new API in test-writing-guidelines.txt.

* Convert madvise06 to the CGroups API

* Better error reporting for the *at functions. Add tst_decode_fd which tries to print the path an FD was opened with.

TODO/NOTES:

* There are other tests which mount CGroups in an ad-hoc way and need to be converted to the new API. This at least includes memcg_test_3 and maybe cgroup_xattr.

Richard Palethorpe (7):
API: Add safe openat, printfat, readat and unlinkat
API: Add macro for the container_of trick Add new CGroups APIs
Add new CGroups API library tests
docs: Update CGroups API
mem: Convert tests to new CGroups API
madvise06: Convert to new CGroups API

 doc/test-writing-guidelines.txt               |  175 ++-
 include/tst_cgroup.h                          |  194 ++-
 include/tst_common.h                          |    5 +
 include/tst_safe_file_ops.h                   |   39 +
 include/tst_test.h                            |    1 -
 lib/Makefile                                  |    2 +
 lib/newlib_tests/.gitignore                   |    2 +
 lib/newlib_tests/test21.c                     |   45 +-
 lib/newlib_tests/tst_cgroup01.c               |   51 +
 lib/newlib_tests/tst_cgroup02.c               |   90 ++
 lib/tst_cgroup.c                              | 1205 ++++++++++++-----
 lib/tst_safe_file_ops.c                       |  171 +++
 testcases/kernel/mem/cpuset/cpuset01.c        |   34 +-
 testcases/kernel/mem/include/mem.h            |    2 +-
 testcases/kernel/mem/ksm/ksm02.c              |   13 +-
 testcases/kernel/mem/ksm/ksm03.c              |   12 +-
 testcases/kernel/mem/ksm/ksm04.c              |   17 +-
 testcases/kernel/mem/lib/mem.c                |   10 +-
 testcases/kernel/mem/oom/oom03.c              |   18 +-
 testcases/kernel/mem/oom/oom04.c              |   19 +-
 testcases/kernel/mem/oom/oom05.c              |   32 +-
 testcases/kernel/syscalls/madvise/madvise06.c |   82 +-
 22 files changed, 1720 insertions(+), 499 deletions(-)
 create mode 100644 lib/newlib_tests/tst_cgroup01.c
 create mode 100644 lib/newlib_tests/tst_cgroup02.c
 create mode 100644 lib/tst_safe_file_ops.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v3 1/7 API: Add safe openat, printfat, readat and unlinkat

Add 'at' variants for a number of system calls and LTP SAFE API functions. This avoids using sprintf everywhere to build paths.

Also adds tst_decode_fd which allows us to retrieve the path for an FD for debugging purposes without having to store it ourselves. However the proc symlink may not be available on some systems.

---
 include/tst_safe_file_ops.h |  39 ++++++++
 lib/tst_safe_file_ops.c     | 171 ++++++++++++++++++++++++++++++++++++
 2 files changed, 210 insertions(+)
 create mode 100644 lib/tst_safe_file_ops.c
include/tst_safe_file_ops.h +39 -0
index 223eddd1f..dff6a793c 100644
@@ -57,4 +57,43 @@
 #define TST_MOUNT_OVERLAY() \
 	(mount_overlay(__FILE__, __LINE__, 0) == 0)
 
+#define SAFE_OPENAT(dirfd, path, oflags, ...)			\
+	safe_openat(__FILE__, __LINE__,				\
+		    (dirfd), (path), (oflags), ## __VA_ARGS__)
+
+#define SAFE_FILE_READAT(dirfd, path, buf, nbyte)			\
+	safe_file_readat(__FILE__, __LINE__,				\
+			 (dirfd), (path), (buf), (nbyte))
+
+
+#define SAFE_FILE_PRINTFAT(dirfd, path, fmt, ...)			\
+	safe_file_printfat(__FILE__, __LINE__,				\
+			   (dirfd), (path), (fmt), __VA_ARGS__)
+
+#define SAFE_UNLINKAT(dirfd, path, flags)				\
+	safe_unlinkat(__FILE__, __LINE__, (dirfd), (path), (flags))
+
+char *tst_decode_fd(int fd);
+
+int safe_openat(const char *file, const int lineno,
+		int dirfd, const char *path, int oflags, ...);
+
+ssize_t safe_file_readat(const char *file, const int lineno,
+			 int dirfd, const char *path, char *buf, size_t nbyte);
+
+int tst_file_vprintfat(int dirfd, const char *path, const char *fmt, va_list va);
+int tst_file_printfat(int dirfd, const char *path, const char *fmt, ...)
+			__attribute__ ((format (printf, 3, 4)));
+
+int safe_file_vprintfat(const char *file, const int lineno,
+			int dirfd, const char *path,
+			const char *fmt, va_list va);
+
+int safe_file_printfat(const char *file, const int lineno,
+		       int dirfd, const char *path, const char *fmt, ...)
+			__attribute__ ((format (printf, 5, 6)));
+
+int safe_unlinkat(const char *file, const int lineno,
+		  int dirfd, const char *path, int flags);
+
 #endif /* TST_SAFE_FILE_OPS */
lib/tst_safe_file_ops.c new file +171 -0

171 lines not on this page · raw message

Signature · 1 line
2.30.2
#2 lore raw

PATCH v3 1/7 API: Add safe openat, printfat, readat and unlinkat

Hi Richard,

Quoted 29 lines · Richard Palethorpe

Add 'at' variants for a number of system calls and LTP SAFE API functions. This avoids using sprintf everywhere to build paths.

Also adds tst_decode_fd which allows us to retrieve the path for an FD for debugging purposes without having to store it ourselves. However the proc symlink may not be available on some systems.

---
 include/tst_safe_file_ops.h |  39 ++++++++
 lib/tst_safe_file_ops.c     | 171 ++++++++++++++++++++++++++++++++++++
 2 files changed, 210 insertions(+)
 create mode 100644 lib/tst_safe_file_ops.c
include/tst_safe_file_ops.h +7 -0
index 223eddd1f..dff6a793c 100644
@@ -57,4 +57,43 @@
 #define TST_MOUNT_OVERLAY() \
        (mount_overlay(__FILE__, __LINE__, 0) == 0)
 
+#define SAFE_OPENAT(dirfd, path, oflags, ...)                  \
+       safe_openat(__FILE__, __LINE__,                         \
+                   (dirfd), (path), (oflags), ## __VA_ARGS__)
+
+#define SAFE_FILE_READAT(dirfd, path, buf, nbyte)                      \
+       safe_file_readat(__FILE__, __LINE__,                            \
+                        (dirfd), (path), (buf), (nbyte))

include/tst_safe_file_ops.h line 76

Quoted 10 lines
include/tst_safe_file_ops.h +9 -0
+
+
+#define SAFE_FILE_PRINTFAT(dirfd, path, fmt, ...)                      \
+       safe_file_printfat(__FILE__, __LINE__,                          \
+                          (dirfd), (path), (fmt), __VA_ARGS__)
+
+#define SAFE_UNLINKAT(dirfd, path, flags)                              \
+       safe_unlinkat(__FILE__, __LINE__, (dirfd), (path), (flags))
+
 

The above macros are suggested to leave in this "tst_safe_file_ops.h" file.

But, the function prototypes below should be moved to "safe_file_ops_fn.h", because that purposely to separate macros and function in different places. (I remember I had commented this in V2, probably you were missing it:)

Quoted 14 lines
include/tst_safe_file_ops.h +7 -0
+char *tst_decode_fd(int fd);
+
+int safe_openat(const char *file, const int lineno,
+               int dirfd, const char *path, int oflags, ...);
+
+ssize_t safe_file_readat(const char *file, const int lineno,
+                        int dirfd, const char *path, char *buf, size_t

nbyte);

include/tst_safe_file_ops.h +2 -0
+
+int tst_file_vprintfat(int dirfd, const char *path, const char *fmt,

va_list va);

include/tst_safe_file_ops.h +3 -0
+int tst_file_printfat(int dirfd, const char *path, const char *fmt, ...)
+                       __attribute__ ((format (printf, 3, 4)));
+

include/tst_safe_file_ops.h line 97

Quoted 10 lines
include/tst_safe_file_ops.h +10 -0
+int safe_file_vprintfat(const char *file, const int lineno,
+                       int dirfd, const char *path,
+                       const char *fmt, va_list va);
+
+int safe_file_printfat(const char *file, const int lineno,
+                      int dirfd, const char *path, const char *fmt, ...)
+                       __attribute__ ((format (printf, 5, 6)));
+
+int safe_unlinkat(const char *file, const int lineno,
+                 int dirfd, const char *path, int flags);

+

lib/tst_safe_file_ops.c

Quoted 11 lines

#endif /* TST_SAFE_FILE_OPS */

lib/tst_safe_file_ops.c new file +0 -0
new file mode 100644
index 000000000..af4157476

And, we'd better achieve all the functions in "lib/safe_file_ops.c" but not create a separate new C file.

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

PATCH v3 1/7 API: Add safe openat, printfat, readat and unlinkat

Hello Li,

Quoted 39 lines · Li Wang

Hi Richard,

Quoted 34 lines · Richard Palethorpe

Add 'at' variants for a number of system calls and LTP SAFE API functions. This avoids using sprintf everywhere to build paths.

Also adds tst_decode_fd which allows us to retrieve the path for an FD for debugging purposes without having to store it ourselves. However the proc symlink may not be available on some systems.

---
 include/tst_safe_file_ops.h |  39 ++++++++
 lib/tst_safe_file_ops.c     | 171 ++++++++++++++++++++++++++++++++++++
 2 files changed, 210 insertions(+)
 create mode 100644 lib/tst_safe_file_ops.c
include/tst_safe_file_ops.h +12 -0
index 223eddd1f..dff6a793c 100644
@@ -57,4 +57,43 @@
 #define TST_MOUNT_OVERLAY() \
        (mount_overlay(__FILE__, __LINE__, 0) == 0)
 
+#define SAFE_OPENAT(dirfd, path, oflags, ...)                  \
+       safe_openat(__FILE__, __LINE__,                         \
+                   (dirfd), (path), (oflags), ## __VA_ARGS__)
+
+#define SAFE_FILE_READAT(dirfd, path, buf, nbyte)                      \
+       safe_file_readat(__FILE__, __LINE__,                            \
+                        (dirfd), (path), (buf), (nbyte))
+
+
+#define SAFE_FILE_PRINTFAT(dirfd, path, fmt, ...)                      \
+       safe_file_printfat(__FILE__, __LINE__,                          \
+                          (dirfd), (path), (fmt), __VA_ARGS__)
Quoted 8 lines

include/tst_safe_file_ops.h

Quoted 5 lines
include/tst_safe_file_ops.h +4 -0
+
+#define SAFE_UNLINKAT(dirfd, path, flags)                              \
+       safe_unlinkat(__FILE__, __LINE__, (dirfd), (path), (flags))
+
 

The above macros are suggested to leave in this "tst_safe_file_ops.h" file.

I think this is just for legacy reasons. To separate new and old API function definitions. These *at functions will never be in the old API though.

Quoted 5 lines

But, the function prototypes below should be moved to "safe_file_ops_fn.h", because that purposely to separate macros and function in different places. (I remember I had commented this in V2, probably you were missing it:)

Probably the best thing to do is create tst_safe_file_at.{c,h} and move these functions there. This would be more consistent with the new API.

I think I was trying to think of a better solution, but then forgot about it.

Quoted 33 lines
Quoted 16 lines
include/tst_safe_file_ops.h +7 -0
+char *tst_decode_fd(int fd);
+
+int safe_openat(const char *file, const int lineno,
+               int dirfd, const char *path, int oflags, ...);
+
+ssize_t safe_file_readat(const char *file, const int lineno,
+                        int dirfd, const char *path, char *buf, size_t

nbyte);

include/tst_safe_file_ops.h +2 -0
+
+int tst_file_vprintfat(int dirfd, const char *path, const char *fmt,

va_list va);

include/tst_safe_file_ops.h +3 -0
+int tst_file_printfat(int dirfd, const char *path, const char *fmt, ...)
+                       __attribute__ ((format (printf, 3, 4)));
+

include/tst_safe_file_ops.h line 97

Quoted 10 lines
include/tst_safe_file_ops.h +10 -0
+int safe_file_vprintfat(const char *file, const int lineno,
+                       int dirfd, const char *path,
+                       const char *fmt, va_list va);
+
+int safe_file_printfat(const char *file, const int lineno,
+                      int dirfd, const char *path, const char *fmt, ...)
+                       __attribute__ ((format (printf, 5, 6)));
+
+int safe_unlinkat(const char *file, const int lineno,
+                 int dirfd, const char *path, int flags);

+

Quoted 2 lines

#endif /* TST_SAFE_FILE_OPS */

Quoted 9 lines

lib/tst_safe_file_ops.c

Quoted 6 lines
lib/tst_safe_file_ops.c new file +0 -0
new file mode 100644
index 000000000..af4157476

And, we'd better achieve all the functions in "lib/safe_file_ops.c" but not create a separate new C file.

This would mean they all have a useless argument for the old API.

Signature · 2 lines
Thank you,
Richard.
#4 lore raw

PATCH v3 2/7 API: Add macro for the container_of trick

In reply to Richard Palethorpe
---
 include/tst_common.h | 5 +++++
 1 file changed, 5 insertions(+)
include/tst_common.h +5 -0
index fd7a900d4..317925d1d 100644
@@ -83,4 +83,9 @@
 #define TST_RES_SUPPORTS_TCONF_TFAIL_TINFO_TPASS_TWARN(condition) \
 	TST_BUILD_BUG_ON(condition)
 
+#define tst_container_of(ptr, type, member) ({		     \
+	const typeof( ((type *)0)->member ) *__mptr = (ptr); \
+	(type *)( (char *)__mptr - offsetof(type,member) );  \
+})
+
 #endif /* TST_COMMON_H__ */
Signature · 1 line
2.30.2
#5 lore raw

PATCH v3 2/7 API: Add macro for the container_of trick

Richard Palethorpe <[email protected]> wrote:

Quoted 1 line
Quoted 9 lines
---
 include/tst_common.h | 5 +++++
 1 file changed, 5 insertions(+)
include/tst_common.h +0 -0
index fd7a900d4..317925d1d 100644

include/tst_common.h line 90

Quoted 8 lines
include/tst_common.h +4 -0
@@ -83,4 +83,9 @@
 #define TST_RES_SUPPORTS_TCONF_TFAIL_TINFO_TPASS_TWARN(condition) \
        TST_BUILD_BUG_ON(condition)
 
+#define tst_container_of(ptr, type, member) ({              \
+       const typeof( ((type *)0)->member ) *__mptr = (ptr); \
+       (type *)( (char *)__mptr - offsetof(type,member) );  \
+})
 

I'd suggest defining it as uppercase 'TST_CONTAINER_OF(...)' to respect other macro's naming policy in tst_common.h.

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

PATCH v3 2/7 API: Add macro for the container_of trick

Hi,

Quoted 19 lines · Li Wang

Richard Palethorpe <[email protected]> wrote:

Quoted 1 line
Quoted 12 lines
---
 include/tst_common.h | 5 +++++
 1 file changed, 5 insertions(+)
include/tst_common.h +0 -0
index fd7a900d4..317925d1d 100644
@@ -83,4 +83,9 @@
 #define TST_RES_SUPPORTS_TCONF_TFAIL_TINFO_TPASS_TWARN(condition) \
        TST_BUILD_BUG_ON(condition)
 
Quoted 8 lines

include/tst_common.h

Quoted 5 lines
include/tst_common.h +4 -0
+#define tst_container_of(ptr, type, member) ({              \
+       const typeof( ((type *)0)->member ) *__mptr = (ptr); \
+       (type *)( (char *)__mptr - offsetof(type,member) );  \
+})
 

I'd suggest defining it as uppercase 'TST_CONTAINER_OF(...)' to respect other macro's naming policy in tst_common.h.

I don't mind either way. I suspect it is lower case to match offsetof and maybe it is expected to become a compiler intrinsic. Perhaps we should remove the tst_. WDYT Cyril??

Signature · 2 lines
Thank you,
Richard.
#7 lore raw

PATCH v3 2/7 API: Add macro for the container_of trick

Hi!

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
---
 include/tst_common.h | 5 +++++
 1 file changed, 5 insertions(+)
include/tst_common.h +0 -0
index fd7a900d4..317925d1d 100644
@@ -83,4 +83,9 @@
 #define TST_RES_SUPPORTS_TCONF_TFAIL_TINFO_TPASS_TWARN(condition) \
        TST_BUILD_BUG_ON(condition)
 
Quoted 8 lines

include/tst_common.h

Quoted 5 lines
include/tst_common.h +4 -0
+#define tst_container_of(ptr, type, member) ({              \
+       const typeof( ((type *)0)->member ) *__mptr = (ptr); \
+       (type *)( (char *)__mptr - offsetof(type,member) );  \
+})
 

I'd suggest defining it as uppercase 'TST_CONTAINER_OF(...)' to respect other macro's naming policy in tst_common.h.

Quoted 3 lines

I don't mind either way. I suspect it is lower case to match offsetof and maybe it is expected to become a compiler intrinsic. Perhaps we should remove the tst_. WDYT Cyril??

I think that it makes sense to use the same name as in the Linux kernel since the macro is well known there. I would go just with container_of() in this case.

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

PATCH v3 3/7 Add new CGroups APIs

In reply to Richard Palethorpe

Complete rewrite of the CGroups API which provides two layers of indirection between the test author and the SUT's CGroup configuration.

---
 include/tst_cgroup.h |  194 ++++++-
 include/tst_test.h   |    1 -
 lib/Makefile         |    2 +
 lib/tst_cgroup.c     | 1205 +++++++++++++++++++++++++++++++-----------
 4 files changed, 1056 insertions(+), 346 deletions(-)
include/tst_cgroup.h +171 -23
index bfd848260..d6842d641 100644
@@ -2,46 +2,194 @@
 /*
  * Copyright (c) 2020 Red Hat, Inc.
  * Copyright (c) 2020 Li Wang <[email protected]>
+ * Copyright (c) 2020-2021 SUSE LLC <[email protected]>
  */
+/*\
+ * [DESCRIPTION]
+ *
+ * The LTP CGroups API tries to present a consistent interface to the
+ * many possible CGroup configurations a system could have.
+ *
+ * You may ask; "Why don't you just mount a simple CGroup hierarchy,
+ * instead of scanning the current setup?". The short answer is that
+ * it is not possible unless no CGroups are currently active and
+ * almost all of our users will have CGroups active. Even if
+ * unmounting the current CGroup hierarchy is a reasonable thing to do
+ * to the sytem manager, it is highly unlikely the CGroup hierarchy
+ * will be destroyed. So users would be forced to remove their CGroup
+ * configuration and reboot the system.
+ *
+ * The core library tries to ensure an LTP CGroup exists on each
+ * hierarchy root. Inside the LTP group it ensures a 'drain' group
+ * exists and creats a test group for the current test. In the worst
+ * case we end up with a set of hierarchies like the follwoing. Where
+ * existing system-manager-created CGroups have been omitted.
+ *
+ * 	(V2 Root)	(V1 Root 1)	...	(V1 Root N)
+ * 	    |		     |			     |
+ *	  (ltp)		   (ltp)	...	   (ltp)
+ *	 /     \	  /	\		  /	\
+ *  (drain) (test-n) (drain)  (test-n)  ...  (drain)  (test-n)
+ *
+ * V2 CGroup controllers use a single unified hierarchy on a single
+ * root. Two or more V1 controllers may share a root or have their own
+ * root. However there may exist only one instance of a controller.
+ * So you can not have the same V1 controller on multiple roots.
+ *
+ * It is possible to have both a V2 hierarchy and V1 hierarchies
+ * active at the same time. Which is what is shown above. Any
+ * controllers attached to V1 hierarchies will not be available in the
+ * V2 hierarchy. The reverse is also true.
+ *
+ * Note that a single hierarchy may be mounted multiple
+ * times. Allowing it to be accessed at different locations. However
+ * subsequent mount operations will fail if the mount options are
+ * different from the first.
+ *
+ * The user may pre-create the CGroup hierarchies and the ltp CGroup,
+ * otherwise the library will try to create them. If the ltp group
+ * already exists and has appropriate permissions, then admin
+ * privileges will not be required to run the tests.
+ *
+ * Because the test may not have access to the CGroup root(s), the
+ * drain CGroup is created. This can be used to store processes which
+ * would otherwise block the destruction of the individual test CGroup
+ * or one of its descendants.
+ *
+ * The test author may create child CGroups within the test CGroup
+ * using the CGroup Item API. The library will create the new CGroup
+ * in all the relevant hierarchies.
+ *
+ * There are many differences between the V1 and V2 CGroup APIs. If a
+ * controller is on both V1 and V2, it may have different parameters
+ * and control files. Some of these control files have a different
+ * name, but similar functionality. In this case the Item API uses
+ * the V2 names and aliases them to the V1 name when appropriate.
+ *
+ * Some control files only exist on one of the versions or they can be
+ * missing due to other reasons. The Item API allows the user to check
+ * if the file exists before trying to use it.
+ *
+ * Often a control file has almost the same functionality between V1
+ * and V2. Which means it can be used in the same way most of the
+ * time, but not all. For now this is handled by exposing the API
+ * version a controller is using to allow the test author to handle
+ * edge cases. (e.g. V2 memory.swap.max accepts "max", but V1
+ * memory.memsw.limit_in_bytes does not).
+\*/
 
 #ifndef TST_CGROUP_H
 #define TST_CGROUP_H
 
-#define PATH_TMP_CG_MEM	"/tmp/cgroup_mem"
-#define PATH_TMP_CG_CST	"/tmp/cgroup_cst"
-
+/* CGroups Kernel API version */
 enum tst_cgroup_ver {
 	TST_CGROUP_V1 = 1,
 	TST_CGROUP_V2 = 2,
 };
 
-enum tst_cgroup_ctrl {
-	TST_CGROUP_MEMCG = 1,
+/* Controller sub-systems */
+enum tst_cgroup_css {
+	TST_CGROUP_MEMORY = 1,
 	TST_CGROUP_CPUSET = 2,
-	/* add cgroup controller */
 };
+#define TST_CGROUP_MAX TST_CGROUP_CPUSET
+
+/* At most we can have one cgroup V1 tree for each controller and one
+ * (empty) v2 tree.
+ */
+#define TST_CGROUP_MAX_TREES (TST_CGROUP_MAX + 1)
+
+
+/* Used to specify CGroup hierarchy configuration options, allowing a
+ * test to request a particular CGroup structure.
+ */
+struct tst_cgroup_opts {
+	/* Only try to mount V1 CGroup controllers. This will not
+	 * prevent V2 from being used if it is already mounted, it
+	 * only indicates that we should mount V1 controllers if
+	 * nothing is present. By default we try to mount V2 first. */
+	int only_mount_v1:1;
+};
+
+struct tst_cgroup_tree;
+
+
+/* A Control Group in LTP's aggregated hierarchy */
+struct tst_cgroup {
+	const char *name;
+	/* Maps controller ID to the tree which contains it. The V2
+	 * tree is at zero even if it contains no controllers.
+	 */
+	struct tst_cgroup_tree *trees_by_css[TST_CGROUP_MAX_TREES];
+	/* NULL terminated list of trees */
+	struct tst_cgroup_tree *trees[TST_CGROUP_MAX_TREES + 1];
+};
+
+/* Search the system for mounted cgroups and available
+ * controllers. Called automatically by tst_cgroup_require.
+ */
+void tst_cgroup_scan(void);
+/* Print the config detected by tst_cgroup_scan */
+void tst_cgroup_print_config(void);
+
+/* Ensure the specified controller is available in the test's default
+ * CGroup, mounting/enabling it if necessary */
+void tst_cgroup_require(enum tst_cgroup_css type,
+			const struct tst_cgroup_opts *options);
+
+/* Tear down any CGroups created by calls to tst_cgroup_require */
+void tst_cgroup_cleanup(void);
+
+/* Get the default CGroup for the test. It allocates memory (in a
+ * guarded buffer) so should always be called from setup
+ */
+const struct tst_cgroup *tst_cgroup_get_test(void);
+/* Get the shared drain group. Also should be called from setup */
+const struct tst_cgroup *tst_cgroup_get_drain(void);
+/* Create a descendant CGroup */
+struct tst_cgroup *tst_cgroup_mk(const struct tst_cgroup *parent,
+				 const char *name);
+/* Remove a descendant CGroup */
+struct tst_cgroup *tst_cgroup_rm(struct tst_cgroup *cg);
+
+#define SAFE_CGROUP_VER(cg, name) \
+	safe_cgroup_ver(__FILE__, __LINE__, (cg), (name))
+
+enum tst_cgroup_ver safe_cgroup_ver(const char *file, const int lineno,
+				    const struct tst_cgroup *cg,
+				    const char *name);
+
+#define SAFE_CGROUP_HAS(cg, name) \
+	safe_cgroup_has(__FILE__, __LINE__, (cg), (name))
+
+int safe_cgroup_has(const char *file, const int lineno,
+		    const struct tst_cgroup *cg, const char *name);
+
+#define SAFE_CGROUP_READ(cg, name, out, len)				\
+	safe_cgroup_read(__FILE__, __LINE__, (cg), (name), (out), (len))
+
+ssize_t safe_cgroup_read(const char *file, const int lineno,
+			 const struct tst_cgroup *cg, const char *name,
+			 char *out, size_t len);
 
-enum tst_cgroup_ver tst_cgroup_version(void);
+#define SAFE_CGROUP_PRINTF(cg, name, fmt, ...)				\
+	safe_cgroup_printf(__FILE__, __LINE__, (cg), (name), (fmt), __VA_ARGS__)
 
-/* To mount/umount specified cgroup controller on 'cgroup_dir' path */
-void tst_cgroup_mount(enum tst_cgroup_ctrl ctrl, const char *cgroup_dir);
-void tst_cgroup_umount(const char *cgroup_dir);
+#define SAFE_CGROUP_PRINT(cg, name, str)				\
+	safe_cgroup_printf(__FILE__, __LINE__, (cg), (name), "%s", (str))
 
-/* To move current process PID to the mounted cgroup tasks */
-void tst_cgroup_move_current(const char *cgroup_dir);
+void safe_cgroup_printf(const char *file, const int lineno,
+			const struct tst_cgroup *cg, const char *name,
+			const char *fmt, ...)
+			__attribute__ ((format (printf, 5, 6)));
 
-/* To set cgroup controller knob with new value */
-void tst_cgroup_set_knob(const char *cgroup_dir, const char *knob, long value);
+#define SAFE_CGROUP_SCANF(cg, name, fmt, ...)				\
+	safe_cgroup_scanf(__FILE__, __LINE__, (cg), (name), (fmt), __VA_ARGS__)
 
-/* Set of functions to set knobs under the memory controller */
-void tst_cgroup_mem_set_maxbytes(const char *cgroup_dir, long memsz);
-int  tst_cgroup_mem_swapacct_enabled(const char *cgroup_dir);
-void tst_cgroup_mem_set_maxswap(const char *cgroup_dir, long memsz);
+void safe_cgroup_scanf(const char *file, const int lineno,
+		       const struct tst_cgroup *cg, const char *name,
+		       const char *fmt, ...)
+		       __attribute__ ((format (scanf, 5, 6)));
 
-/* Set of functions to read/write cpuset controller files content */
-void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename,
-	char *retbuf, size_t retbuf_sz);
-void tst_cgroup_cpuset_write_files(const char *cgroup_dir, const char *filename,
-	const char *buf);
 
 #endif /* TST_CGROUP_H */
include/tst_test.h +0 -1
index 1fbebe752..62ab2981f 100644
@@ -39,7 +39,6 @@
 #include "tst_capability.h"
 #include "tst_hugepage.h"
 #include "tst_assert.h"
-#include "tst_cgroup.h"
 #include "tst_lockdown.h"
 #include "tst_fips.h"
 #include "tst_taint.h"
lib/Makefile +2 -0
index f019432e8..6f641ee9a 100644
@@ -38,6 +38,8 @@ pc_file			:= $(DESTDIR)/$(datarootdir)/pkgconfig/ltp.pc
 
 INSTALL_TARGETS		:= $(pc_file)
 
+tst_cgroup.o: CFLAGS += -Wno-missing-field-initializers
+
 $(pc_file):
 	test -d "$(@D)" || mkdir -p "$(@D)"
 	install -m $(INSTALL_MODE) "$(builddir)/$(@F)" "$@"
lib/tst_cgroup.c +883 -322

1,336 lines not on this page · raw message

Signature · 1 line
2.30.2
#9 lore raw

PATCH v3 3/7 Add new CGroups APIs

Hi!

Quoted 3 lines

Complete rewrite of the CGroups API which provides two layers of indirection between the test author and the SUT's CGroup configuration.

I've spend quite some time reading the code and I think that the generall idea and implementation is good.

There is quite a bit of minor details that I think may be done better, see comments.

Quoted 102 lines
---
 include/tst_cgroup.h |  194 ++++++-
 include/tst_test.h   |    1 -
 lib/Makefile         |    2 +
 lib/tst_cgroup.c     | 1205 +++++++++++++++++++++++++++++++-----------
 4 files changed, 1056 insertions(+), 346 deletions(-)
include/tst_cgroup.h +75 -3
index bfd848260..d6842d641 100644
@@ -2,46 +2,194 @@
 /*
  * Copyright (c) 2020 Red Hat, Inc.
  * Copyright (c) 2020 Li Wang <[email protected]>
+ * Copyright (c) 2020-2021 SUSE LLC <[email protected]>
  */
+/*\
+ * [DESCRIPTION]
+ *
+ * The LTP CGroups API tries to present a consistent interface to the
+ * many possible CGroup configurations a system could have.
+ *
+ * You may ask; "Why don't you just mount a simple CGroup hierarchy,
+ * instead of scanning the current setup?". The short answer is that
+ * it is not possible unless no CGroups are currently active and
+ * almost all of our users will have CGroups active. Even if
+ * unmounting the current CGroup hierarchy is a reasonable thing to do
+ * to the sytem manager, it is highly unlikely the CGroup hierarchy
+ * will be destroyed. So users would be forced to remove their CGroup
+ * configuration and reboot the system.
+ *
+ * The core library tries to ensure an LTP CGroup exists on each
+ * hierarchy root. Inside the LTP group it ensures a 'drain' group
+ * exists and creats a test group for the current test. In the worst
+ * case we end up with a set of hierarchies like the follwoing. Where
+ * existing system-manager-created CGroups have been omitted.
+ *
+ * 	(V2 Root)	(V1 Root 1)	...	(V1 Root N)
+ * 	    |		     |			     |
+ *	  (ltp)		   (ltp)	...	   (ltp)
+ *	 /     \	  /	\		  /	\
+ *  (drain) (test-n) (drain)  (test-n)  ...  (drain)  (test-n)
+ *
+ * V2 CGroup controllers use a single unified hierarchy on a single
+ * root. Two or more V1 controllers may share a root or have their own
+ * root. However there may exist only one instance of a controller.
+ * So you can not have the same V1 controller on multiple roots.
+ *
+ * It is possible to have both a V2 hierarchy and V1 hierarchies
+ * active at the same time. Which is what is shown above. Any
+ * controllers attached to V1 hierarchies will not be available in the
+ * V2 hierarchy. The reverse is also true.
+ *
+ * Note that a single hierarchy may be mounted multiple
+ * times. Allowing it to be accessed at different locations. However
+ * subsequent mount operations will fail if the mount options are
+ * different from the first.
+ *
+ * The user may pre-create the CGroup hierarchies and the ltp CGroup,
+ * otherwise the library will try to create them. If the ltp group
+ * already exists and has appropriate permissions, then admin
+ * privileges will not be required to run the tests.
+ *
+ * Because the test may not have access to the CGroup root(s), the
+ * drain CGroup is created. This can be used to store processes which
+ * would otherwise block the destruction of the individual test CGroup
+ * or one of its descendants.
+ *
+ * The test author may create child CGroups within the test CGroup
+ * using the CGroup Item API. The library will create the new CGroup
+ * in all the relevant hierarchies.
+ *
+ * There are many differences between the V1 and V2 CGroup APIs. If a
+ * controller is on both V1 and V2, it may have different parameters
+ * and control files. Some of these control files have a different
+ * name, but similar functionality. In this case the Item API uses
+ * the V2 names and aliases them to the V1 name when appropriate.
+ *
+ * Some control files only exist on one of the versions or they can be
+ * missing due to other reasons. The Item API allows the user to check
+ * if the file exists before trying to use it.
+ *
+ * Often a control file has almost the same functionality between V1
+ * and V2. Which means it can be used in the same way most of the
+ * time, but not all. For now this is handled by exposing the API
+ * version a controller is using to allow the test author to handle
+ * edge cases. (e.g. V2 memory.swap.max accepts "max", but V1
+ * memory.memsw.limit_in_bytes does not).
+\*/
 
 #ifndef TST_CGROUP_H
 #define TST_CGROUP_H
 
-#define PATH_TMP_CG_MEM	"/tmp/cgroup_mem"
-#define PATH_TMP_CG_CST	"/tmp/cgroup_cst"
-
+/* CGroups Kernel API version */
 enum tst_cgroup_ver {
 	TST_CGROUP_V1 = 1,
 	TST_CGROUP_V2 = 2,

include/tst_cgroup.h line 94

Quoted 10 lines
include/tst_cgroup.h +3 -3
 };
 
-enum tst_cgroup_ctrl {
-	TST_CGROUP_MEMCG = 1,
+/* Controller sub-systems */
+enum tst_cgroup_css {
+	TST_CGROUP_MEMORY = 1,
 	TST_CGROUP_CPUSET = 2,
-	/* add cgroup controller */
 };

I spend a bit of time figuring out what css means, can't we just use controler in the code instead? It's a bit longer but more obvious.

Or is this consitent with kernel naming?

Quoted 8 lines
include/tst_cgroup.h +8 -0
+#define TST_CGROUP_MAX TST_CGROUP_CPUSET
+
+/* At most we can have one cgroup V1 tree for each controller and one
+ * (empty) v2 tree.
+ */
+#define TST_CGROUP_MAX_TREES (TST_CGROUP_MAX + 1)
+
+

include/tst_cgroup.h line 112

Quoted 10 lines
include/tst_cgroup.h +10 -0
+/* Used to specify CGroup hierarchy configuration options, allowing a
+ * test to request a particular CGroup structure.
+ */
+struct tst_cgroup_opts {
+	/* Only try to mount V1 CGroup controllers. This will not
+	 * prevent V2 from being used if it is already mounted, it
+	 * only indicates that we should mount V1 controllers if
+	 * nothing is present. By default we try to mount V2 first. */
+	int only_mount_v1:1;
+};

Do we need to pass the flags in a structure?

This is not an API that has to be future proof, when we find out we need more than a few bitflags we can always change it.

include/tst_cgroup.h line 126

Quoted 13 lines
include/tst_cgroup.h +13 -0
+struct tst_cgroup_tree;
+
+
+/* A Control Group in LTP's aggregated hierarchy */
+struct tst_cgroup {
+	const char *name;
+	/* Maps controller ID to the tree which contains it. The V2
+	 * tree is at zero even if it contains no controllers.
+	 */
+	struct tst_cgroup_tree *trees_by_css[TST_CGROUP_MAX_TREES];
+	/* NULL terminated list of trees */
+	struct tst_cgroup_tree *trees[TST_CGROUP_MAX_TREES + 1];
+};

So this is an array of directories in different trees, can we please name the strucuture better. What about tst_cgroup_nodes or tst_cgroup_dirs?

include/tst_cgroup.h line 138

Quoted 11 lines
include/tst_cgroup.h +11 -0
+/* Search the system for mounted cgroups and available
+ * controllers. Called automatically by tst_cgroup_require.
+ */
+void tst_cgroup_scan(void);
+/* Print the config detected by tst_cgroup_scan */
+void tst_cgroup_print_config(void);
+
+/* Ensure the specified controller is available in the test's default
+ * CGroup, mounting/enabling it if necessary */
+void tst_cgroup_require(enum tst_cgroup_css type,
+			const struct tst_cgroup_opts *options);

This is the only function in the API that uses the enum cgroup_css. I think that it may as well be better if we just passed a string to this function as well and made the enum strictly internal.

I guess that we do not want to split the knob names so that we would have to pass the controller as enum and knob name to make the API consistently use the enum so this would be a better option.

include/tst_cgroup.h line 148

Quoted 9 lines
include/tst_cgroup.h +9 -0
+/* Tear down any CGroups created by calls to tst_cgroup_require */
+void tst_cgroup_cleanup(void);
+
+/* Get the default CGroup for the test. It allocates memory (in a
+ * guarded buffer) so should always be called from setup
+ */
+const struct tst_cgroup *tst_cgroup_get_test(void);
+/* Get the shared drain group. Also should be called from setup */
+const struct tst_cgroup *tst_cgroup_get_drain(void);

I think that these functions could be named better, it's mildly confusing, "get test what?".

Maybe tst_cgroup_get_test_group() ?

Or tst_cgroup_get(enum tst_cgroup_group group) ?

include/tst_cgroup.h line 151

Quoted 3 lines
include/tst_cgroup.h +3 -0
+/* Create a descendant CGroup */
+struct tst_cgroup *tst_cgroup_mk(const struct tst_cgroup *parent,
+				 const char *name);

The name parameter should probably be called node_name or dir_name, it could be easily confused with the name parameter rest of the function use to describe a "controller.knob" pair.

include/tst_cgroup.h

Quoted 9 lines
include/tst_cgroup.h +8 -0
+/* Remove a descendant CGroup */
+struct tst_cgroup *tst_cgroup_rm(struct tst_cgroup *cg);
+
+#define SAFE_CGROUP_VER(cg, name) \
+	safe_cgroup_ver(__FILE__, __LINE__, (cg), (name))
 
+enum tst_cgroup_ver safe_cgroup_ver(const char *file, const int lineno,
+				    const struct tst_cgroup *cg,
+				    const char *name);

The 'name' parameter should really be controller_name or just controller.

Also I was a bit confused why version request may even fail.

I guess that it's fine if we tst_brk() on invalid controller name even without calling this function SAFE_*.

We haven't defined that anywhere but SAFE_FUNCTIONS() are things that may fail at runtime because of failures propagated from the operating system itself.

Quoted 33 lines
include/tst_cgroup.h +21 -8
+#define SAFE_CGROUP_HAS(cg, name) \
+	safe_cgroup_has(__FILE__, __LINE__, (cg), (name))
+
+int safe_cgroup_has(const char *file, const int lineno,
+		    const struct tst_cgroup *cg, const char *name);
+
+#define SAFE_CGROUP_READ(cg, name, out, len)				\
+	safe_cgroup_read(__FILE__, __LINE__, (cg), (name), (out), (len))
+
+ssize_t safe_cgroup_read(const char *file, const int lineno,
+			 const struct tst_cgroup *cg, const char *name,
+			 char *out, size_t len);
 
-enum tst_cgroup_ver tst_cgroup_version(void);
+#define SAFE_CGROUP_PRINTF(cg, name, fmt, ...)				\
+	safe_cgroup_printf(__FILE__, __LINE__, (cg), (name), (fmt), __VA_ARGS__)
 
-/* To mount/umount specified cgroup controller on 'cgroup_dir' path */
-void tst_cgroup_mount(enum tst_cgroup_ctrl ctrl, const char *cgroup_dir);
-void tst_cgroup_umount(const char *cgroup_dir);
+#define SAFE_CGROUP_PRINT(cg, name, str)				\
+	safe_cgroup_printf(__FILE__, __LINE__, (cg), (name), "%s", (str))
 
-/* To move current process PID to the mounted cgroup tasks */
-void tst_cgroup_move_current(const char *cgroup_dir);
+void safe_cgroup_printf(const char *file, const int lineno,
+			const struct tst_cgroup *cg, const char *name,
+			const char *fmt, ...)
+			__attribute__ ((format (printf, 5, 6)));
 
-/* To set cgroup controller knob with new value */
-void tst_cgroup_set_knob(const char *cgroup_dir, const char *knob, long value);
+#define SAFE_CGROUP_SCANF(cg, name, fmt, ...)				\

include/tst_cgroup.h line 192

Quoted 10 lines
include/tst_cgroup.h +5 -4
+	safe_cgroup_scanf(__FILE__, __LINE__, (cg), (name), (fmt), __VA_ARGS__)
 
-/* Set of functions to set knobs under the memory controller */
-void tst_cgroup_mem_set_maxbytes(const char *cgroup_dir, long memsz);
-int  tst_cgroup_mem_swapacct_enabled(const char *cgroup_dir);
-void tst_cgroup_mem_set_maxswap(const char *cgroup_dir, long memsz);
+void safe_cgroup_scanf(const char *file, const int lineno,
+		       const struct tst_cgroup *cg, const char *name,
+		       const char *fmt, ...)
+		       __attribute__ ((format (scanf, 5, 6)));

And for all the read/printf/scanf functions the name parameter should be named knob or something that describes that we are operating on individual files in a directory somewhere in the tree.

Quoted 24 lines
include/tst_cgroup.h +0 -5
-/* Set of functions to read/write cpuset controller files content */
-void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename,
-	char *retbuf, size_t retbuf_sz);
-void tst_cgroup_cpuset_write_files(const char *cgroup_dir, const char *filename,
-	const char *buf);
 
 #endif /* TST_CGROUP_H */
include/tst_test.h +0 -1
index 1fbebe752..62ab2981f 100644
@@ -39,7 +39,6 @@
 #include "tst_capability.h"
 #include "tst_hugepage.h"
 #include "tst_assert.h"
-#include "tst_cgroup.h"
 #include "tst_lockdown.h"
 #include "tst_fips.h"
 #include "tst_taint.h"
lib/Makefile +0 -0
index f019432e8..6f641ee9a 100644

lib/Makefile pc_file := $(DESTDIR)/$(datarootdir)/pkgconfig/ltp.pc · line 41

Quoted 4 lines
lib/Makefile +1 -0
@@ -38,6 +38,8 @@ pc_file			:= $(DESTDIR)/$(datarootdir)/pkgconfig/ltp.pc
 
 INSTALL_TARGETS		:= $(pc_file)
 
+tst_cgroup.o: CFLAGS += -Wno-missing-field-initializers

I'm not sure we want to work around warnings like this.

Quoted 50 lines
lib/Makefile +0 -0
 $(pc_file):
 	test -d "$(@D)" || mkdir -p "$(@D)"
 	install -m $(INSTALL_MODE) "$(builddir)/$(@F)" "$@"
lib/tst_cgroup.c +11 -15
index 96c9524d2..40c9a9bec 100644
@@ -2,453 +2,1014 @@
 /*
  * Copyright (c) 2020 Red Hat, Inc.
  * Copyright (c) 2020 Li Wang <[email protected]>
+ * Copyright (c) 2020-2021 SUSE LLC <[email protected]>
  */
 
 #define TST_NO_DEFAULT_MAIN
 
 #include <stdio.h>
+#include <stddef.h>
 #include <stdlib.h>
+#include <mntent.h>
 #include <sys/mount.h>
-#include <fcntl.h>
-#include <unistd.h>
 
 #include "tst_test.h"
-#include "tst_safe_macros.h"
-#include "tst_safe_stdio.h"
+#include "lapi/fcntl.h"
+#include "lapi/mount.h"
+#include "lapi/mkdirat.h"
 #include "tst_cgroup.h"
-#include "tst_device.h"
 
-static enum tst_cgroup_ver tst_cg_ver;
-static int clone_children;
+/* CGroup Core Implementation
+ *
+ * CGroup Item Implementation is towards the bottom.
+ */
 
-static int tst_cgroup_check(const char *cgroup)
-{
-	char line[PATH_MAX];
-	FILE *file;
-	int cg_check = 0;
+struct cgroup_root;
 
-	file = SAFE_FOPEN("/proc/filesystems", "r");
-	while (fgets(line, sizeof(line), file)) {
-		if (strstr(line, cgroup) != NULL) {

lib/tst_cgroup.c line 36

Quoted 10 lines
lib/tst_cgroup.c +8 -2
-			cg_check = 1;
-			break;
+/* A node in a single CGroup hierarchy. It exists mainly for
+ * convenience so that we do not have to traverse the CGroup structure
+ * for frequent operations.
+ *
+ * This is actually a single-linked list not a tree. We only need to
+ * traverse from leaf towards root.
+ */
+struct tst_cgroup_tree {

Why isn't this called node or dir? Either of these would be more fitting.

Also the tst_cgroup structure could use a better name, the tst_cgroup is actually an array of pointers to directories.

lib/tst_cgroup.c line 37

Quoted 1 line
lib/tst_cgroup.c +1 -0
+	const char *name;

This should be dir_name, can we please avoid having all string pointers called just 'name' I find it horribly confusing...

Quoted 5 lines
lib/tst_cgroup.c +5 -0
+	const struct tst_cgroup_tree *parent;
+
+	/* Shortcut to root */
+	const struct cgroup_root *root;
+

lib/tst_cgroup.c line 52

Quoted 10 lines
lib/tst_cgroup.c +10 -0
+	/* Subsystems (controllers) bit field. Only controllers which
+	 * were required and configured for this node are added to
+	 * this field. So it may be different from root->css_field.
+	 */
+	uint32_t css_field;
+
+	/* In general we avoid having sprintfs everywhere and use
+	 * openat, linkat, etc.
+	 */
+	int dir;

Can we name this dir_fd so it's obvious what it is?

lib/tst_cgroup.c line 61

Quoted 8 lines
lib/tst_cgroup.c +8 -0
+	int we_created_it:1;
+};
+
+/* The root of a CGroup hierarchy/tree */
+struct cgroup_root {
+	enum tst_cgroup_ver ver;
+	/* A mount path */
+	char path[PATH_MAX/2];

Why PATH_MAX/2 ?

Also why don't we call it mount_path[] instead of having a lousy comment that people have to look up when reading code?

Quoted 8 lines
lib/tst_cgroup.c +8 -0
+	/* Subsystems (controllers) bit field. Includes all
+	 * controllers found while scanningthis root.
+	 */
+	uint32_t css_field;
+
+	/* CGroup hierarchy: mnt -> ltp -> {drain, test -> ??? } We
+	 * keep a flat reference to ltp, drain and test for
+	 * convenience.

lib/tst_cgroup.c line 79

Quoted 10 lines
lib/tst_cgroup.c +10 -0
+	 */
+
+	/* Mount directory */
+	struct tst_cgroup_tree mnt;
+	/* LTP CGroup directory, contains drain and test dirs */
+	struct tst_cgroup_tree ltp;
+	/* Drain CGroup, see cgroup_cleanup */
+	struct tst_cgroup_tree drain;
+	/* CGroup for current test. Which may have children. */
+	struct tst_cgroup_tree test;

Here as well, it's sligtly easier to understand if we suffix the names with what it actually is. If we rename struct tst_cgroup_tree to tst_cgroup_node this should be ltp_node, drain_node, etc.

Quoted 63 lines
lib/tst_cgroup.c +60 -2
+	int we_mounted_it:1;
+	/* cpuset is in compatability mode */
+	int no_prefix:1;
+};
+
+/* Always use first item for unified hierarchy */
+struct cgroup_root roots[TST_CGROUP_MAX_TREES + 1];
+
+/* Describes some things that are part of a CGroup
+ *
+ * Usually trunk nodes are controllers and leaves are files exported
+ * by the controllers. Sometimes trunk nodes are components of a
+ * controller (e.g. memory.swap).
+ *
+ * The primary purpose of this is to map V2 names to V1
+ * names. Secondarily we can map name prefixes to controller IDs and
+ * figure out which hierarchy the item should be present on and
+ * whether the current configuration requires yet further work arounds
+ * (e.g. if cpuset is mounted in compatablity mode).
+ */
+struct cgroup_item {
+	/* Canonical name. Is the V2 name unless an item is V1 only */
+	const char *name;
+	/* V1 name or NULL if item is V2 only */
+	const char *name_v1;
+	/* Array of child items or NULL */
+	struct cgroup_item *inner;
+
+	/* The controller this item belongs to or zero for
+	 * 'cgroup.<item>'. Leaf items are statically initialised as
+	 * zero then set at runtime.
+	 */
+	enum tst_cgroup_css css_indx;
+
+	struct cgroup_root *root;
+
+	int we_require_it:1;
+};
+
+/* Lookup tree for item names. */
+typedef struct cgroup_item items_t[];
+static items_t items = {
+	[0] = { "cgroup", .inner = (items_t){
+			{ "cgroup.procs", "tasks" },
+			{ "cgroup.subtree_control" },
+			{ "cgroup.clone_children", "clone_children" },
+			{ NULL }
 		}
-	}
-	SAFE_FCLOSE(file);
+	},
+	[TST_CGROUP_MEMORY] = { "memory", .inner = (items_t){
+			{ "memory.current", "memory.usage_in_bytes" },
+			{ "memory.max", "memory.limit_in_bytes" },
+			{ "memory.swappiness", "memory.swappiness" },
+			{ "memory.swap.current", "memory.memsw.usage_in_bytes" },
+			{ "memory.swap.max", "memory.memsw.limit_in_bytes" },
+			{ "memory.kmem.usage_in_bytes", "memory.kmem.usage_in_bytes" },
+			{ "memory.kmem.limit_in_bytes", "memory.kmem.usage_in_bytes" },
+			{ NULL }
+		},
+	  .css_indx = TST_CGROUP_MEMORY
+	},

lib/tst_cgroup.c line 151

Quoted 10 lines
lib/tst_cgroup.c +10 -0
+	[TST_CGROUP_CPUSET] = { "cpuset", .inner = (items_t){
+			{ "cpuset.cpus", "cpuset.cpus" },
+			{ "cpuset.mems", "cpuset.mems" },
+			{ "cpuset.memory_migrate", "cpuset.memory_migrate" },
+			{ NULL }
+		},
+	  .css_indx = TST_CGROUP_CPUSET
+	},
+	{ NULL }
+};

Item is a very generic word, this is a list of known knobs and map between v1 and v2. So maybe cgroup_knob_map or just cgroup_knobs ?

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

PATCH v3 3/7 Add new CGroups APIs

Hello,

Quoted 10 lines · Cyril Hrubis

Hi!

Quoted 3 lines

Complete rewrite of the CGroups API which provides two layers of indirection between the test author and the SUT's CGroup configuration.

I've spend quite some time reading the code and I think that the generall idea and implementation is good.

There is quite a bit of minor details that I think may be done better, see comments.

Thanks for the review.

Quoted 22 lines
Quoted 8 lines
include/tst_cgroup.h +1 -3
-#define PATH_TMP_CG_MEM	"/tmp/cgroup_mem"
-#define PATH_TMP_CG_CST	"/tmp/cgroup_cst"
-
+/* CGroups Kernel API version */
 enum tst_cgroup_ver {
 	TST_CGROUP_V1 = 1,
 	TST_CGROUP_V2 = 2,

include/tst_cgroup.h line 94

Quoted 10 lines
include/tst_cgroup.h +3 -3
 };
 
-enum tst_cgroup_ctrl {
-	TST_CGROUP_MEMCG = 1,
+/* Controller sub-systems */
+enum tst_cgroup_css {
+	TST_CGROUP_MEMORY = 1,
 	TST_CGROUP_CPUSET = 2,
-	/* add cgroup controller */
 };

I spend a bit of time figuring out what css means, can't we just use controler in the code instead? It's a bit longer but more obvious.

Quoted 1 line

Or is this consitent with kernel naming?

The kernel seems to refer to controllers as subsystems and css appears to mean "CGroup subsystem". I'm not sure if subsystems are necessarily controllers, but it seems that way. Also "controller" has too many letters in it. Indeed it makes some function names very long and I found that harder to read.

I suppose to be totally consistent with the kernel we should randomly name some things css and others subsys. :-p

Quoted 22 lines
Quoted 9 lines
include/tst_cgroup.h +8 -0
+#define TST_CGROUP_MAX TST_CGROUP_CPUSET
+
+/* At most we can have one cgroup V1 tree for each controller and one
+ * (empty) v2 tree.
+ */
+#define TST_CGROUP_MAX_TREES (TST_CGROUP_MAX + 1)
+
+

include/tst_cgroup.h line 112

Quoted 10 lines
include/tst_cgroup.h +10 -0
+/* Used to specify CGroup hierarchy configuration options, allowing a
+ * test to request a particular CGroup structure.
+ */
+struct tst_cgroup_opts {
+	/* Only try to mount V1 CGroup controllers. This will not
+	 * prevent V2 from being used if it is already mounted, it
+	 * only indicates that we should mount V1 controllers if
+	 * nothing is present. By default we try to mount V2 first. */
+	int only_mount_v1:1;
+};

Do we need to pass the flags in a structure?

Quoted 2 lines

This is not an API that has to be future proof, when we find out we need more than a few bitflags we can always change it.

We may need to add xattr to this and there are many other options. However most tests will just have NULL or 0, 0... So we could move it into a vararg or have a tst_require2?

Quoted 10 lines
Quoted 10 lines
include/tst_cgroup.h +9 -0
+struct tst_cgroup_tree;
+
+
+/* A Control Group in LTP's aggregated hierarchy */
+struct tst_cgroup {
+	const char *name;
+	/* Maps controller ID to the tree which contains it. The V2
+	 * tree is at zero even if it contains no controllers.
+	 */
Quoted 8 lines

include/tst_cgroup.h line 126

Quoted 4 lines
include/tst_cgroup.h +4 -0
+	struct tst_cgroup_tree *trees_by_css[TST_CGROUP_MAX_TREES];
+	/* NULL terminated list of trees */
+	struct tst_cgroup_tree *trees[TST_CGROUP_MAX_TREES + 1];
+};

So this is an array of directories in different trees, can we please name the strucuture better. What about tst_cgroup_nodes or tst_cgroup_dirs?

I guess tst_cgroup_tree always represents a directory. So I would go with tst_cgroup_dir.

Quoted 17 lines

include/tst_cgroup.h line 138

Quoted 12 lines
include/tst_cgroup.h +11 -0
+/* Search the system for mounted cgroups and available
+ * controllers. Called automatically by tst_cgroup_require.
+ */
+void tst_cgroup_scan(void);
+/* Print the config detected by tst_cgroup_scan */
+void tst_cgroup_print_config(void);
+
+/* Ensure the specified controller is available in the test's default
+ * CGroup, mounting/enabling it if necessary */
+void tst_cgroup_require(enum tst_cgroup_css type,
+			const struct tst_cgroup_opts *options);

This is the only function in the API that uses the enum cgroup_css. I think that it may as well be better if we just passed a string to this function as well and made the enum strictly internal.

Quoted 3 lines

I guess that we do not want to split the knob names so that we would have to pass the controller as enum and knob name to make the API consistently use the enum so this would be a better option.

+1

Quoted 14 lines

include/tst_cgroup.h line 148

Quoted 10 lines
include/tst_cgroup.h +9 -0
+/* Tear down any CGroups created by calls to tst_cgroup_require */
+void tst_cgroup_cleanup(void);
+
+/* Get the default CGroup for the test. It allocates memory (in a
+ * guarded buffer) so should always be called from setup
+ */
+const struct tst_cgroup *tst_cgroup_get_test(void);
+/* Get the shared drain group. Also should be called from setup */
+const struct tst_cgroup *tst_cgroup_get_drain(void);

I think that these functions could be named better, it's mildly confusing, "get test what?".

Quoted 1 line

Maybe tst_cgroup_get_test_group() ?

+1

Quoted 10 lines

Or tst_cgroup_get(enum tst_cgroup_group group) ?

include/tst_cgroup.h line 151

Quoted 3 lines
include/tst_cgroup.h +3 -0
+/* Create a descendant CGroup */
+struct tst_cgroup *tst_cgroup_mk(const struct tst_cgroup *parent,
+				 const char *name);

The name parameter should probably be called node_name or dir_name, it could be easily confused with the name parameter rest of the function use to describe a "controller.knob" pair.

+1

Quoted 14 lines

include/tst_cgroup.h

Quoted 10 lines
include/tst_cgroup.h +8 -0
+/* Remove a descendant CGroup */
+struct tst_cgroup *tst_cgroup_rm(struct tst_cgroup *cg);
+
+#define SAFE_CGROUP_VER(cg, name) \
+	safe_cgroup_ver(__FILE__, __LINE__, (cg), (name))
 
+enum tst_cgroup_ver safe_cgroup_ver(const char *file, const int lineno,
+				    const struct tst_cgroup *cg,
+				    const char *name);

The 'name' parameter should really be controller_name or just controller.

Quoted 8 lines

Also I was a bit confused why version request may even fail.

I guess that it's fine if we tst_brk() on invalid controller name even without calling this function SAFE_*.

We haven't defined that anywhere but SAFE_FUNCTIONS() are things that may fail at runtime because of failures propagated from the operating system itself.

+1

Quoted 36 lines
Quoted 36 lines
include/tst_cgroup.h +22 -8
+#define SAFE_CGROUP_HAS(cg, name) \
+	safe_cgroup_has(__FILE__, __LINE__, (cg), (name))
+
+int safe_cgroup_has(const char *file, const int lineno,
+		    const struct tst_cgroup *cg, const char *name);
+
+#define SAFE_CGROUP_READ(cg, name, out, len)				\
+	safe_cgroup_read(__FILE__, __LINE__, (cg), (name), (out), (len))
+
+ssize_t safe_cgroup_read(const char *file, const int lineno,
+			 const struct tst_cgroup *cg, const char *name,
+			 char *out, size_t len);
 
-enum tst_cgroup_ver tst_cgroup_version(void);
+#define SAFE_CGROUP_PRINTF(cg, name, fmt, ...)				\
+	safe_cgroup_printf(__FILE__, __LINE__, (cg), (name), (fmt), __VA_ARGS__)
 
-/* To mount/umount specified cgroup controller on 'cgroup_dir' path */
-void tst_cgroup_mount(enum tst_cgroup_ctrl ctrl, const char *cgroup_dir);
-void tst_cgroup_umount(const char *cgroup_dir);
+#define SAFE_CGROUP_PRINT(cg, name, str)				\
+	safe_cgroup_printf(__FILE__, __LINE__, (cg), (name), "%s", (str))
 
-/* To move current process PID to the mounted cgroup tasks */
-void tst_cgroup_move_current(const char *cgroup_dir);
+void safe_cgroup_printf(const char *file, const int lineno,
+			const struct tst_cgroup *cg, const char *name,
+			const char *fmt, ...)
+			__attribute__ ((format (printf, 5, 6)));
 
-/* To set cgroup controller knob with new value */
-void tst_cgroup_set_knob(const char *cgroup_dir, const char *knob, long value);
+#define SAFE_CGROUP_SCANF(cg, name, fmt, ...)				\
+	safe_cgroup_scanf(__FILE__, __LINE__, (cg), (name), (fmt), __VA_ARGS__)
Quoted 12 lines

include/tst_cgroup.h line 192

Quoted 8 lines
include/tst_cgroup.h +4 -4
-/* Set of functions to set knobs under the memory controller */
-void tst_cgroup_mem_set_maxbytes(const char *cgroup_dir, long memsz);
-int  tst_cgroup_mem_swapacct_enabled(const char *cgroup_dir);
-void tst_cgroup_mem_set_maxswap(const char *cgroup_dir, long memsz);
+void safe_cgroup_scanf(const char *file, const int lineno,
+		       const struct tst_cgroup *cg, const char *name,
+		       const char *fmt, ...)
+		       __attribute__ ((format (scanf, 5, 6)));

And for all the read/printf/scanf functions the name parameter should be named knob or something that describes that we are operating on individual files in a directory somewhere in the tree.

+1

Quoted 26 lines
Quoted 26 lines
include/tst_cgroup.h +0 -5
-/* Set of functions to read/write cpuset controller files content */
-void tst_cgroup_cpuset_read_files(const char *cgroup_dir, const char *filename,
-	char *retbuf, size_t retbuf_sz);
-void tst_cgroup_cpuset_write_files(const char *cgroup_dir, const char *filename,
-	const char *buf);
 
 #endif /* TST_CGROUP_H */
include/tst_test.h +0 -1
index 1fbebe752..62ab2981f 100644
@@ -39,7 +39,6 @@
 #include "tst_capability.h"
 #include "tst_hugepage.h"
 #include "tst_assert.h"
-#include "tst_cgroup.h"
 #include "tst_lockdown.h"
 #include "tst_fips.h"
 #include "tst_taint.h"
lib/Makefile +0 -0
index f019432e8..6f641ee9a 100644
@@ -38,6 +38,8 @@ pc_file			:= $(DESTDIR)/$(datarootdir)/pkgconfig/ltp.pc
 
Quoted 5 lines

lib/Makefile pc_file := $(DESTDIR)/$(datarootdir)/pkgconfig/ltp.pc · line 41

Quoted 3 lines
lib/Makefile +1 -0
 INSTALL_TARGETS		:= $(pc_file)
 
+tst_cgroup.o: CFLAGS += -Wno-missing-field-initializers

I'm not sure we want to work around warnings like this.

I suppose this is a result of cgroup_item being overly generic.

Quoted 65 lines
Quoted 51 lines
lib/Makefile +0 -0
 $(pc_file):
 	test -d "$(@D)" || mkdir -p "$(@D)"
 	install -m $(INSTALL_MODE) "$(builddir)/$(@F)" "$@"
lib/tst_cgroup.c +11 -15
index 96c9524d2..40c9a9bec 100644
@@ -2,453 +2,1014 @@
 /*
  * Copyright (c) 2020 Red Hat, Inc.
  * Copyright (c) 2020 Li Wang <[email protected]>
+ * Copyright (c) 2020-2021 SUSE LLC <[email protected]>
  */
 
 #define TST_NO_DEFAULT_MAIN
 
 #include <stdio.h>
+#include <stddef.h>
 #include <stdlib.h>
+#include <mntent.h>
 #include <sys/mount.h>
-#include <fcntl.h>
-#include <unistd.h>
 
 #include "tst_test.h"
-#include "tst_safe_macros.h"
-#include "tst_safe_stdio.h"
+#include "lapi/fcntl.h"
+#include "lapi/mount.h"
+#include "lapi/mkdirat.h"
 #include "tst_cgroup.h"
-#include "tst_device.h"
 
-static enum tst_cgroup_ver tst_cg_ver;
-static int clone_children;
+/* CGroup Core Implementation
+ *
+ * CGroup Item Implementation is towards the bottom.
+ */
 
-static int tst_cgroup_check(const char *cgroup)
-{
-	char line[PATH_MAX];
-	FILE *file;
-	int cg_check = 0;
+struct cgroup_root;
 
-	file = SAFE_FOPEN("/proc/filesystems", "r");
-	while (fgets(line, sizeof(line), file)) {
-		if (strstr(line, cgroup) != NULL) {

lib/tst_cgroup.c line 36

Quoted 10 lines
lib/tst_cgroup.c +8 -2
-			cg_check = 1;
-			break;
+/* A node in a single CGroup hierarchy. It exists mainly for
+ * convenience so that we do not have to traverse the CGroup structure
+ * for frequent operations.
+ *
+ * This is actually a single-linked list not a tree. We only need to
+ * traverse from leaf towards root.
+ */
+struct tst_cgroup_tree {

Why isn't this called node or dir? Either of these would be more fitting.

Quoted 2 lines

Also the tst_cgroup structure could use a better name, the tst_cgroup is actually an array of pointers to directories.

As far as the test author is concerned it represents an actual "Control Group". We keep arrays of directories to join together multiple V1 controllers on different hierarchies, but that is an implementation detail.

If anything it should be called tst_cgroup_group, but I don't like the repetition. :-p

Quoted 24 lines

lib/tst_cgroup.c line 37

Quoted 2 lines
lib/tst_cgroup.c +1 -0
+	const char *name;

This should be dir_name, can we please avoid having all string pointers called just 'name' I find it horribly confusing...

Quoted 5 lines
lib/tst_cgroup.c +5 -0
+	const struct tst_cgroup_tree *parent;
+
+	/* Shortcut to root */
+	const struct cgroup_root *root;
+

lib/tst_cgroup.c line 52

Quoted 10 lines
lib/tst_cgroup.c +10 -0
+	/* Subsystems (controllers) bit field. Only controllers which
+	 * were required and configured for this node are added to
+	 * this field. So it may be different from root->css_field.
+	 */
+	uint32_t css_field;
+
+	/* In general we avoid having sprintfs everywhere and use
+	 * openat, linkat, etc.
+	 */
+	int dir;

Can we name this dir_fd so it's obvious what it is?

Quoted 10 lines

lib/tst_cgroup.c line 61

Quoted 8 lines
lib/tst_cgroup.c +8 -0
+	int we_created_it:1;
+};
+
+/* The root of a CGroup hierarchy/tree */
+struct cgroup_root {
+	enum tst_cgroup_ver ver;
+	/* A mount path */
+	char path[PATH_MAX/2];

Why PATH_MAX/2 ?

This was to prevent static anlyses warnings with sprintf IIRC. It can not be PATH_MAX unless the mount point contents are only expected to be accessed with openat. I suppose that since I removed the sprintfs we can use the full PATH_MAX to be consistent.

Quoted 29 lines
Quoted 29 lines
lib/tst_cgroup.c +26 -2
+/* Lookup tree for item names. */
+typedef struct cgroup_item items_t[];
+static items_t items = {
+	[0] = { "cgroup", .inner = (items_t){
+			{ "cgroup.procs", "tasks" },
+			{ "cgroup.subtree_control" },
+			{ "cgroup.clone_children", "clone_children" },
+			{ NULL }
 		}
-	}
-	SAFE_FCLOSE(file);
+	},
+	[TST_CGROUP_MEMORY] = { "memory", .inner = (items_t){
+			{ "memory.current", "memory.usage_in_bytes" },
+			{ "memory.max", "memory.limit_in_bytes" },
+			{ "memory.swappiness", "memory.swappiness" },
+			{ "memory.swap.current", "memory.memsw.usage_in_bytes" },
+			{ "memory.swap.max", "memory.memsw.limit_in_bytes" },
+			{ "memory.kmem.usage_in_bytes", "memory.kmem.usage_in_bytes" },
+			{ "memory.kmem.limit_in_bytes", "memory.kmem.usage_in_bytes" },
+			{ NULL }
+		},
+	  .css_indx = TST_CGROUP_MEMORY
+	},
+	[TST_CGROUP_CPUSET] = { "cpuset", .inner = (items_t){
+			{ "cpuset.cpus", "cpuset.cpus" },
+			{ "cpuset.mems", "cpuset.mems" },
+			{ "cpuset.memory_migrate", "cpuset.memory_migrate" },
+			{ NULL }
Quoted 8 lines

lib/tst_cgroup.c line 151

Quoted 5 lines
lib/tst_cgroup.c +5 -0
+		},
+	  .css_indx = TST_CGROUP_CPUSET
+	},
+	{ NULL }
+};

Item is a very generic word, this is a list of known knobs and map between v1 and v2. So maybe cgroup_knob_map or just cgroup_knobs ?

I suppose that cgroup_item can be seperated into just two structs now that I removed controller sub items like "memory.swap". This might help solve the missing initializer warnings as well.

I would not describe "memory.current" as a knob. It is a read only file and I think of knobs as something you can write to. So I would prefer cgroup_file and cgroup_subsys.

Signature · 2 lines
Thank you,
Richard.
#11 lore raw

PATCH v3 3/7 Add new CGroups APIs

Hi,

Quoted 12 lines
Quoted 12 lines

-enum tst_cgroup_ctrl {

include/tst_cgroup.h line 94

Quoted 7 lines
include/tst_cgroup.h +3 -2
-    TST_CGROUP_MEMCG = 1,
+/* Controller sub-systems */
+enum tst_cgroup_css {
+    TST_CGROUP_MEMORY = 1,
     TST_CGROUP_CPUSET = 2,
-    /* add cgroup controller */
 };

I spend a bit of time figuring out what css means, can't we just use controler in the code instead? It's a bit longer but more obvious.

Quoted 7 lines
Quoted 1 line

Or is this consitent with kernel naming?

The kernel seems to refer to controllers as subsystems and css appears to mean "CGroup subsystem". I'm not sure if subsystems are necessarily controllers, but it seems that way. Also "controller" has too many letters in it. Indeed it makes some function names very long and I found that harder to read.

I also feel css looks oddly, if "controller" is too long we can go back with "ctrl",
then we might need a one-line comment for the name abbreviation.

e.g. previous "/* add cgroup controller */" is make no sense but a hint for people
to know this is cgroup controller and could be extended in the future.

Quoted 3 lines

I suppose to be totally consistent with the kernel we should randomly name some things css and others subsys. :-p

Not very necessary, this test API is facing for userland, understanding easily
for users is more important I think.

Quoted 8 lines
Quoted 8 lines

+struct tst_cgroup_opts {

Quoted 6 lines
include/tst_cgroup.h +6 -0
+    /* Only try to mount V1 CGroup controllers. This will not
+     * prevent V2 from being used if it is already mounted, it
+     * only indicates that we should mount V1 controllers if
+     * nothing is present. By default we try to mount V2 first. */
+    int only_mount_v1:1;
+};
Quoted 9 lines
Quoted 4 lines

Do we need to pass the flags in a structure?

This is not an API that has to be future proof, when we find out we need more than a few bitflags we can always change it.

We may need to add xattr to this and there are many other options. However most tests will just have NULL or 0, 0... So we could move it into a vararg or have a tst_require2?

My 2cent:

I slightly think tst_cgroup_opts is good stuff, it gives a possibility to let users hook the cgoup mount way or, more options they needed (maybe for customized users). And, in most situations that test just has NULL, that's fine.

@Richard,
do you want to reserve the ?no_cleanup? in this structure as well? I noticed you leave it in tst_cgroup02 test.

Quoted 10 lines
Quoted 6 lines

include/tst_cgroup.h line 126

Quoted 2 lines
include/tst_cgroup.h +2 -0
+    struct tst_cgroup_tree *trees[TST_CGROUP_MAX_TREES + 1];
+};

So this is an array of directories in different trees, can we please name the strucuture better. What about tst_cgroup_nodes or tst_cgroup_dirs?

I guess tst_cgroup_tree always represents a directory. So I would go with tst_cgroup_dir.

+1

Quoted 8 lines
Quoted 7 lines

+struct tst_cgroup_tree {

Why isn't this called node or dir? Either of these would be more fitting.

Also the tst_cgroup structure could use a better name, the tst_cgroup is actually an array of pointers to directories.

Quoted 8 lines

As far as the test author is concerned it represents an actual "Control Group". We keep arrays of directories to join together multiple V1 controllers on different hierarchies, but that is an implementation detail.

If anything it should be called tst_cgroup_group, but I don't like the repetition. :-p

Personally, I think tst_cgroup_node may be better since we have described a tree for cgroup. root, node, and leaves(files) should be more vivid.

Quoted 8 lines
Quoted 7 lines

+

lib/tst_cgroup.c line 52

Quoted 4 lines
lib/tst_cgroup.c +4 -0
+    /* In general we avoid having sprintfs everywhere and use
+     * openat, linkat, etc.
+     */
+    int dir;

Can we name this dir_fd so it's obvious what it is?

+1
I stand by Cyril at this point.

Quoted 40 lines
Quoted 17 lines

+/* Lookup tree for item names. */

Quoted 6 lines
lib/tst_cgroup.c +6 -0
+typedef struct cgroup_item items_t[];
+static items_t items = {
+    [0] = { "cgroup", .inner = (items_t){
+                    { "cgroup.procs", "tasks" },
+                    { "cgroup.subtree_control" },
+                    { "cgroup.clone_children", "clone_children" },

lib/tst_cgroup.c

Quoted 10 lines
lib/tst_cgroup.c +7 -2
+                    { NULL }
             }
-    }
-    SAFE_FCLOSE(file);
+    },
+    [TST_CGROUP_MEMORY] = { "memory", .inner = (items_t){
+                    { "memory.current", "memory.usage_in_bytes" },
+                    { "memory.max", "memory.limit_in_bytes" },
+                    { "memory.swappiness", "memory.swappiness" },
+                    { "memory.swap.current",

"memory.memsw.usage_in_bytes" },

Quoted 1 line
Quoted 1 line

+ { "memory.swap.max", "memory.memsw.limit_in_bytes"

},

Quoted 1 line
Quoted 1 line

+ { "memory.kmem.usage_in_bytes",

"memory.kmem.usage_in_bytes" },

Quoted 1 line
Quoted 1 line

+ { "memory.kmem.limit_in_bytes",

"memory.kmem.usage_in_bytes" },

Quoted 8 lines

lib/tst_cgroup.c

Quoted 8 lines
lib/tst_cgroup.c +8 -0
+                    { NULL }
+            },
+      .css_indx = TST_CGROUP_MEMORY
+    },
+    [TST_CGROUP_CPUSET] = { "cpuset", .inner = (items_t){
+                    { "cpuset.cpus", "cpuset.cpus" },
+                    { "cpuset.mems", "cpuset.mems" },
+                    { "cpuset.memory_migrate", "cpuset.memory_migrate"

},

Quoted 7 lines
Quoted 6 lines
lib/tst_cgroup.c +6 -0
+                    { NULL }
+            },
+      .css_indx = TST_CGROUP_CPUSET
+    },
+    { NULL }
+};
Quoted 6 lines
Quoted 2 lines

Item is a very generic word, this is a list of known knobs and map between v1 and v2. So maybe cgroup_knob_map or just cgroup_knobs ?

I suppose that cgroup_item can be seperated into just two structs now that I removed controller sub items like "memory.swap". This might help solve the missing initializer warnings as well.

To separate into two structs sounds good, the 'cgroup_item' contains both trunk node and controller files make things mess up.

Quoted 3 lines

I would not describe "memory.current" as a knob. It is a read only file and I think of knobs as something you can write to. So I would prefer cgroup_file and cgroup_subsys.

+1
cgroup_file and cgroup_subsys(or cgroup_controller) all very nice.

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

PATCH v3 3/7 Add new CGroups APIs

Quoted 23 lines · Li Wang

Hi,

Quoted 12 lines
Quoted 12 lines

-enum tst_cgroup_ctrl {

include/tst_cgroup.h line 94

Quoted 7 lines
include/tst_cgroup.h +3 -2
-    TST_CGROUP_MEMCG = 1,
+/* Controller sub-systems */
+enum tst_cgroup_css {
+    TST_CGROUP_MEMORY = 1,
     TST_CGROUP_CPUSET = 2,
-    /* add cgroup controller */
 };

I spend a bit of time figuring out what css means, can't we just use controler in the code instead? It's a bit longer but more obvious.

Quoted 7 lines
Quoted 1 line

Or is this consitent with kernel naming?

The kernel seems to refer to controllers as subsystems and css appears to mean "CGroup subsystem". I'm not sure if subsystems are necessarily controllers, but it seems that way. Also "controller" has too many letters in it. Indeed it makes some function names very long and I found that harder to read.

Quoted 7 lines

I also feel css looks oddly, if "controller" is too long we can go back with "ctrl",
then we might need a one-line comment for the name abbreviation.

e.g. previous "/* add cgroup controller */" is make no sense but a hint for people
to know this is cgroup controller and could be extended in the future.

OK ctrl it is and I will create a note about internal kernel naming.

Quoted 29 lines
Quoted 5 lines

I suppose to be totally consistent with the kernel we should randomly name some things css and others subsys. :-p

Not very necessary, this test API is facing for userland, understanding easily
for users is more important I think.

Quoted 8 lines
Quoted 8 lines

+struct tst_cgroup_opts {

Quoted 6 lines
include/tst_cgroup.h +6 -0
+    /* Only try to mount V1 CGroup controllers. This will not
+     * prevent V2 from being used if it is already mounted, it
+     * only indicates that we should mount V1 controllers if
+     * nothing is present. By default we try to mount V2 first. */
+    int only_mount_v1:1;
+};
Quoted 9 lines
Quoted 4 lines

Do we need to pass the flags in a structure?

This is not an API that has to be future proof, when we find out we need more than a few bitflags we can always change it.

We may need to add xattr to this and there are many other options. However most tests will just have NULL or 0, 0... So we could move it into a vararg or have a tst_require2?

Quoted 6 lines

My 2cent:

I slightly think tst_cgroup_opts is good stuff, it gives a possibility to let users hook the cgoup mount way or, more options they needed (maybe for customized users). And, in most situations that test just has NULL, that's fine.

+1

Quoted 4 lines

@Richard,
do you want to reserve the ?no_cleanup? in this structure as well? I noticed you leave it in tst_cgroup02 test.

I think this is only necessary if we add a new flag to all tests in tst_test to prevent CGroup cleanup. It should be easy to add this in a later patch.

Quoted 29 lines
Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
include/tst_cgroup.h +2 -0
+    struct tst_cgroup_tree *trees[TST_CGROUP_MAX_TREES + 1];
+};
Quoted 7 lines
Quoted 3 lines

So this is an array of directories in different trees, can we please name the strucuture better. What about tst_cgroup_nodes or tst_cgroup_dirs?

I guess tst_cgroup_tree always represents a directory. So I would go with tst_cgroup_dir.

+1

Quoted 13 lines
Quoted 7 lines

+struct tst_cgroup_tree {

Why isn't this called node or dir? Either of these would be more fitting.

Also the tst_cgroup structure could use a better name, the tst_cgroup is actually an array of pointers to directories.

As far as the test author is concerned it represents an actual "Control Group". We keep arrays of directories to join together multiple V1 controllers on different hierarchies, but that is an implementation detail.

Quoted 7 lines
Quoted 3 lines

If anything it should be called tst_cgroup_group, but I don't like the repetition. :-p

Personally, I think tst_cgroup_node may be better since we have described a tree for cgroup. root, node, and leaves(files) should be more vivid.

OK

Quoted 67 lines
Quoted 10 lines
Quoted 9 lines

+

lib/tst_cgroup.c line 52

Quoted 4 lines
lib/tst_cgroup.c +4 -0
+    /* In general we avoid having sprintfs everywhere and use
+     * openat, linkat, etc.
+     */
+    int dir;

Can we name this dir_fd so it's obvious what it is?

+1
I stand by Cyril at this point.

Quoted 40 lines
Quoted 17 lines

+/* Lookup tree for item names. */

Quoted 6 lines
lib/tst_cgroup.c +6 -0
+typedef struct cgroup_item items_t[];
+static items_t items = {
+    [0] = { "cgroup", .inner = (items_t){
+                    { "cgroup.procs", "tasks" },
+                    { "cgroup.subtree_control" },
+                    { "cgroup.clone_children", "clone_children" },

lib/tst_cgroup.c

Quoted 10 lines
lib/tst_cgroup.c +7 -2
+                    { NULL }
             }
-    }
-    SAFE_FCLOSE(file);
+    },
+    [TST_CGROUP_MEMORY] = { "memory", .inner = (items_t){
+                    { "memory.current", "memory.usage_in_bytes" },
+                    { "memory.max", "memory.limit_in_bytes" },
+                    { "memory.swappiness", "memory.swappiness" },
+                    { "memory.swap.current",

"memory.memsw.usage_in_bytes" },

Quoted 1 line
Quoted 1 line

+ { "memory.swap.max", "memory.memsw.limit_in_bytes"

},

Quoted 1 line
Quoted 1 line

+ { "memory.kmem.usage_in_bytes",

"memory.kmem.usage_in_bytes" },

Quoted 1 line
Quoted 1 line

+ { "memory.kmem.limit_in_bytes",

"memory.kmem.usage_in_bytes" },

Quoted 8 lines

lib/tst_cgroup.c

Quoted 8 lines
lib/tst_cgroup.c +8 -0
+                    { NULL }
+            },
+      .css_indx = TST_CGROUP_MEMORY
+    },
+    [TST_CGROUP_CPUSET] = { "cpuset", .inner = (items_t){
+                    { "cpuset.cpus", "cpuset.cpus" },
+                    { "cpuset.mems", "cpuset.mems" },
+                    { "cpuset.memory_migrate", "cpuset.memory_migrate"

},

Quoted 7 lines
Quoted 6 lines
lib/tst_cgroup.c +6 -0
+                    { NULL }
+            },
+      .css_indx = TST_CGROUP_CPUSET
+    },
+    { NULL }
+};
Quoted 6 lines
Quoted 2 lines

Item is a very generic word, this is a list of known knobs and map between v1 and v2. So maybe cgroup_knob_map or just cgroup_knobs ?

I suppose that cgroup_item can be seperated into just two structs now that I removed controller sub items like "memory.swap". This might help solve the missing initializer warnings as well.

To separate into two structs sounds good, the 'cgroup_item' contains both trunk node and controller files make things mess up.

Quoted 7 lines
Quoted 3 lines

I would not describe "memory.current" as a knob. It is a read only file and I think of knobs as something you can write to. So I would prefer cgroup_file and cgroup_subsys.

+1
cgroup_file and cgroup_subsys(or cgroup_controller) all very nice.

I will try cgroup_file and cgroup_ctrl.

Signature · 2 lines
Thank you,
Richard.
#13 lore raw

PATCH v3 3/7 Add new CGroups APIs

In reply to Richard Palethorpe

Hi Richard,

Thanks for your work, the whole design looks good.

Minor suggestions as below:

lib/tst_cgroup.c line 168

Quoted 13 lines
lib/tst_cgroup.c +12 -0
+static const char *ltp_cgroup_dir = "ltp";
+static const char *ltp_cgroup_drain_dir = "drain";
+static char test_cgroup_dir[PATH_MAX/4];
+static const char *ltp_mount_prefix = "/tmp/cgroup_";
+static const char *ltp_v2_mount = "unified";
+
+#define first_root                             \
+       (roots[0].ver ? roots : roots + 1)
+#define for_each_root(r)                       \
+       for ((r) = first_root; (r)->ver; (r)++)
+#define for_each_v1_root(r)                    \
+       for ((r) = roots + 1; (r)->ver; (r)++)

If you go through the whole files you will find, there are some places use 'r' to represent the root but other uses 't', even in functions that include ?t?
to represent a tree.

That probably a minor issue to make people get lost:).

So I'd suggest a unified abbreviation in all:

r --> root
t --> tree

if a function has root and tree, plz avoid using abbreviations, just use itself.

Quoted 13 lines
lib/tst_cgroup.c +12 -0
+#define for_each_css(css)                      \
+       for ((css) = items + 1; (css)->name; (css)++)
+
+/* Controller items may only be in a single tree. So when (ss) > 0
+ * we only loop once.
+ */
+#define for_each_tree(cg, css, t)                                      \
+       for ((t) = (css) ? (cg)->trees_by_css + (css) : (cg)->trees;    \
+            *(t);                                                      \
+            (t) = (css) ? (cg)->trees + TST_CGROUP_MAX_TREES : (t) + 1)
+
+static int has_css(uint32_t css_field, enum tst_cgroup_css type)

lib/tst_cgroup.c line 223

Quoted 10 lines
lib/tst_cgroup.c +4 -0
+{
+       return !!(css_field & (1 << type));
 }
 
 
 
 
+struct tst_cgroup *tst_cgroup_mk(const struct tst_cgroup *parent,
+                                const char *name)
 {

Since we have already got the parent tst_cgroup, it means we know which controllers/type has been mounted.

Can we add the required controllers (e.g. "+memory") to subtree_control automatically at here, rather than doing it manually before creating children?
(then we can remove the line#27~30 in tst_cgroup02.c)

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

PATCH v3 3/7 Add new CGroups APIs

Hello,

Quoted 28 lines · Li Wang

Hi Richard,

Thanks for your work, the whole design looks good.

Minor suggestions as below:

lib/tst_cgroup.c line 168

Quoted 13 lines
lib/tst_cgroup.c +12 -0
+static const char *ltp_cgroup_dir = "ltp";
+static const char *ltp_cgroup_drain_dir = "drain";
+static char test_cgroup_dir[PATH_MAX/4];
+static const char *ltp_mount_prefix = "/tmp/cgroup_";
+static const char *ltp_v2_mount = "unified";
+
+#define first_root                             \
+       (roots[0].ver ? roots : roots + 1)
+#define for_each_root(r)                       \
+       for ((r) = first_root; (r)->ver; (r)++)
+#define for_each_v1_root(r)                    \
+       for ((r) = roots + 1; (r)->ver; (r)++)

If you go through the whole files you will find, there are some places use 'r' to represent the root but other uses 't', even in functions that include ?t?
to represent a tree.

That probably a minor issue to make people get lost:).

Quoted 7 lines

So I'd suggest a unified abbreviation in all:

r --> root
t --> tree

if a function has root and tree, plz avoid using abbreviations, just use itself.

Yes, I should do something about this naming. I think this is related to other issues Cyril mentioned in the naming of tree items.

Quoted 27 lines
Quoted 16 lines
lib/tst_cgroup.c +12 -0
+#define for_each_css(css)                      \
+       for ((css) = items + 1; (css)->name; (css)++)
+
+/* Controller items may only be in a single tree. So when (ss) > 0
+ * we only loop once.
+ */
+#define for_each_tree(cg, css, t)                                      \
+       for ((t) = (css) ? (cg)->trees_by_css + (css) : (cg)->trees;    \
+            *(t);                                                      \
+            (t) = (css) ? (cg)->trees + TST_CGROUP_MAX_TREES : (t) + 1)
+
+static int has_css(uint32_t css_field, enum tst_cgroup_css type)

lib/tst_cgroup.c line 223

Quoted 10 lines
lib/tst_cgroup.c +4 -0
+{
+       return !!(css_field & (1 << type));
 }
 
 
 
 
+struct tst_cgroup *tst_cgroup_mk(const struct tst_cgroup *parent,
+                                const char *name)
 {
Quoted 7 lines

Since we have already got the parent tst_cgroup, it means we know which controllers/type has been mounted.

Can we add the required controllers (e.g. "+memory") to subtree_control automatically at here, rather than doing it manually before creating children?
(then we can remove the line#27~30 in tst_cgroup02.c)

It is possible and more consistent with V1 behavior. I suppose users could remove controllers again if they wanted to test some V2 specific configuration.

OTOH we could leave this for a later patch when we actually have some real tests containing child groups? I'm not fully sure what the results of doing this are and we may just have to reverse it.

Signature · 2 lines
Thank you,
Richard.
#15 lore raw

PATCH v3 4/7 Add new CGroups API library tests

In reply to Richard Palethorpe
---
 lib/newlib_tests/.gitignore     |  2 +
 lib/newlib_tests/test21.c       | 45 ++++++++---------
 lib/newlib_tests/tst_cgroup01.c | 51 +++++++++++++++++++
 lib/newlib_tests/tst_cgroup02.c | 90 +++++++++++++++++++++++++++++++++
 4 files changed, 163 insertions(+), 25 deletions(-)
 create mode 100644 lib/newlib_tests/tst_cgroup01.c
 create mode 100644 lib/newlib_tests/tst_cgroup02.c
lib/newlib_tests/.gitignore +2 -0
index 6c2612259..98611e65d 100644
@@ -16,6 +16,8 @@ test15
 test16
 tst_capability01
 tst_capability02
+tst_cgroup01
+tst_cgroup02
 tst_device
 tst_safe_fileops
 tst_res_hexd
lib/newlib_tests/test21.c +20 -25

74 lines not on this page · raw message

lib/newlib_tests/tst_cgroup01.c new file +51 -0

51 lines not on this page · raw message

lib/newlib_tests/tst_cgroup02.c new file +90 -0

90 lines not on this page · raw message

Signature · 1 line
2.30.2
#16 lore raw

PATCH v3 4/7 Add new CGroups API library tests

Quoted 5 lines
 lib/newlib_tests/.gitignore     |  2 +
 lib/newlib_tests/test21.c       | 45 ++++++++---------
 lib/newlib_tests/tst_cgroup01.c | 51 +++++++++++++++++++
 lib/newlib_tests/tst_cgroup02.c | 90 +++++++++++++++++++++++++++++++++

Since test21 is covered by tst_cgroup02, I'd suggest deleting test21.c directly.

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

PATCH v3 5/7 docs: Update CGroups API

In reply to Richard Palethorpe
---
 doc/test-writing-guidelines.txt | 175 +++++++++++++++++++++++++++++---
 1 file changed, 162 insertions(+), 13 deletions(-)
doc/test-writing-guidelines.txt +162 -13
index 469df1184..9c94a282b 100644
@@ -2167,48 +2167,197 @@ the field value of file.
 
 2.2.36 Using Control Group
 ^^^^^^^^^^^^^^^^^^^^^^^^^^
-Some of LTP tests need Control Group in their configuration, tst_cgroup.h provides
-APIs to make cgroup unified mounting at setup phase to be possible. The method?is
-extracted from mem.h with the purpose of?extendible for further developing, and
-trying to compatible the current two versions of cgroup.
 
-Considering there are many differences between cgroup v1 and v2, here we capsulate
-the detail of cgroup mounting in high-level functions, which will be easier to use
-cgroup without caring about too much technical thing.? ?
+Some LTP tests need specific Control Group configurations. tst_cgroup.h provides
+APIs to discover and use CGroups. There are many differences between CGroups API
+V1 and V2. We encapsulate the details of configuring CGroups in high-level
+functions which follow the V2 kernel API. Allowing one to use CGroups without
+caring too much about the current system's configuration.
 
-Also, we do cgroup mount/umount work for the different hierarchy automatically.
+Also, the LTP library will automatically mount/umount and configure the CGroup
+hierarchies if that is required (e.g. if you run the tests from init with no
+system manager).
 
 [source,c]
 -------------------------------------------------------------------------------
 #include "tst_test.h"
+#include "tst_cgroup.h"
+
+static const struct tst_cgroup *cg;
 
 static void run(void)
 {
 	...
+	// do test under cgroup
+	...
+}
+
+static void setup(void)
+{
+	tst_cgroup_require(TST_CGROUP_MEMORY, NULL);
+	cg = tst_cgroup_get_test();
+	SAFE_CGROUP_PRINTF(cg, "cgroup.procs", "%d", getpid());
+	SAFE_CGROUP_PRINTF(cg, "memory.max", "%lu", MEMSIZE);
+	if (SAFE_CGROUP_HAS(cg, "memory.swap.max"))
+		SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%zu", memsw);
+}
 
-	tst_cgroup_move_current(PATH_TMP_CG_MEM);
-	tst_cgroup_mem_set_maxbytes(PATH_TMP_CG_MEM, MEMSIZE);
+static void cleanup(void)
+{
+	tst_cgroup_cleanup();
+}
 
-	// do test under cgroup
+struct tst_test test = {
+	.setup = setup,
+	.test_all = run,
+	.cleanup = cleanup,
 	...
+};
+-------------------------------------------------------------------------------
+
+Above, we first ensure the memory controller is available on the
+test's CGroup with 'tst_cgroup_require'. We then get a structure,
+'cg', which represents the test's CGroup. Note that
+'tst_cgroup_get_test' should not be called many times, as it is
+allocated in a guarded buffer (See section 2.2.31). Therefor it is
+best to call it once in 'setup' and not 'run' because 'run' may be
+repeated with the '-i' option.
+
+We then write the current processes PID into 'cgroup.procs', which
+moves the current process into the test's CGroup. After which we set
+the maximum memory size by writing to 'memory.max'. If the memory
+controller is mounted on CGroups V1 then the library will actually
+write to 'memory.limit_in_bytes'. As a general rule, if a file exists
+on both CGroup versions, then we use the V2 naming.
+
+Some controller features, such as 'memory.swap', can be
+disabled. Therefor we need to check if they exist before accessing
+them. This can be done with 'TST_CGROUP_HAS' which can be called on
+any control file or feature.
+
+Most tests only require setting a few limits similar to the above. In
+such cases the differences between V1 and V2 are hidden. Setup and
+cleanup is also mostly hidden. However things can get much worse.
+
+[source,c]
+-------------------------------------------------------------------------------
+static const struct tst_cgroup *cg;
+static const struct tst_cgroup *cg_drain;
+static struct tst_cgroup *cg_child;
+
+static void run(void)
+{
+	char buf[BUFSIZ];
+	size_t mem = 0;
+
+	cg_child = tst_cgroup_mk(cg, "child");
+	SAFE_CGROUP_PRINTF(cg_child, "cgroup.procs", "%d", getpid());
+
+	if (SAFE_CGROUP_VER(cg, "memory") != TST_CGROUP_V1)
+		SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+memory");
+	if (SAFE_CGROUP_VER(cg, "cpuset") != TST_CGROUP_V1)
+		SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+cpuset");
+
+	if (!SAFE_FORK()) {
+		SAFE_CGROUP_PRINTF(cg_child, "cgroup.procs", "%d", getpid());
+
+		if (SAFE_CGROUP_HAS(cg_child, "memory.swap"))
+			SAFE_CGROUP_SCANF(cg_child, "memory.swap.current", "%zu", &mem);
+		SAFE_CGROUP_READ(cg_child, "cpuset.mems", buf, sizeof(buf));
+
+		// Do something with cpuset.mems and memory.current values
+		...
+
+		exit(0);
+	}
+
+	tst_reap_children();
+	SAFE_CGROUP_PRINTF(cg_drain, "cgroup.procs", "%d", getpid());
+	cg_child = tst_cgroup_rm(cg_child);
 }
 
 static void setup(void)
 {
-	tst_cgroup_mount(TST_CGROUP_MEMCG, PATH_TMP_CG_MEM);
+	tst_cgroup_require(TST_CGROUP_MEMORY, NULL);
+	tst_cgroup_require(TST_CGROUP_CPUSET, NULL);
+
+	cg = tst_cgroup_get_test();
+	cg_drain = tst_cgroup_get_drain();
 }
 
 static void cleanup(void)
 {
-	tst_cgroup_umount(PATH_TMP_CG_MEM);
+	if (cg_child) {
+		SAFE_CGROUP_PRINTF(cg_drain, "cgroup.procs", "%d", getpid());
+		cg_child = tst_cgroup_rm(cg_child);
+	}
+
+	tst_cgroup_cleanup();
 }
 
 struct tst_test test = {
+	.setup = setup,
 	.test_all = run,
+	.cleanup = cleanup,
 	...
 };
 -------------------------------------------------------------------------------
 
+Starting with setup; we can see here that we also fetch the 'drain'
+CGroup. This is a shared group (between parallel tests) which may
+contain processes from other tests. It should have default settings and
+these should not be changed by the test. It can be used to remove
+processes from other CGroups incase the hierarchy root is not
+accessible.
+
+In 'run', we first create a child CGroup with 'tst_cgroup_mk'. As we
+create this CGroup in 'run' we should also remove it at the end of
+run. We also need to check if it exists and remove it in cleanup as
+well. Because there are 'SAFE_' functions which may jump to cleanup.
+
+We then move the main test process into the child CGroup. This is
+important as it means that before we destroy the child CGroup we have
+to move the main test process elsewhere. For that we use the 'drain'
+group.
+
+Next we enable the memory and cpuset controller configuration on the
+test CGroup's descendants (i.e. 'cg_child'). This allows each child to
+have its own settings. The file 'cgroup.subtree_control' does not
+exist on V1. Because it is possible to have both V1 and V2 active at
+the same time. We can not simply check if 'subtree_control' exists
+before writing to it. We have to check if a particular controller is
+on V2 before trying to add it to 'subtree_control'. Trying to add a V1
+controller will result in 'ENOENT'.
+
+We then fork a child process and add this to the child CGroup. Within
+the child process we try to read 'memory.swap.current'. It is possible
+that the memory controller was compiled without swap support, so it is
+necessary to check if 'memory.swap' is enabled. That is unless the
+test will never reach the point where 'memory.swap.*' are used without
+swap support.
+
+The parent process waits for the child process to be reaped before
+destroying the child CGroup. So there is no need to transfer the child
+to drain. However the parent process must be moved otherwise we will
+get 'EBUSY' when trying to remove the child CGroup.
+
+Another example of an edge case is the following.
+
+[source,c]
+-------------------------------------------------------------------------------
+	if (tst_cgroup_ver(cg, "memory") == TST_CGROUP_V1)
+		SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%lu", ~0UL);
+	else
+		SAFE_CGROUP_PRINT(cg, "memory.swap.max", "max");
+-------------------------------------------------------------------------------
+
+CGroups V2 introduced a feature where 'memory[.swap].max' could be set to
+"max". This does not appear to work on V1 'limit_in_bytes' however. For most
+tests, simply using a large number is sufficient and there is no need to use
+"max". Importantly though, one should be careful to read both the V1 and V2
+kernel docs. The LTP library can not handle all edge cases. It does the minimal
+amount of work to make testing on both V1 and V2 feasible.
+
 2.2.37 Require minimum numbers of CPU for a testcase
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
Signature · 1 line
2.30.2
#18 lore raw

PATCH v3 5/7 docs: Update CGroups API

doc/test-writing-guidelines.txt the field value of file. · line 2259

Quoted 13 lines
doc/test-writing-guidelines.txt +12 -0
+static void run(void)
+{
+       char buf[BUFSIZ];
+       size_t mem = 0;
+
+       cg_child = tst_cgroup_mk(cg, "child");
+       SAFE_CGROUP_PRINTF(cg_child, "cgroup.procs", "%d", getpid());
+
+       if (SAFE_CGROUP_VER(cg, "memory") != TST_CGROUP_V1)
+               SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+memory");
+       if (SAFE_CGROUP_VER(cg, "cpuset") != TST_CGROUP_V1)
+               SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+cpuset");

Kind reminder:

If you decide to add controllers automatically (as I suggested in patch3/7) in
tst_cgroup_mk(), then these lines should be removed.

doc/test-writing-guidelines.txt the field value of file. · line 2351

Quoted 10 lines
doc/test-writing-guidelines.txt +8 -0
+Another example of an edge case is the following.
+
+[source,c]
 
+-------------------------------------------------------------------------------
+       if (tst_cgroup_ver(cg, "memory") == TST_CGROUP_V1)
+               SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%lu", ~0UL);
+       else
+               SAFE_CGROUP_PRINT(cg, "memory.swap.max", "max");

typo PRINT --> PRINTF ^.

Btw, these documented works are quite awesome!

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

PATCH v3 5/7 docs: Update CGroups API

Hello,

Quoted 25 lines · Li Wang

doc/test-writing-guidelines.txt the field value of file. · line 2259

Quoted 13 lines
doc/test-writing-guidelines.txt +12 -0
+static void run(void)
+{
+       char buf[BUFSIZ];
+       size_t mem = 0;
+
+       cg_child = tst_cgroup_mk(cg, "child");
+       SAFE_CGROUP_PRINTF(cg_child, "cgroup.procs", "%d", getpid());
+
+       if (SAFE_CGROUP_VER(cg, "memory") != TST_CGROUP_V1)
+               SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+memory");
+       if (SAFE_CGROUP_VER(cg, "cpuset") != TST_CGROUP_V1)
+               SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+cpuset");

Kind reminder:

If you decide to add controllers automatically (as I suggested in patch3/7) in
tst_cgroup_mk(), then these lines should be removed.

Quoted 4 lines
doc/test-writing-guidelines.txt +3 -0
+Another example of an edge case is the following.
+
+[source,c]
Quoted 8 lines

doc/test-writing-guidelines.txt the field value of file. · line 2351

Quoted 6 lines

+-------------------------------------------------------------------------------

doc/test-writing-guidelines.txt +4 -0
+       if (tst_cgroup_ver(cg, "memory") == TST_CGROUP_V1)
+               SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%lu", ~0UL);
+       else
+               SAFE_CGROUP_PRINT(cg, "memory.swap.max", "max");

typo PRINT --> PRINTF ^.

This function actually exists :-p

Quoted 2 lines

Btw, these documented works are quite awesome!

Thanks!

Signature · 2 lines
Thank you,
Richard.
#20 lore raw

PATCH v3 6/7 mem: Convert tests to new CGroups API

In reply to Richard Palethorpe
---
 testcases/kernel/mem/cpuset/cpuset01.c | 34 ++++++++++++--------------
 testcases/kernel/mem/include/mem.h     |  2 +-
 testcases/kernel/mem/ksm/ksm02.c       | 13 +++++++---
 testcases/kernel/mem/ksm/ksm03.c       | 12 ++++++---
 testcases/kernel/mem/ksm/ksm04.c       | 17 +++++++------
 testcases/kernel/mem/lib/mem.c         | 10 +++-----
 testcases/kernel/mem/oom/oom03.c       | 18 ++++++++------
 testcases/kernel/mem/oom/oom04.c       | 19 ++++++++------
 testcases/kernel/mem/oom/oom05.c       | 32 ++++++++++++++----------
 9 files changed, 87 insertions(+), 70 deletions(-)
testcases/kernel/mem/cpuset/cpuset01.c +16 -18
index 528c3eddd..5a14a0a6b 100644
@@ -35,6 +35,8 @@
 
 #ifdef HAVE_NUMA_V2
 
+static const struct tst_cgroup *cg;
+
 volatile int end;
 static int *nodes;
 static int nnodes;
@@ -47,15 +49,14 @@ static long count_cpu(void);
 
 static void test_cpuset(void)
 {
-	int child, i, status;
+	int child, i;
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
-	char mems[BUFSIZ], buf[BUFSIZ];
+	char buf[BUFSIZ];
 
-	tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "cpus", buf, BUFSIZ);
-	tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "cpus", buf);
-	tst_cgroup_cpuset_read_files(PATH_TMP_CG_CST, "mems", mems, BUFSIZ);
-	tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "mems", mems);
-	tst_cgroup_move_current(PATH_TMP_CG_CST);
+	SAFE_CGROUP_READ(cg, "cpuset.cpus", buf, sizeof(buf));
+	SAFE_CGROUP_PRINT(cg, "cpuset.cpus", buf);
+	SAFE_CGROUP_READ(cg, "cpuset.mems", buf, sizeof(buf));
+	SAFE_CGROUP_PRINT(cg, "cpuset.mems", buf);
 
 	child = SAFE_FORK();
 	if (child == 0) {
@@ -69,33 +70,30 @@ static void test_cpuset(void)
 		exit(mem_hog_cpuset(ncpus > 1 ? ncpus : 1));
 	}
 
-	snprintf(buf, BUFSIZ, "%d", nodes[0]);
-	tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "mems", buf);
-	snprintf(buf, BUFSIZ, "%d", nodes[1]);
-	tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "mems", buf);
+	SAFE_CGROUP_PRINTF(cg, "cpuset.mems", "%d", nodes[0]);
+	SAFE_CGROUP_PRINTF(cg, "cpuset.mems", "%d", nodes[1]);
 
-	SAFE_WAITPID(child, &status, WUNTRACED | WCONTINUED);
-	if (WEXITSTATUS(status) != 0) {
-		tst_res(TFAIL, "child exit status is %d", WEXITSTATUS(status));
-		return;
-	}
+	tst_reap_children();
 
 	tst_res(TPASS, "cpuset test pass");
 }
 
 static void setup(void)
 {
-	tst_cgroup_mount(TST_CGROUP_CPUSET, PATH_TMP_CG_CST);
+	tst_cgroup_require(TST_CGROUP_CPUSET, NULL);
 	ncpus = count_cpu();
 	if (get_allowed_nodes_arr(NH_MEMS | NH_CPUS, &nnodes, &nodes) < 0)
 		tst_brk(TBROK | TERRNO, "get_allowed_nodes_arr");
 	if (nnodes <= 1)
 		tst_brk(TCONF, "requires a NUMA system.");
+
+	cg = tst_cgroup_get_test();
+	SAFE_CGROUP_PRINTF(cg, "cgroup.procs", "%d", getpid());
 }
 
 static void cleanup(void)
 {
-	tst_cgroup_umount(PATH_TMP_CG_CST);
+	tst_cgroup_cleanup();
 }
 
 static void sighandler(int signo LTP_ATTRIBUTE_UNUSED)
testcases/kernel/mem/include/mem.h +1 -1
index 42b12a230..549507b0d 100644
@@ -61,7 +61,7 @@ void check_hugepage(void);
 void write_memcg(void);
 
 /* cpuset/memcg - include/tst_cgroup.h */
-void write_cpusets(const char *cgroup_dir, long nd);
+void write_cpusets(const struct tst_cgroup *cg, long nd);
 
 /* shared */
 unsigned int get_a_numa_node(void);
testcases/kernel/mem/ksm/ksm02.c +9 -4
index 51f5d4cca..b4c584bab 100644
@@ -59,6 +59,9 @@
 #ifdef HAVE_NUMA_V2
 #include <numaif.h>
 
+static const struct tst_cgroup *cg;
+static const struct tst_cgroup *cg_drain;
+
 static void verify_ksm(void)
 {
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
@@ -76,9 +79,10 @@ static void verify_ksm(void)
 	}
 	create_same_memory(size, num, unit);
 
-	write_cpusets(PATH_TMP_CG_CST, node);
-	tst_cgroup_move_current(PATH_TMP_CG_CST);
+	write_cpusets(cg, node);
+	SAFE_CGROUP_PRINTF(cg, "cgroup.procs", "%d", getpid());
 	create_same_memory(size, num, unit);
+	SAFE_CGROUP_PRINTF(cg_drain, "cgroup.procs", "%d", getpid());
 }
 
 static void cleanup(void)
@@ -87,7 +91,7 @@ static void cleanup(void)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
 				 "%d", merge_across_nodes);
 
-	tst_cgroup_umount(PATH_TMP_CG_CST);
+	tst_cgroup_cleanup();
 }
 
 static void setup(void)
@@ -103,7 +107,8 @@ static void setup(void)
 		SAFE_FILE_PRINTF(PATH_KSM "merge_across_nodes", "1");
 	}
 
-	tst_cgroup_mount(TST_CGROUP_CPUSET, PATH_TMP_CG_CST);
+	tst_cgroup_require(TST_CGROUP_CPUSET, NULL);
+	cg = tst_cgroup_get_test();
 }
 
 static struct tst_test test = {
testcases/kernel/mem/ksm/ksm03.c +8 -4
index e9949280e..032bb607e 100644
@@ -59,10 +59,10 @@
 #include "mem.h"
 #include "ksm_common.h"
 
+static const struct tst_cgroup *cg;
+
 static void verify_ksm(void)
 {
-	tst_cgroup_move_current(PATH_TMP_CG_MEM);
-	tst_cgroup_mem_set_maxbytes(PATH_TMP_CG_MEM, TESTMEM);
 	create_same_memory(size, num, unit);
 }
 
@@ -78,7 +78,11 @@ static void setup(void)
 	}
 
 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
-	tst_cgroup_mount(TST_CGROUP_MEMCG, PATH_TMP_CG_MEM);
+
+	tst_cgroup_require(TST_CGROUP_MEMORY, NULL);
+	cg = tst_cgroup_get_test();
+	SAFE_CGROUP_PRINTF(cg, "cgroup.procs", "%d", getpid());
+	SAFE_CGROUP_PRINTF(cg, "memory.max", "%lu", TESTMEM);
 }
 
 static void cleanup(void)
@@ -86,7 +90,7 @@ static void cleanup(void)
 	if (access(PATH_KSM "merge_across_nodes", F_OK) == 0)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
 				 "%d", merge_across_nodes);
-	tst_cgroup_umount(PATH_TMP_CG_MEM);
+	tst_cgroup_cleanup();
 }
 
 static struct tst_test test = {
testcases/kernel/mem/ksm/ksm04.c +9 -8
index b4ad41831..980e7c99c 100644
@@ -59,6 +59,8 @@
 #ifdef HAVE_NUMA_V2
 #include <numaif.h>
 
+static const struct tst_cgroup *cg;
+
 static void verify_ksm(void)
 {
 	unsigned long nmask[MAXNODES / BITS_PER_LONG] = { 0 };
@@ -67,8 +69,7 @@ static void verify_ksm(void)
 	node = get_a_numa_node();
 	set_node(nmask, node);
 
-	tst_cgroup_move_current(PATH_TMP_CG_MEM);
-	tst_cgroup_mem_set_maxbytes(PATH_TMP_CG_MEM, TESTMEM);
+	SAFE_CGROUP_PRINTF(cg, "memory.max", "%lu", TESTMEM);
 
 	if (set_mempolicy(MPOL_BIND, nmask, MAXNODES) == -1) {
 		if (errno != ENOSYS)
@@ -79,8 +80,7 @@ static void verify_ksm(void)
 	}
 	create_same_memory(size, num, unit);
 
-	write_cpusets(PATH_TMP_CG_CST, node);
-	tst_cgroup_move_current(PATH_TMP_CG_CST);
+	write_cpusets(cg, node);
 	create_same_memory(size, num, unit);
 }
 
@@ -90,8 +90,7 @@ static void cleanup(void)
 		FILE_PRINTF(PATH_KSM "merge_across_nodes",
 				 "%d", merge_across_nodes);
 
-	tst_cgroup_umount(PATH_TMP_CG_MEM);
-	tst_cgroup_umount(PATH_TMP_CG_CST);
+	tst_cgroup_cleanup();
 }
 
 static void setup(void)
@@ -107,8 +106,10 @@ static void setup(void)
 
 	parse_ksm_options(opt_sizestr, &size, opt_numstr, &num, opt_unitstr, &unit);
 
-	tst_cgroup_mount(TST_CGROUP_MEMCG, PATH_TMP_CG_MEM);
-	tst_cgroup_mount(TST_CGROUP_CPUSET, PATH_TMP_CG_CST);
+	tst_cgroup_require(TST_CGROUP_MEMORY, NULL);
+	tst_cgroup_require(TST_CGROUP_CPUSET, NULL);
+	cg = tst_cgroup_get_test();
+	SAFE_CGROUP_PRINTF(cg, "cgroup.procs", "%d", getpid());
 }
 
 static struct tst_test test = {
testcases/kernel/mem/lib/mem.c +4 -6
index 2de3f83a6..2af2ef20c 100644
@@ -629,13 +629,11 @@ static void gather_node_cpus(char *cpus, long nd)
 	cpus[strlen(cpus) - 1] = '\0';
 }
 
-void write_cpusets(const char *cgroup_dir, long nd)
+void write_cpusets(const struct tst_cgroup *cg, long nd)
 {
-	char buf[BUFSIZ];
 	char cpus[BUFSIZ] = "";
 
-	snprintf(buf, BUFSIZ, "%ld", nd);
-	tst_cgroup_cpuset_write_files(cgroup_dir, "mems", buf);
+	SAFE_CGROUP_PRINTF(cg, "cpuset.mems", "%ld", nd);
 
 	gather_node_cpus(cpus, nd);
 	/*
@@ -644,11 +642,11 @@ void write_cpusets(const char *cgroup_dir, long nd)
 	 * the value of cpuset.cpus.
 	 */
 	if (strlen(cpus) != 0) {
-		tst_cgroup_cpuset_write_files(cgroup_dir, "cpus", cpus);
+		SAFE_CGROUP_PRINT(cg, "cpuset.cpus", cpus);
 	} else {
 		tst_res(TINFO, "No CPUs in the node%ld; "
 				"using only CPU0", nd);
-		tst_cgroup_cpuset_write_files(cgroup_dir, "cpus", "0");
+		SAFE_CGROUP_PRINT(cg, "cpuset.cpus", "0");
 	}
 }
 
testcases/kernel/mem/oom/oom03.c +10 -8
index fc860c660..cd927ba11 100644
@@ -36,19 +36,17 @@
 
 #ifdef HAVE_NUMA_V2
 
+static const struct tst_cgroup *cg;
+
 static void verify_oom(void)
 {
 #ifdef TST_ABI32
 	tst_brk(TCONF, "test is not designed for 32-bit system.");
 #endif
-
-	tst_cgroup_move_current(PATH_TMP_CG_MEM);
-	tst_cgroup_mem_set_maxbytes(PATH_TMP_CG_MEM, TESTMEM);
-
 	testoom(0, 0, ENOMEM, 1);
 
-	if (tst_cgroup_mem_swapacct_enabled(PATH_TMP_CG_MEM)) {
-		tst_cgroup_mem_set_maxswap(PATH_TMP_CG_MEM, TESTMEM);
+	if (SAFE_CGROUP_HAS(cg, "memory.swap.max")) {
+		SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%lu", TESTMEM);
 		testoom(0, 1, ENOMEM, 1);
 	}
 
@@ -65,14 +63,18 @@ static void setup(void)
 {
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
-	tst_cgroup_mount(TST_CGROUP_MEMCG, PATH_TMP_CG_MEM);
+
+	tst_cgroup_require(TST_CGROUP_MEMORY, NULL);
+	cg = tst_cgroup_get_test();
+	SAFE_CGROUP_PRINTF(cg, "cgroup.procs", "%d", getpid());
+	SAFE_CGROUP_PRINTF(cg, "memory.max", "%lu", TESTMEM);
 }
 
 static void cleanup(void)
 {
 	if (overcommit != -1)
 		set_sys_tune("overcommit_memory", overcommit, 0);
-	tst_cgroup_umount(PATH_TMP_CG_MEM);
+	tst_cgroup_cleanup();
 }
 
 static struct tst_test test = {
testcases/kernel/mem/oom/oom04.c +11 -8
index 582663275..bad7b582b 100644
@@ -36,24 +36,25 @@
 
 #ifdef HAVE_NUMA_V2
 
+static const struct tst_cgroup *cg;
+
 static void verify_oom(void)
 {
 #ifdef TST_ABI32
 	tst_brk(TCONF, "test is not designed for 32-bit system.");
 #endif
-
-	tst_cgroup_move_current(PATH_TMP_CG_CST);
-
 	tst_res(TINFO, "OOM on CPUSET...");
 	testoom(0, 0, ENOMEM, 1);
 
-	if (is_numa(NULL, NH_MEMS, 2)) {
+	if (is_numa(NULL, NH_MEMS, 2) &&
+	    SAFE_CGROUP_HAS(cg, "cpuset.memory_migrate")) {
 		/*
 		 * Under NUMA system, the migration of cpuset's memory
 		 * is in charge of cpuset.memory_migrate, we can write
 		 * 1 to cpuset.memory_migrate to enable the migration.
 		 */
-		tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "memory_migrate", "1");
+		SAFE_CGROUP_PRINT(cg, "cpuset.memory_migrate", "1");
+
 		tst_res(TINFO, "OOM on CPUSET with mem migrate:");
 		testoom(0, 0, ENOMEM, 1);
 	}
@@ -69,7 +70,8 @@ static void setup(void)
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
 
-	tst_cgroup_mount(TST_CGROUP_CPUSET, PATH_TMP_CG_CST);
+	tst_cgroup_require(TST_CGROUP_CPUSET, NULL);
+	cg = tst_cgroup_get_test();
 
 	/*
 	 * Some nodes do not contain memory, so use
@@ -81,14 +83,15 @@ static void setup(void)
 	if (ret < 0)
 		tst_brk(TBROK, "Failed to get a memory node "
 			      "using get_allowed_nodes()");
-	write_cpusets(PATH_TMP_CG_CST, memnode);
+	write_cpusets(cg, memnode);
+	SAFE_CGROUP_PRINTF(cg, "cgroup.procs", "%d", getpid());
 }
 
 static void cleanup(void)
 {
 	if (overcommit != -1)
 		set_sys_tune("overcommit_memory", overcommit, 0);
-	tst_cgroup_umount(PATH_TMP_CG_CST);
+	tst_cgroup_cleanup();
 }
 
 static struct tst_test test = {
testcases/kernel/mem/oom/oom05.c +19 -13
index 871f302e3..3be765df9 100644
@@ -36,6 +36,8 @@
 
 #ifdef HAVE_NUMA_V2
 
+static const struct tst_cgroup *cg;
+
 static void verify_oom(void)
 {
 #ifdef TST_ABI32
@@ -43,9 +45,6 @@ static void verify_oom(void)
 #endif
 
 	tst_res(TINFO, "OOM on CPUSET & MEMCG...");
-	tst_cgroup_move_current(PATH_TMP_CG_MEM);
-	tst_cgroup_move_current(PATH_TMP_CG_CST);
-	tst_cgroup_mem_set_maxbytes(PATH_TMP_CG_MEM, TESTMEM);
 	testoom(0, 0, ENOMEM, 1);
 
 	/*
@@ -53,22 +52,26 @@ static void verify_oom(void)
 	 * is in charge of cpuset.memory_migrate, we can write
 	 * 1 to cpuset.memory_migrate to enable the migration.
 	 */
-	if (is_numa(NULL, NH_MEMS, 2)) {
-		tst_cgroup_cpuset_write_files(PATH_TMP_CG_CST, "memory_migrate", "1");
+	if (is_numa(NULL, NH_MEMS, 2) &&
+	    SAFE_CGROUP_HAS(cg, "cpuset.memory_migrate")) {
+		SAFE_CGROUP_PRINT(cg, "cpuset.memory_migrate", "1");
 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
 				"cpuset.memory_migrate=1");
 		testoom(0, 0, ENOMEM, 1);
 	}
 
-	if (tst_cgroup_mem_swapacct_enabled(PATH_TMP_CG_MEM)) {
+	if (SAFE_CGROUP_HAS(cg, "memory.swap.max")) {
 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
 				"special memswap limitation:");
-		tst_cgroup_mem_set_maxswap(PATH_TMP_CG_MEM, TESTMEM);
+		SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%lu", TESTMEM);
 		testoom(0, 0, ENOMEM, 1);
 
 		tst_res(TINFO, "OOM on CPUSET & MEMCG with "
 				"disabled memswap limitation:");
-		tst_cgroup_mem_set_maxswap(PATH_TMP_CG_MEM, -1);
+		if (SAFE_CGROUP_VER(cg, "memory") == TST_CGROUP_V1)
+			SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%lu", ~0UL);
+		else
+			SAFE_CGROUP_PRINT(cg, "memory.swap.max", "max");
 		testoom(0, 0, ENOMEM, 1);
 	}
 }
@@ -83,8 +86,9 @@ void setup(void)
 	overcommit = get_sys_tune("overcommit_memory");
 	set_sys_tune("overcommit_memory", 1, 1);
 
-	tst_cgroup_mount(TST_CGROUP_MEMCG, PATH_TMP_CG_MEM);
-	tst_cgroup_mount(TST_CGROUP_CPUSET, PATH_TMP_CG_CST);
+	tst_cgroup_require(TST_CGROUP_MEMORY, NULL);
+	tst_cgroup_require(TST_CGROUP_CPUSET, NULL);
+	cg = tst_cgroup_get_test();
 
 	/*
 	 * Some nodes do not contain memory, so use
@@ -96,15 +100,17 @@ void setup(void)
 	if (ret < 0)
 		tst_brk(TBROK, "Failed to get a memory node "
 			      "using get_allowed_nodes()");
-	write_cpusets(PATH_TMP_CG_CST, memnode);
+
+	write_cpusets(cg, memnode);
+	SAFE_CGROUP_PRINTF(cg, "cgroup.procs", "%d", getpid());
+	SAFE_CGROUP_PRINTF(cg, "memory.max", "%lu", TESTMEM);
 }
 
 void cleanup(void)
 {
 	if (overcommit != -1)
 		set_sys_tune("overcommit_memory", overcommit, 0);
-	tst_cgroup_umount(PATH_TMP_CG_MEM);
-	tst_cgroup_umount(PATH_TMP_CG_CST);
+	tst_cgroup_cleanup();
 }
 
 static struct tst_test test = {
Signature · 1 line
2.30.2
#21 lore raw

PATCH v3 7/7 madvise06: Convert to new CGroups API

In reply to Richard Palethorpe
---
 testcases/kernel/syscalls/madvise/madvise06.c | 82 +++++++++----------
 1 file changed, 37 insertions(+), 45 deletions(-)
testcases/kernel/syscalls/madvise/madvise06.c +37 -45
index 56e0700d6..47642faf4 100644
@@ -3,8 +3,8 @@
  * Copyright (c) 2016 Red Hat, Inc.
  */
 
-/*
- * DESCRIPTION
+/*\
+ * [DESCRIPTION]
  *
  *   Page fault occurs in spite that madvise(WILLNEED) system call is called
  *   to prefetch the page. This issue is reproduced by running a program
@@ -36,13 +36,14 @@
  *   else unexpected, but irrelevant procedure, registers a fault to
  *   our process.
  *
- */
+\*/
 
 #include <errno.h>
 #include <stdio.h>
 #include <sys/mount.h>
 #include <sys/sysinfo.h>
 #include "tst_test.h"
+#include "tst_cgroup.h"
 
 #define CHUNK_SZ (400*1024*1024L)
 #define MEM_LIMIT (CHUNK_SZ / 2)
@@ -50,8 +51,7 @@
 #define PASS_THRESHOLD (CHUNK_SZ / 4)
 #define PASS_THRESHOLD_KB (PASS_THRESHOLD / 1024)
 
-#define MNT_NAME "memory"
-#define GROUP_NAME "madvise06"
+static const struct tst_cgroup *cg;
 
 static const char drop_caches_fname[] = "/proc/sys/vm/drop_caches";
 static int pg_sz, stat_refresh_sup;
@@ -64,17 +64,19 @@ static void check_path(const char *path)
 		tst_brk(TCONF, "file needed: %s", path);
 }
 
-#define READ_CGMEM(item)						\
-	({long tst_rval = 0;						\
-	  const char *cgpath = MNT_NAME"/"GROUP_NAME"/memory."item;	\
-	  if (!access(cgpath, R_OK))					\
-		  SAFE_FILE_LINES_SCANF(cgpath, "%ld", &tst_rval);	\
-	  tst_rval;})
+static void print_cgmem(const char *name)
+{
+	long ret;
+
+	if (!SAFE_CGROUP_HAS(cg, name))
+		return;
+
+	SAFE_CGROUP_SCANF(cg, name, "%ld", &ret);
+	tst_res(TINFO, "\t%s: %ld Kb", name, ret / 1024);
+}
 
 static void meminfo_diag(const char *point)
 {
-	long rval;
-
 	if (stat_refresh_sup)
 		SAFE_FILE_PRINTF("/proc/sys/vm/stat_refresh", "1");
 
@@ -85,16 +87,10 @@ static void meminfo_diag(const char *point)
 		SAFE_READ_MEMINFO("SwapCached:") - init_swap_cached);
 	tst_res(TINFO, "\tCached: %ld Kb",
 		SAFE_READ_MEMINFO("Cached:") - init_cached);
-	tst_res(TINFO, "\tcgmem.usage_in_bytes: %ld Kb",
-		READ_CGMEM("usage_in_bytes") / 1024);
-
-	rval = READ_CGMEM("memsw.usage_in_bytes") / 1024;
-	if (rval)
-		tst_res(TINFO, "\tcgmem.memsw.usage_in_bytes: %ld Kb", rval);
 
-	rval = READ_CGMEM("kmem.usage_in_bytes") / 1024;
-	if (rval)
-		tst_res(TINFO, "\tcgmem.kmem.usage_in_bytes: %ld Kb", rval);
+	print_cgmem("memory.current");
+	print_cgmem("memory.swap.current");
+	print_cgmem("memory.kmem.usage_in_bytes");
 }
 
 static void setup(void)
@@ -117,28 +113,24 @@ static void setup(void)
 			2 * CHUNK_SZ);
 	}
 
-	SAFE_MKDIR(MNT_NAME, 0700);
-	if (mount("memory", MNT_NAME, "cgroup", 0, "memory") == -1) {
-		if (errno == ENODEV || errno == ENOENT)
-			tst_brk(TCONF, "memory cgroup needed");
-	}
-	SAFE_MKDIR(MNT_NAME"/"GROUP_NAME, 0700);
-
 	check_path("/proc/self/oom_score_adj");
-	check_path(MNT_NAME"/"GROUP_NAME"/memory.limit_in_bytes");
-	check_path(MNT_NAME"/"GROUP_NAME"/memory.swappiness");
-	check_path(MNT_NAME"/"GROUP_NAME"/tasks");
-
 	SAFE_FILE_PRINTF("/proc/self/oom_score_adj", "%d", -1000);
-	SAFE_FILE_PRINTF(MNT_NAME"/"GROUP_NAME"/memory.limit_in_bytes", "%ld\n",
-			 MEM_LIMIT);
 
-	if (!access(MNT_NAME"/"GROUP_NAME"/memory.memsw.limit_in_bytes", W_OK)) {
-		SAFE_FILE_PRINTF(MNT_NAME"/"GROUP_NAME"/memory.memsw.limit_in_bytes",
-				 "%ld\n", MEMSW_LIMIT);
+	tst_cgroup_require(TST_CGROUP_MEMORY, NULL);
+	cg = tst_cgroup_get_test();
+
+	SAFE_CGROUP_PRINTF(cg, "memory.max", "%ld", MEM_LIMIT);
+	if (SAFE_CGROUP_HAS(cg, "memory.swap.max"))
+		SAFE_CGROUP_PRINTF(cg, "memory.swap.max", "%ld", MEMSW_LIMIT);
+
+	if (SAFE_CGROUP_HAS(cg, "memory.swappiness")) {
+		SAFE_CGROUP_PRINT(cg, "memory.swappiness", "60");
+	} else {
+		check_path("/proc/sys/vm/swappiness");
+		SAFE_FILE_PRINTF("/proc/sys/vm/swappiness", "%d", 60);
 	}
-	SAFE_FILE_PRINTF(MNT_NAME"/"GROUP_NAME"/memory.swappiness", "60");
-	SAFE_FILE_PRINTF(MNT_NAME"/"GROUP_NAME"/tasks", "%d\n", getpid());
+
+	SAFE_CGROUP_PRINTF(cg, "cgroup.procs", "%d", getpid());
 
 	meminfo_diag("Initial meminfo, later values are relative to this (except memcg)");
 	init_swap = SAFE_READ_MEMINFO("SwapTotal:") - SAFE_READ_MEMINFO("SwapFree:");
@@ -154,11 +146,7 @@ static void setup(void)
 
 static void cleanup(void)
 {
-	if (!access(MNT_NAME"/tasks", F_OK)) {
-		SAFE_FILE_PRINTF(MNT_NAME"/tasks", "%d\n", getpid());
-		SAFE_RMDIR(MNT_NAME"/"GROUP_NAME);
-		SAFE_UMOUNT(MNT_NAME);
-	}
+	tst_cgroup_cleanup();
 }
 
 static void dirty_pages(char *ptr, long size)
@@ -248,6 +236,10 @@ static struct tst_test test = {
 	.min_kver = "3.10.0",
 	.needs_tmpdir = 1,
 	.needs_root = 1,
+	.save_restore = (const char * const[]) {
+		"?/proc/sys/vm/swappiness",
+		NULL
+	},
 	.tags = (const struct tst_tag[]) {
 		{"linux-git", "55231e5c898c"},
 		{"linux-git", "8de15e920dc8"},
Signature · 1 line
2.30.2