Skip to contentSkip to conversation

Use atomic_xchg() instead atomic_dec_and_test() for stop_machine

PATCH In review OGAWA Hirofumi 1 participant 2 messages Jun 7, 2012 – Jun 14, 2012 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Use atomic_xchg() instead atomic_dec_and_test() for stop_machine

stop_machine_first is just to see if it is first one or not. In this usage, atomic_dec_and_test() makes value less than 0.

I think it is not desirable, because it only triggers atomic_dec_and_test() less than 0 debug patch. (the patch tests result of atomic_dec_and_test() is < 0)

So, this uses atomic_xchg() instead.

---

 arch/x86/kernel/alternative.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kernel/alternative.c renamed from arch/x86/kernel/alternative.c~stop_machine-use-atomic_xchg +1 -1
diff -puN arch/x86/kernel/alternative.c~stop_machine-use-atomic_xchg arch/x86/kernel/alternative.c
@@ -664,7 +664,7 @@ static int __kprobes stop_machine_text_p
 	struct text_poke_param *p;
 	int i;
 
-	if (atomic_dec_and_test(&stop_machine_first)) {
+	if (atomic_xchg(&stop_machine_first, 0)) {
 		for (i = 0; i < tpp->nparams; i++) {
 			p = &tpp->params[i];
 			text_poke(p->addr, p->opcode, p->len);

_

Signature · 1 line
OGAWA Hirofumi <[email protected]>
#1 lore raw

[tip:x86/asm] x86/alternatives: Use atomic_xchg() instead atomic_dec_and_test() for stop_machine_text_poke()

Commit-ID:  2f74759056797054122cdc70844137f70bb3f626
Gitweb:     http://git.kernel.org/tip/2f74759056797054122cdc70844137f70bb3f626
Author:     OGAWA Hirofumi <[email protected]>
AuthorDate: Thu, 7 Jun 2012 22:20:18 +0900
Committer:  Ingo Molnar <[email protected]>
CommitDate: Wed, 13 Jun 2012 15:08:37 +0200

x86/alternatives: Use atomic_xchg() instead atomic_dec_and_test() for stop_machine_text_poke()

stop_machine_text_poke() uses atomic_dec_and_test() to select one of the CPUs executing that function to actually modify the code.

Since the variable is initialized to 1, subsequent CPUs will make the variable go negative. Since going negative is uncommon/unexpected in typical dec_and_test usage change this user to atomic_xchg().

This was found using a patch that warns on dec_and_test going negative.

---
 arch/x86/kernel/alternative.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kernel/alternative.c +1 -1
index 1f84794..53231a0 100644
@@ -664,7 +664,7 @@ static int __kprobes stop_machine_text_poke(void *data)
 	struct text_poke_param *p;
 	int i;
 
-	if (atomic_dec_and_test(&stop_machine_first)) {
+	if (atomic_xchg(&stop_machine_first, 0)) {
 		for (i = 0; i < tpp->nparams; i++) {
 			p = &tpp->params[i];
 			text_poke(p->addr, p->opcode, p->len);