net/sched: tc_dump_qdisc() optimizations
Subsystems (3)
v3 cover net/sched: tc_dump_qdisc() optimizations
Before converting tc_dump_qdisc() to RCU, we make the following changes:
- Use for_each_netdev_dump() instead of for_each_netdev()
- Only dump qdiscs of a single device at user space request.
v2 and v3: addressed Jakub feedback (thanks!)
Eric Dumazet (4):
net/sched: propagate tc_fill_tclass() error net/sched: tc_dump_qdisc_root() refactor
net/sched: switch tc_dump_qdisc() to for_each_netdev_dump() net/sched: speedup tc_dump_qdisc() when tcm_ifindex is provided
net/sched/sch_api.c | 127 +++++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 60 deletions(-)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
PATCH v3 1/4 net/sched: propagate tc_fill_tclass() error
Change tc_fill_tclass() to return -EMSGSIZE when skb is too small.
Change its caller to propagate this error (instead of -EINVAL)
--- net/sched/sch_api.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-)
net/sched/sch_api.c +17 -14
@@ -1987,15 +1987,16 @@ static int tc_fill_tclass(struct sk_buff *skb, struct Qdisc *q, out_nlmsg_trim: nla_put_failure:nlmsg_trim (skb, b); -return -1 ; +return -EMSGSIZE ; }static int tclass_notify (struct net *net,struct sk_buff *oskb,struct nlmsghdr *n,struct Qdisc *q,unsigned long cl,int event,struct netlink_ext_ack *extack) { -struct sk_buff *skb;u32 portid = oskb ?NETLINK_CB (oskb).portid :0 ; +struct sk_buff *skb; +int ret;if (!rtnl_notify_needed (net, n->nlmsg_flags,RTNLGRP_TC ))return 0 ;
@@ -2004,9 +2005,10 @@ static int tclass_notify(struct net *net, struct sk_buff *oskb,if (!skb)return -ENOBUFS ; -if (tc_fill_tclass (skb, q, cl, portid, n->nlmsg_seq,0 , event, extack) <0 ) { + ret =tc_fill_tclass (skb, q, cl, portid, n->nlmsg_seq,0 , event, extack); +if (ret <0 ) {kfree_skb (skb); -return -EINVAL ; +return ret; } return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
@@ -2017,17 +2019,19 @@ static int tclass_get_notify(struct net *net, struct sk_buff *oskb, struct nlmsghdr *n, struct Qdisc *q, unsigned long cl, struct netlink_ext_ack *extack) { - struct sk_buff *skb; u32 portid = oskb ? NETLINK_CB(oskb).portid : 0; +struct sk_buff *skb; +int ret; skb =alloc_skb (NLMSG_GOODSIZE ,GFP_KERNEL );if (!skb)return -ENOBUFS ; -if (tc_fill_tclass (skb, q, cl, portid, n->nlmsg_seq,0 ,RTM_NEWTCLASS , - extack) <0 ) { + ret =tc_fill_tclass (skb, q, cl, portid, n->nlmsg_seq,0 , +RTM_NEWTCLASS , extack); +if (ret <0 ) {kfree_skb (skb); -return -EINVAL ; +return ret; } return rtnetlink_send(skb, net, portid, RTNLGRP_TC,
@@ -2041,7 +2045,7 @@ static int tclass_del_notify(struct net *net, struct netlink_ext_ack *extack) {u32 portid = oskb ?NETLINK_CB (oskb).portid :0 ; -struct sk_buff *skb; +struct sk_buff *skb =NULL ;int err =0 ;if (!cops->delete)
@@ -2052,13 +2056,12 @@ static int tclass_del_notify(struct net *net,if (!skb)return -ENOBUFS ; -if (tc_fill_tclass (skb, q, cl, portid, n->nlmsg_seq,0 , -RTM_DELTCLASS , extack) <0 ) { + err =tc_fill_tclass (skb, q, cl, portid, n->nlmsg_seq,0 , +RTM_DELTCLASS , extack); +if (err <0 ) {kfree_skb (skb); -return -EINVAL ; +return err; } - }else { - skb =NULL ; } err = cops->delete (q, cl, extack);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
PATCH v3 2/4 net/sched: tc_dump_qdisc_root() refactor
In reply to Eric DumazetChange tc_fill_qdisc() to return -EMSGSIZE when skb is too small.
Change tc_dump_qdisc_root() to propagate tc_fill_qdisc() error to its callers.
--- net/sched/sch_api.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-)
net/sched/sch_api.c +15 -14
@@ -976,7 +976,7 @@ static int tc_fill_qdisc(struct sk_buff *skb, struct Qdisc *q, u32 clid, out_nlmsg_trim: nla_put_failure:nlmsg_trim (skb, b); -return -1 ; +return -EMSGSIZE ; }static bool tc_qdisc_dump_ignore (struct Qdisc *q,bool dump_invisible)
@@ -1836,11 +1836,13 @@ static int tc_dump_qdisc_root(struct Qdisc *root, struct sk_buff *skb,if (q_idx < s_q_idx) { q_idx++; }else { -if (!tc_qdisc_dump_ignore (q, dump_invisible) && -tc_fill_qdisc (skb, q, q->parent,NETLINK_CB (cb->skb).portid, - cb->nlh->nlmsg_seq,NLM_F_MULTI , -RTM_NEWQDISC ,NULL ) <=0 ) - goto done; +if (!tc_qdisc_dump_ignore (q, dump_invisible)) + ret =tc_fill_qdisc (skb, q, q->parent, +NETLINK_CB (cb->skb).portid, + cb->nlh->nlmsg_seq,NLM_F_MULTI , +RTM_NEWQDISC ,NULL ); +if (ret <0 ) + goto out; q_idx++; }
@@ -1858,20 +1860,19 @@ static int tc_dump_qdisc_root(struct Qdisc *root, struct sk_buff *skb, q_idx++;continue ; } -if (!tc_qdisc_dump_ignore (q, dump_invisible) && -tc_fill_qdisc (skb, q, q->parent,NETLINK_CB (cb->skb).portid, - cb->nlh->nlmsg_seq,NLM_F_MULTI , -RTM_NEWQDISC ,NULL ) <=0 ) - goto done; +if (!tc_qdisc_dump_ignore (q, dump_invisible)) + ret =tc_fill_qdisc (skb, q, q->parent, +NETLINK_CB (cb->skb).portid, + cb->nlh->nlmsg_seq,NLM_F_MULTI , +RTM_NEWQDISC ,NULL ); +if (ret <0 ) + goto out; q_idx++; } out: *q_idx_p = q_idx;return ret; -done: - ret =-1 ; - goto out; }static int tc_dump_qdisc (struct sk_buff *skb,struct netlink_callback *cb)
Signature · 1 line
2.54.0.545.g6539524ca2-goog
PATCH v3 3/4 net/sched: switch tc_dump_qdisc() to for_each_netdev_dump()
In reply to Eric DumazetUse for_each_netdev_dump() instead of for_each_netdev().
This is more scalable, and will ease RCU conversion.
This also offer better behavior when other threads are adding or deleting netevices concurrently.
This enables dumping qdiscs for a single device
at user space request in the following patch.
---
v3: removed s_ifindex (Jakub)
net/sched/sch_api.c | 60 +++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 32 deletions(-)
net/sched/sch_api.c +28 -32
@@ -1877,18 +1877,17 @@ static int tc_dump_qdisc_root(struct Qdisc *root, struct sk_buff *skb,static int tc_dump_qdisc (struct sk_buff *skb,struct netlink_callback *cb) { -struct net *net =sock_net (skb->sk); -int idx, q_idx; -int s_idx, s_q_idx; -struct net_device *dev;const struct nlmsghdr *nlh = cb->nlh; +struct net *net =sock_net (skb->sk);struct nlattr *tca[TCA_MAX +1 ]; +struct { +unsigned long ifindex; +int q_idx; + } *ctx = (void *)cb->ctx; +struct net_device *dev; +int s_q_idx, q_idx;int err; - s_idx = cb->args[0 ]; - s_q_idx = q_idx = cb->args[1 ]; - - idx =0 ;ASSERT_RTNL (); err = nlmsg_parse_deprecated(nlh, sizeof(struct tcmsg), tca, TCA_MAX,
@@ -1896,42 +1895,39 @@ static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)if (err <0 )return err; -for_each_netdev (net, dev) { + s_q_idx = ctx->q_idx; + +for_each_netdev_dump (net, dev, ctx->ifindex) {struct netdev_queue *dev_queue; +struct Qdisc *q; -if (idx < s_idx) - goto cont; -if (idx > s_idx) - s_q_idx =0 ; q_idx =0 ;netdev_lock_ops (dev); -if (tc_dump_qdisc_root (rtnl_dereference (dev->qdisc), - skb, cb, &q_idx, s_q_idx, - true, tca[TCA_DUMP_INVISIBLE ]) <0 ) { -netdev_unlock_ops (dev); - goto done; - } + q =rtnl_dereference (dev->qdisc); + err =tc_dump_qdisc_root (q, skb, cb, &q_idx, s_q_idx, + true, tca[TCA_DUMP_INVISIBLE ]); +if (err <0 ) + goto error_unlock; dev_queue =dev_ingress_queue (dev); -if (dev_queue && -tc_dump_qdisc_root (rtnl_dereference (dev_queue->qdisc_sleeping), - skb, cb, &q_idx, s_q_idx, false, - tca[TCA_DUMP_INVISIBLE ]) <0 ) { -netdev_unlock_ops (dev); - goto done; +if (dev_queue) { + q =rtnl_dereference (dev_queue->qdisc_sleeping); + err =tc_dump_qdisc_root (q, skb, cb, &q_idx, s_q_idx, + false, tca[TCA_DUMP_INVISIBLE ]); +if (err <0 ) + goto error_unlock; }netdev_unlock_ops (dev); - -cont: - idx++; + s_q_idx =0 ; } +return skb->len; -done: - cb->args[0 ] = idx; - cb->args[1 ] = q_idx; +error_unlock: +netdev_unlock_ops (dev); + ctx->q_idx = q_idx; -return skb->len; +return err; }
Signature · 1 line
2.54.0.545.g6539524ca2-goog
PATCH v3 4/4 net/sched: speedup tc_dump_qdisc() when tcm_ifindex is provided
In reply to Eric DumazetThere is no point dumping qdiscs for all devices when user space wants them for a single device:
tc -s -d qdisc show dev eth1
--- net/sched/sch_api.c | 7 +++++++ 1 file changed, 7 insertions(+)
net/sched/sch_api.c +7 -0
@@ -1884,6 +1884,7 @@ static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)unsigned long ifindex;int q_idx; } *ctx = (void *)cb->ctx; +const struct tcmsg *tcm;struct net_device *dev;int s_q_idx, q_idx;int err;
@@ -1894,6 +1895,9 @@ static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb) rtm_tca_policy, cb->extack);if (err <0 )return err; + tcm =nlmsg_data (nlh); +if (tcm->tcm_ifindex && !ctx->ifindex) + ctx->ifindex = tcm->tcm_ifindex; s_q_idx = ctx->q_idx;
@@ -1901,6 +1905,9 @@ static int tc_dump_qdisc(struct sk_buff *skb, struct netlink_callback *cb)struct netdev_queue *dev_queue;struct Qdisc *q; +if (tcm->tcm_ifindex && ctx->ifindex != tcm->tcm_ifindex) +break ; + q_idx =0 ;netdev_lock_ops (dev);
Signature · 1 line
2.54.0.545.g6539524ca2-goog
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Thu, 30 Apr 2026 02:36:24 +0000 you wrote:
Before converting
tc_dump_qdisc()to RCU, we make the following changes:- Use
for_each_netdev_dump()instead offor_each_netdev()- Only dump qdiscs of a single device at user space request.
v2 and v3: addressed Jakub feedback (thanks!)
[...]
Here is the summary with links:
- [v3,net-next,1/4] net/sched: propagate tc_fill_tclass() error
https://git.kernel.org/netdev/net-next/c/db6ac77b0433 - [v3,net-next,2/4] net/sched: tc_dump_qdisc_root() refactor https://git.kernel.org/netdev/net-next/c/304427709ab8 - [v3,net-next,3/4] net/sched: switch tc_dump_qdisc() to for_each_netdev_dump() https://git.kernel.org/netdev/net-next/c/748bbef5fc6a - [v3,net-next,4/4] net/sched: speedup tc_dump_qdisc() when tcm_ifindex is provided https://git.kernel.org/netdev/net-next/c/6f63b829632c
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html