From patchwork Sun Mar 20 19:56:52 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tejun Heo X-Patchwork-Id: 646801 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p2KJv1F2024024 for ; Sun, 20 Mar 2011 19:57:02 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752311Ab1CTT46 (ORCPT ); Sun, 20 Mar 2011 15:56:58 -0400 Received: from mail-fx0-f46.google.com ([209.85.161.46]:38951 "EHLO mail-fx0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752240Ab1CTT46 (ORCPT ); Sun, 20 Mar 2011 15:56:58 -0400 Received: by fxm17 with SMTP id 17so4972868fxm.19 for ; Sun, 20 Mar 2011 12:56:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:sender:date:from:to:cc:subject:message-id :references:mime-version:content-type:content-disposition :in-reply-to:user-agent; bh=Xs7hI/A+sWwS6qyZweGguT8QWRHa6qN5sUqRyDYiz0E=; b=LgkTyi7/BlRVO840lF0H4zt2HRwefuRK7Pkc78ulct5cCPNTLk0paM1xRAcEJvMFUl jIm3JZrfZqVgI5WR2Z/0Jn/6zKZ4D26lcGDQJRxjeBJEHdVqGqHboYboPOj4BqZKWetC EuH+fJ/sEwU8eMOTHpQcVoD6UawGYZ9K3s+qk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:in-reply-to:user-agent; b=EPg0y71Q5pVkcwV/2YN4TviaWTsW8DD4hP/6eMofeykWq4lsJ3JpvSoK8AnyAV79ey jaFh0e6+4ISQCDvx7h67KFgyKNjCpuWK/00WKV4Cc3i951GGO9Yjdku2y/G6UZcY2FUE w+NjkB5Rh5xpU64mOPQSzAaZBkbcaw110okY4= Received: by 10.223.7.26 with SMTP id b26mr3923794fab.119.1300651016383; Sun, 20 Mar 2011 12:56:56 -0700 (PDT) Received: from htj.dyndns.org ([130.75.117.88]) by mx.google.com with ESMTPS id e17sm1279452fak.0.2011.03.20.12.56.54 (version=SSLv3 cipher=OTHER); Sun, 20 Mar 2011 12:56:55 -0700 (PDT) Date: Sun, 20 Mar 2011 20:56:52 +0100 From: Tejun Heo To: Chris Mason Cc: linux-btrfs@vger.kernel.org Subject: Re: [PATCH RFC] btrfs: Simplify locking Message-ID: <20110320195652.GB12003@htj.dyndns.org> References: <20110320174433.GA12003@htj.dyndns.org> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20110320174433.GA12003@htj.dyndns.org> User-Agent: Mutt/1.5.20 (2009-06-14) Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Sun, 20 Mar 2011 19:57:02 +0000 (UTC) Index: work/fs/btrfs/locking.h =================================================================== --- work.orig/fs/btrfs/locking.h +++ work/fs/btrfs/locking.h @@ -23,7 +23,7 @@ static inline bool btrfs_try_spin_lock(struct extent_buffer *eb) { - return mutex_trylock(&eb->lock); + return mutex_tryspin(&eb->lock); } static inline void btrfs_tree_lock(struct extent_buffer *eb) Index: work/include/linux/mutex.h =================================================================== --- work.orig/include/linux/mutex.h +++ work/include/linux/mutex.h @@ -157,6 +157,7 @@ extern int __must_check mutex_lock_killa * Returns 1 if the mutex has been acquired successfully, and 0 on contention. */ extern int mutex_trylock(struct mutex *lock); +extern int mutex_tryspin(struct mutex *lock); extern void mutex_unlock(struct mutex *lock); extern int atomic_dec_and_mutex_lock(atomic_t *cnt, struct mutex *lock); Index: work/kernel/mutex.c =================================================================== --- work.orig/kernel/mutex.c +++ work/kernel/mutex.c @@ -126,20 +126,8 @@ void __sched mutex_unlock(struct mutex * EXPORT_SYMBOL(mutex_unlock); -/* - * Lock a mutex (possibly interruptible), slowpath: - */ -static inline int __sched -__mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, - unsigned long ip) +static inline bool mutex_spin(struct mutex *lock) { - struct task_struct *task = current; - struct mutex_waiter waiter; - unsigned long flags; - - preempt_disable(); - mutex_acquire(&lock->dep_map, subclass, 0, ip); - #ifdef CONFIG_MUTEX_SPIN_ON_OWNER /* * Optimistic spinning. @@ -158,7 +146,6 @@ __mutex_lock_common(struct mutex *lock, * We can't do this for DEBUG_MUTEXES because that relies on wait_lock * to serialize everything. */ - for (;;) { struct thread_info *owner; @@ -181,7 +168,7 @@ __mutex_lock_common(struct mutex *lock, lock_acquired(&lock->dep_map, ip); mutex_set_owner(lock); preempt_enable(); - return 0; + return true; } /* @@ -190,7 +177,7 @@ __mutex_lock_common(struct mutex *lock, * we're an RT task that will live-lock because we won't let * the owner complete. */ - if (!owner && (need_resched() || rt_task(task))) + if (!owner && (need_resched() || rt_task(current))) break; /* @@ -202,6 +189,26 @@ __mutex_lock_common(struct mutex *lock, cpu_relax(); } #endif + return false; +} + +/* + * Lock a mutex (possibly interruptible), slowpath: + */ +static inline int __sched +__mutex_lock_common(struct mutex *lock, long state, unsigned int subclass, + unsigned long ip) +{ + struct task_struct *task = current; + struct mutex_waiter waiter; + unsigned long flags; + + preempt_disable(); + mutex_acquire(&lock->dep_map, subclass, 0, ip); + + if (mutex_spin(lock)) + return 0; + spin_lock_mutex(&lock->wait_lock, flags); debug_mutex_lock_common(lock, &waiter); @@ -473,6 +480,25 @@ int __sched mutex_trylock(struct mutex * } EXPORT_SYMBOL(mutex_trylock); +static inline int __mutex_tryspin_slowpath(atomic_t *lock_count) +{ + struct mutex *lock = container_of(lock_count, struct mutex, count); + + return __mutex_trylock_slowpath(lock_count) || mutex_spin(lock); +} + +int __sched mutex_tryspin(struct mutex *lock) +{ + int ret; + + ret = __mutex_fastpath_trylock(&lock->count, __mutex_tryspin_slowpath); + if (ret) + mutex_set_owner(lock); + + return ret; +} +EXPORT_SYMBOL(mutex_tryspin); + /** * atomic_dec_and_mutex_lock - return holding mutex if we dec to 0 * @cnt: the atomic which we are to dec