From: eugeniy.paltsev@synopsys.com (Eugeniy Paltsev)
To: linux-snps-arc@lists.infradead.org
Subject: [PATCH 1/2] ARCv2: LIB: memeset: fix doing prefetchw outside of buffer
Date: Tue, 15 Jan 2019 12:00:04 +0000 [thread overview]
Message-ID: <1547553603.24248.49.camel@synopsys.com> (raw)
In-Reply-To: <C2D7FE5348E1B147BCA15975FBA2307501464272C9@US01WEMBX2.internal.synopsys.com>
Hi Vineet,
On Tue, 2019-01-15@01:09 +0000, Vineet Gupta wrote:
> On 1/14/19 7:17 AM, Eugeniy Paltsev wrote:
> > Current ARCv2 memeset implementation may call 'prefetchw'
> > instruction for address which lies outside of memset area.
> > So we got one modified (dirty) cache line outside of memset
> > area. This may lead to data corruption if this area is used
> > for DMA IO.
> >
> > Another issue is that current ARCv2 memeset implementation
> > may call 'prealloc' instruction for L1 cache line which
> > doesn't fully belongs to memeset area in case of 128B L1 D$
> > line length. That leads to data corruption.
> >
> >
* Fix prefetchw/prealloc instructions using in case of 64B L1 data
> > cache line (default case) and don't use prefetch* instructions
> > for other possible L1 data cache line lengths (32B and 128B).
> >
> > Signed-off-by: Eugeniy Paltsev <Eugeniy.Paltsev at synopsys.com>
> > ---
> > arch/arc/lib/memset-archs.S | 30 +++++++++++++++++++++++-------
> > 1 file changed, 23 insertions(+), 7 deletions(-)
> >
> > diff --git a/arch/arc/lib/memset-archs.S b/arch/arc/lib/memset-archs.S
> > index 62ad4bcb841a..c7717832336f 100644
> > --- a/arch/arc/lib/memset-archs.S
> > +++ b/arch/arc/lib/memset-archs.S
> > @@ -7,11 +7,32 @@
> > */
> >
> > #include <linux/linkage.h>
> > +#include <asm/cache.h>
> >
> > #undef PREALLOC_NOT_AVAIL
> >
> > +/*
> > + * The memset implementation below is optimized to use prefetchw and prealloc
> > + * instruction in case of CPU with 64B L1 data cache line (L1_CACHE_SHIFT == 6)
> > + * If you want to implement optimized memset for other possible L1 data cache
> > + * line lengths (32B and 128B) you should rewrite code carefully checking
> > + * we don't call any prefetchw/prealloc instruction for L1 cache lines which
> > + * don't belongs to memset area.
>
> Good point. FWIW, it is possible to support those non common line lengths by using
> L1_CACHE_SHIFT etc in asm code below but I agree its not worth the trouble.
>
> > + */
> > +#if L1_CACHE_SHIFT!=6
> > +# define PREALLOC_INSTR(...)
> > +# define PREFETCHW_INSTR(...)
> > +#else /* L1_CACHE_SHIFT!=6 */
> > +# define PREFETCHW_INSTR(...) prefetchw __VA_ARGS__
> > +# ifdef PREALLOC_NOT_AVAIL
> > +# define PREALLOC_INSTR(...) prefetchw __VA_ARGS__
> > +# else
> > +# define PREALLOC_INSTR(...) prealloc __VA_ARGS__
> > +# endif
> > +#endif /* L1_CACHE_SHIFT!=6 */
> > +
> > ENTRY_CFI(memset)
> > - prefetchw [r0] ; Prefetch the write location
> > + PREFETCHW_INSTR([r0]) ; Prefetch the first write location
> > mov.f 0, r2
> > ;;; if size is zero
> > jz.d [blink]
> > @@ -48,11 +69,7 @@ ENTRY_CFI(memset)
> >
> > lpnz @.Lset64bytes
> > ;; LOOP START
> > -#ifdef PREALLOC_NOT_AVAIL
> > - prefetchw [r3, 64] ;Prefetch the next write location
> > -#else
> > - prealloc [r3, 64]
> > -#endif
> > + PREALLOC_INSTR([r3, 64]) ;Prefetch the next write location
>
> These are not solving the issue - I'd break this up and move these bits to your
> next patch.
Actually these are solving another issue - current implementation may call
'prealloc' instruction for L1 cache line which doesn't fully belongs to
memeset area in case of 128B L1 D$ line length. As the 'prealloc' fill cache line
with zeros this leads to data corruption.
So I would better keep these changes in this 'fix' patch.
BTW, I've forgot again to add Cc: stable at vger.kernel.org, could you add it for me,
when applying patch?
Thanks.
> > #ifdef CONFIG_ARC_HAS_LL64
> > std.ab r4, [r3, 8]
> > std.ab r4, [r3, 8]
> > @@ -85,7 +102,6 @@ ENTRY_CFI(memset)
> > lsr.f lp_count, r2, 5 ;Last remaining max 124 bytes
> > lpnz .Lset32bytes
> > ;; LOOP START
> > - prefetchw [r3, 32] ;Prefetch the next write location
>
> So the real fix for issue at hand is this line. I'll keep this for the fix and
> beef up the changelog. Thing is existing code was already skipping the last 64B
> from the main loop (thus avoided prefetching the next line), but then reintroduced
> prefetchw is last 32B loop, spoiling the party. That prefetchw was pointless anyways
>
> -Vineet
--
Eugeniy Paltsev
WARNING: multiple messages have this Message-ID (diff)
From: Eugeniy Paltsev <eugeniy.paltsev@synopsys.com>
To: Vineet Gupta <vineet.gupta1@synopsys.com>,
"linux-snps-arc@lists.infradead.org"
<linux-snps-arc@lists.infradead.org>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"Alexey Brodkin" <alexey.brodkin@synopsys.com>
Subject: Re: [PATCH 1/2] ARCv2: LIB: memeset: fix doing prefetchw outside of buffer
Date: Tue, 15 Jan 2019 12:00:04 +0000 [thread overview]
Message-ID: <1547553603.24248.49.camel@synopsys.com> (raw)
In-Reply-To: <C2D7FE5348E1B147BCA15975FBA2307501464272C9@US01WEMBX2.internal.synopsys.com>
Hi Vineet,
On Tue, 2019-01-15 at 01:09 +0000, Vineet Gupta wrote:
> On 1/14/19 7:17 AM, Eugeniy Paltsev wrote:
> > Current ARCv2 memeset implementation may call 'prefetchw'
> > instruction for address which lies outside of memset area.
> > So we got one modified (dirty) cache line outside of memset
> > area. This may lead to data corruption if this area is used
> > for DMA IO.
> >
> > Another issue is that current ARCv2 memeset implementation
> > may call 'prealloc' instruction for L1 cache line which
> > doesn't fully belongs to memeset area in case of 128B L1 D$
> > line length. That leads to data corruption.
> >
> >
* Fix prefetchw/prealloc instructions using in case of 64B L1 data
> > cache line (default case) and don't use prefetch* instructions
> > for other possible L1 data cache line lengths (32B and 128B).
> >
> > Signed-off-by: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
> > ---
> > arch/arc/lib/memset-archs.S | 30 +++++++++++++++++++++++-------
> > 1 file changed, 23 insertions(+), 7 deletions(-)
> >
> > diff --git a/arch/arc/lib/memset-archs.S b/arch/arc/lib/memset-archs.S
> > index 62ad4bcb841a..c7717832336f 100644
> > --- a/arch/arc/lib/memset-archs.S
> > +++ b/arch/arc/lib/memset-archs.S
> > @@ -7,11 +7,32 @@
> > */
> >
> > #include <linux/linkage.h>
> > +#include <asm/cache.h>
> >
> > #undef PREALLOC_NOT_AVAIL
> >
> > +/*
> > + * The memset implementation below is optimized to use prefetchw and prealloc
> > + * instruction in case of CPU with 64B L1 data cache line (L1_CACHE_SHIFT == 6)
> > + * If you want to implement optimized memset for other possible L1 data cache
> > + * line lengths (32B and 128B) you should rewrite code carefully checking
> > + * we don't call any prefetchw/prealloc instruction for L1 cache lines which
> > + * don't belongs to memset area.
>
> Good point. FWIW, it is possible to support those non common line lengths by using
> L1_CACHE_SHIFT etc in asm code below but I agree its not worth the trouble.
>
> > + */
> > +#if L1_CACHE_SHIFT!=6
> > +# define PREALLOC_INSTR(...)
> > +# define PREFETCHW_INSTR(...)
> > +#else /* L1_CACHE_SHIFT!=6 */
> > +# define PREFETCHW_INSTR(...) prefetchw __VA_ARGS__
> > +# ifdef PREALLOC_NOT_AVAIL
> > +# define PREALLOC_INSTR(...) prefetchw __VA_ARGS__
> > +# else
> > +# define PREALLOC_INSTR(...) prealloc __VA_ARGS__
> > +# endif
> > +#endif /* L1_CACHE_SHIFT!=6 */
> > +
> > ENTRY_CFI(memset)
> > - prefetchw [r0] ; Prefetch the write location
> > + PREFETCHW_INSTR([r0]) ; Prefetch the first write location
> > mov.f 0, r2
> > ;;; if size is zero
> > jz.d [blink]
> > @@ -48,11 +69,7 @@ ENTRY_CFI(memset)
> >
> > lpnz @.Lset64bytes
> > ;; LOOP START
> > -#ifdef PREALLOC_NOT_AVAIL
> > - prefetchw [r3, 64] ;Prefetch the next write location
> > -#else
> > - prealloc [r3, 64]
> > -#endif
> > + PREALLOC_INSTR([r3, 64]) ;Prefetch the next write location
>
> These are not solving the issue - I'd break this up and move these bits to your
> next patch.
Actually these are solving another issue - current implementation may call
'prealloc' instruction for L1 cache line which doesn't fully belongs to
memeset area in case of 128B L1 D$ line length. As the 'prealloc' fill cache line
with zeros this leads to data corruption.
So I would better keep these changes in this 'fix' patch.
BTW, I've forgot again to add Cc: stable@vger.kernel.org, could you add it for me,
when applying patch?
Thanks.
> > #ifdef CONFIG_ARC_HAS_LL64
> > std.ab r4, [r3, 8]
> > std.ab r4, [r3, 8]
> > @@ -85,7 +102,6 @@ ENTRY_CFI(memset)
> > lsr.f lp_count, r2, 5 ;Last remaining max 124 bytes
> > lpnz .Lset32bytes
> > ;; LOOP START
> > - prefetchw [r3, 32] ;Prefetch the next write location
>
> So the real fix for issue at hand is this line. I'll keep this for the fix and
> beef up the changelog. Thing is existing code was already skipping the last 64B
> from the main loop (thus avoided prefetching the next line), but then reintroduced
> prefetchw is last 32B loop, spoiling the party. That prefetchw was pointless anyways
>
> -Vineet
--
Eugeniy Paltsev
next prev parent reply other threads:[~2019-01-15 12:00 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-14 15:16 [PATCH 1/2] ARCv2: LIB: memeset: fix doing prefetchw outside of buffer Eugeniy Paltsev
2019-01-14 15:16 ` Eugeniy Paltsev
2019-01-14 15:16 ` [PATCH 2/2] ARCv2: LIB: memset: move st instruction series to macros Eugeniy Paltsev
2019-01-14 15:16 ` Eugeniy Paltsev
2019-01-15 1:09 ` [PATCH 1/2] ARCv2: LIB: memeset: fix doing prefetchw outside of buffer Vineet Gupta
2019-01-15 1:09 ` Vineet Gupta
2019-01-15 12:00 ` Eugeniy Paltsev [this message]
2019-01-15 12:00 ` Eugeniy Paltsev
2019-01-15 16:58 ` Vineet Gupta
2019-01-15 16:58 ` Vineet Gupta
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=1547553603.24248.49.camel@synopsys.com \
--to=eugeniy.paltsev@synopsys.com \
--cc=linux-snps-arc@lists.infradead.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 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.