#include <linux/initrd.h>
 #include <linux/cpumask.h>
 #include <linux/gfp.h>
+#include <linux/execmem.h>
 
 #include <asm/asm.h>
 #include <asm/bios_ebda.h>
        pr_info("Write protecting kernel text and read-only data: %luk\n",
                size >> 10);
 
+       execmem_cache_make_ro();
+
        kernel_set_to_readonly = 1;
 
 #ifdef CONFIG_CPA_DEBUG
 
 #include <linux/gfp.h>
 #include <linux/kcore.h>
 #include <linux/bootmem_info.h>
+#include <linux/execmem.h>
 
 #include <asm/processor.h>
 #include <asm/bios_ebda.h>
               (end - start) >> 10);
        set_memory_ro(start, (end - start) >> PAGE_SHIFT);
 
+       execmem_cache_make_ro();
+
        kernel_set_to_readonly = 1;
 
        /*
 
        EXECMEM_ROX_CACHE       = (1 << 1),
 };
 
-#ifdef CONFIG_ARCH_HAS_EXECMEM_ROX
+#if defined(CONFIG_ARCH_HAS_EXECMEM_ROX) && defined(CONFIG_EXECMEM)
 /**
  * execmem_fill_trapping_insns - set memory to contain instructions that
  *                              will trap
  * Return: 0 on success or negative error code on failure.
  */
 int execmem_restore_rox(void *ptr, size_t size);
+
+/*
+ * Called from mark_readonly(), where the system transitions to ROX.
+ */
+void execmem_cache_make_ro(void);
 #else
 static inline int execmem_make_temp_rw(void *ptr, size_t size) { return 0; }
 static inline int execmem_restore_rox(void *ptr, size_t size) { return 0; }
+static inline void execmem_cache_make_ro(void) { }
 #endif
 
 /**
 
        return ptr;
 }
 
+static bool execmem_cache_rox = false;
+
+void execmem_cache_make_ro(void)
+{
+       struct maple_tree *free_areas = &execmem_cache.free_areas;
+       struct maple_tree *busy_areas = &execmem_cache.busy_areas;
+       MA_STATE(mas_free, free_areas, 0, ULONG_MAX);
+       MA_STATE(mas_busy, busy_areas, 0, ULONG_MAX);
+       struct mutex *mutex = &execmem_cache.mutex;
+       void *area;
+
+       execmem_cache_rox = true;
+
+       mutex_lock(mutex);
+
+       mas_for_each(&mas_free, area, ULONG_MAX) {
+               unsigned long pages = mas_range_len(&mas_free) >> PAGE_SHIFT;
+               set_memory_ro(mas_free.index, pages);
+       }
+
+       mas_for_each(&mas_busy, area, ULONG_MAX) {
+               unsigned long pages = mas_range_len(&mas_busy) >> PAGE_SHIFT;
+               set_memory_ro(mas_busy.index, pages);
+       }
+
+       mutex_unlock(mutex);
+}
+
 static int execmem_cache_populate(struct execmem_range *range, size_t size)
 {
        unsigned long vm_flags = VM_ALLOW_HUGE_VMAP;
        /* fill memory with instructions that will trap */
        execmem_fill_trapping_insns(p, alloc_size, /* writable = */ true);
 
-       err = set_memory_rox((unsigned long)p, vm->nr_pages);
-       if (err)
-               goto err_free_mem;
+       if (execmem_cache_rox) {
+               err = set_memory_rox((unsigned long)p, vm->nr_pages);
+               if (err)
+                       goto err_free_mem;
+       } else {
+               err = set_memory_x((unsigned long)p, vm->nr_pages);
+               if (err)
+                       goto err_free_mem;
+       }
 
        err = execmem_cache_add(p, alloc_size);
        if (err)