diff --git a/lib/internal.h b/lib/internal.h index a41560f1..9a3ef89d 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -179,6 +179,7 @@ int crypt_get_debug_level(void); int crypt_memlock_inc(struct crypt_device *ctx); int crypt_memlock_dec(struct crypt_device *ctx); +void crypt_process_priority(struct crypt_device *cd, int *priority, bool raise); int crypt_metadata_locking_enabled(void); diff --git a/lib/utils.c b/lib/utils.c index 8fa4242a..5742d3ec 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -59,6 +59,35 @@ uint64_t crypt_getphysmemory_kb(void) return phys_memory_kb; } +void crypt_process_priority(struct crypt_device *cd, int *priority, bool raise) +{ + int _priority, new_priority; + + if (raise) { + _priority = getpriority(PRIO_PROCESS, 0); + if (_priority < 0) + _priority = 0; + if (priority) + *priority = _priority; + + /* + * Do not bother checking CAP_SYS_NICE as device activation + * requires CAP_SYSADMIN later anyway. + */ + if (getuid() || geteuid()) + new_priority = 0; + else + new_priority = -18; + + if (setpriority(PRIO_PROCESS, 0, new_priority)) + log_dbg(cd, "Cannot raise process priority."); + } else { + _priority = priority ? *priority : 0; + if (setpriority(PRIO_PROCESS, 0, _priority)) + log_dbg(cd, "Cannot reset process priority."); + } +} + /* MEMLOCK */ #define DEFAULT_PROCESS_PRIORITY -18 diff --git a/lib/utils_benchmark.c b/lib/utils_benchmark.c index 780ebd7d..707846fa 100644 --- a/lib/utils_benchmark.c +++ b/lib/utils_benchmark.c @@ -98,7 +98,7 @@ int crypt_benchmark_pbkdf(struct crypt_device *cd, int (*progress)(uint32_t time_ms, void *usrptr), void *usrptr) { - int r; + int r, priority; const char *kdf_opt; if (!pbkdf || (!password && password_size)) @@ -112,10 +112,12 @@ int crypt_benchmark_pbkdf(struct crypt_device *cd, log_dbg(cd, "Running %s(%s) benchmark.", pbkdf->type, kdf_opt); + crypt_process_priority(cd, &priority, true); r = crypt_pbkdf_perf(pbkdf->type, pbkdf->hash, password, password_size, salt, salt_size, volume_key_size, pbkdf->time_ms, pbkdf->max_memory_kb, pbkdf->parallel_threads, &pbkdf->iterations, &pbkdf->max_memory_kb, progress, usrptr); + crypt_process_priority(cd, &priority, false); if (!r) log_dbg(cd, "Benchmark returns %s(%s) %u iterations, %u memory, %u threads (for %zu-bits key).",