ahci: enhance error handling and resource management in ahci_init_one
PATCH v2 1/1 ahci: enhance error handling and resource management in ahci_init_one
Problem:
The current implementation of ahci_init_one has several issues with error handling and resource management. It lacks proper error cleanup paths, doesn't initialize pointers to NULL, and has inconsistent error handling patterns throughout the code. This can lead to resource leaks and make debugging initialization failures difficult.
Solution:
This patch enhances the error handling and resource management in ahci_init_one by:
- Adding comprehensive error checking with descriptive error messages
- Improving error propagation through return codes
- Adding proper error cleanup paths for all resource allocations
- Initializing pointers to NULL to prevent use-after-free bugs
- Implementing proper cleanup of allocated resources in error paths
- Adding more descriptive error messages for all failure points
- Including error codes in log messages for better diagnostics
- Adding warning messages for potential system issues
- Improving code structure with proper error handling paths
- Adding proper error return labels
- Making code more maintainable with consistent error handling patterns
Technical Details:
- Added proper initialization of pointers (hpriv, host) to NULL
- Added error cleanup paths with proper resource release
- Improved error messages to include specific error codes
- Added proper error handling for all resource allocation failures
- Added proper cleanup of allocated resources in error paths
- Improved code organization with clear error handling paths
- Added proper error return labels for better code flow
Note: Some error checks and logging have been simplified to reduce churn while maintaining robust error handling. The focus is on critical error paths and resource management rather than redundant checks. Log levels have been adjusted to use dev_warn for non-fatal warnings and dev_dbg for quirk failures.
--- drivers/ata/ahci.c | 150 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 85 insertions(+), 65 deletions(-)
drivers/ata/ahci.c +56 -17
@@ -1611,460 +1611,555 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) {struct ahci_host_priv *hpriv =NULL ;struct ata_host *host =NULL ;void __iomem *mmio =NULL ; +int n_ports, i, rc = -ENOMEM ; -int n_ports, i, rc;u32 tmp, cap, port_map;u32 saved_cap;struct device *dev = &pdev->dev;VPRINTK ("ahci_init_one enter\n" ); +/* acquire resources with proper error handling */ -/* acquire resources */ rc =pcim_enable_device (pdev);if (rc) { +dev_err (dev,"Failed to enable PCI device: %d\n" , rc); + goto err_out; -return rc; } rc =pcim_iomap_regions (pdev,1 <<AHCI_PCI_BAR_STANDARD ,DRV_NAME );if (rc) { +dev_err (dev,"Failed to map PCI regions: %d\n" , rc); + goto err_out; -return rc; } mmio =pcim_iomap_table (pdev)[AHCI_PCI_BAR_STANDARD ]; rc =pci_alloc_irq_vectors (pdev,1 ,AHCI_MAX_PORTS ,PCI_IRQ_ALL_TYPES );if (rc <0 ) { +dev_err (dev,"Failed to allocate IRQ vectors: %d\n" , rc); + goto err_out; -return rc; } +/* allocate and initialize host private data */ hpriv =devm_kzalloc (dev,sizeof (*hpriv),GFP_KERNEL );if (!hpriv) { +dev_err (dev,"Failed to allocate host private data\n" ); + goto err_out; -return -ENOMEM ; } hpriv->mmio = mmio; hpriv->flags = (unsigned long )ent->driver_data; hpriv->irq = pdev->irq; +/* apply board quirks */ if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { + rc =ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) { +dev_dbg (dev,"Intel PCS quirk failed (%d)\n" , rc); + goto err_host; + } -ahci_intel_pcs_quirk (pdev, hpriv); } +/* apply port map mask if present */ ahci_get_port_map_mask (dev, hpriv); +/* save initial config */ rc =ahci_pci_save_initial_config (pdev, hpriv);if (rc) { +dev_err (dev,"Failed to save initial configuration: %d\n" , rc); + goto err_out; -return rc; } +/* prepare host */ cap = hpriv->cap; saved_cap = cap; port_map = hpriv->port_map; n_ports =ahci_calc_n_ports (cap, port_map); host =ata_host_alloc_pinfo (dev, ahci_port_info + ent->driver_data, n_ports);if (!host) { +dev_err (dev,"Failed to allocate ATA host\n" ); + goto err_out; -return -ENOMEM ; } host->private_data = hpriv; +/* configure DMA masks */ rc =ahci_configure_dma_masks (pdev, hpriv);if (rc) { +dev_err (dev,"Failed to configure DMA masks: %d\n" , rc); + goto err_host; -return rc; } +/* initialize adapter */ ahci_pci_init_controller (host); rc =ahci_reset_controller (host);if (rc) { +dev_err (dev,"Failed to reset controller: %d\n" , rc); + goto err_host; -return rc; } +/* apply fixups for broken systems */ if (ahci_broken_system_poweroff (pdev)) { +dev_warn (dev,"System may need power cycle after shutdown\n" ); -dev_info (dev,"quirky BIOS, skipping spindown on poweroff\n" ); } +/* configure LPM policy */ for (i =0 ; i < n_ports; i++) {ahci_update_initial_lpm_policy (host->ports[i]); } +/* apply platform-specific workarounds */ if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { + rc =ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) { +dev_dbg (dev,"Intel PCS quirk failed (%d)\n" , rc); + goto err_host; + } -ahci_intel_pcs_quirk (pdev, hpriv); } +/* apply Apple MCP89 workaround */ if (is_mcp89_apple (pdev)) { + rc =ahci_mcp89_apple_enable (pdev); +if (rc) { +dev_err (dev,"Failed to enable MCP89 Apple: %d\n" , rc); + goto err_host; + } -ahci_mcp89_apple_enable (pdev); } +/* apply Acer SA5-271 workaround */ acer_sa5_271_workaround (hpriv, pdev); +/* initialize and enable interrupts */ ahci_init_irq (pdev, n_ports, hpriv);ahci_pci_enable_interrupts (host); +/* print information */ ahci_pci_print_info (host); +/* register with libata */ rc =ata_host_activate (host, hpriv->irq, ahci_interrupt,IRQF_SHARED , + &ahci_sht); - &ahci_sht);if (rc) { +dev_err (dev,"Failed to activate ATA host: %d\n" , rc); + goto err_host; -return rc; }return 0 ; +err_host: +ata_host_detach (host);// host is NULL-checked internally +err_out: +return rc; -return 0 ; }
Damien Le Moal
maintainerQuoted 1 line · Alexander Roman
Problem:
This is not needed. It is clear that you are describing the problem.
Quoted 6 lines
The current implementation of
ahci_init_onehas several issues with error handling and resource management. It lacks proper error cleanup paths, doesn't initialize pointers to NULL, and has inconsistent error handling patterns throughout the code. This can lead to resource leaks and make debugging initialization failures difficult.Solution:
Same here. It is clear you are describing how you solve the problem.
Quoted 13 lines
This patch enhances the error handling and resource management in
ahci_init_oneby:
- Adding comprehensive error checking with descriptive error messages
- Improving error propagation through return codes
- Adding proper error cleanup paths for all resource allocations
- Initializing pointers to NULL to prevent use-after-free bugs
- Implementing proper cleanup of allocated resources in error paths
- Adding more descriptive error messages for all failure points
- Including error codes in log messages for better diagnostics
- Adding warning messages for potential system issues
- Improving code structure with proper error handling paths
- Adding proper error return labels
- Making code more maintainable with consistent error handling patterns
Quoted 1 line
Technical Details:
Integrate that in the previous section please.
Quoted 8 lines
- Added proper initialization of pointers (hpriv, host) to NULL
- Added error cleanup paths with proper resource release
- Improved error messages to include specific error codes
- Added proper error handling for all resource allocation failures
- Added proper cleanup of allocated resources in error paths
- Improved code organization with clear error handling paths
- Added proper error return labels for better code flow
Quoted 6 lines
Note: Some error checks and logging have been simplified to reduce churn while maintaining robust error handling. The focus is on critical error paths and resource management rather than redundant checks. Log levels have been adjusted to use
dev_warnfor non-fatal warnings anddev_dbgfor quirk failures.Signed-off-by Alexander Roman <[email protected]>]
Please send patches to the maintainers too.
Quoted 8 lines
--- drivers/ata/ahci.c | 150 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 85 insertions(+), 65 deletions(-)
drivers/ata/ahci.c static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) { · line 1619
Quoted 10 lines
drivers/ata/ahci.c +1
-1@@ -1611,460 +1611,555 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) {struct ahci_host_priv *hpriv =NULL ;struct ata_host *host =NULL ;void __iomem *mmio =NULL ; +int n_ports, i, rc = -ENOMEM ; -int n_ports, i, rc;u32 tmp, cap, port_map;u32 saved_cap;struct device *dev = &pdev->dev;VPRINTK ("ahci_init_one enter\n" );
This is old code. What is this patch against ??? This code does not exist since a long time ago. Please rebase.
drivers/ata/ahci.c static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) { · line 1621
Quoted 3 lines
drivers/ata/ahci.c +1
-1+/* acquire resources with proper error handling */ -/* acquire resources */
The original comment was not useful. Making it more detailed still does not make it useful as it is very clear what this code hunk does. I would be more in favor of completely dropping this useless comment.
drivers/ata/ahci.c static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) { · line 1625
Quoted 4 lines
drivers/ata/ahci.c +2
-0rc =pcim_enable_device (pdev);if (rc) { +dev_err (dev,"Failed to enable PCI device: %d\n" , rc); + goto err_out;
Wrong indentation.
Quoted 9 lines
drivers/ata/ahci.c +2
-2-return rc; } rc =pcim_iomap_regions (pdev,1 <<AHCI_PCI_BAR_STANDARD ,DRV_NAME );if (rc) { +dev_err (dev,"Failed to map PCI regions: %d\n" , rc); + goto err_out; -return rc; }
drivers/ata/ahci.c static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) { · line 1641
Quoted 10 lines
drivers/ata/ahci.c +3
-1mmio =pcim_iomap_table (pdev)[AHCI_PCI_BAR_STANDARD ]; rc =pci_alloc_irq_vectors (pdev,1 ,AHCI_MAX_PORTS ,PCI_IRQ_ALL_TYPES );if (rc <0 ) { +dev_err (dev,"Failed to allocate IRQ vectors: %d\n" , rc); + goto err_out; -return rc; } +/* allocate and initialize host private data */
Same here: comment not needed. It is clear what the code does. Please only add comments for describing things that are not obvious.
Quoted 12 lines
drivers/ata/ahci.c +3
-1hpriv =devm_kzalloc (dev,sizeof (*hpriv),GFP_KERNEL );if (!hpriv) { +dev_err (dev,"Failed to allocate host private data\n" ); + goto err_out; -return -ENOMEM ; } hpriv->mmio = mmio; hpriv->flags = (unsigned long )ent->driver_data; hpriv->irq = pdev->irq; +/* apply board quirks */
drivers/ata/ahci.c static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) { · line 1661
Quoted 10 lines
drivers/ata/ahci.c +6
-1if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { + rc =ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) { +dev_dbg (dev,"Intel PCS quirk failed (%d)\n" , rc); + goto err_host; + } -ahci_intel_pcs_quirk (pdev, hpriv); } +/* apply port map mask if present */
Comment not needed.
drivers/ata/ahci.c static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) { · line 1664
Quoted 3 lines
drivers/ata/ahci.c +1
-0ahci_get_port_map_mask (dev, hpriv); +/* save initial config */
Here too.
drivers/ata/ahci.c static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) { · line 1671
Quoted 8 lines
drivers/ata/ahci.c +3
-1rc =ahci_pci_save_initial_config (pdev, hpriv);if (rc) { +dev_err (dev,"Failed to save initial configuration: %d\n" , rc); + goto err_out; -return rc; } +/* prepare host */
Same.
Quoted 5 lines
drivers/ata/ahci.c +0
-0cap = hpriv->cap; saved_cap = cap; port_map = hpriv->port_map; n_ports =ahci_calc_n_ports (cap, port_map);
drivers/ata/ahci.c static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) { · line 1685
Quoted 10 lines
drivers/ata/ahci.c +3
-1host =ata_host_alloc_pinfo (dev, ahci_port_info + ent->driver_data, n_ports);if (!host) { +dev_err (dev,"Failed to allocate ATA host\n" ); + goto err_out; -return -ENOMEM ; } host->private_data = hpriv; +/* configure DMA masks */
Again...
drivers/ata/ahci.c static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) { · line 1692
Quoted 8 lines
drivers/ata/ahci.c +3
-1rc =ahci_configure_dma_masks (pdev, hpriv);if (rc) { +dev_err (dev,"Failed to configure DMA masks: %d\n" , rc); + goto err_host; -return rc; } +/* initialize adapter */
And again... I stop here. You see my point. The function names are clear enough...
Quoted 68 lines
drivers/ata/ahci.c +28
-7ahci_pci_init_controller (host); rc =ahci_reset_controller (host);if (rc) { +dev_err (dev,"Failed to reset controller: %d\n" , rc); + goto err_host; -return rc; } +/* apply fixups for broken systems */ if (ahci_broken_system_poweroff (pdev)) { +dev_warn (dev,"System may need power cycle after shutdown\n" ); -dev_info (dev,"quirky BIOS, skipping spindown on poweroff\n" ); } +/* configure LPM policy */ for (i =0 ; i < n_ports; i++) {ahci_update_initial_lpm_policy (host->ports[i]); } +/* apply platform-specific workarounds */ if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { + rc =ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) { +dev_dbg (dev,"Intel PCS quirk failed (%d)\n" , rc); + goto err_host; + } -ahci_intel_pcs_quirk (pdev, hpriv); } +/* apply Apple MCP89 workaround */ if (is_mcp89_apple (pdev)) { + rc =ahci_mcp89_apple_enable (pdev); +if (rc) { +dev_err (dev,"Failed to enable MCP89 Apple: %d\n" , rc); + goto err_host; + } -ahci_mcp89_apple_enable (pdev); } +/* apply Acer SA5-271 workaround */ acer_sa5_271_workaround (hpriv, pdev); +/* initialize and enable interrupts */ ahci_init_irq (pdev, n_ports, hpriv);ahci_pci_enable_interrupts (host); +/* print information */ ahci_pci_print_info (host); +/* register with libata */ rc =ata_host_activate (host, hpriv->irq, ahci_interrupt,IRQF_SHARED , + &ahci_sht); - &ahci_sht);if (rc) { +dev_err (dev,"Failed to activate ATA host: %d\n" , rc); + goto err_host; -return rc; }return 0 ; +err_host: +ata_host_detach (host);// host is NULL-checked internally +err_out: +return rc; -return 0 ; }
Signature · 2 lines
Damien Le Moal Western Digital Research
PATCH v3 1/1 ahci: enhance error handling and resource management in ahci_init_one
Problem:
The current implementation of ahci_init_one has several issues with error handling and resource management. It lacks proper error cleanup paths, doesn't initialize pointers to NULL, and has inconsistent error handling patterns throughout the code. This can lead to resource leaks and make debugging initialization failures difficult.
Solution:
This patch enhances the error handling and resource management in ahci_init_one by:
- Adding comprehensive error checking with descriptive error messages
- Improving error propagation through return codes
- Adding proper error cleanup paths for all resource allocations
- Initializing pointers to NULL to prevent use-after-free bugs
- Implementing proper cleanup of allocated resources in error paths
- Adding more descriptive error messages for all failure points
- Including error codes in log messages for better diagnostics
- Adding warning messages for potential system issues
- Improving code structure with proper error handling paths
- Adding proper error return labels
- Making code more maintainable with consistent error handling patterns
Technical Details:
- Added proper initialization of pointers (hpriv, host) to NULL
- Added error cleanup paths with proper resource release
- Improved error messages to include specific error codes
- Added proper error handling for all resource allocation failures
- Added proper cleanup of allocated resources in error paths
- Improved code organization with clear error handling paths
- Added proper error return labels for better code flow
Note: Some error checks and logging have been simplified to reduce churn while maintaining robust error handling. The focus is on critical error paths and resource management rather than redundant checks. Log levels have been adjusted to use dev_warn for non-fatal warnings and dev_dbg for quirk failures.
--- drivers/ata/ahci.c | 150 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 85 insertions(+), 65 deletions(-)
drivers/ata/ahci.c +56 -17
@@ -1611,460 +1611,555 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) {struct ahci_host_priv *hpriv =NULL ;struct ata_host *host =NULL ;void __iomem *mmio =NULL ; +int n_ports, i, rc = -ENOMEM ; -int n_ports, i, rc;u32 tmp, cap, port_map;u32 saved_cap;struct device *dev = &pdev->dev;VPRINTK ("ahci_init_one enter\n" ); +/* acquire resources with proper error handling */ -/* acquire resources */ rc =pcim_enable_device (pdev);if (rc) { +dev_err (dev,"Failed to enable PCI device: %d\n" , rc); + goto err_out; -return rc; } rc =pcim_iomap_regions (pdev,1 <<AHCI_PCI_BAR_STANDARD ,DRV_NAME );if (rc) { +dev_err (dev,"Failed to map PCI regions: %d\n" , rc); + goto err_out; -return rc; } mmio =pcim_iomap_table (pdev)[AHCI_PCI_BAR_STANDARD ]; rc =pci_alloc_irq_vectors (pdev,1 ,AHCI_MAX_PORTS ,PCI_IRQ_ALL_TYPES );if (rc <0 ) { +dev_err (dev,"Failed to allocate IRQ vectors: %d\n" , rc); + goto err_out; -return rc; } +/* allocate and initialize host private data */ hpriv =devm_kzalloc (dev,sizeof (*hpriv),GFP_KERNEL );if (!hpriv) { +dev_err (dev,"Failed to allocate host private data\n" ); + goto err_out; -return -ENOMEM ; } hpriv->mmio = mmio; hpriv->flags = (unsigned long )ent->driver_data; hpriv->irq = pdev->irq; +/* apply board quirks */ if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { + rc =ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) { +dev_dbg (dev,"Intel PCS quirk failed (%d)\n" , rc); + goto err_host; + } -ahci_intel_pcs_quirk (pdev, hpriv); } +/* apply port map mask if present */ ahci_get_port_map_mask (dev, hpriv); +/* save initial config */ rc =ahci_pci_save_initial_config (pdev, hpriv);if (rc) { +dev_err (dev,"Failed to save initial configuration: %d\n" , rc); + goto err_out; -return rc; } +/* prepare host */ cap = hpriv->cap; saved_cap = cap; port_map = hpriv->port_map; n_ports =ahci_calc_n_ports (cap, port_map); host =ata_host_alloc_pinfo (dev, ahci_port_info + ent->driver_data, n_ports);if (!host) { +dev_err (dev,"Failed to allocate ATA host\n" ); + goto err_out; -return -ENOMEM ; } host->private_data = hpriv; +/* configure DMA masks */ rc =ahci_configure_dma_masks (pdev, hpriv);if (rc) { +dev_err (dev,"Failed to configure DMA masks: %d\n" , rc); + goto err_host; -return rc; } +/* initialize adapter */ ahci_pci_init_controller (host); rc =ahci_reset_controller (host);if (rc) { +dev_err (dev,"Failed to reset controller: %d\n" , rc); + goto err_host; -return rc; } +/* apply fixups for broken systems */ if (ahci_broken_system_poweroff (pdev)) { +dev_warn (dev,"System may need power cycle after shutdown\n" ); -dev_info (dev,"quirky BIOS, skipping spindown on poweroff\n" ); } +/* configure LPM policy */ for (i =0 ; i < n_ports; i++) {ahci_update_initial_lpm_policy (host->ports[i]); } +/* apply platform-specific workarounds */ if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { + rc =ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) { +dev_dbg (dev,"Intel PCS quirk failed (%d)\n" , rc); + goto err_host; + } -ahci_intel_pcs_quirk (pdev, hpriv); } +/* apply Apple MCP89 workaround */ if (is_mcp89_apple (pdev)) { + rc =ahci_mcp89_apple_enable (pdev); +if (rc) { +dev_err (dev,"Failed to enable MCP89 Apple: %d\n" , rc); + goto err_host; + } -ahci_mcp89_apple_enable (pdev); } +/* apply Acer SA5-271 workaround */ acer_sa5_271_workaround (hpriv, pdev); +/* initialize and enable interrupts */ ahci_init_irq (pdev, n_ports, hpriv);ahci_pci_enable_interrupts (host); +/* print information */ ahci_pci_print_info (host); +/* register with libata */ rc =ata_host_activate (host, hpriv->irq, ahci_interrupt,IRQF_SHARED , + &ahci_sht); - &ahci_sht);if (rc) { +dev_err (dev,"Failed to activate ATA host: %d\n" , rc); + goto err_host; -return rc; }return 0 ; +err_host: +ata_host_detach (host);// host is NULL-checked internally +err_out: +return rc; -return 0 ; }
Damien Le Moal
maintainerOn 5/22/25 12:00, Alexander Roman wrote:
Please send patches to the subsystem maintainers (myself and Niklas). Use scripts/get_maintainer.pl.
Quoted 203 lines
Problem:
The current implementation ofahci_init_onehas several issues with error handling and resource management. It lacks proper error cleanup paths, doesn't initialize pointers to NULL, and has inconsistent error handling patterns throughout the code. This can lead to resource leaks and make debugging initialization failures difficult.Solution:
This patch enhances the error handling and resource management inahci_init_oneby:
- Adding comprehensive error checking with descriptive error messages
- Improving error propagation through return codes
- Adding proper error cleanup paths for all resource allocations
- Initializing pointers to NULL to prevent use-after-free bugs
- Implementing proper cleanup of allocated resources in error paths
- Adding more descriptive error messages for all failure points
- Including error codes in log messages for better diagnostics
- Adding warning messages for potential system issues
- Improving code structure with proper error handling paths
- Adding proper error return labels
- Making code more maintainable with consistent error handling patternsTechnical Details:
- Added proper initialization of pointers (hpriv, host) to NULL
- Added error cleanup paths with proper resource release
- Improved error messages to include specific error codes
- Added proper error handling for all resource allocation failures
- Added proper cleanup of allocated resources in error paths
- Improved code organization with clear error handling paths
- Added proper error return labels for better code flowNote: Some error checks and logging have been simplified to reduce churn while maintaining robust error handling. The focus is on critical error paths and resource management rather than redundant checks. Log levels have been adjusted to use
dev_warnfor non-fatal warnings anddev_dbgfor quirk failures.Signed-off-by Alexander Roman <[email protected]>--- drivers/ata/ahci.c | 150 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 85 insertions(+), 65 deletions(-)drivers/ata/ahci.c +56
-17@@ -1611,460 +1611,555 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) {struct ahci_host_priv *hpriv =NULL ;struct ata_host *host =NULL ;void __iomem *mmio =NULL ; +int n_ports, i, rc = -ENOMEM ; -int n_ports, i, rc;u32 tmp, cap, port_map;u32 saved_cap;struct device *dev = &pdev->dev;VPRINTK ("ahci_init_one enter\n" ); +/* acquire resources with proper error handling */ -/* acquire resources */ rc =pcim_enable_device (pdev);if (rc) { +dev_err (dev,"Failed to enable PCI device: %d\n" , rc); + goto err_out; -return rc; } rc =pcim_iomap_regions (pdev,1 <<AHCI_PCI_BAR_STANDARD ,DRV_NAME );if (rc) { +dev_err (dev,"Failed to map PCI regions: %d\n" , rc); + goto err_out; -return rc; } mmio =pcim_iomap_table (pdev)[AHCI_PCI_BAR_STANDARD ]; rc =pci_alloc_irq_vectors (pdev,1 ,AHCI_MAX_PORTS ,PCI_IRQ_ALL_TYPES );if (rc <0 ) { +dev_err (dev,"Failed to allocate IRQ vectors: %d\n" , rc); + goto err_out; -return rc; } +/* allocate and initialize host private data */ hpriv =devm_kzalloc (dev,sizeof (*hpriv),GFP_KERNEL );if (!hpriv) { +dev_err (dev,"Failed to allocate host private data\n" ); + goto err_out; -return -ENOMEM ; } hpriv->mmio = mmio; hpriv->flags = (unsigned long )ent->driver_data; hpriv->irq = pdev->irq; +/* apply board quirks */ if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { + rc =ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) { +dev_dbg (dev,"Intel PCS quirk failed (%d)\n" , rc); + goto err_host; + } -ahci_intel_pcs_quirk (pdev, hpriv); } +/* apply port map mask if present */ ahci_get_port_map_mask (dev, hpriv); +/* save initial config */ rc =ahci_pci_save_initial_config (pdev, hpriv);if (rc) { +dev_err (dev,"Failed to save initial configuration: %d\n" , rc); + goto err_out; -return rc; } +/* prepare host */ cap = hpriv->cap; saved_cap = cap; port_map = hpriv->port_map; n_ports =ahci_calc_n_ports (cap, port_map); host =ata_host_alloc_pinfo (dev, ahci_port_info + ent->driver_data, n_ports);if (!host) { +dev_err (dev,"Failed to allocate ATA host\n" ); + goto err_out; -return -ENOMEM ; } host->private_data = hpriv; +/* configure DMA masks */ rc =ahci_configure_dma_masks (pdev, hpriv);if (rc) { +dev_err (dev,"Failed to configure DMA masks: %d\n" , rc); + goto err_host; -return rc; } +/* initialize adapter */ ahci_pci_init_controller (host); rc =ahci_reset_controller (host);if (rc) { +dev_err (dev,"Failed to reset controller: %d\n" , rc); + goto err_host; -return rc; } +/* apply fixups for broken systems */ if (ahci_broken_system_poweroff (pdev)) { +dev_warn (dev,"System may need power cycle after shutdown\n" ); -dev_info (dev,"quirky BIOS, skipping spindown on poweroff\n" ); } +/* configure LPM policy */ for (i =0 ; i < n_ports; i++) {ahci_update_initial_lpm_policy (host->ports[i]); } +/* apply platform-specific workarounds */ if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { + rc =ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) { +dev_dbg (dev,"Intel PCS quirk failed (%d)\n" , rc); + goto err_host; + } -ahci_intel_pcs_quirk (pdev, hpriv); } +/* apply Apple MCP89 workaround */ if (is_mcp89_apple (pdev)) { + rc =ahci_mcp89_apple_enable (pdev); +if (rc) { +dev_err (dev,"Failed to enable MCP89 Apple: %d\n" , rc); + goto err_host; + } -ahci_mcp89_apple_enable (pdev); } +/* apply Acer SA5-271 workaround */ acer_sa5_271_workaround (hpriv, pdev); +/* initialize and enable interrupts */ ahci_init_irq (pdev, n_ports, hpriv);ahci_pci_enable_interrupts (host); +/* print information */ ahci_pci_print_info (host); +/* register with libata */ rc =ata_host_activate (host, hpriv->irq, ahci_interrupt,IRQF_SHARED , + &ahci_sht); - &ahci_sht);if (rc) { +dev_err (dev,"Failed to activate ATA host: %d\n" , rc); + goto err_host; -return rc; }return 0 ; +err_host: +ata_host_detach (host);// host is NULL-checked internally +err_out: +return rc; -return 0 ; }
Signature · 2 lines
Damien Le Moal Western Digital Research
PATCH v3 1/1 ahci: enhance error handling in ahci_init_one
In reply to Damien Le MoalAdd comprehensive error handling to ahci_init_one() to:
1. Prevent resource leaks during initialization failures
2. Ensure proper cleanup of allocated resources
3. Provide detailed error reporting for debugging
4. Maintain consistent error handling patterns
Key changes:
- Initialize all pointers to NULL
- Add centralized error handling via goto labels
- Improve error messages with specific error codes
- Remove duplicate Intel PCS quirk call
- Adjust log levels (dev_err for fatal, dev_dbg for quirks)
--- drivers/ata/ahci.c | 98 ++++++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 43 deletions(-)
drivers/ata/ahci.c +43 -29
@@ -1611,7 +1611,7 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)struct ahci_host_priv *hpriv =NULL ;struct ata_host *host =NULL ;void __iomem *mmio =NULL ; -int n_ports, i, rc; +int n_ports, i, rc = -ENOMEM ;u32 tmp, cap, port_map;u32 saved_cap;struct device *dev = &pdev->dev;
@@ -1619,60 +1619,72 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)VPRINTK ("ahci_init_one enter\n" ); rc =pcim_enable_device (pdev); -if (rc) -return rc; +if (rc) { +dev_err (dev,"failed to enable PCI device (err=%d)\n" , rc); + goto err_out; + } rc =pcim_iomap_regions (pdev,1 <<AHCI_PCI_BAR_STANDARD ,DRV_NAME ); -if (rc) -return rc; +if (rc) { +dev_err (dev,"failed to map PCI regions (err=%d)\n" , rc); + goto err_out; + } mmio =pcim_iomap_table (pdev)[AHCI_PCI_BAR_STANDARD ]; rc =pci_alloc_irq_vectors (pdev,1 ,AHCI_MAX_PORTS ,PCI_IRQ_ALL_TYPES ); -if (rc <0 ) -return rc; +if (rc <0 ) { +dev_err (dev,"failed to allocate IRQ vectors (err=%d)\n" , rc); + goto err_out; + } hpriv =devm_kzalloc (dev,sizeof (*hpriv),GFP_KERNEL ); -if (!hpriv) -return -ENOMEM ; +if (!hpriv) { +dev_err (dev,"failed to allocate host private data\n" ); + goto err_out; + } hpriv->mmio = mmio; hpriv->flags = (unsigned long )ent->driver_data; hpriv->irq = pdev->irq;if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { -ahci_intel_pcs_quirk (pdev, hpriv); + rc =ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) +dev_dbg (dev,"Intel PCS quirk failed (err=%d)\n" , rc); }ahci_get_port_map_mask (dev, hpriv); rc =ahci_pci_save_initial_config (pdev, hpriv); -if (rc) -return rc; +if (rc) { +dev_err (dev,"failed to save initial config (err=%d)\n" , rc); + goto err_out; + } cap = hpriv->cap; saved_cap = cap; port_map = hpriv->port_map; n_ports =ahci_calc_n_ports (cap, port_map); host =ata_host_alloc_pinfo (dev, ahci_port_info + ent->driver_data, n_ports); -if (!host) -return -ENOMEM ; +if (!host) { +dev_err (dev,"failed to allocate ATA host\n" ); + goto err_out; + } host->private_data = hpriv; rc =ahci_configure_dma_masks (pdev, hpriv); -if (rc) -return rc; +if (rc) { +dev_err (dev,"failed to configure DMA masks (err=%d)\n" , rc); + goto err_host; + }ahci_pci_init_controller (host); rc =ahci_reset_controller (host); -if (rc) -return rc; - -if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { -ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) { +dev_err (dev,"failed to reset controller (err=%d)\n" , rc); + goto err_host; }if (ahci_broken_system_poweroff (pdev)) {
@@ -1685,20 +1697,20 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) }if (is_mcp89_apple (pdev)) { -ahci_mcp89_apple_enable (pdev); + rc =ahci_mcp89_apple_enable (pdev); +if (rc) +dev_warn (dev,"Apple MCP89 enable failed (err=%d)\n" , rc); } -acer_sa5_271_workaround (hpriv, pdev); -ahci_init_irq (pdev, n_ports, hpriv);ahci_pci_enable_interrupts (host);ahci_pci_print_info (host); rc =ata_host_activate (host, hpriv->irq, ahci_interrupt,IRQF_SHARED , - &ahci_sht); -if (rc) -return rc; - -return 0 ; + &ahci_sht); +if (rc) { +dev_err (dev,"failed to activate ATA host (err=%d)\n" , rc); + goto err_host; + } }
Damien Le Moal
maintainerRe: [PATCH V3] ahci: enhance error handling in ahci_init_one
Quoted 6 lines · Alexander Roman
Add comprehensive error handling to
ahci_init_one()to:
1. Prevent resource leaks during initialization failures
2. Ensure proper cleanup of allocated resources
3. Provide detailed error reporting for debugging
4. Maintain consistent error handling patterns
Quoted 8 lines
Key changes:
- Initialize all pointers to NULL
- Add centralized error handling via goto labels
- Improve error messages with specific error codes
- Remove duplicate Intel PCS quirk call
- Adjust log levels (dev_errfor fatal,dev_dbgfor quirks)Signed-off-by Alexander Roman <[email protected]>
I received 2 x v3 patches with different commit messages and titles, but these 2 patches touch the same code.. Very confusing...
Which one is the "correct" patch you want us to consider ?
And please send patches to *all* maintainers of the subsystem. You can check that with "scripts/get_maintainer.pl driver/ata" (you are missing Niklas).
Note: it is too late to apply this patch anyway. If accepted, it will go in during 6.16-rc1. So no rush to clean this up. Take your time and make a proper patch please.
Quoted 140 lines
--- drivers/ata/ahci.c | 98 ++++++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 43 deletions(-)drivers/ata/ahci.c +43
-29@@ -1611,7 +1611,7 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)struct ahci_host_priv *hpriv =NULL ;struct ata_host *host =NULL ;void __iomem *mmio =NULL ; -int n_ports, i, rc; +int n_ports, i, rc = -ENOMEM ;u32 tmp, cap, port_map;u32 saved_cap;struct device *dev = &pdev->dev;@@ -1619,60 +1619,72 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)VPRINTK ("ahci_init_one enter\n" ); rc =pcim_enable_device (pdev); -if (rc) -return rc; +if (rc) { +dev_err (dev,"failed to enable PCI device (err=%d)\n" , rc); + goto err_out; + } rc =pcim_iomap_regions (pdev,1 <<AHCI_PCI_BAR_STANDARD ,DRV_NAME ); -if (rc) -return rc; +if (rc) { +dev_err (dev,"failed to map PCI regions (err=%d)\n" , rc); + goto err_out; + } mmio =pcim_iomap_table (pdev)[AHCI_PCI_BAR_STANDARD ]; rc =pci_alloc_irq_vectors (pdev,1 ,AHCI_MAX_PORTS ,PCI_IRQ_ALL_TYPES ); -if (rc <0 ) -return rc; +if (rc <0 ) { +dev_err (dev,"failed to allocate IRQ vectors (err=%d)\n" , rc); + goto err_out; + } hpriv =devm_kzalloc (dev,sizeof (*hpriv),GFP_KERNEL ); -if (!hpriv) -return -ENOMEM ; +if (!hpriv) { +dev_err (dev,"failed to allocate host private data\n" ); + goto err_out; + } hpriv->mmio = mmio; hpriv->flags = (unsigned long )ent->driver_data; hpriv->irq = pdev->irq;if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { -ahci_intel_pcs_quirk (pdev, hpriv); + rc =ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) +dev_dbg (dev,"Intel PCS quirk failed (err=%d)\n" , rc); }ahci_get_port_map_mask (dev, hpriv); rc =ahci_pci_save_initial_config (pdev, hpriv); -if (rc) -return rc; +if (rc) { +dev_err (dev,"failed to save initial config (err=%d)\n" , rc); + goto err_out; + } cap = hpriv->cap; saved_cap = cap; port_map = hpriv->port_map; n_ports =ahci_calc_n_ports (cap, port_map); host =ata_host_alloc_pinfo (dev, ahci_port_info + ent->driver_data, n_ports); -if (!host) -return -ENOMEM ; +if (!host) { +dev_err (dev,"failed to allocate ATA host\n" ); + goto err_out; + } host->private_data = hpriv; rc =ahci_configure_dma_masks (pdev, hpriv); -if (rc) -return rc; +if (rc) { +dev_err (dev,"failed to configure DMA masks (err=%d)\n" , rc); + goto err_host; + }ahci_pci_init_controller (host); rc =ahci_reset_controller (host); -if (rc) -return rc; - -if (pdev->vendor ==PCI_VENDOR_ID_INTEL ) { -ahci_intel_pcs_quirk (pdev, hpriv); +if (rc) { +dev_err (dev,"failed to reset controller (err=%d)\n" , rc); + goto err_host; }if (ahci_broken_system_poweroff (pdev)) {@@ -1685,20 +1697,20 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) }if (is_mcp89_apple (pdev)) { -ahci_mcp89_apple_enable (pdev); + rc =ahci_mcp89_apple_enable (pdev); +if (rc) +dev_warn (dev,"Apple MCP89 enable failed (err=%d)\n" , rc); } -acer_sa5_271_workaround (hpriv, pdev); -ahci_init_irq (pdev, n_ports, hpriv);ahci_pci_enable_interrupts (host);ahci_pci_print_info (host); rc =ata_host_activate (host, hpriv->irq, ahci_interrupt,IRQF_SHARED , - &ahci_sht); -if (rc) -return rc; - -return 0 ; + &ahci_sht); +if (rc) { +dev_err (dev,"failed to activate ATA host (err=%d)\n" , rc); + goto err_host; + } }
Signature · 2 lines
Damien Le Moal Western Digital Research
Niklas Cassel
maintainerRe: [PATCH V3] ahci: enhance error handling in ahci_init_one
Quoted 33 lines · Damien Le Moal
Quoted 6 lines · Alexander Roman
Add comprehensive error handling to
ahci_init_one()to:
1. Prevent resource leaks during initialization failures
2. Ensure proper cleanup of allocated resources
3. Provide detailed error reporting for debugging
4. Maintain consistent error handling patternsQuoted 8 lines
Key changes:
- Initialize all pointers to NULL
- Add centralized error handling via goto labels
- Improve error messages with specific error codes
- Remove duplicate Intel PCS quirk call
- Adjust log levels (dev_errfor fatal,dev_dbgfor quirks)Signed-off-by Alexander Roman <[email protected]>I received 2 x v3 patches with different commit messages and titles, but these 2 patches touch the same code.. Very confusing...
Which one is the "correct" patch you want us to consider ?And please send patches to *all* maintainers of the subsystem. You can check that with "
scripts/get_maintainer.pldriver/ata" (you are missing Niklas).Note: it is too late to apply this patch anyway. If accepted, it will go in during 6.16-rc1. So no rush to clean this up. Take your time and make a proper patch please.
Quoted 4 lines
--- drivers/ata/ahci.c | 98 ++++++++++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 43 deletions(-)
Quoted 15 lines
Quoted 14 lines
drivers/ata/ahci.c +1
-1@@ -1611,7 +1611,7 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)struct ahci_host_priv *hpriv =NULL ;struct ata_host *host =NULL ;void __iomem *mmio =NULL ; -int n_ports, i, rc; +int n_ports, i, rc = -ENOMEM ;u32 tmp, cap, port_map;u32 saved_cap;struct device *dev = &pdev->dev;drivers/ata/ahci.c static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) · line 1619
Quoted 1 line
drivers/ata/ahci.c +0
-0@@ -1619,60 +1619,72 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)VPRINTK ("ahci_init_one enter\n" );
There is no VPRINTK() here since a long time ago.
So this must be based on some ancient kernel version.
Please base your patches on:
https://git.kernel.org/pub/scm/linux/kernel/git/libata/linux.git/log/?h=for-next
Kind regards,
Niklas
Hello Alexander,
Niklas Cassel
maintainerRe: [PATCH V3] ahci: enhance error handling in ahci_init_one
Hello Alexander,
Quoted 9 lines · Niklas Cassel
Quoted 2 lines
drivers/ata/ahci.c static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent) · line 1619
Quoted 2 lines
drivers/ata/ahci.c +0
-0@@ -1619,60 +1619,72 @@ static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)VPRINTK ("ahci_init_one enter\n" );There is no
VPRINTK()here since a long time ago.So this must be based on some ancient kernel version.
Please base your patches on:
https://git.kernel.org/pub/scm/linux/kernel/git/libata/linux.git/log/?h=for-next
Sorry, but I forgot to mention, please don't use the In-Reply-To: header to reference older versions of your patch.
To quote Submitting Patches, it creates an 'unmanageable forest':
https://docs.kernel.org/process/submitting-patches.html#explicit-in-reply-to-headers
Kind regards,
Niklas