Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp2219596imn; Mon, 1 Aug 2022 16:01:27 -0700 (PDT) X-Google-Smtp-Source: AA6agR65SdPBzAypomoMUIRfRzFQ/SKZrqZSmPpcUI+ekWMdzzpr+4hYNhyIBJBunbECutSS6AUf X-Received: by 2002:a17:90b:1c8e:b0:1f1:b5a9:20c3 with SMTP id oo14-20020a17090b1c8e00b001f1b5a920c3mr21719490pjb.47.1659394887631; Mon, 01 Aug 2022 16:01:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659394887; cv=none; d=google.com; s=arc-20160816; b=aTVqr9Q6h3uKVN5S922aHDdHt2FqEVWM/Cdyns8CyxOEfQlZ9D4ZhTjb5cnXXI7OWP 1NIgJnB6vkcJG7U4uxxdGt826sRIvwSXlRYHdNWEiiKQn8k/yxI7JlHWQtHa3NqXVVvr ege2bJ+iUs/bncd3hlO8BztBRa8rNe9RZ6KJesfj/EC8hQTQFOAfBj52IwcW3A/YuUZl VtfGvL5BhkWJ3m89XwUKcyh33aYHYv4i0dHiwbpG64HbmyYoJB7/u4cZmBwqxfXdzZpF YjIE2NFvex7zsP9RomRMlNTLaRow14K4at4eV6VHE7p4yguMrkK/JZC4TxeyBHX3Focc wdDg== 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=C5gulj3V+LjVOIWRA41K2fBbqvQq1arjIETo1B+W8Bc=; b=peXTrY93UHle9KeMEF9h2Fi6fJTz6NSNbCKyXqKrg4OTja5SCruAO9AjBJIRVDGw+0 QxmLX0s+v5QGLd2r+in+ctMqchuvQVPFugHCSb9F/RaXKp5XCNlHSVdSfltX8exvzTr2 bDxuPhEICHt/gFWBYsZ8Pg4kirb/l8pyjnayjtNfSM2/mE52HEqYkxFhW8fW+XsupZdn Y4ZQTcId6GE1poTOrCWUaLfMUwgn9ruMwO81WebnIeWzVy9QDCkvyF4FyMN5FN/IkERA +DdiB3cKdKwKpozBPkyfqhqTvk8lWiqhsgwYXKL2UC/27dC6KwWgcVAWr3L63jW3SSv5 H+ig== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="AvaU/i6I"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s21-20020a63e815000000b0041b7b971b11si8720965pgh.425.2022.08.01.16.01.12; Mon, 01 Aug 2022 16:01:27 -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=@google.com header.s=20210112 header.b="AvaU/i6I"; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235483AbiHAW4B (ORCPT + 99 others); Mon, 1 Aug 2022 18:56:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235469AbiHAWz6 (ORCPT ); Mon, 1 Aug 2022 18:55:58 -0400 Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 339822982A for ; Mon, 1 Aug 2022 15:55:55 -0700 (PDT) Received: by mail-qk1-x730.google.com with SMTP id o1so9506503qkg.9 for ; Mon, 01 Aug 2022 15:55:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=C5gulj3V+LjVOIWRA41K2fBbqvQq1arjIETo1B+W8Bc=; b=AvaU/i6IarMIHH8auej+84EEnlucOm6JPsbblnmJsKfFSucB0wbidIBB9ImaoESPuy D/vBYWruOWw+txPsHm4S1q/DyUBkvWFghGHvWwre5vgzRWy5XumhQbW4R68er0hrzV2C FQ1LAn2CTfvRwIB87bZM1HRFEAck2joY9DuICCp38Khk74qg5BlXOm/YrKr/0vPDdp67 y55BhBcB1o3ow6/CoLlBtqbUX4T1g7vDBMDuUfkU61w3X1Answ8BhsyYBmVCDvh9fip8 BmERpPLyRVAB9KcgcuVZbXY+J0fZggzlxmmixfr1NUAwO3k8ScFlbII7wwwMh79rpQcT HHfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=C5gulj3V+LjVOIWRA41K2fBbqvQq1arjIETo1B+W8Bc=; b=k7qSuHoj0m46lbHn0Y5jdwx4Ac/q0teY/GV8JEkmL86/uLIeeT2bFnt/z5HIaD6Av2 AHJF+afAclRNiAXG2tLihavO6BY6mJKpbiGjKWQ2tLAuF7RR1bqZ7AG7657P6beRxc51 Pan/zLLFDS8xUmZhVVjP9BrZZu4K/rwNmsHwcbxHEWF3QjvUOkEbGNzuUaszgWiIM0DE qvmFeMTZHp+fvQD9hUz+BLsAiJbT7isuDEDiTk7/QBm9RyUI9vI4O7TWh5fZ7YQ4xmA4 nsmbt4L9Q4y85PfR+eNiTttdsc/s9TxH1SMQgGKfKOxVWDEuJdgoW1I59KmGmaCeBvih jMkg== X-Gm-Message-State: AJIora959zl8G+rZu3knSG5E2qYrLNaa2CjoVhMgeYPifs1/Llry5k0b 6Do7ZHJybWD+lZx1XRzmcw0FGPl/n1TtS8BAiLI3vGQtmTyXaw== X-Received: by 2002:a05:620a:4590:b0:6b5:e884:2d2c with SMTP id bp16-20020a05620a459000b006b5e8842d2cmr13691502qkb.267.1659394554140; Mon, 01 Aug 2022 15:55:54 -0700 (PDT) MIME-Version: 1.0 References: <20220801175407.2647869-1-haoluo@google.com> <20220801175407.2647869-6-haoluo@google.com> In-Reply-To: From: Hao Luo Date: Mon, 1 Aug 2022 15:55:43 -0700 Message-ID: Subject: Re: [PATCH bpf-next v6 5/8] selftests/bpf: Test cgroup_iter. To: Andrii Nakryiko 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=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 2:51 PM Andrii Nakryiko wrote: > > 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? > If not, we'd see this snprintf and format all over the place. It looks worse than the current one I think, prefer leave as-is. > > +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) > Oops. Will fix. > > +{ > > + 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)? > Ah. My bad. Copied from bpf_iter but didn't realize that in the bpf_iter case, it didn't care about the content read from buffer. Will fix. > > + > > + 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 > Ack. Still need to ASSERT on 'link' though, so the saving is probably just an indentation. Anyway, will change. > > +} > > + > > [...] > > > 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? > Nah. I don't know about that and see there are other tests that have this pattern. Will fix. > > +static inline u64 cgroup_id(struct cgroup *cgrp) > > +{ > > + return cgrp->kn->id; > > +} > > + > > [...]