From owner-svn-src-head@freebsd.org Thu Nov 12 00:29:23 2020 Return-Path: Delivered-To: svn-src-head@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 95EEC46B125; Thu, 12 Nov 2020 00:29:23 +0000 (UTC) (envelope-from mjg@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 "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CWjCR3tfcz4Vcg; Thu, 12 Nov 2020 00:29:23 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 77F6A1D453; Thu, 12 Nov 2020 00:29:23 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 0AC0TN18037147; Thu, 12 Nov 2020 00:29:23 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0AC0TN87037146; Thu, 12 Nov 2020 00:29:23 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202011120029.0AC0TN87037146@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Thu, 12 Nov 2020 00:29:23 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r367605 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 367605 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 12 Nov 2020 00:29:23 -0000 Author: mjg Date: Thu Nov 12 00:29:23 2020 New Revision: 367605 URL: https://svnweb.freebsd.org/changeset/base/367605 Log: thread: move nthread management out of tid_alloc While this adds more work single-threaded, it also enables SMP-related speed ups. Modified: head/sys/kern/kern_thread.c Modified: head/sys/kern/kern_thread.c ============================================================================== --- head/sys/kern/kern_thread.c Wed Nov 11 22:35:23 2020 (r367604) +++ head/sys/kern/kern_thread.c Thu Nov 12 00:29:23 2020 (r367605) @@ -144,7 +144,7 @@ static int maxthread; SYSCTL_INT(_kern, OID_AUTO, maxthread, CTLFLAG_RDTUN, &maxthread, 0, "Maximum number of threads"); -static int nthreads; +static __exclusive_cache_line int nthreads; static LIST_HEAD(tidhashhead, thread) *tidhashtbl; static u_long tidhash; @@ -158,29 +158,52 @@ EVENTHANDLER_LIST_DEFINE(thread_dtor); EVENTHANDLER_LIST_DEFINE(thread_init); EVENTHANDLER_LIST_DEFINE(thread_fini); -static lwpid_t -tid_alloc(void) +static bool +thread_count_inc(void) { static struct timeval lastfail; static int curfail; - static lwpid_t trytid; - lwpid_t tid; + int nthreads_new; - mtx_lock(&tid_lock); - if (nthreads + 1 >= maxthread - 100) { + thread_reap(); + + nthreads_new = atomic_fetchadd_int(&nthreads, 1) + 1; + if (nthreads_new >= maxthread - 100) { if (priv_check_cred(curthread->td_ucred, PRIV_MAXPROC) != 0 || - nthreads + 1 >= maxthread) { - mtx_unlock(&tid_lock); + nthreads_new >= maxthread) { + atomic_subtract_int(&nthreads, 1); if (ppsratecheck(&lastfail, &curfail, 1)) { printf("maxthread limit exceeded by uid %u " "(pid %d); consider increasing kern.maxthread\n", curthread->td_ucred->cr_ruid, curproc->p_pid); } - return (-1); + return (false); } } + return (true); +} - nthreads++; +static void +thread_count_sub(int n) +{ + + atomic_subtract_int(&nthreads, n); +} + +static void +thread_count_dec(void) +{ + + thread_count_sub(1); +} + +static lwpid_t +tid_alloc(void) +{ + static lwpid_t trytid; + lwpid_t tid; + + mtx_lock(&tid_lock); /* * It is an invariant that the bitmap is big enough to hold maxthread * IDs. If we got to this point there has to be at least one free. @@ -212,7 +235,6 @@ tid_free_locked(lwpid_t rtid) KASSERT(bit_test(tid_bitmap, tid) != 0, ("thread ID %d not allocated\n", rtid)); bit_clear(tid_bitmap, tid); - nthreads--; } static void @@ -398,6 +420,10 @@ threadinit(void) mtx_init(&tid_lock, "TID lock", NULL, MTX_DEF); tid_bitmap = bit_alloc(maxthread, M_TIDHASH, M_WAITOK); + /* + * Handle thread0. + */ + thread_count_inc(); tid0 = tid_alloc(); if (tid0 != THREAD0_TID) panic("tid0 %d != %d\n", tid0, THREAD0_TID); @@ -482,6 +508,7 @@ thread_reap(void) thread_free_batched(itd); if (tidbatchn == nitems(tidbatch)) { tid_free_batch(tidbatch, tidbatchn); + thread_count_sub(tidbatchn); tidbatchn = 0; } itd = ntd; @@ -489,6 +516,7 @@ thread_reap(void) if (tidbatchn != 0) { tid_free_batch(tidbatch, tidbatchn); + thread_count_sub(tidbatchn); } } @@ -501,18 +529,17 @@ thread_alloc(int pages) struct thread *td; lwpid_t tid; - thread_reap(); /* check if any zombies to get */ - - tid = tid_alloc(); - if (tid == -1) { + if (!thread_count_inc()) { return (NULL); } + tid = tid_alloc(); td = uma_zalloc(thread_zone, M_WAITOK); KASSERT(td->td_kstack == 0, ("thread_alloc got thread with kstack")); if (!vm_thread_new(td, pages)) { uma_zfree(thread_zone, td); tid_free(tid); + thread_count_dec(); return (NULL); } td->td_tid = tid; @@ -564,6 +591,7 @@ thread_free(struct thread *td) tid = td->td_tid; thread_free_batched(td); tid_free(tid); + thread_count_dec(); } void