Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1194984imm; Wed, 19 Sep 2018 13:53:27 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaapMXLvYSLvzo3brTgbnwwcwpd9q6hAWKDxX/6OLrYtmSCoVQWfb+hIeYJoDDv8m3OxS+k X-Received: by 2002:aa7:824d:: with SMTP id e13-v6mr37847636pfn.97.1537390407333; Wed, 19 Sep 2018 13:53:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537390407; cv=none; d=google.com; s=arc-20160816; b=aRFUJp9AyUiHa4Nce++xa4cFL8zRi/F8CSm8I5Dg37JTKhM0eOSA4mEYgq43bU1Z+A IjLkhxNOfszKc8SRnFXp4i8IqtBbyFxEJ+X6O7JkfxPhLtFZkfSu4xBrtX8K9sZMCeE8 l8LqgSRloTs51l2JBTeS2LvT6UP8JeoCIhNc4RLNXybB82tvz3hAnHM7cSaSZAEcAH6n fg4sFrLuca+12lEt1QxsGm0FOWQaiASNXT1/4I2ZBgVNT769LEfCx4IZjYEgvIFNHGEp GfwhHtV3yundbr5XmuOnZCncVe+ApB34w4/e1JCy0omI1WMqzeaP1NezVX3NdG90AHyH oTLA== 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=WKtSdkBnxDPJmvhkpFaTo4RYz8881z3zXHLEjda03aw=; b=rDK3W8AmpyV0TiA9sFcarb7pJZCJ0d4I5HLBOnfXHRDxkLn1uCPwPvwDCeAV1EXJcT 1rXcC8oFjeHt46TRppouzUJNg5EmGqL1h2auhhXsejqTsQaYJS/Da+1UkjfmWknsq3Qv tuyohuTOMyFHRYlSVlVgh1bvdhMP8QHg4/pGEX1KjSlZGyPvPj/RcJEIv0u5y3FHpr5D 3Le6gxeZkkjV7fU3+KVXQM/F0lJ9oplegaprFtdZj5QZz/JvPGa3eTNBSs2bGN23ABpN zucDPj7VKVjvONnMYJcc7HHQwMolvD6/saXO6+XlND6DCf6ZY7ZBuNaLaMqjDrnd0VGL PVQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b="na/cDBjf"; 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 81-v6si4076929pgf.599.2018.09.19.13.53.12; Wed, 19 Sep 2018 13:53:27 -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="na/cDBjf"; 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 S1732689AbeITCa2 (ORCPT + 99 others); Wed, 19 Sep 2018 22:30:28 -0400 Received: from mail-ed1-f68.google.com ([209.85.208.68]:40418 "EHLO mail-ed1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732488AbeITCa1 (ORCPT ); Wed, 19 Sep 2018 22:30:27 -0400 Received: by mail-ed1-f68.google.com with SMTP id j62-v6so6018465edd.7 for ; Wed, 19 Sep 2018 13:50:47 -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=WKtSdkBnxDPJmvhkpFaTo4RYz8881z3zXHLEjda03aw=; b=na/cDBjfADbmnnfncUiDb/JsunW4GTJS5sJUDanE0Lmh2YFiGcJZamFgnhbHpL0A19 vrNtF/STLoN7HBl3EDZvot3Bmsc0xPwN/Ihme5KYEP2XcAlCDoURx84yie9oW5TzfsVl +9f5i7QseIgr1vr4ZaDGj+2BFH2xqUCIAqiw+vjCB4jNeznF7dGYAlSFZTP/jdctTGL1 3VaLWexWz5+o37FkAubOkOMJT9i1+gHR5YqdJKawdFOeqvVdojWCdxnf1/DF0RRYJYF/ vbGFQcp1C5M4Uj6PqrovPp3g6q8EbT2akPCYplYHl+G6EPLrCPEDgRjtOye0t1N5h4qM 7oBA== 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=WKtSdkBnxDPJmvhkpFaTo4RYz8881z3zXHLEjda03aw=; b=SSTaMlCVQIYWUd0QPZqMPcTK6RCGd+OCdjaivjG3Se9hA3JwNxdokgcWLvb50KgX1L 8C8vT67EB6hfBZjioc9Ar9iRN+Dna9f6ywqTJGNudOCJlQTzuZbdYfauTeN46pPWc6PF R39+FVGN9/242xFxRd9TTYaLuks5TIkpY6ta7gku27oAl100/ReJnDA2fGHysJew4NFF sz/yzJYzI3A3N03/mQuox5/EZVeNEadSMPiStyZ+R2PU3OqOwHkMEZKZvnuf/M1I9TAE 1nRnOUYZBi9Fu1uWVo8UlmJLvSvW67i7KlCTFILernDdGSbK8wmJq7GdfQekkXwScFw9 ShvQ== X-Gm-Message-State: APzg51BBEPNGkU+/bTE4d/AgGaCFs1KbMQxFjmodR00ss5Dw0bKblDhg hcwaSrvrL7aTlrbdf9GxHtPILhy1LxY= X-Received: by 2002:a50:82c5:: with SMTP id 63-v6mr61267233edg.200.1537390246599; Wed, 19 Sep 2018 13:50:46 -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.45 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 19 Sep 2018 13:50:46 -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 05/20] timerfd/timens: Take into account ns clock offsets Date: Wed, 19 Sep 2018 21:50:22 +0100 Message-Id: <20180919205037.9574-6-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 Make timerfd respect timens offsets. Provide two helpers timens_clock_to_host() timens_clock_from_host() that are useful to wire up timens to different kernel subsystems. Following patches will use timens_clock_from_host(), added here for completeness. Signed-off-by: Andrei Vagin Co-developed-by: Dmitry Safonov Signed-off-by: Dmitry Safonov --- fs/timerfd.c | 16 +++++++++++----- include/linux/time_namespace.h | 11 +++++++++++ kernel/time_namespace.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/fs/timerfd.c b/fs/timerfd.c index d69ad801eb80..001ab7a0fd8b 100644 --- a/fs/timerfd.c +++ b/fs/timerfd.c @@ -26,6 +26,7 @@ #include #include #include +#include struct timerfd_ctx { union { @@ -433,22 +434,27 @@ SYSCALL_DEFINE2(timerfd_create, int, clockid, int, flags) } static int do_timerfd_settime(int ufd, int flags, - const struct itimerspec64 *new, + struct itimerspec64 *new, struct itimerspec64 *old) { struct fd f; struct timerfd_ctx *ctx; int ret; - if ((flags & ~TFD_SETTIME_FLAGS) || - !itimerspec64_valid(new)) - return -EINVAL; - ret = timerfd_fget(ufd, &f); if (ret) return ret; ctx = f.file->private_data; + if (flags & TFD_TIMER_ABSTIME) + timens_clock_to_host(ctx->clockid, &new->it_value); + + if ((flags & ~TFD_SETTIME_FLAGS) || + !itimerspec64_valid(new)) { + fdput(f); + return -EINVAL; + } + if (isalarm(ctx) && !capable(CAP_WAKE_ALARM)) { fdput(f); return -EPERM; diff --git a/include/linux/time_namespace.h b/include/linux/time_namespace.h index 4960c54f1b33..910711d1c39d 100644 --- a/include/linux/time_namespace.h +++ b/include/linux/time_namespace.h @@ -38,6 +38,9 @@ static inline void put_time_ns(struct time_namespace *ns) kref_put(&ns->kref, free_time_ns); } +void timens_clock_to_host(int clockid, struct timespec64 *val); +void timens_clock_from_host(int clockid, struct timespec64 *val); + #else static inline void get_time_ns(struct time_namespace *ns) { @@ -56,6 +59,14 @@ static inline struct time_namespace *copy_time_ns(unsigned long flags, return old_ns; } +static inline void timens_clock_to_host(int clockid, struct timespec64 *val) +{ +} + +static inline void timens_clock_from_host(int clockid, struct timespec64 *val) +{ +} + #endif #endif /* _LINUX_TIMENS_H */ diff --git a/kernel/time_namespace.c b/kernel/time_namespace.c index a985529754b4..f96871cb8124 100644 --- a/kernel/time_namespace.c +++ b/kernel/time_namespace.c @@ -154,6 +154,45 @@ static struct user_namespace *timens_owner(struct ns_common *ns) return to_time_ns(ns)->user_ns; } +static void clock_timens_fixup(int clockid, struct timespec64 *val, bool to_ns) +{ + struct timens_offsets *ns_offsets = current->nsproxy->time_ns->offsets; + struct timespec64 *offsets = NULL; + + if (!ns_offsets) + return; + + if (val->tv_sec == 0 && val->tv_nsec == 0) + return; + + switch (clockid) { + case CLOCK_MONOTONIC: + offsets = &ns_offsets->monotonic_time_offset; + break; + case CLOCK_BOOTTIME: + offsets = &ns_offsets->monotonic_boottime_offset; + break; + } + + if (!offsets) + return; + + if (to_ns) + *val = timespec64_add(*val, *offsets); + else + *val = timespec64_sub(*val, *offsets); +} + +void timens_clock_to_host(int clockid, struct timespec64 *val) +{ + clock_timens_fixup(clockid, val, false); +} + +void timens_clock_from_host(int clockid, struct timespec64 *val) +{ + clock_timens_fixup(clockid, val, true); +} + const struct proc_ns_operations timens_operations = { .name = "time", .type = CLONE_NEWTIME, -- 2.13.6