Re: [LTP] [PATCH v2 1/2] shell: add optional OOM protection

Li Wang <[email protected]> Sat, 1 Aug 2026 19:48:54 +0800
Newsgroups it.linux.lists.ltp
Message-ID <[email protected]>
Andrea Cervesato wrote:

> --- a/testcases/lib/tst_test.sh
> +++ b/testcases/lib/tst_test.sh
> @@ -28,6 +28,57 @@ export TST_USR_GID="${LTP_USR_GID:-65534}"
>  trap "tst_brk TBROK 'test interrupted'" INT
>  trap "unset _tst_setup_timer_pid; tst_brk TBROK 'test terminated'" TERM
>  
> +_tst_set_oom_score_adj()
> +{
> +	local value="$1"
> +	local path="/proc/self/oom_score_adj"
> +
> +	[ -e "$path" ] || return 0
> +
> +	echo "$value" > "$path" 2>/dev/null || return 0
> +}
> +
> +_tst_enable_oom_protection()
> +{
> +	_tst_set_oom_score_adj -1000
> +}
> +
> +_tst_disable_oom_protection()
> +{
> +	_tst_set_oom_score_adj 0
> +}
> +
> +_tst_run_oom_protected()
> +{
> +	local _tst_pid
> +	local _tst_ret
> +
> +	# Shield the harness from the OOM killer and run the test in a child.
> +	# The child keeps the default oom_score_adj so that it, and any process
> +	# it spawns, stay killable under memory pressure while the harness
> +	# survives to report results.
> +	_tst_enable_oom_protection
> +
> +	(
> +		_tst_disable_oom_protection
> +		_TST_OOM_PROTECTION=0
> +		export _TST_OOM_PROTECTION
> +		tst_run "$@"
> +	) &
> +	_tst_pid=$!
> +
> +	wait "$_tst_pid"
> +	_tst_ret=$?
> +
> +	if [ "$_tst_ret" -eq 137 ]; then
> +		tst_res TINFO "Test was SIGKILLed: OOM killer or timeout?"
> +		tst_res TINFO "On a slow machine try exporting LTP_TIMEOUT_MUL > 1"
> +		tst_brk TBROK "Test killed!"
> +	fi
> +
> +	exit "$_tst_ret"
> +}
> +
>  _tst_do_cleanup()
>  {
>  	if [ -n "$TST_DO_CLEANUP" -a -n "$TST_CLEANUP" -a -z "$LTP_NO_CLEANUP" ]; then
> @@ -680,12 +731,16 @@ tst_run()
>  	local _tst_pattern='[='\''"} \t\/:`$\;|].*'
>  	local ret
>  
> +	if [ "$TST_OOM_PROTECTION" = 1 -a "$_TST_OOM_PROTECTION" != 0 ]; then
> +		_tst_run_oom_protected "$@"
> +	fi

In the C library, OOM protection for the LTP library and harness
is enabled by default. But in such Shell implementation, we have to
explicitly enable it via 'TST_OOM_PROTECTION=1'.

If we keep them consistent and remove the TST_OOM_PROTECTION
variable, things will become easier I guess :).

-- 
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp