From owner-dev-commits-src-all@freebsd.org Wed Aug 11 17:45:04 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 6C91765867B; Wed, 11 Aug 2021 17:45:04 +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 4GlHJw2ZZFz3lGf; Wed, 11 Aug 2021 17:45:04 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 4180A13AF0; Wed, 11 Aug 2021 17:45:04 +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 17BHj4cH053179; Wed, 11 Aug 2021 17:45:04 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 17BHj4VP053178; Wed, 11 Aug 2021 17:45:04 GMT (envelope-from git) Date: Wed, 11 Aug 2021 17:45:04 GMT Message-Id: <202108111745.17BHj4VP053178@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mitchell Horne Subject: git: 4ccaa87f695c - main - kdb: Handle process enumeration before procinit() MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mhorne X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 4ccaa87f695c8b9eb31f2ba9ce4913a045755fe0 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 11 Aug 2021 17:45:04 -0000 The branch main has been updated by mhorne: URL: https://cgit.FreeBSD.org/src/commit/?id=4ccaa87f695c8b9eb31f2ba9ce4913a045755fe0 commit 4ccaa87f695c8b9eb31f2ba9ce4913a045755fe0 Author: Mitchell Horne AuthorDate: 2021-08-11 17:40:01 +0000 Commit: Mitchell Horne CommitDate: 2021-08-11 17:44:22 +0000 kdb: Handle process enumeration before procinit() Make kdb_thr_first() and kdb_thr_next() return sane values if the allproc list and pidhashtbl haven't been initialized yet. This can happen if the debugger is entered very early on, for example with the '-d' boot flag. This allows remote gdb to attach at such a time, and fixes some ddb commands like 'show threads'. Be explicit about the static initialization of these variables. This part has no functional change. Reviewed by: markj, imp (previous version) MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D31495 --- sys/kern/kern_proc.c | 5 ++--- sys/kern/subr_kdb.c | 6 ++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index 2017f824f6ad..2649d1d3a58f 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -122,13 +122,13 @@ static void pargs_free(struct pargs *pa); /* * Other process lists */ -struct pidhashhead *pidhashtbl; +struct pidhashhead *pidhashtbl = NULL; struct sx *pidhashtbl_lock; u_long pidhash; u_long pidhashlock; struct pgrphashhead *pgrphashtbl; u_long pgrphash; -struct proclist allproc; +struct proclist allproc = LIST_HEAD_INITIALIZER(allproc); struct sx __exclusive_cache_line allproc_lock; struct sx __exclusive_cache_line proctree_lock; struct mtx __exclusive_cache_line ppeers_lock; @@ -185,7 +185,6 @@ procinit(void) sx_init(&proctree_lock, "proctree"); mtx_init(&ppeers_lock, "p_peers", NULL, MTX_DEF); mtx_init(&procid_lock, "procid", NULL, MTX_DEF); - LIST_INIT(&allproc); pidhashtbl = hashinit(maxproc / 4, M_PROC, &pidhash); pidhashlock = (pidhash + 1) / 64; if (pidhashlock > 0) diff --git a/sys/kern/subr_kdb.c b/sys/kern/subr_kdb.c index 1fabb4044eb1..4f439ff103d7 100644 --- a/sys/kern/subr_kdb.c +++ b/sys/kern/subr_kdb.c @@ -608,6 +608,10 @@ kdb_thr_first(void) struct thread *thr; u_int i; + /* This function may be called early. */ + if (pidhashtbl == NULL) + return (&thread0); + for (i = 0; i <= pidhash; i++) { LIST_FOREACH(p, &pidhashtbl[i], p_hash) { thr = FIRST_THREAD_IN_PROC(p); @@ -651,6 +655,8 @@ kdb_thr_next(struct thread *thr) thr = TAILQ_NEXT(thr, td_plist); if (thr != NULL) return (thr); + if (pidhashtbl == NULL) + return (NULL); hash = p->p_pid & pidhash; for (;;) { p = LIST_NEXT(p, p_hash);