From nobody Thu Nov 6 01:06:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 153911892393179.71064935832123; Tue, 9 Oct 2018 14:02:03 -0700 (PDT) Received: from localhost ([::1]:53840 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z8k-0000fh-U9 for importer@patchew.org; Tue, 09 Oct 2018 17:02:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54457) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z3z-0005di-GE for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:08 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9z3y-0002dh-3J for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:07 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43838) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9z3x-0002dD-R6 for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:06 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2701DB2CD; Tue, 9 Oct 2018 20:57:05 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-105.ams2.redhat.com [10.36.116.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id A3A8F1001F40; Tue, 9 Oct 2018 20:57:02 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 9 Oct 2018 22:56:46 +0200 Message-Id: <20181009205652.10605-2-david@redhat.com> In-Reply-To: <20181009205652.10605-1-david@redhat.com> References: <20181009205652.10605-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 09 Oct 2018 20:57:05 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v1 1/7] qapi: correctly parse uint64_t values from strings X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , Michael Roth , Markus Armbruster , Igor Mammedov , "Dr . David Alan Gilbert" , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Right now, we parse uint64_t values just like int64_t values, resulting in negative values getting accepted and certain valid large numbers only being representable as negative numbers. Also, reported errors indicate that an int64_t is expected. Parse uin64_t separately. Implementation inspired by original parse_str() implementation. E.g. we can now specify -device nvdimm,memdev=3Dmem1,id=3Dnv1,addr=3D0xFFFFFFFFC0000000 Instead of going via negative values -device nvdimm,memdev=3Dmem1,id=3Dnv1,addr=3D-0x40000000 Resulting in the same values (qemu) info memory-devices Memory device [nvdimm]: "nv1" addr: 0xffffffffc0000000 slot: 0 node: 0 Signed-off-by: David Hildenbrand --- qapi/string-input-visitor.c | 117 ++++++++++++++++++++++++++++++++---- 1 file changed, 106 insertions(+), 11 deletions(-) diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index b3fdd0827d..af0a841152 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -19,6 +19,7 @@ #include "qapi/qmp/qnull.h" #include "qemu/option.h" #include "qemu/queue.h" +#include "qemu/cutils.h" #include "qemu/range.h" =20 =20 @@ -44,7 +45,8 @@ static void free_range(void *range, void *dummy) g_free(range); } =20 -static int parse_str(StringInputVisitor *siv, const char *name, Error **er= rp) +static int parse_str_int64(StringInputVisitor *siv, const char *name, + Error **errp) { char *str =3D (char *) siv->string; long long start, end; @@ -118,6 +120,75 @@ error: return -1; } =20 +static int parse_str_uint64(StringInputVisitor *siv, const char *name, + Error **errp) +{ + const char *str =3D (char *) siv->string; + uint64_t start, end; + const char *endptr; + Range *cur; + + if (siv->ranges) { + return 0; + } + + if (!*str) { + return 0; + } + + do { + if (!qemu_strtou64(str, &endptr, 0, &start)) { + if (*endptr =3D=3D '\0') { + cur =3D g_malloc0(sizeof(*cur)); + range_set_bounds(cur, start, start); + siv->ranges =3D range_list_insert(siv->ranges, cur); + cur =3D NULL; + str =3D NULL; + } else if (*endptr =3D=3D '-') { + str =3D endptr + 1; + if (!qemu_strtou64(str, &endptr, 0, &end) && start <=3D en= d) { + if (*endptr =3D=3D '\0') { + cur =3D g_malloc0(sizeof(*cur)); + range_set_bounds(cur, start, end); + siv->ranges =3D range_list_insert(siv->ranges, cur= ); + cur =3D NULL; + str =3D NULL; + } else if (*endptr =3D=3D ',') { + str =3D endptr + 1; + cur =3D g_malloc0(sizeof(*cur)); + range_set_bounds(cur, start, end); + siv->ranges =3D range_list_insert(siv->ranges, cur= ); + cur =3D NULL; + } else { + goto error; + } + } else { + goto error; + } + } else if (*endptr =3D=3D ',') { + str =3D endptr + 1; + cur =3D g_malloc0(sizeof(*cur)); + range_set_bounds(cur, start, start); + siv->ranges =3D range_list_insert(siv->ranges, cur); + cur =3D NULL; + } else { + goto error; + } + } else { + goto error; + } + } while (str); + + return 0; +error: + g_list_foreach(siv->ranges, free_range, NULL); + g_list_free(siv->ranges); + siv->ranges =3D NULL; + error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null", + "an uint64 value or range"); + return -1; +} + static void start_list(Visitor *v, const char *name, GenericList **list, size_t size, Error **errp) @@ -128,7 +199,7 @@ start_list(Visitor *v, const char *name, GenericList **= list, size_t size, assert(list); siv->list =3D list; =20 - if (parse_str(siv, name, errp) < 0) { + if (parse_str_int64(siv, name, errp) < 0) { *list =3D NULL; return; } @@ -216,7 +287,7 @@ static void parse_type_int64(Visitor *v, const char *na= me, int64_t *obj, { StringInputVisitor *siv =3D to_siv(v); =20 - if (parse_str(siv, name, errp) < 0) { + if (parse_str_int64(siv, name, errp) < 0) { return; } =20 @@ -252,15 +323,39 @@ error: static void parse_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp) { - /* FIXME: parse_type_int64 mishandles values over INT64_MAX */ - int64_t i; - Error *err =3D NULL; - parse_type_int64(v, name, &i, &err); - if (err) { - error_propagate(errp, err); - } else { - *obj =3D i; + StringInputVisitor *siv =3D to_siv(v); + + if (parse_str_uint64(siv, name, errp) < 0) { + return; + } + + if (!siv->ranges) { + goto error; + } + + if (!siv->cur_range) { + Range *r; + + siv->cur_range =3D g_list_first(siv->ranges); + if (!siv->cur_range) { + goto error; + } + + r =3D siv->cur_range->data; + if (!r) { + goto error; + } + + siv->cur =3D range_lob(r); } + + *obj =3D siv->cur; + siv->cur++; + return; + +error: + error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null", + "an uint64 value or range"); } =20 static void parse_type_size(Visitor *v, const char *name, uint64_t *obj, --=20 2.17.1 From nobody Thu Nov 6 01:06:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539118742416735.194229263674; Tue, 9 Oct 2018 13:59:02 -0700 (PDT) Received: from localhost ([::1]:53820 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z5p-0006fn-Ay for importer@patchew.org; Tue, 09 Oct 2018 16:59:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54474) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z41-0005eC-3S for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9z40-0002fF-G7 for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:09 -0400 Received: from mx1.redhat.com ([209.132.183.28]:49114) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9z40-0002eq-AF for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:08 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 800D0356E4; Tue, 9 Oct 2018 20:57:07 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-105.ams2.redhat.com [10.36.116.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7209C1001F40; Tue, 9 Oct 2018 20:57:05 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 9 Oct 2018 22:56:47 +0200 Message-Id: <20181009205652.10605-3-david@redhat.com> In-Reply-To: <20181009205652.10605-1-david@redhat.com> References: <20181009205652.10605-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 09 Oct 2018 20:57:07 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v1 2/7] qapi: use qemu_strtoi64() in parse_str_int64 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , Michael Roth , Markus Armbruster , Igor Mammedov , "Dr . David Alan Gilbert" , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The qemu api claims to be easier to use, and the resulting code shows that. Signed-off-by: David Hildenbrand --- qapi/string-input-visitor.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index af0a841152..ee38ab1d10 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -48,10 +48,10 @@ static void free_range(void *range, void *dummy) static int parse_str_int64(StringInputVisitor *siv, const char *name, Error **errp) { - char *str =3D (char *) siv->string; - long long start, end; + const char *str =3D (char *) siv->string; + const char *endptr; + int64_t start, end; Range *cur; - char *endptr; =20 if (siv->ranges) { return 0; @@ -62,9 +62,7 @@ static int parse_str_int64(StringInputVisitor *siv, const= char *name, } =20 do { - errno =3D 0; - start =3D strtoll(str, &endptr, 0); - if (errno =3D=3D 0 && endptr > str) { + if (!qemu_strtoi64(str, &endptr, 0, &start)) { if (*endptr =3D=3D '\0') { cur =3D g_malloc0(sizeof(*cur)); range_set_bounds(cur, start, start); @@ -73,11 +71,7 @@ static int parse_str_int64(StringInputVisitor *siv, cons= t char *name, str =3D NULL; } else if (*endptr =3D=3D '-') { str =3D endptr + 1; - errno =3D 0; - end =3D strtoll(str, &endptr, 0); - if (errno =3D=3D 0 && endptr > str && start <=3D end && - (start > INT64_MAX - 65536 || - end < start + 65536)) { + if (!qemu_strtoi64(str, &endptr, 0, &end) && start < end) { if (*endptr =3D=3D '\0') { cur =3D g_malloc0(sizeof(*cur)); range_set_bounds(cur, start, end); --=20 2.17.1 From nobody Thu Nov 6 01:06:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539119078190321.51724552043356; Tue, 9 Oct 2018 14:04:38 -0700 (PDT) Received: from localhost ([::1]:53851 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9zB6-0002Nn-5o for importer@patchew.org; Tue, 09 Oct 2018 17:04:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54487) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z43-0005fX-IE for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9z42-0002hu-Ml for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:11 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58526) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9z42-0002hP-GV for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:10 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D20803001754; Tue, 9 Oct 2018 20:57:09 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-105.ams2.redhat.com [10.36.116.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id CA8191001F40; Tue, 9 Oct 2018 20:57:07 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 9 Oct 2018 22:56:48 +0200 Message-Id: <20181009205652.10605-4-david@redhat.com> In-Reply-To: <20181009205652.10605-1-david@redhat.com> References: <20181009205652.10605-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Tue, 09 Oct 2018 20:57:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v1 3/7] range: pass const pointer where possible X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , Michael Roth , Markus Armbruster , Igor Mammedov , "Dr . David Alan Gilbert" , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" If there are no changes, let's use a const pointer. Signed-off-by: David Hildenbrand Reviewed-by: Dr. David Alan Gilbert --- include/qemu/range.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/qemu/range.h b/include/qemu/range.h index f28f0c1825..7e75f4e655 100644 --- a/include/qemu/range.h +++ b/include/qemu/range.h @@ -39,7 +39,7 @@ struct Range { uint64_t upb; /* inclusive upper bound */ }; =20 -static inline void range_invariant(Range *range) +static inline void range_invariant(const Range *range) { assert(range->lob <=3D range->upb || range->lob =3D=3D range->upb + 1); } @@ -48,14 +48,14 @@ static inline void range_invariant(Range *range) #define range_empty ((Range){ .lob =3D 1, .upb =3D 0 }) =20 /* Is @range empty? */ -static inline bool range_is_empty(Range *range) +static inline bool range_is_empty(const Range *range) { range_invariant(range); return range->lob > range->upb; } =20 /* Does @range contain @val? */ -static inline bool range_contains(Range *range, uint64_t val) +static inline bool range_contains(const Range *range, uint64_t val) { return val >=3D range->lob && val <=3D range->upb; } --=20 2.17.1 From nobody Thu Nov 6 01:06:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539118764630152.96682319473416; Tue, 9 Oct 2018 13:59:24 -0700 (PDT) Received: from localhost ([::1]:53822 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z6B-000723-GL for importer@patchew.org; Tue, 09 Oct 2018 16:59:23 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54513) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z4E-0005mx-1p for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:22 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9z45-0002jF-5F for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:19 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52556) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9z44-0002iq-Sl for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:13 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 343EC3083390; Tue, 9 Oct 2018 20:57:12 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-105.ams2.redhat.com [10.36.116.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2D31C1001F40; Tue, 9 Oct 2018 20:57:09 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 9 Oct 2018 22:56:49 +0200 Message-Id: <20181009205652.10605-5-david@redhat.com> In-Reply-To: <20181009205652.10605-1-david@redhat.com> References: <20181009205652.10605-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Tue, 09 Oct 2018 20:57:12 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v1 4/7] range: add some more functions X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , Michael Roth , Markus Armbruster , Igor Mammedov , "Dr . David Alan Gilbert" , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Add some more functions that will be used in memory-device context. range_init(): Init using lower bound and size range_valid(): Check if there would be an overflow when initializin range_size(): Extract the size of a range range_overlaps_range(): Check for overlaps of two ranges range_contains_range(): Check if one range is contained in the other range_starts_before_range(): Check if one range starts before another range_ends_after_range(): Check if one range ends after another Signed-off-by: David Hildenbrand --- include/qemu/range.h | 80 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/include/qemu/range.h b/include/qemu/range.h index 7e75f4e655..18e8acf22f 100644 --- a/include/qemu/range.h +++ b/include/qemu/range.h @@ -112,6 +112,86 @@ static inline uint64_t range_upb(Range *range) return range->upb; } =20 +/* + * Initialize @range to span the interval [@lob,@lob + @size - 1]. + * @size may be 0. + */ +static inline void range_init(Range *range, uint64_t lob, uint64_t size) +{ + range->lob =3D lob; + range->upb =3D lob + size - 1; + range_invariant(range); +} + +/* + * Check if the interval [@lob,@lob + @size - 1] would be valid or not + * (result in an overflow). + */ +static inline bool range_valid(uint64_t lob, uint64_t size) +{ + return lob + size >=3D lob; +} + +/* + * Get the size of @range. + */ +static inline uint64_t range_size(const Range *range) +{ + return range->upb - range->lob + 1; +} + +/* + * Check if @range1 overlaps with @range2. If one of the ranges is empty, + * the result is always "false". + */ +static inline bool range_overlaps_range(const Range *range1, + const Range *range2) +{ + if (range_is_empty(range1) || range_is_empty(range2)) { + return false; + } + return !(range2->upb < range1->lob || range1->upb < range2->lob); +} + +/* + * Check if @range1 contains @range2. If one of the ranges is empty, + * the result is always "false". + */ +static inline bool range_contains_range(const Range *range1, + const Range *range2) +{ + if (range_is_empty(range1) || range_is_empty(range2)) { + return false; + } + return range1->lob <=3D range2->lob && range1->upb >=3D range2->upb; +} + +/* + * Check if @range1 starts before @range2. If one of the ranges is empty, + * the result is alsways "false". + */ +static inline bool range_starts_before_range(const Range *range1, + const Range *range2) +{ + if (range_is_empty(range1) || range_is_empty(range2)) { + return false; + } + return range1->lob < range2->lob; +} + +/* + * Check if @range1 ends after @range2. If one of the ranges is empty, + * the result is alsways "false". + */ +static inline bool range_ends_after_range(const Range *range1, + const Range *range2) +{ + if (range_is_empty(range1) || range_is_empty(range2)) { + return false; + } + return range1->upb > range2->upb; +} + /* * Extend @range to the smallest interval that includes @extend_by, too. */ --=20 2.17.1 From nobody Thu Nov 6 01:06:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539118830662913.2723867612913; Tue, 9 Oct 2018 14:00:30 -0700 (PDT) Received: from localhost ([::1]:53826 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z7F-0007up-MB for importer@patchew.org; Tue, 09 Oct 2018 17:00:29 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54553) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z4N-0005vI-P1 for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9z4J-0002oR-Ow for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51774) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9z4C-0002kg-7n for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:24 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 69B9F31500B3; Tue, 9 Oct 2018 20:57:16 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-105.ams2.redhat.com [10.36.116.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id 82C6E1001F40; Tue, 9 Oct 2018 20:57:12 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 9 Oct 2018 22:56:50 +0200 Message-Id: <20181009205652.10605-6-david@redhat.com> In-Reply-To: <20181009205652.10605-1-david@redhat.com> References: <20181009205652.10605-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.41]); Tue, 09 Oct 2018 20:57:16 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v1 5/7] memory-device: use QEMU_IS_ALIGNED X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , Michael Roth , Markus Armbruster , Igor Mammedov , "Dr . David Alan Gilbert" , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Shorter and easier to read. Signed-off-by: David Hildenbrand Reviewed-by: Dr. David Alan Gilbert --- hw/mem/memory-device.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index 7de1ccd497..996ad1490f 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -120,7 +120,7 @@ static uint64_t memory_device_get_free_addr(MachineStat= e *ms, g_assert(address_space_end >=3D address_space_start); =20 /* address_space_start indicates the maximum alignment we expect */ - if (QEMU_ALIGN_UP(address_space_start, align) !=3D address_space_start= ) { + if (!QEMU_IS_ALIGNED(address_space_start, align)) { error_setg(errp, "the alignment (0x%" PRIx64 ") is not supported", align); return 0; @@ -131,13 +131,13 @@ static uint64_t memory_device_get_free_addr(MachineSt= ate *ms, return 0; } =20 - if (hint && QEMU_ALIGN_UP(*hint, align) !=3D *hint) { + if (hint && !QEMU_IS_ALIGNED(*hint, align)) { error_setg(errp, "address must be aligned to 0x%" PRIx64 " bytes", align); return 0; } =20 - if (QEMU_ALIGN_UP(size, align) !=3D size) { + if (!QEMU_IS_ALIGNED(size, align)) { error_setg(errp, "backend memory size must be multiple of 0x%" PRIx64, align); return 0; --=20 2.17.1 From nobody Thu Nov 6 01:06:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 153911892779937.437436634836786; Tue, 9 Oct 2018 14:02:07 -0700 (PDT) Received: from localhost ([::1]:53842 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z8o-0000js-Ct for importer@patchew.org; Tue, 09 Oct 2018 17:02:06 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54554) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z4N-0005vJ-P0 for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9z4M-0002pe-3e for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34928) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9z4L-0002ls-T3 for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:30 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A63F7307D84D; Tue, 9 Oct 2018 20:57:20 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-105.ams2.redhat.com [10.36.116.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id B2A0B1001F40; Tue, 9 Oct 2018 20:57:16 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 9 Oct 2018 22:56:51 +0200 Message-Id: <20181009205652.10605-7-david@redhat.com> In-Reply-To: <20181009205652.10605-1-david@redhat.com> References: <20181009205652.10605-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Tue, 09 Oct 2018 20:57:20 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v1 6/7] memory-device: avoid overflows on very huge devices X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , Michael Roth , Markus Armbruster , Igor Mammedov , "Dr . David Alan Gilbert" , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Should not be a problem right now, but it could theoretically happen in the future. Signed-off-by: David Hildenbrand --- hw/mem/memory-device.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index 996ad1490f..8be63c8032 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -85,7 +85,8 @@ static void memory_device_check_addable(MachineState *ms,= uint64_t size, =20 /* will we exceed the total amount of memory specified */ memory_device_used_region_size(OBJECT(ms), &used_region_size); - if (used_region_size + size > ms->maxram_size - ms->ram_size) { + if (used_region_size + size < used_region_size || + used_region_size + size > ms->maxram_size - ms->ram_size) { error_setg(errp, "not enough space, currently 0x%" PRIx64 " in use of total space for memory devices 0x" RAM_ADDR= _FMT, used_region_size, ms->maxram_size - ms->ram_size); --=20 2.17.1 From nobody Thu Nov 6 01:06:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1539118953057195.08272975008276; Tue, 9 Oct 2018 14:02:33 -0700 (PDT) Received: from localhost ([::1]:53844 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z9D-00011r-Ub for importer@patchew.org; Tue, 09 Oct 2018 17:02:31 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54551) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1g9z4N-0005vH-O6 for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1g9z4L-0002pW-Vi for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:31 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33560) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1g9z4L-0002mt-MD for qemu-devel@nongnu.org; Tue, 09 Oct 2018 16:57:29 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 08490C050002; Tue, 9 Oct 2018 20:57:23 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-105.ams2.redhat.com [10.36.116.105]) by smtp.corp.redhat.com (Postfix) with ESMTP id EFEA71001F40; Tue, 9 Oct 2018 20:57:20 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 9 Oct 2018 22:56:52 +0200 Message-Id: <20181009205652.10605-8-david@redhat.com> In-Reply-To: <20181009205652.10605-1-david@redhat.com> References: <20181009205652.10605-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 09 Oct 2018 20:57:23 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v1 7/7] memory-device: rewrite address assignment using ranges X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eduardo Habkost , "Michael S . Tsirkin" , David Hildenbrand , Michael Roth , Markus Armbruster , Igor Mammedov , "Dr . David Alan Gilbert" , David Gibson Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Let's rewrite it properly using ranges. This fixes certain overflows that are right now possible. E.g. qemu-system-x86_64 -m 4G,slots=3D20,maxmem=3D40G -M pc \ -object memory-backend-file,id=3Dmem1,share,mem-path=3D/dev/zero,size= =3D2G -device pc-dimm,memdev=3Dmem1,id=3Ddimm1,addr=3D-0x40000000 Now properly reports an error instead of succeeding. "can't add memory device [0xffffffffc0000000:0x80000000], range overflow" Signed-off-by: David Hildenbrand --- hw/mem/memory-device.c | 56 +++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/hw/mem/memory-device.c b/hw/mem/memory-device.c index 8be63c8032..1ca9c8e410 100644 --- a/hw/mem/memory-device.c +++ b/hw/mem/memory-device.c @@ -100,9 +100,8 @@ static uint64_t memory_device_get_free_addr(MachineStat= e *ms, uint64_t align, uint64_t size, Error **errp) { - uint64_t address_space_start, address_space_end; GSList *list =3D NULL, *item; - uint64_t new_addr =3D 0; + Range as, new =3D range_empty; =20 if (!ms->device_memory) { error_setg(errp, "memory devices (e.g. for memory hotplug) are not= " @@ -115,13 +114,11 @@ static uint64_t memory_device_get_free_addr(MachineSt= ate *ms, "enabled, please specify the maxmem option"); return 0; } - address_space_start =3D ms->device_memory->base; - address_space_end =3D address_space_start + - memory_region_size(&ms->device_memory->mr); - g_assert(address_space_end >=3D address_space_start); + range_init(&as, ms->device_memory->base, + memory_region_size(&ms->device_memory->mr)); =20 - /* address_space_start indicates the maximum alignment we expect */ - if (!QEMU_IS_ALIGNED(address_space_start, align)) { + /* start of address space indicates the maximum alignment we expect */ + if (!QEMU_IS_ALIGNED(range_lob(&as), align)) { error_setg(errp, "the alignment (0x%" PRIx64 ") is not supported", align); return 0; @@ -145,20 +142,26 @@ static uint64_t memory_device_get_free_addr(MachineSt= ate *ms, } =20 if (hint) { - new_addr =3D *hint; - if (new_addr < address_space_start) { + if (!range_valid(*hint, size)) { error_setg(errp, "can't add memory device [0x%" PRIx64 ":0x%" = PRIx64 - "] before 0x%" PRIx64, new_addr, size, - address_space_start); + "], range overflow", *hint, size); return 0; - } else if ((new_addr + size) > address_space_end) { + } + range_init(&new, *hint, size); + if (range_starts_before_range(&new, &as)) { + error_setg(errp, "can't add memory device [0x%" PRIx64 ":0x%" = PRIx64 + "] before 0x%" PRIx64, range_lob(&new), range_size(= &new), + range_lob(&as)); + return 0; + } else if (range_ends_after_range(&new, &as)) { error_setg(errp, "can't add memory device [0x%" PRIx64 ":0x%" = PRIx64 - "] beyond 0x%" PRIx64, new_addr, size, - address_space_end); + "] beyond 0x%" PRIx64, range_lob(&new), range_size(= &new), + range_upb(&as)); return 0; } } else { - new_addr =3D address_space_start; + /* our previous size checks make sure that this never overflows */ + range_init(&new, range_lob(&as), size); } =20 /* find address range that will fit new memory device */ @@ -166,30 +169,37 @@ static uint64_t memory_device_get_free_addr(MachineSt= ate *ms, for (item =3D list; item; item =3D g_slist_next(item)) { const MemoryDeviceState *md =3D item->data; const MemoryDeviceClass *mdc =3D MEMORY_DEVICE_GET_CLASS(OBJECT(md= )); - uint64_t md_size, md_addr; + uint64_t next_addr; + Range tmp; =20 - md_addr =3D mdc->get_addr(md); - md_size =3D memory_device_get_region_size(md, &error_abort); + range_init(&tmp, mdc->get_addr(md), + memory_device_get_region_size(md, &error_abort)); =20 - if (ranges_overlap(md_addr, md_size, new_addr, size)) { + if (range_overlaps_range(&tmp, &new)) { if (hint) { const DeviceState *d =3D DEVICE(md); error_setg(errp, "address range conflicts with memory devi= ce" " id=3D'%s'", d->id ? d->id : "(unnamed)"); goto out; } - new_addr =3D QEMU_ALIGN_UP(md_addr + md_size, align); + + next_addr =3D QEMU_ALIGN_UP(range_upb(&tmp) + 1, align); + if (!range_valid(next_addr, size)) { + range_make_empty(&new); + break; + } + range_init(&new, next_addr, size); } } =20 - if (new_addr + size > address_space_end) { + if (!range_contains_range(&as, &new)) { error_setg(errp, "could not find position in guest address space f= or " "memory device - memory fragmented due to alignments"); goto out; } out: g_slist_free(list); - return new_addr; + return range_lob(&new); } =20 MemoryDeviceInfoList *qmp_memory_device_list(void) --=20 2.17.1