From patchwork Wed Nov 4 23:18:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11883103 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 31912C4741F for ; Thu, 5 Nov 2020 04:14:52 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (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 9576F20732 for ; Thu, 5 Nov 2020 04:14:51 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="bzRmsCqn"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="vOn9vKeQ"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="coUp8usP" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9576F20732 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=J0NthRtFPW/ncThcWieMoZG1WhutwdETT6SKN5mlalU=; b=bzRmsCqnr2EbMEY86DeFN1wEh 32GIZjUGDQJ7G8+aa03pmYXL7ancFWaY1tkgrag2Gdw0tNF+6dQ1XFc/hxaxSkT7HPv1PEh/OTTQP Cb2/TgeCci2qXnVRc32WjJSXp87ASVgsrZ6W3WH44HcJtOj50X2dU2s0zuEURZOxD4qPQJFd1zcRy PFp7Zct0rC0d7lHuLk5gxGV2qQGjegmU1Qv+C1vY36VTqsbdBOwWITCZ18bHWGrd24AwsbNN9DlGP e0Q9Npp7CCYDYZqdOXECQePKnJdNogH/O5jnhHLzJOZsFtBNwwsv3FxutNHE/qm6zOw4MdWufI/OJ +Nzkaju+Q==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaWfB-0005Ky-Kx; Thu, 05 Nov 2020 04:14:17 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaW50-00016k-Fi for linux-arm-kernel@merlin.infradead.org; Thu, 05 Nov 2020 03:36:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:Cc:To:From:Subject: References:Mime-Version:Message-Id:In-Reply-To:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=DyRPETqC+6C7nrnJCiEiYsvRb/HkPFaD35372QKa3Vk=; b=vOn9vKeQW0gcI9aG/KJmbXG0oM C4o02ijcTmqTjXsGVxtMsOZ9wElX3NkdCZIhX4c9I7oXRHNg6VtrXCoE4065BoBM+yaAqY86PYQpF lSxAbDqJifz2Q8pNTQqv/59yxsHnW70potJ9gegXjSfGoFAGIZdp4rreIGQlaAh+wkyQCs7jp7/Km 3ZOfwce41VkUE+j3Epr437fvIAp4vAhyBENX/+k960TEu5c/AFcPdLuZa9KuWhDu4Snhg9v6DNg3O vm5GfZYa3182YXgF4Fmqcg32p8ol/i1ul0gk3yqOpDahKtPWExLRHrSJlr35C4mzO5Hl5kfWwpQNH TGeWm0bg==; Received: from mail-qk1-x749.google.com ([2607:f8b0:4864:20::749]) by casper.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kaS6C-0004Kv-Jl for linux-arm-kernel@lists.infradead.org; Wed, 04 Nov 2020 23:21:56 +0000 Received: by mail-qk1-x749.google.com with SMTP id x85so14576714qka.14 for ; Wed, 04 Nov 2020 15:21:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=DyRPETqC+6C7nrnJCiEiYsvRb/HkPFaD35372QKa3Vk=; b=coUp8usPwG3tRaZT/czB4IwaENO70bhZYwpPOVmFVE31+gzzFM1eQ4UTkXv5n/Cwx6 HmmXHf7e2Yv2iQl0c/eFCV0eDtFdt+aqYiFbij4bDazub10zadMUBMScNBApOgX2VA2M 1uhVz8MklwM7+riUMg/8+0j7Fi4Bh2qM9bLhWUK6ReMxodoSEFswpWi2kMmZ8Q87CHGp 2BTpkFvNtZjQ22eIAl9d9nUIQU3k/nacre7WR207ivGr7AWJqhJkTyuSxyS+Sa/AD+x3 fzw6/03tLAgh2Aj81Rl+TvGN20JUQneZ9H8VrtmnLsTSYR+SxpV+/eBt1Vr/7rs8v+p0 KW5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=DyRPETqC+6C7nrnJCiEiYsvRb/HkPFaD35372QKa3Vk=; b=PEP4yiNQq7ehjbzRQRr+v6o3ax2pagDzisSe6PzhKO5jk4eyxsnkshEeXovWvn92Kx +7mQdoukt2PyxW6VpgqMzodgMemEjfo2/OXnG8OY7Q+tYZqCE8M9gg9wBtVDXUa3Lzol o/nZgFamYp0r5Dasmw9xpT9MWMwBE7Vm4RoGeD1MbtHDo4NSUA0lOgnKFl3sLxGBns5r z35L15mWlgUC5ncwvdoXQZPG//zTp1WOi07SO7c4eTZ1j4QyrOQGxC8SPXGit4OrejO1 7+vNvLi2JvfOsJtVtpUYwY/pdf06NxduZ0ent5g86bEBRdnYEAtnJwz2ZDs3/0NawecC NXMA== X-Gm-Message-State: AOAM5323reExq8E4vq5Pb5BCaloYiqItMnX92cHAg3kUHnYmJtXHvJsr lVXaHK+5hM6M7Dg5OalaJ/CXX3MBIt5xyDaT X-Google-Smtp-Source: ABdhPJyr9TEssBPzFhk+PU6o+mtfwvl9/Tja+M6tO9w1ZHDXSL5HAtdDJPKKiiOU0eso9TeO61RiAye7zBP4ft8k X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a0c:9bda:: with SMTP id g26mr294283qvf.14.1604532050508; Wed, 04 Nov 2020 15:20:50 -0800 (PST) Date: Thu, 5 Nov 2020 00:18:58 +0100 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog Subject: [PATCH v8 43/43] kselftest/arm64: Check GCR_EL1 after context switch From: Andrey Konovalov To: Catalin Marinas X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201104_232153_068875_63AB4FAE X-CRM114-Status: GOOD ( 19.62 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-kernel@lists.infradead.org, Marco Elver , Andrey Konovalov , Kevin Brodsky , Will Deacon , Branislav Rankov , kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Alexander Potapenko , Evgenii Stepanov , Andrey Ryabinin , Andrew Morton , Vincenzo Frascino , Dmitry Vyukov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Vincenzo Frascino This test is specific to MTE and verifies that the GCR_EL1 register is context switched correctly. It spawn 1024 processes and each process spawns 5 threads. Each thread writes a random setting of GCR_EL1 through the prctl() system call and reads it back verifying that it is the same. If the values are not the same it reports a failure. Note: The test has been extended to verify that even SYNC and ASYNC mode setting is preserved correctly over context switching. Signed-off-by: Vincenzo Frascino Signed-off-by: Andrey Konovalov --- Change-Id: Ia917684a2b8e5f29e705ca5cbf360b010df6f61e --- tools/testing/selftests/arm64/mte/Makefile | 2 +- .../arm64/mte/check_gcr_el1_cswitch.c | 152 ++++++++++++++++++ 2 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/arm64/mte/check_gcr_el1_cswitch.c diff --git a/tools/testing/selftests/arm64/mte/Makefile b/tools/testing/selftests/arm64/mte/Makefile index 2480226dfe57..0b3af552632a 100644 --- a/tools/testing/selftests/arm64/mte/Makefile +++ b/tools/testing/selftests/arm64/mte/Makefile @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 # Copyright (C) 2020 ARM Limited -CFLAGS += -std=gnu99 -I. +CFLAGS += -std=gnu99 -I. -lpthread SRCS := $(filter-out mte_common_util.c,$(wildcard *.c)) PROGS := $(patsubst %.c,%,$(SRCS)) diff --git a/tools/testing/selftests/arm64/mte/check_gcr_el1_cswitch.c b/tools/testing/selftests/arm64/mte/check_gcr_el1_cswitch.c new file mode 100644 index 000000000000..55e33d96794c --- /dev/null +++ b/tools/testing/selftests/arm64/mte/check_gcr_el1_cswitch.c @@ -0,0 +1,152 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (C) 2020 ARM Limited + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "kselftest.h" +#include "mte_common_util.h" + +#define PR_SET_TAGGED_ADDR_CTRL 55 +#define PR_GET_TAGGED_ADDR_CTRL 56 +# define PR_TAGGED_ADDR_ENABLE (1UL << 0) +# define PR_MTE_TCF_SHIFT 1 +# define PR_MTE_TCF_NONE (0UL << PR_MTE_TCF_SHIFT) +# define PR_MTE_TCF_SYNC (1UL << PR_MTE_TCF_SHIFT) +# define PR_MTE_TCF_ASYNC (2UL << PR_MTE_TCF_SHIFT) +# define PR_MTE_TCF_MASK (3UL << PR_MTE_TCF_SHIFT) +# define PR_MTE_TAG_SHIFT 3 +# define PR_MTE_TAG_MASK (0xffffUL << PR_MTE_TAG_SHIFT) + +#include "mte_def.h" + +#define NUM_ITERATIONS 1024 +#define MAX_THREADS 5 +#define THREAD_ITERATIONS 1000 + +void *execute_thread(void *x) +{ + pid_t pid = *((pid_t *)x); + pid_t tid = gettid(); + uint64_t prctl_tag_mask; + uint64_t prctl_set; + uint64_t prctl_get; + uint64_t prctl_tcf; + + srand(time(NULL) ^ (pid << 16) ^ (tid << 16)); + + prctl_tag_mask = rand() % 0xffff; + + if (prctl_tag_mask % 2) + prctl_tcf = PR_MTE_TCF_SYNC; + else + prctl_tcf = PR_MTE_TCF_ASYNC; + + prctl_set = PR_TAGGED_ADDR_ENABLE | prctl_tcf | (prctl_tag_mask << PR_MTE_TAG_SHIFT); + + for (int j = 0; j < THREAD_ITERATIONS; j++) { + if (prctl(PR_SET_TAGGED_ADDR_CTRL, prctl_set, 0, 0, 0)) { + perror("prctl() failed"); + goto fail; + } + + prctl_get = prctl(PR_GET_TAGGED_ADDR_CTRL, 0, 0, 0, 0); + + if (prctl_set != prctl_get) { + ksft_print_msg("Error: prctl_set: 0x%lx != prctl_get: 0x%lx\n", + prctl_set, prctl_get); + goto fail; + } + } + + return (void *)KSFT_PASS; + +fail: + return (void *)KSFT_FAIL; +} + +int execute_test(pid_t pid) +{ + pthread_t thread_id[MAX_THREADS]; + int thread_data[MAX_THREADS]; + + for (int i = 0; i < MAX_THREADS; i++) + pthread_create(&thread_id[i], NULL, + execute_thread, (void *)&pid); + + for (int i = 0; i < MAX_THREADS; i++) + pthread_join(thread_id[i], (void *)&thread_data[i]); + + for (int i = 0; i < MAX_THREADS; i++) + if (thread_data[i] == KSFT_FAIL) + return KSFT_FAIL; + + return KSFT_PASS; +} + +int mte_gcr_fork_test() +{ + pid_t pid[NUM_ITERATIONS]; + int results[NUM_ITERATIONS]; + pid_t cpid; + int res; + + for (int i = 0; i < NUM_ITERATIONS; i++) { + pid[i] = fork(); + + if (pid[i] == 0) { + cpid = getpid(); + + res = execute_test(cpid); + + exit(res); + } + } + + for (int i = 0; i < NUM_ITERATIONS; i++) { + wait(&res); + + if(WIFEXITED(res)) + results[i] = WEXITSTATUS(res); + else + --i; + } + + for (int i = 0; i < NUM_ITERATIONS; i++) + if (results[i] == KSFT_FAIL) + return KSFT_FAIL; + + return KSFT_PASS; +} + +int main(int argc, char *argv[]) +{ + int err; + + err = mte_default_setup(); + if (err) + return err; + + ksft_set_plan(1); + + evaluate_test(mte_gcr_fork_test(), + "Verify that GCR_EL1 is set correctly on context switch\n"); + + mte_restore_setup(); + ksft_print_cnts(); + + return ksft_get_fail_cnt() == 0 ? KSFT_PASS : KSFT_FAIL; +} +