@@ -160,6 +160,39 @@ fn disable_zswap_for_zram() {
160160}
161161
162162
163+ /// Configure MGLRU anti-thrashing protection
164+ /// Sets min_ttl_ms which protects the working set from premature eviction
165+ fn configure_mglru ( config : & Config , recommended : Option < & RecommendedConfig > ) {
166+ const MGLRU_MIN_TTL_PATH : & str = "/sys/kernel/mm/lru_gen/min_ttl_ms" ;
167+
168+ // Check if MGLRU is available
169+ if !Path :: new ( MGLRU_MIN_TTL_PATH ) . exists ( ) {
170+ return ;
171+ }
172+
173+ // Get configured value, or use recommended value from autoconfig
174+ let min_ttl_ms: u32 = config
175+ . get_opt ( "mglru_min_ttl_ms" )
176+ . and_then ( |v| v. parse :: < u32 > ( ) . ok ( ) )
177+ . unwrap_or_else ( || {
178+ recommended
179+ . map ( |r| r. mglru_min_ttl_ms )
180+ . unwrap_or ( defaults:: MGLRU_MIN_TTL_MS )
181+ } ) ;
182+
183+ if min_ttl_ms == 0 {
184+ return ;
185+ }
186+
187+ // Apply the setting
188+ match fs:: write ( MGLRU_MIN_TTL_PATH , min_ttl_ms. to_string ( ) ) {
189+ Ok ( _) => info ! (
190+ "MGLRU: min_ttl_ms = {} (working set protection)" ,
191+ min_ttl_ms
192+ ) ,
193+ Err ( e) => warn ! ( "MGLRU: failed to set min_ttl_ms: {}" , e) ,
194+ }
195+ }
163196
164197/// Start the swap daemon
165198fn start ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
@@ -208,6 +241,9 @@ fn start() -> Result<(), Box<dyn std::error::Error>> {
208241 config. apply_autoconfig ( & recommended) ;
209242 }
210243
244+ // Configure MGLRU early (protects working set during swap operations)
245+ configure_mglru ( & config, Some ( & recommended) ) ;
246+
211247 // Determine effective mode
212248 let effective_mode = match swap_mode {
213249 SwapMode :: Auto => match recommended. swap_mode {
0 commit comments