Received: by 2002:ab2:7444:0:b0:1ef:eae8:a797 with SMTP id f4csp31614lqn; Fri, 15 Mar 2024 11:52:55 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUhXowd+hSBAHI5wNq+uCoWx36ODYW9foNwIgCT//gnl/ODWgYh+Fkky9wy4e5JKJGXAyxCkGIRvF5HnuWLt5zHEqQ3/dYEfTZeVB9pcA== X-Google-Smtp-Source: AGHT+IGjRdx18TK0ir4L0zAIFgoElcxS8VMa/pgeUp4x75RYeUDK+eKfc6bAJsFEptYbbk/Y0Ukt X-Received: by 2002:a05:620a:5d95:b0:789:e6d3:dd28 with SMTP id xx21-20020a05620a5d9500b00789e6d3dd28mr2259348qkn.34.1710528774735; Fri, 15 Mar 2024 11:52:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710528774; cv=pass; d=google.com; s=arc-20160816; b=SM6eLWKjgb0dApRzNFbleT3tHNmK7yLEabasv2/csn0Y/RuPl7BSKk7Nmqv/iGVr+j 4mcbhtSrneKLgZZXIP04wBV6SPgB6CkvKrJa8+AZ2X23FxLGP2vVq1Y1VRFkzakN1V/X NbcGC/utcvBvB1N2PzGFKAvCxcgSJKHaUktUEFzBjmiIQZ7bZsOXabTdNBQ4smW2s3u1 +lh3ZCtq4/MFbGqHy9l1+p3B3RbXTejsUkB5P3FHlaYGn1UCotnJHhcFOcR98giaHpG1 LOkrbAyvYaCnQECj7Uv4bLUnESphlRFmh0SFTuoswLHqXSzvuTbjba/RP09v4rVHKHfh t5Tw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :dkim-signature; bh=DtYC38JTkqYWVAKyPrGYPSxbyTCY8qOTTr3Tw0hY0wc=; fh=AMIH0mc65mg+gj7AYJAFIdAl3WkkdHDGNqvEfJ4RLdk=; b=bEDg2h3VlOv0Z5YMbTf4C1JXvApDikOaEXeA2BpU0JHhPQsbE1mLIqOr9fp6p+dh2Y SCl/7XsZbe2tKUTg6XgkiyhqKvQuAADotTVWw+sYiMYkOWTPgeGDUC74ZWDuU4oxK6V5 HqGKqXqNr+Qcwy4cUEUX8e3xSf0lb8wO2MgpKbdsCSNj1ofYR6KYIKR6afknA89gtXOG 4X+SBfNcCX1Gb0mmnWVgaqModG901/NQcLYt3oa2mmpv8oj/h5FPIgYhH4WchETaARfa YIE2j/CzcQlwvfzVnfT4+Y98O4Oew8XLfF3xaXyYaXJrMHekk+ytbNuYf2wERBUPgRWf k4Sw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Me6YpGFg; arc=pass (i=1 spf=pass spfdomain=flex--sdf.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-104815-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104815-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id y20-20020a05620a0e1400b007881bba9466si4296559qkm.64.2024.03.15.11.52.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 11:52:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-104815-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Me6YpGFg; arc=pass (i=1 spf=pass spfdomain=flex--sdf.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-104815-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104815-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.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 649FA1C2261D for ; Fri, 15 Mar 2024 18:52:54 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id ED5E43D0A8; Fri, 15 Mar 2024 18:50:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Me6YpGFg" Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 2C69155C33 for ; Fri, 15 Mar 2024 18:50:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710528654; cv=none; b=fasXUAuE7+SKyWdGHYmN8+Mv8Jxqo8VV1cvMlNfbMoJm/nSbsujGNWhiyrtZtMBdafE7kfmWcPFbF+ScGtRDMY9A4r6DX6tK7xt+NSX+K8ZYsgf/Y7VCclhoQDthDN9X1MWuPDdPTd2mzS2R3g/WurXgHWaF8QWVzqRkGVhSQ6U= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710528654; c=relaxed/simple; bh=lmLbRKyghwboNWtEfGotXrpbpxUAQ3nWSRJz7Qjgis4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iPtp5Wh7e54u4qjE57iCQorx2dv9o+InVbOh+FIUmLaC36NCm6m00MUNbf5UjLWjRV7yWlgKAib5injdw3INjOgOxOCRbI87QzUMjZQAp6r5Js2Ush8YooUN1QPa1J9Uhpg5Wq6b9qXlTRGkrIfx0m2aRLt2J3tM8GvyY4R6DRo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sdf.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Me6YpGFg; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sdf.bounces.google.com Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-5d8bcf739e5so1846504a12.1 for ; Fri, 15 Mar 2024 11:50:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1710528651; x=1711133451; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=DtYC38JTkqYWVAKyPrGYPSxbyTCY8qOTTr3Tw0hY0wc=; b=Me6YpGFgLPzRDHruPgCnsdhJ68KCQZHfI2goX8dddUqMBoEla5jRvOdvp0wwKedQ4c jCHjgxtcdXX580dtlrqnIEUQIe/E0axvVD5uR8iDREoLJ25c19SMgyeQHM/XmnNqgWMB tWOGGL+IpxpdRvACN4b5G1h41TaBWMvP5ckTuhOZ7hfS8mpqdUk2HAeVeyw8aFWVFOwc gt85LrJKG0527y1qwG5ifEsPyP4mA/HCFYHBGvHUi2NdLVQppvhYhBlg1QFEDxkG06g4 bGzJYZ5dPYmEdCXOfgd00WYcduQpU+adLCz4pzLyF+UgxvlHpdR8olqJbaE+/rYvHi4q 6ogg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710528651; x=1711133451; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DtYC38JTkqYWVAKyPrGYPSxbyTCY8qOTTr3Tw0hY0wc=; b=qHKKqI915OnrcDXTRyKgvHfmgGXmM84MeOsXYAUXLw19arqHZ9dj/FGVYT5zBzQS4g HLw8JjV6MxTOGOoI//uCJctVrYsf5hvfBCmvYRGbgJi83EhGRFl88QkTlniNrd2sGZg0 ImYSzhuy3SHuKObe9SGj1teXWowFA7SN2zE702dtTRHxzboqE6pZAli6kExfmOSCnFdz lYwyVxaBGbQC6lSp/FzC1dW604WAJqczQWkazqJ/LNeFIxurqvgx51uj1o5FsqW98GaG b8kSG0xMlXBL6p2poZtEETaEu9wiI2iBIZARulXj3wE/z/tZgF+1dYjh5u2LszX8ijwd luMw== X-Forwarded-Encrypted: i=1; AJvYcCUB8Ok+0bwtuYroIRcTDXRejX/BiOxIxnQbCR8Pp9gjt51ZZ90wfv4jV6XpQSFkE2fY/CxmKJ6NQu0sm+McSPzgdwGmeehQqxASfLKJ X-Gm-Message-State: AOJu0YxZ/PKqWtlL3drJk5GzsRNQURWffB87COwyNaGUVGdarwTBl/e5 Bz+LpJkcVW6ssFzZ3NpXAw08gGg/EoXYdicTNMmMf/Q6/mO6liV/1TlOkJh61FE7OA== X-Received: from sdf.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5935]) (user=sdf job=sendgmr) by 2002:a63:534f:0:b0:5dc:832e:d805 with SMTP id t15-20020a63534f000000b005dc832ed805mr13318pgl.11.1710528651355; Fri, 15 Mar 2024 11:50:51 -0700 (PDT) Date: Fri, 15 Mar 2024 11:50:49 -0700 In-Reply-To: <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 References: <20240315182804.216081-1-josef@netflix.com> Message-ID: Subject: Re: [PATCH bpf-next 1/2] bpf: add btf_task_get_cgroup_id kfunc From: Stanislav Fomichev To: Jose Fernandez Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Martin KaFai Lau , Eduard Zingerman , Song Liu , Yonghong Song , John Fastabend , KP Singh , Hao Luo , Jiri Olsa , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Tycho Andersen Content-Type: text/plain; charset="utf-8" On 03/15, Jose Fernandez wrote: > 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) Any reason we are not returning 'struct cgroup' pointer? That should allow using other kfuncs that are all 'struct cgrop' based as well.