Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp4609585rdb; Fri, 29 Dec 2023 07:27:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IEbl2KZablQzIbvboXJVID1Hdg7caMm6Pt/9zC/3zUnBYZCGFiFB9SBNJ+moeYcU3rNbsCT X-Received: by 2002:ac8:7d51:0:b0:427:8c57:598d with SMTP id h17-20020ac87d51000000b004278c57598dmr15757145qtb.118.1703863646122; Fri, 29 Dec 2023 07:27:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703863646; cv=none; d=google.com; s=arc-20160816; b=abxv8YJLLG1CrQ9xRx9AQt+kUA5j3j/Zjh6uu4nCaDtbllpz0eCvZlNofXhL32YEwW qKMmvvLAkmGUlJAnoCJWiD+OYu8NWngzMpgbB+ywUJ0icJhBqyOtDc49AabJ58jX59gV CrIM2bKkmu5IUylZur9vMmV2QfCg8EqFtMRxIgDVex+1kFqTtEWLglcRiOPvnwMVTXwL nZ4S91K6PPZLgN3x5AgUJq8eCXiPIwwwEGYqVU/qlpXm7fg8yR2cmLzc+iqXUhauRlEy 6dCg+98u1nlYZFRYGcIDbDQI5W9FPflvJJ2BcoIILKreOsI4jSs6LaJ8GEOKNzWmSzti 58uA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=subject:cc:to:from:date:references:in-reply-to:message-id :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :user-agent:feedback-id:dkim-signature:dkim-signature; bh=a4DE6lOAy5UJ7XbWIZJKIkOz1MpDU+Xi8bnPQlzva/A=; fh=Zvy045mx5F3pv11QQWwZkM1eFIG+f69MK6RoNtDVzxE=; b=XwnGhPQHmBqnIYX8A/plkjPz/3pBkdse93zG5WWYt0gwWKHktvYsHT4Ebncj+YNeR3 +NgkA959DrO/CosCZTv6O6XFx2TbsDTvmAH568h3qMKWH5RfkHQRT5m4V4YtX9He+I3Q /m852yTWvACaJvSJBk5W9RtztKRApLs0PYA/DA5iaTYKQW3GGfsepEEeA7jRx1mnRSvk DTrqilmB9mOSdtOZUlOJuErK/1iqpOrlumU8e9NSZV7zNDKZcShulZZ3E+EsL08jq4X5 /CCOe7kOvivvtuIeeIq31OtVlt+g4gjxb6y+xJVgn8kI9b1GFBp2rIkaXxq3GPfeEsOu Cuuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@arndb.de header.s=fm1 header.b=VJk4pzLZ; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=HSB2MU1I; spf=pass (google.com: domain of linux-kernel+bounces-13139-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13139-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=arndb.de Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id y7-20020ac85f47000000b00427f407a6e0si4592644qta.413.2023.12.29.07.27.25 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Dec 2023 07:27:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-13139-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@arndb.de header.s=fm1 header.b=VJk4pzLZ; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=HSB2MU1I; spf=pass (google.com: domain of linux-kernel+bounces-13139-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-13139-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=arndb.de 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 ny.mirrors.kernel.org (Postfix) with ESMTPS id B39AD1C21C73 for ; Fri, 29 Dec 2023 15:27:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1B66E125C1; Fri, 29 Dec 2023 15:27:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=arndb.de header.i=@arndb.de header.b="VJk4pzLZ"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="HSB2MU1I" X-Original-To: linux-kernel@vger.kernel.org Received: from wout2-smtp.messagingengine.com (wout2-smtp.messagingengine.com [64.147.123.25]) (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 841C6125AD; Fri, 29 Dec 2023 15:27:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arndb.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arndb.de Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 9C1AB3200906; Fri, 29 Dec 2023 10:27:08 -0500 (EST) Received: from imap51 ([10.202.2.101]) by compute5.internal (MEProxy); Fri, 29 Dec 2023 10:27:10 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=arndb.de; h=cc :cc:content-type:content-type:date:date:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:subject :subject:to:to; s=fm1; t=1703863628; x=1703950028; bh=a4DE6lOAy5 UJ7XbWIZJKIkOz1MpDU+Xi8bnPQlzva/A=; b=VJk4pzLZ3jsN5yrTVY9rB5MqEO t63AaOm0BTOpekd7Yk9s0B23makvF6HrsB4oPnU8/WWXGGAMD0p5CIyhVm9k1U7t HJl1Cj+4svu7FSbXuWjdoasn6RgjQtlQyetnuHffeKCOYSj6euS+Ya4YFSOm8FWS gyHJ3kU0KM/WpQCA40aPPmqCO2sVR9BN2UOB4AOM4Vd7Q+JWo69W9J9XyBv8PPzA EFG5EfJEluh2mmHqyUOlQ3G86j5AXK4JA3rL3cxx0MYvicCjCsMJEHOKCkBgv73K rtjsl8vy3GVFwgUidTs14P+JKKZOFI4jpDAw1UK5vKlZRsQUeuCYirsuqg/Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1703863628; x=1703950028; bh=a4DE6lOAy5UJ7XbWIZJKIkOz1MpD U+Xi8bnPQlzva/A=; b=HSB2MU1Ivw5qTw3eOQn25v23N3Fs3zRMA+fDYx66Q7Td 4+rSDlbfUNarkcSpiDlIy5VRZfu3JwxKZOn9KYWGg1JcvAGXSjAzyZ1DPIXloJ2p d9h8ZtuPT9DHzjJyZ4UqEM8rioa75rCtVpXBrYkzkUqbPRXXktxzizocUkqwcPnO iebyH6O3t2o3UNw/PMpq2FyiaUpcBTCN9SkfvNrgVZG5xLiotkfgGEuZh70INKP0 0DEPJGuqQbSLKWJs+zBXQxZ4aBiUeCaBPGTG38yXoIhyUZXwXxAE6KxwtVnVUQzm DArXHKHQrcTgfcJFoOmevU1NDlFYK1m4dXQYFYUMGQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdeffedgjeejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvfevufgtsehttdertderredtnecuhfhrohhmpedftehr nhguuceuvghrghhmrghnnhdfuceorghrnhgusegrrhhnuggsrdguvgeqnecuggftrfgrth htvghrnhepffehueegteeihfegtefhjefgtdeugfegjeelheejueethfefgeeghfektdek teffnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprg hrnhgusegrrhhnuggsrdguvg X-ME-Proxy: Feedback-ID: i56a14606:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id A40F6B6008F; Fri, 29 Dec 2023 10:27:06 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-1364-ga51d5fd3b7-fm-20231219.001-ga51d5fd3 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: In-Reply-To: <20231228122411.3189-1-maimon.sagi@gmail.com> References: <20231228122411.3189-1-maimon.sagi@gmail.com> Date: Fri, 29 Dec 2023 16:26:46 +0100 From: "Arnd Bergmann" To: "Sagi Maimon" , "Richard Cochran" , "Andy Lutomirski" , datglx@linutronix.de, "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , x86@kernel.org, "H. Peter Anvin" , "Geert Uytterhoeven" , "Peter Zijlstra" , "Johannes Weiner" , "Sohil Mehta" , "Rick Edgecombe" , "Nhat Pham" , "Palmer Dabbelt" , "Kees Cook" , "Alexey Gladkov" , "Mark Rutland" Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, Linux-Arch , Netdev Subject: Re: [PATCH v3] posix-timers: add multi_clock_gettime system call Content-Type: text/plain On Thu, Dec 28, 2023, at 13:24, Sagi Maimon wrote: > Some user space applications need to read some clocks. > Each read requires moving from user space to kernel space. > The syscall overhead causes unpredictable delay between N clocks reads > Removing this delay causes better synchronization between N clocks. > > Introduce a new system call multi_clock_gettime, which can be used to measure > the offset between multiple clocks, from variety of types: PHC, virtual PHC > and various system clocks (CLOCK_REALTIME, CLOCK_MONOTONIC, etc). > The offset includes the total time that the driver needs to read the clock > timestamp. > > New system call allows the reading of a list of clocks - up to PTP_MAX_CLOCKS. > Supported clocks IDs: PHC, virtual PHC and various system clocks. > Up to PTP_MAX_SAMPLES times (per clock) in a single system call read. > The system call returns n_clocks timestamps for each measurement: > - clock 0 timestamp > - ... > - clock n timestamp > > Signed-off-by: Sagi Maimon Hi Sagi, Exposing an interface to read multiple clocks makes sense to me, but I wonder if the interface you use is too inflexible. > --- a/include/uapi/asm-generic/unistd.h > +++ b/include/uapi/asm-generic/unistd.h > @@ -828,9 +828,11 @@ __SYSCALL(__NR_futex_wake, sys_futex_wake) > __SYSCALL(__NR_futex_wait, sys_futex_wait) > #define __NR_futex_requeue 456 > __SYSCALL(__NR_futex_requeue, sys_futex_requeue) > +#define __NR_multi_clock_gettime 457 > +__SYSCALL(__NR_multi_clock_gettime, sys_multi_clock_gettime) > > #undef __NR_syscalls > -#define __NR_syscalls 457 > +#define __NR_syscalls 458 Site note: hooking it up only here is sufficient for the code review but not for inclusion: once we have an agreement on the API, this should be added to all architectures at once. > +#define MULTI_PTP_MAX_CLOCKS 5 /* Max number of clocks */ > +#define MULTI_PTP_MAX_SAMPLES 10 /* Max allowed offset measurement samples. */ > + > +struct __ptp_multi_clock_get { > + unsigned int n_clocks; /* Desired number of clocks. */ > + unsigned int n_samples; /* Desired number of measurements per clock. */ > + clockid_t clkid_arr[MULTI_PTP_MAX_CLOCKS]; /* list of clock IDs */ > + /* > + * Array of list of n_clocks clocks time samples n_samples times. > + */ > + struct __kernel_timespec ts[MULTI_PTP_MAX_SAMPLES][MULTI_PTP_MAX_CLOCKS]; > +}; The fixed size arrays here seem to be an unnecessary limitation, both MULTI_PTP_MAX_SAMPLES and MULTI_PTP_MAX_CLOCKS are small enough that one can come up with scenarios where you would want a higher number, but at the same time the structure is already 808 bytes long, which is more than you'd normally want to put on the kernel stack, and which may take a significant time to copy to and from userspace. Since n_clocks and n_samples are always inputs to the syscall, you can just pass them as register arguments and use a dynamically sized array instead. It's not clear to me what you gain from having the n_samples argument over just calling the syscall repeatedly. Does this offer a benefit for accuracy or is this just meant to avoid syscall overhead. > +SYSCALL_DEFINE1(multi_clock_gettime, struct __ptp_multi_clock_get > __user *, ptp_multi_clk_get) > +{ > + const struct k_clock *kc; > + struct timespec64 kernel_tp; > + struct __ptp_multi_clock_get multi_clk_get; > + unsigned int i, j; > + int error; > + > + if (copy_from_user(&multi_clk_get, ptp_multi_clk_get, > sizeof(multi_clk_get))) > + return -EFAULT; Here you copy the entire structure from userspace, but I don't actually see the .ts[] array on the stack being accessed later as you just copy to the user pointer directly. > + for (i = 0; i < multi_clk_get.n_clocks; i++) { > + kc = clockid_to_kclock(multi_clk_get.clkid_arr[i]); > + if (!kc) > + return -EINVAL; > + error = kc->clock_get_timespec(multi_clk_get.clkid_arr[i], > &kernel_tp); > + if (!error && put_timespec64(&kernel_tp, (struct __kernel_timespec > __user *) > + &ptp_multi_clk_get->ts[j][i])) > + error = -EFAULT; > + } The put_timespec64() and possibly the clockid_to_kclock() have some overhead that may introduce jitter, so it may be better to pull that out of the loop and have a fixed-size array of timespec64 values on the stack and then copy them at the end. On the other hand, this will still give less accuracy than the getcrosststamp() callback with ioctl(PTP_SYS_OFFSET_PRECISE), so either the last bit of accuracy isn't all that important, or you need to refine the interface to actually be an improvement over the chardev. Arnd