FS#66617 - Limits set in .service unit are not effective.

Attached to Project: Arch Linux
Opened by Carlo Wood (CarloWood) - Monday, 11 May 2020, 19:46 GMT
Last edited by freswa (frederik) - Monday, 11 May 2020, 22:33 GMT
Task Type Bug Report
Category Packages: Core
Status Closed
Assigned To No-one
Architecture All
Severity Low
Priority Normal
Reported Version
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 0
Private No

Details

Description:

I have installed realtime-privileges and added
myself to the realtime group, so that:

>ulimit -r -l
real-time priority (-r) 98
max locked memory (kbytes, -l) unlimited

Using community/jack2 as test/example,
running it from the command line as myself
works:

>/usr/bin/jackd --realtime --realtime-priority 10 --timeout 2000 -c system --autoconnect a -d alsa -d hw:Generic,0 --inchannels 2 --outchannels 2 --period 1024 --rate 48000
jackdmp 1.9.14
Copyright 2001-2005 Paul Davis and others.
Copyright 2004-2016 Grame.
Copyright 2016-2019 Filipe Coelho.
jackdmp comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome to redistribute it
under certain conditions; see the file COPYING for details
no message buffer overruns
no message buffer overruns
no message buffer overruns
JACK server starting in realtime mode with priority 10
self-connect-mode is "Ignore all self connect requests"
audio_reservation_init
Acquire audio card Audio1
creating alsa driver ... hw:Generic,0|hw:Generic,0|1024|2|48000|2|2|nomon|swmeter|-|32bit
configuring for 48000Hz, period = 1024 frames (21.3 ms), buffer = 2 periods
ALSA: final selected sample format for capture: 32bit integer little-endian
ALSA: use 2 periods for capture
ALSA: final selected sample format for playback: 32bit integer little-endian
ALSA: use 2 periods for playback

which as you can see has no errors and used realtime mode with priority 10.

However, when trying to run the same using systemd it fails:

>systemctl --user start jack@default.service
>systemctl --user status jack@default.service
jack@default.service - JACK server using default.conf profile
Loaded: loaded (/usr/lib/systemd/user/jack@.service; disabled; vendor preset: enabled)
Active: active (running) since Mon 2020-05-11 21:19:38 CEST; 7s ago
Docs: man:jackd(1)
Main PID: 99164 (jackd)
CGroup: /user.slice/user-1000.slice/user@1000.service/jack.slice/jack@default.service
└─99164 /usr/bin/jackd --realtime --realtime-priority 10 --timeout 2000 -c system --autoconnect a -d alsa -d hw:Generic,0 --inchannels 2 --outchannels 2 --period 1024 --rate 48000

May 11 21:19:38 daniel jackd[99164]: Acquire audio card Audio1
May 11 21:19:38 daniel jackd[99164]: creating alsa driver ... hw:Generic,0|hw:Generic,0|1024|2|48000|2|2|nomon|swmeter|-|32bit
May 11 21:19:38 daniel jackd[99164]: configuring for 48000Hz, period = 1024 frames (21.3 ms), buffer = 2 periods
May 11 21:19:38 daniel jackd[99164]: ALSA: final selected sample format for capture: 32bit integer little-endian
May 11 21:19:38 daniel jackd[99164]: ALSA: use 2 periods for capture
May 11 21:19:38 daniel jackd[99164]: ALSA: final selected sample format for playback: 32bit integer little-endian
May 11 21:19:38 daniel jackd[99164]: ALSA: use 2 periods for playback
May 11 21:19:38 daniel jackd[99164]: Cannot use real-time scheduling (RR/10) (1: Operation not permitted)
May 11 21:19:38 daniel jackd[99164]: AcquireSelfRealTime error
May 11 21:19:38 daniel systemd[1161]: Started JACK server using default.conf profile.

which, as you can see prints "Cannot use real-time scheduling (RR/10) (1: Operation not permitted)".
The other limits also do not get set and also result in errors (further on).

This service is using /usr/lib/systemd/user/jack@.service which reads:

[Unit]
Description=JACK server using %i.conf profile
Documentation=man:jackd(1)
After=sound.target local-fs.target

[Service]
Type=notify
EnvironmentFile=-/etc/jack/%i.conf
EnvironmentFile=-%h/.config/jack/%i.conf
ExecStart=/usr/bin/jackd $JACK_OPTIONS -d $DRIVER -d $DEVICE $DRIVER_SETTINGS
LimitRTPRIO=95
LimitRTTIME=infinity
LimitMEMLOCK=infinity
# Caution: use on memory-limited devices only
# OOMScoreAdjust=-1000

[Install]
WantedBy=default.target


Where %i = default and %h = /home/carlo,
and /home/carlo/.config/jack/default.conf is:

JACK_OPTIONS="--realtime --realtime-priority 10 --timeout 2000 -c system --autoconnect a"
DRIVER="alsa"
DEVICE="hw:Generic,0"
DRIVER_SETTINGS="--inchannels 2 --outchannels 2 --period 1024 --rate 48000"

Aka - running the jackd with exactly the same parameters (as you can already see from the status output above).


Thus, in summary: even though LimitRTPRIO=95 is specified
and I have the rights to set the RP priority. Starting the
service through systemd fails to pass on those rights.

In https://jlk.fjfi.cvut.cz/arch/manpages/man/systemd.exec.5
section PROCESS PROPERTIES we learn that,

LimitRTPRIO exists and equals ulimit -r (Realtime Priority)

That page also says "Note that most process resource limits configured with these options are per-process, and processes may fork in order to acquire a new set of resources that are accounted independently of the original process, and may thus escape limits set."
but that is not relevant here (if I start the process myself,
from the CLI, then it works; so the process doesn't fork).

Finally, it says

"For system units these resource limits may be chosen freely. When these settings are configured in a user service (i.e. a service run by the per-user instance of the service manager) they cannot be used to raise the limits above those set for the user manager itself when it was first invoked, as the user's service manager generally lacks the privileges to do so."

but I have rebooted since I gave myself these limits (added myself to
the realtime group).

When running this service the processes involved are:

carlo 1161 0.0 0.0 22744 10748 ? Ss May09 0:00 /usr/lib/systemd/systemd --user

which spawned,

carlo 99337 0.3 0.1 273564 38840 ? SLsl 21:36 0:00 /usr/bin/jackd --realtime --realtime-priority 10 --timeout 2000 -c system --autoconnect a -d alsa -d hw:Generic,0 --inchannels 2 --outchannels 2 --period 1024 --rate 48000

Aka, both are running as me.

I'm not sure what it wrong, but it seems that this systemd process
should have sufficient ulimits and is instructed to pass them
on to the process, but the processes isn't getting them.

Additional info:
* package version(s)

systemd 245.5-2
realtime-privileges 2-1
jack2 1.9.14-1 (to reproduce test case)

* config and/or log files etc.
* link to upstream bug report, if any

Steps to reproduce:

Add a normal user to groups 'audio' and 'realtime',
reboot

Create ~/.config/jack/default.conf as above.
Run /usr/bin/jackd --realtime --realtime-priority 10 --timeout 2000 -c system --autoconnect a -d alsa -d hw:Generic,0 --inchannels 2 --outchannels 2 --period 1024 --rate 48000

replacing hw:Generic,0 with the right card from /proc/asound/cards
(also fix that in ~/.config/jack/default.conf) and observe that
the realtime priority is set.

Stop it again (control-C) and run: systemctl --user start jack@default.service
and observe that status shows above error.
This task depends upon

Closed by  freswa (frederik)
Monday, 11 May 2020, 22:33 GMT
Reason for closing:  Works for me

Loading...