From owner-freebsd-arch@FreeBSD.ORG Wed May 22 13:06:08 2013 Return-Path: Delivered-To: freebsd-arch@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 78285F54 for ; Wed, 22 May 2013 13:06:08 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from bigwig.baldwin.cx (bigwig.baldwin.cx [IPv6:2001:470:1f11:75::1]) by mx1.freebsd.org (Postfix) with ESMTP id 567C7EA5 for ; Wed, 22 May 2013 13:06:08 +0000 (UTC) Received: from jhbbsd.localnet (unknown [209.249.190.124]) by bigwig.baldwin.cx (Postfix) with ESMTPSA id AA2B2B990; Wed, 22 May 2013 09:06:07 -0400 (EDT) From: John Baldwin To: Alfred Perlstein Subject: Re: FreeBSD spinlock - compatibility layer Date: Wed, 22 May 2013 09:05:57 -0400 User-Agent: KMail/1.13.5 (FreeBSD/8.2-CBSD-20110714-p25; KDE/4.5.5; amd64; ; ) References: <981733489AB3BD4DB24B48340F53E0A55B0CFD79@MTLDAG01.mtl.com> <201305200950.26834.jhb@freebsd.org> <519BDBB0.2070302@mu.org> In-Reply-To: <519BDBB0.2070302@mu.org> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201305220905.57939.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.2.7 (bigwig.baldwin.cx); Wed, 22 May 2013 09:06:07 -0400 (EDT) Cc: Orit Moskovich , freebsd-arch@freebsd.org X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 May 2013 13:06:08 -0000 On Tuesday, May 21, 2013 4:40:16 pm Alfred Perlstein wrote: > On 5/20/13 9:50 AM, John Baldwin wrote: > > On Tuesday, May 14, 2013 6:04:21 am Orit Moskovich wrote: > >> Hi, > >> > >> I read about the FreeBSD mutex implementation for spinlock in the > > compatibility layer. > >> I might be wrong, but I noticed a code section that might be problematic: > >> > >> Taken from > > http://svn.freebsd.org/base/release/9.1.0/sys/ofed/include/linux/spinlock.h: > >> static inline void > >> spin_lock_init(spinlock_t *lock) > >> { > >> > >> memset(&lock->m, 0, sizeof(lock->m)); > >> mtx_init(&lock->m, "lnxspin", NULL, MTX_DEF | MTX_NOWITNESS); > >> } > >> > >> But MTX_DEF initializes mutex as a sleep mutex: > >> > >> By default, MTX_DEF mutexes will context switch when they are already > >> > >> held. > >> > >> > >> There is a flag MTX_SPIN Which I think is the right one in this case . > >> > >> > >> > >> I'd appreciate your take on this issue. > > Since FreeBSD uses a different approach to interrupt handlers (they run in > > threads, not in the bottom half), a regular mutex may in fact give the closest > > match to the same semantics. Regular mutexes are also cheaper and in general > > preferable to spin mutexes whenever possible. > > > > Sure, but is it possible that someone might want some of the other > guarantees of MTX_SPIN spinlocks such as: > > critical section/non-pre-emptable/non-migrating on cpu/latency versus > throughput ? Probably not. For example, on FreeBSD you want your driver lock to be preempted by an interrupt to avoid higher interrupt latency for filter handlers. Most drivers should not need temporary pinning. If they want to pin work to threads they should bind threads or IRQs to specific CPUs, not rely on temporary pinning. -- John Baldwin