From nobody Sun May 24 18:41:33 2026 Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 422543A758C for ; Fri, 22 May 2026 08:28:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779438501; cv=none; b=nJfM0Dex7Q5ipL9cLR7d1UBbfxanWCRNoRBSML2ucpMZs35DzE+gX/Da9/LEnM93UHW8/9iFYk9r2UeaZRo5TMk6/A30prxNanOY6IO+y1UoK0TxfhZDv0MNreZ7ab67A0+JJlfv80Htlo2bYpHrhvOlg8jep71oZYg64wyThBI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779438501; c=relaxed/simple; bh=22AnOA2E4gIttKLSIMM4IVgcoqszOvxEuV8pjsI+oNE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=cj5U5yTauvAIXEsDuauLe6yeOGuHO7cymPo7ba2WSnTbr537zcLzdaqzzszd2M7m3eySa1dtK4q4CjXFxtlmyGW1kUDJxF7x6Y9DkNeRVP19St2Wj1QAJVDHwAN7iW0YOM9AAOUj3DWNbjM5XGVMOU91jXeUNJbFSx+3B5Kjj0c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=TMAsfNir; arc=none smtp.client-ip=209.85.128.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TMAsfNir" Received: by mail-wm1-f49.google.com with SMTP id 5b1f17b1804b1-4891e86fabeso91143905e9.1 for ; Fri, 22 May 2026 01:28:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779438497; x=1780043297; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=jsaFe+7Xyz5+ITLDpBQw1qNHqE6BqqIIroG5/kytuLw=; b=TMAsfNirzlZZBZFf77VWDqOXjG1DnyKAexaara9LUqMjWzMkFVof4GlVSYVLyoN33S 7WKkN1QHIgMa/r+EZP85ysqh6FjshabjMdTg8gnIMvJfu4NDs0Oe1q60DSZcRsFy1z+S +8KAqsWeUYshKRTx75wn4Ccv7q9Y6PQex9/jBGEFGLjPsZ7CB1TKu6LlSugtagqLZV+x HGRTJ9ipgl1Luhb35wWsakZPQsHaNbdW67xTfSxLre9qFBRP5VR848abw/wwor11sbDK GksAflN6iCHofv/e/BysBDAMbHcq1lOu8T/aH4jvOdxIXoD6cZaAyvL8vYEwzhRBFcqU zPZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779438497; x=1780043297; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=jsaFe+7Xyz5+ITLDpBQw1qNHqE6BqqIIroG5/kytuLw=; b=W6LUamq/jDl2RFANBzj0mE42lAT720UYr8ZmJzD5H1AS58YIpyvOsq0reVNnc3spr+ /tItNBIIoEJAOzZPRdtW22OEhXc+bXqyNrpq6dbxHCstgrezp8ukTGR2eSJzG8pZZsOx QssfsV3WM7BxYBt4eexRMhb1lq1fUv1WW2fL7lBqCzpTvbjS04QTYZxf1P5hmdRBc44a 9uxHZB6evcX7Xxr/ksQAePxCkPMj1q8syLaU3XINOxeGJG8+ZtZFDUxvuuGqmfCtPgoU DRyAtFLA5sSOsOXlsLYp21N9D2aHCeTB3V2DIt8BJUorRSgJdS84LmUlaWSGPuSpNPDJ ohLQ== X-Forwarded-Encrypted: i=1; AFNElJ8KfNJBY9tV6bqxCmRhxnbpnqRzDrgvWM4qh0wcUJCoETrqJunSoVzdlSSLz+XLszCn3gRPgAX//T0rapg=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6iflUtzmwGO+rfu71GaRpw/HmT3XYvxm7rKo+muQvnnzkGhhr MWSOCsgrRN+rtTqeJfcaaCSK38WZbbcO3/aTl4HsGf/b8mDbXPRhGjme X-Gm-Gg: Acq92OGdALtnGKO99XfeSqoZ/Kcnm3K8rL6jNRPlYOMtGr5NV67oftVOUPUcz9X9voj DVWbi/1MhkksQmQ4WgZZz0TjZQol1iQfdB5RhU3FgSZAd/gJy01OYyW+b4lGo080uLQXciCFkRv 4ZkUO32+2V6Tdj0xWY0AZUp7NawuhwoIY4dEq50BiczqFG9Wsa/WlX9UizAbvm/sIkdwt9qOoiH kBmc5ATeuOvP20627WQQ74aawi9133cHEY2hgHIBqj6XKmVwwV0VocWUYr2Jfyg00zUl6bzbDKX R4d0IuTEP0VFnVeHveixiH5ONFaM9wa3Gj5uAi4Btr6w2v6E0zqpHibfv4lEGhWe4jigLHYl5/5 bKacQ3cS7aVbYj/SCOL/FkGgT6qgLIalVJbvajSTiVaQxSSuENAa+f7EOo85IOgVEtEhzVZZtz+ TZO7ULCjDdnqyLGD13mNQyqKmZJ+8yviaNMC9YB+at X-Received: by 2002:a05:600c:1c0d:b0:48f:d612:3c59 with SMTP id 5b1f17b1804b1-490424b3927mr35735625e9.9.1779438496277; Fri, 22 May 2026 01:28:16 -0700 (PDT) Received: from wujing.localdomain ([23.254.208.9]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49042d081f5sm14065795e9.5.2026.05.22.01.28.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 May 2026 01:28:15 -0700 (PDT) From: Qiliang Yuan Date: Fri, 22 May 2026 16:28:05 +0800 Subject: [PATCH v2] cgroup/dmem: implement dmem.high soft limit via prioritized eviction Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260522-feature-dmem-high-v2-1-1d7d4a0fa5da@gmail.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/x3MQQ5AMBBA0avIrE2iRamriIXoaGfRkhaRiLtrL N/i/wcSRaYEQ/FApIsTbyFDlgUsbg6WkE02yEqqqhUaV5qPMxIaTx4dW4dCad2JpjdL3UPu9kg r3/9znN73A5aNsOBjAAAA X-Change-ID: 20260519-feature-dmem-high-16997148dc38 To: Christian Koenig , Huang Rui , Matthew Auld , Matthew Brost , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , David Airlie , Simona Vetter , Tejun Heo , Johannes Weiner , =?utf-8?q?Michal_Koutn=C3=BD?= , Natalie Vock Cc: dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, Qiliang Yuan X-Mailer: b4 0.14.3 The dmem cgroup v2 controller currently only provides a hard "max" limit, which causes immediate allocation failures when a cgroup's device memory usage reaches its quota. GPU-bound AI workloads need smoother over-subscription support: a soft limit that temporarily allows excess usage while applying backpressure through reclaim rather than outright failure. Add dmem.high, a soft limit that penalizes over-limit cgroups by evicting their buffer objects first when eviction is triggered (e.g. due to a "max" limit hit). Unlike the rejected v1 approach which used sleep-on-allocation throttling, this version provides a meaningful recovery action through prioritized reclaim. Expose "high" as a new cgroupfs control file per region via set_resource_high() and get_resource_high(), and initialize it to PAGE_COUNTER_MAX in reset_all_resource_limits(). Extend dmem_cgroup_state_evict_valuable() with a "try_high" parameter. When set, only pools with usage above their high limit are considered evictable, implementing tier-1 of the prioritized eviction model. For the existing low-priority passes, the original effective-low/effective-min protection logic is unchanged. Refactor ttm_bo_evict_alloc() into a 3-pass eviction strategy. Pass 1 uses trylock and targets only BOs whose cgroup exceeds dmem.high. Pass 2 falls back to the standard above-elow eviction. Pass 3+ uses proper locking and repeats while making progress, with the existing low-watermark fallback. This adds one extra LRU walk when over-limit cgroups are present, but avoids any throttling or sleeping in the charge path, which would be catastrophic for GPU submission pipelines. Signed-off-by: Qiliang Yuan --- Introduce a "high" soft limit for the dmem cgroup v2 controller. When a "max" limit is hit and eviction is triggered, buffer objects belonging to cgroups that exceed their dmem.high limit are targeted first, providing a meaningful recovery action through reclaim. The dmem cgroup currently only supports hard "max" limits, which cause immediate allocation failures for GPU-bound workloads. A soft limit enables smoother over-subscription by penalizing over-limit cgroups via prioritized eviction rather than outright rejection. The implementation adds a "high" cgroupfs control file per region, a try_high parameter to dmem_cgroup_state_evict_valuable() for tier-1 eviction, and a 3-pass strategy in ttm_bo_evict_alloc(). --- V1 -> V2: - Replace sleep-on-allocation throttling with prioritized eviction. When a "max" limit is hit, BOs from cgroups exceeding dmem.high are evicted first in a dedicated pass. No throttling or sleeping is performed in the charge path. - Remove task throttling (schedule_timeout_killable, TIF_NOTIFY_RESUME, resume_user_mode_work() integration) entirely. - Add dmem.high cgroupfs control file per region. - Extend dmem_cgroup_state_evict_valuable() with try_high parameter to target over-limit cgroups as tier-1 eviction. - Refactor ttm_bo_evict_alloc() into a 3-pass eviction strategy: (1) trylock: evict only BOs exceeding dmem.high (2) trylock: above-elow (3) proper-lock: repeat with low fallback. - Initialize high to PAGE_COUNTER_MAX in reset_all_resource_limits(). v1: https://lore.kernel.org/all/20260520-feature-dmem-high-v1-1-97ca0cb7f95= a@gmail.com --- drivers/gpu/drm/ttm/ttm_bo.c | 28 +++++++++++++++++++++------ include/linux/cgroup_dmem.h | 4 ++-- kernel/cgroup/dmem.c | 45 ++++++++++++++++++++++++++++++++++++++++= ++-- 3 files changed, 67 insertions(+), 10 deletions(-) diff --git a/drivers/gpu/drm/ttm/ttm_bo.c b/drivers/gpu/drm/ttm/ttm_bo.c index bcd76f6bb7f02..eefcdb6155d63 100644 --- a/drivers/gpu/drm/ttm/ttm_bo.c +++ b/drivers/gpu/drm/ttm/ttm_bo.c @@ -505,6 +505,8 @@ struct ttm_bo_evict_walk { =20 /** @limit_pool: Which pool limit we should test against */ struct dmem_cgroup_pool_state *limit_pool; + /** @try_high: Whether to only evict BO's above the high watermark (first= pass) */ + bool try_high; /** @try_low: Whether we should attempt to evict BO's with low watermark = threshold */ bool try_low; /** @hit_low: If we cannot evict a bo when @try_low is false (first pass)= */ @@ -518,7 +520,8 @@ static s64 ttm_bo_evict_cb(struct ttm_lru_walk *walk, s= truct ttm_buffer_object * s64 lret; =20 if (!dmem_cgroup_state_evict_valuable(evict_walk->limit_pool, bo->resourc= e->css, - evict_walk->try_low, &evict_walk->hit_low)) + evict_walk->try_high, evict_walk->try_low, + &evict_walk->hit_low)) return 0; =20 if (bo->pin_count || !bo->bdev->funcs->eviction_valuable(bo, evict_walk->= place)) @@ -577,31 +580,44 @@ static int ttm_bo_evict_alloc(struct ttm_device *bdev, }; s64 lret; =20 + /* + * Pass 1 (trylock): Only evict BOs whose cgroup is above its + * dmem.high soft limit. This penalizes over-limit cgroups first. + */ evict_walk.walk.arg.trylock_only =3D true; + evict_walk.try_high =3D true; lret =3D ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1); + evict_walk.try_high =3D false; + if (lret) + goto out; =20 - /* One more attempt if we hit low limit? */ + /* + * Pass 2 (trylock): Evict BOs above the effective low watermark. + * Falls back to low-priority eviction if needed. + */ + lret =3D ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1); if (!lret && evict_walk.hit_low) { evict_walk.try_low =3D true; lret =3D ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1); } + if (lret || !ticket) goto out; =20 - /* Reset low limit */ + /* + * Pass 3+ (properly locked): Evict while making progress. + * Reset flags and retry with try_low if we hit the low watermark. + */ evict_walk.try_low =3D evict_walk.hit_low =3D false; - /* If ticket-locking, repeat while making progress. */ evict_walk.walk.arg.trylock_only =3D false; =20 retry: do { - /* The walk may clear the evict_walk.walk.ticket field */ evict_walk.walk.arg.ticket =3D ticket; evict_walk.evicted =3D 0; lret =3D ttm_lru_walk_for_evict(&evict_walk.walk, bdev, man, 1); } while (!lret && evict_walk.evicted); =20 - /* We hit the low limit? Try once more */ if (!lret && evict_walk.hit_low && !evict_walk.try_low) { evict_walk.try_low =3D true; goto retry; diff --git a/include/linux/cgroup_dmem.h b/include/linux/cgroup_dmem.h index dd4869f1d736e..06115d35509b1 100644 --- a/include/linux/cgroup_dmem.h +++ b/include/linux/cgroup_dmem.h @@ -23,7 +23,7 @@ int dmem_cgroup_try_charge(struct dmem_cgroup_region *reg= ion, u64 size, void dmem_cgroup_uncharge(struct dmem_cgroup_pool_state *pool, u64 size); 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); + bool try_high, bool ignore_low, bool *ret_hit_low); =20 void dmem_cgroup_pool_state_put(struct dmem_cgroup_pool_state *pool); #else @@ -54,7 +54,7 @@ static inline void dmem_cgroup_uncharge(struct dmem_cgrou= p_pool_state *pool, u64 static inline 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) + bool try_high, bool ignore_low, bool *ret_hit_low) { return true; } diff --git a/kernel/cgroup/dmem.c b/kernel/cgroup/dmem.c index 4753a67d0f0f2..3799ecd6d7b52 100644 --- a/kernel/cgroup/dmem.c +++ b/kernel/cgroup/dmem.c @@ -156,6 +156,12 @@ set_resource_low(struct dmem_cgroup_pool_state *pool, = u64 val) page_counter_set_low(&pool->cnt, val); } =20 +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 +173,11 @@ static u64 get_resource_low(struct dmem_cgroup_pool_st= ate *pool) return pool ? READ_ONCE(pool->cnt.low) : 0; } =20 +static u64 get_resource_high(struct dmem_cgroup_pool_state *pool) +{ + return pool ? READ_ONCE(pool->cnt.high) : PAGE_COUNTER_MAX; +} + static u64 get_resource_min(struct dmem_cgroup_pool_state *pool) { return pool ? READ_ONCE(pool->cnt.min) : 0; @@ -186,6 +197,7 @@ static void reset_all_resource_limits(struct dmem_cgrou= p_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); } =20 @@ -289,10 +301,13 @@ dmem_cgroup_calculate_protection(struct dmem_cgroup_p= ool_state *limit_pool, * dmem_cgroup_state_evict_valuable() - Check if we should evict from test= _pool * @limit_pool: The pool for which we hit limits * @test_pool: The pool for which to test + * @try_high: Only evict BOs whose usage exceeds the high limit (first pas= s) * @ignore_low: Whether we have to respect low watermarks. * @ret_hit_low: Pointer to whether it makes sense to consider low waterma= rk. * * This function returns true if we can evict from @test_pool, false if no= t. + * When @try_high is set, only pools with usage above their high limit are + * evictable, enabling prioritized eviction of over-limit cgroups. * When returning false and @ignore_low is false, @ret_hit_low may * be set to true to indicate this function can be retried with @ignore_low * set to true. @@ -301,7 +316,7 @@ dmem_cgroup_calculate_protection(struct dmem_cgroup_poo= l_state *limit_pool, */ 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) + bool try_high, bool ignore_low, bool *ret_hit_low) { struct dmem_cgroup_pool_state *pool =3D test_pool; struct page_counter *ctest; @@ -331,9 +346,18 @@ bool dmem_cgroup_state_evict_valuable(struct dmem_cgro= up_pool_state *limit_pool, =20 ctest =3D &test_pool->cnt; =20 + used =3D page_counter_read(ctest); + + /* + * High-priority pass: only evict BOs whose cgroup is above its + * dmem.high soft limit. This implements tier-1 of the 3-pass + * eviction model, ensuring over-limit cgroups are penalized first. + */ + if (try_high) + return used > READ_ONCE(ctest->high); + dmem_cgroup_calculate_protection(limit_pool, test_pool); =20 - used =3D page_counter_read(ctest); min =3D READ_ONCE(ctest->emin); =20 if (used <=3D min) @@ -835,6 +859,17 @@ static ssize_t dmem_cgroup_region_low_write(struct ker= nfs_open_file *of, return dmemcg_limit_write(of, buf, nbytes, off, set_resource_low); } =20 +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); @@ -868,6 +903,12 @@ static struct cftype files[] =3D { .seq_show =3D dmem_cgroup_region_low_show, .flags =3D CFTYPE_NOT_ON_ROOT, }, + { + .name =3D "high", + .write =3D dmem_cgroup_region_high_write, + .seq_show =3D dmem_cgroup_region_high_show, + .flags =3D CFTYPE_NOT_ON_ROOT, + }, { .name =3D "max", .write =3D dmem_cgroup_region_max_write, --- base-commit: ab5fce87a778cb780a05984a2ca448f2b41aafbf change-id: 20260519-feature-dmem-high-16997148dc38 Best regards, --=20 Qiliang Yuan