* cache coherence problem
@ 2003-11-18 1:16 Juergen Kienhoefer
2003-11-18 2:36 ` Matt Porter
` (2 more replies)
0 siblings, 3 replies; 12+ messages in thread
From: Juergen Kienhoefer @ 2003-11-18 1:16 UTC (permalink / raw)
To: linuxppc-dev
[-- Attachment #1: Type: text/plain, Size: 327 bytes --]
Guys,
Look at the folloging test program. It mmaps memory, puts some
instructions in it and executes it.
Sometimes it works, sometimes it crashes with illegal instruction.
This smells like cache problems.
Should the kernel clean the instruction cache for these addresses
in mmap system call?
Thanks for any thoughts!
Juergen
[-- Attachment #2: mmapexec.c --]
[-- Type: text/plain, Size: 1659 bytes --]
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/ptrace.h>
#include <signal.h>
#include <sched.h>
int main( int argc, char** argv ) {
long* loc;
int (*init)(void);
long returninstr0 = 0x38600009; // li r3,4
long returninstr1 = 0x4e800020; // blr
int returned;
int fd;
char zero;
char tempfile[20] = { "test-XXXXXX" };
printf( "start of test\n" );
fd = mkstemp( tempfile );
printf( "memfile name test-XXXXXX, fd %d\n", fd );
if (fchmod(fd, 0777) < 0){
perror("fchmod");
exit(1);
}
if( lseek( fd, 32768*1024, SEEK_SET ) != 32768*1024 ){ //32M
perror("lseek");
exit(1);
}
zero = 0;
if( write(fd, &zero, 1 ) != 1){
perror("write");
exit(1);
}
if( fcntl(fd, F_SETFD, FD_CLOEXEC ) != 0)
perror("Setting FD_CLOEXEC failed");
loc = mmap( (void *) 0x62800000, 12288,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_SHARED | MAP_FIXED,
fd, 0x3a0000);
printf( "memory allocated at addr %x\n", loc );
printf( "data %x\n", loc[0] );
// random behaviour, sometimes works, sometimes illegal instruction
//loc = (long*)0x62800000; // set the address
loc = (long*)0x62802000; // set the address
// always illegal instruction
//loc = (long*)0x62801b44;
//loc = (long*)0x62801000;
loc[0] = returninstr0;
loc[1] = returninstr1;
printf( "new code: addr %x, %x\n", loc, loc[0] );
init = (int (*)(void))loc;
returned = init();
printf( "function returned %d\n", returned );
}
^ permalink raw reply [flat|nested] 12+ messages in thread* Re: cache coherence problem
2003-11-18 1:16 cache coherence problem Juergen Kienhoefer
@ 2003-11-18 2:36 ` Matt Porter
2003-11-18 2:38 ` Michael R. Zucca
2003-11-18 3:02 ` Benjamin Herrenschmidt
2 siblings, 0 replies; 12+ messages in thread
From: Matt Porter @ 2003-11-18 2:36 UTC (permalink / raw)
To: Juergen Kienhoefer; +Cc: linuxppc-dev
On Mon, Nov 17, 2003 at 05:16:24PM -0800, Juergen Kienhoefer wrote:
> Guys,
> Look at the folloging test program. It mmaps memory, puts some
> instructions in it and executes it.
> Sometimes it works, sometimes it crashes with illegal instruction.
> This smells like cache problems.
> Should the kernel clean the instruction cache for these addresses
> in mmap system call?
What processor is this on?
-Matt
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: cache coherence problem
2003-11-18 1:16 cache coherence problem Juergen Kienhoefer
2003-11-18 2:36 ` Matt Porter
@ 2003-11-18 2:38 ` Michael R. Zucca
2003-11-19 22:35 ` linas
2003-11-18 3:02 ` Benjamin Herrenschmidt
2 siblings, 1 reply; 12+ messages in thread
From: Michael R. Zucca @ 2003-11-18 2:38 UTC (permalink / raw)
To: Juergen Kienhoefer; +Cc: linuxppc-dev
On Monday, November 17, 2003, at 08:16 PM, Juergen Kienhoefer wrote:
> Guys,
> Look at the folloging test program. It mmaps memory, puts some
> instructions in it and executes it.
> Sometimes it works, sometimes it crashes with illegal instruction.
> This smells like cache problems.
> Should the kernel clean the instruction cache for these addresses
> in mmap system call?
> Thanks for any thoughts!
I don't think this is the kernel's responsibility. I think this is just
life with PPCs. If you dump random instructions into random memory, you
have to make sure everything is flushed and the icache is invalidated
before executing the code. You have to do the same thing when you do
self-modifying code.
Why would you want to do this, anyway?
--
----------------------------------------------
Michael Zucca - mrz5149@acm.org
----------------------------------------------
"I'm too old to use Emacs." -- Rod MacDonald
----------------------------------------------
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: cache coherence problem
2003-11-18 2:38 ` Michael R. Zucca
@ 2003-11-19 22:35 ` linas
0 siblings, 0 replies; 12+ messages in thread
From: linas @ 2003-11-19 22:35 UTC (permalink / raw)
To: Michael R. Zucca; +Cc: Juergen Kienhoefer, linuxppc-dev
On Mon, Nov 17, 2003 at 09:38:32PM -0500, Michael R. Zucca wrote:
>
> have to make sure everything is flushed and the icache is invalidated
> before executing the code. You have to do the same thing when you do
> self-modifying code.
>
> Why would you want to do this, anyway?
I don't know what he's doing, but sometimes one has static if tests
inside of tight inner loops, but you don't know (at compile time)
what the compare op will be until runtime. One way to solve this
is build an optimized loop for each possible compare op.
This optimization strategy fails if you have more than a handful of
if-tests, cause then you have a combinatorial explosion (i.e. dozens
of nearly identical .o files eating up megabytes with nearly identical
code). So one way around this to have a few basic loops, and patch
them up at run time.
--linas
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: cache coherence problem
2003-11-18 1:16 cache coherence problem Juergen Kienhoefer
2003-11-18 2:36 ` Matt Porter
2003-11-18 2:38 ` Michael R. Zucca
@ 2003-11-18 3:02 ` Benjamin Herrenschmidt
2003-11-18 5:00 ` Juergen Kienhoefer
2003-11-18 9:37 ` Gabriel Paubert
2 siblings, 2 replies; 12+ messages in thread
From: Benjamin Herrenschmidt @ 2003-11-18 3:02 UTC (permalink / raw)
To: Juergen Kienhoefer; +Cc: linuxppc-dev list
On Tue, 2003-11-18 at 12:16, Juergen Kienhoefer wrote:
> Guys,
> Look at the folloging test program. It mmaps memory, puts some
> instructions in it and executes it.
> Sometimes it works, sometimes it crashes with illegal instruction.
> This smells like cache problems.
> Should the kernel clean the instruction cache for these addresses
> in mmap system call?
> Thanks for any thoughts!
> Juergen
It's your duty to ensure cache coherency. Actually, the kernel
will eventually clean the icache for newly mapped in blank pages,
but it will certainly not enforce flush of your writes to memory.
You need to first flush the data cache to memory using dcbf or
dcbst, then sync for this to complete, then invalidate the
instruction cache, sync and isync.
Ben.
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: cache coherence problem
2003-11-18 3:02 ` Benjamin Herrenschmidt
@ 2003-11-18 5:00 ` Juergen Kienhoefer
2003-11-18 5:22 ` Benjamin Herrenschmidt
2003-11-18 8:04 ` cache coherence problem Wolfgang Denk
2003-11-18 9:37 ` Gabriel Paubert
1 sibling, 2 replies; 12+ messages in thread
From: Juergen Kienhoefer @ 2003-11-18 5:00 UTC (permalink / raw)
To: linuxppc-dev list
Guys,
Thank you very much for the ideas.
Basically, what I need to do is:
dcbst
sync
iccci
sync
isync
for every 32 bytes of the memory block I put code in.
Right?
Cheers
Juergen
Benjamin Herrenschmidt wrote:
>On Tue, 2003-11-18 at 12:16, Juergen Kienhoefer wrote:
>
>
>>Guys,
>>Look at the folloging test program. It mmaps memory, puts some
>>instructions in it and executes it.
>>Sometimes it works, sometimes it crashes with illegal instruction.
>>This smells like cache problems.
>>Should the kernel clean the instruction cache for these addresses
>>in mmap system call?
>>Thanks for any thoughts!
>>Juergen
>>
>>
>
>It's your duty to ensure cache coherency. Actually, the kernel
>will eventually clean the icache for newly mapped in blank pages,
>but it will certainly not enforce flush of your writes to memory.
>
>You need to first flush the data cache to memory using dcbf or
>dcbst, then sync for this to complete, then invalidate the
>instruction cache, sync and isync.
>
>Ben.
>
>
>
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: cache coherence problem
2003-11-18 5:00 ` Juergen Kienhoefer
@ 2003-11-18 5:22 ` Benjamin Herrenschmidt
2003-11-18 5:24 ` Benjamin Herrenschmidt
2003-11-18 8:04 ` cache coherence problem Wolfgang Denk
1 sibling, 1 reply; 12+ messages in thread
From: Benjamin Herrenschmidt @ 2003-11-18 5:22 UTC (permalink / raw)
To: Juergen Kienhoefer; +Cc: linuxppc-dev list
On Tue, 2003-11-18 at 16:00, Juergen Kienhoefer wrote:
> Guys,
> Thank you very much for the ideas.
> Basically, what I need to do is:
> dcbst
> sync
> iccci
^^^^^^^
No: icbi
> sync
> isync
> for every 32 bytes of the memory block I put code in.
> Right?
> Cheers
> Juergen
>
>
> Benjamin Herrenschmidt wrote:
>
> >On Tue, 2003-11-18 at 12:16, Juergen Kienhoefer wrote:
> >
> >
> >>Guys,
> >>Look at the folloging test program. It mmaps memory, puts some
> >>instructions in it and executes it.
> >>Sometimes it works, sometimes it crashes with illegal instruction.
> >>This smells like cache problems.
> >>Should the kernel clean the instruction cache for these addresses
> >>in mmap system call?
> >>Thanks for any thoughts!
> >>Juergen
> >>
> >>
> >
> >It's your duty to ensure cache coherency. Actually, the kernel
> >will eventually clean the icache for newly mapped in blank pages,
> >but it will certainly not enforce flush of your writes to memory.
> >
> >You need to first flush the data cache to memory using dcbf or
> >dcbst, then sync for this to complete, then invalidate the
> >instruction cache, sync and isync.
> >
> >Ben.
> >
> >
> >
>
>
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: cache coherence problem
2003-11-18 5:22 ` Benjamin Herrenschmidt
@ 2003-11-18 5:24 ` Benjamin Herrenschmidt
2003-11-18 17:40 ` cache coherence problem: FIXED Juergen Kienhoefer
0 siblings, 1 reply; 12+ messages in thread
From: Benjamin Herrenschmidt @ 2003-11-18 5:24 UTC (permalink / raw)
To: Juergen Kienhoefer; +Cc: linuxppc-dev list
On Tue, 2003-11-18 at 16:22, Benjamin Herrenschmidt wrote:
> On Tue, 2003-11-18 at 16:00, Juergen Kienhoefer wrote:
> > Guys,
> > Thank you very much for the ideas.
> > Basically, what I need to do is:
> > dcbst
> > sync
> > iccci
> ^^^^^^^
> No: icbi
>
> > sync
> > isync
> > for every 32 bytes of the memory block I put code in.
Actually, to be precise, you need:
1) A loop of dcbst's over every cache line crossed by your code
2) one sync
3) A loop of icbi's over every cache line crossed by your code
4) one sync, one isync
Ben.
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: cache coherence problem: FIXED
2003-11-18 5:24 ` Benjamin Herrenschmidt
@ 2003-11-18 17:40 ` Juergen Kienhoefer
0 siblings, 0 replies; 12+ messages in thread
From: Juergen Kienhoefer @ 2003-11-18 17:40 UTC (permalink / raw)
To: linuxppc-dev list
[-- Attachment #1: Type: text/plain, Size: 733 bytes --]
Ben, et al.
Thanks, the test program is fixed and working.
I appreciate your help very much!!!
See attachment
Juergen
Benjamin Herrenschmidt wrote:
>On Tue, 2003-11-18 at 16:22, Benjamin Herrenschmidt wrote:
>
>
>>On Tue, 2003-11-18 at 16:00, Juergen Kienhoefer wrote:
>>
>>
>>>Guys,
>>>Thank you very much for the ideas.
>>>Basically, what I need to do is:
>>>dcbst
>>>sync
>>>iccci
>>>
>>>
>>^^^^^^^
>>No: icbi
>>
>>
>>
>>>sync
>>>isync
>>>for every 32 bytes of the memory block I put code in.
>>>
>>>
>
>Actually, to be precise, you need:
>
> 1) A loop of dcbst's over every cache line crossed by your code
> 2) one sync
> 3) A loop of icbi's over every cache line crossed by your code
> 4) one sync, one isync
>
>Ben.
>
>
>
[-- Attachment #2: mmapexec.c --]
[-- Type: text/plain, Size: 1882 bytes --]
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/ptrace.h>
#include <signal.h>
#include <sched.h>
static inline void clear_icache(void *p)
{
asm volatile ("dcbst 0,%0;
sync;
icbi 0,%0;
sync;
isync" : : "r" (p));
}
int main( int argc, char** argv ) {
long* loc;
int (*init)(void);
long returninstr0 = 0x38600009; // li r3,4
long returninstr1 = 0x4e800020; // blr
int returned;
int fd;
char zero;
char tempfile[20] = { "test-XXXXXX" };
printf( "start of test\n" );
fd = mkstemp( tempfile );
printf( "memfile name test-XXXXXX, fd %d\n", fd );
if (fchmod(fd, 0777) < 0){
perror("fchmod");
exit(1);
}
if( lseek( fd, 32768*1024, SEEK_SET ) != 32768*1024 ){ //32M
perror("lseek");
exit(1);
}
zero = 0;
if( write(fd, &zero, 1 ) != 1){
perror("write");
exit(1);
}
if( fcntl(fd, F_SETFD, FD_CLOEXEC ) != 0)
perror("Setting FD_CLOEXEC failed");
loc = mmap( (void *) 0x62800000, 12288,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_SHARED | MAP_FIXED,
fd, 0x3a0000);
printf( "memory allocated at addr %x\n", loc );
printf( "data %x\n", loc[0] );
// random behaviour, sometimes works, sometimes illegal instruction
//loc = (long*)0x62800000; // set the address
//loc = (long*)0x62802000; // set the address
// always illegal instruction
loc = (long*)0x62801b44;
//loc = (long*)0x62801000;
loc[0] = returninstr0;
loc[1] = returninstr1;
printf( "new code: addr %x, %x\n", loc, loc[0] );
clear_icache( loc );
init = (int (*)(void))loc;
returned = init();
printf( "function returned %d\n", returned );
}
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: cache coherence problem
2003-11-18 5:00 ` Juergen Kienhoefer
2003-11-18 5:22 ` Benjamin Herrenschmidt
@ 2003-11-18 8:04 ` Wolfgang Denk
1 sibling, 0 replies; 12+ messages in thread
From: Wolfgang Denk @ 2003-11-18 8:04 UTC (permalink / raw)
To: Juergen Kienhoefer; +Cc: linuxppc-dev list
In message <3FB9A77C.3030101@kienhoefer.com> you wrote:
>
> Basically, what I need to do is:
...
> for every 32 bytes of the memory block I put code in.
...
s/32/CACHE_LINE_SIZE/g
Best regards,
Wolfgang Denk
--
Software Engineering: Embedded and Realtime Systems, Embedded Linux
Phone: (+49)-8142-4596-87 Fax: (+49)-8142-4596-88 Email: wd@denx.de
Windows95 = graphical user interface for a single-threaded interrupt
handler (DOS)
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: cache coherence problem
2003-11-18 3:02 ` Benjamin Herrenschmidt
2003-11-18 5:00 ` Juergen Kienhoefer
@ 2003-11-18 9:37 ` Gabriel Paubert
2003-11-19 0:35 ` Benjamin Herrenschmidt
1 sibling, 1 reply; 12+ messages in thread
From: Gabriel Paubert @ 2003-11-18 9:37 UTC (permalink / raw)
To: Benjamin Herrenschmidt; +Cc: Juergen Kienhoefer, linuxppc-dev list
On Tue, Nov 18, 2003 at 02:02:28PM +1100, Benjamin Herrenschmidt wrote:
>
> On Tue, 2003-11-18 at 12:16, Juergen Kienhoefer wrote:
> > Guys,
> > Look at the folloging test program. It mmaps memory, puts some
> > instructions in it and executes it.
> > Sometimes it works, sometimes it crashes with illegal instruction.
> > This smells like cache problems.
> > Should the kernel clean the instruction cache for these addresses
> > in mmap system call?
> > Thanks for any thoughts!
> > Juergen
>
> It's your duty to ensure cache coherency. Actually, the kernel
> will eventually clean the icache for newly mapped in blank pages,
If you mean zeroed pages for blanked, I believe that it is wrong.
The reason being that 0 is an invalid instruction so that the code
would trap in any case.
Maybe I'm wrong, but I seem to remember this as an optimization
that Paulus implemented some time ago.
Gabriel
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: cache coherence problem
2003-11-18 9:37 ` Gabriel Paubert
@ 2003-11-19 0:35 ` Benjamin Herrenschmidt
0 siblings, 0 replies; 12+ messages in thread
From: Benjamin Herrenschmidt @ 2003-11-19 0:35 UTC (permalink / raw)
To: Gabriel Paubert; +Cc: Juergen Kienhoefer, linuxppc-dev list
> newly mapped in blank pages,
>
> If you mean zeroed pages for blanked, I believe that it is wrong.
> The reason being that 0 is an invalid instruction so that the code
> would trap in any case.
>
> Maybe I'm wrong, but I seem to remember this as an optimization
> that Paulus implemented some time ago.
And that we had to undo because glibc relied on it, not invalidating
cache lines in some conditions assuming newly mapped zeroed pages
are delivered icache-clean by the kernel.
One of the arguments of the glibc folks for not fixing that was that
it would be a security hole to let stale icache content leak, so the
kernel has to invalidate them anyway.
Ben.
** Sent via the linuxppc-dev mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2003-11-19 22:35 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-11-18 1:16 cache coherence problem Juergen Kienhoefer
2003-11-18 2:36 ` Matt Porter
2003-11-18 2:38 ` Michael R. Zucca
2003-11-19 22:35 ` linas
2003-11-18 3:02 ` Benjamin Herrenschmidt
2003-11-18 5:00 ` Juergen Kienhoefer
2003-11-18 5:22 ` Benjamin Herrenschmidt
2003-11-18 5:24 ` Benjamin Herrenschmidt
2003-11-18 17:40 ` cache coherence problem: FIXED Juergen Kienhoefer
2003-11-18 8:04 ` cache coherence problem Wolfgang Denk
2003-11-18 9:37 ` Gabriel Paubert
2003-11-19 0:35 ` Benjamin Herrenschmidt
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).