From: Madhuparna Bhowmik <[email protected]>

[ Upstream commit e1c3cdb26ab881b77486dc50370356a349077c74 ]

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).

Signed-off-by: Madhuparna Bhowmik <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Acked-by: Alexandre Bounine <[email protected]>
Cc: Matt Porter <[email protected]>
Cc: Dan Carpenter <[email protected]>
Cc: Mike Marshall <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ira Weiny <[email protected]>
Cc: Allison Randal <[email protected]>
Cc: Pavel Andrianov <[email protected]>
Link: 
http://lkml.kernel.org/r/[email protected]
Signed-off-by: Linus Torvalds <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
---
 drivers/rapidio/devices/rio_mport_cdev.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/rapidio/devices/rio_mport_cdev.c 
b/drivers/rapidio/devices/rio_mport_cdev.c
index 5940780648e0f..f36a8a5261a13 100644
--- a/drivers/rapidio/devices/rio_mport_cdev.c
+++ b/drivers/rapidio/devices/rio_mport_cdev.c
@@ -2385,13 +2385,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);
@@ -2411,6 +2404,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;
-- 
2.25.1

Reply via email to