Skip to content

Commit

Permalink
libcontainer: set PidsLimit to be a pointer
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Hunt <pehunt@redhat.com>
  • Loading branch information
haircommander committed Sep 13, 2023
1 parent fefc1d3 commit aa86265
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 11 deletions.
7 changes: 5 additions & 2 deletions libcontainer/cgroups/fs/pids.go
Expand Up @@ -20,11 +20,14 @@ func (s *PidsGroup) Apply(path string, _ *configs.Resources, pid int) error {
}

func (s *PidsGroup) Set(path string, r *configs.Resources) error {
if r.PidsLimit == nil {
return nil
}
// "max" is the fallback value.
limit := "max"

if r.PidsLimit > 0 {
limit = strconv.FormatInt(r.PidsLimit, 10)
if *r.PidsLimit > 0 {
limit = strconv.FormatInt(*r.PidsLimit, 10)
}

return cgroups.WriteFile(path, "pids.max", limit)
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/cgroups/fs2/create.go
Expand Up @@ -39,7 +39,7 @@ func needAnyControllers(r *configs.Resources) (bool, error) {
return ok
}

if have("pids") {
if r.PidsLimit != nil && have("pids") {
return true, nil
}
if isMemorySet(r) && have("memory") {
Expand Down
5 changes: 4 additions & 1 deletion libcontainer/cgroups/fs2/pids.go
Expand Up @@ -14,7 +14,10 @@ import (
)

func setPids(dirPath string, r *configs.Resources) error {
if val := numToStr(r.PidsLimit); val != "" {
if r.PidsLimit == nil {
return nil
}
if val := numToStr(*r.PidsLimit); val != "" {
if err := cgroups.WriteFile(dirPath, "pids.max", val); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/cgroups/systemd/v1.go
Expand Up @@ -98,9 +98,9 @@ func genV1ResourcesProperties(r *configs.Resources, cm *dbusConnManager) ([]syst
newProp("BlockIOWeight", uint64(r.BlkioWeight)))
}

if r.PidsLimit > 0 || r.PidsLimit == -1 {
if r.PidsLimit != nil && (*r.PidsLimit > 0 || *r.PidsLimit == -1) {
properties = append(properties,
newProp("TasksMax", uint64(r.PidsLimit)))
newProp("TasksMax", uint64(*r.PidsLimit)))
}

err = addCpuset(cm, &properties, r.CpusetCpus, r.CpusetMems)
Expand Down
4 changes: 2 additions & 2 deletions libcontainer/cgroups/systemd/v2.go
Expand Up @@ -257,9 +257,9 @@ func genV2ResourcesProperties(dirPath string, r *configs.Resources, cm *dbusConn

addCpuQuota(cm, &properties, r.CpuQuota, r.CpuPeriod)

if r.PidsLimit > 0 || r.PidsLimit == -1 {
if r.PidsLimit != nil && (*r.PidsLimit > 0 || *r.PidsLimit == -1) {
properties = append(properties,
newProp("TasksMax", uint64(r.PidsLimit)))
newProp("TasksMax", uint64(*r.PidsLimit)))
}

err = addCpuset(cm, &properties, r.CpusetCpus, r.CpusetMems)
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/configs/cgroup_linux.go
Expand Up @@ -88,7 +88,7 @@ type Resources struct {
CPUIdle *int64 `json:"cpu_idle,omitempty"`

// Process limit; set <= `0' to disable limit.
PidsLimit int64 `json:"pids_limit"`
PidsLimit *int64 `json:"pids_limit"`

// Specifies per cgroup weight, range is from 10 to 1000.
BlkioWeight uint16 `json:"blkio_weight"`
Expand Down
3 changes: 2 additions & 1 deletion libcontainer/specconv/spec_linux.go
Expand Up @@ -768,7 +768,8 @@ func CreateCgroupConfig(opts *CreateOpts, defaultDevs []*devices.Device) (*confi
c.Resources.CPUIdle = r.CPU.Idle
}
if r.Pids != nil {
c.Resources.PidsLimit = r.Pids.Limit
p := r.Pids.Limit
c.Resources.PidsLimit = &p
}
if r.BlockIO != nil {
if r.BlockIO.Weight != nil {
Expand Down
3 changes: 2 additions & 1 deletion update.go
Expand Up @@ -310,8 +310,9 @@ other options are ignored.
config.Cgroups.Resources.MemoryReservation = *r.Memory.Reservation
config.Cgroups.Resources.MemorySwap = *r.Memory.Swap
config.Cgroups.Resources.MemoryCheckBeforeUpdate = *r.Memory.CheckBeforeUpdate
config.Cgroups.Resources.PidsLimit = r.Pids.Limit
config.Cgroups.Resources.Unified = r.Unified
l := r.Pids.Limit
config.Cgroups.Resources.PidsLimit = &l

// Update Intel RDT
l3CacheSchema := context.String("l3-cache-schema")
Expand Down

0 comments on commit aa86265

Please sign in to comment.