From patchwork Tue Jun 22 11:14:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Kozlowski X-Patchwork-Id: 1495617 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4G8P202kbRz9sW7 for ; Tue, 22 Jun 2021 21:15:04 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 894383C96E1 for ; Tue, 22 Jun 2021 13:14:59 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-3.smtp.seeweb.it (in-3.smtp.seeweb.it [217.194.8.3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 6F7063C229B for ; Tue, 22 Jun 2021 13:14:48 +0200 (CEST) Received: from youngberry.canonical.com (youngberry.canonical.com [91.189.89.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by in-3.smtp.seeweb.it (Postfix) with ESMTPS id 708E51A008A1 for ; Tue, 22 Jun 2021 13:14:47 +0200 (CEST) Received: from mail-wr1-f71.google.com ([209.85.221.71]) by youngberry.canonical.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1lveMf-0006Es-KI for ltp@lists.linux.it; Tue, 22 Jun 2021 11:14:45 +0000 Received: by mail-wr1-f71.google.com with SMTP id n2-20020adfb7420000b029010e47b59f31so9510651wre.9 for ; Tue, 22 Jun 2021 04:14:45 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=G3wVOV94NLm3mFzMUIx4kDHZskacUz+fGe6l5DH9Qj8=; b=I+UJlFsS0ErIQkiR426iFNYK2vXTts6mM1tgwkdGVRWiBsceiVSxCjy1GO4rE5e3WP m1CfgIqEdr37IKMmflU7rzHTxVoF85KYUenxsEwZ5BYQL/ourjnRuM+ijrdeYR7WYNZG 6RByuCGq+JxSfzAkG/lJq05JlafE8TB7TaegLpCy+la/TzqrvE9ApPvYVxwxDY+XfmpU 2D6C4zuuTTGjy+OWxogG6pW6JxNeF5XGwSD52sxp7lcrzaGmXWz/jK7JmIHCuyfZ8Tau 5WFIQJ83oeLBdP76MZoewE4yb/OEgFCHEpxbvKs/LeMmJMrmqYhSGRNtbnolyjF71q6F jWBg== X-Gm-Message-State: AOAM531x25ZTa6Q7nLXou8iM42UUIPzH5d2Vn8zZhtmzMsCiHkmaztah Uo0kCbZsOmwX1JItCwJ54NKZz0rb6x24eLfji/+tU1/LB3+Zu9SbOGFTNOmkDoZqTKVFqdYDl1H i5qmppiCgmWTJBC45F60wuwBxbsdz X-Received: by 2002:a05:600c:3651:: with SMTP id y17mr3734321wmq.189.1624360484996; Tue, 22 Jun 2021 04:14:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw61sY/XBZIgJeD1C67ivRkaX9tZFZPRJB3w7/+89wfw/M8ODTCF50FRDWDxOkJ7IBOwSXcWQ== X-Received: by 2002:a05:600c:3651:: with SMTP id y17mr3734305wmq.189.1624360484803; Tue, 22 Jun 2021 04:14:44 -0700 (PDT) Received: from localhost.localdomain (xdsl-188-155-177-222.adslplus.ch. [188.155.177.222]) by smtp.gmail.com with ESMTPSA id n16sm10852683wrx.85.2021.06.22.04.14.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Jun 2021 04:14:44 -0700 (PDT) From: Krzysztof Kozlowski To: ltp@lists.linux.it Date: Tue, 22 Jun 2021 13:14:39 +0200 Message-Id: <20210622111440.74722-1-krzysztof.kozlowski@canonical.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-3.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_NONE autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-3.smtp.seeweb.it Subject: [LTP] [PATCH 1/2] syscalls/msgstress03: fix fork failure on small memory systems X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Running syscalls/msgstress03 on a system with less than ~4 GB of RAM fails: msgstress03 1 TFAIL : msgstress03.c:155: Fork failed (may be OK if under stress) In dmesg: LTP: starting msgstress03 cgroup: fork rejected by pids controller in /user.slice/user-1000.slice/session-1.scope The reason is cgroups pid limit set by systemd user.slice. The limit is set for login session, also for root user. For example on 2 GB RAM machine it is set as: /sys/fs/cgroup/pids/user.slice/user-0.slice/pids.max:5207 Read the maximum number of pids and adjust the test limit. For 2 GB RAM machine with systemd this will result in: msgstress03 0 TINFO : Found limit of processes 5056 (from /sys/fs/cgroup/pids/user.slice/user-1000.slice/pids.max) msgstress03 0 TINFO : Requested number of processes higher than user session limit (10000 > 4556), setting to 4556 Signed-off-by: Krzysztof Kozlowski --- include/ipcmsg.h | 2 + libs/libltpipc/libipc.c | 58 +++++++++++++++++++ .../syscalls/ipc/msgstress/msgstress03.c | 15 ++++- 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/include/ipcmsg.h b/include/ipcmsg.h index d89894b726cf..b73b72d6d172 100644 --- a/include/ipcmsg.h +++ b/include/ipcmsg.h @@ -61,8 +61,10 @@ void rm_queue(int); key_t getipckey(); int getuserid(char *); +int get_session_uid(void); int get_max_msgqueues(void); int get_used_msgqueues(void); +int get_pids_limit(void); #endif /* ipcmsg.h */ diff --git a/libs/libltpipc/libipc.c b/libs/libltpipc/libipc.c index d94880f54b64..cd3480ed9f21 100644 --- a/libs/libltpipc/libipc.c +++ b/libs/libltpipc/libipc.c @@ -151,6 +151,31 @@ int getuserid(char *user) return (ent->pw_uid); } +/* + * Get the effective session UID - either one invoking current test via sudo + * or the real UID. + */ +int get_session_uid(void) +{ + const char *sudo_uid; + + sudo_uid = getenv("SUDO_UID"); + if (sudo_uid) { + int real_uid; + + TEST(sscanf(sudo_uid, "%u", &real_uid)); + if (TEST_RETURN != 1) { +#ifdef DEBUG + tst_resm(TINFO, "No SUDO_UID from env"); +#endif + } else { + return real_uid; + } + } + + return getuid(); +} + /* * rm_shm() - removes a shared memory segment. */ @@ -218,3 +243,36 @@ int get_max_msgqueues(void) fclose(f); return atoi(buff); } + +/* + * Get the limit of processes for current session configured by systemd user.slice. + * This also applies to root user. + */ +int get_pids_limit(void) +{ + int real_uid, ret; + char path[PATH_MAX]; + long unsigned int max_pids; + + real_uid = get_session_uid(); + if (TEST_RETURN != 1) { + tst_resm(TINFO, "Cannot get UID"); + return -1; + } + + ret = snprintf(path, sizeof(path), + "/sys/fs/cgroup/pids/user.slice/user-%d.slice/pids.max", + real_uid); + if (ret < 0 || (size_t)ret >= sizeof(path)) + return -1; + + if (access(path, R_OK) != 0) { + tst_resm(TINFO, "Cannot read session user limits from '%s'", path); + return -1; + } + + SAFE_FILE_SCANF(cleanup, path, "%lu", &max_pids); + tst_resm(TINFO, "Found limit of processes %lu (from %s)", max_pids, path); + + return max_pids; +} diff --git a/testcases/kernel/syscalls/ipc/msgstress/msgstress03.c b/testcases/kernel/syscalls/ipc/msgstress/msgstress03.c index 294b401b1b38..9cf96db7956e 100644 --- a/testcases/kernel/syscalls/ipc/msgstress/msgstress03.c +++ b/testcases/kernel/syscalls/ipc/msgstress/msgstress03.c @@ -78,7 +78,7 @@ static void usage(void) int main(int argc, char **argv) { - int i, j, ok, pid; + int i, j, ok, pid, max_session_pids; int count, status; struct sigaction act; @@ -109,6 +109,19 @@ int main(int argc, char **argv) } } + max_session_pids = get_pids_limit(); + if (max_session_pids > 0) { + /* Clamp number of processes to session limit with some buffer for OS */ + max_session_pids = (max_session_pids > 500 ? max_session_pids - 500 : 0); + if (nprocs >= max_session_pids) { + tst_resm(TINFO, + "Requested number of processes higher than user session limit (%d > %d), " + "setting to %d", nprocs, max_session_pids, + max_session_pids); + nprocs = max_session_pids; + } + } + srand(getpid()); tid = -1; From patchwork Tue Jun 22 11:14:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Krzysztof Kozlowski X-Patchwork-Id: 1495616 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.linux.it (client-ip=213.254.12.146; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (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 ozlabs.org (Postfix) with ESMTPS id 4G8P1p4NWyz9sWH for ; Tue, 22 Jun 2021 21:14:53 +1000 (AEST) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 1CC383C701B for ; Tue, 22 Jun 2021 13:14:50 +0200 (CEST) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-7.smtp.seeweb.it (in-7.smtp.seeweb.it [217.194.8.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 441CE3C229B for ; Tue, 22 Jun 2021 13:14:47 +0200 (CEST) Received: from youngberry.canonical.com (youngberry.canonical.com [91.189.89.112]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by in-7.smtp.seeweb.it (Postfix) with ESMTPS id 67EFD200A11 for ; Tue, 22 Jun 2021 13:14:47 +0200 (CEST) Received: from mail-wr1-f72.google.com ([209.85.221.72]) by youngberry.canonical.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1lveMg-0006F0-5i for ltp@lists.linux.it; Tue, 22 Jun 2021 11:14:46 +0000 Received: by mail-wr1-f72.google.com with SMTP id h104-20020adf90710000b029010de8455a3aso9586970wrh.12 for ; Tue, 22 Jun 2021 04:14:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=JqAgaqEcdATLuZwKUnhgB1LotU9tq72u8E/2okEqFPI=; b=GK8lZmzd6DiuyesLV9OrED9/jmCDr2sO6P86DTUNdHdW8A1+3cia2sFGUJAi0JGFBT y/ViM8ifd2gGf+WPGA4TL41qdlfzplqjOsmw2PbibW3EzTDODG2Kj/eMy8D01aqddLbL 0MT0N7mzE+3j26+3OX/cp/TnjBhdJ+L+btbKH3bZxhydYpFElT7zOMJQFdj7wmX4lTAs pjixcqbd2ZiST61E9lV7y+SRQkm1prwe4QZCdcouEQrdBE6k67jVBZc2+7LWZJH7Xmx4 LPekjs0QY2JKo46lIvu1g2gYY1MZQ/KxhGzAr8miZEsP3fg4efbEFsdAaTJC8PLnNgcz BSnw== X-Gm-Message-State: AOAM5310iyB+FzuVDIxC+KpaD0d99YzPchAjukgRJHApo6wx9l0qYF2w ySCKxkyK8jGOOwtwyjJcYuw420DMpt9LAnnNGDqixaqjRBKmN7mwzN5RhErJRotorhN6RsmC2NO kPCI23tPc5kApJbF0x0qulTrJ/mZc X-Received: by 2002:a7b:c187:: with SMTP id y7mr3775284wmi.13.1624360485486; Tue, 22 Jun 2021 04:14:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwzwXM4V21QDKoJWvP4FaHoqjfNSiySuRg7OpkvWMR6fycpD0GOcIBKoMMf+TId3H/KAe2FqQ== X-Received: by 2002:a7b:c187:: with SMTP id y7mr3775268wmi.13.1624360485335; Tue, 22 Jun 2021 04:14:45 -0700 (PDT) Received: from localhost.localdomain (xdsl-188-155-177-222.adslplus.ch. [188.155.177.222]) by smtp.gmail.com with ESMTPSA id n16sm10852683wrx.85.2021.06.22.04.14.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Jun 2021 04:14:45 -0700 (PDT) From: Krzysztof Kozlowski To: ltp@lists.linux.it Date: Tue, 22 Jun 2021 13:14:40 +0200 Message-Id: <20210622111440.74722-2-krzysztof.kozlowski@canonical.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20210622111440.74722-1-krzysztof.kozlowski@canonical.com> References: <20210622111440.74722-1-krzysztof.kozlowski@canonical.com> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.102.4 at in-7.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.0 required=7.0 tests=SPF_HELO_NONE,SPF_NONE autolearn=disabled version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on in-7.smtp.seeweb.it Subject: [LTP] [PATCH 2/2] syscalls/msgstress04: fix fork failure on small memory systems X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it Sender: "ltp" Running syscalls/msgstress04 on a system with less than ~4 GB of RAM fails: Fork failure in the first child of child group 4396 Fork failure in the second child of child group 4413 msgstress04 1 TFAIL : msgstress04.c:222: Child exit status = 1 The reason is cgroups pid limit set by systemd user.slice. The limit is set for login session, also for root user. For example on 2 GB RAM machine it is set as: /sys/fs/cgroup/pids/user.slice/user-0.slice/pids.max:5207 Read the maximum number of pids and adjust the test limit. Signed-off-by: Krzysztof Kozlowski --- testcases/kernel/syscalls/ipc/msgstress/msgstress04.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/testcases/kernel/syscalls/ipc/msgstress/msgstress04.c b/testcases/kernel/syscalls/ipc/msgstress/msgstress04.c index f1c124990cb1..37561b18c651 100644 --- a/testcases/kernel/syscalls/ipc/msgstress/msgstress04.c +++ b/testcases/kernel/syscalls/ipc/msgstress/msgstress04.c @@ -387,7 +387,7 @@ static void term(int sig LTP_ATTRIBUTE_UNUSED) void setup(void) { - int nr_msgqs, free_pids; + int nr_msgqs, free_pids, max_session_pids; tst_tmpdir(); /* You will want to enable some signal handling so you can capture @@ -423,6 +423,13 @@ void setup(void) * For each child we fork up to 2*maxnkids grandchildren. */ maxnprocs = (free_pids / 2) / (1 + 2 * maxnkids); + max_session_pids = get_pids_limit(); + if ((max_session_pids > 0) && (max_session_pids < free_pids)) { + /* Clamp number of processes to session limit with some buffer for OS */ + max_session_pids = (max_session_pids > 500 ? max_session_pids - 500 : 0); + maxnprocs = (max_session_pids / 2) / (1 + 2 * maxnkids); + } + if (!maxnprocs) tst_brkm(TBROK, cleanup, "Not enough free pids");