From patchwork Thu Jun 22 18:57:07 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Boris Ostrovsky X-Patchwork-Id: 9805099 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id EDB2160329 for ; Thu, 22 Jun 2017 18:57:47 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DE3F3283DA for ; Thu, 22 Jun 2017 18:57:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D2B29286E1; Thu, 22 Jun 2017 18:57:47 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 40BAC283DA for ; Thu, 22 Jun 2017 18:57:47 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dO7GZ-0005RK-La; Thu, 22 Jun 2017 18:55:43 +0000 Received: from mail6.bemta5.messagelabs.com ([195.245.231.135]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dO7GY-0005PF-Di for xen-devel@lists.xen.org; Thu, 22 Jun 2017 18:55:42 +0000 Received: from [85.158.139.211] by server-10.bemta-5.messagelabs.com id 96/48-01734-DA21C495; Thu, 22 Jun 2017 18:55:41 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFvrLLMWRWlGSWpSXmKPExsUyZ7p8oO5aIZ9 Ig2+LWC2WfFzM4sDocXT3b6YAxijWzLyk/IoE1oyDk5+zF1yVqeg7tJ6xgbFbrIuRi0NIoINJ Yt+Nb2wQzjdGicWnOlggnI2MEudu/meFcHoYJTZ8/AzkcHKwCRhJnD06nRHEFhGQlrj2+TIjS BGzQAOTxPNzB8ESwgJWEqsPXgOzWQRUJZ6/6gGzeQW8JBbuhIhLCChITHn4nhnE5hTwlnh86i 1YXAioZunbtewQNcYSfbP6WCYw8i1gZFjFqF6cWlSWWqRroZdUlJmeUZKbmJmja2hgqpebWly cmJ6ak5hUrJecn7uJERgsDECwg/Fgs/MhRkkOJiVR3s3F3pFCfEn5KZUZicUZ8UWlOanFhxhl ODiUJHhXC/pECgkWpaanVqRl5gDDFiYtwcGjJML7hQ8ozVtckJhbnJkOkTrFqCglzlsO0icAk sgozYNrg8XKJUZZKWFeRqBDhHgKUotyM0tQ5V8xinMwKgnzigsATeHJzCuBm/4KaDET0OIXRz xAFpckIqSkGhgPRP2ziJpR5vux/GibfcyRt1NU52wKePo5NEE99y2/s2lL0gavaytvqa5QTWS /HWnw4qLUMZ0dlqfe8hecP/77zsG0uMtCxv313g2e780iODbw1LyZ5nM72kjLcmXQjJMtYabb 9qXOsWb/r5mVdNvy/dp00W4FkQdWm/i9H6y5ItDPZplVMFmJpTgj0VCLuag4EQAJRoMckAIAA A== X-Env-Sender: boris.ostrovsky@oracle.com X-Msg-Ref: server-11.tower-206.messagelabs.com!1498157739!84882391!1 X-Originating-IP: [156.151.31.81] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogMTU2LjE1MS4zMS44MSA9PiAyODgzMzk=\n X-StarScan-Received: X-StarScan-Version: 9.4.19; banners=-,-,- X-VirusChecked: Checked Received: (qmail 49972 invoked from network); 22 Jun 2017 18:55:40 -0000 Received: from userp1040.oracle.com (HELO userp1040.oracle.com) (156.151.31.81) by server-11.tower-206.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 22 Jun 2017 18:55:40 -0000 Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v5MItZuU011683 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 22 Jun 2017 18:55:35 GMT Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0021.oracle.com (8.13.8/8.14.4) with ESMTP id v5MItZiS020206 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 22 Jun 2017 18:55:35 GMT Received: from abhmp0015.oracle.com (abhmp0015.oracle.com [141.146.116.21]) by aserv0121.oracle.com (8.13.8/8.13.8) with ESMTP id v5MItWgh003421; Thu, 22 Jun 2017 18:55:34 GMT Received: from ovs104.us.oracle.com (/10.149.76.204) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Thu, 22 Jun 2017 11:55:32 -0700 From: Boris Ostrovsky To: xen-devel@lists.xen.org Date: Thu, 22 Jun 2017 14:57:07 -0400 Message-Id: <1498157830-21845-6-git-send-email-boris.ostrovsky@oracle.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1498157830-21845-1-git-send-email-boris.ostrovsky@oracle.com> References: <1498157830-21845-1-git-send-email-boris.ostrovsky@oracle.com> X-Source-IP: aserv0021.oracle.com [141.146.126.233] Cc: sstabellini@kernel.org, wei.liu2@citrix.com, George.Dunlap@eu.citrix.com, andrew.cooper3@citrix.com, ian.jackson@eu.citrix.com, tim@xen.org, jbeulich@suse.com, Boris Ostrovsky Subject: [Xen-devel] [PATCH v5 5/8] spinlock: Introduce spin_lock_cb() X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP While waiting for a lock we may want to periodically run some code. This code may, for example, allow the caller to release resources held by it that are no longer needed in the critical section protected by the lock. Specifically, this feature will be needed by scrubbing code where the scrubber, while waiting for heap lock to merge back clean pages, may be requested by page allocator (which is currently holding the lock) to abort merging and release the buddy page head that the allocator wants. We could use spin_trylock() but since it doesn't take lock ticket it may take long time until the lock is taken. Instead we add spin_lock_cb() that allows us to grab the ticket and execute a callback while waiting. This callback is executed on every iteration of the spinlock waiting loop. Since we may be sleeping in the lock until it is released we need a mechanism that will make sure that the callback has a chance to run. We add spin_lock_kick() that will wake up the waiter. Signed-off-by: Boris Ostrovsky --- Changes in v5: * Added a sentence in commit message to note that callback function is called on every iteration of the spin loop. xen/common/spinlock.c | 9 ++++++++- xen/include/xen/spinlock.h | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/xen/common/spinlock.c b/xen/common/spinlock.c index 2a06406..3c1caae 100644 --- a/xen/common/spinlock.c +++ b/xen/common/spinlock.c @@ -129,7 +129,7 @@ static always_inline u16 observe_head(spinlock_tickets_t *t) return read_atomic(&t->head); } -void _spin_lock(spinlock_t *lock) +void inline _spin_lock_cb(spinlock_t *lock, void (*cb)(void *), void *data) { spinlock_tickets_t tickets = SPINLOCK_TICKET_INC; LOCK_PROFILE_VAR; @@ -140,6 +140,8 @@ void _spin_lock(spinlock_t *lock) while ( tickets.tail != observe_head(&lock->tickets) ) { LOCK_PROFILE_BLOCK; + if ( unlikely(cb) ) + cb(data); arch_lock_relax(); } LOCK_PROFILE_GOT; @@ -147,6 +149,11 @@ void _spin_lock(spinlock_t *lock) arch_lock_acquire_barrier(); } +void _spin_lock(spinlock_t *lock) +{ + _spin_lock_cb(lock, NULL, NULL); +} + void _spin_lock_irq(spinlock_t *lock) { ASSERT(local_irq_is_enabled()); diff --git a/xen/include/xen/spinlock.h b/xen/include/xen/spinlock.h index c1883bd..91bfb95 100644 --- a/xen/include/xen/spinlock.h +++ b/xen/include/xen/spinlock.h @@ -153,6 +153,7 @@ typedef struct spinlock { #define spin_lock_init(l) (*(l) = (spinlock_t)SPIN_LOCK_UNLOCKED) void _spin_lock(spinlock_t *lock); +void _spin_lock_cb(spinlock_t *lock, void (*cond)(void *), void *data); void _spin_lock_irq(spinlock_t *lock); unsigned long _spin_lock_irqsave(spinlock_t *lock); @@ -169,6 +170,7 @@ void _spin_lock_recursive(spinlock_t *lock); void _spin_unlock_recursive(spinlock_t *lock); #define spin_lock(l) _spin_lock(l) +#define spin_lock_cb(l, c, d) _spin_lock_cb(l, c, d) #define spin_lock_irq(l) _spin_lock_irq(l) #define spin_lock_irqsave(l, f) \ ({ \ @@ -190,6 +192,12 @@ void _spin_unlock_recursive(spinlock_t *lock); 1 : ({ local_irq_restore(flags); 0; }); \ }) +#define spin_lock_kick(l) \ +({ \ + smp_mb(); \ + arch_lock_signal(); \ +}) + /* Ensure a lock is quiescent between two critical operations. */ #define spin_barrier(l) _spin_barrier(l)