Re: [PATCH 2/4] perf tools: Add cgroup_is_v2() helper

From: Arnaldo Carvalho de Melo
Date: Wed Jun 30 2021 - 14:44:06 EST


Em Tue, Jun 29, 2021 at 11:35:17PM -0700, Namhyung Kim escreveu:
> Hi Ian,
>
> On Tue, Jun 29, 2021 at 8:51 AM Ian Rogers <irogers@xxxxxxxxxx> wrote:
> >
> > On Fri, Jun 25, 2021 at 12:18 AM Namhyung Kim <namhyung@xxxxxxxxxx> wrote:
> > >
> > > The cgroup_is_v2() is to check if the given subsystem is mounted on
> > > cgroup v2 or not. It'll be used by BPF cgroup code later.
> > >
> > > Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
> > > ---
> > > tools/perf/util/cgroup.c | 19 +++++++++++++++++++
> > > tools/perf/util/cgroup.h | 2 ++
> > > 2 files changed, 21 insertions(+)
> > >
> > > diff --git a/tools/perf/util/cgroup.c b/tools/perf/util/cgroup.c
> > > index ef18c988c681..e819a4f30fc2 100644
> > > --- a/tools/perf/util/cgroup.c
> > > +++ b/tools/perf/util/cgroup.c
> > > @@ -9,6 +9,7 @@
> > > #include <linux/zalloc.h>
> > > #include <sys/types.h>
> > > #include <sys/stat.h>
> > > +#include <sys/statfs.h>
> > > #include <fcntl.h>
> > > #include <stdlib.h>
> > > #include <string.h>
> > > @@ -70,6 +71,24 @@ int read_cgroup_id(struct cgroup *cgrp)
> > > }
> > > #endif /* HAVE_FILE_HANDLE */
> > >
> > > +#ifndef CGROUP2_SUPER_MAGIC
> > > +#define CGROUP2_SUPER_MAGIC 0x63677270
> > > +#endif
> > > +
> > > +int cgroup_is_v2(const char *subsys)
> > > +{
> > > + char mnt[PATH_MAX + 1];
> > > + struct statfs stbuf;
> > > +
> > > + if (cgroupfs_find_mountpoint(mnt, PATH_MAX + 1, subsys))
> > > + return -1;
> > > +
> > > + if (statfs(mnt, &stbuf) < 0)
> > > + return -1;
> > > +
> > > + return (stbuf.f_type == CGROUP2_SUPER_MAGIC);
> > > +}
> > > +
> > > static struct cgroup *evlist__find_cgroup(struct evlist *evlist, const char *str)
> > > {
> > > struct evsel *counter;
> > > diff --git a/tools/perf/util/cgroup.h b/tools/perf/util/cgroup.h
> > > index 707adbe25123..1549ec2fd348 100644
> > > --- a/tools/perf/util/cgroup.h
> > > +++ b/tools/perf/util/cgroup.h
> > > @@ -47,4 +47,6 @@ int read_cgroup_id(struct cgroup *cgrp)
> > > }
> > > #endif /* HAVE_FILE_HANDLE */
> > >
> > > +int cgroup_is_v2(const char *subsys);
> > > +
> >
> > I think this is okay. It may make sense to have this in
> > tools/lib/api/fs/fs.h, for example fs__valid_mount is already checking
> > magic numbers. Perhaps we can avoid a statfs call, but it'd need some
> > reorganization of the fs.h code.
> >
> > Acked-by: Ian Rogers <irogers@xxxxxxxxxx>
>
> Thanks for your review!
>
> Actually I'm ok with moving it to tools/lib. Will do it in the next spin,
> if it needs one. :)

O think I'll take v4, we can improve this in followup work.

- Arnaldo