Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp5882911ybi; Wed, 12 Jun 2019 10:03:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqyLRaCkm0ZxpIGCqlKeBBLtL7r0nwbba0JIC9mNlZh6PLQjOxEtVy9xKZrPvcfQLsov56+Q X-Received: by 2002:a17:902:110b:: with SMTP id d11mr10435872pla.213.1560359025564; Wed, 12 Jun 2019 10:03:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1560359025; cv=none; d=google.com; s=arc-20160816; b=IGVj2nln/UtqIIE5EF9gztqQVVM5CIDdjiCXpDDWlwY8cxPkv9tL41HfD1twt8PIFl p4A32ym2SM7p8IXWkJm9XVblpz4s+IO4uc0+0/thqpB36RGTKTBzLq+T4ibc+7gL0se0 n3r3IqNcBOL2C0ZJSIYiL36mpDUJ3aW4pKimeGAEYB8Bs71dSTC4VzUqJmzAR+VIUlsU p/eBv0fgdGpGrkmlYdcm+dJTSF/TNaN4HlaDjkeSQ9sOMXcOCtozf0P/rMe+o4PtqDmx LIWb8Ty7cQA055D4Wv9YhFCIf+w7d6nA1txKRKSo9enF5owp2N7U+ds9OuuIHBNTU2gh ImKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=gGgZ3a6eTnEUj++uQZqNg8ohQ9SAhJYSqDRRhc7mNEI=; b=i2cV158IaNa6iD2As3WfSqCrsREGCTaLuqqe+SZndJ74zhPz9Mbv1IQgmRttDTzNLk RtvwPjr3o6wn8J5AXGdE9px50Sly5drV4AwIUUu+M+xKqTkNADINqelBjUh6xAFeEMyd Xfo43Dc7DthJuiersswL17xV+gkB6ilH6YhEsKJOPVgwuTh0eACyJ9Gdf4nHFPS4nlp8 OGEa/7xTger/NyyiEd0dD/WnNy1ASCwdC1f7oO6XG7pKl64DP5a//vxzJuXq8IEZGGin rN6TiilAN/Yssw0BaS47QCt8hRQxtewjaPX7vRTOv9sjWm0Xb2IxmFAo+bCoKEZ9EeC/ ReFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=IyYT4Uba; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v71si285409pgd.417.2019.06.12.10.03.30; Wed, 12 Jun 2019 10:03:45 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=IyYT4Uba; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2438586AbfFLLoc (ORCPT + 99 others); Wed, 12 Jun 2019 07:44:32 -0400 Received: from mail-yb1-f202.google.com ([209.85.219.202]:53545 "EHLO mail-yb1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2438556AbfFLLoZ (ORCPT ); Wed, 12 Jun 2019 07:44:25 -0400 Received: by mail-yb1-f202.google.com with SMTP id e193so11834977ybf.20 for ; Wed, 12 Jun 2019 04:44:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=gGgZ3a6eTnEUj++uQZqNg8ohQ9SAhJYSqDRRhc7mNEI=; b=IyYT4UbafS5twM0xOLKRaXywnDFDdbDqJgnUILJhttHP1uH2L+NNxbnxVgYAee8lTj qqRpjg9Px1/VzqAAM6upfA449si21/4lJ1XkboyBr77Ub0YTrQLbJn5sOIwz1IVA69V5 Ap8ARpNEkRP6VF2Emm63QaD9Cx3fvDSKZaN/HHC/Xocn6hv1419HlST6JCQQ1Z5EHlL/ CiJ+tHOg0rDi38FsnXC87324szrGfxAi8RWcHgDsH+2gfi2ulhbLZaNJ0gAKDJ3X8Gkg JxyfovuFPvVfND0iTfX5ByU+bmOloPTC5uiR6JL5swcvm2UzXNQY3tCHhrlAltKj98wk GeRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=gGgZ3a6eTnEUj++uQZqNg8ohQ9SAhJYSqDRRhc7mNEI=; b=OzXElmigAIs0W4DcavhJKpcgE9xe8tgQfUGtUCPKtAPkUXKR/MTMPv45wVod94uYbw ftR4I+NCEYY5s7QZk3OKagweNRDZlatgR8Fj9g5xNyBiDmc9Nm50NLCfIJV2eWvzaUMb ZjNeuf/xPEVnwkkYSmbQrd3uI+mRGtCufusWsCjjLtRM6WTY7qAaVsNgipeTRu2CahkM FpikPDwCKVw49RjtAp+3SOCIQ2m0Dcv8Q4XL5s3hHc621CjnaYZCX/u6M6V2vOrE0gvC 1NLPwnOhZSoGBJxz5bSTyf9b+ks+nl5DoqSoFy2vBe5SM4m7QvgUCZqEt931+qgBL0F/ VJkg== X-Gm-Message-State: APjAAAVaYH4UU/1bjjbJ3bNT/k3k3S2brl234txck8bcfsb4k0AcDJCG vBRRpOtHOXGxMI+MFEIjbpjT58ahLn50l6Xr X-Received: by 2002:a81:6d46:: with SMTP id i67mr906534ywc.103.1560339863971; Wed, 12 Jun 2019 04:44:23 -0700 (PDT) Date: Wed, 12 Jun 2019 13:43:32 +0200 In-Reply-To: Message-Id: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.22.0.rc2.383.gf4fbbf30c2-goog Subject: [PATCH v17 15/15] selftests, arm64: add a selftest for passing tagged pointers to kernel From: Andrey Konovalov To: linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-rdma@vger.kernel.org, linux-media@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: Catalin Marinas , Vincenzo Frascino , Will Deacon , Mark Rutland , Andrew Morton , Greg Kroah-Hartman , Kees Cook , Yishai Hadas , Felix Kuehling , Alexander Deucher , Christian Koenig , Mauro Carvalho Chehab , Jens Wiklander , Alex Williamson , Leon Romanovsky , Luc Van Oostenryck , Dave Martin , Khalid Aziz , enh , Jason Gunthorpe , Christoph Hellwig , Dmitry Vyukov , Kostya Serebryany , Evgeniy Stepanov , Lee Smith , Ramana Radhakrishnan , Jacob Bramley , Ruben Ayrapetyan , Robin Murphy , Kevin Brodsky , Szabolcs Nagy , Andrey Konovalov Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch is a part of a series that extends arm64 kernel ABI to allow to pass tagged user pointers (with the top byte set to something else other than 0x00) as syscall arguments. This patch adds a simple test, that calls the uname syscall with a tagged user pointer as an argument. Without the kernel accepting tagged user pointers the test fails with EFAULT. Co-developed-by: Catalin Marinas Signed-off-by: Andrey Konovalov --- tools/testing/selftests/arm64/.gitignore | 2 + tools/testing/selftests/arm64/Makefile | 22 +++++++ .../testing/selftests/arm64/run_tags_test.sh | 12 ++++ tools/testing/selftests/arm64/tags_lib.c | 62 +++++++++++++++++++ tools/testing/selftests/arm64/tags_test.c | 18 ++++++ 5 files changed, 116 insertions(+) create mode 100644 tools/testing/selftests/arm64/.gitignore create mode 100644 tools/testing/selftests/arm64/Makefile create mode 100755 tools/testing/selftests/arm64/run_tags_test.sh create mode 100644 tools/testing/selftests/arm64/tags_lib.c create mode 100644 tools/testing/selftests/arm64/tags_test.c diff --git a/tools/testing/selftests/arm64/.gitignore b/tools/testing/selftests/arm64/.gitignore new file mode 100644 index 000000000000..9b6a568de17f --- /dev/null +++ b/tools/testing/selftests/arm64/.gitignore @@ -0,0 +1,2 @@ +tags_test +tags_lib.so diff --git a/tools/testing/selftests/arm64/Makefile b/tools/testing/selftests/arm64/Makefile new file mode 100644 index 000000000000..9dee18727923 --- /dev/null +++ b/tools/testing/selftests/arm64/Makefile @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: GPL-2.0 + +include ../lib.mk + +# ARCH can be overridden by the user for cross compiling +ARCH ?= $(shell uname -m 2>/dev/null || echo not) + +ifneq (,$(filter $(ARCH),aarch64 arm64)) + +TEST_CUSTOM_PROGS := $(OUTPUT)/tags_test + +$(OUTPUT)/tags_test: tags_test.c $(OUTPUT)/tags_lib.so + $(CC) -o $@ $(CFLAGS) $(LDFLAGS) $< + +$(OUTPUT)/tags_lib.so: tags_lib.c + $(CC) -o $@ -shared $(CFLAGS) $(LDFLAGS) $^ + +TEST_PROGS := run_tags_test.sh + +all: $(TEST_CUSTOM_PROGS) + +endif diff --git a/tools/testing/selftests/arm64/run_tags_test.sh b/tools/testing/selftests/arm64/run_tags_test.sh new file mode 100755 index 000000000000..2bbe0cd4220b --- /dev/null +++ b/tools/testing/selftests/arm64/run_tags_test.sh @@ -0,0 +1,12 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +echo "--------------------" +echo "running tags test" +echo "--------------------" +LD_PRELOAD=./tags_lib.so ./tags_test +if [ $? -ne 0 ]; then + echo "[FAIL]" +else + echo "[PASS]" +fi diff --git a/tools/testing/selftests/arm64/tags_lib.c b/tools/testing/selftests/arm64/tags_lib.c new file mode 100644 index 000000000000..55f64fc1aae6 --- /dev/null +++ b/tools/testing/selftests/arm64/tags_lib.c @@ -0,0 +1,62 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include + +#define TAG_SHIFT (56) +#define TAG_MASK (0xffUL << TAG_SHIFT) + +#define PR_SET_TAGGED_ADDR_CTRL 55 +#define PR_GET_TAGGED_ADDR_CTRL 56 +#define PR_TAGGED_ADDR_ENABLE (1UL << 0) + +void *__libc_malloc(size_t size); +void __libc_free(void *ptr); +void *__libc_realloc(void *ptr, size_t size); +void *__libc_calloc(size_t nmemb, size_t size); + +static void *tag_ptr(void *ptr) +{ + static int tagged_addr_err = 1; + unsigned long tag = 0; + + /* + * Note that this code is racy. We only use it as a part of a single + * threaded test application. Beware of using in multithreaded ones. + */ + if (tagged_addr_err == 1) + tagged_addr_err = prctl(PR_SET_TAGGED_ADDR_CTRL, + PR_TAGGED_ADDR_ENABLE, 0, 0, 0); + + if (!ptr) + return ptr; + if (!tagged_addr_err) + tag = rand() & 0xff; + + return (void *)((unsigned long)ptr | (tag << TAG_SHIFT)); +} + +static void *untag_ptr(void *ptr) +{ + return (void *)((unsigned long)ptr & ~TAG_MASK); +} + +void *malloc(size_t size) +{ + return tag_ptr(__libc_malloc(size)); +} + +void free(void *ptr) +{ + __libc_free(untag_ptr(ptr)); +} + +void *realloc(void *ptr, size_t size) +{ + return tag_ptr(__libc_realloc(untag_ptr(ptr), size)); +} + +void *calloc(size_t nmemb, size_t size) +{ + return tag_ptr(__libc_calloc(nmemb, size)); +} diff --git a/tools/testing/selftests/arm64/tags_test.c b/tools/testing/selftests/arm64/tags_test.c new file mode 100644 index 000000000000..263b302874ed --- /dev/null +++ b/tools/testing/selftests/arm64/tags_test.c @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include +#include +#include + +int main(void) +{ + struct utsname *ptr; + int err; + + ptr = (struct utsname *)malloc(sizeof(*ptr)); + err = uname(ptr); + free(ptr); + return err; +} -- 2.22.0.rc2.383.gf4fbbf30c2-goog