{"id":810765,"url":"http://patchwork.ozlabs.org/api/patches/810765/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170906184133.25524-4-dgilbert@redhat.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<20170906184133.25524-4-dgilbert@redhat.com>","list_archive_url":null,"date":"2017-09-06T18:41:28","name":"[PULL,3/8] host-utils: Proactively fix pow2floor(), switch to unsigned","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"d0811dcfa2d0aa1707ca5f6a0d76cde9e567123e","submitter":{"id":48102,"url":"http://patchwork.ozlabs.org/api/people/48102/?format=json","name":"Dr. David Alan Gilbert","email":"dgilbert@redhat.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/20170906184133.25524-4-dgilbert@redhat.com/mbox/","series":[{"id":1865,"url":"http://patchwork.ozlabs.org/api/series/1865/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=1865","date":"2017-09-06T18:41:26","name":"[PULL,1/8] migration: Report when bdrv_inactivate_all fails","version":1,"mbox":"http://patchwork.ozlabs.org/series/1865/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/810765/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/810765/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n\tspf=pass (mailfrom) smtp.mailfrom=nongnu.org\n\t(client-ip=2001:4830:134:3::11; helo=lists.gnu.org;\n\tenvelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n\treceiver=<UNKNOWN>)","ext-mx05.extmail.prod.ext.phx2.redhat.com;\n\tdmarc=none (p=none dis=none) header.from=redhat.com","ext-mx05.extmail.prod.ext.phx2.redhat.com;\n\tspf=fail smtp.mailfrom=dgilbert@redhat.com"],"Received":["from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11])\n\t(using TLSv1 with cipher AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 3xnXbd17hfz9sRY\n\tfor <incoming@patchwork.ozlabs.org>;\n\tThu,  7 Sep 2017 04:46:05 +1000 (AEST)","from localhost ([::1]:37482 helo=lists.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.71) (envelope-from\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1dpfKt-0006Qh-7i\n\tfor incoming@patchwork.ozlabs.org; Wed, 06 Sep 2017 14:46:03 -0400","from eggs.gnu.org ([2001:4830:134:3::10]:57635)\n\tby lists.gnu.org with esmtp (Exim 4.71)\n\t(envelope-from <dgilbert@redhat.com>) id 1dpfGk-0002z1-NN\n\tfor qemu-devel@nongnu.org; Wed, 06 Sep 2017 14:41:48 -0400","from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71)\n\t(envelope-from <dgilbert@redhat.com>) id 1dpfGj-0001ia-HH\n\tfor qemu-devel@nongnu.org; Wed, 06 Sep 2017 14:41:46 -0400","from mx1.redhat.com ([209.132.183.28]:39890)\n\tby eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32)\n\t(Exim 4.71) (envelope-from <dgilbert@redhat.com>) id 1dpfGj-0001gX-9A\n\tfor qemu-devel@nongnu.org; Wed, 06 Sep 2017 14:41:45 -0400","from smtp.corp.redhat.com\n\t(int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15])\n\t(using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits))\n\t(No client certificate requested)\n\tby mx1.redhat.com (Postfix) with ESMTPS id 52850155DF\n\tfor <qemu-devel@nongnu.org>; Wed,  6 Sep 2017 18:41:44 +0000 (UTC)","from dgilbert-t530.redhat.com (ovpn-117-207.ams2.redhat.com\n\t[10.36.117.207])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id F1F395D6A0;\n\tWed,  6 Sep 2017 18:41:42 +0000 (UTC)"],"DMARC-Filter":"OpenDMARC Filter v1.3.2 mx1.redhat.com 52850155DF","From":"\"Dr. David Alan Gilbert (git)\" <dgilbert@redhat.com>","To":"qemu-devel@nongnu.org","Date":"Wed,  6 Sep 2017 19:41:28 +0100","Message-Id":"<20170906184133.25524-4-dgilbert@redhat.com>","In-Reply-To":"<20170906184133.25524-1-dgilbert@redhat.com>","References":"<20170906184133.25524-1-dgilbert@redhat.com>","X-Scanned-By":"MIMEDefang 2.79 on 10.5.11.15","X-Greylist":"Sender IP whitelisted, not delayed by milter-greylist-4.5.16\n\t(mx1.redhat.com [10.5.110.29]);\n\tWed, 06 Sep 2017 18:41:44 +0000 (UTC)","X-detected-operating-system":"by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic]\n\t[fuzzy]","X-Received-From":"209.132.183.28","Subject":"[Qemu-devel] [PULL 3/8] host-utils: Proactively fix pow2floor(),\n\tswitch to unsigned","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.21","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<http://lists.nongnu.org/archive/html/qemu-devel/>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n\t<mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"kwolf@redhat.com, armbru@redhat.com, peterx@redhat.com,\n\tquintela@redhat.com","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n\t<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"From: Markus Armbruster <armbru@redhat.com>\n\nThe function's stated contract is simple enough: \"round down to the\nnearest power of 2\".  Suggests the domain is the representable numbers\n>= 1, because that's the smallest power of two.\n\nThe implementation doesn't check for domain errors, but returns\ngarbage instead:\n\n* For negative arguments, pow2floor() returns -2^63, which is not even\n  a power of two, let alone the nearest one.\n\n  What sort of works is passing *unsigned* arguments >= 2^63.  The\n  implicit conversion to signed is implementation defined, but\n  commonly yields the (negative) two's complement.  pow2floor() then\n  returns -2^63.  Callers that convert that back to unsigned get the\n  correct value 2^63.\n\n* For a zero argument, pow2floor() shifts right by 64.  Undefined\n  behavior.  Common actual behavior is to shift by 0, yielding -2^63.\n\nFix by switching from int64_t to uint64_t and amending the contract to\nmap zero to zero.\n\nCallers are fine with that:\n\n* memory_access_size()\n\n  This function makes no sense unless the argument is positive and the\n  return value fits into int.\n\n* raw_refresh_limits()\n\n  Passes an int between 1 and BDRV_REQUEST_MAX_BYTES.\n\n* iscsi_refresh_limits()\n\n  Passes an integer between 0 and INT_MAX, converts the result to\n  uint32_t.  Passing zero would be undefined behavior, but commonly\n  yield zero.  The patch gives us the zero without the undefined\n  behavior.\n\n* cache_init()\n\n  Passes a positive int64_t argument.\n\n* xbzrle_cache_resize()\n\n  Passes a positive int64_t argument (>= TARGET_PAGE_SIZE, actually).\n\n* spapr_node0_size()\n\n  Passes a positive uint64_t argument, and converts the result to\n  hwaddr, i.e. uint64_t.\n\n* spapr_populate_memory()\n\n  Passes a positive hwaddr argument, and converts the result to\n  hwaddr.\n\nCc: Juan Quintela <quintela@redhat.com>\nCc: Dr. David Alan Gilbert <dgilbert@redhat.com>\nCc: Eric Blake <eblake@redhat.com>\nCc: Peter Maydell <peter.maydell@linaro.org>\nCc: Alexey Kardashevskiy <aik@ozlabs.ru>\nSigned-off-by: Markus Armbruster <armbru@redhat.com>\nMessage-Id: <1501148776-16890-3-git-send-email-armbru@redhat.com>\nReviewed-by: Eric Blake <eblake@redhat.com>\nSigned-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>\n---\n include/qemu/host-utils.h | 13 ++++++++-----\n 1 file changed, 8 insertions(+), 5 deletions(-)","diff":"diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h\nindex 95cf4f4163..6c6005f5cf 100644\n--- a/include/qemu/host-utils.h\n+++ b/include/qemu/host-utils.h\n@@ -369,13 +369,16 @@ static inline bool is_power_of_2(uint64_t value)\n     return !(value & (value - 1));\n }\n \n-/* round down to the nearest power of 2*/\n-static inline int64_t pow2floor(int64_t value)\n+/**\n+ * Return @value rounded down to the nearest power of two or zero.\n+ */\n+static inline uint64_t pow2floor(uint64_t value)\n {\n-    if (!is_power_of_2(value)) {\n-        value = 0x8000000000000000ULL >> clz64(value);\n+    if (!value) {\n+        /* Avoid undefined shift by 64 */\n+        return 0;\n     }\n-    return value;\n+    return 0x8000000000000000ull >> clz64(value);\n }\n \n /* round up to the nearest power of 2 (0 if overflow) */\n","prefixes":["PULL","3/8"]}