From patchwork Fri Jun 30 13:21:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 697937 Delivered-To: patch@linaro.org Received: by 2002:adf:fcc5:0:0:0:0:0 with SMTP id f5csp450157wrs; Fri, 30 Jun 2023 06:23:35 -0700 (PDT) X-Google-Smtp-Source: APBJJlFAxidXa3JEAXC86U+4f3kWKUYk8/Kn0gVuKO1sepLt3HGc18NgYsm2nTtRg2VgaOexPWv2 X-Received: by 2002:ad4:5caf:0:b0:5e6:1bf5:1ae0 with SMTP id q15-20020ad45caf000000b005e61bf51ae0mr3625193qvh.18.1688131414845; Fri, 30 Jun 2023 06:23:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688131414; cv=none; d=google.com; s=arc-20160816; b=AslkUYeLIiy2/QpMoBMxQyRno2+jRsLc+P2D96jp/O77MWnrTHA2zvfLGbP8RagGtD H2wdGR1fkTX/WSBq1D0C7pzG9lM2hSan0gAj5L75zPP3swN9K2NRbDrHLOOakJDy2Tp+ QC2DOswQyUJJ2qL4cqdoi2llK9LpiRUuTz7TV3lLJY0z07rr94Z0oAnoQabJ/qglUf87 D/eeD1q6juFeBFNfLWk8Arls+uOVxbNosu/2jotxlX+GXF7UzC585s3pxiUfzImKfPYv p7m8XM3MtSbNGubRHkiHJa3YmI1VaJRjatNOHT+xpVz5cz9kZraPZW+QZ9E62ofiWmCd pEIQ== 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=0rcUcYz/ENHU71nYFSKtvNzNb/+e+Z1fgOhw2rx254o=; fh=6bnXD+ip8BuPvzTWf97csbR5QVhpZvBV1AID0n9STS8=; b=PQki3qoku8GhDoeB5DX3+PGfRbLsQ9FKfy80bnxpYmq0Jnw1mYW5q0Jeu9j3bUbjrF tAmslaT5bbFjnHzOiywmMaxwKAHK6DsYqh2R4tN/ZJcLCEdsuo06cx+ZMteUriTUv9J4 sfLJCyGgeB5xMVzQ4IsBHC2r8LHbIzAlm6UFXQ2UjxkWqgjPs4/ZxEZEBIBlOq8qGCml zHkce5vx5vW0wnthB7xIXJ/wDHTQPvCF4Edm4UfEHyK93KqI+hxPn266ancPGyfY/TRc L3TjPTcVdk582va663tLTObH/s5Bu96KeTPhnUgLN065JKDCrQ0O/ilqg+U5eFdPZTOn SJ+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="M/V2YWHO"; 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 11-20020ad45bab000000b0060f7b695034si7805225qvq.426.2023.06.30.06.23.34 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Fri, 30 Jun 2023 06:23:34 -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="M/V2YWHO"; 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 1qFE4q-0007kt-0D; Fri, 30 Jun 2023 09:22:20 -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 1qFE4n-0007jG-Ua for qemu-devel@nongnu.org; Fri, 30 Jun 2023 09:22:17 -0400 Received: from mail-wr1-x42c.google.com ([2a00:1450:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qFE4m-0003S2-5X for qemu-devel@nongnu.org; Fri, 30 Jun 2023 09:22:17 -0400 Received: by mail-wr1-x42c.google.com with SMTP id ffacd0b85a97d-313f1085ac2so2045194f8f.1 for ; Fri, 30 Jun 2023 06:22:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1688131334; x=1690723334; 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=0rcUcYz/ENHU71nYFSKtvNzNb/+e+Z1fgOhw2rx254o=; b=M/V2YWHOcTzTeKzlm0waAJuXOJIJBUTVdf9Vu+Eszzes3ILpt/fKz6iQDUd4e+y4Zd Qy4Anq0XMDBsZQQwrq0GI+pq7awn/aC2iE/I7paf2ueOyVIK80bY2O6kzlY5aLJbXlcf dYvjwByWvVmJtxC7mv8+h/wrTs5vCjTCjCOwkZ5CneEZHCX9oP3RvdKuA58WtWZg+giV EYHG1g9u/6U9kcnZ6id4tbQPN5/lYMhRDVOvxFhNGtlIe2bMCJGh+YWikANduTTdcU0q FTBG7lPPaTPK2BXJu01bZ5fTwbidAHGDjfnauZuK+RiH5gbGG09QfcYCunQ5akX+PEFF 2a2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688131334; x=1690723334; 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=0rcUcYz/ENHU71nYFSKtvNzNb/+e+Z1fgOhw2rx254o=; b=kVUrkrriy7pOWtvq1nk8Z320Nv0qv5mEQKnioL70cDoHBww7aRppJ77BFgSdBI5iiV 8PXuq7xm7V8WNZN1CrMzOk/xshPIyJrZi/VxiYVjRW7oQyPcby+fZjkLaHJb1HapBAgQ 40YmreG8+ZjDR5Qi1E4oS3jwg+WRYb50Po8gaGyFmqh3hCwKkZrqGfpcUOfBmmjxqQpd nPwLldz8L060O0KtuKfrPVWIQHqMy63b6f92C9gEZwn2tA9RB0N8Kr/1gWa7A0WOLml0 wiul3NhzsXi/fZhe7Ra/JL9bqUDzvMzAHxf3qZ8Vet+3FJV/59NwV1qUEDLjIflvw3rL Vyag== X-Gm-Message-State: ABy/qLbtrpkNn8Dpduz0Jp600PXKcvNevk1Y6oXp50KTyq/nZVmMj7Kp LlMH3UTIP2eRGslRxqzVNuiy82r8h3oXHhtv+0LQIQ== X-Received: by 2002:a5d:58d8:0:b0:313:ee2e:dae1 with SMTP id o24-20020a5d58d8000000b00313ee2edae1mr2311431wrf.18.1688131334616; Fri, 30 Jun 2023 06:22:14 -0700 (PDT) Received: from localhost.localdomain ([139.47.41.96]) by smtp.gmail.com with ESMTPSA id a16-20020adfdd10000000b00313ef2150dcsm14571092wrm.45.2023.06.30.06.22.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 06:22:14 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: mjt@tls.msk.ru, laurent@vivier.eu Subject: [PATCH 18/24] linux-user: Rewrite mmap_reserve Date: Fri, 30 Jun 2023 15:21:53 +0200 Message-Id: <20230630132159.376995-19-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230630132159.376995-1-richard.henderson@linaro.org> References: <20230630132159.376995-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::42c; envelope-from=richard.henderson@linaro.org; helo=mail-wr1-x42c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, 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 'last' variables instead of 'end' variables; be careful about avoiding overflow. Assert that the mmap succeeded. Signed-off-by: Richard Henderson --- linux-user/mmap.c | 68 +++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/linux-user/mmap.c b/linux-user/mmap.c index 67a117823f..6b030dac42 100644 --- a/linux-user/mmap.c +++ b/linux-user/mmap.c @@ -722,47 +722,63 @@ fail: return -1; } -static void mmap_reserve(abi_ulong start, abi_ulong size) +static void mmap_reserve(abi_ulong start, abi_ulong len) { abi_ulong real_start; - abi_ulong real_end; - abi_ulong addr; - abi_ulong end; + abi_ulong real_last; + abi_ulong real_len; + abi_ulong last; + abi_ulong a; + void *host_start, *ptr; int prot; + last = start + len - 1; real_start = start & qemu_host_page_mask; - real_end = HOST_PAGE_ALIGN(start + size); - end = start + size; - if (start > real_start) { - /* handle host page containing start */ + real_last = HOST_PAGE_ALIGN(last) - 1; + + /* + * If guest pages remain on the first or last host pages, + * adjust the deallocation to retain those guest pages. + * The single page special case is required for the last page, + * lest real_start overflow to zero. + */ + if (real_last - real_start < qemu_host_page_size) { prot = 0; - for (addr = real_start; addr < start; addr += TARGET_PAGE_SIZE) { - prot |= page_get_flags(addr); + for (a = real_start; a < start; a += TARGET_PAGE_SIZE) { + prot |= page_get_flags(a); } - if (real_end == real_start + qemu_host_page_size) { - for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { - prot |= page_get_flags(addr); - } - end = real_end; + for (a = last; a < real_last; a += TARGET_PAGE_SIZE) { + prot |= page_get_flags(a + 1); + } + if (prot != 0) { + return; + } + } else { + for (prot = 0, a = real_start; a < start; a += TARGET_PAGE_SIZE) { + prot |= page_get_flags(a); } if (prot != 0) { real_start += qemu_host_page_size; } - } - if (end < real_end) { - prot = 0; - for (addr = end; addr < real_end; addr += TARGET_PAGE_SIZE) { - prot |= page_get_flags(addr); + + for (prot = 0, a = last; a < real_last; a += TARGET_PAGE_SIZE) { + prot |= page_get_flags(a + 1); } if (prot != 0) { - real_end -= qemu_host_page_size; + real_last -= qemu_host_page_size; + } + + if (real_last < real_start) { + return; } } - if (real_start != real_end) { - mmap(g2h_untagged(real_start), real_end - real_start, PROT_NONE, - MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, - -1, 0); - } + + real_len = real_last - real_start + 1; + host_start = g2h_untagged(real_start); + + ptr = mmap(host_start, real_len, PROT_NONE, + MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, -1, 0); + assert(ptr == host_start); } int target_munmap(abi_ulong start, abi_ulong len)