From: David Laight <David.Laight@ACULAB.COM>
To: 'Borislav Petkov' <bp@alien8.de>, Michael Matz <matz@suse.de>
Cc: 'Dave Jiang' <dave.jiang@intel.com>,
"vkoul@kernel.org" <vkoul@kernel.org>,
"tglx@linutronix.de" <tglx@linutronix.de>,
"mingo@redhat.com" <mingo@redhat.com>,
"dan.j.williams@intel.com" <dan.j.williams@intel.com>,
"tony.luck@intel.com" <tony.luck@intel.com>,
"jing.lin@intel.com" <jing.lin@intel.com>,
"ashok.raj@intel.com" <ashok.raj@intel.com>,
"sanjay.k.kumar@intel.com" <sanjay.k.kumar@intel.com>,
"fenghua.yu@intel.com" <fenghua.yu@intel.com>,
"kevin.tian@intel.com" <kevin.tian@intel.com>,
"dmaengine@vger.kernel.org" <dmaengine@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: RE: [PATCH v5 1/5] x86/asm: Carve out a generic movdir64b() helper for general usage
Date: Thu, 24 Sep 2020 10:42:16 +0000 [thread overview]
Message-ID: <40f740d814764019ac2306800a6b68e4@AcuMS.aculab.com> (raw)
In-Reply-To: <20200924101506.GD5030@zn.tnic>
From: Borislav Petkov
> Sent: 24 September 2020 11:15
> On Thu, Sep 24, 2020 at 08:24:46AM +0000, David Laight wrote:
> > static inline void movdir64b(void *dst, const void *src)
> > {
> > /*
> > * 64 bytes from dst are marked as modified for completeness.
> > * Since the writes bypass the cache later reads may return
> > * old data anyway.
> > */
> > /* MOVDIR64B [rdx], rax */
> > asm volatile (".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
> > : "=m" ((struct { char _[64];} *)dst),
> > : "m" ((struct { char _[64];} *)src), "d" (src), "a" (dst));
>
> Now since you're so generous with your advice on random threads, please
> explain what you're advising here?
>
> The destination operand - in this case in %rax - is "destination memory
> address specified as offset to ES segment in the register operand."
The movdir64b instruction does a 'normal' read of 64 bytes (can be misaligned)
Then a cache-bypassing (probably) write-combining single 64byte write to
an address that must be aligned.
Any reference to segment registers is largely irrelevant since we are
not in real mode.
> So what is the difference between:
>
> ...(void *dst, ... )
>
> volatile struct { char _[64]; } *__dst = dst;
> ...
> : "=m" (__dst)
> : "a" (__dst)
>
> and
>
> ...(void *dst, ... )
> ...
> : "=m" ((struct { char _[64];} *)dst)
> : "a" (__dst)
>
> and why?
>
> Point me to the gcc documentation where this is explained.
Mainly less lines of code to look at.
> To cut to the chase, I don't think you need to do that, otherwise clwb()
> would be broken too but perhaps you know something I don't.
>
> Looking at clwb(), I believe the proper specification should be:
>
> volatile struct { char _[64]; } *__dst = dst;
>
> ...
>
> : "+m" (__dst)
> : "a" (__dst)
No idea what clwb() is doing.
But the "+m" (dst) tells gcc it depends on, and modifies the 64 bytes
at *dst.
I believe the 'volatile' is pointless.
> And if anything, the source specification should be something like that:
>
> volatile struct { char x[64]; } *__src = src;
>
> ...
>
>
> "d" (__src)
>
> because this tells gcc that the source operand would read 64 bytes
> through the pointer in the %rdx reg.
No, that just says the asm uses the value of the pointer.
Not what it points to.
> So this ends up close to what you're saying but it is using local
> variables to make the asm actually readable.
>
> Lemme add Micha to Cc for sanity-checking:
>
> Micha, the instruction is:
>
> MOVDIR64B %(rdx), rax
>
> "Move 64-bytes as direct-store with guaranteed 64-byte write atomicity
> from the source memory operand address to destination memory address
> specified as offset to ES segment in the register operand."
>
> Do I need to tell gcc that both operands are referencing 64 bytes,
> source operand is a memory reference, destination operand is an address
> specified in a register?
>
> What we have currently is:
>
> volatile struct { char _[64]; } *dst = __dst;
>
> /* MOVDIR64B [rdx], rax */
> asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
> : "=m" (dst)
> : "d" (from), "a" (dst));
That is wrong.
Feed this into cc -S -O2 and look at the .s file
static inline void movdir64b(void *dst, const void *src)
{
asm volatile(".byte 0x66, 0x0f, 0x38, 0xf8, 0x02"
:
: /*"m" ((struct { char _[64];} *)src),*/ "d" (src), "a" (dst)
);
void foo(void *dst, int val)
{
long b64[8] = { 0 };
b64[0] = val;
movdir64b(dst, b64);
}
Note that all to code that writes into b64[] is optimised away.
Repeat after uncommenting the "m" constraint and spot the difference.
The "=m" (dst) constraint is much less important here.
The write itself will always happen.
So do we need to tell gcc we did it?
Doing so just ensures gcc doesn't move any instructions that it knows
access the same memory above the movdir64b instruction.
But, because this is a cache bypassing write they are going
to be invalid anyway - without extra strong barriers.
So it is fairly safe to miss it out.
OTOH putting it in does no harm and helps annotate what the
instruction is doing.
I just failed to spot an example of a 'memory size' cast in the
kernel source tree - I'm sure there is an example somewhere.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
next prev parent reply other threads:[~2020-09-24 10:42 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <160090233730.44288.4446779116422752486.stgit@djiang5-desk3.ch.intel.com>
2020-09-23 23:10 ` [PATCH v5 3/5] dmaengine: idxd: Add shared workqueue support Dave Jiang
2020-09-23 23:11 ` [PATCH v5 4/5] dmaengine: idxd: Clean up descriptors with fault error Dave Jiang
2020-09-23 23:11 ` [PATCH v5 5/5] dmaengine: idxd: Add ABI documentation for shared wq Dave Jiang
[not found] ` <160090264332.44288.7575027054245105525.stgit@djiang5-desk3.ch.intel.com>
2020-09-24 8:24 ` [PATCH v5 1/5] x86/asm: Carve out a generic movdir64b() helper for general usage David Laight
2020-09-24 10:15 ` Borislav Petkov
2020-09-24 10:42 ` David Laight [this message]
2020-09-24 11:02 ` Borislav Petkov
2020-09-24 11:25 ` David Laight
2020-09-24 14:07 ` Michael Matz
2020-09-24 13:07 ` Borislav Petkov
2020-09-24 13:27 ` David Laight
2020-09-24 15:07 ` Dave Jiang
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=40f740d814764019ac2306800a6b68e4@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=ashok.raj@intel.com \
--cc=bp@alien8.de \
--cc=dan.j.williams@intel.com \
--cc=dave.jiang@intel.com \
--cc=dmaengine@vger.kernel.org \
--cc=fenghua.yu@intel.com \
--cc=jing.lin@intel.com \
--cc=kevin.tian@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=matz@suse.de \
--cc=mingo@redhat.com \
--cc=sanjay.k.kumar@intel.com \
--cc=tglx@linutronix.de \
--cc=tony.luck@intel.com \
--cc=vkoul@kernel.org \
/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