Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

netlink: introduce type-checking attribute iteration

There are, especially with multi-attr arrays, many cases
of needing to iterate all attributes of a specific type
in a netlink message or a nested attribute. Add specific
macros to support that case.

Also convert many instances using this spatch:

@@
iterator nla_for_each_attr;
iterator name nla_for_each_attr_type;
identifier nla;
expression head, len, rem;
expression ATTR;
type T;
identifier x;
@@
-nla_for_each_attr(nla, head, len, rem)
+nla_for_each_attr_type(nla, ATTR, head, len, rem)
{
<... T x; ...>
-if (nla_type(nla) == ATTR) {
...
-}
}

@@
identifier nla;
iterator nla_for_each_nested;
iterator name nla_for_each_nested_type;
expression attr, rem;
expression ATTR;
type T;
identifier x;
@@
-nla_for_each_nested(nla, attr, rem)
+nla_for_each_nested_type(nla, ATTR, attr, rem)
{
<... T x; ...>
-if (nla_type(nla) == ATTR) {
...
-}
}

@@
iterator nla_for_each_attr;
iterator name nla_for_each_attr_type;
identifier nla;
expression head, len, rem;
expression ATTR;
type T;
identifier x;
@@
-nla_for_each_attr(nla, head, len, rem)
+nla_for_each_attr_type(nla, ATTR, head, len, rem)
{
<... T x; ...>
-if (nla_type(nla) != ATTR) continue;
...
}

@@
identifier nla;
iterator nla_for_each_nested;
iterator name nla_for_each_nested_type;
expression attr, rem;
expression ATTR;
type T;
identifier x;
@@
-nla_for_each_nested(nla, attr, rem)
+nla_for_each_nested_type(nla, ATTR, attr, rem)
{
<... T x; ...>
-if (nla_type(nla) != ATTR) continue;
...
}

Although I had to undo one bad change this made, and
I also adjusted some other code for whitespace and to
use direct variable initialization now.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Link: https://lore.kernel.org/r/20240328203144.b5a6c895fb80.I1869b44767379f204998ff44dd239803f39c23e0@changeid
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Johannes Berg and committed by
Jakub Kicinski
e8058a49 9494dc0b

+65 -79
+1 -4
drivers/net/ethernet/broadcom/bnxt/bnxt.c
··· 14581 14581 if (!br_spec) 14582 14582 return -EINVAL; 14583 14583 14584 - nla_for_each_nested(attr, br_spec, rem) { 14584 + nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 14585 14585 u16 mode; 14586 - 14587 - if (nla_type(attr) != IFLA_BRIDGE_MODE) 14588 - continue; 14589 14586 14590 14587 mode = nla_get_u16(attr); 14591 14588 if (mode == bp->br_mode)
+1 -4
drivers/net/ethernet/emulex/benet/be_main.c
··· 4982 4982 if (!br_spec) 4983 4983 return -EINVAL; 4984 4984 4985 - nla_for_each_nested(attr, br_spec, rem) { 4986 - if (nla_type(attr) != IFLA_BRIDGE_MODE) 4987 - continue; 4988 - 4985 + nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 4989 4986 mode = nla_get_u16(attr); 4990 4987 if (BE3_chip(adapter) && mode == BRIDGE_MODE_VEPA) 4991 4988 return -EOPNOTSUPP;
+2 -6
drivers/net/ethernet/intel/i40e/i40e_main.c
··· 13108 13108 if (!br_spec) 13109 13109 return -EINVAL; 13110 13110 13111 - nla_for_each_nested(attr, br_spec, rem) { 13112 - __u16 mode; 13111 + nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 13112 + __u16 mode = nla_get_u16(attr); 13113 13113 13114 - if (nla_type(attr) != IFLA_BRIDGE_MODE) 13115 - continue; 13116 - 13117 - mode = nla_get_u16(attr); 13118 13114 if ((mode != BRIDGE_MODE_VEPA) && 13119 13115 (mode != BRIDGE_MODE_VEB)) 13120 13116 return -EINVAL;
+2 -5
drivers/net/ethernet/intel/ice/ice_main.c
··· 7993 7993 if (!br_spec) 7994 7994 return -EINVAL; 7995 7995 7996 - nla_for_each_nested(attr, br_spec, rem) { 7997 - __u16 mode; 7996 + nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 7997 + __u16 mode = nla_get_u16(attr); 7998 7998 7999 - if (nla_type(attr) != IFLA_BRIDGE_MODE) 8000 - continue; 8001 - mode = nla_get_u16(attr); 8002 7999 if (mode != BRIDGE_MODE_VEPA && mode != BRIDGE_MODE_VEB) 8003 8000 return -EINVAL; 8004 8001 /* Continue if bridge mode is not being flipped */
+3 -8
drivers/net/ethernet/intel/ixgbe/ixgbe_main.c
··· 10061 10061 if (!br_spec) 10062 10062 return -EINVAL; 10063 10063 10064 - nla_for_each_nested(attr, br_spec, rem) { 10065 - int status; 10066 - __u16 mode; 10064 + nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 10065 + __u16 mode = nla_get_u16(attr); 10066 + int status = ixgbe_configure_bridge_mode(adapter, mode); 10067 10067 10068 - if (nla_type(attr) != IFLA_BRIDGE_MODE) 10069 - continue; 10070 - 10071 - mode = nla_get_u16(attr); 10072 - status = ixgbe_configure_bridge_mode(adapter, mode); 10073 10068 if (status) 10074 10069 return status; 10075 10070
+1 -4
drivers/net/ethernet/mellanox/mlx5/core/en_main.c
··· 4950 4950 if (!br_spec) 4951 4951 return -EINVAL; 4952 4952 4953 - nla_for_each_nested(attr, br_spec, rem) { 4954 - if (nla_type(attr) != IFLA_BRIDGE_MODE) 4955 - continue; 4956 - 4953 + nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 4957 4954 mode = nla_get_u16(attr); 4958 4955 if (mode > BRIDGE_MODE_VEPA) 4959 4956 return -EINVAL;
+1 -4
drivers/net/ethernet/netronome/nfp/nfp_net_common.c
··· 2289 2289 if (!br_spec) 2290 2290 return -EINVAL; 2291 2291 2292 - nla_for_each_nested(attr, br_spec, rem) { 2293 - if (nla_type(attr) != IFLA_BRIDGE_MODE) 2294 - continue; 2295 - 2292 + nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 2296 2293 new_ctrl = nn->dp.ctrl; 2297 2294 mode = nla_get_u16(attr); 2298 2295 if (mode == BRIDGE_MODE_VEPA)
+27
include/net/netlink.h
··· 157 157 * nla_parse() parse and validate stream of attrs 158 158 * nla_parse_nested() parse nested attributes 159 159 * nla_for_each_attr() loop over all attributes 160 + * nla_for_each_attr_type() loop over all attributes with the 161 + * given type 160 162 * nla_for_each_nested() loop over the nested attributes 163 + * nla_for_each_nested_type() loop over the nested attributes with 164 + * the given type 161 165 *========================================================================= 162 166 */ 163 167 ··· 2075 2071 pos = nla_next(pos, &(rem))) 2076 2072 2077 2073 /** 2074 + * nla_for_each_attr_type - iterate over a stream of attributes 2075 + * @pos: loop counter, set to current attribute 2076 + * @type: required attribute type for @pos 2077 + * @head: head of attribute stream 2078 + * @len: length of attribute stream 2079 + * @rem: initialized to len, holds bytes currently remaining in stream 2080 + */ 2081 + #define nla_for_each_attr_type(pos, type, head, len, rem) \ 2082 + nla_for_each_attr(pos, head, len, rem) \ 2083 + if (nla_type(pos) == type) 2084 + 2085 + /** 2078 2086 * nla_for_each_nested - iterate over nested attributes 2079 2087 * @pos: loop counter, set to current attribute 2080 2088 * @nla: attribute containing the nested attributes ··· 2094 2078 */ 2095 2079 #define nla_for_each_nested(pos, nla, rem) \ 2096 2080 nla_for_each_attr(pos, nla_data(nla), nla_len(nla), rem) 2081 + 2082 + /** 2083 + * nla_for_each_nested_type - iterate over nested attributes 2084 + * @pos: loop counter, set to current attribute 2085 + * @type: required attribute type for @pos 2086 + * @nla: attribute containing the nested attributes 2087 + * @rem: initialized to len, holds bytes currently remaining in stream 2088 + */ 2089 + #define nla_for_each_nested_type(pos, type, nla, rem) \ 2090 + nla_for_each_nested(pos, nla, rem) \ 2091 + if (nla_type(pos) == type) 2097 2092 2098 2093 /** 2099 2094 * nla_is_last - Test if attribute is last in stream
+4 -6
net/8021q/vlan_netlink.c
··· 117 117 return err; 118 118 } 119 119 if (data[IFLA_VLAN_INGRESS_QOS]) { 120 - nla_for_each_nested(attr, data[IFLA_VLAN_INGRESS_QOS], rem) { 121 - if (nla_type(attr) != IFLA_VLAN_QOS_MAPPING) 122 - continue; 120 + nla_for_each_nested_type(attr, IFLA_VLAN_QOS_MAPPING, 121 + data[IFLA_VLAN_INGRESS_QOS], rem) { 123 122 m = nla_data(attr); 124 123 vlan_dev_set_ingress_priority(dev, m->to, m->from); 125 124 } 126 125 } 127 126 if (data[IFLA_VLAN_EGRESS_QOS]) { 128 - nla_for_each_nested(attr, data[IFLA_VLAN_EGRESS_QOS], rem) { 129 - if (nla_type(attr) != IFLA_VLAN_QOS_MAPPING) 130 - continue; 127 + nla_for_each_nested_type(attr, IFLA_VLAN_QOS_MAPPING, 128 + data[IFLA_VLAN_EGRESS_QOS], rem) { 131 129 m = nla_data(attr); 132 130 err = vlan_dev_set_egress_priority(dev, m->from, m->to); 133 131 if (err)
+9 -14
net/core/bpf_sk_storage.c
··· 496 496 if (!bpf_capable()) 497 497 return ERR_PTR(-EPERM); 498 498 499 - nla_for_each_nested(nla, nla_stgs, rem) { 500 - if (nla_type(nla) == SK_DIAG_BPF_STORAGE_REQ_MAP_FD) { 501 - if (nla_len(nla) != sizeof(u32)) 502 - return ERR_PTR(-EINVAL); 503 - nr_maps++; 504 - } 499 + nla_for_each_nested_type(nla, SK_DIAG_BPF_STORAGE_REQ_MAP_FD, 500 + nla_stgs, rem) { 501 + if (nla_len(nla) != sizeof(u32)) 502 + return ERR_PTR(-EINVAL); 503 + nr_maps++; 505 504 } 506 505 507 506 diag = kzalloc(struct_size(diag, maps, nr_maps), GFP_KERNEL); 508 507 if (!diag) 509 508 return ERR_PTR(-ENOMEM); 510 509 511 - nla_for_each_nested(nla, nla_stgs, rem) { 512 - struct bpf_map *map; 513 - int map_fd; 510 + nla_for_each_nested_type(nla, SK_DIAG_BPF_STORAGE_REQ_MAP_FD, 511 + nla_stgs, rem) { 512 + int map_fd = nla_get_u32(nla); 513 + struct bpf_map *map = bpf_map_get(map_fd); 514 514 515 - if (nla_type(nla) != SK_DIAG_BPF_STORAGE_REQ_MAP_FD) 516 - continue; 517 - 518 - map_fd = nla_get_u32(nla); 519 - map = bpf_map_get(map_fd); 520 515 if (IS_ERR(map)) { 521 516 err = PTR_ERR(map); 522 517 goto err_free;
+7 -8
net/core/rtnetlink.c
··· 5245 5245 5246 5246 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5247 5247 if (br_spec) { 5248 - nla_for_each_nested(attr, br_spec, rem) { 5249 - if (nla_type(attr) == IFLA_BRIDGE_FLAGS) { 5250 - if (nla_len(attr) < sizeof(flags)) 5251 - return -EINVAL; 5248 + nla_for_each_nested_type(attr, IFLA_BRIDGE_FLAGS, br_spec, 5249 + rem) { 5250 + if (nla_len(attr) < sizeof(flags)) 5251 + return -EINVAL; 5252 5252 5253 - have_flags = true; 5254 - flags = nla_get_u16(attr); 5255 - break; 5256 - } 5253 + have_flags = true; 5254 + flags = nla_get_u16(attr); 5255 + break; 5257 5256 } 5258 5257 } 5259 5258
+4 -8
net/devlink/dev.c
··· 1202 1202 if (err) 1203 1203 goto free_msg; 1204 1204 1205 - nla_for_each_attr(nlattr, (void *)msg->data, msg->len, rem) { 1205 + nla_for_each_attr_type(nlattr, DEVLINK_ATTR_INFO_VERSION_RUNNING, 1206 + (void *)msg->data, msg->len, rem) { 1206 1207 const struct nlattr *kv; 1207 1208 int rem_kv; 1208 1209 1209 - if (nla_type(nlattr) != DEVLINK_ATTR_INFO_VERSION_RUNNING) 1210 - continue; 1211 - 1212 - nla_for_each_nested(kv, nlattr, rem_kv) { 1213 - if (nla_type(kv) != DEVLINK_ATTR_INFO_VERSION_VALUE) 1214 - continue; 1215 - 1210 + nla_for_each_nested_type(kv, DEVLINK_ATTR_INFO_VERSION_VALUE, 1211 + nlattr, rem_kv) { 1216 1212 strlcat(buf, nla_data(kv), len); 1217 1213 strlcat(buf, " ", len); 1218 1214 }
+2 -4
net/sched/sch_mqprio.c
··· 215 215 for (tc = 0; tc < TC_QOPT_MAX_QUEUE; tc++) 216 216 fp[tc] = priv->fp[tc]; 217 217 218 - nla_for_each_attr(n, nlattr_opt, nlattr_opt_len, rem) { 219 - if (nla_type(n) != TCA_MQPRIO_TC_ENTRY) 220 - continue; 221 - 218 + nla_for_each_attr_type(n, TCA_MQPRIO_TC_ENTRY, nlattr_opt, 219 + nlattr_opt_len, rem) { 222 220 err = mqprio_parse_tc_entry(fp, n, &seen_tcs, extack); 223 221 if (err) 224 222 goto out;
+1 -4
net/sched/sch_taprio.c
··· 1752 1752 fp[tc] = q->fp[tc]; 1753 1753 } 1754 1754 1755 - nla_for_each_nested(n, opt, rem) { 1756 - if (nla_type(n) != TCA_TAPRIO_ATTR_TC_ENTRY) 1757 - continue; 1758 - 1755 + nla_for_each_nested_type(n, TCA_TAPRIO_ATTR_TC_ENTRY, opt, rem) { 1759 1756 err = taprio_parse_tc_entry(sch, n, max_sdu, fp, &seen_tcs, 1760 1757 extack); 1761 1758 if (err)