public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max
@ 2026-03-19 21:22 Thadeu Lima de Souza Cascardo
  2026-03-19 21:22 ` [PATCH v2 1/3] cgroup/dmem: remove region parameter from dmemcg_parse_limit Thadeu Lima de Souza Cascardo
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2026-03-19 21:22 UTC (permalink / raw)
  To: Maarten Lankhorst, Maxime Ripard, Natalie Vock, Tejun Heo,
	Johannes Weiner, Michal Koutný
  Cc: cgroups, dri-devel, linux-kernel, Thadeu Lima de Souza Cascardo,
	kernel-dev

When writing to dmem.max, it was noticed that some writes did not take
effect, even though the write was successful.

It turns out that page_counter_set_max checks if the new max value is above
the current usage and returns -EBUSY in case it is, which was being ignored
by dmemcg_limit_write.

It was also noticed that when setting limits for multiple regions in a
single write, while setting one region's limit may fail, others might have
succeeded before. Tejun Heo brought up that this breaks atomicity.

Maarten Lankhorst and Michal Koutný have brought up that instead of
failing, setting max below the current usage should behave like memcg and
start eviction until usage goes below the new max.

However, since there is no current mechanism to evict a given region, they
suggest that setting the new max will at least prevent further allocations.

v1 kept the multiple region support when writing to limit files, while
returning -EBUSY as soon as setting a region's max was above the usage.

This version (v2) only allows setting a single region limit per write,
while allowing any new max to be set.

Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
---
Changes in v2:
- Remove support for setting multiple regions' limits.
- Allow any new max limit to be set.
- Link to v1: https://lore.kernel.org/r/20260318-dmem_max_ebusy-v1-1-b7e461157b29@igalia.com

---
Thadeu Lima de Souza Cascardo (3):
      cgroup/dmem: remove region parameter from dmemcg_parse_limit
      cgroup/dmem: accept a single region when writing to attributes
      cgroup/dmem: allow max to be set below current usage

 kernel/cgroup/dmem.c | 74 +++++++++++++++++++++++-----------------------------
 1 file changed, 32 insertions(+), 42 deletions(-)
---
base-commit: f338e77383789c0cae23ca3d48adcc5e9e137e3c
change-id: 20260318-dmem_max_ebusy-bfd33564f2c3

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


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

* [PATCH v2 1/3] cgroup/dmem: remove region parameter from dmemcg_parse_limit
  2026-03-19 21:22 [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max Thadeu Lima de Souza Cascardo
@ 2026-03-19 21:22 ` Thadeu Lima de Souza Cascardo
  2026-03-19 21:22 ` [PATCH v2 2/3] cgroup/dmem: accept a single region when writing to attributes Thadeu Lima de Souza Cascardo
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2026-03-19 21:22 UTC (permalink / raw)
  To: Maarten Lankhorst, Maxime Ripard, Natalie Vock, Tejun Heo,
	Johannes Weiner, Michal Koutný
  Cc: cgroups, dri-devel, linux-kernel, Thadeu Lima de Souza Cascardo,
	kernel-dev

dmemcg_parse_limit does not use the region parameter. Remove it.

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

diff --git a/kernel/cgroup/dmem.c b/kernel/cgroup/dmem.c
index 9d95824dc6fa09422274422313b63c25986596de..1ab1fb47f2711ecc60dd13e611a8a4920b48f3e9 100644
--- a/kernel/cgroup/dmem.c
+++ b/kernel/cgroup/dmem.c
@@ -707,8 +707,7 @@ static int dmem_cgroup_region_capacity_show(struct seq_file *sf, void *v)
 	return 0;
 }
 
-static int dmemcg_parse_limit(char *options, struct dmem_cgroup_region *region,
-			      u64 *new_limit)
+static int dmemcg_parse_limit(char *options, u64 *new_limit)
 {
 	char *end;
 
@@ -762,7 +761,7 @@ static ssize_t dmemcg_limit_write(struct kernfs_open_file *of,
 		if (!region)
 			return -EINVAL;
 
-		err = dmemcg_parse_limit(options, region, &new_limit);
+		err = dmemcg_parse_limit(options, &new_limit);
 		if (err < 0)
 			goto out_put;
 

-- 
2.47.3


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

* [PATCH v2 2/3] cgroup/dmem: accept a single region when writing to attributes
  2026-03-19 21:22 [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max Thadeu Lima de Souza Cascardo
  2026-03-19 21:22 ` [PATCH v2 1/3] cgroup/dmem: remove region parameter from dmemcg_parse_limit Thadeu Lima de Souza Cascardo
@ 2026-03-19 21:22 ` Thadeu Lima de Souza Cascardo
  2026-03-19 21:22 ` [PATCH v2 3/3] cgroup/dmem: allow max to be set below current usage Thadeu Lima de Souza Cascardo
  2026-03-21 19:27 ` [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max Tejun Heo
  3 siblings, 0 replies; 9+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2026-03-19 21:22 UTC (permalink / raw)
  To: Maarten Lankhorst, Maxime Ripard, Natalie Vock, Tejun Heo,
	Johannes Weiner, Michal Koutný
  Cc: cgroups, dri-devel, linux-kernel, Thadeu Lima de Souza Cascardo,
	kernel-dev

When writing to dmem.{min,low,max}, if multiple lines are given, one of
them might succeed while the next one fails, but an error is returned. That
is, there is no atomicity where either all changes succeed or all of them
fail.

Only accept a single region instead of trying to parse multiple lines and
process multiple regions at the same write.

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

diff --git a/kernel/cgroup/dmem.c b/kernel/cgroup/dmem.c
index 1ab1fb47f2711ecc60dd13e611a8a4920b48f3e9..695d2b7516081256da030c80b54ec1c5fcd6ca16 100644
--- a/kernel/cgroup/dmem.c
+++ b/kernel/cgroup/dmem.c
@@ -729,56 +729,47 @@ static ssize_t dmemcg_limit_write(struct kernfs_open_file *of,
 {
 	struct dmemcg_state *dmemcs = css_to_dmemcs(of_css(of));
 	int err = 0;
+	struct dmem_cgroup_pool_state *pool = NULL;
+	char *region_name;
+	struct dmem_cgroup_region *region;
+	u64 new_limit;
 
-	while (buf && !err) {
-		struct dmem_cgroup_pool_state *pool = NULL;
-		char *options, *region_name;
-		struct dmem_cgroup_region *region;
-		u64 new_limit;
-
-		options = buf;
-		buf = strchr(buf, '\n');
-		if (buf)
-			*buf++ = '\0';
-
-		options = strstrip(options);
+	buf = strstrip(buf);
+	if (!buf[0])
+		return -EINVAL;
 
-		/* eat empty lines */
-		if (!options[0])
-			continue;
+	region_name = strsep(&buf, " \t");
+	if (!region_name[0])
+		return -EINVAL;
 
-		region_name = strsep(&options, " \t");
-		if (!region_name[0])
-			continue;
+	if (!buf || !*buf)
+		return -EINVAL;
 
-		if (!options || !*options)
-			return -EINVAL;
+	buf = skip_spaces(buf);
 
-		rcu_read_lock();
-		region = dmemcg_get_region_by_name(region_name);
-		rcu_read_unlock();
+	err = dmemcg_parse_limit(buf, &new_limit);
+	if (err < 0)
+		return -EINVAL;
 
-		if (!region)
-			return -EINVAL;
+	rcu_read_lock();
+	region = dmemcg_get_region_by_name(region_name);
+	rcu_read_unlock();
 
-		err = dmemcg_parse_limit(options, &new_limit);
-		if (err < 0)
-			goto out_put;
+	if (!region)
+		return -EINVAL;
 
-		pool = get_cg_pool_unlocked(dmemcs, region);
-		if (IS_ERR(pool)) {
-			err = PTR_ERR(pool);
-			goto out_put;
-		}
+	pool = get_cg_pool_unlocked(dmemcs, region);
+	if (IS_ERR(pool)) {
+		err = PTR_ERR(pool);
+		goto out_put;
+	}
 
-		/* And commit */
-		apply(pool, new_limit);
-		dmemcg_pool_put(pool);
+	/* And commit */
+	apply(pool, new_limit);
+	dmemcg_pool_put(pool);
 
 out_put:
-		kref_put(&region->ref, dmemcg_free_region);
-	}
-
+	kref_put(&region->ref, dmemcg_free_region);
 
 	return err ?: nbytes;
 }

-- 
2.47.3


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

* [PATCH v2 3/3] cgroup/dmem: allow max to be set below current usage
  2026-03-19 21:22 [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max Thadeu Lima de Souza Cascardo
  2026-03-19 21:22 ` [PATCH v2 1/3] cgroup/dmem: remove region parameter from dmemcg_parse_limit Thadeu Lima de Souza Cascardo
  2026-03-19 21:22 ` [PATCH v2 2/3] cgroup/dmem: accept a single region when writing to attributes Thadeu Lima de Souza Cascardo
@ 2026-03-19 21:22 ` Thadeu Lima de Souza Cascardo
  2026-03-21 19:27 ` [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max Tejun Heo
  3 siblings, 0 replies; 9+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2026-03-19 21:22 UTC (permalink / raw)
  To: Maarten Lankhorst, Maxime Ripard, Natalie Vock, Tejun Heo,
	Johannes Weiner, Michal Koutný
  Cc: cgroups, dri-devel, linux-kernel, Thadeu Lima de Souza Cascardo,
	kernel-dev

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.

Instead of using page_counter_set_max when writing to dmem.max, atomically
update its value irrespective of the current usage.

Since there is no current mechanism to evict a given dmemcg pool, this will
at least prevent the current usage from growing any further.

Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
---
 kernel/cgroup/dmem.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/kernel/cgroup/dmem.c b/kernel/cgroup/dmem.c
index 695d2b7516081256da030c80b54ec1c5fcd6ca16..bf9e0d11e46156a437196c77fdfde84250e65420 100644
--- a/kernel/cgroup/dmem.c
+++ b/kernel/cgroup/dmem.c
@@ -159,7 +159,7 @@ set_resource_low(struct dmem_cgroup_pool_state *pool, u64 val)
 static void
 set_resource_max(struct dmem_cgroup_pool_state *pool, u64 val)
 {
-	page_counter_set_max(&pool->cnt, val);
+	xchg(&pool->cnt.max, val);
 }
 
 static u64 get_resource_low(struct dmem_cgroup_pool_state *pool)

-- 
2.47.3


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

* Re: [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max
  2026-03-19 21:22 [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max Thadeu Lima de Souza Cascardo
                   ` (2 preceding siblings ...)
  2026-03-19 21:22 ` [PATCH v2 3/3] cgroup/dmem: allow max to be set below current usage Thadeu Lima de Souza Cascardo
@ 2026-03-21 19:27 ` Tejun Heo
  2026-03-23 16:46   ` Maarten Lankhorst
  3 siblings, 1 reply; 9+ messages in thread
From: Tejun Heo @ 2026-03-21 19:27 UTC (permalink / raw)
  To: Thadeu Lima de Souza Cascardo, Maarten Lankhorst, Maxime Ripard,
	Natalie Vock
  Cc: Johannes Weiner, Michal Koutny, cgroups, dri-devel, linux-kernel,
	kernel-dev

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 396 bytes --]

Hello,

Generally looks okay to me. One comment on 3/3 — the naked xchg() in
set_resource_max() needs a comment explaining why it's used instead of
page_counter_set_max() and what the semantics are (unconditionally sets max
regardless of current usage to prevent further allocations, since there's no
eviction mechanism yet).

Applied 1/3. Maarten, Michal, what do you think?

Thanks.
--
tejun

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

* Re: [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max
  2026-03-21 19:27 ` [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max Tejun Heo
@ 2026-03-23 16:46   ` Maarten Lankhorst
  2026-03-23 17:37     ` Thadeu Lima de Souza Cascardo
  0 siblings, 1 reply; 9+ messages in thread
From: Maarten Lankhorst @ 2026-03-23 16:46 UTC (permalink / raw)
  To: Tejun Heo, Thadeu Lima de Souza Cascardo, Maxime Ripard,
	Natalie Vock
  Cc: Johannes Weiner, Michal Koutny, cgroups, dri-devel, linux-kernel,
	kernel-dev

Hey,


Den 2026-03-21 kl. 20:27, skrev Tejun Heo:
> Hello,
> 
> Generally looks okay to me. One comment on 3/3 — the naked xchg() in
> set_resource_max() needs a comment explaining why it's used instead of
> page_counter_set_max() and what the semantics are (unconditionally sets max
> regardless of current usage to prevent further allocations, since there's no
> eviction mechanism yet).
> 
> Applied 1/3. Maarten, Michal, what do you think?

Yeah probably drop 2/3 too since there is no longer a case where setting a limit may fail.

Kind regards,
~Maarten Lankhorst

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

* Re: [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max
  2026-03-23 16:46   ` Maarten Lankhorst
@ 2026-03-23 17:37     ` Thadeu Lima de Souza Cascardo
  2026-03-25 17:40       ` Maarten Lankhorst
  0 siblings, 1 reply; 9+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2026-03-23 17:37 UTC (permalink / raw)
  To: Maarten Lankhorst
  Cc: Tejun Heo, Maxime Ripard, Natalie Vock, Johannes Weiner,
	Michal Koutny, cgroups, dri-devel, linux-kernel, kernel-dev

On Mon, Mar 23, 2026 at 05:46:28PM +0100, Maarten Lankhorst wrote:
> Hey,
> 
> 
> Den 2026-03-21 kl. 20:27, skrev Tejun Heo:
> > Hello,
> > 
> > Generally looks okay to me. One comment on 3/3 — the naked xchg() in
> > set_resource_max() needs a comment explaining why it's used instead of
> > page_counter_set_max() and what the semantics are (unconditionally sets max
> > regardless of current usage to prevent further allocations, since there's no
> > eviction mechanism yet).
> > 
> > Applied 1/3. Maarten, Michal, what do you think?
> 
> Yeah probably drop 2/3 too since there is no longer a case where setting a limit may fail.
> 
> Kind regards,
> ~Maarten Lankhorst

Actually, this can still happen if an invalid region name is given.

So, one could write:

echo -e 'region1 max\ninvalidregion2 max\n' > dmem.max

And even though setting the value for region1 would be applied, the write
would return -EINVAL.

Cascardo.

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

* Re: [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max
  2026-03-23 17:37     ` Thadeu Lima de Souza Cascardo
@ 2026-03-25 17:40       ` Maarten Lankhorst
  2026-03-26 15:24         ` Thadeu Lima de Souza Cascardo
  0 siblings, 1 reply; 9+ messages in thread
From: Maarten Lankhorst @ 2026-03-25 17:40 UTC (permalink / raw)
  To: Thadeu Lima de Souza Cascardo
  Cc: Tejun Heo, Maxime Ripard, Natalie Vock, Johannes Weiner,
	Michal Koutny, cgroups, dri-devel, linux-kernel, kernel-dev

Hey,

Den 2026-03-23 kl. 18:37, skrev Thadeu Lima de Souza Cascardo:
> On Mon, Mar 23, 2026 at 05:46:28PM +0100, Maarten Lankhorst wrote:
>> Hey,
>>
>>
>> Den 2026-03-21 kl. 20:27, skrev Tejun Heo:
>>> Hello,
>>>
>>> Generally looks okay to me. One comment on 3/3 — the naked xchg() in
>>> set_resource_max() needs a comment explaining why it's used instead of
>>> page_counter_set_max() and what the semantics are (unconditionally sets max
>>> regardless of current usage to prevent further allocations, since there's no
>>> eviction mechanism yet).
>>>
>>> Applied 1/3. Maarten, Michal, what do you think?
>>
>> Yeah probably drop 2/3 too since there is no longer a case where setting a limit may fail.
>>
>> Kind regards,
>> ~Maarten Lankhorst
> 
> Actually, this can still happen if an invalid region name is given.
> 
> So, one could write:
> 
> echo -e 'region1 max\ninvalidregion2 max\n' > dmem.max
> 
> And even though setting the value for region1 would be applied, the write
> would return -EINVAL.

Makes sense. It would be good to validate in advance then. If that's not possible we
should at least not error when we try to update 2 regions simultaneously. Likely the
best to do so anyway if we want to handle eviction, which may be handled in a blocking
fashion.

Kind regards,
~Maarten Lankhorst

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

* Re: [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max
  2026-03-25 17:40       ` Maarten Lankhorst
@ 2026-03-26 15:24         ` Thadeu Lima de Souza Cascardo
  0 siblings, 0 replies; 9+ messages in thread
From: Thadeu Lima de Souza Cascardo @ 2026-03-26 15:24 UTC (permalink / raw)
  To: Maarten Lankhorst
  Cc: Tejun Heo, Maxime Ripard, Natalie Vock, Johannes Weiner,
	Michal Koutny, cgroups, dri-devel, linux-kernel, kernel-dev

On Wed, Mar 25, 2026 at 06:40:40PM +0100, Maarten Lankhorst wrote:
> Hey,
> 
> Den 2026-03-23 kl. 18:37, skrev Thadeu Lima de Souza Cascardo:
> > On Mon, Mar 23, 2026 at 05:46:28PM +0100, Maarten Lankhorst wrote:
> >> Hey,
> >>
> >>
> >> Den 2026-03-21 kl. 20:27, skrev Tejun Heo:
> >>> Hello,
> >>>
> >>> Generally looks okay to me. One comment on 3/3 — the naked xchg() in
> >>> set_resource_max() needs a comment explaining why it's used instead of
> >>> page_counter_set_max() and what the semantics are (unconditionally sets max
> >>> regardless of current usage to prevent further allocations, since there's no
> >>> eviction mechanism yet).
> >>>
> >>> Applied 1/3. Maarten, Michal, what do you think?
> >>
> >> Yeah probably drop 2/3 too since there is no longer a case where setting a limit may fail.
> >>
> >> Kind regards,
> >> ~Maarten Lankhorst
> > 
> > Actually, this can still happen if an invalid region name is given.
> > 
> > So, one could write:
> > 
> > echo -e 'region1 max\ninvalidregion2 max\n' > dmem.max
> > 
> > And even though setting the value for region1 would be applied, the write
> > would return -EINVAL.
> 
> Makes sense. It would be good to validate in advance then. If that's not possible we
> should at least not error when we try to update 2 regions simultaneously. Likely the
> best to do so anyway if we want to handle eviction, which may be handled in a blocking
> fashion.
> 
> Kind regards,
> ~Maarten Lankhorst

I have submitted only the last patch with the additional comment for now.

If it turns out that eviction is handled, you bring up a good point that
doing it in a blocking fashion may lead to an underisable effect where
setting the max and starting eviction for one region may be delayed by the
eviction of a previous region.

Is there any reason to keep the support for handling multiple regions in a
single write?

Thanks.
Cascardo.

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

end of thread, other threads:[~2026-03-26 15:24 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-19 21:22 [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max Thadeu Lima de Souza Cascardo
2026-03-19 21:22 ` [PATCH v2 1/3] cgroup/dmem: remove region parameter from dmemcg_parse_limit Thadeu Lima de Souza Cascardo
2026-03-19 21:22 ` [PATCH v2 2/3] cgroup/dmem: accept a single region when writing to attributes Thadeu Lima de Souza Cascardo
2026-03-19 21:22 ` [PATCH v2 3/3] cgroup/dmem: allow max to be set below current usage Thadeu Lima de Souza Cascardo
2026-03-21 19:27 ` [PATCH v2 0/3] cgroup/dmem: allow atomic irrestrictive writes to dmem.max Tejun Heo
2026-03-23 16:46   ` Maarten Lankhorst
2026-03-23 17:37     ` Thadeu Lima de Souza Cascardo
2026-03-25 17:40       ` Maarten Lankhorst
2026-03-26 15:24         ` Thadeu Lima de Souza Cascardo

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