Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp675029pxb; Fri, 22 Apr 2022 09:02:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzo3pvpaLFnm1tLgnSTpMebUWPrQ1VLv7X5t3ilGihwsjYvJFh3WX3UI0eVZPFhQd6Clgwz X-Received: by 2002:aa7:d2d6:0:b0:423:97a4:801c with SMTP id k22-20020aa7d2d6000000b0042397a4801cmr5509907edr.383.1650643338402; Fri, 22 Apr 2022 09:02:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650643338; cv=none; d=google.com; s=arc-20160816; b=pzm9AK2wKCo5/SwQzP72+eX56dHVHXEGXBWU3oUjRB5Dma2x2B8Zj8QsctCL1h9UFs +6y3m7Jbo2xQIOklTavZDCAMz//BSIbFvdytDa5PElSy4S9GtBuTg/5UyvgZk8Lq7BqE V3tGyGG9c9qfwqAgPDCRR5w6x/i2K8O3VpIka1JzN7xLlAGf+szBrxf5Ndb1oHGwT1Ao jg9ehcJ86ueuWlZ+CJTo3KcYnFWTVvnkatNL1b6b3+UvVc1EfYJoCQHlqvqKMRBseGmD t3cBpb2pmbe2W2LvFqgoxB5pJxL/aR2LhVtf6ACTyNLgC2FsB3TYxR5XLM/14KcEw4Dr lCOg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=J8G/ibBIrn0G3wVr557AIgTZhD6q0eUwk63lcPEE0+k=; b=0wKwNGNw5BdmrEf6CiR/v3vp9Af0JIqkrBECKdqXe/yKqx4dTFGFT9S9xCX9fTU1I1 xdIuMZkrXjR+B2uuuP3YJdqJ3HfWFCGEbwv23taZ0yve74/1e6TjVPKE3swxAtUJFkwe +IWb620kP9op8qQJ6KXQnMKdL80GdMnzlV3voGAJv+HEFBOb/vkRKqd0orBI2Z3153jM CLQKkSg5+pbqBKDTcCHkfhK6M/5iq16C4V3CzxmJtnjIz1rs2yvizsgvYi6QmgFdkxm0 ZHu7iEgzf/mqDnpSz5V5+VLdpM7dLYjOUGw+e3uzI4D2QCWV5tVvibXCtCPS0X9j3KOD Ek8Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i7-20020a170906264700b006efe41b91ecsi6947977ejc.490.2022.04.22.09.01.54; Fri, 22 Apr 2022 09:02:18 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358009AbiDSVfq (ORCPT + 99 others); Tue, 19 Apr 2022 17:35:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346877AbiDSVfn (ORCPT ); Tue, 19 Apr 2022 17:35:43 -0400 Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1222D27FEA; Tue, 19 Apr 2022 14:33:00 -0700 (PDT) Received: by mail-qt1-f182.google.com with SMTP id d14so5328934qtw.5; Tue, 19 Apr 2022 14:33:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=J8G/ibBIrn0G3wVr557AIgTZhD6q0eUwk63lcPEE0+k=; b=lKoLNV1zNT5kktAzElOnCutYNXYOsWVcnzmK0HOjKxKiE83QiErA0aWih+IsEgXIAM A0ksdo4XQ+EQ7VfBe0dOWm0/BwUoS42qDZ9HjTCllamS6dzDChRnQmPYu/AzirI6Eapd YMnEjvhGSK1VKnmGfBEGwlw/sHNQh1aM6KfFPoEmFJGgaQLJcwaS4B3MGZm+9EMH0qBk BMbngJgYQLKG7tZv0OEp1cEtd12euLkETzgk4Ik/kXJSdxQjuIYjzFRL4ParOSGgGM7R fQnaxQiKYc9ELkeQYy8RrNSaE7r5I/49/ZKBq74UDm7uAOYYM5YTV+HHkV+1VBi8k5hG 4jUA== X-Gm-Message-State: AOAM532LVdsfs891go7KV9rlH+HiCsJ4UtPpBR86YT8BypLpdXR57gSI OxH63NsSaIFAxEu8KFQKVF4= X-Received: by 2002:a05:622a:8:b0:2e1:cb36:9244 with SMTP id x8-20020a05622a000800b002e1cb369244mr11781499qtw.93.1650403979018; Tue, 19 Apr 2022 14:32:59 -0700 (PDT) Received: from localhost (fwdproxy-ash-007.fbsv.net. [2a03:2880:20ff:7::face:b00c]) by smtp.gmail.com with ESMTPSA id u13-20020a05622a010d00b002f0c6664db1sm628991qtw.49.2022.04.19.14.32.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Apr 2022 14:32:58 -0700 (PDT) From: David Vernet To: tj@kernel.org, lizefan.x@bytedance.com, hannes@cmpxchg.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org Cc: kernel-team@fb.com Subject: [PATCH 1/4] cgroup: Add new test_cpu.c test suite in cgroup selftests Date: Tue, 19 Apr 2022 14:32:42 -0700 Message-Id: <20220419213244.2104972-2-void@manifault.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220419213244.2104972-1-void@manifault.com> References: <20220419213244.2104972-1-void@manifault.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The cgroup selftests suite currently contains tests that validate various aspects of cgroup, such as validating the expected behavior for memory controllers, the expected behavior of cgroup.procs, etc. There are no tests that validate the expected behavior of the cgroup cpu controller. This patch therefore adds a new test_cpu.c file that will contain cpu controller testcases. The file currently only contains a single testcase that validates creating nested cgroups with cgroup.subtree_control including cpu. Future patches will add more sophisticated testcases that validate functional aspects of the cpu controller. Signed-off-by: David Vernet --- tools/testing/selftests/cgroup/.gitignore | 1 + tools/testing/selftests/cgroup/Makefile | 2 + tools/testing/selftests/cgroup/test_cpu.c | 110 ++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 tools/testing/selftests/cgroup/test_cpu.c diff --git a/tools/testing/selftests/cgroup/.gitignore b/tools/testing/selftests/cgroup/.gitignore index be9643ef6285..306ee1b01e72 100644 --- a/tools/testing/selftests/cgroup/.gitignore +++ b/tools/testing/selftests/cgroup/.gitignore @@ -4,3 +4,4 @@ test_core test_freezer test_kmem test_kill +test_cpu diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selftests/cgroup/Makefile index 745fe25fa0b9..478217cc1371 100644 --- a/tools/testing/selftests/cgroup/Makefile +++ b/tools/testing/selftests/cgroup/Makefile @@ -10,6 +10,7 @@ TEST_GEN_PROGS += test_kmem TEST_GEN_PROGS += test_core TEST_GEN_PROGS += test_freezer TEST_GEN_PROGS += test_kill +TEST_GEN_PROGS += test_cpu LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h @@ -20,3 +21,4 @@ $(OUTPUT)/test_kmem: cgroup_util.c $(OUTPUT)/test_core: cgroup_util.c $(OUTPUT)/test_freezer: cgroup_util.c $(OUTPUT)/test_kill: cgroup_util.c +$(OUTPUT)/test_cpu: cgroup_util.c diff --git a/tools/testing/selftests/cgroup/test_cpu.c b/tools/testing/selftests/cgroup/test_cpu.c new file mode 100644 index 000000000000..4faa279bbab3 --- /dev/null +++ b/tools/testing/selftests/cgroup/test_cpu.c @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0 + +#define _GNU_SOURCE +#include +#include + +#include "../kselftest.h" +#include "cgroup_util.h" + +/* + * This test creates two nested cgroups with and without enabling + * the cpu controller. + */ +static int test_cgcpu_subtree_control(const char *root) +{ + char *parent = NULL, *child = NULL, *parent2 = NULL, *child2 = NULL; + int ret = KSFT_FAIL; + + // Create two nested cgroups with the cpu controller enabled. + parent = cg_name(root, "cgcpu_test_0"); + if (!parent) + goto cleanup; + + if (cg_create(parent)) + goto cleanup; + + if (cg_write(parent, "cgroup.subtree_control", "+cpu")) + goto cleanup; + + child = cg_name(parent, "cgcpu_test_child"); + if (!child) + goto cleanup; + + if (cg_create(child)) + goto cleanup; + + if (cg_read_strstr(child, "cgroup.controllers", "cpu")) + goto cleanup; + + // Create two nested cgroups without enabling the cpu controller. + parent2 = cg_name(root, "cgcpu_test_1"); + if (!parent2) + goto cleanup; + + if (cg_create(parent2)) + goto cleanup; + + child2 = cg_name(parent2, "cgcpu_test_child"); + if (!child2) + goto cleanup; + + if (cg_create(child2)) + goto cleanup; + + if (!cg_read_strstr(child2, "cgroup.controllers", "cpu")) + goto cleanup; + + ret = KSFT_PASS; + +cleanup: + cg_destroy(child); + free(child); + cg_destroy(child2); + free(child2); + cg_destroy(parent); + free(parent); + cg_destroy(parent2); + free(parent2); + + return ret; +} + +#define T(x) { x, #x } +struct cgcpu_test { + int (*fn)(const char *root); + const char *name; +} tests[] = { + T(test_cgcpu_subtree_control), +}; +#undef T + +int main(int argc, char *argv[]) +{ + char root[PATH_MAX]; + int i, ret = EXIT_SUCCESS; + + if (cg_find_unified_root(root, sizeof(root))) + ksft_exit_skip("cgroup v2 isn't mounted\n"); + + if (cg_read_strstr(root, "cgroup.subtree_control", "cpu")) + if (cg_write(root, "cgroup.subtree_control", "+cpu")) + ksft_exit_skip("Failed to set cpu controller\n"); + + for (i = 0; i < ARRAY_SIZE(tests); i++) { + switch (tests[i].fn(root)) { + case KSFT_PASS: + ksft_test_result_pass("%s\n", tests[i].name); + break; + case KSFT_SKIP: + ksft_test_result_skip("%s\n", tests[i].name); + break; + default: + ret = EXIT_FAILURE; + ksft_test_result_fail("%s\n", tests[i].name); + break; + } + } + + return ret; +} -- 2.30.2