From patchwork Mon May 30 08:30:47 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 829462 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 p4U8VqMq026511 for ; Mon, 30 May 2011 08:33:28 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751464Ab1E3Ibu (ORCPT ); Mon, 30 May 2011 04:31:50 -0400 Received: from mail-wy0-f174.google.com ([74.125.82.174]:44843 "EHLO mail-wy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751139Ab1E3Ibt (ORCPT ); Mon, 30 May 2011 04:31:49 -0400 Received: by mail-wy0-f174.google.com with SMTP id 21so2460435wya.19 for ; Mon, 30 May 2011 01:31:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:date:message-id:x-mailer :in-reply-to:references; bh=rxl4LYQhF6OKbUUSXXXPuqi/MNUNsaP9Vj3gRucrBLs=; b=qVJAZuDwx9bkLPeoyD3HsArPAT8Y2w3f+cb/qhaxF5KtBe6sUOF8wdAyVza9iSFjCc 0f20FIOGo9+Ts/Vyw25DdE9CiHjToroSTxfq+JxCdoVfnUKW5Wz3zPgZEa0KtKD97w/+ Vgok/NGD/OHyzGOu2NvWhBsmxmFy3aCMOHtTw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; b=phdT6YIVyElBfK1ChCzlmGKbwO39lpdmUgT8fXG0XAA/BpK4uyUN94UHNcPTUG98wr pgEuQPmM7XcQHiUq70fqv+QDiXaVBLe8Z7bQAIdF1xiSTd85sAo6UjaH+vJXVQNWO6lA zGAuaJQpM6kdz8jdsd0v7ZrSAhxEiOLxYK3jM= Received: by 10.216.232.23 with SMTP id m23mr4536048weq.26.1306744308633; Mon, 30 May 2011 01:31:48 -0700 (PDT) Received: from localhost.localdomain (bzq-79-181-201-100.red.bezeqint.net [79.181.201.100]) by mx.google.com with ESMTPS id z66sm2331147weq.24.2011.05.30.01.31.47 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 30 May 2011 01:31:48 -0700 (PDT) From: Sasha Levin To: penberg@kernel.org Cc: kvm@vger.kernel.org, mingo@elte.hu, asias.hejun@gmail.com, gorcunov@gmail.com, prasadjoshi124@gmail.com, Sasha Levin Subject: [PATCH v2 8/8] kvm tools: Use brlock in MMIO and IOPORT Date: Mon, 30 May 2011 11:30:47 +0300 Message-Id: <1306744247-26051-8-git-send-email-levinsasha928@gmail.com> X-Mailer: git-send-email 1.7.5.rc3 In-Reply-To: <1306744247-26051-1-git-send-email-levinsasha928@gmail.com> References: <1306744247-26051-1-git-send-email-levinsasha928@gmail.com> 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]); Mon, 30 May 2011 08:33:28 +0000 (UTC) Use brlock to protect mmio and ioport modules and make them update-safe. Signed-off-by: Sasha Levin --- tools/kvm/ioport.c | 10 +++++++++- tools/kvm/mmio.c | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/tools/kvm/ioport.c b/tools/kvm/ioport.c index d0a1aa8..e00fb59 100644 --- a/tools/kvm/ioport.c +++ b/tools/kvm/ioport.c @@ -2,7 +2,7 @@ #include "kvm/kvm.h" #include "kvm/util.h" - +#include "kvm/brlock.h" #include "kvm/rbtree-interval.h" #include "kvm/mutex.h" @@ -84,6 +84,7 @@ u16 ioport__register(u16 port, struct ioport_operations *ops, int count, void *p { struct ioport *entry; + br_write_lock(); if (port == IOPORT_EMPTY) port = ioport__find_free_port(); @@ -105,6 +106,8 @@ u16 ioport__register(u16 port, struct ioport_operations *ops, int count, void *p ioport_insert(&ioport_tree, entry); + br_write_unlock(); + return port; } @@ -127,6 +130,7 @@ bool kvm__emulate_io(struct kvm *kvm, u16 port, void *data, int direction, int s bool ret = false; struct ioport *entry; + br_read_lock(); entry = ioport_search(&ioport_tree, port); if (!entry) goto error; @@ -141,11 +145,15 @@ bool kvm__emulate_io(struct kvm *kvm, u16 port, void *data, int direction, int s ret = ops->io_out(entry, kvm, port, data, size, count); } + br_read_unlock(); + if (!ret) goto error; return true; error: + br_read_unlock(); + if (ioport_debug) ioport_error(port, data, direction, size, count); diff --git a/tools/kvm/mmio.c b/tools/kvm/mmio.c index ef986bf..a57abeb 100644 --- a/tools/kvm/mmio.c +++ b/tools/kvm/mmio.c @@ -1,5 +1,6 @@ #include "kvm/kvm.h" #include "kvm/rbtree-interval.h" +#include "kvm/brlock.h" #include #include @@ -55,41 +56,53 @@ static const char *to_direction(u8 is_write) bool kvm__register_mmio(u64 phys_addr, u64 phys_addr_len, void (*kvm_mmio_callback_fn)(u64 addr, u8 *data, u32 len, u8 is_write)) { struct mmio_mapping *mmio; + int ret; mmio = malloc(sizeof(*mmio)); if (mmio == NULL) return false; + br_write_lock(); *mmio = (struct mmio_mapping) { .node = RB_INT_INIT(phys_addr, phys_addr + phys_addr_len), .kvm_mmio_callback_fn = kvm_mmio_callback_fn, }; - return mmio_insert(&mmio_tree, mmio); + ret = mmio_insert(&mmio_tree, mmio); + br_write_unlock(); + + return ret; } bool kvm__deregister_mmio(u64 phys_addr) { struct mmio_mapping *mmio; + br_write_lock(); mmio = mmio_search_single(&mmio_tree, phys_addr); if (mmio == NULL) return false; rb_int_erase(&mmio_tree, &mmio->node); + br_write_unlock(); + free(mmio); return true; } bool kvm__emulate_mmio(struct kvm *kvm, u64 phys_addr, u8 *data, u32 len, u8 is_write) { - struct mmio_mapping *mmio = mmio_search(&mmio_tree, phys_addr, len); + struct mmio_mapping *mmio; + + br_read_lock(); + mmio = mmio_search(&mmio_tree, phys_addr, len); if (mmio) mmio->kvm_mmio_callback_fn(phys_addr, data, len, is_write); else fprintf(stderr, "Warning: Ignoring MMIO %s at %016llx (length %u)\n", to_direction(is_write), phys_addr, len); + br_read_unlock(); return true; }