diff mbox series

net: core: devlink.c: Hold devlink->lock from the beginning of devlink_dpipe_table_register()

Message ID 20200222065234.8829-1-madhuparnabhowmik10@gmail.com
State Superseded
Delegated to: David Miller
Headers show
Series net: core: devlink.c: Hold devlink->lock from the beginning of devlink_dpipe_table_register() | expand

Commit Message

Madhuparna Bhowmik Feb. 22, 2020, 6:52 a.m. UTC
From: Madhuparna Bhowmik <madhuparnabhowmik10@gmail.com>

devlink_dpipe_table_find() should be called under either
rcu_read_lock() or devlink->lock. devlink_dpipe_table_register()
calls devlink_dpipe_table_find() without holding the lock
and acquires it later. Therefore hold the devlink->lock
from the beginning of devlink_dpipe_table_register().

Suggested-by: Jiri Pirko <jiri@mellanox.com>
Signed-off-by: Madhuparna Bhowmik <madhuparnabhowmik10@gmail.com>
---
 net/core/devlink.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

Comments

Jiri Pirko Feb. 23, 2020, 6:43 a.m. UTC | #1
Sat, Feb 22, 2020 at 07:52:34AM CET, madhuparnabhowmik10@gmail.com wrote:
>From: Madhuparna Bhowmik <madhuparnabhowmik10@gmail.com>
>
>devlink_dpipe_table_find() should be called under either
>rcu_read_lock() or devlink->lock. devlink_dpipe_table_register()
>calls devlink_dpipe_table_find() without holding the lock
>and acquires it later. Therefore hold the devlink->lock
>from the beginning of devlink_dpipe_table_register().
>
>Suggested-by: Jiri Pirko <jiri@mellanox.com>
>Signed-off-by: Madhuparna Bhowmik <madhuparnabhowmik10@gmail.com>
>---
> net/core/devlink.c | 15 +++++++++++----
> 1 file changed, 11 insertions(+), 4 deletions(-)
>
>diff --git a/net/core/devlink.c b/net/core/devlink.c
>index 3e8c94155d93..ba9dd8cb98c3 100644
>--- a/net/core/devlink.c
>+++ b/net/core/devlink.c
>@@ -6840,22 +6840,29 @@ int devlink_dpipe_table_register(struct devlink *devlink,
> {
> 	struct devlink_dpipe_table *table;
> 
>-	if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
>+	mutex_lock(&devlink->lock);
>+
>+	if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name)) {
>+		mutex_unlock(&devlink->lock);
> 		return -EEXIST;
>+	}
> 
>-	if (WARN_ON(!table_ops->size_get))
>+	if (WARN_ON(!table_ops->size_get)) {
>+		mutex_unlock(&devlink->lock);
> 		return -EINVAL;
>+	}
> 
> 	table = kzalloc(sizeof(*table), GFP_KERNEL);
>-	if (!table)
>+	if (!table) {
>+		mutex_unlock(&devlink->lock);

Please use "goto unlock" instead of unlocking on multiple places.



> 		return -ENOMEM;
>+	}
> 
> 	table->name = table_name;
> 	table->table_ops = table_ops;
> 	table->priv = priv;
> 	table->counter_control_extern = counter_control_extern;
> 
>-	mutex_lock(&devlink->lock);
> 	list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
> 	mutex_unlock(&devlink->lock);
> 	return 0;
>-- 
>2.17.1
>
Madhuparna Bhowmik Feb. 23, 2020, 10:56 a.m. UTC | #2
On Sun, Feb 23, 2020 at 07:43:29AM +0100, Jiri Pirko wrote:
> Sat, Feb 22, 2020 at 07:52:34AM CET, madhuparnabhowmik10@gmail.com wrote:
> >From: Madhuparna Bhowmik <madhuparnabhowmik10@gmail.com>
> >
> >devlink_dpipe_table_find() should be called under either
> >rcu_read_lock() or devlink->lock. devlink_dpipe_table_register()
> >calls devlink_dpipe_table_find() without holding the lock
> >and acquires it later. Therefore hold the devlink->lock
> >from the beginning of devlink_dpipe_table_register().
> >
> >Suggested-by: Jiri Pirko <jiri@mellanox.com>
> >Signed-off-by: Madhuparna Bhowmik <madhuparnabhowmik10@gmail.com>
> >---
> > net/core/devlink.c | 15 +++++++++++----
> > 1 file changed, 11 insertions(+), 4 deletions(-)
> >
> >diff --git a/net/core/devlink.c b/net/core/devlink.c
> >index 3e8c94155d93..ba9dd8cb98c3 100644
> >--- a/net/core/devlink.c
> >+++ b/net/core/devlink.c
> >@@ -6840,22 +6840,29 @@ int devlink_dpipe_table_register(struct devlink *devlink,
> > {
> > 	struct devlink_dpipe_table *table;
> > 
> >-	if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
> >+	mutex_lock(&devlink->lock);
> >+
> >+	if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name)) {
> >+		mutex_unlock(&devlink->lock);
> > 		return -EEXIST;
> >+	}
> > 
> >-	if (WARN_ON(!table_ops->size_get))
> >+	if (WARN_ON(!table_ops->size_get)) {
> >+		mutex_unlock(&devlink->lock);
> > 		return -EINVAL;
> >+	}
> > 
> > 	table = kzalloc(sizeof(*table), GFP_KERNEL);
> >-	if (!table)
> >+	if (!table) {
> >+		mutex_unlock(&devlink->lock);
> 
> Please use "goto unlock" instead of unlocking on multiple places.
>
Sure, I have sent a new patch.
Thank you,
Madhuparna
> 
> 
> > 		return -ENOMEM;
> >+	}
> > 
> > 	table->name = table_name;
> > 	table->table_ops = table_ops;
> > 	table->priv = priv;
> > 	table->counter_control_extern = counter_control_extern;
> > 
> >-	mutex_lock(&devlink->lock);
> > 	list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
> > 	mutex_unlock(&devlink->lock);
> > 	return 0;
> >-- 
> >2.17.1
> >
diff mbox series

Patch

diff --git a/net/core/devlink.c b/net/core/devlink.c
index 3e8c94155d93..ba9dd8cb98c3 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -6840,22 +6840,29 @@  int devlink_dpipe_table_register(struct devlink *devlink,
 {
 	struct devlink_dpipe_table *table;
 
-	if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
+	mutex_lock(&devlink->lock);
+
+	if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name)) {
+		mutex_unlock(&devlink->lock);
 		return -EEXIST;
+	}
 
-	if (WARN_ON(!table_ops->size_get))
+	if (WARN_ON(!table_ops->size_get)) {
+		mutex_unlock(&devlink->lock);
 		return -EINVAL;
+	}
 
 	table = kzalloc(sizeof(*table), GFP_KERNEL);
-	if (!table)
+	if (!table) {
+		mutex_unlock(&devlink->lock);
 		return -ENOMEM;
+	}
 
 	table->name = table_name;
 	table->table_ops = table_ops;
 	table->priv = priv;
 	table->counter_control_extern = counter_control_extern;
 
-	mutex_lock(&devlink->lock);
 	list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
 	mutex_unlock(&devlink->lock);
 	return 0;