From patchwork Thu Nov 18 18:46:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 12693072 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E36B5C433EF for ; Thu, 18 Nov 2021 18:49:34 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id A908961AFD for ; Thu, 18 Nov 2021 18:49:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org A908961AFD Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=kYjzGUS42l5oeHGyuZW93xqL+UUsD1RyNMmiBpdxD/g=; b=zxPR1QmJ74I19I TwKgtvG41pB5SHS69+nORERjufpwCL52+/M5y9Sqf272hhzgTLZ9pM+DgxMLRSJb2OJwly6X3jETl 8opMo/YRm0AXP0PnZwJuYfJr1vkIRZ3cgD+jd7++d53oYgJz8ysSQWrRSjwOyhMIkuMU975P97siP U8K1qPbipk3uaa1b6GcPMDOQAhj9shZ43pd4syTRpntJivFR82Vb4QNZV+tcY6D2nG4jRNojf/cbM UIMADWIZ35vURWvGxAUqy9G0XtcSHBqZFgkzaGYa2DyHEYK6TPp/9LAkUJysg0RCiIjYhBd5tt7SI 9BwtKfSrPjG+cq4Sx9uA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mnmS6-008dFa-A9; Thu, 18 Nov 2021 18:48:06 +0000 Received: from mail-wm1-x335.google.com ([2a00:1450:4864:20::335]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mnmR2-008cvZ-Vp for linux-arm-kernel@lists.infradead.org; Thu, 18 Nov 2021 18:47:02 +0000 Received: by mail-wm1-x335.google.com with SMTP id 77-20020a1c0450000000b0033123de3425so8483591wme.0 for ; Thu, 18 Nov 2021 10:47:00 -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=xmjowUqqjsoP8AlfmYKLxnopZGHsSI/gJkyk5037USk=; b=dwUsDJypNTWzxb1xCgM9KxqhEwspTtdc1biqJsMlafe4KQ1f1qyY6m0l0Z0zHPQOsF d7enZDzFRQjVFro/FbUb7GXF2YQ2Zxv+nyIcw944ptCa5d5jXcOQtc3KLyyfneQN5pX0 fWqT2VYYD359OBstUPubP14iAhvDit6Pkg2L1WRSYKtv4HRgiuQzRQvUJmNkFzQS2R0d I8Vn9Z5+Yax2xgIxMTu5AYd9ddW5k5a75PSGL0uH0sc/qq3hGTdeNAWciRMDKPN77Myo Ee1Pzw1Jca5tEv1dN9YFT6ELo8PZ2aKiu6y1gOrIAwFnD0oplu1WfU1Ut0RN6kEaXHn6 pdZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xmjowUqqjsoP8AlfmYKLxnopZGHsSI/gJkyk5037USk=; b=zHZx+eVR+Q0EIi0HHytNAOddh7B3F6USXY9GYrknAXJ7l+88HckLKX+ZbS1IelW0mo IjPIyy6v8s+ndx+aV5iyt9D3UVtLIdIIEhAgfQPwxFsWvtfcjLNy3j6V9+hcu8sRHuWe 0uDMwMUq/+hIkpDjoEUisG4z0y8NrJo8oJCe+yyMQep9ax6Ne2vVmx5N5BwrgnLNAnLa Kgra/bxPDnhake7azZtkkVfzoRW3oDIltrjatOPHxG4rMppSKy6tB7GYlLP+WOV+lSNT HaCpie54ZAJ+ylJcjMyZG4viv/Oa8aLeXJsPD8A2sW1fz6xnlqTg8j/z5v6vktwpn86J 7I3A== X-Gm-Message-State: AOAM530N9fIV7jdC5N2zWD8jE0BVWRlJSEXnOJlWaworI4WgLE9g6BRO dfsz7emIu71vKQDyr9xvfALdBA== X-Google-Smtp-Source: ABdhPJzYwdTN3EnbaeoJv0JEGoIzLR7U4UyTyp2tmsc/2jx3IUjE2VKN+I4X+3k0ACY//b/8iKbVvQ== X-Received: by 2002:a05:600c:a05:: with SMTP id z5mr12420976wmp.73.1637261219751; Thu, 18 Nov 2021 10:46:59 -0800 (PST) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id g124sm9269456wme.28.2021.11.18.10.46.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Nov 2021 10:46:55 -0800 (PST) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 67B9E1FF9D; Thu, 18 Nov 2021 18:46:50 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: kvm@vger.kernel.org Cc: idan.horowitz@gmail.com, qemu-arm@nongnu.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, christoffer.dall@arm.com, maz@kernel.org, =?utf-8?q?Alex_Benn=C3=A9e?= , Mark Rutland Subject: [kvm-unit-tests PATCH v8 06/10] arm/tlbflush-code: TLB flush during code execution Date: Thu, 18 Nov 2021 18:46:46 +0000 Message-Id: <20211118184650.661575-7-alex.bennee@linaro.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211118184650.661575-1-alex.bennee@linaro.org> References: <20211118184650.661575-1-alex.bennee@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211118_104701_111437_F00ADBAC X-CRM114-Status: GOOD ( 27.86 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This adds a fairly brain dead torture test for TLB flushes intended for stressing the MTTCG QEMU build. It takes the usual -smp option for multiple CPUs. By default it CPU0 will do a TLBIALL flush after each cycle. You can pass options via -append to control additional aspects of the test: - "page" flush each page in turn (one per function) - "self" do the flush after each computation cycle - "verbose" report progress on each computation cycle Signed-off-by: Alex Bennée CC: Mark Rutland --- v2 - rename to tlbflush-test - made makefile changes cleaner - added self/other flush mode - create specific prefix - whitespace fixes v3 - using new SMP framework for test runing v4 - merge in the unitests.cfg v5 - max out at -smp 4 - printf fmtfix v7 - rename to tlbflush-code - int -> bool flags v8 - kernel style fixes - move to separate mttcgtests.cfg --- arm/Makefile.common | 1 + arm/tlbflush-code.c | 209 ++++++++++++++++++++++++++++++++++++++++++++ arm/mttcgtests.cfg | 30 +++++++ 3 files changed, 240 insertions(+) create mode 100644 arm/tlbflush-code.c create mode 100644 arm/mttcgtests.cfg diff --git a/arm/Makefile.common b/arm/Makefile.common index 99bcf3f..e3f04f2 100644 --- a/arm/Makefile.common +++ b/arm/Makefile.common @@ -12,6 +12,7 @@ tests-common += $(TEST_DIR)/gic.flat tests-common += $(TEST_DIR)/psci.flat tests-common += $(TEST_DIR)/sieve.flat tests-common += $(TEST_DIR)/pl031.flat +tests-common += $(TEST_DIR)/tlbflush-code.flat tests-all = $(tests-common) $(tests) all: directories $(tests-all) diff --git a/arm/tlbflush-code.c b/arm/tlbflush-code.c new file mode 100644 index 0000000..ca98c82 --- /dev/null +++ b/arm/tlbflush-code.c @@ -0,0 +1,209 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * TLB Flush Race Tests + * + * These tests are designed to test for incorrect TLB flush semantics + * under emulation. The initial CPU will set all the others working a + * compuation task and will then trigger TLB flushes across the + * system. It doesn't actually need to re-map anything but the flushes + * themselves will trigger QEMU's TCG self-modifying code detection + * which will invalidate any generated code causing re-translation. + * Eventually the code buffer will fill and a general tb_lush() will + * be triggered. + * + * Copyright (C) 2016-2021, Linaro, Alex Bennée + * + * This work is licensed under the terms of the GNU LGPL, version 2. + */ + +#include +#include +#include +#include +#include + +#define SEQ_LENGTH 10 +#define SEQ_HASH 0x7cd707fe + +static cpumask_t smp_test_complete; +static int flush_count = 1000000; +static bool flush_self; +static bool flush_page; +static bool flush_verbose; + +/* + * Work functions + * + * These work functions need to be: + * + * - page aligned, so we can flush one function at a time + * - have branches, so QEMU TCG generates multiple basic blocks + * - call across pages, so we exercise the TCG basic block slow path + */ + +/* Adler32 */ +__attribute__((aligned(PAGE_SIZE))) static +uint32_t hash_array(const void *buf, size_t buflen) +{ + const uint8_t *data = (uint8_t *) buf; + uint32_t s1 = 1; + uint32_t s2 = 0; + + for (size_t n = 0; n < buflen; n++) { + s1 = (s1 + data[n]) % 65521; + s2 = (s2 + s1) % 65521; + } + return (s2 << 16) | s1; +} + +__attribute__((aligned(PAGE_SIZE))) static +void create_fib_sequence(int length, unsigned int *array) +{ + int i; + + /* first two values */ + array[0] = 0; + array[1] = 1; + for (i = 2; i < length; i++) + array[i] = array[i-2] + array[i-1]; +} + +__attribute__((aligned(PAGE_SIZE))) static +unsigned long long factorial(unsigned int n) +{ + unsigned int i; + unsigned long long fac = 1; + + for (i = 1; i <= n; i++) + fac = fac * i; + return fac; +} + +__attribute__((aligned(PAGE_SIZE))) static +void factorial_array(unsigned int n, unsigned int *input, + unsigned long long *output) +{ + unsigned int i; + + for (i = 0; i < n; i++) + output[i] = factorial(input[i]); +} + +__attribute__((aligned(PAGE_SIZE))) static +unsigned int do_computation(void) +{ + unsigned int fib_array[SEQ_LENGTH]; + unsigned long long facfib_array[SEQ_LENGTH]; + uint32_t fib_hash, facfib_hash; + + create_fib_sequence(SEQ_LENGTH, &fib_array[0]); + fib_hash = hash_array(&fib_array[0], sizeof(fib_array)); + factorial_array(SEQ_LENGTH, &fib_array[0], &facfib_array[0]); + facfib_hash = hash_array(&facfib_array[0], sizeof(facfib_array)); + + return (fib_hash ^ facfib_hash); +} + +/* This provides a table of the work functions so we can flush each + * page individually + */ +static void *pages[] = {&hash_array, &create_fib_sequence, &factorial, + &factorial_array, &do_computation}; + +static void do_flush(int i) +{ + if (flush_page) + flush_tlb_page((unsigned long)pages[i % ARRAY_SIZE(pages)]); + else + flush_tlb_all(); +} + + +static void just_compute(void) +{ + int i, errors = 0; + int cpu = smp_processor_id(); + + uint32_t result; + + printf("CPU%d online\n", cpu); + + for (i = 0 ; i < flush_count; i++) { + result = do_computation(); + + if (result != SEQ_HASH) { + errors++; + printf("CPU%d: seq%d 0x%"PRIx32"!=0x%x\n", + cpu, i, result, SEQ_HASH); + } + + if (flush_verbose && (i % 1000) == 0) + printf("CPU%d: seq%d\n", cpu, i); + + if (flush_self) + do_flush(i); + } + + report(errors == 0, "CPU%d: Done - Errors: %d\n", cpu, errors); + + cpumask_set_cpu(cpu, &smp_test_complete); + if (cpu != 0) + halt(); +} + +static void just_flush(void) +{ + int cpu = smp_processor_id(); + int i = 0; + + /* + * Set our CPU as done, keep flushing until everyone else + * finished + */ + cpumask_set_cpu(cpu, &smp_test_complete); + + while (!cpumask_full(&smp_test_complete)) + do_flush(i++); + + report_info("CPU%d: Done - Triggered %d flushes\n", cpu, i); +} + +int main(int argc, char **argv) +{ + int cpu, i; + char prefix[100]; + + for (i = 0; i < argc; i++) { + char *arg = argv[i]; + + if (strcmp(arg, "page") == 0) + flush_page = true; + + if (strcmp(arg, "self") == 0) + flush_self = true; + + if (strcmp(arg, "verbose") == 0) + flush_verbose = true; + } + + snprintf(prefix, sizeof(prefix), "tlbflush_%s_%s", + flush_page?"page":"all", + flush_self?"self":"other"); + report_prefix_push(prefix); + + for_each_present_cpu(cpu) { + if (cpu == 0) + continue; + smp_boot_secondary(cpu, just_compute); + } + + if (flush_self) + just_compute(); + else + just_flush(); + + while (!cpumask_full(&smp_test_complete)) + cpu_relax(); + + return report_summary(); +} diff --git a/arm/mttcgtests.cfg b/arm/mttcgtests.cfg new file mode 100644 index 0000000..d3ff102 --- /dev/null +++ b/arm/mttcgtests.cfg @@ -0,0 +1,30 @@ +############################################################################## +# MTTCG unit tests configuration +# +# These are torture tests for QEMU's Multi-threaded TCG (MTTCG) which +# aim to trigger various races in its emulation code. You can run them +# on a real system if you like but they shouldn't fail. +# +# See unittests.cfg for the file format +############################################################################## + +# TLB Torture Tests +[tlbflush-code::all_other] +file = tlbflush-code.flat +smp = $(($MAX_SMP>4?4:$MAX_SMP)) + +[tlbflush-code::page_other] +file = tlbflush-code.flat +smp = $(($MAX_SMP>4?4:$MAX_SMP)) +extra_params = -append 'page' + +[tlbflush-code::all_self] +file = tlbflush-code.flat +smp = $(($MAX_SMP>4?4:$MAX_SMP)) +extra_params = -append 'self' + +[tlbflush-code::page_self] +file = tlbflush-code.flat +smp = $(($MAX_SMP>4?4:$MAX_SMP)) +extra_params = -append 'page self' +