Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp541196img; Wed, 20 Mar 2019 06:05:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqynSReDMS9TOxHNYUfDhSGeWSqOtWOQ1s+OHoOZOh18lyiZ3qUf4xenUT4o6JBajjzaNKLS X-Received: by 2002:a63:61d7:: with SMTP id v206mr10264828pgb.349.1553087153792; Wed, 20 Mar 2019 06:05:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553087153; cv=none; d=google.com; s=arc-20160816; b=c1EHCmb6fRd8pxR7Y4aLVBCI76nw7oPFR+dxqg1ehz3HtpsPtX6hSseoo4mvIqV/hC A0pYbMwhdhtiZX1F5f63W6pHV6Usw2KX0WS0PNE8yHuPuSQjmolOb3WIWSCrLlVroTOL 9fpM2yGF7bi09xvgRGJ+jzvJ67tSRAv+YLbeoYEy05m5ATRFzY/clbT4QB/bqhW7yNhe zomeEGoaqcR3r0HZ+2ek2ImtfkWWl/9HpJ+4+I5gmPOSjjhLoiG8rJFTG+lCU0t+jujh Dg144D2gxx7RDHhEn7BLLI2j0VYHgUOWzqgeK1AU4nTpVEYQJUps/iEusflam2gNCFVL /awg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:date:message-id:subject:cc:from:to :in-reply-to; bh=sRcGLS6PCTc/KO60PYMnf5P0QYaxX2kHK5fpNZg8X3A=; b=IwRYoarZI2FN3w7dXoAGQqAuzVXuu5Z/nmlTvb/2G/kfiWJoHbpxNgCRcFblr+s85V ThctNMcvEueZsfpb3EvyR4xqzv7VM8orCFzLL2SAnJXXABpA0gtwHFyrWzFyCAPR/3OI U207mTo6kKd8OOPacg0D4d3NlFY1tB7HT4a7RQ0Vejj5Mel0gkm6pbWuPQygwXG7WW/f 1hTvlbacvbk0CU9FaQ4ycYCsqR7fi+/+ts4Y0fiDHurTQ2HgnwB2lPc43Za5VkWq0Ljo /JH9xjJiqXw+Vk/CGcjhgsgXC1yaGfOaLqeIjIxn6fTdfRB1Lm3Wno5Had3csuLKPPmn Ft/Q== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j35si1875879plb.61.2019.03.20.06.05.34; Wed, 20 Mar 2019 06:05:53 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727688AbfCTNEh (ORCPT + 99 others); Wed, 20 Mar 2019 09:04:37 -0400 Received: from ozlabs.org ([203.11.71.1]:33273 "EHLO ozlabs.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726506AbfCTNEe (ORCPT ); Wed, 20 Mar 2019 09:04:34 -0400 Received: by ozlabs.org (Postfix, from userid 1034) id 44PVW34B1Cz9sPY; Thu, 21 Mar 2019 00:04:31 +1100 (AEDT) X-powerpc-patch-notification: thanks X-powerpc-patch-commit: b5b4453e7912f056da1ca7572574cada32ecb60c X-Patchwork-Hint: ignore In-Reply-To: <20190313131438.8212-1-mpe@ellerman.id.au> To: Michael Ellerman , linuxppc-dev@ozlabs.org From: Michael Ellerman Cc: arnd@arndb.de, sboyd@kernel.org, linux-kernel@vger.kernel.org, john.stultz@linaro.org, tglx@linutronix.de, jaydee@email.cz Subject: Re: powerpc/vdso64: Fix CLOCK_MONOTONIC inconsistencies across Y2038 Message-Id: <44PVW34B1Cz9sPY@ozlabs.org> Date: Thu, 21 Mar 2019 00:04:31 +1100 (AEDT) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 2019-03-13 at 13:14:38 UTC, Michael Ellerman wrote: > Jakub Drnec reported: > Setting the realtime clock can sometimes make the monotonic clock go > back by over a hundred years. Decreasing the realtime clock across > the y2k38 threshold is one reliable way to reproduce. Allegedly this > can also happen just by running ntpd, I have not managed to > reproduce that other than booting with rtc at >2038 and then running > ntp. When this happens, anything with timers (e.g. openjdk) breaks > rather badly. > > And included a test case (slightly edited for brevity): > #define _POSIX_C_SOURCE 199309L > #include > #include > #include > #include > > long get_time(void) { > struct timespec tp; > clock_gettime(CLOCK_MONOTONIC, &tp); > return tp.tv_sec + tp.tv_nsec / 1000000000; > } > > int main(void) { > long last = get_time(); > while(1) { > long now = get_time(); > if (now < last) { > printf("clock went backwards by %ld seconds!\n", last - now); > } > last = now; > sleep(1); > } > return 0; > } > > Which when run concurrently with: > # date -s 2040-1-1 > # date -s 2037-1-1 > > Will detect the clock going backward. > > The root cause is that wtom_clock_sec in struct vdso_data is only a > 32-bit signed value, even though we set its value to be equal to > tk->wall_to_monotonic.tv_sec which is 64-bits. > > Because the monotonic clock starts at zero when the system boots the > wall_to_montonic.tv_sec offset is negative for current and future > dates. Currently on a freshly booted system the offset will be in the > vicinity of negative 1.5 billion seconds. > > However if the wall clock is set past the Y2038 boundary, the offset > from wall to monotonic becomes less than negative 2^31, and no longer > fits in 32-bits. When that value is assigned to wtom_clock_sec it is > truncated and becomes positive, causing the VDSO assembly code to > calculate CLOCK_MONOTONIC incorrectly. > > That causes CLOCK_MONOTONIC to jump ahead by ~4 billion seconds which > it is not meant to do. Worse, if the time is then set back before the > Y2038 boundary CLOCK_MONOTONIC will jump backward. > > We can fix it simply by storing the full 64-bit offset in the > vdso_data, and using that in the VDSO assembly code. We also shuffle > some of the fields in vdso_data to avoid creating a hole. > > The original commit that added the CLOCK_MONOTONIC support to the VDSO > did actually use a 64-bit value for wtom_clock_sec, see commit > a7f290dad32e ("[PATCH] powerpc: Merge vdso's and add vdso support to > 32 bits kernel") (Nov 2005). However just 3 days later it was > converted to 32-bits in commit 0c37ec2aa88b ("[PATCH] powerpc: vdso > fixes (take #2)"), and the bug has existed since then AFAICS. > > Fixes: 0c37ec2aa88b ("[PATCH] powerpc: vdso fixes (take #2)") > Cc: stable@vger.kernel.org # v2.6.15+ > Link: http://lkml.kernel.org/r/HaC.ZfES.62bwlnvAvMP.1STMMj@seznam.cz > Reported-by: Jakub Drnec > Signed-off-by: Michael Ellerman Applied to powerpc fixes. https://git.kernel.org/powerpc/c/b5b4453e7912f056da1ca7572574cada cheers