From: Igor Mammedov <imammedo@redhat.com>
To: Zhao Liu <zhao1.liu@intel.com>
Cc: qemu-devel@nongnu.org, pbonzini@redhat.com, peterx@redhat.com,
mst@redhat.com, mtosatti@redhat.com,
richard.henderson@linaro.org, riku.voipio@iki.fi,
thuth@redhat.com, pasic@linux.ibm.com, borntraeger@linux.ibm.com,
david@redhat.com, jjherne@linux.ibm.com, shorne@gmail.com,
eduardo@habkost.net, marcel.apfelbaum@gmail.com,
philmd@linaro.org, wangyanan55@huawei.com,
peter.maydell@linaro.org, agraf@csgraf.de, mads@ynddal.dk,
mrolnik@gmail.com, deller@gmx.de, dirty@apple.com,
rbolshakov@ddn.com, phil@philjordan.eu, reinoud@netbsd.org,
sunilmut@microsoft.com, gaosong@loongson.cn, laurent@vivier.eu,
edgar.iglesias@gmail.com, aurelien@aurel32.net,
jiaxun.yang@flygoat.com, arikalo@gmail.com,
chenhuacai@kernel.org, npiggin@gmail.com, rathc@linux.ibm.com,
harshpb@linux.ibm.com, yoshinori.sato@nifty.com,
iii@linux.ibm.com, mark.cave-ayland@ilande.co.uk,
atar4qemu@gmail.com, qemu-s390x@nongnu.org, qemu-arm@nongnu.org,
qemu-ppc@nongnu.org
Subject: Re: [PATCH v5 6/8] add cpu_test_interrupt()/cpu_set_interrupt() helpers and use them tree wide
Date: Tue, 26 Aug 2025 10:47:31 +0200 [thread overview]
Message-ID: <20250826104731.1440e3ed@fedora> (raw)
In-Reply-To: <aK1mHGan+n9NSAOk@intel.com>
On Tue, 26 Aug 2025 15:45:32 +0800
Zhao Liu <zhao1.liu@intel.com> wrote:
> On Mon, Aug 25, 2025 at 05:19:12PM +0200, Igor Mammedov wrote:
> > Date: Mon, 25 Aug 2025 17:19:12 +0200
> > From: Igor Mammedov <imammedo@redhat.com>
> > Subject: Re: [PATCH v5 6/8] add cpu_test_interrupt()/cpu_set_interrupt()
> > helpers and use them tree wide
> > X-Mailer: Claws Mail 4.3.1 (GTK 3.24.49; x86_64-redhat-linux-gnu)
> >
> > On Mon, 25 Aug 2025 23:28:22 +0800
> > Zhao Liu <zhao1.liu@intel.com> wrote:
> >
> > > Hi Igor,
> > >
> > > > diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h
> > > > index 5eaf41a566..1dee9d4c76 100644
> > > > --- a/include/hw/core/cpu.h
> > > > +++ b/include/hw/core/cpu.h
> > > > @@ -942,6 +942,31 @@ CPUState *cpu_by_arch_id(int64_t id);
> > > >
> > > > void cpu_interrupt(CPUState *cpu, int mask);
> > > >
> > > > +/**
> > > > + * cpu_test_interrupt:
> > > > + * @cpu: The CPU to check interrupt(s) on.
> > > > + * @mask: The interrupts to check.
> > > > + *
> > > > + * Checks if any of interrupts in @mask are pending on @cpu.
> > > > + */
> > > > +static inline bool cpu_test_interrupt(CPUState *cpu, int mask)
> > > > +{
> > > > + return qatomic_load_acquire(&cpu->interrupt_request) & mask;
> > > > +}
> > > > +
> > > > +/**
> > > > + * cpu_set_interrupt:
> > > > + * @cpu: The CPU to set pending interrupt(s) on.
> > > > + * @mask: The interrupts to set.
> > > > + *
> > > > + * Sets interrupts in @mask as pending on @cpu.
> > > > + */
> > > > +static inline void cpu_set_interrupt(CPUState *cpu, int mask)
> > > > +{
> > > > + qatomic_store_release(&cpu->interrupt_request,
> > > > + cpu->interrupt_request | mask);
> > >
> > > It seems the read access of cpu->interrupt_request is not atomic, should
> > > we also protect it by qatomic_read(cpu->interrupt_request)? like
> > >
> > > qatomic_store_release(&cpu->interrupt_request,
> > > qatomic_read(cpu->interrupt_request) | mask)
> >
> > it's not necessary according to doc:
> >
> > - ``qatomic_store_release()``, which guarantees the STORE to appear to
> > happen, ...,
> > after all the LOAD or STORE operations specified before.
> > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >
> > that includes 'cpu->interrupt_request | mask' part
>
> Yes, thanks for your explaination and patience.
>
> > >
> > > or futher,
> > >
> > > qatomic_fetch_or(&cpu->interrupt_request, mask)
> > that would work as well but it also could be more expensive than
> > qatomic_store_release()
>
> Behind this helper, I mainly considerred the case of multiple writers:
>
> thread 0 . thread 1
> .
> load: x .
> OR: x | a .
> .
> . load: x
> . OR: x | b
> . store: x | b
> .
> store: x | a . (x | b is missed)
>
> In the above case, "load" means the direct access:
> cpu->interrupt_request w/o protection, and "store" is done by
> qatomic_store_release.
>
> The memory order is guaranteed, but the operation result of thread 1
> seems lost. Only BQL or other mutex could avoid such case.
>
> qatomic_store_release is already a great step to avoid issues outside
> BQL, so I'm not sure if it's worth going further to ensure atomicity,
> especifically for multiple writers (my initial understanding is that
> iothread or callback may have multiple writers, but I'm also a bit
> unsure.). The overhead is also indeed an issue.
it looks like we are always holding BQL when setting interrupt.
However currently we also have places that check interrupts
without BQL but without using any atomics. This patch aims to ensure
that proper barriers are in place when checking for interrupts
and introduces release/acquire pair helpers for cpu->interrupt_request,
to ensure it's don consistently.
While overhead might be issue, it's better to have correcteness 1st.
(that's why blanket tree wide change to make sure we don't miss places that
set/test interrupts).
Then if performance issues were found somewhere, as was suggested
in previous reviews, we may opencode that place without barriers
with a mandatory comment/justification why it's okey doing so.
(well, at least that's the plan)
>
> Thanks,
> Zhao
>
next prev parent reply other threads:[~2025-08-26 8:48 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-14 16:05 [PATCH v4 0/8] Reinvent BQL-free PIO/MMIO Igor Mammedov
2025-08-14 16:05 ` [PATCH v4 1/8] memory: reintroduce BQL-free fine-grained PIO/MMIO Igor Mammedov
2025-08-25 10:55 ` Philippe Mathieu-Daudé
2025-08-14 16:05 ` [PATCH v4 2/8] acpi: mark PMTIMER as unlocked Igor Mammedov
2025-08-14 16:05 ` [PATCH v4 3/8] hpet: switch to fain-grained device locking Igor Mammedov
2025-08-25 14:43 ` Zhao Liu
2025-08-14 16:05 ` [PATCH v4 4/8] hpet: move out main counter read into a separate block Igor Mammedov
2025-08-25 14:44 ` Zhao Liu
2025-08-14 16:05 ` [PATCH v4 5/8] hpet: make main counter read lock-less Igor Mammedov
2025-08-25 14:55 ` Zhao Liu
2025-08-25 15:10 ` Igor Mammedov
2025-08-14 16:05 ` [PATCH v4 6/8] add cpu_test_interrupt()/cpu_set_interrupt() helpers and use them tree wide Igor Mammedov
2025-08-14 19:05 ` Peter Xu
2025-08-20 15:01 ` Jason J. Herne
2025-08-21 15:57 ` Igor Mammedov
2025-08-21 15:56 ` [PATCH v5 " Igor Mammedov
2025-08-25 8:16 ` Harsh Prateek Bora
2025-08-25 15:06 ` Igor Mammedov
2025-08-25 10:35 ` Philippe Mathieu-Daudé
2025-08-25 15:02 ` Igor Mammedov
2025-08-25 15:28 ` Zhao Liu
2025-08-25 15:19 ` Igor Mammedov
2025-08-26 7:45 ` Zhao Liu
2025-08-26 8:47 ` Igor Mammedov [this message]
2025-08-26 9:27 ` Zhao Liu
2025-08-29 8:18 ` Paolo Bonzini
2025-08-29 12:33 ` Paolo Bonzini
2025-09-01 12:05 ` Igor Mammedov
2025-09-01 12:06 ` Paolo Bonzini
2025-08-14 16:05 ` [PATCH v4 7/8] kvm: i386: irqchip: take BQL only if there is an interrupt Igor Mammedov
2025-08-25 10:46 ` Philippe Mathieu-Daudé
2025-08-27 8:40 ` Igor Mammedov
2025-08-14 16:06 ` [PATCH v4 8/8] tcg: move interrupt caching and single step masking closer to user Igor Mammedov
2025-08-29 8:19 ` [PATCH v4 0/8] Reinvent BQL-free PIO/MMIO Paolo Bonzini
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=20250826104731.1440e3ed@fedora \
--to=imammedo@redhat.com \
--cc=agraf@csgraf.de \
--cc=arikalo@gmail.com \
--cc=atar4qemu@gmail.com \
--cc=aurelien@aurel32.net \
--cc=borntraeger@linux.ibm.com \
--cc=chenhuacai@kernel.org \
--cc=david@redhat.com \
--cc=deller@gmx.de \
--cc=dirty@apple.com \
--cc=edgar.iglesias@gmail.com \
--cc=eduardo@habkost.net \
--cc=gaosong@loongson.cn \
--cc=harshpb@linux.ibm.com \
--cc=iii@linux.ibm.com \
--cc=jiaxun.yang@flygoat.com \
--cc=jjherne@linux.ibm.com \
--cc=laurent@vivier.eu \
--cc=mads@ynddal.dk \
--cc=marcel.apfelbaum@gmail.com \
--cc=mark.cave-ayland@ilande.co.uk \
--cc=mrolnik@gmail.com \
--cc=mst@redhat.com \
--cc=mtosatti@redhat.com \
--cc=npiggin@gmail.com \
--cc=pasic@linux.ibm.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=peterx@redhat.com \
--cc=phil@philjordan.eu \
--cc=philmd@linaro.org \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=qemu-ppc@nongnu.org \
--cc=qemu-s390x@nongnu.org \
--cc=rathc@linux.ibm.com \
--cc=rbolshakov@ddn.com \
--cc=reinoud@netbsd.org \
--cc=richard.henderson@linaro.org \
--cc=riku.voipio@iki.fi \
--cc=shorne@gmail.com \
--cc=sunilmut@microsoft.com \
--cc=thuth@redhat.com \
--cc=wangyanan55@huawei.com \
--cc=yoshinori.sato@nifty.com \
--cc=zhao1.liu@intel.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;
as well as URLs for NNTP newsgroup(s).