Commit Graph

1699 Commits

Author SHA1 Message Date
Kir Kolyshkin e25b8cfcd5 libct/cg/utils: use fscommon.ReadFile
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:57 -07:00
Kir Kolyshkin 6bae53f589 libct/cg/fs2: use fscommon.ReadFile
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin 2588e6f1b8 libct/cg/fs/cpuset: use fscommon.ReadFile
While at it,

 - change some functions to not be methods of CpusetCgroup as
   they don't use any members;
 - simplify isEmpty.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin 1d20cf49f1 libct/cg/fs/cpuacct: use fscommon.ReadFile
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin 9e78b66e88 libct/cg/systemd/v1.enableKmem: use fscommon.ReadFile
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin 316344364c libct/cg/fs2.CreateCgroupPath: use fscommon.*File
fscommon's ReadFile and WriteFile are tailored to cgroupfs,
so let's use them here.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin b7092d84ee libct/cg/fs.setKernelMemory: use fscommon.WriteFile
fscommon.WriteFile is added specifically to work with cgroup files,
and the error it returns does not need to be wrapped.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Kir Kolyshkin 619de9773e libct/cg/fscommon_test: rm cgroups dependency
This removes package dependency on cgroup, as following commits
make cgroup use fscommon, which would result in dependency cycle.

The code to find out memory cgroup root is not really needed,
as 99% of test envrionments will have it at /sys/fs/cgroup/memory.
If not, that means we're either on cgroupv2 or on some very custom
system, so just skip the test.

The code that checks if we're on cgroupv2 is replaced by the check
of the particular v1 control file.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-05 14:07:15 -07:00
Akihiro Suda c23c05eac0 Merge pull request #2528 from zvier/master
cleancode: adjust and make it more readability
2020-10-05 03:56:56 +09:00
Mrunal Patel f671f6bf3d Merge pull request #2597 from kolyshkin/hugepages
Fix/improve GetHugePageSize
2020-10-02 19:23:23 -07:00
Kir Kolyshkin dc775c138e Merge pull request #2625 from KentaTada/modify-rdt-test-perm
libcontainer/intelrdt: modify the incorrect file mode
2020-10-02 18:59:33 -07:00
Mrunal Patel 0d9b0dfc46 Merge pull request #2626 from kolyshkin/mountinfo-0.3.1
vendor: bump mountinfo v0.3.1
2020-10-02 15:11:45 -07:00
Akihiro Suda 750036e41d Merge pull request #2609 from thaJeztah/byte_trim
libcontainer: prefer bytes.TrimSpace() over strings.TrimSpace()
2020-10-02 14:22:39 +09:00
Ashok Pon Kumar fcf210d631 Fix goreport warnings of ineffassign and misspell
Signed-off-by: Ashok Pon Kumar <ashokponkumar@gmail.com>
2020-10-02 09:03:45 +05:30
Kenta Tada 644c107ecb libcontainer/intelrdt: modify the incorrect file mode
Signed-off-by: Kenta Tada <Kenta.Tada@sony.com>
2020-10-02 11:08:06 +09:00
Kir Kolyshkin 87412ee435 vendor: bump mountinfo v0.3.1
It contains some breaking changes, so fix the code.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-10-01 18:51:25 -07:00
Sebastiaan van Stijn 28b452bf65 libcontainer: unconvert
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-10-01 18:36:56 +02:00
Sebastiaan van Stijn b3a8b0742c libcontainer: prefer bytes.TrimSpace() over strings.TrimSpace()
Perform trimming before converting to a string, which should be
somewhat more performant.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-10-01 18:36:53 +02:00
Kir Kolyshkin c82737665a Merge pull request #2621 from KentaTada/refactor-config-test
libcontainer: remove loadConfig which is the unused function
2020-10-01 08:17:21 -07:00
Kir Kolyshkin 2854950e5f Merge pull request #2622 from KentaTada/remove-unused-removepath
libcontainer: remove `removePath` from cgroups
2020-10-01 08:15:20 -07:00
Kenta Tada 3d5dec2f44 libcontainer: remove the unused variable from spec
Signed-off-by: Kenta Tada <Kenta.Tada@sony.com>
2020-10-01 12:29:48 +09:00
Kenta Tada b76652fbeb libcontainer: remove removePath from cgroups
`removePath` is unused as below
https://github.com/opencontainers/runc/commit/11fb94965cab5b2a54da9dca50d0e36d0ee36ef0

Signed-off-by: Kenta Tada <Kenta.Tada@sony.com>
2020-10-01 12:22:12 +09:00
Kenta Tada faaecac77d libcontainer: remove loadConfig which is the unused function
Signed-off-by: Kenta Tada <Kenta.Tada@sony.com>
2020-10-01 12:07:49 +09:00
Akihiro Suda a220b9c6cc Merge pull request #2608 from thaJeztah/no_join
libcontainer: remove redundant strings.Join()
2020-10-01 10:20:23 +09:00
Kir Kolyshkin 8258fae775 Merge pull request #2619 from thaJeztah/nil_return
setFreezer: explicitly return nil
2020-09-30 18:07:10 -07:00
Sebastiaan van Stijn c6ac3c4bde libcontainer/system: remove deprecated GetProcessStartTime
GetProcessStartTime was deprecated over three Years ago in
439eaa3584, so we may as well remove
it now.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-10-01 00:06:47 +02:00
Sebastiaan van Stijn 3eb469b029 libcontainer: remove redundant strings.Join()
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-10-01 00:00:47 +02:00
Sebastiaan van Stijn bc9a7bda58 setFreezer: explicitly return nil
errors.Wrap(err, "some error") returns nil if err is nil, so it's
slightly clearer to just return early than to set the error to nil
and call errors.Wrap(). This is also somewhat defensive in case
we decide to replace `errors.Wrap()` for golang's native `%w`
wrapping.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-09-30 23:41:07 +02:00
Mrunal Patel ffc30bb00d Merge pull request #2606 from kolyshkin/intel-rdt
libcontainer/intelrdt: use moby/sys/mountinfo
2020-09-30 13:14:10 -07:00
Mrunal Patel d5098eaae4 Merge pull request #2604 from kolyshkin/fscommon-I
libct/cgroups/fscommon: nits
2020-09-30 13:12:16 -07:00
Kir Kolyshkin 360981ae1d libct/cgroups: rewrite getHugePageSizeFromFilenames
This is a function to convert huge page sizes (obtained by reading
/sys/kernel/mm/hugepages directory entries) to strings user for hugetlb
cgroup controller resource files. Those strings are when used to get the
hugetlb resource statistics.

This function used external library, floating point numbers, and can
(theoretically) produce invalid values, since the kernel only uses KB,
MB, and GB suffixes.

Rewrite it to produce the same strings as used in the kernel (see [1]).
As a result, it's also faster, more future-proof (entries that do not
start with "hugepages-" and/or incorrect suffix are skipped), and does
more input sanity checks. As a side effect, libcontainer no longer
depends on docker/go-units.

While at it, add more test cases.

Before:
	BenchmarkGetHugePageSize-8       	  187452	      6265 ns/op
	BenchmarkGetHugePageSizeImpl-8   	  396769	      2998 ns/op

After:
	BenchmarkGetHugePageSize-8       	  222898	      4554 ns/op
	BenchmarkGetHugePageSizeImpl-8   	 4738924	       241 ns/op

NOTE on removing HugePageSizeUnitList -- this was added by commit
6f77e35da and was used by kubernetes code in [2], which was later
superceded by [3], so there are (hopefully) no external users.
If there are any, they should not be doing that.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/mm/hugetlb_cgroup.c?id=eff48ddeab782e35e58ccc8853f7386bbae9dec4#n574
[2] https://github.com/kubernetes/kubernetes/pull/78495
[3] https://github.com/kubernetes/kubernetes/pull/84154

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-30 10:58:31 -07:00
Akihiro Suda 890cc2aa60 Merge pull request #2612 from thaJeztah/concat
use string-concatenation instead of sprintf for simple cases
2020-10-01 01:56:28 +09:00
Kir Kolyshkin 22f11bafc3 Merge pull request #2611 from thaJeztah/const
libcontainer/cgroups: use const for templates
2020-09-30 09:01:42 -07:00
Mrunal Patel fdd00a693e Merge pull request #2614 from kolyshkin/systemd-race
libct/cgroups/systemd: eliminate runc/systemd race
2020-09-30 08:20:11 -07:00
Sebastiaan van Stijn 8bf216728c use string-concatenation instead of sprintf for simple cases
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-09-30 10:51:59 +02:00
Kir Kolyshkin a4d5e8a27b libcontainer/ignoreTerminateError: ignore SIGKILL
When we call terminate(), we kill the process, and wait
returns the error indicating the process was killed.
This is exactly what we expect here, so there is no reason
to treat it as an error.

Before this patch, when a container with invalid cgroup parameters is
started:

> WARN[0000] unable to terminate initProcess               error="signal: killed"
> ERRO[0000] container_linux.go:366: starting container process caused: process_linux.go:495: container init caused: process_linux.go:458: setting cgroup config for procHooks process caused: failed to write "555": open /sys/fs/cgroup/blkio/user.slice/xx33/blkio.weight: permission denied

After:

> ERRO[0000] container_linux.go:366: starting container process caused: process_linux.go:495: container init caused: process_linux.go:458: setting cgroup config for procHooks process caused: failed to write "555": open /sys/fs/cgroup/blkio/user.slice/xx33/blkio.weight: permission denied

I.e. the useless warning is gone.

NOTE this breaks a couple of integration test cases, since they were
expecting a particular message in the second line, and now due to
"signal: killed" removed it's in the first line. Fix those, too.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 23:55:02 -07:00
Kir Kolyshkin dc42459130 libct/(*initProcess).start: fix removing cgroups on error
In case cgroup configuration is invalid (some parameters can't be set
etc.), p.manager.Set fails, the error is returned, and then we try to
remove cgroups (by calling p.manager.Destroy) in a defer.

The problem is, the container init is not yet killed (as it is killed in
the caller, i.e. (*linuxContainer).start), so cgroup removal fails like
this:

> time="2020-09-26T07:46:25Z" level=warning msg="Failed to remove cgroup (will retry)" error="rmdir /sys/fs/cgroup/net_cls,net_prio/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod28ce6e74_694c_4b77_a953_dc01e182ac76.slice/crio-f6984c5eeb6c6b49ff3f036bdcb9ded317b3d0b2469ebbb35705442a2afd98c2.scope: device or resource busy"
> ...
> time="2020-09-26T07:46:27Z" level=error msg="Failed to remove cgroup" error="rmdir /sys/fs/cgroup/net_cls,net_prio/kubepods.slice/kubepods-burstable.slice/kubepods-burstable-pod28ce6e74_694c_4b77_a953_dc01e182ac76.slice/crio-f6984c5eeb6c6b49ff3f036bdcb9ded317b3d0b2469ebbb35705442a2afd98c2.scope: device or resource busy"

The above is repeated for every controller, and looks quite scary.

To fix, move the init termination to the abovementioned defer.

Do the same for (*setnsProcess).start() for uniformity.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 18:49:29 -07:00
Kir Kolyshkin 8699596dce libct/(*setnsProcess).Start: use retErr
It is not a good practice to have the name `err` for the error returned
by a function.  Switch to `retErr`.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 18:38:31 -07:00
Kir Kolyshkin 38447895a5 libct/cgroups/systemd: eliminate runc/systemd race
In case it takes more than 1 second for systemd to create a unit,
startUnit() times out with a warning and then runc proceeds
(to create cgroups using fs manager and so on).

Now runc and systemd are racing, and multiple scenarios are possible.

In one such scenario, by the time runc calls systemd manager's Apply()
the unit is not yet created, the dbusConnection.SetUnitProperties()
call fails with "unit xxx.scope not found", and the whole container
start also fails.

To eliminate the race, we need to return an error in case the timeout is
hit.

To reduce the chance to fail, increase the timeout from 1 to 30 seconds,
to not error out too early on a busy/slow system (and times like 3-5
seconds are not unrealistic).

While at it, as the timeout is quite long now, make sure to not leave
a stray timer.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 17:43:15 -07:00
Kir Kolyshkin 6c83d23ffc libcontainer/cgroups/fscommon: improve doc
Document ReadFile and WriteFile. Fix doc for ParseUint to be in
canonical form.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 11:58:14 -07:00
Kir Kolyshkin 31f0f5b7e0 libct/cg/fscommon.GetCgroupParamUint: improve
1. Use GetCgroupParamString as the initial part of both functions are
   the same and we can reuse it. This also gives us whatever security
   measures GetCgroupParamString has (see previous commit).

2. Fix the error wrapping to not add the value, as it is already a part
   of the error returned by ParseUint.

3. Improve docstring.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 11:58:14 -07:00
Kir Kolyshkin e76ac1c054 libct/cg/fscommon.GetCgroupParamString: use ReadFile
1. Use own ReadFile wrapper instead of ioutils.ReadFile.
   This makes it use the security measures of ReadFile.

2. Improve doc.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 11:58:14 -07:00
Kir Kolyshkin aac4d1f5c3 libct/cg/fscommon/GetCgroupParamKeyValue: nits
2. Fix wrapping the error to not have the value as it's already
   part of the error returned from ParseUint.

3. Fix/improve doc.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 11:57:49 -07:00
Kir Kolyshkin d167be2925 libct/cgroups/fs2/statHugeTlb: error message nits
1. Don't wrap the error from fscommon.GetCgroupParamUint as it already
   contains the file name.

2. Don't put file name when wrapping the error from ioutil.ReadFile
   since it already has it.

3. Don't reconstruct file name, use existing one since it's available.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 11:57:18 -07:00
Kir Kolyshkin 2c70d23840 libct/intelrdt: add TestFindIntelRdtMountpointDir
Heavily based on work by Paweł Szulik <pawel.szulik@intel.com>

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-29 09:21:32 -07:00
Sebastiaan van Stijn ab2b5dfa8a libcontainer/cgroups: use const for templates
The names of these templates overlapped with some local
variables, which made reading the code somewhat confusing.

Changing them to a const, given that these were not updated anywere.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2020-09-29 13:34:44 +02:00
Akihiro Suda 49d4507c77 Merge pull request #2595 from thaJeztah/libcontainer_caps_refactor
Libcontainer: refactor capabilities code and remove RHEL6 workaround
2020-09-29 13:53:40 +09:00
Kir Kolyshkin f1c1fdf911 libcontainer/intelrdt: use moby/sys/mountinfo
It might be a tad slower but it surely more correct and well maintained,
so it's better to use it than rely on a custom implementation which is
kind of hard to get entirely right.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-28 20:18:01 -07:00
Akihiro Suda 6e5320ffa8 Merge pull request #2584 from kolyshkin/unified-support
libct/cgroups: support Cgroups.Resources.Unified
2020-09-26 18:48:16 +09:00
Kir Kolyshkin b006f4a180 libct/cgroups: support Cgroups.Resources.Unified
Add support for unified resource map (as per [1]), and add some test
cases for the new functionality.

[1] https://github.com/opencontainers/runtime-spec/pull/1040

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2020-09-24 15:29:35 -07:00