Skip to content

Commit 0d2cb55

Browse files
authored
Merge pull request #79 from kolyshkin/systemd-cpu-quota-overflow
systemd: fix CPU quota overflow and unified cpu.max round-up
2 parents 42d84ca + 0a10b5b commit 0d2cb55

3 files changed

Lines changed: 142 additions & 6 deletions

File tree

‎systemd/common.go‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ const (
2424
// v1: https://www.kernel.org/doc/html/latest/scheduler/sched-bwc.html and
2525
// v2: https://www.kernel.org/doc/html/latest/admin-guide/cgroup-v2.html
2626
defCPUQuotaPeriod = uint64(100000)
27+
28+
// maxCPUQuota is the maximum CPU quota (in microseconds) accepted by the
29+
// kernel (see max_cfs_runtime in kernel/sched/core.c and MAX_BW in
30+
// kernel/sched/sched.h). A larger quota is treated as unlimited.
31+
// Note that maxCPUQuota * 1000000 does not overflow uint64.
32+
maxCPUQuota = 1<<44 - 1
2733
)
2834

2935
var (
@@ -320,11 +326,22 @@ func addCPUQuota(cm *dbusConnManager, properties *[]systemdDbus.Property, quota
320326
// (integer percentage of CPU) internally. This means that if a fractional percent of
321327
// CPU is indicated by Resources.CpuQuota, we need to round up to the nearest
322328
// 10ms (1% of a second) such that child cgroups can set the cpu.cfs_quota_us they expect.
323-
cpuQuotaPerSecUSec = uint64(*quota*1000000) / period
324-
if cpuQuotaPerSecUSec%10000 != 0 {
325-
cpuQuotaPerSecUSec = ((cpuQuotaPerSecUSec / 10000) + 1) * 10000
326-
// Update the requested quota along with the round-up in order to write the same value to cgroupfs.
327-
*quota = int64(cpuQuotaPerSecUSec) * int64(period) / 1000000
329+
if *quota <= maxCPUQuota {
330+
// No overflow is possible here since *quota <= maxCPUQuota.
331+
cpuQuotaPerSecUSec = uint64(*quota) * 1000000 / period
332+
if cpuQuotaPerSecUSec%10000 != 0 {
333+
cpuQuotaPerSecUSec = ((cpuQuotaPerSecUSec / 10000) + 1) * 10000
334+
// Update the requested quota along with the round-up in order to write the same value to cgroupfs.
335+
// This is cpuQuotaPerSecUSec * period / 1000000, rearranged to avoid overflow.
336+
*quota = int64(cpuQuotaPerSecUSec / 10000 * period / 100)
337+
}
338+
}
339+
if *quota > maxCPUQuota {
340+
// The quota (possibly after the round-up above) is too large
341+
// for the kernel to accept. Since it is effectively unlimited,
342+
// treat it as such, for both systemd and cgroupfs.
343+
cpuQuotaPerSecUSec = math.MaxUint64
344+
*quota = -1
328345
}
329346
}
330347
*properties = append(*properties,

‎systemd/systemd_test.go‎

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package systemd
22

33
import (
4+
"maps"
5+
"math"
46
"os"
57
"os/exec"
68
"reflect"
@@ -115,6 +117,7 @@ func TestUnifiedResToSystemdProps(t *testing.T) {
115117
res map[string]string
116118
expError bool
117119
expProps []systemdDbus.Property
120+
resAfter map[string]string // If nil, res is expected to be unchanged.
118121
}{
119122
{
120123
name: "empty map",
@@ -233,13 +236,81 @@ func TestUnifiedResToSystemdProps(t *testing.T) {
233236
},
234237
expError: true,
235238
},
239+
{
240+
name: "cpu.max",
241+
minVer: cpuQuotaPeriodSupportedVersion,
242+
res: map[string]string{
243+
"cpu.max": "500000 1000000",
244+
},
245+
expProps: []systemdDbus.Property{
246+
newProp("CPUQuotaPeriodUSec", uint64(1000000)),
247+
newProp("CPUQuotaPerSecUSec", uint64(500000)),
248+
},
249+
},
250+
{
251+
name: "cpu.max with round up",
252+
minVer: cpuQuotaPeriodSupportedVersion,
253+
res: map[string]string{
254+
"cpu.max": "123456 100000",
255+
},
256+
expProps: []systemdDbus.Property{
257+
newProp("CPUQuotaPeriodUSec", uint64(100000)),
258+
newProp("CPUQuotaPerSecUSec", uint64(1240000)),
259+
},
260+
resAfter: map[string]string{
261+
"cpu.max": "124000 100000",
262+
},
263+
},
264+
{
265+
name: "cpu.max with round up, no period",
266+
minVer: cpuQuotaPeriodSupportedVersion,
267+
res: map[string]string{
268+
"cpu.max": "123456",
269+
},
270+
expProps: []systemdDbus.Property{
271+
newProp("CPUQuotaPeriodUSec", uint64(100000)),
272+
newProp("CPUQuotaPerSecUSec", uint64(1240000)),
273+
},
274+
resAfter: map[string]string{
275+
"cpu.max": "124000",
276+
},
277+
},
278+
{
279+
name: "cpu.max too large",
280+
minVer: cpuQuotaPeriodSupportedVersion,
281+
res: map[string]string{
282+
"cpu.max": "9223372036854700 100000",
283+
},
284+
expProps: []systemdDbus.Property{
285+
newProp("CPUQuotaPeriodUSec", uint64(100000)),
286+
newProp("CPUQuotaPerSecUSec", uint64(math.MaxUint64)),
287+
},
288+
resAfter: map[string]string{
289+
"cpu.max": "max 100000",
290+
},
291+
},
292+
{
293+
name: "cpu.max=max",
294+
minVer: cpuQuotaPeriodSupportedVersion,
295+
res: map[string]string{
296+
"cpu.max": "max 100000",
297+
},
298+
expProps: []systemdDbus.Property{
299+
newProp("CPUQuotaPeriodUSec", uint64(100000)),
300+
newProp("CPUQuotaPerSecUSec", uint64(math.MaxUint64)),
301+
},
302+
},
236303
}
237304

238305
for _, tc := range testCases {
239306
t.Run(tc.name, func(t *testing.T) {
240307
if tc.minVer != 0 && systemdVersion(cm) < tc.minVer {
241308
t.Skipf("requires systemd >= %d", tc.minVer)
242309
}
310+
resAfter := tc.resAfter
311+
if resAfter == nil {
312+
resAfter = maps.Clone(tc.res)
313+
}
243314
props, err := unifiedResToSystemdProps(cm, tc.res)
244315
if err != nil && !tc.expError {
245316
t.Fatalf("expected no error, got: %v", err)
@@ -250,6 +321,9 @@ func TestUnifiedResToSystemdProps(t *testing.T) {
250321
if !reflect.DeepEqual(tc.expProps, props) {
251322
t.Errorf("wrong properties (exp %+v, got %+v)", tc.expProps, props)
252323
}
324+
if !maps.Equal(resAfter, tc.res) {
325+
t.Errorf("wrong resources (exp %+v, got %+v)", resAfter, tc.res)
326+
}
253327
})
254328
}
255329
}
@@ -288,6 +362,37 @@ func TestAddCPUQuota(t *testing.T) {
288362
expectedCPUQuotaPerSecUSec: 560000,
289363
expectedQuota: 504000,
290364
},
365+
{
366+
name: "Large quota with round up",
367+
quota: 10000000000001,
368+
expectedCPUQuotaPerSecUSec: 100000000010000,
369+
expectedQuota: 10000000001000,
370+
},
371+
{
372+
name: "Max quota",
373+
quota: maxCPUQuota,
374+
period: 1500,
375+
expectedCPUQuotaPerSecUSec: 11728124029610000,
376+
expectedQuota: maxCPUQuota,
377+
},
378+
{
379+
name: "Max quota, round up above max",
380+
quota: maxCPUQuota,
381+
expectedCPUQuotaPerSecUSec: math.MaxUint64,
382+
expectedQuota: -1,
383+
},
384+
{
385+
name: "Quota too large",
386+
quota: maxCPUQuota + 1,
387+
expectedCPUQuotaPerSecUSec: math.MaxUint64,
388+
expectedQuota: -1,
389+
},
390+
{
391+
name: "Max int64 quota",
392+
quota: math.MaxInt64,
393+
expectedCPUQuotaPerSecUSec: math.MaxUint64,
394+
expectedQuota: -1,
395+
},
291396
}
292397

293398
for _, tc := range testCases {

‎systemd/v2.go‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package systemd
22

33
import (
44
"fmt"
5+
"maps"
56
"math"
67
"path/filepath"
78
"strconv"
@@ -74,6 +75,8 @@ func shouldSetCPUIdle(cm *dbusConnManager, v string) bool {
7475
// For the list of keys, see https://www.kernel.org/doc/Documentation/cgroup-v2.txt
7576
//
7677
// For the list of systemd unit properties, see systemd.resource-control(5).
78+
//
79+
// The value of res["cpu.max"] may be modified (see addCPUQuota).
7780
func unifiedResToSystemdProps(cm *dbusConnManager, res map[string]string) (props []systemdDbus.Property, _ error) {
7881
var err error
7982

@@ -119,7 +122,17 @@ func unifiedResToSystemdProps(cm *dbusConnManager, res map[string]string) (props
119122
return nil, fmt.Errorf("unified resource %q quota value conversion error: %w", k, err)
120123
}
121124
}
125+
origQuota := quota
122126
addCPUQuota(cm, &props, &quota, period)
127+
if quota != origQuota {
128+
// Update the value along with the round-up in order
129+
// to write the same value to cgroupfs.
130+
sv[0] = "max"
131+
if quota > 0 {
132+
sv[0] = strconv.FormatInt(quota, 10)
133+
}
134+
res[k] = strings.Join(sv, " ")
135+
}
123136

124137
case "cpu.weight":
125138
if shouldSetCPUIdle(cm, strings.TrimSpace(res["cpu.idle"])) {
@@ -513,8 +526,9 @@ func (m *UnifiedManager) Set(r *cgroups.Resources) error {
513526
if r == nil {
514527
return nil
515528
}
516-
// Use a copy since CpuQuota in r may be modified.
529+
// Use a copy since CpuQuota and Unified["cpu.max"] in r may be modified.
517530
rCopy := *r
531+
rCopy.Unified = maps.Clone(r.Unified)
518532
r = &rCopy
519533
properties, err := genV2ResourcesProperties(m.fsMgr.Path(""), r, m.dbus)
520534
if err != nil {

0 commit comments

Comments
 (0)