Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1195051imm; Wed, 19 Sep 2018 13:53:33 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbZI2GNotXqAOwTKIQAyrZIsB90bE4YsMByAbmZ/+xSKYhldYy0sGtMwf3cLwE54uqsd+Cu X-Received: by 2002:a17:902:22:: with SMTP id 31-v6mr35816909pla.190.1537390413885; Wed, 19 Sep 2018 13:53:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537390413; cv=none; d=google.com; s=arc-20160816; b=bsl5Tt6KiPH6FN9H+2bfcozCi+RgDLO+oFvth2S9xUEWERlinN+ou3UxIvR5GZYEZZ ZLcxoUIFfZAsEnO8rWL6eX7hrxqPxKugppyhB+bBO7T3Ozq5Qxeo4CYpAA/A/sBuFF0e i1coKHTyBX8hCWm0GErPApwItiPvW1qvyhm7yWWiwg/6VGC9o3VpOCTJjXLHg5J5OVrw OYkAXD0N9q4RMNauXP+o2U4/YT0RNbtwHMfdWzQHyYnSd2vdDW8pyaVElAVCPb+8HbCJ jHlPbSfzUv41Ehmp5Iw2w33EUwVu/z+mvZ0cVHQReL9G1QXYGOUN3oKmcjpOWuLoxugX YENA== 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=/GN/0v5e4mpHpuFOFRnCIZwBKmgxXnFMSxpKitNeAXo=; b=nJ+rbbs5Od/6BohKp6YxmXPBjwBUJo8eoJWmhjHp3/8uIpR6q9PFxQvDsDqZzx0XUQ sn2fGY+tij5mZJfLXJDM7Z2LG0gc/8ExWT9xKNSsiSzwUHFbaU9RcOpViXCyRi+FnYYj EgExqvKQpGvZKDqlU2lqyq8PyNl3G2gtSD8tZhT3ivgwmoB2zQFQRm6+YU7WcWjtHQaX WYRi9EQMALNFsBDLa8ko/U0Og9nMeceGVikvle1fqJ+mBw1qdo7+kWHdI6GgHTgZERSY kylHUkiFoqjtbng5JlMBZA1N54/3e4qRRnlfG2pL1Jf6fCrP6bhhSprVEyFl0jzlFyif u3tA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arista.com header.s=googlenew header.b=Df1BR2ei; 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 f10-v6si21789863pgk.367.2018.09.19.13.53.18; Wed, 19 Sep 2018 13:53:33 -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=Df1BR2ei; 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 S1732879AbeITCaf (ORCPT + 99 others); Wed, 19 Sep 2018 22:30:35 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:46265 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732834AbeITCae (ORCPT ); Wed, 19 Sep 2018 22:30:34 -0400 Received: by mail-ed1-f67.google.com with SMTP id k14-v6so6004563edr.13 for ; Wed, 19 Sep 2018 13:50:55 -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=/GN/0v5e4mpHpuFOFRnCIZwBKmgxXnFMSxpKitNeAXo=; b=Df1BR2eiVg0pMMtMSqaZcj/d+myni8ow5rNj21y3uhN2h5TdxnAml73SnELbQbAuRe lCWAiGW2bkvRTXVnVDhouOUtk9Bg+yupo2pUB97gXdgRJNNez65RCN2iATn1wAB8jKHQ BIrggy/J3XKDOIF3OProbVFHR7rjc1rZVux55zziNvuWLLyRBqOlQDsJDC9fqWHHoCOD bIexySZybD5BdlT5Et8435pIVB9xwAqDvTUVm4NXy54X88AiCm3KgECNppmu7sfmImMk i6RhJ96bdVYVnlcxedlYGqRkOCT3XdKfer/hPupKZzpKfVTNluXhijNVeN9O2PGX2h3O Oo2g== 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=/GN/0v5e4mpHpuFOFRnCIZwBKmgxXnFMSxpKitNeAXo=; b=nIgj50DD+DNzQC2FIda9l0UPpl8T8VmH3UV/lTQkdBVinGu8U2Z/XCRWsJJV9tVrn5 QZoudZhSqJf08htqeuO2lmBw5g146J2dbGKmbXBXkVh7ILeEkZjoGJnPywMYzsLDrFRv GDTAovUgsjMZkHklwnAwL25qeoTltJeMDDh5LuI3GkrpkmH+8vkbSw3hXju9ug4vD+Dn wWaEiqlHNQCht3cmO04tKMy1N/ybd7oMD5ecZ9xgqjvCmj/GLM56haOivTkfcJKGD/x0 4Uv/oh/lTBSvciDH/bt0HsSy8tPdpqr2v9+dnQOfz2R3JhQ1QlpqXBK4WlI+6x5d9e6U d+EQ== X-Gm-Message-State: APzg51CbEhddJiFatnSDIIov/Ydfu6Jq+MMyMof6vY5yoGzVb805w1Cn 00lgziXIETyG65RlujWe+wWO04rjs38= X-Received: by 2002:aa7:c881:: with SMTP id p1-v6mr59783289eds.295.1537390253998; Wed, 19 Sep 2018 13:50:53 -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.52 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 19 Sep 2018 13:50:53 -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 10/20] x86/vdso: Use set_normalized_timespec() to avoid 32 bit overflow Date: Wed, 19 Sep 2018 21:50:27 +0100 Message-Id: <20180919205037.9574-11-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 ts->tv_nsec + offset->tv_nsec On 32 bit machines that sum can be larger than (1 << 31) and therefor result in a negative value which screws up the result completely. Signed-off-by: Andrei Vagin Signed-off-by: Dmitry Safonov --- arch/x86/entry/vdso/vclock_gettime.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/arch/x86/entry/vdso/vclock_gettime.c b/arch/x86/entry/vdso/vclock_gettime.c index 0594266740b9..a265e2737a9a 100644 --- a/arch/x86/entry/vdso/vclock_gettime.c +++ b/arch/x86/entry/vdso/vclock_gettime.c @@ -231,20 +231,37 @@ notrace static int __always_inline do_realtime(struct timespec *ts) return mode; } +notrace void set_normalized_timespec(struct timespec *ts, time_t sec, s64 nsec) +{ + while (nsec >= NSEC_PER_SEC) { + /* + * The following asm() prevents the compiler from + * optimising this loop into a modulo operation. See + * also __iter_div_u64_rem() in include/linux/time.h + */ + asm("" : "+rm"(nsec)); + nsec -= NSEC_PER_SEC; + ++sec; + } + while (nsec < 0) { + asm("" : "+rm"(nsec)); + nsec += NSEC_PER_SEC; + --sec; + } + ts->tv_sec = sec; + ts->tv_nsec = nsec; +} + notrace static __always_inline void monotonic_to_ns(struct timespec *ts) { #ifdef CONFIG_TIME_NS struct timens_offsets *timens = (struct timens_offsets *) &timens_page; + struct timespec offset; + + offset = timespec64_to_timespec(timens->monotonic_time_offset); + + *ts = timespec_add(*ts, offset); - ts->tv_sec += timens->monotonic_time_offset.tv_sec; - ts->tv_nsec += timens->monotonic_time_offset.tv_nsec; - if (ts->tv_nsec > NSEC_PER_SEC) { - ts->tv_nsec -= NSEC_PER_SEC; - ts->tv_sec++; - } else if (ts->tv_nsec < 0) { - ts->tv_nsec += NSEC_PER_SEC; - ts->tv_sec--; - } #endif } -- 2.13.6