Skip to contentSkip to conversation

CGroup API rewrite

RFC PATCH Superseded Richard Palethorpe 4 participants 13 messages Mar 2, 2021 – Mar 9, 2021 Library code

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover CGroup API rewrite

Hello,

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

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:

* Some objects are allocated in guarded buffers, some with malloc, others are static. This is partially because some of the struct types are static. It might be better to expose them all to the test executable so it can allocate them statitically or use guarded buffers for everything.

* ksm02 segfaults, although this does not appear to be caused by the CGroup API.

* cpuset01 triggers an ASAN splat, which again does not appear to be caused by the new API.

* There are probably other tests like madvise06 which mount CGroups in an ad-hoc way and need to be converted to the new API.

Richard Palethorpe (7):
API: Add safe openat, printfat, readat and unlinkat
API: Add macro for the container_of trick Add new CGroups Core and Item 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               |  165 ++-
 include/tst_cgroup.h                          |  124 +-
 include/tst_cgroup_core.h                     |   58 +
 include/tst_cgroup_item.h                     |  139 ++
 include/tst_common.h                          |    5 +
 include/tst_safe_file_ops.h                   |   39 +
 include/tst_test.h                            |    1 -
 lib/newlib_tests/.gitignore                   |    2 +
 lib/newlib_tests/test21.c                     |   43 +-
 lib/newlib_tests/tst_cgroup01.c               |   51 +
 lib/newlib_tests/tst_cgroup02.c               |   87 ++
 lib/tst_cgroup.c                              | 1258 ++++++++++++-----
 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 |   83 +-
 23 files changed, 1878 insertions(+), 505 deletions(-)
 create mode 100644 include/tst_cgroup_core.h
 create mode 100644 include/tst_cgroup_item.h
 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.0
#1 lore raw

RFC PATCH v2 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.0
#2 lore raw

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

Hi Richard,

Richard Palethorpe <[email protected]> wrote:

...

Quoted 15 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);

(quoted diff) +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);

I guess these prototypes should be moved to "safe_file_ops_fn.h"? (It seems the author did that on purpose to separate macros and function declaration)

--- /dev/null

Quoted 2 lines

+++ b/lib/tst_safe_file_ops.c

Then we can achieve all the above functions in "lib/safe_file_ops.c" without creating a new file.

Quoted 180 lines
lib/tst_safe_file_ops.c new file +53 -0
@@ -0,0 +1,171 @@
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include "lapi/fcntl.h"
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+
+char fd_path[PATH_MAX];
+
+char *tst_decode_fd(int fd)
+{
+       ssize_t ret;
+       char proc_path[32];
+
+       if (fd < 0)
+               return "!";
+
+       sprintf(proc_path, "/proc/self/fd/%d", fd);
+       ret = readlink(proc_path, fd_path, sizeof(fd_path));
+
+       if (ret < 0)
+               return "?";
+
+       fd_path[ret] = '\0';
+
+       return fd_path;
+}
+
+int safe_openat(const char *file, const int lineno,
+               int dirfd, const char *path, int oflags, ...)
+{
+       va_list ap;
+       int fd;
+       mode_t mode;
+
+       va_start(ap, oflags);
+       mode = va_arg(ap, int);
+       va_end(ap);
+
+       fd = openat(dirfd, path, oflags, mode);
+       if (fd > -1)
+               return fd;
+
+       tst_brk_(file, lineno, TBROK | TERRNO,
+                "openat(%d<%s>, '%s', %o, %o)",
+                dirfd, tst_decode_fd(dirfd), path, oflags, mode);
+
+       return fd;
+}
+
+ssize_t safe_file_readat(const char *file, const int lineno,
+                        int dirfd, const char *path, char *buf, size_t

nbyte)

lib/tst_safe_file_ops.c new file +24 -0
+{
+       int fd = safe_openat(file, lineno, dirfd, path, O_RDONLY);
+       ssize_t rval;
+
+       if (fd < 0)
+               return -1;
+
+       rval = safe_read(file, lineno, NULL, 0, fd, buf, nbyte - 1);
+       if (rval < 0)
+               return -1;
+
+       close(fd);
+       buf[rval] = '\0';
+
+       if (rval >= (ssize_t)nbyte - 1) {
+               tst_brk_(file, lineno, TBROK,
+                       "Buffer length %zu too small to read %d<%s>/%s",
+                       nbyte, dirfd, tst_decode_fd(dirfd), path);
+       }
+
+       return rval;
+}
+
+int tst_file_vprintfat(int dirfd, const char *path, const char *fmt,

va_list va)

lib/tst_safe_file_ops.c new file +90 -0
+{
+       int fd = openat(dirfd, path, O_WRONLY);
+
+       if (fd < 0)
+               return -1;
+
+       TEST(vdprintf(fd, fmt, va));
+       close(fd);
+
+       if (TST_RET < 0) {
+               errno = TST_ERR;
+               return -2;
+       }
+
+       return TST_RET;
+}
+
+int tst_file_printfat(int dirfd, const char *path, const char *fmt, ...)
+{
+       va_list va;
+       int rval;
+
+       va_start(va, fmt);
+       rval = tst_file_vprintfat(dirfd, path, fmt, va);
+       va_end(va);
+
+       return rval;
+}
+
+int safe_file_vprintfat(const char *file, const int lineno,
+                       int dirfd, const char *path,
+                       const char *fmt, va_list va)
+{
+       char buf[16];
+       va_list vac;
+       int rval;
+
+       va_copy(vac, va);
+
+       TEST(tst_file_vprintfat(dirfd, path, fmt, va));
+
+       if (TST_RET == -2) {
+               rval = vsnprintf(buf, sizeof(buf), fmt, vac);
+               va_end(vac);
+
+               if (rval >= (ssize_t)sizeof(buf))
+                       strcpy(buf + sizeof(buf) - 5, "...");
+
+               tst_brk_(file, lineno, TBROK | TTERRNO,
+                        "vdprintf(%d<%s>, '%s', '%s'<%s>)",
+                        dirfd, tst_decode_fd(dirfd), path, fmt,
+                        rval > 0 ? buf : "???");
+               return -1;
+       }
+
+       va_end(vac);
+
+       if (TST_RET == -1) {
+               tst_brk_(file, lineno, TBROK | TTERRNO,
+                       "openat(%d<%s>, '%s', O_WRONLY)",
+                       dirfd, tst_decode_fd(dirfd), path);
+       }
+
+       return TST_RET;
+}
+
+int safe_file_printfat(const char *file, const int lineno,
+                      int dirfd, const char *path,
+                      const char *fmt, ...)
+{
+       va_list va;
+       int rval;
+
+       va_start(va, fmt);
+       rval = safe_file_vprintfat(file, lineno, dirfd, path, fmt, va);
+       va_end(va);
+
+       return rval;
+}
+
+int safe_unlinkat(const char *file, const int lineno,
+                 int dirfd, const char *path, int flags)
+{
+       int rval = unlinkat(dirfd, path, flags);
+
+       if (rval > -1)
+               return rval;
+
+       tst_brk_(file, lineno, TBROK | TERRNO,
+                "unlinkat(%d<%s>, '%s', %s)", dirfd,

tst_decode_fd(dirfd), path,
+ flags == AT_REMOVEDIR ? "AT_REMOVEDIR" : (flags ? "?" :
"0"));

lib/tst_safe_file_ops.c new file +3 -1
+
+       return rval;
+}
--

2.30.0

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

RFC PATCH v2 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.0
#4 lore raw

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

Quoted 10 lines · Richard Palethorpe
---
 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 87

Quoted 5 lines
include/tst_common.h +1 -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) ({              \
 

What about using uppercase here? i.e TST_CONTAINER_OF(...)

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

#endif /* TST_COMMON_H__ */
--
2.30.0

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

RFC PATCH v2 3/7 Add new CGroups Core and Item 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      |  124 ++--
 include/tst_cgroup_core.h |   58 ++
 include/tst_cgroup_item.h |  139 ++++
 include/tst_test.h        |    1 -
 lib/tst_cgroup.c          | 1258 +++++++++++++++++++++++++++----------
 5 files changed, 1224 insertions(+), 356 deletions(-)
 create mode 100644 include/tst_cgroup_core.h
 create mode 100644 include/tst_cgroup_item.h
include/tst_cgroup.h +88 -36
index bfd848260..3263537c2 100644
@@ -2,46 +2,98 @@
 /*
  * Copyright (c) 2020 Red Hat, Inc.
  * Copyright (c) 2020 Li Wang <[email protected]>
+ * Copyright (c) 2020-2021 SUSE LLC <[email protected]>
  */
+/*\
+ * [DESCRIPTION]
+ *
+ * The LTP Control Groups library can be seperated into two parts.
+ * - CGroup Core
+ * - CGroup Item
+ *
+ * The CGroup Core is where we scan the current system, build a model
+ * of the current CGroup configuration, then add missing configuration
+ * necessary for the LTP or give up. Once testing is finished it then
+ * also attempts cleanup.
+ *
+ * The CGroup Item interface is mostly what the test author interacts
+ * with. It mirrors the CGroups V2 (unified) kernel API. So even if
+ * the system has some nasty V1 configuration, the test author may
+ * write their code as if for V2 (with exceptions). It builds on the
+ * model created by CGroup Core.
+ *
+ * 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 the same 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.
+ *
+ * In some cases 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"
-
-enum tst_cgroup_ver {
-	TST_CGROUP_V1 = 1,
-	TST_CGROUP_V2 = 2,
-};
-
-enum tst_cgroup_ctrl {
-	TST_CGROUP_MEMCG = 1,
-	TST_CGROUP_CPUSET = 2,
-	/* add cgroup controller */
-};
-
-enum tst_cgroup_ver tst_cgroup_version(void);
-
-/* 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);
-
-/* To move current process PID to the mounted cgroup tasks */
-void tst_cgroup_move_current(const char *cgroup_dir);
-
-/* To set cgroup controller knob with new value */
-void tst_cgroup_set_knob(const char *cgroup_dir, const char *knob, long value);
-
-/* 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);
-
-/* 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);
+#include "tst_cgroup_core.h"
+#include "tst_cgroup_item.h"
 
 #endif /* TST_CGROUP_H */
include/tst_cgroup_core.h new file +58 -0

58 lines not on this page · raw message

include/tst_cgroup_item.h new file +139 -0

139 lines not on this page · raw message

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/tst_cgroup.c +939 -319

1,392 lines not on this page · raw message

Signature · 1 line
2.30.0
#6 lore raw

RFC PATCH v2 3/7 Add new CGroups Core and Item APIs

Hi Richard,

Thanks for your refactor, this is much better than what I have written before:).

But I have to say this version is complicated since there are many interlaced structures defined, especially for the cgroup-item part. And my concern is that it might unfriendly to maintainers and users.

Anyway, I still need time to understand the whole part and will try to reduce something to make it more simplified if I can.

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

RFC PATCH v2 4/7 Add new CGroups API library tests

In reply to Richard Palethorpe
---
 lib/newlib_tests/.gitignore     |  2 +
 lib/newlib_tests/test21.c       | 43 +++++++---------
 lib/newlib_tests/tst_cgroup01.c | 51 +++++++++++++++++++
 lib/newlib_tests/tst_cgroup02.c | 87 +++++++++++++++++++++++++++++++++
 4 files changed, 158 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 +18 -25

72 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 +87 -0

87 lines not on this page · raw message

Signature · 1 line
2.30.0
#8 lore raw

RFC PATCH v2 5/7 docs: Update CGroups API

In reply to Richard Palethorpe
---
 doc/test-writing-guidelines.txt | 165 ++++++++++++++++++++++++++++++--
 1 file changed, 156 insertions(+), 9 deletions(-)
doc/test-writing-guidelines.txt +156 -9
index dd1911ceb..7978c3904 100644
@@ -2165,48 +2165,195 @@ 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
+
+Some of LTP tests need Control Groups in their configurations, tst_cgroup.h provides
+APIs to discover and use CGroups. 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
+Considering there are many differences between cgroup v1 and v2, here we encapsulate
 the detail of cgroup mounting in high-level functions, which will be easier to use
-cgroup without caring about too much technical thing.? ?
+cgroup without caring about too much technical thing.
 
 Also, we do cgroup mount/umount work for the different hierarchy automatically.
+If that is required.
 
 [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_default();
+	SAFE_CGROUP_PRINTF(&cg->cgroup.procs, "%d", getpid());
+	SAFE_CGROUP_PRINTF(&cg->memory.max, "%lu", MEMSIZE);
+	if (TST_CGROUP_HAS(&cg->memory.swap))
+		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_default' 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 (cg->memory.ver != TST_CGROUP_V1)
+		SAFE_CGROUP_PRINT(&cg->cgroup.subtree_control, "+memory");
+	if (cg->cpuset.ver != TST_CGROUP_V1)
+		SAFE_CGROUP_PRINT(&cg->cgroup.subtree_control, "+cpuset");
+
+	if (!SAFE_FORK()) {
+		SAFE_CGROUP_PRINTF(&cg_child->cgroup.procs, "%d", getpid());
+
+		if (TST_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_default();
+	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 processe 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 (cg->memory.ver == 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 however.
+
 2.2.37 Require minimum numbers of CPU for a testcase
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 
Signature · 1 line
2.30.0
#9 lore raw

RFC PATCH v2 5/7 docs: Update CGroups API

Hi!

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

Quoted 6 lines
doc/test-writing-guidelines.txt +6 -0
+static void setup(void)
+{
+	tst_cgroup_require(TST_CGROUP_MEMORY, NULL);
+	cg = tst_cgroup_get_default();
+	SAFE_CGROUP_PRINTF(&cg->cgroup.procs, "%d", getpid());
+	SAFE_CGROUP_PRINTF(&cg->memory.max, "%lu", MEMSIZE);

I've been looking at the API for a while and I do not understand why we need to create the tst_cgroup_files for each supported controller and each node in the hierarchy. Why can't we make this more driver-like? All we need to know is the controller type, cgroup version and file we want to read/write.

If I were designing the API I would have made these so that they take a pointer to a cgroup node, controller type and filename, then the printf-like formatting.

So the end result would look like:

	SAFE_CGROUP_PRINTF(cg, TST_CGROUP_MEMORY, "memory.max", "%lu", MEMSIZE);

Which would be build on the top of:

int tst_cgroup_open(struct tst_cgroup *cg, enum tst_cgroup_ctrl ctrl, const char *fname);

And instead of storing the file structures into the tst_cgroup structure we would translate the v2 to v1 on the fly. We would have to open and close the files on each printf/scanf but I do not think that it's unreasonable given the simplification of the interface.

We would end up with a simple translation tables for different controllers instead of the structures that are allocated for each node then, such as:

static const struct cgroup_map cgroup_memory_map[] = {
	{"memory.usage_in_bytes", "memory.current"},
	{"memory.limit_in_bytes", "memory.max"},
	{"memory.memsw.usage_in_bytes", "memory.swap.current"},
	{"memory.memsw.limitin_bytes", "memory.swap.max"},
	{}
};

Or is there a reason why this cannot be done so?

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

RFC PATCH v2 5/7 docs: Update CGroups API

Hello,

Quoted 14 lines · Cyril Hrubis

Hi!

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

Quoted 6 lines
doc/test-writing-guidelines.txt +6 -0
+static void setup(void)
+{
+	tst_cgroup_require(TST_CGROUP_MEMORY, NULL);
+	cg = tst_cgroup_get_default();
+	SAFE_CGROUP_PRINTF(&cg->cgroup.procs, "%d", getpid());
+	SAFE_CGROUP_PRINTF(&cg->memory.max, "%lu", MEMSIZE);

I've been looking at the API for a while and I do not understand why we need to create the tst_cgroup_files for each supported controller and each node in the hierarchy. Why can't we make this more driver-like? All we need to know is the controller type, cgroup version and file we want to read/write.

Quoted 7 lines

If I were designing the API I would have made these so that they take a pointer to a cgroup node, controller type and filename, then the printf-like formatting.

So the end result would look like:

	SAFE_CGROUP_PRINTF(cg, TST_CGROUP_MEMORY, "memory.max", "%lu", MEMSIZE);

If we use V2 naming the controller is always specified in the file name. So we can just write.

SAFE_CGROUP_PRINTF(cg, "memory.max", ...);

So both you and Li Wang think this is too complex and I wasn't sure, so I will respin it with a lookup table instead.

Quoted 27 lines

Which would be build on the top of:

int tst_cgroup_open(struct tst_cgroup *cg, enum tst_cgroup_ctrl ctrl, const char *fname);

And instead of storing the file structures into the tst_cgroup structure we would translate the v2 to v1 on the fly. We would have to open and close the files on each printf/scanf but I do not think that it's unreasonable given the simplification of the interface.

We would end up with a simple translation tables for different controllers instead of the structures that are allocated for each node then, such as:

static const struct cgroup_map cgroup_memory_map[] = {
	{"memory.usage_in_bytes", "memory.current"},
	{"memory.limit_in_bytes", "memory.max"},
	{"memory.memsw.usage_in_bytes", "memory.swap.current"},
	{"memory.memsw.limitin_bytes", "memory.swap.max"},
	{}
};

Or is there a reason why this cannot be done so?

Signature · 2 lines
Cyril Hrubis
[email protected]
Signature · 2 lines
Thank you,
Richard.
#11 lore raw

RFC PATCH v2 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..9771fb952 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_default();
+	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..fe6d7a096 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_default();
 }
 
 static struct tst_test test = {
testcases/kernel/mem/ksm/ksm03.c +8 -4
index e9949280e..1fde489f6 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_default();
+	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..56d9ae28f 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_default();
+	SAFE_CGROUP_PRINTF(&cg->cgroup.procs, "%d", getpid());
 }
 
 static struct tst_test test = {
testcases/kernel/mem/lib/mem.c +4 -6
index 2de3f83a6..b980daf48 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..abc627833 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 (TST_CGROUP_HAS(&cg->memory.swap)) {
+		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_default();
+	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..acffb0436 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) &&
+	    TST_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_default();
 
 	/*
 	 * 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..7a7959d79 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) &&
+	    TST_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 (TST_CGROUP_HAS(&cg->memory.swap)) {
 		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 (cg->memory.ver == 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_default();
 
 	/*
 	 * 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.0
#12 lore raw

RFC PATCH v2 7/7 madvise06: Convert to new CGroups API

In reply to Richard Palethorpe
---
 testcases/kernel/syscalls/madvise/madvise06.c | 83 +++++++++----------
 1 file changed, 38 insertions(+), 45 deletions(-)
testcases/kernel/syscalls/madvise/madvise06.c +38 -45
index 2099df6c3..209066d77 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,20 @@ static void check_path(const char *path)
 		tst_brk(TCONF, "file needed: %s\n", 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 struct tst_cgroup_file *file)
+{
+	const char *name = file->locations[0].name;
+	long ret;
+
+	if (!TST_CGROUP_HAS(file))
+		return;
+
+	SAFE_CGROUP_SCANF(file, "%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 +88,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(&cg->memory.current);
+	print_cgmem(&cg->memory.swap.current);
+	print_cgmem(&cg->memory.kmem.current);
 }
 
 static void setup(void)
@@ -117,28 +114,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_default();
+
+	SAFE_CGROUP_PRINTF(&cg->memory.max, "%ld", MEM_LIMIT);
+	if (TST_CGROUP_HAS(&cg->memory.swap))
+		SAFE_CGROUP_PRINTF(&cg->memory.swap.max, "%ld", MEMSW_LIMIT);
+
+	if (TST_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 +147,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 +237,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.0