From: Sagi Maimon <maimon.sagi@gmail.com>
To: Arnd Bergmann <arnd@arndb.de>
Cc: Richard Cochran <richardcochran@gmail.com>,
Andy Lutomirski <luto@kernel.org>,
datglx@linutronix.de, Ingo Molnar <mingo@redhat.com>,
Borislav Petkov <bp@alien8.de>,
Dave Hansen <dave.hansen@linux.intel.com>,
x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>,
Geert Uytterhoeven <geert@linux-m68k.org>,
Peter Zijlstra <peterz@infradead.org>,
Johannes Weiner <hannes@cmpxchg.org>,
Sohil Mehta <sohil.mehta@intel.com>,
Rick Edgecombe <rick.p.edgecombe@intel.com>,
Nhat Pham <nphamcs@gmail.com>,
Palmer Dabbelt <palmer@sifive.com>,
Kees Cook <keescook@chromium.org>,
Alexey Gladkov <legion@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
linux-kernel@vger.kernel.org, linux-api@vger.kernel.org,
Linux-Arch <linux-arch@vger.kernel.org>,
Netdev <netdev@vger.kernel.org>
Subject: Re: [PATCH v5] posix-timers: add multi_clock_gettime system call
Date: Wed, 3 Jan 2024 14:59:53 +0200 [thread overview]
Message-ID: <CAMuE1bEN61aFL7dMDqF-v1Htt0K37w7OVwmYNQuPt5QSWUphXQ@mail.gmail.com> (raw)
In-Reply-To: <86fcb951-67e0-4f1d-a441-f3b4bcce8210@app.fastmail.com>
On Tue, Jan 2, 2024 at 12:22 PM Arnd Bergmann <arnd@arndb.de> wrote:
>
> On Tue, Jan 2, 2024, at 10:18, 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 <maimon.sagi@gmail.com>
> > ---
> > Changes since version 4:
> > - fix error : 'struct __ptp_multi_clock_get' declared inside parameter list
> > will not be visible outside of this definition or declaration
>
> I usually put all the changes for previous versions in a
> list here, it helps reviewers.
>
Will be done on patch V6.
> The changes you made for previous versions all look good
> to me, but I think there is still a few things worth
> considering. I'll also follow up on the earlier threads.
>
> > +#define MULTI_PTP_MAX_CLOCKS 32 /* Max number of clocks */
> > +#define MULTI_PTP_MAX_SAMPLES 32 /* 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];
> > +};
>
> Since you now access each member individually, I think it
> makes more sense here to just pass these as four
> register arguments. It helps with argument introspection,
> avoids a couple of get_user(), and lets you remove the fixed
> array dimensions.
>
I prefer the use of get_user(), I will use it to remove the fixed
array dimensions.
which will be done on patch V6.
> > +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 timespec64 *kernel_tp_base;
> > + unsigned int n_clocks; /* Desired number of clocks. */
> > + unsigned int n_samples; /* Desired number of measurements per clock.
> > */
> > + unsigned int i, j;
> > + clockid_t clkid_arr[MULTI_PTP_MAX_CLOCKS]; /* list of clock IDs */
> > + int error = 0;
> > +
> > + if (copy_from_user(&n_clocks, &ptp_multi_clk_get->n_clocks,
> > sizeof(n_clocks)))
> > + return -EFAULT;
> > + if (copy_from_user(&n_samples, &ptp_multi_clk_get->n_samples,
> > sizeof(n_samples)))
>
> If these remain as struct members rather than register arguments,
> you should use get_user() instead of copy_from_user().
>
Will be done on patch V6
> > + kernel_tp_base = kmalloc_array(n_clocks * n_samples,
> > + sizeof(struct timespec64), GFP_KERNEL);
> > + if (!kernel_tp_base)
> > + return -ENOMEM;
>
> To be on the safe side regarding possible data leak, maybe use
> kcalloc() instead of kmalloc_array() here.
>
Will be done on patch V6.
> > + kernel_tp = kernel_tp_base;
> > + for (j = 0; j < n_samples; j++) {
> > + for (i = 0; i < n_clocks; i++) {
> > + if (put_timespec64(kernel_tp++, (struct __kernel_timespec __user *)
> > + &ptp_multi_clk_get->ts[j][i])) {
>
> I think the typecast here can be removed.
>
You are right, will be fixed on patch V6.
> Arnd
Thanks for your Notes.
next prev parent reply other threads:[~2024-01-03 13:00 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-02 9:18 [PATCH v5] posix-timers: add multi_clock_gettime system call Sagi Maimon
2024-01-02 10:21 ` Arnd Bergmann
2024-01-03 12:59 ` Sagi Maimon [this message]
2024-01-04 3:06 ` kernel test robot
2024-01-08 11:09 ` Thomas Gleixner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAMuE1bEN61aFL7dMDqF-v1Htt0K37w7OVwmYNQuPt5QSWUphXQ@mail.gmail.com \
--to=maimon.sagi@gmail.com \
--cc=arnd@arndb.de \
--cc=bp@alien8.de \
--cc=datglx@linutronix.de \
--cc=dave.hansen@linux.intel.com \
--cc=geert@linux-m68k.org \
--cc=hannes@cmpxchg.org \
--cc=hpa@zytor.com \
--cc=keescook@chromium.org \
--cc=legion@kernel.org \
--cc=linux-api@vger.kernel.org \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=nphamcs@gmail.com \
--cc=palmer@sifive.com \
--cc=peterz@infradead.org \
--cc=richardcochran@gmail.com \
--cc=rick.p.edgecombe@intel.com \
--cc=sohil.mehta@intel.com \
--cc=x86@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).