@@ -291,37 +291,67 @@ static ssize_t mem_used_max_store(struct device *dev,
291
291
return len ;
292
292
}
293
293
294
- static ssize_t idle_store (struct device * dev ,
295
- struct device_attribute * attr , const char * buf , size_t len )
294
+ /*
295
+ * Mark all pages which are older than or equal to cutoff as IDLE.
296
+ * Callers should hold the zram init lock in read mode
297
+ */
298
+ static void mark_idle (struct zram * zram , ktime_t cutoff )
296
299
{
297
- struct zram * zram = dev_to_zram ( dev ) ;
300
+ int is_idle = 1 ;
298
301
unsigned long nr_pages = zram -> disksize >> PAGE_SHIFT ;
299
302
int index ;
300
303
301
- if (!sysfs_streq (buf , "all" ))
302
- return - EINVAL ;
303
-
304
- down_read (& zram -> init_lock );
305
- if (!init_done (zram )) {
306
- up_read (& zram -> init_lock );
307
- return - EINVAL ;
308
- }
309
-
310
304
for (index = 0 ; index < nr_pages ; index ++ ) {
311
305
/*
312
306
* Do not mark ZRAM_UNDER_WB slot as ZRAM_IDLE to close race.
313
307
* See the comment in writeback_store.
314
308
*/
315
309
zram_slot_lock (zram , index );
316
310
if (zram_allocated (zram , index ) &&
317
- !zram_test_flag (zram , index , ZRAM_UNDER_WB ))
318
- zram_set_flag (zram , index , ZRAM_IDLE );
311
+ !zram_test_flag (zram , index , ZRAM_UNDER_WB )) {
312
+ #ifdef CONFIG_ZRAM_MEMORY_TRACKING
313
+ is_idle = !cutoff || ktime_after (cutoff , zram -> table [index ].ac_time );
314
+ #endif
315
+ if (is_idle )
316
+ zram_set_flag (zram , index , ZRAM_IDLE );
317
+ }
319
318
zram_slot_unlock (zram , index );
320
319
}
320
+ }
321
321
322
- up_read (& zram -> init_lock );
322
+ static ssize_t idle_store (struct device * dev ,
323
+ struct device_attribute * attr , const char * buf , size_t len )
324
+ {
325
+ struct zram * zram = dev_to_zram (dev );
326
+ ktime_t cutoff_time = 0 ;
327
+ ssize_t rv = - EINVAL ;
323
328
324
- return len ;
329
+ if (!sysfs_streq (buf , "all" )) {
330
+ /*
331
+ * If it did not parse as 'all' try to treat it as an integer when
332
+ * we have memory tracking enabled.
333
+ */
334
+ u64 age_sec ;
335
+
336
+ if (IS_ENABLED (CONFIG_ZRAM_MEMORY_TRACKING ) && !kstrtoull (buf , 0 , & age_sec ))
337
+ cutoff_time = ktime_sub (ktime_get_boottime (),
338
+ ns_to_ktime (age_sec * NSEC_PER_SEC ));
339
+ else
340
+ goto out ;
341
+ }
342
+
343
+ down_read (& zram -> init_lock );
344
+ if (!init_done (zram ))
345
+ goto out_unlock ;
346
+
347
+ /* A cutoff_time of 0 marks everything as idle, this is the "all" behavior */
348
+ mark_idle (zram , cutoff_time );
349
+ rv = len ;
350
+
351
+ out_unlock :
352
+ up_read (& zram -> init_lock );
353
+ out :
354
+ return rv ;
325
355
}
326
356
327
357
#ifdef CONFIG_ZRAM_WRITEBACK
0 commit comments