u32 htid, flags = 0;
        size_t sel_size;
        int err;
-#ifdef CONFIG_CLS_U32_PERF
-       size_t size;
-#endif
 
        if (!opt) {
                if (handle) {
                goto erridr;
        }
 
-       n = kzalloc(offsetof(typeof(*n), sel) + sel_size, GFP_KERNEL);
+       n = kzalloc(struct_size(n, sel.keys, s->nkeys), GFP_KERNEL);
        if (n == NULL) {
                err = -ENOBUFS;
                goto erridr;
        }
 
 #ifdef CONFIG_CLS_U32_PERF
-       size = sizeof(struct tc_u32_pcnt) + s->nkeys * sizeof(u64);
-       n->pf = __alloc_percpu(size, __alignof__(struct tc_u32_pcnt));
+       n->pf = __alloc_percpu(struct_size(n->pf, kcnts, s->nkeys),
+                              __alignof__(struct tc_u32_pcnt));
        if (!n->pf) {
                err = -ENOBUFS;
                goto errfree;
                int cpu;
 #endif
 
-               if (nla_put(skb, TCA_U32_SEL,
-                           sizeof(n->sel) + n->sel.nkeys*sizeof(struct tc_u32_key),
+               if (nla_put(skb, TCA_U32_SEL, struct_size(&n->sel, keys, n->sel.nkeys),
                            &n->sel))
                        goto nla_put_failure;
 
                                goto nla_put_failure;
                }
 #ifdef CONFIG_CLS_U32_PERF
-               gpf = kzalloc(sizeof(struct tc_u32_pcnt) +
-                             n->sel.nkeys * sizeof(u64),
-                             GFP_KERNEL);
+               gpf = kzalloc(struct_size(gpf, kcnts, n->sel.nkeys), GFP_KERNEL);
                if (!gpf)
                        goto nla_put_failure;
 
                                gpf->kcnts[i] += pf->kcnts[i];
                }
 
-               if (nla_put_64bit(skb, TCA_U32_PCNT,
-                                 sizeof(struct tc_u32_pcnt) +
-                                 n->sel.nkeys * sizeof(u64),
+               if (nla_put_64bit(skb, TCA_U32_PCNT, struct_size(gpf, kcnts, n->sel.nkeys),
                                  gpf, TCA_U32_PAD)) {
                        kfree(gpf);
                        goto nla_put_failure;