From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from pdx-out-006.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-006.esa.us-west-2.outbound.mail-perimeter.amazon.com [52.26.1.71]) (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 E9EC9330D24 for ; Mon, 25 May 2026 08:07:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.26.1.71 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779696435; cv=none; b=nKwM5IIxpYyht0ApcxeXijbvG1SyB4WWl1GeaP05bJf5zw+SAys38ormwudnTXtWV6I/VnGn8o3rEnf8fRQx+R6QEQZgN7zpC3pjGHdQZYrtXMruOXrJBXqQYNInsBp71b+qABzrYxfq9VQ8KFRAvqN8FKgw4PgMmFB19Uqku3o= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779696435; c=relaxed/simple; bh=eobZgO7sPnJ7nGJ68ch6/RDInmIfwJGqeT4pKYawKA0=; h=MIME-Version:Content-Type:Subject:From:To:CC:In-Reply-To: References:Date:Message-ID; b=Tk7WFhm/wr0iZaqDs8bCnLeGD/PHRqR5TFu/S0snQT2nRrIMpY+xodFdfdBy/8kToZS8OvVj0qg5tXGoKDcZay9Y5LCzynjY5HI/HVCS5VmpOV7X2y7a2XAuTmaB4fbUWTa3kxsUOyta5LvbUzRqVh08PfLUFPbo368hwrW2zx0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b=tw6X5dL0; arc=none smtp.client-ip=52.26.1.71 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.b="tw6X5dL0" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1779696434; x=1811232434; h=mime-version:content-transfer-encoding:subject:from:to: cc:in-reply-to:references:date:message-id; bh=/0fdQugC+yCghCKiayq4/9j+WzVtojeRnjX8IVNortA=; b=tw6X5dL0DMbDBNLTl/7eTXO3/UkkqSUKByETb8ADGyM9hwkSKmyJEYum 2KJSj9VhLKqjpfSlmfxVDSLOPjObLDUN3YbuRevTSPXHpAKXbBm4OpAGt GJ5S6PVe90ID3LUvJpd75tTlrrVssIhFX3slyZD9P2dmbi2fZhRx5NFb0 +hKMdaM81QCAx7m4+FtJVDtT17R/cwAlQNta85rWrGlfGonBH/ilg880p aGX1m8akly7r013I2qaZ6WOx8N2sUphw1oOKVE5XyNUnQqmjFOn2CYTMr Ise42lERCJ6+dlHsbVrMAJ53A3LZWHlcSeuBBC04knlsQ6aXEGaBz08/X g==; X-CSE-ConnectionGUID: fhUM6pAeQ/u+jgc4Bkw0tQ== X-CSE-MsgGUID: 6feAtpgIQoqJhvvNypch4Q== X-IronPort-AV: E=Sophos;i="6.24,167,1774310400"; d="scan'208";a="20424421" Received: from ip-10-5-12-219.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.12.219]) by internal-pdx-out-006.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 May 2026 08:07:11 +0000 Received: from EX19MTAUWA002.ant.amazon.com [205.251.233.234:31980] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.10.199:2525] with esmtp (Farcaster) id f795743f-03e1-499e-800b-fd51cef33c74; Mon, 25 May 2026 08:07:10 +0000 (UTC) X-Farcaster-Flow-ID: f795743f-03e1-499e-800b-fd51cef33c74 Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.37; Mon, 25 May 2026 08:07:10 +0000 Received: from dev-dsk-akiyano-1c-2138b29d.eu-west-1.amazon.com (172.19.83.6) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.37; Mon, 25 May 2026 08:07:07 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Subject: Re: [RFC PATCH v2 0/8] timekeeping: Fix draft tracking precision and add feed-forward discipline via vmclock From: Arthur Kiyanovski To: Thomas Gleixner CC: David Woodhouse , Miroslav Lichvar , Richard Cochran , Wen Gu , Andrew Lunn , "David S. Miller" , Eric Dumazet , "Jakub Kicinski" , Paolo Abeni , John Stultz , Stephen Boyd , Anna-Maria Behnsen , Frederic Weisbecker , "Shuah Khan" , Peter Zijlstra , =?utf-8?q?Thomas_Wei=C3=9Fschuh?= , "Arnd Bergmann" , Julien Ridoux , Ryan Luu , , Marcelo Tosatti In-Reply-To: <87se7ht25o.ffs@tglx> References: <20260517220326.4625-1-dwmw2@infradead.org> <0d32da75fa88c92ac0225ef23a9045afdf2ac9fe.camel@infradead.org> <87o6i8vcni.ffs@tglx> <6c7137748ccea288fdf55e7c2e24817ccc1673ef.camel@infradead.org> <874ijzvpl8.ffs@tglx> <00609fa255442648e11e2b5596a3b80a29edcfc8.camel@infradead.org> <87v7cftqey.ffs@tglx> <87se7ht25o.ffs@tglx> Date: Mon, 25 May 2026 08:06:05 +0000 Message-ID: <177969636528.22488.1740278196841124440.b4-reply@b4> X-Mailer: b4 0.15.2 X-ClientProxiedBy: EX19D040UWA003.ant.amazon.com (10.13.139.6) To EX19D001UWA001.ant.amazon.com (10.13.138.214) On 2026-05-24 14:36:35+02:00, Thomas Gleixner wrote: > On Fri, May 22 2026 at 17:23, David Woodhouse wrote: > > > On Fri, 2026-05-22 at 17:28 +0200, Thomas Gleixner wrote: > > > > Taking those in reverse order... yes, this means that with a new > > variant of PTP_SYS_OFFSET_EXTENDED, userspace can see actual counter > > values even for the system parts of those ABA timestamps, even for non- > > PTM clocks, and discipline the TSC/archcounter against the external > > clock. > > Correct. > > > Currently I have userspace which literally does rdtsc() either side of > > calling the ioctl :) > > Why am I not surprised? :) > > > And PTM devices can be used with PTP_SYS_OFFSET_PRECISE, which goes > > through get_device_system_crosststamp() as described, and all just > > works? It's just that we now allow userspace to *see* the counter value > > that the driver was already generating. > > A new variant of PRECISE > > > So to your questions: although there's new userspace ioctl support, the > > *drivers* don't need any modification for that (as long as they use the > > standard prets/postts helpers). > > Yes. > > > The remaining question is the device timestamp part (the 'B' in the ABA > > sandwich) for PTP_SYS_OFFSET_EXTENDED with PTM-capable drivers. Should > > that get a counterval? > > PTM-capable driver support cross timestamps, which will with a new > version of PTP_SYS_OFFSET_PRECISE expose the system counterval. No ABA > for that as it's hardware latched AB. > > > I don't have a strong opinion. On one hand we'd have to find a way to > > convert it from PTM for devices where it actually *is* PTM, and that's > > what PTP_SYS_OFFSET_PRECISE is *for*. > > Correct. > > > But on the other hand, can't the conversion be a whole lot simpler than > > get_device_system_crosststamp() because it's not actually dealing with > > any timekeepers; it's basically only invoking convert_base_to_cs()? > > But what for? If you have PTM, use PRECISE. There is _zero_ value of > having pre/post timestamps when the hardware already does the correlated > precise sampling, no? > > > And the ioctl should support it *all* but just have a clear way of > > indicating that any of the optional fields including the attrs are > > *not* populated (or use 0/max values maybe?). > > Yes. > > > So no, I don't think any driver *has* to add any attr support in order > > to expose counter values to userspace. The only reason I asked Arthur > > to mix those things up was for the *userspace* API, to avoid adding yet > > another ioctl over and over again. And now I feel bad for doing so :) > > I think you can create _one_ data structure variant, which fits both > EXTENDED_ATTR and PRECISE_ATTR: > > struct attrs { > u32 valid; > u32 error_bound; > .... > u32 reserved[N]; > }; > > @valid tells user space, which of the attributes has been filled in by > the driver. That avoids bounds based validity checks, which are a pain > as you might end up with different bounds for every attribute. Having a > valid flags field avoids that completely. > > struct devtime { > ptp_clock_time device_time; > struct attrs attrs; > }; > > struct systime { > u64 sys_systime; > u64 sys_rawtime; > u64 sys_counter; > u32 sys_counter_id; > u32 reserved; > }; > > Exposing @sys_counter_id requires to expose CSID_* in the user space ABI > reliably, as otherwise a kernel internal CSID enum change would blow up > the user space guess work. Your ptp_counter_id approach is error prone. > > struct timestamp { > union { > struct systime systime; > struct systime pre_systime; > }; > struct devtime devtime; > struct systime post_systime; > }; > > struct request { > u32 valid; > clockid_t clock_id; > unsigned int num_samples; > u32 reserved[N]; > }; > > I rather have @valid here too. The 'zero the reserved' members approach > is a pain as new kernels have to map 0 to default behavior instead of > being free to make 0 mean what they intend. @valid allows you to use > other sizes than u32 for future fields. All you have to take care of is > to keep the existing fields at the same place as before. > > struct ioctl_data { > struct request request; > struct timestamp timestamps[]; > }; > > So for both PTP_SYS_OFFSET_EXTENDED_ATTRS and > PTP_SYS_OFFSET_PRECISE_ATTRS user space allocates enough space to > accomodate data::request::num_samples. > > For PTP_SYS_OFFSET_PRECISE_ATTRS num_samples has to be 1 and > data::timestamps[0].post_systime is zeroed by the kernel because it has > no meaning. > > So now in the kernel you do: > > ptp_sys_offset_extended_attrs(struct ptp_clock *ptp, void __user *argptr) > { > struct ioctl_data __user *data = argptr; > struct request; > > if (copy_from_user(&request, &data->request, sizeof(request))) > return -EFAULT; > > if (!extattr_request_valid(request)) > return -EINVAL; > > for (unsigned int i; i < request.num_samples; i++) { > struct ptp_system_timestamp sts = { .clock_id = request.clock_id, }; > struct timestamp uts = { }; > struct timespec64 devts; > > if (ptp->info->gettimex64_attr) > ret = ptp->info->gettimex64_attr(ptp->info, &dev_ts, &sts, &uts.attr); > else if (ptp->info->gettimex64) > ret = ptp->info->gettimex64(ptp->info, &dev_ts, &sts); > else > return -ENOTSUPP; > > if (ret) > return ret; > > uts.pre_systime = mangle(sts.pre_systime); > uts.devtime.device_time = mangle(dev_ts); > uts.post_systime = mangle(sts.post_systime); > if (!copy_to_user(&data->timestamps[i], uts, sizeof(uts))) > return -EFAULT; > } > return 0; > } > > ptp_sys_offset_precise_attrs(struct ptp_clock *ptp, void __user *argptr) > { > struct ioctl_data __user *data = argptr; > struct request; > > if (copy_from_user(&request, &data->request, sizeof(request))) > return -EFAULT; > > if (!preciseattr_request_valid(request)) > return -EINVAL; > > struct system_device_crosststamp xtstamp = { .clock_id = request.clock_id, }; > struct timestamp uts = { }; > > if (ptp->info->getcrosststamp_attr) > ret = ptp->info->getcrosststamp_attr(ptp->info, &xtstamp, &uts.attr); > else if (ptp->info->getcrosststamp) > ret = ptp->info->getcrosststamp(ptp->info, &xtstamp); > else > return -ENOTSUPP; > > if (ret) > return ret; > > uts.systime = mangle(xtstamp.systime); > uts.devtime.device_time = mangle(xtstamp.device); > if (!copy_to_user(&data->timestamps[0], uts, sizeof(uts))) > return -EFAULT; > return 0; > } > > Or something like this, which immediately enables the functionality for > all drivers which implement the getcrosststamp() or the gettimex64() > callbacks with a unified user space data structure. > > The attributes.valid bits are all zero and and once drivers implement > the _attr callback variants, those attributes supported by the driver > will magically appear with the corresponding valid bits set. > > Hmm? > > Thanks, > > tglx Hi Thomas, David, Thanks for the layout proposal, Thomas. The unified structure with explicit valid flags is a much cleaner approach than bounds-based validation. I'm the author of the PHC timestamp attributes series [1] that this applies to. Before I spin v4 based on this design, I want to confirm three implementation details: 1. Counter IDs: No stable UAPI clocksource numbering exists today (enum clocksource_ids is kernel-internal). I'll define stable constants in include/uapi/linux/ptp_clock.h (e.g., PTP_CSID_X86_TSC, PTP_CSID_ARM_ARCH) and map internal IDs in the chardev layer. 2. Array sizing: The timestamps array will be fixed at PTP_MAX_SAMPLES (25) in the ioctl struct, not a flexible array, to keep copy_from_user/copy_to_user bounded. 3. Ioctl numbers: Two separate ioctls (PTP_SYS_OFFSET_EXTENDED_ATTRS and PTP_SYS_OFFSET_PRECISE_ATTRS) sharing the same payload struct, matching existing PTP convention. Arthur [1] https://lore.kernel.org/netdev/20260515164033.6403-1-akiyano@amazon.com/