From owner-svn-src-head@freebsd.org Mon Nov 16 03:09:19 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 4F1002D0DAF; Mon, 16 Nov 2020 03:09:19 +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 4CZDZ71jDCz4W61; Mon, 16 Nov 2020 03:09:19 +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 2D86726BAD; Mon, 16 Nov 2020 03:09:19 +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 0AG39J2W067465; Mon, 16 Nov 2020 03:09:19 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 0AG39JmP067464; Mon, 16 Nov 2020 03:09:19 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <202011160309.0AG39JmP067464@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Mon, 16 Nov 2020 03:09:19 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r367713 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 367713 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: Mon, 16 Nov 2020 03:09:19 -0000 Author: mjg Date: Mon Nov 16 03:09:18 2020 New Revision: 367713 URL: https://svnweb.freebsd.org/changeset/base/367713 Log: select: replace reference counting with memory barriers in selfd Refcounting was added to combat a race between selfdfree and doselwakup, but it adds avoidable overhead. selfdfree detects it can free the object by ->sf_si == NULL, thus we can ensure that the condition only holds after all accesses are completed. Modified: head/sys/kern/sys_generic.c Modified: head/sys/kern/sys_generic.c ============================================================================== --- head/sys/kern/sys_generic.c Sun Nov 15 22:49:28 2020 (r367712) +++ head/sys/kern/sys_generic.c Mon Nov 16 03:09:18 2020 (r367713) @@ -156,7 +156,6 @@ struct selfd { struct mtx *sf_mtx; /* Pointer to selinfo mtx. */ struct seltd *sf_td; /* (k) owning seltd. */ void *sf_cookie; /* (k) fd or pollfd. */ - u_int sf_refs; }; MALLOC_DEFINE(M_SELFD, "selfd", "selfd"); @@ -1704,16 +1703,17 @@ static void selfdfree(struct seltd *stp, struct selfd *sfp) { STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); - if (sfp->sf_si != NULL) { + /* + * Paired with doselwakeup. + */ + if (atomic_load_acq_ptr((uintptr_t *)&sfp->sf_si) != (uintptr_t)NULL) { mtx_lock(sfp->sf_mtx); if (sfp->sf_si != NULL) { TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads); - refcount_release(&sfp->sf_refs); } mtx_unlock(sfp->sf_mtx); } - if (refcount_release(&sfp->sf_refs)) - free(sfp, M_SELFD); + free(sfp, M_SELFD); } /* Drain the waiters tied to all the selfd belonging the specified selinfo. */ @@ -1766,7 +1766,6 @@ selrecord(struct thread *selector, struct selinfo *sip */ sfp->sf_si = sip; sfp->sf_mtx = mtxp; - refcount_init(&sfp->sf_refs, 2); STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link); /* * Now that we've locked the sip, check for initialization. @@ -1820,14 +1819,15 @@ doselwakeup(struct selinfo *sip, int pri) * sf_si seltdclear will know to ignore this si. */ TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads); - sfp->sf_si = NULL; stp = sfp->sf_td; + /* + * Paired with selfdfree. + */ + atomic_store_rel_ptr((uintptr_t *)&sfp->sf_si, (uintptr_t)NULL); mtx_lock(&stp->st_mtx); stp->st_flags |= SELTD_PENDING; cv_broadcastpri(&stp->st_wait, pri); mtx_unlock(&stp->st_mtx); - if (refcount_release(&sfp->sf_refs)) - free(sfp, M_SELFD); } mtx_unlock(sip->si_mtx); }