From patchwork Thu Dec 28 12:24:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Maimon X-Patchwork-Id: 183713 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:6f82:b0:100:9c79:88ff with SMTP id tb2csp1958099dyb; Thu, 28 Dec 2023 04:24:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IHtDO7e1XOB9MPL6cxFcwIie9mm3fh1Pqlz/ddkrFp9m93jnIn33a7XotPxQkTJye/uktlP X-Received: by 2002:a0c:f9c6:0:b0:67f:b7db:dd7 with SMTP id j6-20020a0cf9c6000000b0067fb7db0dd7mr10226874qvo.123.1703766287199; Thu, 28 Dec 2023 04:24:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703766287; cv=none; d=google.com; s=arc-20160816; b=dQ+/ZYytkJGaT71UzI+7lBVLh5wbGWlAbfiYC3ay1dpni8eLDmr52gD8su8ioOmKt6 hEufe7IJaYKI/b2glNHaXzMhfl4HpKmzdxBCAb0AkPrMk4lqqZ5FTVFhGFynWF0vzj6/ bpQyBrZMX+UHxJ8WuaKCMjiPiupmROBeTOF/Z7X2u/780kX/ATtg4wy7wYXG54/FUO4r twMpOz4dYR0YKBgSEWKikttV0mFaP+7TZbND5J/cDeJ5Vmaf6RRl3Ewm1wqf1xh5SFFR evpw7j509x4oriXsdnRvzsn26foo8pVacJ1bYsRe28n/dfnHUfUCE9P5rGir9BHKI50N tmRA== ARC-Message-Signature: i=1; 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:message-id:date:subject:cc:to :from:dkim-signature; bh=cwzrGyHtg55O3i+HEC4AZpo/IYoZaxO+3+s9kU5V6zs=; fh=1+c19CDm2H+z3qOlg51/bxwkAVvK6OLVKZM6oZMAgsk=; b=uis/pYpybsRwKBKza8xb6Vd4ffl9Onnj3qIHQVEhwKE5vuYscORsSm5K64jV0RdhkM XcQRhOeYXdUpRxtEGhJZW8oBJZ+NlgIDXUgfAzhgh09rip/D81cN0CAqPRbtHOxA61ol H0qgAoLcknvqw5snHJuuaTZStMlbL2Xz/pl1eWe1+0D2PDdHwBBa8zYzWk5lVhZgkdV5 avBG/99DNG0D8a3sA3xhb2/LrfPHcFykZZDZdE7yTGCfwMceN66Ga15v1MdC2iNjGORO 3ExSsNRpCxpHKRbvMYUxdh7wWisHFZhmB+1FE6f0MrIAyesQifK32k6t7qivgCGNCdkX Mx2A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=hRRuEtiC; spf=pass (google.com: domain of linux-kernel+bounces-12640-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12640-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id o13-20020a0ce40d000000b0067f3a061ad9si16801159qvl.609.2023.12.28.04.24.47 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Dec 2023 04:24:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-12640-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=hRRuEtiC; spf=pass (google.com: domain of linux-kernel+bounces-12640-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12640-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.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 ny.mirrors.kernel.org (Postfix) with ESMTPS id E96D41C21F18 for ; Thu, 28 Dec 2023 12:24:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 807CB8465; Thu, 28 Dec 2023 12:24:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="hRRuEtiC" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-yw1-f170.google.com (mail-yw1-f170.google.com [209.85.128.170]) (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 692397483; Thu, 28 Dec 2023 12:24:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yw1-f170.google.com with SMTP id 00721157ae682-5e76948cda7so40592957b3.3; Thu, 28 Dec 2023 04:24:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1703766260; x=1704371060; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=cwzrGyHtg55O3i+HEC4AZpo/IYoZaxO+3+s9kU5V6zs=; b=hRRuEtiClfgw8Nk6yd7wgH5ak9g4lrp428yZn1aAaAmW0FfIIh8u+vGokmO/8aLb21 1B+fcmgKbYmzm+1Lsk2T4axlPptEl4daJWqvmQMdJKfOeT0v/7OeQVeyw8lcmxdZt7+R DIh4MLY2cIen6kBvBs4cLRehgF6JZHI49fvE1u5CTy2LoP+fN0mCDsQQQhARqpClK4l1 FQErySXgHxg4bXv7vqmka7UKFldIs5DzLtR+u7VWTSWtCKsN+2wtzx3bzlE+Y3O0elxQ 2C+fVwTWVwNuqlzHt6QxXtmfVBeNEDP0no2/zFBzUXJ3JLg3CcwIJnHOFYsiQxntbwNz cn5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1703766260; x=1704371060; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=cwzrGyHtg55O3i+HEC4AZpo/IYoZaxO+3+s9kU5V6zs=; b=UfRX/P/P1MeeBhAnHPKpBTRouFIGBoqRe4QN/DaEau9RpXiwd0F0618wi12xaH3JBE FesWZRhhigy+sH3sDfr1e0dh0v4WuPTyU2CmuvIzPFQq4FvNIF9H7uIFHsQFgS0XSweB YE5nd+1KfuDMWnyS5/JqmNncoP16DgC0cOO7fbinPv+F5Rm3Zo6YtApga/KpDpm+GTpQ q1D1sbDB0X65a4ERDmmw42KJrvYEmVSUMg3CM36DLAQeZxdjdGaOOQA2r74xcI0cV2os hknvH3py77Q30hfDQES5qo9rbZs/GKlVQsVktLQsWlG/yuAHN03A+MbTgmkNdpWOwN5K SXXg== X-Gm-Message-State: AOJu0YzrLi90uLwzlxYwhZLYYKgQ//lc90wuh2nzfVDce0EDVa8XiviI Jd49gJko58ZrTk/XLP4QusU= X-Received: by 2002:a0d:e642:0:b0:5e7:3e3a:1a4f with SMTP id p63-20020a0de642000000b005e73e3a1a4fmr5590594ywe.17.1703766260252; Thu, 28 Dec 2023 04:24:20 -0800 (PST) Received: from ran.advaoptical.com ([82.166.23.19]) by smtp.gmail.com with ESMTPSA id u80-20020a0deb53000000b005e82879d18esm7474661ywe.53.2023.12.28.04.24.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Dec 2023 04:24:19 -0800 (PST) From: Sagi Maimon To: richardcochran@gmail.com, luto@kernel.org, datglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, arnd@arndb.de, geert@linux-m68k.org, peterz@infradead.org, hannes@cmpxchg.org, sohil.mehta@intel.com, rick.p.edgecombe@intel.com, nphamcs@gmail.com, palmer@sifive.com, maimon.sagi@gmail.com, keescook@chromium.org, legion@kernel.org, mark.rutland@arm.com Cc: linux-kernel@vger.kernel.org, linux-api@vger.kernel.org, linux-arch@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v3] posix-timers: add multi_clock_gettime system call Date: Thu, 28 Dec 2023 14:24:11 +0200 Message-Id: <20231228122411.3189-1-maimon.sagi@gmail.com> X-Mailer: git-send-email 2.26.3 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1786528438161608706 X-GMAIL-MSGID: 1786528438161608706 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 --- Addressed comments from: - Thomas Gleixner : https://www.spinics.net/lists/netdev/msg959514.html Changes since version 2: - Change multi PHC syscall to fit the Y2038 safe variant. - Define the syscall data structure under uapi directory, so it will be known in user space. arch/x86/entry/syscalls/syscall_64.tbl | 1 + include/linux/syscalls.h | 2 +- include/uapi/asm-generic/unistd.h | 4 +++- include/uapi/linux/multi_clock.h | 21 ++++++++++++++++ kernel/time/posix-timers.c | 33 ++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 include/uapi/linux/multi_clock.h diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl index 8cb8bf68721c..9cdeb0bf49db 100644 --- a/arch/x86/entry/syscalls/syscall_64.tbl +++ b/arch/x86/entry/syscalls/syscall_64.tbl @@ -378,6 +378,7 @@ 454 common futex_wake sys_futex_wake 455 common futex_wait sys_futex_wait 456 common futex_requeue sys_futex_requeue +457 common multi_clock_gettime sys_multi_clock_gettime # # Due to a historical design error, certain syscalls are numbered differently diff --git a/include/linux/syscalls.h b/include/linux/syscalls.h index fd9d12de7e92..b59fa776ab76 100644 --- a/include/linux/syscalls.h +++ b/include/linux/syscalls.h @@ -1161,7 +1161,7 @@ asmlinkage long sys_mmap_pgoff(unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, unsigned long fd, unsigned long pgoff); asmlinkage long sys_old_mmap(struct mmap_arg_struct __user *arg); - +asmlinkage long sys_multi_clock_gettime(struct __ptp_multi_clock_get __user * ptp_multi_clk_get); /* * Not a real system call, but a placeholder for syscalls which are diff --git a/include/uapi/asm-generic/unistd.h b/include/uapi/asm-generic/unistd.h index 756b013fb832..beb3e0052d3c 100644 --- 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 /* * 32 bit systems traditionally used different diff --git a/include/uapi/linux/multi_clock.h b/include/uapi/linux/multi_clock.h new file mode 100644 index 000000000000..07099045ec32 --- /dev/null +++ b/include/uapi/linux/multi_clock.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _UAPI_MULTI_CLOCK_H +#define _UAPI_MULTI_CLOCK_H + +#include +#include + +#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]; +}; + +#endif /* _UAPI_MULTI_CLOCK_H */ diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index b924f0f096fa..80cbce59d4f4 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -31,6 +31,7 @@ #include #include #include +#include #include "timekeeping.h" #include "posix-timers.h" @@ -1426,6 +1427,38 @@ SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags, #endif +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; + + if (multi_clk_get.n_samples > MULTI_PTP_MAX_SAMPLES) + return -EINVAL; + if (multi_clk_get.n_clocks > MULTI_PTP_MAX_CLOCKS) + return -EINVAL; + + for (j = 0; j < multi_clk_get.n_samples; j++) { + 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; + } + } + + return error; +} + + static const struct k_clock clock_realtime = { .clock_getres = posix_get_hrtimer_res, .clock_get_timespec = posix_get_realtime_timespec,