if (fexit_cnt)
                flags = BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_SKIP_FRAME;
 
+       /* Though the second half of trampoline page is unused a task could be
+        * preempted in the middle of the first half of trampoline and two
+        * updates to trampoline would change the code from underneath the
+        * preempted task. Hence wait for tasks to voluntarily schedule or go
+        * to userspace.
+        */
+       synchronize_rcu_tasks();
+
        err = arch_prepare_bpf_trampoline(new_image, new_image + PAGE_SIZE / 2,
                                          &tr->func.model, flags,
                                          fentry, fentry_cnt,
                goto out;
        if (WARN_ON_ONCE(!hlist_empty(&tr->progs_hlist[BPF_TRAMP_FEXIT])))
                goto out;
+       /* wait for tasks to get out of trampoline before freeing it */
+       synchronize_rcu_tasks();
        bpf_jit_free_exec(tr->image);
        hlist_del(&tr->hlist);
        kfree(tr);