From patchwork Fri Nov 6 03:28:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 320731 Delivered-To: patch@linaro.org Received: by 2002:a92:7b12:0:0:0:0:0 with SMTP id w18csp978159ilc; Thu, 5 Nov 2020 19:34:52 -0800 (PST) X-Google-Smtp-Source: ABdhPJxdYBBLM2VVq9S9fMdWJ+Mqzgj+L8JIbGfxIyzEaVWtKx8aFOtzGJE5XFjOPTMWmONpKHxP X-Received: by 2002:a25:bccc:: with SMTP id l12mr210062ybm.187.1604633692566; Thu, 05 Nov 2020 19:34:52 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604633692; cv=none; d=google.com; s=arc-20160816; b=aomiGuxPSB9yHwsjlnFrLOiSjsBhU8fm4XU/26EEuUhWAaOHjf3TPydpv6iv6hXSOD FhS1m4QD2KjJnXRfaYPUrhraGorGg+Zlv40G6xK9JhHWTmPXM90VaxglIJNObfKrG4pX 95bvON/t/gMCr0e1wxei5icj9yUTH5PkxsVQEoW4rBm8k3OnXNS5JBV0qyKTfYcFVAEl hxfuPyOp3Scct4gd0qhXuHlf0+pTDr3wxFhzwe8ZzgrkmiOg8fV6YFqI/yzGlLloqxpE aSa+TeHF6h7kaYmZ2MDNhzp2uPQ+7AYAZyVTI0353mg+scXdPw7kBovT5YM7TnHdt4th F8xQ== 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=QxiqvQ209wXHxoJuZyW/ayX9Rmszqy3dKezpaxev5Pc=; b=oLBIunQoyY9h6zgu8r8EB1SAcnxXt7QxgU6Sji9YIteHcWrL13o366UQSFPtS3IiWy vnHiRhpEeWJsBbENa4Eot8bJfNxNs3qrxSydpPchFDUT37HMIXuYE8eu5ZPJ7t4GMp2e 6yo4HvR46skjggNyYNmIeHRO40wWvbTZuQ2WsFK+fWAxeHgi5eOPt2dN35XM+H/pjJfj ZhZMr0fRtWlIQLy408/iM3aH59C57KI16toQps1FEBEBuV/jaq4P9DmQTbmgVtIK/F5/ aHt9fmsM0FKQrBVJpkh7VFatsQGppr9AKJVm1DxChHnfFABKJSxJTu8gCtsFJ2223DEV haZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TSmWNu5f; 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 n85si2827121yba.316.2020.11.05.19.34.52 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 05 Nov 2020 19:34:52 -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=TSmWNu5f; 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]:34152 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kasWZ-00032H-WD for patch@linaro.org; Thu, 05 Nov 2020 22:34:52 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:47022) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kasRf-0004sv-R1 for qemu-devel@nongnu.org; Thu, 05 Nov 2020 22:29:47 -0500 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]:38999) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kasRc-0006x4-Jo for qemu-devel@nongnu.org; Thu, 05 Nov 2020 22:29:47 -0500 Received: by mail-pf1-x442.google.com with SMTP id z3so45447pfz.6 for ; Thu, 05 Nov 2020 19:29:43 -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=QxiqvQ209wXHxoJuZyW/ayX9Rmszqy3dKezpaxev5Pc=; b=TSmWNu5fGLyzHJJu9w08BvM4mojzlhQfiimiZuHvYITzrSh/8++k6IrCnS9ilUMPzR 99N8C1HP2APDXp/TmDXA1ecVQIK/SeVOoaYEm6wY6FjjY4IJ9DSCDzYlO/D8XHgM8QyH EfJWGOKMhfp/sSr0cmI9RGC6hojUWEOqhW3T8alRR4FVNdC9oVCMEhFhJj3DgX2cqs+O GSfSW06TExb7BOrTlO3bdVJ0WaRhSqvxJ8NvaAP17XfD3DcSRux+XgGIuIXq7HNxPqsz cR9+XNILqRNbt2IaTOR+SsWIGAJsGOH1QyxeNJCh5xE+OJfKRvqLGU35d6AZw+JwjLkv Jp8A== 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=QxiqvQ209wXHxoJuZyW/ayX9Rmszqy3dKezpaxev5Pc=; b=QeRmcBi3Y5BPgZACPv5yGb+w4DmVHbTOmPYgR72dqxy1vTJO7Uo2Hr2S6oWJh2csTy gEOqvKYJgX//rC1rbP80HQZGVkxj8prI29syju9rRJghV2vJjloHGJJBHUK0SDFOw8ow r0XZZrV5KMiCDagRmoRKfc/esVLBLrDiLIDaEKD2lfC1cgP3tun6OPFEZBpFJfSkqUiI sd9gXan2O5P38mAx0kMkBmHZrPuD6gs60Y5APhHzxxZOUlbl3Z+fPshNFgQ9KvNIchyC 6eumxB6AqvC4POMGwEIrJ6Gatdr1zIUw8L0jRJe13quUk6HWulhcgk6jNMrdIYcjBN0Q le4g== X-Gm-Message-State: AOAM530hnICOiYwksflDm6DVWir7TYAXAsCXw/d0AduT6K7pzeUECnHv DILuiNRAsHNmwdt00gfRYcfj8OZP0llgJA== X-Received: by 2002:a63:134d:: with SMTP id 13mr5293337pgt.370.1604633382549; Thu, 05 Nov 2020 19:29:42 -0800 (PST) Received: from localhost.localdomain (76-14-210-194.or.wavecable.com. [76.14.210.194]) by smtp.gmail.com with ESMTPSA id i10sm40773pfd.60.2020.11.05.19.29.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Nov 2020 19:29:41 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v3 15/41] accel/tcg: Support split-wx for linux with memfd Date: Thu, 5 Nov 2020 19:28:55 -0800 Message-Id: <20201106032921.600200-16-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201106032921.600200-1-richard.henderson@linaro.org> References: <20201106032921.600200-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::442; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x442.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. 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: j@getutm.app 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. 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 a29cb4a42e..1931e65365 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)