From: Andrea Righi <arighi@nvidia.com>
To: Changwoo Min <changwoo@igalia.com>
Cc: Tejun Heo <tj@kernel.org>, David Vernet <void@manifault.com>,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] sched_ext: Fix missing rq lock in scx_bpf_cpuperf_set()
Date: Mon, 21 Apr 2025 10:35:26 +0200 [thread overview]
Message-ID: <aAYDTp-c3SV_Lnsz@gpd3> (raw)
In-Reply-To: <32e5c7cb-5b41-4e02-81d4-5bbed981ab03@igalia.com>
Hi Changwoo,
On Mon, Apr 21, 2025 at 04:44:02PM +0900, Changwoo Min wrote:
> Hi Andrea,
>
> The patchset looks good to me. Thanks!
> I have one comment, that don't need to be addressed in this patch set,
> below:
>
> Acked-by: Changwoo Min <changwoo@igalia.com>
>
> On 4/21/25 04:30, Andrea Righi wrote:
> > scx_bpf_cpuperf_set() can be used to set a performance target level on
> > any CPU. However, it doesn't correctly acquire the corresponding rq
> > lock, which may lead to unsafe behavior and trigger the following
> > warning, due to the lockdep_assert_rq_held() check:
> >
> > [ 51.713737] WARNING: CPU: 3 PID: 3899 at kernel/sched/sched.h:1512 scx_bpf_cpuperf_set+0x1a0/0x1e0
> > ...
> > [ 51.713836] Call trace:
> > [ 51.713837] scx_bpf_cpuperf_set+0x1a0/0x1e0 (P)
> > [ 51.713839] bpf_prog_62d35beb9301601f_bpfland_init+0x168/0x440
> > [ 51.713841] bpf__sched_ext_ops_init+0x54/0x8c
> > [ 51.713843] scx_ops_enable.constprop.0+0x2c0/0x10f0
> > [ 51.713845] bpf_scx_reg+0x18/0x30
> > [ 51.713847] bpf_struct_ops_link_create+0x154/0x1b0
> > [ 51.713849] __sys_bpf+0x1934/0x22a0
> >
> > Fix by properly acquiring the rq lock when possible or raising an error
> > if we try to operate on a CPU that is not the one currently locked.
> >
> > Fixes: d86adb4fc0655 ("sched_ext: Add cpuperf support")
> > Signed-off-by: Andrea Righi <arighi@nvidia.com>
> > ---
> > kernel/sched/ext.c | 27 +++++++++++++++++++++++----
> > 1 file changed, 23 insertions(+), 4 deletions(-)
> >
> > diff --git a/kernel/sched/ext.c b/kernel/sched/ext.c
> > index 51dad94f1952d..6b6681b14488e 100644
> > --- a/kernel/sched/ext.c
> > +++ b/kernel/sched/ext.c
> > @@ -7088,13 +7088,32 @@ __bpf_kfunc void scx_bpf_cpuperf_set(s32 cpu, u32 perf)
> > }
> > if (ops_cpu_valid(cpu, NULL)) {
> > - struct rq *rq = cpu_rq(cpu);
> > + struct rq *rq = cpu_rq(cpu), *locked_rq = scx_locked_rq();
> > + struct rq_flags rf;
> > +
> > + /*
> > + * When called with an rq lock held, restrict the operation
> > + * to the corresponding CPU to prevent ABBA deadlocks.
> > + */
> > + if (locked_rq && rq != locked_rq) {
> > + scx_error("Invalid target CPU %d", cpu);
> > + return;
> > + }
>
>
> Considering almost all chext_ext ops hold an rq lock, the actual ops
> where scx_bpf_cpuperf_set() for a remote CPU is possible will be very
> limited. When there are more use cases for remote CPU kfuncs calls, I
> think we need to come up with some mechanism, for example, extending
> schedule_deferred() to cover more actions.
I agree, in this particular case, I think it's perfectly reasonable to
simply ops error when targeting a remote CPU, I don’t see any valid use
case for changing the cpufreq target of a remote CPU while we're holding a
rq lock.
We do have some users of scx_bpf_cpuperf_set() that invoke it without
holding any rq lock, for example, scx_rusty/wd40 and scx_bpfland can
set the cpuperf target from ops.init(), but this scenario is already
covered by this patchset.
In general, we may have APIs in the future that may need to modify
properties of a remote rq/cpu. In such cases, extending schedule_deferred()
sounds like the right path forward to me as well.
Thanks,
-Andrea
next prev parent reply other threads:[~2025-04-21 8:35 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-20 19:30 [PATCH v2 0/2] sched_ext: Introduce rq lock tracking Andrea Righi
2025-04-20 19:30 ` [PATCH 1/2] sched_ext: Track currently locked rq Andrea Righi
2025-04-21 19:03 ` Tejun Heo
2025-04-22 6:27 ` Andrea Righi
2025-04-20 19:30 ` [PATCH 2/2] sched_ext: Fix missing rq lock in scx_bpf_cpuperf_set() Andrea Righi
2025-04-21 7:44 ` Changwoo Min
2025-04-21 8:35 ` Andrea Righi [this message]
2025-04-21 19:04 ` Tejun Heo
-- strict thread matches above, loose matches on Subject: below --
2025-04-22 8:26 [PATCH v3 0/2] sched_ext: Introduce rq lock tracking Andrea Righi
2025-04-22 8:26 ` [PATCH 2/2] sched_ext: Fix missing rq lock in scx_bpf_cpuperf_set() Andrea Righi
2025-04-19 12:24 [PATCH 0/2] sched_ext: Introduce rq lock tracking Andrea Righi
2025-04-19 12:24 ` [PATCH 2/2] sched_ext: Fix missing rq lock in scx_bpf_cpuperf_set() Andrea Righi
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=aAYDTp-c3SV_Lnsz@gpd3 \
--to=arighi@nvidia.com \
--cc=changwoo@igalia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=tj@kernel.org \
--cc=void@manifault.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