qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* Re: [Qemu-devel] [PATCH 1/1] block migration: fix return value mismatch
@ 2014-11-21 18:08 Gary R Hook
  2014-11-24  9:10 ` Dr. David Alan Gilbert
  0 siblings, 1 reply; 13+ messages in thread
From: Gary R Hook @ 2014-11-21 18:08 UTC (permalink / raw)
  To: qemu-devel; +Cc: stefanha

Stefan Hajnoczi <address@hidden> writes:

 > On Wed, Nov 12, 2014 at 06:48:18PM +0000, Gary Hook wrote:
 >> -    return qemu_ftell(f) - last_ftell;
 >> +    delta_ftell = qemu_ftell(f) - last_ftell;
 >> +    return( (delta_ftell > 0) ? 1 : (delta_ftell < 0) ? -1 : 0 );
 >
 > Good find!
 >
 > Please don't nest the ternary operator, it is hard to read.
 >
 > if (delta_ftell < 0) {
 >     return -1;
 > } else if (delta_ftell > 0) {
 >     return 1;
 > } else {
 >     return 0;
 > }

So now that it's clear that I fully botched the patch submission, I 
would like to follow up: shall I resubmit this change?

I don't see this fix in the current source (which makes sense due to the 
2.2 RC2 work) but folks are asking. And it appears that there's a bug 
report over at RedHat that may be the same problem.

I think I'll add to my .sig: "Look, ma, only 72 columns!"

-- 
Gary R Hook
Senior Kernel Engineer
NIMBOXX, Inc

^ permalink raw reply	[flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH 1/1] block migration: fix return value mismatch
@ 2014-11-13 16:44 Gary Hook
  2014-11-13 17:03 ` Eric Blake
  0 siblings, 1 reply; 13+ messages in thread
From: Gary Hook @ 2014-11-13 16:44 UTC (permalink / raw)
  To: qemu-devel@nongnu.org

[-- Attachment #1: Type: text/plain, Size: 932 bytes --]

On 11/13/14, 6:46 AM, "Markus Armbruster" <armbru@redhat.com<mailto:armbru@redhat.com>> wrote:

Stefan Hajnoczi <stefanha@gmail.com<mailto:stefanha@gmail.com>> writes:

On Wed, Nov 12, 2014 at 06:48:18PM +0000, Gary Hook wrote:
-    return qemu_ftell(f) - last_ftell;
+    delta_ftell = qemu_ftell(f) - last_ftell;
+    return( (delta_ftell > 0) ? 1 : (delta_ftell < 0) ? -1 : 0 );

Good find!

Please don't nest the ternary operator, it is hard to read.

if (delta_ftell < 0) {
     return -1;
} else if (delta_ftell > 0) {
     return 1;
} else {
     return 0;
}

Bah, that's for wimps ;)

    return (delta_ftell > 0) - (delta_ftell < 0);

Look ma, no branches!

Ha-ha! Very good, but even less readable than the compressed ternary version,
IMO. This function only gets called once per migration; I don't see a few branches as performance-critical and worth the sacrifice of clarity as to intent.


[-- Attachment #2: Type: text/html, Size: 2789 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread
* Re: [Qemu-devel] [PATCH 1/1] block migration: fix return value mismatch
@ 2014-11-13 16:43 Gary Hook
  0 siblings, 0 replies; 13+ messages in thread
From: Gary Hook @ 2014-11-13 16:43 UTC (permalink / raw)
  To: qemu-devel@nongnu.org

[-- Attachment #1: Type: text/plain, Size: 908 bytes --]

On 11/13/14, 5:20 AM, "Stefan Hajnoczi" <stefanha@gmail.com<mailto:stefanha@gmail.com>> wrote:

On Wed, Nov 12, 2014 at 06:48:18PM +0000, Gary Hook wrote:
-    return qemu_ftell(f) - last_ftell;
+    delta_ftell = qemu_ftell(f) - last_ftell;
+    return( (delta_ftell > 0) ? 1 : (delta_ftell < 0) ? -1 : 0 );

Good find!

Please don't nest the ternary operator, it is hard to read.

if (delta_ftell < 0) {
    return -1;
} else if (delta_ftell > 0) {
    return 1;
} else {
    return 0;
}

Well, that format wasn¹t my first choice. I¹m still unclear on some of the
style requirements on this project, so I went with terse. Your rewrite
would have been my preference, but I¹m wondering if the syntax checker
will want the parentheses stripped as ³Unnecessary² despite how much they
aid in readability.

This one has caused us no end of pain. It¹s wonderful to eradicate it.


[-- Attachment #2: Type: text/html, Size: 2750 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread
* [Qemu-devel] [PATCH 1/1] block migration: fix return value mismatch
@ 2014-11-12 18:48 Gary Hook
  2014-11-12 20:27 ` Eric Blake
  2014-11-13 11:20 ` Stefan Hajnoczi
  0 siblings, 2 replies; 13+ messages in thread
From: Gary Hook @ 2014-11-12 18:48 UTC (permalink / raw)
  To: qemu-devel@nongnu.org

[-- Attachment #1: Type: text/plain, Size: 1286 bytes --]

The function uses a ternary return value (<, >, == 0) defined as an int. The code in in this function uses int64_t types to collect ftell() return values and use their difference as the return value. Unfortunately, narrowing of integer types results in the disposal of the left-most bits that won't fit in the target type. Here, for values larger than 2GB, the resulting value will be randomly negative or positive, based on total number of blocks. The patch ensures that only +1, -1, or 0 are returned to properly report status.




diff -u -r a/block-migration.c b/block-migration.c

--- a/block-migration.c 2014-04-17 08:30:59.000000000 -0500

+++ b/block-migration.c 2014-11-10 12:39:10.727431187 -0600

@@ -628,6 +628,7 @@

 {

     int ret;

     int64_t last_ftell = qemu_ftell(f);

+    int64_t delta_ftell;



     DPRINTF("Enter save live iterate submitted %d transferred %d\n",

             block_mig_state.submitted, block_mig_state.transferred);

@@ -677,7 +678,8 @@

     }



     qemu_put_be64(f, BLK_MIG_FLAG_EOS);

-    return qemu_ftell(f) - last_ftell;

+    delta_ftell = qemu_ftell(f) - last_ftell;

+    return( (delta_ftell > 0) ? 1 : (delta_ftell < 0) ? -1 : 0 );

 }



 /* Called with iothread lock taken.  */


[-- Attachment #2: Type: text/html, Size: 3956 bytes --]

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2014-11-24  9:10 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-21 18:08 [Qemu-devel] [PATCH 1/1] block migration: fix return value mismatch Gary R Hook
2014-11-24  9:10 ` Dr. David Alan Gilbert
  -- strict thread matches above, loose matches on Subject: below --
2014-11-13 16:44 Gary Hook
2014-11-13 17:03 ` Eric Blake
2014-11-13 17:32   ` Gary Hook
2014-11-13 18:55     ` Stefan Hajnoczi
2014-11-13 21:12       ` Gary R Hook
2014-11-13 16:43 Gary Hook
2014-11-12 18:48 Gary Hook
2014-11-12 20:27 ` Eric Blake
2014-11-13  7:57   ` Markus Armbruster
2014-11-13 11:20 ` Stefan Hajnoczi
2014-11-13 12:46   ` Markus Armbruster

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).