From: Trond Myklebust Subject: Re: [RFC] Add support for semaphore-like structure with support for asynchronous I/O Date: Thu, 31 Mar 2005 07:31:30 -0500 Message-ID: <1112272290.10975.71.camel@lade.trondhjem.org> References: <1112219491.10771.18.camel@lade.trondhjem.org> <16971.44683.69638.768508@gargle.gargle.HOWL> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit Cc: Linux Filesystem Development Return-path: Received: from pat.uio.no ([129.240.130.16]:61848 "EHLO pat.uio.no") by vger.kernel.org with ESMTP id S261387AbVCaMbr (ORCPT ); Thu, 31 Mar 2005 07:31:47 -0500 To: Nikita Danilov In-Reply-To: <16971.44683.69638.768508@gargle.gargle.HOWL> Sender: linux-fsdevel-owner@vger.kernel.org List-Id: linux-fsdevel.vger.kernel.org to den 31.03.2005 Klokka 12:02 (+0400) skreiv Nikita Danilov: > As I understand it, in the blocking path IOSEM_LOCK_EXCLUSIVE is set by > iosem_lock_wake_function() called by the waker thread. But this is > asking for convoy formation: iosem_unlock() transfers ownership of the > lock to the thread that is currently sleeping. This means that all > threads _running_ on another processors and bumping into that lock will > go to sleep too (i.e., lock is owned but unused), thus forming a > "convoy" that has a tendency to grow over time when there is at least > smallest contention. This is known problem with all "early ownership > transfer" locks designs (except maybe in your case contention is not > supposed to happen). You are assuming that all the waiters on the queue are tasks that must sleep if they cannot take the lock. That is not the case here. Whereas some users will indeed fall in this category, I expect that most will rather want to use the non-blocking mode in which the caller is free to go off and do other useful work. > And as a nitpick: struct iosem is emphatically _not_ a semaphore, it > even doesn't have a counter. :) Can it be named iomutex or iolock or > async_lock or something? We have enough confusion going on with struct > semaphore that is mostly used as mutex. I'm planning on expanding it to do more than mutexes. You can also implement shared locks using the same structure (in which case you do need a counter for the readers - the plan is to simply use the lower bits in the iosem->state for that).. > [...] > > > + > > +int fastcall iosem_lock_and_schedule_work(struct iosem *lk, struct iosem_work *wk) > > +{ > > + int ret; > > + > > + init_iosem_waiter(&wk->waiter); > > + wk->waiter.wait.func = iosem_lock_and_schedule_function; > > + ret = __iosem_lock(lk, &wk->waiter); > > + if (ret == 0) > > + ret = schedule_work(&wk->work); > > + return ret; > > +} > > This is actually trylock, right? If iosem_lock_and_schedule_work() > returns -EINPROGRESS lock is not acquired on return and caller has to > call schedule(). This is definitely not the same as trylock. It is the support for asynchronously grabbing the lock. The function containing the critical code that needs to be executed with the lock held should be set up as a work queue item inside wk->work. No matter whether or not the lock could be immediately granted, the caller is never responsible for running that function directly. Rather, as soon as the lock is granted, the work queue item wk->work will be automatically scheduled using keventd. Cheers, Trond -- Trond Myklebust