From patchwork Thu Dec 10 19:54:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Stultz X-Patchwork-Id: 58241 Delivered-To: patches@linaro.org Received: by 10.112.147.194 with SMTP id tm2csp710035lbb; Thu, 10 Dec 2015 11:54:09 -0800 (PST) X-Received: by 10.98.0.138 with SMTP id 132mr9507606pfa.131.1449777249445; Thu, 10 Dec 2015 11:54:09 -0800 (PST) Return-Path: Received: from mail-pf0-x233.google.com (mail-pf0-x233.google.com. [2607:f8b0:400e:c00::233]) by mx.google.com with ESMTPS id 1si22102242pfl.109.2015.12.10.11.54.09 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 Dec 2015 11:54:09 -0800 (PST) Received-SPF: pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::233 as permitted sender) client-ip=2607:f8b0:400e:c00::233; Authentication-Results: mx.google.com; spf=pass (google.com: domain of john.stultz@linaro.org designates 2607:f8b0:400e:c00::233 as permitted sender) smtp.mailfrom=john.stultz@linaro.org; dkim=pass header.i=@linaro-org.20150623.gappssmtp.com Received: by pfbu66 with SMTP id u66so9148196pfb.3 for ; Thu, 10 Dec 2015 11:54:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=j1mj9dP31ZVeQXp28vFzE5AVrY6HDKpzD8zO79S4iUA=; b=BiKVB8OGqFc4sbO+CyjIL8cRpbswRNG2U/BrKf7s0eYehJSpR8un93gA5M9qwKkp+a OuxJ0S1MczVu2cELKielUiCGBV+rXYtBbgYtzSGhNUsDRWVwQWXE9IHXRu+OzdwCPB3u PyqGibNfoWuYSQeOURP0fyS6Ou8rVASr7le9Z3v6rutTUOgUIqQCQ+Ms2toI3kX+L91P Zqs0p1Ee5seGD+pfMluvH9ypiyi3YoUFKlKx+80XU02JjuDS4icWk0VKG1EM1/cnsCot D/FzobH7EkDDqaN8U3ZbSQ6cd0Ruce564kNmzN72P6sWIGUS9WL6huPIkyqG0gwL7yGE ZKcA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=j1mj9dP31ZVeQXp28vFzE5AVrY6HDKpzD8zO79S4iUA=; b=b5nM/wG0i5ATiRTBa/gS4K+qZgCR8N6atJS9I/wjMXzg+kcXFoYmbjQvmLt1WAdbQy WKLf4kVd+QqtwBwRqsbAesPTmnfWKux3Et17FCN9Zjm0aN6criShJY5g8zw1Wi96SVib EM30/gg34fxzz64PeHNY3Z2dWWtQk1/EaJv0jOdK8II6gRg1dHNCt5Gj5IIN+6KQz26a 10g0q0bWTSQ6OBDKNiaTVkpEFOFiYV6P8sWMYG++A2jrMeHe6yCbHlw/7jpyFIKpgW/f rOXUjG+iZGs1UApd/GI0q9FUcbHWF23rp0y1PN/E0pPOHDFDtKQ/8PYyOcFcOX4I91jw OV4g== X-Gm-Message-State: ALoCoQkACIfReOmfQY2kpU4TPBu5Jp2qwwv3WKPb4s/ixm1/EHhO/z2R5jdAZNUbO7OOIagGhdF5UaX3WIAgaCilDNtgi257Tg== X-Received: by 10.98.42.209 with SMTP id q200mr9588501pfq.1.1449777249127; Thu, 10 Dec 2015 11:54:09 -0800 (PST) Return-Path: Received: from localhost.localdomain (c-76-115-103-22.hsd1.or.comcast.net. [76.115.103.22]) by smtp.gmail.com with ESMTPSA id r72sm20232111pfi.0.2015.12.10.11.54.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 10 Dec 2015 11:54:08 -0800 (PST) From: John Stultz To: lkml Cc: John Stultz , Miroslav Lichvar , Thomas Gleixner , Richard Cochran , Prarit Bhargava , Andy Lutomirski Subject: [RFC][PATCH v2] timekeeping: Cap adjustments so they don't exceed the maxadj value Date: Thu, 10 Dec 2015 11:54:01 -0800 Message-Id: <1449777241-22051-1-git-send-email-john.stultz@linaro.org> X-Mailer: git-send-email 1.9.1 Thus its been occasionally noted that users have seen confusing warnings like: Adjusting tsc more than 11% (5941981 vs 7759439) We try to limit the maximum total adjustment to 11% (10% tick adjustment + 0.5% frequency adjustment). But this is done by bounding the requested adjustment values, and the internal steering that is done by tracking the error from what was requested and what was applied, does not have any such limits. This is usually not problematic, but in some cases has a risk that an adjustment could cause the clocksource mult value to overflow, so its an indication things are outside of what is expected. It ends up most of the reports of this 11% warning are on systems using chrony, which utilizes the adjtimex() ADJ_TICK interface (which allows a +-10% adjustment). The original rational for ADJ_TICK unclear to me but my assumption it was originally added to allow broken systems to get a big constant correction at boot (see adjtimex userspace package for an example) which would allow the system to work w/ ntpd's 0.5% adjustment limit. Chrony uses ADJ_TICK to make very aggressive short term corrections (usually right at startup). Which push us close enough to the max bound that a few late ticks can cause the internal steering to push past the max adjust value (tripping the warning). Thus this patch adds some extra logic to enforce the max adjustment cap in the internal steering. Note: This has the potential to slow corrections when the ADJ_TICK value is furthest away from the default value. So it would be good to get some testing from folks using chrony, to make sure we don't cause any troubles there. Cc: Miroslav Lichvar Cc: Thomas Gleixner Cc: Richard Cochran Cc: Prarit Bhargava Cc: Andy Lutomirski Reported-by: Andy Lutomirski Signed-off-by: John Stultz --- v2: Catch single unit adjustment that was being made repeatedly to push us past the limit, as pointed out by Miroslav. kernel/time/timekeeping.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) -- 1.9.1 diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index d9249da..819e069 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1591,9 +1591,12 @@ static __always_inline void timekeeping_freqadjust(struct timekeeper *tk, { s64 interval = tk->cycle_interval; s64 xinterval = tk->xtime_interval; + u32 base = tk->tkr_mono.clock->mult; + u32 max = tk->tkr_mono.clock->maxadj; + u32 cur_adj = tk->tkr_mono.mult; s64 tick_error; bool negative; - u32 adj; + u32 adj_scale; /* Remove any current error adj from freq calculation */ if (tk->ntp_err_mult) @@ -1612,13 +1615,32 @@ static __always_inline void timekeeping_freqadjust(struct timekeeper *tk, /* preserve the direction of correction */ negative = (tick_error < 0); - /* Sort out the magnitude of the correction */ + /* If any adjustment would pass the max, just return */ + if (negative && (cur_adj - 1) < (base - max)) + return; + if (!negative && (cur_adj + 1) > (base + max)) + return; + /* + * Sort out the magnitude of the correction, but + * avoid making so large a correction that we go + * over the max adjustment. + */ + adj_scale = 0; tick_error = abs(tick_error); - for (adj = 0; tick_error > interval; adj++) + while (tick_error > interval) { + u32 adj = 1 << (adj_scale + 1); + + if (negative && (cur_adj - adj) < (base - max)) + break; + if (!negative && (cur_adj + adj) > (base + max)) + break; + + adj_scale++; tick_error >>= 1; + } /* scale the corrections */ - timekeeping_apply_adjustment(tk, offset, negative, adj); + timekeeping_apply_adjustment(tk, offset, negative, adj_scale); } /*