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

TOMOYO Linux Cross Reference
Linux/mm/memtest.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 #include <linux/kernel.h>
  3 #include <linux/types.h>
  4 #include <linux/init.h>
  5 #include <linux/memblock.h>
  6 #include <linux/seq_file.h>
  7 
  8 static bool early_memtest_done;
  9 static phys_addr_t early_memtest_bad_size;
 10 
 11 static u64 patterns[] __initdata = {
 12         /* The first entry has to be 0 to leave memtest with zeroed memory */
 13         0,
 14         0xffffffffffffffffULL,
 15         0x5555555555555555ULL,
 16         0xaaaaaaaaaaaaaaaaULL,
 17         0x1111111111111111ULL,
 18         0x2222222222222222ULL,
 19         0x4444444444444444ULL,
 20         0x8888888888888888ULL,
 21         0x3333333333333333ULL,
 22         0x6666666666666666ULL,
 23         0x9999999999999999ULL,
 24         0xccccccccccccccccULL,
 25         0x7777777777777777ULL,
 26         0xbbbbbbbbbbbbbbbbULL,
 27         0xddddddddddddddddULL,
 28         0xeeeeeeeeeeeeeeeeULL,
 29         0x7a6c7258554e494cULL, /* yeah ;-) */
 30 };
 31 
 32 static void __init reserve_bad_mem(u64 pattern, phys_addr_t start_bad, phys_addr_t end_bad)
 33 {
 34         pr_info("  %016llx bad mem addr %pa - %pa reserved\n",
 35                 cpu_to_be64(pattern), &start_bad, &end_bad);
 36         memblock_reserve(start_bad, end_bad - start_bad);
 37         early_memtest_bad_size += (end_bad - start_bad);
 38 }
 39 
 40 static void __init memtest(u64 pattern, phys_addr_t start_phys, phys_addr_t size)
 41 {
 42         u64 *p, *start, *end;
 43         phys_addr_t start_bad, last_bad;
 44         phys_addr_t start_phys_aligned;
 45         const size_t incr = sizeof(pattern);
 46 
 47         start_phys_aligned = ALIGN(start_phys, incr);
 48         start = __va(start_phys_aligned);
 49         end = start + (size - (start_phys_aligned - start_phys)) / incr;
 50         start_bad = 0;
 51         last_bad = 0;
 52 
 53         for (p = start; p < end; p++)
 54                 WRITE_ONCE(*p, pattern);
 55 
 56         for (p = start; p < end; p++, start_phys_aligned += incr) {
 57                 if (READ_ONCE(*p) == pattern)
 58                         continue;
 59                 if (start_phys_aligned == last_bad + incr) {
 60                         last_bad += incr;
 61                         continue;
 62                 }
 63                 if (start_bad)
 64                         reserve_bad_mem(pattern, start_bad, last_bad + incr);
 65                 start_bad = last_bad = start_phys_aligned;
 66         }
 67         if (start_bad)
 68                 reserve_bad_mem(pattern, start_bad, last_bad + incr);
 69 
 70         early_memtest_done = true;
 71 }
 72 
 73 static void __init do_one_pass(u64 pattern, phys_addr_t start, phys_addr_t end)
 74 {
 75         u64 i;
 76         phys_addr_t this_start, this_end;
 77 
 78         for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &this_start,
 79                                 &this_end, NULL) {
 80                 this_start = clamp(this_start, start, end);
 81                 this_end = clamp(this_end, start, end);
 82                 if (this_start < this_end) {
 83                         pr_info("  %pa - %pa pattern %016llx\n",
 84                                 &this_start, &this_end, cpu_to_be64(pattern));
 85                         memtest(pattern, this_start, this_end - this_start);
 86                 }
 87         }
 88 }
 89 
 90 /* default is disabled */
 91 static unsigned int memtest_pattern __initdata;
 92 
 93 static int __init parse_memtest(char *arg)
 94 {
 95         int ret = 0;
 96 
 97         if (arg)
 98                 ret = kstrtouint(arg, 0, &memtest_pattern);
 99         else
100                 memtest_pattern = ARRAY_SIZE(patterns);
101 
102         return ret;
103 }
104 
105 early_param("memtest", parse_memtest);
106 
107 void __init early_memtest(phys_addr_t start, phys_addr_t end)
108 {
109         unsigned int i;
110         unsigned int idx = 0;
111 
112         if (!memtest_pattern)
113                 return;
114 
115         pr_info("early_memtest: # of tests: %u\n", memtest_pattern);
116         for (i = memtest_pattern-1; i < UINT_MAX; --i) {
117                 idx = i % ARRAY_SIZE(patterns);
118                 do_one_pass(patterns[idx], start, end);
119         }
120 }
121 
122 void memtest_report_meminfo(struct seq_file *m)
123 {
124         unsigned long early_memtest_bad_size_kb;
125 
126         if (!IS_ENABLED(CONFIG_PROC_FS))
127                 return;
128 
129         if (!early_memtest_done)
130                 return;
131 
132         early_memtest_bad_size_kb = early_memtest_bad_size >> 10;
133         if (early_memtest_bad_size && !early_memtest_bad_size_kb)
134                 early_memtest_bad_size_kb = 1;
135         /* When 0 is reported, it means there actually was a successful test */
136         seq_printf(m, "EarlyMemtestBad:   %5lu kB\n", early_memtest_bad_size_kb);
137 }
138 

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