From patchwork Thu Mar 5 13:48:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 1249584 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=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (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 48YBt30F8dz9sPK for ; Fri, 6 Mar 2020 00:48:42 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 690A93C6542 for ; Thu, 5 Mar 2020 14:48:39 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [IPv6:2001:4b78:1:20::2]) by picard.linux.it (Postfix) with ESMTP id 4FF803C650A for ; Thu, 5 Mar 2020 14:48:37 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id D0BA6600B3B for ; Thu, 5 Mar 2020 14:48:36 +0100 (CET) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 6BEA8B125 for ; Thu, 5 Mar 2020 13:48:36 +0000 (UTC) From: Cyril Hrubis To: ltp@lists.linux.it Date: Thu, 5 Mar 2020 14:48:27 +0100 Message-Id: <20200305134834.16736-2-chrubis@suse.cz> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200305134834.16736-1-chrubis@suse.cz> References: <20200305134834.16736-1-chrubis@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.2 required=7.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH 1/8] lapi/namespace_constants.h: Add CLONE_NEWTIME 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" Signed-off-by: Cyril Hrubis --- include/lapi/namespaces_constants.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/lapi/namespaces_constants.h b/include/lapi/namespaces_constants.h index da4a7a4a1..8f73c4302 100644 --- a/include/lapi/namespaces_constants.h +++ b/include/lapi/namespaces_constants.h @@ -24,5 +24,8 @@ #ifndef CLONE_NEWUTS # define CLONE_NEWUTS 0x04000000 #endif +#ifndef CLONE_NEWTIME +# define CLONE_NEWTIME 0x00000080 +#endif #endif /* __NAMESPACES_CONSTANTS_H__ */ From patchwork Thu Mar 5 13:48:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 1249585 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=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz 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 48YBtG08F8z9sPK for ; Fri, 6 Mar 2020 00:48:53 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 27BA53C6521 for ; Thu, 5 Mar 2020 14:48:51 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [217.194.8.2]) by picard.linux.it (Postfix) with ESMTP id 02B103C650A for ; Thu, 5 Mar 2020 14:48:38 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id 84196600BC8 for ; Thu, 5 Mar 2020 14:48:37 +0100 (CET) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 2C15DB125 for ; Thu, 5 Mar 2020 13:48:37 +0000 (UTC) From: Cyril Hrubis To: ltp@lists.linux.it Date: Thu, 5 Mar 2020 14:48:28 +0100 Message-Id: <20200305134834.16736-3-chrubis@suse.cz> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200305134834.16736-1-chrubis@suse.cz> References: <20200305134834.16736-1-chrubis@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.2 required=7.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH 2/8] lapi: Add a configure check and fallback for setns 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" Signed-off-by: Cyril Hrubis --- configure.ac | 1 + include/lapi/setns.h | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 include/lapi/setns.h diff --git a/configure.ac b/configure.ac index c9ec39fce..cd52806de 100644 --- a/configure.ac +++ b/configure.ac @@ -110,6 +110,7 @@ AC_CHECK_FUNCS([ \ renameat2 \ sched_getcpu \ sendmmsg \ + setns \ sigpending \ splice \ statx \ diff --git a/include/lapi/setns.h b/include/lapi/setns.h new file mode 100644 index 000000000..7b0a7afc4 --- /dev/null +++ b/include/lapi/setns.h @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + Copyright (c) 2020 Cyril Hrubis + */ + +#ifndef LAPI_SETNS_H__ +#define LAPI_SETNS_H__ + +#include "config.h" +#include "lapi/syscalls.h" +#include + +#ifndef HAVE_SETNS +int setns(int fd, int nstype) +{ + return tst_syscall(__NR_setns, fd, nstype); +} +#endif + +#endif /* LAPI_SETNS_H__ */ From patchwork Thu Mar 5 13:48:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 1249586 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=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz 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 48YBtS1DzMz9sQt for ; Fri, 6 Mar 2020 00:49:03 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 5A1763C6544 for ; Thu, 5 Mar 2020 14:49:01 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [IPv6:2001:4b78:1:20::2]) by picard.linux.it (Postfix) with ESMTP id 7CA5F3C650A for ; Thu, 5 Mar 2020 14:48:38 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id 44328600BC8 for ; Thu, 5 Mar 2020 14:48:38 +0100 (CET) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id DE4E7B129 for ; Thu, 5 Mar 2020 13:48:37 +0000 (UTC) From: Cyril Hrubis To: ltp@lists.linux.it Date: Thu, 5 Mar 2020 14:48:29 +0100 Message-Id: <20200305134834.16736-4-chrubis@suse.cz> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200305134834.16736-1-chrubis@suse.cz> References: <20200305134834.16736-1-chrubis@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.2 required=7.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH 3/8] include/tst_timer: Fix normalization 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" The timespec_add and timespec_sub functions were producing incorrect results when passed negative input. We have to normalize the result in both cases for nseconds < 0 && nseconds > 1s. Signed-off-by: Cyril Hrubis --- include/tst_timer.h | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/include/tst_timer.h b/include/tst_timer.h index de60bc62a..b091137dc 100644 --- a/include/tst_timer.h +++ b/include/tst_timer.h @@ -134,6 +134,21 @@ static inline int tst_timespec_lt(struct timespec t1, struct timespec t2) return t1.tv_sec < t2.tv_sec; } +static inline struct timespec tst_timespec_normalize(struct timespec t) +{ + if (t.tv_nsec >= 1000000000) { + t.tv_sec++; + t.tv_nsec -= 1000000000; + } + + if (t.tv_nsec < 0) { + t.tv_sec--; + t.tv_nsec += 1000000000; + } + + return t; +} + /* * Adds us microseconds to t. */ @@ -143,12 +158,8 @@ static inline struct timespec tst_timespec_add_us(struct timespec t, t.tv_sec += us / 1000000; t.tv_nsec += (us % 1000000) * 1000; - if (t.tv_nsec >= 1000000000) { - t.tv_sec++; - t.tv_nsec -= 1000000000; - } - return t; + return tst_timespec_normalize(t); } /* @@ -162,12 +173,7 @@ static inline struct timespec tst_timespec_add(struct timespec t1, res.tv_sec = t1.tv_sec + t2.tv_sec; res.tv_nsec = t1.tv_nsec + t2.tv_nsec; - if (res.tv_nsec >= 1000000000) { - res.tv_sec++; - res.tv_nsec -= 1000000000; - } - - return res; + return tst_timespec_normalize(res); } /* @@ -179,12 +185,7 @@ static inline struct timespec tst_timespec_sub_us(struct timespec t, t.tv_sec -= us / 1000000; t.tv_nsec -= (us % 1000000) * 1000; - if (t.tv_nsec < 0) { - t.tv_sec--; - t.tv_nsec += 1000000000; - } - - return t; + return tst_timespec_normalize(t); } /* From patchwork Thu Mar 5 13:48:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 1249587 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=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz 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 48YBtg2Twzz9sPK for ; Fri, 6 Mar 2020 00:49:15 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 85C583C6544 for ; Thu, 5 Mar 2020 14:49:12 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-2.smtp.seeweb.it (in-2.smtp.seeweb.it [IPv6:2001:4b78:1:20::2]) by picard.linux.it (Postfix) with ESMTP id 5A7F93C6540 for ; Thu, 5 Mar 2020 14:48:39 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-2.smtp.seeweb.it (Postfix) with ESMTPS id 07788600BC8 for ; Thu, 5 Mar 2020 14:48:39 +0100 (CET) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 9E8DBAB6D for ; Thu, 5 Mar 2020 13:48:38 +0000 (UTC) From: Cyril Hrubis To: ltp@lists.linux.it Date: Thu, 5 Mar 2020 14:48:30 +0100 Message-Id: <20200305134834.16736-5-chrubis@suse.cz> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200305134834.16736-1-chrubis@suse.cz> References: <20200305134834.16736-1-chrubis@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-2.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.2 required=7.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-2.smtp.seeweb.it Subject: [LTP] [PATCH 4/8] syscalls/sysinfo03: Add time namespace test 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" This tests that the uptime in sysinfo() is adjusted correctly by the namespace offset. Also check that /proc/uptime is consistent with the uptime from the sysinfo() syscall. Signed-off-by: Cyril Hrubis --- runtest/containers | 3 + runtest/syscalls | 1 + testcases/kernel/syscalls/sysinfo/.gitignore | 1 + testcases/kernel/syscalls/sysinfo/sysinfo03.c | 78 +++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 testcases/kernel/syscalls/sysinfo/sysinfo03.c diff --git a/runtest/containers b/runtest/containers index 871cd2a42..4dc05af93 100644 --- a/runtest/containers +++ b/runtest/containers @@ -85,3 +85,6 @@ userns04 userns04 userns05 userns05 userns06 userns06 userns07 userns07 + +# time namespaces +sysinfo03 sysinfo03 diff --git a/runtest/syscalls b/runtest/syscalls index 06d96470c..34bd5c3da 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -1441,6 +1441,7 @@ sysfs06 sysfs06 sysinfo01 sysinfo01 sysinfo02 sysinfo02 +sysinfo03 sysinfo03 syslog01 syslog01 syslog02 syslog02 diff --git a/testcases/kernel/syscalls/sysinfo/.gitignore b/testcases/kernel/syscalls/sysinfo/.gitignore index aa7c26946..8ad2279a4 100644 --- a/testcases/kernel/syscalls/sysinfo/.gitignore +++ b/testcases/kernel/syscalls/sysinfo/.gitignore @@ -1,2 +1,3 @@ /sysinfo01 /sysinfo02 +/sysinfo03 diff --git a/testcases/kernel/syscalls/sysinfo/sysinfo03.c b/testcases/kernel/syscalls/sysinfo/sysinfo03.c new file mode 100644 index 000000000..979e6e0a7 --- /dev/null +++ b/testcases/kernel/syscalls/sysinfo/sysinfo03.c @@ -0,0 +1,78 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + Copyright (c) 2020 Cyril Hrubis + */ +/* + + Test if CLOCK_BOOTTIME namespace offset is applied to sysinfo uptime and that + it's consistent with /proc/uptime as well. + + After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the + process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC + and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'. + + */ + +#include +#include "lapi/namespaces_constants.h" +#include "tst_test.h" + +static int offsets[] = { + 10, + -10, + 3600, +}; + +static long read_proc_uptime(void) +{ + long sec, sec_rem; + + SAFE_FILE_SCANF("/proc/uptime", "%li.%li", &sec, &sec_rem); + + return sec + (sec_rem ? 1 : 0); +} + +static void verify_sysinfo(unsigned int n) +{ + struct sysinfo si; + long uptime; + int off = offsets[n]; + + SAFE_UNSHARE(CLONE_NEWTIME); + + SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0", + CLOCK_BOOTTIME, off); + + sysinfo(&si); + + uptime = si.uptime; + + if (!SAFE_FORK()) { + sysinfo(&si); + long proc_uptime = read_proc_uptime(); + + long diff = si.uptime - uptime; + + if (diff < off || diff > off + 1) + tst_res(TFAIL, "Wrong sysinfo uptime offset %li", diff); + else + tst_res(TPASS, "Correct sysinfo uptime offset %i", off); + + if (si.uptime < proc_uptime || si.uptime > proc_uptime + 1) { + tst_res(TFAIL, "/proc/uptime %li differs from sysinfo %li", + proc_uptime, si.uptime); + } else { + tst_res(TPASS, "/proc/uptime is consistent with sysinfo"); + } + } +} + +static struct tst_test test = { + .tcnt = ARRAY_SIZE(offsets), + .test = verify_sysinfo, + .needs_root = 1, + .forks_child = 1, + .needs_kconfigs = (const char *[]) { + "CONFIG_TIME_NS=y" + } +}; From patchwork Thu Mar 5 13:48:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 1249589 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=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (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 48YBv42q6vz9sQt for ; Fri, 6 Mar 2020 00:49:36 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 294213C653C for ; Thu, 5 Mar 2020 14:49:33 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-5.smtp.seeweb.it (in-5.smtp.seeweb.it [IPv6:2001:4b78:1:20::5]) by picard.linux.it (Postfix) with ESMTP id 0F1203C6549 for ; Thu, 5 Mar 2020 14:48:40 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-5.smtp.seeweb.it (Postfix) with ESMTPS id B61C460CB8E for ; Thu, 5 Mar 2020 14:48:39 +0100 (CET) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 5F2F7AB6D for ; Thu, 5 Mar 2020 13:48:39 +0000 (UTC) From: Cyril Hrubis To: ltp@lists.linux.it Date: Thu, 5 Mar 2020 14:48:31 +0100 Message-Id: <20200305134834.16736-6-chrubis@suse.cz> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200305134834.16736-1-chrubis@suse.cz> References: <20200305134834.16736-1-chrubis@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-5.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.2 required=7.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-5.smtp.seeweb.it Subject: [LTP] [PATCH 5/8] syscalls/clock_nanosleep03: Add test for time namespace 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" Test that absolute timeout for clock nanosleep is adjusted by the offset correctly inside of a time namespace. Signed-off-by: Cyril Hrubis --- runtest/containers | 1 + runtest/syscalls | 1 + .../syscalls/clock_nanosleep/.gitignore | 1 + .../clock_nanosleep/clock_nanosleep03.c | 71 +++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep03.c diff --git a/runtest/containers b/runtest/containers index 4dc05af93..8100cd2bc 100644 --- a/runtest/containers +++ b/runtest/containers @@ -88,3 +88,4 @@ userns07 userns07 # time namespaces sysinfo03 sysinfo03 +clock_nanosleep03 clock_nanosleep03 diff --git a/runtest/syscalls b/runtest/syscalls index 34bd5c3da..d19ae0041 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -87,6 +87,7 @@ clock_getres01 clock_getres01 clock_nanosleep01 clock_nanosleep01 clock_nanosleep02 clock_nanosleep02 clock_nanosleep2_01 clock_nanosleep2_01 +clock_nanosleep03 clock_nanosleep03 clock_gettime01 clock_gettime01 clock_gettime02 clock_gettime02 diff --git a/testcases/kernel/syscalls/clock_nanosleep/.gitignore b/testcases/kernel/syscalls/clock_nanosleep/.gitignore index 6714ff468..406897cde 100644 --- a/testcases/kernel/syscalls/clock_nanosleep/.gitignore +++ b/testcases/kernel/syscalls/clock_nanosleep/.gitignore @@ -1,2 +1,3 @@ /clock_nanosleep01 /clock_nanosleep02 +/clock_nanosleep03 diff --git a/testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep03.c b/testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep03.c new file mode 100644 index 000000000..99ff99736 --- /dev/null +++ b/testcases/kernel/syscalls/clock_nanosleep/clock_nanosleep03.c @@ -0,0 +1,71 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + + Copyright (c) 2020 Cyril Hrubis + + */ +/* + + Test that clock_nanosleep() adds correctly an offset with absolute timeout + and CLOCK_MONOTONIC inside of a timer namespace. + + After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the + process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC + and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'. + + */ + +#include +#include "tst_safe_clocks.h" +#include "tst_timer.h" +#include "lapi/namespaces_constants.h" +#include "tst_test.h" + +#define OFFSET_S 10 +#define SLEEP_US 100000 + +static void verify_clock_nanosleep(void) +{ + struct timespec start, end, sleep_abs; + + SAFE_UNSHARE(CLONE_NEWTIME); + + SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0", CLOCK_MONOTONIC, OFFSET_S); + + SAFE_CLOCK_GETTIME(CLOCK_MONOTONIC, &start); + + sleep_abs = tst_timespec_add_us(start, 1000000 * OFFSET_S + SLEEP_US); + + if (!SAFE_FORK()) { + clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &sleep_abs, NULL); + exit(0); + } + + SAFE_WAIT(NULL); + + SAFE_CLOCK_GETTIME(CLOCK_MONOTONIC, &end); + + long long diff = tst_timespec_diff_us(end, start); + + if (diff > 5 * SLEEP_US) { + tst_res(TFAIL, "clock_nanosleep() slept too long %lli", diff); + return; + } + + if (diff < SLEEP_US) { + tst_res(TFAIL, "clock_nanosleep() slept too short %lli", diff); + return; + } + + tst_res(TPASS, "clock_nanosleep() slept correctly %lli", diff); +} + +static struct tst_test test = { + .test_all = verify_clock_nanosleep, + .needs_root = 1, + .forks_child = 1, + .needs_kconfigs = (const char *[]) { + "CONFIG_TIME_NS=y" + } + +}; From patchwork Thu Mar 5 13:48:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 1249590 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=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (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 48YBvJ5s5yz9sPK for ; Fri, 6 Mar 2020 00:49:48 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 0D7E13C6570 for ; Thu, 5 Mar 2020 14:49:46 +0100 (CET) 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 [IPv6:2001:4b78:1:20::7]) by picard.linux.it (Postfix) with ESMTP id 8D8DE3C6550 for ; Thu, 5 Mar 2020 14:48:41 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-7.smtp.seeweb.it (Postfix) with ESMTPS id E82342016A9 for ; Thu, 5 Mar 2020 14:48:40 +0100 (CET) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 5D416AB6D for ; Thu, 5 Mar 2020 13:48:40 +0000 (UTC) From: Cyril Hrubis To: ltp@lists.linux.it Date: Thu, 5 Mar 2020 14:48:32 +0100 Message-Id: <20200305134834.16736-7-chrubis@suse.cz> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200305134834.16736-1-chrubis@suse.cz> References: <20200305134834.16736-1-chrubis@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-7.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.2 required=7.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-7.smtp.seeweb.it Subject: [LTP] [PATCH 6/8] syscalls/clock_gettime03: Add basic time namespace test 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" Signed-off-by: Cyril Hrubis --- runtest/containers | 1 + runtest/syscalls | 1 + .../kernel/syscalls/clock_gettime/.gitignore | 1 + .../syscalls/clock_gettime/clock_gettime03.c | 113 ++++++++++++++++++ 4 files changed, 116 insertions(+) create mode 100644 testcases/kernel/syscalls/clock_gettime/clock_gettime03.c diff --git a/runtest/containers b/runtest/containers index 8100cd2bc..1006d8d35 100644 --- a/runtest/containers +++ b/runtest/containers @@ -89,3 +89,4 @@ userns07 userns07 # time namespaces sysinfo03 sysinfo03 clock_nanosleep03 clock_nanosleep03 +clock_gettime03 clock_gettime03 diff --git a/runtest/syscalls b/runtest/syscalls index d19ae0041..778f722a3 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -91,6 +91,7 @@ clock_nanosleep03 clock_nanosleep03 clock_gettime01 clock_gettime01 clock_gettime02 clock_gettime02 +clock_gettime03 clock_gettime03 leapsec01 leapsec01 clock_settime01 clock_settime01 diff --git a/testcases/kernel/syscalls/clock_gettime/.gitignore b/testcases/kernel/syscalls/clock_gettime/.gitignore index ba471c859..9d06613b6 100644 --- a/testcases/kernel/syscalls/clock_gettime/.gitignore +++ b/testcases/kernel/syscalls/clock_gettime/.gitignore @@ -1,3 +1,4 @@ clock_gettime01 clock_gettime02 +clock_gettime03 leapsec01 diff --git a/testcases/kernel/syscalls/clock_gettime/clock_gettime03.c b/testcases/kernel/syscalls/clock_gettime/clock_gettime03.c new file mode 100644 index 000000000..533b3898e --- /dev/null +++ b/testcases/kernel/syscalls/clock_gettime/clock_gettime03.c @@ -0,0 +1,113 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + + Copyright (c) 2020 Cyril Hrubis + + */ +/* + + Basic test for timer namespaces. + + After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the + process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC + and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'. + + The child processes also switch to the initial parent namespace and checks + that the offset is set to 0. + + */ + +#define _GNU_SOURCE +#include "lapi/setns.h" +#include "tst_safe_clocks.h" +#include "tst_timer.h" +#include "lapi/namespaces_constants.h" +#include "tst_test.h" + +static struct tcase { + int clk_id; + int clk_off; + int off; +} tcases[] = { + {CLOCK_MONOTONIC, CLOCK_MONOTONIC, 10}, + {CLOCK_BOOTTIME, CLOCK_BOOTTIME, 10}, + + {CLOCK_MONOTONIC, CLOCK_MONOTONIC, -10}, + {CLOCK_BOOTTIME, CLOCK_BOOTTIME, -10}, + + {CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC, 100}, + {CLOCK_MONOTONIC_COARSE, CLOCK_MONOTONIC, 100}, +}; + +static struct timespec now; +static int parent_ns; + +static void child(struct tcase *tc) +{ + struct timespec then; + struct timespec parent_then; + long long diff; + + SAFE_CLOCK_GETTIME(tc->clk_id, &then); + + setns(parent_ns, CLONE_NEWTIME); + + SAFE_CLOCK_GETTIME(tc->clk_id, &parent_then); + + diff = tst_timespec_diff_ms(then, now); + + if (diff/1000 != tc->off) { + tst_res(TFAIL, "Wrong offset (%s) read %llims", + tst_clock_name(tc->clk_id), diff); + } else { + tst_res(TPASS, "Offset (%s) is correct %llims", + tst_clock_name(tc->clk_id), diff); + } + + diff = tst_timespec_diff_ms(parent_then, now); + + if (diff/1000) { + tst_res(TFAIL, "Wrong offset (%s) read %llims", + tst_clock_name(tc->clk_id), diff); + } else { + tst_res(TPASS, "Offset (%s) is correct %llims", + tst_clock_name(tc->clk_id), diff); + } +} + +static void verify_ns_clock(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + + SAFE_UNSHARE(CLONE_NEWTIME); + + SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0", + tc->clk_off, tc->off); + + SAFE_CLOCK_GETTIME(tc->clk_id, &now); + + if (!SAFE_FORK()) + child(tc); +} + +static void setup(void) +{ + parent_ns = SAFE_OPEN("/proc/self/ns/time_for_children", O_RDONLY); +} + +static void cleanup(void) +{ + SAFE_CLOSE(parent_ns); +} + +static struct tst_test test = { + .setup = setup, + .cleanup = cleanup, + .tcnt = ARRAY_SIZE(tcases), + .test = verify_ns_clock, + .needs_root = 1, + .forks_child = 1, + .needs_kconfigs = (const char *[]) { + "CONFIG_TIME_NS=y" + } +}; From patchwork Thu Mar 5 13:48:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 1249591 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=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (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 48YBvW13C4z9sRR for ; Fri, 6 Mar 2020 00:49:58 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id 1EE103C6559 for ; Thu, 5 Mar 2020 14:49:56 +0100 (CET) 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]) by picard.linux.it (Postfix) with ESMTP id 300593C655D for ; Thu, 5 Mar 2020 14:48:42 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-3.smtp.seeweb.it (Postfix) with ESMTPS id 779D11A02405 for ; Thu, 5 Mar 2020 14:48:41 +0100 (CET) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 1C781ACF0 for ; Thu, 5 Mar 2020 13:48:41 +0000 (UTC) From: Cyril Hrubis To: ltp@lists.linux.it Date: Thu, 5 Mar 2020 14:48:33 +0100 Message-Id: <20200305134834.16736-8-chrubis@suse.cz> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200305134834.16736-1-chrubis@suse.cz> References: <20200305134834.16736-1-chrubis@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-3.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.2 required=7.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-3.smtp.seeweb.it Subject: [LTP] [PATCH 7/8] containers/timens: Add basic error test 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" Add basic error handling test for the /proc/$PID/timens_offsets file. Signed-off-by: Cyril Hrubis --- runtest/containers | 1 + testcases/kernel/containers/timens/.gitignore | 1 + testcases/kernel/containers/timens/Makefile | 6 ++ testcases/kernel/containers/timens/timens01.c | 75 +++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 testcases/kernel/containers/timens/.gitignore create mode 100644 testcases/kernel/containers/timens/Makefile create mode 100644 testcases/kernel/containers/timens/timens01.c diff --git a/runtest/containers b/runtest/containers index 1006d8d35..23e4a533d 100644 --- a/runtest/containers +++ b/runtest/containers @@ -90,3 +90,4 @@ userns07 userns07 sysinfo03 sysinfo03 clock_nanosleep03 clock_nanosleep03 clock_gettime03 clock_gettime03 +timens01 timens01 diff --git a/testcases/kernel/containers/timens/.gitignore b/testcases/kernel/containers/timens/.gitignore new file mode 100644 index 000000000..bcd2dd9dd --- /dev/null +++ b/testcases/kernel/containers/timens/.gitignore @@ -0,0 +1 @@ +timens01 diff --git a/testcases/kernel/containers/timens/Makefile b/testcases/kernel/containers/timens/Makefile new file mode 100644 index 000000000..5ea7d67db --- /dev/null +++ b/testcases/kernel/containers/timens/Makefile @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +top_srcdir ?= ../../../.. + +include $(top_srcdir)/include/mk/testcases.mk +include $(top_srcdir)/include/mk/generic_leaf_target.mk diff --git a/testcases/kernel/containers/timens/timens01.c b/testcases/kernel/containers/timens/timens01.c new file mode 100644 index 000000000..08bc449f9 --- /dev/null +++ b/testcases/kernel/containers/timens/timens01.c @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + + Copyright (c) 2020 Cyril Hrubis + + */ +/* + + Basic test for timens_offsets error handling. + + After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the + process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC + and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'. + + */ + +#define _GNU_SOURCE +#include "lapi/setns.h" +#include "lapi/namespaces_constants.h" +#include "lapi/posix_clocks.h" +#include "tst_test.h" + +static struct tcase { + const char *offsets; + int exp_err; +} tcases[] = { + /* obvious garbage */ + {"not an offset", EINVAL}, + /* missing nanoseconds */ + {"1 10", EINVAL}, + /* negative nanoseconds */ + {"1 10 -10", EINVAL}, + /* nanoseconds > 1s */ + {"1 10 1000000001", EINVAL}, + /* unsupported CLOCK_REALTIME */ + {"0 10 0", EINVAL}, + /* mess on the second line */ + {"1 10 0\na", EINVAL}, + /* overflow kernel 64bit nanosecond timer */ + {"1 9223372036 0", ERANGE}, + {"1 -9223372036 0", ERANGE}, +}; + +static void verify_ns_clock(unsigned int n) +{ + struct tcase *tc = &tcases[n]; + int fd, ret; + + SAFE_UNSHARE(CLONE_NEWTIME); + + fd = SAFE_OPEN("/proc/self/timens_offsets", O_WRONLY); + ret = write(fd, tc->offsets, strlen(tc->offsets)); + + if (ret != -1) { + tst_res(TFAIL, "Write returned %i", ret); + return; + } + + if (errno != tc->exp_err) { + tst_res(TFAIL | TERRNO, "Write should fail with %s, got:", + tst_strerrno(tc->exp_err)); + return; + } + + tst_res(TPASS | TERRNO, "Write offsets='%s'", tc->offsets); +} + +static struct tst_test test = { + .tcnt = ARRAY_SIZE(tcases), + .test = verify_ns_clock, + .needs_root = 1, + .needs_kconfigs = (const char *[]) { + "CONFIG_TIME_NS=y" + } +}; From patchwork Thu Mar 5 13:48:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Hrubis X-Patchwork-Id: 1249593 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=2001:1418:10:5::2; helo=picard.linux.it; envelope-from=ltp-bounces+incoming=patchwork.ozlabs.org@lists.linux.it; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=suse.cz Received: from picard.linux.it (picard.linux.it [IPv6:2001:1418:10:5::2]) (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 48YBvh4Wknz9sPK for ; Fri, 6 Mar 2020 00:50:08 +1100 (AEDT) Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id BF1CF3C653C for ; Thu, 5 Mar 2020 14:50:05 +0100 (CET) X-Original-To: ltp@lists.linux.it Delivered-To: ltp@picard.linux.it Received: from in-4.smtp.seeweb.it (in-4.smtp.seeweb.it [IPv6:2001:4b78:1:20::4]) by picard.linux.it (Postfix) with ESMTP id AC2CB3C654C for ; Thu, 5 Mar 2020 14:48:42 +0100 (CET) Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by in-4.smtp.seeweb.it (Postfix) with ESMTPS id 426231000C56 for ; Thu, 5 Mar 2020 14:48:42 +0100 (CET) Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id DA1D4AB6D for ; Thu, 5 Mar 2020 13:48:41 +0000 (UTC) From: Cyril Hrubis To: ltp@lists.linux.it Date: Thu, 5 Mar 2020 14:48:34 +0100 Message-Id: <20200305134834.16736-9-chrubis@suse.cz> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20200305134834.16736-1-chrubis@suse.cz> References: <20200305134834.16736-1-chrubis@suse.cz> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.99.2 at in-4.smtp.seeweb.it X-Virus-Status: Clean X-Spam-Status: No, score=0.2 required=7.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, SPF_HELO_NONE,SPF_PASS autolearn=disabled version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on in-4.smtp.seeweb.it Subject: [LTP] [PATCH 8/8] syscalls/timerfd04: Add time namespace test 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" Signed-off-by: Cyril Hrubis --- runtest/containers | 1 + runtest/syscalls | 1 + testcases/kernel/syscalls/timerfd/.gitignore | 1 + testcases/kernel/syscalls/timerfd/timerfd04.c | 95 +++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 testcases/kernel/syscalls/timerfd/timerfd04.c diff --git a/runtest/containers b/runtest/containers index 23e4a533d..276096709 100644 --- a/runtest/containers +++ b/runtest/containers @@ -91,3 +91,4 @@ sysinfo03 sysinfo03 clock_nanosleep03 clock_nanosleep03 clock_gettime03 clock_gettime03 timens01 timens01 +timerfd04 timerfd04 diff --git a/runtest/syscalls b/runtest/syscalls index 778f722a3..8494a8b04 100644 --- a/runtest/syscalls +++ b/runtest/syscalls @@ -1473,6 +1473,7 @@ times03 times03 timerfd01 timerfd01 timerfd02 timerfd02 timerfd03 timerfd03 +timerfd04 timerfd04 timerfd_create01 timerfd_create01 timerfd_gettime01 timerfd_gettime01 timerfd_settime01 timerfd_settime01 diff --git a/testcases/kernel/syscalls/timerfd/.gitignore b/testcases/kernel/syscalls/timerfd/.gitignore index 1c5329966..ef388685d 100644 --- a/testcases/kernel/syscalls/timerfd/.gitignore +++ b/testcases/kernel/syscalls/timerfd/.gitignore @@ -1,6 +1,7 @@ /timerfd01 /timerfd02 /timerfd03 +/timerfd04 /timerfd_create01 /timerfd_gettime01 /timerfd_settime01 diff --git a/testcases/kernel/syscalls/timerfd/timerfd04.c b/testcases/kernel/syscalls/timerfd/timerfd04.c new file mode 100644 index 000000000..3b8bf761d --- /dev/null +++ b/testcases/kernel/syscalls/timerfd/timerfd04.c @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + + Copyright (c) 2020 Cyril Hrubis + + */ +/* + + Test that timerfd adds correctly an offset with absolute expiration time. + + After a call to unshare(CLONE_NEWTIME) a new timer namespace is created, the + process that has called the unshare() can adjust offsets for CLOCK_MONOTONIC + and CLOCK_BOOTTIME for its children by writing to the '/proc/self/timens_offsets'. + + */ + +#include +#include "tst_safe_clocks.h" +#include "tst_safe_timerfd.h" +#include "tst_timer.h" +#include "lapi/namespaces_constants.h" +#include "tst_test.h" + +#define SLEEP_US 40000 + +static struct tcase { + int clk_id; + int clk_off; + int off; +} tcases[] = { + {CLOCK_MONOTONIC, CLOCK_MONOTONIC, 10}, + {CLOCK_BOOTTIME, CLOCK_BOOTTIME, 10}, + + {CLOCK_MONOTONIC, CLOCK_MONOTONIC, -10}, + {CLOCK_BOOTTIME, CLOCK_BOOTTIME, -10}, +}; + +static void verify_timerfd(unsigned int n) +{ + struct timespec start, end; + struct itimerspec it = {}; + struct tcase *tc = &tcases[n]; + + SAFE_UNSHARE(CLONE_NEWTIME); + + SAFE_FILE_PRINTF("/proc/self/timens_offsets", "%d %d 0", + tc->clk_off, tc->off); + + SAFE_CLOCK_GETTIME(tc->clk_id, &start); + + it.it_value = tst_timespec_add_us(start, 1000000 * tc->off + SLEEP_US); + + if (!SAFE_FORK()) { + uint64_t exp; + int fd = SAFE_TIMERFD_CREATE(tc->clk_id, 0); + + SAFE_TIMERFD_SETTIME(fd, TFD_TIMER_ABSTIME, &it, NULL); + + SAFE_READ(1, fd, &exp, sizeof(exp)); + + if (exp != 1) + tst_res(TFAIL, "Got %llu expirations", (long long unsigned)exp); + + SAFE_CLOSE(fd); + exit(0); + } + + SAFE_WAIT(NULL); + + SAFE_CLOCK_GETTIME(CLOCK_MONOTONIC, &end); + + long long diff = tst_timespec_diff_us(end, start); + + if (diff > 5 * SLEEP_US) { + tst_res(TFAIL, "timerfd %s slept too long %lli", + tst_clock_name(tc->clk_id), diff); + return; + } + + if (diff < SLEEP_US) { + tst_res(TFAIL, "timerfd %s slept too short %lli", + tst_clock_name(tc->clk_id), diff); + return; + } + + tst_res(TPASS, "timerfd %s slept correctly %lli", + tst_clock_name(tc->clk_id), diff); +} + +static struct tst_test test = { + .tcnt = ARRAY_SIZE(tcases), + .test = verify_timerfd, + .needs_root = 1, + .forks_child = 1, +};