Received: by 2002:a17:907:d504:b0:9b2:89ee:1eb8 with SMTP id wb4csp3367191ejc; Fri, 29 Sep 2023 09:17:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFnUlYeUbvlBfwxCg5ALk1WLM5W991L35nyB7O/wn2845PQsyo5TJ+b4DTkT+Kk0HRazZPb X-Received: by 2002:a05:620a:2591:b0:770:e72b:82c with SMTP id x17-20020a05620a259100b00770e72b082cmr4652050qko.29.1696004241310; Fri, 29 Sep 2023 09:17:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696004241; cv=none; d=google.com; s=arc-20160816; b=Sc5DQ7OCX9/UjyoUJ+MMAaLWd5kbgtHPUJCyWJ1WWvF2DpfoTLnJjgZcFK7Wr3WMOF uBhE1uLaUxTGdRnra3Z/Xa5duTzjamaqL2ywRXVUJXKjhyfPVfhihWsTB5AlTTzyEovZ ZLbTOW706IsyxxSlAD0DLCXqwor/hxALaEqEV6g8TOxt6+ZPm9AMrQ2IpPnQa8t1neBl /ky0eIo4LAx6WMn9CSWOU2wZDTgSBsL45kFjOVkXAo/TeEj9SUcI2AomkcZpRVr2VkKJ +CDGZV7Ym/ODZIXNgtQcsTyNHKKHLgSrjwD0nknSyZ4G2a6iiqRVPZHfg4hge6C7yA5U rDDw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=xh1o3yh9k8s8Du3ioGU6aZkjuIl2+uYmVsN67dgAZrQ=; fh=bFpS87vmp7mzhl4ICK4mDM62TwFUbgTYVuM7QVH8Hes=; b=FEYPdNWi7HuvbSoYzSGs/wRFpUJlNs/RRbXs+M44UD4LwvGVR+4y1ZixU1kf1Oyp/D /nJN6upmt0BMbSW25Go17yLBNAHz4f8uGSlCbUjgWgjooZXEjrz33zK2rCLcoNTqXrFh qdId9cyMhyhAW4gNWqJMPqZt5cb1V2n5v/D/PTI2Oq1VZ3SH04ilnobdhu2Txy0bd3Pi PxX4LMqWWruzWkIj4PwzIUQeURsnZ8KWY5j39lOzKyUQZ+p904or5PjX8Tca7wz4MudH mBkpmbmmOL3TF1590U7jR+0QkXKIgqx6LUyncAnLXMywCSmLxDokbwrVnCHKp8le8w2H Fkrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sz3a+wWQ; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id p5-20020a05620a056500b0077411b34546si9205085qkp.243.2023.09.29.09.17.21 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 29 Sep 2023 09:17:21 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=sz3a+wWQ; spf=pass (google.com: domain of qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qmGAm-00032W-Kp; Fri, 29 Sep 2023 12:17:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qmGAe-0002ww-0i for qemu-arm@nongnu.org; Fri, 29 Sep 2023 12:16:52 -0400 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qmGAZ-0006Km-2c for qemu-arm@nongnu.org; Fri, 29 Sep 2023 12:16:50 -0400 Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-3231d67aff2so10674656f8f.0 for ; Fri, 29 Sep 2023 09:16:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1696004205; x=1696609005; darn=nongnu.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=xh1o3yh9k8s8Du3ioGU6aZkjuIl2+uYmVsN67dgAZrQ=; b=sz3a+wWQC0wJdUYiI3JrdQ8uQAIsTJTdvWZiXO2w+0VLZZUTgGfD+KfsO5i67Bto49 SwSzcoaWyIMQG5B6t/WWSPf/BN92YsBtQApD1UDXnMXNMzgFEiTVAUF7YwKuDoFK9hOL CT5DGyZc6JGGWxdAVlOn4IRKjO8y36RpmyvDOlVnV+Gtd/UbZzTjg75dRn2nNeXZ+sGB 2XYXsaO2bPt5OOjiphk+97/wRHvAPz5RMmr+cqvcJW+KqMAyNKzc8kzKo3HbZuKrtMHc TMNPoM+YFakSyYQYK84mGY7UzGSh3ZNX8+B5QUx9OUeG+Jw1ERU7kKGV2jNmkIbrvkIV rfEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696004205; x=1696609005; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=xh1o3yh9k8s8Du3ioGU6aZkjuIl2+uYmVsN67dgAZrQ=; b=u2P8+C5HYWBaoZNgwXazEgL2q5wn2o7/MXEFf52VzrCgpgrudRcISlwJzz4mGIt+qL vXTlS5nfwkwnqM3IPB7cdt/mPZo6A8QMI2NPadMrwoGmELsFqQpY+SjLNqu7R556BYcr UR9/q/ruvhVPnug13hU2jykBZ7N2mEilK4zSch8RG5TcXMAQ1k0Im4hbcjE90VNygEvq GKOSQ2h2s6y2wYtyfwuNwl67i1pgC1nnaGwNTnB4nKKQ3ES18Zvrkz57Q4oXlPSRqsMw xKaStsyyObSGzBlszV8BgN/ZLubRYDfRjNglSVOEBS7Ub0Yh6iGfXHhNe1Jo0+IhKCZa o8VA== X-Gm-Message-State: AOJu0YykC+8jt60z67UZNUD7dmVBUvowgIWiF5kYlpu43F2EcgV91gTJ NvRB1bwnKbxfn6lP1H1uqywXzQ== X-Received: by 2002:a5d:460a:0:b0:317:57f0:fae with SMTP id t10-20020a5d460a000000b0031757f00faemr4026677wrq.63.1696004205163; Fri, 29 Sep 2023 09:16:45 -0700 (PDT) Received: from myrica ([2a02:c7c:7290:b00:fd32:2b31:6755:400c]) by smtp.gmail.com with ESMTPSA id m14-20020a5d64ae000000b0031aca6cc69csm11631083wrp.2.2023.09.29.09.16.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 09:16:44 -0700 (PDT) Date: Fri, 29 Sep 2023 17:16:49 +0100 From: Jean-Philippe Brucker To: Eric Auger Cc: eric.auger.pro@gmail.com, qemu-devel@nongnu.org, qemu-arm@nongnu.org, alex.williamson@redhat.com, clg@redhat.com, mst@redhat.com, pbonzini@redhat.com, peter.maydell@linaro.org, peterx@redhat.com, david@redhat.com, philmd@linaro.org Subject: Re: [PATCH v2 09/12] util/reserved-region: Add new ReservedRegion helpers Message-ID: <20230929161649.GC2957297@myrica> References: <20230913080423.523953-1-eric.auger@redhat.com> <20230913080423.523953-10-eric.auger@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230913080423.523953-10-eric.auger@redhat.com> Received-SPF: pass client-ip=2a00:1450:4864:20::42c; envelope-from=jean-philippe@linaro.org; helo=mail-wr1-x42c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-arm@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org Sender: qemu-arm-bounces+alex.bennee=linaro.org@nongnu.org X-TUID: AHopPgLV461Y On Wed, Sep 13, 2023 at 10:01:44AM +0200, Eric Auger wrote: > Introduce resv_region_list_insert() helper which inserts > a new ReservedRegion into a sorted list of reserved region. > In case of overlap, the new region has higher priority and > hides the existing overlapped segments. If the overlap is > partial, new regions are created for parts which are not > overlapped. The new region has higher priority independently > on the type of the regions. > > Signed-off-by: Eric Auger > --- > include/qemu/reserved-region.h | 32 ++++++++++++ > util/reserved-region.c | 94 ++++++++++++++++++++++++++++++++++ > util/meson.build | 1 + > 3 files changed, 127 insertions(+) > create mode 100644 include/qemu/reserved-region.h > create mode 100644 util/reserved-region.c > > diff --git a/include/qemu/reserved-region.h b/include/qemu/reserved-region.h > new file mode 100644 > index 0000000000..8e6f0a97e2 > --- /dev/null > +++ b/include/qemu/reserved-region.h > @@ -0,0 +1,32 @@ > +/* > + * QEMU ReservedRegion helpers > + * > + * Copyright (c) 2023 Red Hat, Inc. > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public > + * License as published by the Free Software Foundation; either > + * version 2 of the License, or (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * General Public License for more details. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program; if not, see . > + */ > + > +#ifndef QEMU_RESERVED_REGION_H > +#define QEMU_RESERVED_REGION_H > + > +#include "exec/memory.h" > + > +/* > + * Insert a new region into a sorted list of reserved regions. In case > + * there is overlap with existing regions, the new added region has > + * higher priority and replaces the overlapped segment. > + */ > +GList *resv_region_list_insert(GList *list, ReservedRegion *reg); > + > +#endif > diff --git a/util/reserved-region.c b/util/reserved-region.c > new file mode 100644 > index 0000000000..bb26a6bed3 > --- /dev/null > +++ b/util/reserved-region.c > @@ -0,0 +1,94 @@ > +/* > + * QEMU ReservedRegion helpers > + * > + * Copyright (c) 2023 Red Hat, Inc. > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public > + * License as published by the Free Software Foundation; either > + * version 2 of the License, or (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU > + * General Public License for more details. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program; if not, see . > + */ > + > +#include "qemu/osdep.h" > +#include "qemu/range.h" > +#include "qemu/reserved-region.h" > + > +GList *resv_region_list_insert(GList *list, ReservedRegion *reg) > +{ > + ReservedRegion *resv_iter, *new_reg; > + Range *r = ®->range; > + Range *range_iter; > + GList *l; > + > + for (l = list; l ; ) { > + resv_iter = (ReservedRegion *)l->data; > + range_iter = &resv_iter->range; > + > + /* Skip all list elements strictly less than range to add */ > + if (range_compare(range_iter, r) < 0) { > + l = l->next; > + } else if (range_compare(range_iter, r) > 0) { > + return g_list_insert_before(list, l, reg); > + } else { /* there is an operlap */ I guess this could just be 'else if', and the whole block below unindented. But I don't know if the function would look less or more scary :) > + if (range_contains_range(r, range_iter)) { > + /* new range contains current item, simply remove this latter */ > + GList *prev = l->prev; > + g_free(l->data); > + list = g_list_delete_link(list, l); > + if (prev) { These four lines could just be 'l = prev->next'. > + l = prev; > + if (l) { > + l = l->next; > + } > + } else { > + l = list; > + } > + } else if (range_contains_range(range_iter, r)) { > + /* new region is included in the current region */ > + if (range_lob(range_iter) == range_lob(r)) { > + /* adjacent on the left side, derives into 2 regions */ > + range_set_bounds(range_iter, range_upb(r) + 1, > + range_upb(range_iter)); > + return g_list_insert_before(list, l, reg); > + } else if (range_upb(range_iter) == range_upb(r)) { > + /* adjacent on the right side, derives into 2 regions */ > + range_set_bounds(range_iter, range_lob(range_iter), > + range_lob(r) - 1); > + l = l->next; > + } else { > + uint64_t lob = range_lob(range_iter); > + /* > + * the new range is in the middle of an existing one, > + * split this latter into 3 regs instead > + */ > + range_set_bounds(range_iter, range_upb(r) + 1, > + range_upb(range_iter)); > + new_reg = g_new0(ReservedRegion, 1); > + new_reg->type = resv_iter->type; > + range_set_bounds(&new_reg->range, > + lob, range_lob(r) - 1); > + list = g_list_insert_before(list, l, new_reg); > + return g_list_insert_before(list, l, reg); > + } > + } else if (range_lob(r) < range_lob(range_iter)) { > + range_set_bounds(range_iter, range_upb(r) + 1, > + range_upb(range_iter)); > + return g_list_insert_before(list, l, reg); > + } else { /* intersection on the upper range */ > + range_set_bounds(range_iter, range_lob(range_iter), > + range_lob(r) - 1); > + l = l->next; > + } > + } /* overlap */ > + } > + return g_list_append(list, reg); Looks correct overall Reviewed-by: Jean-Philippe Brucker > +} > + > diff --git a/util/meson.build b/util/meson.build > index c4827fd70a..eb677b40c2 100644 > --- a/util/meson.build > +++ b/util/meson.build > @@ -51,6 +51,7 @@ util_ss.add(files('qdist.c')) > util_ss.add(files('qht.c')) > util_ss.add(files('qsp.c')) > util_ss.add(files('range.c')) > +util_ss.add(files('reserved-region.c')) > util_ss.add(files('stats64.c')) > util_ss.add(files('systemd.c')) > util_ss.add(files('transactions.c')) > -- > 2.41.0 >