From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044339; cv=none; d=zohomail.com; s=zohoarc; b=SJsE4GJ+5J1enutQKAv1AqdXqzHdsj1cHVQ/OU1WNyg7eDINPJerdC6swt72UrhkHbdrxQH9iyFO0PBbeottqcYcvZclr+EnWWpNqQQz+FLrTgWzZD3p1oK0LSIin5pe71DYXc60qlcZvlWOztck3wwv9ZAkM7DxkKgNf3ZeD6U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044339; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=VljO4/V5RF5GdhSHEc7pp4EFkxkOTq4sQuAyZ0els/g=; b=B7Z6rwPOoCdxks3YfCluyqZ+an3Vw5Yh5Bqc4ndolWswFRJ7CwLJ4sEOOxClDZB/iz2R+OfJGYSWfP9DiFzlvrJcptnwncLq9Sqbn3b1Uw/wUGFxPbg/KLrebRgKmtjhA9q2G24OjdebNp/xpUwklXpVM9Mo0Q5f+56ENZddDEE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 168004433944835.26607325035525; Tue, 28 Mar 2023 15:58:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGe-0006rj-6K; Tue, 28 Mar 2023 18:58:16 -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 1phIGd-0006r8-6r for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:15 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGY-00072r-6K for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:14 -0400 Received: by mail-pl1-x62c.google.com with SMTP id f22so9048807plr.0 for ; Tue, 28 Mar 2023 15:58:09 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044289; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VljO4/V5RF5GdhSHEc7pp4EFkxkOTq4sQuAyZ0els/g=; b=yrfIfdDGKjojdGNqjz9dCgMIX+Fjws3czkljYJT3cBXMiJU78SPxU2frGEFuY7MyIu AGDtlsrmyJn8E4AXHtTcEUBTxjY1HIJKIDu9kukBuWHic04YmKJGENS9fCvmwqiefF6y Hbge5dHZPl1cdzs0o7nKvoxSzZGVNJ2WzClIfgyZxf91BgikknY2bhrwIuQMyTxf0low 1XRueXfJq3j9X8N4XlME8kQlG4wWRQkPMsAC4O91DUQFd/2cgJha+zZz63JgxNVgQhE/ n8pcRtRPYtCtMobhiWWLfHTp9Tjxxu5MfuVvY/ZtbLo6kdJoc7R05hfggL767+SrhMFL qFtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044289; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VljO4/V5RF5GdhSHEc7pp4EFkxkOTq4sQuAyZ0els/g=; b=z5c3LhBzTADXxq6PcM3vMbDpVXjJxzuvbv9pkX4B4XkclwOXSWPv0StQsphT6LUQ/f qDGAWRoZJANM9XqVGPXOwE4mAhuQmUpRlsh7Cm/Ca4J1Axzr7ZuqfHaQdUqTqn8mVRKD s5KvNbkFsxjEibxNJBhMBUfOr8GtmBWmJ4n5Y1ZRDsL4PpfrJ2pY//YHhD1WV4M9WOAs 9c+sKJP0hgFwJ7oM69zaxrBwqkJrPIS70qKHQAT5p3IhtWw+RfwTtzMsrjHFSf9Ca9Z4 7ixgQ9ZN08L1GkR7SNFPAi5LpZWOJ1nqQAzm+gwAf2bYpwuu3IL2ICXjbpZHuog3lVpO WRfw== X-Gm-Message-State: AAQBX9cChlW7xKU1FjERI5LVPjbjwkaciFqiAM3srYVVY/fCzVfZps6L e4BggZnTG56+/EBCkI8nQl633vhOnIUGR89WesI= X-Google-Smtp-Source: AKy350ZMG834nT/tUiPKGYg29lnWxEuYd1FsJUZkhdPTYrO1oa6Ar3bMJu1kFGckqvwfa+P5yFOeWQ== X-Received: by 2002:a17:90b:3b4f:b0:234:2485:6743 with SMTP id ot15-20020a17090b3b4f00b0023424856743mr18618265pjb.3.1680044288477; Tue, 28 Mar 2023 15:58:08 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, Emilio Cota Subject: [PULL 01/15] util: import GTree as QTree Date: Tue, 28 Mar 2023 15:57:52 -0700 Message-Id: <20230328225806.2278728-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62c.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044340576100009 From: Emilio Cota The only reason to add this implementation is to control the memory allocat= or used. Some users (e.g. TCG) cannot work reliably in multi-threaded environments (e.g. forking in user-mode) with GTree's allocator, GSlice. See https://gitlab.com/qemu-project/qemu/-/issues/285 for details. Importing GTree is a temporary workaround until GTree migrates away from GSlice. This implementation is identical to that in glib v2.75.0, except that we don't import recent additions to the API nor deprecated API calls, none of which are used in QEMU. I've imported tests from glib and added a benchmark just to make sure that performance is similar. Note: it cannot be identical because (1) we are not using GSlice, (2) we use different compilation flags (e.g. -fPIC) and (3) we're linking statically. $ cat /proc/cpuinfo| grep 'model name' | head -1 model name : AMD Ryzen 7 PRO 5850U with Radeon Graphics $ echo '0' | sudo tee /sys/devices/system/cpu/cpufreq/boost $ tests/bench/qtree-bench Tree Op 32 1024 4096 131072 = 1048576 ---------------------------------------------------------------------------= --------------------- GTree Lookup 83.23 43.08 25.31 19.40 = 16.22 QTree Lookup 113.42 (1.36x) 53.83 (1.25x) 28.38 (1.12x) 17.64 (0= .91x) 13.04 (0.80x) GTree Insert 44.23 29.37 25.83 19.49 = 17.03 QTree Insert 46.87 (1.06x) 25.62 (0.87x) 24.29 (0.94x) 16.83 (0= .86x) 12.97 (0.76x) GTree Remove 53.27 35.15 31.43 24.64 = 16.70 QTree Remove 57.32 (1.08x) 41.76 (1.19x) 38.37 (1.22x) 29.30 (1= .19x) 15.07 (0.90x) GTree RemoveAll 135.44 127.52 126.72 120.11 = 64.34 QTree RemoveAll 127.15 (0.94x) 110.37 (0.87x) 107.97 (0.85x) 97.13 (0= .81x) 55.10 (0.86x) GTree Traverse 277.71 276.09 272.78 246.72 = 98.47 QTree Traverse 370.33 (1.33x) 411.97 (1.49x) 400.23 (1.47x) 262.82 (1= .07x) 78.52 (0.80x) ---------------------------------------------------------------------------= --------------------- As a sanity check, the same benchmark when Glib's version is >=3D $glib_dropped_gslice_version (i.e. QTree =3D=3D GTree): Tree Op 32 1024 4096 131072 = 1048576 ---------------------------------------------------------------------------= --------------------- GTree Lookup 82.72 43.09 24.18 19.73 = 16.09 QTree Lookup 81.82 (0.99x) 43.10 (1.00x) 24.20 (1.00x) 19.76 (1= .00x) 16.26 (1.01x) GTree Insert 45.07 29.62 26.34 19.90 = 17.18 QTree Insert 45.72 (1.01x) 29.60 (1.00x) 26.38 (1.00x) 19.71 (0= .99x) 17.20 (1.00x) GTree Remove 54.48 35.36 31.77 24.97 = 16.95 QTree Remove 54.46 (1.00x) 35.32 (1.00x) 31.77 (1.00x) 24.91 (1= .00x) 17.15 (1.01x) GTree RemoveAll 140.68 127.36 125.43 121.45 = 68.20 QTree RemoveAll 140.65 (1.00x) 127.64 (1.00x) 125.01 (1.00x) 121.73 (1= .00x) 67.06 (0.98x) GTree Traverse 278.68 276.05 266.75 251.65 = 104.93 QTree Traverse 278.31 (1.00x) 275.78 (1.00x) 266.42 (1.00x) 247.89 (0= .99x) 104.58 (1.00x) ---------------------------------------------------------------------------= --------------------- Signed-off-by: Emilio Cota Message-Id: <20230205163758.416992-2-cota@braap.org> Signed-off-by: Richard Henderson --- configure | 15 + meson.build | 4 + include/qemu/qtree.h | 201 ++++++ tests/bench/qtree-bench.c | 286 ++++++++ tests/unit/test-qtree.c | 333 +++++++++ util/qtree.c | 1390 +++++++++++++++++++++++++++++++++++++ tests/bench/meson.build | 4 + tests/unit/meson.build | 1 + util/meson.build | 1 + 9 files changed, 2235 insertions(+) create mode 100644 include/qemu/qtree.h create mode 100644 tests/bench/qtree-bench.c create mode 100644 tests/unit/test-qtree.c create mode 100644 util/qtree.c diff --git a/configure b/configure index 05bed4f4a1..800b5850f4 100755 --- a/configure +++ b/configure @@ -231,6 +231,7 @@ safe_stack=3D"" use_containers=3D"yes" gdb_bin=3D$(command -v "gdb-multiarch" || command -v "gdb") gdb_arches=3D"" +glib_has_gslice=3D"no" =20 if test -e "$source_path/.git" then @@ -1494,6 +1495,17 @@ for i in $glib_modules; do fi done =20 +# Check whether glib has gslice, which we have to avoid for correctness. +# TODO: remove this check and the corresponding workaround (qtree) when +# the minimum supported glib is >=3D $glib_dropped_gslice_version. +glib_dropped_gslice_version=3D2.75.3 +for i in $glib_modules; do + if ! $pkg_config --atleast-version=3D$glib_dropped_gslice_version $i; = then + glib_has_gslice=3D"yes" + break + fi +done + glib_bindir=3D"$($pkg_config --variable=3Dbindir glib-2.0)" if test -z "$glib_bindir" ; then glib_bindir=3D"$($pkg_config --variable=3Dprefix glib-2.0)"/bin @@ -2420,6 +2432,9 @@ echo "GLIB_CFLAGS=3D$glib_cflags" >> $config_host_mak echo "GLIB_LIBS=3D$glib_libs" >> $config_host_mak echo "GLIB_BINDIR=3D$glib_bindir" >> $config_host_mak echo "GLIB_VERSION=3D$($pkg_config --modversion glib-2.0)" >> $config_host= _mak +if test "$glib_has_gslice" =3D "yes" ; then + echo "HAVE_GLIB_WITH_SLICE_ALLOCATOR=3Dy" >> $config_host_mak +fi echo "QEMU_LDFLAGS=3D$QEMU_LDFLAGS" >> $config_host_mak echo "EXESUF=3D$EXESUF" >> $config_host_mak =20 diff --git a/meson.build b/meson.build index 29f8644d6d..c44d05a13f 100644 --- a/meson.build +++ b/meson.build @@ -508,6 +508,10 @@ glib =3D declare_dependency(compile_args: config_host[= 'GLIB_CFLAGS'].split(), }) # override glib dep with the configure results (for subprojects) meson.override_dependency('glib-2.0', glib) +# pass down whether Glib has the slice allocator +if config_host.has_key('HAVE_GLIB_WITH_SLICE_ALLOCATOR') + config_host_data.set('HAVE_GLIB_WITH_SLICE_ALLOCATOR', true) +endif =20 gio =3D not_found gdbus_codegen =3D not_found diff --git a/include/qemu/qtree.h b/include/qemu/qtree.h new file mode 100644 index 0000000000..69fe74b50d --- /dev/null +++ b/include/qemu/qtree.h @@ -0,0 +1,201 @@ +/* + * GLIB - Library of useful routines for C programming + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDona= ld + * + * SPDX-License-Identifier: LGPL-2.1-or-later + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + */ + +/* + * Modified by the GLib Team and others 1997-2000. See the AUTHORS + * file for a list of people on the GLib Team. See the ChangeLog + * files for a list of changes. These files are distributed with + * GLib at ftp://ftp.gtk.org/pub/gtk/. + */ + +/* + * QTree is a partial import of Glib's GTree. The parts excluded correspond + * to API calls either deprecated (e.g. g_tree_traverse) or recently added + * (e.g. g_tree_search_node, added in 2.68); neither have callers in QEMU. + * + * The reason for this import is to allow us to control the memory allocat= or + * used by the tree implementation. Until Glib 2.75.3, GTree uses Glib's + * slice allocator, which causes problems when forking in user-mode; + * see https://gitlab.com/qemu-project/qemu/-/issues/285 and glib's + * "45b5a6c1e gslice: Remove slice allocator and use malloc() instead". + * + * TODO: remove QTree when QEMU's minimum Glib version is >=3D 2.75.3. + */ + +#ifndef QEMU_QTREE_H +#define QEMU_QTREE_H + +#include "qemu/osdep.h" + +#ifdef HAVE_GLIB_WITH_SLICE_ALLOCATOR + +typedef struct _QTree QTree; + +typedef struct _QTreeNode QTreeNode; + +typedef gboolean (*QTraverseNodeFunc)(QTreeNode *node, + gpointer user_data); + +/* + * Balanced binary trees + */ +QTree *q_tree_new(GCompareFunc key_compare_func); +QTree *q_tree_new_with_data(GCompareDataFunc key_compare_func, + gpointer key_compare_data); +QTree *q_tree_new_full(GCompareDataFunc key_compare_func, + gpointer key_compare_data, + GDestroyNotify key_destroy_func, + GDestroyNotify value_destroy_func); +QTree *q_tree_ref(QTree *tree); +void q_tree_unref(QTree *tree); +void q_tree_destroy(QTree *tree); +void q_tree_insert(QTree *tree, + gpointer key, + gpointer value); +void q_tree_replace(QTree *tree, + gpointer key, + gpointer value); +gboolean q_tree_remove(QTree *tree, + gconstpointer key); +gboolean q_tree_steal(QTree *tree, + gconstpointer key); +gpointer q_tree_lookup(QTree *tree, + gconstpointer key); +gboolean q_tree_lookup_extended(QTree *tree, + gconstpointer lookup_key, + gpointer *orig_key, + gpointer *value); +void q_tree_foreach(QTree *tree, + GTraverseFunc func, + gpointer user_data); +gpointer q_tree_search(QTree *tree, + GCompareFunc search_func, + gconstpointer user_data); +gint q_tree_height(QTree *tree); +gint q_tree_nnodes(QTree *tree); + +#else /* !HAVE_GLIB_WITH_SLICE_ALLOCATOR */ + +typedef GTree QTree; +typedef GTreeNode QTreeNode; +typedef GTraverseNodeFunc QTraverseNodeFunc; + +static inline QTree *q_tree_new(GCompareFunc key_compare_func) +{ + return g_tree_new(key_compare_func); +} + +static inline QTree *q_tree_new_with_data(GCompareDataFunc key_compare_fun= c, + gpointer key_compare_data) +{ + return g_tree_new_with_data(key_compare_func, key_compare_data); +} + +static inline QTree *q_tree_new_full(GCompareDataFunc key_compare_func, + gpointer key_compare_data, + GDestroyNotify key_destroy_func, + GDestroyNotify value_destroy_func) +{ + return g_tree_new_full(key_compare_func, key_compare_data, + key_destroy_func, value_destroy_func); +} + +static inline QTree *q_tree_ref(QTree *tree) +{ + return g_tree_ref(tree); +} + +static inline void q_tree_unref(QTree *tree) +{ + g_tree_unref(tree); +} + +static inline void q_tree_destroy(QTree *tree) +{ + g_tree_destroy(tree); +} + +static inline void q_tree_insert(QTree *tree, + gpointer key, + gpointer value) +{ + g_tree_insert(tree, key, value); +} + +static inline void q_tree_replace(QTree *tree, + gpointer key, + gpointer value) +{ + g_tree_replace(tree, key, value); +} + +static inline gboolean q_tree_remove(QTree *tree, + gconstpointer key) +{ + return g_tree_remove(tree, key); +} + +static inline gboolean q_tree_steal(QTree *tree, + gconstpointer key) +{ + return g_tree_steal(tree, key); +} + +static inline gpointer q_tree_lookup(QTree *tree, + gconstpointer key) +{ + return g_tree_lookup(tree, key); +} + +static inline gboolean q_tree_lookup_extended(QTree *tree, + gconstpointer lookup_key, + gpointer *orig_key, + gpointer *value) +{ + return g_tree_lookup_extended(tree, lookup_key, orig_key, value); +} + +static inline void q_tree_foreach(QTree *tree, + GTraverseFunc func, + gpointer user_data) +{ + return g_tree_foreach(tree, func, user_data); +} + +static inline gpointer q_tree_search(QTree *tree, + GCompareFunc search_func, + gconstpointer user_data) +{ + return g_tree_search(tree, search_func, user_data); +} + +static inline gint q_tree_height(QTree *tree) +{ + return g_tree_height(tree); +} + +static inline gint q_tree_nnodes(QTree *tree) +{ + return g_tree_nnodes(tree); +} + +#endif /* HAVE_GLIB_WITH_SLICE_ALLOCATOR */ + +#endif /* QEMU_QTREE_H */ diff --git a/tests/bench/qtree-bench.c b/tests/bench/qtree-bench.c new file mode 100644 index 0000000000..f3d7edc76d --- /dev/null +++ b/tests/bench/qtree-bench.c @@ -0,0 +1,286 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#include "qemu/osdep.h" +#include "qemu/qtree.h" +#include "qemu/timer.h" + +enum tree_op { + OP_LOOKUP, + OP_INSERT, + OP_REMOVE, + OP_REMOVE_ALL, + OP_TRAVERSE, +}; + +struct benchmark { + const char * const name; + enum tree_op op; + bool fill_on_init; +}; + +enum impl_type { + IMPL_GTREE, + IMPL_QTREE, +}; + +struct tree_implementation { + const char * const name; + enum impl_type type; +}; + +static const struct benchmark benchmarks[] =3D { + { + .name =3D "Lookup", + .op =3D OP_LOOKUP, + .fill_on_init =3D true, + }, + { + .name =3D "Insert", + .op =3D OP_INSERT, + .fill_on_init =3D false, + }, + { + .name =3D "Remove", + .op =3D OP_REMOVE, + .fill_on_init =3D true, + }, + { + .name =3D "RemoveAll", + .op =3D OP_REMOVE_ALL, + .fill_on_init =3D true, + }, + { + .name =3D "Traverse", + .op =3D OP_TRAVERSE, + .fill_on_init =3D true, + }, +}; + +static const struct tree_implementation impls[] =3D { + { + .name =3D "GTree", + .type =3D IMPL_GTREE, + }, + { + .name =3D "QTree", + .type =3D IMPL_QTREE, + }, +}; + +static int compare_func(const void *ap, const void *bp) +{ + const size_t *a =3D ap; + const size_t *b =3D bp; + + return *a - *b; +} + +static void init_empty_tree_and_keys(enum impl_type impl, + void **ret_tree, size_t **ret_keys, + size_t n_elems) +{ + size_t *keys =3D g_malloc_n(n_elems, sizeof(*keys)); + for (size_t i =3D 0; i < n_elems; i++) { + keys[i] =3D i; + } + + void *tree; + switch (impl) { + case IMPL_GTREE: + tree =3D g_tree_new(compare_func); + break; + case IMPL_QTREE: + tree =3D q_tree_new(compare_func); + break; + default: + g_assert_not_reached(); + } + + *ret_tree =3D tree; + *ret_keys =3D keys; +} + +static gboolean traverse_func(gpointer key, gpointer value, gpointer data) +{ + return FALSE; +} + +static inline void remove_all(void *tree, enum impl_type impl) +{ + switch (impl) { + case IMPL_GTREE: + g_tree_destroy(tree); + break; + case IMPL_QTREE: + q_tree_destroy(tree); + break; + default: + g_assert_not_reached(); + } +} + +static int64_t run_benchmark(const struct benchmark *bench, + enum impl_type impl, + size_t n_elems) +{ + void *tree; + size_t *keys; + + init_empty_tree_and_keys(impl, &tree, &keys, n_elems); + if (bench->fill_on_init) { + for (size_t i =3D 0; i < n_elems; i++) { + switch (impl) { + case IMPL_GTREE: + g_tree_insert(tree, &keys[i], &keys[i]); + break; + case IMPL_QTREE: + q_tree_insert(tree, &keys[i], &keys[i]); + break; + default: + g_assert_not_reached(); + } + } + } + + int64_t start_ns =3D get_clock(); + switch (bench->op) { + case OP_LOOKUP: + for (size_t i =3D 0; i < n_elems; i++) { + void *value; + switch (impl) { + case IMPL_GTREE: + value =3D g_tree_lookup(tree, &keys[i]); + break; + case IMPL_QTREE: + value =3D q_tree_lookup(tree, &keys[i]); + break; + default: + g_assert_not_reached(); + } + (void)value; + } + break; + case OP_INSERT: + for (size_t i =3D 0; i < n_elems; i++) { + switch (impl) { + case IMPL_GTREE: + g_tree_insert(tree, &keys[i], &keys[i]); + break; + case IMPL_QTREE: + q_tree_insert(tree, &keys[i], &keys[i]); + break; + default: + g_assert_not_reached(); + } + } + break; + case OP_REMOVE: + for (size_t i =3D 0; i < n_elems; i++) { + switch (impl) { + case IMPL_GTREE: + g_tree_remove(tree, &keys[i]); + break; + case IMPL_QTREE: + q_tree_remove(tree, &keys[i]); + break; + default: + g_assert_not_reached(); + } + } + break; + case OP_REMOVE_ALL: + remove_all(tree, impl); + break; + case OP_TRAVERSE: + switch (impl) { + case IMPL_GTREE: + g_tree_foreach(tree, traverse_func, NULL); + break; + case IMPL_QTREE: + q_tree_foreach(tree, traverse_func, NULL); + break; + default: + g_assert_not_reached(); + } + break; + default: + g_assert_not_reached(); + } + int64_t ns =3D get_clock() - start_ns; + + if (bench->op !=3D OP_REMOVE_ALL) { + remove_all(tree, impl); + } + g_free(keys); + + return ns; +} + +int main(int argc, char *argv[]) +{ + size_t sizes[] =3D { + 32, + 1024, + 1024 * 4, + 1024 * 128, + 1024 * 1024, + }; + + double res[ARRAY_SIZE(benchmarks)][ARRAY_SIZE(impls)][ARRAY_SIZE(sizes= )]; + for (int i =3D 0; i < ARRAY_SIZE(sizes); i++) { + size_t size =3D sizes[i]; + for (int j =3D 0; j < ARRAY_SIZE(impls); j++) { + const struct tree_implementation *impl =3D &impls[j]; + for (int k =3D 0; k < ARRAY_SIZE(benchmarks); k++) { + const struct benchmark *bench =3D &benchmarks[k]; + + /* warm-up run */ + run_benchmark(bench, impl->type, size); + + int64_t total_ns =3D 0; + int64_t n_runs =3D 0; + while (total_ns < 2e8 || n_runs < 5) { + total_ns +=3D run_benchmark(bench, impl->type, size); + n_runs++; + } + double ns_per_run =3D (double)total_ns / n_runs; + + /* Throughput, in Mops/s */ + res[k][j][i] =3D size / ns_per_run * 1e3; + } + } + } + + printf("# Results' breakdown: Tree, Op and #Elements. Units: Mops/s\n"= ); + printf("%5s %10s ", "Tree", "Op"); + for (int i =3D 0; i < ARRAY_SIZE(sizes); i++) { + printf("%7zu ", sizes[i]); + } + printf("\n"); + char separator[97]; + for (int i =3D 0; i < ARRAY_SIZE(separator) - 1; i++) { + separator[i] =3D '-'; + } + separator[ARRAY_SIZE(separator) - 1] =3D '\0'; + printf("%s\n", separator); + for (int i =3D 0; i < ARRAY_SIZE(benchmarks); i++) { + for (int j =3D 0; j < ARRAY_SIZE(impls); j++) { + printf("%5s %10s ", impls[j].name, benchmarks[i].name); + for (int k =3D 0; k < ARRAY_SIZE(sizes); k++) { + printf("%7.2f ", res[i][j][k]); + if (j =3D=3D 0) { + printf(" "); + } else { + if (res[i][0][k] !=3D 0) { + double speedup =3D res[i][j][k] / res[i][0][k]; + printf("(%4.2fx) ", speedup); + } else { + printf("( ) "); + } + } + } + printf("\n"); + } + } + printf("%s\n", separator); + return 0; +} diff --git a/tests/unit/test-qtree.c b/tests/unit/test-qtree.c new file mode 100644 index 0000000000..4d836d22c7 --- /dev/null +++ b/tests/unit/test-qtree.c @@ -0,0 +1,333 @@ +/* + * SPDX-License-Identifier: LGPL-2.1-or-later + * + * Tests for QTree. + * Original source: glib + * https://gitlab.gnome.org/GNOME/glib/-/blob/main/glib/tests/tree.c + * LGPL license. + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDon= ald + */ + +#include "qemu/osdep.h" +#include "qemu/qtree.h" + +static gint my_compare(gconstpointer a, gconstpointer b) +{ + const char *cha =3D a; + const char *chb =3D b; + + return *cha - *chb; +} + +static gint my_compare_with_data(gconstpointer a, + gconstpointer b, + gpointer user_data) +{ + const char *cha =3D a; + const char *chb =3D b; + + /* just check that we got the right data */ + g_assert(GPOINTER_TO_INT(user_data) =3D=3D 123); + + return *cha - *chb; +} + +static gint my_search(gconstpointer a, gconstpointer b) +{ + return my_compare(b, a); +} + +static gpointer destroyed_key; +static gpointer destroyed_value; +static guint destroyed_key_count; +static guint destroyed_value_count; + +static void my_key_destroy(gpointer key) +{ + destroyed_key =3D key; + destroyed_key_count++; +} + +static void my_value_destroy(gpointer value) +{ + destroyed_value =3D value; + destroyed_value_count++; +} + +static gint my_traverse(gpointer key, gpointer value, gpointer data) +{ + char *ch =3D key; + + g_assert((*ch) > 0); + + if (*ch =3D=3D 'd') { + return TRUE; + } + + return FALSE; +} + +char chars[] =3D + "0123456789" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz"; + +char chars2[] =3D + "0123456789" + "abcdefghijklmnopqrstuvwxyz"; + +static gint check_order(gpointer key, gpointer value, gpointer data) +{ + char **p =3D data; + char *ch =3D key; + + g_assert(**p =3D=3D *ch); + + (*p)++; + + return FALSE; +} + +static void test_tree_search(void) +{ + gint i; + QTree *tree; + gboolean removed; + gchar c; + gchar *p, *d; + + tree =3D q_tree_new_with_data(my_compare_with_data, GINT_TO_POINTER(12= 3)); + + for (i =3D 0; chars[i]; i++) { + q_tree_insert(tree, &chars[i], &chars[i]); + } + + q_tree_foreach(tree, my_traverse, NULL); + + g_assert(q_tree_nnodes(tree) =3D=3D strlen(chars)); + g_assert(q_tree_height(tree) =3D=3D 6); + + p =3D chars; + q_tree_foreach(tree, check_order, &p); + + for (i =3D 0; i < 26; i++) { + removed =3D q_tree_remove(tree, &chars[i + 10]); + g_assert(removed); + } + + c =3D '\0'; + removed =3D q_tree_remove(tree, &c); + g_assert(!removed); + + q_tree_foreach(tree, my_traverse, NULL); + + g_assert(q_tree_nnodes(tree) =3D=3D strlen(chars2)); + g_assert(q_tree_height(tree) =3D=3D 6); + + p =3D chars2; + q_tree_foreach(tree, check_order, &p); + + for (i =3D 25; i >=3D 0; i--) { + q_tree_insert(tree, &chars[i + 10], &chars[i + 10]); + } + + p =3D chars; + q_tree_foreach(tree, check_order, &p); + + c =3D '0'; + p =3D q_tree_lookup(tree, &c); + g_assert(p && *p =3D=3D c); + g_assert(q_tree_lookup_extended(tree, &c, (gpointer *)&d, (gpointer *)= &p)); + g_assert(c =3D=3D *d && c =3D=3D *p); + + c =3D 'A'; + p =3D q_tree_lookup(tree, &c); + g_assert(p && *p =3D=3D c); + + c =3D 'a'; + p =3D q_tree_lookup(tree, &c); + g_assert(p && *p =3D=3D c); + + c =3D 'z'; + p =3D q_tree_lookup(tree, &c); + g_assert(p && *p =3D=3D c); + + c =3D '!'; + p =3D q_tree_lookup(tree, &c); + g_assert(p =3D=3D NULL); + + c =3D '=3D'; + p =3D q_tree_lookup(tree, &c); + g_assert(p =3D=3D NULL); + + c =3D '|'; + p =3D q_tree_lookup(tree, &c); + g_assert(p =3D=3D NULL); + + c =3D '0'; + p =3D q_tree_search(tree, my_search, &c); + g_assert(p && *p =3D=3D c); + + c =3D 'A'; + p =3D q_tree_search(tree, my_search, &c); + g_assert(p && *p =3D=3D c); + + c =3D 'a'; + p =3D q_tree_search(tree, my_search, &c); + g_assert(p && *p =3D=3D c); + + c =3D 'z'; + p =3D q_tree_search(tree, my_search, &c); + g_assert(p && *p =3D=3D c); + + c =3D '!'; + p =3D q_tree_search(tree, my_search, &c); + g_assert(p =3D=3D NULL); + + c =3D '=3D'; + p =3D q_tree_search(tree, my_search, &c); + g_assert(p =3D=3D NULL); + + c =3D '|'; + p =3D q_tree_search(tree, my_search, &c); + g_assert(p =3D=3D NULL); + + q_tree_destroy(tree); +} + +static void test_tree_remove(void) +{ + QTree *tree; + char c, d; + gint i; + gboolean removed; + + tree =3D q_tree_new_full((GCompareDataFunc)my_compare, NULL, + my_key_destroy, + my_value_destroy); + + for (i =3D 0; chars[i]; i++) { + q_tree_insert(tree, &chars[i], &chars[i]); + } + + c =3D '0'; + q_tree_insert(tree, &c, &c); + g_assert(destroyed_key =3D=3D &c); + g_assert(destroyed_value =3D=3D &chars[0]); + destroyed_key =3D NULL; + destroyed_value =3D NULL; + + d =3D '1'; + q_tree_replace(tree, &d, &d); + g_assert(destroyed_key =3D=3D &chars[1]); + g_assert(destroyed_value =3D=3D &chars[1]); + destroyed_key =3D NULL; + destroyed_value =3D NULL; + + c =3D '2'; + removed =3D q_tree_remove(tree, &c); + g_assert(removed); + g_assert(destroyed_key =3D=3D &chars[2]); + g_assert(destroyed_value =3D=3D &chars[2]); + destroyed_key =3D NULL; + destroyed_value =3D NULL; + + c =3D '3'; + removed =3D q_tree_steal(tree, &c); + g_assert(removed); + g_assert(destroyed_key =3D=3D NULL); + g_assert(destroyed_value =3D=3D NULL); + + const gchar *remove =3D "omkjigfedba"; + for (i =3D 0; remove[i]; i++) { + removed =3D q_tree_remove(tree, &remove[i]); + g_assert(removed); + } + + q_tree_destroy(tree); +} + +static void test_tree_destroy(void) +{ + QTree *tree; + gint i; + + tree =3D q_tree_new(my_compare); + + for (i =3D 0; chars[i]; i++) { + q_tree_insert(tree, &chars[i], &chars[i]); + } + + g_assert(q_tree_nnodes(tree) =3D=3D strlen(chars)); + + g_test_message("nnodes: %d", q_tree_nnodes(tree)); + q_tree_ref(tree); + q_tree_destroy(tree); + + g_test_message("nnodes: %d", q_tree_nnodes(tree)); + g_assert(q_tree_nnodes(tree) =3D=3D 0); + + q_tree_unref(tree); +} + +static void test_tree_insert(void) +{ + QTree *tree; + gchar *p; + gint i; + gchar *scrambled; + + tree =3D q_tree_new(my_compare); + + for (i =3D 0; chars[i]; i++) { + q_tree_insert(tree, &chars[i], &chars[i]); + } + p =3D chars; + q_tree_foreach(tree, check_order, &p); + + q_tree_unref(tree); + tree =3D q_tree_new(my_compare); + + for (i =3D strlen(chars) - 1; i >=3D 0; i--) { + q_tree_insert(tree, &chars[i], &chars[i]); + } + p =3D chars; + q_tree_foreach(tree, check_order, &p); + + q_tree_unref(tree); + tree =3D q_tree_new(my_compare); + + scrambled =3D g_strdup(chars); + + for (i =3D 0; i < 30; i++) { + gchar tmp; + gint a, b; + + a =3D g_random_int_range(0, strlen(scrambled)); + b =3D g_random_int_range(0, strlen(scrambled)); + tmp =3D scrambled[a]; + scrambled[a] =3D scrambled[b]; + scrambled[b] =3D tmp; + } + + for (i =3D 0; scrambled[i]; i++) { + q_tree_insert(tree, &scrambled[i], &scrambled[i]); + } + p =3D chars; + q_tree_foreach(tree, check_order, &p); + + g_free(scrambled); + q_tree_unref(tree); +} + +int main(int argc, char *argv[]) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/qtree/search", test_tree_search); + g_test_add_func("/qtree/remove", test_tree_remove); + g_test_add_func("/qtree/destroy", test_tree_destroy); + g_test_add_func("/qtree/insert", test_tree_insert); + + return g_test_run(); +} diff --git a/util/qtree.c b/util/qtree.c new file mode 100644 index 0000000000..deb46c187f --- /dev/null +++ b/util/qtree.c @@ -0,0 +1,1390 @@ +/* + * GLIB - Library of useful routines for C programming + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDona= ld + * + * SPDX-License-Identifier: LGPL-2.1-or-later + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + */ + +/* + * Modified by the GLib Team and others 1997-2000. See the AUTHORS + * file for a list of people on the GLib Team. See the ChangeLog + * files for a list of changes. These files are distributed with + * GLib at ftp://ftp.gtk.org/pub/gtk/. + */ + +/* + * MT safe + */ + +#include "qemu/osdep.h" +#include "qemu/qtree.h" + +/** + * SECTION:trees-binary + * @title: Balanced Binary Trees + * @short_description: a sorted collection of key/value pairs optimized + * for searching and traversing in order + * + * The #QTree structure and its associated functions provide a sorted + * collection of key/value pairs optimized for searching and traversing + * in order. This means that most of the operations (access, search, + * insertion, deletion, ...) on #QTree are O(log(n)) in average and O(n) + * in worst case for time complexity. But, note that maintaining a + * balanced sorted #QTree of n elements is done in time O(n log(n)). + * + * To create a new #QTree use q_tree_new(). + * + * To insert a key/value pair into a #QTree use q_tree_insert() + * (O(n log(n))). + * + * To remove a key/value pair use q_tree_remove() (O(n log(n))). + * + * To look up the value corresponding to a given key, use + * q_tree_lookup() and q_tree_lookup_extended(). + * + * To find out the number of nodes in a #QTree, use q_tree_nnodes(). To + * get the height of a #QTree, use q_tree_height(). + * + * To traverse a #QTree, calling a function for each node visited in + * the traversal, use q_tree_foreach(). + * + * To destroy a #QTree, use q_tree_destroy(). + **/ + +#define MAX_GTREE_HEIGHT 40 + +/** + * QTree: + * + * The QTree struct is an opaque data structure representing a + * [balanced binary tree][glib-Balanced-Binary-Trees]. It should be + * accessed only by using the following functions. + */ +struct _QTree { + QTreeNode *root; + GCompareDataFunc key_compare; + GDestroyNotify key_destroy_func; + GDestroyNotify value_destroy_func; + gpointer key_compare_data; + guint nnodes; + gint ref_count; +}; + +struct _QTreeNode { + gpointer key; /* key for this node */ + gpointer value; /* value stored at this node */ + QTreeNode *left; /* left subtree */ + QTreeNode *right; /* right subtree */ + gint8 balance; /* height (right) - height (left) */ + guint8 left_child; + guint8 right_child; +}; + + +static QTreeNode *q_tree_node_new(gpointer key, + gpointer value); +static QTreeNode *q_tree_insert_internal(QTree *tree, + gpointer key, + gpointer value, + gboolean replace); +static gboolean q_tree_remove_internal(QTree *tree, + gconstpointer key, + gboolean steal); +static QTreeNode *q_tree_node_balance(QTreeNode *node); +static QTreeNode *q_tree_find_node(QTree *tree, + gconstpointer key); +static QTreeNode *q_tree_node_search(QTreeNode *node, + GCompareFunc search_func, + gconstpointer data); +static QTreeNode *q_tree_node_rotate_left(QTreeNode *node); +static QTreeNode *q_tree_node_rotate_right(QTreeNode *node); +#ifdef Q_TREE_DEBUG +static void q_tree_node_check(QTreeNode *node); +#endif + +static QTreeNode* +q_tree_node_new(gpointer key, + gpointer value) +{ + QTreeNode *node =3D g_new(QTreeNode, 1); + + node->balance =3D 0; + node->left =3D NULL; + node->right =3D NULL; + node->left_child =3D FALSE; + node->right_child =3D FALSE; + node->key =3D key; + node->value =3D value; + + return node; +} + +/** + * q_tree_new: + * @key_compare_func: the function used to order the nodes in the #QTree. + * It should return values similar to the standard strcmp() function - + * 0 if the two arguments are equal, a negative value if the first argum= ent + * comes before the second, or a positive value if the first argument co= mes + * after the second. + * + * Creates a new #QTree. + * + * Returns: a newly allocated #QTree + */ +QTree * +q_tree_new(GCompareFunc key_compare_func) +{ + g_return_val_if_fail(key_compare_func !=3D NULL, NULL); + + return q_tree_new_full((GCompareDataFunc) key_compare_func, NULL, + NULL, NULL); +} + +/** + * q_tree_new_with_data: + * @key_compare_func: qsort()-style comparison function + * @key_compare_data: data to pass to comparison function + * + * Creates a new #QTree with a comparison function that accepts user data. + * See q_tree_new() for more details. + * + * Returns: a newly allocated #QTree + */ +QTree * +q_tree_new_with_data(GCompareDataFunc key_compare_func, + gpointer key_compare_data) +{ + g_return_val_if_fail(key_compare_func !=3D NULL, NULL); + + return q_tree_new_full(key_compare_func, key_compare_data, + NULL, NULL); +} + +/** + * q_tree_new_full: + * @key_compare_func: qsort()-style comparison function + * @key_compare_data: data to pass to comparison function + * @key_destroy_func: a function to free the memory allocated for the key + * used when removing the entry from the #QTree or %NULL if you don't + * want to supply such a function + * @value_destroy_func: a function to free the memory allocated for the + * value used when removing the entry from the #QTree or %NULL if you + * don't want to supply such a function + * + * Creates a new #QTree like q_tree_new() and allows to specify functions + * to free the memory allocated for the key and value that get called when + * removing the entry from the #QTree. + * + * Returns: a newly allocated #QTree + */ +QTree * +q_tree_new_full(GCompareDataFunc key_compare_func, + gpointer key_compare_data, + GDestroyNotify key_destroy_func, + GDestroyNotify value_destroy_func) +{ + QTree *tree; + + g_return_val_if_fail(key_compare_func !=3D NULL, NULL); + + tree =3D g_new(QTree, 1); + tree->root =3D NULL; + tree->key_compare =3D key_compare_func; + tree->key_destroy_func =3D key_destroy_func; + tree->value_destroy_func =3D value_destroy_func; + tree->key_compare_data =3D key_compare_data; + tree->nnodes =3D 0; + tree->ref_count =3D 1; + + return tree; +} + +/** + * q_tree_node_first: + * @tree: a #QTree + * + * Returns the first in-order node of the tree, or %NULL + * for an empty tree. + * + * Returns: (nullable) (transfer none): the first node in the tree + * + * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API. + */ +static QTreeNode * +q_tree_node_first(QTree *tree) +{ + QTreeNode *tmp; + + g_return_val_if_fail(tree !=3D NULL, NULL); + + if (!tree->root) { + return NULL; + } + + tmp =3D tree->root; + + while (tmp->left_child) { + tmp =3D tmp->left; + } + + return tmp; +} + +/** + * q_tree_node_previous + * @node: a #QTree node + * + * Returns the previous in-order node of the tree, or %NULL + * if the passed node was already the first one. + * + * Returns: (nullable) (transfer none): the previous node in the tree + * + * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API. + */ +static QTreeNode * +q_tree_node_previous(QTreeNode *node) +{ + QTreeNode *tmp; + + g_return_val_if_fail(node !=3D NULL, NULL); + + tmp =3D node->left; + + if (node->left_child) { + while (tmp->right_child) { + tmp =3D tmp->right; + } + } + + return tmp; +} + +/** + * q_tree_node_next + * @node: a #QTree node + * + * Returns the next in-order node of the tree, or %NULL + * if the passed node was already the last one. + * + * Returns: (nullable) (transfer none): the next node in the tree + * + * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API. + */ +static QTreeNode * +q_tree_node_next(QTreeNode *node) +{ + QTreeNode *tmp; + + g_return_val_if_fail(node !=3D NULL, NULL); + + tmp =3D node->right; + + if (node->right_child) { + while (tmp->left_child) { + tmp =3D tmp->left; + } + } + + return tmp; +} + +/** + * q_tree_remove_all: + * @tree: a #QTree + * + * Removes all nodes from a #QTree and destroys their keys and values, + * then resets the #QTree=E2=80=99s root to %NULL. + * + * Since: 2.70 in GLib. Internal in Qtree, i.e. not in the public API. + */ +static void +q_tree_remove_all(QTree *tree) +{ + QTreeNode *node; + QTreeNode *next; + + g_return_if_fail(tree !=3D NULL); + + node =3D q_tree_node_first(tree); + + while (node) { + next =3D q_tree_node_next(node); + + if (tree->key_destroy_func) { + tree->key_destroy_func(node->key); + } + if (tree->value_destroy_func) { + tree->value_destroy_func(node->value); + } + g_free(node); + +#ifdef Q_TREE_DEBUG + g_assert(tree->nnodes > 0); + tree->nnodes--; +#endif + + node =3D next; + } + +#ifdef Q_TREE_DEBUG + g_assert(tree->nnodes =3D=3D 0); +#endif + + tree->root =3D NULL; +#ifndef Q_TREE_DEBUG + tree->nnodes =3D 0; +#endif +} + +/** + * q_tree_ref: + * @tree: a #QTree + * + * Increments the reference count of @tree by one. + * + * It is safe to call this function from any thread. + * + * Returns: the passed in #QTree + * + * Since: 2.22 + */ +QTree * +q_tree_ref(QTree *tree) +{ + g_return_val_if_fail(tree !=3D NULL, NULL); + + g_atomic_int_inc(&tree->ref_count); + + return tree; +} + +/** + * q_tree_unref: + * @tree: a #QTree + * + * Decrements the reference count of @tree by one. + * If the reference count drops to 0, all keys and values will + * be destroyed (if destroy functions were specified) and all + * memory allocated by @tree will be released. + * + * It is safe to call this function from any thread. + * + * Since: 2.22 + */ +void +q_tree_unref(QTree *tree) +{ + g_return_if_fail(tree !=3D NULL); + + if (g_atomic_int_dec_and_test(&tree->ref_count)) { + q_tree_remove_all(tree); + g_free(tree); + } +} + +/** + * q_tree_destroy: + * @tree: a #QTree + * + * Removes all keys and values from the #QTree and decreases its + * reference count by one. If keys and/or values are dynamically + * allocated, you should either free them first or create the #QTree + * using q_tree_new_full(). In the latter case the destroy functions + * you supplied will be called on all keys and values before destroying + * the #QTree. + */ +void +q_tree_destroy(QTree *tree) +{ + g_return_if_fail(tree !=3D NULL); + + q_tree_remove_all(tree); + q_tree_unref(tree); +} + +/** + * q_tree_insert_node: + * @tree: a #QTree + * @key: the key to insert + * @value: the value corresponding to the key + * + * Inserts a key/value pair into a #QTree. + * + * If the given key already exists in the #QTree its corresponding value + * is set to the new value. If you supplied a @value_destroy_func when + * creating the #QTree, the old value is freed using that function. If + * you supplied a @key_destroy_func when creating the #QTree, the passed + * key is freed using that function. + * + * The tree is automatically 'balanced' as new key/value pairs are added, + * so that the distance from the root to every leaf is as small as possibl= e. + * The cost of maintaining a balanced tree while inserting new key/value + * result in a O(n log(n)) operation where most of the other operations + * are O(log(n)). + * + * Returns: (transfer none): the inserted (or set) node. + * + * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API. + */ +static QTreeNode * +q_tree_insert_node(QTree *tree, + gpointer key, + gpointer value) +{ + QTreeNode *node; + + g_return_val_if_fail(tree !=3D NULL, NULL); + + node =3D q_tree_insert_internal(tree, key, value, FALSE); + +#ifdef Q_TREE_DEBUG + q_tree_node_check(tree->root); +#endif + + return node; +} + +/** + * q_tree_insert: + * @tree: a #QTree + * @key: the key to insert + * @value: the value corresponding to the key + * + * Inserts a key/value pair into a #QTree. + * + * Inserts a new key and value into a #QTree as q_tree_insert_node() does, + * only this function does not return the inserted or set node. + */ +void +q_tree_insert(QTree *tree, + gpointer key, + gpointer value) +{ + q_tree_insert_node(tree, key, value); +} + +/** + * q_tree_replace_node: + * @tree: a #QTree + * @key: the key to insert + * @value: the value corresponding to the key + * + * Inserts a new key and value into a #QTree similar to q_tree_insert_node= (). + * The difference is that if the key already exists in the #QTree, it gets + * replaced by the new key. If you supplied a @value_destroy_func when + * creating the #QTree, the old value is freed using that function. If you + * supplied a @key_destroy_func when creating the #QTree, the old key is + * freed using that function. + * + * The tree is automatically 'balanced' as new key/value pairs are added, + * so that the distance from the root to every leaf is as small as possibl= e. + * + * Returns: (transfer none): the inserted (or set) node. + * + * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API. + */ +static QTreeNode * +q_tree_replace_node(QTree *tree, + gpointer key, + gpointer value) +{ + QTreeNode *node; + + g_return_val_if_fail(tree !=3D NULL, NULL); + + node =3D q_tree_insert_internal(tree, key, value, TRUE); + +#ifdef Q_TREE_DEBUG + q_tree_node_check(tree->root); +#endif + + return node; +} + +/** + * q_tree_replace: + * @tree: a #QTree + * @key: the key to insert + * @value: the value corresponding to the key + * + * Inserts a new key and value into a #QTree as q_tree_replace_node() does, + * only this function does not return the inserted or set node. + */ +void +q_tree_replace(QTree *tree, + gpointer key, + gpointer value) +{ + q_tree_replace_node(tree, key, value); +} + +/* internal insert routine */ +static QTreeNode * +q_tree_insert_internal(QTree *tree, + gpointer key, + gpointer value, + gboolean replace) +{ + QTreeNode *node, *retnode; + QTreeNode *path[MAX_GTREE_HEIGHT]; + int idx; + + g_return_val_if_fail(tree !=3D NULL, NULL); + + if (!tree->root) { + tree->root =3D q_tree_node_new(key, value); + tree->nnodes++; + return tree->root; + } + + idx =3D 0; + path[idx++] =3D NULL; + node =3D tree->root; + + while (1) { + int cmp =3D tree->key_compare(key, node->key, tree->key_compare_da= ta); + + if (cmp =3D=3D 0) { + if (tree->value_destroy_func) { + tree->value_destroy_func(node->value); + } + + node->value =3D value; + + if (replace) { + if (tree->key_destroy_func) { + tree->key_destroy_func(node->key); + } + + node->key =3D key; + } else { + /* free the passed key */ + if (tree->key_destroy_func) { + tree->key_destroy_func(key); + } + } + + return node; + } else if (cmp < 0) { + if (node->left_child) { + path[idx++] =3D node; + node =3D node->left; + } else { + QTreeNode *child =3D q_tree_node_new(key, value); + + child->left =3D node->left; + child->right =3D node; + node->left =3D child; + node->left_child =3D TRUE; + node->balance -=3D 1; + + tree->nnodes++; + + retnode =3D child; + break; + } + } else { + if (node->right_child) { + path[idx++] =3D node; + node =3D node->right; + } else { + QTreeNode *child =3D q_tree_node_new(key, value); + + child->right =3D node->right; + child->left =3D node; + node->right =3D child; + node->right_child =3D TRUE; + node->balance +=3D 1; + + tree->nnodes++; + + retnode =3D child; + break; + } + } + } + + /* + * Restore balance. This is the goodness of a non-recursive + * implementation, when we are done with balancing we 'break' + * the loop and we are done. + */ + while (1) { + QTreeNode *bparent =3D path[--idx]; + gboolean left_node =3D (bparent && node =3D=3D bparent->left); + g_assert(!bparent || bparent->left =3D=3D node || bparent->right = =3D=3D node); + + if (node->balance < -1 || node->balance > 1) { + node =3D q_tree_node_balance(node); + if (bparent =3D=3D NULL) { + tree->root =3D node; + } else if (left_node) { + bparent->left =3D node; + } else { + bparent->right =3D node; + } + } + + if (node->balance =3D=3D 0 || bparent =3D=3D NULL) { + break; + } + + if (left_node) { + bparent->balance -=3D 1; + } else { + bparent->balance +=3D 1; + } + + node =3D bparent; + } + + return retnode; +} + +/** + * q_tree_remove: + * @tree: a #QTree + * @key: the key to remove + * + * Removes a key/value pair from a #QTree. + * + * If the #QTree was created using q_tree_new_full(), the key and value + * are freed using the supplied destroy functions, otherwise you have to + * make sure that any dynamically allocated values are freed yourself. + * If the key does not exist in the #QTree, the function does nothing. + * + * The cost of maintaining a balanced tree while removing a key/value + * result in a O(n log(n)) operation where most of the other operations + * are O(log(n)). + * + * Returns: %TRUE if the key was found (prior to 2.8, this function + * returned nothing) + */ +gboolean +q_tree_remove(QTree *tree, + gconstpointer key) +{ + gboolean removed; + + g_return_val_if_fail(tree !=3D NULL, FALSE); + + removed =3D q_tree_remove_internal(tree, key, FALSE); + +#ifdef Q_TREE_DEBUG + q_tree_node_check(tree->root); +#endif + + return removed; +} + +/** + * q_tree_steal: + * @tree: a #QTree + * @key: the key to remove + * + * Removes a key and its associated value from a #QTree without calling + * the key and value destroy functions. + * + * If the key does not exist in the #QTree, the function does nothing. + * + * Returns: %TRUE if the key was found (prior to 2.8, this function + * returned nothing) + */ +gboolean +q_tree_steal(QTree *tree, + gconstpointer key) +{ + gboolean removed; + + g_return_val_if_fail(tree !=3D NULL, FALSE); + + removed =3D q_tree_remove_internal(tree, key, TRUE); + +#ifdef Q_TREE_DEBUG + q_tree_node_check(tree->root); +#endif + + return removed; +} + +/* internal remove routine */ +static gboolean +q_tree_remove_internal(QTree *tree, + gconstpointer key, + gboolean steal) +{ + QTreeNode *node, *parent, *balance; + QTreeNode *path[MAX_GTREE_HEIGHT]; + int idx; + gboolean left_node; + + g_return_val_if_fail(tree !=3D NULL, FALSE); + + if (!tree->root) { + return FALSE; + } + + idx =3D 0; + path[idx++] =3D NULL; + node =3D tree->root; + + while (1) { + int cmp =3D tree->key_compare(key, node->key, tree->key_compare_da= ta); + + if (cmp =3D=3D 0) { + break; + } else if (cmp < 0) { + if (!node->left_child) { + return FALSE; + } + + path[idx++] =3D node; + node =3D node->left; + } else { + if (!node->right_child) { + return FALSE; + } + + path[idx++] =3D node; + node =3D node->right; + } + } + + /* + * The following code is almost equal to q_tree_remove_node, + * except that we do not have to call q_tree_node_parent. + */ + balance =3D parent =3D path[--idx]; + g_assert(!parent || parent->left =3D=3D node || parent->right =3D=3D n= ode); + left_node =3D (parent && node =3D=3D parent->left); + + if (!node->left_child) { + if (!node->right_child) { + if (!parent) { + tree->root =3D NULL; + } else if (left_node) { + parent->left_child =3D FALSE; + parent->left =3D node->left; + parent->balance +=3D 1; + } else { + parent->right_child =3D FALSE; + parent->right =3D node->right; + parent->balance -=3D 1; + } + } else { + /* node has a right child */ + QTreeNode *tmp =3D q_tree_node_next(node); + tmp->left =3D node->left; + + if (!parent) { + tree->root =3D node->right; + } else if (left_node) { + parent->left =3D node->right; + parent->balance +=3D 1; + } else { + parent->right =3D node->right; + parent->balance -=3D 1; + } + } + } else { + /* node has a left child */ + if (!node->right_child) { + QTreeNode *tmp =3D q_tree_node_previous(node); + tmp->right =3D node->right; + + if (parent =3D=3D NULL) { + tree->root =3D node->left; + } else if (left_node) { + parent->left =3D node->left; + parent->balance +=3D 1; + } else { + parent->right =3D node->left; + parent->balance -=3D 1; + } + } else { + /* node has a both children (pant, pant!) */ + QTreeNode *prev =3D node->left; + QTreeNode *next =3D node->right; + QTreeNode *nextp =3D node; + int old_idx =3D idx + 1; + idx++; + + /* path[idx] =3D=3D parent */ + /* find the immediately next node (and its parent) */ + while (next->left_child) { + path[++idx] =3D nextp =3D next; + next =3D next->left; + } + + path[old_idx] =3D next; + balance =3D path[idx]; + + /* remove 'next' from the tree */ + if (nextp !=3D node) { + if (next->right_child) { + nextp->left =3D next->right; + } else { + nextp->left_child =3D FALSE; + } + nextp->balance +=3D 1; + + next->right_child =3D TRUE; + next->right =3D node->right; + } else { + node->balance -=3D 1; + } + + /* set the prev to point to the right place */ + while (prev->right_child) { + prev =3D prev->right; + } + prev->right =3D next; + + /* prepare 'next' to replace 'node' */ + next->left_child =3D TRUE; + next->left =3D node->left; + next->balance =3D node->balance; + + if (!parent) { + tree->root =3D next; + } else if (left_node) { + parent->left =3D next; + } else { + parent->right =3D next; + } + } + } + + /* restore balance */ + if (balance) { + while (1) { + QTreeNode *bparent =3D path[--idx]; + g_assert(!bparent || + bparent->left =3D=3D balance || + bparent->right =3D=3D balance); + left_node =3D (bparent && balance =3D=3D bparent->left); + + if (balance->balance < -1 || balance->balance > 1) { + balance =3D q_tree_node_balance(balance); + if (!bparent) { + tree->root =3D balance; + } else if (left_node) { + bparent->left =3D balance; + } else { + bparent->right =3D balance; + } + } + + if (balance->balance !=3D 0 || !bparent) { + break; + } + + if (left_node) { + bparent->balance +=3D 1; + } else { + bparent->balance -=3D 1; + } + + balance =3D bparent; + } + } + + if (!steal) { + if (tree->key_destroy_func) { + tree->key_destroy_func(node->key); + } + if (tree->value_destroy_func) { + tree->value_destroy_func(node->value); + } + } + + g_free(node); + + tree->nnodes--; + + return TRUE; +} + +/** + * q_tree_lookup_node: + * @tree: a #QTree + * @key: the key to look up + * + * Gets the tree node corresponding to the given key. Since a #QTree is + * automatically balanced as key/value pairs are added, key lookup + * is O(log n) (where n is the number of key/value pairs in the tree). + * + * Returns: (nullable) (transfer none): the tree node corresponding to + * the key, or %NULL if the key was not found + * + * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API. + */ +static QTreeNode * +q_tree_lookup_node(QTree *tree, + gconstpointer key) +{ + g_return_val_if_fail(tree !=3D NULL, NULL); + + return q_tree_find_node(tree, key); +} + +/** + * q_tree_lookup: + * @tree: a #QTree + * @key: the key to look up + * + * Gets the value corresponding to the given key. Since a #QTree is + * automatically balanced as key/value pairs are added, key lookup + * is O(log n) (where n is the number of key/value pairs in the tree). + * + * Returns: the value corresponding to the key, or %NULL + * if the key was not found + */ +gpointer +q_tree_lookup(QTree *tree, + gconstpointer key) +{ + QTreeNode *node; + + node =3D q_tree_lookup_node(tree, key); + + return node ? node->value : NULL; +} + +/** + * q_tree_lookup_extended: + * @tree: a #QTree + * @lookup_key: the key to look up + * @orig_key: (out) (optional) (nullable): returns the original key + * @value: (out) (optional) (nullable): returns the value associated with + * the key + * + * Looks up a key in the #QTree, returning the original key and the + * associated value. This is useful if you need to free the memory + * allocated for the original key, for example before calling + * q_tree_remove(). + * + * Returns: %TRUE if the key was found in the #QTree + */ +gboolean +q_tree_lookup_extended(QTree *tree, + gconstpointer lookup_key, + gpointer *orig_key, + gpointer *value) +{ + QTreeNode *node; + + g_return_val_if_fail(tree !=3D NULL, FALSE); + + node =3D q_tree_find_node(tree, lookup_key); + + if (node) { + if (orig_key) { + *orig_key =3D node->key; + } + if (value) { + *value =3D node->value; + } + return TRUE; + } else { + return FALSE; + } +} + +/** + * q_tree_foreach: + * @tree: a #QTree + * @func: the function to call for each node visited. + * If this function returns %TRUE, the traversal is stopped. + * @user_data: user data to pass to the function + * + * Calls the given function for each of the key/value pairs in the #QTree. + * The function is passed the key and value of each pair, and the given + * @data parameter. The tree is traversed in sorted order. + * + * The tree may not be modified while iterating over it (you can't + * add/remove items). To remove all items matching a predicate, you need + * to add each item to a list in your #GTraverseFunc as you walk over + * the tree, then walk the list and remove each item. + */ +void +q_tree_foreach(QTree *tree, + GTraverseFunc func, + gpointer user_data) +{ + QTreeNode *node; + + g_return_if_fail(tree !=3D NULL); + + if (!tree->root) { + return; + } + + node =3D q_tree_node_first(tree); + + while (node) { + if ((*func)(node->key, node->value, user_data)) { + break; + } + + node =3D q_tree_node_next(node); + } +} + +/** + * q_tree_search_node: + * @tree: a #QTree + * @search_func: a function used to search the #QTree + * @user_data: the data passed as the second argument to @search_func + * + * Searches a #QTree using @search_func. + * + * The @search_func is called with a pointer to the key of a key/value + * pair in the tree, and the passed in @user_data. If @search_func returns + * 0 for a key/value pair, then the corresponding node is returned as + * the result of q_tree_search(). If @search_func returns -1, searching + * will proceed among the key/value pairs that have a smaller key; if + * @search_func returns 1, searching will proceed among the key/value + * pairs that have a larger key. + * + * Returns: (nullable) (transfer none): the node corresponding to the + * found key, or %NULL if the key was not found + * + * Since: 2.68 in GLib. Internal in Qtree, i.e. not in the public API. + */ +static QTreeNode * +q_tree_search_node(QTree *tree, + GCompareFunc search_func, + gconstpointer user_data) +{ + g_return_val_if_fail(tree !=3D NULL, NULL); + + if (!tree->root) { + return NULL; + } + + return q_tree_node_search(tree->root, search_func, user_data); +} + +/** + * q_tree_search: + * @tree: a #QTree + * @search_func: a function used to search the #QTree + * @user_data: the data passed as the second argument to @search_func + * + * Searches a #QTree using @search_func. + * + * The @search_func is called with a pointer to the key of a key/value + * pair in the tree, and the passed in @user_data. If @search_func returns + * 0 for a key/value pair, then the corresponding value is returned as + * the result of q_tree_search(). If @search_func returns -1, searching + * will proceed among the key/value pairs that have a smaller key; if + * @search_func returns 1, searching will proceed among the key/value + * pairs that have a larger key. + * + * Returns: the value corresponding to the found key, or %NULL + * if the key was not found + */ +gpointer +q_tree_search(QTree *tree, + GCompareFunc search_func, + gconstpointer user_data) +{ + QTreeNode *node; + + node =3D q_tree_search_node(tree, search_func, user_data); + + return node ? node->value : NULL; +} + +/** + * q_tree_height: + * @tree: a #QTree + * + * Gets the height of a #QTree. + * + * If the #QTree contains no nodes, the height is 0. + * If the #QTree contains only one root node the height is 1. + * If the root node has children the height is 2, etc. + * + * Returns: the height of @tree + */ +gint +q_tree_height(QTree *tree) +{ + QTreeNode *node; + gint height; + + g_return_val_if_fail(tree !=3D NULL, 0); + + if (!tree->root) { + return 0; + } + + height =3D 0; + node =3D tree->root; + + while (1) { + height +=3D 1 + MAX(node->balance, 0); + + if (!node->left_child) { + return height; + } + + node =3D node->left; + } +} + +/** + * q_tree_nnodes: + * @tree: a #QTree + * + * Gets the number of nodes in a #QTree. + * + * Returns: the number of nodes in @tree + */ +gint +q_tree_nnodes(QTree *tree) +{ + g_return_val_if_fail(tree !=3D NULL, 0); + + return tree->nnodes; +} + +static QTreeNode * +q_tree_node_balance(QTreeNode *node) +{ + if (node->balance < -1) { + if (node->left->balance > 0) { + node->left =3D q_tree_node_rotate_left(node->left); + } + node =3D q_tree_node_rotate_right(node); + } else if (node->balance > 1) { + if (node->right->balance < 0) { + node->right =3D q_tree_node_rotate_right(node->right); + } + node =3D q_tree_node_rotate_left(node); + } + + return node; +} + +static QTreeNode * +q_tree_find_node(QTree *tree, + gconstpointer key) +{ + QTreeNode *node; + gint cmp; + + node =3D tree->root; + if (!node) { + return NULL; + } + + while (1) { + cmp =3D tree->key_compare(key, node->key, tree->key_compare_data); + if (cmp =3D=3D 0) { + return node; + } else if (cmp < 0) { + if (!node->left_child) { + return NULL; + } + + node =3D node->left; + } else { + if (!node->right_child) { + return NULL; + } + + node =3D node->right; + } + } +} + +static QTreeNode * +q_tree_node_search(QTreeNode *node, + GCompareFunc search_func, + gconstpointer data) +{ + gint dir; + + if (!node) { + return NULL; + } + + while (1) { + dir =3D (*search_func)(node->key, data); + if (dir =3D=3D 0) { + return node; + } else if (dir < 0) { + if (!node->left_child) { + return NULL; + } + + node =3D node->left; + } else { + if (!node->right_child) { + return NULL; + } + + node =3D node->right; + } + } +} + +static QTreeNode * +q_tree_node_rotate_left(QTreeNode *node) +{ + QTreeNode *right; + gint a_bal; + gint b_bal; + + right =3D node->right; + + if (right->left_child) { + node->right =3D right->left; + } else { + node->right_child =3D FALSE; + right->left_child =3D TRUE; + } + right->left =3D node; + + a_bal =3D node->balance; + b_bal =3D right->balance; + + if (b_bal <=3D 0) { + if (a_bal >=3D 1) { + right->balance =3D b_bal - 1; + } else { + right->balance =3D a_bal + b_bal - 2; + } + node->balance =3D a_bal - 1; + } else { + if (a_bal <=3D b_bal) { + right->balance =3D a_bal - 2; + } else { + right->balance =3D b_bal - 1; + } + node->balance =3D a_bal - b_bal - 1; + } + + return right; +} + +static QTreeNode * +q_tree_node_rotate_right(QTreeNode *node) +{ + QTreeNode *left; + gint a_bal; + gint b_bal; + + left =3D node->left; + + if (left->right_child) { + node->left =3D left->right; + } else { + node->left_child =3D FALSE; + left->right_child =3D TRUE; + } + left->right =3D node; + + a_bal =3D node->balance; + b_bal =3D left->balance; + + if (b_bal <=3D 0) { + if (b_bal > a_bal) { + left->balance =3D b_bal + 1; + } else { + left->balance =3D a_bal + 2; + } + node->balance =3D a_bal - b_bal + 1; + } else { + if (a_bal <=3D -1) { + left->balance =3D b_bal + 1; + } else { + left->balance =3D a_bal + b_bal + 2; + } + node->balance =3D a_bal + 1; + } + + return left; +} + +#ifdef Q_TREE_DEBUG +static gint +q_tree_node_height(QTreeNode *node) +{ + gint left_height; + gint right_height; + + if (node) { + left_height =3D 0; + right_height =3D 0; + + if (node->left_child) { + left_height =3D q_tree_node_height(node->left); + } + + if (node->right_child) { + right_height =3D q_tree_node_height(node->right); + } + + return MAX(left_height, right_height) + 1; + } + + return 0; +} + +static void q_tree_node_check(QTreeNode *node) +{ + gint left_height; + gint right_height; + gint balance; + QTreeNode *tmp; + + if (node) { + if (node->left_child) { + tmp =3D q_tree_node_previous(node); + g_assert(tmp->right =3D=3D node); + } + + if (node->right_child) { + tmp =3D q_tree_node_next(node); + g_assert(tmp->left =3D=3D node); + } + + left_height =3D 0; + right_height =3D 0; + + if (node->left_child) { + left_height =3D q_tree_node_height(node->left); + } + if (node->right_child) { + right_height =3D q_tree_node_height(node->right); + } + + balance =3D right_height - left_height; + g_assert(balance =3D=3D node->balance); + + if (node->left_child) { + q_tree_node_check(node->left); + } + if (node->right_child) { + q_tree_node_check(node->right); + } + } +} +#endif diff --git a/tests/bench/meson.build b/tests/bench/meson.build index 7477a1f401..4e6b469066 100644 --- a/tests/bench/meson.build +++ b/tests/bench/meson.build @@ -9,6 +9,10 @@ xbzrle_bench =3D executable('xbzrle-bench', dependencies: [qemuutil,migration]) endif =20 +qtree_bench =3D executable('qtree-bench', + sources: 'qtree-bench.c', + dependencies: [qemuutil]) + executable('atomic_add-bench', sources: files('atomic_add-bench.c'), dependencies: [qemuutil], diff --git a/tests/unit/meson.build b/tests/unit/meson.build index fa63cfe6ff..3bc78d8660 100644 --- a/tests/unit/meson.build +++ b/tests/unit/meson.build @@ -36,6 +36,7 @@ tests =3D { 'test-rcu-slist': [], 'test-qdist': [], 'test-qht': [], + 'test-qtree': [], 'test-bitops': [], 'test-bitcnt': [], 'test-qgraph': ['../qtest/libqos/qgraph.c'], diff --git a/util/meson.build b/util/meson.build index 26c73e586b..3c2cfc6ede 100644 --- a/util/meson.build +++ b/util/meson.build @@ -26,6 +26,7 @@ util_ss.add(when: 'CONFIG_WIN32', if_true: files('oslib-w= in32.c')) util_ss.add(when: 'CONFIG_WIN32', if_true: files('qemu-thread-win32.c')) util_ss.add(when: 'CONFIG_WIN32', if_true: winmm) util_ss.add(when: 'CONFIG_WIN32', if_true: pathcch) +util_ss.add(when: 'HAVE_GLIB_WITH_SLICE_ALLOCATOR', if_true: files('qtree.= c')) util_ss.add(files('envlist.c', 'path.c', 'module.c')) util_ss.add(files('host-utils.c')) util_ss.add(files('bitmap.c', 'bitops.c')) --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044356; cv=none; d=zohomail.com; s=zohoarc; b=B1FmERcb/8iPe/bWxA+wF5MzMG2tvAq6+5yxTVPzbm8EJloBA/uxkQ121UmIt2XMju1kldsT724Flwuohlycoo09YoazVsVVYQ+Wqhm2g5wlW9GLSPXmL+Fyim82A3pOkifnmBzyFJGu8DgfSqooJvgQNe1UPUSw00cT+4N8WjY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044356; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=NMY/vAWySJgtF4AXv2RQlxqwdk/wExvFK2+RnIza0g8=; b=Dqa8g7UpJcku6PX1vDkGmKMXV1Tg0gvNMPss6mVGQVOLwUm1FtWk8cIaU/URAN8SkcSqxg5rVqWws3QZUcBb9h+hKNaymze4oYoVhj2wshKPMhEDHhZ8EVmI8EztMbWBkhebchPaEMEGIiBj060rDWLyRsJhoRHb3u/Vo+u8wmY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044356774681.809470189189; Tue, 28 Mar 2023 15:59:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGc-0006r1-Fh; Tue, 28 Mar 2023 18:58:14 -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 1phIGb-0006qi-Bd for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:13 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGY-00072w-TN for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:13 -0400 Received: by mail-pl1-x632.google.com with SMTP id ix20so13180842plb.3 for ; Tue, 28 Mar 2023 15:58:10 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044289; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=NMY/vAWySJgtF4AXv2RQlxqwdk/wExvFK2+RnIza0g8=; b=Iydm46+LQJT7pESQNbTnaiUf4Eyu8lT7BxaNuACFgdF9J1irFXz456MDheXL7X/+hP xoB9vYBVuf9cEjepn1quFqoe8OA4Tqg5CrFzmGurhR7G0dEF1VBCe8Xgl5iEuey1GEPl uRNo21tVwXmKLTYVrhVjthwt+xbGF1+rpWDNEeNrd/8Yn4GhTbvRrKmsKJ30U2Q1khE1 EcyZBuQ16Kls3mnAPpwSq4AHap/opxW9NedVoTVSzeLdiiW2CJlsm2dPfM87MmNz8++B TU3cVl++2n9n3v+doydWuZslPkO3BAsadpy3LZbRivrpv24FcVfcj4D7C7JW1CKg7GQ6 yeyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044289; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=NMY/vAWySJgtF4AXv2RQlxqwdk/wExvFK2+RnIza0g8=; b=pb3AiFQRpyQ6obQRQrJ3Lx0vt02WBsaU1v4fOWeA9pw3fEc7S6dfPpOvkO7MT8Np5a 9e6cmpRFkFJMCINjE0xqQVNWmMtJ13pWOAfKgdxKp1fQxjv3eGXfHPqSV4NINhu4gBhq JVf0pnGDmrHBpQC8ThS0YwGhkyXgXYvUk0JRBztjQTP6K9DUusEWEQMA18eJTXZ+yJ+n 4Gy2ept867oYti4qBQT9+ZcrAdyWR+Z5gxbQOEYdiwwXAc0eAAYAohBVcMH98a47MqP7 aiYubiZJzA5gYN2wfV+mtMIEDMpI0kmJBCZ2cVoRLYZYQa/YkNVUFEKL6reGJLGABlC6 vxvA== X-Gm-Message-State: AAQBX9fNxwRTmbdyZXpknevNagA0BiTVR7BPzkZ7YkRs+Eeur1/LIWFv QC3G3UKcKisu/GYx8tm3EQtC6bU0UnGc70ou0wM= X-Google-Smtp-Source: AKy350ahG+F7bHZ9ThdX7O42i5KS8Cj/B5H9x/Vythb5wgmSQoFAFqxixcBUby7dt84Dd+WXGGnkAA== X-Received: by 2002:a17:902:a416:b0:19b:107b:698e with SMTP id p22-20020a170902a41600b0019b107b698emr14222078plq.14.1680044289428; Tue, 28 Mar 2023 15:58:09 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, Emilio Cota , Valentin David , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 02/15] tcg: use QTree instead of GTree Date: Tue, 28 Mar 2023 15:57:53 -0700 Message-Id: <20230328225806.2278728-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x632.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044358316100004 From: Emilio Cota qemu-user can hang in a multi-threaded fork. One common reason is that when creating a TB, between fork and exec we manipulate a GTree whose memory allocator (GSlice) is not fork-safe. Although POSIX does not mandate it, the system's allocator (e.g. tcmalloc, libc malloc) is probably fork-safe. Fix some of these hangs by using QTree, which uses the system's allocator regardless of the Glib version that we used at configuration time. Tested with the test program in the original bug report, i.e.: ``` void garble() { int pid =3D fork(); if (pid =3D=3D 0) { exit(0); } else { int wstatus; waitpid(pid, &wstatus, 0); } } void supragarble(unsigned depth) { if (depth =3D=3D 0) return ; std::thread a(supragarble, depth-1); std::thread b(supragarble, depth-1); garble(); a.join(); b.join(); } int main() { supragarble(10); } ``` Resolves: https://gitlab.com/qemu-project/qemu/-/issues/285 Reported-by: Valentin David Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Emilio Cota Message-Id: <20230205163758.416992-3-cota@braap.org> [rth: Add QEMU_DISABLE_CFI for all callback using functions.] Signed-off-by: Richard Henderson --- accel/tcg/tb-maint.c | 17 +++++++++-------- tcg/region.c | 19 ++++++++++--------- util/qtree.c | 8 ++++---- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/accel/tcg/tb-maint.c b/accel/tcg/tb-maint.c index 7246c1c46b..a173db17e6 100644 --- a/accel/tcg/tb-maint.c +++ b/accel/tcg/tb-maint.c @@ -19,6 +19,7 @@ =20 #include "qemu/osdep.h" #include "qemu/interval-tree.h" +#include "qemu/qtree.h" #include "exec/cputlb.h" #include "exec/log.h" #include "exec/exec-all.h" @@ -314,7 +315,7 @@ struct page_entry { * See also: page_collection_lock(). */ struct page_collection { - GTree *tree; + QTree *tree; struct page_entry *max; }; =20 @@ -467,7 +468,7 @@ static bool page_trylock_add(struct page_collection *se= t, tb_page_addr_t addr) struct page_entry *pe; PageDesc *pd; =20 - pe =3D g_tree_lookup(set->tree, &index); + pe =3D q_tree_lookup(set->tree, &index); if (pe) { return false; } @@ -478,7 +479,7 @@ static bool page_trylock_add(struct page_collection *se= t, tb_page_addr_t addr) } =20 pe =3D page_entry_new(pd, index); - g_tree_insert(set->tree, &pe->index, pe); + q_tree_insert(set->tree, &pe->index, pe); =20 /* * If this is either (1) the first insertion or (2) a page whose index @@ -525,13 +526,13 @@ static struct page_collection *page_collection_lock(t= b_page_addr_t start, end >>=3D TARGET_PAGE_BITS; g_assert(start <=3D end); =20 - set->tree =3D g_tree_new_full(tb_page_addr_cmp, NULL, NULL, + set->tree =3D q_tree_new_full(tb_page_addr_cmp, NULL, NULL, page_entry_destroy); set->max =3D NULL; assert_no_pages_locked(); =20 retry: - g_tree_foreach(set->tree, page_entry_lock, NULL); + q_tree_foreach(set->tree, page_entry_lock, NULL); =20 for (index =3D start; index <=3D end; index++) { TranslationBlock *tb; @@ -542,7 +543,7 @@ static struct page_collection *page_collection_lock(tb_= page_addr_t start, continue; } if (page_trylock_add(set, index << TARGET_PAGE_BITS)) { - g_tree_foreach(set->tree, page_entry_unlock, NULL); + q_tree_foreach(set->tree, page_entry_unlock, NULL); goto retry; } assert_page_locked(pd); @@ -551,7 +552,7 @@ static struct page_collection *page_collection_lock(tb_= page_addr_t start, (tb_page_addr1(tb) !=3D -1 && page_trylock_add(set, tb_page_addr1(tb)))) { /* drop all locks, and reacquire in order */ - g_tree_foreach(set->tree, page_entry_unlock, NULL); + q_tree_foreach(set->tree, page_entry_unlock, NULL); goto retry; } } @@ -562,7 +563,7 @@ static struct page_collection *page_collection_lock(tb_= page_addr_t start, static void page_collection_unlock(struct page_collection *set) { /* entries are unlocked and freed via page_entry_destroy */ - g_tree_destroy(set->tree); + q_tree_destroy(set->tree); g_free(set); } =20 diff --git a/tcg/region.c b/tcg/region.c index 88d6bb273f..bef4c4756f 100644 --- a/tcg/region.c +++ b/tcg/region.c @@ -28,6 +28,7 @@ #include "qemu/mprotect.h" #include "qemu/memalign.h" #include "qemu/cacheinfo.h" +#include "qemu/qtree.h" #include "qapi/error.h" #include "exec/exec-all.h" #include "tcg/tcg.h" @@ -36,7 +37,7 @@ =20 struct tcg_region_tree { QemuMutex lock; - GTree *tree; + QTree *tree; /* padding to avoid false sharing is computed at run-time */ }; =20 @@ -163,7 +164,7 @@ static void tcg_region_trees_init(void) struct tcg_region_tree *rt =3D region_trees + i * tree_size; =20 qemu_mutex_init(&rt->lock); - rt->tree =3D g_tree_new_full(tb_tc_cmp, NULL, NULL, tb_destroy); + rt->tree =3D q_tree_new_full(tb_tc_cmp, NULL, NULL, tb_destroy); } } =20 @@ -202,7 +203,7 @@ void tcg_tb_insert(TranslationBlock *tb) =20 g_assert(rt !=3D NULL); qemu_mutex_lock(&rt->lock); - g_tree_insert(rt->tree, &tb->tc, tb); + q_tree_insert(rt->tree, &tb->tc, tb); qemu_mutex_unlock(&rt->lock); } =20 @@ -212,7 +213,7 @@ void tcg_tb_remove(TranslationBlock *tb) =20 g_assert(rt !=3D NULL); qemu_mutex_lock(&rt->lock); - g_tree_remove(rt->tree, &tb->tc); + q_tree_remove(rt->tree, &tb->tc); qemu_mutex_unlock(&rt->lock); } =20 @@ -232,7 +233,7 @@ TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr) } =20 qemu_mutex_lock(&rt->lock); - tb =3D g_tree_lookup(rt->tree, &s); + tb =3D q_tree_lookup(rt->tree, &s); qemu_mutex_unlock(&rt->lock); return tb; } @@ -267,7 +268,7 @@ void tcg_tb_foreach(GTraverseFunc func, gpointer user_d= ata) for (i =3D 0; i < region.n; i++) { struct tcg_region_tree *rt =3D region_trees + i * tree_size; =20 - g_tree_foreach(rt->tree, func, user_data); + q_tree_foreach(rt->tree, func, user_data); } tcg_region_tree_unlock_all(); } @@ -281,7 +282,7 @@ size_t tcg_nb_tbs(void) for (i =3D 0; i < region.n; i++) { struct tcg_region_tree *rt =3D region_trees + i * tree_size; =20 - nb_tbs +=3D g_tree_nnodes(rt->tree); + nb_tbs +=3D q_tree_nnodes(rt->tree); } tcg_region_tree_unlock_all(); return nb_tbs; @@ -296,8 +297,8 @@ static void tcg_region_tree_reset_all(void) struct tcg_region_tree *rt =3D region_trees + i * tree_size; =20 /* Increment the refcount first so that destroy acts as a reset */ - g_tree_ref(rt->tree); - g_tree_destroy(rt->tree); + q_tree_ref(rt->tree); + q_tree_destroy(rt->tree); } tcg_region_tree_unlock_all(); } diff --git a/util/qtree.c b/util/qtree.c index deb46c187f..31f0b46182 100644 --- a/util/qtree.c +++ b/util/qtree.c @@ -310,7 +310,7 @@ q_tree_node_next(QTreeNode *node) * * Since: 2.70 in GLib. Internal in Qtree, i.e. not in the public API. */ -static void +static void QEMU_DISABLE_CFI q_tree_remove_all(QTree *tree) { QTreeNode *node; @@ -532,7 +532,7 @@ q_tree_replace(QTree *tree, } =20 /* internal insert routine */ -static QTreeNode * +static QTreeNode * QEMU_DISABLE_CFI q_tree_insert_internal(QTree *tree, gpointer key, gpointer value, @@ -721,7 +721,7 @@ q_tree_steal(QTree *tree, } =20 /* internal remove routine */ -static gboolean +static gboolean QEMU_DISABLE_CFI q_tree_remove_internal(QTree *tree, gconstpointer key, gboolean steal) @@ -1182,7 +1182,7 @@ q_tree_node_balance(QTreeNode *node) return node; } =20 -static QTreeNode * +static QTreeNode * QEMU_DISABLE_CFI q_tree_find_node(QTree *tree, gconstpointer key) { --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044383; cv=none; d=zohomail.com; s=zohoarc; b=H4mAwUWnlUcAX2mdywLxrMXRHyLAL8fXoPoYuc9bcFCJ4tNST0+4ZqJsvu83ZNSQYkku5EeM981vXKz9lyJkdvH0/XGzekAB3sOy+VODtOHvLOTnQ8Jl5CMzZ8yrdL7VrcjQ8tinIPCP6ID9SIagsnfmNu+vvp7TE0w6zMOxKlI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044383; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=d+avOkff6EBvJ8K9x1TsSkw5p9567zBA1o+YX6GdDwE=; b=ltjjMfv8GyC5P5//L7ack7vkqAvKoWIU+clyn608gl6Xx9dU4DvrG/dW/zk+Bqe/sbH3aKhNfSw2RDTKGa6rpWiva2iARvPq8j4nNkIGlTOPGRq25cspsRRar7yLcn+vYwPNxS+5ike89Imy1AkHMbYbN+kxFHyXhKVIZ0jscSs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044383022907.6751703134797; Tue, 28 Mar 2023 15:59:43 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGd-0006rS-Gb; Tue, 28 Mar 2023 18:58:15 -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 1phIGb-0006qn-GC for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:13 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGZ-000739-S7 for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:13 -0400 Received: by mail-pl1-x62c.google.com with SMTP id ja10so13168899plb.5 for ; Tue, 28 Mar 2023 15:58:11 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044290; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=d+avOkff6EBvJ8K9x1TsSkw5p9567zBA1o+YX6GdDwE=; b=OUXdSWbGXG/mQCkfiX0uRMcLk9Wxl/oBVOg730ASbLbwNzV23abyIvdYitIUlFr5+/ BpZ1uKdlJfWsb6jIBjGr2oTt7DlfkBLyVsvh1ZT+n2HiZLk3FZCgs+9G638TWT89xXW8 ndPYo23Iqfbiwy7L12+LF+vMUdCtcl6k833N9m2B9M0bfu3WmtS/ThHb2mf+Kd0pqHt2 9PcRW1CTMsNcuyBo2V69OgiqzUV/ltXQV+ftV0Vs87JMM69PZQyojm+do67NUfi7JOSD aknR0V31PozHNA2jGC7InyjaWF09L11DDsJd8RWuYDkUSTGiM2x703LhJ5dbsNXGw19T JwEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044290; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=d+avOkff6EBvJ8K9x1TsSkw5p9567zBA1o+YX6GdDwE=; b=mMoIwsmnKyqxDLDBL1/kZjQJCeX4ki1kF6F1aepAaE71w/iTXVUriNWaMOatdtxYku bu1yU2tvh5ihAUD0EfeqfRlxbIqTuc6SyIQPm5B2YMAynOWUUcQtdyFnu09bXsVqRO6k uYpNW3BVv57yBSWKpeXhJGD0T/Y8c+upmWyHK9ibF4uP1dL66CycQWNuXnAMaTC4951X BM0h0PKfrIfdFXD1LEAgbgSBuDvLMs2bJIGJIERoah3hke8TJz5vuIk3ECbQSsdwD4zG pz3R68GsSZidUIqf0rxyI7LVzGpH5AMclJ29Oam1QYtPB7A4kBZc+p6KfkgP/FP/nGey nc1Q== X-Gm-Message-State: AAQBX9e2or7a+wcckz/j4rEZg0Ja2pwUwnkR3T0dl+K9x8AYBB2WdFk6 jTJNqJH5m16v+25id5f4PtWRR5JQ7gWr5/hTgyo= X-Google-Smtp-Source: AKy350Y3vn71tgM7aMesEY/mx3phXw+biPcLkpTwINV3C35MjRFLXGQPYfLArdVQjn88+R6vgF68Zg== X-Received: by 2002:a17:903:70b:b0:1a1:ab92:5c6b with SMTP id kk11-20020a170903070b00b001a1ab925c6bmr13094978plb.57.1680044290541; Tue, 28 Mar 2023 15:58:10 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 03/15] linux-user: Diagnose misaligned -R size Date: Tue, 28 Mar 2023 15:57:54 -0700 Message-Id: <20230328225806.2278728-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::62c; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62c.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044384508100001 We have been enforcing host page alignment for the non-R fallback of MAX_RESERVED_VA, but failing to enforce for -R. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Richard Henderson --- linux-user/main.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/linux-user/main.c b/linux-user/main.c index 4b18461969..39d9bd4d7a 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -793,6 +793,12 @@ int main(int argc, char **argv, char **envp) */ max_reserved_va =3D MAX_RESERVED_VA(cpu); if (reserved_va !=3D 0) { + if (reserved_va % qemu_host_page_size) { + char *s =3D size_to_str(qemu_host_page_size); + fprintf(stderr, "Reserved virtual address not aligned mod %s\n= ", s); + g_free(s); + exit(EXIT_FAILURE); + } if (max_reserved_va && reserved_va > max_reserved_va) { fprintf(stderr, "Reserved virtual address too big\n"); exit(EXIT_FAILURE); --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044411; cv=none; d=zohomail.com; s=zohoarc; b=h0tb+Mesu+7Rk1VPXHpSOcj2E6dPjpui1axzLYCrBTxomYwScHW5KO/TKNRzAElnzJVfsamrkuk0pvDVMFpHrcWYnrKzSKnRQJ+RSSjHSG1368s1kVywjTc4VfFiacTqeZi+4QsL7JRComqd4P25we80E06bZSDN2fEaZFtd8fc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044411; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=bCPczMczM94oK/Alb9CocBQFGZzrUwYTVyWrzUdkU2M=; b=lcJ3Y9QYqGz1IRAxAQXYnh62pZ/PJcApjU1p3oPC13kMtpc339eMH016B9WEwtUnlD53BS2M1nyCYA9sRT7ev77a4fQrLAkJpASmRlsJKFmgRfmBwrOGLcHIbWNTw+KKEixMvA62kjeoicLYsVdogFUNjacONS0GUwAz0cjUWTc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044411446678.0065508661188; Tue, 28 Mar 2023 16:00:11 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGf-0006s6-4c; Tue, 28 Mar 2023 18:58:17 -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 1phIGd-0006rH-9i for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:15 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGb-00073J-0H for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:15 -0400 Received: by mail-pj1-x1030.google.com with SMTP id gp15-20020a17090adf0f00b0023d1bbd9f9eso16757118pjb.0 for ; Tue, 28 Mar 2023 15:58:12 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044291; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=bCPczMczM94oK/Alb9CocBQFGZzrUwYTVyWrzUdkU2M=; b=Rk3LVpjTR0fu/sDX0uHMh8d/PKRRoHN4ldNrrowcBDnFf6HzAGb3vkujlqNpIudoGQ 35diYY5pHVlGXfrFq0YehGFxQUu9dgxGqPZ6OD76FriStj0y+/tubg/8YW6rDDW00REn H749GlfTXkxs/5WYU2/YUSZzdNpoBIPpPme1f0PnovRKr2+5aLqwHa4uH+WvmDdNcLGx 4n5Z0T/oXcPtBWhMlNnMk5rbDECxdPh6B0lcd17xMPKyR8F3YyBIqwwIV9Hi6eKRpJ9o izmH9ie9EDGHei6fgzD1JUgD1EmD+LNnxnJAs3v8eZcQ/X9gwegCmS3lWuB9R4zlJmxB Lfeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044291; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=bCPczMczM94oK/Alb9CocBQFGZzrUwYTVyWrzUdkU2M=; b=jAIWT8iwZHGlEY5Wc+a2xOsaFDoHBX9C63eg5as8FZgOoA8OGYpTkjNHdN5OH/Bt8D eZhzIwRox2lFuvy/edWFqDJF05YiXlFNgfqXEV6Hm1bf5i+nTUEiwS/rhDA5K/oNaVPF gL1GGegMKG9jGylkImiWO372u0YNPHqewhhxqwKKf5bBhEoInum1acI7GbfkfhalduCH rbLslnwXw/wS4vTKubociuiOVcvZ1NTB6IMBt3XR/DNH39on26L5JJIUe+Af4wRQf58v H/5D3/q6pIArmZx2M5a8XkMe8EOT4OlwwtJ+pl9uUwAnANL36fweWrf36hHy//cMTV7Y yMgw== X-Gm-Message-State: AAQBX9dXkL+EOniGiH8EBQGTMxeRS/doBZ/Hv1BFJc2fqRR9Co0nVSfb uTygRsFEFdSdBFqE7WW4YqxaIazZjMXx3WxGovo= X-Google-Smtp-Source: AKy350YsGX+AY7Qzdyb3EX82v80Xu6HVZmuQGaXok5+mwe+bLWXvZ/cm0a0jlT+PugWslUfBM00fAw== X-Received: by 2002:a17:90b:1d02:b0:23d:133a:62cc with SMTP id on2-20020a17090b1d0200b0023d133a62ccmr18603018pjb.17.1680044291591; Tue, 28 Mar 2023 15:58:11 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 04/15] accel/tcg: Pass last not end to page_set_flags Date: Tue, 28 Mar 2023 15:57:55 -0700 Message-Id: <20230328225806.2278728-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1030.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044412672100001 Pass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved. Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1528 Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Richard Henderson --- include/exec/cpu-all.h | 2 +- accel/tcg/user-exec.c | 16 +++++++--------- bsd-user/mmap.c | 6 +++--- linux-user/elfload.c | 11 ++++++----- linux-user/mmap.c | 16 ++++++++-------- linux-user/syscall.c | 4 ++-- 6 files changed, 27 insertions(+), 28 deletions(-) diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h index 548be9c8ea..a2662b1e83 100644 --- a/include/exec/cpu-all.h +++ b/include/exec/cpu-all.h @@ -276,7 +276,7 @@ typedef int (*walk_memory_regions_fn)(void *, target_ul= ong, int walk_memory_regions(void *, walk_memory_regions_fn); =20 int page_get_flags(target_ulong address); -void page_set_flags(target_ulong start, target_ulong end, int flags); +void page_set_flags(target_ulong start, target_ulong last, int flags); void page_reset_target_data(target_ulong start, target_ulong end); int page_check_range(target_ulong start, target_ulong len, int flags); =20 diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c index 7b37fd229e..035f8096b2 100644 --- a/accel/tcg/user-exec.c +++ b/accel/tcg/user-exec.c @@ -480,24 +480,22 @@ static bool pageflags_set_clear(target_ulong start, t= arget_ulong last, * The flag PAGE_WRITE_ORG is positioned automatically depending * on PAGE_WRITE. The mmap_lock should already be held. */ -void page_set_flags(target_ulong start, target_ulong end, int flags) +void page_set_flags(target_ulong start, target_ulong last, int flags) { - target_ulong last; bool reset =3D false; bool inval_tb =3D false; =20 /* This function should never be called with addresses outside the guest address space. If this assert fires, it probably indicates a missing call to h2g_valid. */ - assert(start < end); - assert(end - 1 <=3D GUEST_ADDR_MAX); + assert(start <=3D last); + assert(last <=3D GUEST_ADDR_MAX); /* Only set PAGE_ANON with new mappings. */ assert(!(flags & PAGE_ANON) || (flags & PAGE_RESET)); assert_memory_lock(); =20 - start =3D start & TARGET_PAGE_MASK; - end =3D TARGET_PAGE_ALIGN(end); - last =3D end - 1; + start &=3D TARGET_PAGE_MASK; + last |=3D ~TARGET_PAGE_MASK; =20 if (!(flags & PAGE_VALID)) { flags =3D 0; @@ -510,7 +508,7 @@ void page_set_flags(target_ulong start, target_ulong en= d, int flags) } =20 if (!flags || reset) { - page_reset_target_data(start, end); + page_reset_target_data(start, last + 1); inval_tb |=3D pageflags_unset(start, last); } if (flags) { @@ -518,7 +516,7 @@ void page_set_flags(target_ulong start, target_ulong en= d, int flags) ~(reset ? 0 : PAGE_STICKY)); } if (inval_tb) { - tb_invalidate_phys_range(start, end); + tb_invalidate_phys_range(start, last + 1); } } =20 diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c index d6c5a344c9..696057551a 100644 --- a/bsd-user/mmap.c +++ b/bsd-user/mmap.c @@ -118,7 +118,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int= prot) if (ret !=3D 0) goto error; } - page_set_flags(start, start + len, prot | PAGE_VALID); + page_set_flags(start, start + len - 1, prot | PAGE_VALID); mmap_unlock(); return 0; error: @@ -656,7 +656,7 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, in= t prot, } } the_end1: - page_set_flags(start, start + len, prot | PAGE_VALID); + page_set_flags(start, start + len - 1, prot | PAGE_VALID); the_end: #ifdef DEBUG_MMAP printf("ret=3D0x" TARGET_ABI_FMT_lx "\n", start); @@ -767,7 +767,7 @@ int target_munmap(abi_ulong start, abi_ulong len) } =20 if (ret =3D=3D 0) { - page_set_flags(start, start + len, 0); + page_set_flags(start, start + len - 1, 0); } mmap_unlock(); return ret; diff --git a/linux-user/elfload.c b/linux-user/elfload.c index 1dbc1f0f9b..fa4cc41567 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -213,7 +213,7 @@ static bool init_guest_commpage(void) exit(EXIT_FAILURE); } page_set_flags(TARGET_VSYSCALL_PAGE, - TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE, + TARGET_VSYSCALL_PAGE | ~TARGET_PAGE_MASK, PAGE_EXEC | PAGE_VALID); return true; } @@ -444,7 +444,7 @@ static bool init_guest_commpage(void) exit(EXIT_FAILURE); } =20 - page_set_flags(commpage, commpage + qemu_host_page_size, + page_set_flags(commpage, commpage | ~qemu_host_page_mask, PAGE_READ | PAGE_EXEC | PAGE_VALID); return true; } @@ -1316,7 +1316,7 @@ static bool init_guest_commpage(void) exit(EXIT_FAILURE); } =20 - page_set_flags(LO_COMMPAGE, LO_COMMPAGE + TARGET_PAGE_SIZE, + page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK, PAGE_READ | PAGE_EXEC | PAGE_VALID); return true; } @@ -1728,7 +1728,7 @@ static bool init_guest_commpage(void) * and implement syscalls. Here, simply mark the page executable. * Special case the entry points during translation (see do_page_zero). */ - page_set_flags(LO_COMMPAGE, LO_COMMPAGE + TARGET_PAGE_SIZE, + page_set_flags(LO_COMMPAGE, LO_COMMPAGE | ~TARGET_PAGE_MASK, PAGE_EXEC | PAGE_VALID); return true; } @@ -2209,7 +2209,8 @@ static void zero_bss(abi_ulong elf_bss, abi_ulong las= t_bss, int prot) =20 /* Ensure that the bss page(s) are valid */ if ((page_get_flags(last_bss-1) & prot) !=3D prot) { - page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_V= ALID); + page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss - 1, + prot | PAGE_VALID); } =20 if (host_start < host_map_start) { diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 28135c9e6a..1d07ff5d2c 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -181,7 +181,7 @@ int target_mprotect(abi_ulong start, abi_ulong len, int= target_prot) } } =20 - page_set_flags(start, start + len, page_flags); + page_set_flags(start, start + len - 1, page_flags); ret =3D 0; =20 error: @@ -640,15 +640,15 @@ abi_long target_mmap(abi_ulong start, abi_ulong len, = int target_prot, } page_flags |=3D PAGE_RESET; if (passthrough_start =3D=3D passthrough_end) { - page_set_flags(start, start + len, page_flags); + page_set_flags(start, start + len - 1, page_flags); } else { if (start < passthrough_start) { - page_set_flags(start, passthrough_start, page_flags); + page_set_flags(start, passthrough_start - 1, page_flags); } - page_set_flags(passthrough_start, passthrough_end, + page_set_flags(passthrough_start, passthrough_end - 1, page_flags | PAGE_PASSTHROUGH); if (passthrough_end < start + len) { - page_set_flags(passthrough_end, start + len, page_flags); + page_set_flags(passthrough_end, start + len - 1, page_flags); } } the_end: @@ -763,7 +763,7 @@ int target_munmap(abi_ulong start, abi_ulong len) } =20 if (ret =3D=3D 0) { - page_set_flags(start, start + len, 0); + page_set_flags(start, start + len - 1, 0); } mmap_unlock(); return ret; @@ -849,8 +849,8 @@ abi_long target_mremap(abi_ulong old_addr, abi_ulong ol= d_size, } else { new_addr =3D h2g(host_addr); prot =3D page_get_flags(old_addr); - page_set_flags(old_addr, old_addr + old_size, 0); - page_set_flags(new_addr, new_addr + new_size, + page_set_flags(old_addr, old_addr + old_size - 1, 0); + page_set_flags(new_addr, new_addr + new_size - 1, prot | PAGE_VALID | PAGE_RESET); } mmap_unlock(); diff --git a/linux-user/syscall.c b/linux-user/syscall.c index 27871641f4..69f740ff98 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -4595,7 +4595,7 @@ static inline abi_ulong do_shmat(CPUArchState *cpu_en= v, } raddr=3Dh2g((unsigned long)host_raddr); =20 - page_set_flags(raddr, raddr + shm_info.shm_segsz, + page_set_flags(raddr, raddr + shm_info.shm_segsz - 1, PAGE_VALID | PAGE_RESET | PAGE_READ | (shmflg & SHM_RDONLY ? 0 : PAGE_WRITE)); =20 @@ -4625,7 +4625,7 @@ static inline abi_long do_shmdt(abi_ulong shmaddr) for (i =3D 0; i < N_SHM_REGIONS; ++i) { if (shm_regions[i].in_use && shm_regions[i].start =3D=3D shmaddr) { shm_regions[i].in_use =3D false; - page_set_flags(shmaddr, shmaddr + shm_regions[i].size, 0); + page_set_flags(shmaddr, shmaddr + shm_regions[i].size - 1, 0); break; } } --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044357; cv=none; d=zohomail.com; s=zohoarc; b=ARjZGw/whPYcClNd4wzgUabBPo22+GrkXDhsjonRqHoCjLnbSX+lsOq2qfDgoACgn31fE9ba4BOkow4Lf1GiFRu7w6WKkbmxm2LX79OrYhEA1glfsB+igcihi8dhA7kSQDFWGQB/pHrDcNiqFNP3Z9ZBsKTopnY98vVRx2g9L88= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044357; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=g05J8wW1HhmR3dbxLdTrvqKbK3sSXASxM+uo2e7n0jQ=; b=Zbtpj8TzuSNbXWU+vgdnBGplXakm6IcDsKyeIFc+h19yGlLFyF9bmp42efZvwijRVmKui0uKWF2DPfFaOJ/yoA9axDZ/Nbo7SX4SurDHLElIKI5cYrWqkK+5koanmrgBpOlKHSQrqT4etCD1RmMsQvxWo77vAQeEKfMdguAN+v8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044357272240.76822804225083; Tue, 28 Mar 2023 15:59:17 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGh-0006tE-3F; Tue, 28 Mar 2023 18:58:19 -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 1phIGd-0006rX-LI for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:15 -0400 Received: from mail-pl1-x634.google.com ([2607:f8b0:4864:20::634]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGb-00073U-Tv for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:15 -0400 Received: by mail-pl1-x634.google.com with SMTP id c18so13146923ple.11 for ; Tue, 28 Mar 2023 15:58:13 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044292; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=g05J8wW1HhmR3dbxLdTrvqKbK3sSXASxM+uo2e7n0jQ=; b=x5YOIhpuO7maSU67Ij2gFXqAZsVcRGecZYiD6K5d7sYMTiNjzL1xCYB3/INaWPJ8yf UFDfEnSGRKoouc7v4XqfYM6CKQl71C4okqUbYShbhi9aRg6VmrDh9tC3J1mtvfP3DDOc knQieXxlfy57hA4eLfL/OXSaovM+aoBVelukX+oSVtokz9aBhxBGD//lBaeQ4fLYQx+F 8u2m/f6I1e5NaULHUdmEEIKD3L92+dVhGCYVKoSzN20GUAElu+ZGrJeEKKBYQne7Jjgw e9GD15fymXEL9nnfWSIWeqAmL88qg+q73szc1aez8WO6tCYiO1NNf6fIklQ/SIhNFiyQ 6AMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044292; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=g05J8wW1HhmR3dbxLdTrvqKbK3sSXASxM+uo2e7n0jQ=; b=4eivqGKfDX1tKHaXZTXNYqW+S/Ex0Imm6IPAYIJi1hkI4u3/DGpHrDQCPCzeynyVgi RQ88sf2U71/azBzCVnnFyak//+q0wGSuQmGlBUeOsJrgyKcon212iVtziqJGCEl/eUOy Xj4pYQ9lghYA0ralipXXzA5g2sgUlev6a5nT5F2ExrGmIEICiDOp9jHXCX1rW9TkBERb fG42dFxRmHgLOzCT3C9o3a26bW2OMiLHB1br4NXRPqwo8yFZa2FP3NRUX6p6NaUdMrsP /ZbfMWS7L259AZxs/3ZNKMUuWd373V6xwniTqu4bo8F8xmivzzDnvVctTxlXxNfuLRnD BCZg== X-Gm-Message-State: AAQBX9ehS8rZmQpj50TCsUxdQ7lcLu6bUfl4QIXSz4f4SYl5NWYk8Xdu 6fAR5A1e9f/Ji8V2q2vws7lw8M8hCslYKCzF9b8= X-Google-Smtp-Source: AKy350bwuFqkNcD1nrQTNY3y4xJbEfCP+p45iKH3+CjZNgJFkFLd1jI9DPdqCDE/i32NJPhooiTZZw== X-Received: by 2002:a17:903:11cd:b0:1a2:35f3:360f with SMTP id q13-20020a17090311cd00b001a235f3360fmr14037871plh.15.1680044292516; Tue, 28 Mar 2023 15:58:12 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 05/15] accel/tcg: Pass last not end to page_reset_target_data Date: Tue, 28 Mar 2023 15:57:56 -0700 Message-Id: <20230328225806.2278728-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::634; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x634.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044358294100003 Pass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Richard Henderson --- include/exec/cpu-all.h | 2 +- accel/tcg/user-exec.c | 11 +++++------ linux-user/mmap.c | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h index a2662b1e83..64cb62dc54 100644 --- a/include/exec/cpu-all.h +++ b/include/exec/cpu-all.h @@ -277,7 +277,7 @@ int walk_memory_regions(void *, walk_memory_regions_fn); =20 int page_get_flags(target_ulong address); void page_set_flags(target_ulong start, target_ulong last, int flags); -void page_reset_target_data(target_ulong start, target_ulong end); +void page_reset_target_data(target_ulong start, target_ulong last); int page_check_range(target_ulong start, target_ulong len, int flags); =20 /** diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c index 035f8096b2..20b6fc2f6e 100644 --- a/accel/tcg/user-exec.c +++ b/accel/tcg/user-exec.c @@ -508,7 +508,7 @@ void page_set_flags(target_ulong start, target_ulong la= st, int flags) } =20 if (!flags || reset) { - page_reset_target_data(start, last + 1); + page_reset_target_data(start, last); inval_tb |=3D pageflags_unset(start, last); } if (flags) { @@ -814,15 +814,14 @@ typedef struct TargetPageDataNode { =20 static IntervalTreeRoot targetdata_root; =20 -void page_reset_target_data(target_ulong start, target_ulong end) +void page_reset_target_data(target_ulong start, target_ulong last) { IntervalTreeNode *n, *next; - target_ulong last; =20 assert_memory_lock(); =20 - start =3D start & TARGET_PAGE_MASK; - last =3D TARGET_PAGE_ALIGN(end) - 1; + start &=3D TARGET_PAGE_MASK; + last |=3D ~TARGET_PAGE_MASK; =20 for (n =3D interval_tree_iter_first(&targetdata_root, start, last), next =3D n ? interval_tree_iter_next(n, start, last) : NULL; @@ -885,7 +884,7 @@ void *page_get_target_data(target_ulong address) return t->data[(page - region) >> TARGET_PAGE_BITS]; } #else -void page_reset_target_data(target_ulong start, target_ulong end) { } +void page_reset_target_data(target_ulong start, target_ulong last) { } #endif /* TARGET_PAGE_DATA_SIZE */ =20 /* The softmmu versions of these helpers are in cputlb.c. */ diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 1d07ff5d2c..995146f60d 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -946,7 +946,7 @@ abi_long target_madvise(abi_ulong start, abi_ulong len_= in, int advice) if (can_passthrough_madvise(start, end)) { ret =3D get_errno(madvise(g2h_untagged(start), len, advice)); if ((advice =3D=3D MADV_DONTNEED) && (ret =3D=3D 0)) { - page_reset_target_data(start, start + len); + page_reset_target_data(start, start + len - 1); } } } --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044416; cv=none; d=zohomail.com; s=zohoarc; b=EyrDdCDRWnAiL68b8SyYridzln5dwheXHxkR5dn/nfB8G3gTOYPuEuL54Wgnjk4Gr/GmcH2fmMhnpZNOz9ZSAAROgLd3gEEzm3robgsbba7lg8bfd+3hjekaeu8BELtoWUIhMxU9QANoAki4rfLSUmFaVolapuMtBEfb9oI9+KI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044416; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=s8SVCaZUmE/Km0+IIG6EiSCxwnpR9ibdQSVxm7qmuBg=; b=c3YGMzIixNKC7de0N7hnPv2BJvyheVaU+kPAcmmNAsOjRtrxmjTw+RgL5H7QrT60iBZ/ewNdoQ/uvCOLlVrk3Fdfzq7gC8q/VVq/2FdLG1pgvQK6PVwhBqoHuZai21gSXwwg8wiqqBOImdNpP/e01utyoRdpf26g9wDw4Zph5TM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 168004441617038.601005833970476; Tue, 28 Mar 2023 16:00:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGf-0006sU-RA; Tue, 28 Mar 2023 18:58:17 -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 1phIGe-0006rv-EX for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:16 -0400 Received: from mail-pj1-x1031.google.com ([2607:f8b0:4864:20::1031]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGc-00073k-NL for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:16 -0400 Received: by mail-pj1-x1031.google.com with SMTP id om3-20020a17090b3a8300b0023efab0e3bfso16695170pjb.3 for ; Tue, 28 Mar 2023 15:58:14 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044293; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=s8SVCaZUmE/Km0+IIG6EiSCxwnpR9ibdQSVxm7qmuBg=; b=EDyXLAKt8sR4o7bh2k1Tpgf0oK5fbBojrpiNHQ4HT6EkbcQVJOmWANSrDu5zm1eb+z dOePtEItL3aTXdmcLVuc47AkpJaum09IpbQItK4N+9LR45sxDSf7nF5RVF4hIPYFg9F7 Hb2srV0WIyHc9L36qrh2XNOaAcoBgHPCzeo1ODVS5KkihvuGgvxC4szY4+HgR7NeT58w ORuhD5BiAmnuXBM+D57BEW/5/gKbIQSaPnwUouWno72+EvjKmA7Omn2k14Uq9XVboixq KIO060CiT0iofk2dBdtcGBH9hOjfxA7+gxBR+jykWcukfGOJrODCwbif+uSqTpfsnSvB 2/Lg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044293; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=s8SVCaZUmE/Km0+IIG6EiSCxwnpR9ibdQSVxm7qmuBg=; b=0lgTlxqmfu2NGiec9Hae5c+xllR896uPyUYq9B4N436l4/cyD5y9lv3XQg25X8lykM uwzEi+51cUBImT7S/jAREE4sOlwqCJFDLT+HFDzThYBh2KO62oHisanONrMvN1UU7z7t CP0nXB927pGcw9syDYvtamXNmioTBqQrnBvhLdL19XmUocmS+Cjq5sk+WJaJy/zDUr/v z6Op01KSOugSQa5W2EjVK3PN4byO5qBfyPy6AC5uk9IM329n0BlXb4L/kbztNNeSA+Qi GoHqEUifFk6/R4UiaxyUD2yScQqdyLcwHTldP+wuElvpLBCsptn7If+RuO+jJEVSh+ZT xC9w== X-Gm-Message-State: AAQBX9f2jssQ1VqO2DDI5j46j211InN8YKv7aXhCwehgbU6PQegl9V+4 /gaHo2rM/bU/uP0mvWRnfsMmAPuPvDj7E27zIzU= X-Google-Smtp-Source: AKy350YSPf3Ekgdyq4K9H9Pn9a8KFG53wimV5cSmbvlwBmAfwoDFhpL63QhcCVCBT3qNqRO/r3J/HA== X-Received: by 2002:a17:90a:1953:b0:240:973d:b431 with SMTP id 19-20020a17090a195300b00240973db431mr5945113pjh.42.1680044293438; Tue, 28 Mar 2023 15:58:13 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 06/15] accel/tcg: Pass last not end to PAGE_FOR_EACH_TB Date: Tue, 28 Mar 2023 15:57:57 -0700 Message-Id: <20230328225806.2278728-7-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1031; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1031.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044416665100001 Pass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Richard Henderson --- accel/tcg/tb-maint.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/accel/tcg/tb-maint.c b/accel/tcg/tb-maint.c index a173db17e6..04d2751bb6 100644 --- a/accel/tcg/tb-maint.c +++ b/accel/tcg/tb-maint.c @@ -127,29 +127,29 @@ static void tb_remove(TranslationBlock *tb) } =20 /* TODO: For now, still shared with translate-all.c for system mode. */ -#define PAGE_FOR_EACH_TB(start, end, pagedesc, T, N) \ - for (T =3D foreach_tb_first(start, end), \ - N =3D foreach_tb_next(T, start, end); \ +#define PAGE_FOR_EACH_TB(start, last, pagedesc, T, N) \ + for (T =3D foreach_tb_first(start, last), \ + N =3D foreach_tb_next(T, start, last); \ T !=3D NULL; \ - T =3D N, N =3D foreach_tb_next(N, start, end)) + T =3D N, N =3D foreach_tb_next(N, start, last)) =20 typedef TranslationBlock *PageForEachNext; =20 static PageForEachNext foreach_tb_first(tb_page_addr_t start, - tb_page_addr_t end) + tb_page_addr_t last) { - IntervalTreeNode *n =3D interval_tree_iter_first(&tb_root, start, end = - 1); + IntervalTreeNode *n =3D interval_tree_iter_first(&tb_root, start, last= ); return n ? container_of(n, TranslationBlock, itree) : NULL; } =20 static PageForEachNext foreach_tb_next(PageForEachNext tb, tb_page_addr_t start, - tb_page_addr_t end) + tb_page_addr_t last) { IntervalTreeNode *n; =20 if (tb) { - n =3D interval_tree_iter_next(&tb->itree, start, end - 1); + n =3D interval_tree_iter_next(&tb->itree, start, last); if (n) { return container_of(n, TranslationBlock, itree); } @@ -320,7 +320,7 @@ struct page_collection { }; =20 typedef int PageForEachNext; -#define PAGE_FOR_EACH_TB(start, end, pagedesc, tb, n) \ +#define PAGE_FOR_EACH_TB(start, last, pagedesc, tb, n) \ TB_FOR_EACH_TAGGED((pagedesc)->first_tb, tb, n, page_next) =20 #ifdef CONFIG_DEBUG_TCG @@ -995,10 +995,11 @@ void tb_invalidate_phys_range(tb_page_addr_t start, t= b_page_addr_t end) { TranslationBlock *tb; PageForEachNext n; + tb_page_addr_t last =3D end - 1; =20 assert_memory_lock(); =20 - PAGE_FOR_EACH_TB(start, end, unused, tb, n) { + PAGE_FOR_EACH_TB(start, last, unused, tb, n) { tb_phys_invalidate__locked(tb); } } @@ -1030,6 +1031,7 @@ bool tb_invalidate_phys_page_unwind(tb_page_addr_t ad= dr, uintptr_t pc) bool current_tb_modified; TranslationBlock *tb; PageForEachNext n; + tb_page_addr_t last; =20 /* * Without precise smc semantics, or when outside of a TB, @@ -1046,10 +1048,11 @@ bool tb_invalidate_phys_page_unwind(tb_page_addr_t = addr, uintptr_t pc) assert_memory_lock(); current_tb =3D tcg_tb_lookup(pc); =20 + last =3D addr | ~TARGET_PAGE_MASK; addr &=3D TARGET_PAGE_MASK; current_tb_modified =3D false; =20 - PAGE_FOR_EACH_TB(addr, addr + TARGET_PAGE_SIZE, unused, tb, n) { + PAGE_FOR_EACH_TB(addr, last, unused, tb, n) { if (current_tb =3D=3D tb && (tb_cflags(current_tb) & CF_COUNT_MASK) !=3D 1) { /* @@ -1091,12 +1094,13 @@ tb_invalidate_phys_page_range__locked(struct page_c= ollection *pages, bool current_tb_modified =3D false; TranslationBlock *current_tb =3D retaddr ? tcg_tb_lookup(retaddr) : NU= LL; #endif /* TARGET_HAS_PRECISE_SMC */ + tb_page_addr_t last G_GNUC_UNUSED =3D end - 1; =20 /* * We remove all the TBs in the range [start, end[. * XXX: see if in some cases it could be faster to invalidate all the = code */ - PAGE_FOR_EACH_TB(start, end, p, tb, n) { + PAGE_FOR_EACH_TB(start, last, p, tb, n) { /* NOTE: this is subtle as a TB may span two physical pages */ if (n =3D=3D 0) { /* NOTE: tb_end may be after the end of the page, but --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044338; cv=none; d=zohomail.com; s=zohoarc; b=MhZD/MO+xeOqflPcG+KKyuob6BP348pfeU53+juvn4rQL/KEBOlRgSnNFQ2DFDYVgNEwK1dPj3wBM8LF55xsgvCykskjC0DB01DsiKWOH8IAncIlcXuixzEC0dxl4sV2c+jL5zVFB2QfhXtFWYtx0koensP2SsaleRsdI9yZsk8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044338; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=OMK6G6BOZWlbHX4rg+TLkGN/x34xkpFqL8aYBw8SQ0w=; b=UsJvkHtOigsfl1UNoIog3XbxUwYVpJHm6DnQ/UFc0aps7Y7p4DXVrjok8JUchpmkTUSAL1a7ZvbdZO+MMpZNzY3HmJnBuKpUXlzMIqyxVsN3kk9tSO7fRD0m41F9pyWwz+OgJq85HNmsOHrx772rzNViKTFWbJHoOcc+o4zrvc8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044338126241.35140084876627; Tue, 28 Mar 2023 15:58:58 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGh-0006ta-N4; Tue, 28 Mar 2023 18:58:19 -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 1phIGf-0006sM-Hu for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:17 -0400 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGd-00073u-Qn for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:17 -0400 Received: by mail-pl1-x636.google.com with SMTP id c18so13146980ple.11 for ; Tue, 28 Mar 2023 15:58:15 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044294; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OMK6G6BOZWlbHX4rg+TLkGN/x34xkpFqL8aYBw8SQ0w=; b=qeTk7JCR8d+olCcPf1GPlJfjOn1bGE8ehMOh+7CH8inYsp0jRAkVQuP7f7DkN9LHgl dqROn3hwYM/gE0bsJNOiufo1TgiIFg4CAGGN1hhwwci3+D/iLddizCJlCEk4ql7nZwlK YirILZN52/bWwNORa2hwBGXauLJ2KtAI9/UmxKUSbpxVUje+aarq7UvChp73n1GZiOHf huD/ndx69ANAlYIwc9C5sv5B843WoHflmO3hivG20tc1nGfteWaX2j3x9lAEApngbx8L vSYj5qjobe2dbNYKk1ovdOWDQyoa49IL0XCiy7DmYlRJt4rf1Kn4Vt20IU+KIJKYcSV1 E9qg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044294; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OMK6G6BOZWlbHX4rg+TLkGN/x34xkpFqL8aYBw8SQ0w=; b=d+4vJp2jOLAM8uYI/xH8fwJN+t0x8diK8f3h+rBJX//nBC4ehhvycUJz996erZvJ4G iTCEGw/87Nh7D6qdjhwLweN5NxICjcRaQXgK7t+Ky26EPOJXVIZZ7HCDhlmgO/QX7yq1 Y8tqTBncV+HjF7Ym1Ntwqi5TzoibQxgMre1mnjH+J5HosjemYIn89ng9+kgsdIGBJLTe U+y7WvDrKWTVyVmG/ko5V/4pkPnIZJNFMaYP1ozyIO0ZVe6Ev/cJ02V2xQ0fT2Hnfc/A d/TJawACrX24Stq/vO187KpVf/fLhQ0gW1YCGuDxp1fEUIi93BUZbn966m2WW2bQNph9 UlBQ== X-Gm-Message-State: AAQBX9fLH/rcELjs7txWIcAbrzu6gpXAW1rJLxQPSiA88XOP15iKm0Kk uUZS3+sBIonIG73D+2tnfDxxWjuwdaVLMkzRpl0= X-Google-Smtp-Source: AKy350ZPEnKobKs+qgJZdN0LXPbTmiBZCp1g8z9kYJza+8I+mUs66Yifk+pC8yM+vsvLb0ppgsSUOw== X-Received: by 2002:a17:903:3293:b0:1a0:428b:d8c5 with SMTP id jh19-20020a170903329300b001a0428bd8c5mr14687232plb.45.1680044294399; Tue, 28 Mar 2023 15:58:14 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 07/15] accel/tcg: Pass last not end to page_collection_lock Date: Tue, 28 Mar 2023 15:57:58 -0700 Message-Id: <20230328225806.2278728-8-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x636.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044340536100006 Pass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved. Fixes a bug in the loop comparision where "<=3D end" would lock one more page than required. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Richard Henderson --- accel/tcg/tb-maint.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/accel/tcg/tb-maint.c b/accel/tcg/tb-maint.c index 04d2751bb6..57da2feb2f 100644 --- a/accel/tcg/tb-maint.c +++ b/accel/tcg/tb-maint.c @@ -511,20 +511,20 @@ static gint tb_page_addr_cmp(gconstpointer ap, gconst= pointer bp, gpointer udata) } =20 /* - * Lock a range of pages ([@start,@end[) as well as the pages of all + * Lock a range of pages ([@start,@last]) as well as the pages of all * intersecting TBs. * Locking order: acquire locks in ascending order of page index. */ static struct page_collection *page_collection_lock(tb_page_addr_t start, - tb_page_addr_t end) + tb_page_addr_t last) { struct page_collection *set =3D g_malloc(sizeof(*set)); tb_page_addr_t index; PageDesc *pd; =20 start >>=3D TARGET_PAGE_BITS; - end >>=3D TARGET_PAGE_BITS; - g_assert(start <=3D end); + last >>=3D TARGET_PAGE_BITS; + g_assert(start <=3D last); =20 set->tree =3D q_tree_new_full(tb_page_addr_cmp, NULL, NULL, page_entry_destroy); @@ -534,7 +534,7 @@ static struct page_collection *page_collection_lock(tb_= page_addr_t start, retry: q_tree_foreach(set->tree, page_entry_lock, NULL); =20 - for (index =3D start; index <=3D end; index++) { + for (index =3D start; index <=3D last; index++) { TranslationBlock *tb; PageForEachNext n; =20 @@ -1154,7 +1154,7 @@ tb_invalidate_phys_page_range__locked(struct page_col= lection *pages, void tb_invalidate_phys_page(tb_page_addr_t addr) { struct page_collection *pages; - tb_page_addr_t start, end; + tb_page_addr_t start, last; PageDesc *p; =20 p =3D page_find(addr >> TARGET_PAGE_BITS); @@ -1163,9 +1163,9 @@ void tb_invalidate_phys_page(tb_page_addr_t addr) } =20 start =3D addr & TARGET_PAGE_MASK; - end =3D start + TARGET_PAGE_SIZE; - pages =3D page_collection_lock(start, end); - tb_invalidate_phys_page_range__locked(pages, p, start, end, 0); + last =3D addr | ~TARGET_PAGE_MASK; + pages =3D page_collection_lock(start, last); + tb_invalidate_phys_page_range__locked(pages, p, start, last + 1, 0); page_collection_unlock(pages); } =20 @@ -1181,7 +1181,7 @@ void tb_invalidate_phys_range(tb_page_addr_t start, t= b_page_addr_t end) struct page_collection *pages; tb_page_addr_t next; =20 - pages =3D page_collection_lock(start, end); + pages =3D page_collection_lock(start, end - 1); for (next =3D (start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE; start < end; start =3D next, next +=3D TARGET_PAGE_SIZE) { @@ -1226,7 +1226,7 @@ void tb_invalidate_phys_range_fast(ram_addr_t ram_add= r, { struct page_collection *pages; =20 - pages =3D page_collection_lock(ram_addr, ram_addr + size); + pages =3D page_collection_lock(ram_addr, ram_addr + size - 1); tb_invalidate_phys_page_fast__locked(pages, ram_addr, size, retaddr); page_collection_unlock(pages); } --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044420; cv=none; d=zohomail.com; s=zohoarc; b=Ya94toVmlt70ax1DaQE7TDE5hRj1WZcAK8YGKVH/VrW04HdUHuTIzURPKA43WsDDFpUuraWQ20AKl7XhmzLak+IY7gmTIORkGCCRssSny8qyy9p4Jkmn7Q4PjJHxnlz60lcjkd+WAVqZ5YZ5g7f9CAk7K+d1MnwOf9JHpmq6J4w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044420; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=7lQNHN36cq4JX4MpVuNitP2N8g4BnbyjtKLtJ8yHuJE=; b=TgGrV5+y+pRV16LUKaGyLpMlyFdMYV5hpQKIitnktVCfu9+xfCR3aI4YICOugvqmrFJZZoY5Sz2XcDjYP27B+AJUMEjccPR5/6QQZW2DnS9UIi3BcOjZWEEXYSNA8UuYe68adWnR5zATh9Rz6vKUIa3Gr7ihydCyW756VCaCAv4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044420907125.22833240384182; Tue, 28 Mar 2023 16:00:20 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGl-0006uN-4e; Tue, 28 Mar 2023 18:58:23 -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 1phIGg-0006t5-DW for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:18 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGe-00074I-LG for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:18 -0400 Received: by mail-pj1-x102e.google.com with SMTP id lr16-20020a17090b4b9000b0023f187954acso14260331pjb.2 for ; Tue, 28 Mar 2023 15:58:16 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044295; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=7lQNHN36cq4JX4MpVuNitP2N8g4BnbyjtKLtJ8yHuJE=; b=EDVmEBixoqKCsNzfT33au9L9b1ZaunRZbLgP7crJMv2AJXNSF5qhOAFpzLFlk8f1Cr VQLKDptRMChSq3GD0s/aMxe2RZU5VYvHchHb39y0VGnfUhXPAz2FzYycmk4lGpXjt3PV 52q7bk0VyaeHoHlqeitUrVJIgTanM5MpR4a9+WFTcpFgl4SUzGVRJ7uDBp6mF/KcWAQb hncaAmoYH8gAqquLNAfpgo8qJBGyjQ5ZK+E7cYb6coREe6/T7z2uZN8wOYDYAzFO0eud Jtm34Qz0BYdMWXWOH+E7DRAnlAqnVbr3tay5Q8GwsMf3CqyhaRhmaCH1HIVf0uJeX3rt pNWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044295; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7lQNHN36cq4JX4MpVuNitP2N8g4BnbyjtKLtJ8yHuJE=; b=jOTCK9g76g93yZP5woLFVl//U6T0cZA/W/SYAW6nzqA3e8cnNJFyUe9467TucLykrq jKoU+aCEn8rgB4QrBeJoQ2twe6xxfQhP60XofaarGhKTgIWVArQ5t7MLXSten55cX+LN PnIycdIpu+X58fa+LDeCnNg02xRfiuAOzoScspq8Z3xO9LNsyyn0wKgTJWq5/qa4qZ9i btTvKYh2qiAt943a2YhPxClVJIeLUmSwwkw0qBHnRnWe19SEBBK1pPiMJAuLB/4EnQGn HOTePvx3YHUSy1XqIyLuoflNMrVTGMPg7bmDergRAOcQ6/CIsATzhSDxfxdFDnVfwS+Q FFdw== X-Gm-Message-State: AAQBX9ful5nFXQJ1jRgQj1OCtPYbGxzO8DF2GAi8Sk1djQPQBnVJ5AMI GpiBHWqyN9QzNqzVc9SlfN/B1Ndtqt6NbEdjVy0= X-Google-Smtp-Source: AKy350ZvoKt6k39YF61gz2/DUQHQvxpuuNzKW915vMLgU4t5KUwvUbF3s4DxrzBtSRdC/X9L/1GtbA== X-Received: by 2002:a17:902:f547:b0:1a1:7899:f009 with SMTP id h7-20020a170902f54700b001a17899f009mr244699plf.17.1680044295371; Tue, 28 Mar 2023 15:58:15 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 08/15] accel/tcg: Pass last not end to tb_invalidate_phys_page_range__locked Date: Tue, 28 Mar 2023 15:57:59 -0700 Message-Id: <20230328225806.2278728-9-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102e.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044422720100003 Pass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved. Properly truncate tb_last to the end of the page; the comment about tb_end being past the end of the page being ok is not correct, considering overflow. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Richard Henderson --- accel/tcg/tb-maint.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/accel/tcg/tb-maint.c b/accel/tcg/tb-maint.c index 57da2feb2f..74823ba464 100644 --- a/accel/tcg/tb-maint.c +++ b/accel/tcg/tb-maint.c @@ -1084,35 +1084,33 @@ bool tb_invalidate_phys_page_unwind(tb_page_addr_t = addr, uintptr_t pc) static void tb_invalidate_phys_page_range__locked(struct page_collection *pages, PageDesc *p, tb_page_addr_t start, - tb_page_addr_t end, + tb_page_addr_t last, uintptr_t retaddr) { TranslationBlock *tb; - tb_page_addr_t tb_start, tb_end; PageForEachNext n; #ifdef TARGET_HAS_PRECISE_SMC bool current_tb_modified =3D false; TranslationBlock *current_tb =3D retaddr ? tcg_tb_lookup(retaddr) : NU= LL; #endif /* TARGET_HAS_PRECISE_SMC */ - tb_page_addr_t last G_GNUC_UNUSED =3D end - 1; =20 /* - * We remove all the TBs in the range [start, end[. + * We remove all the TBs in the range [start, last]. * XXX: see if in some cases it could be faster to invalidate all the = code */ PAGE_FOR_EACH_TB(start, last, p, tb, n) { + tb_page_addr_t tb_start, tb_last; + /* NOTE: this is subtle as a TB may span two physical pages */ + tb_start =3D tb_page_addr0(tb); + tb_last =3D tb_start + tb->size - 1; if (n =3D=3D 0) { - /* NOTE: tb_end may be after the end of the page, but - it is not a problem */ - tb_start =3D tb_page_addr0(tb); - tb_end =3D tb_start + tb->size; + tb_last =3D MIN(tb_last, tb_start | ~TARGET_PAGE_MASK); } else { tb_start =3D tb_page_addr1(tb); - tb_end =3D tb_start + ((tb_page_addr0(tb) + tb->size) - & ~TARGET_PAGE_MASK); + tb_last =3D tb_start + (tb_last & ~TARGET_PAGE_MASK); } - if (!(tb_end <=3D start || tb_start >=3D end)) { + if (!(tb_last < start || tb_start > last)) { #ifdef TARGET_HAS_PRECISE_SMC if (current_tb =3D=3D tb && (tb_cflags(current_tb) & CF_COUNT_MASK) !=3D 1) { @@ -1165,7 +1163,7 @@ void tb_invalidate_phys_page(tb_page_addr_t addr) start =3D addr & TARGET_PAGE_MASK; last =3D addr | ~TARGET_PAGE_MASK; pages =3D page_collection_lock(start, last); - tb_invalidate_phys_page_range__locked(pages, p, start, last + 1, 0); + tb_invalidate_phys_page_range__locked(pages, p, start, last, 0); page_collection_unlock(pages); } =20 @@ -1192,7 +1190,7 @@ void tb_invalidate_phys_range(tb_page_addr_t start, t= b_page_addr_t end) continue; } assert_page_locked(pd); - tb_invalidate_phys_page_range__locked(pages, pd, start, bound, 0); + tb_invalidate_phys_page_range__locked(pages, pd, start, bound - 1,= 0); } page_collection_unlock(pages); } @@ -1212,7 +1210,7 @@ static void tb_invalidate_phys_page_fast__locked(stru= ct page_collection *pages, } =20 assert_page_locked(p); - tb_invalidate_phys_page_range__locked(pages, p, start, start + len, ra= ); + tb_invalidate_phys_page_range__locked(pages, p, start, start + len - 1= , ra); } =20 /* --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044359; cv=none; d=zohomail.com; s=zohoarc; b=kd49aeMwPu6in8mMzyDYORCKl5hA4rQANwbdoCfxVX/HSLNGXdQiMR4sbOkONAdffP0xKeeCBwx0jlpwf7BovBWH99dzhWPTVeJiKzDwjNdRPPuwBIA9iy1a5uTfPWp+oW/AgK+E+FUMul9ndebZ3xh1H1kf6Vg7u7bGGGLRN5Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044359; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Kk3UquZ0rVIuilYEk4nV7wLaq1oBqNaW1wOekl52DrA=; b=APCrB1NVJxodw2hBFsYnwjD4fuoHiHDWihSu6aHZowW8kqWhxdygb+oPe9Jwt6y51ehnk2fdYPbU+VPsqZGwvSCrjMvRs69hgC4lCWPTyqnMn7l3sHHGzUwbjCvQ6cIG8pY8AG+71+pWtid16l7mws7t5s02f47tprkbiw6pg88= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 16800443596091013.0331246374346; Tue, 28 Mar 2023 15:59:19 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGm-0006vV-UO; Tue, 28 Mar 2023 18:58:24 -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 1phIGh-0006tk-UG for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:22 -0400 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGg-00074V-0B for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:19 -0400 Received: by mail-pl1-x630.google.com with SMTP id w4so13148624plg.9 for ; Tue, 28 Mar 2023 15:58:17 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044296; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Kk3UquZ0rVIuilYEk4nV7wLaq1oBqNaW1wOekl52DrA=; b=HvSDjnzNOS1ee0cuc0sr8/eD2+lmdHtDZqxUIPPEGaR5szLhae4r85Br0+duySzffF /JaabLscxDAYf7PiYFOM0Gv/G2+wtaavYTvzd9OSD+2cqhFAHf1s43DSuGE/bf5N4/iy jtI9+enDL0jK9epvPWNBFWduFVbZuHEbb8gFiWGjni7Duxu0aCeVi+EhIxFvYQ+Tbn5K cXYDmfb20jKU5tus2/sTTMhXgvbizmug+unvudXf6DVeUIy6YqVXhE6yDu8k9SIAdqfM 0g0NGyCIyxML6ZEwL5ZNa7D/EnwUfHmAs6t54sjWYTBr64CeGleE2MOXvuMzXM+nIPvs ClEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044296; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Kk3UquZ0rVIuilYEk4nV7wLaq1oBqNaW1wOekl52DrA=; b=zyQg2itTv5RKjLgDBsEbdftg8WnsfSAX0Mc3uA1N3EEjQUeAsoFbB5Lqq5QT3rPUdB DkhlsT8vkeQv2dqW184te2N0p4zfUBxp3pxoHGz9OCOVvNSa7Tk8MY0fuLgPQ2P5WQ+H m+PRKh7JPfAHFhTTiUfi1aSkKlyVWxk+5QZu4Ty3za/PEpkp6K6bvc+35iwJH6DTvCIP jG8hhvVTOYHA71xG6xxgvehXm2qng23UthavG2kKObJ9ap1RdtWOGuHBXkDWCu6R54XC CAMn4NEAqAKIbHBTdqUC5cntASBSjALItxPcufsIsJnMTPBJd76IPb+QLTkpu813BLCd cqGg== X-Gm-Message-State: AAQBX9dB/jMxI5lSfS9dAtDC/E2+pi7eQhtxwdwpB2kn0btDEcDp8WT3 cgSW1T12sQPW2GMM9hsffRmvHPENncXmhSIVvWI= X-Google-Smtp-Source: AKy350ZkD6GehuJ0n066XjGad7n0qnkkxiZB4DntCvCT+dDnsKvtS8J9Wu6RXpr5x+TFTo1T8C/ucw== X-Received: by 2002:a17:90b:1a81:b0:23f:ec0f:aaae with SMTP id ng1-20020a17090b1a8100b0023fec0faaaemr18478074pjb.33.1680044296421; Tue, 28 Mar 2023 15:58:16 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 09/15] accel/tcg: Pass last not end to tb_invalidate_phys_range Date: Tue, 28 Mar 2023 15:58:00 -0700 Message-Id: <20230328225806.2278728-10-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x630.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044360333100009 Pass the address of the last byte to be changed, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Richard Henderson --- include/exec/exec-all.h | 2 +- accel/tcg/tb-maint.c | 31 ++++++++++++++++--------------- accel/tcg/translate-all.c | 2 +- accel/tcg/user-exec.c | 2 +- softmmu/physmem.c | 2 +- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/include/exec/exec-all.h b/include/exec/exec-all.h index ad9eb6067b..ecded1f112 100644 --- a/include/exec/exec-all.h +++ b/include/exec/exec-all.h @@ -678,7 +678,7 @@ void tb_invalidate_phys_addr(target_ulong addr); void tb_invalidate_phys_addr(AddressSpace *as, hwaddr addr, MemTxAttrs att= rs); #endif void tb_phys_invalidate(TranslationBlock *tb, tb_page_addr_t page_addr); -void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end); +void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last); void tb_set_jmp_target(TranslationBlock *tb, int n, uintptr_t addr); =20 /* GETPC is the true target of the return instruction that we'll execute. = */ diff --git a/accel/tcg/tb-maint.c b/accel/tcg/tb-maint.c index 74823ba464..cb1f806f00 100644 --- a/accel/tcg/tb-maint.c +++ b/accel/tcg/tb-maint.c @@ -991,11 +991,10 @@ TranslationBlock *tb_link_page(TranslationBlock *tb, = tb_page_addr_t phys_pc, * Called with mmap_lock held for user-mode emulation. * NOTE: this function must not be called while a TB is running. */ -void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end) +void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last) { TranslationBlock *tb; PageForEachNext n; - tb_page_addr_t last =3D end - 1; =20 assert_memory_lock(); =20 @@ -1011,11 +1010,11 @@ void tb_invalidate_phys_range(tb_page_addr_t start,= tb_page_addr_t end) */ void tb_invalidate_phys_page(tb_page_addr_t addr) { - tb_page_addr_t start, end; + tb_page_addr_t start, last; =20 start =3D addr & TARGET_PAGE_MASK; - end =3D start + TARGET_PAGE_SIZE; - tb_invalidate_phys_range(start, end); + last =3D addr | ~TARGET_PAGE_MASK; + tb_invalidate_phys_range(start, last); } =20 /* @@ -1169,28 +1168,30 @@ void tb_invalidate_phys_page(tb_page_addr_t addr) =20 /* * Invalidate all TBs which intersect with the target physical address ran= ge - * [start;end[. NOTE: start and end may refer to *different* physical page= s. + * [start;last]. NOTE: start and end may refer to *different* physical pag= es. * 'is_cpu_write_access' should be true if called from a real cpu write * access: the virtual CPU will exit the current TB if code is modified in= side * this TB. */ -void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end) +void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t last) { struct page_collection *pages; - tb_page_addr_t next; + tb_page_addr_t index, index_last; =20 - pages =3D page_collection_lock(start, end - 1); - for (next =3D (start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE; - start < end; - start =3D next, next +=3D TARGET_PAGE_SIZE) { - PageDesc *pd =3D page_find(start >> TARGET_PAGE_BITS); - tb_page_addr_t bound =3D MIN(next, end); + pages =3D page_collection_lock(start, last); + + index_last =3D last >> TARGET_PAGE_BITS; + for (index =3D start >> TARGET_PAGE_BITS; index <=3D index_last; index= ++) { + PageDesc *pd =3D page_find(index); + tb_page_addr_t bound; =20 if (pd =3D=3D NULL) { continue; } assert_page_locked(pd); - tb_invalidate_phys_page_range__locked(pages, pd, start, bound - 1,= 0); + bound =3D (index << TARGET_PAGE_BITS) | ~TARGET_PAGE_MASK; + bound =3D MIN(bound, last); + tb_invalidate_phys_page_range__locked(pages, pd, start, bound, 0); } page_collection_unlock(pages); } diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 74deb18bd0..5b13281119 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -572,7 +572,7 @@ void tb_check_watchpoint(CPUState *cpu, uintptr_t retad= dr) cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags); addr =3D get_page_addr_code(env, pc); if (addr !=3D -1) { - tb_invalidate_phys_range(addr, addr + 1); + tb_invalidate_phys_range(addr, addr); } } } diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c index 20b6fc2f6e..a7e0c3e2f4 100644 --- a/accel/tcg/user-exec.c +++ b/accel/tcg/user-exec.c @@ -516,7 +516,7 @@ void page_set_flags(target_ulong start, target_ulong la= st, int flags) ~(reset ? 0 : PAGE_STICKY)); } if (inval_tb) { - tb_invalidate_phys_range(start, last + 1); + tb_invalidate_phys_range(start, last); } } =20 diff --git a/softmmu/physmem.c b/softmmu/physmem.c index e35061bba4..0e0182d9f2 100644 --- a/softmmu/physmem.c +++ b/softmmu/physmem.c @@ -2527,7 +2527,7 @@ static void invalidate_and_set_dirty(MemoryRegion *mr= , hwaddr addr, } if (dirty_log_mask & (1 << DIRTY_MEMORY_CODE)) { assert(tcg_enabled()); - tb_invalidate_phys_range(addr, addr + length); + tb_invalidate_phys_range(addr, addr + length - 1); dirty_log_mask &=3D ~(1 << DIRTY_MEMORY_CODE); } cpu_physical_memory_set_dirty_range(addr, length, dirty_log_mask); --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044442; cv=none; d=zohomail.com; s=zohoarc; b=D99RhO2Htoq+QmUnIBHZWYCulOf8mZVrnLb6kHp1STQyKaE5MQBhwvUkUdI+hauk2NNuS7j/dqamODgaek6zCVcZQcddDZasFKbnWrWEI/5qzw2Yy8zTBI2eez3a6BCttITzgFfnSw+BqbOasDL2wI5JUf2T+/LbNtMmClZHEC8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044442; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=DetiRRyvf4ED57xT0bgZ3nZDKqz/yTSD4tOJO8ye8V0=; b=ECyh0Jr7NNxmO820kpffgtCK7frFNjhm1bNCY27Dy4Jfm1baYLac64LSnda5WAnhK+WDD7GNR4vVyIlswJlt+gSCzaxRYn6s9fmHMWItOms5BsH/rquQdToiK2fw71AcGdJ/wuzdg4fLqv3/RfNso6wJwJJFHVpHcV7jGr10A+k= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044442239860.2790783366054; Tue, 28 Mar 2023 16:00:42 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGl-0006ur-Q3; Tue, 28 Mar 2023 18:58:23 -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 1phIGh-0006tl-Rw for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:19 -0400 Received: from mail-pl1-x632.google.com ([2607:f8b0:4864:20::632]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGg-00072w-0H for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:19 -0400 Received: by mail-pl1-x632.google.com with SMTP id ix20so13181127plb.3 for ; Tue, 28 Mar 2023 15:58:17 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044297; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=DetiRRyvf4ED57xT0bgZ3nZDKqz/yTSD4tOJO8ye8V0=; b=PomLFcPiMY4DFisKRqdgTbIgez2ay5Xa6uXCkHw0O9H1rr1NFcTU/fAzVpIEkNC8xJ HjgCxKnfhJkHIgJiIAXUVqXx4b5no1GihqjawhLKwZfphV0CVB6tQvbMNVThdoc9sKWT vbZf0Y+NUu64HmByuUqspLz+mu6thlChd5zCnvPX7KjBhg+0YBr6vrWUU0mjnBvUMeWG dxk0zTH5fyQfUsCCUerfY/RchpTjLgqj18TALdYS/1NXvUsRXh2Rvp5eJh0/UZDRMALK ny/TYQSQ8ESAXhr1P27NOSfk3Yac3Ys8d0xwEIIrI6fuaFl5Ds5I9xg/g2fxf+0RBM1f q4Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044297; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=DetiRRyvf4ED57xT0bgZ3nZDKqz/yTSD4tOJO8ye8V0=; b=4i+t32O/qUoxddT2temCphz5Tb4nxTcfqHc1IygHJk1DFC6SoUY9HdYW/kMBw4lMXx MSA3zjXtO4YhGnmEeE3IM7E0gj8QR/6AhLfeRayWsTf9j9t7e168pURLPafe65iZmTaW RG/bhTwYxHjwb7m71QpcnSPXSKlC+r/IcjDjsPBNKrHYu2oyidvcV7WAWfiSKVtEkUJW YVX4Dy/RZ15xuIw412BYQ9O0qirNT9C6814sNl3faER+t5acDDLyOSZ0rJlDw9oaAOOM s9FI8h3kgixTJ52h2YdRgR9gERq/7Y2lt/A2SNvNiXvfdHTEQV+JX9NoDl3cv2JiMx/7 QCXg== X-Gm-Message-State: AAQBX9fGcfOVlFELRqlqt9OrjUnM2uhfvt+bAqX2Zk+27huLfiyz+mAx OIHo4yu73lrvEdOmaLlW7b455spNitokaXTYuMQ= X-Google-Smtp-Source: AKy350ZrrnZTLcmDFurQVDFViYcu2raz+IPqGGW1myX6DhEoDjQINKcVPIGmu+t/sww1paljXPgi5g== X-Received: by 2002:a17:903:110e:b0:19e:9807:de48 with SMTP id n14-20020a170903110e00b0019e9807de48mr21984394plh.23.1680044297223; Tue, 28 Mar 2023 15:58:17 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 10/15] linux-user: Pass last not end to probe_guest_base Date: Tue, 28 Mar 2023 15:58:01 -0700 Message-Id: <20230328225806.2278728-11-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::632; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x632.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044442893100005 Pass the address of the last byte of the image, rather than the first address past the last byte. This avoids overflow when the last page of the address space is involved. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Richard Henderson --- linux-user/user-internals.h | 12 ++++++------ linux-user/elfload.c | 24 ++++++++++++------------ linux-user/flatload.c | 2 +- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/linux-user/user-internals.h b/linux-user/user-internals.h index 9333db4f51..c63ef45fc7 100644 --- a/linux-user/user-internals.h +++ b/linux-user/user-internals.h @@ -76,19 +76,19 @@ void fork_end(int child); /** * probe_guest_base: * @image_name: the executable being loaded - * @loaddr: the lowest fixed address in the executable - * @hiaddr: the highest fixed address in the executable + * @loaddr: the lowest fixed address within the executable + * @hiaddr: the highest fixed address within the executable * * Creates the initial guest address space in the host memory space. * - * If @loaddr =3D=3D 0, then no address in the executable is fixed, - * i.e. it is fully relocatable. In that case @hiaddr is the size - * of the executable. + * If @loaddr =3D=3D 0, then no address in the executable is fixed, i.e. + * it is fully relocatable. In that case @hiaddr is the size of the + * executable minus one. * * This function will not return if a valid value for guest_base * cannot be chosen. On return, the executable loader can expect * - * target_mmap(loaddr, hiaddr - loaddr, ...) + * target_mmap(loaddr, hiaddr - loaddr + 1, ...) * * to succeed. */ diff --git a/linux-user/elfload.c b/linux-user/elfload.c index fa4cc41567..dfae967908 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -2504,7 +2504,7 @@ static void pgb_have_guest_base(const char *image_nam= e, abi_ulong guest_loaddr, if (guest_hiaddr > reserved_va) { error_report("%s: requires more than reserved virtual " "address space (0x%" PRIx64 " > 0x%lx)", - image_name, (uint64_t)guest_hiaddr, reserved_va); + image_name, (uint64_t)guest_hiaddr + 1, reserved_= va); exit(EXIT_FAILURE); } } else { @@ -2512,7 +2512,7 @@ static void pgb_have_guest_base(const char *image_nam= e, abi_ulong guest_loaddr, if ((guest_hiaddr - guest_base) > ~(uintptr_t)0) { error_report("%s: requires more virtual address space " "than the host can provide (0x%" PRIx64 ")", - image_name, (uint64_t)guest_hiaddr - guest_base); + image_name, (uint64_t)guest_hiaddr + 1 - guest_ba= se); exit(EXIT_FAILURE); } #endif @@ -2525,18 +2525,18 @@ static void pgb_have_guest_base(const char *image_n= ame, abi_ulong guest_loaddr, if (reserved_va) { guest_loaddr =3D (guest_base >=3D mmap_min_addr ? 0 : mmap_min_addr - guest_base); - guest_hiaddr =3D reserved_va; + guest_hiaddr =3D reserved_va - 1; } =20 /* Reserve the address space for the binary, or reserved_va. */ test =3D g2h_untagged(guest_loaddr); - addr =3D mmap(test, guest_hiaddr - guest_loaddr, PROT_NONE, flags, -1,= 0); + addr =3D mmap(test, guest_hiaddr - guest_loaddr + 1, PROT_NONE, flags,= -1, 0); if (test !=3D addr) { pgb_fail_in_use(image_name); } qemu_log_mask(CPU_LOG_PAGE, - "%s: base @ %p for " TARGET_ABI_FMT_ld " bytes\n", - __func__, addr, guest_hiaddr - guest_loaddr); + "%s: base @ %p for %" PRIu64 " bytes\n", + __func__, addr, (uint64_t)guest_hiaddr - guest_loaddr + = 1); } =20 /** @@ -2680,7 +2680,7 @@ static void pgb_static(const char *image_name, abi_ul= ong orig_loaddr, if (hiaddr !=3D orig_hiaddr) { error_report("%s: requires virtual address space that the " "host cannot provide (0x%" PRIx64 ")", - image_name, (uint64_t)orig_hiaddr); + image_name, (uint64_t)orig_hiaddr + 1); exit(EXIT_FAILURE); } =20 @@ -2694,7 +2694,7 @@ static void pgb_static(const char *image_name, abi_ul= ong orig_loaddr, * arithmetic wraps around. */ if (sizeof(uintptr_t) =3D=3D 8 || loaddr >=3D 0x80000000u) { - hiaddr =3D (uintptr_t) 4 << 30; + hiaddr =3D UINT32_MAX; } else { offset =3D -(HI_COMMPAGE & -align); } @@ -2702,7 +2702,7 @@ static void pgb_static(const char *image_name, abi_ul= ong orig_loaddr, loaddr =3D MIN(loaddr, LO_COMMPAGE & -align); } =20 - addr =3D pgb_find_hole(loaddr, hiaddr - loaddr, align, offset); + addr =3D pgb_find_hole(loaddr, hiaddr - loaddr + 1, align, offset); if (addr =3D=3D -1) { /* * If HI_COMMPAGE, there *might* be a non-consecutive allocation @@ -2755,7 +2755,7 @@ static void pgb_reserved_va(const char *image_name, a= bi_ulong guest_loaddr, if (guest_hiaddr > reserved_va) { error_report("%s: requires more than reserved virtual " "address space (0x%" PRIx64 " > 0x%lx)", - image_name, (uint64_t)guest_hiaddr, reserved_va); + image_name, (uint64_t)guest_hiaddr + 1, reserved_va); exit(EXIT_FAILURE); } =20 @@ -3021,7 +3021,7 @@ static void load_elf_image(const char *image_name, in= t image_fd, if (a < loaddr) { loaddr =3D a; } - a =3D eppnt->p_vaddr + eppnt->p_memsz; + a =3D eppnt->p_vaddr + eppnt->p_memsz - 1; if (a > hiaddr) { hiaddr =3D a; } @@ -3112,7 +3112,7 @@ static void load_elf_image(const char *image_name, in= t image_fd, * In both cases, we will overwrite pages in this range with mappings * from the executable. */ - load_addr =3D target_mmap(loaddr, hiaddr - loaddr, PROT_NONE, + load_addr =3D target_mmap(loaddr, (size_t)hiaddr - loaddr + 1, PROT_NO= NE, MAP_PRIVATE | MAP_ANON | MAP_NORESERVE | (ehdr->e_type =3D=3D ET_EXEC ? MAP_FIXED : 0), -1, 0); diff --git a/linux-user/flatload.c b/linux-user/flatload.c index e99570ca18..5efec2630e 100644 --- a/linux-user/flatload.c +++ b/linux-user/flatload.c @@ -448,7 +448,7 @@ static int load_flat_file(struct linux_binprm * bprm, * Allocate the address space. */ probe_guest_base(bprm->filename, 0, - text_len + data_len + extra + indx_len); + text_len + data_len + extra + indx_len - 1); =20 /* * there are a couple of cases here, the separate code/data --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044374; cv=none; d=zohomail.com; s=zohoarc; b=OZGOHjRERV486DKXFFlQvHGURXo0SjyYfkhVnwnw5MfFHud8CypE7qc0S8GaW6TKECulkiCTolQ9KQyA960dqRH9T3uzxLtcFFz1JsouVOzcbPyXNhXlRqViY+L7IQkqVFcWIM3n4KGeP1MGgmAQkcYxlIuys5ExMfk7LPq+Akw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044374; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=HGR1FH4Yfs2yWXOpijdZet2xfEmVpJkhB4HWyQD2rq4=; b=FMNHuPJJN/tiBvUK71+1HgAp6ccQmHLZ7RUuvCIJKzaLAghwBcvGI9rs8aIHkXep99r0XMe6E0WfkP2qcE10Cbo4NX3o9yn2xV6w5LQfSiHNthz0eh6ik1qjHUsC03rY7YMR/xGjrVkbKgR3nvJeiipRtWoH9jvmSem6TUKSvgk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044374857833.9858406847605; Tue, 28 Mar 2023 15:59:34 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGn-0006vp-M2; Tue, 28 Mar 2023 18:58:25 -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 1phIGj-0006tw-PJ for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:22 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGh-00074v-NO for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:21 -0400 Received: by mail-pj1-x102e.google.com with SMTP id p13-20020a17090a284d00b0023d2e945aebso2713343pjf.0 for ; Tue, 28 Mar 2023 15:58:19 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044298; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=HGR1FH4Yfs2yWXOpijdZet2xfEmVpJkhB4HWyQD2rq4=; b=pwBvPTSqq7fdAAc0alzxm8HHZXl4skclJn/S76HToqgQU8zIdeBHEARoX737lMrtir XDWRi+rm9zW2qCdVFoiNIlAJQuoR+aSvG/AualkxlMIQWpTe07RrGlPhI/mBLkHT7WDF Si2Omub72dnYqB7HQg/HfVhTOBQUJJhfMBjExtWzW2tqK3qtTUW23Koqb6pV/XF7PBiv wiGvhi6f1XS/wgdS6hAZWI9XcH3BEQtYjjhlQ93LGxnAQDm/lKcyt3mt56Ea6FzlLfHs g4u6ddl/29tQgkTiJhe8WrDVdNGmcrU4f3KrSFQRwOmPfGhWsPbzHm642T3cvAHUthCW 5t5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044298; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HGR1FH4Yfs2yWXOpijdZet2xfEmVpJkhB4HWyQD2rq4=; b=ye0evlnktUUbjAOmuZ7bHuLJohxqF1h2ZsCKvIKu+xNaAmnBtDt5hbKb9/8qg1v2GH BCHvImxDW3eSCx9YEaFnKrr/kBIF5yFnobZ15KNB6UXqcBeU3n634/JpGPm1q+M50xUZ ytUOzenW1j9gcwaOAw1fM5Pc7Dv48zrvMeJyP49MRgzRLS1eqxRzRkeCl5K8TLh9HyMc yAcNk9otr/Yv3Gn+pSVBsn7QKKmAe7Vg3M04xuz1xW7rrYLlRe9I+4uy0TWvX5lIPPCI 87P48Fh3DinmYpFgYMty2vFo2dZLZBIlGp8R5jAi821kF0E8GRtB+2x5ZjkckpQucNlg P88w== X-Gm-Message-State: AAQBX9fYtKbjswahshFWuJXbQXB2Ng+3L+1r2oW78K3If96lfoazoxa7 CqjX/Lkkp72YNXqYzWQKFo450vY7CQFSA7V62XE= X-Google-Smtp-Source: AKy350ZnQxWzvM33+o7BzE8UBjPXMyllt3Ci49CptZ70w0Zj/qZPv7vUu+6x+xpE+0s1KDh/BC78OQ== X-Received: by 2002:a17:903:886:b0:1a1:8b54:ab58 with SMTP id kt6-20020a170903088600b001a18b54ab58mr13982883plb.19.1680044298299; Tue, 28 Mar 2023 15:58:18 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 11/15] include/exec: Change reserved_va semantics to last byte Date: Tue, 28 Mar 2023 15:58:02 -0700 Message-Id: <20230328225806.2278728-12-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::102e; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102e.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044376636100005 Change the semantics to be the last byte of the guest va, rather than the following byte. This avoids some overflow conditions. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Richard Henderson --- include/exec/cpu-all.h | 11 ++++++++++- linux-user/arm/target_cpu.h | 2 +- bsd-user/main.c | 10 +++------- bsd-user/mmap.c | 4 ++-- linux-user/elfload.c | 14 +++++++------- linux-user/main.c | 27 +++++++++++++-------------- linux-user/mmap.c | 4 ++-- 7 files changed, 38 insertions(+), 34 deletions(-) diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h index 64cb62dc54..090922e4a8 100644 --- a/include/exec/cpu-all.h +++ b/include/exec/cpu-all.h @@ -152,6 +152,15 @@ static inline void tswap64s(uint64_t *s) */ extern uintptr_t guest_base; extern bool have_guest_base; + +/* + * If non-zero, the guest virtual address space is a contiguous subset + * of the host virtual address space, i.e. '-R reserved_va' is in effect + * either from the command-line or by default. The value is the last + * byte of the guest address space e.g. UINT32_MAX. + * + * If zero, the host and guest virtual address spaces are intermingled. + */ extern unsigned long reserved_va; =20 /* @@ -171,7 +180,7 @@ extern unsigned long reserved_va; #define GUEST_ADDR_MAX_ \ ((MIN_CONST(TARGET_VIRT_ADDR_SPACE_BITS, TARGET_ABI_BITS) <=3D 32) ? \ UINT32_MAX : ~0ul) -#define GUEST_ADDR_MAX (reserved_va ? reserved_va - 1 : GUEST_ADDR_MAX_) +#define GUEST_ADDR_MAX (reserved_va ? : GUEST_ADDR_MAX_) =20 #else =20 diff --git a/linux-user/arm/target_cpu.h b/linux-user/arm/target_cpu.h index 89ba274cfc..f6383a7cd1 100644 --- a/linux-user/arm/target_cpu.h +++ b/linux-user/arm/target_cpu.h @@ -30,7 +30,7 @@ static inline unsigned long arm_max_reserved_va(CPUState = *cs) * the high addresses. Restrict linux-user to the * cached write-back RAM in the system map. */ - return 0x80000000ul; + return 0x7ffffffful; } else { /* * We need to be able to map the commpage. diff --git a/bsd-user/main.c b/bsd-user/main.c index 89f225dead..babc3b009b 100644 --- a/bsd-user/main.c +++ b/bsd-user/main.c @@ -68,13 +68,9 @@ bool have_guest_base; # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS # if TARGET_VIRT_ADDR_SPACE_BITS =3D=3D 32 && \ (TARGET_LONG_BITS =3D=3D 32 || defined(TARGET_ABI32)) -/* - * There are a number of places where we assign reserved_va to a variable - * of type abi_ulong and expect it to fit. Avoid the last page. - */ -# define MAX_RESERVED_VA (0xfffffffful & TARGET_PAGE_MASK) +# define MAX_RESERVED_VA 0xfffffffful # else -# define MAX_RESERVED_VA (1ul << TARGET_VIRT_ADDR_SPACE_BITS) +# define MAX_RESERVED_VA ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1) # endif # else # define MAX_RESERVED_VA 0 @@ -466,7 +462,7 @@ int main(int argc, char **argv) envlist_free(envlist); =20 if (reserved_va) { - mmap_next_start =3D reserved_va; + mmap_next_start =3D reserved_va + 1; } =20 { diff --git a/bsd-user/mmap.c b/bsd-user/mmap.c index 696057551a..565b9f97ed 100644 --- a/bsd-user/mmap.c +++ b/bsd-user/mmap.c @@ -234,7 +234,7 @@ static abi_ulong mmap_find_vma_reserved(abi_ulong start= , abi_ulong size, size =3D HOST_PAGE_ALIGN(size) + alignment; end_addr =3D start + size; if (end_addr > reserved_va) { - end_addr =3D reserved_va; + end_addr =3D reserved_va + 1; } addr =3D end_addr - qemu_host_page_size; =20 @@ -243,7 +243,7 @@ static abi_ulong mmap_find_vma_reserved(abi_ulong start= , abi_ulong size, if (looped) { return (abi_ulong)-1; } - end_addr =3D reserved_va; + end_addr =3D reserved_va + 1; addr =3D end_addr - qemu_host_page_size; looped =3D 1; continue; diff --git a/linux-user/elfload.c b/linux-user/elfload.c index dfae967908..f1370a7a8b 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -208,7 +208,7 @@ static bool init_guest_commpage(void) * has specified -R reserved_va, which would trigger an assert(). */ if (reserved_va !=3D 0 && - TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE >=3D reserved_va) { + TARGET_VSYSCALL_PAGE + TARGET_PAGE_SIZE - 1 > reserved_va) { error_report("Cannot allocate vsyscall page"); exit(EXIT_FAILURE); } @@ -2504,7 +2504,7 @@ static void pgb_have_guest_base(const char *image_nam= e, abi_ulong guest_loaddr, if (guest_hiaddr > reserved_va) { error_report("%s: requires more than reserved virtual " "address space (0x%" PRIx64 " > 0x%lx)", - image_name, (uint64_t)guest_hiaddr + 1, reserved_= va); + image_name, (uint64_t)guest_hiaddr, reserved_va); exit(EXIT_FAILURE); } } else { @@ -2525,7 +2525,7 @@ static void pgb_have_guest_base(const char *image_nam= e, abi_ulong guest_loaddr, if (reserved_va) { guest_loaddr =3D (guest_base >=3D mmap_min_addr ? 0 : mmap_min_addr - guest_base); - guest_hiaddr =3D reserved_va - 1; + guest_hiaddr =3D reserved_va; } =20 /* Reserve the address space for the binary, or reserved_va. */ @@ -2755,7 +2755,7 @@ static void pgb_reserved_va(const char *image_name, a= bi_ulong guest_loaddr, if (guest_hiaddr > reserved_va) { error_report("%s: requires more than reserved virtual " "address space (0x%" PRIx64 " > 0x%lx)", - image_name, (uint64_t)guest_hiaddr + 1, reserved_va); + image_name, (uint64_t)guest_hiaddr, reserved_va); exit(EXIT_FAILURE); } =20 @@ -2768,17 +2768,17 @@ static void pgb_reserved_va(const char *image_name,= abi_ulong guest_loaddr, /* Reserve the memory on the host. */ assert(guest_base !=3D 0); test =3D g2h_untagged(0); - addr =3D mmap(test, reserved_va, PROT_NONE, flags, -1, 0); + addr =3D mmap(test, reserved_va + 1, PROT_NONE, flags, -1, 0); if (addr =3D=3D MAP_FAILED || addr !=3D test) { error_report("Unable to reserve 0x%lx bytes of virtual address " "space at %p (%s) for use as guest address space (che= ck your " "virtual memory ulimit setting, min_mmap_addr or rese= rve less " - "using -R option)", reserved_va, test, strerror(errno= )); + "using -R option)", reserved_va + 1, test, strerror(e= rrno)); exit(EXIT_FAILURE); } =20 qemu_log_mask(CPU_LOG_PAGE, "%s: base @ %p for %lu bytes\n", - __func__, addr, reserved_va); + __func__, addr, reserved_va + 1); } =20 void probe_guest_base(const char *image_name, abi_ulong guest_loaddr, diff --git a/linux-user/main.c b/linux-user/main.c index 39d9bd4d7a..fe03293516 100644 --- a/linux-user/main.c +++ b/linux-user/main.c @@ -109,11 +109,9 @@ static const char *last_log_filename; # if HOST_LONG_BITS > TARGET_VIRT_ADDR_SPACE_BITS # if TARGET_VIRT_ADDR_SPACE_BITS =3D=3D 32 && \ (TARGET_LONG_BITS =3D=3D 32 || defined(TARGET_ABI32)) -/* There are a number of places where we assign reserved_va to a variable - of type abi_ulong and expect it to fit. Avoid the last page. */ -# define MAX_RESERVED_VA(CPU) (0xfffffffful & TARGET_PAGE_MASK) +# define MAX_RESERVED_VA(CPU) 0xfffffffful # else -# define MAX_RESERVED_VA(CPU) (1ul << TARGET_VIRT_ADDR_SPACE_BITS) +# define MAX_RESERVED_VA(CPU) ((1ul << TARGET_VIRT_ADDR_SPACE_BITS) - 1) # endif # else # define MAX_RESERVED_VA(CPU) 0 @@ -379,7 +377,9 @@ static void handle_arg_reserved_va(const char *arg) { char *p; int shift =3D 0; - reserved_va =3D strtoul(arg, &p, 0); + unsigned long val; + + val =3D strtoul(arg, &p, 0); switch (*p) { case 'k': case 'K': @@ -393,10 +393,10 @@ static void handle_arg_reserved_va(const char *arg) break; } if (shift) { - unsigned long unshifted =3D reserved_va; + unsigned long unshifted =3D val; p++; - reserved_va <<=3D shift; - if (reserved_va >> shift !=3D unshifted) { + val <<=3D shift; + if (val >> shift !=3D unshifted) { fprintf(stderr, "Reserved virtual address too big\n"); exit(EXIT_FAILURE); } @@ -405,6 +405,8 @@ static void handle_arg_reserved_va(const char *arg) fprintf(stderr, "Unrecognised -R size suffix '%s'\n", p); exit(EXIT_FAILURE); } + /* The representation is size - 1, with 0 remaining "default". */ + reserved_va =3D val ? val - 1 : 0; } =20 static void handle_arg_singlestep(const char *arg) @@ -793,7 +795,7 @@ int main(int argc, char **argv, char **envp) */ max_reserved_va =3D MAX_RESERVED_VA(cpu); if (reserved_va !=3D 0) { - if (reserved_va % qemu_host_page_size) { + if ((reserved_va + 1) % qemu_host_page_size) { char *s =3D size_to_str(qemu_host_page_size); fprintf(stderr, "Reserved virtual address not aligned mod %s\n= ", s); g_free(s); @@ -804,11 +806,8 @@ int main(int argc, char **argv, char **envp) exit(EXIT_FAILURE); } } else if (HOST_LONG_BITS =3D=3D 64 && TARGET_VIRT_ADDR_SPACE_BITS <= =3D 32) { - /* - * reserved_va must be aligned with the host page size - * as it is used with mmap() - */ - reserved_va =3D max_reserved_va & qemu_host_page_mask; + /* MAX_RESERVED_VA + 1 is a large power of 2, so is aligned. */ + reserved_va =3D max_reserved_va; } =20 { diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 995146f60d..0aa8ae7356 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -283,7 +283,7 @@ static abi_ulong mmap_find_vma_reserved(abi_ulong start= , abi_ulong size, end_addr =3D start + size; if (start > reserved_va - size) { /* Start at the top of the address space. */ - end_addr =3D ((reserved_va - size) & -align) + size; + end_addr =3D ((reserved_va + 1 - size) & -align) + size; looped =3D true; } =20 @@ -297,7 +297,7 @@ static abi_ulong mmap_find_vma_reserved(abi_ulong start= , abi_ulong size, return (abi_ulong)-1; } /* Re-start at the top of the address space. */ - addr =3D end_addr =3D ((reserved_va - size) & -align) + size; + addr =3D end_addr =3D ((reserved_va + 1 - size) & -align) + si= ze; looped =3D true; } else { prot =3D page_get_flags(addr); --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044440; cv=none; d=zohomail.com; s=zohoarc; b=h9XfXsLkalnOokIz3TfQ+HKxi/SgIy13uTua++ZNYUS5HGkSVN8bKzUi8bv5OKGgQ9nXWL64oBB1PvhY2Xaf/YPMHBCIu6OoikL7FjwX80vxqVJ5qiqoDpaZ6g6bdpFUoNTJYaNYfi5W5JQUy/BY/VrZZRt51VA34SQl9He3YnM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044440; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=cVAvx2zSwe31o8U36gPBJxc74TN10+p8dpIidum+zXQ=; b=Q+VG61JZV8xCbt1iHknPtIwoD8RjIyebD+qfvD57ipGaBR0nCqo7RsHAMv71ngWNyWcaADTsSNMoeMqO61EMo1qA9NlRP/rEJ6GI0mJ5RbDoYCNkp/vJAW2xocK+q7MOvRHWcxVXDqbYuiVGYnE4OVK5tBcaX27ucCxofLhYlnM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044440505114.73114192164246; Tue, 28 Mar 2023 16:00:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGn-0006vn-Ch; Tue, 28 Mar 2023 18:58:25 -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 1phIGl-0006uM-2M for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:23 -0400 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGi-000755-CQ for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:21 -0400 Received: by mail-pl1-x62a.google.com with SMTP id ix20so13181197plb.3 for ; Tue, 28 Mar 2023 15:58:20 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044299; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cVAvx2zSwe31o8U36gPBJxc74TN10+p8dpIidum+zXQ=; b=MuULEQ/cPZSUthVsMswmpGgsuX74JMHQ5LIT7gPB5VtQHCTfrR9fUdIZqjbAzLLUgh 6y5P/urCpXxewPHEhPAVYgsnbZ/yPA57BPN3MY4OaJaF44cW87tNGUoXcG0ft74sMx5P 7RinFUfCbccpbQf335sDemINxeLpXz/sqCzFGruZOjvZHR/zsQ6/rfD1XZJQWT65/yXW yQ5Q4yX4bgCjECdWEf2LdY11Gsh2f81PjnIl+gcyiUBygXJ+M0jvgXyFvs65OnZXjeA5 p+gkxswavOZABQ/D31FByjVUOg6dRVF0pxTUL7Z2i/kd+WGYRB3Bvb0ne5qp8osuFqp6 jodw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044299; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cVAvx2zSwe31o8U36gPBJxc74TN10+p8dpIidum+zXQ=; b=5oh7U0bbeoGL2whz2/R15YRySMALaouv1wHoJcGthC7pNOUtf9uEjyTg79BIkPnBhF 5ieVKcfU+sSbhMhHto9Uqls2zQhmw5e5u9ygQxC3lHZoNhCzSVtx0AWG6pRHIFWv6tgm ZzPLToXQIGjLEPJ1w75ZbLgHHz6ddl45mXKBTc10MnoUX5xhmdOuOrzUdmPMnWZk7HFe 3kigZga6GJWkFQLljC+wCAAWL9CrE8xPQzBnK1A6ZegzN7WcN+9edNEoAAg+ZTxjwTBL xzrJ8KjE3CJBEx8upYCI41g0IPVhQxbo3jzkPTANIwp6V33soFIFeIg8cm8dA9cIJwJK M0PQ== X-Gm-Message-State: AAQBX9fUIfZeihFV9BPjReFTU10RdvmrKY3Ulzv3x8E8WNIBSqnh6r/a jZeG4ix/xkqJmNg7nvmoVphntAkp04r0D9mXuEE= X-Google-Smtp-Source: AKy350bAto/4tOHU3KQqFt22iveN8+4PT8LLuAVjF41b6e8+Kk8cdSVmmfB3Suk4mimdgf2FvEB9JQ== X-Received: by 2002:a17:90b:1b4a:b0:234:1d1d:6ae6 with SMTP id nv10-20020a17090b1b4a00b002341d1d6ae6mr16960318pjb.1.1680044299261; Tue, 28 Mar 2023 15:58:19 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 12/15] linux-user/arm: Take more care allocating commpage Date: Tue, 28 Mar 2023 15:58:03 -0700 Message-Id: <20230328225806.2278728-13-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::62a; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62a.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044440887100001 User setting of -R reserved_va can lead to an assertion failure in page_set_flags. Sanity check the value of reserved_va and print an error message instead. Do not allocate a commpage at all for m-profile cpus. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Richard Henderson --- linux-user/elfload.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/linux-user/elfload.c b/linux-user/elfload.c index f1370a7a8b..b96b3e566b 100644 --- a/linux-user/elfload.c +++ b/linux-user/elfload.c @@ -423,12 +423,32 @@ enum { =20 static bool init_guest_commpage(void) { - abi_ptr commpage =3D HI_COMMPAGE & -qemu_host_page_size; - void *want =3D g2h_untagged(commpage); - void *addr =3D mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE, - MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); + ARMCPU *cpu =3D ARM_CPU(thread_cpu); + abi_ptr want =3D HI_COMMPAGE & TARGET_PAGE_MASK; + abi_ptr addr; =20 - if (addr =3D=3D MAP_FAILED) { + /* + * M-profile allocates maximum of 2GB address space, so can never + * allocate the commpage. Skip it. + */ + if (arm_feature(&cpu->env, ARM_FEATURE_M)) { + return true; + } + + /* + * If reserved_va does not cover the commpage, we get an assert + * in page_set_flags. Produce an intelligent error instead. + */ + if (reserved_va !=3D 0 && want + TARGET_PAGE_SIZE - 1 > reserved_va) { + error_report("Allocating guest commpage: -R 0x%" PRIx64 " too smal= l", + (uint64_t)reserved_va + 1); + exit(EXIT_FAILURE); + } + + addr =3D target_mmap(want, TARGET_PAGE_SIZE, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); + + if (addr =3D=3D -1) { perror("Allocating guest commpage"); exit(EXIT_FAILURE); } @@ -437,15 +457,12 @@ static bool init_guest_commpage(void) } =20 /* Set kernel helper versions; rest of page is 0. */ - __put_user(5, (uint32_t *)g2h_untagged(0xffff0ffcu)); + put_user_u32(5, 0xffff0ffcu); =20 - if (mprotect(addr, qemu_host_page_size, PROT_READ)) { + if (target_mprotect(addr, qemu_host_page_size, PROT_READ | PROT_EXEC))= { perror("Protecting guest commpage"); exit(EXIT_FAILURE); } - - page_set_flags(commpage, commpage | ~qemu_host_page_mask, - PAGE_READ | PAGE_EXEC | PAGE_VALID); return true; } =20 --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044476; cv=none; d=zohomail.com; s=zohoarc; b=JZnDE/6a7bXkf/GuXzTA9pe77vehEd/ut4bZ2iqDZwOcLAzJV3hKlv1yxuu4Y/tGH+jlEhFOS9T5yYpNw33q4LJblN1Y/ykvxIMQhKHrii3IPbTlbQvLe1O4DloFWL2YLhJRJd8SlMGAFrvikqnEjEkJZKYvunB2CTelq9LktWY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044476; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kdNM442/mFduh1g9/nonfQEYYKdwqbZiYT+039FpfZE=; b=d3XSjwYfurtfxumdeGt0Zy6D/zixsKeFSCRqi7y5zopPGBHdutyUpH/dADQxYogPUhzbaRCxjO3EQh+eXhbS/Yqx5cF7mipItLH3I+kV+DzC8Py780H0CeavnfmCg0bpxD77VA8CrhZBJ6xzw90oJtcfLHRA8bvj37tvs0ynMZI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044476942435.28250448966287; Tue, 28 Mar 2023 16:01:16 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGn-0006vo-LF; Tue, 28 Mar 2023 18:58:25 -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 1phIGl-0006uf-GN for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:23 -0400 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGj-00075P-J8 for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:23 -0400 Received: by mail-pj1-x1033.google.com with SMTP id f6-20020a17090ac28600b0023b9bf9eb63so14243993pjt.5 for ; Tue, 28 Mar 2023 15:58:21 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044300; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kdNM442/mFduh1g9/nonfQEYYKdwqbZiYT+039FpfZE=; b=h/7+et9rY4Xzj9XSdbjw2B0hXRBR1JlBBinSXQl33QqVgYPgobV8A/J7nOfut7P8V1 H53EZ4/xRJVG8eadmqQ2dVXGkfRyW3HwGYd/rAt+Fg65s2FUYq0SCUexgXIGgRVfoanD 5C2nhlNzqdoJVTMmXWHIrZ9x7XlohmDcfuz8YcXtNwMLCXiKp6f974n1rk9CZJBiRGVb wCwZwMIvLmemtF0m900EjmqXjL3R/yDBbLEAO5y4aMz7NGZEd+mPM6cV5qkosnFSKmI+ EwyLomHh8ke8rejI1+5tZE6mXv8Fcr/Tb96AIlZTFKEz9YK6ASvZ3uQdJTfDHd5A2e5d J47w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044300; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kdNM442/mFduh1g9/nonfQEYYKdwqbZiYT+039FpfZE=; b=EaGYD3M9edIyk+59m3ItgYuB+Yr53h7aiSkS+ttDOPd5nfID21ry5jA7dagT0/yVUP Z++UwDwD78Irb1mbt4WOEiC9j4GfrcbWiyLXwzpLB78qJxC+FGCCyLCl3pVT/4CIhsBJ LwkVoeAHhn0xxeK/gl4WTa3UkmzILt+abTijXZPW4Gq2EtY8aDUL5g8pZGwgGc5cgknY P4xxxYL1l9BnU6rfR/p51D1XbKV7xCu9rbomNZO629yP0KIvibiCZFAHH9mLa8ZGw3J3 VnRacwmkf9bnI1EZAVGcmsK8dif4n+/fmmtmV1NaKXwsJ673I8gaVoGDJs6qOY+jOll8 5NgA== X-Gm-Message-State: AAQBX9fC1Ji47S/aV+2PIGUh+bPpw2rNeVfd/f57ZIaUJekft9D5EUyy nO0mGL48iSXpa6bzA4uaY8ab4kLu9WkfJMY5Qxg= X-Google-Smtp-Source: AKy350Z+clzWEdkaO/UVl+GmD+2C3JizfFj7zbTNo9lYPT77w9/PI1+R8hDykrOeOlrPwx/eMMDPWQ== X-Received: by 2002:a17:902:f1d3:b0:19c:dd49:9bf8 with SMTP id e19-20020a170902f1d300b0019cdd499bf8mr122791plc.28.1680044300092; Tue, 28 Mar 2023 15:58:20 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 13/15] softmmu: Restrict cpu_check_watchpoint / address_matches to TCG accel Date: Tue, 28 Mar 2023 15:58:04 -0700 Message-Id: <20230328225806.2278728-14-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1033; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1033.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044479074100003 From: Philippe Mathieu-Daud=C3=A9 Both cpu_check_watchpoint() and cpu_watchpoint_address_matches() are specific to TCG system emulation. Declare them in "tcg-cpu-ops.h" to be sure accessing them from non-TCG code is a compilation error. Signed-off-by: Philippe Mathieu-Daud=C3=A9 Message-Id: <20230328173117.15226-2-philmd@linaro.org> Signed-off-by: Richard Henderson --- include/hw/core/cpu.h | 37 ------------------------------ include/hw/core/tcg-cpu-ops.h | 43 +++++++++++++++++++++++++++++++++++ target/arm/tcg/mte_helper.c | 1 + target/arm/tcg/sve_helper.c | 1 + target/s390x/tcg/mem_helper.c | 1 + 5 files changed, 46 insertions(+), 37 deletions(-) diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index 821e937020..ce312745d5 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -970,17 +970,6 @@ static inline void cpu_watchpoint_remove_by_ref(CPUSta= te *cpu, static inline void cpu_watchpoint_remove_all(CPUState *cpu, int mask) { } - -static inline void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr l= en, - MemTxAttrs atr, int fl, uintptr_t = ra) -{ -} - -static inline int cpu_watchpoint_address_matches(CPUState *cpu, - vaddr addr, vaddr len) -{ - return 0; -} #else int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr len, int flags, CPUWatchpoint **watchpoint); @@ -988,32 +977,6 @@ int cpu_watchpoint_remove(CPUState *cpu, vaddr addr, vaddr len, int flags); void cpu_watchpoint_remove_by_ref(CPUState *cpu, CPUWatchpoint *watchpoint= ); void cpu_watchpoint_remove_all(CPUState *cpu, int mask); - -/** - * cpu_check_watchpoint: - * @cpu: cpu context - * @addr: guest virtual address - * @len: access length - * @attrs: memory access attributes - * @flags: watchpoint access type - * @ra: unwind return address - * - * Check for a watchpoint hit in [addr, addr+len) of the type - * specified by @flags. Exit via exception with a hit. - */ -void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len, - MemTxAttrs attrs, int flags, uintptr_t ra); - -/** - * cpu_watchpoint_address_matches: - * @cpu: cpu context - * @addr: guest virtual address - * @len: access length - * - * Return the watchpoint flags that apply to [addr, addr+len). - * If no watchpoint is registered for the range, the result is 0. - */ -int cpu_watchpoint_address_matches(CPUState *cpu, vaddr addr, vaddr len); #endif =20 /** diff --git a/include/hw/core/tcg-cpu-ops.h b/include/hw/core/tcg-cpu-ops.h index 20e3c0ffbb..0ae08df47e 100644 --- a/include/hw/core/tcg-cpu-ops.h +++ b/include/hw/core/tcg-cpu-ops.h @@ -175,4 +175,47 @@ struct TCGCPUOps { =20 }; =20 +#if defined(CONFIG_USER_ONLY) + +static inline void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr l= en, + MemTxAttrs atr, int fl, uintptr_t = ra) +{ +} + +static inline int cpu_watchpoint_address_matches(CPUState *cpu, + vaddr addr, vaddr len) +{ + return 0; +} + +#else + +/** + * cpu_check_watchpoint: + * @cpu: cpu context + * @addr: guest virtual address + * @len: access length + * @attrs: memory access attributes + * @flags: watchpoint access type + * @ra: unwind return address + * + * Check for a watchpoint hit in [addr, addr+len) of the type + * specified by @flags. Exit via exception with a hit. + */ +void cpu_check_watchpoint(CPUState *cpu, vaddr addr, vaddr len, + MemTxAttrs attrs, int flags, uintptr_t ra); + +/** + * cpu_watchpoint_address_matches: + * @cpu: cpu context + * @addr: guest virtual address + * @len: access length + * + * Return the watchpoint flags that apply to [addr, addr+len). + * If no watchpoint is registered for the range, the result is 0. + */ +int cpu_watchpoint_address_matches(CPUState *cpu, vaddr addr, vaddr len); + +#endif + #endif /* TCG_CPU_OPS_H */ diff --git a/target/arm/tcg/mte_helper.c b/target/arm/tcg/mte_helper.c index fee3c7eb96..a4f3f92bc0 100644 --- a/target/arm/tcg/mte_helper.c +++ b/target/arm/tcg/mte_helper.c @@ -25,6 +25,7 @@ #include "exec/ram_addr.h" #include "exec/cpu_ldst.h" #include "exec/helper-proto.h" +#include "hw/core/tcg-cpu-ops.h" #include "qapi/error.h" #include "qemu/guest-random.h" =20 diff --git a/target/arm/tcg/sve_helper.c b/target/arm/tcg/sve_helper.c index 9a8951afa4..ccf5e5beca 100644 --- a/target/arm/tcg/sve_helper.c +++ b/target/arm/tcg/sve_helper.c @@ -27,6 +27,7 @@ #include "tcg/tcg.h" #include "vec_internal.h" #include "sve_ldst_internal.h" +#include "hw/core/tcg-cpu-ops.h" =20 =20 /* Return a value for NZCV as per the ARM PredTest pseudofunction. diff --git a/target/s390x/tcg/mem_helper.c b/target/s390x/tcg/mem_helper.c index b93dbd3dad..8b58b8d88d 100644 --- a/target/s390x/tcg/mem_helper.c +++ b/target/s390x/tcg/mem_helper.c @@ -26,6 +26,7 @@ #include "exec/helper-proto.h" #include "exec/exec-all.h" #include "exec/cpu_ldst.h" +#include "hw/core/tcg-cpu-ops.h" #include "qemu/int128.h" #include "qemu/atomic128.h" #include "trace.h" --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044463; cv=none; d=zohomail.com; s=zohoarc; b=f+2dHE7Hkc0lfaLmnJYajvFZdwoXZqZGoiQBdmDomxiOW6EQGY6BtGghKfrApfZjeMi8ckuoG4IDDg0ogiMuRxJ7bq7ds6ljKxl8SBdO0omjhAjCmih1qLJtJUX+WzIcUbqJGcR5JMi48AVp0A3HXfTOI4pDdz+vxgZZtHx0GOg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044463; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=xB7EoPcPuhHBkTYLQAr4hW1zXQR4T+cKsdLP9y+p2a0=; b=Qd0Ah3p3TnW8eavZ2p6LYMG6Ka+b/BKPYRaRVIZf3WoWR/tn9EjxCGLF/lvtOWijAbBow/coSafhoEiut7H1uAO0dugb5ApajdAtn/MNIww29HWIVL+L/bRPc68pDQ0cwcyYn0wCqRMuAiSSNmqBzh3/y09IU60AW/0L5aG91OI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044463164666.2209183269966; Tue, 28 Mar 2023 16:01:03 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGo-0006vu-3l; Tue, 28 Mar 2023 18:58:26 -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 1phIGm-0006v3-AW for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:24 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGk-00075Z-RM for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:24 -0400 Received: by mail-pj1-x1030.google.com with SMTP id p3-20020a17090a74c300b0023f69bc7a68so14235620pjl.4 for ; Tue, 28 Mar 2023 15:58:21 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044301; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xB7EoPcPuhHBkTYLQAr4hW1zXQR4T+cKsdLP9y+p2a0=; b=oWDGFSszQisVfdhFMzNUMkDj2u1kDJ9tMfx0hN03Dacp9B2mMhCq+1uIWzfc3B1ZGi 3POdcmqXnexcB0hDnmnxeyPWAu2Pa0WRRAJJK0gK8VJ4g1KMOKdM9mNPryttHAsf5F5M AqePjI0dSECvkBGaWlO0KjmawN3ZYTQs4WxhIYGJZ0wXN8RpT4+sSL2aIVP3wMJrs8Lc JJ18TyWs+cgKzdafoUHozwuPVTWoKF80hZpUTt41mWOEYrjuaG2mF80+bsxatAVpLqpL D+/PUT8yhKEYwcK6T4xy7mLi1PdfiI64rn3uFPy/RgzvnoBxWDZZoMp5YoX8bZsGtr2W B2OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044301; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xB7EoPcPuhHBkTYLQAr4hW1zXQR4T+cKsdLP9y+p2a0=; b=8G9bdWYxWvQtZjm4sCp+R+IZbBrifgd9a0gZCadGQSuYRcWQnssSHdJfeSLY74VamX 1XjrC/4Q0tb3sLSyVgiJYZqgOa2hDdCCAQs8URxNfdQbDh6pHcppvM3FF3Qk+wSfT1oB 2DXKvq8FgW/wCOdoAqdUy/aVDBGjpvWdrfCcUYdH9oueyDZC+y12Y5aIK+4we6cl2Tnl 4oU11r/Pc+IfvlR+tSlC37nczBm6UxbuBI5YXpUV5Cd8aqm/mwWolus1Kos5EWiGi+No c+xIfmK9cckuLo4WZbpFDwkRhHAz95/gT+fH/61DX5V2eW5bNv24+x++H+cHkj6OpPX2 /6fA== X-Gm-Message-State: AAQBX9crLl+KXJVuBDp7J/Iw3iZkO3aZCN/QampO+5XPA2tig3Dy9Zqd 9mP+t0VcM1MvRY2P9AgcjwUwMo+reA8/LL+fJOo= X-Google-Smtp-Source: AKy350bwBs9Tuq4Ks3ex1z2+yV/RZ5JH61bu0v/gmDV3xTgjHH0/qA6ImnJlEgze1o0/rtZjVQvuUg== X-Received: by 2002:a17:902:ecc4:b0:19a:9880:175f with SMTP id a4-20020a170902ecc400b0019a9880175fmr18666014plh.51.1680044301003; Tue, 28 Mar 2023 15:58:21 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 14/15] softmmu/watchpoint: Add missing 'qemu/error-report.h' include Date: Tue, 28 Mar 2023 15:58:05 -0700 Message-Id: <20230328225806.2278728-15-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1030.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044464978100003 From: Philippe Mathieu-Daud=C3=A9 cpu_watchpoint_insert() calls error_report() which is declared in "qemu/error-report.h". When moving this code in commit 2609ec2868 ("softmmu: Extract watchpoint API from physmem.c") we neglected to include this header. This works so far because it is indirectly included by TCG headers -> "qemu/plugin.h" -> "qemu/error-report.h". Currently cpu_watchpoint_insert() is only built with the TCG accelerator. When building it with other ones (or without TCG) we get: softmmu/watchpoint.c:38:9: error: implicit declaration of function 'error= _report' is invalid in C99 [-Werror,-Wimplicit-function-declaration] error_report("tried to set invalid watchpoint at %" ^ Include "qemu/error-report.h" in order to fix this for non-TCG builds. Signed-off-by: Philippe Mathieu-Daud=C3=A9 Message-Id: <20230328173117.15226-3-philmd@linaro.org> Signed-off-by: Richard Henderson --- softmmu/watchpoint.c | 1 + 1 file changed, 1 insertion(+) diff --git a/softmmu/watchpoint.c b/softmmu/watchpoint.c index ad58736787..9d6ae68499 100644 --- a/softmmu/watchpoint.c +++ b/softmmu/watchpoint.c @@ -19,6 +19,7 @@ =20 #include "qemu/osdep.h" #include "qemu/main-loop.h" +#include "qemu/error-report.h" #include "exec/exec-all.h" #include "exec/translate-all.h" #include "sysemu/tcg.h" --=20 2.34.1 From nobody Tue May 14 07:10:33 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1680044458; cv=none; d=zohomail.com; s=zohoarc; b=DL1vlk6gBHSMQwvv/b/phPlWklmqNtoiB0q1IbcJHvvCDBemkIwMSCroeTdoUq6jN3n39ycya2AH4HjKyVXC5FT0k5Q1RWVFayBExc2veLBxaHVQYx3hZu+QAvE5/QFSXzJc/ZLDDDGZJbLVIf1x5MXpB9FuGw7F/hJd2u1SOHM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1680044458; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=aAjWx8wHRPQYJiH1acoUNfkkeaHJ4tKlefw+Iq5YcRg=; b=D3WEbS+gMUtmhff61n7xudtzu7YPBgCaTzIcBc7PBIeKelLgv/kQs877TvIwkbvskwLVVE05IFs+pNSdHQekQ39dW38lpZ4iaiMiePzXQGtoHR5yHxbBOhHH97ccHJjRFSqhO+lTETsZUfP+Y+vApSTvjAhY697avf61in8sUio= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1680044458820513.2701399032603; Tue, 28 Mar 2023 16:00:58 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1phIGp-0006wB-9X; Tue, 28 Mar 2023 18:58:27 -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 1phIGm-0006ve-UZ for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:24 -0400 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1phIGk-00073J-Rz for qemu-devel@nongnu.org; Tue, 28 Mar 2023 18:58:24 -0400 Received: by mail-pj1-x1030.google.com with SMTP id gp15-20020a17090adf0f00b0023d1bbd9f9eso16757482pjb.0 for ; Tue, 28 Mar 2023 15:58:22 -0700 (PDT) Received: from stoup.. ([2602:ae:1541:f901:c23d:b6:6be1:f147]) by smtp.gmail.com with ESMTPSA id jw1-20020a170903278100b0019cd1ee1523sm21670633plb.30.2023.03.28.15.58.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 15:58:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1680044302; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aAjWx8wHRPQYJiH1acoUNfkkeaHJ4tKlefw+Iq5YcRg=; b=h43nSX/T0rQTACB/bnUecG/niMEOLuCZKgmoEwSEDvskUqRXQHlvHIMuaXDqsZBKoF gs0Dw6EHSeHWxg78bAvX2wBGpKQ3TF78rOhz0jz5+JgyuVlLW/nPrR6ArEGILlf1EUUJ yLM7+dTAYA8X3UrPdag1FCvTAy+GovCLHXPnq9mYzw0jqgBH6ZjAjGRW7gFgEq28OBhG G1f7ScpAfYHHCywJYL6Pl0Fg4vB+7O1XNycCsQu462AsTphDcpC6w566WMq9DzKA1i// /b4yE/3V98df0j6GXqulJliYTL1nPghFQriQYn0BcKy7Iz8koJBOSlnX8YPs7fw0OaSM ZsKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680044302; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aAjWx8wHRPQYJiH1acoUNfkkeaHJ4tKlefw+Iq5YcRg=; b=s89Tce0yfL0yZ/0MVs2d4yocpNT3R/gdgI4FvoqgmUvpfhayl6UhpMCIxMx+m5x1N/ wxFoKjgpCsF0hfR2wksnjRDw+8qVctF1ghmckhEWO4ntG0qAmqrXXkijLMSRPwqgFYT/ KPJZ1IE8z+4Wb/klXg2bba/tENdHw19gy1M5sri0vr7oJbLWSCSNur6Qdc2lQb7QSeYc JqbMYSEBHzpMTGEZAvm6llJLW5wsioCWsezJRcJLCBmSgcsgrWOdlTB00OsZzHw3fNfr DqathakPS3v3y/0oMPT9MPoe3Dn5G4vuSrbsx+/78L8Nyhnto3JL81E0aQoKrciGBOJG 2WTw== X-Gm-Message-State: AAQBX9cBSGA0pUdThaA4jdD83C8xVDR2ZHQYQpFiGEM7lpllmiO6EZ/s LwgqkVqSbbCTfgrz4yHWoOHO/ihtUsUNWyY7JdI= X-Google-Smtp-Source: AKy350YvVVvqctoytRFLc60meK51eMeJHhHb+fuQTkTNZo7ES9ti6XDkahcRhWdIIVhJUaRA2V5UWA== X-Received: by 2002:a17:90b:1b0c:b0:23f:5273:1ae1 with SMTP id nu12-20020a17090b1b0c00b0023f52731ae1mr18036577pjb.14.1680044301930; Tue, 28 Mar 2023 15:58:21 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: peter.maydell@linaro.org, =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 15/15] softmmu: Restore use of CPU watchpoint for all accelerators Date: Tue, 28 Mar 2023 15:58:06 -0700 Message-Id: <20230328225806.2278728-16-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230328225806.2278728-1-richard.henderson@linaro.org> References: <20230328225806.2278728-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::1030; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x1030.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-devel@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-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1680044461093100003 From: Philippe Mathieu-Daud=C3=A9 CPU watchpoints can be use by non-TCG accelerators. KVM uses them: $ git grep CPUWatchpoint|fgrep kvm target/arm/kvm64.c:1558: CPUWatchpoint *wp =3D find_hw_watchpoint(= cs, debug_exit->far); target/i386/kvm/kvm.c:5216:static CPUWatchpoint hw_watchpoint; target/ppc/kvm.c:443:static CPUWatchpoint hw_watchpoint; target/s390x/kvm/kvm.c:139:static CPUWatchpoint hw_watchpoint; See for example commit e4482ab7e3 ("target-arm: kvm - add support for HW assisted debug"): This adds basic support for HW assisted debug. The ioctl interface to KVM allows us to pass an implementation defined number of break and watch point registers. [...] This partially reverts commit 2609ec2868e6c286e755a73b4504714a0296a. Fixes: 2609ec2868 ("softmmu: Extract watchpoint API from physmem.c") Signed-off-by: Philippe Mathieu-Daud=C3=A9 Message-Id: <20230328173117.15226-4-philmd@linaro.org> Signed-off-by: Richard Henderson --- include/hw/core/cpu.h | 2 +- softmmu/watchpoint.c | 4 ++++ softmmu/meson.build | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/include/hw/core/cpu.h b/include/hw/core/cpu.h index ce312745d5..397fd3ac68 100644 --- a/include/hw/core/cpu.h +++ b/include/hw/core/cpu.h @@ -949,7 +949,7 @@ static inline bool cpu_breakpoint_test(CPUState *cpu, v= addr pc, int mask) return false; } =20 -#if !defined(CONFIG_TCG) || defined(CONFIG_USER_ONLY) +#if defined(CONFIG_USER_ONLY) static inline int cpu_watchpoint_insert(CPUState *cpu, vaddr addr, vaddr l= en, int flags, CPUWatchpoint **watchpo= int) { diff --git a/softmmu/watchpoint.c b/softmmu/watchpoint.c index 9d6ae68499..5350163385 100644 --- a/softmmu/watchpoint.c +++ b/softmmu/watchpoint.c @@ -104,6 +104,8 @@ void cpu_watchpoint_remove_all(CPUState *cpu, int mask) } } =20 +#ifdef CONFIG_TCG + /* * Return true if this watchpoint address matches the specified * access (ie the address range covered by the watchpoint overlaps @@ -220,3 +222,5 @@ void cpu_check_watchpoint(CPUState *cpu, vaddr addr, va= ddr len, } } } + +#endif /* CONFIG_TCG */ diff --git a/softmmu/meson.build b/softmmu/meson.build index 0180577517..1a7c7ac089 100644 --- a/softmmu/meson.build +++ b/softmmu/meson.build @@ -5,11 +5,11 @@ specific_ss.add(when: 'CONFIG_SOFTMMU', if_true: [files( 'physmem.c', 'qtest.c', 'dirtylimit.c', + 'watchpoint.c', )]) =20 specific_ss.add(when: ['CONFIG_SOFTMMU', 'CONFIG_TCG'], if_true: [files( 'icount.c', - 'watchpoint.c', )]) =20 softmmu_ss.add(files( --=20 2.34.1