Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp573630lqt; Mon, 18 Mar 2024 17:36:58 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWAI/yheYsacfPpQ8Tjqm3KViBSu2uk/Mcz26WOO3fdQXvJIEuUJj9pc2r114oTED1WUc1ZNzfeFfMH0Px3vtB/5Hnzg/Zp8rn50pCCnw== X-Google-Smtp-Source: AGHT+IEtIPjQEfk4RRTmcjg4fcC6dEmCwarPRLzrf3f3+fX0j7JZ3oId0UAwVD9FhtRFjeljRjHU X-Received: by 2002:a05:6512:3447:b0:513:9ae1:4708 with SMTP id j7-20020a056512344700b005139ae14708mr9066957lfr.46.1710808618061; Mon, 18 Mar 2024 17:36:58 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710808618; cv=pass; d=google.com; s=arc-20160816; b=aJvMvJ2Y2neWMP66+dylNmu68beqxsu9qrEm0JFu84Ju5G1bLza7f6GDeGvHgovsID VxWsZRawR94kutsRc8wmKmVe0l5I0aN5zBajTGx0i7gqgKKM54NkqXS+Uqr5BU9Ldjxz 6qKU1arKtleDyKEoIWnfa3YWch2cJodmzJYhBxl10F8wYnp35989o4pUKrDVCNi5WMPr 2TEBR8gzb/JsOq9+Yr1kNFxA8D/M7fwZ96MXUgLsJ2m/WVGZaUIuEgmeFml0PCSm+9iH pQotodA25I/mpAGshdtbdqkxzdbPyTInOmJNsyJg5x878Pfi0dl8WlejY2kbBpGttoDC Rf5g== 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:from:date:dkim-signature; bh=dX5+kGt94i8LgMCxRW3yMQ+Kn76GryytdXFaAC5Pwhc=; fh=JeLPPiEQXvZCGg4dbh1ii+F/bg2P5ceJ1Gb9JcIRONY=; b=j7+JLZ20G2cwIBcVJvzfME/WxUNkjZdTQqJbVMkRzy+AB9bGzr2NDhEVJEFB+N/VP2 3X0ZuzeTCy+WUnDvJ253454AXuw0kUIbn864C4UyCpq63zKHOmo3jd6NFc+rHWto172x bSDiiWk5EdXMiC+BO0Tvx2oxzVIeeFggIvxOZLgFoOj0mIL/niGBox2adZdZuzC+KbZz 3pI7z4lhlSwV1dYvx2PkVoPFtSoOdk/bH20noYrc2e5/xyBZ8CPzZ3A5sStWvPrD3kZJ oG92ELEQLJH0mIzSX0p6AtN7GLiTu6IigVaJSAtBnxBSDfWiyUNnXPA77k1i0zpO7R+5 1n0w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@netflix.com header.s=google header.b=hioRiCan; 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-106861-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-106861-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=netflix.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id op13-20020a170906bced00b00a46c0fe2cd4si1242662ejb.763.2024.03.18.17.36.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Mar 2024 17:36:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-106861-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=@netflix.com header.s=google header.b=hioRiCan; 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-106861-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-106861-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 am.mirrors.kernel.org (Postfix) with ESMTPS id B31E61F21D7B for ; Tue, 19 Mar 2024 00:31:35 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A1733257D; Tue, 19 Mar 2024 00:31:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=netflix.com header.i=@netflix.com header.b="hioRiCan" Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) (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 243AA3C00 for ; Tue, 19 Mar 2024 00:30:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.182 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808259; cv=none; b=C6HcUEGFRjff+dxVlTaq6qWguOy+h6ACwVs6Com6IFJfqBEGUCqER9OOsK/uQV96JUSzmYrKcopi0xnPtKhWChH20HbNhFaJo3qcSfQdkM/NvPxihmtSavW0ym6sg7ctR842q3/w57V47JT7yYkikRvkWyb5azlemrWTDvAX8SA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710808259; c=relaxed/simple; bh=8DfVubo4KjNWHw2+1Glu8UP/tZQT++5v3hFkpgpZ4Ao=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=IsNFRGAuoPJpG/Bsr3fx7HtsbcJ56yL8HWA+JuekBn+oxNfFWHCJ3wSKFD1sk3JSaEj5Ipe59rNma9RcK9fe6twcGeboOheSi26e5S0Ngpk9BD0ENK3tyh0sLGtfsG9uk0dfqIOZ14NJTTy5MWbKqCZKgG6pbPlZmetf5qg34IM= 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=hioRiCan; arc=none smtp.client-ip=209.85.214.182 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-pl1-f182.google.com with SMTP id d9443c01a7336-1dddad37712so47138085ad.3 for ; Mon, 18 Mar 2024 17:30:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netflix.com; s=google; t=1710808257; x=1711413057; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=dX5+kGt94i8LgMCxRW3yMQ+Kn76GryytdXFaAC5Pwhc=; b=hioRiCanH4L0PUR3MthXneI8Mg6RopWapzyK5OBNG75ES51W11rhFOeYCVD2lpneuI +rWCt9VwWPQ5mnK/dL7ZeYiQfmssFYF+XxU4Japfx8UA/3yVeN+Ey/SBKCIjQlLOxUBL UCL5uwfvGaBXU/8nh8/5xNFfuide5nzwTPefo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710808257; x=1711413057; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=dX5+kGt94i8LgMCxRW3yMQ+Kn76GryytdXFaAC5Pwhc=; b=rImLmke9DZ7amQr/CoMugdV33s+OJCwLb8172APc3mPNYOZlTM9TBKDI31iXALb3OR v5tXL9wIFcQbNgcFUvH3BgR8XSGRIpRICrst3fxw8nVIdgl/OhFG9dTNQt+Mc6pUqzjD hieHkIwRrD/9fa3Fk13npklyXd29Lm1vhssWPtdpTPt9LtNUY6DgERZ6Pry0tQbUIMHl eiU5zGsFeuQSw2f0CDKpkaVY5RpPM9O78YY9jfSFLe07fgh44gePalatlxIMsaZP56Fx +LHJHNzJvhdlHCqiugrYvCcHUf2GLROC2zA8K4TaXo6N2Xem9ZA00wHrp22iMKPV1BkZ mZOQ== X-Forwarded-Encrypted: i=1; AJvYcCX/rj5cUyphk8dQlELWUJoljA8WUCT3wT5+LNtRSoN0wyIEHD1pk6SiCadkayNr2N8PSTFtgKtvwSlhjZ3SqkUWjFSygMG66Cuh//tr X-Gm-Message-State: AOJu0YwUucENh5nJfCBCzNsG3oAMo9kI8ZTRRN1wN7EkMcpELk09ALUC /+ZGv6oQRUh0P0W6vMtrIpRL5rAWhgcAQSgiubjTPvkzn6VayeDp2soQcYLTzyqNc2lvokEjetS t8EglKA== X-Received: by 2002:a17:902:ecc9:b0:1de:f3bf:a47a with SMTP id a9-20020a170902ecc900b001def3bfa47amr13747269plh.9.1710808257419; Mon, 18 Mar 2024 17:30:57 -0700 (PDT) Received: from localhost ([2607:fb10:7301::3]) by smtp.gmail.com with UTF8SMTPSA id 13-20020a170902ee4d00b001dd7d66aa18sm9884535plo.67.2024.03.18.17.30.56 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Mar 2024 17:30:56 -0700 (PDT) Date: Mon, 18 Mar 2024 18:30:54 -0600 From: Jose Fernandez To: Jiri Olsa 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 V2 bpf-next 2/2] selftests/bpf: add selftest for bpf_task_get_cgroup Message-ID: <20240319003054.tegzffeydaec45ql@SlimPro9i> References: <20240316162241.628855-1-josef@netflix.com> <20240316162241.628855-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: On 24/03/18 10:58AM, Jiri Olsa wrote: > On Sat, Mar 16, 2024 at 10:22:41AM -0600, Jose Fernandez wrote: > > SNIP > > > +void test_task_get_cgroup(void) > > +{ > > + struct test_task_get_cgroup *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__open(); > > + if (!ASSERT_OK_PTR(skel, "test_task_get_cgroup__open")) > > + goto cleanup; > > + > > + err = test_task_get_cgroup__load(skel); > > + if (!ASSERT_OK(err, "test_task_get_cgroup__load")) > > + goto cleanup; > > nit, you could call test_task_get_cgroup__open_and_load I'll rename. > > + > > + err = test_task_get_cgroup__attach(skel); > > + if (!ASSERT_OK(err, "test_task_get_cgroup__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); > > would smaller sleep do? also we have our own usleep (in test_progs.c) > that calls nanosleep Yes a smaller sleep should be fine. I'll reduce the sleep and use the usleep helper. > > + > > + 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__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..580f8f0657d5 > > --- /dev/null > > +++ b/tools/testing/selftests/bpf/progs/test_task_get_cgroup.c > > @@ -0,0 +1,37 @@ > > +// 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; > > + > > +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) > > +{ > > + struct cgroup *cgrp; > > + u64 cgroup_id; > > + u32 pid; > > + > > could you filter for your pid in here like we do in other places, > (eg in progs/kprobe_multi.c) > > in which case you won't need hash map, but just a single value > to store the cgroup id to > > jirka I'll apply this suggestion as well and include it in V3. Thanks for the feedback. > > > + cgrp = bpf_task_get_cgroup(prev); > > + if (cgrp == NULL) > > + return 0; > > + cgroup_id = cgrp->kn->id; > > + pid = prev->pid; > > + bpf_map_update_elem(&pid_to_cgid_map, &pid, &cgroup_id, BPF_ANY); > > + > > + bpf_cgroup_release(cgrp); > > + return 0; > > +} > > + > > +char _license[] SEC("license") = "GPL"; > > -- > > 2.40.1 > >