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

TOMOYO Linux Cross Reference
Linux/mm/kmsan/init.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ 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.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /mm/kmsan/init.c (Version linux-6.12-rc7) and /mm/kmsan/init.c (Version linux-6.8.12)


  1 // SPDX-License-Identifier: GPL-2.0                 1 // SPDX-License-Identifier: GPL-2.0
  2 /*                                                  2 /*
  3  * KMSAN initialization routines.                   3  * KMSAN initialization routines.
  4  *                                                  4  *
  5  * Copyright (C) 2017-2021 Google LLC               5  * Copyright (C) 2017-2021 Google LLC
  6  * Author: Alexander Potapenko <glider@google.      6  * Author: Alexander Potapenko <glider@google.com>
  7  *                                                  7  *
  8  */                                                 8  */
  9                                                     9 
 10 #include "kmsan.h"                                 10 #include "kmsan.h"
 11                                                    11 
 12 #include <asm/sections.h>                          12 #include <asm/sections.h>
 13 #include <linux/mm.h>                              13 #include <linux/mm.h>
 14 #include <linux/memblock.h>                        14 #include <linux/memblock.h>
 15                                                    15 
 16 #include "../internal.h"                           16 #include "../internal.h"
 17                                                    17 
 18 #define NUM_FUTURE_RANGES 128                      18 #define NUM_FUTURE_RANGES 128
 19 struct start_end_pair {                            19 struct start_end_pair {
 20         u64 start, end;                            20         u64 start, end;
 21 };                                                 21 };
 22                                                    22 
 23 static struct start_end_pair start_end_pairs[N     23 static struct start_end_pair start_end_pairs[NUM_FUTURE_RANGES] __initdata;
 24 static int future_index __initdata;                24 static int future_index __initdata;
 25                                                    25 
 26 /*                                                 26 /*
 27  * Record a range of memory for which the meta     27  * Record a range of memory for which the metadata pages will be created once
 28  * the page allocator becomes available.           28  * the page allocator becomes available.
 29  */                                                29  */
 30 static void __init kmsan_record_future_shadow_     30 static void __init kmsan_record_future_shadow_range(void *start, void *end)
 31 {                                                  31 {
 32         u64 nstart = (u64)start, nend = (u64)e     32         u64 nstart = (u64)start, nend = (u64)end, cstart, cend;
 33         bool merged = false;                       33         bool merged = false;
 34                                                    34 
 35         KMSAN_WARN_ON(future_index == NUM_FUTU     35         KMSAN_WARN_ON(future_index == NUM_FUTURE_RANGES);
 36         KMSAN_WARN_ON((nstart >= nend) ||      !!  36         KMSAN_WARN_ON((nstart >= nend) || !nstart || !nend);
 37                       /* Virtual address 0 is  << 
 38                       (!IS_ENABLED(CONFIG_S390 << 
 39                       !nend);                  << 
 40         nstart = ALIGN_DOWN(nstart, PAGE_SIZE)     37         nstart = ALIGN_DOWN(nstart, PAGE_SIZE);
 41         nend = ALIGN(nend, PAGE_SIZE);             38         nend = ALIGN(nend, PAGE_SIZE);
 42                                                    39 
 43         /*                                         40         /*
 44          * Scan the existing ranges to see if      41          * Scan the existing ranges to see if any of them overlaps with
 45          * [start, end). In that case, merge t     42          * [start, end). In that case, merge the two ranges instead of
 46          * creating a new one.                     43          * creating a new one.
 47          * The number of ranges is less than 2     44          * The number of ranges is less than 20, so there is no need to organize
 48          * them into a more intelligent data s     45          * them into a more intelligent data structure.
 49          */                                        46          */
 50         for (int i = 0; i < future_index; i++)     47         for (int i = 0; i < future_index; i++) {
 51                 cstart = start_end_pairs[i].st     48                 cstart = start_end_pairs[i].start;
 52                 cend = start_end_pairs[i].end;     49                 cend = start_end_pairs[i].end;
 53                 if ((cstart < nstart && cend <     50                 if ((cstart < nstart && cend < nstart) ||
 54                     (cstart > nend && cend > n     51                     (cstart > nend && cend > nend))
 55                         /* ranges are disjoint     52                         /* ranges are disjoint - do not merge */
 56                         continue;                  53                         continue;
 57                 start_end_pairs[i].start = min     54                 start_end_pairs[i].start = min(nstart, cstart);
 58                 start_end_pairs[i].end = max(n     55                 start_end_pairs[i].end = max(nend, cend);
 59                 merged = true;                     56                 merged = true;
 60                 break;                             57                 break;
 61         }                                          58         }
 62         if (merged)                                59         if (merged)
 63                 return;                            60                 return;
 64         start_end_pairs[future_index].start =      61         start_end_pairs[future_index].start = nstart;
 65         start_end_pairs[future_index].end = ne     62         start_end_pairs[future_index].end = nend;
 66         future_index++;                            63         future_index++;
 67 }                                                  64 }
 68                                                    65 
 69 /*                                                 66 /*
 70  * Initialize the shadow for existing mappings     67  * Initialize the shadow for existing mappings during kernel initialization.
 71  * These include kernel text/data sections, NO     68  * These include kernel text/data sections, NODE_DATA and future ranges
 72  * registered while creating other data (e.g.      69  * registered while creating other data (e.g. percpu).
 73  *                                                 70  *
 74  * Allocations via memblock can be only done b     71  * Allocations via memblock can be only done before slab is initialized.
 75  */                                                72  */
 76 void __init kmsan_init_shadow(void)                73 void __init kmsan_init_shadow(void)
 77 {                                                  74 {
 78         const size_t nd_size = sizeof(pg_data_ !!  75         const size_t nd_size = roundup(sizeof(pg_data_t), PAGE_SIZE);
 79         phys_addr_t p_start, p_end;                76         phys_addr_t p_start, p_end;
 80         u64 loop;                                  77         u64 loop;
 81         int nid;                                   78         int nid;
 82                                                    79 
 83         for_each_reserved_mem_range(loop, &p_s     80         for_each_reserved_mem_range(loop, &p_start, &p_end)
 84                 kmsan_record_future_shadow_ran     81                 kmsan_record_future_shadow_range(phys_to_virt(p_start),
 85                                                    82                                                  phys_to_virt(p_end));
 86         /* Allocate shadow for .data */            83         /* Allocate shadow for .data */
 87         kmsan_record_future_shadow_range(_sdat     84         kmsan_record_future_shadow_range(_sdata, _edata);
 88                                                    85 
 89         for_each_online_node(nid)                  86         for_each_online_node(nid)
 90                 kmsan_record_future_shadow_ran     87                 kmsan_record_future_shadow_range(
 91                         NODE_DATA(nid), (char      88                         NODE_DATA(nid), (char *)NODE_DATA(nid) + nd_size);
 92                                                    89 
 93         for (int i = 0; i < future_index; i++)     90         for (int i = 0; i < future_index; i++)
 94                 kmsan_init_alloc_meta_for_rang     91                 kmsan_init_alloc_meta_for_range(
 95                         (void *)start_end_pair     92                         (void *)start_end_pairs[i].start,
 96                         (void *)start_end_pair     93                         (void *)start_end_pairs[i].end);
 97 }                                                  94 }
 98                                                    95 
 99 struct metadata_page_pair {                        96 struct metadata_page_pair {
100         struct page *shadow, *origin;              97         struct page *shadow, *origin;
101 };                                                 98 };
102 static struct metadata_page_pair held_back[NR_     99 static struct metadata_page_pair held_back[NR_PAGE_ORDERS] __initdata;
103                                                   100 
104 /*                                                101 /*
105  * Eager metadata allocation. When the membloc    102  * Eager metadata allocation. When the memblock allocator is freeing pages to
106  * pagealloc, we use 2/3 of them as metadata f    103  * pagealloc, we use 2/3 of them as metadata for the remaining 1/3.
107  * We store the pointers to the returned block    104  * We store the pointers to the returned blocks of pages in held_back[] grouped
108  * by their order: when kmsan_memblock_free_pa    105  * by their order: when kmsan_memblock_free_pages() is called for the first
109  * time with a certain order, it is reserved a    106  * time with a certain order, it is reserved as a shadow block, for the second
110  * time - as an origin block. On the third tim    107  * time - as an origin block. On the third time the incoming block receives its
111  * shadow and origin ranges from the previousl    108  * shadow and origin ranges from the previously saved shadow and origin blocks,
112  * after which held_back[order] can be used ag    109  * after which held_back[order] can be used again.
113  *                                                110  *
114  * At the very end there may be leftover block    111  * At the very end there may be leftover blocks in held_back[]. They are
115  * collected later by kmsan_memblock_discard()    112  * collected later by kmsan_memblock_discard().
116  */                                               113  */
117 bool kmsan_memblock_free_pages(struct page *pa    114 bool kmsan_memblock_free_pages(struct page *page, unsigned int order)
118 {                                                 115 {
119         struct page *shadow, *origin;             116         struct page *shadow, *origin;
120                                                   117 
121         if (!held_back[order].shadow) {           118         if (!held_back[order].shadow) {
122                 held_back[order].shadow = page    119                 held_back[order].shadow = page;
123                 return false;                     120                 return false;
124         }                                         121         }
125         if (!held_back[order].origin) {           122         if (!held_back[order].origin) {
126                 held_back[order].origin = page    123                 held_back[order].origin = page;
127                 return false;                     124                 return false;
128         }                                         125         }
129         shadow = held_back[order].shadow;         126         shadow = held_back[order].shadow;
130         origin = held_back[order].origin;         127         origin = held_back[order].origin;
131         kmsan_setup_meta(page, shadow, origin,    128         kmsan_setup_meta(page, shadow, origin, order);
132                                                   129 
133         held_back[order].shadow = NULL;           130         held_back[order].shadow = NULL;
134         held_back[order].origin = NULL;           131         held_back[order].origin = NULL;
135         return true;                              132         return true;
136 }                                                 133 }
137                                                   134 
138 #define MAX_BLOCKS 8                              135 #define MAX_BLOCKS 8
139 struct smallstack {                               136 struct smallstack {
140         struct page *items[MAX_BLOCKS];           137         struct page *items[MAX_BLOCKS];
141         int index;                                138         int index;
142         int order;                                139         int order;
143 };                                                140 };
144                                                   141 
145 static struct smallstack collect = {              142 static struct smallstack collect = {
146         .index = 0,                               143         .index = 0,
147         .order = MAX_PAGE_ORDER,                  144         .order = MAX_PAGE_ORDER,
148 };                                                145 };
149                                                   146 
150 static void smallstack_push(struct smallstack     147 static void smallstack_push(struct smallstack *stack, struct page *pages)
151 {                                                 148 {
152         KMSAN_WARN_ON(stack->index == MAX_BLOC    149         KMSAN_WARN_ON(stack->index == MAX_BLOCKS);
153         stack->items[stack->index] = pages;       150         stack->items[stack->index] = pages;
154         stack->index++;                           151         stack->index++;
155 }                                                 152 }
156 #undef MAX_BLOCKS                                 153 #undef MAX_BLOCKS
157                                                   154 
158 static struct page *smallstack_pop(struct smal    155 static struct page *smallstack_pop(struct smallstack *stack)
159 {                                                 156 {
160         struct page *ret;                         157         struct page *ret;
161                                                   158 
162         KMSAN_WARN_ON(stack->index == 0);         159         KMSAN_WARN_ON(stack->index == 0);
163         stack->index--;                           160         stack->index--;
164         ret = stack->items[stack->index];         161         ret = stack->items[stack->index];
165         stack->items[stack->index] = NULL;        162         stack->items[stack->index] = NULL;
166         return ret;                               163         return ret;
167 }                                                 164 }
168                                                   165 
169 static void do_collection(void)                   166 static void do_collection(void)
170 {                                                 167 {
171         struct page *page, *shadow, *origin;      168         struct page *page, *shadow, *origin;
172                                                   169 
173         while (collect.index >= 3) {              170         while (collect.index >= 3) {
174                 page = smallstack_pop(&collect    171                 page = smallstack_pop(&collect);
175                 shadow = smallstack_pop(&colle    172                 shadow = smallstack_pop(&collect);
176                 origin = smallstack_pop(&colle    173                 origin = smallstack_pop(&collect);
177                 kmsan_setup_meta(page, shadow,    174                 kmsan_setup_meta(page, shadow, origin, collect.order);
178                 __free_pages_core(page, collec !! 175                 __free_pages_core(page, collect.order);
179         }                                         176         }
180 }                                                 177 }
181                                                   178 
182 static void collect_split(void)                   179 static void collect_split(void)
183 {                                                 180 {
184         struct smallstack tmp = {                 181         struct smallstack tmp = {
185                 .order = collect.order - 1,       182                 .order = collect.order - 1,
186                 .index = 0,                       183                 .index = 0,
187         };                                        184         };
188         struct page *page;                        185         struct page *page;
189                                                   186 
190         if (!collect.order)                       187         if (!collect.order)
191                 return;                           188                 return;
192         while (collect.index) {                   189         while (collect.index) {
193                 page = smallstack_pop(&collect    190                 page = smallstack_pop(&collect);
194                 smallstack_push(&tmp, &page[0]    191                 smallstack_push(&tmp, &page[0]);
195                 smallstack_push(&tmp, &page[1     192                 smallstack_push(&tmp, &page[1 << tmp.order]);
196         }                                         193         }
197         __memcpy(&collect, &tmp, sizeof(tmp));    194         __memcpy(&collect, &tmp, sizeof(tmp));
198 }                                                 195 }
199                                                   196 
200 /*                                                197 /*
201  * Memblock is about to go away. Split the pag    198  * Memblock is about to go away. Split the page blocks left over in held_back[]
202  * and return 1/3 of that memory to the system    199  * and return 1/3 of that memory to the system.
203  */                                               200  */
204 static void kmsan_memblock_discard(void)          201 static void kmsan_memblock_discard(void)
205 {                                                 202 {
206         /*                                        203         /*
207          * For each order=N:                      204          * For each order=N:
208          *  - push held_back[N].shadow and .or    205          *  - push held_back[N].shadow and .origin to @collect;
209          *  - while there are >= 3 elements in    206          *  - while there are >= 3 elements in @collect, do garbage collection:
210          *    - pop 3 ranges from @collect;       207          *    - pop 3 ranges from @collect;
211          *    - use two of them as shadow and     208          *    - use two of them as shadow and origin for the third one;
212          *    - repeat;                           209          *    - repeat;
213          *  - split each remaining element fro    210          *  - split each remaining element from @collect into 2 ranges of
214          *    order=N-1,                          211          *    order=N-1,
215          *  - repeat.                             212          *  - repeat.
216          */                                       213          */
217         collect.order = MAX_PAGE_ORDER;           214         collect.order = MAX_PAGE_ORDER;
218         for (int i = MAX_PAGE_ORDER; i >= 0; i    215         for (int i = MAX_PAGE_ORDER; i >= 0; i--) {
219                 if (held_back[i].shadow)          216                 if (held_back[i].shadow)
220                         smallstack_push(&colle    217                         smallstack_push(&collect, held_back[i].shadow);
221                 if (held_back[i].origin)          218                 if (held_back[i].origin)
222                         smallstack_push(&colle    219                         smallstack_push(&collect, held_back[i].origin);
223                 held_back[i].shadow = NULL;       220                 held_back[i].shadow = NULL;
224                 held_back[i].origin = NULL;       221                 held_back[i].origin = NULL;
225                 do_collection();                  222                 do_collection();
226                 collect_split();                  223                 collect_split();
227         }                                         224         }
228 }                                                 225 }
229                                                   226 
230 void __init kmsan_init_runtime(void)              227 void __init kmsan_init_runtime(void)
231 {                                                 228 {
232         /* Assuming current is init_task */       229         /* Assuming current is init_task */
233         kmsan_internal_task_create(current);      230         kmsan_internal_task_create(current);
234         kmsan_memblock_discard();                 231         kmsan_memblock_discard();
235         pr_info("Starting KernelMemorySanitize    232         pr_info("Starting KernelMemorySanitizer\n");
236         pr_info("ATTENTION: KMSAN is a debuggi    233         pr_info("ATTENTION: KMSAN is a debugging tool! Do not use it on production machines!\n");
237         kmsan_enabled = true;                     234         kmsan_enabled = true;
238 }                                                 235 }
239                                                   236 

~ [ 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