From patchwork Tue Oct 17 12:45: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: 13425095 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 E6A1A405E3 for ; Tue, 17 Oct 2023 12:46:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="E0EWeZq6" Received: from mail-oo1-xc35.google.com (mail-oo1-xc35.google.com [IPv6:2607:f8b0:4864:20::c35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76F7E10E; Tue, 17 Oct 2023 05:46:07 -0700 (PDT) Received: by mail-oo1-xc35.google.com with SMTP id 006d021491bc7-581de3e691dso226036eaf.3; Tue, 17 Oct 2023 05:46:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697546767; x=1698151567; 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=yCXtSvPvz316d1Voc+hvRatGwRHZV08vgvHRUKG7YWA=; b=E0EWeZq6RgytvYJvN0y+Ga/WkK88rEYn8Lr8Rqt5vb4L1Ep0p6901ta9lrpYVNnpC3 IBoPqV+Ibm9CpSQZvUVK7Cm2TkOHROuO03+jBaPuoygUANzE07jrCZfp+uj+CPLAURct RzBt4xm4Nr2o3i/VyAu/DqBMF8N1DIoSjgqscDBHwE6gdBnlW7NXD/aEXHsQhG61fXWl cL7GuPDLFAVVXCIZnFYZOAHkFlbTO632PZpkSIyobr4nvqTyaKflJ7igOM4KiSgaLiKm bv5AIidOprjR9hoDVkxo0BKjHDQFDeWRZEqjw1+S7GI6SVyFu6UlWQnIk0noxc2Hm32q JkEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697546767; x=1698151567; 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=yCXtSvPvz316d1Voc+hvRatGwRHZV08vgvHRUKG7YWA=; b=O7QvY/snbobqJ3/l+NCmGLDTm1eS7v7Cd1rwLQABsAV3zLDvSDG6Y6HxM7t2dDbwLl tvtQZOiO38txENVY5EQx6QwbCbDpziPhDzlkuY0lNeIsn0zvu+88WNnAy89BXBjvNA0O uu8d6mjZw2Uo9KB+CPYhDKpRzBCb3+/5XaRWmT/TdQEUxyHdephA0jg+JNEUZjBaOTUG 8JW57WUWf2mwcXm+vvlixKRzkh31pV1gtYNVqVuYPGRZ0tIz+dYVXEs+Y3EJKbLd4Tax 0lb2vtRFMzRIAotVA1ZoHTm6C7GPGEIA/i1VbRZMHTLYuzk079CaifTQ84Tifn7ZAXN8 /2Ug== X-Gm-Message-State: AOJu0YwkToMh8W2yY28N/ljGVVixJh8t27iwlEc/P695ySdBojqEVgSp EDbeF3XIElvA6pOnsdDxXBvDcKc7/NOEuSpJ X-Google-Smtp-Source: AGHT+IEEJqflxC8e3DYCx6Y5qEdFTsXJBr8j9YkChDHIK4M0U4dIb2kAryppGN93U4HIcrSrwzxp0A== X-Received: by 2002:a05:6359:100e:b0:166:cc78:ee9b with SMTP id ib14-20020a056359100e00b00166cc78ee9bmr2385097rwb.8.1697546766634; Tue, 17 Oct 2023 05:46:06 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:3b2:5400:4ff:fe9b:d21b]) by smtp.gmail.com with ESMTPSA id fa36-20020a056a002d2400b006bdf4dfbe0dsm1375595pfb.12.2023.10.17.05.46.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 05:46:06 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 1/9] cgroup: Make operations on the cgroup root_list RCU safe Date: Tue, 17 Oct 2023 12:45:38 +0000 Message-Id: <20231017124546.24608-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231017124546.24608-1-laoar.shao@gmail.com> References: <20231017124546.24608-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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC 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 --- include/linux/cgroup-defs.h | 1 + kernel/cgroup/cgroup-internal.h | 3 ++- kernel/cgroup/cgroup.c | 17 ++++++++++------- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/linux/cgroup-defs.h b/include/linux/cgroup-defs.h index f1b3151ac30b..8505eeae6e41 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 c56071f150f2..321af20ea15f 100644 --- a/kernel/cgroup/cgroup-internal.h +++ b/kernel/cgroup/cgroup-internal.h @@ -170,7 +170,8 @@ extern struct list_head cgroup_roots; /* 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 1fb7f562289d..bae8f9f27792 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); if (!list_empty(&root->root_list)) { - list_del(&root->root_list); + list_del_rcu(&root->root_list); cgroup_root_count--; } @@ -1386,13 +1386,15 @@ static inline struct cgroup *__cset_cgroup_from_root(struct css_set *cset, } } - BUG_ON(!res_cgroup); + WARN_ON_ONCE(!res_cgroup && lockdep_is_held(&cgroup_mutex)); return res_cgroup; } /* * look up cgroup associated with current task's cgroup namespace on the - * specified hierarchy + * specified hierarchy. Umount synchronization is ensured via VFS layer, + * so we don't have to hold cgroup_mutex to prevent the root from being + * destroyed. */ static struct cgroup * current_cgns_cgroup_from_root(struct cgroup_root *root) @@ -1445,7 +1447,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); @@ -1453,7 +1454,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) @@ -2097,7 +2100,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 Tue Oct 17 12:45:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13425096 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 9507941A80 for ; Tue, 17 Oct 2023 12:46:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Shb7bDIK" Received: from mail-oo1-xc35.google.com (mail-oo1-xc35.google.com [IPv6:2607:f8b0:4864:20::c35]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93AB2FB; Tue, 17 Oct 2023 05:46:09 -0700 (PDT) Received: by mail-oo1-xc35.google.com with SMTP id 006d021491bc7-57bca5b9b0aso2950763eaf.3; Tue, 17 Oct 2023 05:46:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697546769; x=1698151569; 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=TH+izS6IgAWJi6hgilQUZOMAoXXgYkaiGHHHch4343w=; b=Shb7bDIKpkX13ahbT3Wu08UXssFoOBiGzVsCW1UdYv+L7dDcpVylFhiVkzzDJoXLae zHRFsykCKknPNzrzIP+37LVeim4RhDSt8EyuQO7UOvCbP0uYeLbCzRGihpoLcRZzWsKI OpKdIQTzklapeFVsEwjsk1LFr750FLCjcIY4EyIpIQX9Nz3d+HPqfd+sb5M9ZCQCQCbp 7NEMwAqeOTS13rgnOGfWmdZdEmWdmE4IzXv8ndZ7xzK+QraRAed2koSumFVwES0CcuuN 226OP/qxjGxVHdaQCZ9PsVtwpXCAjE7hqxUW5A8XRruMcaGi5lQAfYwzRAzu5guoiDYi pMjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697546769; x=1698151569; 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=TH+izS6IgAWJi6hgilQUZOMAoXXgYkaiGHHHch4343w=; b=mAHqAltRGLMUKnapH/29GYvUOjPvUKeK83GgVB4EZejQxz5apo9cJ6J5oH3ZBtIrX9 OI8vjPUVgRdntxKJOzn0LUIkTe2OWomuxhV09gt7GEnAmhTu1wy/mK0U4EXCYB4TaQjM xn/my5W25gkVl1pqpKzM7roWAG1gyz/62n00BYTTZl9wwpR6gz9tHwsDGXZ20HbMO7iP zh0e8mMQrArrFs+2YhIsSWd184XNISQ6PwjBqQ6eG+ZQqz2nFYGWxzfnOpV+wPHZb69j MwRODkJN2/2vdckgtQNsyIcFrSzwE2nS9T+TUNaeU+0Xd5/9y0rr3SWBeheyqNZ4EaRz 3WHg== X-Gm-Message-State: AOJu0YwYzrY4UqHYRe5s6TIlVKjrHb4tgLZmXfHhaSTCISxbTtQU5ewk kNLMQ1JbOmcMbt30KfIRcnM= X-Google-Smtp-Source: AGHT+IHom0u3EVjkJF7bWSDQ7AT/KDYUsJY1p0g4trXbL5i3GhbvxiGdUHqpMjLfBe5Dqp2kbJ4BYw== X-Received: by 2002:a05:6359:d0f:b0:133:595:1c with SMTP id gp15-20020a0563590d0f00b001330595001cmr1849779rwb.31.1697546768245; Tue, 17 Oct 2023 05:46:08 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:3b2:5400:4ff:fe9b:d21b]) by smtp.gmail.com with ESMTPSA id fa36-20020a056a002d2400b006bdf4dfbe0dsm1375595pfb.12.2023.10.17.05.46.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 05:46:07 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 2/9] cgroup: Eliminate the need for cgroup_mutex in proc_cgroup_show() Date: Tue, 17 Oct 2023 12:45:39 +0000 Message-Id: <20231017124546.24608-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231017124546.24608-1-laoar.shao@gmail.com> References: <20231017124546.24608-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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC 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. It's worth noting that this change can also catch deleted cgroups, as demonstrated by running the following task at the same time: while true; do grep deleted /proc/self/cgroup; done Results in output like: 7995:name=cgrp2: (deleted) 8594:name=cgrp1: (deleted) Signed-off-by: Yafang Shao --- kernel/cgroup/cgroup.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index bae8f9f27792..30bdb3bf1dcd 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -6256,7 +6256,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) { @@ -6279,6 +6279,10 @@ int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns, seq_putc(m, ':'); cgrp = task_cgroup_from_root(tsk, root); + if (!cgrp) { + seq_puts(m, " (deleted)\n"); + continue; + } /* * On traditional hierarchies, all zombie tasks show up as @@ -6311,7 +6315,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 Tue Oct 17 12:45:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13425097 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 46C34405E3 for ; Tue, 17 Oct 2023 12:46:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Yl/lG7wc" Received: from mail-ot1-x336.google.com (mail-ot1-x336.google.com [IPv6:2607:f8b0:4864:20::336]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94A0583; Tue, 17 Oct 2023 05:46:10 -0700 (PDT) Received: by mail-ot1-x336.google.com with SMTP id 46e09a7af769-6c4b9e09528so3821012a34.3; Tue, 17 Oct 2023 05:46:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697546770; x=1698151570; 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=RGlzNf6WdsmkGHoptnlJhKt/0P2xY3kxLxvh9NU6HqI=; b=Yl/lG7wcQKqkenRNoy09FzI71o2a4StV0UIMHsf33iFCYoWarFqf+OPpKwXZq74dE1 MilIzmZxJ9f08+5PHlsJdx/t4V5gUo9r9CZfM0Xrm9tGgrW7RwGuzSkkd3enxfGuWePs qo2jaNyUtlBWk9GuIvFa8tnTNrX7Pghcd/hUmVsZblAFtxC0m0dV4BSEjLdsEJbTBMzs pngb48uUXLw7A+Py+LfmxI7ZDVGtoGY3Odo1EkcXyeHvZnLoHxVLHKqok6k0DTvXmbyV mvLGzMqSWiJLL8gbJLkJDB+n7K8A65f3xcHu0+1hbBHSmkbn6cSwjNnu4+tj+GsvooUK fNrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697546770; x=1698151570; 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=RGlzNf6WdsmkGHoptnlJhKt/0P2xY3kxLxvh9NU6HqI=; b=aJFd7YmR220oLotoziR1TXpnr2ydT//IjwsAUcRtkOUymjUVBLlYxn0fuAYyWK1M/p fFCFegCKJ6x1grAsttoA27fvOSLyN8TVPt4Xvlbmk3tkbc6397LKlhkX+Y89mIL6BAr8 B+LgF2lW+tlWSTqcMMmVZwVc0Iatf4oYf0zv1aOxbbIDfZtOzf2EC5BYc4Sg7Ad3YYBj FELVIl2a43LCP86boxQDCJJMTe3ep/kZZSdM57eGox4StSvqQ59MvKhjVfB5xAPflHU0 nrT6uyH1UPrPW+Rg13R3BuziGFKS98IpNEbDOsyhQ+Go+dZgmUYEylGvw9uUNzvbaZjh BErQ== X-Gm-Message-State: AOJu0Yw2lTsg8C/Tq3ClhytH5rnRDbM0HAPa2i/XnS4DPnbzSyX1vcvY Ok9Mu/E918+DPMtaJJBW6X4= X-Google-Smtp-Source: AGHT+IHJcsuTDiUuaPpXZLWALWPW2owS5P6jdDAXIg+MkS/VjV+JAvCzmQUi9KL0wnpm0RbWS1szhw== X-Received: by 2002:a05:6830:56:b0:6b8:6785:ed0b with SMTP id d22-20020a056830005600b006b86785ed0bmr2266465otp.30.1697546769877; Tue, 17 Oct 2023 05:46:09 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:3b2:5400:4ff:fe9b:d21b]) by smtp.gmail.com with ESMTPSA id fa36-20020a056a002d2400b006bdf4dfbe0dsm1375595pfb.12.2023.10.17.05.46.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 05:46:09 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 3/9] cgroup: Add a new helper for cgroup1 hierarchy Date: Tue, 17 Oct 2023 12:45:40 +0000 Message-Id: <20231017124546.24608-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231017124546.24608-1-laoar.shao@gmail.com> References: <20231017124546.24608-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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC A new helper is added for cgroup1 hierarchy: - task_get_cgroup1_within_hierarchy 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. Signed-off-by: Yafang Shao --- include/linux/cgroup.h | 6 +++++- kernel/cgroup/cgroup-internal.h | 1 - kernel/cgroup/cgroup-v1.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index b307013b9c6c..0cd3983822c3 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 @@ -159,6 +160,8 @@ void css_task_iter_start(struct cgroup_subsys_state *css, unsigned int flags, struct css_task_iter *it); struct task_struct *css_task_iter_next(struct css_task_iter *it); void css_task_iter_end(struct css_task_iter *it); +struct cgroup *task_cgroup_from_root(struct task_struct *task, + struct cgroup_root *root); /** * css_for_each_child - iterate through children of a css @@ -388,7 +391,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 +857,6 @@ static inline void cgroup_bpf_put(struct cgroup *cgrp) {} #endif /* CONFIG_CGROUP_BPF */ +struct cgroup *task_get_cgroup1_within_hierarchy(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 321af20ea15f..3a7b0cc911f7 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 c487ffef6652..571071d45e4d 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_cgroup_id_within_hierarchy - 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_within_hierarchy(struct task_struct *tsk, int hierarchy_id) +{ + struct cgroup *cgrp = ERR_PTR(-ENOENT); + struct cgroup_root *root; + + rcu_read_lock(); + list_for_each_entry(root, &cgroup_roots, root_list) { + /* 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 Tue Oct 17 12:45:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13425098 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 550FB41AB0 for ; Tue, 17 Oct 2023 12:46:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cqmg5RF1" 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 21253F9; Tue, 17 Oct 2023 05:46:12 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-6bd73395bceso1897126b3a.0; Tue, 17 Oct 2023 05:46:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697546771; x=1698151571; 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=E6DXnZqpwxGmHcVwEgywrJ/8DnoqhP9n19zgbidGVUw=; b=cqmg5RF1J7MPT+hoHIs48KB3yUiYT5O144exHgtL47MzKYEdbbH11O4rymLL43TrCC IoeTUMlR4PNCm+WRoi9HVzOWCxWJXTzyJjE9J8umvoV1BqOtHKP2Qr09bRIyBi8GoGWV bfTjNIfRETRvZeb9totUdkxEuOvw16G6j6+cm1w5o7iILgFI4tvochP6ENcgUiqnfllL i5DvPzdkPcXwBuiSP20K4pNBovBXe8KkdSYzXkSfWiEZq4EJrDMmmIWEUEdjc4FuvP6V MeeT3HPP1ECTzJB41/aMIg3Qn8UMyNsRYbU/+qBAbkqdTm+h1/6lUW6eKXsEObfWw/WX k5Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697546771; x=1698151571; 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=E6DXnZqpwxGmHcVwEgywrJ/8DnoqhP9n19zgbidGVUw=; b=cIiCUUKNurMJsDFzpJMpLneGuvXmnNlWyBPAC0hBKvflyWpyLEu6WVf1tQS2RaBHwB 23a5iZxfvYF8uSgY0o48ktIlnRx5zWRf2s1FH+7jFGZrkbmp5m0vE7brCVjTMkNF/BVH pktnNMZyJkUHGITNXxQZrtKx3Qf1rl5BwuvZmIlDKNyQqNKlFalJqezXdKJ6J18kDlXD iECDSd2TW/Gwvq8lGRLQs3RWoibYvgBdQz2/JV0QvF6XSxvT3LtorJLa38mnCwneNv8O ryuRds/bNTiDxiEk0QEm5hg3/T+WBfEJBkrlskFLNRaojOZv945J9Vk5kBwowispqOvX RaGw== X-Gm-Message-State: AOJu0YyXB68axjmZjtW1tgkBlbIs+43R9lbSSTOfF47uq4I2t8CUdJtB /U0zxpVi6bAej8ZeKNaWY6M= X-Google-Smtp-Source: AGHT+IGqcaXnMwulC+pJu4pw5aIquM6SPB8oKRJnDDhse9jwl6ywuTc4Dp8+SHfIM6J6vKnYQAOJSQ== X-Received: by 2002:a05:6a00:2293:b0:693:43b5:aaf3 with SMTP id f19-20020a056a00229300b0069343b5aaf3mr2855789pfe.13.1697546771499; Tue, 17 Oct 2023 05:46:11 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:3b2:5400:4ff:fe9b:d21b]) by smtp.gmail.com with ESMTPSA id fa36-20020a056a002d2400b006bdf4dfbe0dsm1375595pfb.12.2023.10.17.05.46.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 05:46:11 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 4/9] bpf: Add a new kfunc for cgroup1 hierarchy Date: Tue, 17 Oct 2023 12:45:41 +0000 Message-Id: <20231017124546.24608-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231017124546.24608-1-laoar.shao@gmail.com> References: <20231017124546.24608-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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC A new kfunc is added to acquire cgroup1: - bpf_task_get_cgroup1_within_hierarchy 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. Signed-off-by: Yafang Shao --- kernel/bpf/helpers.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c index 62a53ebfedf9..a682b47d3b97 100644 --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -2217,6 +2217,25 @@ __bpf_kfunc long bpf_task_under_cgroup(struct task_struct *task, { return task_under_cgroup_hierarchy(task, ancestor); } + +/** + * bpf_task_get_cgroup_within_hierarchy - 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_within_hierarchy(struct task_struct *task, int hierarchy_id) +{ + struct cgroup *cgrp = task_get_cgroup1_within_hierarchy(task, hierarchy_id); + + if (IS_ERR(cgrp)) + return NULL; + return cgrp; +} #endif /* CONFIG_CGROUPS */ /** @@ -2523,6 +2542,7 @@ BTF_ID_FLAGS(func, bpf_cgroup_release, KF_RELEASE) 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_within_hierarchy, 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 Tue Oct 17 12:45:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13425099 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 3074941E5E for ; Tue, 17 Oct 2023 12:46:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NdZeNaaS" Received: from mail-ot1-x330.google.com (mail-ot1-x330.google.com [IPv6:2607:f8b0:4864:20::330]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0074C106; Tue, 17 Oct 2023 05:46:13 -0700 (PDT) Received: by mail-ot1-x330.google.com with SMTP id 46e09a7af769-6c67060fdfaso3899315a34.2; Tue, 17 Oct 2023 05:46:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697546773; x=1698151573; 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=jiqCJaBgHOvH62i9xa1P2ChEL8G+IgbXj1FnfIkgM98=; b=NdZeNaaSWP6YUMy6j6nTws/ZTVWYt0qahHK8xL6Mg9+Qyt9qesz0HzgB/SUekiDaQS D2+ZNDQjBSDOER8w8irW6Dmhjsq6H6l0Fh1tGtY8Wv6Sgdm5oEYcpNFiYqCL3bpZ0ZHB /J5/UKctCZxgbLycQ0A3yW1bT3yaw/hclgjDZkS8W5H6+QoNYCnIktwknWLFiTmfC6Hx E1utn7bmKC3SEBR/I7fjPBc/LNqSDzLk+qVpedr84uAZySqKlY+dR0llVVs70VQweor7 G2+Ohi9jDjn/VMI8MzE9iToq6I4fle844HTeTG06JRxcqzjBRkPYS8Wu2vd6r+92Y5IO 9AfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697546773; x=1698151573; 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=jiqCJaBgHOvH62i9xa1P2ChEL8G+IgbXj1FnfIkgM98=; b=jqEzdHk2SNZFhH8rs/8EQpm81i0eTco5XDSK4crUjrOdqGGjxyHZQADI23+Wfc3Wp3 GaF3Dlm9LB3wfxMmK0IsUApISKkdeSmi75n3kpGLmegJahLyb+7LIxcUmzdsVLv54yHh Zq68m+v9ePTqahljTqkTfsb1gkCMGoOSiHIzpL3Er1y1JGDFoHNVD0H9uD2aAHxkz9lD VbtEGSVwbbDE/p/QTR8NOsZRgBHvVIpHWam2tCVbS16Fh3Vy0tPAfxQHZFt6PVkShJIu 3riZo++yJm+7p5GmRYSkaikEJoi+Z7cDUYQxrbLi0IRabdVG2tDePa5kZ0IG7fut9rT3 Lb0g== X-Gm-Message-State: AOJu0YxFreNTLaxE2dLPrLVUA1izaT2LQRGSa+WcQrP7AWQbBSNd3rz1 FOQr+mtL+r1EKIwSYw1t7T0= X-Google-Smtp-Source: AGHT+IEVD5B3KICe8VoQ6DF5smnm6R6F+dA1k3JiZ6DklBUNk//eH2gBAogt2fhrbQ4pkADq8CqjWw== X-Received: by 2002:a05:6830:4b5:b0:6b9:2869:bd81 with SMTP id l21-20020a05683004b500b006b92869bd81mr2246831otd.18.1697546773216; Tue, 17 Oct 2023 05:46:13 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:3b2:5400:4ff:fe9b:d21b]) by smtp.gmail.com with ESMTPSA id fa36-20020a056a002d2400b006bdf4dfbe0dsm1375595pfb.12.2023.10.17.05.46.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 05:46:12 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 5/9] selftests/bpf: Fix issues in setup_classid_environment() Date: Tue, 17 Oct 2023 12:45:42 +0000 Message-Id: <20231017124546.24608-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231017124546.24608-1-laoar.shao@gmail.com> References: <20231017124546.24608-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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC 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 5b1da2a32ea7..10b5f42e65e7 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 Tue Oct 17 12:45:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13425100 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 0081B41AB0 for ; Tue, 17 Oct 2023 12:46:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="D44Lpzod" Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 677A195; Tue, 17 Oct 2023 05:46:15 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6b201a93c9cso3353834b3a.0; Tue, 17 Oct 2023 05:46:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697546775; x=1698151575; 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=2QiU9AAAE0pUaYi7vmDEGmJj0rpR0kjK+y05BuChJhI=; b=D44Lpzod3MAln59bsY9yUKWEMgaS7dbETKcNfIMVjxpAUAvEU7JbzUMNKkExWCC3q5 bQFklM8cIWFLM8zIXJxtRKCVmYutLwkZPMsP6eaWKbIHbH26+ctoFCd2z/4o/vZCT/ec esH7uuAE1T0AHDyTLVcXGaZo5DK4VrrKlhly84cn4iVEOw1whvU2fY9hO8KqjdJExANP bM34SUF5bsvJo9M1opgmPvhEddMSD0kiJrx1C+XUFEQmzQJkquIpV5iRHyXPxlXK8HPP ioALZP6rY8PSK0NOn8zQX6M2tJMuLDQpUAksQ5ORBoKSafjIgUjhHxnQZu1HsVMRm+wn oYww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697546775; x=1698151575; 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=2QiU9AAAE0pUaYi7vmDEGmJj0rpR0kjK+y05BuChJhI=; b=ffCljzZqr5v/9XSurk46qzs0AN30H7EEo7RIHFWFkVNkewn3XZrm/3wA2EE5Scgicr twtZa3f70V92+KAHOFNIs1hx0Qed5ciV3fwo/dzJPiAM/y63v6HhdrObdh4yDyzA725L vFVmAlBh4BmTJ3BQQYOz2akSArjnzuBbZOL9XnMAOE5apei5/A5YZPHENN5nP1s/1SgC /NESCS+5FtGnaZYOt2t3cUdDmkJbCdafPikw52Q9Gy0M5C6a4JZz40PKWRt6vsu0QAic ZUce6yaJnZmXpLAddl9kSggtLUhcA077GCnM9nzMFEDqZPOsCRNhZDAJcuuJg+kNn/OO aopg== X-Gm-Message-State: AOJu0YzvSTOoCigTNtz5kINkBfFdz9ATDJ2kScO29OJWqmYJzOfoKueN H0evMwgzB+tGOlSiE/OwWJI= X-Google-Smtp-Source: AGHT+IHF2vvIgoRrEFpx6PDzLtkN727GQVblAAIU8qsHNiCX2BySfyA+t9LSCoimWNoQTDjSFGd0fg== X-Received: by 2002:a05:6a00:2283:b0:6b4:c21c:8b56 with SMTP id f3-20020a056a00228300b006b4c21c8b56mr2374713pfe.23.1697546774839; Tue, 17 Oct 2023 05:46:14 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:3b2:5400:4ff:fe9b:d21b]) by smtp.gmail.com with ESMTPSA id fa36-20020a056a002d2400b006bdf4dfbe0dsm1375595pfb.12.2023.10.17.05.46.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 05:46:14 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 6/9] selftests/bpf: Add parallel support for classid Date: Tue, 17 Oct 2023 12:45:43 +0000 Message-Id: <20231017124546.24608-7-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231017124546.24608-1-laoar.shao@gmail.com> References: <20231017124546.24608-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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC 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 +- .../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 10b5f42e65e7..f18649a79d64 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 5c2cb9c8b546..92fc41daf4a4 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.h +++ b/tools/testing/selftests/bpf/cgroup_helpers.h @@ -29,7 +29,7 @@ int setup_cgroup_environment(void); 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 9026b42914d3..addf720428f7 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 Tue Oct 17 12:45:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13425101 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 685D542BF2 for ; Tue, 17 Oct 2023 12:46:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="X8FR086a" Received: from mail-ot1-x32d.google.com (mail-ot1-x32d.google.com [IPv6:2607:f8b0:4864:20::32d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2C2BDF9; Tue, 17 Oct 2023 05:46:17 -0700 (PDT) Received: by mail-ot1-x32d.google.com with SMTP id 46e09a7af769-6c7b3adbeb6so3985345a34.0; Tue, 17 Oct 2023 05:46:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697546776; x=1698151576; 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=NcMF4eEIqJtrI/6oP100C5PlEtM9pT5G9D1uUQ99pcY=; b=X8FR086aDIzdFh+TNwlQHsMQifG/jJquAA9koPW1KtMMS2P6z/kWBuz6JmMC7PtzTH 7PU2UzEn+LNUIAPw4yLT0CgVnb2qaxA7D+jX7Zetrq/NOz6BpVPPpbmD2SRcC8140cH1 WnANBXv1NHs8R8UY4U/rb49gp4dm5LZZ5XUGamAuDsa0cYmglFtMa1915KYx46hSPGqs kE72Jl8MHdlm9iwmewKP5UyxpJcB/d0u7O5AxMGVR1S41ryaLyQmTqt/WACYH41uSET8 k6papRRSGdztgsoq3WdHIgv2rCA7jbLuaqYzJSxgltilrwhl4avquFUW4QfimxFGEBUc Cn8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697546776; x=1698151576; 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=NcMF4eEIqJtrI/6oP100C5PlEtM9pT5G9D1uUQ99pcY=; b=cLwFXg711XwFuoMQiU+oneM0YhgmP73yIKgYG7/H2giThXHaa3oxXOosMwnaOkHSbh Zjcl0XVI9zlf5wmhCCmGIkolSXfuHDL3cxP+MaNTHEMjAL2u2UXugT4g0I82Hh5uhAaI enGQCkQwEuzYMSisC5BVSD2T/thVNL2OvZkADRQsrd+XG5oDND3XHXotDFt/URdJL7rj 640k+AZlYH8uvwvTM6MxK2O0yD2HOT8tpC5NnuyC0S+xY4IJwGqPepKycwPWIdCwZSRa Mixsm9l+6fxEuijxK8zPKK5YkGemYvQbzazq0BbAGewqA5h7H3QE+oeRTNRHrLN8pNIW t6LQ== X-Gm-Message-State: AOJu0YzIkxqchmKCZVMyFtWT6V/g1kW8D7stVHugm8lJtY89x0sFuzG+ iHYY0N5UztpcivThos3a87E= X-Google-Smtp-Source: AGHT+IFEOCzTu3zygMpFFP6evpcF7YhAzUk0+zEMsq2t5gI/LwmIucYpXdBRbRm3JKngN8XAG7MrWw== X-Received: by 2002:a9d:66d0:0:b0:6bd:152f:9918 with SMTP id t16-20020a9d66d0000000b006bd152f9918mr2131657otm.14.1697546776497; Tue, 17 Oct 2023 05:46:16 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:3b2:5400:4ff:fe9b:d21b]) by smtp.gmail.com with ESMTPSA id fa36-20020a056a002d2400b006bdf4dfbe0dsm1375595pfb.12.2023.10.17.05.46.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 05:46:16 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 7/9] selftests/bpf: Add a new cgroup helper get_classid_cgroup_id() Date: Tue, 17 Oct 2023 12:45:44 +0000 Message-Id: <20231017124546.24608-8-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231017124546.24608-1-laoar.shao@gmail.com> References: <20231017124546.24608-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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC 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 f18649a79d64..63bfa72185be 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 92fc41daf4a4..e71da4ef031b 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.h +++ b/tools/testing/selftests/bpf/cgroup_helpers.h @@ -31,6 +31,7 @@ void cleanup_cgroup_environment(void); /* 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 Tue Oct 17 12:45:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13425102 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 4D90F42BFB for ; Tue, 17 Oct 2023 12:46:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dcH7Abgd" Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACC1FFF; Tue, 17 Oct 2023 05:46:18 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6b3c2607d9bso3118552b3a.1; Tue, 17 Oct 2023 05:46:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697546778; x=1698151578; 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=/ScFjSx13keQbjWIXpryCpmHFOUoMyEMo4B+madkUMA=; b=dcH7AbgdxwMg0+hkB1bkRpP0AdoDHRcaOLoWiqyXvyKsF44t1lYsLQ7GBVEjmS2zMd DPMpOweo6P8/Tcwxksk73bQQzabdMLYoyiQFEqzARY/kPrPE0bTF67YTdGd3nTqSPqfA axJ7SLJfuu2/w5Kb2Qx8r4JZnlS5xOcChL8uBvl4QXpxDQkwtlDpCJ9gUa0nJJXb7Mj7 UweBYGQ/ce+LjqjdB3rewxKekINGrQQHCdUIUahgZg5wd6yrVHXKeBi+qnWDJvRTIGQG Yh9uisRbqZqjcyLe6t6jsxluS89IAXafKEafJ2kqjEYNo9BF2LYVpf70l8EEbJDSoxW0 pUZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697546778; x=1698151578; 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=/ScFjSx13keQbjWIXpryCpmHFOUoMyEMo4B+madkUMA=; b=mglpkdBBFnznTDsvGHDK52Na+UO1p+sOW/V+8Bxak7/yZdezfDX2WjSdH5lgKMYYuM aYcRECmKD3l7sT7I14OFPDeTbIZ5b9jYThd3vEX2/ZLLDXSGFdADlYHl5+MgioQFSNQz qYGj2sKMRV8UweqgW61uJQOJ3X1wh59y3XWyID9WtJBXSikPHY8tfDPQlMPdvrMl0NZq jpIC+Su+n5vrzrezFi8XZbvqz1ck0MJhFGargTXghtoCDAX72IehRdENfOCEZN3u/i5j S+oECdvsOShGXIhrTkMudVb47ouHpfJ929ZWOtsppf+SsvujpmqqLfwpKyYaSHTu0hHA Fw4Q== X-Gm-Message-State: AOJu0YyECPtWnh5k7jyGi71daHJnOy1wqIOAOE+IxE2yU5qZyfH2vMiV JfCN6WLRH3LJRLZ1b/CYmB4= X-Google-Smtp-Source: AGHT+IG+g9A/brY625ZLFiD9hLOcpzFfvYR0FeMmul/GTCkG4bkWbAknBGpd1JYarGEVJpQk/Rkf6Q== X-Received: by 2002:a05:6a00:1a51:b0:6ad:535e:6ed9 with SMTP id h17-20020a056a001a5100b006ad535e6ed9mr2034837pfv.16.1697546778152; Tue, 17 Oct 2023 05:46:18 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:3b2:5400:4ff:fe9b:d21b]) by smtp.gmail.com with ESMTPSA id fa36-20020a056a002d2400b006bdf4dfbe0dsm1375595pfb.12.2023.10.17.05.46.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 05:46:17 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 8/9] selftests/bpf: Add a new cgroup helper get_cgroup_hierarchy_id() Date: Tue, 17 Oct 2023 12:45:45 +0000 Message-Id: <20231017124546.24608-9-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231017124546.24608-1-laoar.shao@gmail.com> References: <20231017124546.24608-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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC 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 63bfa72185be..d75bb875ef03 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 e71da4ef031b..a80c41734a26 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.h +++ b/tools/testing/selftests/bpf/cgroup_helpers.h @@ -20,6 +20,7 @@ int get_root_cgroup(void); 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 Tue Oct 17 12:45:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13425103 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 379DC41E34 for ; Tue, 17 Oct 2023 12:46:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Eqcrwe5N" Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48FD7101; Tue, 17 Oct 2023 05:46:20 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6ba172c5f3dso2334179b3a.0; Tue, 17 Oct 2023 05:46:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697546780; x=1698151580; 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=QANBxWeQVGeTYEVVyQ3g6025jiJVAg51gX0cGDQdMMc=; b=Eqcrwe5NJVxKN/ZOsj7S+xihSHvlplloqDS+CqoM7MXEI8zE1NfaFqYHVPH2ZP5fl3 DaB8WUAA/R3kj7L69inbXbTSVVlArKtfhpZqOAxMuauyvr/+PQIb3Ce8+GwCvup61EDN 08LQbWfocPmPdYUmCWHuik+0CVQeLHiTPY7Q/yqHDlW0/j3fjkr50n8WV9+gbHdI/Z+S 2a99N89w8mbe0G/mL8va2cApOPoa8yIeummFpxpmK+trq/poH6SBTWK6+XTFVORIruOb hQ5DZivXuQqRoQqqqgPKy8DZ3+JhkfrmKLURZCeFADR78wGPANDmUcqtaXdc9u0YcS1D cZbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697546780; x=1698151580; 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=QANBxWeQVGeTYEVVyQ3g6025jiJVAg51gX0cGDQdMMc=; b=GvB6NZBZaTujD6wzM8Vimq32bZDzxh68Jm1LUf3RrgtifPR+K0iuZmjkCEQwJ1PXQd XVsKho+v+yG6GbmsuAmLr7YMNrb2H3vgEzrRfvmzoDnJDzS5yj6SixKTVscZUrN36utr 7i7gCF5sGR0UN3dqShTgGhxpK3X43MmPboDwUsnoHAZ0mKpO46vkUh0U5sJSsMsB/mfO j7yLVKncZp6l43ff8QhGELM/Z2xo+d/NQPPVpvD7/m5+rmgVK/UPaT0YAEWPz9jzEKAm Lq0pLrGLrrZ8UnHkn1b/+n/wQNq4z5DsLGqZBXcHy0GGD0RZkh6ZxtgmrCFZ+pQG+LSn 50Dg== X-Gm-Message-State: AOJu0YwSegtovl2qNmVbGrT3C2EWMLNVIyWHUp3QquqhYascMMyPinhE vsLMuoYEYpNS3njxnhq73Cs= X-Google-Smtp-Source: AGHT+IH05Xt47ES7HySXrSPD3SfQdP60JDoCnLCG+2y4ZVGWH8cEabJCBeT56dOIL1Aubj5rIHek5A== X-Received: by 2002:a05:6a00:1494:b0:68e:41e9:10be with SMTP id v20-20020a056a00149400b0068e41e910bemr2331055pfu.20.1697546779721; Tue, 17 Oct 2023 05:46:19 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac01:3b2:5400:4ff:fe9b:d21b]) by smtp.gmail.com with ESMTPSA id fa36-20020a056a002d2400b006bdf4dfbe0dsm1375595pfb.12.2023.10.17.05.46.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 05:46:19 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next v2 9/9] selftests/bpf: Add selftests for cgroup1 hierarchy Date: Tue, 17 Oct 2023 12:45:46 +0000 Message-Id: <20231017124546.24608-10-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231017124546.24608-1-laoar.shao@gmail.com> References: <20231017124546.24608-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-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net X-Patchwork-Delegate: bpf@iogearbox.net X-Patchwork-State: RFC 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 Signed-off-by: Yafang Shao --- .../bpf/prog_tests/cgroup1_hierarchy.c | 159 ++++++++++++++++++ .../bpf/progs/test_cgroup1_hierarchy.c | 73 ++++++++ 2 files changed, 232 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 000000000000..4aafbc921254 --- /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 000000000000..ca9a631a6499 --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_cgroup1_hierarchy.c @@ -0,0 +1,73 @@ +// 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_within_hierarchy(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_within_hierarchy(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";