From patchwork Fri Apr 26 17:24:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 162957 Delivered-To: patch@linaro.org Received: by 2002:a02:c6d8:0:0:0:0:0 with SMTP id r24csp1011864jan; Fri, 26 Apr 2019 10:33:23 -0700 (PDT) X-Google-Smtp-Source: APXvYqxuVJX2wghH1jb7BkNHyOlKaoxAOVxFk7EeqDdgHZIPpIcL2fabTbbPZkZTeQVyMJ+XriHg X-Received: by 2002:a05:6000:11c2:: with SMTP id i2mr2442372wrx.147.1556300003752; Fri, 26 Apr 2019 10:33:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1556300003; cv=none; d=google.com; s=arc-20160816; b=iTzXwq11Q75Ir065UfT7mxaageCfe8IB34MGoRsdUVDvxrOuw0Ugzi29rC5OnN3EhB tXwAkn4JxqX52QAWwFh5HtKVjfmkGcsNAy1wrex8n798c5GM1TRSIfV6Qt5fq6NqJg1F XuLhKw5dSA5q8XAm6flVu5B5fyjpmU7aPcLCq+XuxqwaLRUdBB3BQXTaxMrqbzRnw4VZ ehEQl2Anv+h0Htrd4q4I8wJKpciq7GjAyX7euPRYH2q6NBy+5uVbT5u9W5CoPCjpPva7 ESh7L3MkbaJ3XrNDbXkFfAAQPG5OirkHqu98qQwvBSpIWyH99UhdKIGCFlTuan0LFX6+ 8kjQ== 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:subject :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature; bh=3CywljlasHfL8Ycnk57vM6Ac8IPl3XHI/QQlXit0YoU=; b=Eod4oh3SOfOts53s+FfYhRno8c8+4X9WOcG2cAmHY2O8S/Jj3k3QM/YlnAnxW8UDyK AGxKVC3f68MDFOX/o42XM1AoUi0NniJVHTDmD+eCW78LYJDtzC2syo/qg1UvAsoNVTQl xtjlOwgS5dS9yV4Iwd3Kry58zvkbhhQiPsN5bTy1/PH9lrruhvUE6680ZuTSeg5cgo0a 9Rit9lkmFMmBzzypA/03CJF7bH4AcrKcLpdQvLsCrnZYi9x8XhRV0dca9bnDDBHywAgJ VJh5gSHsUs+2opKFMu0I4DqNB+816L2rMoijOsNLElUlpWkQgSwQ0jqRlFmse7K9oiXd yvOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@linaro.org header.s=google header.b=MonM3534; 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 b3si2678383wrx.334.2019.04.26.10.33.23 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 26 Apr 2019 10:33:23 -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=MonM3534; 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 ([127.0.0.1]:50027 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hK4iw-00084P-Mx for patch@linaro.org; Fri, 26 Apr 2019 13:33:22 -0400 Received: from eggs.gnu.org ([209.51.188.92]:48000) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hK4aT-0008WU-90 for qemu-devel@nongnu.org; Fri, 26 Apr 2019 13:24:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hK4aR-0006fl-W4 for qemu-devel@nongnu.org; Fri, 26 Apr 2019 13:24:37 -0400 Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]:36430) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hK4aR-0006eX-QF for qemu-devel@nongnu.org; Fri, 26 Apr 2019 13:24:35 -0400 Received: by mail-pg1-x544.google.com with SMTP id 85so1943132pgc.3 for ; Fri, 26 Apr 2019 10:24:35 -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=3CywljlasHfL8Ycnk57vM6Ac8IPl3XHI/QQlXit0YoU=; b=MonM3534UrqIVvSEtZvvDchMqiEFFiNcO8Y5L+Zei3KnAjWH8Pazvml/voPma/mHjk T65hokEglAPGIhkt6aluCVNkuo6ZZEeiThLz3futfmKVKirRl1K2TIVg/RdaYX6cIS2N D+FXYrqKHhWNBrQ67x/wpg8ZrwwUMqa/KL16biHFcgqy2WJAPyBVGT6xXYQp41q+JHVY xpEXBCzSJt3dw3uGO+u2SdbuIYqpmU07+yBUOaIgucXcAjt45yWBGUKDbV1m+IgBwjNk RxMQ0dtFb7kU1B8j1sgziqu8DohqzIZr1YKBQwPTvLKuxoi/85v9ew1WlY/7NxgOosSM vJlA== 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=3CywljlasHfL8Ycnk57vM6Ac8IPl3XHI/QQlXit0YoU=; b=uVxwq4raJ8VQtPov2mrkBFo6t/JoLRrOGOrusdIvQ5BYkkJ6u/rLQxZMHF4jR21TWu s32yW5078ch+SoVh2IiLdGWZ+qCHz5R5J4k0Ph1v1lkMpDhmuWLtl1enSU16WyypMAgm 8dNDi0Wbs+pxkNusLDmMH21UJaOUQ9UIKbgXKVU1tl4NcxoPuofN5+HZYkHOG1wFGTpD 1X1bZt9EraVdG578V45rgELLYgWX56g0CDWOb5NWDXN8zOPumNdH7+dq3qfn3n3s96pH hq+2k8VSzO7SNB5Ly86EvLo/lhmWtL1V4H8Bb/SOo8LKLNiuBeoei3NatZVJBykyJfiy 8qGg== X-Gm-Message-State: APjAAAXr7K3JioqAes4ier/rWikL3k9J0Ye7wN0BVL8NEn0NbEyPNVdp mDketjA01jLj4LF2b2BwMn3FgYe4Pcs= X-Received: by 2002:a63:cc48:: with SMTP id q8mr15704362pgi.202.1556299474477; Fri, 26 Apr 2019 10:24:34 -0700 (PDT) Received: from localhost.localdomain (97-113-179-147.tukw.qwest.net. [97.113.179.147]) by smtp.gmail.com with ESMTPSA id j5sm7901762pfg.186.2019.04.26.10.24.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Apr 2019 10:24:33 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Date: Fri, 26 Apr 2019 10:24:15 -0700 Message-Id: <20190426172421.27133-10-richard.henderson@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20190426172421.27133-1-richard.henderson@linaro.org> References: <20190426172421.27133-1-richard.henderson@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::544 Subject: [Qemu-devel] [PULL 09/15] tcg: Restart after TB code generation overflow X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" If a TB generates too much code, try again with fewer insns. Fixes: https://bugs.launchpad.net/bugs/1824853 Reviewed-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson --- accel/tcg/translate-all.c | 38 ++++++++++++++++++++++++++++++++------ tcg/tcg.c | 4 ++++ 2 files changed, 36 insertions(+), 6 deletions(-) -- 2.17.1 diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index 39532fd44c..20b59f93f4 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -1722,6 +1722,7 @@ TranslationBlock *tb_gen_code(CPUState *cpu, tb->cflags = cflags; tb->trace_vcpu_dstate = *cpu->trace_dstate; tcg_ctx->tb_cflags = cflags; + tb_overflow: #ifdef CONFIG_PROFILER /* includes aborted translations because of exceptions */ @@ -1755,14 +1756,39 @@ TranslationBlock *tb_gen_code(CPUState *cpu, ti = profile_getclock(); #endif - /* ??? Overflow could be handled better here. In particular, we - don't need to re-do gen_intermediate_code, nor should we re-do - the tcg optimization currently hidden inside tcg_gen_code. All - that should be required is to flush the TBs, allocate a new TB, - re-initialize it per above, and re-do the actual code generation. */ gen_code_size = tcg_gen_code(tcg_ctx, tb); if (unlikely(gen_code_size < 0)) { - goto buffer_overflow; + switch (gen_code_size) { + case -1: + /* + * Overflow of code_gen_buffer, or the current slice of it. + * + * TODO: We don't need to re-do gen_intermediate_code, nor + * should we re-do the tcg optimization currently hidden + * inside tcg_gen_code. All that should be required is to + * flush the TBs, allocate a new TB, re-initialize it per + * above, and re-do the actual code generation. + */ + goto buffer_overflow; + + case -2: + /* + * The code generated for the TranslationBlock is too large. + * The maximum size allowed by the unwind info is 64k. + * There may be stricter constraints from relocations + * in the tcg backend. + * + * Try again with half as many insns as we attempted this time. + * If a single insn overflows, there's a bug somewhere... + */ + max_insns = tb->icount; + assert(max_insns > 1); + max_insns /= 2; + goto tb_overflow; + + default: + g_assert_not_reached(); + } } search_size = encode_search(tb, (void *)gen_code_buf + gen_code_size); if (unlikely(search_size < 0)) { diff --git a/tcg/tcg.c b/tcg/tcg.c index c0730f119f..5d255166c0 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -3996,6 +3996,10 @@ int tcg_gen_code(TCGContext *s, TranslationBlock *tb) if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) { return -1; } + /* Test for TB overflow, as seen by gen_insn_end_off. */ + if (unlikely(tcg_current_code_size(s) > UINT16_MAX)) { + return -2; + } } tcg_debug_assert(num_insns >= 0); s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);