From patchwork Thu Jun 14 00:45:08 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ram Pai X-Patchwork-Id: 10463077 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2A96A60532 for ; Thu, 14 Jun 2018 00:47:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1C2B928988 for ; Thu, 14 Jun 2018 00:47:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 10C5428BA8; Thu, 14 Jun 2018 00:47:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9739728B9D for ; Thu, 14 Jun 2018 00:47:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S936014AbeFNArU (ORCPT ); Wed, 13 Jun 2018 20:47:20 -0400 Received: from mail-qk0-f194.google.com ([209.85.220.194]:42147 "EHLO mail-qk0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S936027AbeFNArT (ORCPT ); Wed, 13 Jun 2018 20:47:19 -0400 Received: by mail-qk0-f194.google.com with SMTP id j80-v6so2690204qke.9; Wed, 13 Jun 2018 17:47:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=pijILdgMLnQtvi0ETCeqbqJ8yE5z7l6/T13tWuI7bLM=; b=iTP1fSmJR2QnDzE8HPsVHPs83TB+kxyVrG34OBQ/Nn8EzDvQkh+yViV7znqM1UGvzd mbBQsOefXZyBHQjeEq5bwD7R1FY5iu4IV6TBQVrl+gG9EYgDIYCib7I74DyJ1SfdtvHj A+8/e8tcqW8q9QqduetnFpq6aQoW70tmmFObXY6dlQS87Jmer1NZAQvJuoyrScJy/TBn jZrzGA3OyLlrqqfJAaBki3bIVSY/UeXmdK7iTiNYezt+i2kOB8Cck0Xm3udBTr0VKUpD 2lSPsm3DtX0jZvwCtQhST3zGNjo7uRofbZVaEzq8t/SSaFUJauhsmt1SlhfOSrzHoTqC xIZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=pijILdgMLnQtvi0ETCeqbqJ8yE5z7l6/T13tWuI7bLM=; b=stJZfo6LLklhcQv8DD++x6JiEqYqIpzmC/jMRxrrYy446EZVDdSz4gjhx79gjnzIfX 7librcH4G6JVBj9QDkXFyBvSbHbWaw4uwlLFTLd+zQnD7XYpxGN5HEVRCwMUjgt8LwfF W4dCFjLHm7C2o8mvd/HQWE5pfo2lo1Bep6B8L5iTm3g9iojxiJNEoLnlgIW9gRc3gYl7 6wWlvxTdKBEd9jvyqMouPYfWiCpuQDyvEqnHryqriodwMCKFN3csRX83ChJA93cBEeos 6oWwAl2+XRLEf/Ej04wwxpqev1hvv2WFoMpyOGL+p+CizynFhwBhCnbV1V5R4ghVEb60 c+iQ== X-Gm-Message-State: APt69E3/EaHf0jvbr4CQhSxkelJibIpFm2l2n8CAR8k5vGaoBjPewNu2 a88mn8DaqKzfqf9fAYzjZ2w= X-Google-Smtp-Source: ADUXVKIUhKLpLFqPyEVuFQMMpjoAEN0R7bRXjvWddQ5nRpY/VQsLzE//FAz9BnxR353vw6j5CF6l3A== X-Received: by 2002:a37:8fc3:: with SMTP id r186-v6mr352677qkd.27.1528937238383; Wed, 13 Jun 2018 17:47:18 -0700 (PDT) Received: from localhost.localdomain (50-39-100-161.bvtn.or.frontiernet.net. [50.39.100.161]) by smtp.gmail.com with ESMTPSA id u31-v6sm3225898qtc.28.2018.06.13.17.47.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Jun 2018 17:47:17 -0700 (PDT) From: Ram Pai To: shuahkh@osg.samsung.com, linux-kselftest@vger.kernel.org Cc: mpe@ellerman.id.au, linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, x86@kernel.org, linux-arch@vger.kernel.org, mingo@redhat.com, dave.hansen@intel.com, mhocko@kernel.org, bauerman@linux.vnet.ibm.com, linuxram@us.ibm.com, fweimer@redhat.com, msuchanek@suse.de, aneesh.kumar@linux.vnet.ibm.com Subject: [PATCH v13 17/24] selftests/vm: powerpc implementation to check support for pkey Date: Wed, 13 Jun 2018 17:45:08 -0700 Message-Id: <1528937115-10132-18-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1528937115-10132-1-git-send-email-linuxram@us.ibm.com> References: <1528937115-10132-1-git-send-email-linuxram@us.ibm.com> Sender: linux-kselftest-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP pkey subsystem is supported if the hardware and kernel has support. We determine that by checking if allocation of a key succeeds or not. cc: Dave Hansen cc: Florian Weimer Signed-off-by: Ram Pai --- tools/testing/selftests/vm/pkey-helpers.h | 2 ++ tools/testing/selftests/vm/pkey-powerpc.h | 14 ++++++++++++-- tools/testing/selftests/vm/pkey-x86.h | 8 ++++---- tools/testing/selftests/vm/protection_keys.c | 9 +++++---- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/vm/pkey-helpers.h b/tools/testing/selftests/vm/pkey-helpers.h index 321bbbd..288ccff 100644 --- a/tools/testing/selftests/vm/pkey-helpers.h +++ b/tools/testing/selftests/vm/pkey-helpers.h @@ -76,6 +76,8 @@ static inline void sigsafe_printf(const char *format, ...) __attribute__((noinline)) int read_ptr(int *ptr); void expected_pkey_fault(int pkey); +int sys_pkey_alloc(unsigned long flags, u64 init_val); +int sys_pkey_free(unsigned long pkey); #if defined(__i386__) || defined(__x86_64__) /* arch */ #include "pkey-x86.h" diff --git a/tools/testing/selftests/vm/pkey-powerpc.h b/tools/testing/selftests/vm/pkey-powerpc.h index ec6f5d7..957f6f6 100644 --- a/tools/testing/selftests/vm/pkey-powerpc.h +++ b/tools/testing/selftests/vm/pkey-powerpc.h @@ -62,9 +62,19 @@ static inline void __write_pkey_reg(pkey_reg_t pkey_reg) pkey_reg); } -static inline int cpu_has_pku(void) +static inline bool is_pkey_supported(void) { - return 1; + /* + * No simple way to determine this. + * Lets try allocating a key and see if it succeeds. + */ + int ret = sys_pkey_alloc(0, 0); + + if (ret > 0) { + sys_pkey_free(ret); + return true; + } + return false; } static inline int arch_reserved_keys(void) diff --git a/tools/testing/selftests/vm/pkey-x86.h b/tools/testing/selftests/vm/pkey-x86.h index 95ee952..6820c10 100644 --- a/tools/testing/selftests/vm/pkey-x86.h +++ b/tools/testing/selftests/vm/pkey-x86.h @@ -105,7 +105,7 @@ static inline void __cpuid(unsigned int *eax, unsigned int *ebx, #define X86_FEATURE_PKU (1<<3) /* Protection Keys for Userspace */ #define X86_FEATURE_OSPKE (1<<4) /* OS Protection Keys Enable */ -static inline int cpu_has_pku(void) +static inline bool is_pkey_supported(void) { unsigned int eax; unsigned int ebx; @@ -118,13 +118,13 @@ static inline int cpu_has_pku(void) if (!(ecx & X86_FEATURE_PKU)) { dprintf2("cpu does not have PKU\n"); - return 0; + return false; } if (!(ecx & X86_FEATURE_OSPKE)) { dprintf2("cpu does not have OSPKE\n"); - return 0; + return false; } - return 1; + return true; } #define XSTATE_PKEY_BIT (9) diff --git a/tools/testing/selftests/vm/protection_keys.c b/tools/testing/selftests/vm/protection_keys.c index ba184ca..cb81a47 100644 --- a/tools/testing/selftests/vm/protection_keys.c +++ b/tools/testing/selftests/vm/protection_keys.c @@ -1393,8 +1393,8 @@ void test_mprotect_pkey_on_unsupported_cpu(int *ptr, u16 pkey) int size = PAGE_SIZE; int sret; - if (cpu_has_pku()) { - dprintf1("SKIP: %s: no CPU support\n", __func__); + if (is_pkey_supported()) { + dprintf1("SKIP: %s: no CPU/kernel support\n", __func__); return; } @@ -1458,12 +1458,13 @@ void run_tests_once(void) int main(void) { int nr_iterations = 22; + int pkey_supported = is_pkey_supported(); setup_handlers(); - printf("has pkey: %d\n", cpu_has_pku()); + printf("has pkey: %s\n", pkey_supported ? "Yes" : "No"); - if (!cpu_has_pku()) { + if (!pkey_supported) { int size = PAGE_SIZE; int *ptr;