diff --git a/common/common-splitlock.c b/common/common-splitlock.c new file mode 100644 index 00000000..fa5759d0 --- /dev/null +++ b/common/common-splitlock.c @@ -0,0 +1,64 @@ +/* + +Copyright (c) 2025, MithicSpirit +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 00000000..09a71d8a --- /dev/null +++ b/common/common-splitlock.h @@ -0,0 +1,44 @@ +/* + +Copyright (c) 2025, MithicSpirit +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 34082143..1ebbf5d6 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 8b8d28d9..f09825f4 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/procsysctl.c b/util/procsysctl.c index 261ec380..5769fade 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;