From patchwork Wed May 13 15:13:38 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Williamson X-Patchwork-Id: 23543 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n4DFDUqt018364 for ; Wed, 13 May 2009 15:13:30 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755926AbZEMPN0 (ORCPT ); Wed, 13 May 2009 11:13:26 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1755415AbZEMPNZ (ORCPT ); Wed, 13 May 2009 11:13:25 -0400 Received: from g5t0006.atlanta.hp.com ([15.192.0.43]:45260 "EHLO g5t0006.atlanta.hp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753296AbZEMPNZ (ORCPT ); Wed, 13 May 2009 11:13:25 -0400 Received: from g5t0029.atlanta.hp.com (g5t0029.atlanta.hp.com [16.228.8.141]) by g5t0006.atlanta.hp.com (Postfix) with ESMTP id 5E0ADC7ED; Wed, 13 May 2009 15:13:26 +0000 (UTC) Received: from ldl.fc.hp.com (ldl.fc.hp.com [15.11.146.30]) by g5t0029.atlanta.hp.com (Postfix) with ESMTP id 16D4A100B8; Wed, 13 May 2009 15:13:26 +0000 (UTC) Received: from localhost (ldl.fc.hp.com [127.0.0.1]) by ldl.fc.hp.com (Postfix) with ESMTP id B8F0739C003; Wed, 13 May 2009 09:13:25 -0600 (MDT) X-Virus-Scanned: Debian amavisd-new at ldl.fc.hp.com Received: from ldl.fc.hp.com ([127.0.0.1]) by localhost (ldl.fc.hp.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id QY3NkI7xiaJn; Wed, 13 May 2009 09:13:23 -0600 (MDT) Received: from dl380g6-3.ned.telco.ned.telco (lart.fc.hp.com [15.11.146.31]) by ldl.fc.hp.com (Postfix) with ESMTP id 02CD239C007; Wed, 13 May 2009 09:13:23 -0600 (MDT) From: Alex Williamson Subject: [PATCH v5] kvm: Use a bitmap for tracking used GSIs To: kvm@vger.kernel.org, sheng.yang@intel.com, mst@redhat.com Cc: alex.williamson@hp.com Date: Wed, 13 May 2009 09:13:38 -0600 Message-ID: <20090513150815.8221.49221.stgit@dl380g6-3.ned.telco.ned.telco> In-Reply-To: <20090513043835.6696.27384.stgit@dl380g6-3.ned.telco.ned.telco> References: <20090513043835.6696.27384.stgit@dl380g6-3.ned.telco.ned.telco> User-Agent: StGIT/0.14.2 MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org We're currently using a counter to track the most recent GSI we've handed out. This quickly hits KVM_MAX_IRQ_ROUTES when using device assignment with a driver that regularly toggles the MSI enable bit. This can mean only a few minutes of usable run time. Instead, track used GSIs in a bitmap. Signed-off-by: Alex Williamson --- v2: Added mutex to protect gsi bitmap v3: Updated for comments from Michael Tsirkin No longer depends on "[PATCH] kvm: device-assignment: Catch GSI overflow" v4: Fix gsi_bytes calculation noted by Sheng Yang v5: Remove mutex per Avi Fix negative gsi_count path per Michael Remove KVM_CAP_IRQ_ROUTING per Michael, ppc should still be protected by the KVM_IOAPIC_NUM_PINS check hw/device-assignment.c | 4 ++- kvm/libkvm/kvm-common.h | 3 +- kvm/libkvm/libkvm.c | 74 ++++++++++++++++++++++++++++++++++++++--------- kvm/libkvm/libkvm.h | 10 ++++++ 4 files changed, 75 insertions(+), 16 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/hw/device-assignment.c b/hw/device-assignment.c index a7365c8..a6cc9b9 100644 --- a/hw/device-assignment.c +++ b/hw/device-assignment.c @@ -561,8 +561,10 @@ static void free_dev_irq_entries(AssignedDevice *dev) { int i; - for (i = 0; i < dev->irq_entries_nr; i++) + for (i = 0; i < dev->irq_entries_nr; i++) { kvm_del_routing_entry(kvm_context, &dev->entry[i]); + kvm_free_irq_route_gsi(kvm_context, dev->entry[i].gsi); + } free(dev->entry); dev->entry = NULL; dev->irq_entries_nr = 0; diff --git a/kvm/libkvm/kvm-common.h b/kvm/libkvm/kvm-common.h index 591fb53..c95c591 100644 --- a/kvm/libkvm/kvm-common.h +++ b/kvm/libkvm/kvm-common.h @@ -67,7 +67,8 @@ struct kvm_context { struct kvm_irq_routing *irq_routes; int nr_allocated_irq_routes; #endif - int max_used_gsi; + void *used_gsi_bitmap; + int max_gsi; }; int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory, diff --git a/kvm/libkvm/libkvm.c b/kvm/libkvm/libkvm.c index ba0a5d1..74fb59b 100644 --- a/kvm/libkvm/libkvm.c +++ b/kvm/libkvm/libkvm.c @@ -61,10 +61,13 @@ #define DPRINTF(fmt, args...) do {} while (0) #endif +#define min(x,y) ((x) < (y) ? (x) : (y)) int kvm_abi = EXPECTED_KVM_API_VERSION; int kvm_page_size; +static inline void set_bit(uint32_t *buf, unsigned int bit); + struct slot_info { unsigned long phys_addr; unsigned long len; @@ -285,7 +288,7 @@ kvm_context_t kvm_init(struct kvm_callbacks *callbacks, { int fd; kvm_context_t kvm; - int r; + int r, gsi_count; fd = open("/dev/kvm", O_RDWR); if (fd == -1) { @@ -323,6 +326,28 @@ kvm_context_t kvm_init(struct kvm_callbacks *callbacks, kvm->no_irqchip_creation = 0; kvm->no_pit_creation = 0; + gsi_count = kvm_get_gsi_count(kvm); + if (gsi_count > 0) { + int gsi_bytes, i; + + /* Round up so we can search ints using ffs */ + gsi_bytes = ((gsi_count + 31) / 32) * 4; + kvm->used_gsi_bitmap = malloc(gsi_bytes); + if (!kvm->used_gsi_bitmap) + goto out_close; + memset(kvm->used_gsi_bitmap, 0, gsi_bytes); + kvm->max_gsi = gsi_bytes * 8; + + /* Mark all the IOAPIC pin GSIs and any over-allocated + * GSIs as already in use. */ +#ifdef KVM_IOAPIC_NUM_PINS + for (i = 0; i < min(KVM_IOAPIC_NUM_PINS, gsi_count); i++) + set_bit(kvm->used_gsi_bitmap, i); +#endif + for (i = gsi_count; i < kvm->max_gsi; i++) + set_bit(kvm->used_gsi_bitmap, i); + } + return kvm; out_close: close(fd); @@ -1298,8 +1323,6 @@ int kvm_add_routing_entry(kvm_context_t kvm, new->flags = entry->flags; new->u = entry->u; - if (entry->gsi > kvm->max_used_gsi) - kvm->max_used_gsi = entry->gsi; return 0; #else return -ENOSYS; @@ -1404,19 +1427,42 @@ int kvm_commit_irq_routes(kvm_context_t kvm) #endif } +static inline void set_bit(uint32_t *buf, unsigned int bit) +{ + buf[bit / 32] |= 1U << (bit % 32); +} + +static inline void clear_bit(uint32_t *buf, unsigned int bit) +{ + buf[bit / 32] &= ~(1U << (bit % 32)); +} + +static int kvm_find_free_gsi(kvm_context_t kvm) +{ + int i, bit, gsi; + uint32_t *buf = kvm->used_gsi_bitmap; + + for (i = 0; i < kvm->max_gsi / 32; i++) { + bit = ffs(~buf[i]); + if (!bit) + continue; + + gsi = bit - 1 + i * 32; + set_bit(buf, gsi); + return gsi; + } + + return -ENOSPC; +} + int kvm_get_irq_route_gsi(kvm_context_t kvm) { -#ifdef KVM_CAP_IRQ_ROUTING - if (kvm->max_used_gsi >= KVM_IOAPIC_NUM_PINS) { - if (kvm->max_used_gsi <= kvm_get_gsi_count(kvm)) - return kvm->max_used_gsi + 1; - else - return -ENOSPC; - } else - return KVM_IOAPIC_NUM_PINS; -#else - return -ENOSYS; -#endif + return kvm_find_free_gsi(kvm); +} + +void kvm_free_irq_route_gsi(kvm_context_t kvm, int gsi) +{ + clear_bit(kvm->used_gsi_bitmap, gsi); } #ifdef KVM_CAP_DEVICE_MSIX diff --git a/kvm/libkvm/libkvm.h b/kvm/libkvm/libkvm.h index 4821a1e..845bb8a 100644 --- a/kvm/libkvm/libkvm.h +++ b/kvm/libkvm/libkvm.h @@ -856,6 +856,16 @@ int kvm_commit_irq_routes(kvm_context_t kvm); */ int kvm_get_irq_route_gsi(kvm_context_t kvm); +/*! + * \brief Free used GSI number + * + * Free used GSI number acquired from kvm_get_irq_route_gsi() + * + * \param kvm Pointer to the current kvm_context + * \param gsi GSI number to free + */ +void kvm_free_irq_route_gsi(kvm_context_t kvm, int gsi); + #ifdef KVM_CAP_DEVICE_MSIX int kvm_assign_set_msix_nr(kvm_context_t kvm, struct kvm_assigned_msix_nr *msix_nr);