From patchwork Wed Mar 27 22:53:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Djalal Harouni X-Patchwork-Id: 783788 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (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 1AFBE2DF92; Wed, 27 Mar 2024 22:55:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711580109; cv=none; b=ugumjBWE36wmqKX98eCB7J2nszDtjUTEOQLdzvltXlAyP8xwqSH6kasPcrBYKnV6qUORur5BRcSzS8JtZmhV68ZZXNaf6dU701e9RCE+Mn5SR/+dveVHdY7MEKQBNjrP9BX/ExkVNes7X3z/xmFl1TGJabpzcRNDdoWEdgFN7n4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711580109; c=relaxed/simple; bh=+jtsaYid9d6k38CUcsY8zjQJOAoqkoOupXt7LZrPhpQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Plq8pSz6qh/r3q88OX9rALHPctLXjnecL0xzh6zlWqezAkIGxWE+JMw2VUvXnN+mHX5yZOczFIcHokBdmh5WbV7G1z3HaN9aTG7M9I+/QG60v6P20NIVPFG6sunmGQk9vwgnSaC7/OaYUnlaRh67dtP+Pqeo41ZyHMw9YtB6Rjw= 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=klHJV2sW; arc=none smtp.client-ip=209.85.221.48 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="klHJV2sW" Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-341808b6217so171396f8f.3; Wed, 27 Mar 2024 15:55:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711580106; x=1712184906; 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=nH4tgs9aLkl4Y8wm8Zix864aLcytoWhaI17Seukw8fs=; b=klHJV2sWYetYbRUv2JmPpqr6clbttXA6X2r2Koaa8fLpLf99Fw1fYW5WT/fVn65Sof XwNfzUTxc2aw5HzwPJvnUfUojc+54/7ccMfLHH/5grTnyN6DDsK/bFYRFc2WR4W8DF9l UKKoxWREa1XFRfkMqAkEO5VIpb+XLT//h/H2xv887QS2a0VkJodrIeSFLFOhjL+DvOJ1 DVuqQLKJ+xy7Kh4zo6waQJCa+bht8ivfgyZDz5pW/PFXUojr104jlQJULM7poEbKNlox Xar82KPsa+IIcL4NwHwCDhqeWkv0bSvndEEKcxT2oagr7QFd9Icotf3r+AvxG/m7irnT NmIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711580106; x=1712184906; 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=nH4tgs9aLkl4Y8wm8Zix864aLcytoWhaI17Seukw8fs=; b=sbbhuuSaxcAMi2IBlQoJw2t1KcviF30FMfd7NHKv9K0eSfVXImR5WNm8ZcdDHXhzVu hKb7Fn+kv0gh9J9nSRzxOTeNHWmbWs/NWXR95MP1Fk2TjR3X1h5FFuMAeezQnInRtzkd VF744Y0VaYAIv829iJRJGSSCembw1q0wNnqspitiM7y4NQof4j0JJo2jGUKkFC3Aph3m yrqwVfyoMIfI6hB55puPr1lrWE7jar89mvyyjuK9/+h+lKdIG/793kPSB1aKuAcMhUDa 4kD46fjQQDzLPPvuQMGjQs7pxP0/53RQ18vSgGhfqgp/yc84+8NRi8IgbT7SeTbBcXPe TS0w== X-Forwarded-Encrypted: i=1; AJvYcCUS7Z0NrR2x/b46PvwxU53HycStNM5fzvVDlmtcyoIRKybJ18/Ijh6VuFzQoYtqX2N/Kg9l/pCGCeEQZR0NXrQ/ewElMUbNgwMNpojHZ9N9n5pCLCM6DzAw1pui9LdKfggFvS5ekUt6ygC96Fh6oDhWGiXsKfdAtZz/Msurd+pyPLWZeJNuY9cTfhlCMiboL1m4gpDqjoNs/vqmTQ== X-Gm-Message-State: AOJu0Yw+Boi6t3ydLZ3o9FhFQpy959VcYOgOIARbeKMzt6jiPeKIxNDy ACvcD3Zkm5DaLkwXBHLfjtowGvyitAaL/C40bOCijUIDy6MFwVsm X-Google-Smtp-Source: AGHT+IHdigNsmxm7L+bIAaCTVVAcKK+rZSQ0wKNyArZKj0RkvVRFrWOTxBNeNbH91E28js+snEK0fA== X-Received: by 2002:a5d:6b87:0:b0:33e:7402:f4d3 with SMTP id n7-20020a5d6b87000000b0033e7402f4d3mr938250wrx.33.1711580106330; Wed, 27 Mar 2024 15:55:06 -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.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 15:55:06 -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 1/3] cgroup: add cgroup_freeze_no_kn() to freeze a cgroup from bpf Date: Wed, 27 Mar 2024 23:53:23 +0100 Message-Id: <20240327225334.58474-2-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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This patch adds a new cgroup helper cgroup_freeze_no_kn() to freeze a cgroup hierarchy that is on a default cgroup v2 without going through kernfs interface. 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. The cgroup_freeze_no_kn() will acquire the cgroup_mutex and release it at the end. It also checks if the cgroup is on the default hierarchy and it is not a root cgroup. Signed-off-by: Djalal Harouni --- include/linux/cgroup.h | 2 ++ kernel/cgroup/cgroup.c | 69 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 34aaf0e87def..5019b32ea933 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -137,6 +137,8 @@ int cgroup_init(void); int cgroup_parse_float(const char *input, unsigned dec_shift, s64 *v); +int cgroup_freeze_no_kn(struct cgroup *cgrp, int freeze); + /* * Iteration helpers and macros. */ diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index a66c088c851c..0aafcd9e39b5 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -1595,6 +1595,26 @@ static u16 cgroup_calc_subtree_ss_mask(u16 subtree_control, u16 this_ss_mask) return cur_ss_mask; } +/** + * cgroup_dfl_write_no_kn - check if direct writes to cgroup without going + * through kernfs is allowed. + * @cgrp: the target cgroup + * + * This helper ensures that the cgroup is on the default hierarchy and it + * is not a root cgroup. + * + * Return: %0 on success or a negative errno code on failure. + */ +static int cgroup_dfl_write_no_kn(struct cgroup *cgrp) +{ + lockdep_assert_held(&cgroup_mutex); + + if (!cgroup_on_dfl(cgrp) || !cgroup_parent(cgrp)) + return -EOPNOTSUPP; + + return 0; +} + /** * cgroup_kn_unlock - unlocking helper for cgroup kernfs methods * @kn: the kernfs_node being serviced @@ -1668,6 +1688,25 @@ struct cgroup *cgroup_kn_lock_live(struct kernfs_node *kn, bool drain_offline) return NULL; } +/** + * cgroup_lock_live_no_kn - locking helper for direct writes to cgroup without + * going through kernfs interface. + * @cgrp: the target cgroup + * + * This helper performs cgroup locking and verifies that the associated cgroup + * is alive. Returns the cgroup if alive; otherwise, %NULL. + * A successful return should be undone by a matching cgroup_unlock() + * invocation. + */ +static struct cgroup *cgroup_lock_live_no_kn(struct cgroup *cgrp) +{ + cgroup_lock(); + if (!cgroup_is_dead(cgrp)) + return cgrp; + cgroup_unlock(); + return NULL; +} + static void cgroup_rm_file(struct cgroup *cgrp, const struct cftype *cft) { char name[CGROUP_FILE_NAME_MAX]; @@ -3930,6 +3969,36 @@ static int cgroup_freeze_show(struct seq_file *seq, void *v) return 0; } +/** + * cgroup_freeze_no_kn - Freeze a cgroup that is on the default hierarchy + * without going through kernfs interface. + * + * @cgrp: the target cgroup + * @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 cgroup hierarchy. + * + * Return: %0 on success or a negative errno code on failure. + */ +int cgroup_freeze_no_kn(struct cgroup *cgrp, int freeze) +{ + int ret = 0; + + if (freeze < 0 || freeze > 1) + return -ERANGE; + + if (!cgroup_lock_live_no_kn(cgrp)) + return -ENOENT; + + ret = cgroup_dfl_write_no_kn(cgrp); + if (!ret) + cgroup_freeze(cgrp, freeze); + + cgroup_unlock(); + return ret; +} + static ssize_t cgroup_freeze_write(struct kernfs_open_file *of, char *buf, size_t nbytes, loff_t off) { From patchwork Wed Mar 27 22:53:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Djalal Harouni X-Patchwork-Id: 783382 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="W/4JanH3" 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-Google-Smtp-Source: AGHT+IGmzfBChgCSImBAhgHjs9dtQYNUn9rfk833w7UvcXpMOo/gj/a+75BzNxxtakQDndWUXnw6qg== 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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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) From patchwork Wed Mar 27 22:53:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Djalal Harouni X-Patchwork-Id: 783787 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (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 4B8A6153BED; Wed, 27 Mar 2024 22:55:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711580134; cv=none; b=pUuf9B3m9q5HZyudtmB+WqL4PJEjzbemyEtAVhUJhj5tKXdpUNzQE8MQ0zIeWWnJCsVT+LS3zy4v8H54nwm/A7DAq/Nnb70cyVvmehyd9AH21p2nXSST+BYOgu5UnFYNY50nStb7I6Wmkdp2dZA6VLxarBTru9dLDuRhRxfUJXg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711580134; c=relaxed/simple; bh=VUW8SqAA/+2sHQhA2ammk2FQgHtxhgRr3LE1tD2/QzQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JslUuwivjy5PvuZC8/MCIyzTNw3GwX278ugCJofTsKHzHeUbyUtGhMV+SnhMbxHcxgxsSZ0y9Bt/BdivQtqYswTKkyI66XpYHuyhlPSDrqfABjIZP9yzTrbViG90vrgw/kZu5/dG+qiN/Z4mU7Go0CZpMGeyQ7iKkbDAQM1NKmI= 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=C/Zjr4l+; arc=none smtp.client-ip=209.85.221.46 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="C/Zjr4l+" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-341b01dbebbso225149f8f.0; Wed, 27 Mar 2024 15:55:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711580131; x=1712184931; 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=KP36tHqP7P+dmRhIwVHlpSLJhJGKUjScQFwDQs5TYi4=; b=C/Zjr4l+ZoGJCbtUKuI6eEVuWw7ytQwA85B7OqHbm+1IJENhsToM8Q/YmKGUp8YNBI /dlbd3a4I8oPTbTRBC/oB1hk1PzxF4xJMXdhPYx6lNwR5tYZykJS0JmayosHJBdOsPZ0 9cOhzZL/ADQCxjkw7rhAaBNWAOA6iz0UGjpx4FjRQ+FQqFHM/iYkmg2R4F5vcReFOsfz O81I2usonFRDvDGw4tsB9F+h4ZbQ2cQ7tVwIhxi5G8oQER+m10QmNU5vuBYjUAEMPrOt QUz0bZPi8iIWIqBjh2hWwf8xcEtbArdfzm3/j9hfj0WSbC3axiJlGg13Xu5KEYEQdZzV bxaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711580131; x=1712184931; 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=KP36tHqP7P+dmRhIwVHlpSLJhJGKUjScQFwDQs5TYi4=; b=Nreev+sZ6A/mUJHZ5Vq1d3TJpRp+0fYfXDYOIlA/i2JlC1UQBhjWmG0+L2c4Hk9+tB hKLsDmZQeZqIdXC4UWwpt8sOAdolIdoOBiTAgyfqWnh49NZLXJmpNYVu1e7v7vNpQySw QKttZuJdM0yxadXVxAV7dK2ws/LDJtfGwsTWHokN0n0z3R4W+JiO2wcrvbVAFuIv8Oh1 W5DISwj5k9e+orjBWINZTuvHrH7pNV8ZJn69tzb7SbyySt0l1550fvIZuuIaBWyQr/td DB0FUP5EhVGWShygzln2obOkvZTlES3qFyRJxPOa3K8DL+DQMLxcePWeu4atwqmt/cOP nk2w== X-Forwarded-Encrypted: i=1; AJvYcCXL5RmUvIVqR/IBt00Ew1BKiCGSdjwVQmMrx9WNGNxrYEf3zDWedp++pTbsxRcLuMkqgo4/M67mnEeREAO4IoeRLTJ+pL7xUcE2mUNzWaFMkX/RI6GPLGbvj6lhZbMpNLaM4UIBnOd1qOiSHjJMk/wKuyX0NPhsK2H2ZlKCur8d5Jw9FjflNR4/Kvs81o821N2Dfd9fJrdOleaB4A== X-Gm-Message-State: AOJu0Yw+jlk351tRu/fYMADJ2C1jE9uR847o1Y5FE72QGbyffAJwVphR 2ZZTEiywoUL/PPGqjsSoIAmRKX7xU/HrN2yFmcKddMoXI1YMPTmL X-Google-Smtp-Source: AGHT+IH7e2g0Y7KVgrgTneD3KILz2Xw+4uPpn/4EBcTEadHjIlt00UCW1BykdWehFemRxO7De3P+fw== X-Received: by 2002:a5d:59a8:0:b0:341:e5d1:d353 with SMTP id p8-20020a5d59a8000000b00341e5d1d353mr1355183wrr.14.1711580130666; Wed, 27 Mar 2024 15:55:30 -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.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 15:55:30 -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 3/3] selftests/bpf: add selftest for bpf_task_freeze_cgroup Date: Wed, 27 Mar 2024 23:53:25 +0100 Message-Id: <20240327225334.58474-4-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-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This adds a selftest for `bpf_task_freeze_cgroup` kfunc. The test works by forking a child then: 1. Child: - Migrate to a new cgroup - Loads bpf programs - Trigger the 'lsm_freeze_cgroup' bpf program so it freeze itself. by calling "bpf_task_freeze_cgroup(child, 1)" <- wait for parent to unthaw - On unthaw it continues, forks another process and triggers the 'tp_newchild' bpf program to set some monitored pids of the new process, that are asserted at user space, to ensure that we resumed correctly. 2. Parent: - Keeps reading the 'cgroup.freeze' file of the child cgroup until it prints 1 which means the child cgroup is frozen - Attaches the sample 'lsm_task_free' so it triggers the bpf program and then calls "bpf_task_freeze_cgroup(task, 0);" on the child task to unthaw its cgroup. - Then waits for a clean exit of the child process. The scenario allows to test both: freeze and unthaw a task cgroup. Signed-off-by: Djalal Harouni --- .../bpf/prog_tests/task_freeze_cgroup.c | 165 ++++++++++++++++++ .../bpf/progs/test_task_freeze_cgroup.c | 110 ++++++++++++ 2 files changed, 275 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/task_freeze_cgroup.c create mode 100644 tools/testing/selftests/bpf/progs/test_task_freeze_cgroup.c diff --git a/tools/testing/selftests/bpf/prog_tests/task_freeze_cgroup.c b/tools/testing/selftests/bpf/prog_tests/task_freeze_cgroup.c new file mode 100644 index 000000000000..afb7d46194c5 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/task_freeze_cgroup.c @@ -0,0 +1,165 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Isovalent */ + +#include +#include +#include +#include +#include "test_task_freeze_cgroup.skel.h" + +#define FOO "/test-task-freeze-cgroup" + +static int bpf_sleepable(struct test_task_freeze_cgroup *skel) +{ + int err, foo; + pid_t pid; + + foo = test__join_cgroup(FOO); + if (!ASSERT_OK(foo < 0, "cgroup_join_foo")) + return -errno; + + skel = test_task_freeze_cgroup__open(); + if (!ASSERT_OK_PTR(skel, "test_task_freeze_cgroup__open")) + return -errno; + + skel->rodata->parent_pid = getppid(); + skel->rodata->monitor_pid = getpid(); + skel->rodata->cgid = get_cgroup_id(FOO); + skel->bss->new_pid = getpid(); + skel->bss->freeze = 1; + + err = test_task_freeze_cgroup__load(skel); + if (!ASSERT_OK(err, "test_task_freeze_cgroup__load")) + goto cleanup; + + /* First, attach the LSM program, and then it will be triggered when the + * TP_BTF program is attached. + */ + skel->links.lsm_freeze_cgroup = + bpf_program__attach_lsm(skel->progs.lsm_freeze_cgroup); + if (!ASSERT_OK_PTR(skel->links.lsm_freeze_cgroup, "attach_lsm")) { + err = -errno; + goto cleanup; + } + + /* This will fail */ + skel->links.tp_newchild = + bpf_program__attach_trace(skel->progs.tp_newchild); + if (!ASSERT_EQ(errno, EPERM, "attach_trace")) { + err = -EINVAL; + goto cleanup; + } + + /* Try again now */ + skel->links.tp_newchild = + bpf_program__attach_trace(skel->progs.tp_newchild); + if (!ASSERT_OK_PTR(skel->links.tp_newchild, "attach_trace")) { + err = -EINVAL; + goto cleanup; + } + + /* Trigger a new child and assert unfrozen state */ + pid = fork(); + if (pid == 0) + exit(0); + + err = (pid == -1); + if (ASSERT_OK(err, "fork process")) + wait(NULL); + + /* Now we should continue, assert that new_pid reflects child */ + ASSERT_NEQ(skel->rodata->monitor_pid, skel->bss->new_pid, + "test task_freeze_cgroup failed at monitor_pid != new_pid"); + ASSERT_NEQ(0, skel->bss->new_pid, + "test task_freeze_cgroup failed at remote_pid != 0"); + + /* Assert that bpf set new_pid to new forked child pid */ + ASSERT_EQ(pid, skel->bss->new_pid, + "test task_freeze_cgroup failed at pid == new_pid"); + + test_task_freeze_cgroup__detach(skel); + +cleanup: + test_task_freeze_cgroup__destroy(skel); + close(foo); + return err; +} + +void test_task_freeze_cgroup(void) +{ + pid_t pid, result; + char buf[512] = {0}; + char path[PATH_MAX] = {0}; + int ret, status, attempts, frozen = 0; + struct test_task_freeze_cgroup *skel = NULL; + + pid = fork(); + ret = (pid == -1); + if (!ASSERT_OK(ret, "fork process")) + return; + + if (pid == 0) { + ret = bpf_sleepable(skel); + ASSERT_EQ(0, ret, "bpf_sleepable failed"); + exit(ret); + } + + skel = test_task_freeze_cgroup__open(); + if (!ASSERT_OK_PTR(skel, "test_task_freeze_cgroup__open")) + goto out; + + snprintf(path, sizeof(path), + "/sys/fs/cgroup/cgroup-test-work-dir%d%s/cgroup.freeze", + pid, FOO); + + for (attempts = 5; attempts >= 0; attempts--) { + ret = 0; + int fd = open(path, O_RDONLY); + if (fd > 0) + ret = read(fd, buf, sizeof(buf) - 1); + if (ret > 0) { + errno = 0; + frozen = strtol(buf, NULL, 10); + if (errno) + frozen = 0; + } + + close(fd); + if (frozen) + break; + sleep(1); + } + + /* Assert that child cgroup is frozen */ + if (!ASSERT_EQ(1, frozen, "child cgroup not frozen")) + goto out; + + ret = test_task_freeze_cgroup__load(skel); + if (!ASSERT_OK(ret, "test_task_freeze_cgroup__load")) + goto out; + + /* Unthaw child cgroup from parent */ + skel->links.lsm_task_free = + bpf_program__attach_lsm(skel->progs.lsm_task_free); + if (!ASSERT_OK_PTR(skel->links.lsm_task_free, "attach_lsm")) + goto out; + + result = waitpid(pid, &status, WUNTRACED); + if (!ASSERT_NEQ(result, -1, "waitpid")) + goto detach; + + result = WIFEXITED(status); + if (!ASSERT_EQ(result, 1, "forked process did not terminate normally")) + goto detach; + + result = WEXITSTATUS(status); + if (!ASSERT_EQ(result, 0, "forked process did not exit successfully")) + goto detach; + +detach: + test_task_freeze_cgroup__detach(skel); + +out: + if (skel) + test_task_freeze_cgroup__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_task_freeze_cgroup.c b/tools/testing/selftests/bpf/progs/test_task_freeze_cgroup.c new file mode 100644 index 000000000000..dbd2d60f464e --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_task_freeze_cgroup.c @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2024 Isovalent */ + +#include +#include +#include +#include +#include "bpf_misc.h" + +struct cgroup *bpf_cgroup_from_id(u64 cgid) __ksym; +long bpf_task_under_cgroup(struct task_struct *task, struct cgroup *ancestor) __ksym; +void bpf_cgroup_release(struct cgroup *p) __ksym; +struct task_struct *bpf_task_from_pid(s32 pid) __ksym; +struct task_struct *bpf_task_acquire(struct task_struct *p) __ksym; +void bpf_task_release(struct task_struct *p) __ksym; + +int bpf_task_freeze_cgroup(struct task_struct *task, int freeze) __ksym; + +const volatile int parent_pid; +const volatile int monitor_pid; +const volatile __u64 cgid; +int new_pid; +int freeze; + +SEC("tp_btf/task_newtask") +int BPF_PROG(tp_newchild, struct task_struct *task, u64 clone_flags) +{ + struct cgroup *cgrp = NULL; + struct task_struct *acquired; + + if (monitor_pid != (bpf_get_current_pid_tgid() >> 32)) + return 0; + + acquired = bpf_task_acquire(task); + if (!acquired) + return 0; + + cgrp = bpf_cgroup_from_id(cgid); + if (!cgrp) + goto out; + + if (bpf_task_under_cgroup(acquired, cgrp)) + new_pid = acquired->tgid; + +out: + if (cgrp) + bpf_cgroup_release(cgrp); + bpf_task_release(acquired); + + return 0; +} + +/* This is attached from parent to trigger the bpf lsm hook, so parent + * can unthaw the child. + */ +SEC("lsm/task_free") +int BPF_PROG(lsm_task_free, struct task_struct *task) +{ + return 0; +} + +SEC("lsm.s/bpf") +int BPF_PROG(lsm_freeze_cgroup, int cmd, union bpf_attr *attr, unsigned int size) +{ + int ret = 0; + struct cgroup *cgrp = NULL; + struct task_struct *task; + + if (cmd != BPF_LINK_CREATE) + return ret; + + task = bpf_get_current_task_btf(); + if (parent_pid == task->pid) { + /* Unthaw child from parent */ + task = bpf_task_from_pid(monitor_pid); + if (!task) + return -ENOENT; + + ret = bpf_task_freeze_cgroup(task, 0); + bpf_task_release(task); + return ret; + } + + if (monitor_pid != task->pid) + return 0; + + /* Freeze the child cgroup from its context */ + cgrp = bpf_cgroup_from_id(cgid); + if (!cgrp) + goto out; + + if (!bpf_task_under_cgroup(task, cgrp)) + goto out; + + if (freeze) { + /* Schedule freeze task and return -EPERM */ + ret = bpf_task_freeze_cgroup(task, 1); + if (!ret) { + ret = -EPERM; + /* reset for next call */ + freeze = 0; + } + } +out: + if (cgrp) + bpf_cgroup_release(cgrp); + return ret; +} + +char _license[] SEC("license") = "GPL";