/* If building the body of the JITed code fails somehow,
         * we fall back to the interpretation.
         */
-       if (build_body(&ctx) < 0) {
-               image_ptr = NULL;
-               bpf_jit_binary_free(header);
-               prog = orig_prog;
-               goto out_imms;
-       }
+       if (build_body(&ctx) < 0)
+               goto out_free;
        build_epilogue(&ctx);
 
        /* 3.) Extra pass to validate JITed Code */
-       if (validate_code(&ctx)) {
-               image_ptr = NULL;
-               bpf_jit_binary_free(header);
-               prog = orig_prog;
-               goto out_imms;
-       }
+       if (validate_code(&ctx))
+               goto out_free;
        flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
 
        if (bpf_jit_enable > 1)
                /* there are 2 passes here */
                bpf_jit_dump(prog->len, image_size, 2, ctx.target);
 
-       bpf_jit_binary_lock_ro(header);
+       if (bpf_jit_binary_lock_ro(header))
+               goto out_free;
        prog->bpf_func = (void *)ctx.target;
        prog->jited = 1;
        prog->jited_len = image_size;
                bpf_jit_prog_release_other(prog, prog == orig_prog ?
                                           tmp : orig_prog);
        return prog;
+
+out_free:
+       image_ptr = NULL;
+       bpf_jit_binary_free(header);
+       prog = orig_prog;
+       goto out_imms;
 }
 
 
        flush_icache_range((unsigned long)header, (unsigned long)(ctx.image + ctx.idx));
 
        if (!prog->is_func || extra_pass) {
+               int err;
+
                if (extra_pass && ctx.idx != jit_data->ctx.idx) {
                        pr_err_once("multi-func JIT bug %d != %d\n",
                                    ctx.idx, jit_data->ctx.idx);
-                       bpf_jit_binary_free(header);
-                       prog->bpf_func = NULL;
-                       prog->jited = 0;
-                       prog->jited_len = 0;
-                       goto out_offset;
+                       goto out_free;
+               }
+               err = bpf_jit_binary_lock_ro(header);
+               if (err) {
+                       pr_err_once("bpf_jit_binary_lock_ro() returned %d\n",
+                                   err);
+                       goto out_free;
                }
-               bpf_jit_binary_lock_ro(header);
        } else {
                jit_data->ctx = ctx;
                jit_data->image = image_ptr;
        out_offset = -1;
 
        return prog;
+
+out_free:
+       bpf_jit_binary_free(header);
+       prog->bpf_func = NULL;
+       prog->jited = 0;
+       prog->jited_len = 0;
+       goto out_offset;
 }
 
 /* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */
 
        bpf_prog_fill_jited_linfo(prog, &ctx.descriptors[1]);
 
        /* Set as read-only exec and flush instruction cache */
-       bpf_jit_binary_lock_ro(header);
+       if (bpf_jit_binary_lock_ro(header))
+               goto out_err;
        flush_icache_range((unsigned long)header,
                           (unsigned long)&ctx.target[ctx.jit_index]);
 
 
        bpf_flush_icache(jit_data->header, ctx->insns + ctx->ninsns);
 
        if (!prog->is_func || extra_pass) {
-               bpf_jit_binary_lock_ro(jit_data->header);
+               if (bpf_jit_binary_lock_ro(jit_data->header)) {
+                       bpf_jit_binary_free(jit_data->header);
+                       prog->bpf_func = NULL;
+                       prog->jited = 0;
+                       prog->jited_len = 0;
+                       goto out_offset;
+               }
                prologue_len = ctx->epilogue_offset - ctx->body_len;
                for (i = 0; i < prog->len; i++)
                        ctx->offset[i] += prologue_len;
 
                print_fn_code(jit.prg_buf, jit.size_prg);
        }
        if (!fp->is_func || extra_pass) {
-               bpf_jit_binary_lock_ro(header);
+               if (bpf_jit_binary_lock_ro(header)) {
+                       bpf_jit_binary_free(header);
+                       fp = orig_fp;
+                       goto free_addrs;
+               }
        } else {
                jit_data->header = header;
                jit_data->ctx = jit;
 
        bpf_flush_icache(header, (u8 *)header + header->size);
 
        if (!prog->is_func || extra_pass) {
-               bpf_jit_binary_lock_ro(header);
+               if (bpf_jit_binary_lock_ro(header)) {
+                       bpf_jit_binary_free(header);
+                       prog = orig_prog;
+                       goto out_off;
+               }
        } else {
                jit_data->ctx = ctx;
                jit_data->image = image_ptr;
 
        if (bpf_jit_enable > 1)
                bpf_jit_dump(prog->len, proglen, pass + 1, image);
 
-       if (image) {
-               bpf_jit_binary_lock_ro(header);
+       if (image && !bpf_jit_binary_lock_ro(header)) {
                prog->bpf_func = (void *)image;
                prog->jited = 1;
                prog->jited_len = proglen;
 
        return 0;
 }
 
-static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
+static inline int __must_check
+bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
 {
        set_vm_flush_reset_perms(hdr);
-       set_memory_rox((unsigned long)hdr, hdr->size >> PAGE_SHIFT);
+       return set_memory_rox((unsigned long)hdr, hdr->size >> PAGE_SHIFT);
 }
 
 int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);