Skip to content

Commit 52016e2

Browse files
tomparkindavem330
authored andcommitted
l2tp: don't export tunnel and session free functions
Tunnel and session instances are reference counted, and shouldn't be directly freed by pseudowire code. Rather than exporting l2tp_tunnel_free and l2tp_session_free, make them private to l2tp_core.c, and export the refcount functions instead. In order to do this, the refcount functions cannot be declared as inline. Since the codepaths which take and drop tunnel and session references are not directly in the datapath this shouldn't cause performance issues. Signed-off-by: Tom Parkin <tparkin@katalix.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent b2aecfe commit 52016e2

2 files changed

Lines changed: 46 additions & 47 deletions

File tree

‎net/l2tp/l2tp_core.c‎

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,51 @@ l2tp_session_id_hash(struct l2tp_tunnel *tunnel, u32 session_id)
149149
return &tunnel->session_hlist[hash_32(session_id, L2TP_HASH_BITS)];
150150
}
151151

152-
void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
152+
static void l2tp_tunnel_free(struct l2tp_tunnel *tunnel)
153153
{
154154
sock_put(tunnel->sock);
155155
/* the tunnel is freed in the socket destructor */
156156
}
157-
EXPORT_SYMBOL(l2tp_tunnel_free);
157+
158+
static void l2tp_session_free(struct l2tp_session *session)
159+
{
160+
struct l2tp_tunnel *tunnel = session->tunnel;
161+
162+
if (tunnel) {
163+
if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
164+
goto out;
165+
l2tp_tunnel_dec_refcount(tunnel);
166+
}
167+
168+
out:
169+
kfree(session);
170+
}
171+
172+
void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
173+
{
174+
refcount_inc(&tunnel->ref_count);
175+
}
176+
EXPORT_SYMBOL_GPL(l2tp_tunnel_inc_refcount);
177+
178+
void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
179+
{
180+
if (refcount_dec_and_test(&tunnel->ref_count))
181+
l2tp_tunnel_free(tunnel);
182+
}
183+
EXPORT_SYMBOL_GPL(l2tp_tunnel_dec_refcount);
184+
185+
void l2tp_session_inc_refcount(struct l2tp_session *session)
186+
{
187+
refcount_inc(&session->ref_count);
188+
}
189+
EXPORT_SYMBOL_GPL(l2tp_session_inc_refcount);
190+
191+
void l2tp_session_dec_refcount(struct l2tp_session *session)
192+
{
193+
if (refcount_dec_and_test(&session->ref_count))
194+
l2tp_session_free(session);
195+
}
196+
EXPORT_SYMBOL_GPL(l2tp_session_dec_refcount);
158197

159198
/* Lookup a tunnel. A new reference is held on the returned tunnel. */
160199
struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
@@ -1581,23 +1620,6 @@ void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
15811620
}
15821621
EXPORT_SYMBOL_GPL(l2tp_tunnel_delete);
15831622

1584-
/* Really kill the session.
1585-
*/
1586-
void l2tp_session_free(struct l2tp_session *session)
1587-
{
1588-
struct l2tp_tunnel *tunnel = session->tunnel;
1589-
1590-
if (tunnel) {
1591-
if (WARN_ON(tunnel->magic != L2TP_TUNNEL_MAGIC))
1592-
goto out;
1593-
l2tp_tunnel_dec_refcount(tunnel);
1594-
}
1595-
1596-
out:
1597-
kfree(session);
1598-
}
1599-
EXPORT_SYMBOL_GPL(l2tp_session_free);
1600-
16011623
/* This function is used by the netlink SESSION_DELETE command and by
16021624
* pseudowire modules.
16031625
*/

‎net/l2tp/l2tp_core.h‎

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,16 @@ static inline void *l2tp_session_priv(struct l2tp_session *session)
175175
return &session->priv[0];
176176
}
177177

178+
void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel);
179+
void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel);
180+
void l2tp_session_inc_refcount(struct l2tp_session *session);
181+
void l2tp_session_dec_refcount(struct l2tp_session *session);
182+
178183
struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id);
179184
struct l2tp_tunnel *l2tp_tunnel_get_nth(const struct net *net, int nth);
180185
struct l2tp_session *l2tp_tunnel_get_session(struct l2tp_tunnel *tunnel,
181186
u32 session_id);
182187

183-
void l2tp_tunnel_free(struct l2tp_tunnel *tunnel);
184-
185188
struct l2tp_session *l2tp_session_get(const struct net *net, u32 session_id);
186189
struct l2tp_session *l2tp_session_get_nth(struct l2tp_tunnel *tunnel, int nth);
187190
struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
@@ -202,7 +205,6 @@ int l2tp_session_register(struct l2tp_session *session,
202205
struct l2tp_tunnel *tunnel);
203206

204207
int l2tp_session_delete(struct l2tp_session *session);
205-
void l2tp_session_free(struct l2tp_session *session);
206208
void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
207209
unsigned char *ptr, unsigned char *optr, u16 hdrflags,
208210
int length);
@@ -217,31 +219,6 @@ int l2tp_nl_register_ops(enum l2tp_pwtype pw_type,
217219
void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
218220
int l2tp_ioctl(struct sock *sk, int cmd, unsigned long arg);
219221

220-
static inline void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
221-
{
222-
refcount_inc(&tunnel->ref_count);
223-
}
224-
225-
static inline void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
226-
{
227-
if (refcount_dec_and_test(&tunnel->ref_count))
228-
l2tp_tunnel_free(tunnel);
229-
}
230-
231-
/* Session reference counts. Incremented when code obtains a reference
232-
* to a session.
233-
*/
234-
static inline void l2tp_session_inc_refcount(struct l2tp_session *session)
235-
{
236-
refcount_inc(&session->ref_count);
237-
}
238-
239-
static inline void l2tp_session_dec_refcount(struct l2tp_session *session)
240-
{
241-
if (refcount_dec_and_test(&session->ref_count))
242-
l2tp_session_free(session);
243-
}
244-
245222
static inline int l2tp_get_l2specific_len(struct l2tp_session *session)
246223
{
247224
switch (session->l2specific_type) {

0 commit comments

Comments
 (0)