Repository navigation
Conversation
31326ad to
d658210
Compare
|
@kolyshkin, this fixes parsing in |
|
So I think we should use the same validation rules as the kernel (i.e. writing to The kernel reads
After parsing, the values must satisfy:
1. The kernel accepts but
|
cpu.max |
Kernel result | Why crun rejects it |
|---|---|---|
"\tmax", "\nmax 100000" |
max / current period, max / 100000 | only ' ' is skipped before max (a number may start with any whitespace, because strtoll() skips it) |
"50000\n100000", "max\n100000" |
50000 / 100000, max / 100000 | a newline is not accepted as the separator |
"50000 100000 1", "50000 100000x", "50000 100000\n1" |
50000 / 100000 | trailing garbage |
"50000 -1", "50000 +100000", "50000 abc" |
50000 / current period (the invalid period is ignored) | invalid period |
"50000+200000", "50000-1", "50000x 100000" |
50000 / current period; 50000 / 100000 for the last one | garbage attached to the quota |
"18446744073709552616" (2^64 + 1000) |
quota 1000 (wraps around) | ERANGE |
2. parse_cpu_max() accepts but the kernel rejects
Both drivers fail. With systemd, the scope has already been set up when the
write to the container's cpu.max returns EINVAL, so the error shows up
later and is less clear.
cpu.max |
crun result | Why the kernel rejects it |
|---|---|---|
"+50000 100000" |
50000 / 100000 | %llu does not accept a + sign, and "+50000" is not "max" |
"50000 0", "max 0" |
period 0. With systemd this silently means "no period", and the default is used | period < 1000 |
"50000 999", "50000 1000001", "50000 18446744073709551615" |
period passed to systemd as is (UINT64_MAX is USEC_INFINITY) |
period out of [1000, 1000000] |
"999 100000", "17592186044416 100000" |
quota passed to systemd as is | quota out of [1000, 2^44−1] |
3. Both accept, but with different values
cpu.max |
Kernel | crun |
|---|---|---|
"000000000000000500001000" (a 24-character word) |
quota token "00000000000000050000" is cut at 20 characters, and the remaining "1000" becomes the period: 50000 / 1000 |
500001000 / default period |
"50000" (no period) |
keeps the cgroup's current period | linux.resources.cpu.period if set, otherwise the systemd default (100000) |
The last row only matters if the current period differs from what crun
sends to systemd, so in practice it probably does not.
Notes
- Group 1 is the only one where the systemd driver fails and the cgroupfs
driver works. Most of it is rejected on purpose by this PR, and runc
rejects the same values. Only"\tmax"looks like an accidental
inconsistency. - Group 2 is easiest to close with range checks in
parse_cpu_max()
itself.
|
To clarify: I think our rules must be at least as strict as the kernel (say the kernel does not accept |
d658210 to
0dbc50c
Compare
|
Thanks for going through this so carefully. The tables made it easy to see what was off. I agree with the rule: be at least as strict as the kernel, stricter is fine, but don't reject things like Group 1. Tabs and other whitespace now work around and between the two values, and so do empty lines before or after the value, so I kept rejecting a newline between the two values ( Group 2. I added the same checks the kernel does: no sign on the quota, a period between 1000 and 1000000, and a quota between 1000 and 2^44−1. These values now fail early with Group 3. The 24-character case is now rejected: a quota longer than 20 characters is an error. I took a closer look at I also wrote each test value to a real |
0dbc50c to
8275681
Compare
|
@kolyshkin while working on this, I looked for the same kind of problem in other parts of crun and found a few. I'm sharing them here so you know about them. I'll send fixes as separate PRs once this one is merged. 1. Other unified values with the systemd driver
2. The systemd driver rejects values that cgroupfs accepts This is allowed under the "stricter is fine" rule, but the two drivers behave differently:
I'm not sure these are worth supporting. What do you think? 3. The "memory limit could be too low" hint
|
|
@harche this looks much better now, thanks! Can you split the reading of the missing period during update into a separate commit? Also, Other than that, LGTM |
14f7727 to
ba90afb
Compare
|
@harche can you use your real email in |
|
@kolyshkin that's the address I use to sign off everywhere, including my Kubernetes commits. It's my GitHub noreply address, and I use it to keep my email away from spammers, since commit history is public and gets scraped. It's tied to my GitHub account, the name is my real name, and the DCO check on this PR passes. |
|
@kolyshkin thanks, both done:
Also:
Each commit builds and passes the unit tests on its own. |
|
Thanks for the update. I think this is getting more complicated than it needs to be. The goal is to keep crun from misreading cpu.max in ways that make the systemd scope differ from what the kernel sets. It is not to copy every check the kernel does. Anything we leave out is still rejected by the kernel when the value is written, so the error just shows up a bit later which is OK because we get an error anyway and it rarely makes sense to optimize for faster errors.
What I think the parser needs:
That fixes everything listed in #2405 in a much smaller diff, and the unit test tables get shorter too. The other two commits ("cgroup: add read_cpu_max()" and "cgroup-systemd: keep the current period on update") look good to me as they are. |
ba90afb to
147cfaf
Compare
|
@kolyshkin thanks, that makes sense. Done:
The parser now does what you listed: whitespace or the end of the string after the quota, a period of digits only, only whitespace after the values, and no period of 0. The other two commits are unchanged. One thing to know about 3. A line with only whitespace is now accepted by the parser, for example Tested on Fedora 44 (kernel 6.19, systemd 259):
|
147cfaf to
258f9ee
Compare
mrunalp
left a comment
There was a problem hiding this comment.
I tested both cases with crun update --resources on Fedora 45 (kernel 7.3.3, systemd 262.1), comparing main at 26f0fcff with this PR at 258f9ee2. Each run started with cpu.max set to 50000 100000.
| char *endptr = NULL; | ||
|
|
||
| while (*it == ' ') | ||
| while (isspace ((unsigned char) *it)) |
There was a problem hiding this comment.
Could we reject a whitespace-only line before updating the systemd scope? This loop skips the space and newline in " \nmax 200000", but write_unified_resources() sends the space to the kernel as a separate write.
I ran a systemd container with {"unified":{"cpu.max":" \nmax 200000"}} as the update payload. On the PR, crun update exited 1 with Invalid argument. The container’s cpu.max stayed at 50000 100000, but systemctl show reported that CPUQuotaPerSecUSec went from 500ms to infinity and CPUQuotaPeriodUSec from 100ms to 200ms. Main rejected the value before either scope property changed. This would be worth covering with a failed-update test that checks the scope afterward.
There was a problem hiding this comment.
Thanks, fixed in 4f30a7e. The value must now be a single non-empty line, so a whitespace-only line is rejected before the scope is updated.
crun/src/libcrun/cgroup-utils.c
Lines 1463 to 1467 in 4a2115e
I also added update-unified-cpu-max-invalid-systemd, which runs failed updates and checks that the scope and the container's cpu.max are unchanged.
|
|
||
| it = endptr; | ||
| while (*it == ' ') | ||
| while (*it == ' ' || *it == '\t') |
There was a problem hiding this comment.
Could this loop accept same-line whitespace such as \r, while still stopping at \n? I ran crun update --resources with {"unified":{"cpu.max":"50000\r200000"}}. On main, both cgroupfs and systemd applied 50000 200000. On the PR, cgroupfs still applied it, but systemd returned invalid value for `cpu.max` and left the container at 50000 100000.
write_unified_resources() splits only on \n, so the kernel receives the carriage return in a single write. Adding this input to the valid parser cases would cover the regression.
There was a problem hiding this comment.
Thanks, fixed in 4f30a7e. Any whitespace is accepted between the values now, so "50000\r200000" gives 50000 200000 with both drivers again. I added it to the valid parser cases.
crun/src/libcrun/cgroup-utils.c
Lines 1488 to 1490 in 4a2115e
258f9ee to
ca0596f
Compare
With the systemd driver, parse_cpu_max() turns cpu.max from the unified map into CPUQuotaPerSecUSec and CPUQuotaPeriodUSec, while the raw string is still written to the container's cpu.max. When crun and the kernel read the string differently, the scope and the container end up with different limits, or the container fails to start with an sd-bus error. - "50000+200000", "max5": no separator was required after the quota, so crun read "200000" and "5" as the period, which the kernel does not. Require whitespace or the end of the string after the quota. - "50000 -1", "2 -9": strtoull() accepts a sign and wraps a negative period around, while the kernel ignores such a period. Accept only digits. - "50000\n100000", "max\n100000": everything after the first newline was ignored, but write_unified_resources() writes each line separately, so the kernel gets "50000" and then "100000". Likewise, with " \n50000 100000" or "50000 100000\n " the kernel gets a lone " " and fails after the systemd scope was already changed. Require the value to be a single non-empty line, ignoring empty lines around it, and allow only whitespace after the values. - "50000 0": systemd reads a period of 0 as not set. Reject it. - "\tmax", "\nmax 100000": the kernel accepts them, but only spaces were skipped before the quota. Skip any whitespace around and between the values, so that "50000\r200000" keeps working. Other values that the kernel rejects, such as a quota or a period out of its range, are not checked here. They fail when cpu.max is written, as they do for cpu.quota and cpu.period. Closes: containers#2405 Signed-off-by: Harshal Patil <12152047+harche@users.noreply.github.com> Assisted-by: Claude Code (claude-opus-5-5) Assisted-by: Claude Code (claude-fable-5-2)
Move the reading of a cgroup's cpu.max out of complete_cpu_max(), so that it can be used by the systemd driver too. Signed-off-by: Harshal Patil <12152047+harche@users.noreply.github.com> Assisted-by: Claude Code (claude-opus-5-5)
When cpu.max in the unified map has no period, the kernel keeps the cgroup's current period, but on update crun computed CPUQuotaPerSecUSec with the default period. For a container with a period of 200000, updating cpu.max to "50000" gave the container "50000 200000", while systemd recorded CPUQuotaPerSecUSec=500ms instead of 250ms. Read the current period from the container's cpu.max instead. Only do this when systemd supports CPUQuotaPeriodUSec. Older systemd always applies its default period, so the quota must be computed with that period. Signed-off-by: Harshal Patil <12152047+harche@users.noreply.github.com> Assisted-by: Claude Code (claude-opus-5-5)
ca0596f to
4a2115e
Compare
| *has_period = false; | ||
| if (*it && *it != '\n') | ||
| /* strtoull() would also accept a sign, and wrap a negative value. */ | ||
| if (isdigit ((unsigned char) *it)) |
There was a problem hiding this comment.
do we need the same check for quota (line 1480)? Does the kernel complain in this case or a trailing + is fine?
There was a problem hiding this comment.
I'd also suggest adding some tests with +QUOTA and +PERIOD
|
|
||
| ret = append_resources (m, true, state_dir, resources, cgroup_mode, cgroup_status->scope, &cgroup_status->bpf_dev_set, | ||
| &devices_map, err); | ||
| ret = append_resources (m, true, state_dir, resources, cgroup_mode, cgroup_status->scope, cgroup_status->path, |
There was a problem hiding this comment.
let's use the systemd scope cgroup (could be different if using a subcgroup for the container).
You can just move the definition at line 2462 earlier and use it here
| return ret; | ||
| if (has_period) | ||
| period = max_period; | ||
| else if (period == 0 && quota > 0 && cgroup_path && ! property_missing_p (missing_properties, "CPUQuotaPeriodUSec")) |
There was a problem hiding this comment.
Could we read the container’s current period here even when CPUQuotaPeriodUSec is unavailable? A quota-only cpu.max write keeps the container’s period, so we still need that period to calculate the requested CPU rate for CPUQuotaPerSecUSec.
I checked append_cpu_quota() with a C harness that supplied a current cpu.max of 25000 50000 and a quota-only update of 50000. With the period property available, crun emitted CPUQuotaPerSecUSec=1000000, matching the requested one CPU. Marking just CPUQuotaPeriodUSec missing made crun emit CPUQuotaPerSecUSec=500000. That would cap the parent scope at half a CPU even though the container requests one.
Removing the property check from this condition would preserve the rate calculation. The check below already handles whether to send CPUQuotaPeriodUSec to systemd.
parse_cpu_max()read somecpu.maxvalues differently from the kernel. With the systemd driver, this could give the scope and the container different CPU limits, or make the container fail to start with an sd-bus error. Details are in #2405.This fixes the cases where crun and the kernel read the value differently:
max), whitespace or the end of the string is required.write_unified_resources()writes each line separately, so the kernel would see"50000\n100000"as two writes."\tmax"and"\nmax 100000"are accepted, as the kernel accepts them.crun update, whencpu.maxhas no period, the current period is read from the container'scpu.max, like the kernel does, instead of using the default to computeCPUQuotaPerSecUSec.Other values that the kernel rejects, such as a quota or a period out of its range, are not checked here. They fail when
cpu.maxis written, as they do forcpu.quotaandcpu.period.Well-formed values are read as before, including what the kernel writes in
cpu.max(max 100000\n), whichcomplete_cpu_max()reads back.Testing
On Fedora 44, kernel 6.19, systemd 259.
tests/tests_libcrun_cgroup.c:test_parse_cpu_maxfor valid values,test_parse_cpu_max_invalidfor the malformed ones."2 -9","50000+200000","max5","5100000\n0000","50000 -1") is rejected withinvalid value for cpu.max. So are"50000\n100000"and"max\n100000"(with the cgroupfs driver, the container ends up with quota 100000)."\tmax","\nmax 100000"and"50000 100000\n\n"give the scope and the container the same limits."50000 999","999 100000","+50000 100000") fail at the write tocpu.maxwith both drivers, the same way typedcpu.quota/cpu.perioddo.cpu.period200000, then rancrun updatewithunified["cpu.max"] = "50000". Before, the container got50000 200000but systemd recordedCPUQuotaPerSecUSec=500ms. Now systemd records250mswith a200msperiod, which matches.tests/test_update.pypasses with bothCGROUP_MANAGER=cgroupfsandsystemd.tests/test_cgroup_resources.pypasses exceptcgroup-resources-hugetlb-limits, which fails the same way onmain(no hugetlb controller in the VM).make clang-formatis clean.