linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rapidio: Avoid data race between file operation callbacks and mport_cdev_add().
@ 2020-04-26 11:29 madhuparnabhowmik10
  2020-04-26 15:19 ` alex.b
  0 siblings, 1 reply; 2+ messages in thread
From: madhuparnabhowmik10 @ 2020-04-26 11:29 UTC (permalink / raw)
  To: mporter, alex.bou9, akpm, dan.carpenter, hubcap, tglx, ira.weiny,
	allison
  Cc: linux-kernel, andrianov, ldv-project, Madhuparna Bhowmik

From: Madhuparna Bhowmik <madhuparnabhowmik10@gmail.com>

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 <madhuparnabhowmik10@gmail.com>
---
 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 8155f59ece38..40296378e5ee 100644
--- a/drivers/rapidio/devices/rio_mport_cdev.c
+++ b/drivers/rapidio/devices/rio_mport_cdev.c
@@ -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;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] rapidio: Avoid data race between file operation callbacks and mport_cdev_add().
  2020-04-26 11:29 [PATCH] rapidio: Avoid data race between file operation callbacks and mport_cdev_add() madhuparnabhowmik10
@ 2020-04-26 15:19 ` alex.b
  0 siblings, 0 replies; 2+ messages in thread
From: alex.b @ 2020-04-26 15:19 UTC (permalink / raw)
  To: madhuparnabhowmik10, mporter, akpm, dan.carpenter, hubcap, tglx,
	ira.weiny, allison
  Cc: linux-kernel, andrianov, ldv-project

On 2020-04-26 7:29 a.m., madhuparnabhowmik10@gmail.com wrote:
> From: Madhuparna Bhowmik <madhuparnabhowmik10@gmail.com>
>
> 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 <madhuparnabhowmik10@gmail.com>
> ---
>   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 8155f59ece38..40296378e5ee 100644
> --- a/drivers/rapidio/devices/rio_mport_cdev.c
> +++ b/drivers/rapidio/devices/rio_mport_cdev.c
> @@ -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;

Acked-by: Alexandre Bounine <alex.bou9@gmail.com>



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-04-26 15:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-26 11:29 [PATCH] rapidio: Avoid data race between file operation callbacks and mport_cdev_add() madhuparnabhowmik10
2020-04-26 15:19 ` alex.b

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