From patchwork Thu Feb 22 01:55:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ram Pai X-Patchwork-Id: 876477 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 3zmz2c00Btz9ryl for ; Thu, 22 Feb 2018 13:34:31 +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="gsG4/D9Y"; 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 3zmz2b4rGDzF0tL for ; Thu, 22 Feb 2018 13:34:31 +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="gsG4/D9Y"; 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:c09::243; helo=mail-qk0-x243.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="gsG4/D9Y"; dkim-atps=neutral Received: from mail-qk0-x243.google.com (mail-qk0-x243.google.com [IPv6:2607:f8b0:400d:c09::243]) (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 3zmyCY33S0zF0yc for ; Thu, 22 Feb 2018 12:57:13 +1100 (AEDT) Received: by mail-qk0-x243.google.com with SMTP id h129so4626634qke.8 for ; Wed, 21 Feb 2018 17:57:13 -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=F4k7+kHftgeia7yFMYa/wvJyDAHaYdhNpxSg20wZhJOg1+Fm5xZutK/83mylmkalbo 7mlXdQXjmZFnflWGjT8HdFpKLv+LxkjrQPyukT6Y3Ji9QL6TH/c1spvhkwy5dth3wxDx nz2ez44nr8T4Aj5CPY0SE9kB7ZyLhlLLCW0UlYMB3m3FxhsebeDIpuVmjSyb7U3g0REw zyHQyvR0MOghEAgBKCGlhywO6h4D7iHsT+4ig1CLCzJvp+nbRPIRDxTNt0C4G1pnYwc/ iPTjM+gCW4ykAXXRu3wDqNwNknZktZutJJKiFJAPQvVm6cpLFnT5Qa67smyl1pWfvSyu qZ0Q== X-Gm-Message-State: APf1xPAbIJeN6WDvtGEPPkEXuzXc0XxXDOvSBbE+1zR03L5m1URZkVOB UZqZu2Kcl5ywNUonPMOCV45FPgz+ X-Google-Smtp-Source: AH8x226exOkrdoyANsxUFFdQgPx8dzKYXKOy8o3LWbJytOdIyhRXE+Wg9SngY2sky8cvwkJifoSzFA== 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 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> X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.26 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. 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;