From nobody Tue Apr 25 00:38:23 2023 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Q535C62Csz466XM; Tue, 25 Apr 2023 00:38:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Q535C48QWz4DhN; Tue, 25 Apr 2023 00:38:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682383103; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=p4sYI5iLewrzxrNKpqwWZY1byx0LPafxfDEUQkfqDKs=; b=HxgayMMBKyfYJerRHl2uLASPsAqaO7Tj9LqKcddlbA7wwtMY76hRDJdhjN5P/q6zMuEOlN tASEvnhDCi+K1hVSwkvKjd6Dp0RVmcO3ngwIu5UWSIWc5XrWjUYpEy+LsqWwy8+xjO60zy dxQFGlaqHrZot+GbeEsKrIbHXHdoGx3RKYzTX+2mazGDUNZztU7H/0KtHRHtcADztPb1DF nVVwPHIwuWNKJrNGNL72YA0IiHTyGDA5thwB/ojUJDjw6yTzAWZAasNNYQWqm3NkpTvMNM qRJvuKigvdf0nEtAD3o/tZGKDXVo9V0cGseO8cy4eZk+ZYi1t7GqdIdEwla5Pw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1682383103; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=p4sYI5iLewrzxrNKpqwWZY1byx0LPafxfDEUQkfqDKs=; b=JmL+06tUigZhyeaYh7pqw1fX4nSi6nn8ob+D/X52czt0et/5sGh+IniuukyHA387dXXwtF r7kqK8e4su4CgjPpJV5J4PaSDqumZs8/8KHPpvt8rzKLJY1zYTuoIcRIKEzdIRgsqzSHku ridekScheOvdg7+CK5OBu8lAHzJEqPLGSr9AO2AtXIsHSpkYHBOrT7rxLMMGC/f/NvSooZ SytaoFizP1Ba5buoJf8W4akv2g4Y+VwBjcBFtdh4bTp/eXm2C032v0r3peikZUoH8OHLxL T691+S12NakCcGFL4PTUvuLvZmAYVtndkzbEVWpVFblbZBbE8xaM91cI7GQATw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1682383103; a=rsa-sha256; cv=none; b=WVlqXWvFbh85jFZBJjSsbzGFtVNp5a/NfNQT5Hax/3o1SV6FeHdzRrN2jHY6I3Z9lvotW/ 9PLGfmAT0IAsmmArvLDQQBgeZ2AStYYjXTjQS8vQ2sQsOOdh4qmjd/wEe3eN3I53IS7AHf /bEH8In2g+uBQ1OeQDuAZG6sPyq8gIlVE7dMgsQTHi92z1lZmxMPGoLac9eAz0ezyxFXLk N++xULVV2MpOfg6djDZ/UFl4kejbxoPWV/zIx4a+0Sc6NGDa2HEGu1ikOk/m93WGNX7Sew SAH3CfHEd6pWdsPPt8C45MljwysKB8qzj1HExVGxEnXBycWr5Cfd0gXNCbNv1A== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Q535C3FDNzmMW; Tue, 25 Apr 2023 00:38:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 33P0cNTu057380; Tue, 25 Apr 2023 00:38:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 33P0cNBM057379; Tue, 25 Apr 2023 00:38:23 GMT (envelope-from git) Date: Tue, 25 Apr 2023 00:38:23 GMT Message-Id: <202304250038.33P0cNBM057379@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 68a69739cb90 - stable/13 - umtx: allow to configure minimal timeout (in nanoseconds) List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 68a69739cb902d083aacdb0974b9b957e0e1353b Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=68a69739cb902d083aacdb0974b9b957e0e1353b commit 68a69739cb902d083aacdb0974b9b957e0e1353b Author: Konstantin Belousov AuthorDate: 2023-04-15 17:46:12 +0000 Commit: Konstantin Belousov CommitDate: 2023-04-25 00:27:27 +0000 umtx: allow to configure minimal timeout (in nanoseconds) PR: 270785 (cherry picked from commit 93ca6ff2958cace3b7b883ed797ea6539881a6a2) --- lib/libc/sys/_umtx_op.2 | 11 +++++++++++ lib/libthr/libthr.3 | 6 ++++++ lib/libthr/thread/thr_init.c | 11 +++++++++++ sys/kern/kern_fork.c | 1 + sys/kern/kern_umtx.c | 45 ++++++++++++++++++++++++++++++++++++++++++++ sys/sys/proc.h | 1 + sys/sys/umtx.h | 2 ++ 7 files changed, 77 insertions(+) diff --git a/lib/libc/sys/_umtx_op.2 b/lib/libc/sys/_umtx_op.2 index ecd46b7d0247..7aa095e6532a 100644 --- a/lib/libc/sys/_umtx_op.2 +++ b/lib/libc/sys/_umtx_op.2 @@ -1271,6 +1271,17 @@ in which case list processing is continued. See .Sx ROBUST UMUTEXES subsection for details. +.It Dv UMTX_OP_GET_MIN_TIMEOUT +Writes out the current value of minimal umtx operations timeout, +in nanoseconds, into the long integer variable pointed to by +.Fa uaddr1 . +.It Dv UMTX_OP_SET_MIN_TIMEOUT +Set the minimal amount of time, in nanoseconds, the thread is required +to sleep for umtx operations specifying a timeout using absolute clocks. +The value is taken from the +.Fa val +argument of the call. +Zero means no minimum. .El .Pp The diff --git a/lib/libthr/libthr.3 b/lib/libthr/libthr.3 index 3018a6f20b86..0477ac0647a8 100644 --- a/lib/libthr/libthr.3 +++ b/lib/libthr/libthr.3 @@ -196,6 +196,12 @@ The integer value of the variable specifies how often blocked threads are inserted at the head of the sleep queue, instead of its tail. Bigger values reduce the frequency of the FIFO discipline. The value must be between 0 and 255. +.It Dv LIBPTHREAD_UMTX_MIN_TIMEOUT +The minimal amount of time, in nanoseconds, the thread is required to sleep +for pthread operations specifying a timeout. +If the operation requests a timeout less than the value provided, +it is silently increased to the value. +The value of zero means no minimum (default). .Pp .El The following diff --git a/lib/libthr/thread/thr_init.c b/lib/libthr/thread/thr_init.c index 1c4867cb8185..6010dd4c0451 100644 --- a/lib/libthr/thread/thr_init.c +++ b/lib/libthr/thread/thr_init.c @@ -522,6 +522,17 @@ init_private(void) if (env) _thr_queuefifo = atoi(env); TAILQ_INIT(&_thr_atfork_list); + env = getenv("LIBPTHREAD_UMTX_MIN_TIMEOUT"); + if (env) { + char *endptr; + long mint; + + mint = strtol(env, &endptr, 0); + if (*endptr == '\0' && mint >= 0) { + _umtx_op(NULL, UMTX_OP_SET_MIN_TIMEOUT, + mint, NULL, NULL); + } + } } init_once = 1; } diff --git a/sys/kern/kern_fork.c b/sys/kern/kern_fork.c index d6d90dd9534c..068dfdb097d3 100644 --- a/sys/kern/kern_fork.c +++ b/sys/kern/kern_fork.c @@ -381,6 +381,7 @@ do_fork(struct thread *td, struct fork_req *fr, struct proc *p2, struct thread * bcopy(&p1->p_startcopy, &p2->p_startcopy, __rangeof(struct proc, p_startcopy, p_endcopy)); pargs_hold(p2->p_args); + p2->p_umtx_min_timeout = p1->p_umtx_min_timeout; PROC_UNLOCK(p1); bzero(&p2->p_startzero, diff --git a/sys/kern/kern_umtx.c b/sys/kern/kern_umtx.c index dca7bc34700a..efe548a67bd1 100644 --- a/sys/kern/kern_umtx.c +++ b/sys/kern/kern_umtx.c @@ -701,6 +701,19 @@ umtx_abs_timeout_init2(struct umtx_abs_timeout *timo, (umtxtime->_flags & UMTX_ABSTIME) != 0, &umtxtime->_timeout); } +static void +umtx_abs_timeout_enforce_min(sbintime_t *sbt) +{ + sbintime_t when, mint; + + mint = curproc->p_umtx_min_timeout; + if (__predict_false(mint != 0)) { + when = sbinuptime() + mint; + if (*sbt < when) + *sbt = when; + } +} + static int umtx_abs_timeout_getsbt(struct umtx_abs_timeout *timo, sbintime_t *sbt, int *flags) @@ -740,6 +753,7 @@ umtx_abs_timeout_getsbt(struct umtx_abs_timeout *timo, sbintime_t *sbt, return (0); } *sbt = bttosbt(bt); + umtx_abs_timeout_enforce_min(sbt); /* * Check if the absolute time should be aligned to @@ -4595,6 +4609,33 @@ __umtx_op_robust_lists(struct thread *td, struct _umtx_op_args *uap, return (0); } +static int +__umtx_op_get_min_timeout(struct thread *td, struct _umtx_op_args *uap, + const struct umtx_copyops *ops) +{ + long val; + int error, val1; + + val = sbttons(td->td_proc->p_umtx_min_timeout); + if (ops->compat32) { + val1 = (int)val; + error = copyout(&val1, uap->uaddr1, sizeof(val1)); + } else { + error = copyout(&val, uap->uaddr1, sizeof(val)); + } + return (error); +} + +static int +__umtx_op_set_min_timeout(struct thread *td, struct _umtx_op_args *uap, + const struct umtx_copyops *ops) +{ + if (uap->val < 0) + return (EINVAL); + td->td_proc->p_umtx_min_timeout = nstosbt(uap->val); + return (0); +} + #if defined(__i386__) || defined(__amd64__) /* * Provide the standard 32-bit definitions for x86, since native/compat32 use a @@ -4817,6 +4858,8 @@ static const _umtx_op_func op_table[] = { [UMTX_OP_SEM2_WAKE] = __umtx_op_sem2_wake, [UMTX_OP_SHM] = __umtx_op_shm, [UMTX_OP_ROBUST_LISTS] = __umtx_op_robust_lists, + [UMTX_OP_GET_MIN_TIMEOUT] = __umtx_op_get_min_timeout, + [UMTX_OP_SET_MIN_TIMEOUT] = __umtx_op_set_min_timeout, }; static const struct umtx_copyops umtx_native_ops = { @@ -4991,6 +5034,8 @@ umtx_exec(struct proc *p) umtx_thread_cleanup(td); td->td_rb_list = td->td_rbp_list = td->td_rb_inact = 0; } + + p->p_umtx_min_timeout = 0; } /* diff --git a/sys/sys/proc.h b/sys/sys/proc.h index e3806a86c2a2..0f04921fffe6 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -729,6 +729,7 @@ struct proc { char *p_binname; /* (b) Binary hardlink name. */ void *p_elf_brandinfo; /* (x) Elf_Brandinfo, NULL for non ELF binaries. */ + sbintime_t p_umtx_min_timeout; }; #define p_session p_pgrp->pg_session diff --git a/sys/sys/umtx.h b/sys/sys/umtx.h index 82a8e0e4b0f0..ddd3bd387099 100644 --- a/sys/sys/umtx.h +++ b/sys/sys/umtx.h @@ -103,6 +103,8 @@ #define UMTX_OP_SEM2_WAKE 24 #define UMTX_OP_SHM 25 #define UMTX_OP_ROBUST_LISTS 26 +#define UMTX_OP_GET_MIN_TIMEOUT 27 +#define UMTX_OP_SET_MIN_TIMEOUT 28 /* * Flags for ops; the double-underbar convention must be maintained for future