# \[PATCH\] can: bcm: delay release of struct bcm\_op after synchronize\_rcu

- Subsystems: CAN NETWORK LAYER, THE REST
- Maintainers: Oliver Hartkopp, Marc Kleine-Budde, Linus Torvalds
- Status: LANDED (d5f9023fa61e · 2021-06-19 21:47)
- Landed in mainline as d5f9023fa61e on 2021-06-19.
- List: netdev
- 3 messages, 3 authors
- Page: https://ratatoskr.run/netdev/2021/06/1420022/t

## \[PATCH\] can: bcm: delay release of struct bcm\_op after synchronize\_rcu

- From: Thadeu Lima de Souza Cascardo \<hidden\>
- Date: 2021-06-19 16:19:27 
- Message: https://ratatoskr.run/netdev/2021/06/1420022.md

```text
can_rx_register callbacks may be called concurrently to the call to
can_rx_unregister. The callbacks and callback data, though, are protected by
RCU and the struct sock reference count.

So the callback data is really attached to the life of sk, meaning that it
should be released on sk_destruct. However, bcm_remove_op calls tasklet_kill,
and RCU callbacks may be called under RCU softirq, so that cannot be used on
kernels before the introduction of HRTIMER_MODE_SOFT.

However, bcm_rx_handler is called under RCU protection, so after calling
can_rx_unregister, we may call synchronize_rcu in order to wait for any RCU
read-side critical sections to finish. That is, bcm_rx_handler won't be called
anymore for those ops. So, we only free them, after we do that synchronize_rcu.

Reported-by: <redacted>
Reported-by: Norbert Slusarek <redacted>
Signed-off-by: Thadeu Lima de Souza Cascardo <redacted>
---
 net/can/bcm.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/net/can/bcm.c b/net/can/bcm.c
index f3e4d9528fa3..c67916020e63 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -785,6 +785,7 @@ static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh,
 						  bcm_rx_handler, op);
 
 			list_del(&op->list);
+			synchronize_rcu();
 			bcm_remove_op(op);
 			return 1; /* done */
 		}
@@ -1533,6 +1534,11 @@ static int bcm_release(struct socket *sock)
 					  REGMASK(op->can_id),
 					  bcm_rx_handler, op);
 
+	}
+
+	synchronize_rcu();
+
+	list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
 		bcm_remove_op(op);
 	}
 
-- 
2.30.2

```

## Re: \[PATCH\] can: bcm: delay release of struct bcm\_op after synchronize\_rcu

- From: Oliver Hartkopp \<socketcan@hartkopp.net\>
- Date: 2021-06-19 16:29:01 
- Message: https://ratatoskr.run/netdev/2021/06/1420026.md

```text


On 19.06.21 18:18, Thadeu Lima de Souza Cascardo wrote:
> can_rx_register callbacks may be called concurrently to the call to
> can_rx_unregister. The callbacks and callback data, though, are protected by
> RCU and the struct sock reference count.
>
> So the callback data is really attached to the life of sk, meaning that it
> should be released on sk_destruct. However, bcm_remove_op calls tasklet_kill,
> and RCU callbacks may be called under RCU softirq, so that cannot be used on
> kernels before the introduction of HRTIMER_MODE_SOFT.
>
> However, bcm_rx_handler is called under RCU protection, so after calling
> can_rx_unregister, we may call synchronize_rcu in order to wait for any RCU
> read-side critical sections to finish. That is, bcm_rx_handler won't be called
> anymore for those ops. So, we only free them, after we do that synchronize_rcu.
>
> Reported-by: <redacted>
> Reported-by: Norbert Slusarek <redacted>
> Signed-off-by: Thadeu Lima de Souza Cascardo <redacted>

Fixes: ffd980f976e7 ("[CAN]: Add broadcast manager (bcm) protocol")
Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>

Thanks Norbert for reporting and Thadeu for working out the fix!

Best regards,
Oliver

> ---
>   net/can/bcm.c | 6 ++++++
>   1 file changed, 6 insertions(+)
>
> diff --git a/net/can/bcm.c b/net/can/bcm.c
> index f3e4d9528fa3..c67916020e63 100644
> --- a/net/can/bcm.c
> +++ b/net/can/bcm.c
> @@ -785,6 +785,7 @@ static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh,
>   						  bcm_rx_handler, op);
>   
>   			list_del(&op->list);
> +			synchronize_rcu();
>   			bcm_remove_op(op);
>   			return 1; /* done */
>   		}
> @@ -1533,6 +1534,11 @@ static int bcm_release(struct socket *sock)
>   					  REGMASK(op->can_id),
>   					  bcm_rx_handler, op);
>   
> +	}
> +
> +	synchronize_rcu();
> +
> +	list_for_each_entry_safe(op, next, &bo->rx_ops, list) {
>   		bcm_remove_op(op);
>   	}
>   
>
```

## Re: \[PATCH\] can: bcm: delay release of struct bcm\_op after synchronize\_rcu

- From: Marc Kleine-Budde \<mkl@pengutronix.de\>
- Date: 2021-06-19 20:12:55 
- Message: https://ratatoskr.run/netdev/2021/06/1420042.md

```text
On 19.06.2021 13:18:13, Thadeu Lima de Souza Cascardo wrote:
> can_rx_register callbacks may be called concurrently to the call to
> can_rx_unregister. The callbacks and callback data, though, are protected by
> RCU and the struct sock reference count.
> 
> So the callback data is really attached to the life of sk, meaning that it
> should be released on sk_destruct. However, bcm_remove_op calls tasklet_kill,
> and RCU callbacks may be called under RCU softirq, so that cannot be used on
> kernels before the introduction of HRTIMER_MODE_SOFT.
> 
> However, bcm_rx_handler is called under RCU protection, so after calling
> can_rx_unregister, we may call synchronize_rcu in order to wait for any RCU
> read-side critical sections to finish. That is, bcm_rx_handler won't be called
> anymore for those ops. So, we only free them, after we do that synchronize_rcu.
> 
> Reported-by: <redacted>
> Reported-by: Norbert Slusarek <redacted>
> Signed-off-by: Thadeu Lima de Souza Cascardo <redacted>

Added to linux-can/testing.

Thanks,
Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |
```
