From patchwork Fri Jul 7 20:40:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 700059 Delivered-To: patch@linaro.org Received: by 2002:adf:fcc5:0:0:0:0:0 with SMTP id f5csp4066296wrs; Fri, 7 Jul 2023 13:44:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlEInYMHGgC4LlUQDkHmb/45KWt7/5bdtnUVMRDZ0n1Es3Bxzx/sxbfAkBFKYY8Bnvsd3TGu X-Received: by 2002:a05:622a:1a83:b0:402:6c1d:f249 with SMTP id s3-20020a05622a1a8300b004026c1df249mr7247587qtc.5.1688762699211; Fri, 07 Jul 2023 13:44:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688762699; cv=none; d=google.com; s=arc-20160816; b=LIIvqcwW8TfJJ8jtZgmKAxaswwGCkrHa4losVq6sV74cqmSo/oqT1NMrfNj9wsxMYq CQhhpsyfMhVrw0dfgetMkudfhlNUw2txGwhigAUe5kqOLQNPWnZKYAWqOyyXGgi7Co/L oEEameDAOajtadXAZ/E+z2bVBNmIMUWD/uv0STiA+/+3JOth1lBACpI6uDNpj61ao+xa +Y4k7OwnjSqeHSVAhkYe8zrHLVj3wgggsKo19SR9HBMVJph0kSfPpVsN56psLmzWS2DC tBOtVCjGHLSIv5daX+WB6JUn+MWwpmIB32HodFAKXUojdUjo48GPrY/b52+48cUmQdiS gZgw== 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=BvCmbIPrY85Qj0xJIhdk6WMERPf4zDCfB4ii0LMHB14=; fh=jVqwU2n6uDjg7kXqsBiuuwMdTrtXxwX52l5dkNb4FWE=; b=cezhwIJNq/FrYF/LFgYGSIKFNSfOFFcqNNSwEUa/CMixzQfHYGOGl/0gkhUZEen4i3 1DhGsX8ijS9Svl2gBDFVzZyTh5qVlfgP7ylcZujtjY+PdRdX8ujlom4PO5M3BZRPsLUl OxnUecD+UinHBwFjaSUDuYzJ1YGUAeXNuQvl5fIDRm+877dbxZhKvb/bEoFwYs1fArnb 3ZmswIbQNVHHWI1fUP6g/edNJPG7ktddBpvfVH2AUmMK7/tgD3f687C1HUsL3cQ5WkWM k/Gcn6Oi5r8QgcSx3BzG6FyRM3kqjvh9NNG2+4IpfcQaWoEwkfmChhX9FAfmv2AIWerQ ld7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KLl+5beI; 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 a21-20020a05622a02d500b004035a6b7724si3164024qtx.665.2023.07.07.13.44.59 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 07 Jul 2023 13:44:59 -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=KLl+5beI; 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] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qHsGa-0004zN-2Y; Fri, 07 Jul 2023 16:41: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 1qHsGM-0004o1-2t for qemu-devel@nongnu.org; Fri, 07 Jul 2023 16:41:10 -0400 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qHsGK-0004kr-3Y for qemu-devel@nongnu.org; Fri, 07 Jul 2023 16:41:09 -0400 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-3fb4146e8deso29580435e9.0 for ; Fri, 07 Jul 2023 13:41:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688762466; x=1691354466; 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=BvCmbIPrY85Qj0xJIhdk6WMERPf4zDCfB4ii0LMHB14=; b=KLl+5beIy5d/Or79p7wL2ZUINOf8is3PBYA1CgLVbX+AS5FlnzP9OO9QBnPUm0TLsM /atE6H0yS4rBywUY0vpiHkyzALSCCex2PCGiwFz5PEO3UhtTK8LhZLEC26aqLgBe4w73 ErLGZy7xZnlTVuuuFfutUk3RkZ2rXQkKjv3KpVQH0uFN7QVPdQwaQFFBrpvtLABQgmjk 77dIZbOezX6aopsoNfwKJzCpDMKURN956yXvj+wPS7jA9Bvx6jlVoCa1lVYHrT8PWPqp Aka35xs315wCd7/oRRWRQbc2D5W8m/XWPF9dVL7DDruFydeNwjkYZ/GIJNzwJ4gRpVAu mU3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688762466; x=1691354466; 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=BvCmbIPrY85Qj0xJIhdk6WMERPf4zDCfB4ii0LMHB14=; b=AITctq2Wq31+M/c/CvXM38/5HCBGUzaAN7nKGxFu8w9/A0VFOaIid+/gOsNFT7QWIw T3lI6a4q/fFpSCN8TGBYUYZIxIyIKNQ35BDRlE+LOLUnKLv0+8onF9QXLRRizLXWglDn a1BXHR/soasuJv213k1GXKWjwIF/ZhOr6GV4aGY2ymQBeu+KQnz9YkNIjUg48Ygvbujj AqW4k3x0FJ5rkFlJxEORG7cJT2SzwBmxXrPGSgDHuqmiczPOU2rfGKBq1yHMzRcfoO7E sORcjZj1PbHx4L/PzZG1hTyZ2Kr6OdSFCbOb8FPH8LVQ5o/I/at8dHIW5f4MXOz9XbUP q76Q== X-Gm-Message-State: ABy/qLbBS0W+R4Oi8CShL+tMTk3Yif/QeLHSFH9Pe/+4CkiSRianVSHw XliB32+4ueVhbCKORTTdqJKmBQ8IuawNaVgUAHvKqw== X-Received: by 2002:a1c:4c08:0:b0:3fa:935e:e185 with SMTP id z8-20020a1c4c08000000b003fa935ee185mr6638470wmf.22.1688762466658; Fri, 07 Jul 2023 13:41:06 -0700 (PDT) Received: from localhost.localdomain (host-92-23-208-66.as13285.net. [92.23.208.66]) by smtp.gmail.com with ESMTPSA id q10-20020a1ce90a000000b003fbb25da65bsm3337736wmc.30.2023.07.07.13.41.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 07 Jul 2023 13:41:06 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: laurent@vivier.eu, mjt@tls.msk.ru Subject: [PATCH v2 14/24] accel/tcg: Introduce page_find_range_empty Date: Fri, 7 Jul 2023 21:40:44 +0100 Message-Id: <20230707204054.8792-17-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230707204054.8792-1-richard.henderson@linaro.org> References: <20230707204054.8792-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::32b; envelope-from=richard.henderson@linaro.org; helo=mail-wm1-x32b.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, T_SCC_BODY_TEXT_LINE=-0.01 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-bounces+patch=linaro.org@nongnu.org Use the interval tree to locate an unused range in the VM. Signed-off-by: Richard Henderson --- include/exec/cpu-all.h | 15 +++++++++++++++ accel/tcg/user-exec.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/include/exec/cpu-all.h b/include/exec/cpu-all.h index 94f828b109..eb1c54701a 100644 --- a/include/exec/cpu-all.h +++ b/include/exec/cpu-all.h @@ -236,6 +236,21 @@ int page_check_range(target_ulong start, target_ulong len, int flags); */ bool page_check_range_empty(target_ulong start, target_ulong last); +/** + * page_find_range_empty + * @min: first byte of search range + * @max: last byte of search range + * @len: size of the hole required + * @align: alignment of the hole required (power of 2) + * + * If there is a range [x, x+@len) within [@min, @max] such that + * x % @align == 0, then return x. Otherwise return -1. + * The memory lock must be held, as the caller will want to ensure + * the returned range stays empty until a new mapping can be installed. + */ +target_ulong page_find_range_empty(target_ulong min, target_ulong max, + target_ulong len, target_ulong align); + /** * page_get_target_data(address) * @address: guest virtual address diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c index ab684a3ea2..e4f9563730 100644 --- a/accel/tcg/user-exec.c +++ b/accel/tcg/user-exec.c @@ -605,6 +605,47 @@ bool page_check_range_empty(target_ulong start, target_ulong last) return pageflags_find(start, last) == NULL; } +target_ulong page_find_range_empty(target_ulong min, target_ulong max, + target_ulong len, target_ulong align) +{ + target_ulong len_m1, align_m1; + + assert(min <= max); + assert(max <= GUEST_ADDR_MAX); + assert(len != 0); + assert(is_power_of_2(align)); + assert_memory_lock(); + + len_m1 = len - 1; + align_m1 = align - 1; + + /* Iteratively narrow the search region. */ + while (1) { + PageFlagsNode *p; + + /* Align min and double-check there's enough space remaining. */ + min = (min + align_m1) & ~align_m1; + if (min > max) { + return -1; + } + if (len_m1 > max - min) { + return -1; + } + + p = pageflags_find(min, min + len_m1); + if (p == NULL) { + /* Found! */ + return min; + } + if (max <= p->itree.last) { + /* Existing allocation fills the remainder of the search region. */ + return -1; + } + /* Skip across existing allocation. */ + min = p->itree.last + 1; + } +} + void page_protect(tb_page_addr_t address) { PageFlagsNode *p;