Skip to contentSkip to conversation

locking fix

GIT PULL Ingo Molnar 3 participants 10 messages Oct 26, 2013 – Oct 28, 2013

lore
Share & tools
lore
#0 lore raw

Linus,

Please pull the latest core-urgent-for-linus git tree from:

git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git core-urgent-for-linus

# HEAD: b0267507dfd0187fb7840a0ec461a510a7f041c5 mutex: Avoid gcc version dependent __builtin_constant_p() usage

This tree fixes a boot crash in CONFIG_DEBUG_MUTEXES=y kernels, on kernels built with GCC 3.x. (There are still such distros.)

Thanks,

Ingo

------------------>
Tetsuo Handa (1):
mutex: Avoid gcc version dependent __builtin_constant_p() usage

 kernel/mutex.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
kernel/mutex.c +16 -16
index 6d647ae..d24105b 100644
@@ -410,7 +410,7 @@ ww_mutex_set_context_fastpath(struct ww_mutex *lock,
 static __always_inline int __sched
 __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
 		    struct lockdep_map *nest_lock, unsigned long ip,
-		    struct ww_acquire_ctx *ww_ctx)
+		    struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
 {
 	struct task_struct *task = current;
 	struct mutex_waiter waiter;
@@ -450,7 +450,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
 		struct task_struct *owner;
 		struct mspin_node  node;
 
-		if (!__builtin_constant_p(ww_ctx == NULL) && ww_ctx->acquired > 0) {
+		if (use_ww_ctx && ww_ctx->acquired > 0) {
 			struct ww_mutex *ww;
 
 			ww = container_of(lock, struct ww_mutex, base);
@@ -480,7 +480,7 @@ __mutex_lock_common(struct mutex *lock, long state, unsigned int subclass,
 		if ((atomic_read(&lock->count) == 1) &&
 		    (atomic_cmpxchg(&lock->count, 1, 0) == 1)) {
 			lock_acquired(&lock->dep_map, ip);
-			if (!__builtin_constant_p(ww_ctx == NULL)) {
+			if (use_ww_ctx) {
 				struct ww_mutex *ww;
 				ww = container_of(lock, struct ww_mutex, base);
 
@@ -551,7 +551,7 @@ slowpath:
 			goto err;
 		}
 
-		if (!__builtin_constant_p(ww_ctx == NULL) && ww_ctx->acquired > 0) {
+		if (use_ww_ctx && ww_ctx->acquired > 0) {
 			ret = __mutex_lock_check_stamp(lock, ww_ctx);
 			if (ret)
 				goto err;
@@ -575,7 +575,7 @@ skip_wait:
 	lock_acquired(&lock->dep_map, ip);
 	mutex_set_owner(lock);
 
-	if (!__builtin_constant_p(ww_ctx == NULL)) {
+	if (use_ww_ctx) {
 		struct ww_mutex *ww = container_of(lock, struct ww_mutex, base);
 		struct mutex_waiter *cur;
 
@@ -615,7 +615,7 @@ mutex_lock_nested(struct mutex *lock, unsigned int subclass)
 {
 	might_sleep();
 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
-			    subclass, NULL, _RET_IP_, NULL);
+			    subclass, NULL, _RET_IP_, NULL, 0);
 }
 
 EXPORT_SYMBOL_GPL(mutex_lock_nested);
@@ -625,7 +625,7 @@ _mutex_lock_nest_lock(struct mutex *lock, struct lockdep_map *nest)
 {
 	might_sleep();
 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE,
-			    0, nest, _RET_IP_, NULL);
+			    0, nest, _RET_IP_, NULL, 0);
 }
 
 EXPORT_SYMBOL_GPL(_mutex_lock_nest_lock);
@@ -635,7 +635,7 @@ mutex_lock_killable_nested(struct mutex *lock, unsigned int subclass)
 {
 	might_sleep();
 	return __mutex_lock_common(lock, TASK_KILLABLE,
-				   subclass, NULL, _RET_IP_, NULL);
+				   subclass, NULL, _RET_IP_, NULL, 0);
 }
 EXPORT_SYMBOL_GPL(mutex_lock_killable_nested);
 
@@ -644,7 +644,7 @@ mutex_lock_interruptible_nested(struct mutex *lock, unsigned int subclass)
 {
 	might_sleep();
 	return __mutex_lock_common(lock, TASK_INTERRUPTIBLE,
-				   subclass, NULL, _RET_IP_, NULL);
+				   subclass, NULL, _RET_IP_, NULL, 0);
 }
 
 EXPORT_SYMBOL_GPL(mutex_lock_interruptible_nested);
@@ -682,7 +682,7 @@ __ww_mutex_lock(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
 
 	might_sleep();
 	ret =  __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE,
-				   0, &ctx->dep_map, _RET_IP_, ctx);
+				   0, &ctx->dep_map, _RET_IP_, ctx, 1);
 	if (!ret && ctx->acquired > 1)
 		return ww_mutex_deadlock_injection(lock, ctx);
 
@@ -697,7 +697,7 @@ __ww_mutex_lock_interruptible(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
 
 	might_sleep();
 	ret = __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE,
-				  0, &ctx->dep_map, _RET_IP_, ctx);
+				  0, &ctx->dep_map, _RET_IP_, ctx, 1);
 
 	if (!ret && ctx->acquired > 1)
 		return ww_mutex_deadlock_injection(lock, ctx);
@@ -809,28 +809,28 @@ __mutex_lock_slowpath(atomic_t *lock_count)
 	struct mutex *lock = container_of(lock_count, struct mutex, count);
 
 	__mutex_lock_common(lock, TASK_UNINTERRUPTIBLE, 0,
-			    NULL, _RET_IP_, NULL);
+			    NULL, _RET_IP_, NULL, 0);
 }
 
 static noinline int __sched
 __mutex_lock_killable_slowpath(struct mutex *lock)
 {
 	return __mutex_lock_common(lock, TASK_KILLABLE, 0,
-				   NULL, _RET_IP_, NULL);
+				   NULL, _RET_IP_, NULL, 0);
 }
 
 static noinline int __sched
 __mutex_lock_interruptible_slowpath(struct mutex *lock)
 {
 	return __mutex_lock_common(lock, TASK_INTERRUPTIBLE, 0,
-				   NULL, _RET_IP_, NULL);
+				   NULL, _RET_IP_, NULL, 0);
 }
 
 static noinline int __sched
 __ww_mutex_lock_slowpath(struct ww_mutex *lock, struct ww_acquire_ctx *ctx)
 {
 	return __mutex_lock_common(&lock->base, TASK_UNINTERRUPTIBLE, 0,
-				   NULL, _RET_IP_, ctx);
+				   NULL, _RET_IP_, ctx, 1);
 }
 
 static noinline int __sched
@@ -838,7 +838,7 @@ __ww_mutex_lock_interruptible_slowpath(struct ww_mutex *lock,
 					    struct ww_acquire_ctx *ctx)
 {
 	return __mutex_lock_common(&lock->base, TASK_INTERRUPTIBLE, 0,
-				   NULL, _RET_IP_, ctx);
+				   NULL, _RET_IP_, ctx, 1);
 }
 
 #endif
#1 lore raw
Quoted 3 lines · Ingo Molnar

This tree fixes a boot crash in CONFIG_DEBUG_MUTEXES=y kernels, on kernels built with GCC 3.x. (There are still such distros.)

Btw, it's really not just gcc 3.x. That code was (a) incomprehensible,
(b) wrong and (c) caused problems for LLVM too.

It was wrong because "__builtin_constant_p(ww_ctx == NULL)" simply makes no sense.

Why?

That expression is largely equivalent to "__builtin_constant_p(ww_ctx)" (because iff ww_ctx is constant, then the comparison to NULL is constant), which is actually much easier to read, while carrying a totally different semantic meaning. Making things worse, the comparison to NULL *may* be marked constant under some very random situations (ie the compiler could turn a "taking an address of a variable is never NULL" kind of knowledge and combining it with other knowledge, and turn a complicated "ctx" expression into a "I know this cannot be NULL" thing, and thus the "== NULL" is a constant, even though ctx itself is some dynamic calculation).

Whoever wrote the original should be shot. And this commit shouldn't have been marked as being somehow about gcc-version dependence, but about removing completely crap code.

Linus

#2 lore raw

op 27-10-13 18:28, Linus Torvalds schreef:

Quoted 22 lines · Ingo Molnar
Quoted 2 lines

This tree fixes a boot crash in CONFIG_DEBUG_MUTEXES=y kernels, on kernels built with GCC 3.x. (There are still such distros.)

Btw, it's really not just gcc 3.x. That code was (a) incomprehensible,
(b) wrong and (c) caused problems for LLVM too.

It was wrong because "__builtin_constant_p(ww_ctx == NULL)" simply makes no sense.

Why?

That expression is largely equivalent to "__builtin_constant_p(ww_ctx)" (because iff ww_ctx is constant, then the comparison to NULL is constant), which is actually much easier to read, while carrying a totally different semantic meaning. Making things worse, the comparison to NULL *may* be marked constant under some very random situations (ie the compiler could turn a "taking an address of a variable is never NULL" kind of knowledge and combining it with other knowledge, and turn a complicated "ctx" expression into a "I know this cannot be NULL" thing, and thus the "== NULL" is a constant, even though ctx itself is some dynamic calculation).

Quoted 4 lines

Whoever wrote the original should be shot. And this commit shouldn't have been marked as being somehow about gcc-version dependence, but about removing completely crap code.

Unfortunately gcc disagreed there, which was another compiler bug. __builtin_constant_p(ww_ctx) was NOT equal to __builtin_constant_p(ww_ctx == NULL), iirc. __builtin_constant_p(ww_ctx == NULL) is equal to __builtin_constant_p(ww_ctx != NULL), but the former is more readable, since it shows we expect ww_ctx to be null.

But yeah I guess it was too broken in gcc after all, so that's why it had to be killed altogether.

#3 lore raw
Quoted 13 lines · Maarten Lankhorst

op 27-10-13 18:28, Linus Torvalds schreef:

Quoted 12 lines

That expression is largely equivalent to "__builtin_constant_p(ww_ctx)" (because iff ww_ctx is constant, then the comparison to NULL is constant), which is actually much easier to read, while carrying a totally different semantic meaning. Making things worse, the comparison to NULL *may* be marked constant under some very random situations (ie the compiler could turn a "taking an address of a variable is never NULL" kind of knowledge and combining it with other knowledge, and turn a complicated "ctx" expression into a "I know this cannot be NULL" thing, and thus the "== NULL" is a constant, even though ctx itself is some dynamic calculation).

Quoted 5 lines
Quoted 4 lines

Whoever wrote the original should be shot. And this commit shouldn't have been marked as being somehow about gcc-version dependence, but about removing completely crap code.

Unfortunately gcc disagreed there, which was another compiler bug.

Stop this idiotic "blame gcc bug" crap. Which part of my explanation for why it was *NOT* a compiler bug did you not understand?

Quoted 1 line

__builtin_constant_p(ww_ctx) was NOT equal to __builtin_constant_p(ww_ctx == NULL), iirc.

See my "largely equivalent" comment, with the *EXTRA* logic that gcc may actually find cases where the comparison is a constant even if the ww_ctx thing itself isn't a constant.

Quoted 2 lines

__builtin_constant_p(ww_ctx == NULL) is equal to __builtin_constant_p(ww_ctx != NULL), but the former is more readable, since it shows we expect ww_ctx to be null.

Stop the f*cking around already! The whole "we expect ww_ctx to be null" thing shows that YOU DO NOT SEEM TO UNDERSTAND WHAT THE TEST ACTUALLY IS!

The expression

__builtin_constant_p(ww_ctx == NULL)

has ABSOLUTELY NOTHING to do with whether ww_ctx is NULL or not! Christ, can you really not understand that?

For example, ww_ctx could be "&static_variable", and the compiler can
- and some compiles _will_ - say that ww_ctx clearly cannot be NULL, so "ww_ctx == NULL" is 0, which is a constant, so the __builtin_constant_p() expression returns true. See? That expression has absolutely NOTHING to do with whether you passed in NULL or not. NOTHING.

That __builtin_constant_p() tests whether the comparison is *CONSTANT*. And "0" is just as much a constant as "1" is. Really. So the whole f*cking expression is total and utter crap, because it is entirely and utterly senseless. It lacks all meaning. It's not actually testing for NULL at all. Never was, never will.

The *ONLY* thing it is testing for is "how much can the compiler optimize this", and as such the *ONLY* thing it tests for is compiler differences.

Really. Seriously. If you start blaming the compiler for different compilers giving different results, the only thing *that* shows is that you didn't understand the expression to begin with.

Quoted 1 line

But yeah I guess it was too broken in gcc after all, so that's why it had to be killed altogether.

NO NO NO NO. No a f*cking thousand times. It's not "too broken in gcc". It's too broken in the source code, and the fact that you don't even understand that is sad. You wrote the code, and you seem to be unable to admit that *your* code was buggy.

It's not a compiler bug. It's your bug. Stand up like a man, instead of trying to flail around and blame anything else but yourself.

So guys, get your act together, and stop blaming the compiler already.

Linus

#4 lore raw
Quoted 4 lines · Linus Torvalds

The *ONLY* thing it is testing for is "how much can the compiler optimize this", and as such the *ONLY* thing it tests for is compiler differences.

Side note: testing "can the compiler optimize this expression at compile time" is actually sometimes an interesting question, so it can be a valid thing to test.

But people should understand that the question is literally about THAT (ie visibility into compiler optimization) rather than about the value itself.

So generally, the only thing that a __builtin_constant_p() test can be used for is in *conjunction* with having an actual test for an actual value, and then having special-case logic that pertains to that value.

So for example, *this* is a valid test:

    if (__builtin_constant_p(ww_ctx) && ww_ctx == NULL) {
        ... special compile-time shortcut for the NULL value ..
    } else {
        ... generic code that *also* handles the NULL value ..
    }

and it's useful for triggering a compile-time optimized code-sequence that is only true for NULL. But because __builtin_constant_p() is about "how well can the compiler optimize this", that "else" statement had better be able to handle the generic case too.

And yes, there are a few places where we do expect a certain minimal set of optimizations. So in some cases we *might* have the rule that the only valid use of NULL in a case like the above is when the pointer passed in is passed in as a constant. And then we might say "we rely on the compiler always returning true for __builtin_constant_p(NULL)", and then we might say "so the "generic" version of the code is guaranteed to never see NULL".

But notice how *different* that

    __builtin_constant_p(ww_ctx) && ww_ctx == NULL

test is from

    __builtin_constant_p(ww_ctx == NULL)

and really, the two tests are *fundamentally* really really different. The first one can make sense. While the second one is pure and utter garbage.

Linus

#5 lore raw
In reply to Linus Torvalds

op 27-10-13 20:23, Linus Torvalds schreef:

Quoted 18 lines
Quoted 16 lines · Maarten Lankhorst

op 27-10-13 18:28, Linus Torvalds schreef:

Quoted 15 lines

That expression is largely equivalent to "__builtin_constant_p(ww_ctx)" (because iff ww_ctx is constant, then the comparison to NULL is constant), which is actually much easier to read, while carrying a totally different semantic meaning. Making things worse, the comparison to NULL *may* be marked constant under some very random situations (ie the compiler could turn a "taking an address of a variable is never NULL" kind of knowledge and combining it with other knowledge, and turn a complicated "ctx" expression into a "I know this cannot be NULL" thing, and thus the "== NULL" is a constant, even though ctx itself is some dynamic calculation).

Whoever wrote the original should be shot. And this commit shouldn't have been marked as being somehow about gcc-version dependence, but about removing completely crap code.

Quoted 8 lines
Quoted 1 line

Unfortunately gcc disagreed there, which was another compiler bug.

Stop this idiotic "blame gcc bug" crap. Which part of my explanation for why it was *NOT* a compiler bug did you not understand?

Quoted 1 line

__builtin_constant_p(ww_ctx) was NOT equal to __builtin_constant_p(ww_ctx == NULL), iirc.

See my "largely equivalent" comment, with the *EXTRA* logic that gcc may actually find cases where the comparison is a constant even if the ww_ctx thing itself isn't a constant.

Sure in the theoretical case it's possible.

Quoted 6 lines
Quoted 2 lines

__builtin_constant_p(ww_ctx == NULL) is equal to __builtin_constant_p(ww_ctx != NULL), but
the former is more readable, since it shows we expect ww_ctx to be null.

Stop the f*cking around already! The whole "we expect ww_ctx to be null" thing shows that YOU DO NOT SEEM TO UNDERSTAND WHAT THE TEST ACTUALLY IS!

Quoted 6 lines

The expression

   __builtin_constant_p(ww_ctx == NULL)

has ABSOLUTELY NOTHING to do with whether ww_ctx is NULL or not! Christ, can you really not understand that?

I'm fully aware, I just think the compiler cannot know that the address is always non-null for a generic function that takes an argument and isn't inlined.

Quoted 6 lines

For example, ww_ctx could be "&static_variable", and the compiler can
- and some compiles _will_ - say that ww_ctx clearly cannot be NULL, so "ww_ctx == NULL" is 0, which is a constant, so the __builtin_constant_p() expression returns true. See? That expression has absolutely NOTHING to do with whether you passed in NULL or not. NOTHING.

but __ww_mutex_lock isn't inlined..

Quoted 20 lines

That __builtin_constant_p() tests whether the comparison is *CONSTANT*. And "0" is just as much a constant as "1" is. Really. So the whole f*cking expression is total and utter crap, because it is entirely and utterly senseless. It lacks all meaning. It's not actually testing for NULL at all. Never was, never will.

The *ONLY* thing it is testing for is "how much can the compiler optimize this", and as such the *ONLY* thing it tests for is compiler differences.

Really. Seriously. If you start blaming the compiler for different compilers giving different results, the only thing *that* shows is that you didn't understand the expression to begin with.

Quoted 1 line

But yeah I guess it was too broken in gcc after all, so that's why it had to be killed altogether.

NO NO NO NO. No a f*cking thousand times. It's not "too broken in gcc". It's too broken in the source code, and the fact that you don't even understand that is sad. You wrote the code, and you seem to be unable to admit that *your* code was buggy.

Quoted 4 lines

It's not a compiler bug. It's your bug. Stand up like a man, instead of trying to flail around and blame anything else but yourself.

So guys, get your act together, and stop blaming the compiler already.

I never denied my original code didn't contain bugs, which is why I wrote that fix. I just don't believe gcc will ever be smart enough to determine that ww_ctx is a non-null argument in all calls to __ww_mutex_lock, and then determine for that reason ww_ctx != NULL would be an invariant.

I would love for a compiler to become that smart though, but I do not think it's likely.

But hey it was a bug, my code was buggy and I helped by suggesting how to write the correct fix.

~Maarten

#6 lore raw
Quoted 2 lines · Maarten Lankhorst

I would love for a compiler to become that smart though, but I do not think it's likely.

Dammit, even if that is true, then write the conditional *correctly*.

As mentioned, the conditional

    __builtin_constant_p(ww_ctx) && ww_ctx == NULL

is actually sensible, in a way the original one was *not*. It actually tests what you apparently intended to test, and is more readable to humans to boot.

And no, it still isn't actually guaranteed to do what you want it to do. Historically, in gcc, __builtin_constant_p() really only ever worked in macros, because by the time you use it in inline functions, a constant NULL in the caller will have been turned into a argument variable in the inline function, and __builtin_constant_p() would be done before that was optimized away. Over the years, gcc has pushed some of the builtin evaluation deeper down, and these days it actually works within inline functions, but my point that __builtin_constant_p() is about a certain level of compiler optimization is very much true: you're actually testing for a compiler optimization detail.

I know the LLVM people had similar issues with this comparison, so these days it's not even just about gcc versions. We may never have cared very much about icc, but llvm is actually an interesting target compiler.

Linus

#7 lore raw

op 27-10-13 20:51, Linus Torvalds schreef:

Quoted 5 lines
Quoted 1 line · Maarten Lankhorst

I would love for a compiler to become that smart though, but I do not think it's likely.

Dammit, even if that is true, then write the conditional *correctly*.

Quoted 7 lines

As mentioned, the conditional

    __builtin_constant_p(ww_ctx) && ww_ctx == NULL

is actually sensible, in a way the original one was *not*. It actually tests what you apparently intended to test, and is more readable to humans to boot.

Yeah that mail arrived after I sent mine, I agree that this would have been more sensible.

Quoted 12 lines

And no, it still isn't actually guaranteed to do what you want it to do. Historically, in gcc, __builtin_constant_p() really only ever worked in macros, because by the time you use it in inline functions, a constant NULL in the caller will have been turned into a argument variable in the inline function, and __builtin_constant_p() would be done before that was optimized away. Over the years, gcc has pushed some of the builtin evaluation deeper down, and these days it actually works within inline functions, but my point that __builtin_constant_p() is about a certain level of compiler optimization is very much true: you're actually testing for a compiler optimization detail.

Quoted 5 lines

I know the LLVM people had similar issues with this comparison, so these days it's not even just about gcc versions. We may never have cared very much about icc, but llvm is actually an interesting target compiler.

And this is why ww_ctx == NULL is now passed as an inline argument. :)

~Maarten

#8 lore raw
Quoted 2 lines · Maarten Lankhorst

And this is why ww_ctx == NULL is now passed as an inline argument. :)

Well, more than that - the "optimization" has been done at the source code level, so that the behavior is no longer a matter about how well the compiler optimizes it any more.

I'm not complaining about the fix. I'm complaining about how the fix was claimed to be due to a compiler bug. The "documentation" for the fix (ie the commit message) was actively misleading.

Linus

#9 lore raw
Quoted 6 lines · Linus Torvalds
Quoted 3 lines · Maarten Lankhorst

And this is why ww_ctx == NULL is now passed as an inline argument. :)

Quoted 7 lines

Well, more than that - the "optimization" has been done at the source code level, so that the behavior is no longer a matter about how well the compiler optimizes it any more.

I'm not complaining about the fix. I'm complaining about how the fix was claimed to be due to a compiler bug. The "documentation" for the fix (ie the commit message) was actively misleading.

Agreed, there was quite a bit of back and forth and I genuinely got confused and thought it's purely about a compiler bug (hence the misleading pull request) - will watch out for that pattern better next time around.

Thanks,

Ingo