Received: by 2002:ab2:7444:0:b0:1ef:eae8:a797 with SMTP id f4csp19386lqn; Fri, 15 Mar 2024 11:29:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVls1BLyPnFPCQ9hvXTdBT9zmDmBkVbhRR9pzmaRpl4PH9JeoM+3JpGAdIVbNRbAn3ZxKJHFIHWlM3I55JXG3IUWFW0+miTAJ4enAwmbg== X-Google-Smtp-Source: AGHT+IELof3Fg8RfEMHPYUazEslO03WYzetAL+teSNh9AoVvhp7o9uP/RNdqKADzOJW8ZSWPmidJ X-Received: by 2002:a05:6e02:180b:b0:366:3686:5356 with SMTP id a11-20020a056e02180b00b0036636865356mr7540282ilv.5.1710527369708; Fri, 15 Mar 2024 11:29:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710527369; cv=pass; d=google.com; s=arc-20160816; b=PcrWWjdXTcruImzR7BbJjL+lAyXtfeoE9Sqx+vqSPyi4nVgmrUTEvryWPJjoq/f33A 95HE7EFx8mBCdLQmfL/wZJYfmoU55XZJsYlrWasudnlj3ui+sHbXyRlP2V3VeJRYS5J+ xPapf8Dpt8CP4XFe6m1JdM81TCO65WDhv+DRaDTKp5RjcVILAu4iDtsYj2RfhK+qzaPR wvXZMUts3keEmQ+bfb5fbdqHL0COfhtjMrRc1vpMUR2cCIp+bnycHCUuzvR1oLEXzMxS gp99BUNpPaMNf4yKVXp96OugvknJ3JNQFCJ3iF2BJoPf2OtTl5cjfM7h7GJVIhXeS+55 KHRw== 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=KfZxqGfm794szzm6SQbZFvEP7H7WQ+NfmahR3rYMEb4=; fh=3gOWkjM2eiw5L8XsKf8yd8eemyjgIZFRnX7IqmxGUNI=; b=dNt3Es2smRpO3Fwvlen8DmUSa72vyXRp6VZpHHHN7Q3EJ3JkACCulyUyZNBRAM9PZN +Z7yOIkX8QAycHkWetqSo7r0TSYavT+m0Gnvwfn87eiHU+YvEOKIFlwgsZgsGWboYXbt bFgSUheFyIBqgI9FKSe6fVJwGdnRwy6L0T7uDDttV/FUCqni4gzIdpuxdn8Bj5MANM5Q 41e10MU1QTLZJUq+V9vfcOGbdmLl7GMLKp5nB+lp9ktm3FEjeIoBCihbp4v9gfU6yBAP Qn0zlfDrPHY511d3cFPtfinVnoLMHdjNZ/tz4vHmoxFCsf89RSiCoTD14ieqoX+CVQN7 4oWQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@netflix.com header.s=google header.b=D8K8IlPE; 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-104786-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104786-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=netflix.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id r139-20020a632b91000000b005dcd650abb4si3160636pgr.781.2024.03.15.11.29.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 11:29:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-104786-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@netflix.com header.s=google header.b=D8K8IlPE; 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-104786-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104786-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 26EAB281EA5 for ; Fri, 15 Mar 2024 18:29:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6D08E52F8E; Fri, 15 Mar 2024 18:28:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=netflix.com header.i=@netflix.com header.b="D8K8IlPE" Received: from mail-io1-f52.google.com (mail-io1-f52.google.com [209.85.166.52]) (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 793534F898 for ; Fri, 15 Mar 2024 18:28:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710527338; cv=none; b=pwp/ucMRWY5T1LtXD3KmaPXU33xY093vBWUYJMlw8o15Bf2QFc6hL+N5yOCZ9RcUkHNfiQOz5zfAmX3lAI7LRdQxaHiVe3Mt3E5Ok+vpPp7UDeYuuvLgldIe1Kij8lwP4Y4sM+hbFSYt9Scd7IhXr0S0sz8KA7LBX8xbmYmzsu4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710527338; c=relaxed/simple; bh=ttV9IS7mXHSV0ZtQpCibNPetdR35dyL3P7pVFnt1Rd8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=U2XfHcvvc7kPr8XfA7a3QFTeV26KqlXq7qvHEzyypOJCXFcGK0Crf0cqBXtdq+BimqKGDdE2K+wYvK/5K43gbzsWxx6xZ6Wc2M9t3kn9OJV5Okn029vHqlRk6v67MBV4Q5xu/joj6RrC3M7svVVW2tH7zoVq8XVlJ13u35Df5Sw= 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.52 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-io1-f52.google.com with SMTP id ca18e2360f4ac-7c7476a11e0so101503639f.3 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=fYcFUeqQyHYMIIUZ1Rm0yeZ2GlQAXCE7jw6nM+af2wY99GzN2sn0LZcsniVffxIFEN zEfRW3Z8HXzZi/0GrTkuo3hDOgKETpqH6nOwCZZ0BdQJksfGdp6SpIyN/VNyxjSyntm/ rx20dqqZEvEgFK0DDfOP7gCu7n04XbzGQwrN+ouNvRDxoyWYodXvXY+iMg5SjvB1Wbtk qcdpvF3CRCf34vkXngmYNFtwn0iyjzrsVNgHHfXoVhLvgpx1G4Ag70Et3gQ3Ba8UHRPE 1Q8JCrewHfUPpTVb3tTITRVqTv+O/YXeSdQDwXZ7hCh3+SI+v3EAINlcfj1Q+ir22Ee2 HVxA== X-Forwarded-Encrypted: i=1; AJvYcCWfRyGTBcTjSXcDRUsZk+oklTcIFU/l78DZe5GS88FrEyYyoJuR1+C7raz1AUBX44SSw6oqsM65EC30V0WQgM4xe3uqpZA8CzpLRKXM X-Gm-Message-State: AOJu0YwFDAXzKZ5fF0UHOZTn7Abu0tOdHi7I521llBQRuhHDsXPJaLSi 1CT0aJWng2RwT2j0ULCUuq91LWujXJc2MZHgcKKGt2d/p0ZVZ36Z/aDt3m8v6ms= 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-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_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"; -- 2.40.1