Skip to contentSkip to conversation

kvm/emulate: fix a -Werror=cast-function-type

PATCH Superseded Qian Cai 4 participants 9 messages Feb 14, 2020 – Feb 17, 2020

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Qian Cai

author
#0 lore raw

PATCH 1/1 kvm/emulate: fix a -Werror=cast-function-type

arch/x86/kvm/emulate.c: In function 'x86_emulate_insn':
arch/x86/kvm/emulate.c:5686:22: error: cast between incompatible function types from 'int (*)(struct x86_emulate_ctxt *)' to 'void (*)(struct fastop *)' [-Werror=cast-function-type]

    rc = fastop(ctxt, (fastop_t)ctxt->execute);
Fixes 3009afc6e39e ("KVM: x86: Use a typedef for fastop functions")
---
 arch/x86/kvm/emulate.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +5 -3
index ddbc61984227..17ae820cf59d 100644
@@ -5682,10 +5682,12 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
 		ctxt->eflags &= ~X86_EFLAGS_RF;
 
 	if (ctxt->execute) {
-		if (ctxt->d & Fastop)
-			rc = fastop(ctxt, (fastop_t)ctxt->execute);
-		else
+		if (ctxt->d & Fastop) {
+			fastop_t fop = (void *)ctxt->execute;
+			rc = fastop(ctxt, fop);
+		} else {
 			rc = ctxt->execute(ctxt);
+		}
 		if (rc != X86EMUL_CONTINUE)
 			goto done;
 		goto writeback;
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 17 lines · Qian Cai

arch/x86/kvm/emulate.c: In function 'x86_emulate_insn':
arch/x86/kvm/emulate.c:5686:22: error: cast between incompatible function types from 'int (*)(struct x86_emulate_ctxt *)' to 'void (*)(struct fastop *)' [-Werror=cast-function-type]

    rc = fastop(ctxt, (fastop_t)ctxt->execute);
Fixes 3009afc6e39e ("KVM: x86: Use a typedef for fastop functions")
---
 arch/x86/kvm/emulate.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +0 -0
index ddbc61984227..17ae820cf59d 100644

arch/x86/kvm/emulate.c int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) · line 5686

Quoted 5 lines
arch/x86/kvm/emulate.c +0 -2
@@ -5682,10 +5682,12 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
 		ctxt->eflags &= ~X86_EFLAGS_RF;
 
 	if (ctxt->execute) {
-		if (ctxt->d & Fastop)
-			rc = fastop(ctxt, (fastop_t)ctxt->execute);

Alternatively, can we do -Wno-cast-function-type? That's a silly warning IMO.

If not, will either of these work?

			rc = fastop(ctxt, (void *)ctxt->execute);

or

			rc = fastop(ctxt, (fastop_t)(void *)ctxt->execute);
Quoted 13 lines
arch/x86/kvm/emulate.c +5 -1
-		else
+		if (ctxt->d & Fastop) {
+			fastop_t fop = (void *)ctxt->execute;
+			rc = fastop(ctxt, fop);
+		} else {
 			rc = ctxt->execute(ctxt);
+		}
 		if (rc != X86EMUL_CONTINUE)
 			goto done;
 		goto writeback;
Signature · 1 line
1.8.3.1
author
#2 lore raw
Quoted 20 lines · Sean Christopherson
Quoted 19 lines · Qian Cai

arch/x86/kvm/emulate.c: In function 'x86_emulate_insn':
arch/x86/kvm/emulate.c:5686:22: error: cast between incompatible function types from 'int (*)(struct x86_emulate_ctxt *)' to 'void (*)(struct fastop *)' [-Werror=cast-function-type]

    rc = fastop(ctxt, (fastop_t)ctxt->execute);
Fixes 3009afc6e39e ("KVM: x86: Use a typedef for fastop functions")
---
 arch/x86/kvm/emulate.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +0 -0
index ddbc61984227..17ae820cf59d 100644
@@ -5682,10 +5682,12 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
 		ctxt->eflags &= ~X86_EFLAGS_RF;
 
Quoted 6 lines

arch/x86/kvm/emulate.c int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) · line 5686

Quoted 3 lines
arch/x86/kvm/emulate.c +0 -2
 	if (ctxt->execute) {
-		if (ctxt->d & Fastop)
-			rc = fastop(ctxt, (fastop_t)ctxt->execute);

Alternatively, can we do -Wno-cast-function-type? That's a silly warning IMO.

I am doing W=1 on linux-next where some of the warnings might be silly but the recent commit changes all warnings to errors forces me having to silence those somehow.

Quoted 7 lines

If not, will either of these work?

			rc = fastop(ctxt, (void *)ctxt->execute);

or

			rc = fastop(ctxt, (fastop_t)(void *)ctxt->execute);

I have no strong preference. I originally thought just to go back the previous code style where might be more acceptable, but it is up to maintainers.

Quoted 14 lines
Quoted 14 lines
arch/x86/kvm/emulate.c +5 -1
-		else
+		if (ctxt->d & Fastop) {
+			fastop_t fop = (void *)ctxt->execute;
+			rc = fastop(ctxt, fop);
+		} else {
 			rc = ctxt->execute(ctxt);
+		}
 		if (rc != X86EMUL_CONTINUE)
 			goto done;
 		goto writeback;
Signature · 1 line
1.8.3.1
#3 lore raw
Quoted 36 lines · Qian Cai
Quoted 20 lines · Sean Christopherson
Quoted 19 lines · Qian Cai

arch/x86/kvm/emulate.c: In function 'x86_emulate_insn':
arch/x86/kvm/emulate.c:5686:22: error: cast between incompatible function types from 'int (*)(struct x86_emulate_ctxt *)' to 'void (*)(struct fastop *)' [-Werror=cast-function-type]

    rc = fastop(ctxt, (fastop_t)ctxt->execute);
Fixes 3009afc6e39e ("KVM: x86: Use a typedef for fastop functions")
---
 arch/x86/kvm/emulate.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +0 -0
index ddbc61984227..17ae820cf59d 100644
@@ -5682,10 +5682,12 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
            ctxt->eflags &= ~X86_EFLAGS_RF;
 
Quoted 6 lines

arch/x86/kvm/emulate.c int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) · line 5686

Quoted 3 lines
arch/x86/kvm/emulate.c +0 -2
    if (ctxt->execute) {
-           if (ctxt->d & Fastop)
-                   rc = fastop(ctxt, (fastop_t)ctxt->execute);

Alternatively, can we do -Wno-cast-function-type? That's a silly warning IMO.

I am doing W=1 on linux-next where some of the warnings might be silly but the recent commit changes all warnings to errors forces me having to silence those somehow.

Quoted 3 lines

If not, will either of these work?

Quoted 7 lines
Quoted 4 lines
                      rc = fastop(ctxt, (void *)ctxt->execute);

or

                      rc = fastop(ctxt, (fastop_t)(void *)ctxt->execute);

I have no strong preference. I originally thought just to go back the previous code style where might be more acceptable, but it is up to maintainers.

It seems misguided to define a local variable just to get an implicit cast from (void *) to (fastop_t). Sean's first suggestion gives you the same implicit cast without the local variable. The second suggestion makes both casts explicit.

author
#4 lore raw
Quoted 42 lines · Jim Mattson
Quoted 41 lines · Qian Cai
Quoted 20 lines · Sean Christopherson
Quoted 19 lines · Qian Cai

arch/x86/kvm/emulate.c: In function 'x86_emulate_insn':
arch/x86/kvm/emulate.c:5686:22: error: cast between incompatible function types from 'int (*)(struct x86_emulate_ctxt *)' to 'void (*)(struct fastop *)' [-Werror=cast-function-type]

    rc = fastop(ctxt, (fastop_t)ctxt->execute);
Fixes 3009afc6e39e ("KVM: x86: Use a typedef for fastop functions")
---
 arch/x86/kvm/emulate.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
arch/x86/kvm/emulate.c +0 -0
index ddbc61984227..17ae820cf59d 100644
@@ -5682,10 +5682,12 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
            ctxt->eflags &= ~X86_EFLAGS_RF;
 
Quoted 6 lines

arch/x86/kvm/emulate.c int x86_emulate_insn(struct x86_emulate_ctxt *ctxt) · line 5686

Quoted 3 lines
arch/x86/kvm/emulate.c +0 -2
    if (ctxt->execute) {
-           if (ctxt->d & Fastop)
-                   rc = fastop(ctxt, (fastop_t)ctxt->execute);

Alternatively, can we do -Wno-cast-function-type? That's a silly warning IMO.

I am doing W=1 on linux-next where some of the warnings might be silly but the recent commit changes all warnings to errors forces me having to silence those somehow.

Quoted 7 lines

If not, will either of these work?

                      rc = fastop(ctxt, (void *)ctxt->execute);

or

                      rc = fastop(ctxt, (fastop_t)(void *)ctxt->execute);
Quoted 7 lines
Quoted 2 lines

I have no strong preference. I originally thought just to go back the previous code style where might be more acceptable, but it is up to maintainers.

It seems misguided to define a local variable just to get an implicit cast from (void *) to (fastop_t). Sean's first suggestion gives you the same implicit cast without the local variable. The second suggestion makes both casts explicit.

OK, I'll do a v2 using the first suggestion which looks simpler once it passed compilations.

maintainer
#5 lore raw
Quoted 8 lines · Qian Cai
Quoted 4 lines

It seems misguided to define a local variable just to get an implicit cast from (void *) to (fastop_t). Sean's first suggestion gives you the same implicit cast without the local variable. The second suggestion makes both casts explicit.

OK, I'll do a v2 using the first suggestion which looks simpler once it passed compilations.

Another interesting possibility is to use an unnamed union of a (*execute) function pointer and a (*fastop) function pointer.

Paolo

#6 lore raw
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Qian Cai
Quoted 4 lines

It seems misguided to define a local variable just to get an implicit cast from (void *) to (fastop_t). Sean's first suggestion gives you the same implicit cast without the local variable. The second suggestion makes both casts explicit.

Quoted 6 lines
Quoted 3 lines

OK, I'll do a v2 using the first suggestion which looks simpler once it passed compilations.

Another interesting possibility is to use an unnamed union of a (*execute) function pointer and a (*fastop) function pointer.

I considered that when introducing fastop_t. I don't remember why I didn't go that route. It's entirely possible I completely forgot that anonymous unions are allowed and thought it would mean changing a bunch of use sites.

author
#7 lore raw
In reply to Paolo Bonzini
Quoted 6 lines · Paolo Bonzini
Quoted 5 lines · Qian Cai
Quoted 4 lines

It seems misguided to define a local variable just to get an implicit cast from (void *) to (fastop_t). Sean's first suggestion gives you the same implicit cast without the local variable. The second suggestion makes both casts explicit.

Quoted 7 lines
Quoted 3 lines

OK, I'll do a v2 using the first suggestion which looks simpler once it passed compilations.

Another interesting possibility is to use an unnamed union of a (*execute) function pointer and a (*fastop) function pointer.

This?

(quoted diff) +0 -0

b/arch/x86/include/asm/kvm_emulate.h
index 03946eb3e2b9..2a8f2bd2e5cf 100644

arch/x86/include/asm/kvm_emulate.h +0 -0
@@ -292,6 +292,14 @@ enum x86emul_mode {

 #define X86EMUL_SMM_MASK             (1 << 6)
 #define X86EMUL_SMM_INSIDE_NMI_MASK  (1 << 7)

+/*
+ * fastop functions are declared as taking a never-defined fastop parameter,
+ * so they can't be called from C directly.
+ */
+struct fastop;
+
+typedef void (*fastop_t)(struct fastop *);
+
 struct x86_emulate_ctxt {
  const struct x86_emulate_ops *ops;

@@ -324,7 +332,10 @@ struct x86_emulate_ctxt {
  struct operand src;
  struct operand src2;
  struct operand dst;
- int (*execute)(struct x86_emulate_ctxt *ctxt);
+ union {
+ int (*execute)(struct x86_emulate_ctxt *ctxt);
+ fastop_t fop;
+ };
  int (*check_perm)(struct x86_emulate_ctxt *ctxt);
  /*
   * The following six fields are cleared together,

arch/x86/kvm/emulate.c +0 -0
index ddbc61984227..dd19fb3539e0 100644
@@ -191,25 +191,6 @@

 #define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
 #define FASTOP_SIZE 8

-/*
- * fastop functions have a special calling convention:
- *
- * dst:    rax        (in/out)
- * src:    rdx        (in/out)
- * src2:   rcx        (in)
- * flags:  rflags     (in/out)
- * ex:     rsi        (in:fastop pointer, out:zero if exception)
- *
- * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
- * different operand sizes can be reached by calculation, rather than a jump
- * table (which would be bigger than the code).
- *
- * fastop functions are declared as taking a never-defined fastop parameter,
- * so they can't be called from C directly.
- */
-
-struct fastop;
-
 struct opcode {
  u64 flags : 56;
  u64 intercept : 8;
@@ -311,8 +292,19 @@ static void invalidate_registers(struct x86_emulate_ctxt *ctxt)
 #define ON64(x)
 #endif

-typedef void (*fastop_t)(struct fastop *);
-
+/*
+ * fastop functions have a special calling convention:
+ *
+ * dst:    rax        (in/out)
+ * src:    rdx        (in/out)
+ * src2:   rcx        (in)
+ * flags:  rflags     (in/out)
+ * ex:     rsi        (in:fastop pointer, out:zero if exception)
+ *
+ * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
+ * different operand sizes can be reached by calculation, rather than a jump
+ * table (which would be bigger than the code).
+ */
 static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop);

 #define __FOP_FUNC(name) \
@@ -5683,7 +5675,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)
  if (ctxt->execute) {
  if (ctxt->d & Fastop)
- rc = fastop(ctxt, (fastop_t)ctxt->execute);
+ rc = fastop(ctxt, ctxt->fop);
  else
  rc = ctxt->execute(ctxt);
  if (rc != X86EMUL_CONTINUE)

maintainer
#8 lore raw
Quoted 11 lines · Qian Cai
Quoted 10 lines · Paolo Bonzini
Quoted 8 lines · Qian Cai
Quoted 4 lines

It seems misguided to define a local variable just to get an implicit cast from (void *) to (fastop_t). Sean's first suggestion gives you the same implicit cast without the local variable. The second suggestion makes both casts explicit.

OK, I'll do a v2 using the first suggestion which looks simpler once it passed compilations.

Quoted 5 lines
Quoted 3 lines

Another interesting possibility is to use an unnamed union of a (*execute) function pointer and a (*fastop) function pointer.

This?

Yes, perfect. Can you send it with Signed-off-by and all that?

Thanks,

Paolo

Quoted 95 lines
arch/x86/include/asm/kvm_emulate.h +0 -0
index 03946eb3e2b9..2a8f2bd2e5cf 100644
@@ -292,6 +292,14 @@ enum x86emul_mode {
  #define X86EMUL_SMM_MASK             (1 << 6)
  #define X86EMUL_SMM_INSIDE_NMI_MASK  (1 << 7)

arch/x86/include/asm/kvm_emulate.h +8 -0
+/*
+ * fastop functions are declared as taking a never-defined fastop parameter,
+ * so they can't be called from C directly.
+ */
+struct fastop;
+
+typedef void (*fastop_t)(struct fastop *);
+

 struct x86_emulate_ctxt {
  const struct x86_emulate_ops *ops;

arch/x86/include/asm/kvm_emulate.h +4 -1
@@ -324,7 +332,10 @@ struct x86_emulate_ctxt {
  	struct operand src;
  	struct operand src2;
  	struct operand dst;
-	int (*execute)(struct x86_emulate_ctxt *ctxt);
+	union {
+		int (*execute)(struct x86_emulate_ctxt *ctxt);
+		fastop_t fop;
+	};
  	int (*check_perm)(struct x86_emulate_ctxt *ctxt);
  	/*
  	 * The following six fields are cleared together,
arch/x86/kvm/emulate.c +0 -0
index ddbc61984227..dd19fb3539e0 100644
@@ -191,25 +191,6 @@
  #define NR_FASTOP (ilog2(sizeof(ulong)) + 1)
  #define FASTOP_SIZE 8

arch/x86/kvm/emulate.c +0 -19
-/*
- * fastop functions have a special calling convention:
- *
- * dst:    rax        (in/out)
- * src:    rdx        (in/out)
- * src2:   rcx        (in)
- * flags:  rflags     (in/out)
- * ex:     rsi        (in:fastop pointer, out:zero if exception)
- *
- * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
- * different operand sizes can be reached by calculation, rather than a jump
- * table (which would be bigger than the code).
- *
- * fastop functions are declared as taking a never-defined fastop parameter,
- * so they can't be called from C directly.
- */
-
-struct fastop;
-

 struct opcode {
  u64 flags : 56;
  u64 intercept : 8;

arch/x86/kvm/emulate.c +0 -0
@@ -311,8 +292,19 @@ static void invalidate_registers(struct x86_emulate_ctxt
 *ctxt)
  #define ON64(x)
  #endif

arch/x86/kvm/emulate.c +13 -2
-typedef void (*fastop_t)(struct fastop *);
-
+/*
+ * fastop functions have a special calling convention:
+ *
+ * dst:    rax        (in/out)
+ * src:    rdx        (in/out)
+ * src2:   rcx        (in)
+ * flags:  rflags     (in/out)
+ * ex:     rsi        (in:fastop pointer, out:zero if exception)
+ *
+ * Moreover, they are all exactly FASTOP_SIZE bytes long, so functions for
+ * different operand sizes can be reached by calculation, rather than a jump
+ * table (which would be bigger than the code).
+ */

 static int fastop(struct x86_emulate_ctxt *ctxt, fastop_t fop);
 #define __FOP_FUNC(name) \

arch/x86/kvm/emulate.c +0 -0
@@ -5683,7 +5675,7 @@ int x86_emulate_insn(struct x86_emulate_ctxt *ctxt)


  if (ctxt->execute) {
  if (ctxt->d & Fastop)

arch/x86/kvm/emulate.c +1 -1
-			rc = fastop(ctxt, (fastop_t)ctxt->execute);
+			rc = fastop(ctxt, ctxt->fop);

  else
  rc = ctxt->execute(ctxt);
  if (rc != X86EMUL_CONTINUE)