Re: [PATCH] Runtime memory barrier patching

Linus Torvalds
2003-04-21T19:47:25Z

On Mon, 21 Apr 2003, Andi Kleen wrote:
>
> This patch implements automatic code patching of memory barriers based
> on the CPU capabilities. Normally lock ; addl $0,(%esp) barriers
> are used, but these are a bit slow on the Pentium 4.

Could you fix this part:

+ /* fill the overlap with single byte nops */
+ memset(a->instr + a->replacementlen, 0x90,
+ a->instrlen - a->replacementlen);

to use an array of replacements, something like

#define MAXSIZE 6

char *nop_sizes[MAXSIZE] = {
NULL, // "zero sized nop"? I don't think so
{ 0x90 }, // simple one-byte no-op.
{ .. whatever the two-byte NOP is .. }
...
};

and then have something like

replace = a->instrlen - a->replacementlen;
nop = a->instr + a->replacementlen;
while (replace) {
int size = replace;
if (size > MAXSIZE)
size = MAXSIZE;
memcpy(nop, nop_sizes + size, size);
nop += size;
replace -= size;
}

instead? I think it's silly to have multiple single-byte nops, when there
are well-defined multi-byte nops available.

Other than that this looks pretty good.

Linus