diff mbox series

net: mac802154: Fix racy device stats updates by DEV_STATS_INC() and DEV_STATS_ADD()

Message ID 20240531080739.2608969-1-jiangyunshui@kylinos.cn (mailing list archive)
State Accepted
Headers show
Series net: mac802154: Fix racy device stats updates by DEV_STATS_INC() and DEV_STATS_ADD() | expand

Commit Message

Yunshui Jiang May 31, 2024, 8:07 a.m. UTC
mac802154 devices update their dev->stats fields locklessly. Therefore
these counters should be updated atomically. Adopt SMP safe DEV_STATS_INC()
and DEV_STATS_ADD() to achieve this.

Signed-off-by: Yunshui Jiang <jiangyunshui@kylinos.cn>
---
 net/mac802154/tx.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Stefan Schmidt June 3, 2024, 9:33 a.m. UTC | #1
Hello.

On 31.05.24 10:07, Yunshui Jiang wrote:
> mac802154 devices update their dev->stats fields locklessly. Therefore
> these counters should be updated atomically. Adopt SMP safe DEV_STATS_INC()
> and DEV_STATS_ADD() to achieve this.
> 
> Signed-off-by: Yunshui Jiang <jiangyunshui@kylinos.cn>
> ---
>   net/mac802154/tx.c | 8 ++++----
>   1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/net/mac802154/tx.c b/net/mac802154/tx.c
> index 2a6f1ed763c9..6fbed5bb5c3e 100644
> --- a/net/mac802154/tx.c
> +++ b/net/mac802154/tx.c
> @@ -34,8 +34,8 @@ void ieee802154_xmit_sync_worker(struct work_struct *work)
>   	if (res)
>   		goto err_tx;
>   
> -	dev->stats.tx_packets++;
> -	dev->stats.tx_bytes += skb->len;
> +	DEV_STATS_INC(dev, tx_packets);
> +	DEV_STATS_ADD(dev, tx_bytes, skb->len);
>   
>   	ieee802154_xmit_complete(&local->hw, skb, false);
>   
> @@ -90,8 +90,8 @@ ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
>   		if (ret)
>   			goto err_wake_netif_queue;
>   
> -		dev->stats.tx_packets++;
> -		dev->stats.tx_bytes += len;
> +		DEV_STATS_INC(dev, tx_packets);
> +		DEV_STATS_ADD(dev, tx_bytes, len);
>   	} else {
>   		local->tx_skb = skb;
>   		queue_work(local->workqueue, &local->sync_tx_work);

This patch has been applied to the wpan tree and will be
part of the next pull request to net. Thanks!

regards
Stefan Schmidt
Jakub Kicinski June 3, 2024, 11:55 p.m. UTC | #2
On Mon, 3 Jun 2024 11:33:28 +0200 Stefan Schmidt wrote:
> Hello.
> 
> On 31.05.24 10:07, Yunshui Jiang wrote:
> > mac802154 devices update their dev->stats fields locklessly. Therefore
> > these counters should be updated atomically. Adopt SMP safe DEV_STATS_INC()
> > and DEV_STATS_ADD() to achieve this.
> > 
> > Signed-off-by: Yunshui Jiang <jiangyunshui@kylinos.cn>
> > ---
> >   net/mac802154/tx.c | 8 ++++----
> >   1 file changed, 4 insertions(+), 4 deletions(-)
> > 
> > diff --git a/net/mac802154/tx.c b/net/mac802154/tx.c
> > index 2a6f1ed763c9..6fbed5bb5c3e 100644
> > --- a/net/mac802154/tx.c
> > +++ b/net/mac802154/tx.c
> > @@ -34,8 +34,8 @@ void ieee802154_xmit_sync_worker(struct work_struct *work)
> >   	if (res)
> >   		goto err_tx;
> >   
> > -	dev->stats.tx_packets++;
> > -	dev->stats.tx_bytes += skb->len;
> > +	DEV_STATS_INC(dev, tx_packets);
> > +	DEV_STATS_ADD(dev, tx_bytes, skb->len);
> >   
> >   	ieee802154_xmit_complete(&local->hw, skb, false);
> >   
> > @@ -90,8 +90,8 @@ ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
> >   		if (ret)
> >   			goto err_wake_netif_queue;
> >   
> > -		dev->stats.tx_packets++;
> > -		dev->stats.tx_bytes += len;
> > +		DEV_STATS_INC(dev, tx_packets);
> > +		DEV_STATS_ADD(dev, tx_bytes, len);
> >   	} else {
> >   		local->tx_skb = skb;
> >   		queue_work(local->workqueue, &local->sync_tx_work);  
> 
> This patch has been applied to the wpan tree and will be
> part of the next pull request to net. Thanks!

Hi! I haven't looked in detail, but FWIW

$ git grep LLTX net/mac802154/
$

and similar patch from this author has been rejected:

https://lore.kernel.org/all/CANn89iLPYoOjMxNjBVHY7GwPFBGuxwRoM9gZZ-fWUUYFYjM1Uw@mail.gmail.com/
Alexander Aring June 4, 2024, 1:52 p.m. UTC | #3
Hi,

On Mon, Jun 3, 2024 at 7:56 PM Jakub Kicinski <kuba@kernel.org> wrote:
>
> On Mon, 3 Jun 2024 11:33:28 +0200 Stefan Schmidt wrote:
> > Hello.
> >
> > On 31.05.24 10:07, Yunshui Jiang wrote:
> > > mac802154 devices update their dev->stats fields locklessly. Therefore
> > > these counters should be updated atomically. Adopt SMP safe DEV_STATS_INC()
> > > and DEV_STATS_ADD() to achieve this.
> > >
> > > Signed-off-by: Yunshui Jiang <jiangyunshui@kylinos.cn>
> > > ---
> > >   net/mac802154/tx.c | 8 ++++----
> > >   1 file changed, 4 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/net/mac802154/tx.c b/net/mac802154/tx.c
> > > index 2a6f1ed763c9..6fbed5bb5c3e 100644
> > > --- a/net/mac802154/tx.c
> > > +++ b/net/mac802154/tx.c
> > > @@ -34,8 +34,8 @@ void ieee802154_xmit_sync_worker(struct work_struct *work)
> > >     if (res)
> > >             goto err_tx;
> > >
> > > -   dev->stats.tx_packets++;
> > > -   dev->stats.tx_bytes += skb->len;
> > > +   DEV_STATS_INC(dev, tx_packets);
> > > +   DEV_STATS_ADD(dev, tx_bytes, skb->len);
> > >
> > >     ieee802154_xmit_complete(&local->hw, skb, false);
> > >
> > > @@ -90,8 +90,8 @@ ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
> > >             if (ret)
> > >                     goto err_wake_netif_queue;
> > >
> > > -           dev->stats.tx_packets++;
> > > -           dev->stats.tx_bytes += len;
> > > +           DEV_STATS_INC(dev, tx_packets);
> > > +           DEV_STATS_ADD(dev, tx_bytes, len);
> > >     } else {
> > >             local->tx_skb = skb;
> > >             queue_work(local->workqueue, &local->sync_tx_work);
> >
> > This patch has been applied to the wpan tree and will be
> > part of the next pull request to net. Thanks!
>
> Hi! I haven't looked in detail, but FWIW
>
> $ git grep LLTX net/mac802154/
> $
>
> and similar patch from this author has been rejected:
>
> https://lore.kernel.org/all/CANn89iLPYoOjMxNjBVHY7GwPFBGuxwRoM9gZZ-fWUUYFYjM1Uw@mail.gmail.com/

In the case of ieee802154_tx() yes the tx lock is held so it's like
what the mentioned link says. The workqueue is an ordered workqueue
and you either have the driver async xmit option (the preferred
option) or the driver sync xmit callback on a per driver (implies per
interface) basis.

I also don't see why there is currently a problem with the current
non-atomic way.

- Alex
Stefan Schmidt June 5, 2024, 8:02 p.m. UTC | #4
Hello Jakub, Alex,

On 04.06.24 15:52, Alexander Aring wrote:
> Hi,
> 
> On Mon, Jun 3, 2024 at 7:56 PM Jakub Kicinski <kuba@kernel.org> wrote:
>>
>> On Mon, 3 Jun 2024 11:33:28 +0200 Stefan Schmidt wrote:
>>> Hello.
>>>
>>> On 31.05.24 10:07, Yunshui Jiang wrote:
>>>> mac802154 devices update their dev->stats fields locklessly. Therefore
>>>> these counters should be updated atomically. Adopt SMP safe DEV_STATS_INC()
>>>> and DEV_STATS_ADD() to achieve this.
>>>>
>>>> Signed-off-by: Yunshui Jiang <jiangyunshui@kylinos.cn>
>>>> ---
>>>>    net/mac802154/tx.c | 8 ++++----
>>>>    1 file changed, 4 insertions(+), 4 deletions(-)
>>>>
>>>> diff --git a/net/mac802154/tx.c b/net/mac802154/tx.c
>>>> index 2a6f1ed763c9..6fbed5bb5c3e 100644
>>>> --- a/net/mac802154/tx.c
>>>> +++ b/net/mac802154/tx.c
>>>> @@ -34,8 +34,8 @@ void ieee802154_xmit_sync_worker(struct work_struct *work)
>>>>      if (res)
>>>>              goto err_tx;
>>>>
>>>> -   dev->stats.tx_packets++;
>>>> -   dev->stats.tx_bytes += skb->len;
>>>> +   DEV_STATS_INC(dev, tx_packets);
>>>> +   DEV_STATS_ADD(dev, tx_bytes, skb->len);
>>>>
>>>>      ieee802154_xmit_complete(&local->hw, skb, false);
>>>>
>>>> @@ -90,8 +90,8 @@ ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
>>>>              if (ret)
>>>>                      goto err_wake_netif_queue;
>>>>
>>>> -           dev->stats.tx_packets++;
>>>> -           dev->stats.tx_bytes += len;
>>>> +           DEV_STATS_INC(dev, tx_packets);
>>>> +           DEV_STATS_ADD(dev, tx_bytes, len);
>>>>      } else {
>>>>              local->tx_skb = skb;
>>>>              queue_work(local->workqueue, &local->sync_tx_work);
>>>
>>> This patch has been applied to the wpan tree and will be
>>> part of the next pull request to net. Thanks!
>>
>> Hi! I haven't looked in detail, but FWIW
>>
>> $ git grep LLTX net/mac802154/
>> $
>>
>> and similar patch from this author has been rejected:
>>
>> https://lore.kernel.org/all/CANn89iLPYoOjMxNjBVHY7GwPFBGuxwRoM9gZZ-fWUUYFYjM1Uw@mail.gmail.com/
> 
> In the case of ieee802154_tx() yes the tx lock is held so it's like
> what the mentioned link says. The workqueue is an ordered workqueue
> and you either have the driver async xmit option (the preferred
> option) or the driver sync xmit callback on a per driver (implies per
> interface) basis.

When I reviewed and applied this I did indeed not realize the ordered 
workqueue making this unnecessary.

> I also don't see why there is currently a problem with the current
> non-atomic way.

For me this looks more like a wrapper that could avoid future problems 
for no cost. I would not mind if the patch stays. But you are right, its 
not strictly needed. Want me to back it out again?

regards
Stefan Schmidt
Alexander Aring June 5, 2024, 9:54 p.m. UTC | #5
Hi,

On Wed, Jun 5, 2024 at 4:02 PM Stefan Schmidt <stefan@datenfreihafen.org> wrote:
>
> Hello Jakub, Alex,
>
> On 04.06.24 15:52, Alexander Aring wrote:
> > Hi,
> >
> > On Mon, Jun 3, 2024 at 7:56 PM Jakub Kicinski <kuba@kernel.org> wrote:
> >>
> >> On Mon, 3 Jun 2024 11:33:28 +0200 Stefan Schmidt wrote:
> >>> Hello.
> >>>
> >>> On 31.05.24 10:07, Yunshui Jiang wrote:
> >>>> mac802154 devices update their dev->stats fields locklessly. Therefore
> >>>> these counters should be updated atomically. Adopt SMP safe DEV_STATS_INC()
> >>>> and DEV_STATS_ADD() to achieve this.
> >>>>
> >>>> Signed-off-by: Yunshui Jiang <jiangyunshui@kylinos.cn>
> >>>> ---
> >>>>    net/mac802154/tx.c | 8 ++++----
> >>>>    1 file changed, 4 insertions(+), 4 deletions(-)
> >>>>
> >>>> diff --git a/net/mac802154/tx.c b/net/mac802154/tx.c
> >>>> index 2a6f1ed763c9..6fbed5bb5c3e 100644
> >>>> --- a/net/mac802154/tx.c
> >>>> +++ b/net/mac802154/tx.c
> >>>> @@ -34,8 +34,8 @@ void ieee802154_xmit_sync_worker(struct work_struct *work)
> >>>>      if (res)
> >>>>              goto err_tx;
> >>>>
> >>>> -   dev->stats.tx_packets++;
> >>>> -   dev->stats.tx_bytes += skb->len;
> >>>> +   DEV_STATS_INC(dev, tx_packets);
> >>>> +   DEV_STATS_ADD(dev, tx_bytes, skb->len);
> >>>>
> >>>>      ieee802154_xmit_complete(&local->hw, skb, false);
> >>>>
> >>>> @@ -90,8 +90,8 @@ ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
> >>>>              if (ret)
> >>>>                      goto err_wake_netif_queue;
> >>>>
> >>>> -           dev->stats.tx_packets++;
> >>>> -           dev->stats.tx_bytes += len;
> >>>> +           DEV_STATS_INC(dev, tx_packets);
> >>>> +           DEV_STATS_ADD(dev, tx_bytes, len);
> >>>>      } else {
> >>>>              local->tx_skb = skb;
> >>>>              queue_work(local->workqueue, &local->sync_tx_work);
> >>>
> >>> This patch has been applied to the wpan tree and will be
> >>> part of the next pull request to net. Thanks!
> >>
> >> Hi! I haven't looked in detail, but FWIW
> >>
> >> $ git grep LLTX net/mac802154/
> >> $
> >>
> >> and similar patch from this author has been rejected:
> >>
> >> https://lore.kernel.org/all/CANn89iLPYoOjMxNjBVHY7GwPFBGuxwRoM9gZZ-fWUUYFYjM1Uw@mail.gmail.com/
> >
> > In the case of ieee802154_tx() yes the tx lock is held so it's like
> > what the mentioned link says. The workqueue is an ordered workqueue
> > and you either have the driver async xmit option (the preferred
> > option) or the driver sync xmit callback on a per driver (implies per
> > interface) basis.
>
> When I reviewed and applied this I did indeed not realize the ordered
> workqueue making this unnecessary.
>
> > I also don't see why there is currently a problem with the current
> > non-atomic way.
>
> For me this looks more like a wrapper that could avoid future problems
> for no cost. I would not mind if the patch stays. But you are right, its
> not strictly needed. Want me to back it out again?

it's fine for me. I think atomic ops, depending on $ARCH will mess up
your pipelines in some kind of way but it is better than using locks
of course (but locks are here required for other things).

- Alex
diff mbox series

Patch

diff --git a/net/mac802154/tx.c b/net/mac802154/tx.c
index 2a6f1ed763c9..6fbed5bb5c3e 100644
--- a/net/mac802154/tx.c
+++ b/net/mac802154/tx.c
@@ -34,8 +34,8 @@  void ieee802154_xmit_sync_worker(struct work_struct *work)
 	if (res)
 		goto err_tx;
 
-	dev->stats.tx_packets++;
-	dev->stats.tx_bytes += skb->len;
+	DEV_STATS_INC(dev, tx_packets);
+	DEV_STATS_ADD(dev, tx_bytes, skb->len);
 
 	ieee802154_xmit_complete(&local->hw, skb, false);
 
@@ -90,8 +90,8 @@  ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
 		if (ret)
 			goto err_wake_netif_queue;
 
-		dev->stats.tx_packets++;
-		dev->stats.tx_bytes += len;
+		DEV_STATS_INC(dev, tx_packets);
+		DEV_STATS_ADD(dev, tx_bytes, len);
 	} else {
 		local->tx_skb = skb;
 		queue_work(local->workqueue, &local->sync_tx_work);