All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Hui Zhu" <hui.zhu@linux.dev>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf-next v5 1/2] mm/bpf: Add bpf_proactive_reclaim kfunc
Date: Thu, 27 Aug 2026 10:46:42 +0000	[thread overview]
Message-ID: <20260827104642.D8F951F000E9@smtp.kernel.org> (raw)
In-Reply-To: <5dfdc7800469eac4e9a240f2422ba65d4ef4c4ba.1787826402.git.zhuhui@kylinos.cn>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] BPF kfunc `bpf_proactive_reclaim` can cause kernel deadlocks by triggering memory reclaim with `GFP_KERNEL` from sleepable contexts where filesystem or IO locks may be held.
- [Medium] Integer overflow in calculating the returned reclaimed bytes on 32-bit architectures.
--

commit ef066c78e33635cc7e9aa0fc78438725da7dc30b
Author: Hui Zhu <zhuhui@kylinos.cn>

mm/bpf: Add bpf_proactive_reclaim kfunc

This commit adds a sleepable kfunc bpf_proactive_reclaim() which performs
one proactive reclaim pass on a given memory cgroup. It refuses to reclaim
if the calling task is already in a reclaim context to avoid corrupting the
outer reclaim state.

> diff --git a/mm/bpf_memcontrol.c b/mm/bpf_memcontrol.c
> index 716df49d76477..297ff7f050427 100644
> --- a/mm/bpf_memcontrol.c
> +++ b/mm/bpf_memcontrol.c

[ ... ]

> @@ -159,6 +160,49 @@ __bpf_kfunc void bpf_mem_cgroup_flush_stats(struct mem_cgroup *memcg)
>  	mem_cgroup_flush_stats(memcg);
>  }

[ ... ]

> +__bpf_kfunc unsigned long bpf_proactive_reclaim(struct mem_cgroup *memcg,
> +						unsigned long size)
> +{
> +	unsigned long nr_reclaimed;
> +
> +	if (size < PAGE_SIZE || unlikely(bpf_in_reclaim_context()))
> +		return 0;
> +
> +	nr_reclaimed = try_to_free_mem_cgroup_pages(memcg, size / PAGE_SIZE,
> +						    GFP_KERNEL,

[Severity: High]
Can this cause a kernel deadlock when called from sleepable contexts where
filesystem or IO locks may be held?

Since GFP_KERNEL includes __GFP_FS and __GFP_IO, the direct reclaim path is
allowed to invoke filesystem shrinkers. If a shrinker attempts to acquire a
lock already held by the task (e.g., if a sleepable BPF program attached to
an LSM hook like inode_rmdir or security_file_open calls
bpf_proactive_reclaim), a self-deadlock could occur.

Would it be safer to wrap the reclaim call in memalloc_nofs_save() and
memalloc_nofs_restore() to guarantee safety, as the verifier cannot
statically enforce that no filesystem locks are held?

> +						    MEMCG_RECLAIM_MAY_SWAP |
> +						    MEMCG_RECLAIM_PROACTIVE,
> +						    NULL);
> +
> +	return nr_reclaimed * PAGE_SIZE;

[Severity: Medium]
Does this calculation overflow the 32-bit boundary before returning to the
caller?

On 32-bit architectures, unsigned long is 32 bits, making ULONG_MAX
approximately 4GB. If a user requests a reclaim pass with size close to
ULONG_MAX, the target pages will be close to 1,048,575.

try_to_free_mem_cgroup_pages() reclaims in batches and can easily overshoot
the target.

If nr_reclaimed exceeds 1,048,575 pages, multiplying it by PAGE_SIZE (4096)
will wrap around the 32-bit limit, yielding a severely truncated result.

> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1787826402.git.zhuhui@kylinos.cn?part=1

  reply	other threads:[~2026-08-27 10:46 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-27 10:36 [PATCH bpf-next v5 0/2] bpf: BPF-driven proactive memcg reclaim Hui Zhu
2026-08-27 10:36 ` [PATCH bpf-next v5 1/2] mm/bpf: Add bpf_proactive_reclaim kfunc Hui Zhu
2026-08-27 10:46   ` sashiko-bot [this message]
2026-08-27 11:37   ` bot+bpf-ci
2026-08-28 19:53   ` Shakeel Butt
2026-08-28 21:07     ` Kumar Kartikeya Dwivedi
2026-09-01  2:59       ` Hui Zhu
2026-08-27 10:36 ` [PATCH bpf-next v5 2/2] selftests/bpf: Add memcg async reclaim test Hui Zhu
2026-08-27 10:46   ` sashiko-bot
2026-08-27 11:37   ` bot+bpf-ci

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=20260827104642.D8F951F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=hui.zhu@linux.dev \
    --cc=sashiko-reviews@lists.linux.dev \
    /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.