Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp653726lqt; Mon, 18 Mar 2024 22:05:01 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUkFWNIj5AJmscDZdYbSmi4g+YiU8nNjFCxhWQ8vw+oTkTYUfv5+R20qVCnSRYfAdv1Qb4O1sV+xpkg7g9u9w5IzQ7e21y7ZfpavB8UDw== X-Google-Smtp-Source: AGHT+IH4gmlOxIWEWP5H2zyDEsbaReI8vWLKkIoB2vOfe1S5NMMhwtNjdERrCAEujR5u9ubBf09Z X-Received: by 2002:a05:6102:1276:b0:473:35c3:aff1 with SMTP id q22-20020a056102127600b0047335c3aff1mr927416vsg.27.1710824701216; Mon, 18 Mar 2024 22:05:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710824701; cv=pass; d=google.com; s=arc-20160816; b=c5bnF/AwcrtuUICR/w8gfiRXbTSgSJViLPtsrgCmIAkq8Yv7+L/MwDXNY75+/E3b2T j+nsucBlnGEa4pSGTEGY5k8zu0Yy93MN4ulQ6iuSvcQpbOfEBnlfyELO8KfsAXOyuzaj 4c+u2W/1oP2jaIgJKBOT0NChk8pRjJRVzAc6u0BMxVruw3vjZp2aHfbwuoZPPEShy+ww WF6BllvvjBUFFI9/YQ2C+OKKZxtomcLIG/Aoqk59zCT6AP4TdBKredeA4j48hIMJkEnO rFP0q0nDaRAd5snV5ESk+WoGyB6sQXhWhoOO0BuQi26mJKNZBzDSliXBed5hI+iBK/iN EwZA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=zNBNaRHZ6AfjwYMq1bFzdzJMJiPnkD5V98EDpDv4hWA=; fh=O4uHpnoSCSS836Be3oyRkQTOH+DG0ltcHoiZbknj7xs=; b=hEkzNodJrUDyIN8BnfBYpK8l1C2NgxEzrYFkuTceLTPeFTMSlveF07BEjukw+1zvoi jAhJoe205JVYZed/ML7kuT3A3GYpI8BLLaz/7yvsB5GBb79bWIsAD5+5w/qnNzg+E/Pd 06u3b7vs9dYjIer3L/nnvVYO3v8FYeF0EG/LUBqSFHD9sPSGnt+SHBbHnprKS+i7EIdC Ex8i6+Sso4G7e0HUzJhx1Dlo/F2/n/D9pkRHnjuTHKnUDr9xMtCZvRkZY71bpCWWNXVF FzrSV+ruqaIMe+EKPjPHbj9U1nNqUWnkCu8BY9PAwWrQNeMI2YKeFu57KYbLfy5KYqHR 2zsQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@netflix.com header.s=google header.b=SCC4OIgN; arc=pass (i=1 spf=pass spfdomain=netflix.com dkim=pass dkdomain=netflix.com dmarc=pass fromdomain=netflix.com); spf=pass (google.com: domain of linux-kernel+bounces-107002-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-107002-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=netflix.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id ow20-20020a05620a821400b0078831bd38dfsi10555348qkn.379.2024.03.18.22.05.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 22:05:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-107002-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@netflix.com header.s=google header.b=SCC4OIgN; arc=pass (i=1 spf=pass spfdomain=netflix.com dkim=pass dkdomain=netflix.com dmarc=pass fromdomain=netflix.com); spf=pass (google.com: domain of linux-kernel+bounces-107002-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-107002-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=netflix.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id D8C501C216CB for ; Tue, 19 Mar 2024 05:05:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 44468446B7; Tue, 19 Mar 2024 05:04:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=netflix.com header.i=@netflix.com header.b="SCC4OIgN" Received: from mail-yw1-f173.google.com (mail-yw1-f173.google.com [209.85.128.173]) (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 974F73FBB7 for ; Tue, 19 Mar 2024 05:04:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710824686; cv=none; b=slI6DgN8QBDDVTJJwgTFf1UsJ333XyFJXBNNDWrsmEUOcqyxnF1VoH6gJdqU/rGUm1DojKlBDTfx20yiNvw/ugl9EpRiOvNUuYf6NYE7ryc6K4OsrhN/1PeeGQIpkrBot9N8A1ximy8s2UmVN/c8397FXv04bAlQk5jUe43CzLE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710824686; c=relaxed/simple; bh=o66rBTTTN9Bz/2JCsO8MOAFQm7PEkrdPPz3CiJGnfAI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VhRKPMhCZ6HIPxQSaOoo2poQ3yAskJP2BIV3W6DDRyMIqvBIb4c0Ixz2ZZV4hO5b6zWmFTOHPShwWIL8rONtbkTtiQHiP0eyBErAs924utmJlPkiApt7P5ekdSHLPQ8TEZKgQiuDEP3KTO4GWoj3kZ5eZ8KU5cjNf2N9rSxdQ3Q= 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.173 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 Received: by mail-yw1-f173.google.com with SMTP id 00721157ae682-60a0579a955so60854237b3.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=chGKNEErGnIbKPYa/RxuTOQ4Pgj726WEnDqzcKIGdeqLpp//zVDlU0y5lrrj6uOzVQ qcnpJ/Z2qAU+BNA/jBjNhurXhiOdL6w14YwOq/W76esx5LR+dlZmVwkAo3FC8MLVJbT0 QbKrXmVFk2CIxpco6rrK2YVawc6nnCutGby7TLucH8c7q2qKOcsCazZIoRLw2Dg2fSny 3EiQkL1uZql8Ze8qIsO96qEr+xw6dtU14g4+79z52H1mYNAGbSEVnZUr5xpvwcwbAIns G0CQykFt2AaGHsJn0XVdsZEVb+mL1Wqfz9dYXcQxnBGmPsy+UyL8jCkJq4W02/TjaYOS ULIQ== X-Forwarded-Encrypted: i=1; AJvYcCVpVDjiWsFGyP0NkmuNyztMWgBoY1cZ5iFGby8dGOcq6bhEUr1Qwon+h9x/JHnvK06X1ZwEBzSZbURJMQX83Uc9bFynUXDcgqK3Kotq X-Gm-Message-State: AOJu0YysF1k0DWGCiUVd8bldIPVTfhApJX7u/0UgsJJYwzYVMbEyFIXS Kzqn8oztay38qt6F+7X+EkwML+MkTFpwaO743bFgnM+hSoGvquaFk63inIoEIbo= 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: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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"; -- 2.40.1