Received: by 2002:ab2:6486:0:b0:1ef:eae8:a797 with SMTP id de6csp418417lqb; Sat, 16 Mar 2024 09:23:15 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWK+2W+JmoCKkK+XgnygxVATjuOPWW2tg4jMORA20lF9htMGGpMD11EtLZ+eiNVwR38waJq1ZEZxB7k/MoBMtcDvrsx3vJx2tEdfuvHuQ== X-Google-Smtp-Source: AGHT+IEfVSHvUexd+5hrX29EZhEiKGZG2HPngHjMvSkWUEpuGRRV5VzLxbRjQzsRv+F+CpZH99nU X-Received: by 2002:a05:6870:4349:b0:21f:a11a:e251 with SMTP id x9-20020a056870434900b0021fa11ae251mr9253002oah.5.1710606195333; Sat, 16 Mar 2024 09:23:15 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710606195; cv=pass; d=google.com; s=arc-20160816; b=Op/n9vWF+cgCSbajvD0knH7/D3Olvy+5MDaOOWtSS8EZzbtPM6XEzjxfMsbEo6i2GW dQbt3Sjw5n09A3LWalnwSD1Kx8yJbRM3Ln2GhCo5bx/pSP2+U6eDxT7XQGipgr9Vy6DY p/Z6xJEeGLmJeNLHL60T3v8B8GJ7FDBu8ARA2Y10HAj2FJd6u152ZJ7G6Nmv5awfphvd KXM71OpPLTwd5vfO82e2lBndq7BVyPov2r1uIyaDFqKlGzKwcjv0xtGLmWWxlT0QwL7o ya8hIaPZ6MLCo5d+v4l7styMjkZf+96boYKO+73iYQaDhGL8amEAOe2/rskDCGSQptop JSCg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from:dkim-signature; bh=Foqi+aqcfZXTaDDszcOGG4bLdYmJsSqibIZG875f+rM=; fh=GmkHiX7gNzlzr/jmV2sNez+Q7Sfq12Ar59PwbkAt0AI=; b=cMAH3u1ZUQhz/NFM/73XXC9wnWUOLfUGuAt5SJzDmciizxLDH6Zv1sTP3hcBR13uWg yN9RRNd94Y80yGYnphlEk0nv1tT/cgWOHHFgfhjseANAPCps5NnqaK/ZsPxqESi5ifP+ Zoylbd4KBO4TXIBz+F4iLSR5EsaW7IzHPpZnKa6dkNRUZN39jhDZjjT1yUtSyy1duHjr zax5Yc9JL3MFsK+uXt8WwfecLWpR2kskpDO7IgSSnktt4pRfpOY9R1GcoAM0UaLaodEz jIj30OEZ4ao19Up0jZbZm7x7ANzE/m1dTMSIsBNyF+t/joxjkr0bFotA9RA3CyL0MZZB UdqA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@netflix.com header.s=google header.b=REEodlIJ; 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-105227-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-105227-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=netflix.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id d23-20020aa78697000000b006e6bfd60715si5605468pfo.18.2024.03.16.09.23.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Mar 2024 09:23:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-105227-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=@netflix.com header.s=google header.b=REEodlIJ; 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-105227-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-105227-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 sv.mirrors.kernel.org (Postfix) with ESMTPS id D3BDE282122 for ; Sat, 16 Mar 2024 16:23:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AE3F71BC58; Sat, 16 Mar 2024 16:23:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=netflix.com header.i=@netflix.com header.b="REEodlIJ" Received: from mail-io1-f41.google.com (mail-io1-f41.google.com [209.85.166.41]) (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 01CE01B969 for ; Sat, 16 Mar 2024 16:23:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.166.41 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710606184; cv=none; b=nQCb/ShB4Hq+FxiVRqQWWh7s47tHSAQ2s08z+5KrBHE8yGeBjoKgqPfPladBLoWppi1+uIQ10VsLk5FHRIRdH20gqPV7nAHRSCmTSlP9s/c2voetYp/XkhrmAHRlbZjwawL6L/r07uXABxQJNgfafIrgRPaP2ma2pi5lmZa4Vl0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710606184; c=relaxed/simple; bh=m32Ko5crU9F2s1OcBQcoiBz2kFB3BBOwrX7vUOXoXDA=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version; b=gZXcpO3CYnJyBIG0R50mZcBcJ5J6GDwqzs751n4nXNCCK7gpM5WZBKzw3C14pfcZx+DZ+9Rfy4+NygEjXLoP1HmoOizw0wckKgVL53i/yjWPbJ9jKKRZ9sVbMlptpCl0lb74t8gZLnXOUFtpbTwJ4eAx0NdncJeIMySLvkbcjdI= 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=REEodlIJ; arc=none smtp.client-ip=209.85.166.41 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-f41.google.com with SMTP id ca18e2360f4ac-7cc0e0dba0fso8397339f.3 for ; Sat, 16 Mar 2024 09:23:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netflix.com; s=google; t=1710606182; x=1711210982; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Foqi+aqcfZXTaDDszcOGG4bLdYmJsSqibIZG875f+rM=; b=REEodlIJj2z7005lcKAisTo/7fZLGIN442kfRFN4rDg8XC6ExVRiHgvuwynJqGdlJC UyCU9AQXum+AUE3XQNROfqblO9DXLD6RMk86AB/ziVku0in0hcdm9HEFzxRfp5SzkpCv Aj6Cmy0UloC0GhW5PZI5Hwi9PeQymTS1HIk08= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710606182; x=1711210982; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Foqi+aqcfZXTaDDszcOGG4bLdYmJsSqibIZG875f+rM=; b=Zb5ZFv4HuJ2v4sTO841T+6qwGMzlJtWahjL/RozmWBtMats0ECw3e8VLIEMqeN7I5z d24uawGCNfFpClmF9B7Zotp2NN6A0XZQARTTGYdgaXXhJyoarLOeWoVW7EiGBewBFI0W dXmmzCG3pwF0n0QOGMqCwtiKAl0pb4dfiAqFqB5PCq5ndx5lVWuvfuTHnxLDvvEadl5J QyKzhqZYHaI3R1HaDAMPcr+L/f1+hg2UynkRfmH9MtCLGEIC1X6M7hRv62bvYZA+wh9+ XmjXXRDF9wmcawc8VG8CCcs7kFRnkSkF5Cr5OqppFYcJ+teunCZmcpswL45kwEaJalwR Jnkg== X-Forwarded-Encrypted: i=1; AJvYcCVpoRB3owiFsvvihEJQA4HtunYhd5H68oACYRmK0y73J5CxDH7w5h/CpD9mZudvd9VIIzc4b22hPQphmlyP95cXUcc8I2ye9kTuM7+U X-Gm-Message-State: AOJu0Yy3Ry+xsZn644aDN1D4W03wYf7Sw3CAJiM3Rvk4qaQXi4ZxLXHL /l5ugTfEFrnLZ4abDpkX96is/1BlxAId5blIGnNdl+WUWRL24yyjBsozmhJTP/E= X-Received: by 2002:a6b:c801:0:b0:7cc:5d3:3c9 with SMTP id y1-20020a6bc801000000b007cc05d303c9mr2585462iof.8.1710606182114; Sat, 16 Mar 2024 09:23:02 -0700 (PDT) Received: from localhost ([2601:285:8700:8f20:db82:ef0d:e122:37fc]) by smtp.gmail.com with UTF8SMTPSA id w14-20020a056638138e00b00474d1b1590dsm1353203jad.133.2024.03.16.09.23.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 16 Mar 2024 09:23:01 -0700 (PDT) From: Jose Fernandez To: 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 Cc: bpf@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Jose Fernandez , Tycho Andersen Subject: [PATCH V2 bpf-next 1/2] bpf: add bpf_task_get_cgroup kfunc Date: Sat, 16 Mar 2024 10:22:40 -0600 Message-Id: <20240316162241.628855-1-josef@netflix.com> X-Mailer: git-send-email 2.40.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 returned. + */ +__bpf_kfunc struct cgroup *bpf_task_get_cgroup(struct task_struct *task) +{ + struct cgroup *cgrp; + + rcu_read_lock(); + cgrp = task_dfl_cgroup(task); + if (!cgroup_tryget(cgrp)) + cgrp = NULL; + rcu_read_unlock(); + return cgrp; +} #endif /* CONFIG_CGROUPS */ /** @@ -2573,6 +2598,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, KF_ACQUIRE | KF_RCU | KF_RET_NULL) #endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_throw) base-commit: 4c8644f86c854c214aaabbcc24a27fa4c7e6a951 -- 2.40.1