Skip to contentSkip to conversation

powerpc: Ensure gcc doesn't move around cache flushing in __patch_instruction

PATCH In review Benjamin Herrenschmidt 3 participants 5 messages May 17, 2018 – Jan 31, 2019 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc: Ensure gcc doesn't move around cache flushing in __patch_instruction

The current asm statement in __patch_instruction() for the cache flushes doesn't have a "volatile" statement and no memory clobber. That means gcc can potentially move it around (or move the store done by put_user past the flush).

Add both to ensure gcc doesn't play games.

Found by code inspection, no actual bug reported.

---

arch/powerpc/lib/code-patching.c +3 -2
@@ -32,8 +32,9 @@ static int __patch_instruction(unsigned int *exec_addr, unsigned int instr,
        if (err)
                return err;
 
-       asm ("dcbst 0, %0; sync; icbi 0,%1; sync; isync" :: "r" (patch_addr),
-                                                           "r" (exec_addr));
+       asm volatile("dcbst 0, %0; sync; icbi 0,%1; sync; isync"
+                    :: "r" (patch_addr), "r" (exec_addr)
+                    : "memory");
 
        return 0;
 }
#1 lore raw

Hi!

Quoted 4 lines · Benjamin Herrenschmidt

The current asm statement in __patch_instruction() for the cache flushes doesn't have a "volatile" statement and no memory clobber. That means gcc can potentially move it around (or move the store done by put_user past the flush).

volatile is completely superfluous here, except maybe as documentation:
any asm without outputs is always volatile.

(And the memory clobber does not prevent the compiler from moving the asm around, or duplicating it, etc., and neither does the volatile).

Segher

#2 lore raw
Quoted 10 lines · Segher Boessenkool

Hi!

Quoted 4 lines · Benjamin Herrenschmidt

The current asm statement in __patch_instruction() for the cache flushes doesn't have a "volatile" statement and no memory clobber. That means gcc can potentially move it around (or move the store done by put_user past the flush).

volatile is completely superfluous here, except maybe as documentation:
any asm without outputs is always volatile.

I wasn't aware of that. I was drilled early on to always stick volatile in my asm statements if they have any form of side effect :-)

Quoted 2 lines

(And the memory clobber does not prevent the compiler from moving the asm around, or duplicating it, etc., and neither does the volatile).

It prevents load/stores from moving around doesn't it ? I wanted to make sure the store of the instruction doesn't move in/pass the asm. If you say that's not needed then ignore the patch.

Cheers,
Ben.

Quoted 2 lines

Segher

#3 lore raw
Quoted 7 lines · Benjamin Herrenschmidt
Quoted 6 lines · Segher Boessenkool
Quoted 4 lines · Benjamin Herrenschmidt

The current asm statement in __patch_instruction() for the cache flushes doesn't have a "volatile" statement and no memory clobber. That means gcc can potentially move it around (or move the store done by put_user past the flush).

Quoted 5 lines
Quoted 2 lines

volatile is completely superfluous here, except maybe as documentation:
any asm without outputs is always volatile.

I wasn't aware of that. I was drilled early on to always stick volatile in my asm statements if they have any form of side effect :-)

If an asm without output was not marked automatically as having another side effect, every such asm would be immediately deleted ;-)

Adding volatile as documentation for side effects can be good; it just doesn't do much (nothing, in fact) for asms without output as far as the compiler is concerned.

Quoted 6 lines
Quoted 2 lines

(And the memory clobber does not prevent the compiler from moving the asm around, or duplicating it, etc., and neither does the volatile).

It prevents load/stores from moving around doesn't it ? I wanted to make sure the store of the instruction doesn't move in/pass the asm. If you say that's not needed then ignore the patch.

No, it's fine here, and you want either that or put exactly the memory you are touching in a constraint (probably overkill here). I just wanted to say that a "memory" clobber does nothing more than say the asm touches some unspecified memory; there is no magic other meaning to it. Your patch is correct, just the "volatile" part isn't needed, and the explanation was a bit cargo-culty ;-)

Segher

#4 lore raw

Le 18/05/2018 à 01:00, Segher Boessenkool a écrit :

Quoted 28 lines · Benjamin Herrenschmidt
Quoted 7 lines · Segher Boessenkool
Quoted 6 lines · Benjamin Herrenschmidt
Quoted 4 lines

The current asm statement in __patch_instruction() for the cache flushes doesn't have a "volatile" statement and no memory clobber. That means gcc can potentially move it around (or move the store done by put_user past the flush).

Quoted 5 lines
Quoted 2 lines

volatile is completely superfluous here, except maybe as documentation:
any asm without outputs is always volatile.

I wasn't aware of that. I was drilled early on to always stick volatile in my asm statements if they have any form of side effect :-)

If an asm without output was not marked automatically as having another side effect, every such asm would be immediately deleted ;-)

Adding volatile as documentation for side effects can be good; it just doesn't do much (nothing, in fact) for asms without output as far as the compiler is concerned.

Quoted 6 lines
Quoted 2 lines

(And the memory clobber does not prevent the compiler from moving the asm around, or duplicating it, etc., and neither does the volatile).

It prevents load/stores from moving around doesn't it ? I wanted to make sure the store of the instruction doesn't move in/pass the asm. If you say that's not needed then ignore the patch.

Quoted 7 lines

No, it's fine here, and you want either that or put exactly the memory you are touching in a constraint (probably overkill here). I just wanted to say that a "memory" clobber does nothing more than say the asm touches some unspecified memory; there is no magic other meaning to it. Your patch is correct, just the "volatile" part isn't needed, and the explanation was a bit cargo-culty ;-)

Any plan to get that merged ?

Christophe