From owner-freebsd-hackers@freebsd.org Wed Jun 14 05:05:24 2017 Return-Path: Delivered-To: freebsd-hackers@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 02C92C79F22 for ; Wed, 14 Jun 2017 05:05:24 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 81E0479CEB for ; Wed, 14 Jun 2017 05:05:23 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from tom.home (kib@localhost [127.0.0.1]) by kib.kiev.ua (8.15.2/8.15.2) with ESMTPS id v5E55EPa050495 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 14 Jun 2017 08:05:14 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua v5E55EPa050495 Received: (from kostik@localhost) by tom.home (8.15.2/8.15.2/Submit) id v5E55DiT050488; Wed, 14 Jun 2017 08:05:13 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Wed, 14 Jun 2017 08:05:13 +0300 From: Konstantin Belousov To: Jia-Ju Bai Cc: freebsd-hackers@freebsd.org Subject: Re: A question about in lock usage in FreeBSD Message-ID: <20170614050513.GI2088@kib.kiev.ua> References: <59408D05.7070407@163.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <59408D05.7070407@163.com> User-Agent: Mutt/1.8.2 (2017-04-18) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.1 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on tom.home X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Jun 2017 05:05:24 -0000 On Wed, Jun 14, 2017 at 09:10:29AM +0800, Jia-Ju Bai wrote: > Hi, > > I am a freshman in developing FreeBSD drivers, and I have a question in > lock usage in FreeBSD. > > The kernel provides some kinds of locks in developing drivers, such as > "mutex lock", "mutex spin lock", "rw lock" and "sx lock". I want to know > which locks should be held when the thread can sleep. Replace should with can in the question. > From my knowledge of FreeBSD document, I make the following points: > (1) "rw lock" and "sx lock" can be used in this situation; You cannot own rw lock while trying to sleep. > (2) "mutex lock" and "mutex spin lock" are forbidden in this situation. > > If my points are right, I will make another point: > mutex_lock will let the thread sleep when the lock is requested, so the > mutex_lock can not be called in nested style (namely it is unsafe that > mutex_lock is called again when a "mutex lock" is held). No, mutex does not put the thread which cannot obtain the lock immediately, to sleep. The thread is blocked on turnstile. > > Are my points are right? > I am looking forward to useful opinions and answers :) Read locking(9).