From patchwork Tue Jan 5 17:19:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 356883 Delivered-To: patch@linaro.org Received: by 2002:a17:906:4755:0:0:0:0 with SMTP id j21csp303683ejs; Tue, 5 Jan 2021 09:26:50 -0800 (PST) X-Google-Smtp-Source: ABdhPJwPF322vuTwLAyhEpKoQ6r0saU0iQjnmq033Hl7/u7JPohQItb5QfdFUsmN+HvQQl/qKT5I X-Received: by 2002:a25:d0d4:: with SMTP id h203mr723488ybg.274.1609867610773; Tue, 05 Jan 2021 09:26:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609867610; cv=none; d=google.com; s=arc-20160816; b=eRasf1EhVdHVVQplCm/SY6khgUugcUxoKwtg4ZNWArv+15jGFtzxQkeaAsY1diq+GJ ZIpEkJMjGh+3OT9muTPtDypRvxbwZ04+6zLz2VLWQShn7W3EX02iIzEISQmgYSGHAKYP 6J8PX3looYp2MOikaurA8Mg/OqCesChoipeWQQB2UXKFA4zIkVYYM8fKisLTSTSWJq81 CFyvaNfZUlR78vrMxi1Jf6Mx/c0oPw52G7Hmtms+5NVT73WqzEaYuDGhGAdk42J4myc+ WPQEeT7bye5+QGgH+xji2FzTmC41tyLJjPCHtG9t2xgRFmEWuCRKE0E+8QoFJbmEqMCd Rftw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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=eOIoFRufaJyIw+Oc9PubO7k3MAtBUycjaNlTW//kLYE=; b=cMfaEXQ086il0JFFLgDV0cbcx0POM4zwXmS5mV+4FNh2F34N++SHdpXayFUQHbeJ3v vj0WK5vW/6wemhgJGLXTs6V3IxdeYEez6HguSa8TpsXmxNUZygwh+GDGs9yT3VEveyN3 Z7yXBdYNoXGTXwUIgcuc7e++HAL+oNocoHuxwRnGp+5zTdmwXX1hgQl+y+LBdtS5R+q3 cUl5ODb8a6m8g+HtQ1VAMgB4KGZ4lARon/h0lbPjSm6UuW6A2yB/yihi1tdD3r53rT7W HaGpzKG0qyyZAjXuB7OgR9SWOmBzgk8ojcbS8YllxX1o2v9TjNQfah2li1xMcLnTJ2IB SM2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=EdzMGYE8; 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 j7si378176ybt.380.2021.01.05.09.26.50 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 05 Jan 2021 09:26:50 -0800 (PST) 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=EdzMGYE8; 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]:50114 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kwq6c-0002qy-6n for patch@linaro.org; Tue, 05 Jan 2021 12:26:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51552) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kwq0Y-0002Ob-DO for qemu-devel@nongnu.org; Tue, 05 Jan 2021 12:20:36 -0500 Received: from mail-pg1-x52f.google.com ([2607:f8b0:4864:20::52f]:46614) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kwq0T-0006bP-R9 for qemu-devel@nongnu.org; Tue, 05 Jan 2021 12:20:33 -0500 Received: by mail-pg1-x52f.google.com with SMTP id c22so251665pgg.13 for ; Tue, 05 Jan 2021 09:20:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=eOIoFRufaJyIw+Oc9PubO7k3MAtBUycjaNlTW//kLYE=; b=EdzMGYE8qddmzTjJ2Xhq67S7P48+nbYMWwmBFXkmqfw6/1OjLkEGTIpNXM3nRY4/kj KenehbIIHL7tJFyHW3ndyDJTSLDXB4nhXiHjzh3eEuYT/c7ynfqJ1Ljg0KYsnq6WXEwr kTCBCP5iMw93T7CIv5JLOXCTbfOUS8FZxe4fwUVRqO7JCsMrKOAhRnmrw2jxMQag6+tH t2geJHUS5md/eeHO8ZOJE9SDifcp4fJJ7nCxCB+1Vf4O2j326Dc1PFeAvedcuLvKtxW/ le2Ri5DQfTMOSBDfhmnNF2lN8vuOGkplnP7e9ETr8XVmQj4Vm1stabARJNz5lENYwjmq E4sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=eOIoFRufaJyIw+Oc9PubO7k3MAtBUycjaNlTW//kLYE=; b=bX+/po+mGSwC810AwEYJb2RwEfB2F8SAK1xTH6/i4A+idrr3CaYBwLs/rc8SsTy31r uaPykry8vhiFCrZtUlqsgtsNZyHyCKEiKc8YArGjSlnUb31oAdA3gtOKoGNZvE/YWEL7 SVfeW9FHbah0sUqs1wrZ+UZBx7xXA2m+szMTYIlE/tyGtDZFu1vkmlbi3SCLAbPajAft C39sTBPpICNg0E3/GtiLAnwRyE1Dc4BKv+zTU9AOkhgAB7JMfnLW1FGqba8IWQ9RfstV A53361FSk+1puHkMmZ+N53KYmehA5axhOyT3k6XbOhr3D3cIigwEDSKwvphRgYJNsmdc i3mg== X-Gm-Message-State: AOAM533WdK+2FdvB48gEcR5qDnX3c1cb5w3fmBT9sUHeiwUJSApDMu06 W99wqLnP6as/qQ6UYTeeGDpWm32zE15+wA== X-Received: by 2002:aa7:96d8:0:b029:19e:bc79:cf7 with SMTP id h24-20020aa796d80000b029019ebc790cf7mr237418pfq.22.1609867227705; Tue, 05 Jan 2021 09:20:27 -0800 (PST) Received: from localhost.localdomain (rrcs-173-197-107-21.west.biz.rr.com. [173.197.107.21]) by smtp.gmail.com with ESMTPSA id u12sm2010pfh.98.2021.01.05.09.20.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Jan 2021 09:20:27 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v5 18/43] accel/tcg: Support split-wx for linux with memfd Date: Tue, 5 Jan 2021 07:19:25 -1000 Message-Id: <20210105171950.415486-19-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210105171950.415486-1-richard.henderson@linaro.org> References: <20210105171950.415486-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::52f; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x52f.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: , Cc: Joelle van Dyne Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" We cannot use a real temp file, because we would need to find a filesystem that does not have noexec enabled. However, a memfd is not associated with any filesystem. Reviewed-by: Joelle van Dyne Signed-off-by: Richard Henderson --- accel/tcg/translate-all.c | 84 +++++++++++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 8 deletions(-) -- 2.25.1 diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 0c1801b034..27c88527ea 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -1078,17 +1078,11 @@ static bool alloc_code_gen_buffer(size_t size, int splitwx, Error **errp) return true; } #else -static bool alloc_code_gen_buffer(size_t size, int splitwx, Error **errp) +static bool alloc_code_gen_buffer_anon(size_t size, int prot, + int flags, Error **errp) { - int prot = PROT_WRITE | PROT_READ | PROT_EXEC; - int flags = MAP_PRIVATE | MAP_ANONYMOUS; void *buf; - if (splitwx > 0) { - error_setg(errp, "jit split-wx not supported"); - return false; - } - buf = mmap(NULL, size, prot, flags, -1, 0); if (buf == MAP_FAILED) { error_setg_errno(errp, errno, @@ -1137,6 +1131,80 @@ static bool alloc_code_gen_buffer(size_t size, int splitwx, Error **errp) tcg_ctx->code_gen_buffer = buf; return true; } + +#ifdef CONFIG_POSIX +#include "qemu/memfd.h" + +static bool alloc_code_gen_buffer_splitwx_memfd(size_t size, Error **errp) +{ + void *buf_rw, *buf_rx; + int fd = -1; + + buf_rw = qemu_memfd_alloc("tcg-jit", size, 0, &fd, errp); + if (buf_rw == NULL) { + return false; + } + + buf_rx = mmap(NULL, size, PROT_READ | PROT_EXEC, MAP_SHARED, fd, 0); + if (buf_rx == MAP_FAILED) { + error_setg_errno(errp, errno, + "failed to map shared memory for execute"); + munmap(buf_rw, size); + close(fd); + return false; + } + close(fd); + + tcg_ctx->code_gen_buffer = buf_rw; + tcg_ctx->code_gen_buffer_size = size; + tcg_splitwx_diff = buf_rx - buf_rw; + + /* Request large pages for the buffer and the splitwx. */ + qemu_madvise(buf_rw, size, QEMU_MADV_HUGEPAGE); + qemu_madvise(buf_rx, size, QEMU_MADV_HUGEPAGE); + return true; +} +#endif /* CONFIG_POSIX */ + +static bool alloc_code_gen_buffer_splitwx(size_t size, Error **errp) +{ + if (TCG_TARGET_SUPPORT_MIRROR) { +#ifdef CONFIG_POSIX + return alloc_code_gen_buffer_splitwx_memfd(size, errp); +#endif + } + error_setg(errp, "jit split-wx not supported"); + return false; +} + +static bool alloc_code_gen_buffer(size_t size, int splitwx, Error **errp) +{ + ERRP_GUARD(); + int prot, flags; + + if (splitwx) { + if (alloc_code_gen_buffer_splitwx(size, errp)) { + return true; + } + /* + * If splitwx force-on (1), fail; + * if splitwx default-on (-1), fall through to splitwx off. + */ + if (splitwx > 0) { + return false; + } + error_free_or_abort(errp); + } + + prot = PROT_READ | PROT_WRITE | PROT_EXEC; + flags = MAP_PRIVATE | MAP_ANONYMOUS; +#ifdef CONFIG_TCG_INTERPRETER + /* The tcg interpreter does not need execute permission. */ + prot = PROT_READ | PROT_WRITE; +#endif + + return alloc_code_gen_buffer_anon(size, prot, flags, errp); +} #endif /* USE_STATIC_CODE_GEN_BUFFER, WIN32, POSIX */ static bool tb_cmp(const void *ap, const void *bp)