CGroup API rewrite
v4 cover CGroup API rewrite
Hello,
This is a complete rewrite of the CGroups API. To understand why this is so complicated, please see the commments in tst_cgroup.h and tst_cgroup.c.
V4:
* Move openat based helpers to tst_safe_file_at.h.
* Switch to userland naming of controllers (ctrl for short) instead of kernel's css (CGroup subsystem).
* Use more descriptive names such as dir_fd and tst_cgroup_group. Note that we discussed calling it tst_cgroup_node. However in the end I thought this was too generic and instead went with repetition.
* Split cgroup_item into cgroup_file and cgroup_ctrl. Also make the lookup tree definition more verbose to avoid the missing field warnings and setting the ctrl index at runtime.
* make enum tst_cgroup_ctrl private; use controller name in tst_cgroup_require.
* make struct tst_cgroup_dir private.
* Eliminate use of cgroup_get_ctrl and pass the ctrl struct around instead of the indx.
* Deleted test21 as it is superseded by tst_cgroup02.c.
* Remove container_of macro as it is no longer used by this patch set.
* Fix tst_cgroup_{scan,require,mount} in unlikely case it is run inside cleanup or some other context where tst_brk may return.
* Fix potential null-ptr-deref in cgroup_file_alias.
* Include sys/types.h in header due to use of (s)size_t.
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 (6):
API: Add safe openat, printfat, readat and unlinkat 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 | 179 ++- include/tst_safe_file_at.h | 51 + include/tst_test.h | 1 - lib/newlib_tests/.gitignore | 3 +- lib/newlib_tests/test21.c | 66 - lib/newlib_tests/tst_cgroup01.c | 51 + lib/newlib_tests/tst_cgroup02.c | 90 ++ lib/tst_cgroup.c | 1266 ++++++++++++----- lib/tst_safe_file_at.c | 170 +++ 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 +- 20 files changed, 1753 insertions(+), 538 deletions(-) create mode 100644 include/tst_safe_file_at.h delete mode 100644 lib/newlib_tests/test21.c 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_at.c
Signature · 1 line
2.31.1
PATCH v4 1/6 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_at.h | 51 +++++++++++ lib/tst_safe_file_at.c | 170 +++++++++++++++++++++++++++++++++++++ 2 files changed, 221 insertions(+) create mode 100644 include/tst_safe_file_at.h create mode 100644 lib/tst_safe_file_at.c
include/tst_safe_file_at.h new file +51 -0
@@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2021 SUSE LLC <[email protected]> +*/ + +#ifndef TST_SAFE_FILE_AT_H +#define TST_SAFE_FILE_AT_H + +#include <sys/types.h> +#include <stdarg.h> + +#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
lib/tst_safe_file_at.c new file +170 -0
170 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v4 1/6 API: Add safe openat, printfat, readat and unlinkat
Hi Richie,
lib/tst_safe_file_at.c line 3
Quoted 12 lines
Add 'at' variants for a number of system calls and LTP SAFE API functions. This avoids using sprintf everywhere to build paths.
lib/tst_safe_file_at.c new file +3
-0@@ -0,0 +1,170 @@ +#define _GNU_SOURCE +#include "lapi/fcntl.h" +#include "tst_safe_file_at.h"
This requires <stdio.h> (sprintf).
Also file should have SPDX license + copyright.
Kind regards,
Petr
Quoted 15 lines
lib/tst_safe_file_at.c new file +15
-0+#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));
...
PATCH v4 2/6 Add new CGroups APIs
In reply to Richard PalethorpeComplete 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 | 179 +++++- include/tst_test.h | 1 - lib/tst_cgroup.c | 1266 +++++++++++++++++++++++++++++++----------- 3 files changed, 1103 insertions(+), 343 deletions(-)
include/tst_cgroup.h +156 -23
@@ -2,46 +2,179 @@/* * 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" +#include <sys/types.h> +/* CGroups Kernel API version */ 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 */ +/* 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 ; }; -enum tst_cgroup_ver tst_cgroup_version (void ); +/* A Control Group in LTP's aggregated hierarchy */ +struct tst_cgroup_group ; + +/* 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 (const char *ctrl_name, +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_group *tst_cgroup_get_test_group (void ); +/* Get the shared drain group. Also should be called from setup */ +const struct tst_cgroup_group *tst_cgroup_get_drain_group (void ); +/* Create a descendant CGroup */ +struct tst_cgroup_group * +tst_cgroup_group_mk (const struct tst_cgroup_group *parent, +const char *name); +/* Remove a descendant CGroup */ +struct tst_cgroup_group *tst_cgroup_group_rm (struct tst_cgroup_group *cg); + + +#define TST_CGROUP_VER (cg, ctrl_name) \ + tst_cgroup_ver(__FILE__, __LINE__, (cg), (ctrl_name)) + +enum tst_cgroup_ver tst_cgroup_ver (const char *file,const int lineno, +const struct tst_cgroup_group *cg, +const char *ctrl_name); + +#define SAFE_CGROUP_HAS (cg, file_name) \ + safe_cgroup_has(__FILE__, __LINE__, (cg), (file_name)) + +int safe_cgroup_has (const char *file,const int lineno, +const struct tst_cgroup_group *cg,const char *file_name); + +#define SAFE_CGROUP_READ (cg, file_name, out, len) \ + safe_cgroup_read(__FILE__, __LINE__, \ + (cg), (file_name), (out), (len)) + +ssize_t safe_cgroup_read (const char *file,const int lineno, +const struct tst_cgroup_group *cg, +const char *file_name, +char *out,size_t len); + +#define SAFE_CGROUP_PRINTF (cg, file_name, fmt, ...) \ + safe_cgroup_printf(__FILE__, __LINE__, \ + (cg), (file_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, file_name, str) \ + safe_cgroup_printf(__FILE__, __LINE__, (cg), (file_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_group *cg, +const char *file_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, file_name, fmt, ...) \ + safe_cgroup_scanf(__FILE__, __LINE__, \ + (cg), (file_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_group *cg,const char *file_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
@@ -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 +947 -319
1,400 lines not on this page · raw message
Signature · 1 line
2.31.1
PATCH v4 2/6 Add new CGroups APIs
Hi!
I've did a careful review of the code and found a few minor things I've pointed out below. Overall the code looks really great at this point.
lib/tst_cgroup.c line 793
Quoted 8 lines
lib/tst_cgroup.c +4
-2-void tst_cgroup_umount (const char *cgroup_dir) +static void cgroup_group_init (struct tst_cgroup_group *cg, +const char *group_name) { -char *cgroup_new_dir; +memset (cg,0 ,sizeof (*cg)); + cg->group_name = group_name;
Here we store a pointer to a string that was passed by the caller, which will cause invalid access if user passed a string on a stack.
I guess that the easiest fix would be to add an 32 char array to the tst_cgroup_group and strcpy() the string there. We would have to check that the string is short enough, but that should be it.
Quoted 62 lines
lib/tst_cgroup.c +42
-12+} + +static void cgroup_group_add_dir (struct tst_cgroup_group *cg, +struct cgroup_dir *dir) +{ +const struct cgroup_ctrl *ctrl; +int i; + +if (dir->dir_root->ver ==TST_CGROUP_V2 ) + cg->dirs_by_ctrl[0 ] = dir; - cgroup_new_dir =tst_cgroup_get_path (cgroup_dir); -tst_cgroupN_umount (cgroup_dir, cgroup_new_dir); -tst_cgroup_del_path (cgroup_dir); +for_each_ctrl (ctrl) { +if (has_ctrl (dir->ctrl_field, ctrl)) + cg->dirs_by_ctrl[ctrl->ctrl_indx] = dir; + } + +for (i =0 ; cg->dirs[i]; i++); + cg->dirs[i] = dir; } -void tst_cgroup_set_knob (const char *cgroup_dir,const char *knob,long value) +struct tst_cgroup_group * +tst_cgroup_group_mk (const struct tst_cgroup_group *parent, +const char *group_name) { -char *cgroup_new_dir; -char knob_path[PATH_MAX ]; +struct tst_cgroup_group *cg; +struct cgroup_dir *const *dir; +struct cgroup_dir *new_dir; + + cg =SAFE_MALLOC (sizeof (*cg)); +cgroup_group_init (cg, group_name); - cgroup_new_dir =tst_cgroup_get_path (cgroup_dir); -sprintf (knob_path,"%s/%s" , cgroup_new_dir, knob); -SAFE_FILE_PRINTF (knob_path,"%ld" , value); +for_each_dir (parent,0 , dir) { + new_dir =SAFE_MALLOC (sizeof (*new_dir)); +cgroup_dir_mk (*dir, group_name, new_dir); +cgroup_group_add_dir (cg, new_dir); + } + +return cg; } -void tst_cgroup_move_current (const char *cgroup_dir) +struct tst_cgroup_group *tst_cgroup_group_rm (struct tst_cgroup_group *cg) { -if (tst_cg_ver &TST_CGROUP_V1 ) -tst_cgroup_set_knob (cgroup_dir,"tasks" ,getpid ()); +struct cgroup_dir **dir; + +for_each_dir (cg,0 , dir) { +close ((*dir)->dir_fd); +SAFE_UNLINKAT ((*dir)->dir_parent->dir_fd, + (*dir)->dir_name, +AT_REMOVEDIR ); +free (*dir);
lib/tst_cgroup.c line 850
Quoted 10 lines
lib/tst_cgroup.c +4
-3+ } -if (tst_cg_ver &TST_CGROUP_V2 ) -tst_cgroup_set_knob (cgroup_dir,"cgroup.procs" ,getpid ()); +free (cg); +return NULL ; } -void tst_cgroup_mem_set_maxbytes (const char *cgroup_dir,long memsz) +/* Traverse the item tree to find an item. */
This is no longer called item. Maybe we should just remove this comment.
lib/tst_cgroup.c line 859
Quoted 11 lines
lib/tst_cgroup.c +8
-2+static const struct cgroup_file *cgroup_file_find (const char *file, +const int lineno, +const char *file_name) { -if (tst_cg_ver &TST_CGROUP_V1 ) -tst_cgroup_set_knob (cgroup_dir,"memory.limit_in_bytes" , memsz); +const struct cgroup_file *cfile; +const struct cgroup_ctrl *ctrl; +char ctrl_name[32 ]; +const char *mem_name; +size_t len =MIN (sizeof (ctrl_name) -1 ,strcspn (file_name,"." ));
Aren't all the v2 names in the format controller.knob? Can't we just TBROK if there is '.' in the file_name?
Quoted 8 lines
lib/tst_cgroup.c +8
-0+memcpy (ctrl_name, file_name, len); + ctrl_name[len] ='\0' ; + + ctrl =cgroup_find_ctrl (ctrl_name); + +if (!ctrl) { +tst_brk_ (file, lineno,TBROK , +"Did not find controller '%s'\n" , ctrl_name);
lib/tst_cgroup.c line 878
Quoted 10 lines
lib/tst_cgroup.c +10
-0+return NULL ; + } + + file_name += len +1 ; + +for (cfile = ctrl->files; cfile->file_name; cfile++) { + mem_name = cfile->file_name + len +1 ; + +if (!strcmp (file_name, mem_name)) +break ;
Can't we just compare the whole strings here? The prefix is constant and the same for both, so we may as well avoid this string trickery and just compare the file_name against cfile->file_name.
Quoted 73 lines
lib/tst_cgroup.c +35
-28+ } + +if (!cfile->file_name) { +tst_brk_ (file, lineno,TBROK , +"Did not find '%s' in '%s'\n" , + file_name, ctrl->ctrl_name); +return NULL ; + } -if (tst_cg_ver &TST_CGROUP_V2 ) -tst_cgroup_set_knob (cgroup_dir,"memory.max" , memsz); +return cfile; } -int tst_cgroup_mem_swapacct_enabled (const char *cgroup_dir) +enum tst_cgroup_ver tst_cgroup_ver (const char *file,const int lineno, +const struct tst_cgroup_group *cg, +const char *ctrl_name) { -char *cgroup_new_dir; -char knob_path[PATH_MAX ]; +const struct cgroup_ctrl *const ctrl =cgroup_find_ctrl (ctrl_name); +const struct cgroup_dir *dir; + +if (!strcmp (ctrl_name,"cgroup" )) { +tst_brk_ (file, lineno, +TBROK , +"cgroup may be present on both V1 and V2 hierarchies" ); +return 0 ; + } - cgroup_new_dir =tst_cgroup_get_path (cgroup_dir); +if (!ctrl) { +tst_brk_ (file, lineno, +TBROK ,"Unknown controller '%s'" , ctrl_name); +return 0 ; + } -if (tst_cg_ver &TST_CGROUP_V1 ) { -sprintf (knob_path,"%s/%s" , - cgroup_new_dir,"/memory.memsw.limit_in_bytes" ); + dir = cg->dirs_by_ctrl[ctrl->ctrl_indx]; -if ((access (knob_path,F_OK ) ==-1 )) { -if (errno ==ENOENT ) -tst_res (TCONF ,"memcg swap accounting is disabled" ); -else -tst_brk (TBROK |TERRNO ,"failed to access %s" , knob_path); - }else { -return 1 ; - } +if (!dir) { +tst_brk_ (file, lineno, +TBROK ,"%s controller not attached to CGroup %s" , + ctrl_name, cg->group_name); +return 0 ; } -if (tst_cg_ver &TST_CGROUP_V2 ) { -sprintf (knob_path,"%s/%s" , - cgroup_new_dir,"/memory.swap.max" ); +return dir->dir_root->ver; +} -if ((access (knob_path,F_OK ) ==-1 )) { -if (errno ==ENOENT ) -tst_res (TCONF ,"memcg swap accounting is disabled" ); -else -tst_brk (TBROK |TERRNO ,"failed to access %s" , knob_path); - }else { -return 1 ; - } +static const char *cgroup_file_alias(const struct cgroup_file *const cfile,
lib/tst_cgroup.c line 933
Quoted 10 lines
lib/tst_cgroup.c +9
-0+ const struct cgroup_dir *const dir) +{ +if (dir->dir_root->ver !=TST_CGROUP_V1 ) +return cfile->file_name; + +if (cfile->ctrl_indx ==CTRL_CPUSET && + dir->dir_root->no_cpuset_prefix && + cfile->file_name_v1) { +return strchr (cfile->file_name_v1,'.' ) +1 ; }
This is quite ugly but I do not think that I can come up with better solutution.
Quoted 159 lines
lib/tst_cgroup.c +89
-50-return 0 ; +return cfile->file_name_v1; } -void tst_cgroup_mem_set_maxswap (const char *cgroup_dir,long memsz) +int safe_cgroup_has (const char *file,const int lineno, +const struct tst_cgroup_group *cg,const char *file_name) { -if (tst_cg_ver &TST_CGROUP_V1 ) -tst_cgroup_set_knob (cgroup_dir,"memory.memsw.limit_in_bytes" , memsz); +const struct cgroup_file *const cfile = +cgroup_file_find (file, lineno, file_name); +struct cgroup_dir *const *dir; +const char *alias; -if (tst_cg_ver &TST_CGROUP_V2 ) -tst_cgroup_set_knob (cgroup_dir,"memory.swap.max" , memsz); +if (!cfile) +return 0 ; + +for_each_dir (cg, cfile->ctrl_indx, dir) { +if (!(alias =cgroup_file_alias (cfile, *dir))) +continue ; + +if (!faccessat ((*dir)->dir_fd, file_name,F_OK ,0 )) +return 1 ; + +if (errno ==ENOENT ) +continue ; + +tst_brk_ (file, lineno,TBROK |TERRNO , +"faccessat(%d<%s>, %s, F_OK, 0)" , + (*dir)->dir_fd,tst_decode_fd ((*dir)->dir_fd), alias); + } + +return 0 ; } -void tst_cgroup_cpuset_read_files (const char *cgroup_dir,const char *filename, -char *retbuf,size_t retbuf_sz) +static struct tst_cgroup_group *cgroup_group_from_roots (size_t tree_off) { -int fd; -char *cgroup_new_dir; -char knob_path[PATH_MAX ]; +struct cgroup_root *root; +struct cgroup_dir *dir; +struct tst_cgroup_group *cg; - cgroup_new_dir =tst_cgroup_get_path (cgroup_dir); + cg =tst_alloc (sizeof (*cg)); +cgroup_group_init (cg,NULL ); -/* -* try either '/dev/cpuset/XXXX' or '/dev/cpuset/cpuset.XXXX' -* please see Documentation/cgroups/cpusets.txt from kernel src -* for details -*/ -sprintf (knob_path,"%s/%s" , cgroup_new_dir, filename); - fd =open (knob_path,O_RDONLY ); -if (fd ==-1 ) { -if (errno ==ENOENT ) { -sprintf (knob_path,"%s/cpuset.%s" , - cgroup_new_dir, filename); - fd =SAFE_OPEN (knob_path,O_RDONLY ); - }else -tst_brk (TBROK |TERRNO ,"open %s" , knob_path); +for_each_root (root) { + dir = (typeof (dir))(((char *)root) + tree_off); + +if (dir->ctrl_field) +cgroup_group_add_dir (cg, dir); } -memset (retbuf,0 , retbuf_sz); -if (read (fd, retbuf, retbuf_sz) <0 ) -tst_brk (TBROK |TERRNO ,"read %s" , knob_path); +if (cg->dirs[0 ]) { + cg->group_name = cg->dirs[0 ]->dir_name; +return cg; + } + +tst_brk (TBROK , +"No CGroups found; maybe you forgot to call tst_cgroup_require?" ); -close (fd); +return cg; } -void tst_cgroup_cpuset_write_files (const char *cgroup_dir,const char *filename,const char *buf) +const struct tst_cgroup_group *tst_cgroup_get_test_group (void ) { -int fd; -char *cgroup_new_dir; -char knob_path[PATH_MAX ]; +return cgroup_group_from_roots (offsetof(struct cgroup_root , test_dir)); +} - cgroup_new_dir =tst_cgroup_get_path (cgroup_dir); +const struct tst_cgroup_group *tst_cgroup_get_drain_group (void ) +{ +return cgroup_group_from_roots (offsetof(struct cgroup_root , drain_dir)); +} -/* -* try either '/dev/cpuset/XXXX' or '/dev/cpuset/cpuset.XXXX' -* please see Documentation/cgroups/cpusets.txt from kernel src -* for details -*/ -sprintf (knob_path,"%s/%s" , cgroup_new_dir, filename); - fd =open (knob_path,O_WRONLY ); -if (fd ==-1 ) { -if (errno ==ENOENT ) { -sprintf (knob_path,"%s/cpuset.%s" , cgroup_new_dir, filename); - fd =SAFE_OPEN (knob_path,O_WRONLY ); - }else -tst_brk (TBROK |TERRNO ,"open %s" , knob_path); +ssize_t safe_cgroup_read (const char *file,const int lineno, +const struct tst_cgroup_group *cg,const char *file_name, +char *out,size_t len) +{ +const struct cgroup_file *const cfile = +cgroup_file_find (file, lineno, file_name); +struct cgroup_dir *const *dir; +const char *alias; +size_t prev_len =0 ; +char prev_buf[BUFSIZ ]; + +for_each_dir (cg, cfile->ctrl_indx, dir) { +if (!(alias =cgroup_file_alias (cfile, *dir))) +continue ; + +if (prev_len) +memcpy (prev_buf, out, prev_len); + +TEST (safe_file_readat (file, lineno, + (*dir)->dir_fd, alias, out, len)); +if (TST_RET <0 ) +continue ; + +if (prev_len &&memcmp (out, prev_buf, prev_len)) { +tst_brk_ (file, lineno,TBROK , +"%s has different value across roots" , + file_name); +break ; + } + + prev_len =MIN (sizeof (prev_buf), (size_t )TST_RET ); } -SAFE_WRITE (1 , fd, buf,strlen (buf)); + out[MAX (TST_RET ,0 )] ='\0' ; -close (fd); +return TST_RET ; +} + +void safe_cgroup_printf(const char *file, const int lineno, + const struct tst_cgroup_group *cg,
lib/tst_cgroup.c line 1053
Quoted 10 lines
lib/tst_cgroup.c +10
-0+const char *file_name, + const char *fmt, ...) +{ + const struct cgroup_file *const cfile = +cgroup_file_find (file, lineno, file_name); +struct cgroup_dir *const *dir; +const char *alias; +va_list va; + +for_each_dir (cg, cfile->ctrl_indx, dir) {
So this is a loop because we have to write to all instances of the generic files such as "tasks" to move the the process consistently across the hierarchies, right?
Maybe this deserves a comment at the for_each_dir() macro definition.
Quoted 18 lines
lib/tst_cgroup.c +18
-0+if (!(alias =cgroup_file_alias (cfile, *dir))) +continue ; + +va_start (va, fmt); +safe_file_vprintfat (file, lineno, + (*dir)->dir_fd, alias, fmt, va); +va_end (va); + } +} + +void safe_cgroup_scanf (const char *file,const int lineno, +const struct tst_cgroup_group *cg,const char *file_name, +const char *fmt, ...) +{ +va_list va; +char buf[BUFSIZ ]; +ssize_t len =safe_cgroup_read (file, lineno, + cg, file_name, buf,sizeof (buf));
lib/tst_cgroup.c line 1081
Quoted 10 lines
lib/tst_cgroup.c +10
-0+ +if (len <1 ) +return ; + +va_start (va, fmt); +if (vsscanf (buf, fmt, va) <1 ) { +tst_brk_ (file, lineno,TBROK |TERRNO , +"'%s': vsscanf('%s', '%s', ...)" , file_name, buf, fmt); + } +va_end (va);
This does not allow user to handle if we convert less than requested number of conversions including zero. Either we have to return the number of conversions from this function, or we have to count the conversions in the fmt and TBROK if we got less as we do in safe_file_ops.c with count_scanf_conversions() function.
Quoted 7 lines
}
Signature · 5 lines
2.31.1 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH v4 3/6 Add new CGroups API library tests
In reply to Richard Palethorpe--- lib/newlib_tests/.gitignore | 3 +- lib/newlib_tests/test21.c | 66 ------------------------ lib/newlib_tests/tst_cgroup01.c | 51 +++++++++++++++++++ lib/newlib_tests/tst_cgroup02.c | 90 +++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+), 67 deletions(-) delete mode 100644 lib/newlib_tests/test21.c create mode 100644 lib/newlib_tests/tst_cgroup01.c create mode 100644 lib/newlib_tests/tst_cgroup02.c
lib/newlib_tests/.gitignore +2 -1
@@ -16,6 +16,8 @@ test15 test16 tst_capability01 tst_capability02 +tst_cgroup01 +tst_cgroup02 tst_device tst_safe_fileops tst_res_hexd
@@ -24,7 +26,6 @@ test17 test18 test19 test20 -test21 test22 tst_expiration_timer test_assert
lib/newlib_tests/test21.c deleted +0 -66
66 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.31.1
PATCH v4 4/6 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
@@ -2186,48 +2186,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_group *cg; static void run(void) { ... + // do test under cgroup + ... +} + +static void setup(void) +{ + tst_cgroup_require("memory", NULL); + cg = tst_cgroup_get_test_group(); + 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_group' 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 'SAFE_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_group *cg; +static const struct tst_cgroup_group *cg_drain; +static struct tst_cgroup_group *cg_child; + +static void run(void) +{ + char buf[BUFSIZ]; + size_t mem = 0; + + cg_child = tst_cgroup_group_mk(cg, "child"); + SAFE_CGROUP_PRINTF(cg_child, "cgroup.procs", "%d", getpid()); + + if (TST_CGROUP_VER(cg, "memory") != TST_CGROUP_V1) + SAFE_CGROUP_PRINT(cg, "cgroup.subtree_control", "+memory"); + if (TST_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_group_rm(cg_child); } static void setup(void) { - tst_cgroup_mount(TST_CGROUP_MEMCG, PATH_TMP_CG_MEM); + tst_cgroup_require("memory", NULL); + tst_cgroup_require("cpuset", NULL); + + cg = tst_cgroup_get_test_group(); + cg_drain = tst_cgroup_get_drain_group(); } 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_group_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.31.1
PATCH v4 5/6 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
@@ -35,6 +35,8 @@#ifdef HAVE_NUMA_V2 +static const struct tst_cgroup_group *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 ("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_group (); +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
@@ -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_group *cg,long nd);/* shared */ unsigned int get_a_numa_node (void );
testcases/kernel/mem/ksm/ksm02.c +9 -4
@@ -59,6 +59,9 @@#ifdef HAVE_NUMA_V2 #include <numaif.h> +static const struct tst_cgroup_group *cg; +static const struct tst_cgroup_group *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 ("cpuset" ,NULL ); + cg =tst_cgroup_get_test_group (); }static struct tst_test test = {
testcases/kernel/mem/ksm/ksm03.c +8 -4
@@ -59,10 +59,10 @@#include "mem.h" #include "ksm_common.h" +static const struct tst_cgroup_group *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 ("memory" ,NULL ); + cg =tst_cgroup_get_test_group (); +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
@@ -59,6 +59,8 @@#ifdef HAVE_NUMA_V2 #include <numaif.h> +static const struct tst_cgroup_group *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 ("memory" ,NULL ); +tst_cgroup_require ("cpuset" ,NULL ); + cg =tst_cgroup_get_test_group (); +SAFE_CGROUP_PRINTF (cg,"cgroup.procs" ,"%d" ,getpid ()); }static struct tst_test test = {
testcases/kernel/mem/lib/mem.c +4 -6
@@ -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_group *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
@@ -36,19 +36,17 @@#ifdef HAVE_NUMA_V2 +static const struct tst_cgroup_group *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 ("memory" ,NULL ); + cg =tst_cgroup_get_test_group (); +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
@@ -36,24 +36,25 @@#ifdef HAVE_NUMA_V2 +static const struct tst_cgroup_group *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 ("cpuset" ,NULL ); + cg =tst_cgroup_get_test_group ();/* * 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
@@ -36,6 +36,8 @@#ifdef HAVE_NUMA_V2 +static const struct tst_cgroup_group *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 (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" );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 ("memory" ,NULL ); +tst_cgroup_require ("cpuset" ,NULL ); + cg =tst_cgroup_get_test_group ();/* * 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.31.1
PATCH v4 6/6 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
@@ -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_group *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 ("memory" ,NULL ); + cg =tst_cgroup_get_test_group (); + +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.31.1