Merge pull request #521 from ecsv/batadv-for-17.01
[feed/routing.git] / batman-adv / patches / 0015-batman-adv-Introduce-own-OGM2-buffer-mutex.patch
1 From: Sven Eckelmann <sven@narfation.org>
2 Date: Sun, 13 Oct 2019 21:03:06 +0200
3 Subject: batman-adv: Introduce own OGM2 buffer mutex
4
5 Only a single function is currently automatically locked by the rtnl_lock
6 because (unlike B.A.T.M.A.N. IV) the OGM2 buffer is independent of the hard
7 interfaces on which it will be transmitted. A private mutex can be used
8 instead to avoid unnecessary delays which would have been introduced by the
9 global lock.
10
11 Signed-off-by: Sven Eckelmann <sven@narfation.org>
12
13 Origin: upstream, https://git.open-mesh.org/batman-adv.git/commit/8069c581f9097f1f9398f2d49047a1dab8093821
14
15 diff --git a/net/batman-adv/bat_v_ogm.c b/net/batman-adv/bat_v_ogm.c
16 index f5feaa8c4fd228228fea519771e2c9e123b10345..a9240a0bedad109aba58e30038fe91a421ab4126 100644
17 --- a/net/batman-adv/bat_v_ogm.c
18 +++ b/net/batman-adv/bat_v_ogm.c
19 @@ -28,11 +28,12 @@
20 #include <linux/kernel.h>
21 #include <linux/kref.h>
22 #include <linux/list.h>
23 +#include <linux/lockdep.h>
24 +#include <linux/mutex.h>
25 #include <linux/netdevice.h>
26 #include <linux/random.h>
27 #include <linux/rculist.h>
28 #include <linux/rcupdate.h>
29 -#include <linux/rtnetlink.h>
30 #include <linux/skbuff.h>
31 #include <linux/slab.h>
32 #include <linux/stddef.h>
33 @@ -141,7 +142,7 @@ static void batadv_v_ogm_send_softif(struct batadv_priv *bat_priv)
34 u16 tvlv_len = 0;
35 int ret;
36
37 - ASSERT_RTNL();
38 + lockdep_assert_held(&bat_priv->bat_v.ogm_buff_mutex);
39
40 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
41 goto out;
42 @@ -242,11 +243,12 @@ static void batadv_v_ogm_send(struct work_struct *work)
43 struct batadv_priv_bat_v *bat_v;
44 struct batadv_priv *bat_priv;
45
46 - rtnl_lock();
47 bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
48 bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
49 +
50 + mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
51 batadv_v_ogm_send_softif(bat_priv);
52 - rtnl_unlock();
53 + mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
54 }
55
56 /**
57 @@ -275,13 +277,15 @@ void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
58 struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
59 struct batadv_ogm2_packet *ogm_packet;
60
61 - ASSERT_RTNL();
62 -
63 + mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
64 if (!bat_priv->bat_v.ogm_buff)
65 - return;
66 + goto unlock;
67
68 ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
69 ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
70 +
71 +unlock:
72 + mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
73 }
74
75 /**
76 @@ -886,8 +890,6 @@ int batadv_v_ogm_init(struct batadv_priv *bat_priv)
77 unsigned char *ogm_buff;
78 u32 random_seqno;
79
80 - ASSERT_RTNL();
81 -
82 bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
83 ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
84 if (!ogm_buff)
85 @@ -906,6 +908,8 @@ int batadv_v_ogm_init(struct batadv_priv *bat_priv)
86 atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
87 INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
88
89 + mutex_init(&bat_priv->bat_v.ogm_buff_mutex);
90 +
91 return 0;
92 }
93
94 @@ -917,7 +921,11 @@ void batadv_v_ogm_free(struct batadv_priv *bat_priv)
95 {
96 cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
97
98 + mutex_lock(&bat_priv->bat_v.ogm_buff_mutex);
99 +
100 kfree(bat_priv->bat_v.ogm_buff);
101 bat_priv->bat_v.ogm_buff = NULL;
102 bat_priv->bat_v.ogm_buff_len = 0;
103 +
104 + mutex_unlock(&bat_priv->bat_v.ogm_buff_mutex);
105 }
106 diff --git a/net/batman-adv/types.h b/net/batman-adv/types.h
107 index 8906e551c85991e6b085108cf6f2b734b48fc231..57df7c68dfee8d6bed72edbd044d95016a5dd75e 100644
108 --- a/net/batman-adv/types.h
109 +++ b/net/batman-adv/types.h
110 @@ -27,6 +27,7 @@
111 #include <linux/compiler.h>
112 #include <linux/if_ether.h>
113 #include <linux/kref.h>
114 +#include <linux/mutex.h>
115 #include <linux/netdevice.h>
116 #include <linux/netlink.h>
117 #include <linux/sched.h> /* for linux/wait.h */
118 @@ -984,15 +985,17 @@ struct batadv_softif_vlan {
119
120 /**
121 * struct batadv_priv_bat_v - B.A.T.M.A.N. V per soft-interface private data
122 - * @ogm_buff: buffer holding the OGM packet. rtnl protected
123 - * @ogm_buff_len: length of the OGM packet buffer. rtnl protected
124 + * @ogm_buff: buffer holding the OGM packet
125 + * @ogm_buff_len: length of the OGM packet buffer
126 * @ogm_seqno: OGM sequence number - used to identify each OGM
127 + * @ogm_buff_mutex: lock protecting ogm_buff and ogm_buff_len
128 * @ogm_wq: workqueue used to schedule OGM transmissions
129 */
130 struct batadv_priv_bat_v {
131 unsigned char *ogm_buff;
132 int ogm_buff_len;
133 atomic_t ogm_seqno;
134 + struct mutex ogm_buff_mutex;
135 struct delayed_work ogm_wq;
136 };
137