From nobody Sun Sep 03 05:22:33 2023 X-Original-To: dev-commits-src-main@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 4RdgBf2QGpz4rFY8; Sun, 3 Sep 2023 05:22:34 +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 4RdgBf0Rrlz4bff; Sun, 3 Sep 2023 05:22:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693718554; 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=CbTopMa7VKH6zBADC+luHWBG96O0OgRxM2arc5RFLs4=; b=q1TmqciaXUPB+bBjLfaYLQLLQlxhU09fjfoVqSqZJ13e5xDzlnknA6WbjhEz5dcUrVdQVZ auxo1tZh3QxoK9xEAlijnTpqoXXqb5jGjTsnBsRD6/gpOV7jhLb9P1FMUZsvdEGNJNPmd3 flW3xqAijkhWIEIt0G02qJxaqNgJDhw+gSDdMgS/bZjEhb4EwY0PEwlNdbmAU+LDT2aFa/ BXj8j/Jy5dTGDLuKE8Pf26bkGDdebk24BF6TwZXcHGPFYnOuuQXRqBwhkLaf+JT4Oxkdj8 5LOnZDaW1oS+tB1jJ21eDxYd8xpsIdo7h2vVL97F/fnWF7j2nnsQzOOYuuRsAw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1693718554; a=rsa-sha256; cv=none; b=Ic8/wUAJ96AvBcmuA5xXEiOq22RRjJ4McC6ydIh5Sg10sREji78oEUvaOX5jh+zwbwyHHU 4K+ltuPhbRsO3SnDocxm3PT86vXU+1D/QSYHP0NTqc2baIkImCryPe3SGEWy7SDblmMgJq pKCeYTe7s9V2BEcdj4EZs9kUSUWBHyI3/zwc5PSX7FZyk0u/8iBo5kLoha1b07YNUIxpfF hsHhk8VLpoUWl4af9winDkdEQQiwYIOs/HjWLg9b+94JEi2Ua0Y5eiP1dSLeqvrL3ynDu5 EUBpLCSppKKDjPJMMNqgVSov+GzQxDHTu1N09Q5QYT6YhoOO+fkSLfVcMVfmcA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1693718554; 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=CbTopMa7VKH6zBADC+luHWBG96O0OgRxM2arc5RFLs4=; b=Quyzv+s05BQpZSKcRu/wMbhjByIizV0nzdR7OERKXf84H+sfJp4eEpxKI+nkmqQtdIlU0z 9CCfnB+zYZ3kkLwyhhpNxd8V8/WeI91XglPs92AeI4KEN3tXqtOxdO4yEgOlg6xk/12QPV Dvm+AhGo2+8A2Q+AQBmcAHMUKScThsYiGdyTjAN/CdzQziUg/pHJYRo4DhC+pXuj46S2A8 BIJZRvrfpABTRHstrXonl2nitJNpGM0ATtY8jX7Rw4Tu2H3ZfnQOF32E6wlnJdGoDot3LS 99DJDMumj9rPXXGBSWVNo0Xdocd0kcYL2vZnALHfvxbarEF2bne/MJi20lZkNA== 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 4RdgBd6c3zz1BsD; Sun, 3 Sep 2023 05:22:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3835MXbM000058; Sun, 3 Sep 2023 05:22:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3835MX7T000055; Sun, 3 Sep 2023 05:22:33 GMT (envelope-from git) Date: Sun, 3 Sep 2023 05:22:33 GMT Message-Id: <202309030522.3835MX7T000055@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 525bc87f54f2 - main - kern_kthread: fork1() does not handle locked Giant List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@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/main X-Git-Reftype: branch X-Git-Commit: 525bc87f54f288793a95abbcd0a845efddb8c41b Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=525bc87f54f288793a95abbcd0a845efddb8c41b commit 525bc87f54f288793a95abbcd0a845efddb8c41b Author: Konstantin Belousov AuthorDate: 2023-09-02 07:16:48 +0000 Commit: Konstantin Belousov CommitDate: 2023-09-03 05:21:53 +0000 kern_kthread: fork1() does not handle locked Giant fork1() does not behave if called under Giant. For instance, it might need to call thread_suspend_check() which explicitly verifies that Giant is not locked. On the other hand, the kthread KPI is often called from SYSINIT() which is still Giant-locked. Correct this by dropping Giant in kthread_add() and kproc_create(). Reported by: pho Reviewed by: jhb Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D41694 --- sys/kern/kern_kthread.c | 60 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/sys/kern/kern_kthread.c b/sys/kern/kern_kthread.c index 3604d63531da..5e32aea1f56d 100644 --- a/sys/kern/kern_kthread.c +++ b/sys/kern/kern_kthread.c @@ -78,13 +78,12 @@ kproc_start(const void *udata) * flags are flags to fork1 (in unistd.h) * fmt and following will be *printf'd into (*newpp)->p_comm (for ps, etc.). */ -int -kproc_create(void (*func)(void *), void *arg, - struct proc **newpp, int flags, int pages, const char *fmt, ...) +static int +kproc_create1(void (*func)(void *), void *arg, + struct proc **newpp, int flags, int pages, const char *tdname) { struct fork_req fr; int error; - va_list ap; struct thread *td; struct proc *p2; @@ -105,13 +104,9 @@ kproc_create(void (*func)(void *), void *arg, *newpp = p2; /* set up arg0 for 'ps', et al */ - va_start(ap, fmt); - vsnprintf(p2->p_comm, sizeof(p2->p_comm), fmt, ap); - va_end(ap); + strcpy(p2->p_comm, tdname); td = FIRST_THREAD_IN_PROC(p2); - va_start(ap, fmt); - vsnprintf(td->td_name, sizeof(td->td_name), fmt, ap); - va_end(ap); + strcpy(td->td_name, tdname); #ifdef KTR sched_clear_tdname(td); #endif @@ -142,6 +137,23 @@ kproc_create(void (*func)(void *), void *arg, return (0); } +int +kproc_create(void (*func)(void *), void *arg, + struct proc **newpp, int flags, int pages, const char *fmt, ...) +{ + va_list ap; + int error; + char tdname[MAXCOMLEN + 1]; + + va_start(ap, fmt); + vsnprintf(tdname, sizeof(tdname), fmt, ap); + va_end(ap); + DROP_GIANT(); + error = kproc_create1(func, arg, newpp, flags, pages, tdname); + PICKUP_GIANT(); + return (error); +} + void kproc_exit(int ecode) { @@ -250,11 +262,10 @@ kthread_start(const void *udata) * ** XXX fix this --> flags are flags to fork1 (in unistd.h) * fmt and following will be *printf'd into (*newtd)->td_name (for ps, etc.). */ -int -kthread_add(void (*func)(void *), void *arg, struct proc *p, - struct thread **newtdp, int flags, int pages, const char *fmt, ...) +static int +kthread_add1(void (*func)(void *), void *arg, struct proc *p, + struct thread **newtdp, int flags, int pages, const char *tdname) { - va_list ap; struct thread *newtd, *oldtd; if (!proc0.p_stats) @@ -278,9 +289,7 @@ kthread_add(void (*func)(void *), void *arg, struct proc *p, __rangeof(struct thread, td_startcopy, td_endcopy)); /* set up arg0 for 'ps', et al */ - va_start(ap, fmt); - vsnprintf(newtd->td_name, sizeof(newtd->td_name), fmt, ap); - va_end(ap); + strcpy(newtd->td_name, tdname); TSTHREAD(newtd, newtd->td_name); @@ -323,6 +332,23 @@ kthread_add(void (*func)(void *), void *arg, struct proc *p, return (0); } +int +kthread_add(void (*func)(void *), void *arg, struct proc *p, + struct thread **newtdp, int flags, int pages, const char *fmt, ...) +{ + va_list ap; + int error; + char tdname[MAXCOMLEN + 1]; + + va_start(ap, fmt); + vsnprintf(tdname, sizeof(tdname), fmt, ap); + va_end(ap); + DROP_GIANT(); + error = kthread_add1(func, arg, p, newtdp, flags, pages, tdname); + PICKUP_GIANT(); + return (error); +} + void kthread_exit(void) {