From patchwork Mon Mar 17 13:29:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 14019299 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A6BE622759C; Mon, 17 Mar 2025 13:29:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218167; cv=none; b=Q/4w7KjC3vsXBxv+RZAsGynqIW5QLkmpIl41ooksLmjKcStecYK+N76vjLUHN2SAEviTIHp0rpG1u97Iv9VmDH5Uy0JN1M+vTUJiSaGFjtHu2O6tOB05ppWZalKImDGwzkN65sCjH6uU+kkSE27lDHTYfQfjOcuanBU9MAyJrj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218167; c=relaxed/simple; bh=ho8Pzt9rnRzxEycPk61Hi2KRQSUXlk2Hs9Tw9uulgDA=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=H7d/Eev2HxluH/KiTO7bnDu+kpAwdYDpdD/EUi8h6TruQLdVrBy/JmIpAQqQsipTboMMHDi5SpESWvPrUZjstvKc7pt0LRffIWvmGHVLKD1omYkhYZyCvC4oMiJHXNJ9FYpp9/G4eND/XVgWHYY628DU9AU6/RqnPCX56ztBO2Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ikfuYWB2; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=9YalfP8x; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ikfuYWB2"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="9YalfP8x" Message-ID: <20250317092945.764490535@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742218163; 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: references:references; bh=dp0eYeKfrNlzvU3/WMfnQjPzD6qavekPLwUv4OZ2OOs=; b=ikfuYWB22wmHGAZ1ZCYxuHRBkfaFcMntu8Y2Xs5ZKHP20JMm5tpDEfW6mSqnqvIO7/Ajcd T6v6qi7q6R4ILWkgkDOA9Euq/2Zb29uV0VXgMO3S6EOiTvIiTE+3fMRc2bW6TgCzStokN0 2qNbE69vg0xjvKiRyG8sm76ROcQAiH7DcvK+2xFh2EUbUaH3RmjIZEbbqrki7c8bXpVgqd r29lTkocOA2rivUwQDjCvDUL23d+Y5cLsrbZNqF9pkBjMX/+mz0WWQ4SK1qFQ2nxUnM/Od +UNYZUNZd4i2O78LP2ilCXUXc58YfEFyuX8tC6NzyGzyPdar813GtKp6JAeFXA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742218163; 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: references:references; bh=dp0eYeKfrNlzvU3/WMfnQjPzD6qavekPLwUv4OZ2OOs=; b=9YalfP8xBbKHQiBQ+hYehlm/nMIPvpPeNa78JCueUr9eWpmBcupHLF1l2sIP1/dDDyvv38 onqG0szZCKhD09Cg== From: Thomas Gleixner To: LKML Cc: Marc Zyngier , Peter Zijlstra , Jonathan Cameron , Nishanth Menon , Dhruva Gole , Tero Kristo , Santosh Shilimkar , Logan Gunthorpe , Dave Jiang , Jon Mason , Allen Hubbe , ntb@lists.linux.dev, Bjorn Helgaas , linux-pci@vger.kernel.org, Michael Kelley , Wei Liu , Haiyang Zhang , linux-hyperv@vger.kernel.org, Wei Huang , Manivannan Sadhasivam , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Jonathan Cameron Subject: [patch V3 01/10] cleanup: Provide retain_ptr() References: <20250317092919.008573387@linutronix.de> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 17 Mar 2025 14:29:22 +0100 (CET) In cases where an allocation is consumed by another function, the allocation needs to be retained on success or freed on failure. The code pattern is usually: struct foo *f = kzalloc(sizeof(*f), GFP_KERNEL); struct bar *b; ,,, // Initialize f ... if (ret) goto free; ... bar = bar_create(f); if (!bar) { ret = -ENOMEM; goto free; } ... return 0; free: kfree(f); return ret; This prevents using __free(kfree) on @f because there is no canonical way to tell the cleanup code that the allocation should not be freed. Abusing no_free_ptr() by force ignoring the return value is not really a sensible option either. Provide an explicit macro retain_ptr(), which NULLs the cleanup pointer. That makes it easy to analyze and reason about. Signed-off-by: Thomas Gleixner Reviewed-by: Jonathan Cameron Cc: Peter Zijlstra --- include/linux/cleanup.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) --- a/include/linux/cleanup.h +++ b/include/linux/cleanup.h @@ -216,6 +216,23 @@ const volatile void * __must_check_fn(co #define return_ptr(p) return no_free_ptr(p) +/* + * Only for situations where an allocation is handed in to another function + * and consumed by that function on success. + * + * struct foo *f __free(kfree) = kzalloc(sizeof(*f), GFP_KERNEL); + * + * setup(f); + * if (some_condition) + * return -EINVAL; + * .... + * ret = bar(f); + * if (!ret) + * retain_ptr(f); + * return ret; + */ +#define retain_ptr(p) \ + __get_and_null(p, NULL) /* * DEFINE_CLASS(name, type, exit, init, init_args...): From patchwork Mon Mar 17 13:29:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 14019300 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D2FFA23F299; Mon, 17 Mar 2025 13:29:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218167; cv=none; b=J16k30oUIe4XvAdC9mj5JKKMllUCFXf1kK2H7GD+BWxJxn0zOEJI8h8zw+78owzwtb7vMjw4Ev+YO3OjhVcTR//gttM3eq04uCi2N/jRmDB/K7iCGi4oc7MVmNSSLztfm5bxFYX1k+NVyaENnd0CwMfk0DYjX2h3IB15mwlJb6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218167; c=relaxed/simple; bh=GqT6LEvfjw5V1Texga+EKQjOrCWKb2zLK3NMPUGdOeQ=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=ciVo7fvuo3MTpmziWLkNGB1SbntLDZwgUkYqDIjqm1BidIPl9B3dmIZ984WT3LxMxPWkzD547R7k1ElDMIvculAYCeeGuVAApdQfVJylmRubVCxKJ2WgcrQiGb44ZKPLYXriL5ifuSKLtmRTEtG1qOkQngq1x1Ld3PT7FV1vWAI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=2PedsQnH; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Ao8ARgnG; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="2PedsQnH"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Ao8ARgnG" Message-ID: <20250317092945.827614406@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742218164; 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: references:references; bh=LG6QH15/7GkasLvFadFofFDWvwiYCXheBUiRg6rySO4=; b=2PedsQnHpuRctlN5BETlHL9GX+DpIdYVMnpFlSCtsVIst/Hv2U2WdsQGjrWpKaTiDJzKVX g082ynZRga3zcqQxXOM/EUDMmjlEDV2o/YsfFCEIuT0jjgT6LmvrFbbTkvin+vJOBy2P87 1T0V7V4ftUdmf2xmblv3SRkrmUF1dJhb+9/ANVJtNzRKWnROenMC1y3fVgeflzr5FkMnME ipMTYbAIVQ9+9pzHRVFA6oGt1SNzGG/JL9jdHdeXSGhamf4hIroHnogpeFKTxPa3vkiFGt GKKRmSV2C/lD+x3FHFMITP0JRfftniqtZByNecHv3J7rKF+ywfj3P99PKwdfrw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742218164; 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: references:references; bh=LG6QH15/7GkasLvFadFofFDWvwiYCXheBUiRg6rySO4=; b=Ao8ARgnGfVqgHK8vrdVVW4XX3TC6pUPnKH81uglbJITIiGQH53UYZXNuonp6nO6CQR0NFF 0y2hw2V4+nabKFCQ== From: Thomas Gleixner To: LKML Cc: Marc Zyngier , Peter Zijlstra , Jonathan Cameron , Nishanth Menon , Dhruva Gole , Tero Kristo , Santosh Shilimkar , Logan Gunthorpe , Dave Jiang , Jon Mason , Allen Hubbe , ntb@lists.linux.dev, Bjorn Helgaas , linux-pci@vger.kernel.org, Michael Kelley , Wei Liu , Haiyang Zhang , linux-hyperv@vger.kernel.org, Wei Huang , Manivannan Sadhasivam , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Jonathan Cameron Subject: [patch V3 02/10] genirq/msi: Use lock guards for MSI descriptor locking References: <20250317092919.008573387@linutronix.de> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 17 Mar 2025 14:29:23 +0100 (CET) Provide a lock guard for MSI descriptor locking and update the core code accordingly. No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Jonathan Cameron --- V2: Remove the gotos - Jonathan --- include/linux/irqdomain.h | 2 include/linux/msi.h | 3 + kernel/irq/msi.c | 109 ++++++++++++++++------------------------------ 3 files changed, 45 insertions(+), 69 deletions(-) --- a/include/linux/irqdomain.h +++ b/include/linux/irqdomain.h @@ -281,6 +281,8 @@ static inline struct fwnode_handle *irq_ void irq_domain_free_fwnode(struct fwnode_handle *fwnode); +DEFINE_FREE(irq_domain_free_fwnode, struct fwnode_handle *, if (_T) irq_domain_free_fwnode(_T)) + struct irq_domain_chip_generic_info; /** --- a/include/linux/msi.h +++ b/include/linux/msi.h @@ -227,6 +227,9 @@ int msi_setup_device_data(struct device void msi_lock_descs(struct device *dev); void msi_unlock_descs(struct device *dev); +DEFINE_LOCK_GUARD_1(msi_descs_lock, struct device, msi_lock_descs(_T->lock), + msi_unlock_descs(_T->lock)); + struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid, enum msi_desc_filter filter); --- a/kernel/irq/msi.c +++ b/kernel/irq/msi.c @@ -443,7 +443,6 @@ EXPORT_SYMBOL_GPL(msi_next_desc); unsigned int msi_domain_get_virq(struct device *dev, unsigned int domid, unsigned int index) { struct msi_desc *desc; - unsigned int ret = 0; bool pcimsi = false; struct xarray *xa; @@ -457,7 +456,7 @@ unsigned int msi_domain_get_virq(struct if (dev_is_pci(dev) && domid == MSI_DEFAULT_DOMAIN) pcimsi = to_pci_dev(dev)->msi_enabled; - msi_lock_descs(dev); + guard(msi_descs_lock)(dev); xa = &dev->msi.data->__domains[domid].store; desc = xa_load(xa, pcimsi ? 0 : index); if (desc && desc->irq) { @@ -466,16 +465,12 @@ unsigned int msi_domain_get_virq(struct * PCI-MSIX and platform MSI use a descriptor per * interrupt. */ - if (pcimsi) { - if (index < desc->nvec_used) - ret = desc->irq + index; - } else { - ret = desc->irq; - } + if (!pcimsi) + return desc->irq; + if (index < desc->nvec_used) + return desc->irq + index; } - - msi_unlock_descs(dev); - return ret; + return 0; } EXPORT_SYMBOL_GPL(msi_domain_get_virq); @@ -993,9 +988,8 @@ bool msi_create_device_irq_domain(struct void *chip_data) { struct irq_domain *domain, *parent = dev->msi.domain; - struct fwnode_handle *fwnode, *fwnalloced = NULL; - struct msi_domain_template *bundle; const struct msi_parent_ops *pops; + struct fwnode_handle *fwnode; if (!irq_domain_is_msi_parent(parent)) return false; @@ -1003,7 +997,8 @@ bool msi_create_device_irq_domain(struct if (domid >= MSI_MAX_DEVICE_IRQDOMAINS) return false; - bundle = kmemdup(template, sizeof(*bundle), GFP_KERNEL); + struct msi_domain_template *bundle __free(kfree) = + kmemdup(template, sizeof(*bundle), GFP_KERNEL); if (!bundle) return false; @@ -1026,41 +1021,36 @@ bool msi_create_device_irq_domain(struct * node as they are not guaranteed to have a fwnode. They are never * looked up and always handled in the context of the device. */ - if (bundle->info.flags & MSI_FLAG_USE_DEV_FWNODE) - fwnode = dev->fwnode; + struct fwnode_handle *fwnode_alloced __free(irq_domain_free_fwnode) = NULL; + + if (!(bundle->info.flags & MSI_FLAG_USE_DEV_FWNODE)) + fwnode = fwnode_alloced = irq_domain_alloc_named_fwnode(bundle->name); else - fwnode = fwnalloced = irq_domain_alloc_named_fwnode(bundle->name); + fwnode = dev->fwnode; if (!fwnode) - goto free_bundle; + return false; if (msi_setup_device_data(dev)) - goto free_fwnode; - - msi_lock_descs(dev); + return false; + guard(msi_descs_lock)(dev); if (WARN_ON_ONCE(msi_get_device_domain(dev, domid))) - goto fail; + return false; if (!pops->init_dev_msi_info(dev, parent, parent, &bundle->info)) - goto fail; + return false; domain = __msi_create_irq_domain(fwnode, &bundle->info, IRQ_DOMAIN_FLAG_MSI_DEVICE, parent); if (!domain) - goto fail; + return false; + /* @bundle and @fwnode_alloced are now in use. Prevent cleanup */ + retain_ptr(bundle); + retain_ptr(fwnode_alloced); domain->dev = dev; dev->msi.data->__domains[domid].domain = domain; - msi_unlock_descs(dev); return true; - -fail: - msi_unlock_descs(dev); -free_fwnode: - irq_domain_free_fwnode(fwnalloced); -free_bundle: - kfree(bundle); - return false; } /** @@ -1074,12 +1064,10 @@ void msi_remove_device_irq_domain(struct struct msi_domain_info *info; struct irq_domain *domain; - msi_lock_descs(dev); - + guard(msi_descs_lock)(dev); domain = msi_get_device_domain(dev, domid); - if (!domain || !irq_domain_is_msi_device(domain)) - goto unlock; + return; dev->msi.data->__domains[domid].domain = NULL; info = domain->host_data; @@ -1088,9 +1076,6 @@ void msi_remove_device_irq_domain(struct irq_domain_remove(domain); irq_domain_free_fwnode(fwnode); kfree(container_of(info, struct msi_domain_template, info)); - -unlock: - msi_unlock_descs(dev); } /** @@ -1106,16 +1091,14 @@ bool msi_match_device_irq_domain(struct { struct msi_domain_info *info; struct irq_domain *domain; - bool ret = false; - msi_lock_descs(dev); + guard(msi_descs_lock)(dev); domain = msi_get_device_domain(dev, domid); if (domain && irq_domain_is_msi_device(domain)) { info = domain->host_data; - ret = info->bus_token == bus_token; + return info->bus_token == bus_token; } - msi_unlock_descs(dev); - return ret; + return false; } static int msi_domain_prepare_irqs(struct irq_domain *domain, struct device *dev, @@ -1365,12 +1348,9 @@ int msi_domain_alloc_irqs_range(struct d .last = last, .nirqs = last + 1 - first, }; - int ret; - msi_lock_descs(dev); - ret = msi_domain_alloc_locked(dev, &ctrl); - msi_unlock_descs(dev); - return ret; + guard(msi_descs_lock)(dev); + return msi_domain_alloc_locked(dev, &ctrl); } EXPORT_SYMBOL_GPL(msi_domain_alloc_irqs_range); @@ -1474,12 +1454,8 @@ struct msi_map msi_domain_alloc_irq_at(s const struct irq_affinity_desc *affdesc, union msi_instance_cookie *icookie) { - struct msi_map map; - - msi_lock_descs(dev); - map = __msi_domain_alloc_irq_at(dev, domid, index, affdesc, icookie); - msi_unlock_descs(dev); - return map; + guard(msi_descs_lock)(dev); + return __msi_domain_alloc_irq_at(dev, domid, index, affdesc, icookie); } /** @@ -1516,13 +1492,11 @@ int msi_device_domain_alloc_wired(struct icookie.value = ((u64)type << 32) | hwirq; - msi_lock_descs(dev); + guard(msi_descs_lock)(dev); if (WARN_ON_ONCE(msi_get_device_domain(dev, domid) != domain)) map.index = -EINVAL; else map = __msi_domain_alloc_irq_at(dev, domid, MSI_ANY_INDEX, NULL, &icookie); - msi_unlock_descs(dev); - return map.index >= 0 ? map.virq : map.index; } @@ -1615,9 +1589,8 @@ static void msi_domain_free_irqs_range_l void msi_domain_free_irqs_range(struct device *dev, unsigned int domid, unsigned int first, unsigned int last) { - msi_lock_descs(dev); + guard(msi_descs_lock)(dev); msi_domain_free_irqs_range_locked(dev, domid, first, last); - msi_unlock_descs(dev); } EXPORT_SYMBOL_GPL(msi_domain_free_irqs_all); @@ -1647,9 +1620,8 @@ void msi_domain_free_irqs_all_locked(str */ void msi_domain_free_irqs_all(struct device *dev, unsigned int domid) { - msi_lock_descs(dev); + guard(msi_descs_lock)(dev); msi_domain_free_irqs_all_locked(dev, domid); - msi_unlock_descs(dev); } /** @@ -1668,12 +1640,11 @@ void msi_device_domain_free_wired(struct if (WARN_ON_ONCE(!dev || !desc || domain->bus_token != DOMAIN_BUS_WIRED_TO_MSI)) return; - msi_lock_descs(dev); - if (!WARN_ON_ONCE(msi_get_device_domain(dev, MSI_DEFAULT_DOMAIN) != domain)) { - msi_domain_free_irqs_range_locked(dev, MSI_DEFAULT_DOMAIN, desc->msi_index, - desc->msi_index); - } - msi_unlock_descs(dev); + guard(msi_descs_lock)(dev); + if (WARN_ON_ONCE(msi_get_device_domain(dev, MSI_DEFAULT_DOMAIN) != domain)) + return; + msi_domain_free_irqs_range_locked(dev, MSI_DEFAULT_DOMAIN, desc->msi_index, + desc->msi_index); } /** From patchwork Mon Mar 17 13:29:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 14019301 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DC45F23F404; Mon, 17 Mar 2025 13:29:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218168; cv=none; b=CJH5OdICIC6OUmJtZyFvq2lrayoMcLThWmPtPwiO+jDiqjbk7gKSmZlVRghXnL93/uXqlQchVoktEI/i6QPwmncKPpN9xl4LXqH1lsene/UGIJTMkABoBei0uWo2dNUW2ftQBxkFj6x3ep7Mluf7NbkHjGExB8OPjdbkr+P1aOs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218168; c=relaxed/simple; bh=RvJxo5ar40+unPIeF/B/OFE+2jPTP7dHPiKnIeqq5Ws=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=oYXww3dhOfTyxTq31oUzaLKOHKdCEywtUd/zcWuKBk9gvnKRXP5ZCvxulxHUxH510dNUghQe5Ay9I8sxpzrNIma0oJf3HBfX601NvTeSXBik9V1W6tXgLFuX/jgWgQ2GYCdhxhrqy7oMEApenJEs5lLraMTOZS7UbmyP6HY0c3E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ZXgDw2Xg; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=v5VcbI0Y; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ZXgDw2Xg"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="v5VcbI0Y" Message-ID: <20250317092945.889932275@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742218165; 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: references:references; bh=Au16aChR8IOH+YeFbnBW4TcQleO7ukWx0zDehmMH2S0=; b=ZXgDw2XgxMHj0HftP/VDW1yVeAZ8nzFkaGjuOfO8cRtoswl1ExtC5xQyQxpBoeuijYHOg4 MkJ2gghxl3eRl0wdd8jqGVrxSp6sT+lMu8WedWlOiLdXTtiKgorxTw+GbXLiyBcCLMARYw nF0ap//kj5Tw7249Gma2UsXb78dFQV2dkY0jp5Vj7zvnZHF08Hzr/ZpuDwKAfylW2yXOYV iJuDni3zUp2P1ByxoDbNZm8SxUbzmnYexL8SOwPpiDQQFb5YGLft2c11YC5MiMEWh8Xfwx y8zcbqqT+xTcRo1ZAqYVwKE2SDqIIWolGr6AX0vz1IPG2Foeh3ghz+YdMGJ74A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742218165; 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: references:references; bh=Au16aChR8IOH+YeFbnBW4TcQleO7ukWx0zDehmMH2S0=; b=v5VcbI0YRMhLvO4XpGpSQD8ONn1g2PrWR38hWuuzs7bjf/FDh/7NRXJvvZoI9SJQOU+70x 2bK7aLTu3JJmyGAw== From: Thomas Gleixner To: LKML Cc: Marc Zyngier , Peter Zijlstra , Nishanth Menon , Jonathan Cameron , Dhruva Gole , Tero Kristo , Santosh Shilimkar , Logan Gunthorpe , Dave Jiang , Jon Mason , Allen Hubbe , ntb@lists.linux.dev, Bjorn Helgaas , linux-pci@vger.kernel.org, Michael Kelley , Wei Liu , Haiyang Zhang , linux-hyperv@vger.kernel.org, Wei Huang , Manivannan Sadhasivam , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Jonathan Cameron Subject: [patch V3 03/10] soc: ti: ti_sci_inta_msi: Switch MSI descriptor locking to guard() References: <20250317092919.008573387@linutronix.de> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 17 Mar 2025 14:29:24 +0100 (CET) Convert the code to use the new guard(msi_descs_lock). No functional change intended. Signed-off-by: Thomas Gleixner Tested-by: Nishanth Menon Reviewed-by: Jonathan Cameron Reviewed-by: Dhruva Gole Cc: Tero Kristo Cc: Santosh Shilimkar --- drivers/soc/ti/ti_sci_inta_msi.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) --- a/drivers/soc/ti/ti_sci_inta_msi.c +++ b/drivers/soc/ti/ti_sci_inta_msi.c @@ -103,19 +103,15 @@ int ti_sci_inta_msi_domain_alloc_irqs(st if (ret) return ret; - msi_lock_descs(dev); + guard(msi_descs_lock)(dev); nvec = ti_sci_inta_msi_alloc_descs(dev, res); - if (nvec <= 0) { - ret = nvec; - goto unlock; - } + if (nvec <= 0) + return nvec; /* Use alloc ALL as it's unclear whether there are gaps in the indices */ ret = msi_domain_alloc_irqs_all_locked(dev, MSI_DEFAULT_DOMAIN, nvec); if (ret) dev_err(dev, "Failed to allocate IRQs %d\n", ret); -unlock: - msi_unlock_descs(dev); return ret; } EXPORT_SYMBOL_GPL(ti_sci_inta_msi_domain_alloc_irqs); From patchwork Mon Mar 17 13:29:26 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 14019302 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B646423F41D; Mon, 17 Mar 2025 13:29:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218169; cv=none; b=WqHTHXxufBAuEfLuImVzsTw/ak/eaFhLSzfo46jPtK4PtP35/EiJQRuJhBkioo3L02eThgUrOTG7kUs4h38FwUHy61HXgEQwSiUXjUbVYtwBjIr8lUkP7WmUGL3CAMZA8iRESH1U6UqrDzOPDhYYSk6PB8PngiijuCvw+4tnGtc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218169; c=relaxed/simple; bh=3FyrNeDeo6PuudCPLtSK1Wvo8s/2uyQqMBBJ+Hz0upU=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=mbjCumfAnSR1Mtm0XLJFpSlDhWHef+pnNHMaqgyrk1a8qqEquFziWbV/crOAboatmEaWBAXxP+/N07hYYWnQv0lRR4Xt9hn0w9nc1FEPkVt7kW215OPXtyT0tDE8XWM1Yn4QV9RMf5NFStu+E9nS12n/uHexKaIhL+Q72OT5Kdo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=A0fuhMpQ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=JPutzsMP; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="A0fuhMpQ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JPutzsMP" Message-ID: <20250317092945.953189553@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742218166; 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: references:references; bh=j38ae1J786gvtJe/THvWhwBIOiRP6lJqXx4r70BjdWE=; b=A0fuhMpQc4rT72k4I3p9aWAwouOb045M4R4kxN1HcvFY3aA04PoVnFyJQ6qCHka6Z7NpSm 4rl4dhHcAO0DqsoHKs33Y2P64ZRbz8IC6S09ym3CMqk9yQ62NC8TKXcsCN5RF4M2isVgPp FVbQ2FsOcS2iHEwp2UOtNsqXXJddARazMO+Hdu2134rKpJeuSIeX2MXOCkxiZLfx5nAQln TQuxZB+MRZ+n7/NEtg0C6WSRYMfL6h+tj/nxzdBFIOjFzJNfgpO+WsXJJFSt5gDLqXs/QP zhvyfjNNgVaE+pHLz75g8gA7vCX/PYxF/U6I1n2p5TIe5EpcpLB3/hwQHHJhHw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742218166; 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: references:references; bh=j38ae1J786gvtJe/THvWhwBIOiRP6lJqXx4r70BjdWE=; b=JPutzsMPXYyTRbsOtw/zuQsFh12RMgm7HrdsKssaS/80A9owsje+/jMUa1UjCWTgNvToXf qrrWloOn508zQyAw== From: Thomas Gleixner To: LKML Cc: Marc Zyngier , Peter Zijlstra , Jonathan Cameron , Logan Gunthorpe , Dave Jiang , Jon Mason , Allen Hubbe , ntb@lists.linux.dev, Nishanth Menon , Dhruva Gole , Tero Kristo , Santosh Shilimkar , Bjorn Helgaas , linux-pci@vger.kernel.org, Michael Kelley , Wei Liu , Haiyang Zhang , linux-hyperv@vger.kernel.org, Wei Huang , Manivannan Sadhasivam , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Jonathan Cameron Subject: [patch V3 04/10] NTB/msi: Switch MSI descriptor locking to lock guard() References: <20250317092919.008573387@linutronix.de> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 17 Mar 2025 14:29:26 +0100 (CET) Convert the code to use the new guard(msi_descs_lock). No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Jonathan Cameron Reviewed-by: Logan Gunthorpe Acked-by: Dave Jiang Cc: Jon Mason Cc: Allen Hubbe Cc: ntb@lists.linux.dev --- drivers/ntb/msi.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) --- a/drivers/ntb/msi.c +++ b/drivers/ntb/msi.c @@ -106,10 +106,10 @@ int ntb_msi_setup_mws(struct ntb_dev *nt if (!ntb->msi) return -EINVAL; - msi_lock_descs(&ntb->pdev->dev); - desc = msi_first_desc(&ntb->pdev->dev, MSI_DESC_ASSOCIATED); - addr = desc->msg.address_lo + ((uint64_t)desc->msg.address_hi << 32); - msi_unlock_descs(&ntb->pdev->dev); + scoped_guard (msi_descs_lock, &ntb->pdev->dev) { + desc = msi_first_desc(&ntb->pdev->dev, MSI_DESC_ASSOCIATED); + addr = desc->msg.address_lo + ((uint64_t)desc->msg.address_hi << 32); + } for (peer = 0; peer < ntb_peer_port_count(ntb); peer++) { peer_widx = ntb_peer_highest_mw_idx(ntb, peer); @@ -289,7 +289,7 @@ int ntbm_msi_request_threaded_irq(struct if (!ntb->msi) return -EINVAL; - msi_lock_descs(dev); + guard(msi_descs_lock)(dev); msi_for_each_desc(entry, dev, MSI_DESC_ASSOCIATED) { if (irq_has_action(entry->irq)) continue; @@ -307,17 +307,11 @@ int ntbm_msi_request_threaded_irq(struct ret = ntbm_msi_setup_callback(ntb, entry, msi_desc); if (ret) { devm_free_irq(&ntb->dev, entry->irq, dev_id); - goto unlock; + return ret; } - - ret = entry->irq; - goto unlock; + return entry->irq; } - ret = -ENODEV; - -unlock: - msi_unlock_descs(dev); - return ret; + return -ENODEV; } EXPORT_SYMBOL(ntbm_msi_request_threaded_irq); From patchwork Mon Mar 17 13:29:27 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 14019303 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 42E21241122; Mon, 17 Mar 2025 13:29:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218171; cv=none; b=A4FySLboIVvoPALxIpcI9BN9yUeye4ksnjVE1Pfa01dsEcClOe1hskp5QsWCPczg4Te/35umPse+b8C7i26APuryBGqh1BdE/uLA1SkV1FU4HA0qAhtM0vubAdVdSeyxb5QiImaJoEBdLnm/DeCMDQCXixkQKomYu41tN8iBzRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218171; c=relaxed/simple; bh=PqSI/P0dzDbOACXOLeXj1VsqwY6q0rlK8DUfnb+2PDg=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=X3Xr9nj7L/ZiUqzAYuwu/hNM84sDTVP3IBDVYh4/cjBCh9VKlakw3P90islnHtxGToDnQDikrf/pXcHq90XnFdfPDFMBDVbh7aQIGiylDQGfPueUyZ7is8dAGNCn7lHdrjEdTf6Pjp7gNAFopuV4v9l08nl9Q+MRLn+fTTw9tks= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rQD7k2HM; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Ri7ncOsF; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rQD7k2HM"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Ri7ncOsF" Message-ID: <20250317092946.014189955@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742218167; 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: references:references; bh=spOY6B/Pxm1imkpFatgjpncHqaXY2u8V4fMs4Kj+kNI=; b=rQD7k2HMMqO1mrHz7/O42k+ZW2DjZwNUxWG8keHDl9fM57Xmx7DZV+tdMKSmemBiOquulb N8hP3d7B/P2TFKmPYb+EC1B/aulbJJbEkqHwyotMb03Chef6VZgAyzp4sxYMBMWUfNYWBm X/R4vwXAwjoHjziLnZewzJF17BE5sY+iuIb8C0QmGKlKYZbGRTBEN1+FVZOBNYuC4RWVMU L7kX8+dsEaHrmazNQZnm1Y53MiiPwt9in3AnIk0fcIisWLg2Sdphh4qnu9r27tx/G1lNmw HSiGtJ9VlUT5iU0H29TIDSMq5+7C2gN/w5xZMJdejyGUpNP+svUH7AXQplyQOA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742218167; 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: references:references; bh=spOY6B/Pxm1imkpFatgjpncHqaXY2u8V4fMs4Kj+kNI=; b=Ri7ncOsF9EZ2VeeOYQsq3rKeeJjNsSIcO4b/uSsbiuIgRfK/p1aty8xsOTGT2mn2beiPU7 NtmiLaG+7ExkX0AA== From: Thomas Gleixner To: LKML Cc: Marc Zyngier , Peter Zijlstra , Jonathan Cameron , Bjorn Helgaas , linux-pci@vger.kernel.org, Nishanth Menon , Dhruva Gole , Tero Kristo , Santosh Shilimkar , Logan Gunthorpe , Dave Jiang , Jon Mason , Allen Hubbe , ntb@lists.linux.dev, Michael Kelley , Wei Liu , Haiyang Zhang , linux-hyperv@vger.kernel.org, Wei Huang , Manivannan Sadhasivam , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Jonathan Cameron Subject: [patch V3 05/10] PCI/MSI: Switch to MSI descriptor locking to guard() References: <20250317092919.008573387@linutronix.de> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 17 Mar 2025 14:29:27 +0100 (CET) Convert the code to use the new guard(msi_descs_lock). No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Jonathan Cameron Cc: Bjorn Helgaas Cc: linux-pci@vger.kernel.org --- V3: Use__free in __msix_setup_interrupts() - PeterZ V2: Remove the gotos - Jonathan --- drivers/pci/msi/api.c | 6 -- drivers/pci/msi/msi.c | 124 +++++++++++++++++++++++++------------------------- 2 files changed, 64 insertions(+), 66 deletions(-) --- a/drivers/pci/msi/api.c +++ b/drivers/pci/msi/api.c @@ -53,10 +53,9 @@ void pci_disable_msi(struct pci_dev *dev if (!pci_msi_enabled() || !dev || !dev->msi_enabled) return; - msi_lock_descs(&dev->dev); + guard(msi_descs_lock)(&dev->dev); pci_msi_shutdown(dev); pci_free_msi_irqs(dev); - msi_unlock_descs(&dev->dev); } EXPORT_SYMBOL(pci_disable_msi); @@ -196,10 +195,9 @@ void pci_disable_msix(struct pci_dev *de if (!pci_msi_enabled() || !dev || !dev->msix_enabled) return; - msi_lock_descs(&dev->dev); + guard(msi_descs_lock)(&dev->dev); pci_msix_shutdown(dev); pci_free_msi_irqs(dev); - msi_unlock_descs(&dev->dev); } EXPORT_SYMBOL(pci_disable_msix); --- a/drivers/pci/msi/msi.c +++ b/drivers/pci/msi/msi.c @@ -336,41 +336,11 @@ static int msi_verify_entries(struct pci return !entry ? 0 : -EIO; } -/** - * msi_capability_init - configure device's MSI capability structure - * @dev: pointer to the pci_dev data structure of MSI device function - * @nvec: number of interrupts to allocate - * @affd: description of automatic IRQ affinity assignments (may be %NULL) - * - * Setup the MSI capability structure of the device with the requested - * number of interrupts. A return value of zero indicates the successful - * setup of an entry with the new MSI IRQ. A negative return value indicates - * an error, and a positive return value indicates the number of interrupts - * which could have been allocated. - */ -static int msi_capability_init(struct pci_dev *dev, int nvec, - struct irq_affinity *affd) +static int __msi_capability_init(struct pci_dev *dev, int nvec, struct irq_affinity_desc *masks) { - struct irq_affinity_desc *masks = NULL; + int ret = msi_setup_msi_desc(dev, nvec, masks); struct msi_desc *entry, desc; - int ret; - - /* Reject multi-MSI early on irq domain enabled architectures */ - if (nvec > 1 && !pci_msi_domain_supports(dev, MSI_FLAG_MULTI_PCI_MSI, ALLOW_LEGACY)) - return 1; - - /* - * Disable MSI during setup in the hardware, but mark it enabled - * so that setup code can evaluate it. - */ - pci_msi_set_enable(dev, 0); - dev->msi_enabled = 1; - - if (affd) - masks = irq_create_affinity_masks(nvec, affd); - msi_lock_descs(&dev->dev); - ret = msi_setup_msi_desc(dev, nvec, masks); if (ret) goto fail; @@ -399,19 +369,48 @@ static int msi_capability_init(struct pc pcibios_free_irq(dev); dev->irq = entry->irq; - goto unlock; - + return 0; err: pci_msi_unmask(&desc, msi_multi_mask(&desc)); pci_free_msi_irqs(dev); fail: dev->msi_enabled = 0; -unlock: - msi_unlock_descs(&dev->dev); - kfree(masks); return ret; } +/** + * msi_capability_init - configure device's MSI capability structure + * @dev: pointer to the pci_dev data structure of MSI device function + * @nvec: number of interrupts to allocate + * @affd: description of automatic IRQ affinity assignments (may be %NULL) + * + * Setup the MSI capability structure of the device with the requested + * number of interrupts. A return value of zero indicates the successful + * setup of an entry with the new MSI IRQ. A negative return value indicates + * an error, and a positive return value indicates the number of interrupts + * which could have been allocated. + */ +static int msi_capability_init(struct pci_dev *dev, int nvec, + struct irq_affinity *affd) +{ + /* Reject multi-MSI early on irq domain enabled architectures */ + if (nvec > 1 && !pci_msi_domain_supports(dev, MSI_FLAG_MULTI_PCI_MSI, ALLOW_LEGACY)) + return 1; + + /* + * Disable MSI during setup in the hardware, but mark it enabled + * so that setup code can evaluate it. + */ + pci_msi_set_enable(dev, 0); + dev->msi_enabled = 1; + + struct irq_affinity_desc *masks __free(kfree) = + affd ? irq_create_affinity_masks(nvec, affd) : NULL; + + guard(msi_descs_lock)(&dev->dev); + return __msi_capability_init(dev, nvec, masks); +} + int __pci_enable_msi_range(struct pci_dev *dev, int minvec, int maxvec, struct irq_affinity *affd) { @@ -666,38 +665,39 @@ static void msix_mask_all(void __iomem * writel(ctrl, base + PCI_MSIX_ENTRY_VECTOR_CTRL); } -static int msix_setup_interrupts(struct pci_dev *dev, struct msix_entry *entries, - int nvec, struct irq_affinity *affd) -{ - struct irq_affinity_desc *masks = NULL; - int ret; +DEFINE_FREE(free_msi_irqs, struct pci_dev *, if (_T) pci_free_msi_irqs(_T)); - if (affd) - masks = irq_create_affinity_masks(nvec, affd); +static int __msix_setup_interrupts(struct pci_dev *__dev, struct msix_entry *entries, + int nvec, struct irq_affinity_desc *masks) +{ + struct pci_dev *dev __free(free_msi_irqs) = __dev; - msi_lock_descs(&dev->dev); - ret = msix_setup_msi_descs(dev, entries, nvec, masks); + int ret = msix_setup_msi_descs(dev, entries, nvec, masks); if (ret) - goto out_free; + return ret; ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX); if (ret) - goto out_free; + return ret; /* Check if all MSI entries honor device restrictions */ ret = msi_verify_entries(dev); if (ret) - goto out_free; + return ret; + retain_ptr(dev); msix_update_entries(dev, entries); - goto out_unlock; + return 0; +} -out_free: - pci_free_msi_irqs(dev); -out_unlock: - msi_unlock_descs(&dev->dev); - kfree(masks); - return ret; +static int msix_setup_interrupts(struct pci_dev *dev, struct msix_entry *entries, + int nvec, struct irq_affinity *affd) +{ + struct irq_affinity_desc *masks __free(kfree) = + affd ? irq_create_affinity_masks(nvec, affd) : NULL; + + guard(msi_descs_lock)(&dev->dev); + return __msix_setup_interrupts(dev, entries, nvec, masks); } /** @@ -871,13 +871,13 @@ void __pci_restore_msix_state(struct pci write_msg = arch_restore_msi_irqs(dev); - msi_lock_descs(&dev->dev); - msi_for_each_desc(entry, &dev->dev, MSI_DESC_ALL) { - if (write_msg) - __pci_write_msi_msg(entry, &entry->msg); - pci_msix_write_vector_ctrl(entry, entry->pci.msix_ctrl); + scoped_guard (msi_descs_lock, &dev->dev) { + msi_for_each_desc(entry, &dev->dev, MSI_DESC_ALL) { + if (write_msg) + __pci_write_msi_msg(entry, &entry->msg); + pci_msix_write_vector_ctrl(entry, entry->pci.msix_ctrl); + } } - msi_unlock_descs(&dev->dev); pci_msix_clear_and_set_ctrl(dev, PCI_MSIX_FLAGS_MASKALL, 0); } From patchwork Mon Mar 17 13:29:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 14019304 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6DE0724169A; Mon, 17 Mar 2025 13:29:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218172; cv=none; b=GknlNtn1swAJfsPWXUU7VLsUpC86urmSV3MdS1E9c9go0pcDEC3y2MxWWmeSTUU5RzyE7kiV6/75/HT5Vl+l5jPU0UBOCWqagpt/rAytjEj2oxmEAeU+fRmh+Pe5q7sgcSQZW6zuRVfOPvOzeUdKC12uEEEtxoe4NYtaGyOp8Kc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218172; c=relaxed/simple; bh=p1T7rPE6PheQf1PP3APwWVg7WjhczJsCxKMiW9/IOi4=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=gidLd/DyGMKINI1WD6aJkfkN85kmakiTIQ3UWvNWWW2bQhuzk45+m0mD71H/Ub9MUmG/bspw5fydnWALh7usAcKdM74dV29KfCdJfOoBK3Wk+LeN6zVtHYTy6hZOGpPhmLjwz6WKH4ZC3qtBOUyX0A8+YAdT/g5bllLYGYxfu+Y= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=I+lmDMBo; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=kD+t1zcj; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="I+lmDMBo"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="kD+t1zcj" Message-ID: <20250317092946.075206592@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742218168; 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: references:references; bh=3/dRMN7oC/HSArm4ElW7bOG9bX8b/YPinUN++M1pXqM=; b=I+lmDMBoCXWAe1aotpi9TzLp2syIkoYuxIIkcAv2AlYOPe2S/+dhRwasZEUe1P1bkGhVA9 408wd+oLUdwowQzLUtOzZlhI0kOmujCj3CtzG5vdX+GDCQN80WcxQFm6pWqfF9uu/yLAwo Qf+rugx8daut868JVXE+JaG6zsIgbiQh7B8Qli/TFav+bjjI8aZsJ9e2rqw4Lgf5cwt9Lc rvGCV/Jzfy1LEA9nvKEVvLlyY7pFKMyL/z8kiZZQfdNJJh5OMwEqOB6iuloxiT/iLQsTJS Swh8sq5Mx67ukMmheyWs8D5uE5jootv55P9+VmlsqmNWS/Xm9Hn3N1GNmbdZOg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742218168; 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: references:references; bh=3/dRMN7oC/HSArm4ElW7bOG9bX8b/YPinUN++M1pXqM=; b=kD+t1zcjj/G+g2DMtE4DoeqeCmi99mS8B8MhBunWIJqfeR6v1OSO6xWw1rXnRRonnrYdAH sfpNSZc2I8ZJbCAw== From: Thomas Gleixner To: LKML Cc: Marc Zyngier , Peter Zijlstra , Jonathan Cameron , Michael Kelley , Wei Liu , Bjorn Helgaas , Haiyang Zhang , linux-hyperv@vger.kernel.org, linux-pci@vger.kernel.org, Nishanth Menon , Dhruva Gole , Tero Kristo , Santosh Shilimkar , Logan Gunthorpe , Dave Jiang , Jon Mason , Allen Hubbe , ntb@lists.linux.dev, Wei Huang , Manivannan Sadhasivam , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Jonathan Cameron Subject: [patch V3 06/10] PCI: hv: Switch MSI descriptor locking to guard() References: <20250317092919.008573387@linutronix.de> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 17 Mar 2025 14:29:28 +0100 (CET) Convert the code to use the new guard(msi_descs_lock). No functional change intended. Signed-off-by: Thomas Gleixner Reviewed-by: Jonathan Cameron Reviewed-by: Michael Kelley Acked-by: Wei Liu Acked-by: Bjorn Helgaas Cc: Haiyang Zhang Cc: linux-hyperv@vger.kernel.org Cc: linux-pci@vger.kernel.org --- drivers/pci/controller/pci-hyperv.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) --- a/drivers/pci/controller/pci-hyperv.c +++ b/drivers/pci/controller/pci-hyperv.c @@ -3976,24 +3976,18 @@ static int hv_pci_restore_msi_msg(struct { struct irq_data *irq_data; struct msi_desc *entry; - int ret = 0; if (!pdev->msi_enabled && !pdev->msix_enabled) return 0; - msi_lock_descs(&pdev->dev); + guard(msi_descs_lock)(&pdev->dev); msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) { irq_data = irq_get_irq_data(entry->irq); - if (WARN_ON_ONCE(!irq_data)) { - ret = -EINVAL; - break; - } - + if (WARN_ON_ONCE(!irq_data)) + return -EINVAL; hv_compose_msi_msg(irq_data, &entry->msg); } - msi_unlock_descs(&pdev->dev); - - return ret; + return 0; } /* From patchwork Mon Mar 17 13:29:29 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 14019305 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74D0B241C87; Mon, 17 Mar 2025 13:29:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218173; cv=none; b=loTkViA7bjtWcBOJUZhMofQmitWT7aVzxAUre3WzKNUrwBZh6876KJowTcr5ip9nSufMqp5iCkFkgFC8aA3ZMiPOeLGlF4PdwhI4CgJZTMdC1QPqW2GHS7X/8PqOGgX59l+85/CJwfA80TpJRYxlifN73zvaTRmeA6d0wiiXxmI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218173; c=relaxed/simple; bh=xcaQ1WnuImEftvSdjIsQL5Zr2UJObjDBYpaP48Hg0CY=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=rkXpmTC6y1t1ynwUuwO1jUtJDN/Vhqd0mKu9U58MuS8f/fcxVnILQ5jV8uPbgsuuJYn9KrU5puXvODlab3HseDmZBLGgP3oHmRaerigSj3S9sjDriT67JbDs/yiyMM6dtK4QrXtRFFn+5m8AdH5vYzE6uh8eXr2frZfe9ivxWVU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mbOX4CP7; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=3JM0QmmW; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mbOX4CP7"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="3JM0QmmW" Message-ID: <20250317092946.137260005@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742218170; 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: references:references; bh=AcpTRZSWWsFoQkYWXkpRWk+2+ojMqUKmtLbTL950SRE=; b=mbOX4CP7Nn7eTypfyrwTIjKa6xVAgzVcPDro7GrIZd1KNHxcm7Fp8Khp8rNx4NrHhRMzoW Qf71OQXVlY8T+k/F2diBBXiuWs09me9almpkgPImMtn3KVxAdB+W1iao0EKaULohdBxSxH LpGYlmHKRzxtG850CmmkQALDwz/Fxc7135pwqkkV7ane8cubzL1vvX+qHEwCXFedk6PgL3 Sj7Vh+5v3eBcnh7SFluOi0k9hfLnKlrhZldVfBod1updabgrEC9gXpOFSGkqDzg2gYeDa9 lDP6mzwMqcpCI3+qj50oFR5xLYQcn7DwVd5uIhuJoV1pj3onV0ruXaRT6QdwzQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742218170; 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: references:references; bh=AcpTRZSWWsFoQkYWXkpRWk+2+ojMqUKmtLbTL950SRE=; b=3JM0QmmWphpbTOYSePe90IY8ofn5fR+T6+SzgNlaoW5Ud6tO5IWjGjtmSeDo0QWorQHwA2 zWHI60Oeo6+kZtDA== From: Thomas Gleixner To: LKML Cc: Marc Zyngier , Peter Zijlstra , Bjorn Helgaas , Wei Huang , linux-pci@vger.kernel.org, Jonathan Cameron , Nishanth Menon , Dhruva Gole , Tero Kristo , Santosh Shilimkar , Logan Gunthorpe , Dave Jiang , Jon Mason , Allen Hubbe , ntb@lists.linux.dev, Michael Kelley , Wei Liu , Haiyang Zhang , linux-hyperv@vger.kernel.org, Manivannan Sadhasivam , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Jonathan Cameron Subject: [patch V3 07/10] PCI/MSI: Provide a sane mechanism for TPH References: <20250317092919.008573387@linutronix.de> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 17 Mar 2025 14:29:29 +0100 (CET) The PCI/TPH driver fiddles with the MSI-X control word of an active interrupt completely unserialized against concurrent operations issued from the interrupt core. It also brings the PCI/MSI-X internal cached control word out of sync. Provide a function, which has the required serialization and keeps the control word cache in sync. Unfortunately this requires to look up and lock the interrupt descriptor, which should be only done in the interrupt core code. But confining this particular oddity in the PCI/MSI core is the lesser of all evil. A interrupt core implementation would require a larger pile of infrastructure and indirections for dubious value. Signed-off-by: Thomas Gleixner Acked-by: Bjorn Helgaas Cc: Wei Huang Cc: linux-pci@vger.kernel.org --- drivers/pci/msi/msi.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ drivers/pci/pci.h | 9 +++++++++ 2 files changed, 56 insertions(+) --- a/drivers/pci/msi/msi.c +++ b/drivers/pci/msi/msi.c @@ -910,6 +910,53 @@ void pci_free_msi_irqs(struct pci_dev *d } } +#ifdef CONFIG_PCIE_TPH +/** + * pci_msix_write_tph_tag - Update the TPH tag for a given MSI-X vector + * @pdev: The PCIe device to update + * @index: The MSI-X index to update + * @tag: The tag to write + * + * Returns: 0 on success, error code on failure + */ +int pci_msix_write_tph_tag(struct pci_dev *pdev, unsigned int index, u16 tag) +{ + struct msi_desc *msi_desc; + struct irq_desc *irq_desc; + unsigned int virq; + + if (!pdev->msix_enabled) + return -ENXIO; + + guard(msi_descs_lock)(&pdev->dev); + virq = msi_get_virq(&pdev->dev, index); + if (!virq) + return -ENXIO; + /* + * This is a horrible hack, but short of implementing a PCI + * specific interrupt chip callback and a huge pile of + * infrastructure, this is the minor nuissance. It provides the + * protection against concurrent operations on this entry and keeps + * the control word cache in sync. + */ + irq_desc = irq_to_desc(virq); + if (!irq_desc) + return -ENXIO; + + guard(raw_spinlock_irq)(&irq_desc->lock); + msi_desc = irq_data_get_msi_desc(&irq_desc->irq_data); + if (!msi_desc || msi_desc->pci.msi_attrib.is_virtual) + return -ENXIO; + + msi_desc->pci.msix_ctrl &= ~PCI_MSIX_ENTRY_CTRL_ST; + msi_desc->pci.msix_ctrl |= FIELD_PREP(PCI_MSIX_ENTRY_CTRL_ST, tag); + pci_msix_write_vector_ctrl(msi_desc, msi_desc->pci.msix_ctrl); + /* Flush the write */ + readl(pci_msix_desc_addr(msi_desc)); + return 0; +} +#endif + /* Misc. infrastructure */ struct pci_dev *msi_desc_to_pci_dev(struct msi_desc *desc) --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -989,6 +989,15 @@ int pcim_request_region_exclusive(struct const char *name); void pcim_release_region(struct pci_dev *pdev, int bar); +#ifdef CONFIG_PCI_MSI +int pci_msix_write_tph_tag(struct pci_dev *pdev, unsigned int index, u16 tag); +#else +static inline int pci_msix_write_tph_tag(struct pci_dev *pdev, unsigned int index, u16 tag) +{ + return -ENODEV; +} +#endif + /* * Config Address for PCI Configuration Mechanism #1 * From patchwork Mon Mar 17 13:29:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 14019306 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BFA424337D; Mon, 17 Mar 2025 13:29:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218174; cv=none; b=sL82meSkP5esbWRSulDbAKAk1iMyfEbiqYoMSTOKcBJdzLZrM8/KU3G/Nma+pzfuyJ+EUX9y82q/lgpnWDHnZIcGe18lxGOsjmm2yA26jrKcvlQII11WYK5c/maQN1773aWrelciAXsVHCujO71uvcd25fAdSN+G8f5Scabl400= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218174; c=relaxed/simple; bh=IxHn07/XNQfIoK2wd6oiU3iYlGYr8/vxJDTHGWM2l2k=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=EY7yYS/w/lOgmbSCUVE6N+I7c8mscIMucDdePpSDEbEFbHTKcA6PffxLlM7YLBnl5icTUmxYH30R8eiYTGmPqKpOVrBd7Y5SujO7AR+3hyngfJ22g8cJiUGsrUiK79ZAkiPjtY+SdMqT8p6PJf94rPGfhl7fJ8TsBHPqlQQQWus= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hJNbAwIm; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=3lBC7lQY; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hJNbAwIm"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="3lBC7lQY" Message-ID: <20250317092946.200676504@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742218171; 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: references:references; bh=tmOjEs51BuxJwnK86G31xxICC3Vf/dETy+yJGnO8adc=; b=hJNbAwImdxYqKEn+Tmz2QA6MrjkP64xGjBOjG16qtbSb57mKbuwu/bjkMxBZiFr2XPM4mM GWhUuJQMZ23b0HWshTQtpHT9QpSAcML8uSFh98sq+2Th94pVOpg/cvQFOpJiwEZr8h2XO8 A2s4o41dMl36LQ/X0qPKoJLBj3ToeM7PrqsOAgJgww3SGVxuk3rwkvSniE2TQhJV9Jxi8z LmKgftI9v5MNzQsAbg2bFAcDLgwEhZ7r/ytGYs/xE0IFv+DdzmePkSyPrVSOKuRaPn2KLK B3aEFxWdix5jIpiz3Im3N5InTn8QCGF3NX3JbUmzYx4La6Iyrb4ie+NnbgujeQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742218171; 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: references:references; bh=tmOjEs51BuxJwnK86G31xxICC3Vf/dETy+yJGnO8adc=; b=3lBC7lQY3sq9luHO+Yql2/2wNHPWdLP+1T9ZyPeqFAOlNCa+42Vl6eIL0NKqy90pigjJQ4 lErLAECnGf1aA8Bw== From: Thomas Gleixner To: LKML Cc: Marc Zyngier , Peter Zijlstra , Bjorn Helgaas , Wei Huang , linux-pci@vger.kernel.org, Jonathan Cameron , Nishanth Menon , Dhruva Gole , Tero Kristo , Santosh Shilimkar , Logan Gunthorpe , Dave Jiang , Jon Mason , Allen Hubbe , ntb@lists.linux.dev, Michael Kelley , Wei Liu , Haiyang Zhang , linux-hyperv@vger.kernel.org, Manivannan Sadhasivam , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Jonathan Cameron Subject: [patch V3 08/10] PCI/TPH: Replace the broken MSI-X control word update References: <20250317092919.008573387@linutronix.de> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 17 Mar 2025 14:29:31 +0100 (CET) The driver walks the MSI descriptors to test whether a descriptor exists for a given index. That's just abuse of the MSI internals. The same test can be done with a single function call by looking up whether there is a Linux interrupt number assigned at the index. What's worse is that the function is completely unserialized against modifications of the MSI-X control by operations issued from the interrupt core. It also brings the PCI/MSI-X internal cached control word out of sync. Remove the trainwreck and invoke the function provided by the PCI/MSI core to update it. Signed-off-by: Thomas Gleixner Acked-by: Bjorn Helgaas Cc: Wei Huang Cc: linux-pci@vger.kernel.org --- drivers/pci/tph.c | 44 +------------------------------------------- 1 file changed, 1 insertion(+), 43 deletions(-) --- a/drivers/pci/tph.c +++ b/drivers/pci/tph.c @@ -204,48 +204,6 @@ static u8 get_rp_completer_type(struct p return FIELD_GET(PCI_EXP_DEVCAP2_TPH_COMP_MASK, reg); } -/* Write ST to MSI-X vector control reg - Return 0 if OK, otherwise -errno */ -static int write_tag_to_msix(struct pci_dev *pdev, int msix_idx, u16 tag) -{ -#ifdef CONFIG_PCI_MSI - struct msi_desc *msi_desc = NULL; - void __iomem *vec_ctrl; - u32 val; - int err = 0; - - msi_lock_descs(&pdev->dev); - - /* Find the msi_desc entry with matching msix_idx */ - msi_for_each_desc(msi_desc, &pdev->dev, MSI_DESC_ASSOCIATED) { - if (msi_desc->msi_index == msix_idx) - break; - } - - if (!msi_desc) { - err = -ENXIO; - goto err_out; - } - - /* Get the vector control register (offset 0xc) pointed by msix_idx */ - vec_ctrl = pdev->msix_base + msix_idx * PCI_MSIX_ENTRY_SIZE; - vec_ctrl += PCI_MSIX_ENTRY_VECTOR_CTRL; - - val = readl(vec_ctrl); - val &= ~PCI_MSIX_ENTRY_CTRL_ST; - val |= FIELD_PREP(PCI_MSIX_ENTRY_CTRL_ST, tag); - writel(val, vec_ctrl); - - /* Read back to flush the update */ - val = readl(vec_ctrl); - -err_out: - msi_unlock_descs(&pdev->dev); - return err; -#else - return -ENODEV; -#endif -} - /* Write tag to ST table - Return 0 if OK, otherwise -errno */ static int write_tag_to_st_table(struct pci_dev *pdev, int index, u16 tag) { @@ -346,7 +304,7 @@ int pcie_tph_set_st_entry(struct pci_dev switch (loc) { case PCI_TPH_LOC_MSIX: - err = write_tag_to_msix(pdev, index, tag); + err = pci_msix_write_tph_tag(pdev, index, tag); break; case PCI_TPH_LOC_CAP: err = write_tag_to_st_table(pdev, index, tag); From patchwork Mon Mar 17 13:29:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 14019307 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5210A245024; Mon, 17 Mar 2025 13:29:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218176; cv=none; b=p0e11Yx+cqqHXl51DrDVQI0u3dO5ONPOzD8MYhjCPzJJVNm+ZWOSoFfD0qI9hng6gXlbxQnTelnKCf4NbHswc/Cjj5UH3Goj3GwInRPa47hhR8xytwxc3Ye4BFK4y3PpPQmnCNa7JCEEQk6sXv1O2qw/3ZgER39LzVExIhNWMws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218176; c=relaxed/simple; bh=DY28VPVW7+fB4Zvn75h8NeN5XlSmuJ+VAlmKTe+S6Po=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=dFrt2Kvswd7QKnG6DMmqdT0Ru9NI/HgcogQBosM3vik6g7pQV7BKlrS4GwibfOvJJkFxpwieZFgEKF1VaKwI6gXEh/0PdkvpIBID08/56yjtEwYk7ZsbdGcxpNJKzY58fhwan876K9WqS3tdSz/AEXEb+hl/UEVuML6LCLgUVug= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=01rPvh45; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=7mxQzyHb; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="01rPvh45"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="7mxQzyHb" Message-ID: <20250317092946.265146293@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742218172; 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: references:references; bh=giQPcsIXKp9+NdfLXd28Oq1XCDh7033RWcO16H9UKIo=; b=01rPvh45Moon1ZOcQ+mTZePGCLxv4aQ8wTIujvZSSR2UY2fhlN8BtpJC+bayMBooyyPeMJ nglM1FHZ5nv/gKLzv4Wch/m0lcRZ5LywD/TondtP7c0voU+MiOmapVhB0QFiOffqBMm+/A g5j+OW4oLiSUpqYT5FhSUucfy2XIL7eEpUdaeO0rVosyujj7GCTQ739faU0p42oXLiiKZN A+am/SFU/TDbBxoIW1nOVpdHu6sQUE/P+nl1C+LisnAya3lOgKfI8UlG+FKhLoa0TZaSJK KLqC1QkmkSH7cUaTJ8sJodpLsttdbU//ymY8KUyNgXN4BDlcHqE3WAv+TsEMWA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742218172; 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: references:references; bh=giQPcsIXKp9+NdfLXd28Oq1XCDh7033RWcO16H9UKIo=; b=7mxQzyHbgg767VhQJxzjb4HM7OyD44MVyf9TVQP4RbBTz2kFoaSaclt63SjscKePwZDuSB 0tRxvXKqu2XI1XCw== From: Thomas Gleixner To: LKML Cc: Marc Zyngier , Peter Zijlstra , Manivannan Sadhasivam , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org, Jonathan Cameron , Nishanth Menon , Dhruva Gole , Tero Kristo , Santosh Shilimkar , Logan Gunthorpe , Dave Jiang , Jon Mason , Allen Hubbe , ntb@lists.linux.dev, Bjorn Helgaas , linux-pci@vger.kernel.org, Michael Kelley , Wei Liu , Haiyang Zhang , linux-hyperv@vger.kernel.org, Wei Huang , Jonathan Cameron Subject: [patch V3 09/10] scsi: ufs: qcom: Remove the MSI descriptor abuse References: <20250317092919.008573387@linutronix.de> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 17 Mar 2025 14:29:32 +0100 (CET) The driver abuses the MSI descriptors for internal purposes. Aside of core code and MSI providers nothing has to care about their existence. They have been encapsulated with a lot of effort because this kind of abuse caused all sorts of issues including a maintainability nightmare. Rewrite the code so it uses dedicated storage to hand the required information to the interrupt handler. No functional change intended. Signed-off-by: Thomas Gleixner Cc: Manivannan Sadhasivam Cc: "James E.J. Bottomley" Cc: "Martin K. Petersen" Cc: linux-scsi@vger.kernel.org --- drivers/ufs/host/ufs-qcom.c | 77 ++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 37 deletions(-) --- a/drivers/ufs/host/ufs-qcom.c +++ b/drivers/ufs/host/ufs-qcom.c @@ -1782,15 +1782,19 @@ static void ufs_qcom_write_msi_msg(struc ufshcd_mcq_config_esi(hba, msg); } +struct ufs_qcom_irq { + unsigned int irq; + unsigned int idx; + struct ufs_hba *hba; +}; + static irqreturn_t ufs_qcom_mcq_esi_handler(int irq, void *data) { - struct msi_desc *desc = data; - struct device *dev = msi_desc_to_dev(desc); - struct ufs_hba *hba = dev_get_drvdata(dev); - u32 id = desc->msi_index; - struct ufs_hw_queue *hwq = &hba->uhq[id]; + struct ufs_qcom_irq *qi = data; + struct ufs_hba *hba = qi->hba; + struct ufs_hw_queue *hwq = &hba->uhq[qi->idx]; - ufshcd_mcq_write_cqis(hba, 0x1, id); + ufshcd_mcq_write_cqis(hba, 0x1, qi->idx); ufshcd_mcq_poll_cqe_lock(hba, hwq); return IRQ_HANDLED; @@ -1799,8 +1803,7 @@ static irqreturn_t ufs_qcom_mcq_esi_hand static int ufs_qcom_config_esi(struct ufs_hba *hba) { struct ufs_qcom_host *host = ufshcd_get_variant(hba); - struct msi_desc *desc; - struct msi_desc *failed_desc = NULL; + struct ufs_qcom_irq *qi; int nr_irqs, ret; if (host->esi_enabled) @@ -1811,47 +1814,47 @@ static int ufs_qcom_config_esi(struct uf * 2. Poll queues do not need ESI. */ nr_irqs = hba->nr_hw_queues - hba->nr_queues[HCTX_TYPE_POLL]; + qi = devm_kcalloc(hba->dev, nr_irqs, sizeof(*qi), GFP_KERNEL); + if (qi) + return -ENOMEM; + ret = platform_device_msi_init_and_alloc_irqs(hba->dev, nr_irqs, ufs_qcom_write_msi_msg); if (ret) { dev_err(hba->dev, "Failed to request Platform MSI %d\n", ret); - return ret; + goto cleanup; } - msi_lock_descs(hba->dev); - msi_for_each_desc(desc, hba->dev, MSI_DESC_ALL) { - ret = devm_request_irq(hba->dev, desc->irq, - ufs_qcom_mcq_esi_handler, - IRQF_SHARED, "qcom-mcq-esi", desc); + for (int idx = 0; idx < nr_irqs; idx++) { + qi[idx].irq = msi_get_virq(hba->dev, idx); + qi[idx].idx = idx; + qi[idx].hba = hba; + + ret = devm_request_irq(hba->dev, qi[idx].irq, ufs_qcom_mcq_esi_handler, + IRQF_SHARED, "qcom-mcq-esi", qi + idx); if (ret) { dev_err(hba->dev, "%s: Fail to request IRQ for %d, err = %d\n", - __func__, desc->irq, ret); - failed_desc = desc; - break; + __func__, qi[idx].irq, ret); + qi[idx].irq = 0; + goto cleanup; } } - msi_unlock_descs(hba->dev); - if (ret) { - /* Rewind */ - msi_lock_descs(hba->dev); - msi_for_each_desc(desc, hba->dev, MSI_DESC_ALL) { - if (desc == failed_desc) - break; - devm_free_irq(hba->dev, desc->irq, hba); - } - msi_unlock_descs(hba->dev); - platform_device_msi_free_irqs_all(hba->dev); - } else { - if (host->hw_ver.major == 6 && host->hw_ver.minor == 0 && - host->hw_ver.step == 0) - ufshcd_rmwl(hba, ESI_VEC_MASK, - FIELD_PREP(ESI_VEC_MASK, MAX_ESI_VEC - 1), - REG_UFS_CFG3); - ufshcd_mcq_enable_esi(hba); - host->esi_enabled = true; + if (host->hw_ver.major == 6 && host->hw_ver.minor == 0 && + host->hw_ver.step == 0) { + ufshcd_rmwl(hba, ESI_VEC_MASK, + FIELD_PREP(ESI_VEC_MASK, MAX_ESI_VEC - 1), + REG_UFS_CFG3); } - + ufshcd_mcq_enable_esi(hba); + host->esi_enabled = true; + return 0; + +cleanup: + for (int idx = 0; qi[idx].irq; idx++) + devm_free_irq(hba->dev, qi[idx].irq, hba); + platform_device_msi_free_irqs_all(hba->dev); + devm_kfree(hba->dev, qi); return ret; } From patchwork Mon Mar 17 13:29:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Gleixner X-Patchwork-Id: 14019308 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1CD362459C6; Mon, 17 Mar 2025 13:29:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218176; cv=none; b=d89UmpQeePWrWtXeauw4A4tEf2JYXnsE5nHqL7CRbO04ug4UYTgXR+UUo4HUR44wKNCLelU+DpQDiU6in5zyupemhc/a3ZOMu9Lb4POzionl0jZQYohTlO1sAzphP0AJVBNjackflA9CPzErNjKIBx6P1Luc7xubCVa3UwOs56Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1742218176; c=relaxed/simple; bh=OsAT6D9CldSVNahrghb7ysAo5aIZUfq3/QpPX5ODrUA=; h=Message-ID:From:To:Cc:Subject:References:MIME-Version: Content-Type:Date; b=OJxH/4a53zTCVc7YpABLvbcdZJMmenxcjuI/B5CZGrq8ao/l5Qehjg11auNP9k2dwCcoo4d9YChFfuA1/KuTF5afTrOHCsAeD7wFPbDwqPxmlfGZZKJ3nB7OWWqZd0VYtByY7342ezyj0yPqqw3te61KiTVd8AWDOvyfr+NiJoM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=w58zQrDa; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=uKTLVrch; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="w58zQrDa"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="uKTLVrch" Message-ID: <20250317092946.328138598@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1742218173; 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: references:references; bh=ia2tEh37yjsfzlatAAKITICyRXBXhsYCZirmqzcl6Tc=; b=w58zQrDaR37YtihrS3LzLZ/Z/H2aikA987p75/CIGz3gQSOYNYHwChnsLAPhw0qpYckVXk VqJjGAYMf+V13ZeKwexivKjbE0uld1SLY6VFxmKteflqQSLKUts/Iz/QZucBJB2HlDSftG gq/oWLFMO6TAyop/OOHVmTHW79LCSoZm43qfOvr1CPNCIPzHqzNGNfXbfow88tffhtoh8P vJl0B9tJc66VzxHNl5hnio6yiJuMv1YaSmQ4pfLFHL1AHDxIj6tnoNAITd1yGCvdMkEgHl wy40lt9bqE3RUQGVeJVkmsvmzCjrkGqFxDAQujpDnFdCZYqMUrU/Z9i8OUbfow== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1742218173; 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: references:references; bh=ia2tEh37yjsfzlatAAKITICyRXBXhsYCZirmqzcl6Tc=; b=uKTLVrcht/LXQCye+FwJgpfJTT2+/bln1MD8dmbjx/rEUxv7f52EMIrPOzIMJE+v3zAXJ8 08cOmwCYVxbhIIBQ== From: Thomas Gleixner To: LKML Cc: Marc Zyngier , Peter Zijlstra , Jonathan Cameron , Jonathan Cameron , Nishanth Menon , Dhruva Gole , Tero Kristo , Santosh Shilimkar , Logan Gunthorpe , Dave Jiang , Jon Mason , Allen Hubbe , ntb@lists.linux.dev, Bjorn Helgaas , linux-pci@vger.kernel.org, Michael Kelley , Wei Liu , Haiyang Zhang , linux-hyperv@vger.kernel.org, Wei Huang , Manivannan Sadhasivam , "James E.J. Bottomley" , "Martin K. Petersen" , linux-scsi@vger.kernel.org Subject: [patch V3 10/10] genirq/msi: Rename msi_[un]lock_descs() References: <20250317092919.008573387@linutronix.de> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Date: Mon, 17 Mar 2025 14:29:33 +0100 (CET) Now that all abuse is gone and the legit users are converted to guard(msi_descs_lock), rename the lock functions and document them as internal. No functional change. Signed-off-by: Thomas Gleixner Reviewed-by: Jonathan Cameron --- include/linux/msi.h | 8 ++++---- kernel/irq/msi.c | 16 ++++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) --- a/include/linux/msi.h +++ b/include/linux/msi.h @@ -224,11 +224,11 @@ struct msi_dev_domain { int msi_setup_device_data(struct device *dev); -void msi_lock_descs(struct device *dev); -void msi_unlock_descs(struct device *dev); +void __msi_lock_descs(struct device *dev); +void __msi_unlock_descs(struct device *dev); -DEFINE_LOCK_GUARD_1(msi_descs_lock, struct device, msi_lock_descs(_T->lock), - msi_unlock_descs(_T->lock)); +DEFINE_LOCK_GUARD_1(msi_descs_lock, struct device, __msi_lock_descs(_T->lock), + __msi_unlock_descs(_T->lock)); struct msi_desc *msi_domain_first_desc(struct device *dev, unsigned int domid, enum msi_desc_filter filter); --- a/kernel/irq/msi.c +++ b/kernel/irq/msi.c @@ -337,26 +337,30 @@ int msi_setup_device_data(struct device } /** - * msi_lock_descs - Lock the MSI descriptor storage of a device + * __msi_lock_descs - Lock the MSI descriptor storage of a device * @dev: Device to operate on + * + * Internal function for guard(msi_descs_lock). Don't use in code. */ -void msi_lock_descs(struct device *dev) +void __msi_lock_descs(struct device *dev) { mutex_lock(&dev->msi.data->mutex); } -EXPORT_SYMBOL_GPL(msi_lock_descs); +EXPORT_SYMBOL_GPL(__msi_lock_descs); /** - * msi_unlock_descs - Unlock the MSI descriptor storage of a device + * __msi_unlock_descs - Unlock the MSI descriptor storage of a device * @dev: Device to operate on + * + * Internal function for guard(msi_descs_lock). Don't use in code. */ -void msi_unlock_descs(struct device *dev) +void __msi_unlock_descs(struct device *dev) { /* Invalidate the index which was cached by the iterator */ dev->msi.data->__iter_idx = MSI_XA_MAX_INDEX; mutex_unlock(&dev->msi.data->mutex); } -EXPORT_SYMBOL_GPL(msi_unlock_descs); +EXPORT_SYMBOL_GPL(__msi_unlock_descs); static struct msi_desc *msi_find_desc(struct msi_device_data *md, unsigned int domid, enum msi_desc_filter filter)