Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp5663437ima; Tue, 5 Feb 2019 16:12:34 -0800 (PST) X-Google-Smtp-Source: AHgI3IaIOtXmJEHoeq2mm0Ge5T7CSxGAGxbSUzNrqdjmdEnOD8KeyUQhvSj+gcwAi2/gxEtuqFzX X-Received: by 2002:a62:931a:: with SMTP id b26mr7958946pfe.65.1549411953972; Tue, 05 Feb 2019 16:12:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549411953; cv=none; d=google.com; s=arc-20160816; b=PAWuL2BmOomDeeFSaE2XM6hCGPG3tr1548BmfXgxEgFqOkkUtoO6ClhhCjX9CLpREb LA2TrbBnIzx+LiYMmy2/05NscObOsHSVN7ABNJ/zklZ5CD1oM5q/quLyaTUdOrhsKXgz HN3zpMvv6g4fAtpGsyOmmdkchFY/BpCxSFkFBA2LKIR8NlrFvaBlI2KQiRlPK5lqmoMB j1K1LTeTPIvb0yv53k6cDcfrl00tJ+bqK6QkMoN9mOR1hH09BHpY+XXDamiDbPFitfxk Zcc5jADgb/rRmwqDBrsLmcPJrNCYAbPQl9T+vPcd8dvHZA0KHRiJXgsQF/wH25MQOdSx 9r9Q== 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=Fl8AOsvztsHa751NXA7/6V+IVjg1Wd0DoDDENkF65DM=; b=tQrzi8CiHib+ObxE9YkV59gw3UUZ8XbgWc7lxZdP50BbXLb1XPzjxhhUnuS0u5KJch n0ShlJ776uvk55MaCK+AOFTkIMOsaXzYlWzkMm8B8plSoET8tXijAG1iU1uPt6tAV6dM R2ZSWaiAtdcr3QQm2f5LC/O/yEVZWyiZk0nxlir5o8IcdD3IX71LCOqPDVrcg6O2BxM9 FZjbATWY1L+//TrZ86LEMKq2e1iQZF01qD4nUxL4y5kBrgA6TUQ3o79C2Cpuhc7KRpb1 Q3mtQePx4TjtQf8n51RMYFZbb0JL4U0EdOSX0ZpCS1tNhuDSASRylH40dZRFDtuGMwRn FXEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=fFrBudei; 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 e21si3966581pgv.271.2019.02.05.16.12.18; Tue, 05 Feb 2019 16:12:33 -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=fFrBudei; 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 S1729614AbfBFALx (ORCPT + 99 others); Tue, 5 Feb 2019 19:11:53 -0500 Received: from mail-ed1-f66.google.com ([209.85.208.66]:33872 "EHLO mail-ed1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729393AbfBFALq (ORCPT ); Tue, 5 Feb 2019 19:11:46 -0500 Received: by mail-ed1-f66.google.com with SMTP id b3so4515581ede.1 for ; Tue, 05 Feb 2019 16:11:45 -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=Fl8AOsvztsHa751NXA7/6V+IVjg1Wd0DoDDENkF65DM=; b=fFrBudeiT+jYMw7NT1bddLBFNxD1uUVxy0dHM2b13Pt7BqCV6ZyH8uwF6bDE0V1qNc G3dgSpfZifmN4hAKugcRerszgT4P5xC6+ZT+RSb1LTgzTBDztBfCtlr19uSPoM+cfH5Y 82jf66qwpqXN/y0BAYjLiuSUd8xOByOdLjWvFZ3Krj4v/yta68hmvsOenYuisotR5Txg i9BZLA62Jjvrmb8NALafTwCw5MipWIxrzj2rjP+PoeySn0dmDTlB9kR65yydtVXX95wK 3yB6zgufwQc36Wg3UwUNoEPga4ADi/pqAFWf5lodKjgZZwS0DZa6h3FvQVWadnEAYNLp Q8nw== 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=Fl8AOsvztsHa751NXA7/6V+IVjg1Wd0DoDDENkF65DM=; b=eHplgKje2TgfebmLBlXJO1pB0qXopPTHUDH1OHKPKVM1uTnJngd1GMqmXa5MswMLvg j02ArAxTDp9/5IKrEfSJfo3ghzd51g8ZkuCfSAW3YT9VQii89hZybhL4uJOsqcjTOKlR /H0t3ElhrDfQj+7tJncGEYNzAE73ZqaIM/2xlej9XvO5+NBmU25TmsKzPhqBqrbB/fcI ZP56qpLQw+Hc7EJBrrZIIOvZx/mPA1T9Wn5OG0Fwu2I9eMlxYO9nR2Kx4IpIFZQjDzwM quCiAFNkDRx0afT06+dvQpsqqxrx9kNjMWu2mVbuBP6EYXMr2aZLx0hnx+D5NophhKz4 THRQ== X-Gm-Message-State: AHQUAuaujA9eimf4uZBb37Uzh91Qm9832b9SI1Dq/zg1K/Uk30qVOy8V L7+uqYuJ/nsBOU0PrZtO3x8txAOuBNY= X-Received: by 2002:a50:b5c6:: with SMTP id a64mr6071277ede.112.1549411904434; Tue, 05 Feb 2019 16:11:44 -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.43 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Tue, 05 Feb 2019 16:11:43 -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 26/32] selftest/timens: Add a test for clock_nanosleep() Date: Wed, 6 Feb 2019 00:11:00 +0000 Message-Id: <20190206001107.16488-27-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 clock_nanosleep() 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 +- .../selftests/timens/clock_nanosleep.c | 99 +++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/timens/clock_nanosleep.c diff --git a/tools/testing/selftests/timens/.gitignore b/tools/testing/selftests/timens/.gitignore index b609f6ee9fb9..9b6c8ddac2c8 100644 --- a/tools/testing/selftests/timens/.gitignore +++ b/tools/testing/selftests/timens/.gitignore @@ -1,2 +1,3 @@ +clock_nanosleep timens timerfd diff --git a/tools/testing/selftests/timens/Makefile b/tools/testing/selftests/timens/Makefile index 66b90cd28e5c..76a1dc891184 100644 --- a/tools/testing/selftests/timens/Makefile +++ b/tools/testing/selftests/timens/Makefile @@ -1,4 +1,4 @@ -TEST_GEN_PROGS := timens timerfd +TEST_GEN_PROGS := timens timerfd clock_nanosleep CFLAGS := -Wall -Werror diff --git a/tools/testing/selftests/timens/clock_nanosleep.c b/tools/testing/selftests/timens/clock_nanosleep.c new file mode 100644 index 000000000000..9a1689e5a0e1 --- /dev/null +++ b/tools/testing/selftests/timens/clock_nanosleep.c @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "log.h" +#include "timens.h" + +static long long get_elapsed_time(int clockid, struct timespec *start) +{ + struct timespec curr; + long long secs, nsecs; + + if (clock_gettime(clockid, &curr) == -1) + return pr_perror("clock_gettime"); + + secs = curr.tv_sec - start->tv_sec; + nsecs = curr.tv_nsec - start->tv_nsec; + if (nsecs < 0) { + secs--; + nsecs += 1000000000; + } + if (nsecs > 1000000000) { + secs++; + nsecs -= 1000000000; + } + return secs * 1000 + nsecs / 1000000; +} + +int run_test(int clockid) +{ + long long elapsed; + int i; + + for (i = 0; i < 2; i++) { + struct timespec now = {}; + struct timespec start; + + if (clock_gettime(clockid, &start) == -1) + return pr_perror("clock_gettime"); + + + if (i == 1) { + now.tv_sec = start.tv_sec; + now.tv_nsec = start.tv_nsec; + } + + now.tv_sec += 2; + clock_nanosleep(clockid, i ? TIMER_ABSTIME : 0, &now, NULL); + + elapsed = get_elapsed_time(clockid, &start); + if (elapsed < 1900 || elapsed > 2100) { + pr_fail("clockid: %d abs: %d elapsed: %lld\n", + clockid, i, elapsed); + return 1; + } + ksft_test_result_pass("clockid: %d abs:%d\n", clockid, i); + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + int ret, nsfd; + + nscheck(); + + if (unshare(CLONE_NEWTIME)) + return pr_perror("unshare"); + + if (_settime(CLOCK_MONOTONIC, 7 * 24 * 3600)) + return 1; + if (_settime(CLOCK_BOOTTIME, 9 * 24 * 3600)) + return 1; + + nsfd = open("/proc/self/ns/time_for_children", O_RDONLY); + if (nsfd < 0) + return pr_perror("Unable to open timens_for_children"); + + if (setns(nsfd, CLONE_NEWTIME)) + return pr_perror("Unable to set timens"); + + ret = 0; + ret |= run_test(CLOCK_MONOTONIC); + + if (ret) + ksft_exit_fail(); + ksft_exit_pass(); + return ret; +} + -- 2.20.1