From owner-svn-src-head@freebsd.org Thu Jul 9 10:11:38 2015 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 92327996E28; Thu, 9 Jul 2015 10:11:38 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: from mail-wi0-x22c.google.com (mail-wi0-x22c.google.com [IPv6:2a00:1450:400c:c05::22c]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 2B9A11D2A; Thu, 9 Jul 2015 10:11:38 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: by wiga1 with SMTP id a1so308440001wig.0; Thu, 09 Jul 2015 03:11:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; bh=lWy3GksPNAsnQcX7MgdvU3jpSFB9O4VwhwcVQxOrSds=; b=JbCn6owo+CaKOITs+E64u/u5xPi80/RAtG80F78wptDQskNj4rdidfVJAUlDUhtbXi ydskWzbGvMLiHeOjwnGP2sya0vZgeohaMcRGPDoAMV5I40u+YqFM26F0v3CCXFYTBQNP ekFJtxuHPeYnxdZBo4P1EMxXwNZFzHGB7JAGoMODl026ePqZryRcPkPnO59KlXeDAuQb xGWjFKtWsLBisaVTlHKe1eN4uiy+dQsmR9x90XoikhHd/d2mEzvs6tDSG9mLlHraPEVU 5GSXy2GgHHl1WMyn/gH+L58RVxL1tX3qn9+cKfCptfOs3R1DVy22tbLXsw/1F9O/oDLe a/NQ== X-Received: by 10.180.72.145 with SMTP id d17mr122551079wiv.69.1436436696571; Thu, 09 Jul 2015 03:11:36 -0700 (PDT) Received: from dft-labs.eu (n1x0n-1-pt.tunnel.tserv5.lon1.ipv6.he.net. [2001:470:1f08:1f7::2]) by smtp.gmail.com with ESMTPSA id q4sm7989234wju.14.2015.07.09.03.11.34 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 09 Jul 2015 03:11:35 -0700 (PDT) Date: Thu, 9 Jul 2015 12:11:32 +0200 From: Mateusz Guzik To: Konstantin Belousov Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: Re: svn commit: r285310 - head/sys/kern Message-ID: <20150709101131.GA1718@dft-labs.eu> References: <201507090922.t699MMa9029429@repo.freebsd.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <201507090922.t699MMa9029429@repo.freebsd.org> User-Agent: Mutt/1.5.21 (2010-09-15) X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.20 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, 09 Jul 2015 10:11:38 -0000 On Thu, Jul 09, 2015 at 09:22:22AM +0000, Konstantin Belousov wrote: > Author: kib > Date: Thu Jul 9 09:22:21 2015 > New Revision: 285310 > URL: https://svnweb.freebsd.org/changeset/base/285310 > > Log: > Cover a race between doselwakeup() and selfdfree(). If doselwakeup() > loop finds the selfd entry and clears its sf_si pointer, which is > handled by selfdfree() in parallel, NULL sf_si makes selfdfree() free > the memory. The result is the race and accesses to the freed memory. > > Refcount the selfd ownership. One reference is for the sf_link > linkage, which is unconditionally dereferenced by selfdfree(). > Another reference is for sf_threads, both selfdfree() and > doselwakeup() race to deref it, the winner unlinks and than frees the > selfd entry. > > MFC after: 2 weeks Looks like my bug introduced as a result of r273549 + r273555. It was not MFCed, so this change does not need a MFC either. With the lock taken unconditionally there is no window where sf_si NULL is visible to selfdfree while doselwakeup still uses the sfp. afair the motivation for the change was some minor contention shown by lock profiling during buildworld/buildkernel, as sf_si comes from a mtx pool. So just in case, I vote for leaving stuff as it is, I'm just noting there is no need to MFC. > > Modified: > head/sys/kern/sys_generic.c > > Modified: head/sys/kern/sys_generic.c > ============================================================================== > --- head/sys/kern/sys_generic.c Thu Jul 9 07:31:40 2015 (r285309) > +++ head/sys/kern/sys_generic.c Thu Jul 9 09:22:21 2015 (r285310) > @@ -153,6 +153,7 @@ 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; > }; > > static uma_zone_t selfd_zone; > @@ -1685,11 +1686,14 @@ selfdfree(struct seltd *stp, struct self > STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link); > if (sfp->sf_si != NULL) { > mtx_lock(sfp->sf_mtx); > - if (sfp->sf_si != NULL) > + 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); > } > - uma_zfree(selfd_zone, sfp); > + if (refcount_release(&sfp->sf_refs)) > + uma_zfree(selfd_zone, sfp); > } > > /* Drain the waiters tied to all the selfd belonging the specified selinfo. */ > @@ -1745,6 +1749,7 @@ selrecord(selector, 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. > @@ -1809,6 +1814,8 @@ doselwakeup(sip, pri) > stp->st_flags |= SELTD_PENDING; > cv_broadcastpri(&stp->st_wait, pri); > mtx_unlock(&stp->st_mtx); > + if (refcount_release(&sfp->sf_refs)) > + uma_zfree(selfd_zone, sfp); > } > mtx_unlock(sip->si_mtx); > } > _______________________________________________ > svn-src-all@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/svn-src-all > To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org" -- Mateusz Guzik