~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/mm/execmem.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * Copyright (C) 2002 Richard Henderson
  4  * Copyright (C) 2001 Rusty Russell, 2002, 2010 Rusty Russell IBM.
  5  * Copyright (C) 2023 Luis Chamberlain <mcgrof@kernel.org>
  6  * Copyright (C) 2024 Mike Rapoport IBM.
  7  */
  8 
  9 #include <linux/mm.h>
 10 #include <linux/vmalloc.h>
 11 #include <linux/execmem.h>
 12 #include <linux/moduleloader.h>
 13 
 14 static struct execmem_info *execmem_info __ro_after_init;
 15 static struct execmem_info default_execmem_info __ro_after_init;
 16 
 17 static void *__execmem_alloc(struct execmem_range *range, size_t size)
 18 {
 19         bool kasan = range->flags & EXECMEM_KASAN_SHADOW;
 20         unsigned long vm_flags  = VM_FLUSH_RESET_PERMS;
 21         gfp_t gfp_flags = GFP_KERNEL | __GFP_NOWARN;
 22         unsigned long start = range->start;
 23         unsigned long end = range->end;
 24         unsigned int align = range->alignment;
 25         pgprot_t pgprot = range->pgprot;
 26         void *p;
 27 
 28         if (kasan)
 29                 vm_flags |= VM_DEFER_KMEMLEAK;
 30 
 31         p = __vmalloc_node_range(size, align, start, end, gfp_flags,
 32                                  pgprot, vm_flags, NUMA_NO_NODE,
 33                                  __builtin_return_address(0));
 34         if (!p && range->fallback_start) {
 35                 start = range->fallback_start;
 36                 end = range->fallback_end;
 37                 p = __vmalloc_node_range(size, align, start, end, gfp_flags,
 38                                          pgprot, vm_flags, NUMA_NO_NODE,
 39                                          __builtin_return_address(0));
 40         }
 41 
 42         if (!p) {
 43                 pr_warn_ratelimited("execmem: unable to allocate memory\n");
 44                 return NULL;
 45         }
 46 
 47         if (kasan && (kasan_alloc_module_shadow(p, size, GFP_KERNEL) < 0)) {
 48                 vfree(p);
 49                 return NULL;
 50         }
 51 
 52         return kasan_reset_tag(p);
 53 }
 54 
 55 void *execmem_alloc(enum execmem_type type, size_t size)
 56 {
 57         struct execmem_range *range = &execmem_info->ranges[type];
 58 
 59         return __execmem_alloc(range, size);
 60 }
 61 
 62 void execmem_free(void *ptr)
 63 {
 64         /*
 65          * This memory may be RO, and freeing RO memory in an interrupt is not
 66          * supported by vmalloc.
 67          */
 68         WARN_ON(in_interrupt());
 69         vfree(ptr);
 70 }
 71 
 72 static bool execmem_validate(struct execmem_info *info)
 73 {
 74         struct execmem_range *r = &info->ranges[EXECMEM_DEFAULT];
 75 
 76         if (!r->alignment || !r->start || !r->end || !pgprot_val(r->pgprot)) {
 77                 pr_crit("Invalid parameters for execmem allocator, module loading will fail");
 78                 return false;
 79         }
 80 
 81         return true;
 82 }
 83 
 84 static void execmem_init_missing(struct execmem_info *info)
 85 {
 86         struct execmem_range *default_range = &info->ranges[EXECMEM_DEFAULT];
 87 
 88         for (int i = EXECMEM_DEFAULT + 1; i < EXECMEM_TYPE_MAX; i++) {
 89                 struct execmem_range *r = &info->ranges[i];
 90 
 91                 if (!r->start) {
 92                         if (i == EXECMEM_MODULE_DATA)
 93                                 r->pgprot = PAGE_KERNEL;
 94                         else
 95                                 r->pgprot = default_range->pgprot;
 96                         r->alignment = default_range->alignment;
 97                         r->start = default_range->start;
 98                         r->end = default_range->end;
 99                         r->flags = default_range->flags;
100                         r->fallback_start = default_range->fallback_start;
101                         r->fallback_end = default_range->fallback_end;
102                 }
103         }
104 }
105 
106 struct execmem_info * __weak execmem_arch_setup(void)
107 {
108         return NULL;
109 }
110 
111 static void __init __execmem_init(void)
112 {
113         struct execmem_info *info = execmem_arch_setup();
114 
115         if (!info) {
116                 info = execmem_info = &default_execmem_info;
117                 info->ranges[EXECMEM_DEFAULT].start = VMALLOC_START;
118                 info->ranges[EXECMEM_DEFAULT].end = VMALLOC_END;
119                 info->ranges[EXECMEM_DEFAULT].pgprot = PAGE_KERNEL_EXEC;
120                 info->ranges[EXECMEM_DEFAULT].alignment = 1;
121         }
122 
123         if (!execmem_validate(info))
124                 return;
125 
126         execmem_init_missing(info);
127 
128         execmem_info = info;
129 }
130 
131 #ifdef CONFIG_ARCH_WANTS_EXECMEM_LATE
132 static int __init execmem_late_init(void)
133 {
134         __execmem_init();
135         return 0;
136 }
137 core_initcall(execmem_late_init);
138 #else
139 void __init execmem_init(void)
140 {
141         __execmem_init();
142 }
143 #endif
144 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php