Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2555910lqp; Mon, 25 Mar 2024 02:27:47 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUxdZ+ub/Hrj2xo6M+0CoNX+0DXSk5KeA+Urzm8MFj1HfrIspW4ntkwAOb/6E5Jds0PucFp3tjYDeYeh2LTCCT5tYZtIDNFToXcrtoMXw== X-Google-Smtp-Source: AGHT+IHOcGXOXRSL9dw4b96b3DP9XxyzJ+ZMC+L51YTR3DmNwJDqYF574rJTWSgIEKbZifYXkM2+ X-Received: by 2002:a17:902:ea05:b0:1e0:a7b1:ee92 with SMTP id s5-20020a170902ea0500b001e0a7b1ee92mr6418534plg.62.1711358866825; Mon, 25 Mar 2024 02:27:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711358866; cv=pass; d=google.com; s=arc-20160816; b=CqB0gyO+yxsSArIBy1o4Rd8OsCCAlD2HTdmrJQM5rwqZuDDERKYFWJ58oJS5fnLziG y7ztBxo3dBrUgAt5L9AYvKgX4Gx4+gbHL2oXnXHQqJQxDDOc138aQF3ePWanHsHq+QPm 8nF3SPb4au2S3JMDjeI/kKAvdHhH0WzlAxkn8XbBV9AZmaoT8YUxlyWO/cxGX5NjyCGH 8E2v49VF7i+xYbeQt1+ei6GIBdcyu7hh5GEK3yNu9xoqi2vHTdr87P/3GYp9C8G5NUDu jv0kJn4F2o2zFodJX3003xr2pyl1/6hIL2zEuaDqoByGvB4V7ELS9Qfrq6Xxuk+oxNo2 uBWw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=lBGjsMkhksmzBZI40B0/3Li9r19VzRSgbgFLBr0iJfA=; fh=1CdkGrFYXvsM0P1aYcpwJYMMBw+K7u125ibfbL7YWtU=; b=uBuV+oQl8XaanDdBwBAITIdVzRJqVu3yekwwocEJSuE6pnQR5nMAF1mtT6iIAMZPAb LlxZ7VwFPE8+J8jkl3rF/GSObuELic23Haurm0JtZ6HWRVwgOb+AKWYYgXSCJY3hwhW6 yvaKUSzJ6pXIrecHKOuzEHGBlUrjqcHQiBGcE6T1xYLca7k4A6ijYR4n+Q1Cn8KxVNjS SLOjLWjjzj7kSDgv8wD9H/mj1okZDhRciBWiiuqDEBZsM5IHiZzwoIzhpSoYrfotOnXC k6Rmtp2TRn/zVgQ1P+R2YKF29qFaRmVATE9mvCPRd8yszFKOMkeJjaBAoOvCntOIwUTI 2Tcg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WVfPP0Di; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-115816-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-115816-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id d15-20020a170902654f00b001e028766f04si4842581pln.537.2024.03.25.02.27.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 02:27:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-115816-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=WVfPP0Di; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-115816-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-115816-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 84E8129C379 for ; Mon, 25 Mar 2024 09:27:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B1F54210245; Mon, 25 Mar 2024 03:01:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="WVfPP0Di" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7EB0053393; Sun, 24 Mar 2024 23:13:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711322008; cv=none; b=NakidWAZSJITehE2+O/FhgWmBVm3SRjHpKXEWdhdt/R07kLg/GOvKD+2cSIxe66pCPPtVsk2+4JIXufdNFcmVyKKOKj/5NExifKGjC7dneMNhBHnoLjSRp/OhaUHAUrPl9W2ViouoP/giL0S0PXwp1kBR9LGIG+nMFx724eAT6o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711322008; c=relaxed/simple; bh=vyQTzW01LzpT77c/Erw5ECScfZD/VrCOI384JKt66sU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XqY2qrvcn2FZa0OsJSCf2pCdoRYrAor23FxT3DBzf/3mVa48vwZ5UXBCLWxa9JqU99UPKWNnM9dbaZv5MADDNRR9lb8gHE2uTKKiKxROisHO8aQzuZmAoZYBwRPTCsy2O294C9nBu8egLD+dqGNSv/Avcor1yz2cHJh2PkfDY8M= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=WVfPP0Di; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7E17C43390; Sun, 24 Mar 2024 23:13:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711322007; bh=vyQTzW01LzpT77c/Erw5ECScfZD/VrCOI384JKt66sU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WVfPP0DimzXG+SHu5UiB2TMvzZJpj55tSgY+U+TX3ATbF+a5Ehujt1NuNu+Ub5Tut 9jewRYCuTNXi48JrLJm/tfIxEddw5GT8aueZidfM+yCJIzCkBX1DKXo7i3FMkyr3Mm E2eQvXrIgEjFBDPyfdt+job2i2hBZ6csy6p1gLhfdehl+c/hWaFNfD/ekitBi1ys+8 TeFh0XK10LgRrzv2xyenm422qFNX5J6QYGCosvVlRgqeGPoi/85nptwG0Pc0Mb55PY tcDTBI86plnxL2P5YdM8HoeequSqm3he0ibsyd6KNy5wNeTe+SJgbWOCySOjvEAsbn T+dxy2zjpiQxw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Peter Hilber , Thomas Gleixner , Sasha Levin Subject: [PATCH 6.1 079/451] timekeeping: Fix cross-timestamp interpolation corner case decision Date: Sun, 24 Mar 2024 19:05:55 -0400 Message-ID: <20240324231207.1351418-80-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324231207.1351418-1-sashal@kernel.org> References: <20240324231207.1351418-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Peter Hilber [ Upstream commit 87a41130881995f82f7adbafbfeddaebfb35f0ef ] The cycle_between() helper checks if parameter test is in the open interval (before, after). Colloquially speaking, this also applies to the counter wrap-around special case before > after. get_device_system_crosststamp() currently uses cycle_between() at the first call site to decide whether to interpolate for older counter readings. get_device_system_crosststamp() has the following problem with cycle_between() testing against an open interval: Assume that, by chance, cycles == tk->tkr_mono.cycle_last (in the following, "cycle_last" for brevity). Then, cycle_between() at the first call site, with effective argument values cycle_between(cycle_last, cycles, now), returns false, enabling interpolation. During interpolation, get_device_system_crosststamp() will then call cycle_between() at the second call site (if a history_begin was supplied). The effective argument values are cycle_between(history_begin->cycles, cycles, cycles), since system_counterval.cycles == interval_start == cycles, per the assumption. Due to the test against the open interval, cycle_between() returns false again. This causes get_device_system_crosststamp() to return -EINVAL. This failure should be avoided, since get_device_system_crosststamp() works both when cycles follows cycle_last (no interpolation), and when cycles precedes cycle_last (interpolation). For the case cycles == cycle_last, interpolation is actually unneeded. Fix this by changing cycle_between() into timestamp_in_interval(), which now checks against the closed interval, rather than the open interval. This changes the get_device_system_crosststamp() behavior for three corner cases: 1. Bypass interpolation in the case cycles == tk->tkr_mono.cycle_last, fixing the problem described above. 2. At the first timestamp_in_interval() call site, cycles == now no longer causes failure. 3. At the second timestamp_in_interval() call site, history_begin->cycles == system_counterval.cycles no longer causes failure. adjust_historical_crosststamp() also works for this corner case, where partial_history_cycles == total_history_cycles. These behavioral changes should not cause any problems. Fixes: 2c756feb18d9 ("time: Add history to cross timestamp interface supporting slower devices") Signed-off-by: Peter Hilber Signed-off-by: Thomas Gleixner Link: https://lore.kernel.org/r/20231218073849.35294-3-peter.hilber@opensynergy.com Signed-off-by: Sasha Levin --- kernel/time/timekeeping.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index c168931c78e01..1749a712f72d1 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -1180,13 +1180,15 @@ static int adjust_historical_crosststamp(struct system_time_snapshot *history, } /* - * cycle_between - true if test occurs chronologically between before and after + * timestamp_in_interval - true if ts is chronologically in [start, end] + * + * True if ts occurs chronologically at or after start, and before or at end. */ -static bool cycle_between(u64 before, u64 test, u64 after) +static bool timestamp_in_interval(u64 start, u64 end, u64 ts) { - if (test > before && test < after) + if (ts >= start && ts <= end) return true; - if (before > after && (test > before || test < after)) + if (start > end && (ts >= start || ts <= end)) return true; return false; } @@ -1246,7 +1248,7 @@ int get_device_system_crosststamp(int (*get_time_fn) */ now = tk_clock_read(&tk->tkr_mono); interval_start = tk->tkr_mono.cycle_last; - if (!cycle_between(interval_start, cycles, now)) { + if (!timestamp_in_interval(interval_start, now, cycles)) { clock_was_set_seq = tk->clock_was_set_seq; cs_was_changed_seq = tk->cs_was_changed_seq; cycles = interval_start; @@ -1277,13 +1279,13 @@ int get_device_system_crosststamp(int (*get_time_fn) bool discontinuity; /* - * Check that the counter value occurs after the provided + * Check that the counter value is not before the provided * history reference and that the history doesn't cross a * clocksource change */ if (!history_begin || - !cycle_between(history_begin->cycles, - system_counterval.cycles, cycles) || + !timestamp_in_interval(history_begin->cycles, + cycles, system_counterval.cycles) || history_begin->cs_was_changed_seq != cs_was_changed_seq) return -EINVAL; partial_history_cycles = cycles - system_counterval.cycles; -- 2.43.0