Skip to contentSkip to conversation

rapidio: Avoid data race between file operation callbacks and mport_cdev_add().

PATCH Applied [email protected] 2 participants 2 messages Apr 26, 2020 Rapidio

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rapidio: Avoid data race between file operation callbacks and mport_cdev_add().

From: Madhuparna Bhowmik <[email protected]>

Fields of md(mport_dev) are set after cdev_device_add(). However, the file operation callbacks can be called after cdev_device_add() and therefore accesses to fields of md in the callbacks can race with the rest of the mport_cdev_add() function.

One such example is INIT_LIST_HEAD(&md->portwrites) in mport_cdev_add(), the list is initialised after cdev_device_add(). This can race with list_add_tail(&pw_filter->md_node,&md->portwrites) in rio_mport_add_pw_filter() which is called by unlocked_ioctl.

To avoid such data races use cdev_device_add() after initializing md.

Found by Linux Driver Verification project (linuxtesting.org).

---
 drivers/rapidio/devices/rio_mport_cdev.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/rapidio/devices/rio_mport_cdev.c +7 -7
index 8155f59ece38..40296378e5ee 100644
@@ -2379,13 +2379,6 @@ static struct mport_dev *mport_cdev_add(struct rio_mport *mport)
 	cdev_init(&md->cdev, &mport_fops);
 	md->cdev.owner = THIS_MODULE;
 
-	ret = cdev_device_add(&md->cdev, &md->dev);
-	if (ret) {
-		rmcd_error("Failed to register mport %d (err=%d)",
-		       mport->id, ret);
-		goto err_cdev;
-	}
-
 	INIT_LIST_HEAD(&md->doorbells);
 	spin_lock_init(&md->db_lock);
 	INIT_LIST_HEAD(&md->portwrites);
@@ -2405,6 +2398,13 @@ static struct mport_dev *mport_cdev_add(struct rio_mport *mport)
 #else
 	md->properties.transfer_mode |= RIO_TRANSFER_MODE_TRANSFER;
 #endif
+
+	ret = cdev_device_add(&md->cdev, &md->dev);
+	if (ret) {
+		rmcd_error("Failed to register mport %d (err=%d)",
+		       mport->id, ret);
+		goto err_cdev;
+	}
 	ret = rio_query_mport(mport, &attr);
 	if (!ret) {
 		md->properties.flags = attr.flags;
Signature · 1 line
2.17.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 43 lines · On 2020-04-26 7:29 a.m., [email protected] wrote:

From: Madhuparna Bhowmik <[email protected]>

Fields of md(mport_dev) are set after cdev_device_add(). However, the file operation callbacks can be called after cdev_device_add() and therefore accesses to fields of md in the callbacks can race with the rest of the mport_cdev_add() function.

One such example is INIT_LIST_HEAD(&md->portwrites) in mport_cdev_add(), the list is initialised after cdev_device_add(). This can race with list_add_tail(&pw_filter->md_node,&md->portwrites) in rio_mport_add_pw_filter() which is called by unlocked_ioctl.

To avoid such data races use cdev_device_add() after initializing md.

Found by Linux Driver Verification project (linuxtesting.org).

---
  drivers/rapidio/devices/rio_mport_cdev.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)
drivers/rapidio/devices/rio_mport_cdev.c +0 -7
index 8155f59ece38..40296378e5ee 100644
@@ -2379,13 +2379,6 @@ static struct mport_dev *mport_cdev_add(struct rio_mport *mport)
  	cdev_init(&md->cdev, &mport_fops);
  	md->cdev.owner = THIS_MODULE;
 
-	ret = cdev_device_add(&md->cdev, &md->dev);
-	if (ret) {
-		rmcd_error("Failed to register mport %d (err=%d)",
-		       mport->id, ret);
-		goto err_cdev;
-	}
-
  	INIT_LIST_HEAD(&md->doorbells);
  	spin_lock_init(&md->db_lock);
  	INIT_LIST_HEAD(&md->portwrites);
@@ -2405,6 +2398,13 @@ static struct mport_dev *mport_cdev_add(struct rio_mport *mport)
  #else
  	md->properties.transfer_mode |= RIO_TRANSFER_MODE_TRANSFER;
  #endif

drivers/rapidio/devices/rio_mport_cdev.c static struct mport_dev *mport_cdev_add(struct rio_mport *mport) · line 2410

Quoted 10 lines
drivers/rapidio/devices/rio_mport_cdev.c +7 -0
+
+	ret = cdev_device_add(&md->cdev, &md->dev);
+	if (ret) {
+		rmcd_error("Failed to register mport %d (err=%d)",
+		       mport->id, ret);
+		goto err_cdev;
+	}
  	ret = rio_query_mport(mport, &attr);
  	if (!ret) {
  		md->properties.flags = attr.flags;