From: Peter Xu <peterx@redhat.com>
To: Gavin Shan <gshan@redhat.com>
Cc: "Peter Maydell" <peter.maydell@linaro.org>,
"Philippe Mathieu-Daudé" <philmd@oss.qualcomm.com>,
qemu-arm@nongnu.org, qemu-devel@nongnu.org, mst@redhat.com,
richard.henderson@linaro.org, alex@shazbot.org,
berrange@redhat.com, philmd@mailo.com, david@kernel.org,
clg@redhat.com, pbonzini@redhat.com, phrdina@redhat.com,
jugraham@redhat.com, liugang24219@sangfor.com.cn,
dinghui@sangfor.com.cn, shan.gavin@gmail.com
Subject: Re: [PATCH v4 2/3] system/memory: Use qemu_ram_move() for directly accessible regions
Date: Tue, 28 Jul 2026 10:34:08 -0400 [thread overview]
Message-ID: <ami94CmeBNRKy_es@x1.local> (raw)
In-Reply-To: <2322668a-20a3-4a6b-8d7f-c04ddfef84f1@redhat.com>
On Tue, Jul 28, 2026 at 08:23:48PM +1000, Gavin Shan wrote:
> On 7/28/26 7:35 PM, Peter Maydell wrote:
> > On Tue, 28 Jul 2026 at 10:22, Philippe Mathieu-Daudé
> > <philmd@oss.qualcomm.com> wrote:
> > >
> > > On 28/7/26 11:01, Peter Maydell wrote:
> > > > On Tue, 28 Jul 2026 at 04:01, Gavin Shan <gshan@redhat.com> wrote:
> > > > >
> > > > > On 7/27/26 10:51 PM, Peter Maydell wrote:
> >
> >
> > > > > > > + test = (uintptr_t)src | (uintptr_t)dst | n;
> > > > > > > + len = test & -test;
> > > > > >
> > > > > > What is this doing? I am not a fan of clever bit twiddling
> > > > > > that isn't commented to explain itself. Readers of the code
> > > > > > shouldn't have to go off and search for an explanation of what
> > > > > > is going on.
> > > > > >
> > > > >
> > > > > The following comments will be added for (v5).
> > > > >
> > > > > /*
> > > > > * Maximal length of aligned access that are determined by @src,
> > > > > * @dst and @n
> > > > > */
> > > >
> > > > What I mean is more that it ought to say why "x & -x"
> > > > achieves that goal.
> > >
> > > Hmm isn't "x & -x" restricted to power of 2? Here we have no garanty
> > > @test is, because we have no garanty @n is. Ah, this is catched by
> > > the 'len != n' check and returns after calling memmove(). Indeed not
> > > very clear notation. Again:
> > >
> > > ...
> > >
> > > /* Overlapping buffers, unaligned or oversized access */
> > > if (!QEMU_IS_ALIGNED((uintptr_t)src | (uintptr_t)dst, n)) {
> > > memmove(dst, src, n);
> > > return;
> > > }
> > >
> > > switch (n) {
> > > ...
> >
> > I'm not inherently against the bit twiddling (QEMU_IS_ALIGNED
> > on a variable length will do a division, maybe we care?);
> > I just want that if we do use a bit-twiddling trick that we
> > explain why it does the thing we want it to do.
I think the bit ops should be more efficient. I plan to queue v5 with a
fixup, v5 here:
https://lore.kernel.org/qemu-devel/20260728031731.286666-1-gshan@redhat.com/
Fixup:
diff --git a/system/physmem.c b/system/physmem.c
index fbe7df2391..2f37cbeb07 100644
--- a/system/physmem.c
+++ b/system/physmem.c
@@ -3162,13 +3162,18 @@ void qemu_ram_move(void *dst, const void *src, size_t n)
{
uintptr_t test, len;
- if (src == dst || n == 0) {
+ if (n == 0) {
return;
}
/*
- * Maximal length of aligned access that are determined by @src,
- * @dst and @n
+ * Calculate "the lowest set bit" over @src, @dst and @n, result put
+ * into @len (which guarantees a power-of-two). With that and the
+ * later check (len!=n), it makes sure that we will only do the atomic
+ * ops when:
+ *
+ * (1) @n is a power-of-two
+ * (2) @src and @dst addresses are both aligned to @n
*/
test = (uintptr_t)src | (uintptr_t)dst | n;
len = test & -test;
Does it look ok to all of you?
Thanks,
--
Peter Xu
next prev parent reply other threads:[~2026-07-28 14:34 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-27 3:26 [PATCH v4 0/3] system/memory: Make ram device region directly accessible Gavin Shan
2026-07-27 3:26 ` [PATCH v4 1/3] system/memory: Use memmove() for directly accessible regions Gavin Shan
2026-07-27 10:51 ` Peter Maydell
2026-07-27 14:03 ` Peter Xu
2026-07-27 3:26 ` [PATCH v4 2/3] system/memory: Use qemu_ram_move() " Gavin Shan
2026-07-27 12:51 ` Peter Maydell
2026-07-27 13:52 ` Philippe Mathieu-Daudé
2026-07-28 3:09 ` Gavin Shan
2026-07-27 14:02 ` Peter Xu
2026-07-28 3:02 ` Gavin Shan
2026-07-28 3:00 ` Gavin Shan
2026-07-28 9:01 ` Peter Maydell
2026-07-28 9:22 ` Philippe Mathieu-Daudé
2026-07-28 9:35 ` Peter Maydell
2026-07-28 10:23 ` Gavin Shan
2026-07-28 14:34 ` Peter Xu [this message]
2026-07-28 14:38 ` Peter Maydell
2026-07-28 15:17 ` Peter Xu
2026-07-27 3:26 ` [PATCH v4 3/3] system/memory: Make ram device region directly accessible Gavin Shan
2026-07-27 12:07 ` Peter Maydell
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=ami94CmeBNRKy_es@x1.local \
--to=peterx@redhat.com \
--cc=alex@shazbot.org \
--cc=berrange@redhat.com \
--cc=clg@redhat.com \
--cc=david@kernel.org \
--cc=dinghui@sangfor.com.cn \
--cc=gshan@redhat.com \
--cc=jugraham@redhat.com \
--cc=liugang24219@sangfor.com.cn \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=philmd@mailo.com \
--cc=philmd@oss.qualcomm.com \
--cc=phrdina@redhat.com \
--cc=qemu-arm@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=richard.henderson@linaro.org \
--cc=shan.gavin@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 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.