Skip to contentSkip to conversation

scsi: libsas: Grab the host lock in sas_ata_device_link_abort()

PATCH In review Xingui Yang 5 participants 10 messages Dec 20, 2022 – Dec 21, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: libsas: Grab the host lock in sas_ata_device_link_abort()

Grab the host lock in sas_ata_device_link_abort() before calling ata_link_abort(), as the comment in ata_link_abort() mentions.

---
 drivers/scsi/libsas/sas_ata.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/scsi/libsas/sas_ata.c +3 -0
index f7439bf9cdc6..4f2017b21e6d 100644
@@ -889,7 +889,9 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
 {
 	struct ata_port *ap = device->sata_dev.ap;
 	struct ata_link *link = &ap->link;
+	unsigned long flags;
 
+	spin_lock_irqsave(ap->lock, flags);
 	device->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */
 	device->sata_dev.fis[3] = ATA_ABORTED; /* tf error */
 
@@ -897,6 +899,7 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
 	if (force_reset)
 		link->eh_info.action |= ATA_EH_RESET;
 	ata_link_abort(link);
+	spin_unlock_irqrestore(ap->lock, flags);
 }
 EXPORT_SYMBOL_GPL(sas_ata_device_link_abort);
 
Signature · 1 line
2.17.1
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 1 line · Xingui Yang

Grab the host lock in sas_ata_device_link_abort() before calling

This is should be the ata port lock, right? I know that the ata comments say differently.

Quoted 2 lines

ata_link_abort(), as the comment in ata_link_abort() mentions.

Can you please add a fixes tag?

Quoted 1 line
Quoted 26 lines
---
  drivers/scsi/libsas/sas_ata.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/scsi/libsas/sas_ata.c +3 -0
index f7439bf9cdc6..4f2017b21e6d 100644
@@ -889,7 +889,9 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
  {
  	struct ata_port *ap = device->sata_dev.ap;
  	struct ata_link *link = &ap->link;
+	unsigned long flags;
 
+	spin_lock_irqsave(ap->lock, flags);
  	device->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */
  	device->sata_dev.fis[3] = ATA_ABORTED; /* tf error */
 
@@ -897,6 +899,7 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
  	if (force_reset)
  		link->eh_info.action |= ATA_EH_RESET;
  	ata_link_abort(link);
+	spin_unlock_irqrestore(ap->lock, flags);
  }
  EXPORT_SYMBOL_GPL(sas_ata_device_link_abort);
 
#2 lore raw
Quoted 29 lines · John Garry
Quoted 1 line · Xingui Yang

Grab the host lock in sas_ata_device_link_abort() before calling

This is should be the ata port lock, right? I know that the ata comments say differently.

Quoted 2 lines

ata_link_abort(), as the comment in ata_link_abort() mentions.

Can you please add a fixes tag?

Quoted 1 line
Quoted 14 lines
---
  drivers/scsi/libsas/sas_ata.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/scsi/libsas/sas_ata.c +1 -0
index f7439bf9cdc6..4f2017b21e6d 100644
@@ -889,7 +889,9 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
  {
  	struct ata_port *ap = device->sata_dev.ap;
  	struct ata_link *link = &ap->link;
+	unsigned long flags;
 
Quoted 8 lines

drivers/scsi/libsas/sas_ata.c void sas_ata_device_link_abort(struct domain_device *device, bool force_reset) · line 901

Quoted 8 lines
drivers/scsi/libsas/sas_ata.c +1 -0
+	spin_lock_irqsave(ap->lock, flags);
  	device->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */
  	device->sata_dev.fis[3] = ATA_ABORTED; /* tf error */
 
@@ -897,6 +899,7 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
  	if (force_reset)
  		link->eh_info.action |= ATA_EH_RESET;
  	ata_link_abort(link);

Really need to add lockdep annotations in libata to avoid/catch such bugs... Will work on that.

Quoted 5 lines
Quoted 4 lines
drivers/scsi/libsas/sas_ata.c +1 -0
+	spin_unlock_irqrestore(ap->lock, flags);
  }
  EXPORT_SYMBOL_GPL(sas_ata_device_link_abort);
 
Signature · 2 lines
Damien Le Moal
Western Digital Research
reviewer
#3 lore raw
Quoted 34 lines · Damien Le Moal
Quoted 33 lines · John Garry
Quoted 1 line · Xingui Yang

Grab the host lock in sas_ata_device_link_abort() before calling

This is should be the ata port lock, right? I know that the ata comments say differently.

Quoted 2 lines

ata_link_abort(), as the comment in ata_link_abort() mentions.

Can you please add a fixes tag?

Quoted 1 line
Quoted 18 lines
---
   drivers/scsi/libsas/sas_ata.c | 3 +++
   1 file changed, 3 insertions(+)
drivers/scsi/libsas/sas_ata.c +2 -0
index f7439bf9cdc6..4f2017b21e6d 100644
@@ -889,7 +889,9 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
   {
   	struct ata_port *ap = device->sata_dev.ap;
   	struct ata_link *link = &ap->link;
+	unsigned long flags;
 
+	spin_lock_irqsave(ap->lock, flags);
   	device->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */
   	device->sata_dev.fis[3] = ATA_ABORTED; /* tf error */
 
Quoted 7 lines
Quoted 4 lines

drivers/scsi/libsas/sas_ata.c void sas_ata_device_link_abort(struct domain_device *device, bool force_reset) · line 901

Quoted 4 lines
drivers/scsi/libsas/sas_ata.c +0 -0
@@ -897,6 +899,7 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
   	if (force_reset)
   		link->eh_info.action |= ATA_EH_RESET;
   	ata_link_abort(link);

Really need to add lockdep annotations in libata to avoid/catch such bugs... Will work on that.

Actually in libata there are many places calling ata_link_abort() not holding port lock. And some places are holding the real host lock(ata_host->lock) while calling ata_link_abort(). So if you add the lockdep annotations, there may be too many warnings. If these are real issues, we should fix them first.

Thanks,
Jason

#4 lore raw
Quoted 43 lines · Jason Yan
Quoted 34 lines · Damien Le Moal
Quoted 33 lines · John Garry
Quoted 1 line · Xingui Yang

Grab the host lock in sas_ata_device_link_abort() before calling

This is should be the ata port lock, right? I know that the ata comments say differently.

Quoted 2 lines

ata_link_abort(), as the comment in ata_link_abort() mentions.

Can you please add a fixes tag?

Quoted 1 line
Quoted 18 lines
---
   drivers/scsi/libsas/sas_ata.c | 3 +++
   1 file changed, 3 insertions(+)
drivers/scsi/libsas/sas_ata.c +2 -0
index f7439bf9cdc6..4f2017b21e6d 100644
@@ -889,7 +889,9 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
   {
   	struct ata_port *ap = device->sata_dev.ap;
   	struct ata_link *link = &ap->link;
+	unsigned long flags;
 
+	spin_lock_irqsave(ap->lock, flags);
   	device->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */
   	device->sata_dev.fis[3] = ATA_ABORTED; /* tf error */
 
Quoted 7 lines
Quoted 4 lines

drivers/scsi/libsas/sas_ata.c void sas_ata_device_link_abort(struct domain_device *device, bool force_reset) · line 901

Quoted 4 lines
drivers/scsi/libsas/sas_ata.c +0 -0
@@ -897,6 +899,7 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
   	if (force_reset)
   		link->eh_info.action |= ATA_EH_RESET;
   	ata_link_abort(link);

Really need to add lockdep annotations in libata to avoid/catch such bugs... Will work on that.

Quoted 5 lines

Actually in libata there are many places calling ata_link_abort() not holding port lock. And some places are holding the real host lock(ata_host->lock) while calling ata_link_abort(). So if you add the lockdep annotations, there may be too many warnings. If these are real issues, we should fix them first.

libata-EH does most of its work without the port lock held because by the time we get EH started, we are guaranteed to be idle with no commands in flight. That is why the calls you mention look like "bugs" but are not.

lockdep annotation will be a little tricky, but not too hard to do either.

Quoted 3 lines

Thanks,
Jason

Signature · 2 lines
Damien Le Moal
Western Digital Research
reviewer
#5 lore raw
Quoted 50 lines · Damien Le Moal
Quoted 43 lines · Jason Yan
Quoted 34 lines · Damien Le Moal
Quoted 33 lines · John Garry
Quoted 1 line · Xingui Yang

Grab the host lock in sas_ata_device_link_abort() before calling

This is should be the ata port lock, right? I know that the ata comments say differently.

Quoted 2 lines

ata_link_abort(), as the comment in ata_link_abort() mentions.

Can you please add a fixes tag?

Quoted 1 line
Quoted 18 lines
---
    drivers/scsi/libsas/sas_ata.c | 3 +++
    1 file changed, 3 insertions(+)
drivers/scsi/libsas/sas_ata.c +2 -0
index f7439bf9cdc6..4f2017b21e6d 100644
@@ -889,7 +889,9 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
    {
    	struct ata_port *ap = device->sata_dev.ap;
    	struct ata_link *link = &ap->link;
+	unsigned long flags;
 
+	spin_lock_irqsave(ap->lock, flags);
    	device->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */
    	device->sata_dev.fis[3] = ATA_ABORTED; /* tf error */
 
Quoted 7 lines
Quoted 4 lines

drivers/scsi/libsas/sas_ata.c void sas_ata_device_link_abort(struct domain_device *device, bool force_reset) · line 901

Quoted 4 lines
drivers/scsi/libsas/sas_ata.c +0 -0
@@ -897,6 +899,7 @@ void sas_ata_device_link_abort(struct domain_device *device, bool force_reset)
    	if (force_reset)
    		link->eh_info.action |= ATA_EH_RESET;
    	ata_link_abort(link);

Really need to add lockdep annotations in libata to avoid/catch such bugs... Will work on that.

Quoted 5 lines

Actually in libata there are many places calling ata_link_abort() not holding port lock. And some places are holding the real host lock(ata_host->lock) while calling ata_link_abort(). So if you add the lockdep annotations, there may be too many warnings. If these are real issues, we should fix them first.

Quoted 3 lines

libata-EH does most of its work without the port lock held because by the time we get EH started, we are guaranteed to be idle with no commands in flight. That is why the calls you mention look like "bugs" but are not.

What about the interrupt handler such as ahci_error_intr()? I didn't see the callers hold the port lock too. Do they need the port lock?

#6 lore raw
Quoted 54 lines · Jason Yan
Quoted 53 lines · Damien Le Moal
Quoted 51 lines · Jason Yan
Quoted 36 lines · Damien Le Moal
Quoted 35 lines · John Garry
Quoted 1 line · Xingui Yang

Grab the host lock in sas_ata_device_link_abort() before calling

This is should be the ata port lock, right? I know that the ata comments
say differently.

Quoted 2 lines

ata_link_abort(), as the comment in ata_link_abort() mentions.

Can you please add a fixes tag?

Quoted 1 line
Quoted 19 lines

---
    drivers/scsi/libsas/sas_ata.c | 3 +++

    1 file changed, 3 insertions(+)
drivers/scsi/libsas/sas_ata.c +1 -0
index f7439bf9cdc6..4f2017b21e6d 100644
@@ -889,7 +889,9 @@ void sas_ata_device_link_abort(struct
 domain_device *device, bool force_reset)
     {
         struct ata_port *ap = device->sata_dev.ap;
         struct ata_link *link = &ap->link;
+    unsigned long flags;
     +    spin_lock_irqsave(ap->lock, flags);

        device->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */ device->sata_dev.fis[3] = ATA_ABORTED; /* tf error */ @@ -897,6 +899,7 @@ void sas_ata_device_link_abort(struct

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

domain_device *device, bool force_reset)
        if (force_reset)
            link->eh_info.action |= ATA_EH_RESET;
        ata_link_abort(link);

Really need to add lockdep annotations in libata to avoid/catch such bugs...
Will work on that.

Actually in libata there are many places calling ata_link_abort() not holding port lock. And some places are holding the real host lock(ata_host->lock) while calling ata_link_abort(). So if you add the lockdep annotations, there may be too many warnings. If these are real issues, we should fix them first.

Quoted 8 lines
Quoted 5 lines

libata-EH does most of its work without the port lock held because by the time
we get EH started, we are guaranteed to be idle with no commands in flight. That
is why the calls you mention look like "bugs" but are not.

What about the interrupt handler such as ahci_error_intr()? I didn't see the callers hold the port lock too. Do they need the port lock?

It looks like it is missing for ahci_thunderx_irq_handler() but that one takes the host lock. Same for xgene_ahci_irq_intr(), again no port lock but host lock taken. And again for ahci_single_level_irq_intr() for the non MSI case. For modern MSI adapters, the port lock is taken in

For other cases, ahci_multi_irqs_intr_hard) takes the port lock.

So it looks like ahci_port_intr() needs to take the lock and some cleanups overall (the host lock should not be necessary in the command path. But nobody seems to have issues with the "bad" cases... Probably because they are not mainstream adapters.

Definitely some work needed here.

Signature · 2 lines
Damien Le Moal
Western Digital Research
#7 lore raw
Quoted 9 lines · Damien Le Moal
Quoted 3 lines

What about the interrupt handler such as ahci_error_intr()? I didn't see the callers hold the port lock too. Do they need the port lock?

It looks like it is missing for ahci_thunderx_irq_handler() but that one takes the host lock. Same for xgene_ahci_irq_intr(), again no port lock but host lock taken. And again for ahci_single_level_irq_intr() for the non MSI case. For modern MSI adapters, the port lock is taken in

Quoted 8 lines

For other cases, ahci_multi_irqs_intr_hard) takes the port lock.

So it looks like ahci_port_intr() needs to take the lock and some cleanups overall (the host lock should not be necessary in the command path. But nobody seems to have issues with the "bad" cases... Probably because they are not mainstream adapters.

Definitely some work needed here.

ahci_multi_irqs_intr_hard() takes the ap->lock before calling ahci_handle_port_interrupt(), which calls ahci_port_intr(), so I don't think there is any work needed for multi IRQ AHCI.

For ahci_single_level_irq_intr() the host lock is taken before calling ahci_handle_port_intr(), so I don't see why we need any extra work for single IRQ AHCI.

Remember, while the default is that:

	ap->lock = &host->lock;

see:
https://github.com/torvalds/linux/blob/v6.1/drivers/ata/libata-core.c#L5305

In case of MULTI MSI, the ap->lock is using its own lock:
https://github.com/torvalds/linux/blob/v6.1/drivers/ata/libahci.c#L2460

So what is it that needs to be fixed for AHCI?

I haven't looked at ahci_thunderx_irq_handler() and xgene_ahci_irq_intr() so I can't speak for these.

Kind regards,
Niklas

#8 lore raw
Quoted 17 lines · Niklas Cassel
Quoted 16 lines · Damien Le Moal
Quoted 3 lines

What about the interrupt handler such as ahci_error_intr()? I didn't see the callers hold the port lock too. Do they need the port lock?

It looks like it is missing for ahci_thunderx_irq_handler() but that one takes the host lock. Same for xgene_ahci_irq_intr(), again no port lock but host lock taken. And again for ahci_single_level_irq_intr() for the non MSI case. For modern MSI adapters, the port lock is taken in

For other cases, ahci_multi_irqs_intr_hard) takes the port lock.

So it looks like ahci_port_intr() needs to take the lock and some cleanups overall (the host lock should not be necessary in the command path. But nobody seems to have issues with the "bad" cases... Probably because they are not mainstream adapters.

Quoted 5 lines
Quoted 1 line

Definitely some work needed here.

ahci_multi_irqs_intr_hard() takes the ap->lock before calling ahci_handle_port_interrupt(), which calls ahci_port_intr(), so I don't think there is any work needed for multi IRQ AHCI.

Yes. I tried to say that above.

Quoted 8 lines

For ahci_single_level_irq_intr() the host lock is taken before calling ahci_handle_port_intr(), so I don't see why we need any extra work for single IRQ AHCI.

Remember, while the default is that:

	ap->lock = &host->lock;

Ah ! Yes ! good point ! So there are no issues anywhere. This is only confusing because of the comments I think.

Quoted 11 lines

see:
https://github.com/torvalds/linux/blob/v6.1/drivers/ata/libata-core.c#L5305

In case of MULTI MSI, the ap->lock is using its own lock:
https://github.com/torvalds/linux/blob/v6.1/drivers/ata/libahci.c#L2460

So what is it that needs to be fixed for AHCI?

I haven't looked at ahci_thunderx_irq_handler() and xgene_ahci_irq_intr() so I can't speak for these.

These are not multi-irq and use the &host->lock directly, which is the same as taking the ap->lock. We could clean that up for consistency and always use ap->lock. But not a big deal. No bugs, just a little confusing with a simple reading of the code.

Quoted 4 lines

Kind regards,
Niklas

Signature · 2 lines
Damien Le Moal
Western Digital Research
author
#9 lore raw
In reply to John Garry
Quoted 4 lines · John Garry
Quoted 1 line · Xingui Yang

Grab the host lock in sas_ata_device_link_abort() before calling

This is should be the ata port lock, right? I know that the ata comments say differently.

ok, I will update the commit message and use ata port lock instead.

Quoted 5 lines
Quoted 3 lines

ata_link_abort(), as the comment in ata_link_abort() mentions.

Can you please add a fixes tag?

ok, I will update and add a fix tag.

Thanks,
Xingui

Quoted 32 lines
Quoted 2 lines
Quoted 18 lines

---
  drivers/scsi/libsas/sas_ata.c | 3 +++

  1 file changed, 3 insertions(+)
drivers/scsi/libsas/sas_ata.c +2 -0
index f7439bf9cdc6..4f2017b21e6d 100644
@@ -889,7 +889,9 @@ void sas_ata_device_link_abort(struct 
 domain_device *device, bool force_reset)
   {
       struct ata_port *ap = device->sata_dev.ap;
       struct ata_link *link = &ap->link;
+    unsigned long flags;
+    spin_lock_irqsave(ap->lock, flags);

      device->sata_dev.fis[2] = ATA_ERR | ATA_DRDY; /* tf status */ device->sata_dev.fis[3] = ATA_ABORTED; /* tf error */

drivers/scsi/libsas/sas_ata.c void sas_ata_device_link_abort(struct · line 905

Quoted 7 lines
drivers/scsi/libsas/sas_ata.c +1 -0
@@ -897,6 +899,7 @@ void sas_ata_device_link_abort(struct 
 domain_device *device, bool force_reset)
       if (force_reset)
           link->eh_info.action |= ATA_EH_RESET;
       ata_link_abort(link);
+    spin_unlock_irqrestore(ap->lock, flags);
   }
   EXPORT_SYMBOL_GPL(sas_ata_device_link_abort);

.