genirq/msi: Spring cleaning
Subsystems (12)
v2 cover genirq/msi: Spring cleaning
This is version 2 of the cleanup work. The previous version can be found here:
https://lore.kernel.org/all/[email protected]
While converting the MSI descriptor locking to a lock guard() I stumbled over various abuse of MSI descriptors (again).
The following series cleans up the offending code and converts the MSI descriptor locking over to lock guards.
Changes vs. V1:
- Introduce retain_ptr() to allow using __free() when the allocation is consumed by a called function (on success) and therefore no_free_ptr() can't be used.
- Rework the PCI/MSI changes to avoid gotos in guard sections
- Drop patch 1 as it's already applied
- Collect Reviewed/Tested/Acked-by tags where appropriate
Patches 3,4,6-10 are unmodifed.
The series applies on:
git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git irq/msi
and is available from git:
git://git.kernel.org/pub/scm/linux/kernel/git/tglx/devel.git irq/msi
Thanks,
tglx
--- drivers/ntb/msi.c | 22 +--- drivers/pci/controller/pci-hyperv.c | 14 --- drivers/pci/msi/api.c | 6 - drivers/pci/msi/msi.c | 168 ++++++++++++++++++++++-------------- drivers/pci/pci.h | 9 + drivers/pci/tph.c | 44 --------- drivers/soc/ti/ti_sci_inta_msi.c | 10 -- drivers/ufs/host/ufs-qcom.c | 75 ++++++++-------- include/linux/cleanup.h | 17 +++ include/linux/irqdomain.h | 2 include/linux/msi.h | 7 + kernel/irq/msi.c | 125 ++++++++++---------------- 12 files changed, 247 insertions(+), 252 deletions(-)
PATCH v2 1/10 cleanup: Provide retain_ptr()
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.
--- include/linux/cleanup.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
include/linux/cleanup.h +17 -0
@@ -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...):
PATCH v2 2/10 genirq/msi: Use lock guards for MSI descriptor locking
In reply to Thomas GleixnerProvide a lock guard for MSI descriptor locking and update the core code accordingly.
No functional change intended.
---
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(-)
include/linux/irqdomain.h +2 -0
@@ -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 ;/**
include/linux/msi.h +3 -0
@@ -227,6 +227,9 @@ int msi_setup_device_data(struct devicevoid 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);
kernel/irq/msi.c +40 -69
231 lines not on this page · raw message
PATCH v2 3/10 soc: ti: ti_sci_inta_msi: Switch MSI descriptor locking to guard()
In reply to Thomas GleixnerConvert the code to use the new guard(msi_descs_lock).
No functional change intended.
--- drivers/soc/ti/ti_sci_inta_msi.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
drivers/soc/ti/ti_sci_inta_msi.c +3 -7
@@ -103,19 +103,15 @@ int ti_sci_inta_msi_domain_alloc_irqs(stif (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);
PATCH v2 4/10 NTB/msi: Switch MSI descriptor locking to lock guard()
In reply to Thomas GleixnerConvert the code to use the new guard(msi_descs_lock).
No functional change intended.
--- drivers/ntb/msi.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-)
drivers/ntb/msi.c +8 -14
@@ -106,10 +106,10 @@ int ntb_msi_setup_mws(struct ntb_dev *ntif (!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(structif (!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);
PATCH v2 5/10 PCI/MSI: Switch to MSI descriptor locking to guard()
In reply to Thomas GleixnerConvert the code to use the new guard(msi_descs_lock).
No functional change intended.
---
V2: Remove the gotos - Jonathan
--- drivers/pci/msi/api.c | 6 -- drivers/pci/msi/msi.c | 121 ++++++++++++++++++++++++-------------------------- 2 files changed, 62 insertions(+), 65 deletions(-)
drivers/pci/msi/api.c +2 -4
@@ -53,10 +53,9 @@ void pci_disable_msi(struct pci_dev *devif (!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 *deif (!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);
drivers/pci/msi/msi.c +60 -61
170 lines not on this page · raw message
PATCH v2 6/10 PCI: hv: Switch MSI descriptor locking to guard()
In reply to Thomas GleixnerConvert the code to use the new guard(msi_descs_lock).
No functional change intended.
--- drivers/pci/controller/pci-hyperv.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
drivers/pci/controller/pci-hyperv.c +4 -10
@@ -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 ; }/*
PATCH v2 7/10 PCI/MSI: Provide a sane mechanism for TPH
In reply to Thomas GleixnerThe 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.
--- drivers/pci/msi/msi.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ drivers/pci/pci.h | 9 +++++++++ 2 files changed, 56 insertions(+)
drivers/pci/msi/msi.c +47 -0
@@ -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)
drivers/pci/pci.h +9 -0
@@ -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 *
PATCH v2 8/10 PCI/TPH: Replace the broken MSI-X control word update
In reply to Thomas GleixnerThe 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.
--- drivers/pci/tph.c | 44 +------------------------------------------- 1 file changed, 1 insertion(+), 43 deletions(-)
drivers/pci/tph.c +1 -43
@@ -204,48 +204,6 @@ static u8 get_rp_completer_type(struct preturn 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_devswitch (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);
PATCH v2 9/10 scsi: ufs: qcom: Remove the MSI descriptor abuse
In reply to Thomas GleixnerThe 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.
--- drivers/ufs/host/ufs-qcom.c | 77 ++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 37 deletions(-)
drivers/ufs/host/ufs-qcom.c +40 -37
@@ -1782,15 +1782,19 @@ static void ufs_qcom_write_msi_msg(strucufshcd_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_handstatic 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; }
PATCH v2 10/10 genirq/msi: Rename msi_[un]lock_descs()
In reply to Thomas GleixnerNow 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.
--- include/linux/msi.h | 8 ++++---- kernel/irq/msi.c | 16 ++++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-)
include/linux/msi.h +4 -4
@@ -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);
kernel/irq/msi.c +10 -6
@@ -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)
Re: [patch V2 01/10] cleanup: Provide retain_ptr()
In reply to Thomas GleixnerQuoted 27 lines · On Thu, 13 Mar 2025 14:03:38 +0100 (CET) Thomas Gleixner <[email protected]>…
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.
Quoted 8 lines
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 <[email protected]>Cc Peter Zijlstra <[email protected]>
Seems sensible to me and the resulting code is reasonably easy to follow / contained in a small region.
Quoted 31 lines
--- include/linux/cleanup.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)include/linux/cleanup.h +17
-0@@ -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...):
Re: [patch V2 02/10] genirq/msi: Use lock guards for MSI descriptor locking
In reply to Thomas GleixnerQuoted 6 lines · On Thu, 13 Mar 2025 14:03:39 +0100 (CET) Thomas Gleixner <[email protected]>…
Provide a lock guard for MSI descriptor locking and update the core code accordingly.
No functional change intended.
Signed-off-by Thomas Gleixner <[email protected]>
Re: [patch V2 05/10] PCI/MSI: Switch to MSI descriptor locking to guard()
In reply to Thomas GleixnerQuoted 9 lines · On Thu, 13 Mar 2025 14:03:44 +0100 (CET) Thomas Gleixner <[email protected]>…
Convert the code to use the new
guard(msi_descs_lock).No functional change intended.
Signed-off-by Thomas Gleixner <[email protected]>Cc Bjorn Helgaas <[email protected]>---
V2: Remove the gotos - Jonathan
Hi Thomas,
There is a bit of the original code that is carried forwards here that superficially seemed overly complex. However as far as I can tell this is functionally the same as you intended. So with that in mind if my question isn't complete garbage, maybe a readability issue for another day.
Quoted 5 lines
drivers/pci/msi/msi.c +0
-0
drivers/pci/msi/msi.c static void msix_mask_all(void __iomem * · line 698
Quoted 10 lines
drivers/pci/msi/msi.c +10
-0+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); +int ret =__msix_setup_interrupts (dev, entries, nvec, masks); +if (ret) +pci_free_msi_irqs (dev);
It's not immediately obvious what this is undoing (i.e. where the alloc is). I think it is at least mostly the pci_msi_setup_msi_irqs in __msix_setup_interrupts
Why not handle the error in __msix_setup_interrupts and make that function side effect free. Does require gotos but in a function that isn't doing any cleanup magic so should be fine.
Mind you I'm not following the logic in msix_setup_interrupts() before this series either. i.e. why doesn't msix_setup_msi_descs() clean up after itself on failure (i.e. undo loop iterations that weren't failures) as that at least superficially looks like it would give more readable code.
So this is the same as current and as such the patch is fine I think.
Quoted 3 lines
return ret;
}
Re: [patch V2 05/10] PCI/MSI: Switch to MSI descriptor locking to guard()
Quoted 8 lines · Jonathan Cameron
Review
Quoted 4 lines
(quoted diff) +4
-0+ guard(msi_descs_lock)(&dev->dev); + int ret = __msix_setup_interrupts(dev, entries, nvec, masks); + if (ret) + pci_free_msi_irqs(dev);It's not immediately obvious what this is undoing (i.e. where the alloc is). I think it is at least mostly the
pci_msi_setup_msi_irqsin__msix_setup_interrupts
It's a universal cleanup for all possible error cases.
Quoted 3 lines
Why not handle the error in
__msix_setup_interruptsand make that function side effect free. Does require gotos but in a function that isn't doing any cleanup magic so should be fine.
I had the gotos first and then hated them. But you are right, it's better to have them than having the magic clean up at the call site.
I'll fold the delta patch below.
Thanks,
tglx
---
drivers/pci/msi/msi.c +8 -7
@@ -671,19 +671,23 @@ static int __msix_setup_interrupts(strucint ret =msix_setup_msi_descs (dev, entries, nvec, masks);if (ret) -return ret; + goto fail; ret =pci_msi_setup_msi_irqs (dev, nvec,PCI_CAP_ID_MSIX );if (ret) -return ret; + goto fail;/* Check if all MSI entries honor device restrictions */ ret =msi_verify_entries (dev);if (ret) -return ret; + goto fail;msix_update_entries (dev, entries);return 0 ; + +fail: +pci_free_msi_irqs (dev); +return ret; } static int msix_setup_interrupts(struct pci_dev *dev, struct msix_entry *entries,
@@ -693,10 +697,7 @@ static int msix_setup_interrupts(struct affd ?irq_create_affinity_masks (nvec, affd) :NULL ;guard (msi_descs_lock)(&dev->dev); -int ret =__msix_setup_interrupts (dev, entries, nvec, masks); -if (ret) -pci_free_msi_irqs (dev); -return ret; +return __msix_setup_interrupts (dev, entries, nvec, masks); }/**
Re: [patch V2 01/10] cleanup: Provide retain_ptr()
In reply to Thomas GleixnerQuoted 24 lines · Thomas Gleixner
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;
Quoted 8 lines
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.
So no objection per se, but one way to solve this is by handing ownership to bar_create(), such that it is responsible for freeing f on failure.
Anyway, I suspect the __must_check came from Linus, OTOH take_fd(), the
equivalent for file descriptors also don't have that __must_check. So
clearly we have precedent here.
Re: [patch V2 05/10] PCI/MSI: Switch to MSI descriptor locking to guard()
In reply to Thomas GleixnerQuoted 42 lines · Thomas Gleixner
Quoted 8 lines · Jonathan Cameron
Review
Quoted 4 lines
(quoted diff) +4
-0+ guard(msi_descs_lock)(&dev->dev); + int ret = __msix_setup_interrupts(dev, entries, nvec, masks); + if (ret) + pci_free_msi_irqs(dev);It's not immediately obvious what this is undoing (i.e. where the alloc is). I think it is at least mostly the
pci_msi_setup_msi_irqsin__msix_setup_interruptsIt's a universal cleanup for all possible error cases.
Quoted 3 lines
Why not handle the error in
__msix_setup_interruptsand make that function side effect free. Does require gotos but in a function that isn't doing any cleanup magic so should be fine.I had the gotos first and then hated them. But you are right, it's better to have them than having the magic clean up at the call site.
I'll fold the delta patch below.
Thanks,
tglx
---drivers/pci/msi/msi.c +2
-2@@ -671,19 +671,23 @@ static int __msix_setup_interrupts(strucint ret =msix_setup_msi_descs (dev, entries, nvec, masks);if (ret) -return ret; + goto fail; ret =pci_msi_setup_msi_irqs (dev, nvec,PCI_CAP_ID_MSIX );if (ret) -return ret; + goto fail;/* Check if all MSI entries honor device restrictions */ ret =msi_verify_entries (dev);if (ret)
drivers/pci/msi/msi.c static int __msix_setup_interrupts(struc · line 691
Quoted 10 lines
drivers/pci/msi/msi.c +5
-1-return ret; + goto fail;msix_update_entries (dev, entries);return 0 ; + +fail: +pci_free_msi_irqs (dev); +return ret; }
How about something like:
int __msix_setup_interrupts(struct device *_dev,... )
{
struct device *dev __free(msi_irqs) = _dev;
int ret = msix_setup_msi_descs(dev, entries, nvec, masks);
if (ret)
return ret;
ret = pci_msi_setup_msi_irqs(dev, nvec, PCI_CAP_ID_MSIX);
if (ret)
return ret;
/* Check if all MSI entries honor device restrictions */
ret = msi_verify_entries(dev);
if (ret)
return ret;
retain_ptr(dev);
msix_update_entries(dev, entries);
return 0;
}?
Re: [patch V2 01/10] cleanup: Provide retain_ptr()
In reply to Thomas GleixnerQuoted 49 lines · Thomas Gleixner
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 <[email protected]>Cc Peter Zijlstra <[email protected]>--- include/linux/cleanup.h | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)include/linux/cleanup.h +4
-0@@ -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. +*
include/linux/cleanup.h const volatile void * __must_check_fn(co · line 232
Quoted 10 lines
include/linux/cleanup.h +10
-0+* 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;
Is it better like
ret = bar(f); if (ret) return ret; retain_ptr(f); return 0;
If there are more than one f, like f1, f2, f3....
ret= bar(f1, f2, ....) if (ret) return ret; retain_ptr(f1); retain_ptr(f2); ... return 0;
Or define a macro like
#defne no_free_ptr_on_ok(ret, p) ret ? ret : __get_and_null(p, NULL), 0
ret = bar (f); return no_free_ptr_on_ok(ret, f);
Frank
Quoted 7 lines
include/linux/cleanup.h +3
-0+*/ +#define retain_ptr (p) \ + __get_and_null(p, NULL)/* * DEFINE_CLASS(name, type, exit, init, init_args...):
Re: [patch V2 09/10] scsi: ufs: qcom: Remove the MSI descriptor abuse
In reply to Thomas GleixnerQuoted 11 lines · Thomas Gleixner
drivers/ufs/host/ufs-qcom.c +1
-2@@ -1799,8 +1803,7 @@ static irqreturn_t ufs_qcom_mcq_esi_handstatic 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)
drivers/ufs/host/ufs-qcom.c static int ufs_qcom_config_esi(struct uf · line 1818
Quoted 5 lines
drivers/ufs/host/ufs-qcom.c +2
-0@@ -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)
This NULL check is reversed. Missing !.
regards,
dan carpenter
Quoted 4 lines
drivers/ufs/host/ufs-qcom.c +2
-0+return -ENOMEM ; + ret =platform_device_msi_init_and_alloc_irqs (hba->dev, nr_irqs, ufs_qcom_write_msi_msg);
Re: [patch V2 09/10] scsi: ufs: qcom: Remove the MSI descriptor abuse
Quoted 10 lines · Dan Carpenter
Quoted 9 lines · Thomas Gleixner
drivers/ufs/host/ufs-qcom.c +1
-2@@ -1799,8 +1803,7 @@ static irqreturn_t ufs_qcom_mcq_esi_handstatic 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;
Quoted 9 lines
drivers/ufs/host/ufs-qcom.c static int ufs_qcom_config_esi(struct uf · line 1818
Quoted 7 lines
drivers/ufs/host/ufs-qcom.c +2
-0if (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)This NULL check is reversed. Missing !.
Duh. I'm sure I've fixed that before.