net: shaper: fix various minor bugs
Subsystems (4)
Jakub Kicinski
maintainerv2 cover net: shaper: fix various minor bugs
Fix various minor bugs in the net shaper API.
First 2 patches deal with ordering issues around inserting and publishing new shapers. Shapers are inserted "tentatively" and marked valid only after HW op succeeded, this used to be slightly racy.
Only other patch of note is patch 8. We want to add a Netlink policy check on the handle ID. This necessitates patch 7.
The rest are simple and self-explanatory.
v2:
- drop the first 2 patches - the corresponding changes will be sent later to net-next.
v1: https://lore.kernel.org/[email protected]
Jakub Kicinski (10):
net: shaper: flip the polarity of the valid flag
net: shaper: fix trivial ordering issue in net_shaper_commit()
net: shaper: reject duplicate leaves in GROUP request
selftests: drv-net: add shaper test for duplicate leaves
net: shaper: set ret to -ENOMEM when genlmsg_new() fails in group_doit
net: shaper: fix undersized reply skb allocation in GROUP command
tools: ynl: add scope qualifier for definitions
net: shaper: reject handle IDs exceeding internal bit-width
net: shaper: enforce singleton NETDEV scope with id 0
net: shaper: reject QUEUE scope handle with missing id
Documentation/netlink/genetlink-c.yaml | 9 ++ Documentation/netlink/genetlink-legacy.yaml | 9 ++ Documentation/netlink/genetlink.yaml | 9 ++ Documentation/netlink/netlink-raw.yaml | 9 ++ Documentation/netlink/specs/net_shaper.yaml | 7 + net/shaper/shaper_nl_gen.h | 2 + net/shaper/shaper.c | 136 +++++++++++++----- net/shaper/shaper_nl_gen.c | 7 +- tools/net/ynl/pyynl/ynl_gen_c.py | 31 +++- tools/testing/selftests/drivers/net/shaper.py | 24 +++- 10 files changed, 200 insertions(+), 43 deletions(-)
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerPATCH v2 1/10 net: shaper: flip the polarity of the valid flag
The usual way of inserting entries which are not yet fully ready into XArray is to have a VALID flag. The shaper code has a NOT_VALID flag. Since XArray code does not let us create entries with marks already set - the creation of entries is currently not atomic.
Flip the polarity of the VALID flag. This closes the tiny race in net_shaper_pre_insert() of entries being created without the NOT_VALID flag.
--- net/shaper/shaper.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
net/shaper/shaper.c +17 -17
@@ -275,11 +275,13 @@ static void net_shaper_default_parent(const struct net_shaper_handle *handle, parent->id =0 ; } -/* -* MARK_0 is already in use due to XA_FLAGS_ALLOC, can't reuse such flag as -* it's cleared by xa_store(). +/* MARK_0 is already in use due to XA_FLAGS_ALLOC. The VALID mark is set on +* an entry only after the device-side configuration has completed +* successfully (see net_shaper_commit()). Lookups and dumps must filter on +* this mark to avoid exposing tentative entries inserted by +* net_shaper_pre_insert() while the driver call is still in flight. */ -#define NET_SHAPER_NOT_VALID XA_MARK_1 +#define NET_SHAPER_VALID XA_MARK_1static struct net_shaper * net_shaper_lookup(struct net_shaper_binding *binding,
@@ -289,8 +291,8 @@ net_shaper_lookup(struct net_shaper_binding *binding,struct net_shaper_hierarchy *hierarchy; hierarchy =net_shaper_hierarchy_rcu (binding); -if (!hierarchy ||xa_get_mark (&hierarchy->shapers, index, -NET_SHAPER_NOT_VALID )) +if (!hierarchy || !xa_get_mark (&hierarchy->shapers, index, +NET_SHAPER_VALID ))return NULL ;return xa_load (&hierarchy->shapers, index);
@@ -370,13 +372,10 @@ static int net_shaper_pre_insert(struct net_shaper_binding *binding, goto free_id; } -/* Mark 'tentative' shaper inside the hierarchy container. -* xa_set_mark is a no-op if the previous store fails. +/* Insert as 'tentative' (no VALID mark). The mark will be set by +* net_shaper_commit() once the driver-side configuration succeeds. */ -xa_lock (&hierarchy->shapers); - prev =__xa_store (&hierarchy->shapers, index, cur,GFP_KERNEL ); -__xa_set_mark (&hierarchy->shapers, index,NET_SHAPER_NOT_VALID ); -xa_unlock (&hierarchy->shapers); + prev =xa_store (&hierarchy->shapers, index, cur,GFP_KERNEL );if (xa_err (prev)) {NL_SET_ERR_MSG (extack,"Can't insert shaper into device store" );kfree_rcu (cur, rcu);
@@ -413,8 +412,7 @@ static void net_shaper_commit(struct net_shaper_binding *binding,/* Successful update: drop the tentative mark * and update the hierarchy container. */ -__xa_clear_mark (&hierarchy->shapers, index, -NET_SHAPER_NOT_VALID ); +__xa_set_mark (&hierarchy->shapers, index,NET_SHAPER_VALID ); *cur = shapers[i]; }xa_unlock (&hierarchy->shapers);
@@ -431,8 +429,9 @@ static void net_shaper_rollback(struct net_shaper_binding *binding)return ;xa_lock (&hierarchy->shapers); -xa_for_each_marked (&hierarchy->shapers, index, cur, -NET_SHAPER_NOT_VALID ) { +xa_for_each (&hierarchy->shapers, index, cur) { +if (xa_get_mark (&hierarchy->shapers, index,NET_SHAPER_VALID )) +continue ;__xa_erase (&hierarchy->shapers, index);kfree (cur); }
@@ -836,7 +835,8 @@ int net_shaper_nl_get_dumpit(struct sk_buff *skb, goto out_unlock;for (; (shaper =xa_find (&hierarchy->shapers, &ctx->start_index, -U32_MAX ,XA_PRESENT )); ctx->start_index++) { +U32_MAX ,NET_SHAPER_VALID )); + ctx->start_index++) { ret =net_shaper_fill_one (skb, binding, shaper, info);if (ret)break ;
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerPATCH v2 2/10 net: shaper: fix trivial ordering issue in net_shaper_commit()
In reply to Jakub KicinskiWe should update the entry before we mark it as valid.
--- net/shaper/shaper.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
net/shaper/shaper.c +10 -1
@@ -295,6 +295,10 @@ net_shaper_lookup(struct net_shaper_binding *binding, NET_SHAPER_VALID))return NULL ; +/* Pairs with smp_wmb() in net_shaper_commit(): if the entry is +* valid, its contents must be visible too. +*/ +smp_rmb ();return xa_load (&hierarchy->shapers, index); }
@@ -412,8 +416,9 @@ static void net_shaper_commit(struct net_shaper_binding *binding,/* Successful update: drop the tentative mark * and update the hierarchy container. */ -__xa_set_mark (&hierarchy->shapers, index,NET_SHAPER_VALID ); *cur = shapers[i]; +smp_wmb (); +__xa_set_mark (&hierarchy->shapers, index,NET_SHAPER_VALID ); }xa_unlock (&hierarchy->shapers); }
@@ -837,6 +842,10 @@ int net_shaper_nl_get_dumpit(struct sk_buff *skb,for (; (shaper =xa_find (&hierarchy->shapers, &ctx->start_index,U32_MAX ,NET_SHAPER_VALID )); ctx->start_index++) { +/* Pairs with smp_wmb() in net_shaper_commit(): the entry +* is marked VALID, so its contents must be visible too. +*/ +smp_rmb (); ret =net_shaper_fill_one (skb, binding, shaper, info);if (ret)break ;
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerPATCH v2 3/10 net: shaper: reject duplicate leaves in GROUP request
In reply to Jakub Kicinskinet_shaper_nl_group_doit() does not deduplicate NET_SHAPER_A_LEAVES entries. When userspace supplies the same leaf handle twice, the same old-parent pointer lands twice in old_nodes[]. The cleanup loop double frees the parent. Of course the same parent may still be in old_nodes[] twice if we are moving multiple of its leaves.
Note that this patch also implicitly fixes the fact that the i >= leaves_count path forgets to set ret.
--- net/shaper/shaper.c | 60 +++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 15 deletions(-)
net/shaper/shaper.c +45 -15
@@ -941,6 +941,46 @@ static int net_shaper_handle_cmp(const struct net_shaper_handle *a,return memcmp (a, b,sizeof (*a)); } +static int net_shaper_parse_leaves (struct net_shaper_binding *binding, +struct genl_info *info, +const struct net_shaper *node, +struct net_shaper *leaves, +int leaves_count) +{ +struct nlattr *attr; +int i, j, ret, rem; + + i =0 ; +nla_for_each_attr_type (attr,NET_SHAPER_A_LEAVES , +genlmsg_data (info->genlhdr), +genlmsg_len (info->genlhdr), rem) { +if (WARN_ON_ONCE (i >= leaves_count)) +return -EINVAL ; + + ret =net_shaper_parse_leaf (binding, attr, info, + node, &leaves[i]); +if (ret) +return ret; + +/* Reject duplicates */ +for (j =0 ; j < i; j++) { +if (net_shaper_handle_cmp (&leaves[i].handle, + &leaves[j].handle)) +continue ; + +NL_SET_ERR_MSG_ATTR_FMT (info->extack, attr, +"Duplicate leaf shaper %d:%d" , + leaves[i].handle.scope, + leaves[i].handle.id); +return -EINVAL ; + } + + i++; + } + +return 0 ; +} + static int net_shaper_parent_from_leaves(int leaves_count, const struct net_shaper *leaves, struct net_shaper *node,
@@ -1197,10 +1237,9 @@ int net_shaper_nl_group_doit(struct sk_buff *skb, struct genl_info *info)struct net_shaper **old_nodes, *leaves, node = {};struct net_shaper_hierarchy *hierarchy;struct net_shaper_binding *binding; -int i, ret, rem, leaves_count; +int i, ret, leaves_count;int old_nodes_count =0 ;struct sk_buff *msg; -struct nlattr *attr;if (GENL_REQ_ATTR_CHECK (info,NET_SHAPER_A_LEAVES ))return -EINVAL ;
@@ -1228,19 +1267,10 @@ int net_shaper_nl_group_doit(struct sk_buff *skb, struct genl_info *info)if (ret) goto free_leaves; - i =0 ; -nla_for_each_attr_type (attr,NET_SHAPER_A_LEAVES , -genlmsg_data (info->genlhdr), -genlmsg_len (info->genlhdr), rem) { -if (WARN_ON_ONCE (i >= leaves_count)) - goto free_leaves; - - ret =net_shaper_parse_leaf (binding, attr, info, - &node, &leaves[i]); -if (ret) - goto free_leaves; - i++; - } + ret =net_shaper_parse_leaves (binding, info, &node, + leaves, leaves_count); +if (ret) + goto free_leaves;/* Prepare the msg reply in advance, to avoid device operation * rollback on allocation failure.
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerPATCH v2 4/10 selftests: drv-net: add shaper test for duplicate leaves
In reply to Jakub KicinskiAdd test exercising duplicate leaves.
--- tools/testing/selftests/drivers/net/shaper.py | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-)
tools/testing/selftests/drivers/net/shaper.py +22 -2
@@ -1,7 +1,10 @@ #!/usr/bin/env python3 # SPDX-License-Identifier: GPL-2.0 -from lib.py import ksft_run, ksft_exit, ksft_eq, ksft_true, KsftSkipEx +import errno + +from lib.py import ksft_run, ksft_exit +from lib.py import ksft_eq, ksft_raises, ksft_true, KsftSkipEx from lib.py import EthtoolFamily, NetshaperFamily from lib.py import NetDrvEnv from lib.py import NlError
@@ -438,6 +441,21 @@ from lib.py import cmd nl_shaper.delete({'ifindex': cfg.ifindex, 'handle': {'scope': 'queue', 'id': i}}) +def dup_leaves(cfg, nl_shaper) -> None: + """ Ensure that the kernel rejects duplicate leaves. """ + if not cfg.groups: + raise KsftSkipEx("device does not support node scope") + + with ksft_raises(NlError) as cm: + nl_shaper.group({ + 'ifindex': cfg.ifindex, + 'leaves':[{'handle': {'scope': 'queue', 'id': 0}}, + {'handle': {'scope': 'queue', 'id': 0}}], + 'handle': {'scope':'node'}, + 'metric': 'bps', + 'bw-max': 10000}) + ksft_eq(cm.exception.error, errno.EINVAL) + def main() -> None: with NetDrvEnv(__file__, queue_count=4) as cfg: cfg.queues = False
@@ -453,7 +471,9 @@ from lib.py import cmd basic_groups, qgroups, delegation, - queue_update], args=(cfg, NetshaperFamily())) + dup_leaves, + queue_update], + args=(cfg, NetshaperFamily())) ksft_exit()
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerPATCH v2 5/10 net: shaper: set ret to -ENOMEM when genlmsg_new() fails in group_doit
In reply to Jakub Kicinskigenlmsg_new() alloc failure path in net_shaper_nl_group_doit() forgets to set ret before jumping to error handling.
--- net/shaper/shaper.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
net/shaper/shaper.c +3 -1
@@ -1276,8 +1276,10 @@ int net_shaper_nl_group_doit(struct sk_buff *skb, struct genl_info *info)* rollback on allocation failure. */ msg =genlmsg_new (net_shaper_handle_size (),GFP_KERNEL ); -if (!msg) +if (!msg) { + ret = -ENOMEM ; goto free_leaves; + } hierarchy =net_shaper_hierarchy_setup (binding);if (!hierarchy) {
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerPATCH v2 6/10 net: shaper: fix undersized reply skb allocation in GROUP command
In reply to Jakub Kicinskinet_shaper_group_send_reply() writes both the NET_SHAPER_A_IFINDEX attribute (via net_shaper_fill_binding()) and the nested NET_SHAPER_A_HANDLE attribute (via net_shaper_fill_handle()), but the reply skb at the call site in net_shaper_nl_group_doit() is allocated using net_shaper_handle_size(), which only accounts for the nested handle.
The allocation is therefore short by nla_total_size(sizeof(u32)) (8 bytes) for the IFINDEX attribute. In practice the slab allocator rounds up the small allocation so the bug is latent, but the size accounting is wrong and could bite if the reply grew further.
Introduce net_shaper_group_reply_size() that accounts for the full reply payload and use it both at the genlmsg_new() call site and in the defensive WARN_ONCE message.
--- net/shaper/shaper.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
net/shaper/shaper.c +8 -2
@@ -90,6 +90,12 @@ static int net_shaper_handle_size(void) nla_total_size(sizeof(u32))); } +static int net_shaper_group_reply_size (void ) +{ +return nla_total_size (sizeof (u32)) +/* NET_SHAPER_A_IFINDEX */ +net_shaper_handle_size ();/* NET_SHAPER_A_HANDLE */ +} +static int net_shaper_fill_binding (struct sk_buff *msg,const struct net_shaper_binding *binding,u32 type)
@@ -1227,7 +1233,7 @@ static int net_shaper_group_send_reply(struct net_shaper_binding *binding, free_msg:/* Should never happen as msg is pre-allocated with enough space. */ WARN_ONCE (true,"calculated message payload length (%d)" , -net_shaper_handle_size ()); +net_shaper_group_reply_size ());nlmsg_free (msg);return -EMSGSIZE ; }
@@ -1275,7 +1281,7 @@ int net_shaper_nl_group_doit(struct sk_buff *skb, struct genl_info *info)/* Prepare the msg reply in advance, to avoid device operation * rollback on allocation failure. */ - msg =genlmsg_new (net_shaper_handle_size (),GFP_KERNEL ); + msg =genlmsg_new (net_shaper_group_reply_size (),GFP_KERNEL );if (!msg) { ret = -ENOMEM ; goto free_leaves;
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerPATCH v2 7/10 tools: ynl: add scope qualifier for definitions
In reply to Jakub KicinskiUsing definitions in kernel policies is awkward right now. On one hand we want defines for max values and such. On the other we don't have a way of adding kernel-only defines. Adding unnecessary defines to uAPI is a bad idea, we won't be able to delete them. And when it comes to policy user space should just query it via the policy dump, not use hard coded defines.
Add a "scope" property to definitions, which will let us tell the codegen that a definition is for kernel use only. Support following values:
- uapi: render into the uAPI header (default, today's behavior)
- kernel: render to kernel header only
- user: same as kernel but for the user-side generated header
Definitions may have a header property (definition is "external", provided by existing header). Extend the scope to headers, too. If definition has both scope and header properties we will only generate the includes in the right scope.
---
This will fail yamllint, sticking to the same format as the rest of the file (extra spaces inside brackets).
--- Documentation/netlink/genetlink-c.yaml | 9 ++++++ Documentation/netlink/genetlink-legacy.yaml | 9 ++++++ Documentation/netlink/genetlink.yaml | 9 ++++++ Documentation/netlink/netlink-raw.yaml | 9 ++++++ tools/net/ynl/pyynl/ynl_gen_c.py | 31 +++++++++++++++++++-- 5 files changed, 65 insertions(+), 2 deletions(-)
Documentation/netlink/genetlink-c.yaml +9 -0
@@ -69,6 +69,15 @@ additionalProperties: False header: description: For C-compatible languages, header which already defines this value. type: string + scope: + description: | + Visibility of this definition. "uapi" (default) renders into + the uAPI header, "kernel" renders into the kernel-side + generated header, "user" renders into the user-side + generated header. When combined with `header:`, the + definition is not rendered, and the named header is + included only by code matching the scope. + enum: [ uapi, kernel, user ] type: enum: [ const, enum, flags ] doc:
Documentation/netlink/genetlink-legacy.yaml +9 -0
@@ -83,6 +83,15 @@ additionalProperties: False header: description: For C-compatible languages, header which already defines this value. type: string + scope: + description: | + Visibility of this definition. "uapi" (default) renders into + the uAPI header, "kernel" renders into the kernel-side + generated header, "user" renders into the user-side + generated header. When combined with `header:`, the + definition is not rendered, and the named header is + included only by code matching the scope. + enum: [ uapi, kernel, user ] type: enum: [ const, enum, flags, struct ] # Trim doc:
Documentation/netlink/genetlink.yaml +9 -0
@@ -55,6 +55,15 @@ additionalProperties: False header: description: For C-compatible languages, header which already defines this value. type: string + scope: + description: | + Visibility of this definition. "uapi" (default) renders into + the uAPI header, "kernel" renders into the kernel-side + generated header, "user" renders into the user-side + generated header. When combined with `header:`, the + definition is not rendered, and the named header is + included only by code matching the scope. + enum: [ uapi, kernel, user ] type: enum: [ const, enum, flags ] doc:
Documentation/netlink/netlink-raw.yaml +9 -0
@@ -87,6 +87,15 @@ additionalProperties: False header: description: For C-compatible languages, header which already defines this value. type: string + scope: + description: | + Visibility of this definition. "uapi" (default) renders into + the uAPI header, "kernel" renders into the kernel-side + generated header, "user" renders into the user-side + generated header. When combined with `header:`, the + definition is not rendered, and the named header is + included only by code matching the scope. + enum: [ uapi, kernel, user ] type: enum: [ const, enum, flags, struct ] # Trim doc:
tools/net/ynl/pyynl/ynl_gen_c.py +29 -2
@@ -3212,6 +3212,8 @@ _C_KW = { for const in family['definitions']: if const.get('header'): continue + if const.get('scope', 'uapi') != 'uapi': + continue if const['type'] != 'const': cw.writes_defines(defines)
@@ -3339,6 +3341,25 @@ _C_KW = { cw.p(f'#endif /* {hdr_prot} */') +def render_scoped_consts(family, cw, scope): + defines = [] + for const in family['definitions']: + if const['type'] != 'const': + continue + if const.get('header'): + continue + if const.get('scope') != scope: + continue + name_pfx = const.get('name-prefix', f"{family.ident_name}-") + defines.append([ + c_upper(family.get('c-define-name', + f"{name_pfx}{const['name']}")), + const['value']]) + if defines: + cw.writes_defines(defines) + cw.nl() + + def _render_user_ntf_entry(ri, op): if not ri.family.is_classic(): ri.cw.block_start(line=f"[{op.enum_name}] = ")
@@ -3504,8 +3525,12 @@ _C_KW = { cw.p('#include "ynl.h"') headers = [] for definition in parsed['definitions'] + parsed['attribute-sets']: - if 'header' in definition: - headers.append(definition['header']) + if 'header' not in definition: + continue + scope = definition.get('scope', 'uapi') + if scope != 'uapi' and scope != args.mode: + continue + headers.append(definition['header']) if args.mode == 'user': headers.append(parsed.uapi_header) seen_header = []
@@ -3522,6 +3547,7 @@ _C_KW = { for one in args.user_header: cw.p(f'#include "{one}"') else: + render_scoped_consts(parsed, cw, 'user') cw.p('struct ynl_sock;') cw.nl() render_user_family(parsed, cw, True)
@@ -3529,6 +3555,7 @@ _C_KW = { if args.mode == "kernel": if args.header: + render_scoped_consts(parsed, cw, 'kernel') for _, struct in sorted(parsed.pure_nested_structs.items()): if struct.request: cw.p('/* Common nested types */')
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerPATCH v2 8/10 net: shaper: reject handle IDs exceeding internal bit-width
In reply to Jakub Kicinskinet_shaper_parse_handle() reads the user-supplied handle ID via nla_get_u32(), accepting the full u32 range. However, the xarray key is built by net_shaper_handle_to_index() using FIELD_PREP(NET_SHAPER_ID_MASK, handle->id), where NET_SHAPER_ID_MASK is GENMASK(25, 0) - only 26 bits wide. FIELD_PREP silently masks off the upper bits at runtime. A user-supplied NODE id like 0x04000123 becomes id 0x123.
Additionally, a user-supplied id equal to NET_SHAPER_ID_UNSPEC (0x03FFFFFF, which is NET_SHAPER_ID_MASK itself) would collide with the sentinel used internally by the group operation to signal "allocate a new NODE id".
Reject user-supplied IDs >= NET_SHAPER_ID_MASK (i.e., >= 0x03FFFFFF) in the policy.
--- Documentation/netlink/specs/net_shaper.yaml | 7 +++++++ net/shaper/shaper_nl_gen.h | 2 ++ net/shaper/shaper.c | 4 +++- net/shaper/shaper_nl_gen.c | 7 ++++++- 4 files changed, 18 insertions(+), 2 deletions(-)
Documentation/netlink/specs/net_shaper.yaml +7 -0
@@ -33,6 +33,11 @@ doc: | @cap-get operation. definitions: + - + type: const + name: max-handle-id + value: 0x3fffffe + scope: kernel - type: enum name: scope
@@ -140,6 +145,8 @@ doc: | - name: id type: u32 + checks: + max: max-handle-id doc: | Numeric identifier of a shaper. The id semantic depends on the scope. For @queue scope it's the queue id and for @node
net/shaper/shaper_nl_gen.h +2 -0
@@ -12,6 +12,8 @@#include <uapi/linux/net_shaper.h> +#define NET_SHAPER_MAX_HANDLE_ID 67108862 +/* Common nested types */ extern const struct nla_policy net_shaper_handle_nl_policy[NET_SHAPER_A_HANDLE_ID +1 ];extern const struct nla_policy net_shaper_leaf_info_nl_policy[NET_SHAPER_A_WEIGHT +1 ];
net/shaper/shaper.c +3 -1
@@ -21,6 +21,8 @@#define NET_SHAPER_ID_UNSPEC NET_SHAPER_ID_MASK +static_assert (NET_SHAPER_ID_UNSPEC ==NET_SHAPER_MAX_HANDLE_ID +1 ); +struct net_shaper_hierarchy {struct xarray shapers; };
@@ -360,7 +362,7 @@ static int net_shaper_pre_insert(struct net_shaper_binding *binding, handle->id == NET_SHAPER_ID_UNSPEC) {u32 min, max; - handle->id =NET_SHAPER_ID_MASK -1 ; + handle->id =NET_SHAPER_MAX_HANDLE_ID ; max =net_shaper_handle_to_index (handle); handle->id =0 ; min =net_shaper_handle_to_index (handle);
net/shaper/shaper_nl_gen.c +6 -1
@@ -11,10 +11,15 @@#include <uapi/linux/net_shaper.h> +/* Integer value ranges */ +static const struct netlink_range_validation net_shaper_a_handle_id_range = { + .max =NET_SHAPER_MAX_HANDLE_ID , +}; +/* Common nested types */ const struct nla_policy net_shaper_handle_nl_policy[NET_SHAPER_A_HANDLE_ID +1 ] = { [NET_SHAPER_A_HANDLE_SCOPE ] =NLA_POLICY_MAX (NLA_U32 ,3 ), - [NET_SHAPER_A_HANDLE_ID ] = { .type =NLA_U32 , }, + [NET_SHAPER_A_HANDLE_ID ] =NLA_POLICY_FULL_RANGE (NLA_U32 , &net_shaper_a_handle_id_range), };const struct nla_policy net_shaper_leaf_info_nl_policy[NET_SHAPER_A_WEIGHT +1 ] = {
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerPATCH v2 9/10 net: shaper: enforce singleton NETDEV scope with id 0
In reply to Jakub KicinskiThe NETDEV scope represents a singleton root shaper in the per-device hierarchy. All code assumes NETDEV shapers have id 0:net_shaper_default_parent() hardcodes parent->id = 0 when returning the NETDEV parent for QUEUE/NODE children, and the UAPI documentation describes NETDEV scope as "the main shaper" (singular, not plural).
Make sure we reject non-0 IDs.
--- net/shaper/shaper.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/shaper/shaper.c +6 -0
@@ -482,6 +482,12 @@ static int net_shaper_parse_handle(const struct nlattr *attr,else if (handle->scope ==NET_SHAPER_SCOPE_NODE ) id =NET_SHAPER_ID_UNSPEC ; +if (id && handle->scope ==NET_SHAPER_SCOPE_NETDEV ) { +NL_SET_ERR_MSG_ATTR (info->extack, id_attr, +"Netdev scope is a singleton, must use ID 0" ); +return -EINVAL ; + } + handle->id = id;return 0 ; }
Signature · 1 line
2.54.0
Jakub Kicinski
maintainerPATCH v2 10/10 net: shaper: reject QUEUE scope handle with missing id
In reply to Jakub Kicinskinet_shaper_parse_handle() does not enforce that the user provides the handle ID. For NODE the ID defaults to UNSPEC for all other cases it defaults to 0.
For NETDEV 0 is the only option. For QUEUE defaulting to 0 makes less intuitive sense. Specifically because the behavior should (IMHO) be the same for all cases where there may be more than one ID (QUEUE and NODE).
We should either document this as intentional or reject. I picked the latter with no strong conviction.
--- net/shaper/shaper.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
net/shaper/shaper.c +7 -2
@@ -477,10 +477,15 @@ static int net_shaper_parse_handle(const struct nlattr *attr,* shaper (any other value). */ id_attr = tb[NET_SHAPER_A_HANDLE_ID ]; -if (id_attr) +if (id_attr) { id =nla_get_u32 (id_attr); -else if (handle->scope ==NET_SHAPER_SCOPE_NODE ) + }else if (handle->scope ==NET_SHAPER_SCOPE_NODE ) { id =NET_SHAPER_ID_UNSPEC ; + }else if (handle->scope ==NET_SHAPER_SCOPE_QUEUE ) { +NL_SET_ERR_ATTR_MISS (info->extack, attr, +NET_SHAPER_A_HANDLE_ID ); +return -EINVAL ; + }if (id && handle->scope ==NET_SHAPER_SCOPE_NETDEV ) { NL_SET_ERR_MSG_ATTR(info->extack, id_attr,
Signature · 1 line
2.54.0
This series was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:
Hello:
This series was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:
Quoted 8 lines · On Sun, 10 May 2026 12:28:54 -0700 you wrote:
Fix various minor bugs in the net shaper API.
First 2 patches deal with ordering issues around inserting and publishing new shapers. Shapers are inserted "tentatively" and marked valid only after HW op succeeded, this used to be slightly racy.
[...]
Here is the summary with links:
- [net,v2,01/10] net: shaper: flip the polarity of the valid flag
https://git.kernel.org/netdev/net/c/7cee43fcb0c3 - [net,v2,02/10] net: shaper: fix trivial ordering issue in net_shaper_commit() https://git.kernel.org/netdev/net/c/235fb5376139 - [net,v2,03/10] net: shaper: reject duplicate leaves in GROUP request https://git.kernel.org/netdev/net/c/a9a2fa1da619 - [net,v2,04/10] selftests: drv-net: add shaper test for duplicate leaves https://git.kernel.org/netdev/net/c/6e8ae9d805d4 - [net,v2,05/10] net: shaper: set ret to -ENOMEM when genlmsg_new() fails in group_doit https://git.kernel.org/netdev/net/c/8054f85b83f4 - [net,v2,06/10] net: shaper: fix undersized reply skb allocation in GROUP command https://git.kernel.org/netdev/net/c/0f9a857e34d0 - [net,v2,07/10] tools: ynl: add scope qualifier for definitions https://git.kernel.org/netdev/net/c/fbf5df34a4db - [net,v2,08/10] net: shaper: reject handle IDs exceeding internal bit-width https://git.kernel.org/netdev/net/c/8d5806c600fd - [net,v2,09/10] net: shaper: enforce singleton NETDEV scope with id 0 https://git.kernel.org/netdev/net/c/b62b29e6de67 - [net,v2,10/10] net: shaper: reject QUEUE scope handle with missing id https://git.kernel.org/netdev/net/c/ce372e869f9f
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html