From owner-svn-src-all@freebsd.org Mon Feb 19 00:38:15 2018 Return-Path: Delivered-To: svn-src-all@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 192ECF157D1; Mon, 19 Feb 2018 00:38:15 +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 C003B7543D; Mon, 19 Feb 2018 00:38:14 +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 B66AE13611; Mon, 19 Feb 2018 00:38:14 +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 w1J0cElw090404; Mon, 19 Feb 2018 00:38:14 GMT (envelope-from mjg@FreeBSD.org) Received: (from mjg@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w1J0cE7U090402; Mon, 19 Feb 2018 00:38:14 GMT (envelope-from mjg@FreeBSD.org) Message-Id: <201802190038.w1J0cE7U090402@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: mjg set sender to mjg@FreeBSD.org using -f From: Mateusz Guzik Date: Mon, 19 Feb 2018 00:38:14 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r329540 - in head/sys: kern sys X-SVN-Group: head X-SVN-Commit-Author: mjg X-SVN-Commit-Paths: in head/sys: kern sys X-SVN-Commit-Revision: 329540 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 19 Feb 2018 00:38:15 -0000 Author: mjg Date: Mon Feb 19 00:38:14 2018 New Revision: 329540 URL: https://svnweb.freebsd.org/changeset/base/329540 Log: mtx: add mtx_spin_wait_unlocked The primitive can be used to wait for the lock to be released. Intended usage is for locks in structures which are about to be freed. The benefit is the avoided interrupt enable/disable trip + atomic op to grab the lock and shorter wait if the lock is held (since there is no worry someone will contend on the lock, re-reads can be more aggressive). Briefly discussed with: kib Modified: head/sys/kern/kern_mutex.c head/sys/sys/mutex.h Modified: head/sys/kern/kern_mutex.c ============================================================================== --- head/sys/kern/kern_mutex.c Sun Feb 18 23:35:23 2018 (r329539) +++ head/sys/kern/kern_mutex.c Mon Feb 19 00:38:14 2018 (r329540) @@ -1226,6 +1226,23 @@ _mtx_lock_indefinite_check(struct mtx *m, struct lock_ cpu_spinwait(); } +void +mtx_spin_wait_unlocked(struct mtx *m) +{ + struct lock_delay_arg lda; + + lda.spin_cnt = 0; + + while (atomic_load_acq_ptr(&m->mtx_lock) != MTX_UNOWNED) { + if (__predict_true(lda.spin_cnt < 10000000)) { + cpu_spinwait(); + lda.spin_cnt++; + } else { + _mtx_lock_indefinite_check(m, &lda); + } + } +} + #ifdef DDB void db_show_mtx(const struct lock_object *lock) Modified: head/sys/sys/mutex.h ============================================================================== --- head/sys/sys/mutex.h Sun Feb 18 23:35:23 2018 (r329539) +++ head/sys/sys/mutex.h Mon Feb 19 00:38:14 2018 (r329540) @@ -125,6 +125,8 @@ int __mtx_trylock_spin_flags(volatile uintptr_t *c, in const char *file, int line); void __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file, int line); +void mtx_spin_wait_unlocked(struct mtx *m); + #if defined(INVARIANTS) || defined(INVARIANT_SUPPORT) void __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line);