Skip to contentSkip to conversation

x86-32: clean up rwsem inline asm statements

DISCUSSION George Spelvin 3 participants 5 messages Jan 13, 2010 – Jan 14, 2010

lore
Share & tools
lore
#0 lore raw
Quoted 9 lines

As far as I can tell, very few of these assembly statements actually need a size at all -- the very first inc statement is purely to memory, and as such it needs a size marker, but almost any operation which is register-register or register-memory will simply take its size from the register operand. For those, it seems cleaner to simply drop the size suffix, and in fact this is the style we have been pushing people towards (use the suffix where mandatory or where the size is fixed anyway, to help catch bugs; use no suffix where the size can vary and is implied by the operands.)

The one thing is that for a register-memory operation, using the size of the memory operand can catch bugs where it doesn't match the size of the register operand.

GCC's inline asm doesn't make operand size very implicit, and it's awkward to cast output operands, so there's a potential for bugs. I especially get nervous when the operand itself is an immediate constant, as I can't remember the ANSI rules for the type very well. (Quick: is 0x80000000 an unsigned 32-bit int or a signed 64-bit one? What about 2147483648 or 1<<31?)

Since this is mostly inline functions, it's not so big a problem, but I'd consider something like:

static inline void __up_write(struct rw_semaphore *sem)

{
       unsigned long tmp;
	asm volatile("# beginning __up_write\n\t"
		     LOCK_PREFIX "  xadd%z0	%1,(%2)\n\t"
		     /* tries to transition
			0xffff0001 -> 0x00000000 */
		     "  jz	1f\n"
		     "  call call_rwsem_wake\n"
		     "1:\n\t"
		     "# ending __up_write\n"
       	     : "+m" (sem->count), "=d" (tmp)
       	     : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS)
       	     : "memory", "cc");
}

Just in case the size of -RWSEM_ACTIVE_WRITE_BIAS doesn't match that of sem->count. It'll explode when you try to run it, of course, but there's something to be said for compile-time errors.

#1 lore raw
Quoted 40 lines · George Spelvin
Quoted 9 lines

As far as I can tell, very few of these assembly statements actually need a size at all -- the very first inc statement is purely to memory, and as such it needs a size marker, but almost any operation which is register-register or register-memory will simply take its size from the register operand. For those, it seems cleaner to simply drop the size suffix, and in fact this is the style we have been pushing people towards (use the suffix where mandatory or where the size is fixed anyway, to help catch bugs; use no suffix where the size can vary and is implied by the operands.)

The one thing is that for a register-memory operation, using the size of the memory operand can catch bugs where it doesn't match the size of the register operand.

GCC's inline asm doesn't make operand size very implicit, and it's awkward to cast output operands, so there's a potential for bugs. I especially get nervous when the operand itself is an immediate constant, as I can't remember the ANSI rules for the type very well. (Quick: is 0x80000000 an unsigned 32-bit int or a signed 64-bit one? What about 2147483648 or 1<<31?)

Since this is mostly inline functions, it's not so big a problem, but I'd consider something like:

static inline void __up_write(struct rw_semaphore *sem)

{
       unsigned long tmp;
	asm volatile("# beginning __up_write\n\t"
		     LOCK_PREFIX "  xadd%z0	%1,(%2)\n\t"
		     /* tries to transition
			0xffff0001 -> 0x00000000 */
		     "  jz	1f\n"
		     "  call call_rwsem_wake\n"
		     "1:\n\t"
		     "# ending __up_write\n"
       	     : "+m" (sem->count), "=d" (tmp)
       	     : "a" (sem), "1" (-RWSEM_ACTIVE_WRITE_BIAS)
       	     : "memory", "cc");
}
Quoted 3 lines

Just in case the size of -RWSEM_ACTIVE_WRITE_BIAS doesn't match that of sem->count. It'll explode when you try to run it, of course, but there's something to be said for compile-time errors.

There are a number of things that can be done better... for one thing, "+m" (sem->count) and "a" (sem) is just bloody wrong. The right thing would be "a" (&sem->count) for proper robustness.

There is no real point in being concerned about the type of immediates, because the immediate type isn't really used... it shows up as a literal in the assembly language. However, if you're really concerned, the right thing to do is to do a cast in C, not playing games with the assembly.

-hpa

#2 lore raw
Quoted 3 lines

There are a number of things that can be done better... for one thing, "+m" (sem->count) and "a" (sem) is just bloody wrong. The right thing would be "a" (&sem->count) for proper robustness.

Actually, no. The "+m" (sem->count) is telling GCC that sem->count is updated; "a" (&sem->count) does *not* tell it to invalidate cached copies of sem->count that it may have lying around.

However, we can't just use "+m" (sem->count) because GCC has a poor grasp on the concept of atomic operations; as far as it is concerned, it is exactly equivalent to copying the value into a stack slot, do the operation there, and copy it back.

(It's much more likely to do that with "g" operand constraints, but it has been known to point to a stack copy that it's made for some other reason.)

The current situation is, as far as I can remember the previous discussion on the subject, the simplest way to explain to GCC just what it needs to do.

Quoted 4 lines

There is no real point in being concerned about the type of immediates, because the immediate type isn't really used... it shows up as a literal in the assembly language. However, if you're really concerned, the right thing to do is to do a cast in C, not playing games with the assembly.

Ah, right, sorry; I remembered having this problem, but it was with register operands.

#3 lore raw
Quoted 4 lines · George Spelvin
Quoted 3 lines

There are a number of things that can be done better... for one thing, "+m" (sem->count) and "a" (sem) is just bloody wrong. The right thing would be "a" (&sem->count) for proper robustness.

Quoted 8 lines

Actually, no. The "+m" (sem->count) is telling GCC that sem->count is updated; "a" (&sem->count) does *not* tell it to invalidate cached copies of sem->count that it may have lying around.

However, we can't just use "+m" (sem->count) because GCC has a poor grasp on the concept of atomic operations; as far as it is concerned, it is exactly equivalent to copying the value into a stack slot, do the operation there, and copy it back.

You completely missed the point.

The reason we need both "+m" (sem->count) and "a" (sem) is because we need the address of the semaphore in %eax in case we hit the slow path.
They're actually orthogonal requirements, and we could implement them as:

	LOCK_PREFIX xadd %1,%0
	jz 1f
	call call_rwsem_wake

1:

	: "+m" (sem->count), "=d" (tmp)
       	: "a" (sem)

... just fine, and arguably that would be more correct in the sense that if sem->count isn't the first member of *sem, then we still pass the address of sem in %eax to call_rwsem_wake. In practice, with the zero-offset sem->count, gcc will typically generate (%eax) as the argument since it has it available anyway, but it wouldn't have to.

The code as it currently is:

	LOCK_PREFIX xadd %1,(%2)
	jz 1f
	call call_rwsem_wake

1:

	: "+m" (sem->count), "=d" (tmp)
       	: "a" (sem)

... implicitly assumes the offset of "count" is zero but doesn't enforce it in any way. My comment was that since the assumption is clearly that arguments 0 and 2 point to the same memory object, it should be (&sem->count) in the second case, but that's actually not really the "proper" fix because call_rwsem_wake presumably expects the value of sem.

-hpa

#4 lore raw
In reply to H. Peter Anvin
Quoted 4 lines · H. Peter Anvin

There are a number of things that can be done better... for one thing, "+m" (sem->count) and "a" (sem) is just bloody wrong. The right thing would be "a" (&sem->count) for proper robustness.

Actually, no.

Strictly speaking, we should use "a" (sem), and then use '%0' (pointing to the "+m" (sem->count)) for the actual memory access in the inline asm, rather than '(%1)'.

We do need %eax to contain the pointer to the semaphore, because _that_ is what we pass in as an argument (and return as a value!) to the rwsem slow paths.

So "a" (sem) is absolutely the right thing to do.

The reason we use "(%1)" rather than "%0" is - if I recall correctly - that back when we inlined it, gcc would often stupidly use the original value of the semaphore address rather than %eax (which obviously also contained it), and it generated larger code with big constants etc.

Now, since we only inline it in one place anyway, and the semaphore is always an argument to that inlining site anyway, I don't think it matters (it's never going to be some global pointer), and we probably could/should just do this right. So instead of

	LOCK_PREFIX "  inc%z0      (%1)\n\t"

we'd probably be better off with just

	LOCK_PREFIX "  inc%z0 %0\n\t"

instead, letting gcc generate the memory pointer to "sem->count" itself.

Linus