From owner-svn-src-head@freebsd.org Thu Nov 19 19:25:48 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 642EE472F23; Thu, 19 Nov 2020 19:25:48 +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 4CcV5S2QV5z3Jqn; Thu, 19 Nov 2020 19:25:48 +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 3741E1596; Thu, 19 Nov 2020 19:25:48 +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 0AJJPmDT089605; Thu, 19 Nov 2020 19:25:48 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0AJJPlDc089603; Thu, 19 Nov 2020 19:25:47 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202011191925.0AJJPlDc089603@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Thu, 19 Nov 2020 19:25:47 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r367852 - in head/sys: kern sys X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: in head/sys: kern sys X-SVN-Commit-Revision: 367852 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, 19 Nov 2020 19:25:48 -0000 Author: mjg Date: Thu Nov 19 19:25:47 2020 New Revision: 367852 URL: https://svnweb.freebsd.org/changeset/base/367852 Log: pipe: thundering herd problem in pipelock All reads and writes are serialized with a hand-rolled lock, but unlocking it always wakes up all waiters. Existing flag fields get resized to make room for introduction of waiter counter without growing the struct. Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D27273 Modified: head/sys/kern/sys_pipe.c head/sys/sys/pipe.h Modified: head/sys/kern/sys_pipe.c ============================================================================== --- head/sys/kern/sys_pipe.c Thu Nov 19 19:05:16 2020 (r367851) +++ head/sys/kern/sys_pipe.c Thu Nov 19 19:25:47 2020 (r367852) @@ -622,9 +622,13 @@ pipelock(struct pipe *cpipe, int catch) if (catch) prio |= PCATCH; while (cpipe->pipe_state & PIPE_LOCKFL) { - cpipe->pipe_state |= PIPE_LWANT; + KASSERT(cpipe->pipe_waiters >= 0, + ("%s: bad waiter count %d", __func__, + cpipe->pipe_waiters)); + cpipe->pipe_waiters++; error = msleep(cpipe, PIPE_MTX(cpipe), prio, "pipelk", 0); + cpipe->pipe_waiters--; if (error != 0) return (error); } @@ -642,10 +646,12 @@ pipeunlock(struct pipe *cpipe) PIPE_LOCK_ASSERT(cpipe, MA_OWNED); KASSERT(cpipe->pipe_state & PIPE_LOCKFL, ("Unlocked pipe passed to pipeunlock")); + KASSERT(cpipe->pipe_waiters >= 0, + ("%s: bad waiter count %d", __func__, + cpipe->pipe_waiters)); cpipe->pipe_state &= ~PIPE_LOCKFL; - if (cpipe->pipe_state & PIPE_LWANT) { - cpipe->pipe_state &= ~PIPE_LWANT; - wakeup(cpipe); + if (cpipe->pipe_waiters > 0) { + wakeup_one(cpipe); } } Modified: head/sys/sys/pipe.h ============================================================================== --- head/sys/sys/pipe.h Thu Nov 19 19:05:16 2020 (r367851) +++ head/sys/sys/pipe.h Thu Nov 19 19:25:47 2020 (r367852) @@ -116,9 +116,10 @@ struct pipe { struct pipe *pipe_peer; /* link with other direction */ struct pipepair *pipe_pair; /* container structure pointer */ u_short pipe_state; /* pipe status info */ - u_short pipe_type; /* pipe type info */ + u_char pipe_type; /* pipe type info */ + u_char pipe_present; /* still present? */ + int pipe_waiters; /* pipelock waiters */ int pipe_busy; /* busy flag, mostly to handle rundown sanely */ - int pipe_present; /* still present? */ int pipe_wgen; /* writer generation for named pipe */ ino_t pipe_ino; /* fake inode for stat(2) */ };