From patchwork Fri Mar 15 18:28:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jose Fernandez X-Patchwork-Id: 780655 Received: from mail-io1-f46.google.com (mail-io1-f46.google.com [209.85.166.46]) (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 803EC4DA1F for ; Fri, 15 Mar 2024 18:28:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710527335; cv=none; b=cbMmqAifjRvt1U0zw/+OiUESeoO86/4glu6iZquarywCm8oyunklAMQt+WbFz+N7bkzx7cmUGgGlctq9dfp5LpAmG8TCFlPPo9knG/gTzzTauslbRFGRJVhY1WzmG/LaRDfgEXzgL5wzwRTkIB8gp2oId5YbjDtoEYBmU8Yrkk8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710527335; c=relaxed/simple; bh=lWf98zPH7AshEAfiihgiLaljEiVmaYNX6Bsa9pdVB3Q=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=B1fesp1dSLpBxscodMOeV6mb7uH4C8dNuQdCulfBVZU9G9YO9mNKgKykQ1M8r72C+ao6usSo0vbmRg7NQfkXCbAic21Gb3PBM9KXpk4JKJKdq9WD1a9grLTYPswU7iZda8R9f/2J/xUdczIYbVRItYj/Fk+cv/kl27/eNPt07Ec= 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=fkMPeTfX; arc=none smtp.client-ip=209.85.166.46 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="fkMPeTfX" Received: by mail-io1-f46.google.com with SMTP id ca18e2360f4ac-7c7f3f66d17so49085739f.0 for ; Fri, 15 Mar 2024 11:28:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netflix.com; s=google; t=1710527329; x=1711132129; 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=lCgfh3Sxm/BV+pCR26EGSXvQQ7SOcSkk18NjS+RmHtA=; b=fkMPeTfXdww4tYqDlvBxrFDh4WXGOgfIsQ4XeTXXCCzBD1jXRy+CIsK521iYLfcmNA D6rf+ZmZqeqH+RZFa9pmr44vr5hljIw9aEVbb6R5uiqs0oF2j9NwbhiVaMEDCl/uRhui KpvrSY6oZ29LOLP23D06QZLwkD+wdAQcKbBxo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710527329; x=1711132129; 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=lCgfh3Sxm/BV+pCR26EGSXvQQ7SOcSkk18NjS+RmHtA=; b=TdqiMLKLOE6zSjM03aS8zzraS0GSrLl3f1u94mEwTn5LGr/r0OEYOoCjLC2BnUTLgA ZcG5EgO/fTvBFFqAn5slkY45AEivuSN+Nj+vKjyu+XOAZoyKSaBZb/Qn2KTCOlKsiAfM LqUnmrmAFgoUTHobMaOBRbAQDyFr+QhRh0Us/pmXMQxkClnBI4fMc2VrqEdcdS9fMIEP n3Q6UJsay/3mHzqOFrVJvGeBWIE6aoSrSvvuDnX+rD0Xgm9jWgiHWLrLNtD4kRGVVs8r 9olovR1QMQk8wlAqQGR9XIYVWqLEON88WqCMdfUgL3/oTlFoNRGwxXGvXhRKyKDkmfzj dA+A== X-Forwarded-Encrypted: i=1; AJvYcCW5vOcivFUYGtEy6ApYw9YO/t2uZOguLFNpHnZJt1drQxVl7XHh/zYZJ/YuC92FYR+Pz4DwQAxe6DfFowo1YbA1lK/tdTsgRTKO9A9Pyjra X-Gm-Message-State: AOJu0YzDE2dD6+4AJdUcXqmlka6kRJ4A+fGpB7pnASgdk0ZEaTq7/4zR qV8/eWknDXei8QfuQ4EIktuN7M+qwFe1QiqJ5eu12LtLe8Yi/Np69qBeO6ncolo= X-Google-Smtp-Source: AGHT+IGbfe1+m911z4rDE0JLVaBIDIJ1kWDx4d7pMYs3vMUJE4YT+724SXKHssUqJerD08G4ekyvPw== X-Received: by 2002:a05:6602:184b:b0:7ca:ebbc:ee27 with SMTP id d11-20020a056602184b00b007caebbcee27mr5832106ioi.21.1710527329584; Fri, 15 Mar 2024 11:28:49 -0700 (PDT) Received: from localhost ([2601:285:8700:8f20:c760:7c0b:344d:cd33]) by smtp.gmail.com with UTF8SMTPSA id dl22-20020a056638279600b00477b9971a45sm185526jab.17.2024.03.15.11.28.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 15 Mar 2024 11:28:49 -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 bpf-next 1/2] bpf: add btf_task_get_cgroup_id kfunc Date: Fri, 15 Mar 2024 12:28:03 -0600 Message-Id: <20240315182804.216081-1-josef@netflix.com> X-Mailer: git-send-email 2.40.1 Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This patch enhances the BPF helpers by adding a kfunc to retrieve the cgroup v2 ID of a specific 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 previous and next tasks. The bpf_task_get_cgroup_id kfunc returns a task's cgroup ID, correctly implementing RCU read locking and unlocking for safe data access, and leverages existing cgroup.h helpers to fetch the cgroup and its ID. Signed-off-by: Jose Fernandez Reviewed-by: Tycho Andersen --- kernel/bpf/helpers.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index a89587859571..8038b2bd3488 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2266,6 +2266,27 @@ bpf_task_get_cgroup1(struct task_struct *task, int hierarchy_id) return NULL; return cgrp; } + +/** + * bpf_task_get_cgroup_id - Get the cgroup ID of a task. + * @task: The target task + * + * This function returns the ID of 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. + */ +__bpf_kfunc u64 bpf_task_get_cgroup_id(struct task_struct *task) +{ + struct cgroup *cgrp; + u64 cgrp_id; + + rcu_read_lock(); + cgrp = task_dfl_cgroup(task); + cgrp_id = cgroup_id(cgrp); + rcu_read_unlock(); + return cgrp_id; +} #endif /* CONFIG_CGROUPS */ /** @@ -2573,6 +2594,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_id, KF_RCU) #endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_throw) From patchwork Fri Mar 15 18:28:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jose Fernandez X-Patchwork-Id: 780790 Received: from mail-io1-f51.google.com (mail-io1-f51.google.com [209.85.166.51]) (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 7FFE74F8B2 for ; Fri, 15 Mar 2024 18:28:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710527339; cv=none; b=sXJ4UwtTcbGQYbwsEk4R97c9RO6qEdtcDWVZou9+uIYlAzFw8wcqYdpyUWmWB+bDDmA4+fIvKQbKTXemY2brquaIL+DO4fygbBtFErK6VSZiuT9mlMweijFW5bt/cn/z3Yir/f/Mz8SBOigXdVtbrRFwrHHnrwXI4fm8cgimFDA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710527339; c=relaxed/simple; bh=ttV9IS7mXHSV0ZtQpCibNPetdR35dyL3P7pVFnt1Rd8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=B5nPHEygJAKj43dkb1hnKv36Ajz5xFC/XyQ265F60eW/W7xcZAolOGb/EV4sdkqSDCbfkHnMzBSYpZvKI9xnj7dTJrmIIZdJxYCHmepgfWUDGozMjKjDyVWtlR5VVBxnU+24RI/0LoLd6JJDaDRY26xiqpx/h9Zll9wskegYH28= 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=D8K8IlPE; arc=none smtp.client-ip=209.85.166.51 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="D8K8IlPE" Received: by mail-io1-f51.google.com with SMTP id ca18e2360f4ac-7c8ae457b27so70691939f.2 for ; Fri, 15 Mar 2024 11:28:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netflix.com; s=google; t=1710527334; x=1711132134; 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=KfZxqGfm794szzm6SQbZFvEP7H7WQ+NfmahR3rYMEb4=; b=D8K8IlPEXZOu25BFtPybWQ2xEAuj/EIu06ufGthSon+K2097ecUOUzfCT54KET6HPI 6XmQblr8Ka3KZRTJtt0kk4Qz+I9+qzNu500tjNatmCefiE5BZKzs2lS0UAt2PMOCMxdo gsBHM2q0VEGN+D/I4vpTJh5TZ7pPhJfc+zfJQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710527334; x=1711132134; 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=KfZxqGfm794szzm6SQbZFvEP7H7WQ+NfmahR3rYMEb4=; b=J+vNJfF6X7y1N4TzQiYmVLJN9n5kg5lk0fEvuJClZgPKqBkBlnMI9YHypt6WicZMrs 2jj5kXoQGe1sas5bhxrKtMZ0xpaFsUlpWi22iMDcyeuTNAU2LANHNoIOdNEJgyDIf9xt O6BUiotuKB+yHSzkA8Skwf9gRO4a/7gJ1p9nM1sBtAEDlsGsNXslyS5dqMHJmspxXQ4+ 46gzsTaA4gjjaypb+9kPiKkd1Ruqj3v0l8jeR3WMtWr+vpfbmV5M0UVDjRC5KqCewz/L 2uRme6KqybOWZxXV08fgl9Fz0X/UbX6w3FtBVgSeTZsUmaEQMBQ6mEI2qeXOOeWjr9Rd lWlA== X-Forwarded-Encrypted: i=1; AJvYcCW4fpFazp4yrEcievXov+5AbNMfdSe/Zcl5L2YKilNnZcJdJPa2b0J9SFFciKWiG2LGg1Pt/dvxBdSGpxP+vqQn4E1GdJdS/LHIlj9c6Z7t X-Gm-Message-State: AOJu0YyX8EYGmvXwDmYr/N4h5s1tdnMrr/hKWBIY2NqOsLnIeN/52UkO Pj7wb79WuW0UJEwy7DuQDkpoLFTor0FPR9QNNfCDYhAiaM+5GipJm4n/d7R6CPIKZyK41Vy999g rfrfYgINy X-Google-Smtp-Source: AGHT+IHT56pFwD1DPWvseGf1YhKPgQoet77lNXaEyjmj1UMSb6H2RIBWlwfZ3LdWLPAnX9PdX7qxJA== X-Received: by 2002:a05:6602:4914:b0:7cb:dd4a:428d with SMTP id ef20-20020a056602491400b007cbdd4a428dmr3749532iob.10.1710527334615; Fri, 15 Mar 2024 11:28:54 -0700 (PDT) Received: from localhost ([2601:285:8700:8f20:c760:7c0b:344d:cd33]) by smtp.gmail.com with UTF8SMTPSA id j1-20020a02a681000000b00476e0c80ec6sm864616jam.93.2024.03.15.11.28.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 15 Mar 2024 11:28:54 -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 bpf-next 2/2] selftests/bpf: add selftest for btf_task_get_cgroup_id Date: Fri, 15 Mar 2024 12:28:04 -0600 Message-Id: <20240315182804.216081-2-josef@netflix.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240315182804.216081-1-josef@netflix.com> References: <20240315182804.216081-1-josef@netflix.com> Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This patch adds a selftest for the `bpf_task_get_cgroup_id` 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_id` during a `sched_switch` tracepoint, and validating that the obtained cgroup ID for the previous task matches the expected cgroup ID. task_get_cgroup_id:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Jose Fernandez Reviewed-by: Tycho Andersen --- .../bpf/prog_tests/task_get_cgroup_id.c | 58 +++++++++++++++++++ .../bpf/progs/test_task_get_cgroup_id.c | 30 ++++++++++ 2 files changed, 88 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_get_cgroup_id.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_get_cgroup_id.c diff --git a/tools/testing/selftests/bpf/prog_tests/task_get_cgroup_id.c b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup_id.c new file mode 100644 index 000000000000..b8c4551195d3 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/task_get_cgroup_id.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright 2024 Netflix, Inc. + +#include +#include +#include "test_task_get_cgroup_id.skel.h" +#include + +#define TEST_CGROUP "/test-task-get-cgroup-id/" + +void test_task_get_cgroup_id(void) +{ + struct test_task_get_cgroup_id *skel; + int err, fd; + pid_t pid; + __u64 cgroup_id, expected_cgroup_id; + const struct timespec req = { + .tv_sec = 1, + .tv_nsec = 0, + }; + + fd = test__join_cgroup(TEST_CGROUP); + if (!ASSERT_OK(fd < 0, "test_join_cgroup_TEST_CGROUP")) + return; + + skel = test_task_get_cgroup_id__open(); + if (!ASSERT_OK_PTR(skel, "test_task_get_cgroup_id__open")) + goto cleanup; + + err = test_task_get_cgroup_id__load(skel); + if (!ASSERT_OK(err, "test_task_get_cgroup_id__load")) + goto cleanup; + + err = test_task_get_cgroup_id__attach(skel); + if (!ASSERT_OK(err, "test_task_get_cgroup_id__attach")) + goto cleanup; + + 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 */ + syscall(__NR_nanosleep, &req, NULL); + + err = bpf_map_lookup_elem(bpf_map__fd(skel->maps.pid_to_cgid_map), &pid, + &cgroup_id); + + if (!ASSERT_OK(err, "bpf_map_lookup_elem")) + goto cleanup; + + ASSERT_EQ(cgroup_id, expected_cgroup_id, "cgroup_id"); + +cleanup: + test_task_get_cgroup_id__destroy(skel); + close(fd); +} diff --git a/tools/testing/selftests/bpf/progs/test_task_get_cgroup_id.c b/tools/testing/selftests/bpf/progs/test_task_get_cgroup_id.c new file mode 100644 index 000000000000..7e6bc008970f --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_task_get_cgroup_id.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright 2024 Netflix, Inc. + +#include "vmlinux.h" +#include +#include + +u64 bpf_task_get_cgroup_id(struct task_struct *task) __ksym; + +struct { + __uint(type, BPF_MAP_TYPE_HASH); + __uint(max_entries, 4096); + __type(key, __u32); + __type(value, __u64); +} pid_to_cgid_map SEC(".maps"); + +SEC("tp_btf/sched_switch") +int BPF_PROG(sched_switch, bool preempt, struct task_struct *prev, + struct task_struct *next) +{ + u32 pid = prev->pid; + u64 cgroup_id; + + cgroup_id = bpf_task_get_cgroup_id(prev); + bpf_map_update_elem(&pid_to_cgid_map, &pid, &cgroup_id, BPF_ANY); + + return 0; +} + +char _license[] SEC("license") = "GPL";