Re: Cache flushing...

Linus Torvalds (
Sun, 2 Jul 1995 21:40:03 +0300

Tim Olson: "Re: Cache flushing..." (Jul 2, 9:22):
> The PowerPC architecture is defined so that implementations which have
> split I/D caches don't need to maintain instruction cache coherency in
> hardware (the icache doesn't have to snoop bus transactions, nor does
> it have to watch data transactions on its own processor). This means
> that I/D coherency must be maintained by software. Anytime
> instructions are "generated" (fork, exec) the icache must be made coherent.

I have a different approach to this: instead of flushing the cache every
time we map in a new page in a process (to make sure that there is no
invalid icache associated with that page), I'd go for the "reverse"
setup: make sure that all pages on the free list have no icache
associated with them.

If the free pages have no icache in them, we can safely do a
fork/exec/whetever, as any new pages given to a process will be
guaranteed to not have stale icache data. Note that it's not the
fork/exec system calls per se that are problematic for icache: any
operation that maps in pages into user space would otherwise have to
make sure that any new pages loaded aren't "tainted" by icache.

So, easy way to handle icache invalidation:
- when a process is unmapped (free_page_tables() or
clear_page_tables()), invalidate the whole icache for that process.
- when any page is free'd from a process address space, invalidate the
icache for that page (this happens either when a memory map is
closed down or when a page is swapped out).

The reason we do it this way is that neither the process unmap or the
page unmap operation is very timing critical (the former happens just
once in the lifetime of a program, and the latter happens mainly when
you're low on memory and you need to swap, in whcih case other things
will make the machine slow anyway). So if flushing the icache is a
costly operation (on the alpha, for example, it's an all-or-nothing
operation), this is the better approach (instead of flushing the icache
every time you map in a new page).