Received: by 2002:ab2:620c:0:b0:1ef:ffd0:ce49 with SMTP id o12csp1320857lqt; Tue, 19 Mar 2024 23:03:43 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVGTuJ/W34VZ8oKwZU26FHcjtLz13AVeVpA5CauL552Synvd2PCGyqNnLmgwMEs9EBmKLvvOhSp/KtdpqXLEtyOlQiP158Ne43R7Kcykg== X-Google-Smtp-Source: AGHT+IHirKEcOgH641+xG+m3B0IbRZiFOrV+wQGp3+wlQDsUe2D1J8y875kwpxf2YBInPvw4lyZV X-Received: by 2002:a17:902:b705:b0:1e0:59f4:a776 with SMTP id d5-20020a170902b70500b001e059f4a776mr1024882pls.13.1710914623085; Tue, 19 Mar 2024 23:03:43 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710914623; cv=pass; d=google.com; s=arc-20160816; b=rOlwTesPYMdrkHIRgxAtkoVo1q/J3ZimuAhwXvNsyxdDPIqUWm6TGYHfjghcTsK+66 C6NOojnhgInszuRPIhsmZv1V2qfTgenQkPdeTWpq6Lxl1jQDRZfmNUGrkQVjCEKAV/+W SRJ9H2kFB3pbGd8DiXVK6Sz4LNZNup1Opl92ErnT3w691jhA5JufaQ2O/W0pf/+WImrZ YL+EiN2jg1P9LaYhtZh0gmbYmKs2DvecMXtGprsLMRWoyHxXREIt4W75bemh6BhblB/H V0DnQVSYlLzXnXt/HSUCjDm8Uwh8VBmIwxvuSNHK9OUTrq6Pf1nB7OMuykzEzgoYRmvN GoNQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=ao8qG1eSPb4R5Tf5aIIiQnLt3AJxqdYQvtHZM6vC9CY=; fh=dO1pydhS09QzVl3hI1YjSqMHBCXgbmSf1MhE+eOHF20=; b=ARFwOf8GMKH2xmeNQu60TsiW9Qy2sJDygf+HKT1LZmjsVMLEyJEa9vFLvUTzEQ2hFP PI+oKhic9R3tqQhqqsbS9ripIsu9yF8NwsvVW4hvcod7dawU7imDAaY6t2nxmieqYWUm uhdeDx41yypCShoxUc3LEk4uoZzBqTbsntWMmKyQbC0323FltDkqmxd+0VjDJeycAKBs 4H41vZikZ1VrwIoeM/eTCdeRy5knKdeQMj6v80YbR8QLmkuNz+JXeU0EfuIA1O2UJDto wDaC7onLz4Nvj0ZRtsWMj0qsk3Dus9s9Jm6Hca8Ci2JrS7yDZRIwBr/HuLS2ZsBfEH0y tCSg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=PH6vwttF; 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-108473-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-108473-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id a16-20020a170902ecd000b001dd78f7e436si12948132plh.434.2024.03.19.23.03.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 23:03:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-108473-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=PH6vwttF; 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-108473-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-108473-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id BE218282D05 for ; Wed, 20 Mar 2024 06:03:42 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5F39A1802E; Wed, 20 Mar 2024 06:03:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="PH6vwttF" Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (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 C9C621A5A2; Wed, 20 Mar 2024 06:03:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.54 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710914609; cv=none; b=mJbIPYwAviDf4QPhygR9QCiG53FM1nZuJwQpJNGPXQsTk2qdSoBsechRCxak6TGQJsGpWfo8MO+N0mp3O3XBfo50Tifj3/RKTJI1ffuP/DoXNZ5U9as0zUhlAZ9ygls2Cy4dSHqXHd+uv0vxdNwcgira0/oLMbqPVNypiCmVNhw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710914609; c=relaxed/simple; bh=Xj+0EJwM7uAPM9IYGHMym6tea0yeShSg9UcqikTwriI=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=SzTVOuNIK0HdtfiaSnU8H+3ZXj5NJ8/g8eIvz6Y2zsMFvgVIhP6rRdS3FS1bI21DwJtJR/tQN4De9Um0GqGXObQ1rbc+8XPcaLBZpzlRfq2cTsRuE0kciOJLs3nksrAmIPfh6Ywd/hQo+BX9oeOETxhmfhQwl6GZACCm2m7ZcQs= 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=PH6vwttF; arc=none smtp.client-ip=209.85.221.54 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-wr1-f54.google.com with SMTP id ffacd0b85a97d-341730bfc46so1779863f8f.3; Tue, 19 Mar 2024 23:03:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710914606; x=1711519406; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=ao8qG1eSPb4R5Tf5aIIiQnLt3AJxqdYQvtHZM6vC9CY=; b=PH6vwttFtq+Qjy+GnG+JelPOdmjZenrcU8KkxGoZkM7ERCqFC9KVMwto9bNPRIqPyF Akv/ftL3ItZwrM3UTI6OtUSGGANuD+V/qvObV+pLXffW+Bq2USEYOF8cn2HG8/H6jaiO QSiqX5eBWKWxyOUaZxPeu3COzFF23PFk94/K7Mb0nQVRYu97oY87HAI95/Z+qSLV7x32 nO/FL+eBdS0RMS5KwAJRJGfZBBbfaSXteeAWUWn/hDeD0xkmsdpU0hD4lpmb1MHDlZaV RS/VejVnd5CwHqgxGdkhiactK2ly7AqaPBj5dt4biG8d5nanq+ikTrjbzY9gdpJzKErB ZxhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710914606; x=1711519406; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ao8qG1eSPb4R5Tf5aIIiQnLt3AJxqdYQvtHZM6vC9CY=; b=ISpBvJab7WrBrKNpJsCdw43Z1tWmTuy+xgQgR/FyT5p/Yy8MjJAvFRZvYoIg3xu44I LUYGBW3dBl5XcNIYABV2GfSZnILAO2TejjFk19s1hYfoSkWXW0i13K75oWnQIQm4ivjB rcicbYLcnj1I8t/Wg8y3J2u13OlKXVOgKUy+tMKO7DTxGe5Uel61NQmyZHY2UO2mpuO7 3M7gEMmFW7nRrgowWLsJiEUMx27s07/McOjko/SysW31LpfZQmcrpCiOcUVJk7YA90tP GqB7UO9nlEmnipECOTt6MmbtXoQxnaZG68Xc857BGQqWkWutgKvkMxj2OLkYkcitfAbw 8JVQ== X-Forwarded-Encrypted: i=1; AJvYcCWJY47Du8+YxZkxyPWZnQpI65ZyI5oHih4zyQbgUtiXWTsZuj2jLsW2Vx4RDg/xTocEH0XwVKJlK+02AYF52GdW9vwtm7vc5piiLrrU2B+HdP+wpdLuLUf/3KBNYWMpvd5CS1L+Bi7jlXU9ZY9PhH9UcpFOcnEEhto4Or3IQnIQzhY4 X-Gm-Message-State: AOJu0YyUvGtmxDUEwwCV/Kkbc3qSDVFrWCioqypnemE0VKsEBtW7/+xi 9kCzeuRAWb37/p2tRVYZOJ6QtH/U/6Ana77UYpzSSYcnhvVvJqkAB+VR30qVVpcyxOImsS0Gt3m q1Lxq/G4AW5NznCeMHAIcq/WlTRA= X-Received: by 2002:adf:f38c:0:b0:33e:7a1f:5824 with SMTP id m12-20020adff38c000000b0033e7a1f5824mr12414766wro.0.1710914605898; Tue, 19 Mar 2024 23:03:25 -0700 (PDT) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240319050302.1085006-1-josef@netflix.com> In-Reply-To: <20240319050302.1085006-1-josef@netflix.com> From: Alexei Starovoitov Date: Tue, 19 Mar 2024 23:03:14 -0700 Message-ID: Subject: Re: [PATCH V3 bpf-next 1/2] bpf: add bpf_task_get_cgroup kfunc To: Jose Fernandez , Tejun Heo Cc: 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 , bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Tycho Andersen Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Mar 18, 2024 at 10:04=E2=80=AFPM Jose Fernandez = wrote: > > This patch enhances the BPF helpers by adding a kfunc to retrieve the > cgroup v2 of a 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 prev and next tasks. > > The bpf_task_get_cgroup kfunc acquires and returns a reference to a > task's default cgroup, ensuring thread-safe access by correctly > implementing RCU read locking and unlocking. It leverages the existing > cgroup.h helper, and cgroup_tryget to safely acquire a reference to it. > > Signed-off-by: Jose Fernandez > Reviewed-by: Tycho Andersen > Acked-by: Yonghong Song > Acked-by: Stanislav Fomichev > --- > V2 -> V3: No changes > V1 -> V2: Return a pointer to the cgroup instead of the cgroup ID > > kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++ > 1 file changed, 26 insertions(+) > > diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > index a89587859571..bbd19d5eedb6 100644 > --- a/kernel/bpf/helpers.c > +++ b/kernel/bpf/helpers.c > @@ -2266,6 +2266,31 @@ bpf_task_get_cgroup1(struct task_struct *task, int= hierarchy_id) > return NULL; > return cgrp; > } > + > +/** > + * bpf_task_get_cgroup - Acquire a reference to the default cgroup of a = task. > + * @task: The target task > + * > + * This function returns 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. > + * A cgroup returned by this kfunc which is not subsequently stored in a > + * map, must be released by calling bpf_cgroup_release(). > + * > + * Return: On success, the cgroup is returned. On failure, NULL is retur= ned. > + */ > +__bpf_kfunc struct cgroup *bpf_task_get_cgroup(struct task_struct *task) > +{ > + struct cgroup *cgrp; > + > + rcu_read_lock(); > + cgrp =3D task_dfl_cgroup(task); > + if (!cgroup_tryget(cgrp)) > + cgrp =3D NULL; > + rcu_read_unlock(); > + return cgrp; > +} Tejun, Does this patch look good to you?