From patchwork Sun Oct 29 06:14:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13439721 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F1A4441F; Sun, 29 Oct 2023 06:14:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="K4lzu45R" Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 16E9CD9; Sat, 28 Oct 2023 23:14:48 -0700 (PDT) Received: by mail-oi1-x235.google.com with SMTP id 5614622812f47-3b2ec9a79bdso2464553b6e.3; Sat, 28 Oct 2023 23:14:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698560087; x=1699164887; 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=pZ8I0hc4htgXjz6UnkXw7mUnO4qqNaJY7OsjOdR55Pc=; b=K4lzu45RNJKG90G6AaOMwpWGo5MDuiMFaFDi4fpUGAe79k5NqIPMsT7FdjXZrMgZ2I wR0xkt07YO7+nf0ZiouF6RX149M2Cxvt0KqjYUEcGkgov86L1RG07MWBDjT/V2fx1VtU BPvUoEq6E7lkbS07/5qyb/ONHSR28fgA2hsIUJJvat+1l+a6ixnTccmeLZ3DUEYLMAtj 6ZPgX842aSF+iAjpqJ6OZFqcP98ycFeylXmLaeRACChENgsJJxWYfCSHhlx+CG0LRjeq 4d7yKU6CpL8fDswfkyXDLsbpQMjlXrqvdxNcngIIOiwwP6ZrmYvsbJIwLh/McrR99v47 Fmbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698560087; x=1699164887; 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=pZ8I0hc4htgXjz6UnkXw7mUnO4qqNaJY7OsjOdR55Pc=; b=Cf7j8CAA3PsggIP+0tetOGGdtVB2dya6uTAJ9b5WppRABCse4YeUpokXMKm7hIAzWW GUyR4NksLKFZrk4YST22ukIb/Xozu6YXmY4XbiAqLjafVecm7ty9dwHRqWX9gmKs5rTG m81wgQaz/NSaxBkoncTEqeMiBV3R77GDY/jsPn1i62vT+JSAkPzga5Da0pQkGMnyB/v7 0Tyjttm28WwEGd6jdqUqSv9Es6151u3suLbWSug3SbwGIkNOqKZi76MsOq5wQun/8Onj VHB9yPuhUCgnZ8jqpOdw3JWKIPPbs5ZQxbOAbj34YR34AjItOjEA/sPs0TNwtdpiskin okUg== X-Gm-Message-State: AOJu0YyqIa+EFLfvJ+5ii+ZCDVdS9VuBXpKpHl459XvOlt2T0Xcq0Xfq WiffI0/dgLK/6+ABHaLV6OU= X-Google-Smtp-Source: AGHT+IEah1CjvGyiroJr6UiA/GDjGCwuMJvXEEfFL+qEYMkQCElKojth+1devgY9jRtHZJAvDORq0Q== X-Received: by 2002:a05:6808:a10:b0:3a7:3988:87ee with SMTP id n16-20020a0568080a1000b003a7398887eemr7207263oij.58.1698560087274; Sat, 28 Oct 2023 23:14:47 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:2b5:5400:4ff:fea0:d066]) by smtp.gmail.com with ESMTPSA id m2-20020aa79002000000b006b225011ee5sm3775106pfo.6.2023.10.28.23.14.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 23:14:46 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com, longman@redhat.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, oliver.sang@intel.com, Yafang Shao Subject: [PATCH v3 bpf-next 01/11] cgroup: Remove unnecessary list_empty() Date: Sun, 29 Oct 2023 06:14:28 +0000 Message-Id: <20231029061438.4215-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029061438.4215-1-laoar.shao@gmail.com> References: <20231029061438.4215-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net The root hasn't been removed from the root_list, so the list can't be NULL. However, if it had been removed, attempting to destroy it once more is not possible. Let's replace this with WARN_ON_ONCE() for clarity. Signed-off-by: Yafang Shao Acked-by: Tejun Heo --- kernel/cgroup/cgroup.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 1fb7f56..3053d42 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -1345,10 +1345,9 @@ static void cgroup_destroy_root(struct cgroup_root *root) spin_unlock_irq(&css_set_lock); - if (!list_empty(&root->root_list)) { - list_del(&root->root_list); - cgroup_root_count--; - } + WARN_ON_ONCE(list_empty(&root->root_list)); + list_del(&root->root_list); + cgroup_root_count--; cgroup_favor_dynmods(root, false); cgroup_exit_root_id(root); From patchwork Sun Oct 29 06:14:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13439723 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A48A6112; Sun, 29 Oct 2023 06:14:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="e2RhI2hv" Received: from mail-oi1-x22f.google.com (mail-oi1-x22f.google.com [IPv6:2607:f8b0:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BCE02E4; Sat, 28 Oct 2023 23:14:49 -0700 (PDT) Received: by mail-oi1-x22f.google.com with SMTP id 5614622812f47-3b2e73a17a0so2239432b6e.3; Sat, 28 Oct 2023 23:14:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698560089; x=1699164889; 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=kCchiRA8pA+IFVpZf4nT3Gq5eOjm8JawUtfCWTnKHFA=; b=e2RhI2hvM1O4ZSD2mnJOKrmhnT+InAELxFucoO7SJQVPT7HcBPFpiilJgwLq1QKB5u ZZ41fzqx337Gat+b3VEiu+e5yi9ilfYlVeHPthqqVwIXNLo+gitFUAeVAKX6WptUlJX2 vdVZMkd9lg5diaY6gZv0Bm933Zb8A66d6RC3eRv/iLa4TKKDGDeD+sCfeP9YOvLApCyy RzVt+JYPm3kmyFV2ffTW1WqRTF2JyMCIUkT552vlI7tSHv6bnr8UJxZa5eYOigQXNsr2 sq30SvmXVP7yQA/Jz0nTB6HtgH9mWAxn754ya+/m02I2DgBNO+DnTKvX7JZyxpEr3bE4 KoyQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698560089; x=1699164889; 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=kCchiRA8pA+IFVpZf4nT3Gq5eOjm8JawUtfCWTnKHFA=; b=phOHhg289cEi9lugFQeo2pa2YUWcXzbKT6O86RJyi0mQFZeyamCG4+xrOyZDpKFYMn YFIOpgOlIuImypM/Gjj4QXlHqtv8d7s7RkAFGQ/PQ45Z4EvpsyXs1uIpDLarfg+1IXBB qCirGByCpqfglTCByCUP8wapVU2zADS0rMrKMnvP4Akw1DUtXiZ8mPLz0v/6BwjbZTj9 h8i68wWgxDNsOys5vYZJKCW2Tpu7hGmqcyqDR23bLU939kJzVLmNrDoMHTg2zvXpkFp+ KgTjwokXAzE1e6d6NXN2acFMwoLHYm4Mah3V9g3aGKWW6s6B+bDgFvbgMiJXZHn7pIB8 IDCw== X-Gm-Message-State: AOJu0YxJf70xF7xSXflj8xQj+VHGq+ZX26Gnq+OjBz1uV86Q7GxGCg8J RSen6VDdqTUFT1ZobjUu9Cf/J5wpMjEP79/X X-Google-Smtp-Source: AGHT+IGGVDeUR8CbYEFV0N7JMgUXsZnbaWsAbygNQ5Jt830Y4DuVPIEIZY8zK1qEB1l356Aub7xpAw== X-Received: by 2002:a05:6808:152c:b0:3a7:4987:d44 with SMTP id u44-20020a056808152c00b003a749870d44mr8017774oiw.20.1698560088966; Sat, 28 Oct 2023 23:14:48 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:2b5:5400:4ff:fea0:d066]) by smtp.gmail.com with ESMTPSA id m2-20020aa79002000000b006b225011ee5sm3775106pfo.6.2023.10.28.23.14.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 23:14:48 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com, longman@redhat.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, oliver.sang@intel.com, Yafang Shao Subject: [PATCH v3 bpf-next 02/11] cgroup: Make operations on the cgroup root_list RCU safe Date: Sun, 29 Oct 2023 06:14:29 +0000 Message-Id: <20231029061438.4215-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029061438.4215-1-laoar.shao@gmail.com> References: <20231029061438.4215-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net At present, when we perform operations on the cgroup root_list, we must hold the cgroup_mutex, which is a relatively heavyweight lock. In reality, we can make operations on this list RCU-safe, eliminating the need to hold the cgroup_mutex during traversal. Modifications to the list only occur in the cgroup root setup and destroy paths, which should be infrequent in a production environment. In contrast, traversal may occur frequently. Therefore, making it RCU-safe would be beneficial. Signed-off-by: Yafang Shao Acked-by: Tejun Heo --- include/linux/cgroup-defs.h | 1 + kernel/cgroup/cgroup-internal.h | 3 ++- kernel/cgroup/cgroup.c | 23 ++++++++++++++++------- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index f1b3151..8505eea 100644 --- a/include/linux/cgroup-defs.h +++ b/include/linux/cgroup-defs.h @@ -558,6 +558,7 @@ struct cgroup_root { /* A list running through the active hierarchies */ struct list_head root_list; + struct rcu_head rcu; /* Hierarchy-specific flags */ unsigned int flags; diff --git a/kernel/cgroup/cgroup-internal.h b/kernel/cgroup/cgroup-internal.h index c56071f..5e17f01 100644 --- a/kernel/cgroup/cgroup-internal.h +++ b/kernel/cgroup/cgroup-internal.h @@ -170,7 +170,8 @@ struct cgroup_mgctx { /* iterate across the hierarchies */ #define for_each_root(root) \ - list_for_each_entry((root), &cgroup_roots, root_list) + list_for_each_entry_rcu((root), &cgroup_roots, root_list, \ + lockdep_is_held(&cgroup_mutex)) /** * for_each_subsys - iterate all enabled cgroup subsystems diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 3053d42..28b8ccc 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -1313,7 +1313,7 @@ static void cgroup_exit_root_id(struct cgroup_root *root) void cgroup_free_root(struct cgroup_root *root) { - kfree(root); + kfree_rcu(root, rcu); } static void cgroup_destroy_root(struct cgroup_root *root) @@ -1346,7 +1346,7 @@ static void cgroup_destroy_root(struct cgroup_root *root) spin_unlock_irq(&css_set_lock); WARN_ON_ONCE(list_empty(&root->root_list)); - list_del(&root->root_list); + list_del_rcu(&root->root_list); cgroup_root_count--; cgroup_favor_dynmods(root, false); @@ -1385,7 +1385,15 @@ static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset, } } - BUG_ON(!res_cgroup); + /* + * If cgroup_mutex is not held, the cgrp_cset_link will be freed + * before we remove the cgroup root from the root_list. Consequently, + * when accessing a cgroup root, the cset_link may have already been + * freed, resulting in a NULL res_cgroup. However, by holding the + * cgroup_mutex, we ensure that res_cgroup can't be NULL. + * If we don't hold cgroup_mutex in the caller, we must do the NULL + * check. + */ return res_cgroup; } @@ -1444,7 +1452,6 @@ static struct cgroup *current_cgns_cgroup_dfl(void) static struct cgroup *cset_cgroup_from_root(struct css_set *cset, struct cgroup_root *root) { - lockdep_assert_held(&cgroup_mutex); lockdep_assert_held(&css_set_lock); return __cset_cgroup_from_root(cset, root); @@ -1452,7 +1459,9 @@ static struct cgroup *cset_cgroup_from_root(struct css_set *cset, /* * Return the cgroup for "task" from the given hierarchy. Must be - * called with cgroup_mutex and css_set_lock held. + * called with css_set_lock held to prevent task's groups from being modified. + * Must be called with either cgroup_mutex or rcu read lock to prevent the + * cgroup root from being destroyed. */ struct cgroup *task_cgroup_from_root(struct task_struct *task, struct cgroup_root *root) @@ -2013,7 +2022,7 @@ void init_cgroup_root(struct cgroup_fs_context *ctx) struct cgroup_root *root = ctx->root; struct cgroup *cgrp = &root->cgrp; - INIT_LIST_HEAD(&root->root_list); + INIT_LIST_HEAD_RCU(&root->root_list); atomic_set(&root->nr_cgrps, 1); cgrp->root = root; init_cgroup_housekeeping(cgrp); @@ -2096,7 +2105,7 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask) * care of subsystems' refcounts, which are explicitly dropped in * the failure exit path. */ - list_add(&root->root_list, &cgroup_roots); + list_add_rcu(&root->root_list, &cgroup_roots); cgroup_root_count++; /* From patchwork Sun Oct 29 06:14:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13439722 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 931275C8B; Sun, 29 Oct 2023 06:14:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ey0THoYh" Received: from mail-oi1-x236.google.com (mail-oi1-x236.google.com [IPv6:2607:f8b0:4864:20::236]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 80C11CC; Sat, 28 Oct 2023 23:14:51 -0700 (PDT) Received: by mail-oi1-x236.google.com with SMTP id 5614622812f47-3b2f4a5ccebso2313940b6e.3; Sat, 28 Oct 2023 23:14:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698560091; x=1699164891; 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=szve6xHz78YhCfEIrRy12Iw80pSpOk1k3LDcIqwnpCY=; b=ey0THoYhtT+vDQhv7l9oRiRTc08U3TCSMGxyyYXKQcTAV1FnjVQ5EMJgKiD+FitMvp b35P8Yi+SX8Yy6j7OyC3SxmGDYK4DLc0L47Lcw8Q+iHjLMkytiluz5Cgcy0B3+PdxNIN 9F5JGjnZzmTkoqJxcSUtqU8ptugeoyXrSLzEKWA5bR76oNvTawxyC823fPtEzXhYYHXz 2vQYM+EeIvi1tiu7hQ4UErsTuw/5pAOC9RDdTnZ31JgRTudPj1DkojmI0MeBjpFQJdYA ZeBmQG6IzjWkn3AJP3T4pQ4VccNSYYvRb+mONOTiEzIiZhKZr0YtTlQ6bqAdz49yTFuo ywYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698560091; x=1699164891; 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=szve6xHz78YhCfEIrRy12Iw80pSpOk1k3LDcIqwnpCY=; b=uDnhfOFCbyWrAhiSA7xdAw2TpMWXJcmHyfpaq6huvvYmpbnl5cVxzSboG2A9X59gjq d7ud7yhKF+WtJMpKUP1yXpeLmALIFxfDWmOZe9v9ZRq6fAyw7Zs4N1kcLOzHBIQmNcAN McXr70Xrr4TPTfxQYkl7UpHAI3p7p/FhCH+PyGQGrRGXe6XWmp6lrQkZLv+h5T+TGdNy QrvkjL/O5ugMPCohaoucizLIzvy8U1D2CkPPxEVIXFW/IXDbSD/p/LOf9EnmOe2kzcWm nV8SvV7SreUC8Q2MawaWF0bYGidjMJ2h5FDm4nKzcfwkZf1jA73d6MNtUIRhgp7wPpTi ZFPw== X-Gm-Message-State: AOJu0YyxtUutu/vC2ZF98BUSHhHxQgXCVCGzZgNGweQUQLqUVv/azsVF w9A39lfvDX+l3m48dT9JX24= X-Google-Smtp-Source: AGHT+IEyTZFYEy7qBmUEhNNYew5E1crPltN4+TuI8/Q0arUz5hgvqRsbZESzpeqUn/E3847quGZZjA== X-Received: by 2002:a05:6808:68a:b0:3a8:ccf0:103f with SMTP id k10-20020a056808068a00b003a8ccf0103fmr6997110oig.3.1698560090770; Sat, 28 Oct 2023 23:14:50 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:2b5:5400:4ff:fea0:d066]) by smtp.gmail.com with ESMTPSA id m2-20020aa79002000000b006b225011ee5sm3775106pfo.6.2023.10.28.23.14.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 23:14:50 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com, longman@redhat.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, oliver.sang@intel.com, Yafang Shao Subject: [PATCH v3 bpf-next 03/11] cgroup: Eliminate the need for cgroup_mutex in proc_cgroup_show() Date: Sun, 29 Oct 2023 06:14:30 +0000 Message-Id: <20231029061438.4215-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029061438.4215-1-laoar.shao@gmail.com> References: <20231029061438.4215-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net The cgroup root_list is already RCU-safe. Therefore, we can replace the cgroup_mutex with the RCU read lock in some particular paths. This change will be particularly beneficial for frequent operations, such as `cat /proc/self/cgroup`, in a cgroup1-based container environment. I did stress tests with this change, as outlined below (with CONFIG_PROVE_RCU_LIST enabled): - Continuously mounting and unmounting named cgroups in some tasks, for example: cgrp_name=$1 while true do mount -t cgroup -o none,name=$cgrp_name none /$cgrp_name umount /$cgrp_name done - Continuously triggering proc_cgroup_show() in some tasks concurrently, for example: while true; do cat /proc/self/cgroup > /dev/null; done They can ran successfully after implementing this change, with no RCU warnings in dmesg. Signed-off-by: Yafang Shao Acked-by: Tejun Heo --- kernel/cgroup/cgroup.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 28b8ccc..cc6a6d9 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -6261,7 +6261,7 @@ int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, if (!buf) goto out; - cgroup_lock(); + rcu_read_lock(); spin_lock_irq(&css_set_lock); for_each_root(root) { @@ -6272,6 +6272,11 @@ int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, if (root == &cgrp_dfl_root && !READ_ONCE(cgrp_dfl_visible)) continue; + cgrp = task_cgroup_from_root(tsk, root); + /* The root has already been unmounted. */ + if (!cgrp) + continue; + seq_printf(m, "%d:", root->hierarchy_id); if (root != &cgrp_dfl_root) for_each_subsys(ss, ssid) @@ -6282,9 +6287,6 @@ int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, seq_printf(m, "%sname=%s", count ? "," : "", root->name); seq_putc(m, ':'); - - cgrp = task_cgroup_from_root(tsk, root); - /* * On traditional hierarchies, all zombie tasks show up as * belonging to the root cgroup. On the default hierarchy, @@ -6316,7 +6318,7 @@ int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, retval = 0; out_unlock: spin_unlock_irq(&css_set_lock); - cgroup_unlock(); + rcu_read_unlock(); kfree(buf); out: return retval; From patchwork Sun Oct 29 06:14:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13439724 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33E2A6AA0; Sun, 29 Oct 2023 06:14:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="CL+yn1Qu" Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 57E2BD3; Sat, 28 Oct 2023 23:14:53 -0700 (PDT) Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-6b89ab5ddb7so3459692b3a.0; Sat, 28 Oct 2023 23:14:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698560093; x=1699164893; 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=67CBbtnv19SbltoBUAc4Ce8xxrA/4bdASUdtuBTZWyI=; b=CL+yn1QuuZnJPUQwP301GY1rnwJr95ptw4OToYGfgKtNDWkyrToGH7a68Yt5aWx7UV Aez0f49xEcdYKj6S2rEtb0U/CvcAZXn1g4dfdcjCLVyvKeH2xmV2k19/k6yHKwr8yqQQ QVwGqIQkACaSEIQN4MBI4sC4Jk2sW6Sq5Zdz/QrczFYO7B1K02BVWyMpgoewEdSfK3w+ mO2H77MsnDWA+FpT5fdo3ebXbue1vGV4u20NtMxcJpxIoF3CltyBWzf6QAeUnHNn+Ekx MOiXaI7Jg2YAyXCaGf8Aab9NVdFhhOczYevibnNsu2MuxUOHT42bu5Ekgoyml281p2i+ fKpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698560093; x=1699164893; 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=67CBbtnv19SbltoBUAc4Ce8xxrA/4bdASUdtuBTZWyI=; b=sY6FH7r6uI+rc1EVgFnrAWyGRj0zhwKApxKzfZIvpGc0IIuf5hDJYHAjHpfslqw1/t mj+gDzHst6Vi6oXSZM9t8YM5h34OjYPpESZqp1cOuM1GeuVX7wmJg12rlcnO61dsRsyf sV6T7u/C5TPH4m48SDlXZdD1PMSOzeJmcm1QQgomG18lya+O1y7obSlyLnXql70r2iZR mFVvrOOSXX75LAGE8ko0Hzjo01p06HwPbUghGzLXZFPJG3WP7srCsBs5VPDGq6fTVz1x GsUZQzDzSu3lWD03MkvKi5caOt8D5YdjNkdOBGw9/ksWd4QdsVqpl+SDaFbG83HprmNL FN6g== X-Gm-Message-State: AOJu0YwMr9Ks2eXt0jhat/AC4jpTpBLGWV0P/b/5dGZsR26PnhfRSjjG dnR/nvv60KY0nc4K0i10rEw= X-Google-Smtp-Source: AGHT+IFra60Chv7uZYrOX/X1RyhBNyyBW8pmQw6YT0y8Hbn4ygPnrOkdLNGyIXQHWLnhOOAx+12cJw== X-Received: by 2002:a05:6a00:1143:b0:6b2:69c:346a with SMTP id b3-20020a056a00114300b006b2069c346amr7632922pfm.19.1698560092632; Sat, 28 Oct 2023 23:14:52 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:2b5:5400:4ff:fea0:d066]) by smtp.gmail.com with ESMTPSA id m2-20020aa79002000000b006b225011ee5sm3775106pfo.6.2023.10.28.23.14.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 23:14:52 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com, longman@redhat.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, oliver.sang@intel.com, Yafang Shao Subject: [PATCH v3 bpf-next 04/11] cgroup: Add annotation for holding namespace_sem in current_cgns_cgroup_from_root() Date: Sun, 29 Oct 2023 06:14:31 +0000 Message-Id: <20231029061438.4215-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029061438.4215-1-laoar.shao@gmail.com> References: <20231029061438.4215-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net When I initially examined the function current_cgns_cgroup_from_root(), I was perplexed by its lack of holding cgroup_mutex. However, after Michal explained the reason[0] to me, I realized that it already holds the namespace_sem. I believe this intricacy could also confuse others, so it would be advisable to include an annotation for clarification. After we replace the cgroup_mutex with RCU read lock, if current doesn't hold the namespace_sem, the root cgroup will be NULL. So let's add a WARN_ON_ONCE() for it. [0]. https://lore.kernel.org/bpf/afdnpo3jz2ic2ampud7swd6so5carkilts2mkygcaw67vbw6yh@5b5mncf7qyet Signed-off-by: Yafang Shao Cc: Michal Koutný Acked-by: Tejun Heo --- kernel/cgroup/cgroup.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index cc6a6d9..baba4b1 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -1416,6 +1416,11 @@ static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset, rcu_read_unlock(); + /* + * The namespace_sem is held by current, so the root cgroup can't + * be umounted. Therefore, we can ensure that the res is non-NULL. + */ + WARN_ON_ONCE(!res); return res; } From patchwork Sun Oct 29 06:14:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13439725 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D64725256; Sun, 29 Oct 2023 06:14:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Gkqe8aFf" Received: from mail-oi1-x233.google.com (mail-oi1-x233.google.com [IPv6:2607:f8b0:4864:20::233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 165A0CC; Sat, 28 Oct 2023 23:14:55 -0700 (PDT) Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-3b2f2b9a176so2325179b6e.0; Sat, 28 Oct 2023 23:14:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698560094; x=1699164894; 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=6+GKCMm3ywxhq8MnvGKc+S0I8QLqm/LrvywuBerA5qs=; b=Gkqe8aFfMhWLoG3Qq76lKkt+nl3xNTnSDuL9xUzGJIA1Rc2+5Y+HMZeajLtGUvP7+K KxiRVxa7k25zPQYMEKGmmDahXnZxcAi51vT0UsVEWdNne1yV3y2nqG6+IqxZNCCydj9/ UYC4g8hOsXoQ8SoveBfq0X4gFlvuj2DPltbct/nc7jlD1QtXBiU/i6dRaay9z34qDOV/ I08s1TN4DAGqjpiHje4iuXD9hwPkvQPJvSmd+gk3DDMSGaTLSdtAthUp38QV/rU1k5Hl jnnkA57crGGclNOnF5CDtyVkIqeuzhzWxMW9YL4odi0FxoYg3JFVIUWvpuOW22chxzH4 jGsA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698560094; x=1699164894; 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=6+GKCMm3ywxhq8MnvGKc+S0I8QLqm/LrvywuBerA5qs=; b=Rn623KJNg/zNw0KKwUB6+MfoCBYeRr/CqZe/MR0xN+9QDOQSu44AkiCBERJkgyt0Tm FA9h9cfb9r63kBd/HXd04FzTXHrcSdDUoMq2siFTR+Rc3YqzAk8A7RS4YYCGI7p05Cy9 pXUnWAjgZ0N5HPoyfQCqXgSxkAID8lcY2NuGe1F+SlbdCQlGzrGI5Bz825J6QraWE7kf fBc3pt/iyKqD5nURZ8JXj5bpz2Ach93rTj5nq8WBvDzltS+ll5n+CfBdOoyVIGGESc3C ohclnoQipHonKYBqU+1WR63ySR53HiIVElquWFLU6Y1EuNOIEd3UNh7ebndMqjTyOCYu hMZA== X-Gm-Message-State: AOJu0YwrDiDHtF3s+4U+UFo7xy/l5sYeUrraX6/ImNqG+Bb0ZoEOh5b9 fRy4lqmyCw1zFf8MGeRsgaQ= X-Google-Smtp-Source: AGHT+IHRWm5nG/sOfw5Ed/y+BCWmOfbUzZTasvwRvtOoHQuaMEtaT4lvfMMXF8bJVGfKohvAR4/oIA== X-Received: by 2002:a05:6808:1243:b0:3ae:b06:2131 with SMTP id o3-20020a056808124300b003ae0b062131mr7995051oiv.0.1698560094374; Sat, 28 Oct 2023 23:14:54 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:2b5:5400:4ff:fea0:d066]) by smtp.gmail.com with ESMTPSA id m2-20020aa79002000000b006b225011ee5sm3775106pfo.6.2023.10.28.23.14.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 23:14:53 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com, longman@redhat.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, oliver.sang@intel.com, Yafang Shao Subject: [PATCH v3 bpf-next 05/11] cgroup: Add a new helper for cgroup1 hierarchy Date: Sun, 29 Oct 2023 06:14:32 +0000 Message-Id: <20231029061438.4215-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029061438.4215-1-laoar.shao@gmail.com> References: <20231029061438.4215-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net A new helper is added for cgroup1 hierarchy: - task_get_cgroup1 Acquires the associated cgroup of a task within a specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its hierarchy ID. This helper function is added to facilitate the tracing of tasks within a particular container or cgroup dir in BPF programs. It's important to note that this helper is designed specifically for cgroup1 only. Suggested-by: Tejun Heo Signed-off-by: Yafang Shao Signed-off-by: Yafang Shao Signed-off-by: Tejun Heo --- include/linux/cgroup.h | 4 +++- kernel/cgroup/cgroup-internal.h | 1 - kernel/cgroup/cgroup-v1.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index b307013..e063e4c 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -71,6 +71,7 @@ struct css_task_iter { extern struct file_system_type cgroup_fs_type; extern struct cgroup_root cgrp_dfl_root; extern struct css_set init_css_set; +extern spinlock_t css_set_lock; #define SUBSYS(_x) extern struct cgroup_subsys _x ## _cgrp_subsys; #include @@ -388,7 +389,6 @@ static inline void cgroup_unlock(void) * as locks used during the cgroup_subsys::attach() methods. */ #ifdef CONFIG_PROVE_RCU -extern spinlock_t css_set_lock; #define task_css_set_check(task, __c) \ rcu_dereference_check((task)->cgroups, \ rcu_read_lock_sched_held() || \ @@ -855,4 +855,6 @@ static inline void cgroup_bpf_put(struct cgroup *cgrp) {} #endif /* CONFIG_CGROUP_BPF */ +struct cgroup *task_get_cgroup1(struct task_struct *tsk, int hierarchy_id); + #endif /* _LINUX_CGROUP_H */ diff --git a/kernel/cgroup/cgroup-internal.h b/kernel/cgroup/cgroup-internal.h index 5e17f01..520b90d 100644 --- a/kernel/cgroup/cgroup-internal.h +++ b/kernel/cgroup/cgroup-internal.h @@ -164,7 +164,6 @@ struct cgroup_mgctx { #define DEFINE_CGROUP_MGCTX(name) \ struct cgroup_mgctx name = CGROUP_MGCTX_INIT(name) -extern spinlock_t css_set_lock; extern struct cgroup_subsys *cgroup_subsys[]; extern struct list_head cgroup_roots; diff --git a/kernel/cgroup/cgroup-v1.c b/kernel/cgroup/cgroup-v1.c index c487ffe..f41767f 100644 --- a/kernel/cgroup/cgroup-v1.c +++ b/kernel/cgroup/cgroup-v1.c @@ -1263,6 +1263,39 @@ int cgroup1_get_tree(struct fs_context *fc) return ret; } +/** + * task_get_cgroup1 - Acquires the associated cgroup of a task within a + * specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its + * hierarchy ID. + * @tsk: The target task + * @hierarchy_id: The ID of a cgroup1 hierarchy + * + * On success, the cgroup is returned. On failure, ERR_PTR is returned. + * We limit it to cgroup1 only. + */ +struct cgroup *task_get_cgroup1(struct task_struct *tsk, int hierarchy_id) +{ + struct cgroup *cgrp = ERR_PTR(-ENOENT); + struct cgroup_root *root; + + rcu_read_lock(); + for_each_root(root) { + /* cgroup1 only*/ + if (root == &cgrp_dfl_root) + continue; + if (root->hierarchy_id != hierarchy_id) + continue; + spin_lock_irq(&css_set_lock); + cgrp = task_cgroup_from_root(tsk, root); + if (!cgrp || !cgroup_tryget(cgrp)) + cgrp = ERR_PTR(-ENOENT); + spin_unlock_irq(&css_set_lock); + break; + } + rcu_read_unlock(); + return cgrp; +} + static int __init cgroup1_wq_init(void) { /* From patchwork Sun Oct 29 06:14:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13439726 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 16E8C7494; Sun, 29 Oct 2023 06:14:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="IgHVozCH" Received: from mail-oi1-x230.google.com (mail-oi1-x230.google.com [IPv6:2607:f8b0:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 03B06D3; Sat, 28 Oct 2023 23:14:57 -0700 (PDT) Received: by mail-oi1-x230.google.com with SMTP id 5614622812f47-3b3f6dd612cso2272249b6e.3; Sat, 28 Oct 2023 23:14:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698560096; x=1699164896; 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=t+813JukxzL+6wkyiKgmo5W2H4NKk3atr6arbVRyW+U=; b=IgHVozCH57atnvOPhX+3cWa66l0hLxJpu6rmjZ9JcXqS2MjUtnCVHiAvFzcFVt0Go6 uq+kTHtZrYHDGAkXS1AtWPouYyMzqPMkO48oB9n04IAyczN+Dxk1WPPHOuMPW494xMdS 8IozPCC0Hw3EC2jHIALcAPxe56s3LZqXZ1hkDURsGrnZe2Mxxem1GbdsawRN4HWmgRqH MmpUFOVq3jJj1Kwro4lFU6nUUVbIYOXz1vPU1c/MgWtH+OfzToaFgucpnkWuz6cSZtCT FHneKE4/iyMYIu2qHIvF8I2VChSA0wbL0Y3Hq3yj+8FTcBlIJe6arapYoCZUwFsDtaQl FddA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698560096; x=1699164896; 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=t+813JukxzL+6wkyiKgmo5W2H4NKk3atr6arbVRyW+U=; b=KyHJIPs/fz372NpV1soNAw5wBqHY7PVWiP5ZwW2NaKcBoWsMjXpAoqpwtrQ2z3yynh eJy/rgycE9TkljKewfLfWOpNxIaJod2ToK3Ggy+M//VIpdsFy6qXjQcAFr0BX8mi+h53 igo4iK/J1rGZyEFzGUAf5ErKiyKGQs/VsQG+zdLzizFV5fZhDOEQC4AfcEz3xlKi2B3b TBzDBTLnWsQyTTkPUt6vu94Bf4S/moDjgfdy6npWQ1LT/5TjFHrF5F6g5k4rDgz7LkQN pMl8bdhcVNqP7HlmD9ZmioIH7/zJEd3TRwdcKMd6trRToYsoqLRB8YdrGAAOtEF/Ri// U3kQ== X-Gm-Message-State: AOJu0YwXgqfmxLWe5eNmRIvXIHxGyYo24TGnPFAp5sjTxKh3qbvck/3j Pz6xgvEokGscZTy/tvWT5bM= X-Google-Smtp-Source: AGHT+IEqGHX/nd0eOkGIV3mum7IyRHIYm3fEGFoFZe1TEGey7bK9yTLipeCRTWQzXmmxxQymdArURA== X-Received: by 2002:a05:6808:1a13:b0:3b2:ea7c:c402 with SMTP id bk19-20020a0568081a1300b003b2ea7cc402mr9026932oib.25.1698560096250; Sat, 28 Oct 2023 23:14:56 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:2b5:5400:4ff:fea0:d066]) by smtp.gmail.com with ESMTPSA id m2-20020aa79002000000b006b225011ee5sm3775106pfo.6.2023.10.28.23.14.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 23:14:55 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com, longman@redhat.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, oliver.sang@intel.com, Yafang Shao Subject: [PATCH v3 bpf-next 06/11] bpf: Add a new kfunc for cgroup1 hierarchy Date: Sun, 29 Oct 2023 06:14:33 +0000 Message-Id: <20231029061438.4215-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029061438.4215-1-laoar.shao@gmail.com> References: <20231029061438.4215-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net A new kfunc is added to acquire cgroup1 of a task: - bpf_task_get_cgroup1 Acquires the associated cgroup of a task whithin a specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its hierarchy ID. This new kfunc enables the tracing of tasks within a designated container or cgroup directory in BPF programs. Suggested-by: Tejun Heo Signed-off-by: Yafang Shao Acked-by: Tejun Heo --- kernel/bpf/helpers.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 61f51de..f4cdeae 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2222,6 +2222,25 @@ __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task, rcu_read_unlock(); return ret; } + +/** + * bpf_task_get_cgroup1 - Acquires the associated cgroup of a task within a + * specific cgroup1 hierarchy. The cgroup1 hierarchy is identified by its + * hierarchy ID. + * @task: The target task + * @hierarchy_id: The ID of a cgroup1 hierarchy + * + * On success, the cgroup is returen. On failure, NULL is returned. + */ +__bpf_kfunc struct cgroup * +bpf_task_get_cgroup1(struct task_struct *task, int hierarchy_id) +{ + struct cgroup *cgrp = task_get_cgroup1(task, hierarchy_id); + + if (IS_ERR(cgrp)) + return NULL; + return cgrp; +} #endif /* CONFIG_CGROUPS */ /** @@ -2528,6 +2547,7 @@ __bpf_kfunc void bpf_throw(u64 cookie) 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) #endif BTF_ID_FLAGS(func, bpf_task_from_pid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_throw) From patchwork Sun Oct 29 06:14:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13439727 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1EB78C19; Sun, 29 Oct 2023 06:14:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bQxW/IEB" Received: from mail-oi1-x22e.google.com (mail-oi1-x22e.google.com [IPv6:2607:f8b0:4864:20::22e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98EE6CC; Sat, 28 Oct 2023 23:14:58 -0700 (PDT) Received: by mail-oi1-x22e.google.com with SMTP id 5614622812f47-3b2e22a4004so2437898b6e.3; Sat, 28 Oct 2023 23:14:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698560098; x=1699164898; 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=yBmbTwV/SCzHF6Ex+v7R9Tm3nCh0Ha6QemKh6sWeBKo=; b=bQxW/IEBLqyfhnEamEK0dIJ7KO3ZdkJREX09hT6NoQviYobRqBUhUMcx82f/AXoD2V N9kq5VTM54QSkhFZah9mLSdFe/DfvfNkK1jN0ig8fHy1TWNjGhO95/0Xmvtl9fZTbdzn d1vA5tU4hTEbcs8Mw8E7AzKjGczfjIM0Iv3w4sMSW8We3B3qy8N6jZOLWQIrUJsORo1L jcfPnl8f/6X+sDt7NC6D35num+ujjNQwb+qvQS4j5bOzZ8L5wf1gRW31HU4ojrpTxH5V ipJb2m68Jh5dOJ1X5yPy1Ka0UX6r57jPLSOh0BfM8vYxyYgwTmiKFPKumlujCyJpogWe 2BJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698560098; x=1699164898; 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=yBmbTwV/SCzHF6Ex+v7R9Tm3nCh0Ha6QemKh6sWeBKo=; b=mVvoosrDN8ThE77rkwV7xRzIlu9RgC584wNfXzyZq8iWVyGzkHi3RBI1fNpqb3D4ex NxHgNLlqXs6ZCzE4PycyiSSG0cuuzdYHiw1n4qljVtRgL6bSHDqvPCIGPJwO+AnRx/0a gwobb9ZWcTzVgGJ2fk9xIiBebN1LnchiBE+qtXhyoGSQPVY9RQ0tXe0Bv2SqRHdrQDef PoemZCZ4AABEw0z3L0V+nyN3cBr+HmeuyYOhSkxqCwEl7G3kMShqQi+doNaY0v9Wc3Ir +wBQOOXab5OeI3GDYfnCWRtD9detB/EdH1rR0/A+GqaU0rwHIAgiQV/IJcyJqI61CdvS rPKQ== X-Gm-Message-State: AOJu0Yye9CdRd93U0nHWp+eEXCB4lMKa8t+61ljeqvxBMLZK8grOROZF rSa/rXpT1YQL9BHIbN5sCXk= X-Google-Smtp-Source: AGHT+IGVnXoQqAnMqSaErKSKkEc/768WtOCUQNP5tNBS9BwmfnBafvZocKuIYi5V/5RnQFedOzqp6g== X-Received: by 2002:aca:1909:0:b0:3b2:df82:812e with SMTP id l9-20020aca1909000000b003b2df82812emr7689811oii.32.1698560097914; Sat, 28 Oct 2023 23:14:57 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:2b5:5400:4ff:fea0:d066]) by smtp.gmail.com with ESMTPSA id m2-20020aa79002000000b006b225011ee5sm3775106pfo.6.2023.10.28.23.14.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 23:14:57 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com, longman@redhat.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, oliver.sang@intel.com, Yafang Shao Subject: [PATCH v3 bpf-next 07/11] selftests/bpf: Fix issues in setup_classid_environment() Date: Sun, 29 Oct 2023 06:14:34 +0000 Message-Id: <20231029061438.4215-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029061438.4215-1-laoar.shao@gmail.com> References: <20231029061438.4215-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net If the net_cls subsystem is already mounted, attempting to mount it again in setup_classid_environment() will result in a failure with the error code EBUSY. Despite this, tmpfs will have been successfully mounted at /sys/fs/cgroup/net_cls. Consequently, the /sys/fs/cgroup/net_cls directory will be empty, causing subsequent setup operations to fail. Here's an error log excerpt illustrating the issue when net_cls has already been mounted at /sys/fs/cgroup/net_cls prior to running setup_classid_environment(): - Before that change $ tools/testing/selftests/bpf/test_progs --name=cgroup_v1v2 test_cgroup_v1v2:PASS:server_fd 0 nsec test_cgroup_v1v2:PASS:client_fd 0 nsec test_cgroup_v1v2:PASS:cgroup_fd 0 nsec test_cgroup_v1v2:PASS:server_fd 0 nsec run_test:PASS:skel_open 0 nsec run_test:PASS:prog_attach 0 nsec test_cgroup_v1v2:PASS:cgroup-v2-only 0 nsec (cgroup_helpers.c:248: errno: No such file or directory) Opening Cgroup Procs: /sys/fs/cgroup/net_cls/cgroup.procs (cgroup_helpers.c:540: errno: No such file or directory) Opening cgroup classid: /sys/fs/cgroup/net_cls/cgroup-test-work-dir/net_cls.classid run_test:PASS:skel_open 0 nsec run_test:PASS:prog_attach 0 nsec (cgroup_helpers.c:248: errno: No such file or directory) Opening Cgroup Procs: /sys/fs/cgroup/net_cls/cgroup-test-work-dir/cgroup.procs run_test:FAIL:join_classid unexpected error: 1 (errno 2) test_cgroup_v1v2:FAIL:cgroup-v1v2 unexpected error: -1 (errno 2) (cgroup_helpers.c:248: errno: No such file or directory) Opening Cgroup Procs: /sys/fs/cgroup/net_cls/cgroup.procs #44 cgroup_v1v2:FAIL Summary: 0/0 PASSED, 0 SKIPPED, 1 FAILED - After that change $ tools/testing/selftests/bpf/test_progs --name=cgroup_v1v2 #44 cgroup_v1v2:OK Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yafang Shao --- tools/testing/selftests/bpf/cgroup_helpers.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index 5b1da2a..10b5f42 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -523,10 +523,20 @@ int setup_classid_environment(void) return 1; } - if (mount("net_cls", NETCLS_MOUNT_PATH, "cgroup", 0, "net_cls") && - errno != EBUSY) { - log_err("mount cgroup net_cls"); - return 1; + if (mount("net_cls", NETCLS_MOUNT_PATH, "cgroup", 0, "net_cls")) { + if (errno != EBUSY) { + log_err("mount cgroup net_cls"); + return 1; + } + + if (rmdir(NETCLS_MOUNT_PATH)) { + log_err("rmdir cgroup net_cls"); + return 1; + } + if (umount(CGROUP_MOUNT_DFLT)) { + log_err("umount cgroup base"); + return 1; + } } cleanup_classid_environment(); From patchwork Sun Oct 29 06:14:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13439728 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6B12B8F60; Sun, 29 Oct 2023 06:15:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="miYtJ1lr" Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1FE22D3; Sat, 28 Oct 2023 23:15:00 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-6b5e6301a19so3422930b3a.0; Sat, 28 Oct 2023 23:15:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698560099; x=1699164899; 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=+WGPpwvnjBN7lnD2fLR5fUdnGAqoZjDwFnJqTNXRdHE=; b=miYtJ1lr2EwiNR6HZnqsgKm6jDSke7XxX/744VYSS1x1PAVLSO7QDoKsyjZOIDoQwW 4A9KyRiyic5PJWSZhnz5c9dtGiA8MXqD5tpDogconBnLC0xHVTIdfzuJIBfa5OZK5Amy LqFK0Ciq8gt6El8x0VXzvAA/vKcavIgvx5yAaMAuGQ0b8yf434/B9ZWIVb08voMp9vkQ fxZvo7KLJUP6jtjAxv32Eaq0HfBg6fJqMmCqoX4SmdQVhl73IPkAa4TuaUK+cJPJ/yPv uvgi+F/kBh8oWbCWQNkR8xLEbHfAr1FR4wv/qovkcBzVl++EX2Y0W20fbmN3528R8e7H q6/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698560099; x=1699164899; 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=+WGPpwvnjBN7lnD2fLR5fUdnGAqoZjDwFnJqTNXRdHE=; b=rLvlcpQRYteMxZSUbFyPuzry/kGNSSwgb99XrGTd/DwKpzturCiHT0i/lmRPwxvxSV f3pqSNP70xO+0R7G1o0qgjAD6rXCy8RjxgbUewWatRs13iasB8DWFKgNGNA7lV+2LEek XvsHO4ACgXfYL1R+9ntShUXyhgD28h3l76QeURm7q1fRBUv59IfGhWOgcPON+F50gDYH 6Mef6v/KXpPDPxwmC2HSZvirN/4i8vk0qHOu7mXKhbfRXUVwkCzW0I+AJPe8nDUJw0IX iLTCi8pNK9H4kNjv0LXAjMCv6Bf93YXZVT7rxXshdeqjA8vo467e7oO1Shi0K+Y+yGOR aFBw== X-Gm-Message-State: AOJu0YyyDj8ovTov5PNfGBIlV/vqZ6UQtObJt2KHZpCj2p8MUHgNCuOn kLzEOGdEmRE9G/5dP+a9dF4= X-Google-Smtp-Source: AGHT+IFcHD54lCioxi35XUAefYmbg48pl0iYFvjsVywudDNbnRraEoFm73JMX0cwBjueGpkmZ0A9ig== X-Received: by 2002:a05:6300:8088:b0:17f:fef8:1f3f with SMTP id ap8-20020a056300808800b0017ffef81f3fmr3467702pzc.4.1698560099531; Sat, 28 Oct 2023 23:14:59 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:2b5:5400:4ff:fea0:d066]) by smtp.gmail.com with ESMTPSA id m2-20020aa79002000000b006b225011ee5sm3775106pfo.6.2023.10.28.23.14.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 23:14:59 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com, longman@redhat.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, oliver.sang@intel.com, Yafang Shao Subject: [PATCH v3 bpf-next 08/11] selftests/bpf: Add parallel support for classid Date: Sun, 29 Oct 2023 06:14:35 +0000 Message-Id: <20231029061438.4215-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029061438.4215-1-laoar.shao@gmail.com> References: <20231029061438.4215-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Include the current pid in the classid cgroup path. This way, different testers relying on classid-based configurations will have distinct classid cgroup directories, enabling them to run concurrently. Additionally, we leverage the current pid as the classid, ensuring unique identification. Signed-off-by: Yafang Shao --- tools/testing/selftests/bpf/cgroup_helpers.c | 18 +++++++++++------- tools/testing/selftests/bpf/cgroup_helpers.h | 2 +- tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index 10b5f42..f18649a 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -45,9 +45,12 @@ #define format_parent_cgroup_path(buf, path) \ format_cgroup_path_pid(buf, path, getppid()) -#define format_classid_path(buf) \ - snprintf(buf, sizeof(buf), "%s%s", NETCLS_MOUNT_PATH, \ - CGROUP_WORK_DIR) +#define format_classid_path_pid(buf, pid) \ + snprintf(buf, sizeof(buf), "%s%s%d", NETCLS_MOUNT_PATH, \ + CGROUP_WORK_DIR, pid) + +#define format_classid_path(buf) \ + format_classid_path_pid(buf, getpid()) static __thread bool cgroup_workdir_mounted; @@ -551,15 +554,16 @@ int setup_classid_environment(void) /** * set_classid() - Set a cgroupv1 net_cls classid - * @id: the numeric classid * - * Writes the passed classid into the cgroup work dir's net_cls.classid + * Writes the classid into the cgroup work dir's net_cls.classid * file in order to later on trigger socket tagging. * + * We leverage the current pid as the classid, ensuring unique identification. + * * On success, it returns 0, otherwise on failure it returns 1. If there * is a failure, it prints the error to stderr. */ -int set_classid(unsigned int id) +int set_classid(void) { char cgroup_workdir[PATH_MAX - 42]; char cgroup_classid_path[PATH_MAX + 1]; @@ -575,7 +579,7 @@ int set_classid(unsigned int id) return 1; } - if (dprintf(fd, "%u\n", id) < 0) { + if (dprintf(fd, "%u\n", getpid()) < 0) { log_err("Setting cgroup classid"); rc = 1; } diff --git a/tools/testing/selftests/bpf/cgroup_helpers.h b/tools/testing/selftests/bpf/cgroup_helpers.h index 5c2cb9c..92fc41d 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.h +++ b/tools/testing/selftests/bpf/cgroup_helpers.h @@ -29,7 +29,7 @@ int write_cgroup_file_parent(const char *relative_path, const char *file, void cleanup_cgroup_environment(void); /* cgroupv1 related */ -int set_classid(unsigned int id); +int set_classid(void); int join_classid(void); int setup_classid_environment(void); diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c index 9026b42..addf720 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_v1v2.c @@ -71,7 +71,7 @@ void test_cgroup_v1v2(void) } ASSERT_OK(run_test(cgroup_fd, server_fd, false), "cgroup-v2-only"); setup_classid_environment(); - set_classid(42); + set_classid(); ASSERT_OK(run_test(cgroup_fd, server_fd, true), "cgroup-v1v2"); cleanup_classid_environment(); close(server_fd); From patchwork Sun Oct 29 06:14:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13439729 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CB8C3610D; Sun, 29 Oct 2023 06:15:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="eJkWcyjN" Received: from mail-io1-xd2b.google.com (mail-io1-xd2b.google.com [IPv6:2607:f8b0:4864:20::d2b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46495CC; Sat, 28 Oct 2023 23:15:02 -0700 (PDT) Received: by mail-io1-xd2b.google.com with SMTP id ca18e2360f4ac-7a6907e9aa8so117736139f.1; Sat, 28 Oct 2023 23:15:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698560101; x=1699164901; 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=4ke1D6m74mgBawz9JZev+dTdh8Sh2Lm5iPjGvjgkU9M=; b=eJkWcyjNP49fR4m87rktCvvNFThRJDjAEHzrgNS2Qc7fy83P08/Szdk0AzJ3Eykn/y V0IesyqIbfZIbHjiUqjFDcmhhL97XNakoieQzZDKgFnGR5KP5c6q+mvYSTQ/jd7ECNgU cN/v6sO4h/+xQdGH5WqhiQhMnOVvCicbirZQVlWD5/R7n4KxwbbTLt6LjoimfU5ZzHsY 2ZXfKlupdnbVA98NOHYR8ERjtfPju2MjYTinRSkYI/sk+s54Q0iM6rRFG6yQ4XQCcXPe 2t5f8pm9/t7i/ExmbNQfu9+jEC6UOaIIYfcKVfx1n28NGwuJQjTYEKt8T1BptLkU3UlP OcTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698560101; x=1699164901; 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=4ke1D6m74mgBawz9JZev+dTdh8Sh2Lm5iPjGvjgkU9M=; b=HiZ53oik+btiPif5ojebj1z02n++fPXOmOwm9HMvVIqK1MhHNdE8a+rsdMjxP1ZajG mi/l5TyiFIrURAEo0hxCawfrdBBSL5IwmhKjkMN/qV8s0qt9SjXJXEw0qEqxzBD2SNdf WbounFY9A9fiOECYx2sjZsrkzre4f65MtxA3y4YtIzfqTH1oLPa10E0xaJVs0Z0JuVox vOiogMBpMWVlGxhxUEJ69D7MrMUg0pFEcVl+wDloNBC5JqNDh0FYe7HO9rIb6aQsj3Gf EQpe1K90gDjILbl638O6nz0LkSyvt/r1jJOY/uxfmj+yzk/nmA1O1rQRd53eaBL41Qdd lhqg== X-Gm-Message-State: AOJu0YxtFtNDnFu+b1TANkJfSJfVXi/seAJk8/BVwjK+nYV46VcnmzmX kMajiAyNtzgdiW+eyTkFnpE= X-Google-Smtp-Source: AGHT+IH8707bd7hPq7eNfJWhCFO60YW5a3Ui0CaWpUnW1BC5KC3rKfrzpXppmvH7ZqdiqXow3HAf2w== X-Received: by 2002:a05:6602:154a:b0:79f:e707:8813 with SMTP id h10-20020a056602154a00b0079fe7078813mr10669797iow.4.1698560101327; Sat, 28 Oct 2023 23:15:01 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:2b5:5400:4ff:fea0:d066]) by smtp.gmail.com with ESMTPSA id m2-20020aa79002000000b006b225011ee5sm3775106pfo.6.2023.10.28.23.14.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 23:15:00 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com, longman@redhat.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, oliver.sang@intel.com, Yafang Shao Subject: [PATCH v3 bpf-next 09/11] selftests/bpf: Add a new cgroup helper get_classid_cgroup_id() Date: Sun, 29 Oct 2023 06:14:36 +0000 Message-Id: <20231029061438.4215-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029061438.4215-1-laoar.shao@gmail.com> References: <20231029061438.4215-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Introduce a new helper function to retrieve the cgroup ID from a net_cls cgroup directory. Signed-off-by: Yafang Shao --- tools/testing/selftests/bpf/cgroup_helpers.c | 28 ++++++++++++++++++++++------ tools/testing/selftests/bpf/cgroup_helpers.h | 1 + 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index f18649a..63bfa72 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -422,26 +422,23 @@ int create_and_get_cgroup(const char *relative_path) } /** - * get_cgroup_id() - Get cgroup id for a particular cgroup path - * @relative_path: The cgroup path, relative to the workdir, to join + * get_cgroup_id_from_path - Get cgroup id for a particular cgroup path + * @cgroup_workdir: The absolute cgroup path * * On success, it returns the cgroup id. On failure it returns 0, * which is an invalid cgroup id. * If there is a failure, it prints the error to stderr. */ -unsigned long long get_cgroup_id(const char *relative_path) +unsigned long long get_cgroup_id_from_path(const char *cgroup_workdir) { int dirfd, err, flags, mount_id, fhsize; union { unsigned long long cgid; unsigned char raw_bytes[8]; } id; - char cgroup_workdir[PATH_MAX + 1]; struct file_handle *fhp, *fhp2; unsigned long long ret = 0; - format_cgroup_path(cgroup_workdir, relative_path); - dirfd = AT_FDCWD; flags = 0; fhsize = sizeof(*fhp); @@ -477,6 +474,14 @@ unsigned long long get_cgroup_id(const char *relative_path) return ret; } +unsigned long long get_cgroup_id(const char *relative_path) +{ + char cgroup_workdir[PATH_MAX + 1]; + + format_cgroup_path(cgroup_workdir, relative_path); + return get_cgroup_id_from_path(cgroup_workdir); +} + int cgroup_setup_and_join(const char *path) { int cg_fd; @@ -621,3 +626,14 @@ void cleanup_classid_environment(void) join_cgroup_from_top(NETCLS_MOUNT_PATH); nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT); } + +/** + * get_classid_cgroup_id - Get the cgroup id of a net_cls cgroup + */ +unsigned long long get_classid_cgroup_id(void) +{ + char cgroup_workdir[PATH_MAX + 1]; + + format_classid_path(cgroup_workdir); + return get_cgroup_id_from_path(cgroup_workdir); +} diff --git a/tools/testing/selftests/bpf/cgroup_helpers.h b/tools/testing/selftests/bpf/cgroup_helpers.h index 92fc41d..e71da4e 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.h +++ b/tools/testing/selftests/bpf/cgroup_helpers.h @@ -31,6 +31,7 @@ int write_cgroup_file_parent(const char *relative_path, const char *file, /* cgroupv1 related */ int set_classid(void); int join_classid(void); +unsigned long long get_classid_cgroup_id(void); int setup_classid_environment(void); void cleanup_classid_environment(void); From patchwork Sun Oct 29 06:14:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13439730 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 049DA8F60; Sun, 29 Oct 2023 06:15:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hj42s4Df" Received: from mail-pf1-x429.google.com (mail-pf1-x429.google.com [IPv6:2607:f8b0:4864:20::429]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A7CED3; Sat, 28 Oct 2023 23:15:03 -0700 (PDT) Received: by mail-pf1-x429.google.com with SMTP id d2e1a72fcca58-6bf03b98b9bso3207597b3a.1; Sat, 28 Oct 2023 23:15:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698560103; x=1699164903; 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=sa07Z9Tnf6gPzoCSdY0mE9Qwjplz9eHiRUctPRFvz3A=; b=hj42s4DflsqbcUzB5arhulJaGqjIKybrpY6DRvZkqYtZepvTU8dn9KxNFkOgwGeMe6 DjT5/0fFZlcUWSPmD+osFFWCHTfjnMISftRNSPhm/vPFVWmFJ8G8XjmJBGISnRL6WUVG ecLx6JRPNeOE9YynX/VNIkiCP6gI7r7SHv3aqz5uRm64kAbP4M5By5RnvAYgGHYGdgz+ 6NeW+uKbfkkzkVQTQeTrS8uRQC97+gC1DkT1+ukQCv2nAHwlqG/PX/goLEK13bOz9rNR RwZBygusFy6ECIIWExQa385d1HypvL6mJhOFkUwW53a0OWdgW7uZfiJNcqwosWsAmuKG /7CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698560103; x=1699164903; 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=sa07Z9Tnf6gPzoCSdY0mE9Qwjplz9eHiRUctPRFvz3A=; b=kcod7RPWunKvXSvykCNWcUNiKKZXiDNp7i/7+Bdiy1VCVNCOn4EEybm4nF3JwfUpOp ViNfh+WxJFsSCvxypSODLHbOhOQbL8yDltmwxWvuYIC53tIZVQX6FSxkDkkuvJOXXJO4 0a/G+fQDEjbMsP215vjjpXjO+0aThGKz2vw04HTiXCi1maLeewfO4T95j2u6VIiBS5q5 5t2VQ/b8E3im7DkVEToZYK+DPdT3PAYIPb+saVAfLpB07v7vaeKk7vzdh9WlXrK0Gs57 RSmObU4acfKGw0e+BSzIt0ZC3Ne5ic0Td/MlB3kqn5SK9+ROrc/91r9uqLLaJTrF3P/e NuQg== X-Gm-Message-State: AOJu0YxzbfgUz2P3Wlu1Z5Iq2k8qVC3wa7tkHle8XxRtpd77LwQaXS4f WeinKBv16VHqk08oMC/lHpk= X-Google-Smtp-Source: AGHT+IGjaDJASCPUTLvbci15ulVbGCl/dqqbojUBn6MEr8alcSorg0sHR4dy08CSeg9Zaia2lSOzIg== X-Received: by 2002:a05:6a21:a583:b0:161:aef5:6395 with SMTP id gd3-20020a056a21a58300b00161aef56395mr9055218pzc.24.1698560102960; Sat, 28 Oct 2023 23:15:02 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:2b5:5400:4ff:fea0:d066]) by smtp.gmail.com with ESMTPSA id m2-20020aa79002000000b006b225011ee5sm3775106pfo.6.2023.10.28.23.15.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 23:15:02 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com, longman@redhat.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, oliver.sang@intel.com, Yafang Shao Subject: [PATCH v3 bpf-next 10/11] selftests/bpf: Add a new cgroup helper get_cgroup_hierarchy_id() Date: Sun, 29 Oct 2023 06:14:37 +0000 Message-Id: <20231029061438.4215-11-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029061438.4215-1-laoar.shao@gmail.com> References: <20231029061438.4215-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net A new cgroup helper function, get_cgroup1_hierarchy_id(), has been introduced to obtain the ID of a cgroup1 hierarchy based on the provided cgroup name. This cgroup name can be obtained from the /proc/self/cgroup file. Signed-off-by: Yafang Shao --- tools/testing/selftests/bpf/cgroup_helpers.c | 49 ++++++++++++++++++++++++++++ tools/testing/selftests/bpf/cgroup_helpers.h | 1 + 2 files changed, 50 insertions(+) diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index 63bfa72..d75bb87 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -637,3 +637,52 @@ unsigned long long get_classid_cgroup_id(void) format_classid_path(cgroup_workdir); return get_cgroup_id_from_path(cgroup_workdir); } + +/** + * get_cgroup1_hierarchy_id - Retrieves the ID of a cgroup1 hierarchy from the cgroup1 name + * @cgrp_name: The cgroup1 name, which can be retrieved from /proc/self/cgroup. + */ +int get_cgroup1_hierarchy_id(const char *cgrp_name) +{ + char *c, *c2, *c3, *c4; + bool found = false; + char line[1024]; + FILE *file; + int i, id; + + if (!cgrp_name) + return -1; + + file = fopen("/proc/self/cgroup", "r"); + if (!file) { + log_err("fopen /proc/self/cgroup"); + return -1; + } + + while (fgets(line, 1024, file)) { + i = 0; + for (c = strtok_r(line, ":", &c2); c && i < 2; c = strtok_r(NULL, ":", &c2)) { + if (i == 0) { + id = strtol(c, NULL, 10); + } else if (i == 1) { + if (!strcmp(c, cgrp_name)) { + found = true; + break; + } + + /* Multiple subsystems may share one single mount point */ + for (c3 = strtok_r(c, ",", &c4); c3; + c3 = strtok_r(NULL, ",", &c4)) { + if (!strcmp(c, cgrp_name)) { + found = true; + break; + } + } + } + i++; + } + if (found) + break; + } + return found ? id : -1; +} diff --git a/tools/testing/selftests/bpf/cgroup_helpers.h b/tools/testing/selftests/bpf/cgroup_helpers.h index e71da4e..a80c417 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.h +++ b/tools/testing/selftests/bpf/cgroup_helpers.h @@ -20,6 +20,7 @@ int write_cgroup_file_parent(const char *relative_path, const char *file, int create_and_get_cgroup(const char *relative_path); void remove_cgroup(const char *relative_path); unsigned long long get_cgroup_id(const char *relative_path); +int get_cgroup1_hierarchy_id(const char *cgrp_name); int join_cgroup(const char *relative_path); int join_root_cgroup(void); From patchwork Sun Oct 29 06:14:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13439731 X-Patchwork-Delegate: bpf@iogearbox.net Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 252328F78; Sun, 29 Oct 2023 06:15:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Bx/2qO8f" Received: from mail-oi1-x230.google.com (mail-oi1-x230.google.com [IPv6:2607:f8b0:4864:20::230]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6B6CED9; Sat, 28 Oct 2023 23:15:05 -0700 (PDT) Received: by mail-oi1-x230.google.com with SMTP id 5614622812f47-3b2e44c7941so2432874b6e.2; Sat, 28 Oct 2023 23:15:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698560105; x=1699164905; 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=X+y5IvZGTJVNsINeBbqs6/CPtfFJoedZnxRZu6VJyjA=; b=Bx/2qO8f0EcsZOk2rNn4bT+ZOjtODoYRAUdn12LwOjZRDiGJ4Y23FQaXqWZlQAkxnt WdIueM1Nk5VWo61/nkkwlnkqUPn9l0mSo5tz7ggZB1fBhJWc79RZZahNw8T5qVL+obEF RPGf0QOhkZXwWOlJxgzO48AUaGZxLVS/OdgcKZp5ZEq42MAFa6zZR26pAb2+Mn2vYIUW MGGMTmMUm4kRGIiEGgKAd8q1fuXzF061S95bjmuar7NXa1csKO+dEDyeWv8tl2BHxYrE /UEyThaKyQLcw2QEzrvo/je+jCQPjWuQhv9aXC+IPOD2ow6Ydc6409dv/bHJ6/FClww1 vMLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698560105; x=1699164905; 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=X+y5IvZGTJVNsINeBbqs6/CPtfFJoedZnxRZu6VJyjA=; b=h5xz4AGfepHLM6+a5k1w2oMsk9CycyEY3pFjpPxPGM0KZVgVqj2PzxNIpSGX/qn+nQ kbuoILFfjMhkkIsJ8zK0njHOhdME+Cmaz38omoEzXAZ6M2YwWZgWIFP/K3IpEimGLujA yyToh+SknBdgtE5a0GG7iqbpUk9xUCrNRJ7Epj7aEE3MeYNYHO7Aq99+/wjX25r3aEbc T8AhakwK3FnAy2mzHmgM/RIlGMwpeqZHdzxFN2xBNUPDzBakbzMvO/z3R7wrAq95R/gG iUeY8zQXN6O6xgu/HCFszP02ZtSZcifD0i/WXfrjx1Oeel9I6HGoAMj7m+BMuTE2JV3i a1sw== X-Gm-Message-State: AOJu0Yx77CuWhD7H7t8I6yuLN/jiShFiPLxVZRJf2/uKHrFDx3KZm+vx Rn9lyxUDFlX9L70ZLvNNAJ8= X-Google-Smtp-Source: AGHT+IEL/zFegIBdXR5fcscjYR4WrvXL+mrGreh3egZnEoN+g71G0TvRDGrDqJL6UgaUuHlcWdwLFw== X-Received: by 2002:a05:6808:13d6:b0:3b2:ef9a:4339 with SMTP id d22-20020a05680813d600b003b2ef9a4339mr8876787oiw.49.1698560104727; Sat, 28 Oct 2023 23:15:04 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:2b5:5400:4ff:fea0:d066]) by smtp.gmail.com with ESMTPSA id m2-20020aa79002000000b006b225011ee5sm3775106pfo.6.2023.10.28.23.15.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Oct 2023 23:15:04 -0700 (PDT) From: Yafang Shao To: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev, kpsingh@kernel.org, sdf@google.com, haoluo@google.com, jolsa@kernel.org, tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, yosryahmed@google.com, mkoutny@suse.com, sinquersw@gmail.com, longman@redhat.com Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, oliver.sang@intel.com, Yafang Shao Subject: [PATCH v3 bpf-next 11/11] selftests/bpf: Add selftests for cgroup1 hierarchy Date: Sun, 29 Oct 2023 06:14:38 +0000 Message-Id: <20231029061438.4215-12-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231029061438.4215-1-laoar.shao@gmail.com> References: <20231029061438.4215-1-laoar.shao@gmail.com> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: bpf@iogearbox.net Add selftests for cgroup1 hierarchy. The result as follows, $ tools/testing/selftests/bpf/test_progs --name=cgroup1_hierarchy #36/1 cgroup1_hierarchy/test_cgroup1_hierarchy:OK #36/2 cgroup1_hierarchy/test_root_cgid:OK #36/3 cgroup1_hierarchy/test_invalid_level:OK #36/4 cgroup1_hierarchy/test_invalid_cgid:OK #36/5 cgroup1_hierarchy/test_invalid_hid:OK #36/6 cgroup1_hierarchy/test_invalid_cgrp_name:OK #36/7 cgroup1_hierarchy/test_invalid_cgrp_name2:OK #36/8 cgroup1_hierarchy/test_sleepable_prog:OK #36 cgroup1_hierarchy:OK Summary: 1/8 PASSED, 0 SKIPPED, 0 FAILED Besides, I also did some stress test similar to the patch #2 in this series, as follows (with CONFIG_PROVE_RCU_LIST enabled): - Continuously mounting and unmounting named cgroups in some tasks, for example: cgrp_name=$1 while true do mount -t cgroup -o none,name=$cgrp_name none /$cgrp_name umount /$cgrp_name done - Continuously run this selftest concurrently, while true; do ./test_progs --name=cgroup1_hierarchy; done They can ran successfully without any RCU warnings in dmesg. Signed-off-by: Yafang Shao --- .../selftests/bpf/prog_tests/cgroup1_hierarchy.c | 159 +++++++++++++++++++++ .../selftests/bpf/progs/test_cgroup1_hierarchy.c | 72 ++++++++++ 2 files changed, 231 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c create mode 100644 tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c b/tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c new file mode 100644 index 0000000..4aafbc9 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/cgroup1_hierarchy.c @@ -0,0 +1,159 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (C) 2023 Yafang Shao */ + +#include +#include +#include +#include "cgroup_helpers.h" +#include "test_cgroup1_hierarchy.skel.h" + +static void bpf_cgroup1(struct test_cgroup1_hierarchy *skel) +{ + int err; + + /* Attach LSM prog first */ + skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run); + if (!ASSERT_OK_PTR(skel->links.lsm_run, "lsm_attach")) + return; + + /* LSM prog will be triggered when attaching fentry */ + skel->links.fentry_run = bpf_program__attach_trace(skel->progs.fentry_run); + ASSERT_NULL(skel->links.fentry_run, "fentry_attach_fail"); + + err = bpf_link__destroy(skel->links.lsm_run); + ASSERT_OK(err, "destroy_lsm"); + skel->links.lsm_run = NULL; +} + +static void bpf_cgroup1_sleepable(struct test_cgroup1_hierarchy *skel) +{ + int err; + + /* Attach LSM prog first */ + skel->links.lsm_s_run = bpf_program__attach_lsm(skel->progs.lsm_s_run); + if (!ASSERT_OK_PTR(skel->links.lsm_s_run, "lsm_attach")) + return; + + /* LSM prog will be triggered when attaching fentry */ + skel->links.fentry_run = bpf_program__attach_trace(skel->progs.fentry_run); + ASSERT_NULL(skel->links.fentry_run, "fentry_attach_fail"); + + err = bpf_link__destroy(skel->links.lsm_s_run); + ASSERT_OK(err, "destroy_lsm"); + skel->links.lsm_s_run = NULL; +} + +static void bpf_cgroup1_invalid_id(struct test_cgroup1_hierarchy *skel) +{ + int err; + + /* Attach LSM prog first */ + skel->links.lsm_run = bpf_program__attach_lsm(skel->progs.lsm_run); + if (!ASSERT_OK_PTR(skel->links.lsm_run, "lsm_attach")) + return; + + /* LSM prog will be triggered when attaching fentry */ + skel->links.fentry_run = bpf_program__attach_trace(skel->progs.fentry_run); + if (!ASSERT_OK_PTR(skel->links.fentry_run, "fentry_attach_success")) + goto cleanup; + + err = bpf_link__destroy(skel->links.lsm_run); + ASSERT_OK(err, "destroy_lsm"); + skel->links.lsm_run = NULL; + +cleanup: + err = bpf_link__destroy(skel->links.fentry_run); + ASSERT_OK(err, "destroy_fentry"); + skel->links.fentry_run = NULL; +} + +void test_cgroup1_hierarchy(void) +{ + struct test_cgroup1_hierarchy *skel; + __u64 current_cgid; + int hid, err; + + skel = test_cgroup1_hierarchy__open(); + if (!ASSERT_OK_PTR(skel, "open")) + return; + + skel->bss->target_pid = getpid(); + + err = bpf_program__set_attach_target(skel->progs.fentry_run, 0, "bpf_fentry_test1"); + if (!ASSERT_OK(err, "fentry_set_target")) + goto destroy; + + err = test_cgroup1_hierarchy__load(skel); + if (!ASSERT_OK(err, "load")) + goto destroy; + + /* Setup cgroup1 hierarchy */ + err = setup_classid_environment(); + if (!ASSERT_OK(err, "setup_classid_environment")) + goto destroy; + + err = join_classid(); + if (!ASSERT_OK(err, "join_cgroup1")) + goto cleanup; + + current_cgid = get_classid_cgroup_id(); + if (!ASSERT_GE(current_cgid, 0, "cgroup1 id")) + goto cleanup; + + hid = get_cgroup1_hierarchy_id("net_cls"); + if (!ASSERT_GE(hid, 0, "cgroup1 id")) + goto cleanup; + skel->bss->target_hid = hid; + + if (test__start_subtest("test_cgroup1_hierarchy")) { + skel->bss->target_ancestor_cgid = current_cgid; + bpf_cgroup1(skel); + } + + if (test__start_subtest("test_root_cgid")) { + skel->bss->target_ancestor_cgid = 1; + skel->bss->target_ancestor_level = 0; + bpf_cgroup1(skel); + } + + if (test__start_subtest("test_invalid_level")) { + skel->bss->target_ancestor_cgid = 1; + skel->bss->target_ancestor_level = 1; + bpf_cgroup1_invalid_id(skel); + } + + if (test__start_subtest("test_invalid_cgid")) { + skel->bss->target_ancestor_cgid = 0; + bpf_cgroup1_invalid_id(skel); + } + + if (test__start_subtest("test_invalid_hid")) { + skel->bss->target_ancestor_cgid = 1; + skel->bss->target_ancestor_level = 0; + skel->bss->target_hid = -1; + bpf_cgroup1_invalid_id(skel); + } + + if (test__start_subtest("test_invalid_cgrp_name")) { + skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cl"); + skel->bss->target_ancestor_cgid = current_cgid; + bpf_cgroup1_invalid_id(skel); + } + + if (test__start_subtest("test_invalid_cgrp_name2")) { + skel->bss->target_hid = get_cgroup1_hierarchy_id("net_cls,"); + skel->bss->target_ancestor_cgid = current_cgid; + bpf_cgroup1_invalid_id(skel); + } + + if (test__start_subtest("test_sleepable_prog")) { + skel->bss->target_hid = hid; + skel->bss->target_ancestor_cgid = current_cgid; + bpf_cgroup1_sleepable(skel); + } + +cleanup: + cleanup_classid_environment(); +destroy: + test_cgroup1_hierarchy__destroy(skel); +} diff --git a/tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c b/tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c new file mode 100644 index 0000000..979ff4e --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: GPL-2.0 +//#endif +/* Copyright (C) 2023 Yafang Shao */ + +#include "vmlinux.h" +#include +#include +#include + +__u32 target_ancestor_level; +__u64 target_ancestor_cgid; +int target_pid, target_hid; + +struct cgroup *bpf_task_get_cgroup1(struct task_struct *task, int hierarchy_id) __ksym; +struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level) __ksym; +void bpf_cgroup_release(struct cgroup *cgrp) __ksym; + +static int bpf_link_create_verify(int cmd) +{ + struct cgroup *cgrp, *ancestor; + struct task_struct *task; + int ret = 0; + + if (cmd != BPF_LINK_CREATE) + return 0; + + task = bpf_get_current_task_btf(); + + /* Then it can run in parallel with others */ + if (task->pid != target_pid) + return 0; + + cgrp = bpf_task_get_cgroup1(task, target_hid); + if (!cgrp) + return 0; + + /* Refuse it if its cgid or its ancestor's cgid is the target cgid */ + if (cgrp->kn->id == target_ancestor_cgid) + ret = -1; + + ancestor = bpf_cgroup_ancestor(cgrp, target_ancestor_level); + if (!ancestor) + goto out; + + if (ancestor->kn->id == target_ancestor_cgid) + ret = -1; + bpf_cgroup_release(ancestor); + +out: + bpf_cgroup_release(cgrp); + return ret; +} + +SEC("lsm/bpf") +int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size) +{ + return bpf_link_create_verify(cmd); +} + +SEC("lsm.s/bpf") +int BPF_PROG(lsm_s_run, int cmd, union bpf_attr *attr, unsigned int size) +{ + return bpf_link_create_verify(cmd); +} + +SEC("fentry") +int BPF_PROG(fentry_run) +{ + return 0; +} + +char _license[] SEC("license") = "GPL";