From 5d3942eec37a2608d20711ad2abb6065226a360f Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Wed, 26 Jun 2024 17:22:02 -0700 Subject: [PATCH] libct: unify IOPriority setting For some reason, io priority is set in different places between runc start/run and runc exec: - for runc start/run, it is done in the middle of (*linuxStandardInit).Init, close to the place where we exec runc init. - for runc exec, it is done much earlier, in (*setnsProcess) start(). Let's move setIOPriority call for runc exec to (*linuxSetnsInit).Init, so it is in the same logical place as for runc start/run. Also, move the function itself to init_linux.go as it's part of init. Should not have any visible effect, except part of runc init is run with a different I/O priority. While at it, rename setIOPriority to setupIOPriority, and make it accept the whole *configs.Config, for uniformity with other similar functions. Fixes: bfbd0305 ("Add I/O priority") Signed-off-by: Kir Kolyshkin --- libcontainer/init_linux.go | 22 ++++++++++++++++++++++ libcontainer/process_linux.go | 25 ------------------------- libcontainer/setns_init_linux.go | 3 +++ libcontainer/standard_init_linux.go | 2 +- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/libcontainer/init_linux.go b/libcontainer/init_linux.go index cc897703a..086f31258 100644 --- a/libcontainer/init_linux.go +++ b/libcontainer/init_linux.go @@ -678,6 +678,28 @@ func setupScheduler(config *configs.Config) error { return nil } +func setupIOPriority(config *configs.Config) error { + const ioprioWhoPgrp = 1 + + ioprio := config.IOPriority + if ioprio == nil { + return nil + } + class, ok := configs.IOPrioClassMapping[ioprio.Class] + if !ok { + return fmt.Errorf("invalid io priority class: %s", ioprio.Class) + } + + // Combine class and priority into a single value + // https://github.com/torvalds/linux/blob/v5.18/include/uapi/linux/ioprio.h#L5-L17 + iop := (class << 13) | ioprio.Priority + _, _, errno := unix.RawSyscall(unix.SYS_IOPRIO_SET, ioprioWhoPgrp, 0, uintptr(iop)) + if errno != 0 { + return fmt.Errorf("failed to set io priority: %w", errno) + } + return nil +} + func setupPersonality(config *configs.Config) error { return system.SetLinuxPersonality(config.Personality.Domain) } diff --git a/libcontainer/process_linux.go b/libcontainer/process_linux.go index 7e17fe511..9b20f2d66 100644 --- a/libcontainer/process_linux.go +++ b/libcontainer/process_linux.go @@ -166,10 +166,6 @@ type setnsProcess struct { func (p *setnsProcess) start() (retErr error) { defer p.comm.closeParent() - if err := setIOPriority(p.process.IOPriority); err != nil { - return err - } - // get the "before" value of oom kill count oom, _ := p.manager.OOMKillCount() err := p.cmd.Start() @@ -906,24 +902,3 @@ func (p *Process) InitializeIO(rootuid, rootgid int) (i *IO, err error) { } return i, nil } - -func setIOPriority(ioprio *configs.IOPriority) error { - const ioprioWhoPgrp = 1 - - if ioprio == nil { - return nil - } - class, ok := configs.IOPrioClassMapping[ioprio.Class] - if !ok { - return fmt.Errorf("invalid io priority class: %s", ioprio.Class) - } - - // Combine class and priority into a single value - // https://github.com/torvalds/linux/blob/v5.18/include/uapi/linux/ioprio.h#L5-L17 - iop := (class << 13) | ioprio.Priority - _, _, errno := unix.RawSyscall(unix.SYS_IOPRIO_SET, ioprioWhoPgrp, 0, uintptr(iop)) - if errno != 0 { - return fmt.Errorf("failed to set io priority: %w", errno) - } - return nil -} diff --git a/libcontainer/setns_init_linux.go b/libcontainer/setns_init_linux.go index db4aa1463..462662a84 100644 --- a/libcontainer/setns_init_linux.go +++ b/libcontainer/setns_init_linux.go @@ -75,6 +75,9 @@ func (l *linuxSetnsInit) Init() error { return err } + if err := setupIOPriority(l.config.Config); err != nil { + return err + } // Tell our parent that we're ready to exec. This must be done before the // Seccomp rules have been applied, because we need to be able to read and // write to a socket. diff --git a/libcontainer/standard_init_linux.go b/libcontainer/standard_init_linux.go index fe6af5704..65444f38a 100644 --- a/libcontainer/standard_init_linux.go +++ b/libcontainer/standard_init_linux.go @@ -159,7 +159,7 @@ func (l *linuxStandardInit) Init() error { return err } - if err := setIOPriority(l.config.Config.IOPriority); err != nil { + if err := setupIOPriority(l.config.Config); err != nil { return err }