diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index efb5195da60c56161ae88aa025c3015d2f3cf335..bdccf4ea251ae036b004b73cb6918843f7692afa 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -5517,6 +5517,9 @@ static void ata_host_release(struct kref *kref)
 	for (i = 0; i < host->n_ports; i++) {
 		struct ata_port *ap = host->ports[i];
 
+		if (!ap)
+			continue;
+
 		kfree(ap->pmp_link);
 		kfree(ap->slave_link);
 		kfree(ap->ncq_sense_buf);