From d84c5ded1c6e86de31ac277cefa191acdd8ad9c5 Mon Sep 17 00:00:00 2001 From: MithicSpirit Date: Tue, 28 Jan 2025 20:11:46 -0500 Subject: [PATCH] Extract split lock mitigation into common file This reduces duplication of the split lock mitigation path. Additionally, it allows extending the functionality of procsysctl into also getting the split lock mitigation state. --- common/common-profile.c | 2 +- common/common-profile.h | 2 +- common/common-splitlock.c | 64 +++++++++++++++++++++++++++++++++++++++ common/common-splitlock.h | 44 +++++++++++++++++++++++++++ common/meson.build | 1 + daemon/gamemode-context.c | 23 +++----------- util/platprofctl.c | 2 +- util/procsysctl.c | 5 +-- 8 files changed, 119 insertions(+), 24 deletions(-) create mode 100644 common/common-splitlock.c create mode 100644 common/common-splitlock.h diff --git a/common/common-profile.c b/common/common-profile.c index d26b4cf..399e1a1 100644 --- a/common/common-profile.c +++ b/common/common-profile.c @@ -1,6 +1,6 @@ /* -Copyright (c) 2025, MithicSpirit +Copyright (c) 2025, the GameMode contributors All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/common/common-profile.h b/common/common-profile.h index 76b0e59..6fa8628 100644 --- a/common/common-profile.h +++ b/common/common-profile.h @@ -1,6 +1,6 @@ /* -Copyright (c) 2025, MithicSpirit +Copyright (c) 2025, the GameMode contributors All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/common/common-splitlock.c b/common/common-splitlock.c new file mode 100644 index 0000000..830e8d1 --- /dev/null +++ b/common/common-splitlock.c @@ -0,0 +1,64 @@ +/* + +Copyright (c) 2025, the GameMode contributors +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Feral Interactive nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + + */ + +#define _GNU_SOURCE + +#include "common-splitlock.h" +#include "common-logging.h" + +/** + * Path for the split lock mitigation state + */ +const char *splitlock_path = "/proc/sys/kernel/split_lock_mitigate"; + +/** + * Return the current split lock mitigation state + */ +long get_splitlock_state(void) +{ + FILE *f = fopen(splitlock_path, "r"); + if (!f) { + LOG_ERROR("Failed to open file for read %s\n", splitlock_path); + return -1; + } + + char contents[41] = { 0 }; + long value = -1; + + if (fread(contents, 1, sizeof contents - 1, f) > 0) { + value = strtol(contents, NULL, 10); + } else { + LOG_ERROR("Failed to read contents of %s\n", splitlock_path); + } + fclose(f); + + return value; +} diff --git a/common/common-splitlock.h b/common/common-splitlock.h new file mode 100644 index 0000000..27faaea --- /dev/null +++ b/common/common-splitlock.h @@ -0,0 +1,44 @@ +/* + +Copyright (c) 2025, the GameMode contributors +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Feral Interactive nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + + */ + +#pragma once + +#include + +/** + * Path for the split lock mitagation state + */ +extern const char *splitlock_path; + +/** + * Get the current split lock mitigation state + */ +long get_splitlock_state(void); diff --git a/common/meson.build b/common/meson.build index 3408214..1ebbf5d 100644 --- a/common/meson.build +++ b/common/meson.build @@ -3,6 +3,7 @@ common_sources = [ 'common-logging.c', 'common-governors.c', 'common-profile.c', + 'common-splitlock.c', 'common-external.c', 'common-helpers.c', 'common-gpu.c', diff --git a/daemon/gamemode-context.c b/daemon/gamemode-context.c index ddf82a1..e21a298 100644 --- a/daemon/gamemode-context.c +++ b/daemon/gamemode-context.c @@ -37,6 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "common-logging.h" #include "common-power.h" #include "common-profile.h" +#include "common-splitlock.h" #include "gamemode.h" #include "gamemode-config.h" @@ -220,25 +221,9 @@ void game_mode_context_destroy(GameModeContext *self) static void game_mode_store_splitlock(GameModeContext *self) { - FILE *f = fopen("/proc/sys/kernel/split_lock_mitigate", "r"); - if (f == NULL) { - if (errno == ENOENT) - return; - - LOG_ERROR("Couldn't open /proc/sys/kernel/split_lock_mitigate : %s\n", strerror(errno)); - return; - } - - char value_str[40]; - if (fgets(value_str, sizeof value_str, f) == NULL) { - LOG_ERROR("Couldn't read from /proc/sys/kernel/split_lock_mitigate : %s\n", - strerror(errno)); - fclose(f); - return; - } - fclose(f); - - self->initial_split_lock_mitigate = strtol(value_str, NULL, 10); + long initial_state = get_splitlock_state(); + self->initial_split_lock_mitigate = initial_state; + LOG_MSG("split lock mitigation was initially set to [%ld]\n", initial_state); } static int game_mode_disable_splitlock(GameModeContext *self, bool disable) diff --git a/util/platprofctl.c b/util/platprofctl.c index b6e5c59..3ca2a4a 100644 --- a/util/platprofctl.c +++ b/util/platprofctl.c @@ -1,6 +1,6 @@ /* -Copyright (c) 2025, MithicSpirit +Copyright (c) 2025, the GameMode contributors All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/util/procsysctl.c b/util/procsysctl.c index e5a69b5..9c32d9b 100644 --- a/util/procsysctl.c +++ b/util/procsysctl.c @@ -27,8 +27,9 @@ POSSIBILITY OF SUCH DAMAGE. #define _GNU_SOURCE #include #include "common-logging.h" +#include "common-splitlock.h" -static bool write_value(char *key, char *value) +static bool write_value(const char *key, const char *value) { FILE *f = fopen(key, "w"); @@ -58,7 +59,7 @@ int main(int argc, char *argv[]) if (argc == 3) { if (strcmp(argv[1], "split_lock_mitigate") == 0) { - if (!write_value("/proc/sys/kernel/split_lock_mitigate", argv[2])) + if (!write_value(splitlock_path, argv[2])) return EXIT_FAILURE; return EXIT_SUCCESS;