(ptype == BPF_PROG_TYPE_LSM && eatype == BPF_LSM_MAC);
 }
 
-void bpf_image_ksym_add(void *data, struct bpf_ksym *ksym)
+void bpf_image_ksym_add(void *data, unsigned int size, struct bpf_ksym *ksym)
 {
        ksym->start = (unsigned long) data;
-       ksym->end = ksym->start + PAGE_SIZE;
+       ksym->end = ksym->start + size;
        bpf_ksym_add(ksym);
        perf_event_ksymbol(PERF_RECORD_KSYMBOL_TYPE_BPF, ksym->start,
                           PAGE_SIZE, false, ksym->name);
 static void bpf_tramp_image_free(struct bpf_tramp_image *im)
 {
        bpf_image_ksym_del(&im->ksym);
-       arch_free_bpf_trampoline(im->image, PAGE_SIZE);
-       bpf_jit_uncharge_modmem(PAGE_SIZE);
+       arch_free_bpf_trampoline(im->image, im->size);
+       bpf_jit_uncharge_modmem(im->size);
        percpu_ref_exit(&im->pcref);
        kfree_rcu(im, rcu);
 }
        call_rcu_tasks_trace(&im->rcu, __bpf_tramp_image_put_rcu_tasks);
 }
 
-static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key)
+static struct bpf_tramp_image *bpf_tramp_image_alloc(u64 key, int size)
 {
        struct bpf_tramp_image *im;
        struct bpf_ksym *ksym;
        if (!im)
                goto out;
 
-       err = bpf_jit_charge_modmem(PAGE_SIZE);
+       err = bpf_jit_charge_modmem(size);
        if (err)
                goto out_free_im;
+       im->size = size;
 
        err = -ENOMEM;
-       im->image = image = arch_alloc_bpf_trampoline(PAGE_SIZE);
+       im->image = image = arch_alloc_bpf_trampoline(size);
        if (!image)
                goto out_uncharge;
 
        ksym = &im->ksym;
        INIT_LIST_HEAD_RCU(&ksym->lnode);
        snprintf(ksym->name, KSYM_NAME_LEN, "bpf_trampoline_%llu", key);
-       bpf_image_ksym_add(image, ksym);
+       bpf_image_ksym_add(image, size, ksym);
        return im;
 
 out_free_image:
-       arch_free_bpf_trampoline(im->image, PAGE_SIZE);
+       arch_free_bpf_trampoline(im->image, im->size);
 out_uncharge:
-       bpf_jit_uncharge_modmem(PAGE_SIZE);
+       bpf_jit_uncharge_modmem(size);
 out_free_im:
        kfree(im);
 out:
        struct bpf_tramp_links *tlinks;
        u32 orig_flags = tr->flags;
        bool ip_arg = false;
-       int err, total;
+       int err, total, size;
 
        tlinks = bpf_trampoline_get_progs(tr, &total, &ip_arg);
        if (IS_ERR(tlinks))
                goto out;
        }
 
-       im = bpf_tramp_image_alloc(tr->key);
-       if (IS_ERR(im)) {
-               err = PTR_ERR(im);
-               goto out;
-       }
-
        /* clear all bits except SHARE_IPMODIFY and TAIL_CALL_CTX */
        tr->flags &= (BPF_TRAMP_F_SHARE_IPMODIFY | BPF_TRAMP_F_TAIL_CALL_CTX);
 
                tr->flags |= BPF_TRAMP_F_ORIG_STACK;
 #endif
 
-       err = arch_prepare_bpf_trampoline(im, im->image, im->image + PAGE_SIZE,
+       size = arch_bpf_trampoline_size(&tr->func.model, tr->flags,
+                                       tlinks, tr->func.addr);
+       if (size < 0) {
+               err = size;
+               goto out;
+       }
+
+       if (size > PAGE_SIZE) {
+               err = -E2BIG;
+               goto out;
+       }
+
+       im = bpf_tramp_image_alloc(tr->key, size);
+       if (IS_ERR(im)) {
+               err = PTR_ERR(im);
+               goto out;
+       }
+
+       err = arch_prepare_bpf_trampoline(im, im->image, im->image + size,
                                          &tr->func.model, tr->flags, tlinks,
                                          tr->func.addr);
        if (err < 0)
                goto out_free;
 
-       arch_protect_bpf_trampoline(im->image, PAGE_SIZE);
+       arch_protect_bpf_trampoline(im->image, im->size);
 
        WARN_ON(tr->cur_image && total == 0);
        if (tr->cur_image)
                tr->fops->func = NULL;
                tr->fops->trampoline = 0;
 
-               /* reset im->image memory attr for arch_prepare_bpf_trampoline */
-               arch_unprotect_bpf_trampoline(im->image, PAGE_SIZE);
+               /* free im memory and reallocate later */
+               bpf_tramp_image_free(im);
                goto again;
        }
 #endif