From owner-cvs-all@FreeBSD.ORG Thu Sep 16 22:19:12 2004 Return-Path: Delivered-To: cvs-all@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 3E2EF16A4CE; Thu, 16 Sep 2004 22:19:12 +0000 (GMT) Received: from pooker.samsco.org (pooker.samsco.org [168.103.85.57]) by mx1.FreeBSD.org (Postfix) with ESMTP id BEE1243D48; Thu, 16 Sep 2004 22:19:11 +0000 (GMT) (envelope-from scottl@samsco.org) Received: from [192.168.254.11] (junior-wifi.samsco.home [192.168.254.11]) (authenticated bits=0) by pooker.samsco.org (8.12.11/8.12.10) with ESMTP id i8GMIcB6009523; Thu, 16 Sep 2004 16:18:38 -0600 (MDT) (envelope-from scottl@samsco.org) Message-ID: <414A109E.4080601@samsco.org> Date: Thu, 16 Sep 2004 16:15:58 -0600 From: Scott Long User-Agent: Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7.2) Gecko/20040831 X-Accept-Language: en-us, en MIME-Version: 1.0 To: Nate Lawson References: <20040916185923.2F92316A552@hub.freebsd.org> <4149EC27.9080200@root.org> <20040916204321.GE30151@darkness.comp.waw.pl> <414A1073.8010404@root.org> In-Reply-To: <414A1073.8010404@root.org> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, hits=0.0 required=3.8 tests=none autolearn=no version=2.63 X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on pooker.samsco.org cc: cvs-src@FreeBSD.org cc: src-committers@FreeBSD.org cc: Pawel Jakub Dawidek cc: cvs-all@FreeBSD.org Subject: Re: cvs commit: src/sys/dev/md md.c X-BeenThere: cvs-all@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: CVS commit messages for the entire tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 16 Sep 2004 22:19:12 -0000 Nate Lawson wrote: > Pawel Jakub Dawidek wrote: > >> On Thu, Sep 16, 2004 at 12:40:23PM -0700, Nate Lawson wrote: >> +> >@@ -379,9 +379,8 @@ >> +> > bp->bio_bcount = bp->bio_length; >> +> > mtx_lock(&sc->queue_mtx); >> +> > bioq_disksort(&sc->bio_queue, bp); >> +> >- mtx_unlock(&sc->queue_mtx); >> +> >- >> +> > wakeup(sc); >> +> >+ mtx_unlock(&sc->queue_mtx); >> +> > } >> +> +> I think the original order is correct since you can occur 2 >> switches if +> you wakeup first and then unlock. >> >> Nope, this order was wrong: >> >> thread1 thread2 >> ----------------------- >> mtx_lock(mtx) >> ... >> mtx_unlock(mtx) >> mtx_lock(mtx) >> wakeup(ptr) >> msleep(ptr, mtx) <- Race, it will be never woken up. > > > You still have a race, like this: > > thread1 thread2 > ----------------------------- > mtx_lock(mtx) > wakeup(ptr) > mtx_unlock(mtx) > mtx_lock(mtx) > msleep(ptr, mtx) > > You should be checking the work condition in thread 2 while holding the > mutex but before going to sleep. Adding work to the queue happens in > thread 1 where you write "..." and that is done with the mutex held so > there is no race. The full diagram with this detail included is: > > thread1 thread2 > ----------------------------- > mtx_lock(mtx) > add work to queue > mtx_unlock(mtx) > mtx_lock(mtx) > wakeup(ptr) > check queue for work item > if (!work item) > msleep(ptr, mtx) > else > dequeue work item and loop > > Since the work item is added in thread1 with the mutex held, the check > for it in thread2 is safe and race-free. A wakeup is only there to > kickstart thread2 if it's asleep. If it's running, it needs to check > atomically that there is no work before sleeping. If it doesn't do > this, it's a bug. > Or just use a semaphore. Scott