Received: by 2002:ab2:b82:0:b0:1f3:401:3cfb with SMTP id 2csp85943lqh; Wed, 27 Mar 2024 15:56:01 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVFrO0MEE3R9avhJqvMyjJlzRwgDvzzm4xmg0huBwoPZs5W8G5o7QnpNrQ1r+DGMiOJi9EFnTaJF/pFQEMoaXCAyMU3x5t9f1ZdAN9fFQ== X-Google-Smtp-Source: AGHT+IFjapUhAZPl4NOIfLq9EfO2/W4vyvGl0ts/l2FojUj1wd9GoJGPyIoiWXBB7GCf5HJcHxtn X-Received: by 2002:a05:6359:4113:b0:17f:30e7:9b6c with SMTP id kh19-20020a056359411300b0017f30e79b6cmr889640rwc.11.1711580161099; Wed, 27 Mar 2024 15:56:01 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711580161; cv=pass; d=google.com; s=arc-20160816; b=WWg/xCVdysv8hnFxlnSPcivKFEZ/LEpDvqXwJDXonEt3dHsIhY5+9QZ+hQEw7CuhUH kJZzhMuj8Z8PERnagKQ6X+a3ZiQ9dmGtzvTLkTqUQHOFipjPdHT51LNbFhLXU6JDjyQ4 5z6Ev00JjNPCRRRVkqFJ7Pkb+NLA6G/8GFtGUDrZyp+LZLOlZDLa5jCUs4IqDPle5X4n W46NEVeCo0XJcu8CAuBRmHIEx0+ymUgRXwWVO8Nprii5ck1hV5wkGRMMid0IighIVcOq 9qk7+3KmcADtj8UeVVYtiGS7g0gdnHA/RGjUMpEU6FBOwD9wvjQmwq5y/+PMvB790Fo9 fejQ== 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:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=v5+m79uhLEXdVoaBrPdTY3BAK3Wv4St0V6/3gWbwJOI=; fh=TdWY6ZqMCyb4/HRhFbveaZ4wSmA7NLKw9ifzArXRG+8=; b=BVe8duMUT8pyn2sgrPGqgbGfrdPk3x81bzsMpAnvVzKMKAVVdj5o83d9gP7RatTjGA 5C9yCF7LHW3JEojzDEFAFRRo+LjNIrrmgLtgM1jl9iZmN6n+AVQXC36vQprOONuoQZA9 xOuYuPvDB0KkW5Rn2S97+4y/XWIM77h32sk2o9BxtYONyPcnhYIqimot/f0ay+dZmaPS GJfUGxRxov+I9VP2Mo4e5sfOA36TUgkYvXLpQou99hhjO/zUGgpOcoc9Nj0ET0DXm28V nGOviORVKvcdPkGNCM2HYSEnZMnja5GcAr14FRQ+wBDjDOAYGAJ05b/AbkETNJHFiwem Ixgw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="W/4JanH3"; 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-122129-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122129-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 f10-20020a631f0a000000b005dc1b73dd16si79321pgf.372.2024.03.27.15.56.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 15:56:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-122129-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="W/4JanH3"; 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-122129-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-122129-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 A7FF129CA01 for ; Wed, 27 Mar 2024 22:56:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4CB4E155300; Wed, 27 Mar 2024 22:55:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="W/4JanH3" Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) (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 B3B8415445D; Wed, 27 Mar 2024 22:55:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.52 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711580121; cv=none; b=t1ePngiNRKxxw6y1dAG9OxypWgVPc03P5QCeNIVqZE1xbgK9LZUgnvMxNVxxFgnE/WdP4FUZKFxAA1CpP112FTtWl1/qq8of1e/TMyWiS2KGO+eyTSzRR9KEh46jFOfxFpa4bbKiT+ES+s/KrHlA2da1ei2y1vsU65yZgNRNesc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711580121; c=relaxed/simple; bh=dMyOthoNkHLVW5M5sdud8Cz8ZlP/s0vnSETYelLAw/M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HcxULMeQBjQVeCrkDCfFGrOxZyk2pZngX7vGr3BrMi9Vls5h4YjO//54a3DuQkD1ffK6YkRZzfdq0oF4hwg2qB//uEfG5jtBTPkPLcj36wS1r7lTouVX5YoPB+HAWBQi6TaE+x9wUkRYUYrQCP0s+BmGxNqVKihxu1L6rAtTifo= 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=W/4JanH3; arc=none smtp.client-ip=209.85.221.52 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-f52.google.com with SMTP id ffacd0b85a97d-33eee0258abso166515f8f.3; Wed, 27 Mar 2024 15:55:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711580118; x=1712184918; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=v5+m79uhLEXdVoaBrPdTY3BAK3Wv4St0V6/3gWbwJOI=; b=W/4JanH3d4lVPe0HI+t5Mr+de+59mxYR7cTWgcyLrm5PoiicxSQGFnrtug+Y/i878U Jyj8n/PyDXx3YALp+/vcI+Z+PBnPrbtHAzhL+iFvkeu3FCxYJMElEZe4vkPIPWH6y2Sn WT11TNx94dSH6jUOALjhnYfp4GSBHQ/mnfd2b54V65O0w/IcHnGRuxHaIDp7DmwFjD2a IhVHQTG2hL2fpk1SgSFgVvMq+b5SX5I6Qsini0Zg0JrvPlwxZBMnMZwZZR9XHLBEA4pf xjuks767ibMxZFJEdrkiCjOradY33QQO9bZtnSYgxW7ZHoaUkxc2OivhXVDxUrZ+u18R ygNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711580118; x=1712184918; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=v5+m79uhLEXdVoaBrPdTY3BAK3Wv4St0V6/3gWbwJOI=; b=LaTpI6om0PwNHNCgMSK8imhBYcj2N3cVwCr2Fn24Bf8T0Sv5Q3n0Y7V37NnGgSWjMc STqwHureYfBolPln+NTUeHaVz1z0DlrfWPYsY8gVd/D8UXCCV5ME+KcMAjFdJlLzqnlg Nxm8W/vLkVHkq0X2iIfyGG7DhHQZy+NWIl/QHDdugKcn4UrIfDOqLuANmlyEANO4q90B OWgFlAUVlH4B8i3TOEHhxj+wzeriEGGLF90tGCdBb9pP1KyDYVZCARSjVORbqM8VM0WH 2Ty3s7zRBd6pMBmwjg7GYGdocmiAEFj3B5nX041PQHUWhFoGnvFtToRguVtoDZMdQHBE W/zA== X-Forwarded-Encrypted: i=1; AJvYcCVx3SZwK4p3bnLSwn3MvyoMKrakM7uZ1FUm4zd0dtq8G6z3kLxcidKpFU4WYPOb8B8bByEcn4oV4E7Rl56P6w8mntYy67x79Gpj1jO/FIdgUNEvIq/O8ZZ8ks+dCuRTmXdq6dH07s0tyg62tyTJtUWUyQRp8F/JTiMCysYA0roJ/LOE8Au21v892xNsYOj5SghZct5lSzCm5dLEww== X-Gm-Message-State: AOJu0YwQUM2sbA9h0AdKBzM9TC6mVUK7C3unU/C9CRsOaY7SJEg1rEvy QHe2wmxFoKd2X1k4JI83zGsz6jxMFTGWaoyIw+oxbVlcba++Azgg X-Received: by 2002:a05:6000:400a:b0:341:cf9c:1c43 with SMTP id cp10-20020a056000400a00b00341cf9c1c43mr919781wrb.43.1711580117876; Wed, 27 Mar 2024 15:55:17 -0700 (PDT) Received: from rorona-tty.. ([154.121.114.3]) by smtp.googlemail.com with ESMTPSA id cc7-20020a5d5c07000000b0033e75e5f280sm152348wrb.113.2024.03.27.15.55.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 15:55:17 -0700 (PDT) From: Djalal Harouni To: tixxdz@gmail.com Cc: Tejun Heo , Zefan Li , Johannes Weiner , 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 , Mykola Lysenko , Shuah Khan , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, bpf@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH bpf-next 2/3] bpf: add bpf_task_freeze_cgroup() to freeze the cgroup of a task Date: Wed, 27 Mar 2024 23:53:24 +0100 Message-Id: <20240327225334.58474-3-tixxdz@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240327225334.58474-1-tixxdz@gmail.com> References: <20240327-ccb56fc7a6e80136db80876c@djalal> <20240327225334.58474-1-tixxdz@gmail.com> 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 adds a new bpf helper bpf_task_freeze_cgroup() to freeze a cgroup of a task and all its descendant cgroups. It requires the task to be on the default cgroup v2 hierarchy. For some cases we want to freeze the cgroup of a task based on some signals, doing so from bpf is better than user space which could be too late. Planned users of this feature are: tetragon and systemd when freezing a cgroup hierarchy that could be a K8s pod, container, system service or a user session. This helper will acquire the cgroup_mutex during its operation and release it before it returns. Signed-off-by: Djalal Harouni --- kernel/bpf/helpers.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 9234174ccb21..8d510a1b265c 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2270,6 +2270,36 @@ bpf_task_get_cgroup1(struct task_struct *task, int hierarchy_id) return NULL; return cgrp; } + +/** + * bpf_task_freeze_cgroup - Freeze the task cgroup and all its descendant cgroups. + * + * @task: The target task + * @freeze: freeze state, passing value 1 causes the freezing of the cgroup + * and all descendant cgroups. Processes under this cgroup hierarchy will + * be stopped and will not run until the cgroup is explicitly unfrozen. + * Passing value 0 unthaws the task cgroup and its descendant cgroups. + * + * Return: %0 on success or a negative errno code on failure. + */ +__bpf_kfunc int bpf_task_freeze_cgroup(struct task_struct *task, int freeze) +{ + int ret; + struct cgroup *cgrp; + + rcu_read_lock(); + cgrp = task_dfl_cgroup(task); + if (!cgrp || !cgroup_tryget(cgrp)) { + rcu_read_unlock(); + return -ENOENT; + } + rcu_read_unlock(); + + ret = cgroup_freeze_no_kn(cgrp, freeze); + cgroup_put(cgrp); + + return ret; +} #endif /* CONFIG_CGROUPS */ /** @@ -2577,6 +2607,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_freeze_cgroup, KF_TRUSTED_ARGS | KF_SLEEPABLE) #endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_throw) -- 2.34.1