From patchwork Mon Jan 22 18:52: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: 864371 Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zQMPk2Wt5z9ryk for ; Tue, 23 Jan 2018 06:46:34 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="A+f1qaxU"; dkim-atps=neutral Received: from bilbo.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 3zQMPk0n4mzDqTc for ; Tue, 23 Jan 2018 06:46:34 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="A+f1qaxU"; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gmail.com (client-ip=2607:f8b0:400d:c0d::242; helo=mail-qt0-x242.google.com; envelope-from=ram.n.pai@gmail.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="A+f1qaxU"; dkim-atps=neutral Received: from mail-qt0-x242.google.com (mail-qt0-x242.google.com [IPv6:2607:f8b0:400d:c0d::242]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 3zQLDY3PcczF0Sd for ; Tue, 23 Jan 2018 05:53:33 +1100 (AEDT) Received: by mail-qt0-x242.google.com with SMTP id s3so23455970qtb.10 for ; Mon, 22 Jan 2018 10:53:33 -0800 (PST) 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=Y6U7pUJ7NDlstMWM3djZSb7F9da877mbcC9sr3ZA5pU=; b=A+f1qaxUpMCoRfpYOn27z871qNcY6HF2bNH+sovI0GcN86WL62n+MiFzHCV4+bAtqq hOtjTT7/LNx06FJOY5P70/1QkYPMzCzN9PMGfoHWJI33F8jMGLMc8qI30Z9v79BGYdnR FmQM86tsJZ6sRGyPVPVyDAHEABZU4h+9UIQlVnAr+SdSG3a5qucakRx1vdeHSq2FRSK6 3/IVbgPlNNrWX8/PJQbhKwAHYlJK1qp/KXqpHawyBKwy8PrNw3uViV0SFI9E6cBH1Q84 dDrjhp4WHHdgAREPasLN8naimLiP7/MC+8kQQYOyIiTAq+weD+sCmiz0zzSt7qdQcbRv hAtQ== 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=Y6U7pUJ7NDlstMWM3djZSb7F9da877mbcC9sr3ZA5pU=; b=OqByvKC/jLitsgN2srleDgtYiVGMHxyozKqMejqyYHxa5U8xJsNi/hIDhzPbHATssR zk92zCTTQ9Cp/rOAw/dRABvHwE6H5whsthJoEPs8dHQEz0ygrzyoqfc1FV8u84LWtYDT tnH1iMugm12dsH8GAjZDCiRjTy3K1gkHZeGqcu/BxPXwvZ311/qm2eTfKJq6mMQpAFT/ Sb2iO2W1JZ+uTcZoP9KwNytck3rExwMvDx9/x1pHJixNUtGEXYS/DFtfmI1B1BqLyy61 hFdm3UPA+hCldRmLTMk/CAM2UnSm2g86VWBMVT5JeyVJ2bNcjGi3CNqbF6OHPJ4SBylc OhFw== X-Gm-Message-State: AKwxytf18SrtnrwuNR1O+hgLiJ23+iLQYz6tzsXW66KwQGpKLWHRvpQS BmVz0Cw3IsKadoLdZ6/9Oms= X-Google-Smtp-Source: AH8x227rS3/p5Bf4ol36/QC3zdSxRdbg5n2ddhWN7ureW7kbwOftfjQaVYtAa4iaIge9hRhBjguztg== X-Received: by 10.237.45.225 with SMTP id i88mr11813904qtd.196.1516647211606; Mon, 22 Jan 2018 10:53:31 -0800 (PST) Received: from localhost.localdomain (50-39-100-161.bvtn.or.frontiernet.net. [50.39.100.161]) by smtp.gmail.com with ESMTPSA id o20sm10790061qkl.14.2018.01.22.10.53.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Jan 2018 10:53:31 -0800 (PST) From: Ram Pai To: shuahkh@osg.samsung.com, linux-kselftest@vger.kernel.org Subject: [PATCH v10 15/24] selftests/vm: powerpc implementation to check support for pkey Date: Mon, 22 Jan 2018 10:52:08 -0800 Message-Id: <1516647137-11174-16-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1516647137-11174-1-git-send-email-linuxram@us.ibm.com> References: <1516647137-11174-1-git-send-email-linuxram@us.ibm.com> X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, ebiederm@xmission.com, arnd@arndb.de, linux-doc@vger.kernel.org, x86@kernel.org, dave.hansen@intel.com, linux-kernel@vger.kernel.org, linuxram@us.ibm.com, mhocko@kernel.org, linux-mm@kvack.org, mingo@redhat.com, paulus@samba.org, aneesh.kumar@linux.vnet.ibm.com, bauerman@linux.vnet.ibm.com, akpm@linux-foundation.org, linuxppc-dev@lists.ozlabs.org, khandual@linux.vnet.ibm.com Errors-To: linuxppc-dev-bounces+patchwork-incoming=ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" pkey subsystem is supported if the hardware and kernel has support. We determine that by checking if allocation of a key succeeds or not. Signed-off-by: Ram Pai --- tools/testing/selftests/vm/pkey-helpers.h | 22 ++++++++++++++++------ tools/testing/selftests/vm/protection_keys.c | 9 +++++---- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/vm/pkey-helpers.h b/tools/testing/selftests/vm/pkey-helpers.h index cfc1a18..3559527 100644 --- a/tools/testing/selftests/vm/pkey-helpers.h +++ b/tools/testing/selftests/vm/pkey-helpers.h @@ -261,7 +261,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; @@ -274,13 +274,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) @@ -326,9 +326,19 @@ static inline void __page_o_noops(void) #elif __powerpc64__ /* arch */ #define PAGE_SIZE (0x1UL << 16) -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; } /* 8-bytes of instruction * 16384bytes = 1 page */ diff --git a/tools/testing/selftests/vm/protection_keys.c b/tools/testing/selftests/vm/protection_keys.c index d1cbdfe..65e6dd6 100644 --- a/tools/testing/selftests/vm/protection_keys.c +++ b/tools/testing/selftests/vm/protection_keys.c @@ -1299,8 +1299,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; } @@ -1362,12 +1362,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;