]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
cgroup/misc: Introduce misc.peak
authorXiu Jianfeng <xiujianfeng@huawei.com>
Wed, 3 Jul 2024 00:36:46 +0000 (00:36 +0000)
committerTejun Heo <tj@kernel.org>
Wed, 3 Jul 2024 18:08:43 +0000 (08:08 -1000)
Introduce misc.peak to record the historical maximum usage of the
resource, as in some scenarios the value of misc.max could be
adjusted based on the peak usage of the resource.

Signed-off-by: Xiu Jianfeng <xiujianfeng@huawei.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
Documentation/admin-guide/cgroup-v2.rst
include/linux/misc_cgroup.h
kernel/cgroup/misc.c

index 2e4e74bea6ef95b315421e3e7ac9e4c4f232356b..52763d6b2919c9b0f24465503982345cde66cba4 100644 (file)
@@ -2642,6 +2642,15 @@ Miscellaneous controller provides 3 interface files. If two misc resources (res_
          res_a 3
          res_b 0
 
+  misc.peak
+        A read-only flat-keyed file shown in all cgroups.  It shows the
+        historical maximum usage of the resources in the cgroup and its
+        children.::
+
+         $ cat misc.peak
+         res_a 10
+         res_b 8
+
   misc.max
         A read-write flat-keyed file shown in the non root cgroups. Allowed
         maximum usage of the resources in the cgroup and its children.::
index d70eab2501ee031efb8d4c18b1325461f1f675dd..618392d4197532a779ea21ea5b7a4b5ba0fa4431 100644 (file)
@@ -31,11 +31,13 @@ struct misc_cg;
 /**
  * struct misc_res: Per cgroup per misc type resource
  * @max: Maximum limit on the resource.
+ * @watermark: Historical maximum usage of the resource.
  * @usage: Current usage of the resource.
  * @events: Number of times, the resource limit exceeded.
  */
 struct misc_res {
        u64 max;
+       atomic64_t watermark;
        atomic64_t usage;
        atomic64_t events;
 };
index 79a3717a5803aa40c7674edc8e0c37443168b33f..b92daf5d234d047e9107fa286b0078d0c39c1519 100644 (file)
@@ -121,6 +121,19 @@ static void misc_cg_cancel_charge(enum misc_res_type type, struct misc_cg *cg,
                  misc_res_name[type]);
 }
 
+static void misc_cg_update_watermark(struct misc_res *res, u64 new_usage)
+{
+       u64 old;
+
+       while (true) {
+               old = atomic64_read(&res->watermark);
+               if (new_usage <= old)
+                       break;
+               if (atomic64_cmpxchg(&res->watermark, old, new_usage) == old)
+                       break;
+       }
+}
+
 /**
  * misc_cg_try_charge() - Try charging the misc cgroup.
  * @type: Misc res type to charge.
@@ -159,6 +172,7 @@ int misc_cg_try_charge(enum misc_res_type type, struct misc_cg *cg, u64 amount)
                        ret = -EBUSY;
                        goto err_charge;
                }
+               misc_cg_update_watermark(res, new_usage);
        }
        return 0;
 
@@ -307,6 +321,29 @@ static int misc_cg_current_show(struct seq_file *sf, void *v)
        return 0;
 }
 
+/**
+ * misc_cg_peak_show() - Show the peak usage of the misc cgroup.
+ * @sf: Interface file
+ * @v: Arguments passed
+ *
+ * Context: Any context.
+ * Return: 0 to denote successful print.
+ */
+static int misc_cg_peak_show(struct seq_file *sf, void *v)
+{
+       int i;
+       u64 watermark;
+       struct misc_cg *cg = css_misc(seq_css(sf));
+
+       for (i = 0; i < MISC_CG_RES_TYPES; i++) {
+               watermark = atomic64_read(&cg->res[i].watermark);
+               if (READ_ONCE(misc_res_capacity[i]) || watermark)
+                       seq_printf(sf, "%s %llu\n", misc_res_name[i], watermark);
+       }
+
+       return 0;
+}
+
 /**
  * misc_cg_capacity_show() - Show the total capacity of misc res on the host.
  * @sf: Interface file
@@ -357,6 +394,10 @@ static struct cftype misc_cg_files[] = {
                .name = "current",
                .seq_show = misc_cg_current_show,
        },
+       {
+               .name = "peak",
+               .seq_show = misc_cg_peak_show,
+       },
        {
                .name = "capacity",
                .seq_show = misc_cg_capacity_show,