Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2197711imn; Mon, 1 Aug 2022 15:02:33 -0700 (PDT) X-Google-Smtp-Source: AA6agR4utVCAmVxPNRoSxCjmcd78UmmdLaPmc9SYEz9xzQXLi6Ar0Vghls3Eo5ojhjUeEv+LXVhr X-Received: by 2002:a17:90b:1e49:b0:1f5:1dbf:4c3a with SMTP id pi9-20020a17090b1e4900b001f51dbf4c3amr1879889pjb.232.1659391353362; Mon, 01 Aug 2022 15:02:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659391353; cv=none; d=google.com; s=arc-20160816; b=xqrpR2Z4ZVY9E248E57nYsKrkFnt6WCKQestYts3rTZNf36toLAEZ8jxhCwGqmvhVF 0QAuhRi4WR4TRiZvDcmTtZbkjDce+pXjEPsX5vaHZlvACFtxU7Kqxawz43fTqCyNyOM8 o+nj/LBpQjkhdrPH0zoMfi7KI2ov7cqNNjystNSePNb9y+OCJOdxwTH0SUqQVp8NQw/+ 9JoUnLFXqASYH1BPdrNvRmYLvfAPmGGleEFDqLCQiWSk4vz5h2NwFORIaeI5CgA/ZiVs 7VgMFW/UriFZ6vBV3T1/GXVVJwfZfoLCAmceLN+yI8n2geJjWjCPQRD4cAEESVDRdQ6g rhpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=VLuEcIkJtnKoMl8gBoUClGYBdOKVju+Fv4CrOEtOKBE=; b=Z8WZqCIptMlszotpAvhIWJ5YiQkX2EmdaJgxShxq34v0QiyR79N82wIQj7DuDiJYTv 8Yyt2JSoJ6U+jxnoRyzB+o0WAGtASphmcy2myz8Ap9sCXnDdLjNfZBZJQC9KDb0T+Vkj WEtl9iClNWKeMd9QHmf+VRdKMDxAhv+fuW7oNP2Vv/9dN87PFrZa9w9eI1yzSos38zi7 rJoYEbwlhzP4SgT2cSFCIEFNzl/lfxYz2boFBoW4H32InUkqoU/KWz7eoqnzTikC2+gz k8lUjo2XTktWyM1m6nLVuNwAXzCnGH6G8ICbiQSNKabXjSseAhHbOMUp7ocSoiCWAcuW 7OUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=eQywm0SY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p42-20020a056a000a2a00b0052ae4903fb7si15395117pfh.330.2022.08.01.15.02.18; Mon, 01 Aug 2022 15:02:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=eQywm0SY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234568AbiHAVvr (ORCPT + 99 others); Mon, 1 Aug 2022 17:51:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229914AbiHAVvp (ORCPT ); Mon, 1 Aug 2022 17:51:45 -0400 Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E397039BA0; Mon, 1 Aug 2022 14:51:43 -0700 (PDT) Received: by mail-ed1-x532.google.com with SMTP id m8so15392980edd.9; Mon, 01 Aug 2022 14:51:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=VLuEcIkJtnKoMl8gBoUClGYBdOKVju+Fv4CrOEtOKBE=; b=eQywm0SYRown4Vd/F5Sr8QZB48pltddGfTACwJqvJydCzDVTxghTZycjVyzWDiMkmx tyyyQjbdCUDr7XtTQ8Ow/Tih7gktgV/xyfyLcPy3yJy4r2s7Hw+2X2ghPlZqM7nVHMLG QCJX6wLvU8q8xx94nsmL3NoW5mKF0nqg+5QEysJ6T6a29nFkAFoXXobQSBocpv57pRcL z4XvKnxw7JfBduEJtfa4MjDQ3/1NK5RV/ssYjkCtpAm2mGhzllxE7Kc1LCHKxObtsWr3 B0lzm8o+zw0y3SOBWcaSuVIiA8/z+5kCPMPgv6COUw0f4mbhhWge+Ay9Uh2mSj/gehvA g+gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=VLuEcIkJtnKoMl8gBoUClGYBdOKVju+Fv4CrOEtOKBE=; b=ICV92xa1efoaTRYgnd1C1x2J/yS0ye0Iu69cKne491bsw7uQkcc9UpltVjN1W75/Cr nkkyTt48pL4611zpYYzSQgRJiSAvIZiLmLQANsfY0J29/zchHqCXWshhMLkoZnV86u63 XyKUMGI5ChWykYxnxtUgj2kLwqiekAbBc2L1fPaSgGC6EI2GHfLLUDN0NBMheE86vI+n oNTWbCE4ax35CMugzanDNWnnE/afj2lqJFrMueT3AxBQ6r2b7lki2CU7dwsigE2ndovu RPBX6h/DGadhU1/CVP8W9hnCs6F8POWN05eAlt71/lqv/08ZrKIXyMZUghqojSw72zks c1sg== X-Gm-Message-State: ACgBeo2o9T6Rwi9niqGYkY9w9o/iWX2q+2bGQiIWU0jlhYPRmlAeq5Ox xI4i7XSmTnE6/7bgeR2pqCW2evwjNIwPZJWt+PQ= X-Received: by 2002:aa7:de18:0:b0:43d:30e2:d22b with SMTP id h24-20020aa7de18000000b0043d30e2d22bmr15551672edv.224.1659390702488; Mon, 01 Aug 2022 14:51:42 -0700 (PDT) MIME-Version: 1.0 References: <20220801175407.2647869-1-haoluo@google.com> <20220801175407.2647869-6-haoluo@google.com> In-Reply-To: <20220801175407.2647869-6-haoluo@google.com> From: Andrii Nakryiko Date: Mon, 1 Aug 2022 14:51:31 -0700 Message-ID: Subject: Re: [PATCH bpf-next v6 5/8] selftests/bpf: Test cgroup_iter. To: Hao Luo Cc: linux-kernel@vger.kernel.org, bpf@vger.kernel.org, cgroups@vger.kernel.org, netdev@vger.kernel.org, Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , Tejun Heo , Zefan Li , KP Singh , Johannes Weiner , Michal Hocko , Benjamin Tissoires , John Fastabend , Michal Koutny , Roman Gushchin , David Rientjes , Stanislav Fomichev , Shakeel Butt , Yosry Ahmed Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Aug 1, 2022 at 10:54 AM Hao Luo wrote: > > Add a selftest for cgroup_iter. The selftest creates a mini cgroup tree > of the following structure: > > ROOT (working cgroup) > | > PARENT > / \ > CHILD1 CHILD2 > > and tests the following scenarios: > > - invalid cgroup fd. > - pre-order walk over descendants from PARENT. > - post-order walk over descendants from PARENT. > - walk of ancestors from PARENT. > - early termination. > > Acked-by: Yonghong Song > Signed-off-by: Hao Luo > --- > .../selftests/bpf/prog_tests/cgroup_iter.c | 193 ++++++++++++++++++ > tools/testing/selftests/bpf/progs/bpf_iter.h | 7 + > .../testing/selftests/bpf/progs/cgroup_iter.c | 39 ++++ > 3 files changed, 239 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup_iter.c > create mode 100644 tools/testing/selftests/bpf/progs/cgroup_iter.c > > diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_iter.c b/tools/testing/selftests/bpf/prog_tests/cgroup_iter.c > new file mode 100644 > index 000000000000..5dc843a3f507 > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_iter.c > @@ -0,0 +1,193 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (c) 2022 Google */ > + > +#include > +#include > +#include > +#include "cgroup_iter.skel.h" > +#include "cgroup_helpers.h" > + > +#define ROOT 0 > +#define PARENT 1 > +#define CHILD1 2 > +#define CHILD2 3 > +#define NUM_CGROUPS 4 > + > +#define PROLOGUE "prologue\n" > +#define EPILOGUE "epilogue\n" > + > +#define format_expected_output1(cg_id1) \ > + snprintf(expected_output, sizeof(expected_output), \ > + PROLOGUE "%8llu\n" EPILOGUE, (cg_id1)) > + > +#define format_expected_output2(cg_id1, cg_id2) \ > + snprintf(expected_output, sizeof(expected_output), \ > + PROLOGUE "%8llu\n%8llu\n" EPILOGUE, \ > + (cg_id1), (cg_id2)) > + > +#define format_expected_output3(cg_id1, cg_id2, cg_id3) \ > + snprintf(expected_output, sizeof(expected_output), \ > + PROLOGUE "%8llu\n%8llu\n%8llu\n" EPILOGUE, \ > + (cg_id1), (cg_id2), (cg_id3)) > + you use format_expected_output{1,2} just once and format_expected_output3 twice. Is it worth defining macros for that? > +const char *cg_path[] = { > + "/", "/parent", "/parent/child1", "/parent/child2" > +}; > + > +static int cg_fd[] = {-1, -1, -1, -1}; > +static unsigned long long cg_id[] = {0, 0, 0, 0}; > +static char expected_output[64]; > + > +int setup_cgroups(void) > +{ > + int fd, i = 0; > + > + for (i = 0; i < NUM_CGROUPS; i++) { > + fd = create_and_get_cgroup(cg_path[i]); > + if (fd < 0) > + return fd; > + > + cg_fd[i] = fd; > + cg_id[i] = get_cgroup_id(cg_path[i]); > + } > + return 0; > +} > + > +void cleanup_cgroups(void) some more statics to cover (same for setup_cgroups) > +{ > + int i; > + > + for (i = 0; i < NUM_CGROUPS; i++) > + close(cg_fd[i]); > +} > + > +static void read_from_cgroup_iter(struct bpf_program *prog, int cgroup_fd, > + int order, const char *testname) > +{ > + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); > + union bpf_iter_link_info linfo; > + struct bpf_link *link; > + int len, iter_fd; > + static char buf[64]; > + > + memset(&linfo, 0, sizeof(linfo)); > + linfo.cgroup.cgroup_fd = cgroup_fd; > + linfo.cgroup.traversal_order = order; > + opts.link_info = &linfo; > + opts.link_info_len = sizeof(linfo); > + > + link = bpf_program__attach_iter(prog, &opts); > + if (!ASSERT_OK_PTR(link, "attach_iter")) > + return; > + > + iter_fd = bpf_iter_create(bpf_link__fd(link)); > + if (iter_fd < 0) > + goto free_link; > + > + memset(buf, 0, sizeof(buf)); > + while ((len = read(iter_fd, buf, sizeof(buf))) > 0) > + ; this is broken, in general, you are overriding buffer content with each call to len I think you intended to advance buf after each read() call (and reduce remaining available buf size)? > + > + ASSERT_STREQ(buf, expected_output, testname); > + > + /* read() after iter finishes should be ok. */ > + if (len == 0) > + ASSERT_OK(read(iter_fd, buf, sizeof(buf)), "second_read"); > + > + close(iter_fd); > +free_link: > + bpf_link__destroy(link); > +} > + > +/* Invalid cgroup. */ > +static void test_invalid_cgroup(struct cgroup_iter *skel) > +{ > + DECLARE_LIBBPF_OPTS(bpf_iter_attach_opts, opts); > + union bpf_iter_link_info linfo; > + struct bpf_link *link; > + > + memset(&linfo, 0, sizeof(linfo)); > + linfo.cgroup.cgroup_fd = (__u32)-1; > + opts.link_info = &linfo; > + opts.link_info_len = sizeof(linfo); > + > + link = bpf_program__attach_iter(skel->progs.cgroup_id_printer, &opts); > + if (!ASSERT_ERR_PTR(link, "attach_iter")) > + bpf_link__destroy(link); nit: you can call bpf_link__destroy() even if link is NULL or IS_ERR > +} > + [...] > diff --git a/tools/testing/selftests/bpf/progs/cgroup_iter.c b/tools/testing/selftests/bpf/progs/cgroup_iter.c > new file mode 100644 > index 000000000000..2a34d146d6df > --- /dev/null > +++ b/tools/testing/selftests/bpf/progs/cgroup_iter.c > @@ -0,0 +1,39 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright (c) 2022 Google */ > + > +#include "bpf_iter.h" > +#include > +#include > + > +char _license[] SEC("license") = "GPL"; > +volatile int terminate_early = 0; > +volatile u64 terminal_cgroup = 0; > + nit: you shouldn't need volatile for non-const global variables. Did you see any problems without volatile? > +static inline u64 cgroup_id(struct cgroup *cgrp) > +{ > + return cgrp->kn->id; > +} > + [...]