anakryiko wrote:

> tbh the whole relocatable enums is starting to feel like a big can of worms. 
> See inline comments for tradeoff and the main concern...
> 
> A missing enumerator is normal for the enums that need the relocation. Take 
> `enum cgroup_subsys_id`. Its values shift because Kconfig removes enumerators.
> 
>   switch (ssid) {
>   case memory_cgrp_id:
>     return 1;
>   case hugetlb_cgrp_id:
>     return 2;
>   }
> A kernel without `CONFIG_CGROUP_HUGETLB` has no `hugetlb_cgrp_id`. libbpf 
> poisons the `ld_imm64` for that label, so the verifier rejects the whole 
> prog. `-fbpf-no-enum-reloc=` cannot be used, since this enum actually needs 
> the relocation.

I think this *should* fail to verify if kernel doesn't know have 
hugetlb_cgrp_id, if the code is written this way. Realistically, your switch 
should only have enumerators you expect always to be present, and if you want 
to handle something that might not be there, you'll have to handle that with 
existence check with explicit if/else:

```
if (bpf_core_enum_value_exists(enum cgroup_subsys_id, hugetlb_cgpr_id) &&
     ssid == hugetlb_cgrp_id /* relocatable, poisoned if doesn't exist, but 
unreachable then */) {
    ...
}
```


I'd also argue that switches like that, if they have to be exhaustive (whatever 
that warning is for not all possible cases handled in switch), they should 
always have `default:` case, even if we listed all currently known enumerators. 
Because future kernels can add another enumerator which we don't even know 
about, so there is always default cause that's possible (at least can't be 
ruled out).

https://github.com/llvm/llvm-project/pull/243338
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to