From patchwork Fri Oct 30 00:49:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 319973 Delivered-To: patch@linaro.org Received: by 2002:a92:7b12:0:0:0:0:0 with SMTP id w18csp979479ilc; Thu, 29 Oct 2020 18:18:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxfu7XgOMkAP5//jJ9yxKjxsB5CBJAh491Js0+s6g0VYthhc2SR0CA7wPBVJGEo37Boj9gC X-Received: by 2002:a25:10c5:: with SMTP id 188mr262664ybq.181.1604020735699; Thu, 29 Oct 2020 18:18:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1604020735; cv=none; d=google.com; s=arc-20160816; b=CLTrVf18nOwebqIkPQD0xqQkwlUf8cwbJXHiCE8y5vl2svI1x4gYHEX7qlHJfJXNs7 VfXpm9ikSghryqH6i4Ef38YQpWcBt4knJfs2c0d/mSMNuQ9nEBddx22IKSYhgCrrdDNX B2EKS09I5DSp2EDi32iZ0CRgF1yBXd78MISruzNkVm1xIS20qyc+GhOxYP4+AF7UXGCk QlyTQvnV2ipsVuoe9pWWrYvm1QJwtOPr1KQrtWkJHbAzitoya//d+NTiFMdzdAL0jzLi zLr/BDah9dRF2GX0Wrdv6rG4oL32l0AUPXqEFywmUvMSj1FTFG1bgoA565cY/jsQBTVu gzcg== 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=T96qfuK9LFACPCMFZJEu5AkD3dQj8mh/jlsxNmhHWgc=; b=B0+OEmYoSU/78S3TEG0rjh0bCXh2EeNcBpKwl+V4yXYK0xTw4HfNl2IyA6J4kGIHdt f+a6mZFuhnE8XceQOFvbtAfbtTWzfrIYEfeVSX0xRsjNAP1NYRsz5ybdWe8JVw+Aw9O9 Vp5apbWRJ8E9jfwmy93WXPloFjSvuBJ6AgS/hz5Tf/bsytr3U3HXwhMZZvryiuPZaJ7K 1ReJlhkQ68loxiIhO5U9R/qHKpyZ8vdKXuCFO6zMAJWHbGpo0OE7UL0opOCeODfNeXpX 1XCa8NTIdw+q1P0gkT0nqEbCWO/AVRVz8pMHI1n2KrAzQ6PUBa8akKrPHPi1l7mmE89I 7gIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=aEzjT9Mb; 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=fail (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 t11si4675704ybb.115.2020.10.29.18.18.55 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 29 Oct 2020 18:18:55 -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=fail header.i=@linaro.org header.s=google header.b=aEzjT9Mb; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:47438 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kYJ4B-0005d4-1L for patch@linaro.org; Thu, 29 Oct 2020 21:18:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46870) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kYIc0-0004Lk-Ff for qemu-devel@nongnu.org; Thu, 29 Oct 2020 20:49:48 -0400 Received: from mail-pg1-x543.google.com ([2607:f8b0:4864:20::543]:35646) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kYIbu-0001rV-Ha for qemu-devel@nongnu.org; Thu, 29 Oct 2020 20:49:48 -0400 Received: by mail-pg1-x543.google.com with SMTP id f38so3768782pgm.2 for ; Thu, 29 Oct 2020 17:49:42 -0700 (PDT) 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=T96qfuK9LFACPCMFZJEu5AkD3dQj8mh/jlsxNmhHWgc=; b=aEzjT9MbPPJEh1dWF/Lcp/5LIMr+SHddMruzd6YFuUrSCOtTaE92IIgAqSMFepEvDg vZ4JSwcHLP+XBRiTQmNhOWPvWAXTxtDKbYJIHzt5nZqBLTjbxf/mYOnABBu4onHjSXUl nkAsALOBuisaP88jr3dGcqOOeBodzvdNqbsrcK0NCG8tqFVsSI32egzRtgcZ6b3FGDiw 3C8Nqa2mJwF7s/eVo7szITQ1d985p6V+9EvV1uoijarMU87XlZCtfH+EnRCpavduXtsb B/QJBugUVyMJm7m+OLzty9ISlq8XuyngsH8+w2hccpihnNNBUvh7vjC44R/H4k//6etf GZNA== 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=T96qfuK9LFACPCMFZJEu5AkD3dQj8mh/jlsxNmhHWgc=; b=a8fd5aS1DBJlsncGhwkGH5NcShFZiHSU4kyay3l9N9KVst6ViKcMdCycsA0Kn4ejdV P/2F+jk8IjzSd4NQ8smA764+Lxxt1UiOdcrQzUWGw6GXpuN92mdQYPkuezSEpN2wc3et 0R9KBnq+VeHN2PEXjtKrg1XSB5nNiBBkJQA9gDmwRi0cCoczsePd5TxGvYXThj/FTGzO 0qiGBmdP41zrQM0jX5c2Lu82fgOrROs0JGwM9HedF5pczNMWzXD3onqf2klIHZd5KePC FXaOdTBhGNzgeo2XgCz+IFkrFre5fPZGjc2nql80pC5Tebxwt4xJY/Tmjhd7cBF/mgdg 5HEg== X-Gm-Message-State: AOAM533Z1r5YXb4pRc0saYfHcpO1yZq3n75X0Zp85QCKv6bncs/ERjd+ 7FNU2+agGVmGSQWhKlj6JLDqNQkyvUHmBA== X-Received: by 2002:a63:f84c:: with SMTP id v12mr6265769pgj.125.1604018980939; Thu, 29 Oct 2020 17:49:40 -0700 (PDT) Received: from localhost.localdomain ([71.212.141.89]) by smtp.gmail.com with ESMTPSA id b6sm3557264pgq.58.2020.10.29.17.49.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Oct 2020 17:49:40 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 13/19] accel/tcg: Support split-rwx for linux with memfd Date: Thu, 29 Oct 2020 17:49:15 -0700 Message-Id: <20201030004921.721096-14-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201030004921.721096-1-richard.henderson@linaro.org> References: <20201030004921.721096-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::543; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x543.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: pbonzini@redhat.com, j@getutm.app, laurent@vivier.eu 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 | 87 +++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 7 deletions(-) -- 2.25.1 diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 8918a09f10..3e69ebd1d3 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -1088,17 +1088,11 @@ static bool alloc_code_gen_buffer(size_t size, int mirror, Error **errp) return true; } #else -static bool alloc_code_gen_buffer(size_t size, int mirror, Error **errp) +static bool alloc_code_gen_buffer_anon(size_t size, int prot, Error **errp) { - int prot = PROT_WRITE | PROT_READ | PROT_EXEC; int flags = MAP_PRIVATE | MAP_ANONYMOUS; void *buf; - if (mirror > 0) { - error_setg(errp, "jit split-rwx not supported"); - return false; - } - buf = mmap(NULL, size, prot, flags, -1, 0); if (buf == MAP_FAILED) { error_setg_errno(errp, errno, @@ -1147,6 +1141,85 @@ static bool alloc_code_gen_buffer(size_t size, int mirror, Error **errp) tcg_ctx->code_gen_buffer = buf; return true; } + +#ifdef CONFIG_LINUX +#include "qemu/memfd.h" + +static bool alloc_code_gen_buffer_mirror_memfd(size_t size, Error **errp) +{ + void *buf_rw, *buf_rx; + int fd; + + fd = qemu_memfd_create("tcg-jit", size, false, 0, 0, errp); + if (fd < 0) { + return false; + } + + buf_rw = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (buf_rw == MAP_FAILED) { + error_setg_errno(errp, errno, + "allocate %zu bytes for jit buffer", size); + close(fd); + 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, + "allocate %zu bytes for jit mirror", size); + 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_rx_mirror_diff = buf_rx - buf_rw; + + /* Request large pages for the buffer and the mirror. */ + qemu_madvise(buf_rw, size, QEMU_MADV_HUGEPAGE); + qemu_madvise(buf_rx, size, QEMU_MADV_HUGEPAGE); + return true; +} +#endif + +static bool alloc_code_gen_buffer_mirror(size_t size, Error **errp) +{ + if (TCG_TARGET_SUPPORT_MIRROR) { +#ifdef CONFIG_LINUX + return alloc_code_gen_buffer_mirror_memfd(size, errp); +#endif + } + error_setg(errp, "jit split-rwx not supported"); + return false; +} + +static bool alloc_code_gen_buffer(size_t size, int mirror, Error **errp) +{ + if (mirror) { + Error *local_err = NULL; + if (alloc_code_gen_buffer_mirror(size, &local_err)) { + return true; + } + /* + * If mirror force-on (1), fail; + * if mirror default-on (-1), fall through to mirror off. + */ + if (mirror > 0) { + error_propagate(errp, local_err); + return false; + } + } + + int prot = PROT_READ | PROT_WRITE | PROT_EXEC; +#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, errp); +} #endif /* USE_STATIC_CODE_GEN_BUFFER, WIN32, POSIX */ static bool tb_cmp(const void *ap, const void *bp)