Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp848178lqt; Tue, 19 Mar 2024 06:01:02 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXM4yp5hU7NsnIkq//fJms6zsdIv7PtV91FEcaTjQC+6TN7twUZ0X852m+qJ+rY5euPcIbo7jHEd2smUII7cqE28tuLemO63w0488s1bg== X-Google-Smtp-Source: AGHT+IHVI1tnQgZ3rWnfwwLR2igJuJSgI1NT5PhQOzJCH8L/HoIUcrxMgETcQU28uimUJvbZtYsA X-Received: by 2002:a50:d542:0:b0:568:b71a:5954 with SMTP id f2-20020a50d542000000b00568b71a5954mr2263980edj.10.1710853262429; Tue, 19 Mar 2024 06:01:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710853262; cv=pass; d=google.com; s=arc-20160816; b=LT9tL87kEln5rIZFxKn61JUog1ZrcdiPXXDix51ygSfZ5OHVULMxFNavI3OZy8DmDv FqNDYcvbUfAQTWkq3mN3aGm93b/hk1OzE+I0+nNvMpuqL9PRSl90AO+91zQYHAKaoZeP Vh0rdCETN6wOx04kK2PRonFk7L4P1wPVHTDLjbmLtM36VA3GDBo8Zu61yGDH9y46IGBL yMRUY+ICUur+bOwzM0cY7RVD3kSO55ZcxOosPIxa7YsS2hs4VXWCDlX/dvp0DY1ag5Ap /NA5O9bj3V9UbrInza3RTzcMUAJJdDD0hACdIcsJoEzIpkvPUU/qfdtfmLvwqHco2XQL lBtw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:date:from:dkim-signature; bh=QEQcU4Am6cFlGB3h7vopamQ/UuLga+7FMVkz+H+gtzs=; fh=T+HA3sjEXEbGu8quoX/jlj4buzT3X2hQttHpoavDW1g=; b=WIt9LNj4yXR9EwdVVyKZQxhn6Wumd2Z9yCai6z45OT7AqhKV4+Gh61sA8qrwYsqTmx aRYjaw7kXmhqmZCO/90DpnBIQaN1iTD+QpZSBv2irKJ7zo0CTFPbgpEKFUrbfVxMKd7O lKQP1XeoIUZ3TBafuQ6SlwnEdFec5hu05wZggu7PpNqgtyFRcTkIj30lpOLmGaxW789/ YZJyJyj0bvs5gr/yAOOZjRm3Uc5J9+BIXchGa0j1Z5Ncoa2Kd2YQtOfJj78CqJFkDC63 JDPrPkkEnshS0A+HM9bLZQvX7CVpdHer7v+K/zXWMLKZ3DNhp41STve6FEEnFiUy8d0Q Tppg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Yoyj1Ahn; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-107548-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-107548-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id cs15-20020a0564020c4f00b00568c29e8720si3254182edb.159.2024.03.19.06.01.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 06:01:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-107548-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Yoyj1Ahn; arc=pass (i=1 spf=pass spfdomain=gmail.com dkim=pass dkdomain=gmail.com dmarc=pass fromdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-107548-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-107548-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 am.mirrors.kernel.org (Postfix) with ESMTPS id F1EB81F2289F for ; Tue, 19 Mar 2024 13:01:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0BFE05A4C7; Tue, 19 Mar 2024 13:00:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Yoyj1Ahn" Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 5FFFD7F7C1; Tue, 19 Mar 2024 13:00:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710853250; cv=none; b=TZFRIDb6sXM+BV2wuIpzrQnjPHJUfyPX6caATFd45CoqF977KpoWyZy4OVnMKCq7H3O8DtsbjuXNcVfpnHSA1YApYeNy/yz5/Q/C/wmsamy1qg4OA9/gMbcCODkfC82Xa9ILANBWvt/3nsmFpPDHunOrbtp3GgHroLtVd3LhXKA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710853250; c=relaxed/simple; bh=jdQKmmzqzwh98Iw0dr9vu1w/LhZiXOcHFPjsdzHyLTo=; h=From:Date:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=BUzgkaOpXI0AmmmRt5afNup6Fk3/4cVG4m/Xv5iWBg8Fg/GZhiU+ojAHE4OR0rjT8bGnjVJBbtyKR7sZyOKjL/57rBMBh90a0nrN7+5FkkKzRbrYAFCjbFU7unlxQlu4I2D51JI1cagcgyETwrZFJo4b+bt+JKnjEp4VzBSumHE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Yoyj1Ahn; arc=none smtp.client-ip=209.85.128.45 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-41465864644so3242835e9.0; Tue, 19 Mar 2024 06:00:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710853246; x=1711458046; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:from:to:cc:subject:date:message-id:reply-to; bh=QEQcU4Am6cFlGB3h7vopamQ/UuLga+7FMVkz+H+gtzs=; b=Yoyj1AhnMDIMEl6mnO0sjiAtlti83RdokXokPZoJoaUYok7HYSbkJgmwPaQdSqLs7d y/AqjKI0AJTpKzdq2W3TgMhjTOEZ2FR3Ad6qE/jzFfogHabkxC33zY7+GVzwCQbP8U8p dV53zNjYZ9HKH3OqkbObuEpzVA9GZDc/uHdj54hunUGJ2vjyX4FxJxAmJr2mO7CTQngp w90pNnle/yopinIBY2jOvuGLbmX/OoZ7bs7+cX1JyJ0QUpVCiTAfwAODIO1bCfLeu/M1 ICQ3wSfeFzttxs3uSuQHykSlS04Yic8ZblyXOVS/gniYVHdxfLo+b2ShgbkwG+aTsEgq TtAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710853246; x=1711458046; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=QEQcU4Am6cFlGB3h7vopamQ/UuLga+7FMVkz+H+gtzs=; b=QyyzATMDMPsZpzRZv92+pOBSKPLJi0nQGuwiw6b/aXJVJv/+GXxeJsDc7fkHyKDfvx 5zID/aHV8dpETmbIHbqOcfyMCYRyuDKcLXp8hWGkrwhhWLyIBPtykkg6MEyiMTf8xOjD sN8q6MoENJ3wazPCm1NhKzfRTN03YRv4j1JOJCT/BR6EckPT6AKSThuLa1xeoIpkBw8E VT+biD2lH0szaiXoR/FJ2yeynxO4hbxisgS78Ex/fcE0o3+nGFvLYkvSd9rN145MJ4ly 6bDfTN/qIdmBwqJli2U9dG8cuc2bdgSTBmWnafsaNRcr+RXzFGLghgg17Aex6CxHJhRQ dfyA== X-Forwarded-Encrypted: i=1; AJvYcCWkB0e6jV7AlvyQ83+9lWSl66+konbWrU7Z1+z968AgLBFwveswy8lHPxGLpehHpcRuWT/3rOp9apdZS3yeEdLjebJAHqOffB8TMbQKnJujQNX4MshPhMcLM80p9C8tLqEMpXN+QN7pqhw9SEOuAYKAxPSjMJ15XxQF1bkBUeTUucJD X-Gm-Message-State: AOJu0YzKJ1vKoFuUaseB6I/ylVk4cVslsaujDosm8G3IqjBt3FCGJfmt n5ln9KPs/tsJZXyc25cvPhL/NaIG7gm73imGGF4evciJ969+fivo X-Received: by 2002:a05:600c:1c27:b0:414:f4:c6c0 with SMTP id j39-20020a05600c1c2700b0041400f4c6c0mr2052280wms.20.1710853245765; Tue, 19 Mar 2024 06:00:45 -0700 (PDT) Received: from krava (2001-1ae9-1c2-4c00-726e-c10f-8833-ff22.ip6.tmcz.cz. [2001:1ae9:1c2:4c00:726e:c10f:8833:ff22]) by smtp.gmail.com with ESMTPSA id fk5-20020a05600c0cc500b0041468b3b84dsm447258wmb.13.2024.03.19.06.00.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 06:00:45 -0700 (PDT) From: Jiri Olsa X-Google-Original-From: Jiri Olsa Date: Tue, 19 Mar 2024 14:00:41 +0100 To: Jose Fernandez Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Stanislav Fomichev , Hao Luo , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Tycho Andersen Subject: Re: [PATCH V3 bpf-next 2/2] selftests/bpf: add selftest for bpf_task_get_cgroup Message-ID: References: <20240319050302.1085006-1-josef@netflix.com> <20240319050302.1085006-2-josef@netflix.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240319050302.1085006-2-josef@netflix.com> On Mon, Mar 18, 2024 at 11:03:02PM -0600, Jose Fernandez wrote: > 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 jirka > --- > 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 >