tipc: call start and done ops directly in __tipc_nl_compat_dumpit()
Subsystems (3)
PATCH v3 1/1 tipc: call start and done ops directly in __tipc_nl_compat_dumpit()
__tipc_nl_compat_dumpit() uses a netlink_callback on stack, so the only way to align it with other ->dumpit() call path is calling tipc_dump_start() and tipc_dump_done() directly inside it. Otherwise ->dumpit() would always get NULL from cb->args[].
But tipc_dump_start() uses sock_net(cb->skb->sk) to retrieve net pointer, the cb->skb here doesn't set skb->sk, the net pointer is saved in msg->net instead, so introduce a helper function __tipc_dump_start() to pass in msg->net.
Ying pointed out cb->args[0...3] are already used by other callbacks on this call path, so we can't use cb->args[0] any more, use cb->args[4] instead.
---
v2: fix sock_net(cb->skb->sk)
v3: use cb->args[4] instead of cb->args[0]
net/tipc/netlink_compat.c | 2 ++ net/tipc/socket.c | 17 +++++++++++------ net/tipc/socket.h | 1 + 3 files changed, 14 insertions(+), 6 deletions(-)
net/tipc/netlink_compat.c +2 -0
@@ -185,6 +185,7 @@ static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd,return -ENOMEM ; buf->sk = msg->dst_sk; +__tipc_dump_start (&cb, msg->net); do { int rem;
@@ -216,6 +217,7 @@ static int __tipc_nl_compat_dumpit(struct tipc_nl_compat_cmd_dump *cmd, err =0 ; err_out: +tipc_dump_done (&cb);kfree_skb (buf);if (err == -EMSGSIZE ) {
net/tipc/socket.c +11 -6
@@ -3230,7 +3230,7 @@ int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,struct netlink_callback *cb, struct tipc_sock *tsk)) { - struct rhashtable_iter *iter = (void *)cb->args[0]; + struct rhashtable_iter *iter = (void *)cb->args[4];struct tipc_sock *tsk;int err;
@@ -3266,8 +3266,14 @@ EXPORT_SYMBOL(tipc_nl_sk_walk);int tipc_dump_start (struct netlink_callback *cb) { -struct rhashtable_iter *iter = (void *)cb->args[0 ]; -struct net *net =sock_net (cb->skb->sk); +return __tipc_dump_start (cb,sock_net (cb->skb->sk)); +} +EXPORT_SYMBOL (tipc_dump_start); + +int __tipc_dump_start (struct netlink_callback *cb,struct net *net) +{ +/* tipc_nl_name_table_dump() uses cb->args[0...3]. */ +struct rhashtable_iter *iter = (void *)cb->args[4 ];struct tipc_net *tn =tipc_net (net);if (!iter) {
@@ -3275,17 +3281,16 @@ int tipc_dump_start(struct netlink_callback *cb)if (!iter)return -ENOMEM ; - cb->args[0 ] = (long )iter; + cb->args[4 ] = (long )iter; }rhashtable_walk_enter (&tn->sk_rht, iter);return 0 ; } -EXPORT_SYMBOL (tipc_dump_start);int tipc_dump_done (struct netlink_callback *cb) { -struct rhashtable_iter *hti = (void *)cb->args[0 ]; +struct rhashtable_iter *hti = (void *)cb->args[4 ];rhashtable_walk_exit (hti);kfree (hti);
net/tipc/socket.h +1 -0
@@ -69,5 +69,6 @@ int tipc_nl_sk_walk(struct sk_buff *skb, struct netlink_callback *cb,struct netlink_callback *cb, struct tipc_sock *tsk));int tipc_dump_start (struct netlink_callback *cb); +int __tipc_dump_start (struct netlink_callback *cb,struct net *net);int tipc_dump_done (struct netlink_callback *cb);#endif
Signature · 1 line
2.14.4
Quoted 6 lines · Cong Wang
__tipc_nl_compat_dumpit()uses anetlink_callbackon stack, so the only way to align it with other ->dumpit() call path is callingtipc_dump_start()andtipc_dump_done()directly inside it. Otherwise ->dumpit() would always get NULL from cb->args[].
Quoted 8 lines
But
tipc_dump_start()usessock_net(cb->skb->sk)to retrieve net pointer, thecb->skbhere doesn't setskb->sk, the net pointer is saved inmsg->netinstead, so introduce a helper function__tipc_dump_start()to pass inmsg->net.Ying pointed out cb->args[0...3] are already used by other callbacks on this call path, so we can't use cb->args[0] any more, use cb->args[4] instead.
It's a common mechanism to save rhashtable iterator pointer in cb->args after tipc_dump_start() and tipc_dump_done() are introduced. Someday probably we will involve new dumpit function. In order to lower the risk that rhashtable iterator pointer saved is overwritten, it's better to use the last slot, ie, cb->args[5].
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Quoted 17 lines · Ying Xue
Quoted 6 lines · Cong Wang
__tipc_nl_compat_dumpit()uses anetlink_callbackon stack, so the only way to align it with other ->dumpit() call path is callingtipc_dump_start()andtipc_dump_done()directly inside it. Otherwise ->dumpit() would always get NULL from cb->args[].Quoted 8 lines
But
tipc_dump_start()usessock_net(cb->skb->sk)to retrieve net pointer, thecb->skbhere doesn't setskb->sk, the net pointer is saved inmsg->netinstead, so introduce a helper function__tipc_dump_start()to pass inmsg->net.Ying pointed out cb->args[0...3] are already used by other callbacks on this call path, so we can't use cb->args[0] any more, use cb->args[4] instead.
Quoted 5 lines
It's a common mechanism to save rhashtable iterator pointer in
cb->argsaftertipc_dump_start()andtipc_dump_done()are introduced. Someday probably we will involve new dumpit function. In order to lower the risk that rhashtable iterator pointer saved is overwritten, it's better to use the last slot, ie, cb->args[5].
I don't understand, currently only cb->args[0..3] are used at most, therefore cb->args[4] is pretty safe in current code base, there is no reason to be so defensive to use cb->args[5].
If you really worry about future, you probably want to extend cb->args from 6 to whatever larger, rather than just skipping cb->args[4].
I don't see any reason to do so.
Ying Xue
Acked-byQuoted 24 lines · Cong Wang
Quoted 17 lines · Ying Xue
Quoted 6 lines · Cong Wang
__tipc_nl_compat_dumpit()uses anetlink_callbackon stack, so the only way to align it with other ->dumpit() call path is callingtipc_dump_start()andtipc_dump_done()directly inside it. Otherwise ->dumpit() would always get NULL from cb->args[].Quoted 8 lines
But
tipc_dump_start()usessock_net(cb->skb->sk)to retrieve net pointer, thecb->skbhere doesn't setskb->sk, the net pointer is saved inmsg->netinstead, so introduce a helper function__tipc_dump_start()to pass inmsg->net.Ying pointed out cb->args[0...3] are already used by other callbacks on this call path, so we can't use cb->args[0] any more, use cb->args[4] instead.
Quoted 5 lines
It's a common mechanism to save rhashtable iterator pointer in
cb->argsaftertipc_dump_start()andtipc_dump_done()are introduced. Someday probably we will involve new dumpit function. In order to lower the risk that rhashtable iterator pointer saved is overwritten, it's better to use the last slot, ie, cb->args[5].
Quoted 4 lines
I don't understand, currently only cb->args[0..3] are used at most, therefore cb->args[4] is pretty safe in current code base, there is no reason to be so defensive to use cb->args[5].
Yes, at present cb->args[4] is safe.
Quoted 3 lines
If you really worry about future, you probably want to extend
cb->argsfrom 6 to whatever larger, rather than just skipping cb->args[4].
When we have to use the fifth slot of cb->args[] in the future, we need to skip sb->args[4], which is a bit wried. This is the reason why I suggested we could use the last one.
Quoted 1 line
I don't see any reason to do so.
As I said, the current version is safe. If you think it's unnecessary to change, it's okay to me.
Quoted 1 line
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
From: Cong Wang <[email protected]>
Date: Tue, 4 Sep 2018 14:54:55 -0700
Quoted 15 lines
__tipc_nl_compat_dumpit()uses anetlink_callbackon stack, so the only way to align it with other ->dumpit() call path is callingtipc_dump_start()andtipc_dump_done()directly inside it. Otherwise ->dumpit() would always get NULL from cb->args[].But
tipc_dump_start()usessock_net(cb->skb->sk)to retrieve net pointer, thecb->skbhere doesn't setskb->sk, the net pointer is saved inmsg->netinstead, so introduce a helper function__tipc_dump_start()to pass inmsg->net.Ying pointed out cb->args[0...3] are already used by other callbacks on this call path, so we can't use cb->args[0] any more, use cb->args[4] instead.
Quoted 5 lines
Fixes 9a07efa9aea2 ("tipc: switch to rhashtable iterator")Reported-and-tested-by [email protected]Cc Jon Maloy <[email protected]>Cc Ying Xue <[email protected]>Signed-off-by Cong Wang <[email protected]>
Applied, thanks Cong.