From e67a0f6a573f0ec5c3a5aa4ef3c05b3e7d4e7f7d Mon Sep 17 00:00:00 2001 From: Geliang Tang Date: Wed, 1 Nov 2023 12:38:24 +0800 Subject: [PATCH] mptcp: add netlink pm addr entry refcount This patch adds netlink PM address entry refcount. Init 'refcont' of every address entry to 1.. Increase this refcount counter when a subflow connecting or an address signaling in mptcp_pm_create_subflow_or_signal_addr() and fill_local_addresses_vec(). Decrease it in __mptcp_pm_release_addr_entry(). When the counter reaches 1, then free this entry. Signed-off-by: Geliang Tang --- net/mptcp/pm_netlink.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/net/mptcp/pm_netlink.c b/net/mptcp/pm_netlink.c index e5deb05293c6ed..74dd2613775d70 100644 --- a/net/mptcp/pm_netlink.c +++ b/net/mptcp/pm_netlink.c @@ -603,8 +603,10 @@ static void mptcp_pm_create_subflow_or_signal_addr(struct mptcp_sock *msk) continue; spin_unlock_bh(&msk->pm.lock); - for (i = 0; i < nr; i++) - __mptcp_subflow_connect(sk, &local->addr, &addrs[i]); + for (i = 0; i < nr; i++) { + if (refcount_inc_not_zero(&local->refcnt)) + __mptcp_subflow_connect(sk, &local->addr, &addrs[i]); + } spin_lock_bh(&msk->pm.lock); } mptcp_pm_nl_check_work_pending(msk); @@ -644,7 +646,8 @@ static unsigned int fill_local_addresses_vec(struct mptcp_sock *msk, if (!mptcp_pm_addr_families_match(sk, &entry->addr, remote)) continue; - if (msk->pm.subflows < subflows_max) { + if (msk->pm.subflows < subflows_max && + refcount_inc_not_zero(&entry->refcnt)) { msk->pm.subflows++; addrs[i++] = entry->addr; } @@ -895,9 +898,11 @@ static bool address_use_port(struct mptcp_pm_addr_entry *entry) /* caller must ensure the RCU grace period is already elapsed */ static void __mptcp_pm_release_addr_entry(struct mptcp_pm_addr_entry *entry) { - if (entry->lsk) - sock_release(entry->lsk); - kfree(entry); + if (!refcount_dec_not_one(&entry->refcnt)) { + if (entry->lsk) + sock_release(entry->lsk); + kfree(entry); + } } static int mptcp_pm_nl_append_new_local_addr(struct pm_nl_pernet *pernet, @@ -1087,6 +1092,7 @@ int mptcp_pm_nl_get_local_id(struct mptcp_sock *msk, struct mptcp_addr_info *skc entry->ifindex = 0; entry->flags = MPTCP_PM_ADDR_FLAG_IMPLICIT; entry->lsk = NULL; + refcount_set(&entry->refcnt, 1); ret = mptcp_pm_nl_append_new_local_addr(pernet, entry); if (ret < 0) kfree(entry); @@ -1314,6 +1320,7 @@ int mptcp_pm_nl_add_addr_doit(struct sk_buff *skb, struct genl_info *info) } *entry = addr; + refcount_set(&entry->refcnt, 1); if (entry->addr.port) { ret = mptcp_pm_nl_create_listen_socket(skb->sk, entry); if (ret) {