All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Thomas Hellström" <thomas.hellstrom@linux.intel.com>
Cc: tj@kernel.org, hannes@cmpxchg.org, mkoutny@suse.com,
	dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v8 3/6] cgroup/dmem: Add reclaim callback for lowering max below current usage
Date: Thu, 23 Jul 2026 10:21:21 +0000	[thread overview]
Message-ID: <20260723102122.AB3B21F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260723100350.16895-4-thomas.hellstrom@linux.intel.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Unbounded SRCU read lock hold time in set_resource_max() can stall unregistration indefinitely

Pre-existing issues:
- [High] 32-bit integer overflow and truncation in dmem cgroup limits and accounting
--

--- Patch [3]: [PATCH v8 3/6] cgroup/dmem: Add reclaim callback for lowering max below current usage ---
commit 1477e3308e9932d9b9ab70b4c6666ef7191157d5
Author: Thomas Hellström <thomas.hellstrom@linux.intel.com>

cgroup/dmem: Add reclaim callback for lowering max below current usage

This commit adds an optional reclaim callback to dmem_cgroup_region. When
dmem.max is lowered below current usage, the driver is asked to evict memory,
and concurrent allocations are throttled. It introduces an SRCU domain to
serialize these reclaim callbacks against region unregistration.

> diff --git a/kernel/cgroup/dmem.c b/kernel/cgroup/dmem.c
> index eb46e99b73666..7e427895f23ea 100644
> --- a/kernel/cgroup/dmem.c
> +++ b/kernel/cgroup/dmem.c

[ ... ]

> @@ -145,21 +161,52 @@ static void free_cg_pool(struct dmem_cgroup_pool_state *pool)
>  }
>  
>  static void
> -set_resource_max(struct dmem_cgroup_pool_state *pool, u64 val)
> +set_resource_max(struct dmem_cgroup_pool_state *pool, u64 val, bool nonblock)
>  {
> -	page_counter_set_max(&pool->cnt, val);
> +	struct dmem_cgroup_region *region = pool->region;
> +	unsigned long limit = (unsigned long)val;

[Severity: High]
This isn't a bug introduced by this patch, but does this truncation break
cgroup limits on 32-bit systems?

The dmem subsystem appears to be tracking bytes rather than pages using the
page_counter API. On 32-bit kernels, an unsigned long is 32 bits, which
means any limit or usage exceeding 4GB will be silently truncated when a
u64 is cast to unsigned long in set_resource_max().

This same issue seems to apply in dmem_cgroup_try_charge() where the u64
size argument is passed to page_counter_try_charge():

kernel/cgroup/dmem.c:dmem_cgroup_try_charge() {
    ...
    if (!page_counter_try_charge(&pool->cnt, size, &fail)) {
    ...
}

If the allocation exceeds 4GB, or if the total usage tracked in the
atomic_long_t exceeds 4GB, would this cause the counter to wrap and
silently bypass the limits?

> +
> +	/* Apply the new limit immediately so concurrent allocations are throttled. */
> +	xchg(&pool->cnt.max, limit);
> +
> +	if (nonblock)
> +		return;
> +
> +	int srcu_idx = srcu_read_lock(&dmemcg_srcu);
> +
> +	if (!READ_ONCE(region->unregistered) && region->ops && region->ops->reclaim) {
> +		for (int retries = DMEM_MAX_RECLAIM_RETRIES; ; ) {
> +			u64 usage = page_counter_read(&pool->cnt);
> +			int ret;
> +
> +			if (usage <= limit)
> +				break;
> +
> +			if (signal_pending(current))
> +				break;
> +
> +			ret = region->ops->reclaim(pool, usage - limit, region->reclaim_priv);
> +
> +			/* -ENOSPC means no progress; other errors are fatal. */
> +			if (ret && (ret != -ENOSPC || !retries--))
> +				break;
> +
> +			cond_resched();
> +		}
> +	}
> +	srcu_read_unlock(&dmemcg_srcu, srcu_idx);
>  }

[Severity: High]
Can this loop stall indefinitely and block region unregistration?

In set_resource_max(), if region->ops->reclaim() continually successfully
frees some memory but not enough to fall below the limit, it returns 0.
When it returns 0, the retries counter is not decremented:

    if (ret && (ret != -ENOSPC || !retries--))
        break;

Since srcu_read_lock(&dmemcg_srcu) is held for the entire duration of this
retry loop, an infinite loop here would indefinitely hold the SRCU read
lock.

If a cgroup administrator sets dmem.max below current usage while
concurrent processes continually allocate memory, could this block the
synchronize_srcu() call in dmem_cgroup_unregister_region() forever,
hanging the system during device hot-unplug or module unload?

Would it be safer to periodically drop and reacquire the SRCU read lock
inside the loop, or limit the total number of retries regardless of the
return code?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260723100350.16895-1-thomas.hellstrom@linux.intel.com?part=3

  reply	other threads:[~2026-07-23 10:21 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-23 10:03 [PATCH v8 0/6] Add reclaim to the dmem cgroup controller Thomas Hellström
2026-07-23 10:03 ` [PATCH v8 1/6] drm/amdgpu: Fix init ordering in amdgpu_vram_mgr_init() Thomas Hellström
2026-07-23 10:23   ` sashiko-bot
2026-07-23 10:03 ` [PATCH v8 2/6] cgroup/dmem: Introduce struct dmem_cgroup_init for region initialization Thomas Hellström
2026-07-23 10:03 ` [PATCH v8 3/6] cgroup/dmem: Add reclaim callback for lowering max below current usage Thomas Hellström
2026-07-23 10:21   ` sashiko-bot [this message]
2026-07-23 10:03 ` [PATCH v8 4/6] drm/ttm: Hook up a cgroup-aware reclaim callback for the dmem controller Thomas Hellström
2026-07-23 10:31   ` sashiko-bot
2026-07-23 12:02   ` Maarten Lankhorst
2026-07-23 15:55     ` Thomas Hellström
2026-07-23 10:03 ` [PATCH v8 5/6] drm/xe: Wire up dmem cgroup reclaim for VRAM manager Thomas Hellström
2026-07-23 10:03 ` [PATCH v8 6/6] drm/amdgpu: " Thomas Hellström
2026-07-23 10:50   ` sashiko-bot
2026-07-23 11:47 ` ✗ CI.checkpatch: warning for Add reclaim to the dmem cgroup controller (rev8) Patchwork
2026-07-23 11:48 ` ✓ CI.KUnit: success " Patchwork
2026-07-23 12:29 ` ✓ Xe.CI.BAT: " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260723102122.AB3B21F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hannes@cmpxchg.org \
    --cc=mkoutny@suse.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=thomas.hellstrom@linux.intel.com \
    --cc=tj@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.