From owner-svn-src-head@freebsd.org Wed Mar 1 05:06:24 2017 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 194FECF290F; Wed, 1 Mar 2017 05:06:24 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (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 3EAC5652EE; Wed, 1 Mar 2017 05:06:23 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v2156Miq034674; Wed, 1 Mar 2017 05:06:22 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v2156MNW034671; Wed, 1 Mar 2017 05:06:22 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <201703010506.v2156MNW034671@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Wed, 1 Mar 2017 05:06:22 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r314474 - in head/sys: kern sys X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 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: Wed, 01 Mar 2017 05:06:24 -0000 Author: mjg Date: Wed Mar 1 05:06:21 2017 New Revision: 314474 URL: https://svnweb.freebsd.org/changeset/base/314474 Log: locks: ensure proper barriers are used with atomic ops when necessary Unclear how, but the locking routine for mutexes was using the *release* barrier instead of acquire. This must have been either a copy-pasto or bad completion. Going through other uses of atomics shows no barriers in: - upgrade routines (addressed in this patch) - sections protected with turnstile locks - this should be fine as necessary barriers are in the worst case provided by turnstile unlock I would like to thank Mark Millard and andreast@ for reporting the problem and testing previous patches before the issue got identified. ps. .-'---`-. ,' `. | \ | \ \ _ \ ,\ _ ,'-,/-)\ ( * \ \,' ,' ,'-) `._,) -',-') \/ ''/ ) / / / ,'-' Hardware provided by: IBM LTC Modified: head/sys/kern/kern_rwlock.c head/sys/kern/kern_sx.c head/sys/sys/mutex.h Modified: head/sys/kern/kern_rwlock.c ============================================================================== --- head/sys/kern/kern_rwlock.c Wed Mar 1 05:05:05 2017 (r314473) +++ head/sys/kern/kern_rwlock.c Wed Mar 1 05:06:21 2017 (r314474) @@ -1151,7 +1151,7 @@ __rw_try_upgrade(volatile uintptr_t *c, if (RW_READERS(v) > 1) break; if (!(v & RW_LOCK_WAITERS)) { - success = atomic_cmpset_ptr(&rw->rw_lock, v, tid); + success = atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid); if (!success) continue; break; Modified: head/sys/kern/kern_sx.c ============================================================================== --- head/sys/kern/kern_sx.c Wed Mar 1 05:05:05 2017 (r314473) +++ head/sys/kern/kern_sx.c Wed Mar 1 05:06:21 2017 (r314474) @@ -410,7 +410,7 @@ sx_try_upgrade_(struct sx *sx, const cha * we will wake up the exclusive waiters when we drop the lock. */ x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS; - success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x, + success = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x, (uintptr_t)curthread | x); LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line); if (success) { Modified: head/sys/sys/mutex.h ============================================================================== --- head/sys/sys/mutex.h Wed Mar 1 05:05:05 2017 (r314473) +++ head/sys/sys/mutex.h Wed Mar 1 05:06:21 2017 (r314474) @@ -185,7 +185,7 @@ void thread_lock_flags_(struct thread *, atomic_cmpset_acq_ptr(&(mp)->mtx_lock, MTX_UNOWNED, (tid)) #define _mtx_obtain_lock_fetch(mp, vp, tid) \ - atomic_fcmpset_rel_ptr(&(mp)->mtx_lock, vp, (tid)) + atomic_fcmpset_acq_ptr(&(mp)->mtx_lock, vp, (tid)) /* Try to release mtx_lock if it is unrecursed and uncontested. */ #define _mtx_release_lock(mp, tid) \