From patchwork Sat Apr 9 13:28:48 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pekka Enberg X-Patchwork-Id: 695971 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 p39DSsHI014589 for ; Sat, 9 Apr 2011 13:28:54 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756482Ab1DIN2v (ORCPT ); Sat, 9 Apr 2011 09:28:51 -0400 Received: from filtteri1.pp.htv.fi ([213.243.153.184]:51178 "EHLO filtteri1.pp.htv.fi" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753805Ab1DIN2u (ORCPT ); Sat, 9 Apr 2011 09:28:50 -0400 Received: from localhost (localhost [127.0.0.1]) by filtteri1.pp.htv.fi (Postfix) with ESMTP id D5BC48BBBD; Sat, 9 Apr 2011 16:28:49 +0300 (EEST) X-Virus-Scanned: Debian amavisd-new at pp.htv.fi Received: from smtp5.welho.com ([213.243.153.39]) by localhost (filtteri1.pp.htv.fi [213.243.153.184]) (amavisd-new, port 10024) with ESMTP id bLMhOLHMTrz2; Sat, 9 Apr 2011 16:28:49 +0300 (EEST) Received: from localhost.localdomain (cs181148025.pp.htv.fi [82.181.148.25]) by smtp5.welho.com (Postfix) with ESMTP id 73CA15BC005; Sat, 9 Apr 2011 16:28:49 +0300 (EEST) From: Pekka Enberg To: kvm@vger.kernel.org Cc: Pekka Enberg , Asias He , Cyrill Gorcunov , Ingo Molnar Subject: [PATCH] kvm tools: Use mutex_lock() and mutex_unlock() wrappers Date: Sat, 9 Apr 2011 16:28:48 +0300 Message-Id: <1302355728-11896-1-git-send-email-penberg@kernel.org> X-Mailer: git-send-email 1.7.0.4 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@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]); Sat, 09 Apr 2011 13:28:54 +0000 (UTC) This patch implements less hostile mutex_lock() and mutex_lock() wrappers on top of the pthread API equivalents as suggested by Ingo Molnar: glibc/pthreads mutex API semantics are pretty silly IMO. I *think* it would be better to try to match the kernel API here, and provide trivial wrappers around mutex_lock()/mutex_unlock(). We wont ever bring down threads in a hostile way, so we wont actually need the error returns. CPU threads should probably only exit once the kvm process exits, after all cleanup has been done. That way usage would be more obvious and more familar to kernel developers :-) [ It would also open up the possibility, in the far future, to bring lockdep to user-space ;-) ] Cc: Asias He Cc: Cyrill Gorcunov Cc: Ingo Molnar Signed-off-by: Pekka Enberg --- tools/kvm/8250-serial.c | 19 +++++++------------ tools/kvm/include/kvm/mutex.h | 20 ++++++++++++++++++++ tools/kvm/virtio-blk.c | 13 +++++-------- 3 files changed, 32 insertions(+), 20 deletions(-) create mode 100644 tools/kvm/include/kvm/mutex.h diff --git a/tools/kvm/8250-serial.c b/tools/kvm/8250-serial.c index 9394e88..6d4b216 100644 --- a/tools/kvm/8250-serial.c +++ b/tools/kvm/8250-serial.c @@ -2,6 +2,7 @@ #include "kvm/read-write.h" #include "kvm/ioport.h" +#include "kvm/mutex.h" #include "kvm/util.h" #include "kvm/term.h" #include "kvm/kvm.h" @@ -116,8 +117,7 @@ void serial8250__inject_interrupt(struct kvm *self) { struct serial8250_device *dev = &devices[0]; - if (pthread_mutex_lock(&dev->mutex) != 0) - die("pthread_mutex_lock"); + mutex_lock(&dev->mutex); serial8250__receive(self, dev); @@ -133,8 +133,7 @@ void serial8250__inject_interrupt(struct kvm *self) kvm__irq_line(self, dev->irq, 1); } - if (pthread_mutex_unlock(&dev->mutex) != 0) - die("pthread_mutex_unlock"); + mutex_unlock(&dev->mutex); } void serial8250__inject_sysrq(struct kvm *self) @@ -165,8 +164,7 @@ static bool serial8250_out(struct kvm *self, uint16_t port, void *data, int size if (!dev) return false; - if (pthread_mutex_lock(&dev->mutex) != 0) - die("pthread_mutex_lock"); + mutex_lock(&dev->mutex); offset = port - dev->iobase; @@ -239,8 +237,7 @@ static bool serial8250_out(struct kvm *self, uint16_t port, void *data, int size } out_unlock: - if (pthread_mutex_unlock(&dev->mutex) != 0) - die("pthread_mutex_unlock"); + mutex_unlock(&dev->mutex); return ret; } @@ -255,8 +252,7 @@ static bool serial8250_in(struct kvm *self, uint16_t port, void *data, int size, if (!dev) return false; - if (pthread_mutex_lock(&dev->mutex) != 0) - die("pthread_mutex_lock"); + mutex_lock(&dev->mutex); offset = port - dev->iobase; @@ -321,8 +317,7 @@ static bool serial8250_in(struct kvm *self, uint16_t port, void *data, int size, goto out_unlock; } out_unlock: - if (pthread_mutex_unlock(&dev->mutex) != 0) - die("pthread_mutex_unlock"); + mutex_unlock(&dev->mutex); return ret; } diff --git a/tools/kvm/include/kvm/mutex.h b/tools/kvm/include/kvm/mutex.h new file mode 100644 index 0000000..006ade3 --- /dev/null +++ b/tools/kvm/include/kvm/mutex.h @@ -0,0 +1,20 @@ +#ifndef KVM__MUTEX_H +#define KVM__MUTEX_H + +#include + +#include "kvm/util.h" + +static inline void mutex_lock(pthread_mutex_t *mutex) +{ + if (pthread_mutex_lock(mutex) != 0) + die("unexpected pthread_mutex_lock() failure!"); +} + +static inline void mutex_unlock(pthread_mutex_t *mutex) +{ + if (pthread_mutex_unlock(mutex) != 0) + die("unexpected pthread_mutex_unlock() failure!"); +} + +#endif /* KVM__MUTEX_H */ diff --git a/tools/kvm/virtio-blk.c b/tools/kvm/virtio-blk.c index 5e7f57d..79f27c1 100644 --- a/tools/kvm/virtio-blk.c +++ b/tools/kvm/virtio-blk.c @@ -5,6 +5,7 @@ #include "kvm/disk-image.h" #include "kvm/virtio.h" #include "kvm/ioport.h" +#include "kvm/mutex.h" #include "kvm/util.h" #include "kvm/kvm.h" #include "kvm/pci.h" @@ -70,8 +71,7 @@ static bool virtio_blk_pci_io_in(struct kvm *self, uint16_t port, void *data, in unsigned long offset; bool ret = true; - if (pthread_mutex_lock(&blk_device.mutex) != 0) - die("pthread_mutex_lock"); + mutex_lock(&blk_device.mutex); offset = port - IOPORT_VIRTIO_BLK; @@ -108,8 +108,7 @@ static bool virtio_blk_pci_io_in(struct kvm *self, uint16_t port, void *data, in }; out_unlock: - if (pthread_mutex_unlock(&blk_device.mutex) != 0) - die("pthread_mutex_unlock"); + mutex_unlock(&blk_device.mutex); return ret; } @@ -180,8 +179,7 @@ static bool virtio_blk_pci_io_out(struct kvm *self, uint16_t port, void *data, i unsigned long offset; bool ret = true; - if (pthread_mutex_lock(&blk_device.mutex) != 0) - die("pthread_mutex_lock"); + mutex_lock(&blk_device.mutex); offset = port - IOPORT_VIRTIO_BLK; @@ -226,8 +224,7 @@ static bool virtio_blk_pci_io_out(struct kvm *self, uint16_t port, void *data, i }; out_unlock: - if (pthread_mutex_unlock(&blk_device.mutex) != 0) - die("pthread_mutex_unlock"); + mutex_unlock(&blk_device.mutex); return ret; }