Received: by 2002:ab2:6203:0:b0:1f5:f2ab:c469 with SMTP id o3csp2573895lqt; Mon, 22 Apr 2024 15:07:43 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUqdfLQpIQQBJ2SW8BulwjC5jSjmdN+oXJKIXiWAQ8AfS1kSVd8lj1YtPstSTb7+jtH2E54FjElvMiSWW84P6NPk9n1gBrFgBJVvesuXA== X-Google-Smtp-Source: AGHT+IHv1QZCICyKLM9eT8wyDpk1efJrXpd7f4S3OLB7hiKY4XC+SciKDqAJC6xbLEhgSMISwYj8 X-Received: by 2002:a17:903:32c5:b0:1e4:3535:142d with SMTP id i5-20020a17090332c500b001e43535142dmr17521727plr.34.1713823662912; Mon, 22 Apr 2024 15:07:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1713823662; cv=pass; d=google.com; s=arc-20160816; b=UNNOBHwaXL5zksh2UWYs+YpX73QXArqXXyPt+rTEn4A3UALULTIUj1zkZ0qtUmHCbh MbqdvFUr5N6rGuTLrcewH1fKZ4/gCpyH8Ya+iOCuvRNrUP8ngInEZEaMZJIxNmNp8u0z bQnNK19RMgXgmTQDVsjmVYSAa0lwC/0MQTJAciFtwknAFTkwuM3RewDmAOf51t+QPyVm XR7bh5vzKh4js6lY0nM4G8gvpc5BBdlMwKeaumrcKhyDbPBZCw7AcsrxvQhqeb2CLqeD Oh5AufPgiVkS1fPdJ0u9OCDrQ3iUJ73M69Xr+t1JcS2/deya1b+yV9VwHFCvYHHToXCH YMfA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:dkim-signature; bh=QhIuTeQ4b/V8Y8odbgjn2k8qbJdlep6wbUyfYlchvs4=; fh=axeYBHZ1wRb6B9I+kI4dwGldcv3RXTBse0lNgXH4PMQ=; b=eWjnpeMs1TocBDLxhgGg6p0M0ZUmOS6Bk0JjKS9e+Bz3IQz4KvAaX9Arog/lU2D3if HIl5E//yhmK0dS0E3LmGemztbA2wpnWfd7ZH+VKNF76zJGp2DhWnmFK6yngVYHUNg6Q9 ZaAMhkBRSyyRGshtFihUEh//QcqF99lgryY0QJ8nZDoLt80y4j/gjfxHlS/qRc6yczVK 0ZEE5psMdRAzItaPdIb+uC+MW6phnVyHVUrD3pN413UfSrMUwl+U04JeF1ruizXLFUNJ IFg40OsEyCpFbNsKQlHwA4Ygu24JLPUm1fUkY5S+65mjCHKXMdp8whk0ozkouAXGlDtH qUvw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=sCbuPCVZ; arc=pass (i=1 spf=pass spfdomain=flex--maheshb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-154070-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154070-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id x8-20020a1709029a4800b001e47d98b4e1si8372083plv.591.2024.04.22.15.07.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 22 Apr 2024 15:07:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-154070-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=sCbuPCVZ; arc=pass (i=1 spf=pass spfdomain=flex--maheshb.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-154070-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-154070-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com 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 B3F3D283697 for ; Mon, 22 Apr 2024 22:07:24 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5D35B1D551; Mon, 22 Apr 2024 22:07:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="sCbuPCVZ" Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D7184208D7 for ; Mon, 22 Apr 2024 22:07:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713823635; cv=none; b=DehePYfAinkbkLv+hqCxQDHGWZe9qbN4HXPNWfDK2tLEG5JatjI761D8T2szqkwT7XCz4GZEsWNlpuRZ89fGOeXp7EF+JcuBpvoVgNuvIw5q+gB1fF0QAfPpxIms58fohgpliIR0VzmNp2t8wti8rfNTY9NWB/0iFjVkbbUvgUQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713823635; c=relaxed/simple; bh=hgxYVB5gsQzHSWZ1yqm8jOmRL3pkwC76kyby2jLAmlM=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=rb7ZqReM3Z0lbCDqdrB+PHzTVpJSNk6eP7DYuLpMl9MChjxuibCr5UsOLFvfnTslAD3KDQTt/pklcCTHmU70IbG5yWXU6BAfS7VcIrDtE/Eluv3KhgNLPriR5nNhQ+7VDUz9UfljbFoVUt6Uq8ijb8Ws1idmpwo/fawunR9hNkE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--maheshb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=sCbuPCVZ; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--maheshb.bounces.google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-de45d8ec875so9852700276.3 for ; Mon, 22 Apr 2024 15:07:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1713823633; x=1714428433; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=QhIuTeQ4b/V8Y8odbgjn2k8qbJdlep6wbUyfYlchvs4=; b=sCbuPCVZBqjNDWUuszFdCMzUzuZ4u9pYrffZ6B1pgf6DqvUHse242ARuB+bKtOYJJu DG1Demmvh30/DH4XLFU/wNJfwBIsStCq6CPL96XZLM0Cqj8OFcDOnD7jWk7Xv8/DCpgv xJladbtb06pDRmShaBpjT1fKnfuLWHiN0AoO9P8B6TEotFkldrgoJ9lCxA20nGn+ak+n Cbi8zPqI5Ql6MyF+uy1fdJwX0lYKHF8b4knVT3BgfB56EhyVqOvrSN2LYI4Nan/08j/r RglOqzLhIm/+DjyMHNLoWWB1Wg6kP6+nuOQYMQ22KOeprN/LC/tf9VwIaXPjd6q7KqlH 4U6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1713823633; x=1714428433; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=QhIuTeQ4b/V8Y8odbgjn2k8qbJdlep6wbUyfYlchvs4=; b=YdGL44blqWMKrqGa18cb/wgiB6NOQz8xR+KaAXgXjbck6bTGVDF3JBdeLiHb3LCQsK bcaRjT1HmOmMUUP6/pc+jB0AsuLtJLzcouHUb7Lkz0+8JvlTGzN76JLa0Tkd+PuwJLfS fILvtuInB4SKfB0T75t3sEEW2FGcaDT4ORNVGKYs3fuQ9VB0olpPs1e5XfvuSvoUsh9O DfH2ROtP/Q1qthvkKEXULj1kxjaEWWk7ygtmAdPlmJDGIHMv1bQwKo/VjczKdGx1zhjR IYcs1U7FoDKBky5ocM3pD6O+oGCcFSwjfo6pMq4pLa1L6Ec+s/2mluWn2y6ujhIvLLmH Lubg== X-Forwarded-Encrypted: i=1; AJvYcCWayOshaGtHipiMbzTBJLHBzx6egie2rhI3GdnNVnSHKkm4xG6zydVfqhq8Cq0GR8rx+BbuxSuKQ/CfFvpcqFf6PHx1dlwbcqZeeKIs X-Gm-Message-State: AOJu0YzCPmuB61JkaqQc0Bjf2KMdHRtoXnIzPBBWHXGZ74wqPv7lLp9b D6QGGiA5EMLOreHJZbrfCUjd+O++XXg72f8w7V9i3p5I3ycxyB09VNOB5wkiRVvcHn4ZwrPYieg C4nLAcQ== X-Received: from coldfire.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:2b7a]) (user=maheshb job=sendgmr) by 2002:a05:6902:f88:b0:de5:3003:4b7b with SMTP id ft8-20020a0569020f8800b00de530034b7bmr1121893ybb.1.1713823632802; Mon, 22 Apr 2024 15:07:12 -0700 (PDT) Date: Mon, 22 Apr 2024 15:07:07 -0700 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.769.g3c40516874-goog Message-ID: <20240422220707.162146-1-maheshb@google.com> Subject: [PATCHv3 next] ptp/ioctl: support MONOTONIC_RAW timestamps for PTP_SYS_OFFSET_EXTENDED From: Mahesh Bandewar To: Netdev , Linux , David Miller , Jakub Kicinski , Eric Dumazet , Paolo Abeni , Thomas Gleixner , Richard Cochran , Arnd Bergmann , Sagi Maimon Cc: Jonathan Corbet , John Stultz , Mahesh Bandewar , Mahesh Bandewar Content-Type: text/plain; charset="UTF-8" The ability to read the PHC (Physical Hardware Clock) alongside multiple system clocks is currently dependent on the specific hardware architecture. This limitation restricts the use of PTP_SYS_OFFSET_PRECISE to certain hardware configurations. The generic soultion which would work across all architectures is to read the PHC along with the latency to perform PHC-read as offered by PTP_SYS_OFFSET_EXTENDED which provides pre and post timestamps. However, these timestamps are currently limited to the CLOCK_REALTIME timebase. Since CLOCK_REALTIME is affected by NTP (or similar time synchronization services), it can experience significant jumps forward or backward. This hinders the precise latency measurements that PTP_SYS_OFFSET_EXTENDED is designed to provide. This problem could be addressed by supporting MONOTONIC_RAW timestamps within PTP_SYS_OFFSET_EXTENDED. Unlike CLOCK_REALTIME or CLOCK_MONOTONIC, the MONOTONIC_RAW timebase is unaffected by NTP adjustments. This enhancement can be implemented by utilizing one of the three reserved words within the PTP_SYS_OFFSET_EXTENDED struct to pass the clock-id for timestamps. The current behavior aligns with clock-id for CLOCK_REALTIME timebase (value of 0), ensuring backward compatibility of the UAPI. Signed-off-by: Mahesh Bandewar --- v1 -> v2 * Code-style fixes v2 -> v3 * Reword commit log * Fix the compilation issue by using __kernel_clockid instead of clockid_t which has kernel only scope. drivers/ptp/ptp_chardev.c | 7 +++++-- include/linux/ptp_clock_kernel.h | 30 ++++++++++++++++++++++++++---- include/uapi/linux/ptp_clock.h | 26 ++++++++++++++++++++------ 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/drivers/ptp/ptp_chardev.c b/drivers/ptp/ptp_chardev.c index 7513018c9f9a..c109109c9e8e 100644 --- a/drivers/ptp/ptp_chardev.c +++ b/drivers/ptp/ptp_chardev.c @@ -358,11 +358,14 @@ long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd, extoff = NULL; break; } - if (extoff->n_samples > PTP_MAX_SAMPLES - || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) { + if (extoff->n_samples > PTP_MAX_SAMPLES || + extoff->rsv[0] || extoff->rsv[1] || + (extoff->clockid != CLOCK_REALTIME && + extoff->clockid != CLOCK_MONOTONIC_RAW)) { err = -EINVAL; break; } + sts.clockid = extoff->clockid; for (i = 0; i < extoff->n_samples; i++) { err = ptp->info->gettimex64(ptp->info, &ts, &sts); if (err) diff --git a/include/linux/ptp_clock_kernel.h b/include/linux/ptp_clock_kernel.h index 6e4b8206c7d0..74ded5f95d95 100644 --- a/include/linux/ptp_clock_kernel.h +++ b/include/linux/ptp_clock_kernel.h @@ -47,10 +47,12 @@ struct system_device_crosststamp; * struct ptp_system_timestamp - system time corresponding to a PHC timestamp * @pre_ts: system timestamp before capturing PHC * @post_ts: system timestamp after capturing PHC + * @clockid: clock-base used for capturing the system timestamps */ struct ptp_system_timestamp { struct timespec64 pre_ts; struct timespec64 post_ts; + clockid_t clockid; }; /** @@ -457,14 +459,34 @@ static inline ktime_t ptp_convert_timestamp(const ktime_t *hwtstamp, static inline void ptp_read_system_prets(struct ptp_system_timestamp *sts) { - if (sts) - ktime_get_real_ts64(&sts->pre_ts); + if (sts) { + switch (sts->clockid) { + case CLOCK_REALTIME: + ktime_get_real_ts64(&sts->pre_ts); + break; + case CLOCK_MONOTONIC_RAW: + ktime_get_raw_ts64(&sts->pre_ts); + break; + default: + break; + } + } } static inline void ptp_read_system_postts(struct ptp_system_timestamp *sts) { - if (sts) - ktime_get_real_ts64(&sts->post_ts); + if (sts) { + switch (sts->clockid) { + case CLOCK_REALTIME: + ktime_get_real_ts64(&sts->post_ts); + break; + case CLOCK_MONOTONIC_RAW: + ktime_get_raw_ts64(&sts->post_ts); + break; + default: + break; + } + } } #endif diff --git a/include/uapi/linux/ptp_clock.h b/include/uapi/linux/ptp_clock.h index 053b40d642de..02684d7e00e6 100644 --- a/include/uapi/linux/ptp_clock.h +++ b/include/uapi/linux/ptp_clock.h @@ -155,13 +155,27 @@ struct ptp_sys_offset { struct ptp_clock_time ts[2 * PTP_MAX_SAMPLES + 1]; }; +/* + * ptp_sys_offset_extended - data structure for IOCTL operation + * PTP_SYS_OFFSET_EXTENDED + * + * @n_samples: Desired number of measurements. + * @clockid: clockid of a clock-base used for pre/post timestamps. + * @rsv: Reserved for future use. + * @ts: Array of samples in the form [pre-TS, PHC, post-TS]. The + * kernel provides @n_samples. + * + * History: + * v1: Initial implementation. + * + * v2: Use the first word of the reserved-field for @clockid. That's + * backword compatible since v1 expects @rsv[3] to be 0 while the + * clockid for CLOCK_REALTIME is '0'. + */ struct ptp_sys_offset_extended { - unsigned int n_samples; /* Desired number of measurements. */ - unsigned int rsv[3]; /* Reserved for future use. */ - /* - * Array of [system, phc, system] time stamps. The kernel will provide - * 3*n_samples time stamps. - */ + unsigned int n_samples; + __kernel_clockid_t clockid; + unsigned int rsv[2]; struct ptp_clock_time ts[PTP_MAX_SAMPLES][3]; }; -- 2.44.0.769.g3c40516874-goog