Received: by 10.192.165.148 with SMTP id m20csp4719665imm; Tue, 24 Apr 2018 07:23:58 -0700 (PDT) X-Google-Smtp-Source: AIpwx48TIY7EdnMtt/sZ6mW4lfMyz9CkoQGk41oabxe+K2fUXPYiu8KSxsgVqx/chFveo1zBJlka X-Received: by 2002:a17:902:2947:: with SMTP id g65-v6mr22346003plb.346.1524579838940; Tue, 24 Apr 2018 07:23:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524579838; cv=none; d=google.com; s=arc-20160816; b=CKjTwn617BTrovO/5sTyhRMCDPEdVSqTdUkmEDHw0o+RsJ87Sjd81DOufULBgNqNSS cdYM9R5wSoeZOIQigThb+t9Tz+x3QxobcR1+ftd1jm9VDmPDawWqCIu50Onn159gwvcv k3E7S89DSJZvsLfIUH1G/upQC5u23OfQEIo2N4k02oAuLsMKidIXYFYoYctdrCjvTzhR b7EacIDYvL85UG2OG/YVHTHJgUFkcV+tirjWFveVqpZtjrGtvkGUyCvCVwkMyc17E7dI IGjBxerAkoQD8ZMJto2VJFcqnl5G5RPSUJJ2vVodmX95onS9yiT6EhZnF2CAtvwRMKs0 b7aQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date :arc-authentication-results; bh=dicVwhyxi2+9YDA3NKTmk/Z5rywordOishFPEBTW+Yg=; b=HPWSdRfoc8xx2xb2yF5r29B6Tk229I/4LaQDOLCUhb1h/Sd90VeOc2WZwgfpnfkZcn fw0nhQj+ovBnxcqSYKVgt/5I6SF4L6LamPC0QTeMN94syE1AbrgkwHsqrQU/473m3SS3 Ty7SmjMJLNQ5k7sXIdVcjSOvIJce/RA33PJO2CsAs0/lIS4v2HsYa5aLJaazVwGhjG5x dHK7mZlEYuzNL5eYdRebe2uUfbytqqXDM6guYKJN74e7WXFKzBWIrlEgrPAxQJ5+Xemk wk62Y6C1vCOBWzO5iuVk6pO5sv7F7wn4jS/SiHe5pGbzhyiJ2ulCBmrbZRpR1pXDgkF+ wD6g== 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 u8-v6si758711plz.392.2018.04.24.07.23.44; Tue, 24 Apr 2018 07:23:58 -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 S1758328AbeDXOVH (ORCPT + 99 others); Tue, 24 Apr 2018 10:21:07 -0400 Received: from mga03.intel.com ([134.134.136.65]:57480 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758278AbeDXOVG (ORCPT ); Tue, 24 Apr 2018 10:21:06 -0400 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Apr 2018 07:21:05 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,323,1520924400"; d="scan'208";a="44248494" Received: from stinkbox.fi.intel.com (HELO stinkbox) ([10.237.72.174]) by FMSMGA003.fm.intel.com with SMTP; 24 Apr 2018 07:21:02 -0700 Received: by stinkbox (sSMTP sendmail emulation); Tue, 24 Apr 2018 17:21:01 +0300 Date: Tue, 24 Apr 2018 17:21:01 +0300 From: Ville =?iso-8859-1?Q?Syrj=E4l=E4?= To: Imre Deak Cc: Thomas Gleixner , LKML , Peter Zijlstra , Mika Kuoppala , Chris Wilson Subject: Re: Early timeouts due to inaccurate jiffies during system suspend/resume Message-ID: <20180424142101.GO13908@intel.com> References: <20180419013200.wxkzqfdacfsijci5@ideak-desk.fi.intel.com> <20180423170128.mf7g26rniimm7asf@ideak-desk.fi.intel.com> <20180424140741.yxn5u6rdviblhtzx@ideak-desk.fi.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20180424140741.yxn5u6rdviblhtzx@ideak-desk.fi.intel.com> User-Agent: Mutt/1.9.4 (2018-02-28) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 24, 2018 at 05:07:41PM +0300, Imre Deak wrote: > On Mon, Apr 23, 2018 at 08:01:28PM +0300, Imre Deak wrote: > > On Thu, Apr 19, 2018 at 01:05:39PM +0200, Thomas Gleixner wrote: > > > On Thu, 19 Apr 2018, Imre Deak wrote: > > > > Hi, > > > > > > > > while checking bug [1], I noticed that jiffies based timing loops like > > > > > > > > expire = jiffies + timeout + 1; > > > > while (!time_after(jiffies, expire)) > > > > do_something; > > > > > > > > can last shorter than expected (that is less than timeout). > > > > > > Yes, that can happen when the timer interrupt is delayed long enough for > > > whatever reason. If you need accurate timing then you need to use > > > ktime_get(). > > > > Thanks. I always regarded jiffies as non-accurate, but something that > > gives a minimum time delay guarantee (when adjusted by +1 as above). I > > wonder if there are other callers in kernel that don't expect an early > > timeout. > > msleep and any other schedule_timeout based waits are also affected. At the > same time for example msleep's documentation says: > "msleep - sleep safely even with waitqueue interruptions". > > To me that suggests a wait with a minimum guaranteed delay. > > Ville had an idea to make the behavior more deterministic by clamping > the jiffies increment to 1 for each timer interrupt. Would that work? Another observation is that this is basically the same problem that we had with the drm vblank counter. I solved that by introducing drm_accurate_vblank_count() which makes sure the counter is up to date before sampling it. Then we can safely do stuff like: count = drm_accurate_vblank_count(); while (drm_vblank_count() == count) ...; As long as we don't lose all vblank interrupts that will work and never complete prematurely. And we still allow the vblank counter to increment by >1. I suppose doing something similar for jiffies would be nice as well, but I'm not sure how feasible that would be. At the very least it would involve patching a lot of code. > > > > > We switched now to using ktime_get_raw() in the i915 driver. > > > > > > > > > After some ftracing it seems like jiffies gets stale due to a missed > > > > LAPIC timer interrupt after the interrupt is armed in > > > > lapic_next_deadline() and before jiffies is sampled at 2. above. > > > > Eventually the interrupt does get delivered, at which point jiffies gets > > > > updated via tick_do_update_jiffies64() with a >1 ticks increment. > > > > Between lapic_next_deadline() and the - late - delivery of the interrupt > > > > the CPU on which the interrupt is armed doesn't go idle. > > > > > > That's odd. I have no real explanation for that. > > > > Looks like the reason is IRQ latency. For reference here are the > > longest ones I found with irqsoff ftracing, all running with IRQs disabled > > during system resume: > > > > hpet_rtc_interrupt()->hpet_rtc_timer_reinit(): > > do { ... } while(!hpet_cnt_ahead(...)); > > takes sometimes up to ~40msec for me. > > > > hpet_rtc_interrupt()->mc146818_get_time(): > > if (mc146818_is_updating()) mdelay(20); > > > > driver_probe_device->atkbd_connect()->i8042_port_close()->__i8042_command()->i8042_wait_write(): > > takes sometimes up to ~10msec for me. > > > > All the above paired with asynchronous calling of the drivers' resume > > hooks may result in the jumps in jiffies I saw. > > > > --Imre -- Ville Syrj?l? Intel