From patchwork Tue Oct 31 05:04:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuyi Zhou X-Patchwork-Id: 13441048 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 BAB286FD8 for ; Tue, 31 Oct 2023 05:04:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="VX7M9y5J" Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2B85C9 for ; Mon, 30 Oct 2023 22:04:49 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id 41be03b00d2f7-5b92b852390so4009831a12.2 for ; Mon, 30 Oct 2023 22:04:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1698728689; x=1699333489; 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=A7JCp9A8T8BQUKcY2EItpe9D/Y+wTEq+ZM8JBxOMT8M=; b=VX7M9y5JwXuenVkBuVuomVgyNoj07Bv0LgASVAKS5DNiN2MTSSlSIVkLP8e1bxMG5Q +LGON9mGdGO25BR+vbKJmZ+1F8Q9l/LspFrFlOrFsLpWe4Kc9k19lfMHs/y0N/djQdIc wQ+HWHg4L2ROz3EqLSxqplTELzNmhEwZj+9l7XzUZaMoRXdi/T3bNZdV3GTR7oYCBTEe EdLqy0bKlqNueyzneBu1+2b3Xl/pI3S/dZpE15loh/WX9Ds4lW7lVGwyVDB8h3WzN9Gw eQ8VtyxmzhjvElOa7rnqptHu5+9sw/H4QWE9DwjycrpDyrld8ZNvDt4s0EkDXh3YQgH1 RLMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698728689; x=1699333489; 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=A7JCp9A8T8BQUKcY2EItpe9D/Y+wTEq+ZM8JBxOMT8M=; b=AD4CAFj9NXnLhOp+SA3X2B8LOuK2p1BnlOtr4Bn9otpA0QvMPf5Z9l0rCg3qoGB0Vj sd19mgOd2oQ8eu3a1OsdjVivmebfE3ZB/PInnxtplCr99E1AANO+4FEsAD0CPAhAuGFZ STZJ7CHRgqTyD4Im9M77LrUF+We05ZBlqIJ2h9ZT3+K295LSHl4y80Zf1trc2wovkB7g lFjk6ow6xwrsmV0APY58j78lZipqjA1kDNqrcd8i0npRNWj7YTCLKlLQHz5SJL/+B4jO RnlMJrtUpLD4nvP2ZgR4oOTi+d5IHLw245ObymPrqBet8CbZmC0Mhjgu5Fg/pLkIlbfk maqg== X-Gm-Message-State: AOJu0YwnbCQO4MiUj4IXrIFIfXCI3wv+Jeu973jGSbvjCYnp799rR24X u5yxZoV3gXNFZKUFKDfwTsCdq5eFcTtGbVLtaXk= X-Google-Smtp-Source: AGHT+IG12fyhSHeGL4lKMTptdYtuwjedHinO3nP7jAwjRtZzq3eDxHpf5XQqbyd+6TqmhlSA3whppA== X-Received: by 2002:a05:6a21:6d9c:b0:162:4f45:b415 with SMTP id wl28-20020a056a216d9c00b001624f45b415mr15906260pzb.51.1698728689089; Mon, 30 Oct 2023 22:04:49 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.142]) by smtp.gmail.com with ESMTPSA id 21-20020a17090a195500b0027ce34334f5sm350951pjh.37.2023.10.30.22.04.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 22:04:48 -0700 (PDT) From: Chuyi Zhou To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, Chuyi Zhou , Yonghong Song Subject: [PATCH bpf-next v4 1/3] bpf: Relax allowlist for css_task iter Date: Tue, 31 Oct 2023 13:04:36 +0800 Message-Id: <20231031050438.93297-2-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20231031050438.93297-1-zhouchuyi@bytedance.com> References: <20231031050438.93297-1-zhouchuyi@bytedance.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net The newly added open-coded css_task iter would try to hold the global css_set_lock in bpf_iter_css_task_new, so the bpf side has to be careful in where it allows to use this iter. The mainly concern is dead locking on css_set_lock. check_css_task_iter_allowlist() in verifier enforced css_task can only be used in bpf_lsm hooks and sleepable bpf_iter. This patch relax the allowlist for css_task iter. Any lsm and any iter (even non-sleepable) and any sleepable are safe since they would not hold the css_set_lock before entering BPF progs context. This patch also fixes the misused BPF_TRACE_ITER in check_css_task_iter_allowlist which compared bpf_prog_type with bpf_attach_type. Fixes: 9c66dc94b62ae ("bpf: Introduce css_task open-coded iterator kfuncs") Signed-off-by: Chuyi Zhou Acked-by: Yonghong Song --- kernel/bpf/verifier.c | 16 ++++++++++++---- .../selftests/bpf/progs/iters_task_failure.c | 4 ++-- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 857d76694517..b2ddecfdff9d 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -11394,6 +11394,12 @@ static int process_kf_arg_ptr_to_rbtree_node(struct bpf_verifier_env *env, &meta->arg_rbtree_root.field); } +/* + * css_task iter allowlist is needed to avoid dead locking on css_set_lock. + * LSM hooks and iters (both sleepable and non-sleepable) are safe. + * Any sleepable progs are also safe since bpf_check_attach_target() enforce + * them can only be attached to some specific hook points. + */ static bool check_css_task_iter_allowlist(struct bpf_verifier_env *env) { enum bpf_prog_type prog_type = resolve_prog_type(env->prog); @@ -11401,10 +11407,12 @@ static bool check_css_task_iter_allowlist(struct bpf_verifier_env *env) switch (prog_type) { case BPF_PROG_TYPE_LSM: return true; - case BPF_TRACE_ITER: - return env->prog->aux->sleepable; + case BPF_PROG_TYPE_TRACING: + if (env->prog->expected_attach_type == BPF_TRACE_ITER) + return true; + fallthrough; default: - return false; + return env->prog->aux->sleepable; } } @@ -11663,7 +11671,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ case KF_ARG_PTR_TO_ITER: if (meta->func_id == special_kfunc_list[KF_bpf_iter_css_task_new]) { if (!check_css_task_iter_allowlist(env)) { - verbose(env, "css_task_iter is only allowed in bpf_lsm and bpf iter-s\n"); + verbose(env, "css_task_iter is only allowed in bpf_lsm, bpf_iter and sleepable progs\n"); return -EINVAL; } } diff --git a/tools/testing/selftests/bpf/progs/iters_task_failure.c b/tools/testing/selftests/bpf/progs/iters_task_failure.c index c3bf96a67dba..6b1588d70652 100644 --- a/tools/testing/selftests/bpf/progs/iters_task_failure.c +++ b/tools/testing/selftests/bpf/progs/iters_task_failure.c @@ -84,8 +84,8 @@ int BPF_PROG(iter_css_lock_and_unlock) return 0; } -SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") -__failure __msg("css_task_iter is only allowed in bpf_lsm and bpf iter-s") +SEC("?fentry/" SYS_PREFIX "sys_getpgid") +__failure __msg("css_task_iter is only allowed in bpf_lsm, bpf_iter and sleepable progs") int BPF_PROG(iter_css_task_for_each) { u64 cg_id = bpf_get_current_cgroup_id(); From patchwork Tue Oct 31 05:04:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuyi Zhou X-Patchwork-Id: 13441049 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 F15716FD8 for ; Tue, 31 Oct 2023 05:04:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="IvpHygHq" Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6070EF9 for ; Mon, 30 Oct 2023 22:04:52 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id 98e67ed59e1d1-27d0e3d823fso3702471a91.1 for ; Mon, 30 Oct 2023 22:04:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1698728691; x=1699333491; 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=JqCtKR6VA8EtQ7XQ5WZSgGs52BD+takdwiFmeHfAd18=; b=IvpHygHq0xXnJPsM7HZJz35sR6OzGeuGBd227lCi8qImDWeLMQDC3hNhU0KpvreFzl kLwGBqUvEk2Q8EAVX6CdBM7Bk1y0uzu8hIpLy9Pol87oUHGEsrL8hlfa0IRJIGzpouaG gjEwPw+efVK4EEZ4NuMJ8rvqKn1tFLiehIpousqnnuqtb23dbO2sQG917egwkgRp7pyf XnKEUMAJkOhlAwfBZvetHUut3D+SEz1YSSXfTaLIr1k9UuyA8AhDf+Yhl8qg/ka6/oE9 0uXrIkQ00/9ccOCsSjXSUckBNSDMX7MF2XLlQUkwRSMWrxPWFsr1IEBFQVR7lnSDaVGE wJmA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698728691; x=1699333491; 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=JqCtKR6VA8EtQ7XQ5WZSgGs52BD+takdwiFmeHfAd18=; b=P3gRm+he97+QgErhrifgcKoi8F7Lz60VZzikDHCFGnbymbHBp/IMS2E/r6MRyctMPy BIBSlX/hitbixWe/idb6JDpA/cyl2l7AlRyJcYUvYTyo216JlvQqsrviRmoA+tvhFtOi Jw/r74d95RRJnlq96LpJNkq3UknGm72xZNVmkC+g9yYmcTB9NCqplAFVTyXs/g48liHR ZUe7F0jw9gneiDVZargV4hnyvkeY1kHwHz3QcEOb7+D9rbEPnE4aR/9AdwSBKDUKRWPi ZqSbO6tGcs7DdD9Ta7GHyszn7aSQ3D+fe+kw4i+Ju0+Zr0LKzpPdKkew72lY+IIVVJNT rx6A== X-Gm-Message-State: AOJu0YzxMi7aoyTY03sxk/VM5D1UHRt4NHTPLR7GX3Cgm+RRKICNkbK1 CBIhL6nxBsdb8OsabtUtsns33RUAD/Yj3NYkRxA= X-Google-Smtp-Source: AGHT+IEeAuMjgbWw0ybKIuiRRXdzgtWXz0PMeAZ9+966Wo0Rzf6dCL6JWbvLUtg3xtXrP8iyeEhlYA== X-Received: by 2002:a17:90a:357:b0:27c:f9e7:30fd with SMTP id 23-20020a17090a035700b0027cf9e730fdmr8398717pjf.7.1698728691350; Mon, 30 Oct 2023 22:04:51 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.142]) by smtp.gmail.com with ESMTPSA id 21-20020a17090a195500b0027ce34334f5sm350951pjh.37.2023.10.30.22.04.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 22:04:51 -0700 (PDT) From: Chuyi Zhou To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, Chuyi Zhou , Yonghong Song Subject: [PATCH bpf-next v4 2/3] selftests/bpf: Add tests for css_task iter combining with cgroup iter Date: Tue, 31 Oct 2023 13:04:37 +0800 Message-Id: <20231031050438.93297-3-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20231031050438.93297-1-zhouchuyi@bytedance.com> References: <20231031050438.93297-1-zhouchuyi@bytedance.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net This patch adds a test which demonstrates how css_task iter can be combined with cgroup iter and it won't cause deadlock, though cgroup iter is not sleepable. Signed-off-by: Chuyi Zhou Acked-by: Yonghong Song --- .../selftests/bpf/prog_tests/cgroup_iter.c | 33 ++++++++++++++ .../selftests/bpf/progs/iters_css_task.c | 44 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_iter.c b/tools/testing/selftests/bpf/prog_tests/cgroup_iter.c index e02feb5fae97..574d9a0cdc8e 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_iter.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_iter.c @@ -4,6 +4,7 @@ #include #include #include +#include "iters_css_task.skel.h" #include "cgroup_iter.skel.h" #include "cgroup_helpers.h" @@ -263,6 +264,35 @@ static void test_walk_dead_self_only(struct cgroup_iter *skel) close(cgrp_fd); } +static void test_walk_self_only_css_task(void) +{ + struct iters_css_task *skel; + int err; + + skel = iters_css_task__open(); + if (!ASSERT_OK_PTR(skel, "skel_open")) + return; + + bpf_program__set_autoload(skel->progs.cgroup_id_printer, true); + + err = iters_css_task__load(skel); + if (!ASSERT_OK(err, "skel_load")) + goto cleanup; + + err = join_cgroup(cg_path[CHILD2]); + if (!ASSERT_OK(err, "join_cgroup")) + goto cleanup; + + skel->bss->target_pid = getpid(); + snprintf(expected_output, sizeof(expected_output), + PROLOGUE "%8llu\n" EPILOGUE, cg_id[CHILD2]); + read_from_cgroup_iter(skel->progs.cgroup_id_printer, cg_fd[CHILD2], + BPF_CGROUP_ITER_SELF_ONLY, "test_walk_self_only_css_task"); + ASSERT_EQ(skel->bss->css_task_cnt, 1, "css_task_cnt"); +cleanup: + iters_css_task__destroy(skel); +} + void test_cgroup_iter(void) { struct cgroup_iter *skel = NULL; @@ -293,6 +323,9 @@ void test_cgroup_iter(void) test_walk_self_only(skel); if (test__start_subtest("cgroup_iter__dead_self_only")) test_walk_dead_self_only(skel); + if (test__start_subtest("cgroup_iter__self_only_css_task")) + test_walk_self_only_css_task(); + out: cgroup_iter__destroy(skel); cleanup_cgroups(); diff --git a/tools/testing/selftests/bpf/progs/iters_css_task.c b/tools/testing/selftests/bpf/progs/iters_css_task.c index 5089ce384a1c..384ff806990f 100644 --- a/tools/testing/selftests/bpf/progs/iters_css_task.c +++ b/tools/testing/selftests/bpf/progs/iters_css_task.c @@ -10,6 +10,7 @@ char _license[] SEC("license") = "GPL"; +struct cgroup *bpf_cgroup_acquire(struct cgroup *p) __ksym; struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym; void bpf_cgroup_release(struct cgroup *p) __ksym; @@ -45,3 +46,46 @@ int BPF_PROG(iter_css_task_for_each, struct vm_area_struct *vma, return -EPERM; } + +static inline u64 cgroup_id(struct cgroup *cgrp) +{ + return cgrp->kn->id; +} + +SEC("?iter/cgroup") +int cgroup_id_printer(struct bpf_iter__cgroup *ctx) +{ + struct seq_file *seq = ctx->meta->seq; + struct cgroup *cgrp, *acquired; + struct cgroup_subsys_state *css; + struct task_struct *task; + u64 cgrp_id; + + cgrp = ctx->cgroup; + + /* epilogue */ + if (cgrp == NULL) { + BPF_SEQ_PRINTF(seq, "epilogue\n"); + return 0; + } + + /* prologue */ + if (ctx->meta->seq_num == 0) + BPF_SEQ_PRINTF(seq, "prologue\n"); + + cgrp_id = cgroup_id(cgrp); + + BPF_SEQ_PRINTF(seq, "%8llu\n", cgrp_id); + + acquired = bpf_cgroup_from_id(cgrp_id); + if (!acquired) + return 0; + css = &acquired->self; + css_task_cnt = 0; + bpf_for_each(css_task, task, css, CSS_TASK_ITER_PROCS) { + if (task->pid == target_pid) + css_task_cnt++; + } + bpf_cgroup_release(acquired); + return 0; +} From patchwork Tue Oct 31 05:04:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuyi Zhou X-Patchwork-Id: 13441050 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 8ED31CA68 for ; Tue, 31 Oct 2023 05:04:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="jvl0qBy8" Received: from mail-pj1-x1032.google.com (mail-pj1-x1032.google.com [IPv6:2607:f8b0:4864:20::1032]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30548F3 for ; Mon, 30 Oct 2023 22:04:54 -0700 (PDT) Received: by mail-pj1-x1032.google.com with SMTP id 98e67ed59e1d1-2802b744e52so1898900a91.0 for ; Mon, 30 Oct 2023 22:04:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1698728693; x=1699333493; 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=KHjScvx8WEZuHjDOhihyelPybn4gwPGSDSuXuehLacg=; b=jvl0qBy8+E7FU2CHoFTD36zGOAQBaZjYyANqcb8bmSS/hQAkfxdn3aQGYdxoYqRID8 ILNZPLkznpJ5/nAuUCWgic+HnKa0EjfWH6vHBnHz1zit58dTHI1vWlkUzzTp11z4g/vj ZgRkdx1r1uG05aEP7QCIqcYpP1hXrfjLavZTi7QAipo4DyX4dvHcqeCK54JLyFrpFjrm vL+2ceE6hxgyTJ8cfxcbg6oTpmmMOvPFzYJ0FuTgPTnY1puNwKfkvpWJIIwLzpTbA5BN qUb/fA5yivEl+opXRExgqiYmCojC7b65XJSH4d8LkHCkC9qHMRaxhZ26SLShwE6KCwka Qr/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698728693; x=1699333493; 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=KHjScvx8WEZuHjDOhihyelPybn4gwPGSDSuXuehLacg=; b=IbS9rvczD+ip3GcP8beOeqwq+rMf/eIc6jClI6YhPTgoyc5WzaVD79W3iyvo8+6ZES 0743AQXUGVmdbk5DIWct/BFc2PZ9ee+Xy3qJGHgVqRPvuqvY9/EeR+4PF73MFDGM7AKl 8Buy11wKF+Tcoh+Akz1A15+tWFr33tpyemcuPS1xrbOMhzORu+aWrXJkhkcUM2QKxUL2 D9xPY5EGnttp05P6KmIsVQjKPMK0+50FwEe8fgMNYUpJsnjmJ1g8ya1r9qIDCGRIUQ7D 7kjaNErbECdqPFakdC9F6U6EL7IaEFhKadYwfGcHyLl8WHh1MpsUtn/gEmoMPGIyxgxQ sS3g== X-Gm-Message-State: AOJu0YyD7BinQiDhVQX3KVZFczm7GumXHErwpNwKw8UT3lNTikeHfyPp oBPspZNv7CctJXtANA+g763e/uyXcv+A1Aj0bKA= X-Google-Smtp-Source: AGHT+IFcJ2iLnCohdPUcXXSHvhUQBOAl8xtEfhb33V9KDv/UuBjYUO+VPeNYJsoPOHdVmsR5HQdBEg== X-Received: by 2002:a17:90a:600f:b0:280:8e7d:5701 with SMTP id y15-20020a17090a600f00b002808e7d5701mr2095099pji.2.1698728693503; Mon, 30 Oct 2023 22:04:53 -0700 (PDT) Received: from n37-019-243.byted.org ([180.184.51.142]) by smtp.gmail.com with ESMTPSA id 21-20020a17090a195500b0027ce34334f5sm350951pjh.37.2023.10.30.22.04.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 22:04:53 -0700 (PDT) From: Chuyi Zhou To: bpf@vger.kernel.org Cc: ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, Chuyi Zhou Subject: [PATCH bpf-next v4 3/3] selftests/bpf: Add test for using css_task iter in sleepable progs Date: Tue, 31 Oct 2023 13:04:38 +0800 Message-Id: <20231031050438.93297-4-zhouchuyi@bytedance.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20231031050438.93297-1-zhouchuyi@bytedance.com> References: <20231031050438.93297-1-zhouchuyi@bytedance.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net This Patch add a test to prove css_task iter can be used in normal sleepable progs. Signed-off-by: Chuyi Zhou Acked-by: Yonghong Song --- .../testing/selftests/bpf/prog_tests/iters.c | 1 + .../selftests/bpf/progs/iters_css_task.c | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/tools/testing/selftests/bpf/prog_tests/iters.c b/tools/testing/selftests/bpf/prog_tests/iters.c index c2425791c923..bf84d4a1d9ae 100644 --- a/tools/testing/selftests/bpf/prog_tests/iters.c +++ b/tools/testing/selftests/bpf/prog_tests/iters.c @@ -294,6 +294,7 @@ void test_iters(void) RUN_TESTS(iters_state_safety); RUN_TESTS(iters_looping); RUN_TESTS(iters); + RUN_TESTS(iters_css_task); if (env.has_testmod) RUN_TESTS(iters_testmod_seq); diff --git a/tools/testing/selftests/bpf/progs/iters_css_task.c b/tools/testing/selftests/bpf/progs/iters_css_task.c index 384ff806990f..e180aa1b1d52 100644 --- a/tools/testing/selftests/bpf/progs/iters_css_task.c +++ b/tools/testing/selftests/bpf/progs/iters_css_task.c @@ -89,3 +89,22 @@ int cgroup_id_printer(struct bpf_iter__cgroup *ctx) bpf_cgroup_release(acquired); return 0; } + +SEC("?fentry.s/" SYS_PREFIX "sys_getpgid") +int BPF_PROG(iter_css_task_for_each_sleep) +{ + u64 cgrp_id = bpf_get_current_cgroup_id(); + struct cgroup *cgrp = bpf_cgroup_from_id(cgrp_id); + struct cgroup_subsys_state *css; + struct task_struct *task; + + if (cgrp == NULL) + return 0; + css = &cgrp->self; + + bpf_for_each(css_task, task, css, CSS_TASK_ITER_PROCS) { + + } + bpf_cgroup_release(cgrp); + return 0; +}