From patchwork Wed Jul 20 13:53:59 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shanker Donthineni X-Patchwork-Id: 9239647 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 184E660574 for ; Wed, 20 Jul 2016 13:56:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0930D27BF7 for ; Wed, 20 Jul 2016 13:56:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F257F27BFF; Wed, 20 Jul 2016 13:56:55 +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 autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 86B6627BF7 for ; Wed, 20 Jul 2016 13:56:55 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.85_2 #1 (Red Hat Linux)) id 1bPrxk-00074i-JZ; Wed, 20 Jul 2016 13:55:00 +0000 Received: from smtp.codeaurora.org ([198.145.29.96]) by bombadil.infradead.org with esmtps (Exim 4.85_2 #1 (Red Hat Linux)) id 1bPrxC-0006l9-1U for linux-arm-kernel@lists.infradead.org; Wed, 20 Jul 2016 13:54:28 +0000 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id C5A5961367; Wed, 20 Jul 2016 13:54:06 +0000 (UTC) Received: from shankerd-ubuntu.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: shankerd@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id B25C5611C4; Wed, 20 Jul 2016 13:54:05 +0000 (UTC) From: Shanker Donthineni To: Marc Zyngier , linux-kernel , linux-arm-kernel Subject: [PATCH V3 3/4] xen/arm: io: Use binary search for mmio handler lookup Date: Wed, 20 Jul 2016 08:53:59 -0500 Message-Id: <1469022840-2142-4-git-send-email-shankerd@codeaurora.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1469022840-2142-1-git-send-email-shankerd@codeaurora.org> References: <1469022840-2142-1-git-send-email-shankerd@codeaurora.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160720_065426_199072_45FFB00C X-CRM114-Status: GOOD ( 16.14 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Thomas Gleixner , Philip Elcan , Shanker Donthineni , Jason Cooper , Vikram Sethi MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP As the number of I/O handlers increase, the overhead associated with linear lookup also increases. The system might have maximum of 144 (assuming CONFIG_NR_CPUS=128) mmio handlers. In worst case scenario, it would require 144 iterations for finding a matching handler. Now it is time for us to change from linear (complexity O(n)) to a binary search (complexity O(log n) for reducing mmio handler lookup overhead. Signed-off-by: Shanker Donthineni --- xen/arch/arm/io.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/xen/arch/arm/io.c b/xen/arch/arm/io.c index 40330f0..e8aa7fa 100644 --- a/xen/arch/arm/io.c +++ b/xen/arch/arm/io.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -70,27 +71,31 @@ static int handle_write(const struct mmio_handler *handler, struct vcpu *v, handler->priv); } -static const struct mmio_handler *find_mmio_handler(struct domain *d, - paddr_t gpa) +/* This function assumes that mmio regions are not overlapped */ +static int cmp_mmio_handler(const void *key, const void *elem) { - const struct mmio_handler *handler; - unsigned int i; - struct vmmio *vmmio = &d->arch.vmmio; + const struct mmio_handler *handler0 = key; + const struct mmio_handler *handler1 = elem; - read_lock(&vmmio->lock); + if ( handler0->addr < handler1->addr ) + return -1; - for ( i = 0; i < vmmio->num_entries; i++ ) - { - handler = &vmmio->handlers[i]; + if ( handler0->addr > (handler1->addr + handler1->size) ) + return 1; - if ( (gpa >= handler->addr) && - (gpa < (handler->addr + handler->size)) ) - break; - } + return 0; +} - if ( i == vmmio->num_entries ) - handler = NULL; +static const struct mmio_handler *find_mmio_handler(struct domain *d, + paddr_t gpa) +{ + struct vmmio *vmmio = &d->arch.vmmio; + struct mmio_handler key = {.addr = gpa}; + const struct mmio_handler *handler; + read_lock(&vmmio->lock); + handler = bsearch(&key, vmmio->handlers, vmmio->num_entries, + sizeof(*handler), cmp_mmio_handler); read_unlock(&vmmio->lock); return handler; @@ -131,6 +136,10 @@ void register_mmio_handler(struct domain *d, vmmio->num_entries++; + /* Sort mmio handlers in ascending order based on base address */ + sort(vmmio->handlers, vmmio->num_entries, sizeof(struct mmio_handler), + cmp_mmio_handler, NULL); + write_unlock(&vmmio->lock); }