Received: by 2002:a25:683:0:0:0:0:0 with SMTP id 125csp2007859ybg; Fri, 5 Jun 2020 03:14:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw8x26eSvWU+a3hVFcF9c03lttwqTS/NmU0O5lx0Yn01EXmDzCH5gSg2uxQlLpB2gFkdZMD X-Received: by 2002:a17:906:f155:: with SMTP id gw21mr7861902ejb.388.1591352064267; Fri, 05 Jun 2020 03:14:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1591352064; cv=none; d=google.com; s=arc-20160816; b=HURI+Swv+W/sOzmYJXsFryU/eBIlOgvAjbDvig1KdJe45j75AWzwHWTdnPDFovKs18 3om75b9r1cU3eocfr+JoZB6ZeWYR+fwsf4hWNRNjauhjfwtRfWse7H6y6IfMxzDbtp7P VDsczzmtcCmBgVm5N1HiOnMLMLHof43Q09pw1PBFOg5wKhfu1mDy/DgW/mhvZgnGo0Bv f9jD+aRGhwfBewvkA0fgAARvDraqREgHWpvKi/d0o/GzVh4fmxD4R1/WjjZAhEUEhiL1 7P0ipVFHFaP5OcNaRu44zxnHf20PVsFfZEgWX8CubgJ//ns2k7czDnOPolateMs1pZHU 6/Rw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:in-reply-to :subject:cc:to:from; bh=dNC/mzdqI4pmatI5L58sfzT4j/NpOgOaJpvh28SS99c=; b=tGBhuQbumDZXF80Iq0pTH65Vtokume1/4iTLT+v8ETP5NxhUocb+3uM+IPwXBJMvQf MpscGFDySHmOmrT0EGlETSheRQ5qCMqeb1dqzjj2xfnjMqiDlmAgztKukdU7ibpQ4T8J pRBTaELjBN3plEesj9JbQR3m2XOqNb0iBmPz7nMq1F/DuRdjE8Yy2NNzwnBcVLUOjxAV pPxEBcRmLhYPnWt6YZaFhiudlhu/Y90IfM3IfZekvPuHHqwEcPlWiZiX/qZ8lYihkIgv gJboSOlQJvuvC6t1H75+iFLh/0z+bmTOfiZG8s4IVQHUAQAnsRZHHOdlYkDaTIC6u7v5 k2rg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id pw15si3188820ejb.583.2020.06.05.03.14.00; Fri, 05 Jun 2020 03:14:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726291AbgFEKMK (ORCPT + 99 others); Fri, 5 Jun 2020 06:12:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725926AbgFEKMJ (ORCPT ); Fri, 5 Jun 2020 06:12:09 -0400 Received: from Galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 849E2C08C5C2; Fri, 5 Jun 2020 03:12:09 -0700 (PDT) Received: from p5de0bf0b.dip0.t-ipconnect.de ([93.224.191.11] helo=nanos.tec.linutronix.de) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1jh9KT-00021g-M4; Fri, 05 Jun 2020 12:12:01 +0200 Received: by nanos.tec.linutronix.de (Postfix, from userid 1000) id 2D2B9101090; Fri, 5 Jun 2020 12:11:59 +0200 (CEST) From: Thomas Gleixner To: Paolo Bonzini , Miklos Szeredi Cc: kvm@vger.kernel.org, Vincenzo Frascino , Juergen Gross , linux-kernel@vger.kernel.org Subject: Re: system time goes weird in kvm guest after host suspend/resume In-Reply-To: <1a1c32fe-d124-0e47-c9e4-695be7ea7567@redhat.com> Date: Fri, 05 Jun 2020 12:11:59 +0200 Message-ID: <87k10l7rf4.fsf@nanos.tec.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Paolo Bonzini writes: > On 04/06/20 21:28, Miklos Szeredi wrote: >> time(2) returns good time, while clock_gettime(2) returns bad time. >> Here's an example: >> >> time=1591298725 RT=1591300383 MONO=39582 MONO_RAW=39582 BOOT=39582 >> time=1591298726 RT=1591300383 MONO=39582 MONO_RAW=39582 BOOT=39582 >> time=1591298727 RT=1591300383 MONO=39582 MONO_RAW=39582 BOOT=39582 >> time=1591298728 RT=1591300383 MONO=39582 MONO_RAW=39582 BOOT=39582 >> time=1591298729 RT=1591300383 MONO=39582 MONO_RAW=39582 BOOT=39582 >> >> As you can see, only time(2) is updated, the others remain the same. >> date(1) uses clock_gettime(CLOCK_REALTIME) so that shows the bad date. >> >> When the correct time reaches the value returned by CLOCK_REALTIME, >> the value jumps exactly 2199 seconds. Which value jumps? > clockid_to_kclock(CLOCK_REALTIME) is &clock_realtime, so clock_gettime > calls ktime_get_real_ts64, which is: > > > do { > seq = read_seqcount_begin(&tk_core.seq); > > ts->tv_sec = tk->xtime_sec; > nsecs = timekeeping_get_ns(&tk->tkr_mono); > > } while (read_seqcount_retry(&tk_core.seq, seq)); > > ts->tv_nsec = 0; > timespec64_add_ns(ts, nsecs); > > time(2) instead should actually be gettimeofday(2), which just returns > tk->xtime_sec. time(2) is either handled in the VDSO or it is handled via syscall and yes, it's only looking at the xtime_sec value. gettimeofday(2) returns seconds and microseconds. It's using the same mechanism as clock_gettime(2) and divides the nanoseconds part by 1000. > So the problem is the nanosecond part which is off by > 2199*10^9 nanoseconds, and that is suspiciously close to 2^31... Not really. It's 2^41. I can actually now reproduce, but I won't be able to investigate that before monday. Thanks, tglx