Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp1309797rwo; Wed, 2 Aug 2023 11:54:09 -0700 (PDT) X-Google-Smtp-Source: APBJJlGT//IagL940soBRrZK4DAYXYy60VBd2EgGg0pUHU0C3UebIFNIUkpv2Wd/S92UQZM0dMIM X-Received: by 2002:a17:907:7782:b0:994:54af:e282 with SMTP id ky2-20020a170907778200b0099454afe282mr5731636ejc.10.1691002449313; Wed, 02 Aug 2023 11:54:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691002449; cv=none; d=google.com; s=arc-20160816; b=c/hhkQgHxGYRUDyPP0kW3DHZHchR9Y00xe2MHoP7xEtYsGITx64pYMhb2hzmrx5gUW +ubJndLj6IjoJACCBtcGLN/U7eJaGG/oNC2IQblQP0OTi9lvZLSYuqeeVgxPdEPLjBPP LyVKyNBX2Xd6+WyNJ6YFcVx1/6hP9lvuhgz/ecUGCgQZCqdahWnQojkLE/VjLX8NkB21 5AkaU/LTMpyi5+pkeGnBHFcJFVKkUuWV8wtus/S9sXgEPg6fncJjhpj9RYCfiHvJzzmZ iKn6dxUmfq1MsaEzBHbIfqbbwNoTq5muOGSC4SO3qVIt7mfOj17omMvWCkMSdJuQnwFD KkNg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=ZVdmius26RlSXht2Utt64qaaZXvCxBapXtuIJrKTQ8A=; fh=yi2ojIX5efqtkINRikL2maCs0tMSj0r16O5TzeadjFo=; b=QD/o9L02XgoSg92mvffmhe1MBpZJvxVXtw5QxujaX499MvHbKDZIAJ/C0ny1Aia7qP ca+DuSGL3BEP+hx1lzRrgOAX+SPBTCuyznp3j0cIWkUhkWLozS61VjerJ93nlMy5eQFW Tt8QyUp5leBVSlON1rf8M51XiZYOr7tSm0Ddx3wUUvG6MYcrngRSSarcRWMOjghXaRPw E41o7mpOJO9V6PEX6S2r2Tmb54SWm/YBRL2VFqcfpXhhWY/yWkoweG0S1TuGTbx38uL/ JHPQIL4C83zK5bu6gAL/b+xex8n9ivxgKoSyjuB0ru7SBFfQ6D9aISrOjh7+4IHg3yhB aGAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZJlwRzMb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g11-20020a170906594b00b00992b55d152esi10688386ejr.242.2023.08.02.11.53.45; Wed, 02 Aug 2023 11:54:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=ZJlwRzMb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234324AbjHBSbI (ORCPT + 99 others); Wed, 2 Aug 2023 14:31:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231775AbjHBSbG (ORCPT ); Wed, 2 Aug 2023 14:31:06 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05D8419A8 for ; Wed, 2 Aug 2023 11:30:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691001023; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZVdmius26RlSXht2Utt64qaaZXvCxBapXtuIJrKTQ8A=; b=ZJlwRzMb4wLXeVzkBSLXRDEQCcL55pjo7iNL9v6MBQlxVpiwwVn75ybqgRJe2i8CYNnnJb cqIFfFM7w1MO68144xEgia0gY55uqa8Z8hHDX7sTmFBjXa4KTgTornxWuR6bii03wtgP48 G/3ANnNcoqd2wblkD5DOSQrTroARhpw= Received: from mail-io1-f70.google.com (mail-io1-f70.google.com [209.85.166.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-195-Uy9w5EL-N52MMiF-MPsLtQ-1; Wed, 02 Aug 2023 14:30:21 -0400 X-MC-Unique: Uy9w5EL-N52MMiF-MPsLtQ-1 Received: by mail-io1-f70.google.com with SMTP id ca18e2360f4ac-7867b689079so10044539f.0 for ; Wed, 02 Aug 2023 11:30:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691001020; x=1691605820; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZVdmius26RlSXht2Utt64qaaZXvCxBapXtuIJrKTQ8A=; b=I6puyH2ajB5a4h5yNlsiHZPz9YOrWLXN1t6M6t5XKSHHCDGSyT2Qytg5QUtIRwcGuO FCLTEzxtdAVxIugYzeVHF6+03MBuZ8qaXz+lDjmJh11rKhLCAWYoknkw1LD6ZRGQaNDb E4qV/e/srF8iDQ9RPkKf9dejUjSNeWDKMSktG0n5IfAMQ+G1T8kJPIKscqwwPs7H5CyJ xS7BrKn3F5agSl13eUzk5ftNdFaciSLj6V2+wJKiKHz7VuP/rIp7aDLK7wHOj+apCxaD cToggbVr2XZiGFFuKPQfzWKuhGOuI1CqAcMeoXW7BQ3kKgkTf1mqT/BB38B5B89Qg2SV ggLA== X-Gm-Message-State: ABy/qLbcSBtiuD0lKDHLOtc8nRjtNBRJjQtMV9vVTkwVWiaEJlULRTJb jnNUiSn3Ni34BJh4YrZ38TBWhjdElRgff2EASfeQxp06Wu0Fpbytpymcn9ifcsBmLgU84HE8WNb S5Y9ddInKULmcf6U1c1sRHwdv X-Received: by 2002:a05:6e02:1d95:b0:343:c8b1:b7f0 with SMTP id h21-20020a056e021d9500b00343c8b1b7f0mr20169463ila.23.1691001020469; Wed, 02 Aug 2023 11:30:20 -0700 (PDT) X-Received: by 2002:a05:6e02:1d95:b0:343:c8b1:b7f0 with SMTP id h21-20020a056e021d9500b00343c8b1b7f0mr20169442ila.23.1691001020147; Wed, 02 Aug 2023 11:30:20 -0700 (PDT) Received: from redhat.com ([38.15.60.12]) by smtp.gmail.com with ESMTPSA id z5-20020a02cea5000000b0042b4b1246cbsm4607233jaq.148.2023.08.02.11.30.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 11:30:19 -0700 (PDT) Date: Wed, 2 Aug 2023 12:30:17 -0600 From: Alex Williamson To: Like Xu Cc: Paolo Bonzini , linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: Re: [PATCH v2 2/2] KVM: irqbypass: Convert producers/consumers single linked list to XArray Message-ID: <20230802123017.5695fe0a.alex.williamson@redhat.com> In-Reply-To: <20230802051700.52321-3-likexu@tencent.com> References: <20230802051700.52321-1-likexu@tencent.com> <20230802051700.52321-3-likexu@tencent.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.35; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, 2 Aug 2023 13:17:00 +0800 Like Xu wrote: > From: Like Xu > > Replace producers/consumers linked list with XArray. There are no changes > in functionality, but lookup performance has been improved. > > The producers and consumers in current IRQ bypass manager are stored in > simple linked lists, and a single mutex is held while traversing the lists > and connecting a consumer to a producer (and vice versa). With this design > and implementation, if there are a large number of KVM agents concurrently > creating irqfds and all requesting to register their irqfds in the global > consumers list, the global mutex contention will exponentially increase > the avg wait latency, which is no longer tolerable in modern systems with > a large number of CPU cores. For example: > > the wait time latency to acquire the mutex in a stress test where 174000 > irqfds were created concurrently on an 2.70GHz ICX w/ 144 cores: > > - avg = 117.855314 ms > - min = 20 ns > - max = 11428.340858 ms > > To reduce latency introduced by the irq_bypass_register_consumer() in > the above usage scenario, the data structure XArray and its normal API > is applied to track the producers and consumers so that lookups don't > require a linear walk since the "tokens" used to match producers and > consumers are just kernel pointers. > > Thanks to the nature of XArray (more memory-efficient, parallelisable > and cache friendly), the latecny is significantly reduced (compared to > list and hlist proposal) under the same environment and testing: > > - avg = 314 ns > - min = 124 ns > - max = 47637 ns > > In this conversion, the non-NULL opaque token to match between producer > and consumer () is used as the XArray index. The list_for_each_entry() is > replaced by xa_load(), and list_add/del() is replaced by xa_store/erase(). > The list_head member for linked list is removed, along with comments. > > Cc: Alex Williamson > Reported-by: Yong He > Closes: https://bugzilla.kernel.org/show_bug.cgi?id=217379 > Suggested-by: Sean Christopherson > Suggested-by: Paolo Bonzini > Signed-off-by: Like Xu > --- > include/linux/irqbypass.h | 8 +-- > virt/lib/irqbypass.c | 127 +++++++++++++++++++------------------- > 2 files changed, 63 insertions(+), 72 deletions(-) > > diff --git a/include/linux/irqbypass.h b/include/linux/irqbypass.h > index 9bdb2a781841..dbcc1b4d0ccf 100644 > --- a/include/linux/irqbypass.h > +++ b/include/linux/irqbypass.h > @@ -8,14 +8,12 @@ > #ifndef IRQBYPASS_H > #define IRQBYPASS_H > > -#include > - > struct irq_bypass_consumer; > > /* > * Theory of operation > * > - * The IRQ bypass manager is a simple set of lists and callbacks that allows > + * The IRQ bypass manager is a simple set of xarrays and callbacks that allows > * IRQ producers (ex. physical interrupt sources) to be matched to IRQ > * consumers (ex. virtualization hardware that allows IRQ bypass or offload) > * via a shared token (ex. eventfd_ctx). Producers and consumers register > @@ -30,7 +28,6 @@ struct irq_bypass_consumer; > > /** > * struct irq_bypass_producer - IRQ bypass producer definition > - * @node: IRQ bypass manager private list management > * @token: opaque token to match between producer and consumer (non-NULL) > * @irq: Linux IRQ number for the producer device > * @add_consumer: Connect the IRQ producer to an IRQ consumer (optional) > @@ -43,7 +40,6 @@ struct irq_bypass_consumer; > * for a physical device assigned to a VM. > */ > struct irq_bypass_producer { > - struct list_head node; > void *token; > int irq; > int (*add_consumer)(struct irq_bypass_producer *, > @@ -56,7 +52,6 @@ struct irq_bypass_producer { > > /** > * struct irq_bypass_consumer - IRQ bypass consumer definition > - * @node: IRQ bypass manager private list management > * @token: opaque token to match between producer and consumer (non-NULL) > * @add_producer: Connect the IRQ consumer to an IRQ producer > * @del_producer: Disconnect the IRQ consumer from an IRQ producer > @@ -69,7 +64,6 @@ struct irq_bypass_producer { > * portions of the interrupt handling to the VM. > */ > struct irq_bypass_consumer { > - struct list_head node; > void *token; > int (*add_producer)(struct irq_bypass_consumer *, > struct irq_bypass_producer *); > diff --git a/virt/lib/irqbypass.c b/virt/lib/irqbypass.c > index e0aabbbf27ec..3f8736951e92 100644 > --- a/virt/lib/irqbypass.c > +++ b/virt/lib/irqbypass.c > @@ -15,15 +15,15 @@ > */ > > #include > -#include > #include > #include > +#include > > MODULE_LICENSE("GPL v2"); > MODULE_DESCRIPTION("IRQ bypass manager utility module"); > > -static LIST_HEAD(producers); > -static LIST_HEAD(consumers); > +static DEFINE_XARRAY(producers); > +static DEFINE_XARRAY(consumers); > static DEFINE_MUTEX(lock); > > /* @lock must be held when calling connect */ > @@ -78,11 +78,12 @@ static void __disconnect(struct irq_bypass_producer *prod, > * irq_bypass_register_producer - register IRQ bypass producer > * @producer: pointer to producer structure > * > - * Add the provided IRQ producer to the list of producers and connect > - * with any matching token found on the IRQ consumers list. > + * Add the provided IRQ producer to the xarray of producers and connect > + * with any matching token found on the IRQ consumers xarray. > */ > int irq_bypass_register_producer(struct irq_bypass_producer *producer) > { > + unsigned long token = (unsigned long)producer->token; > struct irq_bypass_producer *tmp; > struct irq_bypass_consumer *consumer; > int ret; > @@ -97,24 +98,23 @@ int irq_bypass_register_producer(struct irq_bypass_producer *producer) > > mutex_lock(&lock); > > - list_for_each_entry(tmp, &producers, node) { > - if (tmp->token == producer->token || tmp == producer) { > - ret = -EBUSY; > + tmp = xa_load(&producers, token); > + if (tmp || tmp == producer) { > + ret = -EBUSY; > + goto out_err; > + } > + > + ret = xa_err(xa_store(&producers, token, producer, GFP_KERNEL)); > + if (ret) > + goto out_err; > + > + consumer = xa_load(&consumers, token); > + if (consumer) { > + ret = __connect(producer, consumer); > + if (ret) > goto out_err; This doesn't match previous behavior, the producer is registered to the xarray regardless of the result of the connect operation and the caller cannot distinguish between failures. The module reference is released regardless of xarray item. Nak. > - } > } > > - list_for_each_entry(consumer, &consumers, node) { > - if (consumer->token == producer->token) { > - ret = __connect(producer, consumer); > - if (ret) > - goto out_err; > - break; > - } > - } > - > - list_add(&producer->node, &producers); > - > mutex_unlock(&lock); > > return 0; > @@ -129,11 +129,12 @@ EXPORT_SYMBOL_GPL(irq_bypass_register_producer); > * irq_bypass_unregister_producer - unregister IRQ bypass producer > * @producer: pointer to producer structure > * > - * Remove a previously registered IRQ producer from the list of producers > + * Remove a previously registered IRQ producer from the xarray of producers > * and disconnect it from any connected IRQ consumer. > */ > void irq_bypass_unregister_producer(struct irq_bypass_producer *producer) > { > + unsigned long token = (unsigned long)producer->token; > struct irq_bypass_producer *tmp; > struct irq_bypass_consumer *consumer; > > @@ -143,24 +144,18 @@ void irq_bypass_unregister_producer(struct irq_bypass_producer *producer) > might_sleep(); > > if (!try_module_get(THIS_MODULE)) > - return; /* nothing in the list anyway */ > + return; /* nothing in the xarray anyway */ > > mutex_lock(&lock); > > - list_for_each_entry(tmp, &producers, node) { > - if (tmp != producer) > - continue; > + tmp = xa_load(&producers, token); > + if (tmp == producer) { > + consumer = xa_load(&consumers, token); > + if (consumer) > + __disconnect(producer, consumer); > > - list_for_each_entry(consumer, &consumers, node) { > - if (consumer->token == producer->token) { > - __disconnect(producer, consumer); > - break; > - } > - } > - > - list_del(&producer->node); > + xa_erase(&producers, token); > module_put(THIS_MODULE); > - break; > } > > mutex_unlock(&lock); > @@ -173,11 +168,12 @@ EXPORT_SYMBOL_GPL(irq_bypass_unregister_producer); > * irq_bypass_register_consumer - register IRQ bypass consumer > * @consumer: pointer to consumer structure > * > - * Add the provided IRQ consumer to the list of consumers and connect > - * with any matching token found on the IRQ producer list. > + * Add the provided IRQ consumer to the xarray of consumers and connect > + * with any matching token found on the IRQ producer xarray. > */ > int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer) > { > + unsigned long token = (unsigned long)consumer->token; > struct irq_bypass_consumer *tmp; > struct irq_bypass_producer *producer; > int ret; > @@ -193,24 +189,23 @@ int irq_bypass_register_consumer(struct irq_bypass_consumer *consumer) > > mutex_lock(&lock); > > - list_for_each_entry(tmp, &consumers, node) { > - if (tmp->token == consumer->token || tmp == consumer) { > - ret = -EBUSY; > + tmp = xa_load(&consumers, token); > + if (tmp || tmp == consumer) { > + ret = -EBUSY; > + goto out_err; > + } > + > + ret = xa_err(xa_store(&consumers, token, consumer, GFP_KERNEL)); > + if (ret) > + goto out_err; > + > + producer = xa_load(&producers, token); > + if (producer) { > + ret = __connect(producer, consumer); > + if (ret) > goto out_err; Same. Thanks, Alex > - } > } > > - list_for_each_entry(producer, &producers, node) { > - if (producer->token == consumer->token) { > - ret = __connect(producer, consumer); > - if (ret) > - goto out_err; > - break; > - } > - } > - > - list_add(&consumer->node, &consumers); > - > mutex_unlock(&lock); > > return 0; > @@ -225,11 +220,12 @@ EXPORT_SYMBOL_GPL(irq_bypass_register_consumer); > * irq_bypass_unregister_consumer - unregister IRQ bypass consumer > * @consumer: pointer to consumer structure > * > - * Remove a previously registered IRQ consumer from the list of consumers > + * Remove a previously registered IRQ consumer from the xarray of consumers > * and disconnect it from any connected IRQ producer. > */ > void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer) > { > + unsigned long token = (unsigned long)consumer->token; > struct irq_bypass_consumer *tmp; > struct irq_bypass_producer *producer; > > @@ -239,24 +235,18 @@ void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer) > might_sleep(); > > if (!try_module_get(THIS_MODULE)) > - return; /* nothing in the list anyway */ > + return; /* nothing in the xarray anyway */ > > mutex_lock(&lock); > > - list_for_each_entry(tmp, &consumers, node) { > - if (tmp != consumer) > - continue; > + tmp = xa_load(&consumers, token); > + if (tmp == consumer) { > + producer = xa_load(&producers, token); > + if (producer) > + __disconnect(producer, consumer); > > - list_for_each_entry(producer, &producers, node) { > - if (producer->token == consumer->token) { > - __disconnect(producer, consumer); > - break; > - } > - } > - > - list_del(&consumer->node); > + xa_erase(&consumers, token); > module_put(THIS_MODULE); > - break; > } > > mutex_unlock(&lock); > @@ -264,3 +254,10 @@ void irq_bypass_unregister_consumer(struct irq_bypass_consumer *consumer) > module_put(THIS_MODULE); > } > EXPORT_SYMBOL_GPL(irq_bypass_unregister_consumer); > + > +static void __exit irqbypass_exit(void) > +{ > + xa_destroy(&producers); > + xa_destroy(&consumers); > +} > +module_exit(irqbypass_exit);