Linux SCSI subsystem development
 help / color / mirror / Atom feed
From: Maram Srimannarayana Murthy <msmurthy@linux.ibm.com>
To: Kyle Mahlkuch <kmahlkuc@linux.ibm.com>,
	linux-scsi@vger.kernel.org, linux-kernel@vger.kernel.org,
	paul.ely@broadcom.com
Cc: thinhtr@linux.ibm.com
Subject: Re: [PATCH 2/3] scsi: fc_transport: Fix TOCTOU races and workqueue
Date: Tue, 1 Sep 2026 16:13:47 +0530	[thread overview]
Message-ID: <cf63416f-c342-4698-bf66-5008aa01c91b@linux.ibm.com> (raw)
In-Reply-To: <f4f4e9a7-e3bd-4cc5-a3df-829b981ae836@linux.ibm.com>


On 09/04/26 8:42 pm, Kyle Mahlkuch wrote:
> Fix the TOCTOU races in workqueue access, use READ_ONCE() in
> fc_queue_work(), fc_flush_work(), fc_queue_devloss_work(), and
> fc_flush_devloss().
>
> The workqueue destruction in fc_remove_host() uses WRITE_ONCE() to set
> the pointer to NULL to prevents new work, flushing the work queued
> before NULL, then safely destroying it.
>
> Signed-off-by: Thinh Tran <thinhtr@linux.ibm.com>
> Signed-off-by: Kyle Mahlkuch <kmahlkuc@linux.ibm.com>
> ---


Tested-by: Maram Srimannarayana Murthy <msmurthy@linux.ibm.com>

Tested the complete 3-patch series on an IBM Power11 (ppc64le) server 
equipped with an Emulex FC HBA.
The patches applied cleanly, and FC driver parameter validation testing 
was executed continuously for 36 hours.
No crashes, hangs, or functional issues were observed during the test 
period.

Thanks,
Maram Srimannarayana Murthy

>  drivers/scsi/scsi_transport_fc.c | 36 ++++++++++++++++++++++----------
>  1 file changed, 25 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/scsi/scsi_transport_fc.c 
> b/drivers/scsi/scsi_transport_fc.c
> index 3a821afee9bc..123b22b52640 100644
> --- a/drivers/scsi/scsi_transport_fc.c
> +++ b/drivers/scsi/scsi_transport_fc.c
> @@ -2774,16 +2774,18 @@ EXPORT_SYMBOL(fc_release_transport);
>  static int
>  fc_queue_work(struct Scsi_Host *shost, struct work_struct *work)
>  {
> -    if (unlikely(!fc_host_work_q(shost))) {
> +    struct workqueue_struct *wq = READ_ONCE(fc_host_work_q(shost));
> +
> +    if (unlikely(!wq)) {
>          printk(KERN_ERR
>              "ERROR: FC host '%s' attempted to queue work, "
>              "when no workqueue created.\n", shost->hostt->name);
>          dump_stack();
> -
>          return -EINVAL;
>      }
>
> -    return queue_work(fc_host_work_q(shost), work);
> +    /* Use local copy to prevent TOCTOU race */
> +    return queue_work(wq, work);
>  }
>
>  /**
> @@ -2793,7 +2795,9 @@ fc_queue_work(struct Scsi_Host *shost, struct 
> work_struct *work)
>  static void
>  fc_flush_work(struct Scsi_Host *shost)
>  {
> -    if (!fc_host_work_q(shost)) {
> +    struct workqueue_struct *wq = READ_ONCE(fc_host_work_q(shost));
> +
> +    if (!wq) {
>          printk(KERN_ERR
>              "ERROR: FC host '%s' attempted to flush work, "
>              "when no workqueue created.\n", shost->hostt->name);
> @@ -2801,7 +2805,8 @@ fc_flush_work(struct Scsi_Host *shost)
>          return;
>      }
>
> -    flush_workqueue(fc_host_work_q(shost));
> +    /* Use local copy to prevent TOCTOU race */
> +    flush_workqueue(wq);
>  }
>
>  /**
> @@ -2818,16 +2823,18 @@ static int
>  fc_queue_devloss_work(struct Scsi_Host *shost, struct fc_rport *rport,
>                struct delayed_work *work, unsigned long delay)
>  {
> -    if (unlikely(!rport->devloss_work_q)) {
> +    struct workqueue_struct *wq = READ_ONCE(rport->devloss_work_q);
> +
> +    if (unlikely(!wq)) {
>          printk(KERN_ERR
>              "ERROR: FC host '%s' attempted to queue work, "
>              "when no workqueue created.\n", shost->hostt->name);
>          dump_stack();
> -
>          return -EINVAL;
>      }
>
> -    return queue_delayed_work(rport->devloss_work_q, work, delay);
> +    /* Use local copy to prevent TOCTOU race */
> +    return queue_delayed_work(wq, work, delay);
>  }
>
>  /**
> @@ -2838,7 +2845,9 @@ fc_queue_devloss_work(struct Scsi_Host *shost, 
> struct fc_rport *rport,
>  static void
>  fc_flush_devloss(struct Scsi_Host *shost, struct fc_rport *rport)
>  {
> -    if (unlikely(!rport->devloss_work_q)) {
> +    struct workqueue_struct *wq = READ_ONCE(rport->devloss_work_q);
> +
> +    if (unlikely(!wq)) {
>          printk(KERN_ERR
>              "ERROR: FC host '%s' attempted to flush work, "
>              "when no workqueue created.\n", shost->hostt->name);
> @@ -2846,7 +2855,7 @@ fc_flush_devloss(struct Scsi_Host *shost, struct 
> fc_rport *rport)
>          return;
>      }
>
> -    flush_workqueue(rport->devloss_work_q);
> +    flush_workqueue(wq);
>  }
>
>
> @@ -2905,7 +2914,12 @@ fc_remove_host(struct Scsi_Host *shost)
>      /* flush all stgt delete, and rport delete work items, then kill 
> it  */
>      if (fc_host->work_q) {
>          work_q = fc_host->work_q;
> -        fc_host->work_q = NULL;
> +        /* Prevent new work from being queued by setting work_q to 
> NULL */
> +        WRITE_ONCE(fc_host->work_q, NULL);
> +        /* Ensures NULL is visible to other CPUs before flush */
> +        smp_mb();
> +        /* Flush any work that was queued before NULL assignment */
> +        flush_workqueue(work_q);
>          destroy_workqueue(work_q);
>      }
>  }

      parent reply	other threads:[~2026-09-01 10:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-09 15:12 [PATCH 2/3] scsi: fc_transport: Fix TOCTOU races and workqueue Kyle Mahlkuch
2026-07-06 12:43 ` Hannes Reinecke
2026-09-01 10:43 ` Maram Srimannarayana Murthy [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=cf63416f-c342-4698-bf66-5008aa01c91b@linux.ibm.com \
    --to=msmurthy@linux.ibm.com \
    --cc=kmahlkuc@linux.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=paul.ely@broadcom.com \
    --cc=thinhtr@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox