From owner-svn-src-head@freebsd.org Sun Mar 4 22:01:24 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 074B6F37A7E; Sun, 4 Mar 2018 22:01:24 +0000 (UTC) (envelope-from mjg@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id B1DB07275A; Sun, 4 Mar 2018 22:01:23 +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 mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id ACE7F1B1CD; Sun, 4 Mar 2018 22:01: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 w24M1NN9000731; Sun, 4 Mar 2018 22:01:23 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w24M1N0U000730; Sun, 4 Mar 2018 22:01:23 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <201803042201.w24M1N0U000730@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Sun, 4 Mar 2018 22:01:23 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r330418 - head/sys/kern X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: head/sys/kern X-SVN-Commit-Revision: 330418 X-SVN-Commit-Repository: base 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.25 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: Sun, 04 Mar 2018 22:01:24 -0000 Author: mjg Date: Sun Mar 4 22:01:23 2018 New Revision: 330418 URL: https://svnweb.freebsd.org/changeset/base/330418 Log: mtx: tidy up recursion handling in thread lock Normally after grabbing the lock it has to be verified we got the right one to begin with. However, if we are recursing, it must not change thus the check can be avoided. In particular this avoids a lock read for non-recursing case which found out the lock was changed. While here avoid an irq trip of this happens. Tested by: pho (previous version) Modified: head/sys/kern/kern_mutex.c Modified: head/sys/kern/kern_mutex.c ============================================================================== --- head/sys/kern/kern_mutex.c Sun Mar 4 21:58:55 2018 (r330417) +++ head/sys/kern/kern_mutex.c Sun Mar 4 22:01:23 2018 (r330418) @@ -829,10 +829,8 @@ _thread_lock(struct thread *td) WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line); return; } - if (m->mtx_recurse != 0) - m->mtx_recurse--; - else - _mtx_release_lock_quick(m); + MPASS(m->mtx_recurse == 0); + _mtx_release_lock_quick(m); slowpath_unlocked: spinlock_exit(); slowpath_noirq: @@ -886,9 +884,10 @@ thread_lock_flags_(struct thread *td, int opts, const if (__predict_false(doing_lockprof)) spin_time -= lockstat_nsecs(&td->td_lock->lock_object); #endif + spinlock_enter(); + for (;;) { retry: - spinlock_enter(); m = td->td_lock; thread_lock_validate(m, opts, file, line); v = MTX_READ_VALUE(m); @@ -900,6 +899,7 @@ retry: } if (v == tid) { m->mtx_recurse++; + MPASS(m == td->td_lock); break; } lock_profile_obtain_lock_failed(&m->lock_object, @@ -912,15 +912,18 @@ retry: } else { _mtx_lock_indefinite_check(m, &lda); } - if (m != td->td_lock) + if (m != td->td_lock) { + spinlock_enter(); goto retry; + } v = MTX_READ_VALUE(m); } while (v != MTX_UNOWNED); spinlock_enter(); } if (m == td->td_lock) break; - __mtx_unlock_spin(m); /* does spinlock_exit() */ + MPASS(m->mtx_recurse == 0); + _mtx_release_lock_quick(m); } LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, line);