From patchwork Tue Mar 19 05:03:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jose Fernandez X-Patchwork-Id: 13596119 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yb1-f176.google.com (mail-yb1-f176.google.com [209.85.219.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0DB313FBA5 for ; Tue, 19 Mar 2024 05:04:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710824676; cv=none; b=c1lf4WMbIWE+B+OJraVCHtSl3hBejZvbvfyD3zwQhsD3mouj50qmIJi1T48H/MsLTL3E2xRMSQoNJy6liopMVHPnN4IXu9cXb+g+eZaU6hhbzR1w/i0JVSvfw0CwD5vCDC+IB8mBfD5Igzho6Cre6T3zItjirr6cBoWaYLDEAjQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710824676; c=relaxed/simple; bh=Lz2CrH8A7WjP27QDwUAd0Apjplhh1crikLeGS9SaF0E=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=u5dn7I+ogXqU1+XtynYNbtLCt9Y40uTu1KopNhIAY0m2kGEaJhSHcapb14/b/gEVU5eFZC3pgUGOQeG5PlOmoP5z9zJ0D9YiwZsDc6KFnb7k74+esaGtB+GciyMRFJdPHcPxhPKUcfAE/N63UrDHCoawksWy7R9IuroCAHNWbp4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=netflix.com; spf=pass smtp.mailfrom=netflix.com; dkim=pass (1024-bit key) header.d=netflix.com header.i=@netflix.com header.b=OMRvrYg7; arc=none smtp.client-ip=209.85.219.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=netflix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netflix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=netflix.com header.i=@netflix.com header.b="OMRvrYg7" Received: by mail-yb1-f176.google.com with SMTP id 3f1490d57ef6-dcbc00f6c04so4453647276.3 for ; Mon, 18 Mar 2024 22:04:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netflix.com; s=google; t=1710824674; x=1711429474; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=C+tjYLQhJo+BMVuUInD+N/p7F8UX37fJMVNt77Cc/o8=; b=OMRvrYg7HgBuIVmUkj1ch1iK0S0ygVW8fUtADWjIpylwb6Hxo3l66hHvDxyVbY7eoa 221LGIRkuutwtnLCR6FLdS2PTXMf+ruKaVuZAn0yWT3ov0u/tQDXkwqNRtx+yf81aodF 1IJEPTgS0ODyBAQ9uFkRyrmGgrh15Tb25B3qg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710824674; x=1711429474; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=C+tjYLQhJo+BMVuUInD+N/p7F8UX37fJMVNt77Cc/o8=; b=wD/b7QAOuE5Zo/ujbU5stWfDRUsEXslV9GARCZr6v0M+67sPxzTFSyWxgc8g0cIUTk Qn8d/ZlygHCCXWNjK2Gv3lJ4sM3PWdfN5j5ulwxs40gkTb2cCWNfik1HUVlF5KMq6rbs vUn46BTLO7d6HHojo/Oo0SooMMJATdCE5yhdBXYCBn+Mu0ZG0SRr/AjcBHfmLYgfVUXL YEwiwU9KuoyO1x9/DMTvSrZjZ7B+3QZa0d90bOL3gg7855cHZJWxsLku5Wtr0FoKKdJB SORGk88awa/TTsN/IXP29XL2tpEFA2ZsgA4Ge5/dbCC4Sh5xZqVupC0uL1W7+cpFmssA PS4w== X-Gm-Message-State: AOJu0YzJhmxo5uqImLsG80rDmCBKEBzVieaAbIFukhgyk+2OTHIxp/mg E6zm0+4UR3tLbs3+ik5V2yh4RZcBGLXotlFp15ahKpSdPPzqIiiOcUmtTuL8tE8= X-Google-Smtp-Source: AGHT+IGKnEb1MFeHqBSw8vDsqBbEODWdU6GBl76O84BfZEbLJavR+eqn8sFeEHheRyEbc1ga1F6bAQ== X-Received: by 2002:a25:6405:0:b0:dc6:deca:468f with SMTP id y5-20020a256405000000b00dc6deca468fmr9032612ybb.29.1710824673931; Mon, 18 Mar 2024 22:04:33 -0700 (PDT) Received: from localhost ([12.220.158.163]) by smtp.gmail.com with UTF8SMTPSA id t10-20020a25908a000000b00dcc0cbb0aeesm2023977ybl.27.2024.03.18.22.04.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Mar 2024 22:04:33 -0700 (PDT) From: Jose Fernandez To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Jose Fernandez , Tycho Andersen Subject: [PATCH V3 bpf-next 1/2] bpf: add bpf_task_get_cgroup kfunc Date: Mon, 18 Mar 2024 23:03:01 -0600 Message-Id: <20240319050302.1085006-1-josef@netflix.com> X-Mailer: git-send-email 2.40.1 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 enhances the BPF helpers by adding a kfunc to retrieve the cgroup v2 of a task, addressing a previous limitation where only bpf_task_get_cgroup1 was available for cgroup v1. The new kfunc is particularly useful for scenarios where obtaining the cgroup ID of a task other than the "current" one is necessary, which the existing bpf_get_current_cgroup_id helper cannot accommodate. A specific use case at Netflix involved the sched_switch tracepoint, where we had to get the cgroup IDs of both the prev and next tasks. The bpf_task_get_cgroup kfunc acquires and returns a reference to a task's default cgroup, ensuring thread-safe access by correctly implementing RCU read locking and unlocking. It leverages the existing cgroup.h helper, and cgroup_tryget to safely acquire a reference to it. Signed-off-by: Jose Fernandez Reviewed-by: Tycho Andersen Acked-by: Yonghong Song Acked-by: Stanislav Fomichev Acked-by: Tejun Heo --- V2 -> V3: No changes V1 -> V2: Return a pointer to the cgroup instead of the cgroup ID kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) base-commit: c733239f8f530872a1f80d8c45dcafbaff368737 diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index a89587859571..bbd19d5eedb6 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2266,6 +2266,31 @@ bpf_task_get_cgroup1(struct task_struct *task, int hierarchy_id) return NULL; return cgrp; } + +/** + * bpf_task_get_cgroup - Acquire a reference to the default cgroup of a task. + * @task: The target task + * + * This function returns the task's default cgroup, primarily + * designed for use with cgroup v2. In cgroup v1, the concept of default + * cgroup varies by subsystem, and while this function will work with + * cgroup v1, it's recommended to use bpf_task_get_cgroup1 instead. + * A cgroup returned by this kfunc which is not subsequently stored in a + * map, must be released by calling bpf_cgroup_release(). + * + * Return: On success, the cgroup is returned. On failure, NULL is returned. + */ +__bpf_kfunc struct cgroup *bpf_task_get_cgroup(struct task_struct *task) +{ + struct cgroup *cgrp; + + rcu_read_lock(); + cgrp = task_dfl_cgroup(task); + if (!cgroup_tryget(cgrp)) + cgrp = NULL; + rcu_read_unlock(); + return cgrp; +} #endif /* CONFIG_CGROUPS */ /** @@ -2573,6 +2598,7 @@ 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_get_cgroup1, KF_ACQUIRE | KF_RCU | KF_RET_NULL) +BTF_ID_FLAGS(func, bpf_task_get_cgroup, KF_ACQUIRE | KF_RCU | KF_RET_NULL) #endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_throw) From patchwork Tue Mar 19 05:03:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jose Fernandez X-Patchwork-Id: 13596120 X-Patchwork-Delegate: bpf@iogearbox.net Received: from mail-yw1-f175.google.com (mail-yw1-f175.google.com [209.85.128.175]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9BECA40840 for ; Tue, 19 Mar 2024 05:04:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.175 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710824685; cv=none; b=kHQDFDBe2VRtEg3U1oc5DahQt4k4Rz5H2HHpYri2ySmNGq0dDY8zeQRUw7j3VXX6wXKWDwtTsxKp8wM8ZTEIoMZQEMlzRgaSqjemVsvkD4OgLlAlH69yM1Su1H7KQrAPrUEokXiLdX0zB4VEmjKiyhRqfiacRChIY4LyxNIzqkE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710824685; c=relaxed/simple; bh=o66rBTTTN9Bz/2JCsO8MOAFQm7PEkrdPPz3CiJGnfAI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fxj4xq/kQINca23sdVyyFKZQ46xBgF+l29Lp8ClB7FrO21y5YlLKxY1QM7phql/Do5UExMfXs1c5NpPVGYv0zFSlkSt9Us2ZOOqAjCvaKNkp0gbNRj+EwZLJyEbgCebI7/6sMuiSW5Ik5YgVh4GQfzMamnMXj+dFfHPYSAz+ovQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=netflix.com; spf=pass smtp.mailfrom=netflix.com; dkim=pass (1024-bit key) header.d=netflix.com header.i=@netflix.com header.b=SCC4OIgN; arc=none smtp.client-ip=209.85.128.175 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=netflix.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netflix.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=netflix.com header.i=@netflix.com header.b="SCC4OIgN" Received: by mail-yw1-f175.google.com with SMTP id 00721157ae682-60a0579a955so60854227b3.3 for ; Mon, 18 Mar 2024 22:04:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netflix.com; s=google; t=1710824682; x=1711429482; 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=zNBNaRHZ6AfjwYMq1bFzdzJMJiPnkD5V98EDpDv4hWA=; b=SCC4OIgNjdzwhsm7MxjAGgVH/THmjIQ4RiL3DZsbnenpw1c30+uz+rAHeXEp1eDfwE hIF8k95Jzj2N3JCynE43EnRJSjQr25a6W39KfDzz0g2xYD/axXltnU5dh7vW+vpw2Ms0 9Zyzs9+GVKoBOBMqhsKbqTaVjju9xOEjR2sOg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710824682; x=1711429482; 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=zNBNaRHZ6AfjwYMq1bFzdzJMJiPnkD5V98EDpDv4hWA=; b=q39JBjQ0PHVHV+h5oQm5XgiE5zFfoNGynjiZIj8tQU4kd65usPd+Ylt2JZ3v0skh+m ooTOhWJYe8HKAIwTgUylE3rnc2XdeCfYuwi+RSaO1jwQZXtM68A9SIAQ95oOBQ8pYc96 iHYq/6aqEjCGY08sJw5uBXttwcPcNuqPynDw9KaoA0Zsw+3dTuZBV0yNhnR13VxrXzXZ UT9SbBcraWlqe54MdrUD87Ed3f96qfZTkn8k4B0gUWXcl8g5U0FuPTl4D5yeophbJnh+ ouDP+jfj30WnmYQ8Q3Tn8rJCsKEQu/2QopLzL++9JMUjNLJx8w33Tvh2/Bjx7vrvPw9+ fhHw== X-Gm-Message-State: AOJu0YwjiGO+5BFlMPR0GPMo4SCaolgOi/ZhePaBo0DSLk5824GDwIcE 9fPd5OurZTFhghoZO/qDXFo1MBdFnBtarOo6V6dMcEuZBCcPnNpRIwwadTaXP0o= X-Google-Smtp-Source: AGHT+IGUcPg4y8TA8iAfSq3V63mc8bIMbNH87magc9JoKtuTwKmArOL4jXrrqj2sJVI43B2iEtlmoA== X-Received: by 2002:a0d:db87:0:b0:609:18e2:91b2 with SMTP id d129-20020a0ddb87000000b0060918e291b2mr1836326ywe.32.1710824682582; Mon, 18 Mar 2024 22:04:42 -0700 (PDT) Received: from localhost ([12.220.158.163]) by smtp.gmail.com with UTF8SMTPSA id eq4-20020a05690c2d0400b00609f4170662sm2181268ywb.54.2024.03.18.22.04.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Mar 2024 22:04:42 -0700 (PDT) From: Jose Fernandez To: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Jose Fernandez , Tycho Andersen Subject: [PATCH V3 bpf-next 2/2] selftests/bpf: add selftest for bpf_task_get_cgroup Date: Mon, 18 Mar 2024 23:03:02 -0600 Message-Id: <20240319050302.1085006-2-josef@netflix.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240319050302.1085006-1-josef@netflix.com> References: <20240319050302.1085006-1-josef@netflix.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 selftest for the `bpf_task_get_cgroup` kfunc. The test focuses on the use case of obtaining the cgroup ID of the previous task in a `sched_switch` tracepoint. The selftest involves creating a test cgroup, attaching a BPF program that utilizes the `bpf_task_get_cgroup` during a `sched_switch` tracepoint, and validating that the obtained cgroup ID for the previous task matches the expected cgroup ID. Signed-off-by: Jose Fernandez Reviewed-by: Tycho Andersen Acked-by: Jiri Olsa --- V2 -> v3: Use _open_and_load(), usleep helper, and drop map usage V1 -> V2: Refactor test to work with a cgroup pointer instead of the ID .../bpf/prog_tests/task_get_cgroup.c | 43 +++++++++++++++++++ .../bpf/progs/test_task_get_cgroup.c | 32 ++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_get_cgroup.c diff --git a/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c new file mode 100644 index 000000000000..031623067e7e --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup.c @@ -0,0 +1,43 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright 2024 Netflix, Inc. + +#include +#include +#include "test_task_get_cgroup.skel.h" +#include + +#define TEST_CGROUP "/test-task-get-cgroup/" + +void test_task_get_cgroup(void) +{ + struct test_task_get_cgroup *skel; + int err, fd; + __u64 expected_cgroup_id; + + fd = test__join_cgroup(TEST_CGROUP); + if (!ASSERT_OK(fd < 0, "test_join_cgroup_TEST_CGROUP")) + return; + + skel = test_task_get_cgroup__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_task_get_cgroup__open_and_load")) + goto cleanup; + + err = test_task_get_cgroup__attach(skel); + if (!ASSERT_OK(err, "test_task_get_cgroup__attach")) + goto cleanup; + + skel->bss->pid = getpid(); + expected_cgroup_id = get_cgroup_id(TEST_CGROUP); + if (!ASSERT_GT(expected_cgroup_id, 0, "get_cgroup_id")) + goto cleanup; + + /* Trigger nanosleep to enter the sched_switch tracepoint */ + /* The previous task should be this process */ + usleep(100); + + ASSERT_EQ(skel->bss->cgroup_id, expected_cgroup_id, "cgroup_id"); + +cleanup: + test_task_get_cgroup__destroy(skel); + close(fd); +} diff --git a/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c b/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c new file mode 100644 index 000000000000..30d4499c6bc5 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright 2024 Netflix, Inc. + +#include "vmlinux.h" +#include +#include + +struct cgroup *bpf_task_get_cgroup(struct task_struct *task) __ksym; +void bpf_cgroup_release(struct cgroup *cgrp) __ksym; + +int pid = 0; +u64 cgroup_id = 0; + +SEC("tp_btf/sched_switch") +int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev, + struct task_struct *next) +{ + struct cgroup *cgrp; + + if (prev->pid != pid) + return 0; + + cgrp = bpf_task_get_cgroup(prev); + if (cgrp == NULL) + return 0; + cgroup_id = cgrp->kn->id; + + bpf_cgroup_release(cgrp); + return 0; +} + +char _license[] SEC("license") = "GPL";