Received: by 2002:ab2:5182:0:b0:1f4:61d5:3ad4 with SMTP id x2csp76771lqi; Fri, 5 Apr 2024 10:09:57 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXcYF/RwxDEGpKNZvRNR+pgZ7qgPiD3yfdzvfr4Utn6mSWfrz77MkYyVZISh5HjzhFSm64UHHnMK3dneFNN3K+5AUI3rx+/drinHzPa8Q== X-Google-Smtp-Source: AGHT+IFkKdfjNZge652Vl+K9mKf1AukGJeleejYhgMWeyLNV8rarRJa6Tra17PdKwZ/ZwwAK+Ea9 X-Received: by 2002:a05:6a00:c85:b0:6ea:e009:1ae6 with SMTP id a5-20020a056a000c8500b006eae0091ae6mr2440473pfv.25.1712336996972; Fri, 05 Apr 2024 10:09:56 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712336996; cv=pass; d=google.com; s=arc-20160816; b=MDuGy329lDrKaYrPf9jiGT9HW6zA95f0NPH8tu2NeETEZFMpILZ+YCSbNW8Fm5Sh6v Wp8LK8VoZt/XIIZcP0XGdGdKnjVjpdMI0TgO6T9R6NxxTtv6ilec08VaE3RoXo2kQGU0 Ame59ansHclxKl4ACzxZwtyOSjHZU8QMVK1LXXtNLzSvajKUT9ynTmjX0f0AVSzwTHii yZ2ouyaOTZhwl2gMROqxu9ibi8WMTConMvQdl+5J5bdcZm79Q/LRG7FN7V2JbgJSvr7d CwWRnmzUwXcWiGOKhnmz8yMSMrDP2azJow/Ak6+Fv66Klk3HFAn0N/iRuxNl5kDyt/tS jCPQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature:dkim-signature; bh=8zUqm5lIfGuNd8A8EKniU9Rq618E06v9L0Eq25l5kFU=; fh=p0J9JzyCwzl+Dp1hxu+aosaMJXYbpVHbkqE47vScQDY=; b=JAUXdCc1xRYdaGitiWEEhFaVrlaBx7D+rLcZdzc1bWprEiHFMne2IeT6Tbs6n53FjS sZrxYL8CB2142f3rOkIjYJydSLfxYySjovNpjK2DwzwM1Waggq7qMHlkfTXlhGQMfZ1z Bn6+8JesZjZ+gvGMH5lWpDAVk45neGHK9DmvI07SFjZH2Dpcur4yCucaIH/xGEwBF6+z 5T+DShQWORffLiS2fj+9g5KpGZG9520enhtjIBexszLRgxA0rmmib5eL9c0lSOpCaQWQ 57bm+7FbVUBVXwG2ughZ/66avSdceoI45T45yJmOOnlDGpbptRX/G6DHDKMKpRZ/svQJ HFSA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=vLvNRChO; dkim=pass header.i=@suse.com header.s=susede1 header.b=vLvNRChO; arc=pass (i=1 spf=pass spfdomain=suse.com dkim=pass dkdomain=suse.com dkim=pass dkdomain=suse.com dmarc=pass fromdomain=suse.com); spf=pass (google.com: domain of linux-kernel+bounces-133387-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-133387-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id hq25-20020a056a00681900b006e63eb52befsi1642282pfb.92.2024.04.05.10.09.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Apr 2024 10:09:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-133387-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=vLvNRChO; dkim=pass header.i=@suse.com header.s=susede1 header.b=vLvNRChO; arc=pass (i=1 spf=pass spfdomain=suse.com dkim=pass dkdomain=suse.com dkim=pass dkdomain=suse.com dmarc=pass fromdomain=suse.com); spf=pass (google.com: domain of linux-kernel+bounces-133387-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-133387-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 83BF2B245A4 for ; Fri, 5 Apr 2024 17:06:48 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 45C0B172796; Fri, 5 Apr 2024 17:05:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="vLvNRChO"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="vLvNRChO" Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2E1A171652; Fri, 5 Apr 2024 17:05:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712336756; cv=none; b=tGO5PLDxGroHAbJwVFS5LITkGhbqL1LNePrfvpyDS9whmSiSBEpuSDdjxTZf9Jb7YRIdJATFwrpFMMBtAsJFNZjJOyb+oMpvnb18h6FtLbO1DuDOHvFKcbqgp+E0nKMDoFxTujLfWW5tCFlq5UGTj3OWMFcHNdl+7PBKCaxC7Rc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712336756; c=relaxed/simple; bh=wMulK/G+6RPSUybH7+ViBKiUepR9/Avresur9lAF0vs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=JMOcv0XEZ/KbzmCnrmHyMHQSVG4FDUZ+zPrK8jCRT06qWPS+fEs8PyZNqkjYWRRP7hNlW321Og6dZh3JRjXJQGWC1nbjwlQcEP5ReseB+t3HOtNnuxbC4LxQGI/dN4tipjgs/j3tuKwocCLn09S8AJOyBzimTMPZTJZ4NPig65k= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=vLvNRChO; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=vLvNRChO; arc=none smtp.client-ip=195.135.223.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 345BC1F7F3; Fri, 5 Apr 2024 17:05:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1712336752; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8zUqm5lIfGuNd8A8EKniU9Rq618E06v9L0Eq25l5kFU=; b=vLvNRChO5/oY4AuD2uppLI5q3fvHax4KqhQw6bnqNTRJoWKDfNjMxPI2Drpb37Zh3rNzcB NbR07Ev1/P1pO/tFFzY/0hK1L0uJmaTUCC5mTTpOPuQOrJnQsA4Nqlh2cNkoKPPTYI8kik 3g1GSgj9/oNghqjxBaH2jq22dPZynGI= Authentication-Results: smtp-out2.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=vLvNRChO DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1712336752; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=8zUqm5lIfGuNd8A8EKniU9Rq618E06v9L0Eq25l5kFU=; b=vLvNRChO5/oY4AuD2uppLI5q3fvHax4KqhQw6bnqNTRJoWKDfNjMxPI2Drpb37Zh3rNzcB NbR07Ev1/P1pO/tFFzY/0hK1L0uJmaTUCC5mTTpOPuQOrJnQsA4Nqlh2cNkoKPPTYI8kik 3g1GSgj9/oNghqjxBaH2jq22dPZynGI= Received: from imap2.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id BD9C113AB0; Fri, 5 Apr 2024 17:05:51 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap2.dmz-prg2.suse.org with ESMTPSA id ePF8LW8vEGYteAAAn2gu4w (envelope-from ); Fri, 05 Apr 2024 17:05:51 +0000 From: =?UTF-8?q?Michal=20Koutn=C3=BD?= To: cgroups@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Tejun Heo , Zefan Li , Johannes Weiner , Jonathan Corbet , Shuah Khan Subject: [RFC PATCH v3 6/9] selftests: cgroup: Add basic tests for pids controller Date: Fri, 5 Apr 2024 19:05:45 +0200 Message-ID: <20240405170548.15234-7-mkoutny@suse.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240405170548.15234-1-mkoutny@suse.com> References: <20240405170548.15234-1-mkoutny@suse.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Level: X-Spamd-Result: default: False [-3.51 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; FUZZY_BLOCKED(0.00)[rspamd.com]; RCVD_VIA_SMTP_AUTH(0.00)[]; MIME_TRACE(0.00)[0:+]; ARC_NA(0.00)[]; TO_DN_SOME(0.00)[]; RCPT_COUNT_SEVEN(0.00)[9]; RCVD_TLS_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b281:104:10:150:64:98:from]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.com:dkim,suse.com:email,imap2.dmz-prg2.suse.org:helo,imap2.dmz-prg2.suse.org:rdns]; DKIM_TRACE(0.00)[suse.com:+] X-Rspamd-Action: no action X-Rspamd-Queue-Id: 345BC1F7F3 X-Rspamd-Server: rspamd1.dmz-prg2.suse.org X-Spam-Flag: NO X-Spam-Score: -3.51 This commit adds (and wires in) new test program for checking basic pids controller functionality -- restricting tasks in a cgroup and correct event counting. Signed-off-by: Michal Koutný --- tools/testing/selftests/cgroup/Makefile | 2 + tools/testing/selftests/cgroup/test_pids.c | 187 +++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 tools/testing/selftests/cgroup/test_pids.c diff --git a/tools/testing/selftests/cgroup/Makefile b/tools/testing/selftests/cgroup/Makefile index f3e1ef69e88d..f5f0886a2c4a 100644 --- a/tools/testing/selftests/cgroup/Makefile +++ b/tools/testing/selftests/cgroup/Makefile @@ -15,6 +15,7 @@ TEST_GEN_PROGS += test_hugetlb_memcg TEST_GEN_PROGS += test_kill TEST_GEN_PROGS += test_kmem TEST_GEN_PROGS += test_memcontrol +TEST_GEN_PROGS += test_pids TEST_GEN_PROGS += test_zswap LOCAL_HDRS += $(selfdir)/clone3/clone3_selftests.h $(selfdir)/pidfd/pidfd.h @@ -29,4 +30,5 @@ $(OUTPUT)/test_hugetlb_memcg: cgroup_util.c $(OUTPUT)/test_kill: cgroup_util.c $(OUTPUT)/test_kmem: cgroup_util.c $(OUTPUT)/test_memcontrol: cgroup_util.c +$(OUTPUT)/test_pids: cgroup_util.c $(OUTPUT)/test_zswap: cgroup_util.c diff --git a/tools/testing/selftests/cgroup/test_pids.c b/tools/testing/selftests/cgroup/test_pids.c new file mode 100644 index 000000000000..c1c3a3965624 --- /dev/null +++ b/tools/testing/selftests/cgroup/test_pids.c @@ -0,0 +1,187 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include + +#include "../kselftest.h" +#include "cgroup_util.h" + +static int run_success(const char *cgroup, void *arg) +{ + return 0; +} + +static int run_pause(const char *cgroup, void *arg) +{ + return pause(); +} + +/* + * This test checks that pids.max prevents forking new children above the + * specified limit in the cgroup. + */ +static int test_pids_max(const char *root) +{ + int ret = KSFT_FAIL; + char *cg_pids; + int pid; + + + cg_pids = cg_name(root, "pids_test"); + if (!cg_pids) + goto cleanup; + + if (cg_create(cg_pids)) + goto cleanup; + + if (cg_read_strcmp(cg_pids, "pids.max", "max\n")) + goto cleanup; + + if (cg_write(cg_pids, "pids.max", "2")) + goto cleanup; + + if (cg_enter_current(cg_pids)) + goto cleanup; + + pid = cg_run_nowait(cg_pids, run_pause, NULL); + if (pid < 0) + goto cleanup; + + if (cg_run_nowait(cg_pids, run_success, NULL) != -1 || errno != EAGAIN) + goto cleanup; + + if (kill(pid, SIGINT)) + goto cleanup; + + ret = KSFT_PASS; + +cleanup: + cg_enter_current(root); + cg_destroy(cg_pids); + free(cg_pids); + + return ret; +} + +/* + * This test checks that pids.max prevents forking new children above the + * specified limit in the cgroup. + */ +static int test_pids_events(const char *root) +{ + int ret = KSFT_FAIL; + char *cg_parent = NULL, *cg_child = NULL; + int pid; + + + cg_parent = cg_name(root, "pids_parent"); + cg_child = cg_name(cg_parent, "pids_child"); + if (!cg_parent || !cg_child) + goto cleanup; + + if (cg_create(cg_parent)) + goto cleanup; + if (cg_write(cg_parent, "cgroup.subtree_control", "+pids")) + goto cleanup; + if (cg_create(cg_child)) + goto cleanup; + + if (cg_write(cg_parent, "pids.max", "2")) + goto cleanup; + + if (cg_read_strcmp(cg_child, "pids.max", "max\n")) + goto cleanup; + + if (cg_enter_current(cg_child)) + goto cleanup; + + pid = cg_run_nowait(cg_child, run_pause, NULL); + if (pid < 0) + goto cleanup; + + if (cg_run_nowait(cg_child, run_success, NULL) != -1 || errno != EAGAIN) + goto cleanup; + + if (kill(pid, SIGINT)) + goto cleanup; + + + if (cg_read_key_long(cg_child, "pids.events", "max ") != 0) + goto cleanup; + if (cg_read_key_long(cg_child, "pids.events", "max.imposed ") != 1) + goto cleanup; + + if (cg_read_key_long(cg_parent, "pids.events", "max ") != 1) + goto cleanup; + if (cg_read_key_long(cg_parent, "pids.events", "max.imposed ") != 1) + goto cleanup; + + + ret = KSFT_PASS; + +cleanup: + cg_enter_current(root); + if (cg_child) + cg_destroy(cg_child); + if (cg_parent) + cg_destroy(cg_parent); + free(cg_child); + free(cg_parent); + + return ret; +} + + + +#define T(x) { x, #x } +struct pids_test { + int (*fn)(const char *root); + const char *name; +} tests[] = { + T(test_pids_max), + T(test_pids_events), +}; +#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"); + + /* + * Check that pids controller is available: + * pids is listed in cgroup.controllers + */ + if (cg_read_strstr(root, "cgroup.controllers", "pids")) + ksft_exit_skip("pids controller isn't available\n"); + + if (cg_read_strstr(root, "cgroup.subtree_control", "pids")) + if (cg_write(root, "cgroup.subtree_control", "+pids")) + ksft_exit_skip("Failed to set pids 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.44.0