[lxc-devel] [lxd/master] Fix blkio.weight cgroup handling

stgraber on Github lxc-bot at linuxcontainers.org
Sun Nov 8 23:39:51 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 301 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20201108/6ce5a5c9/attachment.bin>
-------------- next part --------------
From ce44bbbbe0d7ef2f07c31db3142c6be715ec4ff1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Sun, 8 Nov 2020 16:59:32 -0500
Subject: [PATCH 1/2] lxd/cgroup: Add V2 for GetBlkioWeight and SetBlkioWeight
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/cgroup/abstraction.go | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/lxd/cgroup/abstraction.go b/lxd/cgroup/abstraction.go
index bc9b085375..e2acd6ce08 100644
--- a/lxd/cgroup/abstraction.go
+++ b/lxd/cgroup/abstraction.go
@@ -376,7 +376,12 @@ func (cg *CGroup) GetBlkioWeight() (int64, error) {
 
 		return strconv.ParseInt(val, 10, 64)
 	case V2:
-		return -1, ErrControllerMissing
+		val, err := cg.rw.Get(version, "io", "io.weight")
+		if err != nil {
+			return -1, err
+		}
+
+		return strconv.ParseInt(val, 10, 64)
 	}
 
 	return -1, ErrUnknownVersion
@@ -391,7 +396,7 @@ func (cg *CGroup) SetBlkioWeight(limit int64) error {
 	case V1:
 		return cg.rw.Set(version, "blkio", "blkio.weight", fmt.Sprintf("%d", limit))
 	case V2:
-		return ErrControllerMissing
+		return cg.rw.Set(version, "blkio", "io.weight", fmt.Sprintf("%d", limit))
 	}
 
 	return ErrUnknownVersion

From 8830827fb9bfae94fc098daee6e3e8c537b5362e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Sun, 8 Nov 2020 18:18:22 -0500
Subject: [PATCH 2/2] lxd/device: Move disk priority back to lxc
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 lxd/device/disk.go                 | 25 -------------------------
 lxd/instance/drivers/driver_lxc.go | 25 +++++++++++++++++++++++++
 2 files changed, 25 insertions(+), 25 deletions(-)

diff --git a/lxd/device/disk.go b/lxd/device/disk.go
index da3f862a68..7981de70e3 100644
--- a/lxd/device/disk.go
+++ b/lxd/device/disk.go
@@ -716,31 +716,6 @@ func (d *disk) applyQuota(newSize string) error {
 
 // generateLimits adds a set of cgroup rules to apply specified limits to the supplied RunConfig.
 func (d *disk) generateLimits(runConf *deviceConfig.RunConfig) error {
-	// Disk priority limits.
-	diskPriority := d.inst.ExpandedConfig()["limits.disk.priority"]
-	if diskPriority != "" {
-		if d.state.OS.CGInfo.Supports(cgroup.BlkioWeight, nil) {
-			priorityInt, err := strconv.Atoi(diskPriority)
-			if err != nil {
-				return err
-			}
-
-			priority := priorityInt * 100
-
-			// Minimum valid value is 10
-			if priority == 0 {
-				priority = 10
-			}
-
-			runConf.CGroups = append(runConf.CGroups, deviceConfig.RunConfigItem{
-				Key:   "blkio.weight",
-				Value: fmt.Sprintf("%d", priority),
-			})
-		} else {
-			return fmt.Errorf("Cannot apply limits.disk.priority as blkio.weight cgroup controller is missing")
-		}
-	}
-
 	// Disk throttle limits.
 	hasDiskLimits := false
 	for _, dev := range d.inst.ExpandedDevices() {
diff --git a/lxd/instance/drivers/driver_lxc.go b/lxd/instance/drivers/driver_lxc.go
index 74657da7a8..31d1961697 100644
--- a/lxd/instance/drivers/driver_lxc.go
+++ b/lxd/instance/drivers/driver_lxc.go
@@ -1224,6 +1224,31 @@ func (c *lxc) initLXC(config bool) error {
 		}
 	}
 
+	// Disk priority limits.
+	diskPriority := c.ExpandedConfig()["limits.disk.priority"]
+	if diskPriority != "" {
+		if c.state.OS.CGInfo.Supports(cgroup.BlkioWeight, nil) {
+			priorityInt, err := strconv.Atoi(diskPriority)
+			if err != nil {
+				return err
+			}
+
+			priority := priorityInt * 100
+
+			// Minimum valid value is 10
+			if priority == 0 {
+				priority = 10
+			}
+
+			err = cg.SetBlkioWeight(int64(priority))
+			if err != nil {
+				return err
+			}
+		} else {
+			return fmt.Errorf("Cannot apply limits.disk.priority as blkio.weight cgroup controller is missing")
+		}
+	}
+
 	// Processes
 	if c.state.OS.CGInfo.Supports(cgroup.Pids, cg) {
 		processes := c.expandedConfig["limits.processes"]


More information about the lxc-devel mailing list