From patchwork Fri Oct 27 17:00:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reinette Chatre X-Patchwork-Id: 13438667 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7869C25B48 for ; Fri, 27 Oct 2023 17:02:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346456AbjJ0RCM (ORCPT ); Fri, 27 Oct 2023 13:02:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346229AbjJ0RBn (ORCPT ); Fri, 27 Oct 2023 13:01:43 -0400 Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.8]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8C74CE1; Fri, 27 Oct 2023 10:01:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1698426098; x=1729962098; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=cOtSDallyPYUFy6e95eMcuO12DmemiUO7bjfEUG72Hs=; b=UOoENk6mSu2BDDxdP2ANEVWmMrqs30KqbzERkRwTwAlfhpYwsg/aCq/e fj7+8MgAX65XvX3W0o+lUD2DcSs2P8NQGDzps298EhXIwuF/nhkQYsLWG 5h7uCqzVT+zQt3YXkK5P3uahl/8e+t0iNx2hYxumFUe2JPYT5voaqo2cN F1CyzRw1x4S/eTWjXMG3wNXCyqJC4oxut0V2ztBJXMcKY0dkTw2X+7ouD Ceb6hu3ijB+e+s2UhtOjgknWivyD4qrxaDIT+CDbWtufnFhR1/Qo7dqlh uzL3HhszKy2IjqFJty4U0COTIekhgVN2T193SH4KxbQN7MEf4HQty50UC w==; X-IronPort-AV: E=McAfee;i="6600,9927,10876"; a="612093" X-IronPort-AV: E=Sophos;i="6.03,256,1694761200"; d="scan'208";a="612093" Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmvoesa102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Oct 2023 10:01:20 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10876"; a="1090988223" X-IronPort-AV: E=Sophos;i="6.03,256,1694761200"; d="scan'208";a="1090988223" Received: from rchatre-ws.ostc.intel.com ([10.54.69.144]) by fmsmga005-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Oct 2023 10:01:19 -0700 From: Reinette Chatre To: jgg@nvidia.com, yishaih@nvidia.com, shameerali.kolothum.thodi@huawei.com, kevin.tian@intel.com, alex.williamson@redhat.com Cc: 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, reinette.chatre@intel.com, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [RFC PATCH V3 20/26] vfio/pci: Separate frontend and backend code during interrupt enable/disable Date: Fri, 27 Oct 2023 10:00:52 -0700 Message-Id: <12f5f47089d4c9e988ab48e66266e0c6a420f842.1698422237.git.reinette.chatre@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org vfio_msi_set_vector_signal() contains a mix of generic and backend specific code. Separate the backend specific code into functions that can be replaced by backend-specific callbacks. The dev_info() used in error message is replaced by a pr_info() that prints the device name generated by the backend specific code intended to be used during request_irq(). Signed-off-by: Reinette Chatre --- Changes since RFC V2: - New patch drivers/vfio/pci/vfio_pci_intrs.c | 110 +++++++++++++++++++----------- 1 file changed, 70 insertions(+), 40 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_intrs.c b/drivers/vfio/pci/vfio_pci_intrs.c index fd0713dc9f81..c1f65b8adfe2 100644 --- a/drivers/vfio/pci/vfio_pci_intrs.c +++ b/drivers/vfio/pci/vfio_pci_intrs.c @@ -416,28 +416,81 @@ static int vfio_msi_alloc_irq(struct vfio_pci_core_device *vdev, return map.index < 0 ? map.index : map.virq; } -static int vfio_msi_set_vector_signal(struct vfio_pci_intr_ctx *intr_ctx, - unsigned int vector, int fd, +static void vfio_msi_free_interrupt(struct vfio_pci_intr_ctx *intr_ctx, + struct vfio_pci_irq_ctx *ctx, + unsigned int vector) +{ + struct vfio_pci_core_device *vdev = intr_ctx->priv; + u16 cmd; + + cmd = vfio_pci_memory_lock_and_enable(vdev); + free_irq(ctx->virq, ctx->trigger); + vfio_pci_memory_unlock_and_restore(vdev, cmd); + ctx->virq = 0; + /* Interrupt stays allocated, will be freed at MSI-X disable. */ +} + +static int vfio_msi_request_interrupt(struct vfio_pci_intr_ctx *intr_ctx, + struct vfio_pci_irq_ctx *ctx, + unsigned int vector, unsigned int index) +{ + bool msix = (index == VFIO_PCI_MSIX_IRQ_INDEX) ? true : false; + struct vfio_pci_core_device *vdev = intr_ctx->priv; + int irq, ret; + u16 cmd; + + /* Interrupt stays allocated, will be freed at MSI-X disable. */ + irq = vfio_msi_alloc_irq(vdev, vector, msix); + if (irq < 0) + return irq; + + /* + * If the vector was previously allocated, refresh the on-device + * message data before enabling in case it had been cleared or + * corrupted (e.g. due to backdoor resets) since writing. + */ + 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); + } + + ret = request_irq(irq, vfio_msihandler, 0, ctx->name, ctx->trigger); + vfio_pci_memory_unlock_and_restore(vdev, cmd); + + ctx->virq = irq; + + return ret; +} + +static char *vfio_msi_device_name(struct vfio_pci_intr_ctx *intr_ctx, + unsigned int vector, + unsigned int index) { bool msix = (index == VFIO_PCI_MSIX_IRQ_INDEX) ? true : false; struct vfio_pci_core_device *vdev = intr_ctx->priv; struct pci_dev *pdev = vdev->pdev; + + return kasprintf(GFP_KERNEL_ACCOUNT, "vfio-msi%s[%d](%s)", + msix ? "x" : "", vector, pci_name(pdev)); +} + +static int vfio_msi_set_vector_signal(struct vfio_pci_intr_ctx *intr_ctx, + unsigned int vector, int fd, + unsigned int index) +{ struct vfio_pci_irq_ctx *ctx; struct eventfd_ctx *trigger; - int irq = -EINVAL, ret; - u16 cmd; + int ret; ctx = vfio_irq_ctx_get(intr_ctx, vector); if (ctx && ctx->trigger) { irq_bypass_unregister_producer(&ctx->producer); - irq = ctx->virq; - cmd = vfio_pci_memory_lock_and_enable(vdev); - free_irq(ctx->virq, ctx->trigger); - vfio_pci_memory_unlock_and_restore(vdev, cmd); - ctx->virq = 0; - /* Interrupt stays allocated, will be freed at MSI-X disable. */ + vfio_msi_free_interrupt(intr_ctx, ctx, vector); kfree(ctx->name); ctx->name = NULL; eventfd_ctx_put(ctx->trigger); @@ -447,13 +500,6 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_intr_ctx *intr_ctx, if (fd < 0) return 0; - if (irq == -EINVAL) { - /* Interrupt stays allocated, will be freed at MSI-X disable. */ - irq = vfio_msi_alloc_irq(vdev, vector, msix); - if (irq < 0) - return irq; - } - /* Per-interrupt context remain allocated. */ if (!ctx) { ctx = vfio_irq_ctx_alloc(intr_ctx, vector); @@ -461,8 +507,7 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_intr_ctx *intr_ctx, return -ENOMEM; } - ctx->name = kasprintf(GFP_KERNEL_ACCOUNT, "vfio-msi%s[%d](%s)", - msix ? "x" : "", vector, pci_name(pdev)); + ctx->name = vfio_msi_device_name(intr_ctx, vector, index); if (!ctx->name) return -ENOMEM; @@ -472,42 +517,27 @@ static int vfio_msi_set_vector_signal(struct vfio_pci_intr_ctx *intr_ctx, goto out_free_name; } - /* - * If the vector was previously allocated, refresh the on-device - * message data before enabling in case it had been cleared or - * corrupted (e.g. due to backdoor resets) since writing. - */ - 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); - } + ctx->trigger = trigger; - ret = request_irq(irq, vfio_msihandler, 0, ctx->name, trigger); - vfio_pci_memory_unlock_and_restore(vdev, cmd); + ret = vfio_msi_request_interrupt(intr_ctx, ctx, vector, index); if (ret) goto out_put_eventfd_ctx; - ctx->virq = irq; - ctx->producer.token = trigger; ctx->producer.irq = ctx->virq; ret = irq_bypass_register_producer(&ctx->producer); if (unlikely(ret)) { - dev_info(&pdev->dev, - "irq bypass producer (token %p) registration fails: %d\n", - ctx->producer.token, ret); + pr_info("%s irq bypass producer (token %p) registration fails: %d\n", + ctx->name, ctx->producer.token, ret); ctx->producer.token = NULL; } - ctx->trigger = trigger; return 0; out_put_eventfd_ctx: - eventfd_ctx_put(trigger); + eventfd_ctx_put(ctx->trigger); + ctx->trigger = NULL; out_free_name: kfree(ctx->name); ctx->name = NULL;