Received: by 10.223.185.116 with SMTP id b49csp1380373wrg; Wed, 21 Feb 2018 18:00:13 -0800 (PST) X-Google-Smtp-Source: AH8x224BDpFDP8BjRSZZ1sNQNKS28iIAyPSSd1sSZnN0LGTmvvsxqXby+KyinLhyJ5yQe5MLwHjj X-Received: by 10.101.89.6 with SMTP id f6mr4335048pgu.22.1519264813177; Wed, 21 Feb 2018 18:00:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519264813; cv=none; d=google.com; s=arc-20160816; b=q0GLnZyedV15nqd8cJHEIk2U+GkhtPX0MJ8kCsGoZhNuzVnrnOH2gESTm5PIXwpkj6 M437P/zQ+/ur1+MD6x/lUA4DLdrdsFeEd4Bix/UQj4a2EImDGVSWTQRKwVMooaieyn5x MQtcFdzhuARfzLIB7uQaj7JOK0q4GRGeI4Lty1hwxOfdtgEKJhiGYXjQnzWJvEj23uD5 7XIzzwj75CVVftdqGBnA47KUcszM4/33aOoqLOFUrN2AoGRcsdIlvzOu94pJpKpYymfE b8VarqZvlnTJYaI5WNFQsYN9GWoVLaXYrjN4JIpw/4mmExFAV50ea8SMQXETlV0xrZUp 5gKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=/cVWF8CB+MbOSFGyeONRyJ08SigZiwFJM5xyStVDFrs=; b=VqtnFCXf1A8+6MNkNelHYw8vv1Mw1vAmFHodh1V8LNxWwQYSW4FvTj/r7zZv85w1/U YhbrjD6U53S3Zh5EMp99fFfGgZEzPt4fPQFqYNHDO6xqrnaJ2htrT4IEBwsjrJNwMvFF jfCB3XzP0Cr9msrXi50x6zNmnEHgfi18EmrQeEwLhfoa7xzwBGa9yyC30FNUrq0iBH13 0yixKhaQ3zyEW0EXyYckanT/0luyW7iyhSmtArt748FAnMGTIEcio9s+6GA0YnG/avZF etDdOlF8tUGWdGeVQfHGXQmoKXxY/c5Vef6zRxDjCiqZu3W6tvjiHmH2rBkcO2h3cv66 kplg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=gsG4/D9Y; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p20si655864pfi.13.2018.02.21.17.59.58; Wed, 21 Feb 2018 18:00:13 -0800 (PST) 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=fail header.i=@gmail.com header.s=20161025 header.b=gsG4/D9Y; 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=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752146AbeBVB5P (ORCPT + 99 others); Wed, 21 Feb 2018 20:57:15 -0500 Received: from mail-qk0-f195.google.com ([209.85.220.195]:42725 "EHLO mail-qk0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751847AbeBVB5M (ORCPT ); Wed, 21 Feb 2018 20:57:12 -0500 Received: by mail-qk0-f195.google.com with SMTP id b130so4626920qkg.9; Wed, 21 Feb 2018 17:57:11 -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=/cVWF8CB+MbOSFGyeONRyJ08SigZiwFJM5xyStVDFrs=; b=gsG4/D9YXKGQ0GUbCkWsWSnIPOc2tLahk+WiXaGo45R2QoQcs7PqIesywwyByo5GV2 g8Can14GILNdiV9zv1fAsPZ7P5n9ahO74vXyarrNxB3bAclag2Sp0AyyOqVYfs0ZzH3I thV5R+JhOl00J+kZCDfq1f3CmNDm4hQwSImes+xle1Fn8RF6kkNHntKssOO/FFlYN4KN 72dtDnFVDYgaQxzFJ/I0efruy1M1e/DfSJtbP50KoMD1HKYcAcnNM7lBr2657udQIaQy xoMHzPZMjRHeFZYIYOJWHY67Unq+/hF/vc9RM57mJvfJp2CnD4fOKgRNwKo9CEBp5SfM KmWQ== 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=/cVWF8CB+MbOSFGyeONRyJ08SigZiwFJM5xyStVDFrs=; b=R3BoGOJ9MNAiWKTqKxl4bLaTNN3HuIuwA7Xax8XaHfH6Dadq0l/ZUobcndgItiDbut XsJVCzu3iiub0V5dW0Emsw1qiGOYiJ9/U0aq2QV9CZlEZKzrzw0e3Ol4vkZ9K8qijRu+ 2pPNcDy2epiKSq2lN1y0BbyiJuEtAjdYAHY5orPrFhNTESQ92sBk6S+Z2fj4QmyDxhNV ouWxFZZfvvGvzfikPxrOY1tNJDENRZDuU3Gvvav4TfFcJg4KL+zhb8J1Xz3dxocaPd8j wdF5Czq992/+F/lYUD37sYYCYwwlnwxrLO47VXvryQu9AMb64NJdgQME51PpRJS/uY1U NSRA== X-Gm-Message-State: APf1xPC9di16oA3H/ZUCxEK8tyIY/lGBb/UFSPe7jWkS9gwQTYPUjFk3 bg2FS0VUX/7hdAdAGLs1AYc= X-Received: by 10.55.204.71 with SMTP id r68mr8488145qki.356.1519264631512; Wed, 21 Feb 2018 17:57:11 -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 n29sm14182630qtf.18.2018.02.21.17.57.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 21 Feb 2018 17:57:11 -0800 (PST) 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, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, mingo@redhat.com, akpm@linux-foundation.org, dave.hansen@intel.com, benh@kernel.crashing.org, paulus@samba.org, khandual@linux.vnet.ibm.com, aneesh.kumar@linux.vnet.ibm.com, bsingharora@gmail.com, hbabu@us.ibm.com, mhocko@kernel.org, bauerman@linux.vnet.ibm.com, ebiederm@xmission.com, linuxram@us.ibm.com, arnd@arndb.de Subject: [PATCH v12 15/22] selftests/vm: powerpc implementation to check support for pkey Date: Wed, 21 Feb 2018 17:55:34 -0800 Message-Id: <1519264541-7621-16-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1519264541-7621-1-git-send-email-linuxram@us.ibm.com> References: <1519264541-7621-1-git-send-email-linuxram@us.ibm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 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 c47aead..88ef58f 100644 --- a/tools/testing/selftests/vm/pkey-helpers.h +++ b/tools/testing/selftests/vm/pkey-helpers.h @@ -258,7 +258,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; @@ -271,13 +271,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) @@ -323,9 +323,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 e82bd88..58da5a0 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; -- 1.7.1