From patchwork Sun Sep 3 14:27:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13373244 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 187C33D6A for ; Sun, 3 Sep 2023 14:28:10 +0000 (UTC) Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4A8697; Sun, 3 Sep 2023 07:28:09 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-68c0cb00fb3so585550b3a.2; Sun, 03 Sep 2023 07:28:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693751289; x=1694356089; 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=cMXLcIWio9WW57rG1glRWMEPDKGAafnI6Sr1JX6PcGQ=; b=LzVEieRrkSFlJ8mJ+2vRTvgW+b8tuZ1vl3MVX0HWrPwLSZpeV2sJGWL3iZ6LtUg3We aIoe90saY0SE/4qVQTwIq496PdeKhSpaZHQFnmuEeS6BpoRNvz9SnXjtBK8qUPbQYy1C XOPZc6WMep7QcvL9nMB/jlhT2+1/6D5BfllT5fNX15djA+4qvqFP365NbRAu+CGjLTqF bkaheS4ccN4TYmzML38Dh6IhVwMD3mrCjyj4g17UZJRERfG0q3X9rgOrjtDC4OTCiSY5 /YxFUU7KgA5zRBQvYBVIDRPy4LQp1MpZFPEHwwzX+bGceV4mqwEdKRniJOA27wCRfk6D e1Ng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693751289; x=1694356089; 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=cMXLcIWio9WW57rG1glRWMEPDKGAafnI6Sr1JX6PcGQ=; b=MMSz60v7pIN3jgnsw0KTstU0Z/78K+0DBH/2EWH96/CaNY4NEaSrBuL8pGxEgc5sRo LdSeMBRz3rBH0uJz9ERMgJH9WwXwv/+kuTHhcKW3Oh/3/WcrxFrlttLSa86xMN6zbzGW nNCJdifkqFfxXxZKsF7nRbVsrpnlTUFF3ohmI7VOzByxAtPQblcZByQezyXO8EpVjXoX ZLOpkN5FsjxXyIl159hI+r6jj1DakQwsB1UQtPL4VIW7vN6joCtjzq+uBgj9hrVK4sS+ yX4Y7a5bFuh1LcpFWQaGlmxjxH/Ym6UNQoNazjXLByrUkfRNMwMZTMSRZixf+dgm3asI 2SXg== X-Gm-Message-State: AOJu0Yxc8zWFb1Kg2LGiedozMq9umdYsDXCkwq5Uzc0M6x0oOMbyRQ5V lbZU2p2jkvWeukrHE41kk04= X-Google-Smtp-Source: AGHT+IGOu8boHCQGqBvBcdoaGjSTQeTtKihvjht+tXjBHfxM2y+irCbA+orNtXS9XNif83fvsXPpmQ== X-Received: by 2002:a05:6a00:22c7:b0:68c:4c29:9c59 with SMTP id f7-20020a056a0022c700b0068c4c299c59mr9909256pfj.14.1693751289245; Sun, 03 Sep 2023 07:28:09 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:185:5400:4ff:fe8f:9150]) by smtp.gmail.com with ESMTPSA id b23-20020aa78117000000b0065a1b05193asm5809977pfi.185.2023.09.03.07.28.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Sep 2023 07:28:08 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 1/5] cgroup: Enable task_under_cgroup_hierarchy() on cgroup1 Date: Sun, 3 Sep 2023 14:27:56 +0000 Message-Id: <20230903142800.3870-2-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230903142800.3870-1-laoar.shao@gmail.com> References: <20230903142800.3870-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 Currently, the function task_under_cgroup_hierarchy() allows us to determine if a task resides exclusively within a cgroup2 hierarchy. Nevertheless, given the continued prevalence of cgroup1, it's useful that we make a minor adjustment to extend its functionality to cgroup1 as well. Once this modification is implemented, we will have the ability to effortlessly verify a task's cgroup membership within BPF programs. For instance, we can easily check if a task belongs to a cgroup1 directory, such as /sys/fs/cgroup/cpu,cpuacct/kubepods/burstable/ or /sys/fs/cgroup/cpu,cpuacct/kubepods/besteffort/. Signed-off-by: Yafang Shao --- include/linux/cgroup.h | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index b307013..5414a2c 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -543,15 +543,33 @@ static inline struct cgroup *cgroup_ancestor(struct cgroup *cgrp, * @ancestor: possible ancestor of @task's cgroup * * Tests whether @task's default cgroup hierarchy is a descendant of @ancestor. - * It follows all the same rules as cgroup_is_descendant, and only applies - * to the default hierarchy. + * It follows all the same rules as cgroup_is_descendant. */ static inline bool task_under_cgroup_hierarchy(struct task_struct *task, struct cgroup *ancestor) { struct css_set *cset = task_css_set(task); + struct cgroup *cgrp; + bool ret = false; + int ssid; + + if (ancestor->root == &cgrp_dfl_root) + return cgroup_is_descendant(cset->dfl_cgrp, ancestor); + + for (ssid = 0; ssid < CGROUP_SUBSYS_COUNT; ssid++) { + if (!ancestor->subsys[ssid]) + continue; - return cgroup_is_descendant(cset->dfl_cgrp, ancestor); + cgrp = task_css(task, ssid)->cgroup; + if (!cgrp) + continue; + + if (!cgroup_is_descendant(cgrp, ancestor)) + return false; + if (!ret) + ret = true; + } + return ret; } /* no synchronization, the result can only be used as a hint */ From patchwork Sun Sep 3 14:27:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13373245 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 611CE3D6A for ; Sun, 3 Sep 2023 14:28:12 +0000 (UTC) Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51E2B97; Sun, 3 Sep 2023 07:28:11 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-68c3b9f8333so340632b3a.1; Sun, 03 Sep 2023 07:28:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693751291; x=1694356091; 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=jjy5F3GLFbzbY5Veg10sJXncHFcZhNqzR3hA1fgbxTA=; b=cdFTQqqsDTeRINqpsMTKC2OPmXg59qKykpNyvZwX4LhU46NrFvBlbPJQo11Cgfh4TR XD1jUwLVvPWd9rMvKvaLPYAfxdPD9Diea3P9MZTl72+Ma5ODcubogdVO7OBzZkp2rG2H Z7BS0be/KQ5Z79XfKnyJ9BcpJlUvIkkOVryMqCln1/bNbDD/UKbw/Eq/d/zNefmmDjvZ ZP5LjUU2vM7eVeOsvJNgTsodKZc7toTwdiyl6RdyOkPYCIF7Ld3zRY+4UQCPXlXKsYsM OzPc2zI9Wbw7uxK4dOjo7wHyQsQhN8mqnY9geYjSZxwU3iKNGE0L1VespYXF03zpR901 15cw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693751291; x=1694356091; 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=jjy5F3GLFbzbY5Veg10sJXncHFcZhNqzR3hA1fgbxTA=; b=eviPEKgTOwwplNfYg1H5BJwx4cd/egou0DaXThjHVSWJ+O8zYf98ZC6XxUQSRtvgpj ew4m1bFkogZ2B84q4KckhC0iucpc4UAVOjxQJ18Cqh49K5j4hHagCQDN0jTXnukXiKKe MXg0ZhJ2PDqtJqNY9amQOXfCkOS/hCxzTAtSqb72AfYtt7vAhNongWCqUrg/i8N4p4cZ yYu2kW8IsTW2e1r7QeO/1bteAZ6vZRrrOe4vcK2L8orfk6mFrWfshp0eHENvPzUBOz/4 CJRoWm9qJqJ1iJxuVlglhMwNT0EfHjdZQjl9KhXcCw/2Zn9X28dFXB9YEIQk1HlrNziw wpig== X-Gm-Message-State: AOJu0YxgxUl3wt8RLBGCn8PI8BBuTfaxh0pnUAgz8z58FAj15IUppF+D qHzTUGs9eOgRASB59zFmylk= X-Google-Smtp-Source: AGHT+IFh5BUV/PWiZfvyHOKjMcTIEJkoFfn1UHQWnmSqJ4mqCxqezQnl+prtpsTeZNSVYsCu9DQbvg== X-Received: by 2002:a05:6a00:328b:b0:68a:2c6e:c2cc with SMTP id ck11-20020a056a00328b00b0068a2c6ec2ccmr9771054pfb.0.1693751290755; Sun, 03 Sep 2023 07:28:10 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:185:5400:4ff:fe8f:9150]) by smtp.gmail.com with ESMTPSA id b23-20020aa78117000000b0065a1b05193asm5809977pfi.185.2023.09.03.07.28.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Sep 2023 07:28:10 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 2/5] bpf: Enable cgroup_array map on cgroup1 Date: Sun, 3 Sep 2023 14:27:57 +0000 Message-Id: <20230903142800.3870-3-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230903142800.3870-1-laoar.shao@gmail.com> References: <20230903142800.3870-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_array map currently has support exclusively for cgroup2, owing to the fact that cgroup_get_from_fd() is only valid for cgroup2 file descriptors. However, an alternative approach is available where we can use cgroup_v1v2_get_from_fd() for both cgroup1 and cgroup2 file descriptors. The corresponding cgroup pointer extracted from the cgroup file descriptor will be utilized by functions like bpf_current_task_under_cgroup() or bpf_skb_under_cgroup() to determine whether a task or socket buffer (skb) is associated with a specific cgroup. In a previous commit, we successfully enabled bpf_current_task_under_cgroup(), ensuring the safety of storing a cgroup1 pointer within the cgroup_array map. Regarding bpf_skb_under_cgroup(), it is currently restricted to cgroup2 functionality only. Nevertheless, it remains safe to verify a cgroup1 pointer within this context as well, with the understanding that it will return a "false" result in such cases. Signed-off-by: Yafang Shao --- kernel/bpf/arraymap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/bpf/arraymap.c b/kernel/bpf/arraymap.c index 2058e89..30ea57c 100644 --- a/kernel/bpf/arraymap.c +++ b/kernel/bpf/arraymap.c @@ -1291,7 +1291,7 @@ static void *cgroup_fd_array_get_ptr(struct bpf_map *map, struct file *map_file /* not used */, int fd) { - return cgroup_get_from_fd(fd); + return cgroup_v1v2_get_from_fd(fd); } static void cgroup_fd_array_put_ptr(void *ptr) From patchwork Sun Sep 3 14:27:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13373246 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 59D5D3D6A for ; Sun, 3 Sep 2023 14:28:16 +0000 (UTC) Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CE32FA; Sun, 3 Sep 2023 07:28:13 -0700 (PDT) Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6bf298ef1f5so615224a34.0; Sun, 03 Sep 2023 07:28:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693751292; x=1694356092; 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=3LgHnZQNGB4Eit9U3o8mixt+awQqbQlREXizLoHuW3w=; b=OpwgeB0rPVExtv+MYfamdJI2GM78Y3tsmCPhK/B2beO1FkvnHisykeZiZbPoHL3jGP gVRmGyjq0rD+PH0PNe79+4M2+Lh1U+edEEqgtl5JdifSV+bb+G3MsHp1Jpc4EW3zQszd fZGaJwO+kcufP73gJWHhARrytMEdqXWhlaNOTR+q8bf5Dy+ZVocpfiQ+8/gvUCaR0/UX uTx/YUrColnT2ZKTlNSBADWErptez0NiS9cI+SzczFo4CCppLjaFN6ptdrlAsbZL+/ZI cnRwBn8PFFHcv8CIdcBjA9WjW8Y+G5T6JHRsN11jqPcy5jPOY1eer5NQg2glrA9y3LPa kE4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693751292; x=1694356092; 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=3LgHnZQNGB4Eit9U3o8mixt+awQqbQlREXizLoHuW3w=; b=e7RWE9UfQdByM75rUoig6qISPNdSHMympMHLkVOfq942KqbuXUHruUPLqVqwpwnHNf nB3NQPq/8Niw654zANy0lPh6oCk6QbnIv30+hZ8iZ/aDrRF9V0XcBDjK3ivK9Mjr3by/ Vy8DN5SIrbB809pMJ6Kc5VieAraj4jqLH8lU30xcBuKYREQtR0+6gBC+xFCYEba2ZF+w dytfPVF566POgqBf4ssddbVjzX0Z/INLn8O7+JdI8A5viSQsgh+BTmRAvwiWW68zw2Zx 3z+hhQ0ugI3b/IqxsnSXVtvjSZ4DoBJl9SlbaVSAZfop5coVePTaC9Nc3zPCyg2wBJjT t7Sg== X-Gm-Message-State: AOJu0YwAkxAV/fYlsloQFTOVQ7X2FsIkKH6Je8S034mP3cRRJJDLM46n nkMp8xTTY28tFzuJqvxktJ0= X-Google-Smtp-Source: AGHT+IF/4x1DTf3nTbnErroz86b5PDz7cTr8M1aC5V17cBJnzVBJJURcQ+gJ/IjztiHIiKd28XJBoA== X-Received: by 2002:a05:6830:478c:b0:6bd:cf64:d105 with SMTP id df12-20020a056830478c00b006bdcf64d105mr8691478otb.12.1693751292265; Sun, 03 Sep 2023 07:28:12 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:185:5400:4ff:fe8f:9150]) by smtp.gmail.com with ESMTPSA id b23-20020aa78117000000b0065a1b05193asm5809977pfi.185.2023.09.03.07.28.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Sep 2023 07:28: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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 3/5] selftests/bpf: Fix issues in setup_classid_environment() Date: Sun, 3 Sep 2023 14:27:58 +0000 Message-Id: <20230903142800.3870-4-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230903142800.3870-1-laoar.shao@gmail.com> References: <20230903142800.3870-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 2caee84..f68fbc6 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -499,10 +499,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 Sep 3 14:27:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13373247 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 A5D4B3FE6 for ; Sun, 3 Sep 2023 14:28:16 +0000 (UTC) Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49FCFFE; Sun, 3 Sep 2023 07:28:14 -0700 (PDT) Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-56c2e882416so233679a12.3; Sun, 03 Sep 2023 07:28:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693751294; x=1694356094; 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=U5k29RdSdgFGrpvgVrSsKwz4bkGSk589c/WSq1cJmNg=; b=UeRpMoL3RZo/I6HJpcYqS1LGiile/X+MgCuNlDOKMNRtUE6jqnjPceQQttncvAEK2+ aYcJ8+J7L+V6f2gFhH+unfJ90KOwHhJRqPjeWjpyF5OVbadZkibzHnC1VzgZ4EOx5DGd m4EMWLXBON//VOBc3USZEo623Bt5+Qzf97HwkchdwJDhAQQ5lutidD1qGLm019WVnfU2 hs38f59fE1QvY0MHvfFsn9vtiVoPC1B0IbLF9/O0Ll0u2bZceeAzR0pMF7iYsSyljDI7 i/0ZPzjS7dz8G5d9ySjiPXB52qZiJ3PIqKqt1mii7arGXDPhTZm7V1rDrcF3W/SlwgIU MkVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693751294; x=1694356094; 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=U5k29RdSdgFGrpvgVrSsKwz4bkGSk589c/WSq1cJmNg=; b=Kg8p7sMm0gRXM/2lGNUIpufIm6pLgvKsaGpFWXG0EVFmyMb3inY42+m1Dl/3SrmA73 +GD9Q74vJdQXXCg/0XM+v2q1hGTDvhG2+B2Qnu/J0pSKG/Nqdt2FSiwPI4taUhStaDHT iOtjl54NOKjNZ1Ng94vuKdAUZZJWzEh6r5sCGZt+eZCYxFedcO4eHK38oZQQ3gKO9ZYm LjkuJJpTL8bqx68rXDRljYVZmHoem7/qiR3C067AtyDKoNE9mNz9Seh/8MgVPkuCTxOm QxjEJ0tNn8C0X7YJR11vpMoPwlp4kGDgI77gyokJAiYUBQvBH2gdMnOaKcZTvM3RcOYU 93fg== X-Gm-Message-State: AOJu0Yx2PHmBL6raMZF6k/jBcNssRe/qdF5zOUOdfUVRBq0WXRyngaUy ei11davpK3Izs8hY6HHQjw9XfmM/i3cVJejpO5M= X-Google-Smtp-Source: AGHT+IEhqDvfZebErKX2mJ2WwUkV4KdTuuuwOqND+auGlH5Y9pn2RAyagPR8DzO2OiSjJEqrBrK08w== X-Received: by 2002:a05:6a20:974f:b0:14c:f4e1:d9e9 with SMTP id hs15-20020a056a20974f00b0014cf4e1d9e9mr6207216pzc.45.1693751293727; Sun, 03 Sep 2023 07:28:13 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:185:5400:4ff:fe8f:9150]) by smtp.gmail.com with ESMTPSA id b23-20020aa78117000000b0065a1b05193asm5809977pfi.185.2023.09.03.07.28.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Sep 2023 07:28:13 -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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 4/5] selftests/bpf: Add new cgroup helper open_classid() Date: Sun, 3 Sep 2023 14:27:59 +0000 Message-Id: <20230903142800.3870-5-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230903142800.3870-1-laoar.shao@gmail.com> References: <20230903142800.3870-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 a new cgroup helper open_classid() to get the net_cls cgroup fd. Signed-off-by: Yafang Shao --- tools/testing/selftests/bpf/cgroup_helpers.c | 16 ++++++++++++++++ tools/testing/selftests/bpf/cgroup_helpers.h | 1 + 2 files changed, 17 insertions(+) diff --git a/tools/testing/selftests/bpf/cgroup_helpers.c b/tools/testing/selftests/bpf/cgroup_helpers.c index f68fbc6..2631efe 100644 --- a/tools/testing/selftests/bpf/cgroup_helpers.c +++ b/tools/testing/selftests/bpf/cgroup_helpers.c @@ -578,6 +578,22 @@ int join_classid(void) } /** + * open_classid() - Open a cgroupv1 net_cls classid + * + * This function expects the cgroup work dir to be already created, as we + * open it here. + * + * On success, it returns the file descriptor. On failure it returns -1. + */ +int open_classid(void) +{ + char cgroup_workdir[PATH_MAX + 1]; + + format_classid_path(cgroup_workdir); + return open(cgroup_workdir, O_RDONLY); +} + +/** * cleanup_classid_environment() - Cleanup the cgroupv1 net_cls environment * * At call time, it moves the calling process to the root cgroup, and then diff --git a/tools/testing/selftests/bpf/cgroup_helpers.h b/tools/testing/selftests/bpf/cgroup_helpers.h index 5c2cb9c..ebc0513 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(unsigned int id); int join_classid(void); +int open_classid(void); int setup_classid_environment(void); void cleanup_classid_environment(void); From patchwork Sun Sep 3 14:28:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yafang Shao X-Patchwork-Id: 13373248 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 6DDB1539A for ; Sun, 3 Sep 2023 14:28:17 +0000 (UTC) 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 CA11697; Sun, 3 Sep 2023 07:28:15 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-68c3b9f85b7so206487b3a.2; Sun, 03 Sep 2023 07:28:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693751295; x=1694356095; 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=GXV1VH0aENFBhlgyeq6hQvsVgH339T3EWStJqjKjW+o=; b=iry41PyrpkV7Ny57Cr/afcfK2/SS8eUHsGTbKNY/Em4PSPLQkoufrHTR8RHtHkMNyK GIWr3j1nnjSquvklGMlkBdZv9BjuUiglmYUAlB8imGpwus5VZZMky1QclI5sMcPtfOON r0BldredxqtXiKss1b1oKadfQP22tGo/+lfNVuJJI+ZEW3Kmr+/B2qCSwe31X45fLRM/ R4H6rNwkASw7AluoQZiiNMxoxqaYysU5hwrww9WuUm3HRaxq/SqMujV4y7/22F9NZkYy iZJ0iLUcj1HV6VIhR+NzB8SaiB07fJGie3z3Naj9eeGIahFWcyt6tQFPVn8rUqXGmQTu /LMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693751295; x=1694356095; 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=GXV1VH0aENFBhlgyeq6hQvsVgH339T3EWStJqjKjW+o=; b=TzlknI79jo77jsDa0FC4L1qDRE/VeWpILVigMl5xuF2G1MQXwDqDpZbyzC/GJEDYWn XJBanESnK2MoM3ChRoXqAB1d7SxcGUa0I73jP6LLCQhNZDSQ4WXs82WNz7YB33Z1qTv7 2VSiqwnOYyMDk3/aVmiYTh0H8v7+QbfqoZ7ObIwsNT8+3DLZbjY/FLRxocV3LHVnzCfn 2P/AXwALAguil1iAx4NPZbsTMOqkUOeGgdJNFTNSocdjlw5PXd8n5DA9vwNBfmLs4CV1 ArIR1kilBLXHXkN4Sv7ijGefXQVQMxj4IeM8sR06xhAO/+haP+spP7KJjkSbNxOF2T/V 1Rxg== X-Gm-Message-State: AOJu0YwayB2pZfQt9OvZ3cQqdDGREaVuyRGkch31EadI6WKmKJll59Di lWbMUpvUI16GOmR4XpuwMV4= X-Google-Smtp-Source: AGHT+IFdvq/JFf626BfUvtNVCJRF9B3u4aRY6TYZl2FFVejJvFTTxCfr34+oEMSX0gsBmAmrtMW9Ng== X-Received: by 2002:a05:6a00:1893:b0:68c:42:d3dd with SMTP id x19-20020a056a00189300b0068c0042d3ddmr8198912pfh.27.1693751295248; Sun, 03 Sep 2023 07:28:15 -0700 (PDT) Received: from vultr.guest ([2001:19f0:ac02:185:5400:4ff:fe8f:9150]) by smtp.gmail.com with ESMTPSA id b23-20020aa78117000000b0065a1b05193asm5809977pfi.185.2023.09.03.07.28.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Sep 2023 07:28: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 Cc: cgroups@vger.kernel.org, bpf@vger.kernel.org, Yafang Shao Subject: [RFC PATCH bpf-next 5/5] selftests/bpf: Add selftests for current_under_cgroupv1v2 Date: Sun, 3 Sep 2023 14:28:00 +0000 Message-Id: <20230903142800.3870-6-laoar.shao@gmail.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20230903142800.3870-1-laoar.shao@gmail.com> References: <20230903142800.3870-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 bpf_current_task_under_cgroup() on both cgroup1 and cgroup2. The result as follows, $ tools/testing/selftests/bpf/test_progs --name=current_under_cgroupv1v2 #62/1 current_under_cgroupv1v2/test_current_under_cgroup2:OK #62/2 current_under_cgroupv1v2/test_current_under_cgroup1:OK #62 current_under_cgroupv1v2:OK Summary: 1/2 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Yafang Shao --- .../bpf/prog_tests/current_under_cgroupv1v2.c | 76 ++++++++++++++++++++++ .../bpf/progs/test_current_under_cgroupv1v2.c | 31 +++++++++ 2 files changed, 107 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/current_under_cgroupv1v2.c create mode 100644 tools/testing/selftests/bpf/progs/test_current_under_cgroupv1v2.c diff --git a/tools/testing/selftests/bpf/prog_tests/current_under_cgroupv1v2.c b/tools/testing/selftests/bpf/prog_tests/current_under_cgroupv1v2.c new file mode 100644 index 0000000..62efca3 --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/current_under_cgroupv1v2.c @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include "cgroup_helpers.h" +#include "test_current_under_cgroupv1v2.skel.h" + +#define CGROUP2_DIR "/current_under_cgroup2" + +static void attach_progs(int cgrp_fd) +{ + struct test_current_under_cgroupv1v2 *skel; + int cgrp_map_fd, ret, idx = 0; + + skel = test_current_under_cgroupv1v2__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_current_under_cgroupv1v2__open")) + return; + + cgrp_map_fd = bpf_map__fd(skel->maps.cgrp_map); + ret = bpf_map_update_elem(cgrp_map_fd, &idx, &cgrp_fd, BPF_ANY); + if (!ASSERT_OK(ret, "update_cgrp_map")) + goto cleanup; + + /* 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")) + goto cleanup; + + /* 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"); + +cleanup: + test_current_under_cgroupv1v2__destroy(skel); +} + +static void current_under_cgroup1(void) +{ + int cgrp_fd, ret; + + /* Setup cgroup1 hierarchy */ + ret = setup_classid_environment(); + if (!ASSERT_OK(ret, "setup_classid_environment")) + return; + + ret = join_classid(); + if (!ASSERT_OK(ret, "join_cgroup1")) + goto cleanup; + + cgrp_fd = open_classid(); + attach_progs(cgrp_fd); + close(cgrp_fd); + +cleanup: + /* Cleanup cgroup1 hierarchy */ + cleanup_classid_environment(); +} + +static void current_under_cgroup2(void) +{ + int cgrp_fd; + + cgrp_fd = test__join_cgroup(CGROUP2_DIR); + if (!ASSERT_GE(cgrp_fd, 0, "cgroup_join_cgroup2")) + return; + + attach_progs(cgrp_fd); + close(cgrp_fd); +} + +void test_current_under_cgroupv1v2(void) +{ + if (test__start_subtest("test_current_under_cgroup2")) + current_under_cgroup2(); + if (test__start_subtest("test_current_under_cgroup1")) + current_under_cgroup1(); +} diff --git a/tools/testing/selftests/bpf/progs/test_current_under_cgroupv1v2.c b/tools/testing/selftests/bpf/progs/test_current_under_cgroupv1v2.c new file mode 100644 index 0000000..9f0af0b --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_current_under_cgroupv1v2.c @@ -0,0 +1,31 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "vmlinux.h" +#include +#include + +struct { + __uint(type, BPF_MAP_TYPE_CGROUP_ARRAY); + __uint(key_size, sizeof(u32)); + __uint(value_size, sizeof(u32)); + __uint(max_entries, 1); +} cgrp_map SEC(".maps"); + +SEC("lsm/bpf") +int BPF_PROG(lsm_run, int cmd, union bpf_attr *attr, unsigned int size) +{ + if (cmd != BPF_LINK_CREATE) + return 0; + + if (bpf_current_task_under_cgroup(&cgrp_map, 0 /* map index */)) + return -1; + return 0; +} + +SEC("fentry/bpf_fentry_test1") +int BPF_PROG(fentry_run) +{ + return 0; +} + +char _license[] SEC("license") = "GPL";