Received: by 2002:ab2:6486:0:b0:1ef:eae8:a797 with SMTP id de6csp69438lqb; Fri, 15 Mar 2024 15:42:29 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW3LPFmcPGQx/Ml6aHInpq5ioF/5Pva7Udbtcir4Cv6OBxv1PQM5e2tjouWxEd85H0lDMM58hS+SYEBdd7Q5fRmP05H7Cpqo57XVT+zMg== X-Google-Smtp-Source: AGHT+IEGe+YmUED9mN5CsBbC4V/cShEUIOx9yGHlQRy+YOl16xo9amM09EM6jmqA7vntOffmJBED X-Received: by 2002:a05:6808:1289:b0:3c2:520a:c791 with SMTP id a9-20020a056808128900b003c2520ac791mr7924987oiw.8.1710542549312; Fri, 15 Mar 2024 15:42:29 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710542549; cv=pass; d=google.com; s=arc-20160816; b=D18Kbv1eB7DCfP/1QXaeCnFt8aN0pfr90gRmPTcEzkXVaT8F9x7qUHAHHkxEgTDWyu YRsbWrw6cqjxJmNFc4pUNMJ3TYL5k8JFDbAIBX7JmbooCRAsjUoimGpb9tEzIxgiukfQ xfRWcfPLpvdGxlovJmJ07Ss9FcwQyYXbnNAdZxFDOkCxCqztHZVA896CN770cR3ZePdn Hz6O1/bHMBVH5sI5OBmT8W9dJmksXGH9HJLxwqlIN2jrrLTpyAULC2SFXMgA4W50/2OM RGlkIa/xf32dpXCPqy1qhIwfPhSFkaRhrvrKLSYPPVWFmBWC0no8YuZEFg2RWfRwU3Z4 JTYw== 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=LsRqTXgMaYQKprnwZS46sKtunWtNyYt/xYWjCFRYquc=; fh=nCg5jteudkE5NYmjusQOythoIZETX6RjQcYecUXfyL8=; b=r6BdtEpoEC7qf8cEji5UZB2wWEV3dnRL8fU++TmRuY/Rs8eZ+7mbfXtHYZWStTuq3H DCrRBNHwWnIjq+tqJf0b7jOBWX/NVOaFqznAoCG3OYl5gwLvayHtaK24d5kCnq8bVRb/ RYQW34uWj5ntNgFP3uklMK8ZhBopb/EtCkWpUMKKTsDwqPPweAOD99I1xGPmYPV+chht 9B/6/mgcGUyMb2p0UM8dCT9DkimOjxUpD1DUQ4mAQG3U3hXMy/L8F0loTCJm+L6fP/to xtyQNp1uMWGbpftADK9ZDlBxwkesnFO/iQIvoaRPWcdP51Oyzs5BjRGvNfxoOX0hAUPV Aw/A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@netflix.com header.s=google header.b=NuG1v4kN; 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-104969-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104969-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. [147.75.199.223]) by mx.google.com with ESMTPS id de7-20020a05620a370700b00789d99cc609si4438620qkb.549.2024.03.15.15.42.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Mar 2024 15:42:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-104969-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=@netflix.com header.s=google header.b=NuG1v4kN; 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-104969-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-104969-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 DB2F71C210FA for ; Fri, 15 Mar 2024 22:42:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D037759B6C; Fri, 15 Mar 2024 22:42:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=netflix.com header.i=@netflix.com header.b="NuG1v4kN" Received: from mail-io1-f50.google.com (mail-io1-f50.google.com [209.85.166.50]) (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 C3ABD59B67 for ; Fri, 15 Mar 2024 22:42:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710542539; cv=none; b=arDb1EiPA1ZDy8u+SldODf8zMSqw20G4xCCuGm4OEWjjvcOso1Bw2O8yjQk5WrpLuXTBL2kWvTl2/iqn+vMrbW+S1T9BWlYBaCKzdJUzOFgrtFRROkaefFPCVWpHfT2xCvrRj3QknmqNTAWnCk8pNvyGyGjV3opXlsL+NQCECDc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710542539; c=relaxed/simple; bh=9aTC8xKicnal5qtnDL3cjqNci//gvsNOASreQ2FWddk=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=AAbgYwmvxWyWORAY2rwOl/gDrfV5wXt3H3cXmdnFL7dPAWybnh426KpdE1j70acJJPxgHtq82XZxmBQABmtHOJpQnMR1J+/KHA1nTikQWj1l5uHwbXaNaMY3i5rQkNF6ho6MSEwrlsY4mPUAZ+afAHL4yxozwh1EimIv/90BBCI= 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=NuG1v4kN; arc=none smtp.client-ip=209.85.166.50 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-f50.google.com with SMTP id ca18e2360f4ac-7cbfa0796e8so36973639f.3 for ; Fri, 15 Mar 2024 15:42:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netflix.com; s=google; t=1710542534; x=1711147334; 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=LsRqTXgMaYQKprnwZS46sKtunWtNyYt/xYWjCFRYquc=; b=NuG1v4kN0tBNM8FYruKU8M5f/mqN01+dXs/ZiwkD3SyRgzyIcBPzyUKEiCnzcT5Zhn Gnnbzhlio4SeWTNsxFlj8zgWvLreRGORREl4v+nrDWiUbAljBMiyD2WekGJWqleN6bHo 42CwjqS6jRNLV9etWkx8479k0SF79AMR/q6fc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710542534; x=1711147334; 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=LsRqTXgMaYQKprnwZS46sKtunWtNyYt/xYWjCFRYquc=; b=e4gojX6oKzLz5LnfKhcKCD4rxZBENp/7HI6zjMD1CyxN6Qcu+6Aw/6AFbulKPfCLB2 O59AQ5qZLi4uyRD6dIpbMGvGhQBs3bG32Nywgc9NUYhxnoj2Uep2/TGpID+24KNJ5/nG G9tb17MIEmLVJS3uDFOFi/GjJoIWyE1tbZRG/pb7OvGjbqNEI6b85Y79RdCHbqkA3+6s cdNYnJX2NY7h3D56+v3yxyMOLTD8erWnNle0vMXnPuvhok/WKM5EN+nP81Dqrm9KF1B+ cJ/4TE4QDGhB7RbfMO3apCKYFia8MIb8cVV3cNdFA5meukDrfTySIDjF3BMZQ0Up10KY 51hA== X-Forwarded-Encrypted: i=1; AJvYcCV8j+ZsB6CJRglE66NurZ110LeAe19gKABohUQQWXjgP5X+W1nkW+lqsb6KtbA0i+IJSumPc91SKtQ8ZcZ2tmU2EPzoDKNgi4tk/xkJ X-Gm-Message-State: AOJu0YziE4y2nIS7UxIrrz/SWweC/rS/3oiirIKEkBb6a5fj5ktMkuB9 GVBHdxOF5g8hXNzsGX2J4kleF4oV9btesaTpWrgG2b0lwb4uk/+XPCqb2jZGxds= X-Received: by 2002:a6b:4f17:0:b0:7cb:f1d0:a1cc with SMTP id d23-20020a6b4f17000000b007cbf1d0a1ccmr3704914iob.4.1710542533892; Fri, 15 Mar 2024 15:42:13 -0700 (PDT) Received: from localhost ([2601:285:8700:8f20:80e2:186e:c8ca:d2a2]) by smtp.gmail.com with UTF8SMTPSA id w25-20020a029699000000b00474350caf1asm958836jai.175.2024.03.15.15.42.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 15 Mar 2024 15:42:13 -0700 (PDT) Date: Fri, 15 Mar 2024 16:42:12 -0600 From: Jose Fernandez To: Stanislav Fomichev 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 Subject: Re: [PATCH bpf-next 1/2] bpf: add btf_task_get_cgroup_id kfunc Message-ID: <20240315224212.sinyco7bxbnlgw2e@SlimPro9i> 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-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On 24/03/15 11:50AM, Stanislav Fomichev wrote: > 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. Returning the cgroup pointer would make this kfunc more flexible, agreed. My intention was to make the kfunc more user friendly by returning the cgroup ID, but I can see how it would be beneficial to have the cgroup pointer as well. I'll update the patch to return the cgroup pointer.