From patchwork Tue Nov 10 22:10:41 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11895593 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3D10415E6 for ; Tue, 10 Nov 2020 22:12:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DE9FB20674 for ; Tue, 10 Nov 2020 22:12:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="sn0Rmbep" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DE9FB20674 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 94B9A6B00C9; Tue, 10 Nov 2020 17:12:54 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8FD826B00CA; Tue, 10 Nov 2020 17:12:54 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 79C6D6B00CB; Tue, 10 Nov 2020 17:12:54 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0227.hostedemail.com [216.40.44.227]) by kanga.kvack.org (Postfix) with ESMTP id 4C40E6B00C9 for ; Tue, 10 Nov 2020 17:12:54 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id EEF10362B for ; Tue, 10 Nov 2020 22:12:53 +0000 (UTC) X-FDA: 77469909426.12.able36_3412c1b272f8 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id C6DCD18014AF7 for ; Tue, 10 Nov 2020 22:12:53 +0000 (UTC) X-Spam-Summary: 1,0,0,0eac5a5276518758,d41d8cd98f00b204,3zbcrxwokcdureuivpbemcxffxcv.tfdczelo-ddbmrtb.fix@flex--andreyknvl.bounces.google.com,,RULES_HIT:2:41:152:355:379:541:800:960:973:988:989:1260:1277:1313:1314:1345:1359:1431:1437:1516:1518:1535:1593:1594:1605:1606:1730:1747:1777:1792:2393:2538:2559:2562:2899:2915:3138:3139:3140:3141:3142:3152:3865:3866:3867:3868:3871:3872:3874:4119:4250:4321:4384:4605:5007:6117:6261:6609:6653:6742:7576:7904:8660:9969:10004:11026:11232:11473:11657:11658:11914:12043:12296:12297:12438:12555:12679:12701:12737:12895:12986:13148:13180:13229:13230:13972:14394:14659:14877:21080:21324:21365:21433:21444:21451:21524:21627:21939:21990:30054:30056:30067:30069:30070,0,RBL:209.85.221.73:@flex--andreyknvl.bounces.google.com:.lbl8.mailshell.net-62.18.0.100 66.100.201.100;04yg3ke5iioeedapanyiopkf6n54iycr6966sohy38o35uss57q866emwuq5eo3.qtp86jhzhky95c1tq3dgn4wfffyjjwy6hj45ik74xf66bczd8whobwcmytuzikj.e-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian: 0.5,0.5, X-HE-Tag: able36_3412c1b272f8 X-Filterd-Recvd-Size: 8522 Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf14.hostedemail.com (Postfix) with ESMTP for ; Tue, 10 Nov 2020 22:12:53 +0000 (UTC) Received: by mail-wr1-f73.google.com with SMTP id h11so6171274wrq.20 for ; Tue, 10 Nov 2020 14:12:53 -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=gElbIAirXSeZEu+ZxUvh6N5W/Nh8clkwQRgJHSlXRug=; b=sn0RmbepjOdoD/GNs2otWI2t/10RzM8DrRj2gzOwFn5x8lfx/VAc+H1IbcZuHEaGZM ffyPIShyj/5GlQOPi+ya+ePlMqfyRXXzlg0A/YhlVLw/Ba/GHL7ObCgvZeUOhyZawTH5 qkyj5NX674SxfgWBwaALTN7mEGgcji1qFXipNlaK4QbYlIereSjpfTjrpUfk3Ga7lV1L UraVWCDYykR337WyGkaYRWfTV1KS+Hnt6xP2LYnhYx5ygpRen5U7rCu1I5iDlmS5OLHh 5pvMYf+3MlAcw/IueDCCQ+JxD8gNvIx6lPggbmlhuqEvUNrkzKnQhLnRF3maMEkLRhpd KK3A== 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=gElbIAirXSeZEu+ZxUvh6N5W/Nh8clkwQRgJHSlXRug=; b=swz/wvcBjECdV+sWVNfRrEEkhSJXjuMiQnnusKn5KXeSLjWjOgXDbQ4O59jxB+kVBE OxuAVPLOm082lHvD1Kk0GVv2dRa87g+JqT3VcAdNEmH5BiVDjUwgPBaX0+xtO8KcWHfU /9flvWDj9NG2QEivSGiuOAcRxH4gkcvaulUoZXHS6UMcCw7dZLdI5ClUvoGvly0cLmhR /vNg+BOJKK7cJ8/CB9/FUhR6REtit9FzEPbxfwUgjIa5P7LvCrq/u7VQHLtfgtqnGQVA 3Zm0+blo2s+mMD2TrC9hJfbPElAxz4cPI46ykShz1K+Iaw/qKL4heuKivI3brU8sDJYg mc3g== X-Gm-Message-State: AOAM5339kwLoslacTozeVLV1y+LO5tNNXXOMaB8dx9y/BFUSmqyrAdj5 j5btQ3eNekXFDaQfBXn8eqD7Bj533kdq5yB5 X-Google-Smtp-Source: ABdhPJxTJbnNO0DFMT0MGGDz7gNKaSPg0L0LTnm2p/mSpC2JkjHHPh9OpKOTwY+Z3rlHfL6W/JdlFgNWjXbov/s/ X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a7b:c1d2:: with SMTP id a18mr251913wmj.41.1605046372211; Tue, 10 Nov 2020 14:12:52 -0800 (PST) Date: Tue, 10 Nov 2020 23:10:41 +0100 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.222.g5d2a92d10f8-goog Subject: [PATCH v9 44/44] kselftest/arm64: Check GCR_EL1 after context switch From: Andrey Konovalov To: Catalin Marinas Cc: Will Deacon , Vincenzo Frascino , Dmitry Vyukov , Andrey Ryabinin , Alexander Potapenko , Marco Elver , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , Andrew Morton , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov X-Bogosity: Ham, tests=bogofilter, spamicity=0.000031, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 Acked-by: Catalin Marinas --- 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; +} +