1

l2tp: improve tunnel/session refcount helpers

l2tp_tunnel_inc_refcount and l2tp_session_inc_refcount wrap
refcount_inc. They add no value so just use the refcount APIs directly
and drop l2tp's helpers. l2tp already uses refcount_inc_not_zero
anyway.

Rename l2tp_tunnel_dec_refcount and l2tp_session_dec_refcount to
l2tp_tunnel_put and l2tp_session_put to better match their use pairing
various _get getters.

Signed-off-by: James Chapman <jchapman@katalix.com>
Signed-off-by: Tom Parkin <tparkin@katalix.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
James Chapman 2024-08-07 07:54:50 +01:00 committed by David S. Miller
parent 1f4c3dce91
commit abe7a1a7d0
8 changed files with 79 additions and 93 deletions

View File

@ -170,7 +170,7 @@ static void l2tp_session_free(struct l2tp_session *session)
{
trace_free_session(session);
if (session->tunnel)
l2tp_tunnel_dec_refcount(session->tunnel);
l2tp_tunnel_put(session->tunnel);
kfree_rcu(session, rcu);
}
@ -197,31 +197,19 @@ struct l2tp_tunnel *l2tp_sk_to_tunnel(const struct sock *sk)
}
EXPORT_SYMBOL_GPL(l2tp_sk_to_tunnel);
void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel)
{
refcount_inc(&tunnel->ref_count);
}
EXPORT_SYMBOL_GPL(l2tp_tunnel_inc_refcount);
void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel)
void l2tp_tunnel_put(struct l2tp_tunnel *tunnel)
{
if (refcount_dec_and_test(&tunnel->ref_count))
l2tp_tunnel_free(tunnel);
}
EXPORT_SYMBOL_GPL(l2tp_tunnel_dec_refcount);
EXPORT_SYMBOL_GPL(l2tp_tunnel_put);
void l2tp_session_inc_refcount(struct l2tp_session *session)
{
refcount_inc(&session->ref_count);
}
EXPORT_SYMBOL_GPL(l2tp_session_inc_refcount);
void l2tp_session_dec_refcount(struct l2tp_session *session)
void l2tp_session_put(struct l2tp_session *session)
{
if (refcount_dec_and_test(&session->ref_count))
l2tp_session_free(session);
}
EXPORT_SYMBOL_GPL(l2tp_session_dec_refcount);
EXPORT_SYMBOL_GPL(l2tp_session_put);
/* Lookup a tunnel. A new reference is held on the returned tunnel. */
struct l2tp_tunnel *l2tp_tunnel_get(const struct net *net, u32 tunnel_id)
@ -454,7 +442,7 @@ struct l2tp_session *l2tp_session_get_by_ifname(const struct net *net,
if (tunnel) {
list_for_each_entry_rcu(session, &tunnel->session_list, list) {
if (!strcmp(session->ifname, ifname)) {
l2tp_session_inc_refcount(session);
refcount_inc(&session->ref_count);
rcu_read_unlock_bh();
return session;
@ -471,7 +459,7 @@ EXPORT_SYMBOL_GPL(l2tp_session_get_by_ifname);
static void l2tp_session_coll_list_add(struct l2tp_session_coll_list *clist,
struct l2tp_session *session)
{
l2tp_session_inc_refcount(session);
refcount_inc(&session->ref_count);
WARN_ON_ONCE(session->coll_list);
session->coll_list = clist;
spin_lock(&clist->lock);
@ -557,7 +545,7 @@ static void l2tp_session_collision_del(struct l2tp_net *pn,
spin_unlock(&clist->lock);
if (refcount_dec_and_test(&clist->ref_count))
kfree(clist);
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
}
}
@ -606,7 +594,7 @@ int l2tp_session_register(struct l2tp_session *session,
goto out;
}
l2tp_tunnel_inc_refcount(tunnel);
refcount_inc(&tunnel->ref_count);
WRITE_ONCE(session->tunnel, tunnel);
list_add_rcu(&session->list, &tunnel->session_list);
@ -1089,7 +1077,7 @@ int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
if (!session || !session->recv_skb) {
if (session)
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
/* Not found? Pass to userspace to deal with */
goto pass;
@ -1103,12 +1091,12 @@ int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
if (version == L2TP_HDR_VER_3 &&
l2tp_v3_ensure_opt_in_linear(session, skb, &ptr, &optr)) {
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
goto invalid;
}
l2tp_recv_common(session, skb, ptr, optr, hdrflags, length);
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
return 0;
@ -1408,7 +1396,7 @@ static void l2tp_udp_encap_destroy(struct sock *sk)
tunnel = l2tp_sk_to_tunnel(sk);
if (tunnel) {
l2tp_tunnel_delete(tunnel);
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
}
}
@ -1443,10 +1431,10 @@ static void l2tp_tunnel_del_work(struct work_struct *work)
l2tp_tunnel_remove(tunnel->l2tp_net, tunnel);
/* drop initial ref */
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
/* drop workqueue ref */
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
}
/* Create a socket for the tunnel, if one isn't set up by
@ -1634,7 +1622,7 @@ static int l2tp_validate_socket(const struct sock *sk, const struct net *net,
tunnel = l2tp_sk_to_tunnel(sk);
if (tunnel) {
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
return -EBUSY;
}
@ -1726,7 +1714,7 @@ void l2tp_tunnel_delete(struct l2tp_tunnel *tunnel)
{
if (!test_and_set_bit(0, &tunnel->dead)) {
trace_delete_tunnel(tunnel);
l2tp_tunnel_inc_refcount(tunnel);
refcount_inc(&tunnel->ref_count);
queue_work(l2tp_wq, &tunnel->del_work);
}
}
@ -1736,7 +1724,7 @@ void l2tp_session_delete(struct l2tp_session *session)
{
if (!test_and_set_bit(0, &session->dead)) {
trace_delete_session(session);
l2tp_session_inc_refcount(session);
refcount_inc(&session->ref_count);
queue_work(l2tp_wq, &session->del_work);
}
}
@ -1754,10 +1742,10 @@ static void l2tp_session_del_work(struct work_struct *work)
(*session->session_close)(session);
/* drop initial ref */
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
/* drop workqueue ref */
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
}
/* We come here whenever a session's send_seq, cookie_len or

View File

@ -209,10 +209,8 @@ static inline void *l2tp_session_priv(struct l2tp_session *session)
}
/* Tunnel and session refcounts */
void l2tp_tunnel_inc_refcount(struct l2tp_tunnel *tunnel);
void l2tp_tunnel_dec_refcount(struct l2tp_tunnel *tunnel);
void l2tp_session_inc_refcount(struct l2tp_session *session);
void l2tp_session_dec_refcount(struct l2tp_session *session);
void l2tp_tunnel_put(struct l2tp_tunnel *tunnel);
void l2tp_session_put(struct l2tp_session *session);
/* Tunnel and session lookup.
* These functions take a reference on the instances they return, so

View File

@ -44,7 +44,7 @@ static void l2tp_dfs_next_tunnel(struct l2tp_dfs_seq_data *pd)
{
/* Drop reference taken during previous invocation */
if (pd->tunnel)
l2tp_tunnel_dec_refcount(pd->tunnel);
l2tp_tunnel_put(pd->tunnel);
pd->tunnel = l2tp_tunnel_get_next(pd->net, &pd->tkey);
pd->tkey++;
@ -54,7 +54,7 @@ static void l2tp_dfs_next_session(struct l2tp_dfs_seq_data *pd)
{
/* Drop reference taken during previous invocation */
if (pd->session)
l2tp_session_dec_refcount(pd->session);
l2tp_session_put(pd->session);
pd->session = l2tp_session_get_next(pd->net, pd->tunnel->sock,
pd->tunnel->version,
@ -111,11 +111,11 @@ static void l2tp_dfs_seq_stop(struct seq_file *p, void *v)
* or l2tp_dfs_next_tunnel().
*/
if (pd->session) {
l2tp_session_dec_refcount(pd->session);
l2tp_session_put(pd->session);
pd->session = NULL;
}
if (pd->tunnel) {
l2tp_tunnel_dec_refcount(pd->tunnel);
l2tp_tunnel_put(pd->tunnel);
pd->tunnel = NULL;
}
}

View File

@ -283,7 +283,7 @@ static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
spriv = l2tp_session_priv(session);
l2tp_session_inc_refcount(session);
refcount_inc(&session->ref_count);
rtnl_lock();
@ -301,7 +301,7 @@ static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
if (rc < 0) {
rtnl_unlock();
l2tp_session_delete(session);
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
free_netdev(dev);
return rc;
@ -312,17 +312,17 @@ static int l2tp_eth_create(struct net *net, struct l2tp_tunnel *tunnel,
rtnl_unlock();
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
__module_get(THIS_MODULE);
return 0;
err_sess_dev:
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
free_netdev(dev);
err_sess:
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
err:
return rc;
}

View File

@ -167,7 +167,7 @@ static int l2tp_ip_recv(struct sk_buff *skb)
goto discard_sess;
l2tp_recv_common(session, skb, ptr, optr, 0, skb->len);
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
return 0;
@ -200,7 +200,7 @@ pass_up:
return sk_receive_skb(sk, skb, 1);
discard_sess:
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
goto discard;
discard_put:
@ -265,7 +265,7 @@ static void l2tp_ip_destroy_sock(struct sock *sk)
tunnel = l2tp_sk_to_tunnel(sk);
if (tunnel) {
l2tp_tunnel_delete(tunnel);
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
}
}

View File

@ -177,7 +177,7 @@ static int l2tp_ip6_recv(struct sk_buff *skb)
goto discard_sess;
l2tp_recv_common(session, skb, ptr, optr, 0, skb->len);
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
return 0;
@ -210,7 +210,7 @@ pass_up:
return sk_receive_skb(sk, skb, 1);
discard_sess:
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
goto discard;
discard_put:
@ -276,7 +276,7 @@ static void l2tp_ip6_destroy_sock(struct sock *sk)
tunnel = l2tp_sk_to_tunnel(sk);
if (tunnel) {
l2tp_tunnel_delete(tunnel);
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
}
}

View File

@ -63,7 +63,7 @@ static struct l2tp_session *l2tp_nl_session_get(struct genl_info *info)
if (tunnel) {
session = l2tp_session_get(net, tunnel->sock, tunnel->version,
tunnel_id, session_id);
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
}
}
@ -242,7 +242,7 @@ static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info
if (ret < 0)
goto out;
l2tp_tunnel_inc_refcount(tunnel);
refcount_inc(&tunnel->ref_count);
ret = l2tp_tunnel_register(tunnel, net, &cfg);
if (ret < 0) {
kfree(tunnel);
@ -250,7 +250,7 @@ static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info
}
ret = l2tp_tunnel_notify(&l2tp_nl_family, info, tunnel,
L2TP_CMD_TUNNEL_CREATE);
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
out:
return ret;
@ -280,7 +280,7 @@ static int l2tp_nl_cmd_tunnel_delete(struct sk_buff *skb, struct genl_info *info
l2tp_tunnel_delete(tunnel);
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
out:
return ret;
@ -308,7 +308,7 @@ static int l2tp_nl_cmd_tunnel_modify(struct sk_buff *skb, struct genl_info *info
ret = l2tp_tunnel_notify(&l2tp_nl_family, info,
tunnel, L2TP_CMD_TUNNEL_MODIFY);
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
out:
return ret;
@ -479,12 +479,12 @@ static int l2tp_nl_cmd_tunnel_get(struct sk_buff *skb, struct genl_info *info)
if (ret < 0)
goto err_nlmsg_tunnel;
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
return genlmsg_unicast(net, msg, info->snd_portid);
err_nlmsg_tunnel:
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
err_nlmsg:
nlmsg_free(msg);
err:
@ -511,10 +511,10 @@ static int l2tp_nl_cmd_tunnel_dump(struct sk_buff *skb, struct netlink_callback
if (l2tp_nl_tunnel_send(skb, NETLINK_CB(cb->skb).portid,
cb->nlh->nlmsg_seq, NLM_F_MULTI,
tunnel, L2TP_CMD_TUNNEL_GET) < 0) {
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
goto out;
}
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
key++;
}
@ -647,12 +647,12 @@ static int l2tp_nl_cmd_session_create(struct sk_buff *skb, struct genl_info *inf
if (session) {
ret = l2tp_session_notify(&l2tp_nl_family, info, session,
L2TP_CMD_SESSION_CREATE);
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
}
}
out_tunnel:
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
out:
return ret;
}
@ -677,7 +677,7 @@ static int l2tp_nl_cmd_session_delete(struct sk_buff *skb, struct genl_info *inf
if (l2tp_nl_cmd_ops[pw_type] && l2tp_nl_cmd_ops[pw_type]->session_delete)
l2tp_nl_cmd_ops[pw_type]->session_delete(session);
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
out:
return ret;
@ -713,7 +713,7 @@ static int l2tp_nl_cmd_session_modify(struct sk_buff *skb, struct genl_info *inf
ret = l2tp_session_notify(&l2tp_nl_family, info,
session, L2TP_CMD_SESSION_MODIFY);
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
out:
return ret;
@ -824,14 +824,14 @@ static int l2tp_nl_cmd_session_get(struct sk_buff *skb, struct genl_info *info)
ret = genlmsg_unicast(genl_info_net(info), msg, info->snd_portid);
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
return ret;
err_ref_msg:
nlmsg_free(msg);
err_ref:
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
err:
return ret;
}
@ -856,7 +856,7 @@ static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback
tunnel->tunnel_id, &skey);
if (!session) {
tkey++;
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
tunnel = NULL;
skey = 0;
continue;
@ -865,11 +865,11 @@ static int l2tp_nl_cmd_session_dump(struct sk_buff *skb, struct netlink_callback
if (l2tp_nl_session_send(skb, NETLINK_CB(cb->skb).portid,
cb->nlh->nlmsg_seq, NLM_F_MULTI,
session, L2TP_CMD_SESSION_GET) < 0) {
l2tp_session_dec_refcount(session);
l2tp_tunnel_dec_refcount(tunnel);
l2tp_session_put(session);
l2tp_tunnel_put(tunnel);
break;
}
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
skey++;
}

View File

@ -313,12 +313,12 @@ static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
l2tp_xmit_skb(session, skb);
local_bh_enable();
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
return total_len;
error_put_sess:
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
error:
return error;
}
@ -372,12 +372,12 @@ static int pppol2tp_xmit(struct ppp_channel *chan, struct sk_buff *skb)
l2tp_xmit_skb(session, skb);
local_bh_enable();
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
return 1;
abort_put_sess:
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
abort:
/* Free the original skb */
kfree_skb(skb);
@ -413,7 +413,7 @@ static void pppol2tp_session_close(struct l2tp_session *session)
sock_put(ps->__sk);
/* drop ref taken when we referenced socket via sk_user_data */
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
}
}
@ -444,7 +444,7 @@ static int pppol2tp_release(struct socket *sock)
if (session) {
l2tp_session_delete(session);
/* drop ref taken by pppol2tp_sock_to_session */
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
}
release_sock(sk);
@ -668,7 +668,7 @@ static struct l2tp_tunnel *pppol2tp_tunnel_get(struct net *net,
if (error < 0)
return ERR_PTR(error);
l2tp_tunnel_inc_refcount(tunnel);
refcount_inc(&tunnel->ref_count);
error = l2tp_tunnel_register(tunnel, net, &tcfg);
if (error < 0) {
kfree(tunnel);
@ -684,7 +684,7 @@ static struct l2tp_tunnel *pppol2tp_tunnel_get(struct net *net,
/* Error if socket is not prepped */
if (!tunnel->sock) {
l2tp_tunnel_dec_refcount(tunnel);
l2tp_tunnel_put(tunnel);
return ERR_PTR(-ENOENT);
}
}
@ -774,13 +774,13 @@ static int pppol2tp_connect(struct socket *sock, struct sockaddr *uservaddr,
pppol2tp_session_init(session);
ps = l2tp_session_priv(session);
l2tp_session_inc_refcount(session);
refcount_inc(&session->ref_count);
mutex_lock(&ps->sk_lock);
error = l2tp_session_register(session, tunnel);
if (error < 0) {
mutex_unlock(&ps->sk_lock);
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
goto end;
}
@ -836,8 +836,8 @@ end:
l2tp_tunnel_delete(tunnel);
}
if (drop_refcnt)
l2tp_session_dec_refcount(session);
l2tp_tunnel_dec_refcount(tunnel);
l2tp_session_put(session);
l2tp_tunnel_put(tunnel);
release_sock(sk);
return error;
@ -877,7 +877,7 @@ static int pppol2tp_session_create(struct net *net, struct l2tp_tunnel *tunnel,
return 0;
err_sess:
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
err:
return error;
}
@ -988,7 +988,7 @@ static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
error = len;
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
end:
return error;
}
@ -1038,12 +1038,12 @@ static int pppol2tp_tunnel_copy_stats(struct pppol2tp_ioc_stats *stats,
return -EBADR;
if (session->pwtype != L2TP_PWTYPE_PPP) {
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
return -EBADR;
}
pppol2tp_copy_stats(stats, &session->stats);
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
return 0;
}
@ -1261,7 +1261,7 @@ static int pppol2tp_setsockopt(struct socket *sock, int level, int optname,
err = pppol2tp_session_setsockopt(sk, session, optname, val);
}
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
end:
return err;
}
@ -1382,7 +1382,7 @@ static int pppol2tp_getsockopt(struct socket *sock, int level, int optname,
err = 0;
end_put_sess:
l2tp_session_dec_refcount(session);
l2tp_session_put(session);
end:
return err;
}
@ -1407,7 +1407,7 @@ static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
{
/* Drop reference taken during previous invocation */
if (pd->tunnel)
l2tp_tunnel_dec_refcount(pd->tunnel);
l2tp_tunnel_put(pd->tunnel);
for (;;) {
pd->tunnel = l2tp_tunnel_get_next(net, &pd->tkey);
@ -1417,7 +1417,7 @@ static void pppol2tp_next_tunnel(struct net *net, struct pppol2tp_seq_data *pd)
if (!pd->tunnel || pd->tunnel->version == 2)
return;
l2tp_tunnel_dec_refcount(pd->tunnel);
l2tp_tunnel_put(pd->tunnel);
}
}
@ -1425,7 +1425,7 @@ static void pppol2tp_next_session(struct net *net, struct pppol2tp_seq_data *pd)
{
/* Drop reference taken during previous invocation */
if (pd->session)
l2tp_session_dec_refcount(pd->session);
l2tp_session_put(pd->session);
pd->session = l2tp_session_get_next(net, pd->tunnel->sock,
pd->tunnel->version,
@ -1485,11 +1485,11 @@ static void pppol2tp_seq_stop(struct seq_file *p, void *v)
* or pppol2tp_next_tunnel().
*/
if (pd->session) {
l2tp_session_dec_refcount(pd->session);
l2tp_session_put(pd->session);
pd->session = NULL;
}
if (pd->tunnel) {
l2tp_tunnel_dec_refcount(pd->tunnel);
l2tp_tunnel_put(pd->tunnel);
pd->tunnel = NULL;
}
}