linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: "Martin K . Petersen" <martin.petersen@oracle.com>
Cc: linux-scsi@vger.kernel.org, Daniel Lee <chullee@google.com>,
	"James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>,
	Peter Wang <peter.wang@mediatek.com>,
	Avri Altman <avri.altman@sandisk.com>,
	Bean Huo <beanhuo@micron.com>,
	"Bao D. Nguyen" <quic_nguyenb@quicinc.com>,
	Adrian Hunter <adrian.hunter@intel.com>
Subject: Re: [PATCH 1/8] ufs: core: Fix a race condition related to the "hid" attribute group
Date: Wed, 15 Oct 2025 15:24:45 -0700	[thread overview]
Message-ID: <baaf9d5b-06e7-4683-a88c-c9f303f5270e@acm.org> (raw)
In-Reply-To: <20251014200118.3390839-2-bvanassche@acm.org>

On 10/14/25 1:00 PM, Bart Van Assche wrote:
> ufs_sysfs_add_nodes() is called concurrently with ufs_get_device_desc().
> This may cause the following code to be called before
> ufs_sysfs_add_nodes():
(replying to my own email)

I'm considering to replace this patch with the patch below:

diff --git a/drivers/ufs/core/ufs-sysfs.c b/drivers/ufs/core/ufs-sysfs.c
index 875aab4a9cce..05f700f9cfa7 100644
--- a/drivers/ufs/core/ufs-sysfs.c
+++ b/drivers/ufs/core/ufs-sysfs.c
@@ -2086,18 +2086,23 @@ const struct attribute_group 
ufs_sysfs_lun_attributes_group = {
  	.attrs = ufs_sysfs_lun_attributes,
  };

-void ufs_sysfs_add_nodes(struct device *dev)
+void ufs_sysfs_add_nodes(struct ufs_hba *hba)
  {
+	struct device *dev = hba->dev;
  	int ret;

  	ret = sysfs_create_groups(&dev->kobj, ufs_sysfs_groups);
-	if (ret)
+	if (ret) {
  		dev_err(dev,
  			"%s: sysfs groups creation failed (err = %d)\n",
  			__func__, ret);
+	} else {
+		WRITE_ONCE(hba->sysfs_attrs_added, true);
+	}
  }

-void ufs_sysfs_remove_nodes(struct device *dev)
+void ufs_sysfs_remove_nodes(struct ufs_hba *hba)
  {
-	sysfs_remove_groups(&dev->kobj, ufs_sysfs_groups);
+	WRITE_ONCE(hba->sysfs_attrs_added, false);
+	sysfs_remove_groups(&hba->dev->kobj, ufs_sysfs_groups);
  }
diff --git a/drivers/ufs/core/ufs-sysfs.h b/drivers/ufs/core/ufs-sysfs.h
index 6efb82a082fd..c9e3751c6793 100644
--- a/drivers/ufs/core/ufs-sysfs.h
+++ b/drivers/ufs/core/ufs-sysfs.h
@@ -8,9 +8,10 @@
  #include <linux/sysfs.h>

  struct device;
+struct ufs_hba;

-void ufs_sysfs_add_nodes(struct device *dev);
-void ufs_sysfs_remove_nodes(struct device *dev);
+void ufs_sysfs_add_nodes(struct ufs_hba *hba);
+void ufs_sysfs_remove_nodes(struct ufs_hba *hba);

  extern const struct attribute_group ufs_sysfs_unit_descriptor_group;
  extern const struct attribute_group ufs_sysfs_lun_attributes_group;
diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 0013d21e2169..2887c5623992 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -8604,7 +8604,8 @@ static int ufs_get_device_desc(struct ufs_hba *hba)
  				DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP) &
  				UFS_DEV_HID_SUPPORT;

-	sysfs_update_group(&hba->dev->kobj, &ufs_sysfs_hid_group);
+	if (READ_ONCE(hba->sysfs_attrs_added))
+		sysfs_update_group(&hba->dev->kobj, &ufs_sysfs_hid_group);

  	model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];

@@ -10539,7 +10540,7 @@ void ufshcd_remove(struct ufs_hba *hba)
  		ufshcd_rpm_get_sync(hba);
  	ufs_hwmon_remove(hba);
  	ufs_bsg_remove(hba);
-	ufs_sysfs_remove_nodes(hba->dev);
+	ufs_sysfs_remove_nodes(hba);
  	cancel_delayed_work_sync(&hba->ufs_rtc_update_work);
  	blk_mq_destroy_queue(hba->tmf_queue);
  	blk_put_queue(hba->tmf_queue);
@@ -11003,8 +11004,8 @@ int ufshcd_init(struct ufs_hba *hba, void 
__iomem *mmio_base, unsigned int irq)
  	if (err)
  		goto out_disable;

+	ufs_sysfs_add_nodes(hba);
  	async_schedule(ufshcd_async_scan, hba);
-	ufs_sysfs_add_nodes(dev);
  	trace_android_vh_ufs_update_sysfs(hba);

  	device_enable_async_suspend(dev);
diff --git a/include/ufs/ufshcd.h b/include/ufs/ufshcd.h
index fba73d28ad87..9d7a88e73ba5 100644
--- a/include/ufs/ufshcd.h
+++ b/include/ufs/ufshcd.h
@@ -905,6 +905,8 @@ enum ufshcd_mcq_opr {
   * @ee_ctrl_mutex: Used to serialize exception event information.
   * @is_powered: flag to check if HBA is powered
   * @shutting_down: flag to check if shutdown has been invoked
+ * @sysfs_attrs_added: Whether or not the sysfs attributes have been 
added to
+ *	hba->dev.
   * @host_sem: semaphore used to serialize concurrent contexts
   * @eh_wq: Workqueue that eh_work works on
   * @eh_work: Worker to handle UFS errors that require s/w attention
@@ -1054,6 +1056,7 @@ struct ufs_hba {
  	struct mutex ee_ctrl_mutex;
  	bool is_powered;
  	bool shutting_down;
+	bool sysfs_attrs_added;
  	struct semaphore host_sem;

  	/* Work Queues */


  reply	other threads:[~2025-10-15 22:24 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-14 20:00 [PATCH 0/8] Eight small UFS patches Bart Van Assche
2025-10-14 20:00 ` [PATCH 1/8] ufs: core: Fix a race condition related to the "hid" attribute group Bart Van Assche
2025-10-15 22:24   ` Bart Van Assche [this message]
2025-10-17  8:27   ` Peter Wang (王信友)
2025-10-17 16:25     ` Bart Van Assche
2025-10-20  6:51       ` Peter Wang (王信友)
2025-10-20 16:13         ` Bart Van Assche
2025-10-21  3:04           ` Peter Wang (王信友)
2025-10-21 15:01             ` Bart Van Assche
2025-10-22 12:39               ` Peter Wang (王信友)
2025-10-27 14:04   ` Neil Armstrong
2025-10-27 17:12     ` Bart Van Assche
2025-10-14 20:00 ` [PATCH 2/8] ufs: core: Reduce link startup failure logging Bart Van Assche
2025-10-14 20:00 ` [PATCH 3/8] ufs: core: Improve documentation in include/ufs/ufshci.h Bart Van Assche
2025-10-14 20:00 ` [PATCH 4/8] ufs: core: Change the type of uic_command::cmd_active Bart Van Assche
2025-10-14 20:00 ` [PATCH 5/8] ufs: core: Remove UFS_DEV_COMP Bart Van Assche
2025-10-17  8:28   ` Peter Wang (王信友)
2025-10-14 20:00 ` [PATCH 6/8] ufs: core: Move the ufshcd_enable_intr() declaration Bart Van Assche
2025-10-17  8:29   ` Peter Wang (王信友)
2025-10-14 20:00 ` [PATCH 7/8] ufs: core: Remove a goto label from ufshcd_uic_cmd_compl() Bart Van Assche
2025-10-17  8:29   ` Peter Wang (王信友)
2025-10-14 20:01 ` [PATCH 8/8] ufs: core: Simplify ufshcd_mcq_sq_cleanup() using guard() Bart Van Assche
2025-10-17  8:30   ` Peter Wang (王信友)
2025-10-22  2:13 ` [PATCH 0/8] Eight small UFS patches Martin K. Petersen
2025-10-22 11:04   ` Manivannan Sadhasivam
2025-10-22 16:59     ` Bart Van Assche
2025-10-22 17:31       ` Manivannan Sadhasivam
2025-10-23 17:29         ` Bart Van Assche
2025-10-24  2:16           ` Martin K. Petersen

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=baaf9d5b-06e7-4683-a88c-c9f303f5270e@acm.org \
    --to=bvanassche@acm.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=adrian.hunter@intel.com \
    --cc=avri.altman@sandisk.com \
    --cc=beanhuo@micron.com \
    --cc=chullee@google.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=peter.wang@mediatek.com \
    --cc=quic_nguyenb@quicinc.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;
as well as URLs for NNTP newsgroup(s).