@@ -390,17 +390,6 @@ static unsigned long damon_region_sz_limit(struct damon_ctx *ctx)
390
390
return sz ;
391
391
}
392
392
393
- static bool damon_kdamond_running (struct damon_ctx * ctx )
394
- {
395
- bool running ;
396
-
397
- mutex_lock (& ctx -> kdamond_lock );
398
- running = ctx -> kdamond != NULL ;
399
- mutex_unlock (& ctx -> kdamond_lock );
400
-
401
- return running ;
402
- }
403
-
404
393
static int kdamond_fn (void * data );
405
394
406
395
/*
@@ -418,7 +407,6 @@ static int __damon_start(struct damon_ctx *ctx)
418
407
mutex_lock (& ctx -> kdamond_lock );
419
408
if (!ctx -> kdamond ) {
420
409
err = 0 ;
421
- ctx -> kdamond_stop = false;
422
410
ctx -> kdamond = kthread_run (kdamond_fn , ctx , "kdamond.%d" ,
423
411
nr_running_ctxs );
424
412
if (IS_ERR (ctx -> kdamond )) {
@@ -474,13 +462,15 @@ int damon_start(struct damon_ctx **ctxs, int nr_ctxs)
474
462
*/
475
463
static int __damon_stop (struct damon_ctx * ctx )
476
464
{
465
+ struct task_struct * tsk ;
466
+
477
467
mutex_lock (& ctx -> kdamond_lock );
478
- if (ctx -> kdamond ) {
479
- ctx -> kdamond_stop = true;
468
+ tsk = ctx -> kdamond ;
469
+ if (tsk ) {
470
+ get_task_struct (tsk );
480
471
mutex_unlock (& ctx -> kdamond_lock );
481
- while (damon_kdamond_running (ctx ))
482
- usleep_range (ctx -> sample_interval ,
483
- ctx -> sample_interval * 2 );
472
+ kthread_stop (tsk );
473
+ put_task_struct (tsk );
484
474
return 0 ;
485
475
}
486
476
mutex_unlock (& ctx -> kdamond_lock );
@@ -925,12 +915,8 @@ static bool kdamond_need_update_primitive(struct damon_ctx *ctx)
925
915
static bool kdamond_need_stop (struct damon_ctx * ctx )
926
916
{
927
917
struct damon_target * t ;
928
- bool stop ;
929
918
930
- mutex_lock (& ctx -> kdamond_lock );
931
- stop = ctx -> kdamond_stop ;
932
- mutex_unlock (& ctx -> kdamond_lock );
933
- if (stop )
919
+ if (kthread_should_stop ())
934
920
return true;
935
921
936
922
if (!ctx -> primitive .target_valid )
@@ -1021,13 +1007,6 @@ static int kdamond_wait_activation(struct damon_ctx *ctx)
1021
1007
return - EBUSY ;
1022
1008
}
1023
1009
1024
- static void set_kdamond_stop (struct damon_ctx * ctx )
1025
- {
1026
- mutex_lock (& ctx -> kdamond_lock );
1027
- ctx -> kdamond_stop = true;
1028
- mutex_unlock (& ctx -> kdamond_lock );
1029
- }
1030
-
1031
1010
/*
1032
1011
* The monitoring daemon that runs as a kernel thread
1033
1012
*/
@@ -1038,25 +1017,26 @@ static int kdamond_fn(void *data)
1038
1017
struct damon_region * r , * next ;
1039
1018
unsigned int max_nr_accesses = 0 ;
1040
1019
unsigned long sz_limit = 0 ;
1020
+ bool done = false;
1041
1021
1042
1022
pr_debug ("kdamond (%d) starts\n" , current -> pid );
1043
1023
1044
1024
if (ctx -> primitive .init )
1045
1025
ctx -> primitive .init (ctx );
1046
1026
if (ctx -> callback .before_start && ctx -> callback .before_start (ctx ))
1047
- set_kdamond_stop ( ctx ) ;
1027
+ done = true ;
1048
1028
1049
1029
sz_limit = damon_region_sz_limit (ctx );
1050
1030
1051
- while (!kdamond_need_stop (ctx )) {
1031
+ while (!kdamond_need_stop (ctx ) && ! done ) {
1052
1032
if (kdamond_wait_activation (ctx ))
1053
1033
continue ;
1054
1034
1055
1035
if (ctx -> primitive .prepare_access_checks )
1056
1036
ctx -> primitive .prepare_access_checks (ctx );
1057
1037
if (ctx -> callback .after_sampling &&
1058
1038
ctx -> callback .after_sampling (ctx ))
1059
- set_kdamond_stop ( ctx ) ;
1039
+ done = true ;
1060
1040
1061
1041
usleep_range (ctx -> sample_interval , ctx -> sample_interval + 1 );
1062
1042
@@ -1069,7 +1049,7 @@ static int kdamond_fn(void *data)
1069
1049
sz_limit );
1070
1050
if (ctx -> callback .after_aggregation &&
1071
1051
ctx -> callback .after_aggregation (ctx ))
1072
- set_kdamond_stop ( ctx ) ;
1052
+ done = true ;
1073
1053
kdamond_apply_schemes (ctx );
1074
1054
kdamond_reset_aggregated (ctx );
1075
1055
kdamond_split_regions (ctx );
@@ -1088,9 +1068,8 @@ static int kdamond_fn(void *data)
1088
1068
damon_destroy_region (r , t );
1089
1069
}
1090
1070
1091
- if (ctx -> callback .before_terminate &&
1092
- ctx -> callback .before_terminate (ctx ))
1093
- set_kdamond_stop (ctx );
1071
+ if (ctx -> callback .before_terminate )
1072
+ ctx -> callback .before_terminate (ctx );
1094
1073
if (ctx -> primitive .cleanup )
1095
1074
ctx -> primitive .cleanup (ctx );
1096
1075
0 commit comments