2020-05-20 11:13:46

by Horatiu Vultur

[permalink] [raw]
Subject: [PATCH 1/3] bridge: mrp: Add br_mrp_unique_ifindex function

It is not allow to have the same net bridge port part of multiple MRP
rings. Therefore add a check if the port is used already in a different
MRP. In that case return failure.

Fixes: 9a9f26e8f7ea ("bridge: mrp: Connect MRP API with the switchdev API")
Signed-off-by: Horatiu Vultur <[email protected]>
---
net/bridge/br_mrp.c | 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)

diff --git a/net/bridge/br_mrp.c b/net/bridge/br_mrp.c
index d7bc09de4c139..a5a3fa59c078a 100644
--- a/net/bridge/br_mrp.c
+++ b/net/bridge/br_mrp.c
@@ -37,6 +37,32 @@ static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id)
return res;
}

+static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex)
+{
+ struct br_mrp *mrp;
+ bool res = true;
+
+ rcu_read_lock();
+ list_for_each_entry_rcu(mrp, &br->mrp_list, list,
+ lockdep_rtnl_is_held()) {
+ struct net_bridge_port *p;
+
+ p = rcu_dereference(mrp->p_port);
+ if (p && p->dev->ifindex == ifindex) {
+ res = false;
+ break;
+ }
+
+ p = rcu_dereference(mrp->s_port);
+ if (p && p->dev->ifindex == ifindex) {
+ res = false;
+ break;
+ }
+ }
+ rcu_read_unlock();
+ return res;
+}
+
static struct br_mrp *br_mrp_find_port(struct net_bridge *br,
struct net_bridge_port *p)
{
@@ -255,6 +281,11 @@ int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance)
!br_mrp_get_port(br, instance->s_ifindex))
return -EINVAL;

+ /* It is not possible to have the same port part of multiple rings */
+ if (!br_mrp_unique_ifindex(br, instance->p_ifindex) ||
+ !br_mrp_unique_ifindex(br, instance->s_ifindex))
+ return -EINVAL;
+
mrp = kzalloc(sizeof(*mrp), GFP_KERNEL);
if (!mrp)
return -ENOMEM;
--
2.26.2


2020-05-21 08:19:04

by Nikolay Aleksandrov

[permalink] [raw]
Subject: Re: [PATCH 1/3] bridge: mrp: Add br_mrp_unique_ifindex function

On 20/05/2020 16:09, Horatiu Vultur wrote:
> It is not allow to have the same net bridge port part of multiple MRP
> rings. Therefore add a check if the port is used already in a different
> MRP. In that case return failure.
>
> Fixes: 9a9f26e8f7ea ("bridge: mrp: Connect MRP API with the switchdev API")
> Signed-off-by: Horatiu Vultur <[email protected]>
> ---
> net/bridge/br_mrp.c | 31 +++++++++++++++++++++++++++++++
> 1 file changed, 31 insertions(+)
>
> diff --git a/net/bridge/br_mrp.c b/net/bridge/br_mrp.c
> index d7bc09de4c139..a5a3fa59c078a 100644
> --- a/net/bridge/br_mrp.c
> +++ b/net/bridge/br_mrp.c
> @@ -37,6 +37,32 @@ static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id)
> return res;
> }
>
> +static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex)
> +{
> + struct br_mrp *mrp;
> + bool res = true;
> +
> + rcu_read_lock();

Why do you need the rcu_read_lock() here when lockdep_rtnl_is_held() is used?
You should be able to just do rtnl_dereference() below as this is used only
under rtnl.

> + list_for_each_entry_rcu(mrp, &br->mrp_list, list,
> + lockdep_rtnl_is_held()) {
> + struct net_bridge_port *p;
> +
> + p = rcu_dereference(mrp->p_port);
> + if (p && p->dev->ifindex == ifindex) {
> + res = false;
> + break;
> + }
> +
> + p = rcu_dereference(mrp->s_port);
> + if (p && p->dev->ifindex == ifindex) {
> + res = false;
> + break;
> + }
> + }
> + rcu_read_unlock();
> + return res;
> +}
> +
> static struct br_mrp *br_mrp_find_port(struct net_bridge *br,
> struct net_bridge_port *p)
> {
> @@ -255,6 +281,11 @@ int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance)
> !br_mrp_get_port(br, instance->s_ifindex))
> return -EINVAL;
>
> + /* It is not possible to have the same port part of multiple rings */
> + if (!br_mrp_unique_ifindex(br, instance->p_ifindex) ||
> + !br_mrp_unique_ifindex(br, instance->s_ifindex))
> + return -EINVAL;
> +
> mrp = kzalloc(sizeof(*mrp), GFP_KERNEL);
> if (!mrp)
> return -ENOMEM;
>

2020-05-21 16:53:41

by Horatiu Vultur

[permalink] [raw]
Subject: Re: [PATCH 1/3] bridge: mrp: Add br_mrp_unique_ifindex function

The 05/21/2020 11:16, Nikolay Aleksandrov wrote:
> On 20/05/2020 16:09, Horatiu Vultur wrote:
> > It is not allow to have the same net bridge port part of multiple MRP
> > rings. Therefore add a check if the port is used already in a different
> > MRP. In that case return failure.
> >
> > Fixes: 9a9f26e8f7ea ("bridge: mrp: Connect MRP API with the switchdev API")
> > Signed-off-by: Horatiu Vultur <[email protected]>
> > ---
> > net/bridge/br_mrp.c | 31 +++++++++++++++++++++++++++++++
> > 1 file changed, 31 insertions(+)
> >
> > diff --git a/net/bridge/br_mrp.c b/net/bridge/br_mrp.c
> > index d7bc09de4c139..a5a3fa59c078a 100644
> > --- a/net/bridge/br_mrp.c
> > +++ b/net/bridge/br_mrp.c
> > @@ -37,6 +37,32 @@ static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id)
> > return res;
> > }
> >
> > +static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex)
> > +{
> > + struct br_mrp *mrp;
> > + bool res = true;
> > +
> > + rcu_read_lock();
>
> Why do you need the rcu_read_lock() here when lockdep_rtnl_is_held() is used?
> You should be able to just do rtnl_dereference() below as this is used only
> under rtnl.

Hi Nik,

Also initially I thought that is not needed, but when I enabled all the
RCU debug configs to see if I use correctly the RCU, I got a warning
regarding suspicious RCU usage.
And that is the reason why I have put it.

>
> > + list_for_each_entry_rcu(mrp, &br->mrp_list, list,
> > + lockdep_rtnl_is_held()) {
> > + struct net_bridge_port *p;
> > +
> > + p = rcu_dereference(mrp->p_port);
> > + if (p && p->dev->ifindex == ifindex) {
> > + res = false;
> > + break;
> > + }
> > +
> > + p = rcu_dereference(mrp->s_port);
> > + if (p && p->dev->ifindex == ifindex) {
> > + res = false;
> > + break;
> > + }
> > + }
> > + rcu_read_unlock();
> > + return res;
> > +}
> > +
> > static struct br_mrp *br_mrp_find_port(struct net_bridge *br,
> > struct net_bridge_port *p)
> > {
> > @@ -255,6 +281,11 @@ int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance)
> > !br_mrp_get_port(br, instance->s_ifindex))
> > return -EINVAL;
> >
> > + /* It is not possible to have the same port part of multiple rings */
> > + if (!br_mrp_unique_ifindex(br, instance->p_ifindex) ||
> > + !br_mrp_unique_ifindex(br, instance->s_ifindex))
> > + return -EINVAL;
> > +
> > mrp = kzalloc(sizeof(*mrp), GFP_KERNEL);
> > if (!mrp)
> > return -ENOMEM;
> >
>

--
/Horatiu

2020-05-21 17:02:29

by Nikolay Aleksandrov

[permalink] [raw]
Subject: Re: [PATCH 1/3] bridge: mrp: Add br_mrp_unique_ifindex function

On 21/05/2020 21:49, Horatiu Vultur wrote:
> The 05/21/2020 11:16, Nikolay Aleksandrov wrote:
>> On 20/05/2020 16:09, Horatiu Vultur wrote:
>>> It is not allow to have the same net bridge port part of multiple MRP
>>> rings. Therefore add a check if the port is used already in a different
>>> MRP. In that case return failure.
>>>
>>> Fixes: 9a9f26e8f7ea ("bridge: mrp: Connect MRP API with the switchdev API")
>>> Signed-off-by: Horatiu Vultur <[email protected]>
>>> ---
>>> net/bridge/br_mrp.c | 31 +++++++++++++++++++++++++++++++
>>> 1 file changed, 31 insertions(+)
>>>
>>> diff --git a/net/bridge/br_mrp.c b/net/bridge/br_mrp.c
>>> index d7bc09de4c139..a5a3fa59c078a 100644
>>> --- a/net/bridge/br_mrp.c
>>> +++ b/net/bridge/br_mrp.c
>>> @@ -37,6 +37,32 @@ static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id)
>>> return res;
>>> }
>>>
>>> +static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex)
>>> +{
>>> + struct br_mrp *mrp;
>>> + bool res = true;
>>> +
>>> + rcu_read_lock();
>>
>> Why do you need the rcu_read_lock() here when lockdep_rtnl_is_held() is used?
>> You should be able to just do rtnl_dereference() below as this is used only
>> under rtnl.
>
> Hi Nik,
>
> Also initially I thought that is not needed, but when I enabled all the
> RCU debug configs to see if I use correctly the RCU, I got a warning
> regarding suspicious RCU usage.
> And that is the reason why I have put it.
>

Did you try using rtnl_dereference() instead of rcu_dereference() ?

>>
>>> + list_for_each_entry_rcu(mrp, &br->mrp_list, list,
>>> + lockdep_rtnl_is_held()) {
>>> + struct net_bridge_port *p;
>>> +
>>> + p = rcu_dereference(mrp->p_port);
>>> + if (p && p->dev->ifindex == ifindex) {
>>> + res = false;
>>> + break;
>>> + }
>>> +
>>> + p = rcu_dereference(mrp->s_port);
>>> + if (p && p->dev->ifindex == ifindex) {
>>> + res = false;
>>> + break;
>>> + }
>>> + }
>>> + rcu_read_unlock();
>>> + return res;
>>> +}
>>> +
>>> static struct br_mrp *br_mrp_find_port(struct net_bridge *br,
>>> struct net_bridge_port *p)
>>> {
>>> @@ -255,6 +281,11 @@ int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance)
>>> !br_mrp_get_port(br, instance->s_ifindex))
>>> return -EINVAL;
>>>
>>> + /* It is not possible to have the same port part of multiple rings */
>>> + if (!br_mrp_unique_ifindex(br, instance->p_ifindex) ||
>>> + !br_mrp_unique_ifindex(br, instance->s_ifindex))
>>> + return -EINVAL;
>>> +
>>> mrp = kzalloc(sizeof(*mrp), GFP_KERNEL);
>>> if (!mrp)
>>> return -ENOMEM;
>>>
>>
>

2020-05-21 17:33:01

by Horatiu Vultur

[permalink] [raw]
Subject: Re: [PATCH 1/3] bridge: mrp: Add br_mrp_unique_ifindex function

The 05/21/2020 19:58, Nikolay Aleksandrov wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
>
> On 21/05/2020 21:49, Horatiu Vultur wrote:
> > The 05/21/2020 11:16, Nikolay Aleksandrov wrote:
> >> On 20/05/2020 16:09, Horatiu Vultur wrote:
> >>> It is not allow to have the same net bridge port part of multiple MRP
> >>> rings. Therefore add a check if the port is used already in a different
> >>> MRP. In that case return failure.
> >>>
> >>> Fixes: 9a9f26e8f7ea ("bridge: mrp: Connect MRP API with the switchdev API")
> >>> Signed-off-by: Horatiu Vultur <[email protected]>
> >>> ---
> >>> net/bridge/br_mrp.c | 31 +++++++++++++++++++++++++++++++
> >>> 1 file changed, 31 insertions(+)
> >>>
> >>> diff --git a/net/bridge/br_mrp.c b/net/bridge/br_mrp.c
> >>> index d7bc09de4c139..a5a3fa59c078a 100644
> >>> --- a/net/bridge/br_mrp.c
> >>> +++ b/net/bridge/br_mrp.c
> >>> @@ -37,6 +37,32 @@ static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id)
> >>> return res;
> >>> }
> >>>
> >>> +static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex)
> >>> +{
> >>> + struct br_mrp *mrp;
> >>> + bool res = true;
> >>> +
> >>> + rcu_read_lock();
> >>
> >> Why do you need the rcu_read_lock() here when lockdep_rtnl_is_held() is used?
> >> You should be able to just do rtnl_dereference() below as this is used only
> >> under rtnl.
> >
> > Hi Nik,
> >
> > Also initially I thought that is not needed, but when I enabled all the
> > RCU debug configs to see if I use correctly the RCU, I got a warning
> > regarding suspicious RCU usage.
> > And that is the reason why I have put it.
> >
>
> Did you try using rtnl_dereference() instead of rcu_dereference() ?

I have just tried it now and that seems to work fine.
I will redo the patch and send a new patch series.

>
> >>
> >>> + list_for_each_entry_rcu(mrp, &br->mrp_list, list,
> >>> + lockdep_rtnl_is_held()) {
> >>> + struct net_bridge_port *p;
> >>> +
> >>> + p = rcu_dereference(mrp->p_port);
> >>> + if (p && p->dev->ifindex == ifindex) {
> >>> + res = false;
> >>> + break;
> >>> + }
> >>> +
> >>> + p = rcu_dereference(mrp->s_port);
> >>> + if (p && p->dev->ifindex == ifindex) {
> >>> + res = false;
> >>> + break;
> >>> + }
> >>> + }
> >>> + rcu_read_unlock();
> >>> + return res;
> >>> +}
> >>> +
> >>> static struct br_mrp *br_mrp_find_port(struct net_bridge *br,
> >>> struct net_bridge_port *p)
> >>> {
> >>> @@ -255,6 +281,11 @@ int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance)
> >>> !br_mrp_get_port(br, instance->s_ifindex))
> >>> return -EINVAL;
> >>>
> >>> + /* It is not possible to have the same port part of multiple rings */
> >>> + if (!br_mrp_unique_ifindex(br, instance->p_ifindex) ||
> >>> + !br_mrp_unique_ifindex(br, instance->s_ifindex))
> >>> + return -EINVAL;
> >>> +
> >>> mrp = kzalloc(sizeof(*mrp), GFP_KERNEL);
> >>> if (!mrp)
> >>> return -ENOMEM;
> >>>
> >>
> >
>

--
/Horatiu