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

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/powerpc/security/rfi_flush.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 /*
  4  * Copyright 2018 IBM Corporation.
  5  */
  6 
  7 #define __SANE_USERSPACE_TYPES__
  8 
  9 #include <sys/types.h>
 10 #include <stdint.h>
 11 #include <malloc.h>
 12 #include <unistd.h>
 13 #include <stdlib.h>
 14 #include <string.h>
 15 #include <stdio.h>
 16 #include "utils.h"
 17 #include "flush_utils.h"
 18 
 19 
 20 int rfi_flush_test(void)
 21 {
 22         char *p;
 23         int repetitions = 10;
 24         int fd, passes = 0, iter, rc = 0;
 25         struct perf_event_read v;
 26         __u64 l1d_misses_total = 0;
 27         unsigned long iterations = 100000, zero_size = 24 * 1024;
 28         unsigned long l1d_misses_expected;
 29         int rfi_flush_orig, rfi_flush;
 30         int have_entry_flush, entry_flush_orig;
 31 
 32         SKIP_IF(geteuid() != 0);
 33 
 34         // The PMU event we use only works on Power7 or later
 35         SKIP_IF(!have_hwcap(PPC_FEATURE_ARCH_2_06));
 36 
 37         if (read_debugfs_int("powerpc/rfi_flush", &rfi_flush_orig) < 0) {
 38                 perror("Unable to read powerpc/rfi_flush debugfs file");
 39                 SKIP_IF(1);
 40         }
 41 
 42         if (read_debugfs_int("powerpc/entry_flush", &entry_flush_orig) < 0) {
 43                 have_entry_flush = 0;
 44         } else {
 45                 have_entry_flush = 1;
 46 
 47                 if (entry_flush_orig != 0) {
 48                         if (write_debugfs_int("powerpc/entry_flush", 0) < 0) {
 49                                 perror("error writing to powerpc/entry_flush debugfs file");
 50                                 return 1;
 51                         }
 52                 }
 53         }
 54 
 55         rfi_flush = rfi_flush_orig;
 56 
 57         fd = perf_event_open_counter(PERF_TYPE_HW_CACHE, PERF_L1D_READ_MISS_CONFIG, -1);
 58         FAIL_IF(fd < 0);
 59 
 60         p = (char *)memalign(zero_size, CACHELINE_SIZE);
 61 
 62         FAIL_IF(perf_event_enable(fd));
 63 
 64         // disable L1 prefetching
 65         set_dscr(1);
 66 
 67         iter = repetitions;
 68 
 69         /*
 70          * We expect to see l1d miss for each cacheline access when rfi_flush
 71          * is set. Allow a small variation on this.
 72          */
 73         l1d_misses_expected = iterations * (zero_size / CACHELINE_SIZE - 2);
 74 
 75 again:
 76         FAIL_IF(perf_event_reset(fd));
 77 
 78         syscall_loop(p, iterations, zero_size);
 79 
 80         FAIL_IF(read(fd, &v, sizeof(v)) != sizeof(v));
 81 
 82         if (rfi_flush && v.l1d_misses >= l1d_misses_expected)
 83                 passes++;
 84         else if (!rfi_flush && v.l1d_misses < (l1d_misses_expected / 2))
 85                 passes++;
 86 
 87         l1d_misses_total += v.l1d_misses;
 88 
 89         while (--iter)
 90                 goto again;
 91 
 92         if (passes < repetitions) {
 93                 printf("FAIL (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d failures]\n",
 94                        rfi_flush, l1d_misses_total, rfi_flush ? '<' : '>',
 95                        rfi_flush ? repetitions * l1d_misses_expected :
 96                        repetitions * l1d_misses_expected / 2,
 97                        repetitions - passes, repetitions);
 98                 rc = 1;
 99         } else
100                 printf("PASS (L1D misses with rfi_flush=%d: %llu %c %lu) [%d/%d pass]\n",
101                        rfi_flush, l1d_misses_total, rfi_flush ? '>' : '<',
102                        rfi_flush ? repetitions * l1d_misses_expected :
103                        repetitions * l1d_misses_expected / 2,
104                        passes, repetitions);
105 
106         if (rfi_flush == rfi_flush_orig) {
107                 rfi_flush = !rfi_flush_orig;
108                 if (write_debugfs_int("powerpc/rfi_flush", rfi_flush) < 0) {
109                         perror("error writing to powerpc/rfi_flush debugfs file");
110                         return 1;
111                 }
112                 iter = repetitions;
113                 l1d_misses_total = 0;
114                 passes = 0;
115                 goto again;
116         }
117 
118         perf_event_disable(fd);
119         close(fd);
120 
121         set_dscr(0);
122 
123         if (write_debugfs_int("powerpc/rfi_flush", rfi_flush_orig) < 0) {
124                 perror("unable to restore original value of powerpc/rfi_flush debugfs file");
125                 return 1;
126         }
127 
128         if (have_entry_flush) {
129                 if (write_debugfs_int("powerpc/entry_flush", entry_flush_orig) < 0) {
130                         perror("unable to restore original value of powerpc/entry_flush "
131                                "debugfs file");
132                         return 1;
133                 }
134         }
135 
136         return rc;
137 }
138 
139 int main(int argc, char *argv[])
140 {
141         return test_harness(rfi_flush_test, "rfi_flush_test");
142 }
143 

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