BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vernon Yang" <vernon2gm@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH 2/4] bpf: add bpf_cgroup_{flush_stats,stall} function
Date: Sun, 03 May 2026 17:25:19 +0000	[thread overview]
Message-ID: <20260503172520.376AAC2BCB4@smtp.kernel.org> (raw)
In-Reply-To: <20260503165024.1526680-3-vernon2gm@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Missing bounds check on the `states` enum parameter in `bpf_cgroup_stall` allows out-of-bounds memory access.
- [High] Both `bpf_cgroup_stall` and `bpf_cgroup_flush_stats` unconditionally dereference the pointer returned by `cgroup_psi()`, which can be NULL.
- [High] Compilation fails when `CONFIG_CGROUPS=y` but `CONFIG_PSI=n`.
- [Medium] The `bpf_cgroup_stall` function truncates its 64-bit return value on 32-bit architectures.
--

commit 11e75919a882d61e2b11423881cab05305d77c18
Author: Vernon Yang <yanglincheng@kylinos.cn>

bpf: add bpf_cgroup_{flush_stats,stall} function

Add bpf_cgroup_{flush_stats,stall} function to prepare for the
subsequent mthp_ext ebpf program.

> diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
> index 2bb60200c266..87f3072adce3 100644
> --- a/kernel/bpf/helpers.c
> +++ b/kernel/bpf/helpers.c

[ ... ]

> @@ -2819,6 +2820,32 @@ __bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid)
>  	return cgrp;
>  }
>  
> +/**
> + * bpf_cgroup_stall - acquire the total stall time of cgroup
> + * @cgrp: cgroup struct
> + * @states: psi states
> + *
> + * Return the total stall time.
> + */
> +__bpf_kfunc unsigned long bpf_cgroup_stall(struct cgroup *cgrp,
> +					   enum psi_states states)

Is it possible that div_u64() returns a 64-bit value that gets truncated
here on 32-bit architectures? Since unsigned long is 32 bits on those
systems, would changing the return type to u64 prevent the stall time
from wrapping around?

One more thing regarding the build, what happens when CONFIG_CGROUPS is
enabled but CONFIG_PSI is disabled? Since struct psi_group might be defined
as an empty struct without a total field in include/linux/psi_types.h, and
cgroup_psi() wouldn't be declared, would it be better to wrap these kfuncs
and their BTF_ID_FLAGS in an #ifdef CONFIG_PSI block?

> +{
> +	struct psi_group *group = cgroup_psi(cgrp);
> +
> +	return div_u64(group->total[PSI_AVGS][states], NSEC_PER_MSEC);

Could states be out of bounds here? Since the BPF verifier treats enum
parameters as unconstrained scalars, a BPF program might pass an arbitrary
or negative integer. Does this need a runtime bounds check like
states < 0 || states >= NR_PSI_STATES - 1 ?

Also, is it possible for group to be NULL? If cgroup PSI tracking is
disabled at runtime via the cgroup_disable=pressure boot parameter,
cgroup_psi(cgrp) can return NULL for non-root cgroups. Does this dereference
of group->total need a NULL check first?

> +}
> +
> +/**
> + * bpf_cgroup_flush_stats - Flush cgroup's statistics
> + * @cgrp: cgroup struct
> + */
> +__bpf_kfunc void bpf_cgroup_flush_stats(struct cgroup *cgrp)
> +{
> +	struct psi_group *group = cgroup_psi(cgrp);
> +
> +	psi_group_flush_stats(group);

Similar to bpf_cgroup_stall(), if PSI tracking is disabled, group could
be NULL here. Since psi_group_flush_stats() might unconditionally acquire
a mutex, does this code need a NULL check on group to prevent a panic?

> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260503165024.1526680-1-vernon2gm@gmail.com?part=2

  parent reply	other threads:[~2026-05-03 17:25 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-03 16:50 [PATCH 0/4] mm: introduce mthp_ext via cgroup-bpf to make mTHP more transparent Vernon Yang
2026-05-03 16:50 ` [PATCH 1/4] psi: add psi_group_flush_stats() function Vernon Yang
2026-05-03 16:50 ` [PATCH 2/4] bpf: add bpf_cgroup_{flush_stats,stall} function Vernon Yang
2026-05-03 17:23   ` bot+bpf-ci
2026-05-06 12:38     ` Vernon Yang
2026-05-03 17:25   ` sashiko-bot [this message]
2026-05-06 12:55     ` Vernon Yang
2026-05-03 16:50 ` [PATCH 3/4] mm: introduce bpf_mthp_ops struct ops Vernon Yang
2026-05-03 17:35   ` bot+bpf-ci
2026-05-06 13:06     ` Vernon Yang
2026-05-03 17:41   ` sashiko-bot
2026-05-06 13:26     ` Vernon Yang
2026-05-03 16:50 ` [PATCH 4/4] samples: bpf: add mthp_ext Vernon Yang
2026-05-03 17:35   ` bot+bpf-ci
2026-05-06 13:30     ` Vernon Yang
2026-05-03 17:57   ` sashiko-bot
2026-05-06 13:50     ` Vernon Yang
2026-05-07  3:34 ` [PATCH 0/4] mm: introduce mthp_ext via cgroup-bpf to make mTHP more transparent Yafang Shao
2026-05-07 12:50   ` Vernon Yang
2026-05-07 13:18     ` Yafang Shao
2026-05-07 15:19       ` Vernon Yang

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=20260503172520.376AAC2BCB4@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko@lists.linux.dev \
    --cc=vernon2gm@gmail.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox