From patchwork Thu Oct 6 03:11:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 612858 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp1195402pvb; Wed, 5 Oct 2022 20:28:08 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4s7TAmJ8Ek6S7dSn8WVa1WKpdWLBGOGQQU+JCHSTujXnDpMLdSQAypianA4yZ6qMqR6Np3 X-Received: by 2002:a37:b2c5:0:b0:6df:f8d6:6ea0 with SMTP id b188-20020a37b2c5000000b006dff8d66ea0mr1938711qkf.386.1665026888318; Wed, 05 Oct 2022 20:28:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665026888; cv=none; d=google.com; s=arc-20160816; b=EXykpbCMUt0NJq/OZeo8Ht7jnJX1Ff96fNWFI+UwKD3qVCy8VdQg3bAPqAHfUoD9/J cn/KFZCJsKZziiKzFZDjwvwgRp60lp7Gwlnu+jQv/4KpJFt3AU0W+pCDzS1nLH1oQs7M 4O7ukTE04ON/LCisbtH/vlj//kSqjZCFMrv7A3aZoxiDAi5epUXraxkZgAHzn4BxzZOr /CNsPoLtcpMawhMR8NndV4fOadzCzRJhv27BT0k/GZhAHUH0dtDJZao+FVo3rigWam19 vQ8FnJRakIDYfdH0DWW9rU758gIdBco3AsqTSw3W27fF3JboyzMHyc/8bE95u1r8Tive 8TiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=fMNzVDjZno1MDGZKBogmjYP5Xb4oEHZ4nbrRSgPIHno=; b=iAGdc+WnnHllivUBkH6JyWkPDbzkQcfLkdqQ01jcJ358ub9z0ufYFKNY0EBdcaKh51 dgk+ZHIKaxKVojvgm3Srey9+MoPAP16e0pedo1IzizfQqXWAWNWMTjsmVuBI4IBW6h2J S8+MfvoTxoHMfXq77F1Hp/aJX72FjeDFzSz0JYzXFFMqwE6csEae3/ulwt5XGUrnZz7T exvssfX/410+aMAQ7oDvWTNAqjlwmLHXaeKK9sGAOQYYHsT6NrF/iF0x6+G6/XdCaOxD mcwi1yMtycqw0udeq4NSYOA/Zyxw8pJbnZTPNv8x1J/vAnee6pfUxjSVOivrR/K7hIUr 4fOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kMSfNngl; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id r20-20020ac87ef4000000b0039230c9c5fbsi1091132qtc.209.2022.10.05.20.28.08 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 05 Oct 2022 20:28:08 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=kMSfNngl; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:55570 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ogHYN-0006XK-Qn for patch@linaro.org; Wed, 05 Oct 2022 23:28:07 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:42022) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ogHIW-0006Bb-2B for qemu-devel@nongnu.org; Wed, 05 Oct 2022 23:11:44 -0400 Received: from mail-pg1-x529.google.com ([2607:f8b0:4864:20::529]:37441) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ogHIT-00070q-7e for qemu-devel@nongnu.org; Wed, 05 Oct 2022 23:11:43 -0400 Received: by mail-pg1-x529.google.com with SMTP id bh13so717596pgb.4 for ; Wed, 05 Oct 2022 20:11:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date; bh=fMNzVDjZno1MDGZKBogmjYP5Xb4oEHZ4nbrRSgPIHno=; b=kMSfNngl6T80Y7wxEjAeFMYD+mM3xMZc3HqpAxcCGe4D3LFDYdJtuGcONYsSxXSg33 sezHon3kU4r+wIXklSP8ABec5obpbVvsePk26b1S4CLMMlMci5tZ2bKFNr5xQa509j5f rN1xTyGA6dYIuIttXpsANPaNgbtmixDH+AzgjGGD/dDAzC/Usud3AAxkAThU6F/2w+6o kAuzqC1BPjK4m2bSGkyrehVmg1VA6j1/psZvpw9Qg6/FP7KdXexrWSdKSJLCwFZp8FV/ /hNA/TetP9cuIU8CImVkUCylJMZ0RHAyv+R64mxZ1Z6MAjq8OmML0TSwnhKrEoPR+syb TTbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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; bh=fMNzVDjZno1MDGZKBogmjYP5Xb4oEHZ4nbrRSgPIHno=; b=PNSpD1iPq4QB2V6RqXqmamifljttcUcWaVVQx2MnxAfye+ayppaWed9bVdj2iCr43z 2J5TX+5+FLb3Wnw8+QN2j1rSMoLwc5X0Ys2+7rCuYeC5Zq9O+eBYSeZeU4PYTZS+sNem x7d5+GBtmjy1E+9mpbqg/NHhEH3vueAYVixLfQOvZLzc5CFW1udkGHIZT/AuK27Y8KRZ Seg+XCUP06KjBkzlIXbwARwqWEIOIROfUZly4mLq3GBc0ud4qbNayqQoLcD5ox9ddcqj 05hAB8Wet06YsfIvi/Nom0PBmOQAxwKX+o77abnbgbbvwahEyikXN8QqfneGuGliIVbN 0Wnw== X-Gm-Message-State: ACrzQf1wWAiz/qiiVq5mYKvSSPYY6j8IGQYu0tJmuMJkWSS+onrMEZxO 5+39NhfqabjF6DueoeNz96qLgjdJB2ivHg== X-Received: by 2002:a05:6a00:a8c:b0:558:991a:6691 with SMTP id b12-20020a056a000a8c00b00558991a6691mr2584469pfl.53.1665025899866; Wed, 05 Oct 2022 20:11:39 -0700 (PDT) Received: from stoup.. ([2602:47:d49d:ec01:9ad0:4307:7d39:bb61]) by smtp.gmail.com with ESMTPSA id u128-20020a627986000000b0056281da3bcbsm58360pfc.149.2022.10.05.20.11.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 05 Oct 2022 20:11:39 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: alex.bennee@linaro.org, laurent@vivier.eu, pbonzini@redhat.com, imp@bsdimp.com, f4bug@amsat.org Subject: [PATCH 20/24] accel/tcg: Use interval tree for TARGET_PAGE_DATA_SIZE Date: Wed, 5 Oct 2022 20:11:09 -0700 Message-Id: <20221006031113.1139454-21-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221006031113.1139454-1-richard.henderson@linaro.org> References: <20221006031113.1139454-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::529; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x529.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+patch=linaro.org@nongnu.org Sender: "Qemu-devel" Continue weaning user-only away from PageDesc. Use an interval tree to record target data. Chunk the data, to minimize allocation overhead. Signed-off-by: Richard Henderson --- accel/tcg/internal.h | 1 - accel/tcg/user-exec.c | 110 ++++++++++++++++++++++++++++++++---------- 2 files changed, 85 insertions(+), 26 deletions(-) diff --git a/accel/tcg/internal.h b/accel/tcg/internal.h index 1bd5a02911..8731dc52e2 100644 --- a/accel/tcg/internal.h +++ b/accel/tcg/internal.h @@ -26,7 +26,6 @@ typedef struct PageDesc { #ifdef CONFIG_USER_ONLY unsigned long flags; - void *target_data; #else QemuSpin lock; /* list of TBs intersecting this ram page */ diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c index fb7d6ee9e9..bce3d5f335 100644 --- a/accel/tcg/user-exec.c +++ b/accel/tcg/user-exec.c @@ -210,47 +210,107 @@ tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr, return addr; } +#ifdef TARGET_PAGE_DATA_SIZE +/* + * Allocate chunks of target data together. For the only current user, + * if we allocate one hunk per page, we have overhead of 40/128 or 40%. + * Therefore, allocate memory for 64 pages at a time for overhead < 1%. + */ +#define TPD_PAGES 64 +#define TBD_MASK (TARGET_PAGE_MASK * TPD_PAGES) + +typedef struct TargetPageDataNode { + IntervalTreeNode itree; + char data[TPD_PAGES][TARGET_PAGE_DATA_SIZE] __attribute__((aligned)); +} TargetPageDataNode; + +static IntervalTreeRoot targetdata_root; + void page_reset_target_data(target_ulong start, target_ulong end) { -#ifdef TARGET_PAGE_DATA_SIZE - target_ulong addr, len; + IntervalTreeNode *n, *next; + target_ulong last; - /* - * 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(end - 1 <= GUEST_ADDR_MAX); - assert(start < end); assert_memory_lock(); start = start & TARGET_PAGE_MASK; - end = TARGET_PAGE_ALIGN(end); + last = TARGET_PAGE_ALIGN(end) - 1; - for (addr = start, len = end - start; - len != 0; - len -= TARGET_PAGE_SIZE, addr += TARGET_PAGE_SIZE) { - PageDesc *p = page_find_alloc(addr >> TARGET_PAGE_BITS, 1); + for (n = interval_tree_iter_first(&targetdata_root, start, last), + next = n ? interval_tree_iter_next(n, start, last) : NULL; + n != NULL; + n = next, + next = next ? interval_tree_iter_next(n, start, last) : NULL) { + target_ulong n_start, n_last, p_ofs, p_len; + TargetPageDataNode *t; - g_free(p->target_data); - p->target_data = NULL; + if (n->start >= start && n->last <= last) { + interval_tree_remove(n, &targetdata_root); + g_free(n); + continue; + } + + if (n->start < start) { + n_start = start; + p_ofs = (start - n->start) >> TARGET_PAGE_BITS; + } else { + n_start = n->start; + p_ofs = 0; + } + n_last = MIN(last, n->last); + p_len = (n_last + 1 - n_start) >> TARGET_PAGE_BITS; + + t = container_of(n, TargetPageDataNode, itree); + memset(t->data[p_ofs], 0, p_len * TARGET_PAGE_DATA_SIZE); } -#endif } -#ifdef TARGET_PAGE_DATA_SIZE void *page_get_target_data(target_ulong address) { - PageDesc *p = page_find(address >> TARGET_PAGE_BITS); - void *ret = p->target_data; + IntervalTreeNode *n; + TargetPageDataNode *t; + target_ulong page, region; + bool locked; - if (!ret) { - ret = g_malloc0(TARGET_PAGE_DATA_SIZE); - p->target_data = ret; + page = address & TARGET_PAGE_MASK; + region = address & TBD_MASK; + + n = interval_tree_iter_first(&targetdata_root, page, page); + if (n) { + goto found; } - return ret; + + /* + * See util/interval-tree.c re lockless lookups: no false positives but + * there are false negatives. If we find nothing, retry with the mmap + * lock acquired. We also need the lock for the allocation + insert. + */ + locked = have_mmap_lock(); + if (!locked) { + mmap_lock(); + n = interval_tree_iter_first(&targetdata_root, page, page); + if (n) { + mmap_unlock(); + goto found; + } + } + + t = g_new0(TargetPageDataNode, 1); + n = &t->itree; + n->start = region; + n->last = region | ~TBD_MASK; + interval_tree_insert(n, &targetdata_root); + if (!locked) { + mmap_unlock(); + } + + found: + t = container_of(n, TargetPageDataNode, itree); + return t->data[(page - region) >> TARGET_PAGE_BITS]; } -#endif +#else +void page_reset_target_data(target_ulong start, target_ulong end) { } +#endif /* TARGET_PAGE_DATA_SIZE */ /* The softmmu versions of these helpers are in cputlb.c. */