#include <linux/bsearch.h>
 #include <linux/kobject.h>
 #include <linux/sysfs.h>
+#include <linux/overflow.h>
 
 #include <net/netfilter/nf_bpf_link.h>
 
        /* This needs to be kzalloc to zero out padding and unused fields, see
         * comment in btf_record_equal.
         */
-       rec = kzalloc(offsetof(struct btf_record, fields[cnt]), GFP_KERNEL | __GFP_NOWARN);
+       rec = kzalloc(struct_size(rec, fields, cnt), GFP_KERNEL | __GFP_NOWARN);
        if (!rec)
                return ERR_PTR(-ENOMEM);
 
                if (id < 0)
                        continue;
 
-               new_aof = krealloc(aof, offsetof(struct btf_id_set, ids[aof->cnt + 1]),
+               new_aof = krealloc(aof, struct_size(new_aof, ids, aof->cnt + 1),
                                   GFP_KERNEL | __GFP_NOWARN);
                if (!new_aof) {
                        ret = -ENOMEM;
                if (ret != BTF_FIELD_FOUND)
                        continue;
 
-               new_aof = krealloc(aof, offsetof(struct btf_id_set, ids[aof->cnt + 1]),
+               new_aof = krealloc(aof, struct_size(new_aof, ids, aof->cnt + 1),
                                   GFP_KERNEL | __GFP_NOWARN);
                if (!new_aof) {
                        ret = -ENOMEM;
                continue;
        parse:
                tab_cnt = tab ? tab->cnt : 0;
-               new_tab = krealloc(tab, offsetof(struct btf_struct_metas, types[tab_cnt + 1]),
+               new_tab = krealloc(tab, struct_size(new_tab, types, tab_cnt + 1),
                                   GFP_KERNEL | __GFP_NOWARN);
                if (!new_tab) {
                        ret = -ENOMEM;
 
        /* Grow set */
        set = krealloc(tab->sets[hook],
-                      offsetof(struct btf_id_set8, pairs[set_cnt + add_set->cnt]),
+                      struct_size(set, pairs, set_cnt + add_set->cnt),
                       GFP_KERNEL | __GFP_NOWARN);
        if (!set) {
                ret = -ENOMEM;
        }
 
        tab = krealloc(btf->dtor_kfunc_tab,
-                      offsetof(struct btf_id_dtor_kfunc_tab, dtors[tab_cnt + add_cnt]),
+                      struct_size(tab, dtors, tab_cnt + add_cnt),
                       GFP_KERNEL | __GFP_NOWARN);
        if (!tab) {
                ret = -ENOMEM;
 
        tab = btf->struct_ops_tab;
        if (!tab) {
-               tab = kzalloc(offsetof(struct btf_struct_ops_tab, ops[4]),
-                             GFP_KERNEL);
+               tab = kzalloc(struct_size(tab, ops, 4), GFP_KERNEL);
                if (!tab)
                        return -ENOMEM;
                tab->capacity = 4;
 
        if (tab->cnt == tab->capacity) {
                new_tab = krealloc(tab,
-                                  offsetof(struct btf_struct_ops_tab,
-                                           ops[tab->capacity * 2]),
+                                  struct_size(tab, ops, tab->capacity * 2),
                                   GFP_KERNEL);
                if (!new_tab)
                        return -ENOMEM;