mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Xi Ruoyao <xry111@xry111.site>
To: Huacai Chen <chenhuacai@kernel.org>
Cc: WANG Xuerui <kernel@xen0n.name>,
	Hengqi Chen <hengqi.chen@gmail.com>,
	 Mingcong Bai <jeffbai@aosc.io>, Zixing Liu <liushuyu@aosc.io>,
	Alexei Starovoitov <ast@kernel.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	Andrii Nakryiko <andrii@kernel.org>,
	Martin KaFai Lau	 <martin.lau@linux.dev>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu	 <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	John Fastabend	 <john.fastabend@gmail.com>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev	 <sdf@fomichev.me>,
	Hao Luo <haoluo@google.com>, Jiri Olsa <jolsa@kernel.org>,
	 Tiezhu Yang <yangtiezhu@loongson.cn>,
	George Guo <guodongtai@kylinos.cn>,
	Chenghao Duan	 <duanchenghao@kylinos.cn>,
	loongarch@lists.linux.dev, 	linux-kernel@vger.kernel.org,
	bpf@vger.kernel.org
Subject: Re: [PATCH] LoongArch: Fix calling smp_processor_id() in preemptible code
Date: Thu, 26 Feb 2026 17:31:58 +0800	[thread overview]
Message-ID: <7b5200ab6541cbee3b1a6ba0b7185596a78bed2c.camel@xry111.site> (raw)
In-Reply-To: <CAAhV-H7rF+XwXmp_wfcHFmAgaybQdtzSUDRinFneLrGJKaNQCw@mail.gmail.com>

On Thu, 2026-02-26 at 17:08 +0800, Huacai Chen wrote:
> Hi, Ruoyao,
> 
> On Wed, Feb 25, 2026 at 10:15 PM Xi Ruoyao <xry111@xry111.site> wrote:
> > 
> > Fix the warning recently exploited by commit 4ab17e762b34 ("LoongArch:
> > BPF: Use BPF prog pack allocator"):
> > 
> >     BUG: using smp_processor_id() in preemptible [00000000] code: systemd/1
> >     caller is larch_insn_text_copy+0x40/0xf0
> > 
> > Simply changing it to raw_smp_processor_id() is not enough: if preempt
> > and CPU hotplug happens after raw_smp_processor_id() but before
> > stop_machine(), the CPU where raw_smp_processor_id() has run may be
> > offline when stop_machine() and no CPU will run copy_to_kernel_nofault()
> > in text_copy_cb().  Thus guard the larch_insn_text_copy() calls with
> > cpus_read_lock() and change stop_machine() to stop_machine_cpuslocked()
> > to prevent this.
> > 
> > Fixes: 9fbd18cf4c69 ("LoongArch: BPF: Add dynamic code modification support")
> > Signed-off-by: Xi Ruoyao <xry111@xry111.site>
> > ---
> > 
> > P.S. I've considered moving the locks inside larch_insn_text_copy() but
> > doing so seems not an easy hack.  In bpf_arch_text_poke() obviously the
> > memcpy() call must be guarded by text_mutex, so we have to leave the
> > acquire of text_mutex out of larch_insn_text_copy.  But in the entire
> > kernel the acquire of mutexes is always after cpus_read_lock(), so we
> > cannot put cpus_read_lock() into larch_insn_text_copy() while leaving
> > the text_mutex acquire out (or we risk a deadlock due to inconsistent
> > lock acquire order).  So I'd just submit this as-is to fix the bug and
> > leave the posssible refactor for others.
> > 
> >  arch/loongarch/kernel/inst.c | 4 ++--
> >  arch/loongarch/net/bpf_jit.c | 6 ++++++
> >  2 files changed, 8 insertions(+), 2 deletions(-)
> > 
> > diff --git a/arch/loongarch/kernel/inst.c b/arch/loongarch/kernel/inst.c
> > index bf037f0c6b26..db236e2b34a7 100644
> > --- a/arch/loongarch/kernel/inst.c
> > +++ b/arch/loongarch/kernel/inst.c
> > @@ -263,14 +263,14 @@ int larch_insn_text_copy(void *dst, void *src, size_t len)
> >                 .dst = dst,
> >                 .src = src,
> >                 .len = len,
> > -               .cpu = smp_processor_id(),
> > +               .cpu = raw_smp_processor_id(),
> I think we should keep the old function here, because it can help
> others to catch new bugs if the code is refactored in future.

Hmm, IIUC using smp_processor_id will still trigger the warning as we've
only acquired the cpu hotplug lock, instead of disabling the preemption
completely.

How about adding a lockdep_assert_cpus_held() in this function with an
explanatory comment?
> 

-- 
Xi Ruoyao <xry111@xry111.site>

  reply	other threads:[~2026-02-26  9:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-25 14:14 Xi Ruoyao
2026-02-26  9:08 ` Huacai Chen
2026-02-26  9:31   ` Xi Ruoyao [this message]
2026-02-26  9:49     ` Huacai Chen
2026-02-26 10:05       ` Xi Ruoyao
2026-02-26 10:16         ` Huacai Chen

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=7b5200ab6541cbee3b1a6ba0b7185596a78bed2c.camel@xry111.site \
    --to=xry111@xry111.site \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=chenhuacai@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=duanchenghao@kylinos.cn \
    --cc=eddyz87@gmail.com \
    --cc=guodongtai@kylinos.cn \
    --cc=haoluo@google.com \
    --cc=hengqi.chen@gmail.com \
    --cc=jeffbai@aosc.io \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kernel@xen0n.name \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=liushuyu@aosc.io \
    --cc=loongarch@lists.linux.dev \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=yangtiezhu@loongson.cn \
    --cc=yonghong.song@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®