Skip to content

Commit 5a5db1a

Browse files
Geliang Tangintel-lab-lkp
authored andcommitted
mptcp: reuse sending nlmsg code in get_addr
The netlink messages are sent both in mptcp_pm_nl_get_addr() and mptcp_userspace_pm_get_addr(), this makes the code somewhat repetitive. This is because the netlink PM and userspace PM use different locks to protect the address entry that needs to be sent via the netlink message. The former uses rcu read lock, and the latter uses msk->pm.lock. The current get_addr() flow looks like this: lock(); entry = get_entry(); send_nlmsg(entry); unlock(); After holding the lock, get the entry from the list, send the entry, and finally release the lock. This patch changes the process by getting the entry while holding the lock, then making a copy of the entry so that the lock can be released. Finally, the copy of the entry is sent without locking: lock(); entry = get_entry(); *copy = *entry; unlock(); send_nlmsg(copy); This way we can reuse the send_nlmsg() code in get_addr() interfaces between the netlink PM and userspace PM. They only need to implement their own get_addr() interfaces to hold the different locks, get the entry from the different lists, then release the locks. Signed-off-by: Geliang Tang <[email protected]>
1 parent 0d3d96d commit 5a5db1a

File tree

4 files changed

+50
-75
lines changed

4 files changed

+50
-75
lines changed

net/mptcp/pm.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -434,24 +434,55 @@ bool mptcp_pm_is_backup(struct mptcp_sock *msk, struct sock_common *skc)
434434
return mptcp_pm_nl_is_backup(msk, &skc_local);
435435
}
436436

437-
static int mptcp_pm_get_addr(u8 id, struct genl_info *info)
437+
static int mptcp_pm_get_addr(u8 id, struct mptcp_pm_addr_entry *addr,
438+
struct genl_info *info)
438439
{
439440
if (info->attrs[MPTCP_PM_ATTR_TOKEN])
440-
return mptcp_userspace_pm_get_addr(id, info);
441-
return mptcp_pm_nl_get_addr(id, info);
441+
return mptcp_userspace_pm_get_addr(id, addr, info);
442+
return mptcp_pm_nl_get_addr(id, addr, info);
442443
}
443444

444445
int mptcp_pm_nl_get_addr_doit(struct sk_buff *skb, struct genl_info *info)
445446
{
446447
struct nlattr *attr = info->attrs[MPTCP_PM_ENDPOINT_ADDR];
447448
struct mptcp_pm_addr_entry addr;
449+
struct sk_buff *msg;
450+
void *reply;
448451
int ret;
449452

450453
ret = mptcp_pm_parse_entry(attr, info, false, &addr);
451454
if (ret < 0)
452455
return ret;
453456

454-
return mptcp_pm_get_addr(addr.addr.id, info);
457+
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
458+
if (!msg)
459+
return -ENOMEM;
460+
461+
reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
462+
info->genlhdr->cmd);
463+
if (!reply) {
464+
GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
465+
ret = -EMSGSIZE;
466+
goto fail;
467+
}
468+
469+
ret = mptcp_pm_get_addr(addr.addr.id, &addr, info);
470+
if (ret) {
471+
GENL_SET_ERR_MSG(info, "address not found");
472+
goto fail;
473+
}
474+
475+
ret = mptcp_nl_fill_addr(msg, &addr);
476+
if (ret)
477+
goto fail;
478+
479+
genlmsg_end(msg, reply);
480+
ret = genlmsg_reply(msg, info);
481+
return ret;
482+
483+
fail:
484+
nlmsg_free(msg);
485+
return ret;
455486
}
456487

457488
static int mptcp_pm_dump_addr(struct sk_buff *msg, struct netlink_callback *cb)

net/mptcp/pm_netlink.c

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,48 +1762,21 @@ int mptcp_nl_fill_addr(struct sk_buff *skb,
17621762
return -EMSGSIZE;
17631763
}
17641764

1765-
int mptcp_pm_nl_get_addr(u8 id, struct genl_info *info)
1765+
int mptcp_pm_nl_get_addr(u8 id, struct mptcp_pm_addr_entry *addr,
1766+
struct genl_info *info)
17661767
{
17671768
struct pm_nl_pernet *pernet = genl_info_pm_nl(info);
17681769
struct mptcp_pm_addr_entry *entry;
1769-
struct sk_buff *msg;
1770-
void *reply;
1771-
int ret;
1772-
1773-
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1774-
if (!msg)
1775-
return -ENOMEM;
1776-
1777-
reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
1778-
info->genlhdr->cmd);
1779-
if (!reply) {
1780-
GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
1781-
ret = -EMSGSIZE;
1782-
goto fail;
1783-
}
1770+
int ret = -EINVAL;
17841771

17851772
rcu_read_lock();
17861773
entry = __lookup_addr_by_id(pernet, id);
1787-
if (!entry) {
1788-
GENL_SET_ERR_MSG(info, "address not found");
1789-
ret = -EINVAL;
1790-
goto unlock_fail;
1774+
if (entry) {
1775+
*addr = *entry;
1776+
ret = 0;
17911777
}
1792-
1793-
ret = mptcp_nl_fill_addr(msg, entry);
1794-
if (ret)
1795-
goto unlock_fail;
1796-
1797-
genlmsg_end(msg, reply);
1798-
ret = genlmsg_reply(msg, info);
1799-
rcu_read_unlock();
1800-
return ret;
1801-
1802-
unlock_fail:
18031778
rcu_read_unlock();
18041779

1805-
fail:
1806-
nlmsg_free(msg);
18071780
return ret;
18081781
}
18091782

net/mptcp/pm_userspace.c

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -663,61 +663,30 @@ int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,
663663
return ret;
664664
}
665665

666-
int mptcp_userspace_pm_get_addr(u8 id, struct genl_info *info)
666+
int mptcp_userspace_pm_get_addr(u8 id, struct mptcp_pm_addr_entry *addr,
667+
struct genl_info *info)
667668
{
668669
struct mptcp_pm_addr_entry *entry;
669670
struct mptcp_sock *msk;
670-
struct sk_buff *msg;
671671
int ret = -EINVAL;
672672
struct sock *sk;
673-
void *reply;
674673

675674
msk = mptcp_userspace_pm_get_sock(info);
676675
if (!msk)
677676
return ret;
678677

679678
sk = (struct sock *)msk;
680679

681-
msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
682-
if (!msg) {
683-
ret = -ENOMEM;
684-
goto out;
685-
}
686-
687-
reply = genlmsg_put_reply(msg, info, &mptcp_genl_family, 0,
688-
info->genlhdr->cmd);
689-
if (!reply) {
690-
GENL_SET_ERR_MSG(info, "not enough space in Netlink message");
691-
ret = -EMSGSIZE;
692-
goto fail;
693-
}
694-
695680
lock_sock(sk);
696681
spin_lock_bh(&msk->pm.lock);
697682
entry = mptcp_userspace_pm_lookup_addr_by_id(msk, id);
698-
if (!entry) {
699-
GENL_SET_ERR_MSG(info, "address not found");
700-
ret = -EINVAL;
701-
goto unlock_fail;
683+
if (entry) {
684+
*addr = *entry;
685+
ret = 0;
702686
}
703-
704-
ret = mptcp_nl_fill_addr(msg, entry);
705-
if (ret)
706-
goto unlock_fail;
707-
708-
genlmsg_end(msg, reply);
709-
ret = genlmsg_reply(msg, info);
710687
spin_unlock_bh(&msk->pm.lock);
711688
release_sock(sk);
712-
sock_put(sk);
713-
return ret;
714689

715-
unlock_fail:
716-
spin_unlock_bh(&msk->pm.lock);
717-
release_sock(sk);
718-
fail:
719-
nlmsg_free(msg);
720-
out:
721690
sock_put(sk);
722691
return ret;
723692
}

net/mptcp/protocol.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,8 +1131,10 @@ int mptcp_pm_nl_dump_addr(struct sk_buff *msg,
11311131
struct netlink_callback *cb);
11321132
int mptcp_userspace_pm_dump_addr(struct sk_buff *msg,
11331133
struct netlink_callback *cb);
1134-
int mptcp_pm_nl_get_addr(u8 id, struct genl_info *info);
1135-
int mptcp_userspace_pm_get_addr(u8 id, struct genl_info *info);
1134+
int mptcp_pm_nl_get_addr(u8 id, struct mptcp_pm_addr_entry *addr,
1135+
struct genl_info *info);
1136+
int mptcp_userspace_pm_get_addr(u8 id, struct mptcp_pm_addr_entry *addr,
1137+
struct genl_info *info);
11361138

11371139
static inline u8 subflow_get_local_id(const struct mptcp_subflow_context *subflow)
11381140
{

0 commit comments

Comments
 (0)