selftests/powerpc: Add read/write debugfs file, int
authorBenjamin Gray <bgray@linux.ibm.com>
Fri, 3 Feb 2023 00:39:44 +0000 (11:39 +1100)
committerMichael Ellerman <mpe@ellerman.id.au>
Thu, 9 Feb 2023 12:56:45 +0000 (23:56 +1100)
Debugfs files are not always integers, so make *_file return/write a
byte buffer, and *_int deal with int values specifically. This increases
consistency with the other file read/write helpers.

Signed-off-by: Benjamin Gray <bgray@linux.ibm.com>
Reviewed-by: Andrew Donnellan <ajd@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20230203003947.38033-3-bgray@linux.ibm.com
tools/testing/selftests/powerpc/include/utils.h
tools/testing/selftests/powerpc/security/entry_flush.c
tools/testing/selftests/powerpc/security/rfi_flush.c
tools/testing/selftests/powerpc/security/uaccess_flush.c
tools/testing/selftests/powerpc/utils.c

index 70885e5..de5e379 100644 (file)
@@ -35,8 +35,10 @@ int pick_online_cpu(void);
 
 int read_file(const char *path, char *buf, size_t count, size_t *len);
 int write_file(const char *path, const char *buf, size_t count);
-int read_debugfs_file(char *debugfs_file, int *result);
-int write_debugfs_file(char *debugfs_file, int result);
+int read_debugfs_file(const char *debugfs_file, char *buf, size_t count);
+int write_debugfs_file(const char *debugfs_file, const char *buf, size_t count);
+int read_debugfs_int(const char *debugfs_file, int *result);
+int write_debugfs_int(const char *debugfs_file, int result);
 int read_sysfs_file(char *debugfs_file, char *result, size_t result_size);
 int perf_event_open_counter(unsigned int type,
                            unsigned long config, int group_fd);
index 68ce377..e01c573 100644 (file)
@@ -34,18 +34,18 @@ int entry_flush_test(void)
        // The PMU event we use only works on Power7 or later
        SKIP_IF(!have_hwcap(PPC_FEATURE_ARCH_2_06));
 
-       if (read_debugfs_file("powerpc/rfi_flush", &rfi_flush_orig) < 0) {
+       if (read_debugfs_int("powerpc/rfi_flush", &rfi_flush_orig) < 0) {
                perror("Unable to read powerpc/rfi_flush debugfs file");
                SKIP_IF(1);
        }
 
-       if (read_debugfs_file("powerpc/entry_flush", &entry_flush_orig) < 0) {
+       if (read_debugfs_int("powerpc/entry_flush", &entry_flush_orig) < 0) {
                perror("Unable to read powerpc/entry_flush debugfs file");
                SKIP_IF(1);
        }
 
        if (rfi_flush_orig != 0) {
-               if (write_debugfs_file("powerpc/rfi_flush", 0) < 0) {
+               if (write_debugfs_int("powerpc/rfi_flush", 0) < 0) {
                        perror("error writing to powerpc/rfi_flush debugfs file");
                        FAIL_IF(1);
                }
@@ -105,7 +105,7 @@ again:
 
        if (entry_flush == entry_flush_orig) {
                entry_flush = !entry_flush_orig;
-               if (write_debugfs_file("powerpc/entry_flush", entry_flush) < 0) {
+               if (write_debugfs_int("powerpc/entry_flush", entry_flush) < 0) {
                        perror("error writing to powerpc/entry_flush debugfs file");
                        return 1;
                }
@@ -120,12 +120,12 @@ again:
 
        set_dscr(0);
 
-       if (write_debugfs_file("powerpc/rfi_flush", rfi_flush_orig) < 0) {
+       if (write_debugfs_int("powerpc/rfi_flush", rfi_flush_orig) < 0) {
                perror("unable to restore original value of powerpc/rfi_flush debugfs file");
                return 1;
        }
 
-       if (write_debugfs_file("powerpc/entry_flush", entry_flush_orig) < 0) {
+       if (write_debugfs_int("powerpc/entry_flush", entry_flush_orig) < 0) {
                perror("unable to restore original value of powerpc/entry_flush debugfs file");
                return 1;
        }
index f73484a..6bedc86 100644 (file)
@@ -34,18 +34,18 @@ int rfi_flush_test(void)
        // The PMU event we use only works on Power7 or later
        SKIP_IF(!have_hwcap(PPC_FEATURE_ARCH_2_06));
 
-       if (read_debugfs_file("powerpc/rfi_flush", &rfi_flush_orig) < 0) {
+       if (read_debugfs_int("powerpc/rfi_flush", &rfi_flush_orig) < 0) {
                perror("Unable to read powerpc/rfi_flush debugfs file");
                SKIP_IF(1);
        }
 
-       if (read_debugfs_file("powerpc/entry_flush", &entry_flush_orig) < 0) {
+       if (read_debugfs_int("powerpc/entry_flush", &entry_flush_orig) < 0) {
                have_entry_flush = 0;
        } else {
                have_entry_flush = 1;
 
                if (entry_flush_orig != 0) {
-                       if (write_debugfs_file("powerpc/entry_flush", 0) < 0) {
+                       if (write_debugfs_int("powerpc/entry_flush", 0) < 0) {
                                perror("error writing to powerpc/entry_flush debugfs file");
                                return 1;
                        }
@@ -105,7 +105,7 @@ again:
 
        if (rfi_flush == rfi_flush_orig) {
                rfi_flush = !rfi_flush_orig;
-               if (write_debugfs_file("powerpc/rfi_flush", rfi_flush) < 0) {
+               if (write_debugfs_int("powerpc/rfi_flush", rfi_flush) < 0) {
                        perror("error writing to powerpc/rfi_flush debugfs file");
                        return 1;
                }
@@ -120,13 +120,13 @@ again:
 
        set_dscr(0);
 
-       if (write_debugfs_file("powerpc/rfi_flush", rfi_flush_orig) < 0) {
+       if (write_debugfs_int("powerpc/rfi_flush", rfi_flush_orig) < 0) {
                perror("unable to restore original value of powerpc/rfi_flush debugfs file");
                return 1;
        }
 
        if (have_entry_flush) {
-               if (write_debugfs_file("powerpc/entry_flush", entry_flush_orig) < 0) {
+               if (write_debugfs_int("powerpc/entry_flush", entry_flush_orig) < 0) {
                        perror("unable to restore original value of powerpc/entry_flush "
                               "debugfs file");
                        return 1;
index cf80f96..fcf23ea 100644 (file)
@@ -36,30 +36,30 @@ int uaccess_flush_test(void)
        // The PMU event we use only works on Power7 or later
        SKIP_IF(!have_hwcap(PPC_FEATURE_ARCH_2_06));
 
-       if (read_debugfs_file("powerpc/rfi_flush", &rfi_flush_orig) < 0) {
+       if (read_debugfs_int("powerpc/rfi_flush", &rfi_flush_orig) < 0) {
                perror("Unable to read powerpc/rfi_flush debugfs file");
                SKIP_IF(1);
        }
 
-       if (read_debugfs_file("powerpc/entry_flush", &entry_flush_orig) < 0) {
+       if (read_debugfs_int("powerpc/entry_flush", &entry_flush_orig) < 0) {
                perror("Unable to read powerpc/entry_flush debugfs file");
                SKIP_IF(1);
        }
 
-       if (read_debugfs_file("powerpc/uaccess_flush", &uaccess_flush_orig) < 0) {
+       if (read_debugfs_int("powerpc/uaccess_flush", &uaccess_flush_orig) < 0) {
                perror("Unable to read powerpc/entry_flush debugfs file");
                SKIP_IF(1);
        }
 
        if (rfi_flush_orig != 0) {
-               if (write_debugfs_file("powerpc/rfi_flush", 0) < 0) {
+               if (write_debugfs_int("powerpc/rfi_flush", 0) < 0) {
                        perror("error writing to powerpc/rfi_flush debugfs file");
                        FAIL_IF(1);
                }
        }
 
        if (entry_flush_orig != 0) {
-               if (write_debugfs_file("powerpc/entry_flush", 0) < 0) {
+               if (write_debugfs_int("powerpc/entry_flush", 0) < 0) {
                        perror("error writing to powerpc/entry_flush debugfs file");
                        FAIL_IF(1);
                }
@@ -119,7 +119,7 @@ again:
 
        if (uaccess_flush == uaccess_flush_orig) {
                uaccess_flush = !uaccess_flush_orig;
-               if (write_debugfs_file("powerpc/uaccess_flush", uaccess_flush) < 0) {
+               if (write_debugfs_int("powerpc/uaccess_flush", uaccess_flush) < 0) {
                        perror("error writing to powerpc/uaccess_flush debugfs file");
                        return 1;
                }
@@ -134,17 +134,17 @@ again:
 
        set_dscr(0);
 
-       if (write_debugfs_file("powerpc/rfi_flush", rfi_flush_orig) < 0) {
+       if (write_debugfs_int("powerpc/rfi_flush", rfi_flush_orig) < 0) {
                perror("unable to restore original value of powerpc/rfi_flush debugfs file");
                return 1;
        }
 
-       if (write_debugfs_file("powerpc/entry_flush", entry_flush_orig) < 0) {
+       if (write_debugfs_int("powerpc/entry_flush", entry_flush_orig) < 0) {
                perror("unable to restore original value of powerpc/entry_flush debugfs file");
                return 1;
        }
 
-       if (write_debugfs_file("powerpc/uaccess_flush", uaccess_flush_orig) < 0) {
+       if (write_debugfs_int("powerpc/uaccess_flush", uaccess_flush_orig) < 0) {
                perror("unable to restore original value of powerpc/uaccess_flush debugfs file");
                return 1;
        }
index 22ba134..495299a 100644 (file)
@@ -105,6 +105,24 @@ int read_auxv(char *buf, ssize_t buf_size)
        return 0;
 }
 
+int read_debugfs_file(const char *subpath, char *buf, size_t count)
+{
+       char path[PATH_MAX] = "/sys/kernel/debug/";
+
+       strncat(path, subpath, sizeof(path) - strlen(path) - 1);
+
+       return read_file(path, buf, count, NULL);
+}
+
+int write_debugfs_file(const char *subpath, const char *buf, size_t count)
+{
+       char path[PATH_MAX] = "/sys/kernel/debug/";
+
+       strncat(path, subpath, sizeof(path) - strlen(path) - 1);
+
+       return write_file(path, buf, count);
+}
+
 void *find_auxv_entry(int type, char *auxv)
 {
        ElfW(auxv_t) *p;
@@ -197,16 +215,12 @@ int read_sysfs_file(char *fpath, char *result, size_t result_size)
        return read_file(path, result, result_size, NULL);
 }
 
-int read_debugfs_file(char *debugfs_file, int *result)
+int read_debugfs_int(const char *debugfs_file, int *result)
 {
        int err;
-       char path[PATH_MAX];
        char value[16] = {0};
 
-       strcpy(path, "/sys/kernel/debug/");
-       strncat(path, debugfs_file, PATH_MAX - strlen(path) - 1);
-
-       err = read_file(path, value, sizeof(value) - 1, NULL);
+       err = read_debugfs_file(debugfs_file, value, sizeof(value) - 1);
        if (err)
                return err;
 
@@ -215,17 +229,13 @@ int read_debugfs_file(char *debugfs_file, int *result)
        return 0;
 }
 
-int write_debugfs_file(char *debugfs_file, int result)
+int write_debugfs_int(const char *debugfs_file, int result)
 {
-       char path[PATH_MAX];
        char value[16];
 
-       strcpy(path, "/sys/kernel/debug/");
-       strncat(path, debugfs_file, PATH_MAX - strlen(path) - 1);
-
        snprintf(value, 16, "%d", result);
 
-       return write_file(path, value, strlen(value));
+       return write_debugfs_file(debugfs_file, value, strlen(value));
 }
 
 static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,