[PATCH v2 3/5] cgroup: selftests: Move cgroup_util into its own library

James Houghton posted 5 patches 8 months, 3 weeks ago
There is a newer version of this series
[PATCH v2 3/5] cgroup: selftests: Move cgroup_util into its own library
Posted by James Houghton 8 months, 3 weeks ago
KVM selftests will soon need to use some of the cgroup creation and
deletion functionality from cgroup_util.

Suggested-by: David Matlack <dmatlack@google.com>
Signed-off-by: James Houghton <jthoughton@google.com>
---
 tools/testing/selftests/cgroup/Makefile       | 21 ++++++++++---------
 .../selftests/cgroup/{ => lib}/cgroup_util.c  |  2 +-
 .../cgroup/{ => lib/include}/cgroup_util.h    |  4 ++--
 .../testing/selftests/cgroup/lib/libcgroup.mk | 14 +++++++++++++
 4 files changed, 28 insertions(+), 13 deletions(-)
 rename tools/testing/selftests/cgroup/{ => lib}/cgroup_util.c (99%)
 rename tools/testing/selftests/cgroup/{ => lib/include}/cgroup_util.h (99%)
 create mode 100644 tools/testing/selftests/cgroup/lib/libcgroup.mk

diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selftests/cgroup/Makefile
index 1b897152bab6e..e01584c2189ac 100644
--- a/tools/testing/selftests/cgroup/Makefile
+++ b/tools/testing/selftests/cgroup/Makefile
@@ -21,14 +21,15 @@ TEST_GEN_PROGS += test_zswap
 LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h
 
 include ../lib.mk
+include lib/libcgroup.mk
 
-$(OUTPUT)/test_core: cgroup_util.c
-$(OUTPUT)/test_cpu: cgroup_util.c
-$(OUTPUT)/test_cpuset: cgroup_util.c
-$(OUTPUT)/test_freezer: cgroup_util.c
-$(OUTPUT)/test_hugetlb_memcg: cgroup_util.c
-$(OUTPUT)/test_kill: cgroup_util.c
-$(OUTPUT)/test_kmem: cgroup_util.c
-$(OUTPUT)/test_memcontrol: cgroup_util.c
-$(OUTPUT)/test_pids: cgroup_util.c
-$(OUTPUT)/test_zswap: cgroup_util.c
+$(OUTPUT)/test_core: $(LIBCGROUP_O)
+$(OUTPUT)/test_cpu: $(LIBCGROUP_O)
+$(OUTPUT)/test_cpuset: $(LIBCGROUP_O)
+$(OUTPUT)/test_freezer: $(LIBCGROUP_O)
+$(OUTPUT)/test_hugetlb_memcg: $(LIBCGROUP_O)
+$(OUTPUT)/test_kill: $(LIBCGROUP_O)
+$(OUTPUT)/test_kmem: $(LIBCGROUP_O)
+$(OUTPUT)/test_memcontrol: $(LIBCGROUP_O)
+$(OUTPUT)/test_pids: $(LIBCGROUP_O)
+$(OUTPUT)/test_zswap: $(LIBCGROUP_O)
diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/lib/cgroup_util.c
similarity index 99%
rename from tools/testing/selftests/cgroup/cgroup_util.c
rename to tools/testing/selftests/cgroup/lib/cgroup_util.c
index 1e2d46636a0ca..f047d8adaec65 100644
--- a/tools/testing/selftests/cgroup/cgroup_util.c
+++ b/tools/testing/selftests/cgroup/lib/cgroup_util.c
@@ -17,7 +17,7 @@
 #include <unistd.h>
 
 #include "cgroup_util.h"
-#include "../clone3/clone3_selftests.h"
+#include "../../clone3/clone3_selftests.h"
 
 /* Returns read len on success, or -errno on failure. */
 static ssize_t read_text(const char *path, char *buf, size_t max_len)
diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
similarity index 99%
rename from tools/testing/selftests/cgroup/cgroup_util.h
rename to tools/testing/selftests/cgroup/lib/include/cgroup_util.h
index 19b131ee77072..7a0441e5eb296 100644
--- a/tools/testing/selftests/cgroup/cgroup_util.h
+++ b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
@@ -2,9 +2,9 @@
 #include <stdbool.h>
 #include <stdlib.h>
 
-#include "../kselftest.h"
-
+#ifndef PAGE_SIZE
 #define PAGE_SIZE 4096
+#endif
 
 #define MB(x) (x << 20)
 
diff --git a/tools/testing/selftests/cgroup/lib/libcgroup.mk b/tools/testing/selftests/cgroup/lib/libcgroup.mk
new file mode 100644
index 0000000000000..12323041a5ce6
--- /dev/null
+++ b/tools/testing/selftests/cgroup/lib/libcgroup.mk
@@ -0,0 +1,14 @@
+CGROUP_DIR := $(selfdir)/cgroup
+
+LIBCGROUP_C := lib/cgroup_util.c
+
+LIBCGROUP_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBCGROUP_C))
+
+CFLAGS += -I$(CGROUP_DIR)/lib/include
+
+EXTRA_HDRS := $(selfdir)/clone3/clone3_selftests.h
+
+$(LIBCGROUP_O): $(OUTPUT)/%.o : $(CGROUP_DIR)/%.c $(EXTRA_HDRS)
+	$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
+
+EXTRA_CLEAN += $(LIBCGROUP_O)
-- 
2.49.0.472.ge94155a9ec-goog
Re: [PATCH v2 3/5] cgroup: selftests: Move cgroup_util into its own library
Posted by David Matlack 8 months, 2 weeks ago
On Mon, Mar 31, 2025 at 2:30 PM James Houghton <jthoughton@google.com> wrote:
>
> KVM selftests will soon need to use some of the cgroup creation and
> deletion functionality from cgroup_util.
>
> Suggested-by: David Matlack <dmatlack@google.com>
> Signed-off-by: James Houghton <jthoughton@google.com>
> ---
>  tools/testing/selftests/cgroup/Makefile       | 21 ++++++++++---------
>  .../selftests/cgroup/{ => lib}/cgroup_util.c  |  2 +-
>  .../cgroup/{ => lib/include}/cgroup_util.h    |  4 ++--
>  .../testing/selftests/cgroup/lib/libcgroup.mk | 14 +++++++++++++
>  4 files changed, 28 insertions(+), 13 deletions(-)
>  rename tools/testing/selftests/cgroup/{ => lib}/cgroup_util.c (99%)
>  rename tools/testing/selftests/cgroup/{ => lib/include}/cgroup_util.h (99%)
>  create mode 100644 tools/testing/selftests/cgroup/lib/libcgroup.mk
>
> diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selftests/cgroup/Makefile
> index 1b897152bab6e..e01584c2189ac 100644
> --- a/tools/testing/selftests/cgroup/Makefile
> +++ b/tools/testing/selftests/cgroup/Makefile
> @@ -21,14 +21,15 @@ TEST_GEN_PROGS += test_zswap
>  LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h
>
>  include ../lib.mk
> +include lib/libcgroup.mk
>
> -$(OUTPUT)/test_core: cgroup_util.c
> -$(OUTPUT)/test_cpu: cgroup_util.c
> -$(OUTPUT)/test_cpuset: cgroup_util.c
> -$(OUTPUT)/test_freezer: cgroup_util.c
> -$(OUTPUT)/test_hugetlb_memcg: cgroup_util.c
> -$(OUTPUT)/test_kill: cgroup_util.c
> -$(OUTPUT)/test_kmem: cgroup_util.c
> -$(OUTPUT)/test_memcontrol: cgroup_util.c
> -$(OUTPUT)/test_pids: cgroup_util.c
> -$(OUTPUT)/test_zswap: cgroup_util.c
> +$(OUTPUT)/test_core: $(LIBCGROUP_O)
> +$(OUTPUT)/test_cpu: $(LIBCGROUP_O)
> +$(OUTPUT)/test_cpuset: $(LIBCGROUP_O)
> +$(OUTPUT)/test_freezer: $(LIBCGROUP_O)
> +$(OUTPUT)/test_hugetlb_memcg: $(LIBCGROUP_O)
> +$(OUTPUT)/test_kill: $(LIBCGROUP_O)
> +$(OUTPUT)/test_kmem: $(LIBCGROUP_O)
> +$(OUTPUT)/test_memcontrol: $(LIBCGROUP_O)
> +$(OUTPUT)/test_pids: $(LIBCGROUP_O)
> +$(OUTPUT)/test_zswap: $(LIBCGROUP_O)
> diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/lib/cgroup_util.c
> similarity index 99%
> rename from tools/testing/selftests/cgroup/cgroup_util.c
> rename to tools/testing/selftests/cgroup/lib/cgroup_util.c
> index 1e2d46636a0ca..f047d8adaec65 100644
> --- a/tools/testing/selftests/cgroup/cgroup_util.c
> +++ b/tools/testing/selftests/cgroup/lib/cgroup_util.c
> @@ -17,7 +17,7 @@
>  #include <unistd.h>
>
>  #include "cgroup_util.h"
> -#include "../clone3/clone3_selftests.h"
> +#include "../../clone3/clone3_selftests.h"
>
>  /* Returns read len on success, or -errno on failure. */
>  static ssize_t read_text(const char *path, char *buf, size_t max_len)
> diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
> similarity index 99%
> rename from tools/testing/selftests/cgroup/cgroup_util.h
> rename to tools/testing/selftests/cgroup/lib/include/cgroup_util.h
> index 19b131ee77072..7a0441e5eb296 100644
> --- a/tools/testing/selftests/cgroup/cgroup_util.h
> +++ b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
> @@ -2,9 +2,9 @@
>  #include <stdbool.h>
>  #include <stdlib.h>
>
> -#include "../kselftest.h"
> -
> +#ifndef PAGE_SIZE
>  #define PAGE_SIZE 4096
> +#endif
>
>  #define MB(x) (x << 20)
>
> diff --git a/tools/testing/selftests/cgroup/lib/libcgroup.mk b/tools/testing/selftests/cgroup/lib/libcgroup.mk
> new file mode 100644
> index 0000000000000..12323041a5ce6
> --- /dev/null
> +++ b/tools/testing/selftests/cgroup/lib/libcgroup.mk
> @@ -0,0 +1,14 @@
> +CGROUP_DIR := $(selfdir)/cgroup
> +
> +LIBCGROUP_C := lib/cgroup_util.c
> +
> +LIBCGROUP_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBCGROUP_C))
> +
> +CFLAGS += -I$(CGROUP_DIR)/lib/include
> +
> +EXTRA_HDRS := $(selfdir)/clone3/clone3_selftests.h
> +
> +$(LIBCGROUP_O): $(OUTPUT)/%.o : $(CGROUP_DIR)/%.c $(EXTRA_HDRS)
> +       $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
> +
> +EXTRA_CLEAN += $(LIBCGROUP_O)
> --
> 2.49.0.472.ge94155a9ec-goog

This works since KVM selftests already have a lib/ directory. But if
it didn't, the KVM selftests would fail to link against
lib/cgroup_util.o. To future proof against using this from other
selftests (or if someone were to add a subdirectory to
selftests/cgroup/lib), you can add a rule to create any missing
directories in the $(OUTPUT) path:

LIBCGROUP_O_DIRS := $(shell dirname $(LIBCGROUP_O) | uniq)

$(LIBCGROUP_O_DIRS):
        mkdir -p $@

Then add $(LIBCGROUP_O_DIRS) as a dependency of $(LIBCGROUP_O).
Re: [PATCH v2 3/5] cgroup: selftests: Move cgroup_util into its own library
Posted by James Houghton 8 months, 1 week ago
On Mon, Apr 7, 2025 at 5:18 PM David Matlack <dmatlack@google.com> wrote:
>
> On Mon, Mar 31, 2025 at 2:30 PM James Houghton <jthoughton@google.com> wrote:
> >
> > KVM selftests will soon need to use some of the cgroup creation and
> > deletion functionality from cgroup_util.
> >
> > Suggested-by: David Matlack <dmatlack@google.com>
> > Signed-off-by: James Houghton <jthoughton@google.com>
> > ---
> >  tools/testing/selftests/cgroup/Makefile       | 21 ++++++++++---------
> >  .../selftests/cgroup/{ => lib}/cgroup_util.c  |  2 +-
> >  .../cgroup/{ => lib/include}/cgroup_util.h    |  4 ++--
> >  .../testing/selftests/cgroup/lib/libcgroup.mk | 14 +++++++++++++
> >  4 files changed, 28 insertions(+), 13 deletions(-)
> >  rename tools/testing/selftests/cgroup/{ => lib}/cgroup_util.c (99%)
> >  rename tools/testing/selftests/cgroup/{ => lib/include}/cgroup_util.h (99%)
> >  create mode 100644 tools/testing/selftests/cgroup/lib/libcgroup.mk
> >
> > diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selftests/cgroup/Makefile
> > index 1b897152bab6e..e01584c2189ac 100644
> > --- a/tools/testing/selftests/cgroup/Makefile
> > +++ b/tools/testing/selftests/cgroup/Makefile
> > @@ -21,14 +21,15 @@ TEST_GEN_PROGS += test_zswap
> >  LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h
> >
> >  include ../lib.mk
> > +include lib/libcgroup.mk
> >
> > -$(OUTPUT)/test_core: cgroup_util.c
> > -$(OUTPUT)/test_cpu: cgroup_util.c
> > -$(OUTPUT)/test_cpuset: cgroup_util.c
> > -$(OUTPUT)/test_freezer: cgroup_util.c
> > -$(OUTPUT)/test_hugetlb_memcg: cgroup_util.c
> > -$(OUTPUT)/test_kill: cgroup_util.c
> > -$(OUTPUT)/test_kmem: cgroup_util.c
> > -$(OUTPUT)/test_memcontrol: cgroup_util.c
> > -$(OUTPUT)/test_pids: cgroup_util.c
> > -$(OUTPUT)/test_zswap: cgroup_util.c
> > +$(OUTPUT)/test_core: $(LIBCGROUP_O)
> > +$(OUTPUT)/test_cpu: $(LIBCGROUP_O)
> > +$(OUTPUT)/test_cpuset: $(LIBCGROUP_O)
> > +$(OUTPUT)/test_freezer: $(LIBCGROUP_O)
> > +$(OUTPUT)/test_hugetlb_memcg: $(LIBCGROUP_O)
> > +$(OUTPUT)/test_kill: $(LIBCGROUP_O)
> > +$(OUTPUT)/test_kmem: $(LIBCGROUP_O)
> > +$(OUTPUT)/test_memcontrol: $(LIBCGROUP_O)
> > +$(OUTPUT)/test_pids: $(LIBCGROUP_O)
> > +$(OUTPUT)/test_zswap: $(LIBCGROUP_O)
> > diff --git a/tools/testing/selftests/cgroup/cgroup_util.c b/tools/testing/selftests/cgroup/lib/cgroup_util.c
> > similarity index 99%
> > rename from tools/testing/selftests/cgroup/cgroup_util.c
> > rename to tools/testing/selftests/cgroup/lib/cgroup_util.c
> > index 1e2d46636a0ca..f047d8adaec65 100644
> > --- a/tools/testing/selftests/cgroup/cgroup_util.c
> > +++ b/tools/testing/selftests/cgroup/lib/cgroup_util.c
> > @@ -17,7 +17,7 @@
> >  #include <unistd.h>
> >
> >  #include "cgroup_util.h"
> > -#include "../clone3/clone3_selftests.h"
> > +#include "../../clone3/clone3_selftests.h"
> >
> >  /* Returns read len on success, or -errno on failure. */
> >  static ssize_t read_text(const char *path, char *buf, size_t max_len)
> > diff --git a/tools/testing/selftests/cgroup/cgroup_util.h b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
> > similarity index 99%
> > rename from tools/testing/selftests/cgroup/cgroup_util.h
> > rename to tools/testing/selftests/cgroup/lib/include/cgroup_util.h
> > index 19b131ee77072..7a0441e5eb296 100644
> > --- a/tools/testing/selftests/cgroup/cgroup_util.h
> > +++ b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
> > @@ -2,9 +2,9 @@
> >  #include <stdbool.h>
> >  #include <stdlib.h>
> >
> > -#include "../kselftest.h"
> > -
> > +#ifndef PAGE_SIZE
> >  #define PAGE_SIZE 4096
> > +#endif
> >
> >  #define MB(x) (x << 20)
> >
> > diff --git a/tools/testing/selftests/cgroup/lib/libcgroup.mk b/tools/testing/selftests/cgroup/lib/libcgroup.mk
> > new file mode 100644
> > index 0000000000000..12323041a5ce6
> > --- /dev/null
> > +++ b/tools/testing/selftests/cgroup/lib/libcgroup.mk
> > @@ -0,0 +1,14 @@
> > +CGROUP_DIR := $(selfdir)/cgroup
> > +
> > +LIBCGROUP_C := lib/cgroup_util.c
> > +
> > +LIBCGROUP_O := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBCGROUP_C))
> > +
> > +CFLAGS += -I$(CGROUP_DIR)/lib/include
> > +
> > +EXTRA_HDRS := $(selfdir)/clone3/clone3_selftests.h
> > +
> > +$(LIBCGROUP_O): $(OUTPUT)/%.o : $(CGROUP_DIR)/%.c $(EXTRA_HDRS)
> > +       $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@
> > +
> > +EXTRA_CLEAN += $(LIBCGROUP_O)
> > --
> > 2.49.0.472.ge94155a9ec-goog
>
> This works since KVM selftests already have a lib/ directory. But if
> it didn't, the KVM selftests would fail to link against
> lib/cgroup_util.o. To future proof against using this from other
> selftests (or if someone were to add a subdirectory to
> selftests/cgroup/lib), you can add a rule to create any missing
> directories in the $(OUTPUT) path:
>
> LIBCGROUP_O_DIRS := $(shell dirname $(LIBCGROUP_O) | uniq)
>
> $(LIBCGROUP_O_DIRS):
>         mkdir -p $@
>
> Then add $(LIBCGROUP_O_DIRS) as a dependency of $(LIBCGROUP_O).

Indeed, thanks David! Applied your change exactly as described.
Re: [PATCH v2 3/5] cgroup: selftests: Move cgroup_util into its own library
Posted by Tejun Heo 8 months, 3 weeks ago
On Mon, Mar 31, 2025 at 09:30:23PM +0000, James Houghton wrote:
> KVM selftests will soon need to use some of the cgroup creation and
> deletion functionality from cgroup_util.
> 
> Suggested-by: David Matlack <dmatlack@google.com>
> Signed-off-by: James Houghton <jthoughton@google.com>

Acked-by: Tejun Heo <tj@kernel.org>

Please feel free to route with the rest of the series.

Thanks.

-- 
tejun