From patchwork Fri Nov 13 22:16:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 11904935 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 4BC531391 for ; Fri, 13 Nov 2020 22:18:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 046A92225B for ; Fri, 13 Nov 2020 22:17:59 +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="anvVrn5/" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 046A92225B 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 48E996B00C5; Fri, 13 Nov 2020 17:17:58 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 43DAB6B00C6; Fri, 13 Nov 2020 17:17:58 -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 32DF36B00C7; Fri, 13 Nov 2020 17:17:58 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0095.hostedemail.com [216.40.44.95]) by kanga.kvack.org (Postfix) with ESMTP id 026F06B00C5 for ; Fri, 13 Nov 2020 17:17:57 -0500 (EST) Received: from smtpin30.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A653B824999B for ; Fri, 13 Nov 2020 22:17:57 +0000 (UTC) X-FDA: 77480808594.30.boot46_1514fa727312 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin30.hostedemail.com (Postfix) with ESMTP id 830A6180B3C8B for ; Fri, 13 Nov 2020 22:17:57 +0000 (UTC) X-Spam-Summary: 1,0,0,56aa41f9ae2281eb,d41d8cd98f00b204,3faavxwokcn8boesfzlowmhpphmf.dpnmjovy-nnlwbdl.psh@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:7901:7904:8660:9969:10004:11026:11232:11473:11657:11658:11914:12043:12296:12297:12438:12555:12679:12694: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.128.73:@flex--andreyknvl.bounces.google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100;04yg14jij1qqgf3djow3w45ekdu6pop7oe1mu7p9f8ahpbht45q866emwuq5eo3.qtp86jhzhky95c1t93zotp518fcyn9zcqrf5ik74xf66yxh961dqzt5u71njo6j.s-lbl8.mailshell.net-223.238.255.100,CacheIP:non e,Bayesi X-HE-Tag: boot46_1514fa727312 X-Filterd-Recvd-Size: 8604 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf26.hostedemail.com (Postfix) with ESMTP for ; Fri, 13 Nov 2020 22:17:57 +0000 (UTC) Received: by mail-wm1-f73.google.com with SMTP id 14so4718671wmg.1 for ; Fri, 13 Nov 2020 14:17:56 -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=5qMij167ClOELbqeSBDKfuvF2LQUPsW2wggzZGec9i4=; b=anvVrn5/8taAIGD7eUO68rbUK3PzFWt1pPTNTJwfAYKOPr32Q3hG8PcMvkYFFNaZp7 rcaThcmF3dEr0kzDC3ZTXj+xu/jSq1AN8/CR/5G7qJmL3RkTfueraEf8UMv3VtIKm04V 7rSR8jMKHoY6wgMmeE+VdHB6rgAp53MsDEwWLKyafUdtaSqT0FmDMAPd/R90EfsVPO1Z tVvaW+L5YweNd86uWNujlNAnsk/9MAxO2AgXyJCXuPzcsplD9YDexmPSgu837bbr5FH2 DDP8ZUMrkwLlLwoUJBIaBccnqyEAdZKdr1HlpzF9gotclcid78LITN5o4vHclsQGrR1V nVog== 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=5qMij167ClOELbqeSBDKfuvF2LQUPsW2wggzZGec9i4=; b=A5Jq/zXCNlbMB7n0k+V+/xU8URoJE7O4CvZoH24a8DM03skzPVY6EiNpDu7ER+JSCa +lfKxN+1u6wRc7bJeS4aQ6fm0PEXTA7G/wJd8kbTExqABzlXLBOliEBG0dBzE61JCtS5 5plAKeLvgqEfUV+BZkG6bScKj6p0C19zF7Pl93Xpk096k2sWbHVo5um5Yq1vfUucDLHE +UqA62ddpu6arsZQ/+YMpVBEAIXvnR4cpc5QJ05mtgNdQzga/D7Z5bXwfkCDvIYEpvzr tTQBobYx0FeFZ6Q+1IMuEUkWdJ0INhGevsa300NFhz/qeFq66Opzmg3qhgR3rsbXPCIu xkRg== X-Gm-Message-State: AOAM531QRGElZUr6de9kTumGypxTq6DypnBxoXkiFjmxNDItnEZJejFM zymrOvUh6dIbg/hY2eed8VzKFGjAWMVt78ng X-Google-Smtp-Source: ABdhPJwkP9MccgMdI75jZGUWuaYRM3HxoeGVlxbVAQB3aBD27lrfQya4OWDfPows/YQTexgR9UG5Pm8UI7OxNpgM X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:adf:c58f:: with SMTP id m15mr6387961wrg.144.1605305876026; Fri, 13 Nov 2020 14:17:56 -0800 (PST) Date: Fri, 13 Nov 2020 23:16:10 +0100 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.2.299.gdc1121823c-goog Subject: [PATCH mm v10 42/42] kselftest/arm64: Check GCR_EL1 after context switch From: Andrey Konovalov To: Andrew Morton Cc: Catalin Marinas , Will Deacon , Vincenzo Frascino , Dmitry Vyukov , Andrey Ryabinin , Alexander Potapenko , Marco Elver , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , 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.000007, 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 spawns 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 | 155 ++++++++++++++++++ 2 files changed, 156 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..de5066aca097 --- /dev/null +++ b/tools/testing/selftests/arm64/mte/check_gcr_el1_cswitch.c @@ -0,0 +1,155 @@ +// 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(void) +{ + pid_t pid; + int results[NUM_ITERATIONS]; + pid_t cpid; + int res; + + for (int i = 0; i < NUM_ITERATIONS; i++) { + pid = fork(); + + if (pid < 0) + return KSFT_FAIL; + + if (pid == 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; +} +