public inbox for cgroups@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cgroup/dmem: return error when failing to set dmem.max
@ 2026-03-18 19:34 Thadeu Lima de Souza Cascardo
  2026-03-18 22:56 ` Tejun Heo
  2026-03-19  7:33 ` Maarten Lankhorst
  0 siblings, 2 replies; 4+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2026-03-18 19:34 UTC (permalink / raw)
  To: Maarten Lankhorst, Maxime Ripard, Natalie Vock, Tejun Heo,
	Johannes Weiner, Michal Koutný
  Cc: cgroups, dri-devel, linux-kernel, kernel-dev,
	Thadeu Lima de Souza Cascardo

page_counter_set_max may return -EBUSY in case the current usage is above
the new max. When writing to dmem.max, this error is ignored and the new
max is not set.

Return as soon as setting one of the regions max limit fails. This keeps
with the current behavior of returning when one of the region names is not
valid.

After this fix, setting a max value below the current value returns -EBUSY.

 # cat dmem.current
 drm/0000:04:00.0/vram 1060864
 # echo drm/0000:04:00.0/vram 0 > dmem.max
 -bash: echo: write error: Device or resource busy
 #

Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
---
 kernel/cgroup/dmem.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/kernel/cgroup/dmem.c b/kernel/cgroup/dmem.c
index 9d95824dc6fa09422274422313b63c25986596de..3e6d4c0b26a1f972b6c6e875f274a091fc9e2b75 100644
--- a/kernel/cgroup/dmem.c
+++ b/kernel/cgroup/dmem.c
@@ -144,22 +144,24 @@ static void free_cg_pool(struct dmem_cgroup_pool_state *pool)
 	dmemcg_pool_put(pool);
 }
 
-static void
+static int
 set_resource_min(struct dmem_cgroup_pool_state *pool, u64 val)
 {
 	page_counter_set_min(&pool->cnt, val);
+	return 0;
 }
 
-static void
+static int
 set_resource_low(struct dmem_cgroup_pool_state *pool, u64 val)
 {
 	page_counter_set_low(&pool->cnt, val);
+	return 0;
 }
 
-static void
+static int
 set_resource_max(struct dmem_cgroup_pool_state *pool, u64 val)
 {
-	page_counter_set_max(&pool->cnt, val);
+	return page_counter_set_max(&pool->cnt, val);
 }
 
 static u64 get_resource_low(struct dmem_cgroup_pool_state *pool)
@@ -726,7 +728,7 @@ static int dmemcg_parse_limit(char *options, struct dmem_cgroup_region *region,
 
 static ssize_t dmemcg_limit_write(struct kernfs_open_file *of,
 				 char *buf, size_t nbytes, loff_t off,
-				 void (*apply)(struct dmem_cgroup_pool_state *, u64))
+				 int (*apply)(struct dmem_cgroup_pool_state *, u64))
 {
 	struct dmemcg_state *dmemcs = css_to_dmemcs(of_css(of));
 	int err = 0;
@@ -773,7 +775,7 @@ static ssize_t dmemcg_limit_write(struct kernfs_open_file *of,
 		}
 
 		/* And commit */
-		apply(pool, new_limit);
+		err = apply(pool, new_limit);
 		dmemcg_pool_put(pool);
 
 out_put:

---
base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c
change-id: 20260318-dmem_max_ebusy-bfd33564f2c3

Best regards,
-- 
Thadeu Lima de Souza Cascardo <cascardo@igalia.com>


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] cgroup/dmem: return error when failing to set dmem.max
  2026-03-18 19:34 [PATCH] cgroup/dmem: return error when failing to set dmem.max Thadeu Lima de Souza Cascardo
@ 2026-03-18 22:56 ` Tejun Heo
  2026-03-19  7:33 ` Maarten Lankhorst
  1 sibling, 0 replies; 4+ messages in thread
From: Tejun Heo @ 2026-03-18 22:56 UTC (permalink / raw)
  To: Thadeu Lima de Souza Cascardo
  Cc: Maarten Lankhorst, Maxime Ripard, Natalie Vock, Johannes Weiner,
	Michal Koutný, cgroups, dri-devel, linux-kernel, kernel-dev

On Wed, Mar 18, 2026 at 04:34:17PM -0300, Thadeu Lima de Souza Cascardo wrote:
> page_counter_set_max may return -EBUSY in case the current usage is above
> the new max. When writing to dmem.max, this error is ignored and the new
> max is not set.
> 
> Return as soon as setting one of the regions max limit fails. This keeps
> with the current behavior of returning when one of the region names is not
> valid.

Ugh, I don't know why dmemcg_limit_write() is trying to handle multi-line
inputs. After this, there's no atomicity w.r.t. failures either, so this
seems entirely pointless. I'd much prefer to strip out the multiline
handling.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cgroup/dmem: return error when failing to set dmem.max
  2026-03-18 19:34 [PATCH] cgroup/dmem: return error when failing to set dmem.max Thadeu Lima de Souza Cascardo
  2026-03-18 22:56 ` Tejun Heo
@ 2026-03-19  7:33 ` Maarten Lankhorst
  2026-03-19  9:40   ` Michal Koutný
  1 sibling, 1 reply; 4+ messages in thread
From: Maarten Lankhorst @ 2026-03-19  7:33 UTC (permalink / raw)
  To: Thadeu Lima de Souza Cascardo, Maxime Ripard, Natalie Vock,
	Tejun Heo, Johannes Weiner, Michal Koutný
  Cc: cgroups, dri-devel, linux-kernel, kernel-dev

Hey,

Den 2026-03-18 kl. 20:34, skrev Thadeu Lima de Souza Cascardo:
> page_counter_set_max may return -EBUSY in case the current usage is above
> the new max. When writing to dmem.max, this error is ignored and the new
> max is not set.
> 
> Return as soon as setting one of the regions max limit fails. This keeps
> with the current behavior of returning when one of the region names is not
> valid.
> 
> After this fix, setting a max value below the current value returns -EBUSY.
> 
>  # cat dmem.current
>  drm/0000:04:00.0/vram 1060864
>  # echo drm/0000:04:00.0/vram 0 > dmem.max
>  -bash: echo: write error: Device or resource busy
>  #
> 
> Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
The semantics of dmemcg should not substantially differ from the memory cgroup
controller. I believe the memory cgroup controller does allow setting a lower
max, and will evict until below the new max.

See mm/memcontrol.c:memory_max_write

We should probably do the same in dmemcg instead, although we currently have no
mechanism to evict, setting a new lower max at least prevents future allocations
from failing.

I believe we should have a similar loop in dmemcg, and allow ttm to evict until
the new max is reached.

Kind regards,
~Maarten Lankhorst

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cgroup/dmem: return error when failing to set dmem.max
  2026-03-19  7:33 ` Maarten Lankhorst
@ 2026-03-19  9:40   ` Michal Koutný
  0 siblings, 0 replies; 4+ messages in thread
From: Michal Koutný @ 2026-03-19  9:40 UTC (permalink / raw)
  To: Maarten Lankhorst
  Cc: Thadeu Lima de Souza Cascardo, Maxime Ripard, Natalie Vock,
	Tejun Heo, Johannes Weiner, cgroups, dri-devel, linux-kernel,
	kernel-dev

[-- Attachment #1: Type: text/plain, Size: 865 bytes --]

On Thu, Mar 19, 2026 at 08:33:12AM +0100, Maarten Lankhorst <dev@lankhorst.se> wrote:
> The semantics of dmemcg should not substantially differ from the memory cgroup
> controller. I believe the memory cgroup controller does allow setting a lower
> max, and will evict until below the new max.
> 
> See mm/memcontrol.c:memory_max_write
> 
> We should probably do the same in dmemcg instead, although we currently have no
> mechanism to evict, setting a new lower max at least prevents future allocations
> from failing.

+1

Yes, if the dmem resource is preemptible, the limit decrement should take an
action to fullfill the limit (like with memory.max).
Even as non-preemptible resource, the behavior could be more consistent
with misc controller that allows "storing" any value (with the effect of
preventing further growth).

Thanks,
Michal

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 265 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-03-19  9:40 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-18 19:34 [PATCH] cgroup/dmem: return error when failing to set dmem.max Thadeu Lima de Souza Cascardo
2026-03-18 22:56 ` Tejun Heo
2026-03-19  7:33 ` Maarten Lankhorst
2026-03-19  9:40   ` Michal Koutný

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox