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.

Merge branch 'devlink-sanitize-variable-typed-attributes'

Jiri Pirko says:

====================
devlink: sanitize variable typed attributes

This is continuation based on first two patches of
https://lore.kernel.org/20250425214808.507732-1-saeed@kernel.org

Better to take it as a separate patchset, as the rest of the original
patchset is probably settled.

This patchset is taking care of incorrect usage of internal NLA_* values
in uapi, introduces new enum (in patch #2) that shadows NLA_* values and
makes that part of UAPI.

The last two patches removes unnecessary translations with maintaining
clear devlink param driver api.
====================

Link: https://patch.msgid.link/20250505114513.53370-1-jiri@resnulli.us
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+147 -87
+24
Documentation/netlink/specs/devlink.yaml
··· 202 202 name: exception 203 203 - 204 204 name: control 205 + - 206 + type: enum 207 + name: var-attr-type 208 + entries: 209 + - 210 + name: u8 211 + value: 1 212 + - 213 + name: u16 214 + - 215 + name: u32 216 + - 217 + name: u64 218 + - 219 + name: string 220 + - 221 + name: flag 222 + - 223 + name: nul_string 224 + value: 10 225 + - 226 + name: binary 205 227 206 228 attribute-sets: 207 229 - ··· 520 498 - 521 499 name: param-type 522 500 type: u8 501 + enum: var-attr-type 523 502 524 503 # TODO: fill in the attributes in between 525 504 ··· 615 592 - 616 593 name: fmsg-obj-value-type 617 594 type: u8 595 + enum: var-attr-type 618 596 619 597 # TODO: fill in the attributes in between 620 598
+5 -5
include/net/devlink.h
··· 420 420 421 421 #define __DEVLINK_PARAM_MAX_STRING_VALUE 32 422 422 enum devlink_param_type { 423 - DEVLINK_PARAM_TYPE_U8, 424 - DEVLINK_PARAM_TYPE_U16, 425 - DEVLINK_PARAM_TYPE_U32, 426 - DEVLINK_PARAM_TYPE_STRING, 427 - DEVLINK_PARAM_TYPE_BOOL, 423 + DEVLINK_PARAM_TYPE_U8 = DEVLINK_VAR_ATTR_TYPE_U8, 424 + DEVLINK_PARAM_TYPE_U16 = DEVLINK_VAR_ATTR_TYPE_U16, 425 + DEVLINK_PARAM_TYPE_U32 = DEVLINK_VAR_ATTR_TYPE_U32, 426 + DEVLINK_PARAM_TYPE_STRING = DEVLINK_VAR_ATTR_TYPE_STRING, 427 + DEVLINK_PARAM_TYPE_BOOL = DEVLINK_VAR_ATTR_TYPE_FLAG, 428 428 }; 429 429 430 430 union devlink_param_value {
+15
include/uapi/linux/devlink.h
··· 385 385 DEVLINK_LINECARD_STATE_MAX = __DEVLINK_LINECARD_STATE_MAX - 1 386 386 }; 387 387 388 + /* Variable attribute type. */ 389 + enum devlink_var_attr_type { 390 + /* Following values relate to the internal NLA_* values */ 391 + DEVLINK_VAR_ATTR_TYPE_U8 = 1, 392 + DEVLINK_VAR_ATTR_TYPE_U16, 393 + DEVLINK_VAR_ATTR_TYPE_U32, 394 + DEVLINK_VAR_ATTR_TYPE_U64, 395 + DEVLINK_VAR_ATTR_TYPE_STRING, 396 + DEVLINK_VAR_ATTR_TYPE_FLAG, 397 + DEVLINK_VAR_ATTR_TYPE_NUL_STRING = 10, 398 + DEVLINK_VAR_ATTR_TYPE_BINARY, 399 + __DEVLINK_VAR_ATTR_TYPE_CUSTOM_BASE = 0x80, 400 + /* Any possible custom types, unrelated to NLA_* values go below */ 401 + }; 402 + 388 403 enum devlink_attr { 389 404 /* don't change the order or add anything between, this is ABI! */ 390 405 DEVLINK_ATTR_UNSPEC,
+21 -31
net/devlink/health.c
··· 735 735 return; 736 736 } 737 737 738 - item->nla_type = NLA_NUL_STRING; 738 + item->nla_type = DEVLINK_VAR_ATTR_TYPE_NUL_STRING; 739 739 item->len = strlen(name) + 1; 740 740 item->attrtype = DEVLINK_ATTR_FMSG_OBJ_NAME; 741 741 memcpy(&item->value, name, item->len); ··· 822 822 static void devlink_fmsg_bool_put(struct devlink_fmsg *fmsg, bool value) 823 823 { 824 824 devlink_fmsg_err_if_binary(fmsg); 825 - devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_FLAG); 825 + devlink_fmsg_put_value(fmsg, &value, sizeof(value), 826 + DEVLINK_VAR_ATTR_TYPE_FLAG); 826 827 } 827 828 828 829 static void devlink_fmsg_u8_put(struct devlink_fmsg *fmsg, u8 value) 829 830 { 830 831 devlink_fmsg_err_if_binary(fmsg); 831 - devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U8); 832 + devlink_fmsg_put_value(fmsg, &value, sizeof(value), 833 + DEVLINK_VAR_ATTR_TYPE_U8); 832 834 } 833 835 834 836 void devlink_fmsg_u32_put(struct devlink_fmsg *fmsg, u32 value) 835 837 { 836 838 devlink_fmsg_err_if_binary(fmsg); 837 - devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U32); 839 + devlink_fmsg_put_value(fmsg, &value, sizeof(value), 840 + DEVLINK_VAR_ATTR_TYPE_U32); 838 841 } 839 842 EXPORT_SYMBOL_GPL(devlink_fmsg_u32_put); 840 843 841 844 static void devlink_fmsg_u64_put(struct devlink_fmsg *fmsg, u64 value) 842 845 { 843 846 devlink_fmsg_err_if_binary(fmsg); 844 - devlink_fmsg_put_value(fmsg, &value, sizeof(value), NLA_U64); 847 + devlink_fmsg_put_value(fmsg, &value, sizeof(value), 848 + DEVLINK_VAR_ATTR_TYPE_U64); 845 849 } 846 850 847 851 void devlink_fmsg_string_put(struct devlink_fmsg *fmsg, const char *value) 848 852 { 849 853 devlink_fmsg_err_if_binary(fmsg); 850 - devlink_fmsg_put_value(fmsg, value, strlen(value) + 1, NLA_NUL_STRING); 854 + devlink_fmsg_put_value(fmsg, value, strlen(value) + 1, 855 + DEVLINK_VAR_ATTR_TYPE_NUL_STRING); 851 856 } 852 857 EXPORT_SYMBOL_GPL(devlink_fmsg_string_put); 853 858 ··· 862 857 if (!fmsg->err && !fmsg->putting_binary) 863 858 fmsg->err = -EINVAL; 864 859 865 - devlink_fmsg_put_value(fmsg, value, value_len, NLA_BINARY); 860 + devlink_fmsg_put_value(fmsg, value, value_len, 861 + DEVLINK_VAR_ATTR_TYPE_BINARY); 866 862 } 867 863 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_put); 868 864 ··· 934 928 EXPORT_SYMBOL_GPL(devlink_fmsg_binary_pair_put); 935 929 936 930 static int 937 - devlink_fmsg_item_fill_type(struct devlink_fmsg_item *msg, struct sk_buff *skb) 938 - { 939 - switch (msg->nla_type) { 940 - case NLA_FLAG: 941 - case NLA_U8: 942 - case NLA_U32: 943 - case NLA_U64: 944 - case NLA_NUL_STRING: 945 - case NLA_BINARY: 946 - return nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE, 947 - msg->nla_type); 948 - default: 949 - return -EINVAL; 950 - } 951 - } 952 - 953 - static int 954 931 devlink_fmsg_item_fill_data(struct devlink_fmsg_item *msg, struct sk_buff *skb) 955 932 { 956 933 int attrtype = DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA; 957 934 u8 tmp; 958 935 959 936 switch (msg->nla_type) { 960 - case NLA_FLAG: 937 + case DEVLINK_VAR_ATTR_TYPE_FLAG: 961 938 /* Always provide flag data, regardless of its value */ 962 939 tmp = *(bool *)msg->value; 963 940 964 941 return nla_put_u8(skb, attrtype, tmp); 965 - case NLA_U8: 942 + case DEVLINK_VAR_ATTR_TYPE_U8: 966 943 return nla_put_u8(skb, attrtype, *(u8 *)msg->value); 967 - case NLA_U32: 944 + case DEVLINK_VAR_ATTR_TYPE_U32: 968 945 return nla_put_u32(skb, attrtype, *(u32 *)msg->value); 969 - case NLA_U64: 946 + case DEVLINK_VAR_ATTR_TYPE_U64: 970 947 return devlink_nl_put_u64(skb, attrtype, *(u64 *)msg->value); 971 - case NLA_NUL_STRING: 948 + case DEVLINK_VAR_ATTR_TYPE_NUL_STRING: 972 949 return nla_put_string(skb, attrtype, (char *)&msg->value); 973 - case NLA_BINARY: 950 + case DEVLINK_VAR_ATTR_TYPE_BINARY: 974 951 return nla_put(skb, attrtype, msg->len, (void *)&msg->value); 975 952 default: 976 953 return -EINVAL; ··· 987 998 err = nla_put_flag(skb, item->attrtype); 988 999 break; 989 1000 case DEVLINK_ATTR_FMSG_OBJ_VALUE_DATA: 990 - err = devlink_fmsg_item_fill_type(item, skb); 1001 + err = nla_put_u8(skb, DEVLINK_ATTR_FMSG_OBJ_VALUE_TYPE, 1002 + item->nla_type); 991 1003 if (err) 992 1004 break; 993 1005 err = devlink_fmsg_item_fill_data(item, skb);
+2 -44
net/devlink/param.c
··· 167 167 } 168 168 169 169 static int 170 - devlink_param_type_to_nla_type(enum devlink_param_type param_type) 171 - { 172 - switch (param_type) { 173 - case DEVLINK_PARAM_TYPE_U8: 174 - return NLA_U8; 175 - case DEVLINK_PARAM_TYPE_U16: 176 - return NLA_U16; 177 - case DEVLINK_PARAM_TYPE_U32: 178 - return NLA_U32; 179 - case DEVLINK_PARAM_TYPE_STRING: 180 - return NLA_STRING; 181 - case DEVLINK_PARAM_TYPE_BOOL: 182 - return NLA_FLAG; 183 - default: 184 - return -EINVAL; 185 - } 186 - } 187 - 188 - static int 189 170 devlink_nl_param_value_fill_one(struct sk_buff *msg, 190 171 enum devlink_param_type type, 191 172 enum devlink_param_cmode cmode, ··· 228 247 struct devlink_param_gset_ctx ctx; 229 248 struct nlattr *param_values_list; 230 249 struct nlattr *param_attr; 231 - int nla_type; 232 250 void *hdr; 233 251 int err; 234 252 int i; ··· 273 293 goto param_nest_cancel; 274 294 if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC)) 275 295 goto param_nest_cancel; 276 - 277 - nla_type = devlink_param_type_to_nla_type(param->type); 278 - if (nla_type < 0) 279 - goto param_nest_cancel; 280 - if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type)) 296 + if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, param->type)) 281 297 goto param_nest_cancel; 282 298 283 299 param_values_list = nla_nest_start_noflag(msg, ··· 395 419 if (GENL_REQ_ATTR_CHECK(info, DEVLINK_ATTR_PARAM_TYPE)) 396 420 return -EINVAL; 397 421 398 - switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) { 399 - case NLA_U8: 400 - *param_type = DEVLINK_PARAM_TYPE_U8; 401 - break; 402 - case NLA_U16: 403 - *param_type = DEVLINK_PARAM_TYPE_U16; 404 - break; 405 - case NLA_U32: 406 - *param_type = DEVLINK_PARAM_TYPE_U32; 407 - break; 408 - case NLA_STRING: 409 - *param_type = DEVLINK_PARAM_TYPE_STRING; 410 - break; 411 - case NLA_FLAG: 412 - *param_type = DEVLINK_PARAM_TYPE_BOOL; 413 - break; 414 - default: 415 - return -EINVAL; 416 - } 422 + *param_type = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE]); 417 423 418 424 return 0; 419 425 }
+52 -6
tools/net/ynl/pyynl/ynl_gen_c.py
··· 389 389 if 'enum' in self.attr: 390 390 enum = self.family.consts[self.attr['enum']] 391 391 low, high = enum.value_range() 392 - if 'min' not in self.checks: 393 - if low != 0 or self.type[0] == 's': 394 - self.checks['min'] = low 395 - if 'max' not in self.checks: 396 - self.checks['max'] = high 392 + if low == None and high == None: 393 + self.checks['sparse'] = True 394 + else: 395 + if 'min' not in self.checks: 396 + if low != 0 or self.type[0] == 's': 397 + self.checks['min'] = low 398 + if 'max' not in self.checks: 399 + self.checks['max'] = high 397 400 398 401 if 'min' in self.checks and 'max' in self.checks: 399 402 if self.get_limit('min') > self.get_limit('max'): ··· 428 425 return f"NLA_POLICY_MIN({policy}, {self.get_limit_str('min')})" 429 426 elif 'max' in self.checks: 430 427 return f"NLA_POLICY_MAX({policy}, {self.get_limit_str('max')})" 428 + elif 'sparse' in self.checks: 429 + return f"NLA_POLICY_VALIDATE_FN({policy}, &{c_lower(self.enum_name)}_validate)" 431 430 return super()._attr_policy(policy) 432 431 433 432 def _attr_typol(self): ··· 935 930 high = max([x.value for x in self.entries.values()]) 936 931 937 932 if high - low + 1 != len(self.entries): 938 - raise Exception("Can't get value range for a noncontiguous enum") 933 + return None, None 939 934 940 935 return low, high 941 936 ··· 2431 2426 cw.nl() 2432 2427 2433 2428 2429 + def print_kernel_policy_sparse_enum_validates(family, cw): 2430 + first = True 2431 + for _, attr_set in family.attr_sets.items(): 2432 + if attr_set.subset_of: 2433 + continue 2434 + 2435 + for _, attr in attr_set.items(): 2436 + if not attr.request: 2437 + continue 2438 + if not attr.enum_name: 2439 + continue 2440 + if 'sparse' not in attr.checks: 2441 + continue 2442 + 2443 + if first: 2444 + cw.p('/* Sparse enums validation callbacks */') 2445 + first = False 2446 + 2447 + sign = '' if attr.type[0] == 'u' else '_signed' 2448 + suffix = 'ULL' if attr.type[0] == 'u' else 'LL' 2449 + cw.write_func_prot('static int', f'{c_lower(attr.enum_name)}_validate', 2450 + ['const struct nlattr *attr', 'struct netlink_ext_ack *extack']) 2451 + cw.block_start() 2452 + cw.block_start(line=f'switch (nla_get_{attr["type"]}(attr))') 2453 + enum = family.consts[attr['enum']] 2454 + first_entry = True 2455 + for entry in enum.entries.values(): 2456 + if first_entry: 2457 + first_entry = False 2458 + else: 2459 + cw.p('fallthrough;') 2460 + cw.p(f'case {entry.c_name}:') 2461 + cw.p('return 0;') 2462 + cw.block_end() 2463 + cw.p('NL_SET_ERR_MSG_ATTR(extack, attr, "invalid enum value");') 2464 + cw.p('return -EINVAL;') 2465 + cw.block_end() 2466 + cw.nl() 2467 + 2468 + 2434 2469 def print_kernel_op_table_fwd(family, cw, terminate): 2435 2470 exported = not kernel_can_gen_family_struct(family) 2436 2471 ··· 3142 3097 print_kernel_family_struct_hdr(parsed, cw) 3143 3098 else: 3144 3099 print_kernel_policy_ranges(parsed, cw) 3100 + print_kernel_policy_sparse_enum_validates(parsed, cw) 3145 3101 3146 3102 for _, struct in sorted(parsed.pure_nested_structs.items()): 3147 3103 if struct.request: