Skip to contentSkip to conversation

util/async: avoid useless cast

PATCH Applied Frediano Ziglio 3 participants 18 messages Oct 23, 2019 – Oct 24, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 util/async: avoid useless cast

event_notifier_dummy_cb is already compatible with EventNotifierHandler.

---
 util/async.c | 1 -
 1 file changed, 1 deletion(-)
util/async.c +0 -1
index ca83e32c7f..b1fa5319e5 100644
@@ -429,7 +429,6 @@ AioContext *aio_context_new(Error **errp)
 
     aio_set_event_notifier(ctx, &ctx->notifier,
                            false,
-                           (EventNotifierHandler *)
                            event_notifier_dummy_cb,
                            event_notifier_poll);
 #ifdef CONFIG_LINUX_AIO
Signature · 1 line
2.21.0
#1 lore raw

PATCH 2/3 event_notifier: avoid dandling file descriptor in event_notifier_cleanup

If rfd is equal to wfd the file descriptor is closed but rfd will still have the closed value.
The EventNotifier structure should not be used again after calling event_notifier_cleanup or should be initialized again but make sure to not have dandling file descriptors around.

---
 util/event_notifier-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/event_notifier-posix.c +1 -1
index 73c4046b58..00d93204f9 100644
@@ -80,8 +80,8 @@ void event_notifier_cleanup(EventNotifier *e)
 {
     if (e->rfd != e->wfd) {
         close(e->rfd);
-        e->rfd = -1;
     }
+    e->rfd = -1;
     close(e->wfd);
     e->wfd = -1;
 }
Signature · 1 line
2.21.0
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] event_notifier: avoid dandling file descriptor in event_notifier_cleanup

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 17 lines

If rfd is equal to wfd the file descriptor is closed but rfd will still have the closed value.
The EventNotifier structure should not be used again after calling event_notifier_cleanup or should be initialized again but make sure to not have dandling file descriptors around.

---
 util/event_notifier-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/event_notifier-posix.c +0 -0
index 73c4046b58..00d93204f9 100644

util/event_notifier-posix.c void event_notifier_cleanup(EventNotifier *e) · line 87

Quoted 9 lines
util/event_notifier-posix.c +1 -1
@@ -80,8 +80,8 @@ void event_notifier_cleanup(EventNotifier *e)
 {
     if (e->rfd != e->wfd) {
         close(e->rfd);
-        e->rfd = -1;
     }
+    e->rfd = -1;
     close(e->wfd);
     e->wfd = -1;
 }
#3 lore raw

Re: [PATCH 2/3] event_notifier: avoid dandling file descriptor in event_notifier_cleanup

In reply to Frediano Ziglio

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 17 lines

If rfd is equal to wfd the file descriptor is closed but rfd will still have the closed value.
The EventNotifier structure should not be used again after calling event_notifier_cleanup or should be initialized again but make sure to not have dandling file descriptors around.

---
 util/event_notifier-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/event_notifier-posix.c +0 -0
index 73c4046b58..00d93204f9 100644

util/event_notifier-posix.c void event_notifier_cleanup(EventNotifier *e) · line 87

Quoted 9 lines
util/event_notifier-posix.c +1 -1
@@ -80,8 +80,8 @@ void event_notifier_cleanup(EventNotifier *e)
 {
     if (e->rfd != e->wfd) {
         close(e->rfd);
-        e->rfd = -1;
     }
+    e->rfd = -1;
     close(e->wfd);
     e->wfd = -1;
 }

Applied to my trivial-patches branch.

Thanks,
Laurent

#4 lore raw

PATCH 3/3 qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

In reply to Frediano Ziglio
---
 util/qemu-timer.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
util/qemu-timer.c +1 -5
index d428fec567..094a20a05a 100644
@@ -322,11 +322,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)
     ms = DIV_ROUND_UP(ns, SCALE_MS);
 
     /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
-    if (ms > (int64_t) INT32_MAX) {
-        ms = INT32_MAX;
-    }
-
-    return (int) ms;
+    return (int) MIN(ms, (int64_t) INT32_MAX);
 }
 
 
Signature · 1 line
2.21.0
Reviewed-by on v1 3/3
#5 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 13 lines
---
 util/qemu-timer.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
util/qemu-timer.c +0 -0
index d428fec567..094a20a05a 100644
@@ -322,11 +322,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)
     ms = DIV_ROUND_UP(ns, SCALE_MS);
 

util/qemu-timer.c int qemu_timeout_ns_to_ms(int64_t ns) · line 328

Quoted 10 lines
util/qemu-timer.c +1 -5
     /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
-    if (ms > (int64_t) INT32_MAX) {
-        ms = INT32_MAX;
-    }
-
-    return (int) ms;
+    return (int) MIN(ms, (int64_t) INT32_MAX);
 }
 
 
#6 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Quoted 13 lines · Laurent Vivier

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 12 lines
---
  util/qemu-timer.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
util/qemu-timer.c +0 -0
index d428fec567..094a20a05a 100644
@@ -322,11 +322,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)
      ms = DIV_ROUND_UP(ns, SCALE_MS);
 
Quoted 8 lines

util/qemu-timer.c int qemu_timeout_ns_to_ms(int64_t ns) · line 326

Quoted 8 lines
util/qemu-timer.c +1 -5
      /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
-    if (ms > (int64_t) INT32_MAX) {
-        ms = INT32_MAX;
-    }
-
-    return (int) ms;
+    return (int) MIN(ms, (int64_t) INT32_MAX);
  }

Why so many casts? It should also work as:

return MIN(ms, INT32_MAX);

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#7 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Quoted 27 lines · Laurent Vivier
Quoted 13 lines

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 12 lines
---
  util/qemu-timer.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
util/qemu-timer.c +0 -0
index d428fec567..094a20a05a 100644
@@ -322,11 +322,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)
      ms = DIV_ROUND_UP(ns, SCALE_MS);
 
Quoted 9 lines

util/qemu-timer.c int qemu_timeout_ns_to_ms(int64_t ns) · line 326

Quoted 9 lines
util/qemu-timer.c +1 -5
      /* To avoid overflow problems, limit this to 2^31, i.e. approx 25
      days */
-    if (ms > (int64_t) INT32_MAX) {
-        ms = INT32_MAX;
-    }
-
-    return (int) ms;
+    return (int) MIN(ms, (int64_t) INT32_MAX);
  }

Why so many casts? It should also work as:

Quoted 2 lines

return MIN(ms, INT32_MAX);

This was former version. Laurent pointed out that MIN macro is using ternary operator which is expected to find the same time on second and third part so the cast inside the MIN macro. The cast before MIN was kept from previous code.

Frediano

#8 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Quoted 27 lines · Frediano Ziglio
Quoted 27 lines · Laurent Vivier
Quoted 13 lines

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 12 lines
---
   util/qemu-timer.c | 6 +-----
   1 file changed, 1 insertion(+), 5 deletions(-)
util/qemu-timer.c +0 -0
index d428fec567..094a20a05a 100644
@@ -322,11 +322,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)
       ms = DIV_ROUND_UP(ns, SCALE_MS);
 
Quoted 9 lines

util/qemu-timer.c int qemu_timeout_ns_to_ms(int64_t ns) · line 326

Quoted 9 lines
util/qemu-timer.c +1 -5
       /* To avoid overflow problems, limit this to 2^31, i.e. approx 25
       days */
-    if (ms > (int64_t) INT32_MAX) {
-        ms = INT32_MAX;
-    }
-
-    return (int) ms;
+    return (int) MIN(ms, (int64_t) INT32_MAX);
   }

Why so many casts? It should also work as:

Quoted 7 lines
Quoted 2 lines

return MIN(ms, INT32_MAX);

This was former version. Laurent pointed out that MIN macro is using ternary operator which is expected to find the same time on second and third part so the cast inside the MIN macro. The cast before MIN was kept from previous code.

The C rules for ternary type promotion guarantee that the MIN macro produces the correct type without the cast ('cond ? int64_t : int32_t' produces int64_t). I agree that the (int) cast was pre-existing, but as long as we are cleaning up useless casts, we might as well clean up both.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#9 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Le 23/10/2019 à 16:23, Eric Blake a écrit :

Quoted 31 lines · Frediano Ziglio
Quoted 30 lines
Quoted 29 lines · Laurent Vivier
Quoted 6 lines

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 5 lines

---
   util/qemu-timer.c | 6 +-----

   1 file changed, 1 insertion(+), 5 deletions(-)
Quoted 16 lines
Quoted 8 lines
util/qemu-timer.c +0 -0
index d428fec567..094a20a05a 100644
@@ -322,11 +322,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)
        ms = DIV_ROUND_UP(ns, SCALE_MS);
           /* To avoid overflow problems, limit this to 2^31, i.e.

approx 25

Quoted 8 lines

       days */
-    if (ms > (int64_t) INT32_MAX) {
-        ms = INT32_MAX;
-    }
-
-    return (int) ms;
+    return (int) MIN(ms, (int64_t) INT32_MAX);
   }

Why so many casts?  It should also work as:

return MIN(ms, INT32_MAX);

Quoted 8 lines
Quoted 4 lines

This was former version. Laurent pointed out that MIN macro is using ternary operator which is expected to find the same time on second and third part so the cast inside the MIN macro. The cast before MIN was kept from previous code.

The C rules for ternary type promotion guarantee that the MIN macro produces the correct type without the cast ('cond ? int64_t : int32_t' produces int64_t).

gdb seems to disagree with that:

(gdb) whatis l
type = long long
(gdb) whatis i
type = int
(gdb) whatis 1 ? l : i
type = long long
(gdb) whatis 0 ? l : i
type = int

It's on what I based my previous comment.

But both approaches are correct in the end.

Thanks,
Laurent

#10 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Quoted 6 lines · Laurent Vivier
Quoted 3 lines

The C rules for ternary type promotion guarantee that the MIN macro produces the correct type without the cast ('cond ? int64_t : int32_t' produces int64_t).

gdb seems to disagree with that:

Quoted 8 lines

(gdb) whatis l
type = long long
(gdb) whatis i
type = int
(gdb) whatis 1 ? l : i
type = long long
(gdb) whatis 0 ? l : i
type = int

It looks like you've found a gdb bug.

C99 6.5.15 p5 states:
"If both the second and third operands have arithmetic type, the result type that would be determined by the usual arithmetic conversions, were they applied to those two operands, is the type of the result."

and the usual arithmetic conversion of 'long long OP int' is 'long long', per 6.3.1.8.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#11 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Le 23/10/2019 à 16:51, Eric Blake a écrit :

Quoted 19 lines · Laurent Vivier
Quoted 7 lines
Quoted 4 lines

The C rules for ternary type promotion guarantee that the MIN macro produces the correct type without the cast ('cond ? int64_t : int32_t' produces int64_t).

gdb seems to disagree with that:

Quoted 8 lines

(gdb) whatis l
type = long long
(gdb) whatis i
type = int
(gdb) whatis 1 ? l : i
type = long long
(gdb) whatis 0 ? l : i
type = int

It looks like you've found a gdb bug.

Quoted 8 lines

C99 6.5.15 p5 states:
"If both the second and third operands have arithmetic type, the result type that would be determined by the usual arithmetic conversions, were they applied to those two operands, is the type of the result."

and the usual arithmetic conversion of 'long long OP int' is 'long long', per 6.3.1.8.

Ok, you're right [1]

Frediano, sorry for my misleading comment.

Thanks,
Laurent

[1] and gcc agrees:

int main(void)

{
        long long l;
        int i;
        typeof(0 ? l : i) f;
        typeof(1 ? l : i) t;
}

(gdb) whatis l
type = long long
(gdb) whatis i
type = int
(gdb) whatis f
type = long long
(gdb) whatis t
type = long long

#12 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

In reply to Frediano Ziglio

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 13 lines
---
 util/qemu-timer.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
util/qemu-timer.c +0 -0
index d428fec567..094a20a05a 100644
@@ -322,11 +322,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)
     ms = DIV_ROUND_UP(ns, SCALE_MS);
 

util/qemu-timer.c int qemu_timeout_ns_to_ms(int64_t ns) · line 328

Quoted 10 lines
util/qemu-timer.c +1 -5
     /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
-    if (ms > (int64_t) INT32_MAX) {
-        ms = INT32_MAX;
-    }
-
-    return (int) ms;
+    return (int) MIN(ms, (int64_t) INT32_MAX);
 }
 
 

Applied to my trivial-patches branch.

I've updated the patch to remove the two useless casts.

Eric, if you want to add your R-b, I can add it to the queued patch.

Thanks,
Laurent

Reviewed-by
#13 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Quoted 9 lines · Laurent Vivier

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 7 lines
---
  util/qemu-timer.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
Quoted 5 lines

Applied to my trivial-patches branch.

I've updated the patch to remove the two useless casts.

Eric, if you want to add your R-b, I can add it to the queued patch.

I don't see it queued on https://github.com/vivier/qemu/branches yet, but if removing the two casts is the only difference from the original:

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
#14 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Le 24/10/2019 à 20:03, Eric Blake a écrit :

Quoted 10 lines · Laurent Vivier
Quoted 9 lines

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 7 lines

---
  util/qemu-timer.c | 6 +-----

  1 file changed, 1 insertion(+), 5 deletions(-)
Quoted 7 lines
Quoted 5 lines

Applied to my trivial-patches branch.

I've updated the patch to remove the two useless casts.

Eric, if you want to add your R-b, I can add it to the queued patch.

I don't see it queued on https://github.com/vivier/qemu/branches yet,

Sorry, forgot to push it.

Quoted 4 lines

but if removing the two casts is the only difference from the original:

Added and pushed (branch trivial-patches), you can check.

Thanks,
Laurent

#15 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Quoted 19 lines · Laurent Vivier

Le 24/10/2019 à 20:03, Eric Blake a écrit :

Quoted 10 lines · Laurent Vivier
Quoted 9 lines

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 7 lines
---
   util/qemu-timer.c | 6 +-----
   1 file changed, 1 insertion(+), 5 deletions(-)
Quoted 7 lines
Quoted 5 lines

Applied to my trivial-patches branch.

I've updated the patch to remove the two useless casts.

Eric, if you want to add your R-b, I can add it to the queued patch.

I don't see it queued on https://github.com/vivier/qemu/branches yet,

Quoted 8 lines

Sorry, forgot to push it.

Quoted 4 lines

but if removing the two casts is the only difference from the original:

Added and pushed (branch trivial-patches), you can check.

Looks good.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v1 1/3
#16 lore raw
In reply to Frediano Ziglio

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 13 lines

event_notifier_dummy_cb is already compatible with EventNotifierHandler.

---
 util/async.c | 1 -
 1 file changed, 1 deletion(-)
util/async.c +0 -0
index ca83e32c7f..b1fa5319e5 100644

util/async.c AioContext *aio_context_new(Error **errp) · line 434

Quoted 7 lines
util/async.c +0 -1
@@ -429,7 +429,6 @@ AioContext *aio_context_new(Error **errp)
 
     aio_set_event_notifier(ctx, &ctx->notifier,
                            false,
-                           (EventNotifierHandler *)
                            event_notifier_dummy_cb,
                            event_notifier_poll);
 #ifdef CONFIG_LINUX_AIO
#17 lore raw
In reply to Frediano Ziglio

Le 23/10/2019 à 14:26, Frediano Ziglio a écrit :

Quoted 13 lines

event_notifier_dummy_cb is already compatible with EventNotifierHandler.

---
 util/async.c | 1 -
 1 file changed, 1 deletion(-)
util/async.c +0 -0
index ca83e32c7f..b1fa5319e5 100644

util/async.c AioContext *aio_context_new(Error **errp) · line 434

Quoted 7 lines
util/async.c +0 -1
@@ -429,7 +429,6 @@ AioContext *aio_context_new(Error **errp)
 
     aio_set_event_notifier(ctx, &ctx->notifier,
                            false,
-                           (EventNotifierHandler *)
                            event_notifier_dummy_cb,
                            event_notifier_poll);
 #ifdef CONFIG_LINUX_AIO

Applied to my trivial-patches branch.

Thanks,
Laurent