Received: by 2002:ac0:e350:0:0:0:0:0 with SMTP id g16csp417253imn; Fri, 29 Jul 2022 10:48:46 -0700 (PDT) X-Google-Smtp-Source: AA6agR5lfjMCXhtaKuOdF9DDVFFzdzMukO/hBb3adRlWHu+qCW5yFG46QMUF1kuRbjQ4AbjgX5Xo X-Received: by 2002:a05:6a00:1386:b0:52a:d5f9:2837 with SMTP id t6-20020a056a00138600b0052ad5f92837mr4966101pfg.5.1659116926379; Fri, 29 Jul 2022 10:48:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659116926; cv=none; d=google.com; s=arc-20160816; b=I1SAYnxJBj7KSK8A713ORA3CjubjwNh1+lHJQekJpWDjemaH2gA+vS4/Anke6ytOP8 J8sDSm32sUpcOEgnu3QeYlzRSrwB+rId57+3xWhR/S63QYniwD0Q1kHbUgRlQuzIqxAU M6Vn1V5SEFKA8SqYsmU12vqR1Sw9s4mAeQq6gXSWoZrthWZ3wpGw7PJFlO48JkkgCW7t SGHt9xrH7uA7EYYMagDF2K/iwnPYH0DHsNum/J+XVHSJJUx8uYrUngvLUjXotFJe4tod DAU9lZULl1OlfnM7Gf4O4LGiewPqwBLK+vrUCIecO8fXtvb6prrwHORHBn7TWguRzpdb StxA== 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=eVMmcjZdpoYkfAfcG11mdG+DM47ViTyLlI/HvC0NVqs=; b=wlPoJphON4cVDfR/NywNGEFCDA4z57s+ICh6Ih04BCyRBxHjrTerY+Jjh8dhbrHWFW HTNxlc28AuEG9B0eQoVfC4CrJ+iG7fwDDOK+s6Ltq8bohdVoeTG8xOIjfv8iSO+0CzOg zSrrecZZ/Ii0cKjL1hACgqlBZcW146Asq9/Afgd1XGcZLJ4Uxg658DbtGfAV/9A/4+vp hLKMQPrKbEZkybmqv1rbh2McQ1sC/OhG71FBD7k1PTaEZbzjRzgoYqi6BbDJPC8cGcaA k35F/n6lwUMXCZ9fVGcBgOsdR8nL60dhBO8h0/TkM1ZDdq3Zv9ZIrFiyDdNldXTQxLjU tWgQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=hrGaOCnw; 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 u5-20020a170902a60500b00169ff93b923si4185288plq.48.2022.07.29.10.48.31; Fri, 29 Jul 2022 10:48:46 -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=hrGaOCnw; 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 S232587AbiG2Rgo (ORCPT + 99 others); Fri, 29 Jul 2022 13:36:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231669AbiG2Rgj (ORCPT ); Fri, 29 Jul 2022 13:36:39 -0400 Received: from mail-qt1-x82e.google.com (mail-qt1-x82e.google.com [IPv6:2607:f8b0:4864:20::82e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D75D3683FF for ; Fri, 29 Jul 2022 10:36:37 -0700 (PDT) Received: by mail-qt1-x82e.google.com with SMTP id l14so3815205qtv.4 for ; Fri, 29 Jul 2022 10:36:37 -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=eVMmcjZdpoYkfAfcG11mdG+DM47ViTyLlI/HvC0NVqs=; b=hrGaOCnwS06hYR6y6b1XfcUUnaDFW93EvujdUgaK7Ka1oAslOsoQxI+GrU6NDwkrRm Bm/Oa2nrMa2QlCUPi2rwlMhDvM8LHxk5y1toex4ln/EOra9ZW1TWaPfT1p6nfAKF7wcZ IX8ksKXqVjWZSFtHzQsp2hpzy/rIRcdeCVIS5QR14U5wN7UB7U+RZZNnXplmxBT6VUeP Pa/gyCFzbAhxsgL3w5kZQndZItJQAj/WT20YCzfZhILOuqdyL1iLoHIq/vN2o1DWFIp5 Qa6OP91GDqftH6j4Ker359n7z+m5ajtPmTPun+GXwGgSaQSpMyuCsG36Xyoo/lchRbqW BBhA== 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=eVMmcjZdpoYkfAfcG11mdG+DM47ViTyLlI/HvC0NVqs=; b=qTijmB+jdcAMY2Wcynwzl/MyAccB79fJim503upm0u1jXzETWHmEvLUwOG140OZqrx 3Rlibg30LkxUi+hDcdujmTawUZUL/LG63PISMgsInu+WMuxARo0ibPZZjMPn8hZUDFMm dNez0EsAwdTQqcBDTiu+WiVgusKm5UAg2AnNEOYpY4S1B74onERoBT0o0YDYolqkBZYH hIKdDbVbn1gwov0H8FBVVR5hReeprF9WCF0EDchyho+2P81rBQBY6PPzWsDu5nNeaR9u Qrsl6alZfbJDSjGaV1L35LjRKbiNVVuApP4kZEK+W2GHgibRHbwdOgv5V1nkBU+Pvn2l K1ew== X-Gm-Message-State: AJIora87a8dmJpiph1VG2TgX/3qtLJzKBcao6pBVPuYKasdklTzs4sZj 4klJ41oJHIpRUJhdtk5Oyeah57iT4fenp34O1t4yuw== X-Received: by 2002:a05:622a:190c:b0:31e:fc7b:e017 with SMTP id w12-20020a05622a190c00b0031efc7be017mr4420798qtc.168.1659116196846; Fri, 29 Jul 2022 10:36:36 -0700 (PDT) MIME-Version: 1.0 References: <20220722174829.3422466-1-yosryahmed@google.com> <20220722174829.3422466-9-yosryahmed@google.com> In-Reply-To: From: Hao Luo Date: Fri, 29 Jul 2022 10:36:26 -0700 Message-ID: Subject: Re: [PATCH bpf-next v5 8/8] bpf: add a selftest for cgroup hierarchical stats collection To: Andrii Nakryiko Cc: Yosry Ahmed , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , Tejun Heo , Zefan Li , Johannes Weiner , Shuah Khan , Michal Hocko , KP Singh , Benjamin Tissoires , John Fastabend , =?UTF-8?Q?Michal_Koutn=C3=BD?= , Roman Gushchin , David Rientjes , Stanislav Fomichev , Greg Thelen , Shakeel Butt , open list , Networking , bpf , Cgroups 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=unavailable 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 Hi Andrii, Thanks for taking a look. On Thu, Jul 28, 2022 at 3:40 PM Andrii Nakryiko wrote: > > On Fri, Jul 22, 2022 at 10:49 AM Yosry Ahmed wrote: > > [...] > > + > > +#define CGROUP_PATH(p, n) {.path = #p"/"#n, .name = #n} > > + > > +static struct { > > + const char *path, *name; > > + unsigned long long id; > > + int fd; > > +} cgroups[] = { > > + CGROUP_PATH(/, test), > > + CGROUP_PATH(/test, child1), > > + CGROUP_PATH(/test, child2), > > + CGROUP_PATH(/test/child1, child1_1), > > + CGROUP_PATH(/test/child1, child1_2), > > + CGROUP_PATH(/test/child2, child2_1), > > + CGROUP_PATH(/test/child2, child2_2), > > nit: why are these arguments not explicit string literals?... > CGROUP_PATH("/test/child1", "child1_1") explicitly shows that those > values are used as strings > No particular reason I think. String literals are good. Will fix in v6. > > +}; > > + > > +#define N_CGROUPS ARRAY_SIZE(cgroups) > > +#define N_NON_LEAF_CGROUPS 3 > > + > > +int root_cgroup_fd; > > +bool mounted_bpffs; > > + > > static? > Yeah, we were careless about 'static' or 'inline'. I am going to go over the code and mark functions/vars 'static' properly. > > +static int read_from_file(const char *path, char *buf, size_t size) > > +{ > > + int fd, len; > > + > > + fd = open(path, O_RDONLY); > > + if (fd < 0) { > > + log_err("Open %s", path); > > + return 1; > > + } > > + len = read(fd, buf, size); > > + if (len < 0) > > + log_err("Read %s", path); > > + else > > + buf[len] = 0; > > + close(fd); > > + return len < 0; > > +} > > + > > [...] > > > + /* Also dump stats for root */ > > + err = setup_cgroup_iter(obj, root_cgroup_fd, CG_ROOT_NAME); > > + if (!ASSERT_OK(err, "setup_cgroup_iter")) > > + return err; > > + > > + /* Attach rstat flusher */ > > + link = bpf_program__attach(obj->progs.vmscan_flush); > > + if (!ASSERT_OK_PTR(link, "attach rstat")) > > + return libbpf_get_error(link); > > this is dangerous, because ASSERT_OK_PTR might overwrite errno by the > time we get to libbpf_get_error() call. link is NULL and > libbpf_get_error() extracts error from errno. It's best to just return > fixed error code here, or otherwise you'd need to remember err before > ASSERT_OK_PTR() call. > Ack. We can just return a fixed error code here. Thanks. > > + obj->links.vmscan_flush = link; > > + > > + /* Attach tracing programs that will calculate vmscan delays */ > > + link = bpf_program__attach(obj->progs.vmscan_start); > > + if (!ASSERT_OK_PTR(obj, "attach raw_tracepoint")) > > + return libbpf_get_error(link); > > + obj->links.vmscan_start = link; > > + > > + link = bpf_program__attach(obj->progs.vmscan_end); > > + if (!ASSERT_OK_PTR(obj, "attach raw_tracepoint")) > > + return libbpf_get_error(link); > > + obj->links.vmscan_end = link; > > + > > + *skel = obj; > > + return 0; > > +} > > + > > +void destroy_progs(struct cgroup_hierarchical_stats *skel) > > static? > Ack. > > +{ > > + char path[128]; > > + int i; > > + > > + for (i = 0; i < N_CGROUPS; i++) { > > + /* Delete files in bpffs that cgroup_iters are pinned in */ > > + snprintf(path, 128, "%s%s", BPFFS_VMSCAN, > > + cgroups[i].name); > > + ASSERT_OK(remove(path), "remove cgroup_iter pin"); > > + } > > + > > + /* Delete root file in bpffs */ > > + snprintf(path, 128, "%s%s", BPFFS_VMSCAN, CG_ROOT_NAME); > > + ASSERT_OK(remove(path), "remove cgroup_iter root pin"); > > + cgroup_hierarchical_stats__destroy(skel); > > +} > > + > > +void test_cgroup_hierarchical_stats(void) > > +{ > > + struct cgroup_hierarchical_stats *skel = NULL; > > + > > + if (setup_hierarchy()) > > + goto hierarchy_cleanup; > > + if (setup_progs(&skel)) > > + goto cleanup; > > + if (induce_vmscan()) > > + goto cleanup; > > + check_vmscan_stats(); > > +cleanup: > > + destroy_progs(skel); > > +hierarchy_cleanup: > > + destroy_hierarchy(); > > +} > > diff --git a/tools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c b/tools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c > > new file mode 100644 > > index 000000000000..85a65a72482e > > --- /dev/null > > +++ b/tools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c > > @@ -0,0 +1,239 @@ > > +// SPDX-License-Identifier: GPL-2.0-only > > +/* > > + * Functions to manage eBPF programs attached to cgroup subsystems > > + * > > + * Copyright 2022 Google LLC. > > + */ > > +#include "vmlinux.h" > > +#include > > +#include > > + > > +char _license[] SEC("license") = "GPL"; > > + > > +/* > > + * Start times are stored per-task, not per-cgroup, as multiple tasks in one > > + * cgroup can perform reclain concurrently. > > typo: reclaim? > Ack. Will fix. > > + */ > > +struct { > > + __uint(type, BPF_MAP_TYPE_TASK_STORAGE); > > + __uint(map_flags, BPF_F_NO_PREALLOC); > > + __type(key, int); > > + __type(value, __u64); > > +} vmscan_start_time SEC(".maps"); > > + > > [...] > > > +static inline int create_vmscan_percpu_elem(__u64 cg_id, __u64 state) > > +{ > > + struct vmscan_percpu pcpu_init = {.state = state, .prev = 0}; > > + int err; > > + > > + err = bpf_map_update_elem(&pcpu_cgroup_vmscan_elapsed, &cg_id, > > + &pcpu_init, BPF_NOEXIST); > > + if (err) { > > + bpf_printk("failed to create pcpu entry for cgroup %llu: %d\n" > > + , cg_id, err); > > + return 1; > > + } > > + return 0; > > +} > > + > > +static inline int create_vmscan_elem(__u64 cg_id, __u64 state, __u64 pending) > > all those inlines above are not necessary, they don't have to be > actually inlined, right? > No. They don't have to. Will fix this. > > +{ > > + struct vmscan init = {.state = state, .pending = pending}; > > + int err; > > + > > + err = bpf_map_update_elem(&cgroup_vmscan_elapsed, &cg_id, > > + &init, BPF_NOEXIST); > > + if (err) { > > + bpf_printk("failed to create entry for cgroup %llu: %d\n" > > + , cg_id, err); > > + return 1; > > + } > > + return 0; > > +} > > + > > +SEC("tp_btf/mm_vmscan_memcg_reclaim_begin") > > +int BPF_PROG(vmscan_start, int order, gfp_t gfp_flags) > > +{ > > + struct task_struct *task = bpf_get_current_task_btf(); > > + __u64 *start_time_ptr; > > + > > + start_time_ptr = bpf_task_storage_get(&vmscan_start_time, task, 0, > > + BPF_LOCAL_STORAGE_GET_F_CREATE); > > + if (!start_time_ptr) { > > + bpf_printk("error retrieving storage\n"); > > does user-space part read these trace_printk messages? If not, let's > remove them from the test > No. I will remove them in v6. > > + return 0; > > + } > > + > > + *start_time_ptr = bpf_ktime_get_ns(); > > + return 0; > > +} > > + > > [...]