Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp5664517ima; Tue, 5 Feb 2019 16:13:51 -0800 (PST) X-Google-Smtp-Source: AHgI3IYripivlZl8Ba6kkl7ZZAIXemupqp9dPH7PMaXS5PzUV9WqRqL4DCz3m6YEbWu6viWxFzPV X-Received: by 2002:a63:d005:: with SMTP id z5mr7002543pgf.64.1549412031756; Tue, 05 Feb 2019 16:13:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549412031; cv=none; d=google.com; s=arc-20160816; b=kgvH/e6LexXmpOJ3rJ87BPKBeg0QflXC5+xPCoqFDpkUD+QRS8/PLGPmuIRLddCDrU wtUpCusUCE1OGGvMC7CM8c8Ck17UJD3ZMajItToxiELm2BydIcH3dZ30PV/k1/dIah6k wMS78ulS+cwH8fX6ipDhnoZQKGo3NaWd8Z+XKvceg2jKfXO69Cave59eOLmiQrEJkj5b lgrwvHeCz84IgQJTX12IP9vuOSDU/PzfD58Ndb9RYyj+Yf+pRAzk2mLtRXGjvcOoIaUy 6kYncr9r1o9Nl6nXY4LEr3rTgRrEUkFVctXAYh7jQZJ3jjE6xVmFWMmzmdAb/z7pDUU9 JJQQ== 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=WNAadzlGShFp88tN5oZN8oGMjlN0KV8n9+OjpGdbo7U=; b=CgxUrUyoVQk8Q2DchFYh+lmHiAI6et91A0EzK70Ad6H+zLnU0qzKScy5N4Etvx7029 9FV9/M96jh1ygXeUIh42Aj1hOBPrEXPEg/ChBjz+t4WTKK4i9bBQVVSHTz3nUra+X5Cd 1Xs59qhllNl6iBqiF0gy8nFmkP8DTIgD4Wn/Uxarf/QGFZqQFGUQrat/pbUFm+Jy0pUO IZ6nYAOi/1yNABK43FXwPU0HkFlyNMkgCWJ6sulIWu6oFKNdq5ewKFV7OjjlHuMcHzGq dFQXyHBe3cBsNtVDAF/1qRCxfRcNprP1gIy9JSSfCe6GkJ7U3J0FNHt+7sWCgxAAeuFq zCyg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=isj724TP; 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 1si4771475plv.138.2019.02.05.16.13.36; Tue, 05 Feb 2019 16:13:51 -0800 (PST) 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=isj724TP; 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 S1729428AbfBFALr (ORCPT + 99 others); Tue, 5 Feb 2019 19:11:47 -0500 Received: from mail-ed1-f66.google.com ([209.85.208.66]:36776 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729356AbfBFALp (ORCPT ); Tue, 5 Feb 2019 19:11:45 -0500 Received: by mail-ed1-f66.google.com with SMTP id d12so3445559edy.3 for ; Tue, 05 Feb 2019 16:11:43 -0800 (PST) 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=WNAadzlGShFp88tN5oZN8oGMjlN0KV8n9+OjpGdbo7U=; b=isj724TPBuwVcGNGGsL6SA9D84/N7TBxjk5m2YYcRtblCx/ZL96WaKbvF4h+sg5Z7e JUYWDhwtk/HAeYFTWwoP90OGvNsazFHZzEghdEjqiFnnRAwLHLnzvDPdxjk1dV29t959 OBO4pxKVQCYUzee1tBrgr8vEQQkqlf9giiY32cTxUIU3wAt81M4O4GgYOft6ZdnEJX4N b9cyfWxCmIXmcOpcoRMswFaxGCgZjeSv8mpPNS8/VcnOigk0jV/iFum7Xk0XWSgL2vt1 WjKKyc41uUA3yQOYbIKbIwYgdXZIX3AA1bkKVyJD4p6C02OIufQ/k2czvcrJEVbtyY4c UBEw== 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=WNAadzlGShFp88tN5oZN8oGMjlN0KV8n9+OjpGdbo7U=; b=fSgJdh103OhHDbWzfsvH/EicYOwZFsTsWku8lEVnVgjto9n2A6hjbtHd/oFTOnav+l kzMpdAcUMaaXoQKDxpP5NsuVGvv6nL+CQ7YW19QsjTJY6btSntjAyjdpDSS1MYmevLNs i8EnZ+yA6efggRmLGa90Kaeh87LmdHIdyiNNbMiFQIqX396qgSIrg0DxMwlS5UXtM+UU zodb4FdwSCFNB3Uq5Q3JYS0P1w/cOzEbhbhVkrt6X8OrOO5OzYX9z1C2EPYgC3D5DWGh z49FF3dzX4rMvlKuZ58IV2xA/cegbD+tlb+tSvoF/AwFxgAAjOYC9wZHmStTmfV5exBr 1lPw== X-Gm-Message-State: AHQUAuahSPD9i/GCAkBPFkFN4xNHG3Z27MFPG/wzfWef78Ts7CLJVkhZ DVfmyjq8B3iK59g2V4jr4keckv1bY+Y= X-Received: by 2002:aa7:c88e:: with SMTP id p14mr3990134eds.51.1549411903060; Tue, 05 Feb 2019 16:11:43 -0800 (PST) Received: from Mindolluin.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id p30sm5489594eda.68.2019.02.05.16.11.41 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 05 Feb 2019 16:11:42 -0800 (PST) From: Dmitry Safonov To: linux-kernel@vger.kernel.org Cc: Andrei Vagin , Dmitry Safonov , Adrian Reber , Andrei Vagin , Andy Lutomirski , Andy Tucker , Arnd Bergmann , Christian Brauner , Cyrill Gorcunov , Dmitry Safonov <0x7f454c46@gmail.com>, "Eric W. Biederman" , "H. Peter Anvin" , Ingo Molnar , Jeff Dike , Oleg Nesterov , Pavel Emelyanov , Shuah Khan , Thomas Gleixner , containers@lists.linux-foundation.org, criu@openvz.org, linux-api@vger.kernel.org, x86@kernel.org Subject: [PATCH 25/32] selftest/timens: Add a test for timerfd Date: Wed, 6 Feb 2019 00:10:59 +0000 Message-Id: <20190206001107.16488-26-dima@arista.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190206001107.16488-1-dima@arista.com> References: <20190206001107.16488-1-dima@arista.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Dmitry Safonov --- tools/testing/selftests/timens/.gitignore | 1 + tools/testing/selftests/timens/Makefile | 2 +- tools/testing/selftests/timens/timerfd.c | 119 ++++++++++++++++++++++ 3 files changed, 121 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..8ec2604d26c9 --- /dev/null +++ b/tools/testing/selftests/timens/timerfd.c @@ -0,0 +1,119 @@ +// 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" + +int run_test(int clockid, struct timespec now) +{ + struct itimerspec new_value; + long long elapsed; + int fd, i; + + if (clock_gettime(clockid, &now)) + return pr_perror("clock_gettime"); + + 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"); + + if (i == 1) + flags |= TFD_TIMER_ABSTIME; + + if (timerfd_settime(fd, flags, &new_value, NULL)) + return pr_perror("timerfd_settime"); + + if (timerfd_gettime(fd, &new_value)) + return pr_perror("timerfd_gettime"); + + 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(); + + 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); + + 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.20.1