From patchwork Sat Oct 7 14:02:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13412355 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 633962771D for ; Sat, 7 Oct 2023 14:03:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="QGu9kd9H" Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40C66BA; Sat, 7 Oct 2023 07:03:17 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1c3d8fb23d9so22730875ad.0; Sat, 07 Oct 2023 07:03:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696687397; x=1697292197; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=b0XN7Vex7MkGdSDdhpaAp8dz+3fJJkVdmToBRqKC+GA=; b=QGu9kd9HrQBeWBOll61sV782W7azIZk3t3+HZzRqEzi1oryreVe3kI5JRwAeqqxHAl YaurZrAA+jWQafGaDHmLDs31EMAV9SEx26D7YiYFXWKyuioiM9FRWYvDes+e652YYvjc O/eA/Yk/hR8trX5z4GzyClizPlbp5nlqd2YfhvEeDr/OI3uFE9e4TCZVcpOdvO0QG48B NDzWS4Q+PrO90Wj+QLB8Sy9NNMQIBcOk+5/dtgJkvKqh3ybfUr3AD5hF1Cr60gIE8c6r Fcp/gb25c4kAIOAuT8P45JhtACMbKL5KtFcAE1HAuvBOyly+uGstaApQPLrQOFzpztrl exhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696687397; x=1697292197; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=b0XN7Vex7MkGdSDdhpaAp8dz+3fJJkVdmToBRqKC+GA=; b=jA+ELtQ40Us1RwlSIA+14UFz2viRRhXguCaKtjgLqAL+Q3iUwgdHRwh3uaHUfB/m7X rEx5rIll9RDtX277b8kr/J3b4lDJNkEhQ3wZwFFtZA0FRxiUPmYBh4Tb/dZd9Hzvrzzn Ez+FM+rK/hbVxsY1x+KwOOQPA+7tTBsGHHpVXShjiZL8Pagxws7KkArKoj39hVO8kiT2 7rmDek/WEBUtWzb0xQ9TeG9SOpgz4JWppV0/sG3mY7LSId2nLTpQYJ4LqSe621pVbDsD UvLFL3/hu+r2Exjqb7X6hBNaYZZZgPdMrWDzGNk6HPFduFgUpsnt78GrvJnKTpeMHo6J OMIw== X-Gm-Message-State: AOJu0YxQFi5Ctr5lkT8lN0K6SD74Syr0nq4lcK8Ne6kHmMaWFNebmx8O 3ztEygrwTC9lCQG/vLJABGU= X-Google-Smtp-Source: AGHT+IG3kaDi0ghKnwH1wunGSJ9WRy7cYwWt06n6MnRzZ+SH1YfRKbazOlN2CKSbuK+9nyXZn+wy+A== X-Received: by 2002:a17:902:dac3:b0:1bb:b86e:8d60 with SMTP id q3-20020a170902dac300b001bbb86e8d60mr12849206plx.46.1696687396664; Sat, 07 Oct 2023 07:03:16 -0700 (PDT) Received: from vultr.guest ([45.77.191.53]) by smtp.gmail.com with ESMTPSA id l13-20020a170902f68d00b001c0a414695dsm5897550plg.62.2023.10.07.07.03.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 07:03:16 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 1/8] cgroup: Don't have to hold cgroup_mutex in task_cgroup_from_root() Date: Sat, 7 Oct 2023 14:02:57 +0000 Message-Id: <20231007140304.4390-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231007140304.4390-1-laoar.shao@gmail.com> References: <20231007140304.4390-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC The task cannot modify cgroups if we have already acquired the css_set_lock, thus eliminating the need to hold the cgroup_mutex. Following this change, task_cgroup_from_root() can be employed in non-sleepable contexts. Signed-off-by: Yafang Shao --- kernel/cgroup/cgroup.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 1fb7f562289d..bd1692f48be5 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -1453,7 +1453,7 @@ static struct cgroup *cset_cgroup_from_root(struct css_set *cset, /* * Return the cgroup for "task" from the given hierarchy. Must be - * called with cgroup_mutex and css_set_lock held. + * called with css_set_lock held. */ struct cgroup *task_cgroup_from_root(struct task_struct *task, struct cgroup_root *root) @@ -1462,7 +1462,8 @@ struct cgroup *task_cgroup_from_root(struct task_struct *task, * No need to lock the task - since we hold css_set_lock the * task can't change groups. */ - return cset_cgroup_from_root(task_css_set(task), root); + lockdep_assert_held(&css_set_lock); + return __cset_cgroup_from_root(task_css_set(task), root); } /* From patchwork Sat Oct 7 14:02:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13412356 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 69F6827EFB for ; Sat, 7 Oct 2023 14:03:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cs8JY3Ei" Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 013E9BF; Sat, 7 Oct 2023 07:03:18 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1c88b46710bso15851165ad.1; Sat, 07 Oct 2023 07:03:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696687398; x=1697292198; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MkiSkPJNlBcvA6l1NV7Ut81Ds5KMxgHRuVtA5iQDQsI=; b=cs8JY3EimbuWuvf4U8SkPXEl7a/GolsviKZrx2EpFy4Uo2ktVZ+r3z6x21t+1Wlq+v SQhJA2TPyKzab+nf2D7nmE9hWpfgYEEy+iLLmMNDLOKGFwI6trR8OWtfiNMV5Ehw9GhH gq8cK+vAtBZFZawwCkDL66kcQqQsXhlLHBC40v6pI8ELjZEyVOdvYh6GS+fPs+3gFRgx i2ahi04fSVXk8ICbVa9MLp3PDrndfiF2Sk2NN9JapvgBu8eLRmkDUOXhBC92sDZcjCCr XWjnb5iGoDAOM+S8T0IBEwobNj1eGNyOSxJTz5dp3G4MXzAW0kHXFWY0mh0HtQIvKZ5b 4AOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696687398; x=1697292198; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MkiSkPJNlBcvA6l1NV7Ut81Ds5KMxgHRuVtA5iQDQsI=; b=EbNAE2HZ2oVekCcUjWrAQGm2Z8lfWsZRdEAIIz7Q5VCCesWmC9xzQTSfQYK4dqKm3q FeyckhYAnOLf98GYfNKLT92z8o6HxaTYFnjhUPQfetIVmJ5dzivPtOjz7jLZtYYhrBiM 1X1o6kbMP399JmthsERttD5GwHd7U+D5RYwQ00pMINkgJl3B0c+7m2AE07tQhXol+X5P wxSJspvAuI11dg7rJsgnjfAVuylrM+RTPkeaGCOrOuxzU3se8QWC1fHarYsOdjxr0p/f XJXXwDxAvAPehOKIw8JnlSIHITLQJy+qH287tbRs7g5tV7RurrYbdwpH+OmRg9Dj7TeJ BPAg== X-Gm-Message-State: AOJu0YxETiB4tvWGN7fP3wVTVyJinFGpOpscVtecOOJv86NmBlk/Dt4m /kCeWTyD6ScD3AeDpYzCINg= X-Google-Smtp-Source: AGHT+IFp1QBFglX6sgNBthLjSggy2Qb0y5OJgEH+4nXhgW0J6vvd+IMzpex1q4E2TiCX2lYtihQZHA== X-Received: by 2002:a17:903:41c1:b0:1c6:30d1:7214 with SMTP id u1-20020a17090341c100b001c630d17214mr11696784ple.55.1696687398305; Sat, 07 Oct 2023 07:03:18 -0700 (PDT) Received: from vultr.guest ([45.77.191.53]) by smtp.gmail.com with ESMTPSA id l13-20020a170902f68d00b001c0a414695dsm5897550plg.62.2023.10.07.07.03.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 07:03:17 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 2/8] cgroup: Add new helpers for cgroup1 hierarchy Date: Sat, 7 Oct 2023 14:02:58 +0000 Message-Id: <20231007140304.4390-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231007140304.4390-1-laoar.shao@gmail.com> References: <20231007140304.4390-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Two new helpers are added for cgroup1 hierarchy: - task_cgroup1_id_within_hierarchy Retrieves the associated cgroup ID of a task within a specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its hierarchy ID. - task_ancestor_cgroup1_id_within_hierarchy Retrieves the associated ancestor cgroup ID of a task whithin a specific cgroup1 hierarchy. The specific ancestor level is determined by its ancestor level. These helper functions have been added to facilitate the tracing of tasks within a particular container or cgroup in BPF programs. It's important to note that these helpers are designed specifically for cgroup1. Signed-off-by: Yafang Shao --- include/linux/cgroup.h | 9 ++++- kernel/cgroup/cgroup-internal.h | 2 - kernel/cgroup/cgroup-v1.c | 67 +++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index b307013b9c6c..65bde6eb41ef 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -71,6 +71,8 @@ struct css_task_iter { extern struct file_system_type cgroup_fs_type; extern struct cgroup_root cgrp_dfl_root; extern struct css_set init_css_set; +extern struct list_head cgroup_roots; +extern spinlock_t css_set_lock; #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys; #include @@ -159,6 +161,8 @@ void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags, struct css_task_iter *it); struct task_struct *css_task_iter_next(struct css_task_iter *it); void css_task_iter_end(struct css_task_iter *it); +struct cgroup *task_cgroup_from_root(struct task_struct *task, + struct cgroup_root *root); /** * css_for_each_child - iterate through children of a css @@ -388,7 +392,6 @@ static inline void cgroup_unlock(void) * as locks used during the cgroup_subsys::attach() methods. */ #ifdef CONFIG_PROVE_RCU -extern spinlock_t css_set_lock; #define task_css_set_check(task, __c) \ rcu_dereference_check((task)->cgroups, \ rcu_read_lock_sched_held() || \ @@ -855,4 +858,8 @@ static inline void cgroup_bpf_put(struct cgroup *cgrp) {} #endif /* CONFIG_CGROUP_BPF */ +u64 task_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id); +u64 task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id, + int ancestor_level); + #endif /* _LINUX_CGROUP_H */ diff --git a/kernel/cgroup/cgroup-internal.h b/kernel/cgroup/cgroup-internal.h index c56071f150f2..2c32a80c1334 100644 --- a/kernel/cgroup/cgroup-internal.h +++ b/kernel/cgroup/cgroup-internal.h @@ -164,9 +164,7 @@ struct cgroup_mgctx { #define DEFINE_CGROUP_MGCTX(name) \ struct cgroup_mgctx name = CGROUP_MGCTX_INIT(name) -extern spinlock_t css_set_lock; extern struct cgroup_subsys *cgroup_subsys[]; -extern struct list_head cgroup_roots; /* iterate across the hierarchies */ #define for_each_root(root) \ diff --git a/kernel/cgroup/cgroup-v1.c b/kernel/cgroup/cgroup-v1.c index c487ffef6652..18064de0a883 100644 --- a/kernel/cgroup/cgroup-v1.c +++ b/kernel/cgroup/cgroup-v1.c @@ -1263,6 +1263,73 @@ int cgroup1_get_tree(struct fs_context *fc) return ret; } +/** + * task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID from + * a task within a specific cgroup1 hierarchy. + * @task: The task to be tested + * @hierarchy_id: The hierarchy ID of a cgroup1 + * + * We limit it to cgroup1 only. + */ +u64 task_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id) +{ + struct cgroup_root *root; + struct cgroup *cgrp; + u64 cgid = 0; + + spin_lock_irq(&css_set_lock); + list_for_each_entry(root, &cgroup_roots, root_list) { + /* cgroup1 only*/ + if (root == &cgrp_dfl_root) + continue; + if (root->hierarchy_id != hierarchy_id) + continue; + cgrp = task_cgroup_from_root(tsk, root); + WARN_ON_ONCE(!cgrp); + cgid = cgroup_id(cgrp); + break; + } + spin_unlock_irq(&css_set_lock); + return cgid; +} + +/** + * task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated ancestor + * cgroup ID from a task within a specific cgroup1 hierarchy. + * @task: The task to be tested + * @hierarchy_id: The hierarchy ID of a cgroup1 + * @ancestor_level: level of ancestor to find starting from root + * + * We limit it to cgroup1 only. + */ +u64 task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *tsk, int hierarchy_id, + int ancestor_level) +{ + struct cgroup *cgrp, *ancestor; + struct cgroup_root *root; + u64 cgid = 0; + + spin_lock_irq(&css_set_lock); + list_for_each_entry(root, &cgroup_roots, root_list) { + /* cgroup1 only*/ + if (root == &cgrp_dfl_root) + continue; + if (root->hierarchy_id != hierarchy_id) + continue; + + cgrp = task_cgroup_from_root(tsk, root); + WARN_ON_ONCE(!cgrp); + ancestor = cgroup_ancestor(cgrp, ancestor_level); + if (!ancestor) + break; + + cgid = cgroup_id(ancestor); + break; + } + spin_unlock_irq(&css_set_lock); + return cgid; +} + static int __init cgroup1_wq_init(void) { /* From patchwork Sat Oct 7 14:02:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13412357 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0E4E72771D for ; Sat, 7 Oct 2023 14:03:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KU23cJD0" Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E1F3BD; Sat, 7 Oct 2023 07:03:20 -0700 (PDT) Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1c723f1c80fso22655865ad.1; Sat, 07 Oct 2023 07:03:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696687400; x=1697292200; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=LivZSfWCIUy5PpRi4LmDDyNFOui9+vIDSPEwDR4bKrQ=; b=KU23cJD0fQVql8ViEE/maaRDtcoGXWt0mtsIRir78EuFkRiZk5cj7xevhr0N4LWu9u 6pBbHffamFLbWmmTerx3YdbTUnqNPGfRyIy1zY1t56jRBduT5eHrAsN/+yE8KZvvW+VS cZkzaBReNyjxY/2AI5KL6oMbqxfhw1YZW1f3X82r1fBStWKJFoS1G/V+lNHXOOhWBYfb +MakrpWp9NTXABUKFkC9G+6jlMNl+p5PVb+Yygro2boqnXeGVPxqxsbMdDGGxktuUQl2 KPfU3v065OrZ/I9MCfznacbkrtpfig5tV2cO+QBrdylWlJ65jrX/gds9MpR1HyheNqiW ys1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696687400; x=1697292200; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LivZSfWCIUy5PpRi4LmDDyNFOui9+vIDSPEwDR4bKrQ=; b=sU4ryyMYJHaFdYjHXEQ9XXrWqpHWnMJYC0dTBE4Lr5f3oIa9lgWeBpRaNqZzze8EtQ 6aDdrguoutM8Zbt/pMGdtldU9cy1x1TFOx9dFO1qxj8MFoiZapqxjysQ3bF9RlyjEJ+7 A5v/nNQtfxWtB4hWEi1gsstl+v7GCrQIRbNkoV07MxbvYOfUfUwSw7v4GHPzG/f7plSV BMGLi2Yen6rGeVUcc16c3IY3QzCcK/LNCFU0n/TF4p7hrX3zVT/6t95YcFPRLxhHIvro ZM50P43fbrkweXlkxzydVID9ADVbhqCelJoGKNiJO+B5yPO4YszVAm0VkR3xeDZXX16C PFxw== X-Gm-Message-State: AOJu0Yyp+dbyY0QhtogTv8GzoK7avmjg1kbr1pALxsM5KbQWjenZGdZ4 cYWR0lTYbEw/PD4kqtIouoE= X-Google-Smtp-Source: AGHT+IGnJGM/pQcim+t2+9WjcgTCzzeVDoPAyYRShRNA04b+Tvx3soDrYvABnMi3D4KMNSmDUQ3nQg== X-Received: by 2002:a17:903:32cf:b0:1c7:56b9:eae with SMTP id i15-20020a17090332cf00b001c756b90eaemr12318684plr.32.1696687399959; Sat, 07 Oct 2023 07:03:19 -0700 (PDT) Received: from vultr.guest ([45.77.191.53]) by smtp.gmail.com with ESMTPSA id l13-20020a170902f68d00b001c0a414695dsm5897550plg.62.2023.10.07.07.03.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 07:03:19 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 3/8] bpf: Add kfuncs for cgroup1 hierarchy Date: Sat, 7 Oct 2023 14:02:59 +0000 Message-Id: <20231007140304.4390-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231007140304.4390-1-laoar.shao@gmail.com> References: <20231007140304.4390-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Two new kfuncs are added to retrieve the cgroup1 IDs. - bpf_task_cgroup1_id_within_hierarchy Retrieves the associated cgroup ID of a task whithin a specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its hierarchy ID. - bpf_task_ancestor_cgroup1_id_within_hierarchy Retrieves the associated ancestor cgroup ID of a task whithin a specific cgroup1 hierarchy. he specific ancestor cgroup is determined by the ancestor level within the cgroup1 hierarchy. These two new kfuncs enable the tracing of tasks within a designated container or cgroup directory in BPF programs. Signed-off-by: Yafang Shao --- kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index dd1c69ee3375..39ec6f9f2027 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2214,6 +2214,30 @@ __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task, { return task_under_cgroup_hierarchy(task, ancestor); } + +/** + * bpf_task_cgroup_id_within_hierarchy - Retrieves the associated cgroup ID of a + * task within a specific cgroup1 hierarchy. + * @task: The target task + * @hierarchy_id: The ID of a cgroup1 hierarchy + */ +__bpf_kfunc u64 bpf_task_cgroup1_id_within_hierarchy(struct task_struct *task, int hierarchy_id) +{ + return task_cgroup1_id_within_hierarchy(task, hierarchy_id); +} + +/** + * bpf_task_ancestor_cgroup_id_within_hierarchy - Retrieves the associated + * ancestor cgroup ID of a task within a specific cgroup1 hierarchy. + * @task: The target task + * @hierarchy_id: The ID of a cgroup1 hierarchy + * @ancestor_level: The cgroup level of the ancestor in the cgroup1 hierarchy + */ +__bpf_kfunc u64 bpf_task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *task, + int hierarchy_id, int ancestor_level) +{ + return task_ancestor_cgroup1_id_within_hierarchy(task, hierarchy_id, ancestor_level); +} #endif /* CONFIG_CGROUPS */ /** @@ -2520,6 +2544,8 @@ BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE) BTF_ID_FLAGS(func, bpf_cgroup_ancestor, KF_ACQUIRE | KF_RCU | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_cgroup_from_id, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_task_under_cgroup, KF_RCU) +BTF_ID_FLAGS(func, bpf_task_cgroup1_id_within_hierarchy, KF_RCU) +BTF_ID_FLAGS(func, bpf_task_ancestor_cgroup1_id_within_hierarchy, KF_RCU) #endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_throw) From patchwork Sat Oct 7 14:03:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13412358 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 83C5627EFB for ; Sat, 7 Oct 2023 14:03:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Sa9UsKe3" Received: from mail-pl1-x636.google.com (mail-pl1-x636.google.com [IPv6:2607:f8b0:4864:20::636]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 33043C5; Sat, 7 Oct 2023 07:03:22 -0700 (PDT) Received: by mail-pl1-x636.google.com with SMTP id d9443c01a7336-1c723f1c80fso22656085ad.1; Sat, 07 Oct 2023 07:03:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696687401; x=1697292201; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YxmghcOcb4Lv1mKG5g6exWWWkajf/SPl1xKizlQXNEE=; b=Sa9UsKe33dTiCY3bB2MaRtiMogiuNxho6tGFMfRq09aWsDEflo0zr/PiFGDsMJ9T1M 0SX4DQw1fvdOkOTV+zg0tJMTGO7IjiI7mhFtCDlSswmvI374Kop3OzRwdNKi3HiRPTEJ CUVoW1IH/4sQbSlWs4aojpxtkbLxxPTqMRuXenj9eDXAQYdbZfnF42uJPfohFZgxeMvJ e8F36ghm6huXPjdGLlf+Ark3p9IOY/QzGYAqEXIAtLeX6Ml3IqiV83se8A5KqwIR3+dP iA/4SlHfLuHss4BKxpoZcPJDyvauo0gkwx866hv7omf+gVFEdYGt4UWiae2OyYsg4cmP dn/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696687401; x=1697292201; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=YxmghcOcb4Lv1mKG5g6exWWWkajf/SPl1xKizlQXNEE=; b=r0awJEgu5+VWmVDzBb8tjnwiFTdR0fw7a4NCd+ZAf54xnAAnnzmEgCH+btIYywEOQ3 dundZJChF5sXP9KrwZ/FzxnKZ7jbQj6iJfYCJOPlBsBTwlqC5tYHwOiDhrkPnzZAGcIF uF8l1XPkUTN09wS2Ao6qtouUkXTUqoV0MP752qQhF/+8RZEyU1ZGoZD1cu7hXWpbAHsh uu9kfwdakBYpFOphaTeBExi4g7FNIlKgd21AbHh9tCPv/LfqW1dJ2eNfDj/PAGvnzsUC /WmAq6CuzQiDcZTgMSMmz6u/L6MsOqd+jTvaPNwCrrZxsQaqDhUh1o+qq6m4xW71ac2s ZjTg== X-Gm-Message-State: AOJu0YzvSWT3ya94YGVRo//AlOxTUIajPppp7U7xaxsGSVVcK0M8umWv 8HWZmf8FcXudulzhfWOeIFU= X-Google-Smtp-Source: AGHT+IEezGGRhbK03ax3JNIzFoLUpg5F1k0ZqeF9wak/y1NjlOW2XuFsrwhecuU/W1o0P5dcQkrwLQ== X-Received: by 2002:a17:903:1d0:b0:1c5:bea4:8537 with SMTP id e16-20020a17090301d000b001c5bea48537mr12692272plh.15.1696687401584; Sat, 07 Oct 2023 07:03:21 -0700 (PDT) Received: from vultr.guest ([45.77.191.53]) by smtp.gmail.com with ESMTPSA id l13-20020a170902f68d00b001c0a414695dsm5897550plg.62.2023.10.07.07.03.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 07:03:21 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 4/8] selftests/bpf: Fix issues in setup_classid_environment() Date: Sat, 7 Oct 2023 14:03:00 +0000 Message-Id: <20231007140304.4390-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231007140304.4390-1-laoar.shao@gmail.com> References: <20231007140304.4390-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC If the net_cls subsystem is already mounted, attempting to mount it again in setup_classid_environment() will result in a failure with the error code EBUSY. Despite this, tmpfs will have been successfully mounted at /sys/fs/cgroup/net_cls. Consequently, the /sys/fs/cgroup/net_cls directory will be empty, causing subsequent setup operations to fail. Here's an error log excerpt illustrating the issue when net_cls has already been mounted at /sys/fs/cgroup/net_cls prior to running setup_classid_environment(): - Before that change $ tools/testing/selftests/bpf/test_progs --name=cgroup_v1v2 test_cgroup_v1v2:PASS:server_fd 0 nsec test_cgroup_v1v2:PASS:client_fd 0 nsec test_cgroup_v1v2:PASS:cgroup_fd 0 nsec test_cgroup_v1v2:PASS:server_fd 0 nsec run_test:PASS:skel_open 0 nsec run_test:PASS:prog_attach 0 nsec test_cgroup_v1v2:PASS:cgroup-v2-only 0 nsec (cgroup_helpers.c:248: errno: No such file or directory) Opening Cgroup Procs: /sys/fs/cgroup/net_cls/cgroup.procs (cgroup_helpers.c:540: errno: No such file or directory) Opening cgroup classid: /sys/fs/cgroup/net_cls/cgroup-test-work-dir/net_cls.classid run_test:PASS:skel_open 0 nsec run_test:PASS:prog_attach 0 nsec (cgroup_helpers.c:248: errno: No such file or directory) Opening Cgroup Procs: /sys/fs/cgroup/net_cls/cgroup-test-work-dir/cgroup.procs run_test:FAIL:join_classid unexpected error: 1 (errno 2) test_cgroup_v1v2:FAIL:cgroup-v1v2 unexpected error: -1 (errno 2) (cgroup_helpers.c:248: errno: No such file or directory) Opening Cgroup Procs: /sys/fs/cgroup/net_cls/cgroup.procs #44 cgroup_v1v2:FAIL Summary: 0/0 PASSED, 0 SKIPPED, 1 FAILED - After that change $ tools/testing/selftests/bpf/test_progs --name=cgroup_v1v2 #44 cgroup_v1v2:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yafang Shao --- tools/testing/selftests/bpf/cgroup_helpers.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index 24ba56d42f2d..9c36d1db9f94 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -518,10 +518,20 @@ int setup_classid_environment(void) return 1; } - if (mount("net_cls", NETCLS_MOUNT_PATH, "cgroup", 0, "net_cls") && - errno != EBUSY) { - log_err("mount cgroup net_cls"); - return 1; + if (mount("net_cls", NETCLS_MOUNT_PATH, "cgroup", 0, "net_cls")) { + if (errno != EBUSY) { + log_err("mount cgroup net_cls"); + return 1; + } + + if (rmdir(NETCLS_MOUNT_PATH)) { + log_err("rmdir cgroup net_cls"); + return 1; + } + if (umount(CGROUP_MOUNT_DFLT)) { + log_err("umount cgroup base"); + return 1; + } } cleanup_classid_environment(); From patchwork Sat Oct 7 14:03:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13412359 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E522127ED3 for ; Sat, 7 Oct 2023 14:03:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="FwSZbahf" Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 099D2BD; Sat, 7 Oct 2023 07:03:24 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1c5c91bec75so23168595ad.3; Sat, 07 Oct 2023 07:03:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696687403; x=1697292203; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=STXdhtnmTI3etV8zPaY3R9oC/tPAoDIEDC13dA2eLMM=; b=FwSZbahfWgi2/baWR7klHWtCrdDGwmkX4RmxB1y7tD7wn5+Bkq+Y7jgCw0WjEvD+xd e6rQXD/Day6GlBv9zry/8Nl5Vn3eny8nfrCKrq2FxUMnDDF9m2wZ9zoqQWCa7uXBXYk1 xjLaxvfJiU88jpXHWHarBscV++DDBLexkn8fF/IHnFgaIcwBh77dAr+qeQ+4z22cpeTu gDk7kKBYvh63pEsJG6NUL0DO+CZ0Ajiu5tPWHR7Qp9fyDoVucjKLSTmnl0higF3EgeTq yMDiTN8uXv/V+nSa+Ek8pcvS6YK4Y2+iurSFu9sJCbMUUoklLaP+f3aL7+0xqlRLF1qe wbgA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696687403; x=1697292203; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=STXdhtnmTI3etV8zPaY3R9oC/tPAoDIEDC13dA2eLMM=; b=IYXT2CIoa/G9xlEhFaWSyQCQV5kfj40WNZs0G0ArpKvuUqQ0QZM4TW8YXVaFeNzsmk mx1W1XimZxArvb7XVu8SDvY6s5sXWCb8+5LBHfCygBtMFEIS7dtB4ydiDmyVaTO/ErUS hOzUguHEgCT6XSuTVVOT6Ndb8kz+kH48pLcVIiedUxpQ9X8EiXpzWIC2bGhe+GVuNCEI XHQ/owpTMErmB6/qrSWT+g4W5/xUvz5dtdqmhX7fEBksIBsYQOIJjU41YV3VPk0+s80o rEDjFizOS/PEMB7VtjL+mogb59Xe9gW7+Wsk/L4E3M5wuQx7YI7VdCfGHCiHAubBo8Ex +e/Q== X-Gm-Message-State: AOJu0Yx0X6rHj5RtMK3Luggx+nG0oGC6ou01FDaN7YRNe4BAR1Wc2d+Z QAbgTJizTkj0k3NhhYtSkZM= X-Google-Smtp-Source: AGHT+IHtvkrd6iYtNGvkTLbptz6laKnr44PN3Ek6NbIjaWkMapFuSct5onQ0NQ6wq5211TZ8p4QYVQ== X-Received: by 2002:a17:902:be03:b0:1c4:c5d:d7fa with SMTP id r3-20020a170902be0300b001c40c5dd7famr9316704pls.45.1696687403452; Sat, 07 Oct 2023 07:03:23 -0700 (PDT) Received: from vultr.guest ([45.77.191.53]) by smtp.gmail.com with ESMTPSA id l13-20020a170902f68d00b001c0a414695dsm5897550plg.62.2023.10.07.07.03.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 07:03:22 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 5/8] selftests/bpf: Add parallel support for classid Date: Sat, 7 Oct 2023 14:03:01 +0000 Message-Id: <20231007140304.4390-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231007140304.4390-1-laoar.shao@gmail.com> References: <20231007140304.4390-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Include the current pid in the classid cgroup path. This way, different testers relying on classid-based configurations will have distinct classid cgroup directories, enabling them to run concurrently. Additionally, we leverage the current pid as the classid, ensuring unique identification. Signed-off-by: Yafang Shao --- tools/testing/selftests/bpf/cgroup_helpers.c | 19 ++++++++++++------- tools/testing/selftests/bpf/cgroup_helpers.h | 2 +- .../selftests/bpf/prog_tests/cgroup_v1v2.c | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index 9c36d1db9f94..e378fa057757 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -45,9 +45,12 @@ #define format_parent_cgroup_path(buf, path) \ format_cgroup_path_pid(buf, path, getppid()) -#define format_classid_path(buf) \ - snprintf(buf, sizeof(buf), "%s%s", NETCLS_MOUNT_PATH, \ - CGROUP_WORK_DIR) +#define format_classid_path_pid(buf, pid) \ + snprintf(buf, sizeof(buf), "%s%s%d", NETCLS_MOUNT_PATH, \ + CGROUP_WORK_DIR, pid) + +#define format_classid_path(buf) \ + format_classid_path_pid(buf, getpid()) static __thread bool cgroup_workdir_mounted; @@ -546,15 +549,17 @@ int setup_classid_environment(void) /** * set_classid() - Set a cgroupv1 net_cls classid - * @id: the numeric classid * - * Writes the passed classid into the cgroup work dir's net_cls.classid + * Writes the classid into the cgroup work dir's net_cls.classid * file in order to later on trigger socket tagging. * + * To make sure different classid testers have different classids, we use + * the current pid as the classid by default. + * * On success, it returns 0, otherwise on failure it returns 1. If there * is a failure, it prints the error to stderr. */ -int set_classid(unsigned int id) +int set_classid(void) { char cgroup_workdir[PATH_MAX - 42]; char cgroup_classid_path[PATH_MAX + 1]; @@ -570,7 +575,7 @@ int set_classid(unsigned int id) return 1; } - if (dprintf(fd, "%u\n", id) < 0) { + if (dprintf(fd, "%u\n", getpid()) < 0) { log_err("Setting cgroup classid"); rc = 1; } diff --git a/tools/testing/selftests/bpf/cgroup_helpers.h b/tools/testing/selftests/bpf/cgroup_helpers.h index 5c2cb9c8b546..92fc41daf4a4 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.h +++ b/tools/testing/selftests/bpf/cgroup_helpers.h @@ -29,7 +29,7 @@ int setup_cgroup_environment(void); void cleanup_cgroup_environment(void); /* cgroupv1 related */ -int set_classid(unsigned int id); +int set_classid(void); int join_classid(void); int setup_classid_environment(void); diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c index 9026b42914d3..addf720428f7 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c @@ -71,7 +71,7 @@ void test_cgroup_v1v2(void) } ASSERT_OK(run_test(cgroup_fd, server_fd, false), "cgroup-v2-only"); setup_classid_environment(); - set_classid(42); + set_classid(); ASSERT_OK(run_test(cgroup_fd, server_fd, true), "cgroup-v1v2"); cleanup_classid_environment(); close(server_fd); From patchwork Sat Oct 7 14:03:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13412360 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7739426E00 for ; Sat, 7 Oct 2023 14:03:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TfAZfiWW" Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A9D40CF; Sat, 7 Oct 2023 07:03:25 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id d9443c01a7336-1c5ff5f858dso23182505ad.2; Sat, 07 Oct 2023 07:03:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696687405; x=1697292205; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Fo3tjZPZMMIDp299nbsdxVbUIehDi8P0BDIlhn8r2cE=; b=TfAZfiWWKHBmQY6e6w8KLDIVEZHt8uYIsJN9P4scNiV8mFWYdSWSG3BYlt5qJo+1/h m7SIW3VNnICg4cfWUdgLco/ivXOhdA+o4XOXNLQElZSfID1STqhi0WFlN29SawCKaOE9 clTtHdk+FqrOdi2oEaeEWzQiezc428Qj53n+xge81jvYjkhX0cfj+HC0MTI+tRWokw7v sCHgKTzjAlEag6j1d+xXeaEfNxBC/qY/fHZoj0qfctpBzt5n7KuIsiif5sApXturvYzN MTlG6/aDadWAOsKSZf/fdGFxMIRYhc4O7t37kgKhQwFAHILOtV926tOi1VhSn3UZtW8P Bkxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696687405; x=1697292205; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Fo3tjZPZMMIDp299nbsdxVbUIehDi8P0BDIlhn8r2cE=; b=faKa/msagCEcrGmskGAZ8GD1IGpr4HBIbvYP2yMSBGrHXSTL+Grh7XHHZSmD7wBik8 37tV9wbQ5KDBeCg5ac+B1WCUbvwN+BqNwsfsV5cSKk3BU9i8eaA4LJG4BUyIqdsIownE AaVwxoGLE886bZOryoElmzEupM0OnsQGsv1Bddz/aPqdJQuvcdaaUWrxwlTRe1qNwQH4 AJYLF+CEe6AOt5j/zC+K99QKmXit3VIRhlAIwaQ3IafHHUY7hUMCWmiU/HCD7wf2MYDr 7HHVgPDSgAcdafJvBJHGwVUzdZ+/qqUmmaMfpT51eCznMtaHmSy/P647ZKn5TGyzamwx 3h3Q== X-Gm-Message-State: AOJu0YxaEAnW1krS2zpnbSoMjakYkZ2UWgyhISshfmZaAfT/jda2Us5K 2ooccLYS5Pssgul2Cje/o1U= X-Google-Smtp-Source: AGHT+IF8+lxyYtEXmACREetEG767x7KHyYq5G7BDTGM2JGM4lrA2gisSMki2ek3tbEFsDs1D4r3ERw== X-Received: by 2002:a17:903:32cb:b0:1c5:ea60:85c5 with SMTP id i11-20020a17090332cb00b001c5ea6085c5mr12000997plr.2.1696687405076; Sat, 07 Oct 2023 07:03:25 -0700 (PDT) Received: from vultr.guest ([45.77.191.53]) by smtp.gmail.com with ESMTPSA id l13-20020a170902f68d00b001c0a414695dsm5897550plg.62.2023.10.07.07.03.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 07:03:24 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 6/8] selftests/bpf: Add a new cgroup helper get_classid_cgroup_id() Date: Sat, 7 Oct 2023 14:03:02 +0000 Message-Id: <20231007140304.4390-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231007140304.4390-1-laoar.shao@gmail.com> References: <20231007140304.4390-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Introduce a new helper function to retrieve the cgroup ID from a net_cls cgroup directory. Signed-off-by: Yafang Shao --- tools/testing/selftests/bpf/cgroup_helpers.c | 28 +++++++++++++++----- tools/testing/selftests/bpf/cgroup_helpers.h | 1 + 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index e378fa057757..7cb2c9597b8f 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -417,26 +417,23 @@ int create_and_get_cgroup(const char *relative_path) } /** - * get_cgroup_id() - Get cgroup id for a particular cgroup path - * @relative_path: The cgroup path, relative to the workdir, to join + * get_cgroup_id_from_path - Get cgroup id for a particular cgroup path + * @cgroup_workdir: The absolute cgroup path * * On success, it returns the cgroup id. On failure it returns 0, * which is an invalid cgroup id. * If there is a failure, it prints the error to stderr. */ -unsigned long long get_cgroup_id(const char *relative_path) +unsigned long long get_cgroup_id_from_path(const char *cgroup_workdir) { int dirfd, err, flags, mount_id, fhsize; union { unsigned long long cgid; unsigned char raw_bytes[8]; } id; - char cgroup_workdir[PATH_MAX + 1]; struct file_handle *fhp, *fhp2; unsigned long long ret = 0; - format_cgroup_path(cgroup_workdir, relative_path); - dirfd = AT_FDCWD; flags = 0; fhsize = sizeof(*fhp); @@ -472,6 +469,14 @@ unsigned long long get_cgroup_id(const char *relative_path) return ret; } +unsigned long long get_cgroup_id(const char *relative_path) +{ + char cgroup_workdir[PATH_MAX + 1]; + + format_cgroup_path(cgroup_workdir, relative_path); + return get_cgroup_id_from_path(cgroup_workdir); +} + int cgroup_setup_and_join(const char *path) { int cg_fd; @@ -617,3 +622,14 @@ void cleanup_classid_environment(void) join_cgroup_from_top(NETCLS_MOUNT_PATH); nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT); } + +/** + * get_classid_cgroup_id - Get the cgroup id of a net_cls cgroup + */ +unsigned long long get_classid_cgroup_id(void) +{ + char cgroup_workdir[PATH_MAX + 1]; + + format_classid_path(cgroup_workdir); + return get_cgroup_id_from_path(cgroup_workdir); +} diff --git a/tools/testing/selftests/bpf/cgroup_helpers.h b/tools/testing/selftests/bpf/cgroup_helpers.h index 92fc41daf4a4..e71da4ef031b 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.h +++ b/tools/testing/selftests/bpf/cgroup_helpers.h @@ -31,6 +31,7 @@ void cleanup_cgroup_environment(void); /* cgroupv1 related */ int set_classid(void); int join_classid(void); +unsigned long long get_classid_cgroup_id(void); int setup_classid_environment(void); void cleanup_classid_environment(void); From patchwork Sat Oct 7 14:03:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13412361 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5BCE827EE2 for ; Sat, 7 Oct 2023 14:03:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="j93ymMmA" Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CBBFCA; Sat, 7 Oct 2023 07:03:27 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1c8a1541233so1359915ad.1; Sat, 07 Oct 2023 07:03:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696687407; x=1697292207; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=p5sPylPqdXFTIzAaQ9WSlcqSNKIac6K+arZml60zhvo=; b=j93ymMmASoRSY5yq2xvcf7s4gov03A3Kt4xN3hnnkGokp4SpvXuJTrB4yilm6M+lfP vaTfP0txULmc7793RzbMUj3kRy3oRWoBeQoNWPi08IwpygmSqVNk2jikZnn1/aqzNKip fwN7awa0Kjjnsjv4Y5S9NyT3bJfmqERt38vS5NaXzpu8O/0RUklfGNaSveI7PUIfSODr 4EoRoPQjsxusqQYEigEBkyb0tmSfMCchJUCPtU2svdjXR3VB+N3Lds15pVCehQKcr/kk 2k245TqsluoYRpBmy/l0Hx6KFG9ecYIJN8rtMCUNnmKsL2AjkarXOE8n9GvyrGo0EVyn nOVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696687407; x=1697292207; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=p5sPylPqdXFTIzAaQ9WSlcqSNKIac6K+arZml60zhvo=; b=Rt2r7Cx4ESRNsLYw4CQrQQmN2Mqv7seAyKph/36/uJfZ6w9/x+S8fHfHZuHfpNx91d dJQHHL69xdoG8EGIVUVKBMl8bXGmp2e3+yi8ZHrjs5nXIWOCQq5JaDd/YCZ+cxq/RkWd jYfz3XV3prD/s05DszEszkoHU3aOIv7JbmQffnaWsbI9oCQD9ZDgT7LGKI2cYPyoBL6/ 1Evcuq+6dt2W4sB0FZKj7rstx0tHzyn2GRgSh6g2RjYdo/uCkfbtdFI/2YMHaxJtgYDf w9D47SJ2gOLfOniVfQViwGh3u3tu5gS8oP3xmjU1Wn36RbINqsjjvAEcJCmjMc1iBYXV nFkg== X-Gm-Message-State: AOJu0YzfNXRkA+qb7syv0U71s0gLDXh+aWTE9aEiNZ0TUxuyUjV8GBM3 OcpZtaSZykhNKSpnIur16rE= X-Google-Smtp-Source: AGHT+IHeAl1Oob/Tzh52pWVkd6NU0MNoUXvXhk50P5AWi2y7lkZfZzrCUBCGLpu4cy1BiUy8Y4NBvw== X-Received: by 2002:a17:902:6f02:b0:1b8:94e9:e7b0 with SMTP id w2-20020a1709026f0200b001b894e9e7b0mr9299064plk.9.1696687406812; Sat, 07 Oct 2023 07:03:26 -0700 (PDT) Received: from vultr.guest ([45.77.191.53]) by smtp.gmail.com with ESMTPSA id l13-20020a170902f68d00b001c0a414695dsm5897550plg.62.2023.10.07.07.03.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 07:03:26 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 7/8] selftests/bpf: Add a new cgroup helper get_cgroup_hierarchy_id() Date: Sat, 7 Oct 2023 14:03:03 +0000 Message-Id: <20231007140304.4390-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231007140304.4390-1-laoar.shao@gmail.com> References: <20231007140304.4390-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC A new cgroup helper function, get_cgroup1_hierarchy_id(), has been introduced to obtain the ID of a cgroup1 hierarchy based on the provided cgroup name. This cgroup name can be obtained from the /proc/self/cgroup file. Signed-off-by: Yafang Shao --- tools/testing/selftests/bpf/cgroup_helpers.c | 49 ++++++++++++++++++++ tools/testing/selftests/bpf/cgroup_helpers.h | 1 + 2 files changed, 50 insertions(+) diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index 7cb2c9597b8f..5cb66fb3d4fe 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -633,3 +633,52 @@ unsigned long long get_classid_cgroup_id(void) format_classid_path(cgroup_workdir); return get_cgroup_id_from_path(cgroup_workdir); } + +/** + * get_cgroup1_hierarchy_id - Retrieves the ID of a cgroup1 hierarchy from the cgroup1 name + * @cgrp_name: The cgroup1 name, which can be retrieved from /proc/self/cgroup. + */ +int get_cgroup1_hierarchy_id(const char *cgrp_name) +{ + char *c, *c2, *c3, *c4; + bool found = false; + char line[1024]; + FILE *file; + int i, id; + + if (!cgrp_name) + return -1; + + file = fopen("/proc/self/cgroup", "r"); + if (!file) { + log_err("fopen /proc/self/cgroup"); + return -1; + } + + while (fgets(line, 1024, file)) { + i = 0; + for (c = strtok_r(line, ":", &c2); c && i < 2; c = strtok_r(NULL, ":", &c2)) { + if (i == 0) { + id = strtol(c, NULL, 10); + } else if (i == 1) { + if (!strcmp(c, cgrp_name)) { + found = true; + break; + } + + /* Multiple subsystems may share one single mount point */ + for (c3 = strtok_r(c, ",", &c4); c3; + c3 = strtok_r(NULL, ",", &c4)) { + if (!strcmp(c, cgrp_name)) { + found = true; + break; + } + } + } + i++; + } + if (found) + break; + } + return found ? id : -1; +} diff --git a/tools/testing/selftests/bpf/cgroup_helpers.h b/tools/testing/selftests/bpf/cgroup_helpers.h index e71da4ef031b..a80c41734a26 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.h +++ b/tools/testing/selftests/bpf/cgroup_helpers.h @@ -20,6 +20,7 @@ int get_root_cgroup(void); int create_and_get_cgroup(const char *relative_path); void remove_cgroup(const char *relative_path); unsigned long long get_cgroup_id(const char *relative_path); +int get_cgroup1_hierarchy_id(const char *cgrp_name); int join_cgroup(const char *relative_path); int join_root_cgroup(void); From patchwork Sat Oct 7 14:03:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13412362 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF84126E00 for ; Sat, 7 Oct 2023 14:03:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="I6XUzClO" Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0BA64B6; Sat, 7 Oct 2023 07:03:29 -0700 (PDT) Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1c61acd1285so21559925ad.2; Sat, 07 Oct 2023 07:03:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696687408; x=1697292208; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KOZHmpDxZlsRaziqYq5XhmSN+iTkRWEd1l7APG5ymMQ=; b=I6XUzClOpVXM31rwCuTKagSS6RTgvHtNpn7o+ZV6RSpJjeuVkoUh4XIAVhIR78qBAU ZLdAQAUIj8rxlPmWLXwvogf+aV8f8yXatlCiYKGeU08jAgj7eOqJ9ihFMVC2Wo7lhEKG hWz0jT8NXcAXZKYpXp66QvjasEcClj2LziX0QKVapcnZ7wgOeEvwEpHL72E0Wbpn3I8Q 3XBhGJWoGHck4O60m082MfBcCtq87c3xcLydwUArsWhi8FJFgprI1VjPfn7feAO3mcYr WmcFnWsoTsVszWxdGAjpXWovU4cWXT4eYXMPv9u1L8NHVC6eNvN4TXBFtMDZlGVdZcwq Qw9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696687408; x=1697292208; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KOZHmpDxZlsRaziqYq5XhmSN+iTkRWEd1l7APG5ymMQ=; b=nhFwC1SbEPujKcz1ttSyxDLLK46cumf2Is5Eqff7PTFxhjtWwaGbJYX/6M2kvLSTnS nLwzJqN9oN2yBHUs2hBe5Omf+IEfmeFgPslc8Imc35uBlpVUPOV9eLnSlJw5wzbRBr4h Q3gT0YozCc2TvbAbGL51W+1LKHx1W6qZL4FXz3nhseCe80AD5HhxHa670NHzTSJbP4Op y39GxynW5kJdjHbkTD6kUroHNk/ijHrinuAm+IHMa595YrC+GbLdTsvQabYu0nbCX8vQ k10w14PjFdNPc42FrIXFMIRvccQDZxksBgorUSGfORzEXtqP6xn7c7IyeOPEsiIm6pEv 6NOw== X-Gm-Message-State: AOJu0YwRuK9J05awm4bYYQGzqxmxx3asyya0vht/Vns9a9JPmaW04SnD 9yUClWPgLxElzIIRND3kshc= X-Google-Smtp-Source: AGHT+IFgu5qJyLsOlq9OtiabwHdiLMmH4cC8/KW7dHKWID3Fv5Yi+2/V0cRoFTGicmCzb3ZJdG4K4g== X-Received: by 2002:a17:902:e5cb:b0:1c8:852f:241a with SMTP id u11-20020a170902e5cb00b001c8852f241amr7974552plf.40.1696687408481; Sat, 07 Oct 2023 07:03:28 -0700 (PDT) Received: from vultr.guest ([45.77.191.53]) by smtp.gmail.com with ESMTPSA id l13-20020a170902f68d00b001c0a414695dsm5897550plg.62.2023.10.07.07.03.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Oct 2023 07:03:28 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 8/8] selftests/bpf: Add selftests for cgroup1 hierarchy Date: Sat, 7 Oct 2023 14:03:04 +0000 Message-Id: <20231007140304.4390-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231007140304.4390-1-laoar.shao@gmail.com> References: <20231007140304.4390-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC Add selftests for cgroup1 hierarchy. The result as follows, $ tools/testing/selftests/bpf/test_progs --name=cgroup1_hierarchy #36/1 cgroup1_hierarchy/test_cgroup1_hierarchy:OK #36/2 cgroup1_hierarchy/test_root_cgid:OK #36/3 cgroup1_hierarchy/test_invalid_level:OK #36/4 cgroup1_hierarchy/test_invalid_cgid:OK #36/5 cgroup1_hierarchy/test_invalid_hid:OK #36/6 cgroup1_hierarchy/test_invalid_cgrp_name:OK #36/7 cgroup1_hierarchy/test_invalid_cgrp_name2:OK #36/8 cgroup1_hierarchy/test_sleepable_prog:OK #36 cgroup1_hierarchy:OK Summary: 1/8 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yafang Shao --- .../bpf/prog_tests/cgroup1_hierarchy.c | 159 ++++++++++++++++++ .../bpf/progs/test_cgroup1_hierarchy.c | 62 +++++++ 2 files changed, 221 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c create mode 100644 tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c b/tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c new file mode 100644 index 000000000000..4aafbc921254 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c @@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2023 Yafang Shao */ + +#include +#include +#include +#include "cgroup_helpers.h" +#include "test_cgroup1_hierarchy.skel.h" + +static void bpf_cgroup1(struct test_cgroup1_hierarchy *skel) +{ + int err; + + /* Attach LSM prog first */ + skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run); + if (!ASSERT_OK_PTR(skel->links.lsm_run, "lsm_attach")) + return; + + /* LSM prog will be triggered when attaching fentry */ + skel->links.fentry_run = bpf_program__attach_trace(skel->progs.fentry_run); + ASSERT_NULL(skel->links.fentry_run, "fentry_attach_fail"); + + err = bpf_link__destroy(skel->links.lsm_run); + ASSERT_OK(err, "destroy_lsm"); + skel->links.lsm_run = NULL; +} + +static void bpf_cgroup1_sleepable(struct test_cgroup1_hierarchy *skel) +{ + int err; + + /* Attach LSM prog first */ + skel->links.lsm_s_run = bpf_program__attach_lsm(skel->progs.lsm_s_run); + if (!ASSERT_OK_PTR(skel->links.lsm_s_run, "lsm_attach")) + return; + + /* LSM prog will be triggered when attaching fentry */ + skel->links.fentry_run = bpf_program__attach_trace(skel->progs.fentry_run); + ASSERT_NULL(skel->links.fentry_run, "fentry_attach_fail"); + + err = bpf_link__destroy(skel->links.lsm_s_run); + ASSERT_OK(err, "destroy_lsm"); + skel->links.lsm_s_run = NULL; +} + +static void bpf_cgroup1_invalid_id(struct test_cgroup1_hierarchy *skel) +{ + int err; + + /* Attach LSM prog first */ + skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run); + if (!ASSERT_OK_PTR(skel->links.lsm_run, "lsm_attach")) + return; + + /* LSM prog will be triggered when attaching fentry */ + skel->links.fentry_run = bpf_program__attach_trace(skel->progs.fentry_run); + if (!ASSERT_OK_PTR(skel->links.fentry_run, "fentry_attach_success")) + goto cleanup; + + err = bpf_link__destroy(skel->links.lsm_run); + ASSERT_OK(err, "destroy_lsm"); + skel->links.lsm_run = NULL; + +cleanup: + err = bpf_link__destroy(skel->links.fentry_run); + ASSERT_OK(err, "destroy_fentry"); + skel->links.fentry_run = NULL; +} + +void test_cgroup1_hierarchy(void) +{ + struct test_cgroup1_hierarchy *skel; + __u64 current_cgid; + int hid, err; + + skel = test_cgroup1_hierarchy__open(); + if (!ASSERT_OK_PTR(skel, "open")) + return; + + skel->bss->target_pid = getpid(); + + err = bpf_program__set_attach_target(skel->progs.fentry_run, 0, "bpf_fentry_test1"); + if (!ASSERT_OK(err, "fentry_set_target")) + goto destroy; + + err = test_cgroup1_hierarchy__load(skel); + if (!ASSERT_OK(err, "load")) + goto destroy; + + /* Setup cgroup1 hierarchy */ + err = setup_classid_environment(); + if (!ASSERT_OK(err, "setup_classid_environment")) + goto destroy; + + err = join_classid(); + if (!ASSERT_OK(err, "join_cgroup1")) + goto cleanup; + + current_cgid = get_classid_cgroup_id(); + if (!ASSERT_GE(current_cgid, 0, "cgroup1 id")) + goto cleanup; + + hid = get_cgroup1_hierarchy_id("net_cls"); + if (!ASSERT_GE(hid, 0, "cgroup1 id")) + goto cleanup; + skel->bss->target_hid = hid; + + if (test__start_subtest("test_cgroup1_hierarchy")) { + skel->bss->target_ancestor_cgid = current_cgid; + bpf_cgroup1(skel); + } + + if (test__start_subtest("test_root_cgid")) { + skel->bss->target_ancestor_cgid = 1; + skel->bss->target_ancestor_level = 0; + bpf_cgroup1(skel); + } + + if (test__start_subtest("test_invalid_level")) { + skel->bss->target_ancestor_cgid = 1; + skel->bss->target_ancestor_level = 1; + bpf_cgroup1_invalid_id(skel); + } + + if (test__start_subtest("test_invalid_cgid")) { + skel->bss->target_ancestor_cgid = 0; + bpf_cgroup1_invalid_id(skel); + } + + if (test__start_subtest("test_invalid_hid")) { + skel->bss->target_ancestor_cgid = 1; + skel->bss->target_ancestor_level = 0; + skel->bss->target_hid = -1; + bpf_cgroup1_invalid_id(skel); + } + + if (test__start_subtest("test_invalid_cgrp_name")) { + skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cl"); + skel->bss->target_ancestor_cgid = current_cgid; + bpf_cgroup1_invalid_id(skel); + } + + if (test__start_subtest("test_invalid_cgrp_name2")) { + skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cls,"); + skel->bss->target_ancestor_cgid = current_cgid; + bpf_cgroup1_invalid_id(skel); + } + + if (test__start_subtest("test_sleepable_prog")) { + skel->bss->target_hid = hid; + skel->bss->target_ancestor_cgid = current_cgid; + bpf_cgroup1_sleepable(skel); + } + +cleanup: + cleanup_classid_environment(); +destroy: + test_cgroup1_hierarchy__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c b/tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c new file mode 100644 index 000000000000..95d9031892e4 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 +//#endif +/* Copyright (C) 2023 Yafang Shao */ + +#include "vmlinux.h" +#include +#include +#include + +__u32 target_ancestor_level; +__u64 target_ancestor_cgid; +int target_pid, target_hid; + +u64 bpf_task_cgroup1_id_within_hierarchy(struct task_struct *task, int hierarchy_id) __ksym; +u64 bpf_task_ancestor_cgroup1_id_within_hierarchy(struct task_struct *task, int hierarchy_id, + int ancestor_level) __ksym; + +static int bpf_link_create_verify(int cmd) +{ + __u64 cgid, ancestor_cgid; + struct task_struct *task; + int ret = 0; + + if (cmd != BPF_LINK_CREATE) + return 0; + + task = bpf_get_current_task_btf(); + /* Then it can run in parallel */ + if (task->pid != target_pid) + return 0; + + /* Refuse it if its cgid or its ancestor's cgid is the target cgid */ + cgid = bpf_task_cgroup1_id_within_hierarchy(task, target_hid); + if (cgid == target_ancestor_cgid) + ret = -1; + + ancestor_cgid = bpf_task_ancestor_cgroup1_id_within_hierarchy(task, target_hid, + target_ancestor_level); + if (ancestor_cgid == target_ancestor_cgid) + ret = -1; + return ret; +} + +SEC("lsm/bpf") +int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size) +{ + return bpf_link_create_verify(cmd); +} + +SEC("lsm.s/bpf") +int BPF_PROG(lsm_s_run, int cmd, union bpf_attr *attr, unsigned int size) +{ + return bpf_link_create_verify(cmd); +} + +SEC("fentry") +int BPF_PROG(fentry_run) +{ + return 0; +} + +char _license[] SEC("license") = "GPL";