From patchwork Sun May 2 23:18:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 430482 Delivered-To: patch@linaro.org Received: by 2002:a17:907:764d:0:0:0:0 with SMTP id kj13csp1229991ejc; Sun, 2 May 2021 16:36:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxyrdU3c0RKsNpPsp1TzqHSYEuDBgDtLcsEQZuYYV8Gc0a1P/NFXAJ5OYZWxKpwGqAQ77Ny X-Received: by 2002:a05:6e02:156c:: with SMTP id k12mr13639815ilu.49.1619998572452; Sun, 02 May 2021 16:36:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619998572; cv=none; d=google.com; s=arc-20160816; b=F5x+JPpfnz1399ze7WDivPHM76DqkpEFTaWn/uF1AArAs8crxI+upIRAFsLXmh54PP l3vcgeV6AXQZ1cuXQ/gx0QgcdoikqYu7y5AkMKnjUSiKK9817BXgccuqVrAn5IRkcHDN GE+rA7SYphQbv65SATSSJ7/CBnpGGUvbarV3fbYp05OiNWtvaQ2blSqz/1FRzu96klZ1 HrJ5QdKaNJ2ob7b8n41MsjqmW53taoqXy1uF/GLw/nUp5qrVz1KHOH1JAiFnZnC6KfkG HpGkCYp5KJ95YiQbnJCd8U89IIMQ4SxVDP4i67Zt2LamH+G3/69jTc9NNwJiS9P/aYOF 7hHg== 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:to:from :dkim-signature; bh=5l4dTEQ+iJa2jxENce/P7jthFYg4uP2TXzJ1O/jGZ+Y=; b=XhEb7xhyup+UPwdeeDJChYLSjyCqegGoU1G5TVA7kW3DzH5zoSBpwszaqn2fwodX7q TFlm4q2KOoWHDzeWLDjzGskJ1GdfTwJ7UxSTl6u3+LOWMRsXVkC+nSpWtnGRD9gn2qOe slOEhrz4HcjvKxvPM/xOpaV49cyEjKqsz1aPH7HrMzlkdQNI1N9fuU/fauh6JocVVkTl Y356NWkERltS1Lsz6E0BSqf/I+WPbxmKm7NQ+yeItUL8TpsghN7ONylR/Sa0O4onsgms kqFCvmwwTYAC4uxWgYtkbNcREHlP60NJl2H0eLxv/u10WwQTuXmJWpkadTAaFZ3O0OGa o3hw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=oWB3YD69; 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 d6si6066698ilv.134.2021.05.02.16.36.12 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sun, 02 May 2021 16:36:12 -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=oWB3YD69; 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]:55212 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ldLdD-0004lJ-PF for patch@linaro.org; Sun, 02 May 2021 19:36:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37198) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ldLMn-0008OC-Sb for qemu-devel@nongnu.org; Sun, 02 May 2021 19:19:13 -0400 Received: from mail-pj1-x102e.google.com ([2607:f8b0:4864:20::102e]:34534) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ldLMh-0002zT-IC for qemu-devel@nongnu.org; Sun, 02 May 2021 19:19:13 -0400 Received: by mail-pj1-x102e.google.com with SMTP id t2-20020a17090a0242b0290155433387beso8876112pje.1 for ; Sun, 02 May 2021 16:19:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=5l4dTEQ+iJa2jxENce/P7jthFYg4uP2TXzJ1O/jGZ+Y=; b=oWB3YD69tY3UrXqCZ6ReDTbFcFYpoaXmPC1VE50NiJB0i0AqqhPj+opLD+I07E6cCQ b4E413RmBsjfo+xRdkgRwATew9lX9+nLUd66qIiDCvuYKAvdjNc3SNEqD9Mj/L2g42BE frVGfuT+vajsHy25OJ7pzOE8S1abTS8Rsh1Z1EfEZhGDPPSB6+9xd0SeZAOMlePLcAIb yOptrfD0SDknoIhE5Unq61pd0bCX0kr3eB686obMP98PZnzM/bASOta73BIJGJVT2xHB Uh7g27iluObewQ2ZWpNEFD7vxleUpDrAbcWYq9qWtynFkqOgVewCTWSL3wozmE7bSkpP BjVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5l4dTEQ+iJa2jxENce/P7jthFYg4uP2TXzJ1O/jGZ+Y=; b=T0m+U479UnGNVBHwU73/UJC8QaQkLXj1VpCwjJZK+Xp1yTGpCvmX+ePohMQSLfwyEI QasoD18IoIHq9uPO7kWytIyLdrYWTPo+QrMUlBGK42RaNBE8GgJoNHNSDF8tFM4WEFgR uSO5sL3Y22yKA+3ZadSZboYmpUG1f7gkV0E8gVFFPY3POLpmYd164BK7X0AgWPXphabt 1ZWEmCGnDyxRmyruhi7rj8Kl2QKSbPw3XwGJ8cvDLXWRkIX36yYt1Dwdjf08uz9Qn73M E4eFEZkdRmEneZO784+aKhwvN/NN+gcjwU72dLi8MTEadMlQRnNqfs6Yqtf08tBLjwct zzeA== X-Gm-Message-State: AOAM532cRH0/KBLL/fyFIve2DS5eaJSgExzOVwOtbiRNVfQhoMjJmucz 40gKM5TOYCBUgho3uUlU8bRSEuHp07RYPQ== X-Received: by 2002:a17:902:6941:b029:ee:dacc:3ab1 with SMTP id k1-20020a1709026941b02900eedacc3ab1mr1003352plt.3.1619997545408; Sun, 02 May 2021 16:19:05 -0700 (PDT) Received: from localhost.localdomain ([71.212.144.24]) by smtp.gmail.com with ESMTPSA id k17sm7146236pfa.68.2021.05.02.16.19.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 02 May 2021 16:19:05 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v3 26/28] tcg: Merge buffer protection and guard page protection Date: Sun, 2 May 2021 16:18:42 -0700 Message-Id: <20210502231844.1977630-27-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210502231844.1977630-1-richard.henderson@linaro.org> References: <20210502231844.1977630-1-richard.henderson@linaro.org> MIME-Version: 1.0 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.23 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" Do not handle protections on a case-by-case basis in the various alloc_code_gen_buffer instances; do it within a single loop in tcg_region_init. Signed-off-by: Richard Henderson --- tcg/region.c | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) -- 2.25.1 Reviewed-by: Alex Bennée Reviewed-by: Luis Pires diff --git a/tcg/region.c b/tcg/region.c index 49764b40dc..604530b902 100644 --- a/tcg/region.c +++ b/tcg/region.c @@ -530,11 +530,6 @@ static int alloc_code_gen_buffer(size_t tb_size, int splitwx, Error **errp) } #endif - if (qemu_mprotect_rwx(buf, size)) { - error_setg_errno(errp, errno, "mprotect of jit buffer"); - return false; - } - region.start_aligned = buf; region.total_size = size; @@ -818,8 +813,7 @@ void tcg_region_init(size_t tb_size, int splitwx, unsigned max_cpus) { const size_t page_size = qemu_real_host_page_size; size_t region_size; - size_t i; - int have_prot; + int have_prot, need_prot; /* Size the buffer. */ if (tb_size == 0) { @@ -879,18 +873,41 @@ void tcg_region_init(size_t tb_size, int splitwx, unsigned max_cpus) * Set guard pages in the rw buffer, as that's the one into which * buffer overruns could occur. Do not set guard pages in the rx * buffer -- let that one use hugepages throughout. + * Work with the page protections set up with the initial mapping. */ - for (i = 0; i < region.n; i++) { + need_prot = PAGE_READ | PAGE_WRITE; +#ifndef CONFIG_TCG_INTERPRETER + if (tcg_splitwx_diff == 0) { + need_prot |= PAGE_EXEC; + } +#endif + for (size_t i = 0, n = region.n; i < n; i++) { void *start, *end; tcg_region_bounds(i, &start, &end); + if (have_prot != need_prot) { + int rc; - /* - * macOS 11.2 has a bug (Apple Feedback FB8994773) in which mprotect - * rejects a permission change from RWX -> NONE. Guard pages are - * nice for bug detection but are not essential; ignore any failure. - */ - (void)qemu_mprotect_none(end, page_size); + if (need_prot == (PAGE_READ | PAGE_WRITE | PAGE_EXEC)) { + rc = qemu_mprotect_rwx(start, end - start); + } else if (need_prot == (PAGE_READ | PAGE_WRITE)) { + rc = qemu_mprotect_rw(start, end - start); + } else { + g_assert_not_reached(); + } + if (rc) { + error_setg_errno(&error_fatal, errno, + "mprotect of jit buffer"); + } + } + if (have_prot != 0) { + /* + * macOS 11.2 has a bug (Apple Feedback FB8994773) in which mprotect + * rejects a permission change from RWX -> NONE. Guard pages are + * nice for bug detection but are not essential; ignore any failure. + */ + (void)qemu_mprotect_none(end, page_size); + } } tcg_region_trees_init();