From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.3 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE, SPF_PASS,USER_AGENT_SANE_1 autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F22EC56202 for ; Thu, 26 Nov 2020 11:34:01 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1E280207BC for ; Thu, 26 Nov 2020 11:34:01 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389262AbgKZLdp (ORCPT ); Thu, 26 Nov 2020 06:33:45 -0500 Received: from foss.arm.com ([217.140.110.172]:57076 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2389236AbgKZLdo (ORCPT ); Thu, 26 Nov 2020 06:33:44 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id BE0641478; Thu, 26 Nov 2020 03:33:43 -0800 (PST) Received: from [10.37.12.45] (unknown [10.37.12.45]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id C824B3F71F; Thu, 26 Nov 2020 03:33:41 -0800 (PST) Subject: Re: [PATCH 1/2] syscalls: avoid time() using __cvdso_gettimeofday in use-level's VDSO To: Thomas Gleixner , Cyril Hrubis , Li Wang Cc: ltp@lists.linux.it, Chunyu Hu , LKML , Peter Zijlstra , John Stultz , Arnd Bergmann , Andy Lutomirski , "Michael Kerrisk (man-pages)" , Carlos O'Donell References: <20201123083137.11575-1-liwang@redhat.com> <20201124153837.GA24470@yuki.lan> <875z5tllih.fsf@nanos.tec.linutronix.de> From: Vincenzo Frascino Message-ID: Date: Thu, 26 Nov 2020 11:36:54 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <875z5tllih.fsf@nanos.tec.linutronix.de> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Thomas. On 11/25/20 11:32 AM, Thomas Gleixner wrote: [...] >>> Here we propose to use '__NR_time' to invoke syscall directly that makes >>> test all get real seconds via ktime_get_real_second. > > This is a general problem and not really just for this particular test > case. > > Due to the internal implementation of ktime_get_real_seconds(), which is > a 2038 safe replacement for the former get_seconds() function, this > accumulation issue can be observed. (time(2) via syscall and newer > versions of VDSO use the same mechanism). > > clock_gettime(CLOCK_REALTIME, &ts); > sec = time(); > assert(sec >= ts.tv_sec); > > That assert can trigger for two reasons: > > 1) Clock was set between the clock_gettime() and time(). > > 2) The clock has advanced far enough that: > > timekeeper.tv_nsec + (clock_now_ns() - last_update_ns) > NSEC_PER_SEC > > #1 is just a property of clock REALTIME. There is nothing we can do > about that. > > #2 is due to the optimized get_seconds()/time() access which avoids to > read the clock. This can happen on bare metal as well, but is far > more likely to be exposed on virt. > > The same problem exists for CLOCK_XXX vs. CLOCK_XXX_COARSE > > clock_gettime(CLOCK_XXX, &ts); > clock_gettime(CLOCK_XXX_COARSE, &tc); > assert(tc.tv_sec >= ts.tv_sec); > > The _COARSE variants return their associated timekeeper.tv_sec,tv_nsec > pair without reading the clock. Same as #2 above just extended to clock > MONOTONIC. > > There is no way to fix this except giving up on the fast accessors and > make everything take the slow path and read the clock, which might make > a lot of people unhappy. > > For clock REALTIME #1 is anyway an issue, so I think documenting this > proper is the right thing to do. > > Thoughts? > I completely agree with your analysis and the fact that we should document this information. My proposal would be to use either the vDSO document present in the kernel [1] or the man pages for time(2) and clock_gettime(2). Probably the second would be more accessible to user space developers. [1] Documentation/ABI/stable/vdso > Thanks, > > tglx > -- Regards, Vincenzo From mboxrd@z Thu Jan 1 00:00:00 1970 From: Vincenzo Frascino Date: Thu, 26 Nov 2020 11:36:54 +0000 Subject: [LTP] [PATCH 1/2] syscalls: avoid time() using __cvdso_gettimeofday in use-level's VDSO In-Reply-To: <875z5tllih.fsf@nanos.tec.linutronix.de> References: <20201123083137.11575-1-liwang@redhat.com> <20201124153837.GA24470@yuki.lan> <875z5tllih.fsf@nanos.tec.linutronix.de> Message-ID: List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: ltp@lists.linux.it Hi Thomas. On 11/25/20 11:32 AM, Thomas Gleixner wrote: [...] >>> Here we propose to use '__NR_time' to invoke syscall directly that makes >>> test all get real seconds via ktime_get_real_second. > > This is a general problem and not really just for this particular test > case. > > Due to the internal implementation of ktime_get_real_seconds(), which is > a 2038 safe replacement for the former get_seconds() function, this > accumulation issue can be observed. (time(2) via syscall and newer > versions of VDSO use the same mechanism). > > clock_gettime(CLOCK_REALTIME, &ts); > sec = time(); > assert(sec >= ts.tv_sec); > > That assert can trigger for two reasons: > > 1) Clock was set between the clock_gettime() and time(). > > 2) The clock has advanced far enough that: > > timekeeper.tv_nsec + (clock_now_ns() - last_update_ns) > NSEC_PER_SEC > > #1 is just a property of clock REALTIME. There is nothing we can do > about that. > > #2 is due to the optimized get_seconds()/time() access which avoids to > read the clock. This can happen on bare metal as well, but is far > more likely to be exposed on virt. > > The same problem exists for CLOCK_XXX vs. CLOCK_XXX_COARSE > > clock_gettime(CLOCK_XXX, &ts); > clock_gettime(CLOCK_XXX_COARSE, &tc); > assert(tc.tv_sec >= ts.tv_sec); > > The _COARSE variants return their associated timekeeper.tv_sec,tv_nsec > pair without reading the clock. Same as #2 above just extended to clock > MONOTONIC. > > There is no way to fix this except giving up on the fast accessors and > make everything take the slow path and read the clock, which might make > a lot of people unhappy. > > For clock REALTIME #1 is anyway an issue, so I think documenting this > proper is the right thing to do. > > Thoughts? > I completely agree with your analysis and the fact that we should document this information. My proposal would be to use either the vDSO document present in the kernel [1] or the man pages for time(2) and clock_gettime(2). Probably the second would be more accessible to user space developers. [1] Documentation/ABI/stable/vdso > Thanks, > > tglx > -- Regards, Vincenzo