Introduce the "high" soft limit for the dmem cgroup v2 controller. When a cgroup's device memory usage exceeds its high limit, tasks belonging to that cgroup are throttled by being forced into a sleep before returning to user space, instead of being failed outright as with the "max" limit.
Key changes: - Add high counter configuration to dmem_cgroup_pool. - Add over-high check in the try_charge path and set TIF_NOTIFY_RESUME. - Inject the dmem throttling handler into resume_user_mode_work. - Implement the handler to perform a 100ms interruptible sleep for over-limit tasks. This mechanism provides smoother over-subscription support for device memory resources. Signed-off-by: Qiliang Yuan <[email protected]> --- This series introduces the "high" soft limit and associated task throttling mechanism to the dmem cgroup v2 controller. The device memory (VRAM) management currently only supports hard limits (max), which leads to immediate allocation failures when reached. This can be disruptive for GPU-bound AI workloads. By introducing a soft limit, we allow cgroups to exceed their quota temporarily while applying backpressure via task throttling before the process returns to user space. The mechanism is inspired by the memory cgroup's high limit: - When usage > high, the task is marked with TIF_NOTIFY_RESUME. - Upon returning to user space, it triggers a 100ms sleep. - This provides a smoother over-subscription model for GPU resources. Qiliang Yuan (1): cgroup/dmem: implement dmem.high soft limit and throttling --- To: Maarten Lankhorst <[email protected]> To: Maxime Ripard <[email protected]> To: Natalie Vock <[email protected]> To: Tejun Heo <[email protected]> To: Johannes Weiner <[email protected]> To: Michal Koutný <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] --- include/linux/cgroup_dmem.h | 10 +++++++ include/linux/resume_user_mode.h | 2 ++ kernel/cgroup/dmem.c | 60 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/include/linux/cgroup_dmem.h b/include/linux/cgroup_dmem.h index dd4869f1d736e..d58972de7c910 100644 --- a/include/linux/cgroup_dmem.h +++ b/include/linux/cgroup_dmem.h @@ -21,6 +21,13 @@ int dmem_cgroup_try_charge(struct dmem_cgroup_region *region, u64 size, struct dmem_cgroup_pool_state **ret_pool, struct dmem_cgroup_pool_state **ret_limit_pool); void dmem_cgroup_uncharge(struct dmem_cgroup_pool_state *pool, u64 size); +void __dmem_cgroup_handle_over_high(void); + +static inline void dmem_cgroup_handle_over_high(void) +{ + __dmem_cgroup_handle_over_high(); +} + bool dmem_cgroup_state_evict_valuable(struct dmem_cgroup_pool_state *limit_pool, struct dmem_cgroup_pool_state *test_pool, bool ignore_low, bool *ret_hit_low); @@ -51,6 +58,9 @@ static inline int dmem_cgroup_try_charge(struct dmem_cgroup_region *region, u64 static inline void dmem_cgroup_uncharge(struct dmem_cgroup_pool_state *pool, u64 size) { } +static inline void dmem_cgroup_handle_over_high(void) +{ } + static inline bool dmem_cgroup_state_evict_valuable(struct dmem_cgroup_pool_state *limit_pool, struct dmem_cgroup_pool_state *test_pool, diff --git a/include/linux/resume_user_mode.h b/include/linux/resume_user_mode.h index bf92227c78d0d..afcab20998c41 100644 --- a/include/linux/resume_user_mode.h +++ b/include/linux/resume_user_mode.h @@ -8,6 +8,7 @@ #include <linux/memcontrol.h> #include <linux/rseq.h> #include <linux/blk-cgroup.h> +#include <linux/cgroup_dmem.h> /** * set_notify_resume - cause resume_user_mode_work() to be called @@ -58,6 +59,7 @@ static inline void resume_user_mode_work(struct pt_regs *regs) mem_cgroup_handle_over_high(GFP_KERNEL); blkcg_maybe_throttle_current(); + dmem_cgroup_handle_over_high(); rseq_handle_slowpath(regs); } diff --git a/kernel/cgroup/dmem.c b/kernel/cgroup/dmem.c index 4753a67d0f0f2..f77c692b887b1 100644 --- a/kernel/cgroup/dmem.c +++ b/kernel/cgroup/dmem.c @@ -15,6 +15,7 @@ #include <linux/page_counter.h> #include <linux/parser.h> #include <linux/refcount.h> +#include <linux/resume_user_mode.h> #include <linux/rculist.h> #include <linux/slab.h> @@ -156,6 +157,12 @@ set_resource_low(struct dmem_cgroup_pool_state *pool, u64 val) page_counter_set_low(&pool->cnt, val); } +static void +set_resource_high(struct dmem_cgroup_pool_state *pool, u64 val) +{ + page_counter_set_high(&pool->cnt, val); +} + static void set_resource_max(struct dmem_cgroup_pool_state *pool, u64 val) { @@ -167,6 +174,11 @@ static u64 get_resource_low(struct dmem_cgroup_pool_state *pool) return pool ? READ_ONCE(pool->cnt.low) : 0; } +static u64 get_resource_high(struct dmem_cgroup_pool_state *pool) +{ + return pool ? READ_ONCE(pool->cnt.high) : 0; +} + static u64 get_resource_min(struct dmem_cgroup_pool_state *pool) { return pool ? READ_ONCE(pool->cnt.min) : 0; @@ -186,6 +198,7 @@ static void reset_all_resource_limits(struct dmem_cgroup_pool_state *rpool) { set_resource_min(rpool, 0); set_resource_low(rpool, 0); + set_resource_high(rpool, PAGE_COUNTER_MAX); set_resource_max(rpool, PAGE_COUNTER_MAX); } @@ -685,6 +698,9 @@ int dmem_cgroup_try_charge(struct dmem_cgroup_region *region, u64 size, goto err; } + if (page_counter_read(&pool->cnt) > READ_ONCE(pool->cnt.high)) + set_notify_resume(current); + /* On success, reference from get_current_dmemcs is transferred to *ret_pool */ *ret_pool = pool; return 0; @@ -835,13 +851,24 @@ static ssize_t dmem_cgroup_region_low_write(struct kernfs_open_file *of, return dmemcg_limit_write(of, buf, nbytes, off, set_resource_low); } +static int dmem_cgroup_region_high_show(struct seq_file *sf, void *v) +{ + return dmemcg_limit_show(sf, v, get_resource_high); +} + +static ssize_t dmem_cgroup_region_high_write(struct kernfs_open_file *of, + char *buf, size_t nbytes, loff_t off) +{ + return dmemcg_limit_write(of, buf, nbytes, off, set_resource_high); +} + static int dmem_cgroup_region_max_show(struct seq_file *sf, void *v) { return dmemcg_limit_show(sf, v, get_resource_max); } static ssize_t dmem_cgroup_region_max_write(struct kernfs_open_file *of, - char *buf, size_t nbytes, loff_t off) + char *buf, size_t nbytes, loff_t off) { return dmemcg_limit_write(of, buf, nbytes, off, set_resource_max); } @@ -868,6 +895,12 @@ static struct cftype files[] = { .seq_show = dmem_cgroup_region_low_show, .flags = CFTYPE_NOT_ON_ROOT, }, + { + .name = "high", + .write = dmem_cgroup_region_high_write, + .seq_show = dmem_cgroup_region_high_show, + .flags = CFTYPE_NOT_ON_ROOT, + }, { .name = "max", .write = dmem_cgroup_region_max_write, @@ -877,6 +910,31 @@ static struct cftype files[] = { { } /* Zero entry terminates. */ }; +void __dmem_cgroup_handle_over_high(void) +{ + struct dmemcg_state *dmemcs; + struct dmem_cgroup_pool_state *pool; + + dmemcs = css_to_dmemcs(task_get_css(current, dmem_cgrp_id)); + if (!dmemcs) + return; + + rcu_read_lock(); + list_for_each_entry_rcu(pool, &dmemcs->pools, css_node) { + unsigned long usage, high; + + usage = page_counter_read(&pool->cnt); + high = READ_ONCE(pool->cnt.high); + + if (usage > high) + schedule_timeout_killable(HZ / 10); + } + rcu_read_unlock(); + + css_put(&dmemcs->css); +} +EXPORT_SYMBOL_GPL(__dmem_cgroup_handle_over_high); + struct cgroup_subsys dmem_cgrp_subsys = { .css_alloc = dmemcs_alloc, .css_free = dmemcs_free, --- base-commit: ab5fce87a778cb780a05984a2ca448f2b41aafbf change-id: 20260519-feature-dmem-high-16997148dc38 Best regards, -- Qiliang Yuan <[email protected]>
