Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp262127rwl; Thu, 30 Mar 2023 15:43:51 -0700 (PDT) X-Google-Smtp-Source: AKy350ZF6QUHMbcMnfNBdxqCylAmjSgz4Mfsx6huoY6T+iOAMPttunvcpw41GMrbPE63OVWyMaMJ X-Received: by 2002:a17:903:1384:b0:19e:94ff:6780 with SMTP id jx4-20020a170903138400b0019e94ff6780mr3904587plb.6.1680216231455; Thu, 30 Mar 2023 15:43:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680216231; cv=none; d=google.com; s=arc-20160816; b=g+8Ky0p/GvlXYIySv0mXuv7sc/SLfLiQq9ktVcLkkqsbJ/h9JOXjT5Z8aPcAnbHytz lMy+fz6AySOQwnhPHZ9D3Sv5yhjtTx2B7y0JqnawX00w6ESVmhU/lr6rVyO6yc6xJfeg /YBMY6b5xfzwFEzmHuj7bmEfzev/fkDlM+KB2dWrkqv2TxNBe44pjNpB1KMTnuJAp0mS oUm6flqmWRj2yzT2oGu2cebnBUtRQ5susugfyeenCzhTkx3O58ij6eFF76d/ZpEJMEzt xc0ZefJmj70lnGd4IgoVfp6ODzMuw9iV5rIyZi69bxC0vfa9/Yg3rvgvEt7UoRgsgJR1 2m2g== 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=NUUQk1Rw4826YTliONysO8di5cIDKkIlgTF2OVJ91P8=; b=N/Lp0voG1FrMhBdEab4Lt+UG3ABij1rr94E8Fi8lH9Ji+1UaZuO6nWUztOWQNz4+Ue eM8/lKBpffHZ8PSfEvLj+AJ60eNTa3dSyvC4lUqhCTJxmw+EDakS3efp6GwJVT/42Avo a7L/qXYFoS1+zXqh6BHxwBsIsDeULG54oiq5l7AzabY3Q2YnZVAl+njaquGN5oGtMnWh Inyw0PQb0E+92k+eyAv+KPTObGnNCHhwu7xE+DgJG/qt4Lo/u/d1FMJP2fq4hF5NtgvF q4DYLGisWlRMIj+pHOU5oEIFLCc6fsxIdiZT27jksKM0hNy0XB//YiURGfXflfAF1nOY qimg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=S8Dmt7FY; 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 q17-20020a170902789100b001a203d56d9csi581052pll.83.2023.03.30.15.43.33; Thu, 30 Mar 2023 15:43:51 -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=S8Dmt7FY; 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 S230461AbjC3WnJ (ORCPT + 99 others); Thu, 30 Mar 2023 18:43:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230445AbjC3WnH (ORCPT ); Thu, 30 Mar 2023 18:43:07 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CC07ECC28 for ; Thu, 30 Mar 2023 15:42:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680216140; 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=NUUQk1Rw4826YTliONysO8di5cIDKkIlgTF2OVJ91P8=; b=S8Dmt7FYEgXs3kZ1NUf2fCFjM13i3cWKIbJGCUVBMPqJ4kfA1nNRB1IFj1d2MdMDhgZ4aE B4Qtk5mJiz/JFH8v7ht5aMBeogeh4lBinMmge1IVJJXhLfIUE85UF2bkXRI0vvyqGlD3rf 8f4z5hragUtHbgJCOmVZW46LZdB7oEo= Received: from mail-il1-f197.google.com (mail-il1-f197.google.com [209.85.166.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-136-vkarHPiRMzexw3v7txQ_hw-1; Thu, 30 Mar 2023 18:42:16 -0400 X-MC-Unique: vkarHPiRMzexw3v7txQ_hw-1 Received: by mail-il1-f197.google.com with SMTP id z19-20020a056e02089300b00326098d01d9so8892309ils.2 for ; Thu, 30 Mar 2023 15:42:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680216136; 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=NUUQk1Rw4826YTliONysO8di5cIDKkIlgTF2OVJ91P8=; b=6EqOYWxKTQaGeAxLy2nbYqx1SRtVJNuv8BYDezHJi8vQWfuUjah1xk+dopBNPZTWl1 fOKyT+NeEp3yp28eSrZz/ZaaCl4cvSAysZuWHtijdHf5dykb14BnR3v09C+hJGufOlLp pNJc8Whm97lcFnHZbfwkEroUhN/ix/aiGToGUoXZYlKQ4gMjJLJ3zMMEDb93ZFcGrArT TSVf+PpmZVXDKFevPegMqe1aCuKjYLd9EI0sUGGuwlYZX6FF0cLWRpRApAA3tdpIvbvq slZerggadL5z2pmh8KCPC4m1+E7N0Silx/Iy/hDjUOiHYKH2285axZGRGl8d79JAUYCI Lc5A== X-Gm-Message-State: AO0yUKXROZgWUf6q3kg8U+Grozh7jwkjz7dfPSo0ontDl15kZGbrDKIg +wTvWnE/TSbDB3stP/gbdE6Hovp/n3NhGwwZbwt7uu27PCg0HvZZfSRZRRABAJ9WnuR/R8Gfgtu Dx8/Yf2uY9VC3LhEsNhBpcyXi X-Received: by 2002:a05:6602:1844:b0:753:34ae:a00 with SMTP id d4-20020a056602184400b0075334ae0a00mr18463133ioi.3.1680216135899; Thu, 30 Mar 2023 15:42:15 -0700 (PDT) X-Received: by 2002:a05:6602:1844:b0:753:34ae:a00 with SMTP id d4-20020a056602184400b0075334ae0a00mr18463112ioi.3.1680216135550; Thu, 30 Mar 2023 15:42:15 -0700 (PDT) Received: from redhat.com ([38.15.36.239]) by smtp.gmail.com with ESMTPSA id b28-20020a056638389c00b0040af4b40201sm215319jav.86.2023.03.30.15.42.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 15:42:15 -0700 (PDT) Date: Thu, 30 Mar 2023 16:42:14 -0600 From: Alex Williamson To: Reinette Chatre Cc: jgg@nvidia.com, yishaih@nvidia.com, shameerali.kolothum.thodi@huawei.com, kevin.tian@intel.com, tglx@linutronix.de, darwi@linutronix.de, kvm@vger.kernel.org, dave.jiang@intel.com, jing2.liu@intel.com, ashok.raj@intel.com, fenghua.yu@intel.com, tom.zanussi@linux.intel.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH V2 7/8] vfio/pci: Support dynamic MSI-x Message-ID: <20230330164214.67ccbdfa.alex.williamson@redhat.com> In-Reply-To: <20230330164050.0069e2a5.alex.williamson@redhat.com> References: <419f3ba2f732154d8ae079b3deb02d0fdbe3e258.1680038771.git.reinette.chatre@intel.com> <20230330164050.0069e2a5.alex.williamson@redhat.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=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 Thu, 30 Mar 2023 16:40:50 -0600 Alex Williamson wrote: > On Tue, 28 Mar 2023 14:53:34 -0700 > Reinette Chatre wrote: > > > Recently introduced pci_msix_alloc_irq_at() and pci_msix_free_irq() > > enables an individual MSI-X index to be allocated and freed after > > MSI-X enabling. > > > > Support dynamic MSI-X if supported by the device. Keep the association > > between allocated interrupt and vfio interrupt context. Allocate new > > context together with the new interrupt if no interrupt context exist > > for an MSI-X interrupt. Similarly, release an interrupt with its > > context. > > > > Signed-off-by: Reinette Chatre > > --- > > Changes since RFC V1: > > - Add pointer to interrupt context as function parameter to > > vfio_irq_ctx_free(). (Alex) > > - Initialize new_ctx to false. (Dan Carpenter) > > - Only support dynamic allocation if device supports it. (Alex) > > > > drivers/vfio/pci/vfio_pci_intrs.c | 93 +++++++++++++++++++++++++------ > > 1 file changed, 76 insertions(+), 17 deletions(-) > > > > diff --git a/drivers/vfio/pci/vfio_pci_intrs.c b/drivers/vfio/pci/vfio_pci_intrs.c > > index b3a258e58625..755b752ca17e 100644 > > --- a/drivers/vfio/pci/vfio_pci_intrs.c > > +++ b/drivers/vfio/pci/vfio_pci_intrs.c > > @@ -55,6 +55,13 @@ struct vfio_pci_irq_ctx *vfio_irq_ctx_get(struct vfio_pci_core_device *vdev, > > return xa_load(&vdev->ctx, index); > > } > > > > +static void vfio_irq_ctx_free(struct vfio_pci_core_device *vdev, > > + struct vfio_pci_irq_ctx *ctx, unsigned long index) > > +{ > > + xa_erase(&vdev->ctx, index); > > + kfree(ctx); > > +} Also, the function below should use this rather than open coding the same now. Thanks, Alex > > + > > static void vfio_irq_ctx_free_all(struct vfio_pci_core_device *vdev) > > { > > struct vfio_pci_irq_ctx *ctx; > > @@ -409,33 +416,62 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_core_device *vdev, > > { > > struct pci_dev *pdev = vdev->pdev; > > struct vfio_pci_irq_ctx *ctx; > > + struct msi_map msix_map = {}; > > + bool allow_dyn_alloc = false; > > struct eventfd_ctx *trigger; > > + bool new_ctx = false; > > int irq, ret; > > u16 cmd; > > > > + /* Only MSI-X allows dynamic allocation. */ > > + if (msix && pci_msix_can_alloc_dyn(vdev->pdev)) > > + allow_dyn_alloc = true; > > Should vfio-pci-core probe this and store it in a field on > vfio_pci_core_device so that we can simply use something like > vdev->has_dyn_msix throughout? > > > + > > ctx = vfio_irq_ctx_get(vdev, vector); > > - if (!ctx) > > + if (!ctx && !allow_dyn_alloc) > > return -EINVAL; > > + > > irq = pci_irq_vector(pdev, vector); > > + /* Context and interrupt are always allocated together. */ > > + WARN_ON((ctx && irq == -EINVAL) || (!ctx && irq != -EINVAL)); > > > > - if (ctx->trigger) { > > + if (ctx && ctx->trigger) { > > irq_bypass_unregister_producer(&ctx->producer); > > > > cmd = vfio_pci_memory_lock_and_enable(vdev); > > free_irq(irq, ctx->trigger); > > + if (allow_dyn_alloc) { > > It almost seems easier to define msix_map in each scope that it's used: > > struct msi_map map = { .index = vector, > .virq = irq }; > > > + msix_map.index = vector; > > + msix_map.virq = irq; > > + pci_msix_free_irq(pdev, msix_map); > > + irq = -EINVAL; > > + } > > vfio_pci_memory_unlock_and_restore(vdev, cmd); > > kfree(ctx->name); > > eventfd_ctx_put(ctx->trigger); > > ctx->trigger = NULL; > > + if (allow_dyn_alloc) { > > + vfio_irq_ctx_free(vdev, ctx, vector); > > + ctx = NULL; > > + } > > } > > > > if (fd < 0) > > return 0; > > > > + if (!ctx) { > > + ctx = vfio_irq_ctx_alloc_single(vdev, vector); > > + if (!ctx) > > + return -ENOMEM; > > + new_ctx = true; > > + } > > + > > ctx->name = kasprintf(GFP_KERNEL_ACCOUNT, "vfio-msi%s[%d](%s)", > > msix ? "x" : "", vector, pci_name(pdev)); > > - if (!ctx->name) > > - return -ENOMEM; > > + if (!ctx->name) { > > + ret = -ENOMEM; > > + goto out_free_ctx; > > + } > > > > trigger = eventfd_ctx_fdget(fd); > > if (IS_ERR(trigger)) { > > @@ -443,25 +479,38 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_core_device *vdev, > > goto out_free_name; > > } > > > > - /* > > - * The MSIx vector table resides in device memory which may be cleared > > - * via backdoor resets. We don't allow direct access to the vector > > - * table so even if a userspace driver attempts to save/restore around > > - * such a reset it would be unsuccessful. To avoid this, restore the > > - * cached value of the message prior to enabling. > > - */ > > cmd = vfio_pci_memory_lock_and_enable(vdev); > > if (msix) { > > - struct msi_msg msg; > > - > > - get_cached_msi_msg(irq, &msg); > > - pci_write_msi_msg(irq, &msg); > > + if (irq == -EINVAL) { > > + msix_map = pci_msix_alloc_irq_at(pdev, vector, NULL); > > struct msi_map map = pci_msix_alloc_irq_at(pdev, > vector, NULL); > > + if (msix_map.index < 0) { > > + vfio_pci_memory_unlock_and_restore(vdev, cmd); > > + ret = msix_map.index; > > + goto out_put_eventfd_ctx; > > + } > > + irq = msix_map.virq; > > + } else { > > + /* > > + * The MSIx vector table resides in device memory which > > + * may be cleared via backdoor resets. We don't allow > > + * direct access to the vector table so even if a > > + * userspace driver attempts to save/restore around > > + * such a reset it would be unsuccessful. To avoid > > + * this, restore the cached value of the message prior > > + * to enabling. > > + */ > > You've only just copied this comment down to here, but I think it's a > bit stale. Maybe we should update it to something that helps explain > this split better, maybe: > > /* > * If the vector was previously allocated, refresh the > * on-device message data before enabling in case it had > * been cleared or corrupted since writing. > */ > > IIRC, that was the purpose of writing it back to the device and the > blocking of direct access is no longer accurate anyway. > > > + struct msi_msg msg; > > + > > + get_cached_msi_msg(irq, &msg); > > + pci_write_msi_msg(irq, &msg); > > + } > > } > > > > ret = request_irq(irq, vfio_msihandler, 0, ctx->name, trigger); > > - vfio_pci_memory_unlock_and_restore(vdev, cmd); > > if (ret) > > - goto out_put_eventfd_ctx; > > + goto out_free_irq_locked; > > + > > + vfio_pci_memory_unlock_and_restore(vdev, cmd); > > > > ctx->producer.token = trigger; > > ctx->producer.irq = irq; > > @@ -477,11 +526,21 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_core_device *vdev, > > > > return 0; > > > > +out_free_irq_locked: > > + if (allow_dyn_alloc && new_ctx) { > > struct msi_map map = { .index = vector, > .virq = irq }; > > > + msix_map.index = vector; > > + msix_map.virq = irq; > > + pci_msix_free_irq(pdev, msix_map); > > + } > > + vfio_pci_memory_unlock_and_restore(vdev, cmd); > > out_put_eventfd_ctx: > > eventfd_ctx_put(trigger); > > out_free_name: > > kfree(ctx->name); > > ctx->name = NULL; > > +out_free_ctx: > > + if (allow_dyn_alloc && new_ctx) > > + vfio_irq_ctx_free(vdev, ctx, vector); > > return ret; > > } > > > > Do we really need the new_ctx test in the above cases? Thanks, > > Alex