Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1305570rdh; Fri, 27 Oct 2023 10:07:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHQblb6UFFR+LkJOgctiUTFT0PyQblP5gjOQAjRIri1P1ISc9dXw5Xx1mLqDCw4LBE5G5Bo X-Received: by 2002:a67:f2d8:0:b0:457:c953:bc39 with SMTP id a24-20020a67f2d8000000b00457c953bc39mr3605351vsn.1.1698426433000; Fri, 27 Oct 2023 10:07:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698426432; cv=none; d=google.com; s=arc-20160816; b=TqK5cZSM7Jng2/Sql2uJ3jfSyYLqkb+VthGBuvW6FX/LUFXDgqW82BwduKcfIFsoVh JuhVVLksJ0mCnYvANng0V9APWwPBhfNg1UPsPkzwjPq60b01xhTKuuTVSMWqJkP9JPgw GjONDCTGn148cVYEoMGKe/zUEaAIii2MARrPkE2gpN2gfvfnPmOJwwrXPuzts2+tmflH MYnli4KbrF6d2L9wcFzSBZHvuwg2JEwHMfY88ZbwPfxV6p1HqdqRqGsQkDIqK5qDVsa6 2ZAoBM1DXFd3yM++hGqqY86nvQ4xqfIFT70lrF7d72g1D95wnId5EhRVhpcmQU9XufdD 6r4Q== 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:date:subject:cc:to:from :dkim-signature; bh=h/c1+7I4mUPxfar8RSjq9dOf57l6BgvLcg+umH1Xm+M=; fh=k582jSlci6uTztoHpzXN2a6Foym20J+xbJc48KNsUhM=; b=hpLZ3T4mPTbrrxFnf55eZ+PX+xHk1b+VuIVvEziiXoef860W3XYNfX9uRo8IyrYdpz vEiMQO+EtyUgp3xLlK4XPHvee0UGVybYM9NWBvrLrqA8CNrq7ApIBY6p3rUuKwmcB/0K lAd3PlbMdJR2TI6yJTs01lvRd3rX1av+TxEr3IBVzuq99CfyieYwDdtEhuwG9aoQGIi6 tqEM0ILJUAVlT7gRCuID6/CPglZBGIZJWfRY9LL8Eq2TMsloKqR4zNvDBHhkM1CCyl/a qIzes6FtEp1VLa9kefqnPt/Ko2sdmR6OgObfTfU3naCnC7Gc7uQr76gqUrJmvc0dHrfq XE2g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UOoENk6m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id a20-20020a05610222d400b004527acf43c5si315269vsh.534.2023.10.27.10.06.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 10:07:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=UOoENk6m; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 9290F813898B; Fri, 27 Oct 2023 10:02:58 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346530AbjJ0RCs (ORCPT + 99 others); Fri, 27 Oct 2023 13:02:48 -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 Content-Transfer-Encoding: 8bit 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_BLOCKED,SPF_HELO_NONE,SPF_NONE,URIBL_BLOCKED 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 27 Oct 2023 10:02:58 -0700 (PDT) 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; -- 2.34.1