git: d89523644308 - stable/13 - libprocstat kstack: fix race with thread creation

From: Eric van Gyzen <vangyzen_at_FreeBSD.org>
Date: Wed, 02 Mar 2022 21:57:16 UTC
The branch stable/13 has been updated by vangyzen:

URL: https://cgit.FreeBSD.org/src/commit/?id=d89523644308afd24d47ca3dcf4098d0a719e919

commit d89523644308afd24d47ca3dcf4098d0a719e919
Author:     Eric van Gyzen <vangyzen@FreeBSD.org>
AuthorDate: 2021-05-27 16:33:22 +0000
Commit:     Eric van Gyzen <vangyzen@FreeBSD.org>
CommitDate: 2022-03-02 21:56:30 +0000

    libprocstat kstack: fix race with thread creation
    
    When collecting kernel stacks for a target process, if the process
    adds a thread between the two calls to sysctl, ignore the additional
    threads.  Previously, procstat would print only a useless error
    message.  Now, it prints a consistent snapshot of the stacks.
    We know that snapshot is already stale, but it could still be stale
    even with a more complex fix to reallocate and retry, so such a fix
    is hardly worth the effort.
    
    Reported by:    Daniel.Mitchell@emc.com
    MFC after:      1 week
    Sponsored by:   Dell EMC Isilon
    
    (cherry picked from commit 427f12f150e875c40acb84f292a80bfa0b90a1a2)
---
 lib/libprocstat/libprocstat.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/libprocstat/libprocstat.c b/lib/libprocstat/libprocstat.c
index 89896a813888..9409fb0e1964 100644
--- a/lib/libprocstat/libprocstat.c
+++ b/lib/libprocstat/libprocstat.c
@@ -2610,7 +2610,8 @@ procstat_getkstack_sysctl(pid_t pid, int *cntp)
 		warn("malloc(%zu)", len);
 		return (NULL);
 	}
-	if (sysctl(name, nitems(name), kkstp, &len, NULL, 0) == -1) {
+	if (sysctl(name, nitems(name), kkstp, &len, NULL, 0) == -1 &&
+	    errno != ENOMEM) {
 		warn("sysctl: kern.proc.pid: %d", pid);
 		free(kkstp);
 		return (NULL);