Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1194599imm; Wed, 19 Sep 2018 13:52:54 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYjTDIlAq/CX73OcoQC/isBSe0MTq+n8bOUbCKDJMPQ3zFvYpMzsw8nHKHT/D/xhGcDsGdo X-Received: by 2002:a17:902:2f43:: with SMTP id s61-v6mr35243186plb.176.1537390374814; Wed, 19 Sep 2018 13:52:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537390374; cv=none; d=google.com; s=arc-20160816; b=Yta96DatIoQEduQX7nU6AtbXjOpC74HPaHrhXj/qU5/N3LKtKU3b/fF0UQfsEEuta9 tXFm0Tja40EVRoovdQgyEz5KOrzx9zVpNqLDp4wO3teQh83/j6RfcuX86wzfyjpPIKvx S8goa6mJ0sJYf73ODt1j3oulIUTfc0rZuQbs77ddnkSj+YhlacyFgGynu8UOGdS8U9t4 fVVF5LTWiDDeNPimw0TrrsWooExI7Mg2OShC1Db7/A9uDgO3VHyeL8ScylR9podNqHj9 Byb8r71KAvsNIEXVQiRmR5Cg2endFwPaRpj8qpsMY0c+UtDCEQnBftJvQtFqHGffCgF+ czcQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=/My90qQYivh+ggnDkzV7nIXKz/st4P1p9Q6cxQyPS9Y=; b=xcCsh2AGG+eyFRlUweBTieilBKf6oQ1DUJg8u800mdD3+lnokWQrLk7RK/qdU3XzYA j+KurreDBp7QvN72cnFHwvM1Rvb3tak2xcfPVsUhXbZ+Gxa500QQR2slQNCUP/ldfS6b 8Hy/RpB21rgm6JyeZBYvPC8XV/KziFRp+7ZwdfPG+W/W3uplvV8KWEgBrKH3fpZvumgc cDEy2yKv9eW22ArAv0oWuPX9se4uyTUWCbqxP8brxq1TQK/deR/ogD9mIFl+IWCABQga zaLcOqCeXzmwHHbq2X6R8QGQXiIwrD+lRHyyzZ5kDxv/N1OrcwRSJ3E95B7xYIAU3K2b wlNg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=dTUtpW96; 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 n1-v6si23142928pfe.66.2018.09.19.13.52.39; Wed, 19 Sep 2018 13:52:54 -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=dTUtpW96; 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 S1733042AbeITCal (ORCPT + 99 others); Wed, 19 Sep 2018 22:30:41 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:43365 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732982AbeITCaj (ORCPT ); Wed, 19 Sep 2018 22:30:39 -0400 Received: by mail-ed1-f67.google.com with SMTP id z27-v6so6012169edb.10 for ; Wed, 19 Sep 2018 13:50:59 -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; bh=/My90qQYivh+ggnDkzV7nIXKz/st4P1p9Q6cxQyPS9Y=; b=dTUtpW96x8E2guWneUGZkMU7Q3mhcUAu/7M5f/Acp/9HYODk1PvEOiB47Pq1eWmWdh FHBZT8dWcdDbK4LBPVOsLTfmBxM0CxlZVqJf+47/uEtyWIbC0BjSlH6yVAeg0ojjbc22 hKp1gLGwJ6n08j2NhwsbK/N10xjgGSG5MxTUvEDCSNiicNJeT1BmkLAAVeh/cDy/Dclq brocTeWDd06EKnIK2ObeKMe3VQsJ2OTmDrzEyTF2bT50WCJxCfMvV74Q+qw/WmLX9teO 35a0ub0qwjBNvTdSVhgdfSE0C9XFTp3//JgJmSbJDtjSnKvbcMyzy+fsy+pmrHzEDX2r NXTA== 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; bh=/My90qQYivh+ggnDkzV7nIXKz/st4P1p9Q6cxQyPS9Y=; b=LnWPwXCgzfI4s5RHS23B99idrSwkU9+Jf++IH3yqiS33zx5msxnGj3akW6479yfyFd UYiPy4iy5y9c9J0u9pcf93+wX23vq3/XAh0AO7yaXcTvYQMBpDo9bwkktfOLRGVQrHvT 9YUAGJdnKVOZxYk2Uy7XlaEjk6MD071yBKOi4n9f+9joBxERJVcdVLjehmHJuSZiNnlf t6J9vVz7jxbb+/VgKHmkdV7spNs+R83mp18rBt/DU4j+IQGUSLxipaJW6d+Kx+ojDZrP eFDW/Q65E/GSbpO9ksrIGeCoYTTRjd2dT5gUAXQoH3VXth9OmsHsj7XjJj2/J9T8x7xq JAlA== X-Gm-Message-State: APzg51AeN4XZiwK44aqBVgwRi3ekQWawsWAEgwt6UTMQNz/qORbNXWBy eBK3C/g0Lf3V2puKQzW31XdqUS6n0Xg= X-Received: by 2002:aa7:c314:: with SMTP id l20-v6mr61308355edq.53.1537390258514; Wed, 19 Sep 2018 13:50:58 -0700 (PDT) Received: from dhcp.ire.aristanetworks.com ([217.173.96.166]) by smtp.gmail.com with ESMTPSA id t17-v6sm1747729edb.27.2018.09.19.13.50.57 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 19 Sep 2018 13:50:57 -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 , Christian Brauner , Cyrill Gorcunov , "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: [RFC 13/20] posix-timers/timens: Take into account clock offsets Date: Wed, 19 Sep 2018 21:50:30 +0100 Message-Id: <20180919205037.9574-14-dima@arista.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180919205037.9574-1-dima@arista.com> References: <20180919205037.9574-1-dima@arista.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrei Vagin Provide a helper that will convert clocks to time namespace. Signed-off-by: Andrei Vagin Signed-off-by: Dmitry Safonov --- kernel/time/posix-timers.c | 52 +++++++++++++++++++++++++++++++--------------- kernel/time/posix-timers.h | 2 ++ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index d38835a21c5d..701cb0602b7a 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -206,12 +206,26 @@ static int posix_clock_realtime_adj(const clockid_t which_clock, return do_adjtimex(t); } -static void timens_adjust_monotonic(struct timespec64 *tp) +static void common_timens_adjust(clockid_t which_clock, struct timespec64 *tp) { struct timens_offsets *ns_offsets = current->nsproxy->time_ns->offsets; - if (ns_offsets) + if (!ns_offsets) + return; + + switch (which_clock) { + case CLOCK_MONOTONIC: + case CLOCK_MONOTONIC_RAW: + case CLOCK_MONOTONIC_COARSE: *tp = timespec64_add(*tp, ns_offsets->monotonic_time_offset); + break; + case CLOCK_BOOTTIME: + *tp = timespec64_add(*tp, ns_offsets->monotonic_boottime_offset); + break; + default: + WARN_ONCE(1, "Time Namespace offset for %d is not realized", + which_clock); + } } static int posix_ktime_set_ts(clockid_t which_clock, @@ -239,7 +253,6 @@ static int posix_ktime_set_ts(clockid_t which_clock, static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp) { ktime_get_ts64(tp); - timens_adjust_monotonic(tp); return 0; } @@ -249,7 +262,6 @@ static int posix_ktime_get_ts(clockid_t which_clock, struct timespec64 *tp) static int posix_get_monotonic_raw(clockid_t which_clock, struct timespec64 *tp) { ktime_get_raw_ts64(tp); - timens_adjust_monotonic(tp); return 0; } @@ -264,7 +276,6 @@ static int posix_get_monotonic_coarse(clockid_t which_clock, struct timespec64 *tp) { ktime_get_coarse_ts64(tp); - timens_adjust_monotonic(tp); return 0; } @@ -276,15 +287,7 @@ static int posix_get_coarse_res(const clockid_t which_clock, struct timespec64 * static int posix_get_boottime(const clockid_t which_clock, struct timespec64 *tp) { - struct timens_offsets *ns_offsets = current->nsproxy->time_ns->offsets; - ktime_get_boottime_ts64(tp); - - if (!ns_offsets) - return 0; - - *tp = timespec64_add(*tp, ns_offsets->monotonic_boottime_offset); - return 0; } @@ -933,10 +936,6 @@ static int do_timer_settime(timer_t timer_id, int flags, unsigned long flag; int error = 0; - if (!timespec64_valid(&new_spec64->it_interval) || - !timespec64_valid(&new_spec64->it_value)) - return -EINVAL; - if (old_spec64) memset(old_spec64, 0, sizeof(*old_spec64)); retry: @@ -944,6 +943,15 @@ static int do_timer_settime(timer_t timer_id, int flags, if (!timr) return -EINVAL; + if (flags & TIMER_ABSTIME) + timens_clock_to_host(timr->it_clock, &new_spec64->it_value); + + if (!timespec64_valid(&new_spec64->it_interval) || + !timespec64_valid(&new_spec64->it_value)) { + unlock_timer(timr, flag); + return -EINVAL; + } + kc = timr->kclock; if (WARN_ON_ONCE(!kc || !kc->timer_set)) error = -EINVAL; @@ -1121,6 +1129,9 @@ SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock, error = kc->clock_get(which_clock, &kernel_tp); + if (!error && kc->clock_timens_adjust) + kc->clock_timens_adjust(which_clock, &kernel_tp); + if (!error && put_timespec64(&kernel_tp, tp)) error = -EFAULT; @@ -1197,6 +1208,9 @@ COMPAT_SYSCALL_DEFINE2(clock_gettime, clockid_t, which_clock, err = kc->clock_get(which_clock, &ts); + if (!err && kc->clock_timens_adjust) + kc->clock_timens_adjust(which_clock, &ts); + if (!err && compat_put_timespec64(&ts, tp)) err = -EFAULT; @@ -1340,6 +1354,7 @@ static const struct k_clock clock_monotonic = { .clock_getres = posix_get_hrtimer_res, .clock_get = posix_ktime_get_ts, .clock_set = posix_ktime_set_ts, + .clock_timens_adjust = common_timens_adjust, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set, @@ -1356,6 +1371,7 @@ static const struct k_clock clock_monotonic_raw = { .clock_getres = posix_get_hrtimer_res, .clock_get = posix_get_monotonic_raw, .clock_set = posix_ktime_set_ts, + .clock_timens_adjust = common_timens_adjust, }; static const struct k_clock clock_realtime_coarse = { @@ -1367,6 +1383,7 @@ static const struct k_clock clock_monotonic_coarse = { .clock_getres = posix_get_coarse_res, .clock_get = posix_get_monotonic_coarse, .clock_set = posix_ktime_set_ts, + .clock_timens_adjust = common_timens_adjust, }; static const struct k_clock clock_tai = { @@ -1388,6 +1405,7 @@ static const struct k_clock clock_boottime = { .clock_getres = posix_get_hrtimer_res, .clock_get = posix_get_boottime, .clock_set = posix_set_boottime, + .clock_timens_adjust = common_timens_adjust, .nsleep = common_nsleep, .timer_create = common_timer_create, .timer_set = common_timer_set, diff --git a/kernel/time/posix-timers.h b/kernel/time/posix-timers.h index ddb21145211a..308774bea32a 100644 --- a/kernel/time/posix-timers.h +++ b/kernel/time/posix-timers.h @@ -8,6 +8,8 @@ struct k_clock { const struct timespec64 *tp); int (*clock_get)(const clockid_t which_clock, struct timespec64 *tp); + void (*clock_timens_adjust)(const clockid_t which_clock, + struct timespec64 *tp); int (*clock_adj)(const clockid_t which_clock, struct timex *tx); int (*timer_create)(struct k_itimer *timer); int (*nsleep)(const clockid_t which_clock, int flags, -- 2.13.6