From owner-svn-src-head@freebsd.org Tue Nov 17 01:58:34 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 A1C4F47A5F8; Tue, 17 Nov 2020 01:58:34 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (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 mx1.freebsd.org (Postfix) with ESMTPS id 4CZpy22cK9z4vtf; Tue, 17 Nov 2020 01:58:33 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.16.1/8.16.1) with ESMTPS id 0AH1wQno055007 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NO); Tue, 17 Nov 2020 03:58:29 +0200 (EET) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 0AH1wQno055007 Received: (from kostik@localhost) by tom.home (8.16.1/8.16.1/Submit) id 0AH1wPkV055006; Tue, 17 Nov 2020 03:58:25 +0200 (EET) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Tue, 17 Nov 2020 03:58:25 +0200 From: Konstantin Belousov To: Mateusz Guzik Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: Re: svn commit: r367713 - head/sys/kern Message-ID: References: <202011160309.0AG39JmP067464@repo.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202011160309.0AG39JmP067464@repo.freebsd.org> X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on tom.home X-Rspamd-Queue-Id: 4CZpy22cK9z4vtf X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[] 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: Tue, 17 Nov 2020 01:58:34 -0000 On Mon, Nov 16, 2020 at 03:09:19AM +0000, Mateusz Guzik wrote: > 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) { This could be != 0. > 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); What guarantees that doselwakeup() finished with sfp ? > } > > /* 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); > }