From 573a599589063ecfe7cfc5290f05ecd490fa90b8 Mon Sep 17 00:00:00 2001 From: Ilya Zlobintsev Date: Fri, 20 Sep 2024 16:01:00 +0300 Subject: [PATCH] feat: prevent repeated enabling/disabling of overclocking before reboot --- lact-daemon/src/server/system.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/lact-daemon/src/server/system.rs b/lact-daemon/src/server/system.rs index d907570..c972ccc 100644 --- a/lact-daemon/src/server/system.rs +++ b/lact-daemon/src/server/system.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Context}; +use anyhow::{anyhow, ensure, Context}; use lact_schema::{InitramfsType, SystemInfo, GIT_COMMIT}; use os_release::{OsRelease, OS_RELEASE}; use std::{ @@ -6,10 +6,13 @@ use std::{ io::Write, os::unix::prelude::PermissionsExt, path::Path, + sync::atomic::{AtomicBool, Ordering}, }; use tokio::process::Command; use tracing::{info, warn}; +static OC_TOGGLED: AtomicBool = AtomicBool::new(false); + const PP_OVERDRIVE_MASK: u64 = 0x4000; pub const PP_FEATURE_MASK_PATH: &str = "/sys/module/amdgpu/parameters/ppfeaturemask"; pub const MODULE_CONF_PATH: &str = "/etc/modprobe.d/99-amdgpu-overdrive.conf"; @@ -49,6 +52,11 @@ pub async fn info() -> anyhow::Result { } pub async fn enable_overdrive() -> anyhow::Result { + ensure!( + !OC_TOGGLED.load(Ordering::SeqCst), + "Overdrive support was already toggled - please reboot to apply the changes" + ); + let current_mask = read_current_mask()?; let new_mask = current_mask | PP_OVERDRIVE_MASK; @@ -67,6 +75,7 @@ pub async fn enable_overdrive() -> anyhow::Result { let message = match regenerate_initramfs().await { Ok(initramfs_type) => { + OC_TOGGLED.store(true, Ordering::SeqCst); format!("Initramfs was successfully regenerated (detected type {initramfs_type:?})") } Err(err) => format!("{err:#}"), @@ -76,12 +85,20 @@ pub async fn enable_overdrive() -> anyhow::Result { } pub async fn disable_overdrive() -> anyhow::Result { + ensure!( + !OC_TOGGLED.load(Ordering::SeqCst), + "Overdrive support was already toggled - please reboot to apply the changes" + ); + if Path::new(MODULE_CONF_PATH).exists() { fs::remove_file(MODULE_CONF_PATH).context("Could not remove module config file")?; match regenerate_initramfs().await { - Ok(initramfs_type) => Ok(format!( - "Initramfs was successfully regenerated (detected type {initramfs_type:?})" - )), + Ok(initramfs_type) => { + OC_TOGGLED.store(true, Ordering::SeqCst); + Ok(format!( + "Initramfs was successfully regenerated (detected type {initramfs_type:?})" + )) + } Err(err) => Ok(format!("{err:#}")), } } else {