Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4139869ybi; Mon, 29 Jul 2019 20:07:49 -0700 (PDT) X-Google-Smtp-Source: APXvYqz4RuK9SxxuyYQsqJxDW+rHopMHthlsKH0iR8yDDPbaF2HTEjvU+aH8QPBLs7IkhoYEtSsY X-Received: by 2002:a17:902:4283:: with SMTP id h3mr109776272pld.15.1564456069370; Mon, 29 Jul 2019 20:07:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564456069; cv=none; d=google.com; s=arc-20160816; b=Ekxsd95QTfWVumFGjX1ci+933/GQy2GpooM7oxaKWCrZ/DFx54kGaFd9A4qkKgoUUb 84M01BwxCQ01XC9H9TEJFo22m4GA+nsRIRtZAXHlDF06FheSwRqI01+EF2pQaSVnhlWx 3bAWrJyUKePs1auyR6rKHXItuaOPonCClQkelumruz5pngsUmEaCqZcxNrcvoIecNDAH ++Kk2kCGbsss6bMner6T2GqnSQxC4dvdcA9GBi5x+I/N9i9rePxTxV7Uw8teQUv6MkQh oHFlm7bISQPr1VBLRtx1liYG9nRdlUx1I3kwYlqcagn4BsR/MwDiri9LxKm/fK5ZiD+f buhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kW0VPhYCrDSswd0PPA3RvCOD8M4i/u3OaqhqtDVaSgw=; b=0FJfIExH4p48BKx5LGD/7qy8yUxnZ+KirmOi7CRvMmsqCNAemEADRP2GZty6/d7opA fec4adcVEdv1uCVd0XDCEW5YCMVEZqBU9f77F0Y/ILLtS+hM2lzeQPnk/mcQwlS2VsjG LCWY+8UcrW3whzI4JCzVkJp+2CULsyLtelBUUA8Mius3Tw/YwUsryz5/PonJiububf/E r5jTyYItOQuqXi0BvBJMDo63BRFkrLAHhHoNP9vO16bwTNYPe36kkl98ZOVM8L8843mp PGoxOCzI4zhMAAoO564MlL7ZkUZ0WeO9Kg98UvpE7jHjZK0SvMtLmllUuuTvwZBgY7K/ J2xA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=SACzK5Xq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=arista.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a23si28317837pls.189.2019.07.29.20.07.34; Mon, 29 Jul 2019 20:07:49 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=SACzK5Xq; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=REJECT dis=NONE) header.from=arista.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729037AbfG2WAd (ORCPT + 99 others); Mon, 29 Jul 2019 18:00:33 -0400 Received: from mail-wm1-f67.google.com ([209.85.128.67]:54261 "EHLO mail-wm1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730551AbfG2V6r (ORCPT ); Mon, 29 Jul 2019 17:58:47 -0400 Received: by mail-wm1-f67.google.com with SMTP id x15so55203634wmj.3 for ; Mon, 29 Jul 2019 14:58:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arista.com; s=googlenew; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=kW0VPhYCrDSswd0PPA3RvCOD8M4i/u3OaqhqtDVaSgw=; b=SACzK5XqeM8TF6GrRbw7c2yX1YrOhwrjp164LAluLWqd4S6lLUfO8QcCUlMoQZwHKb /z8GFLL+CoRlMh2yb634ud+tVfZx2sw9BvFQfUvb/dL3eXT0m/WRhaeeO3+fqIy4q1Fc UG/vUA2cF9wSk5r5ZLZPBYEtQiEjkO7EuIZqm7cexpSDFoQJPUvRtUpZfozGJk9gh1i+ DM25kMBHoUxARXrx6LmZyNOUPkKCEObHVDTwDxvyL1EzlZwbb7w8S0IHh7RZkNs6pIDR eeX2iq+oMJUcuYJySfYksfx1ohmym41vvjzUiswYr0UdyFrB1X6JPp0FpPQd+9OexBX9 V9ng== 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=kW0VPhYCrDSswd0PPA3RvCOD8M4i/u3OaqhqtDVaSgw=; b=luf1OhhtvNdtqpxdFdVkI70/zrCU6yqPjAYvSIdJ80b6IuM/08YQuw1KvnRdn4rItz mkU1aI9YQP50OMGNiiVONEwyZ35AKqy0YF56MmrFw149a0xpxUL1uWwX6w14ToHlPcCR dbwkOJvub1Qwz6MAXTV39Qp6XMHF9UJYGLQsRRJCoAPRqbMTuwgPiO/2GvBgbCWDbyXL 1Uujf6u8cigS1rRWXxLMaKkw8b4X7zEeceBAIrB5134D8JqjWjF6+Cb/UymabKwEmnlT eWykkP8qC9UcwnW+nRDp9ypeEhJShjgLZAQqhfUPp89B9xCc8P076+n0qIq02eqolH4a Loog== X-Gm-Message-State: APjAAAUzxBpGJNDVnM1jjuynHDS2iifGojP3KGup3fETRkRoboaONHgb b6JUHjek8n8MQYv9ZBcabbklf2AV2jQRfVGQx4VQc/jnwIM+Ao3NSRjtpAvdJJY82BJk0SfXp51 G86noH+Z8WGiccLWGGYX2Ss7SO2ISNtEp9N+UEXrCU7uDOSZxDf3p/nmxJqY8wCl9a4OLOiqmhr xsnEd+H/mSR4DgmjuHqMHp9cQOXk6Fll5BFb9o4gI= X-Received: by 2002:a1c:c545:: with SMTP id v66mr103550930wmf.51.1564437524406; Mon, 29 Jul 2019 14:58:44 -0700 (PDT) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id x20sm49230728wmc.1.2019.07.29.14.58.43 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 29 Jul 2019 14:58:43 -0700 (PDT) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Dmitry Safonov <0x7f454c46@gmail.com>, Andrei Vagin , Dmitry Safonov , Adrian Reber , Andrei Vagin , Andy Lutomirski , Arnd Bergmann , Christian Brauner , Cyrill Gorcunov , "Eric W. Biederman" , "H. Peter Anvin" , Ingo Molnar , Jann Horn , Jeff Dike , Oleg Nesterov , Pavel Emelyanov , Shuah Khan , Thomas Gleixner , Vincenzo Frascino , containers@lists.linux-foundation.org, criu@openvz.org, linux-api@vger.kernel.org, x86@kernel.org Subject: [PATCHv5 32/37] selftest/timens: Add a test for timerfd Date: Mon, 29 Jul 2019 22:57:14 +0100 Message-Id: <20190729215758.28405-33-dima@arista.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729215758.28405-1-dima@arista.com> References: <20190729215758.28405-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CLOUD-SEC-AV-Info: arista,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrei Vagin Check that timerfd_create() takes into account clock offsets. Signed-off-by: Andrei Vagin Co-developed-by: Dmitry Safonov Signed-off-by: Dmitry Safonov --- tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 2 +- tools/testing/selftests/timens/timerfd.c | 129 ++++++++++++++++++++++ 3 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/timerfd.c diff --git a/tools/testing/selftests/timens/.gitignore b/tools/testing/selftests/timens/.gitignore index 27a693229ce1..b609f6ee9fb9 100644 --- a/tools/testing/selftests/timens/.gitignore +++ b/tools/testing/selftests/timens/.gitignore @@ -1 +1,2 @@ timens +timerfd diff --git a/tools/testing/selftests/timens/Makefile b/tools/testing/selftests/timens/Makefile index b877efb78974..66b90cd28e5c 100644 --- a/tools/testing/selftests/timens/Makefile +++ b/tools/testing/selftests/timens/Makefile @@ -1,4 +1,4 @@ -TEST_GEN_PROGS := timens +TEST_GEN_PROGS := timens timerfd CFLAGS := -Wall -Werror diff --git a/tools/testing/selftests/timens/timerfd.c b/tools/testing/selftests/timens/timerfd.c new file mode 100644 index 000000000000..b87076aae040 --- /dev/null +++ b/tools/testing/selftests/timens/timerfd.c @@ -0,0 +1,129 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "timens.h" + +static int tclock_gettime(clock_t clockid, struct timespec *now) +{ + if (clockid == CLOCK_BOOTTIME_ALARM) + clockid = CLOCK_BOOTTIME; + return clock_gettime(clockid, now); +} + +int run_test(int clockid, struct timespec now) +{ + struct itimerspec new_value; + long long elapsed; + int fd, i; + + if (tclock_gettime(clockid, &now)) + return pr_perror("clock_gettime(%d)", clockid); + + for (i = 0; i < 2; i++) { + int flags = 0; + + new_value.it_value.tv_sec = 3600; + new_value.it_value.tv_nsec = 0; + new_value.it_interval.tv_sec = 1; + new_value.it_interval.tv_nsec = 0; + + if (i == 1) { + new_value.it_value.tv_sec += now.tv_sec; + new_value.it_value.tv_nsec += now.tv_nsec; + } + + fd = timerfd_create(clockid, 0); + if (fd == -1) + return pr_perror("timerfd_create(%d)", clockid); + + if (i == 1) + flags |= TFD_TIMER_ABSTIME; + + if (timerfd_settime(fd, flags, &new_value, NULL)) + return pr_perror("timerfd_settime(%d)", clockid); + + if (timerfd_gettime(fd, &new_value)) + return pr_perror("timerfd_gettime(%d)", clockid); + + elapsed = new_value.it_value.tv_sec; + if (abs(elapsed - 3600) > 60) { + ksft_test_result_fail("clockid: %d elapsed: %lld\n", + clockid, elapsed); + return 1; + } + + close(fd); + } + + ksft_test_result_pass("clockid=%d\n", clockid); + + return 0; +} + +int main(int argc, char *argv[]) +{ + int ret, status, len, fd; + char buf[4096]; + pid_t pid; + struct timespec btime_now, mtime_now; + + nscheck(); + + ksft_set_plan(3); + + clock_gettime(CLOCK_MONOTONIC, &mtime_now); + clock_gettime(CLOCK_BOOTTIME, &btime_now); + + if (unshare(CLONE_NEWTIME)) + return pr_perror("unshare"); + + len = snprintf(buf, sizeof(buf), "%d %d 0\n%d %d 0", + CLOCK_MONOTONIC, 70 * 24 * 3600, + CLOCK_BOOTTIME, 9 * 24 * 3600); + fd = open("/proc/self/timens_offsets", O_WRONLY); + if (fd < 0) + return pr_perror("/proc/self/timens_offsets"); + + if (write(fd, buf, len) != len) + return pr_perror("/proc/self/timens_offsets"); + + close(fd); + mtime_now.tv_sec += 70 * 24 * 3600; + btime_now.tv_sec += 9 * 24 * 3600; + + pid = fork(); + if (pid < 0) + return pr_perror("Unable to fork"); + if (pid == 0) { + ret = 0; + ret |= run_test(CLOCK_BOOTTIME, btime_now); + ret |= run_test(CLOCK_MONOTONIC, mtime_now); + ret |= run_test(CLOCK_BOOTTIME_ALARM, btime_now); + + if (ret) + ksft_exit_fail(); + ksft_exit_pass(); + return ret; + } + + if (waitpid(pid, &status, 0) != pid) + return pr_perror("Unable to wait the child process"); + + if (WIFEXITED(status)) + return WEXITSTATUS(status); + + return 1; +} + -- 2.22.0