Received: by 10.223.164.200 with SMTP id h8csp930645wrb; Mon, 6 Nov 2017 01:10:55 -0800 (PST) X-Google-Smtp-Source: ABhQp+Qs1flv972fq1ni3vxv6wmds7227TLXfDMVCvcw/QU2TEPKxJ36RZABPCbh4WvmcKrP6omh X-Received: by 10.84.236.74 with SMTP id h10mr13645241pln.2.1509959455811; Mon, 06 Nov 2017 01:10:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1509959455; cv=none; d=google.com; s=arc-20160816; b=xxwNV2kOz25+6404sX759rm4NPb/D7LFI/JI45k2nTA+eGOloRBqWe/NcgnoC53rvn 593eim8554nglE1COFaUWl99SIy/SyM12Tg9dv1HQ1EEi5RuAMihqMJHpEiRCZ0hlfKl U0+N515lhdqu3QKozel0iws7UrVQm+c1Ddxnkbzbu/UvOvSxEvKS0eI1gv+4U/nV8ZEr uZgbA++UYSFXL7dbuX4ufxQQ9IpiieSk3CG+hIlX9VZToqTU2Ro8WgsD4EIksXU2hWLv 4E8aJY1tyeCRR9Shem7J9znKHnapyIxq9Y9pASneXcC3mbjzMnCde56nx9l0rf/vDyPi Nj2Q== 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=XSfq1+ZqGV7ATe+s3wv2CRc1I9F1j/wO5NvW2IPJI5M=; b=b4vyy/duL4nkYWe+ltM1F2aHhgav49hP8UYpbbmRd+VRixvJul8fvc7wu0hPNqon1F GnxFBBdt1+gsBEvvH31q0YzYT06afBMI/0KXYGWO1bHOmT8nDEHA8it2c+Y9fDsTv3sJ TJxNVIjKuyHjSxDCc7f0ZE5jGo0YRQX5NPFu1WcNU41xeFazflUJfonxoCYn+gzl8GGX jBxa0qpaARZj+Okq4XRpoppEoYNpEGHmFslRgtbrqfMR/SorVLN7OJQPU74JHjFoGdLL E2ajHQMbXqaZzcpbm1qC8+3aoqE+39toz6dnTdO5paMPm9BLJks3sG7Z3wnrA95IzLaX GCVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=Nc3Yc+0f; 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 k70si10882736pge.427.2017.11.06.01.10.42; Mon, 06 Nov 2017 01:10:55 -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=Nc3Yc+0f; 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 S1752508AbdKFI7u (ORCPT + 98 others); Mon, 6 Nov 2017 03:59:50 -0500 Received: from mail-qk0-f195.google.com ([209.85.220.195]:48287 "EHLO mail-qk0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752445AbdKFI7o (ORCPT ); Mon, 6 Nov 2017 03:59:44 -0500 Received: by mail-qk0-f195.google.com with SMTP id a142so1235752qkb.5; Mon, 06 Nov 2017 00:59:43 -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=XSfq1+ZqGV7ATe+s3wv2CRc1I9F1j/wO5NvW2IPJI5M=; b=Nc3Yc+0fWBVBLR0R7F2JCfxxsqvEp86TW1BrsvD4XOVSY6ZWbMsgsLbSK3fIb2rTq2 BEtWjVrdKcd+3bV1npEzubtCN8lgAgMvoDSVd0OPdJzYx63OCBmQR0EbvNczETtgEKIM KLDeEM27pBRccTDaIg0tQET5g43LUc3AXQHRHMQWe5O4kQjwkHHXGfIP6Qpbk6FiwM4F 3r5HD5JtRxsVKZSp7a074NmXYHhjo2ydCdukUnbAWqDRck2abGkNeCxu+nYZl4l/cIqN kOXQ/foNwS/t4ifUJFUQviiqgLQi1B6tLL2uBlGrUnYGqTNHeHmJaQ8pZxfwWTckjgEl kp8g== 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=XSfq1+ZqGV7ATe+s3wv2CRc1I9F1j/wO5NvW2IPJI5M=; b=tOce0SqVmkfpHCMBgajWAFVNuOgHRCLw6+G1oIzAbOy+Lf1jv1GHBLEyNMmQkhAB7Z S2egIJFSXkbkuQYF7RkuR79S+a+VSmSdxV9QNx+O+NDQ8jeYFwhqmNNYgc3tAisP4tus YkjO7u+r7xZsk6YySL9hwLLEffuz4Iai2Cv5tOVMGbt6DiB885VgsfJkOOj1sf9EwEDE KloP93mE3cGzMrNM27gqW17Jr2xGZShgoPL+YoduR2VJ0QvcmfQ0XOf3wf7hm+UucpPY 9nOQ8gqOYyXu4g+I3i5FDC60bxahk92Xq82u9w+i+Jxt2kg1kXERY2uwbpWFoAyJQTEh jSag== X-Gm-Message-State: AJaThX5wCxi1Sqogi8SfT4AB4GgUlJQWmES8eMEaOIFlYNOfqDcym+F6 PPwKM9TtWxY28fPXk5/xAck= X-Received: by 10.55.214.25 with SMTP id t25mr2249968qki.225.1509958783152; Mon, 06 Nov 2017 00:59:43 -0800 (PST) Received: from localhost.localdomain (50-39-103-96.bvtn.or.frontiernet.net. [50.39.103.96]) by smtp.gmail.com with ESMTPSA id r26sm8001094qki.42.2017.11.06.00.59.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Nov 2017 00:59:42 -0800 (PST) From: Ram Pai To: mpe@ellerman.id.au, mingo@redhat.com, akpm@linux-foundation.org, corbet@lwn.net, arnd@arndb.de Cc: linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, x86@kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.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 Subject: [PATCH v9 30/51] Documentation/x86: Move protecton key documentation to arch neutral directory Date: Mon, 6 Nov 2017 00:57:22 -0800 Message-Id: <1509958663-18737-31-git-send-email-linuxram@us.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1509958663-18737-1-git-send-email-linuxram@us.ibm.com> References: <1509958663-18737-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 Since PowerPC and Intel both support memory protection keys, moving the documenation to arch-neutral directory. Signed-off-by: Ram Pai --- Documentation/vm/protection-keys.txt | 85 +++++++++++++++++++++++++++++++++ Documentation/x86/protection-keys.txt | 85 --------------------------------- 2 files changed, 85 insertions(+), 85 deletions(-) create mode 100644 Documentation/vm/protection-keys.txt delete mode 100644 Documentation/x86/protection-keys.txt diff --git a/Documentation/vm/protection-keys.txt b/Documentation/vm/protection-keys.txt new file mode 100644 index 0000000..fa46dcb --- /dev/null +++ b/Documentation/vm/protection-keys.txt @@ -0,0 +1,85 @@ +Memory Protection Keys for Userspace (PKU aka PKEYs) is a CPU feature +which will be found on future Intel CPUs. + +Memory Protection Keys provides a mechanism for enforcing page-based +protections, but without requiring modification of the page tables +when an application changes protection domains. It works by +dedicating 4 previously ignored bits in each page table entry to a +"protection key", giving 16 possible keys. + +There is also a new user-accessible register (PKRU) with two separate +bits (Access Disable and Write Disable) for each key. Being a CPU +register, PKRU is inherently thread-local, potentially giving each +thread a different set of protections from every other thread. + +There are two new instructions (RDPKRU/WRPKRU) for reading and writing +to the new register. The feature is only available in 64-bit mode, +even though there is theoretically space in the PAE PTEs. These +permissions are enforced on data access only and have no effect on +instruction fetches. + +=========================== Syscalls =========================== + +There are 3 system calls which directly interact with pkeys: + + int pkey_alloc(unsigned long flags, unsigned long init_access_rights) + int pkey_free(int pkey); + int pkey_mprotect(unsigned long start, size_t len, + unsigned long prot, int pkey); + +Before a pkey can be used, it must first be allocated with +pkey_alloc(). An application calls the WRPKRU instruction +directly in order to change access permissions to memory covered +with a key. In this example WRPKRU is wrapped by a C function +called pkey_set(). + + int real_prot = PROT_READ|PROT_WRITE; + pkey = pkey_alloc(0, PKEY_DISABLE_WRITE); + ptr = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); + ret = pkey_mprotect(ptr, PAGE_SIZE, real_prot, pkey); + ... application runs here + +Now, if the application needs to update the data at 'ptr', it can +gain access, do the update, then remove its write access: + + pkey_set(pkey, 0); // clear PKEY_DISABLE_WRITE + *ptr = foo; // assign something + pkey_set(pkey, PKEY_DISABLE_WRITE); // set PKEY_DISABLE_WRITE again + +Now when it frees the memory, it will also free the pkey since it +is no longer in use: + + munmap(ptr, PAGE_SIZE); + pkey_free(pkey); + +(Note: pkey_set() is a wrapper for the RDPKRU and WRPKRU instructions. + An example implementation can be found in + tools/testing/selftests/x86/protection_keys.c) + +=========================== Behavior =========================== + +The kernel attempts to make protection keys consistent with the +behavior of a plain mprotect(). For instance if you do this: + + mprotect(ptr, size, PROT_NONE); + something(ptr); + +you can expect the same effects with protection keys when doing this: + + pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ); + pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey); + something(ptr); + +That should be true whether something() is a direct access to 'ptr' +like: + + *ptr = foo; + +or when the kernel does the access on the application's behalf like +with a read(): + + read(fd, ptr, 1); + +The kernel will send a SIGSEGV in both cases, but si_code will be set +to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when +the plain mprotect() permissions are violated. diff --git a/Documentation/x86/protection-keys.txt b/Documentation/x86/protection-keys.txt deleted file mode 100644 index fa46dcb..0000000 --- a/Documentation/x86/protection-keys.txt +++ /dev/null @@ -1,85 +0,0 @@ -Memory Protection Keys for Userspace (PKU aka PKEYs) is a CPU feature -which will be found on future Intel CPUs. - -Memory Protection Keys provides a mechanism for enforcing page-based -protections, but without requiring modification of the page tables -when an application changes protection domains. It works by -dedicating 4 previously ignored bits in each page table entry to a -"protection key", giving 16 possible keys. - -There is also a new user-accessible register (PKRU) with two separate -bits (Access Disable and Write Disable) for each key. Being a CPU -register, PKRU is inherently thread-local, potentially giving each -thread a different set of protections from every other thread. - -There are two new instructions (RDPKRU/WRPKRU) for reading and writing -to the new register. The feature is only available in 64-bit mode, -even though there is theoretically space in the PAE PTEs. These -permissions are enforced on data access only and have no effect on -instruction fetches. - -=========================== Syscalls =========================== - -There are 3 system calls which directly interact with pkeys: - - int pkey_alloc(unsigned long flags, unsigned long init_access_rights) - int pkey_free(int pkey); - int pkey_mprotect(unsigned long start, size_t len, - unsigned long prot, int pkey); - -Before a pkey can be used, it must first be allocated with -pkey_alloc(). An application calls the WRPKRU instruction -directly in order to change access permissions to memory covered -with a key. In this example WRPKRU is wrapped by a C function -called pkey_set(). - - int real_prot = PROT_READ|PROT_WRITE; - pkey = pkey_alloc(0, PKEY_DISABLE_WRITE); - ptr = mmap(NULL, PAGE_SIZE, PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); - ret = pkey_mprotect(ptr, PAGE_SIZE, real_prot, pkey); - ... application runs here - -Now, if the application needs to update the data at 'ptr', it can -gain access, do the update, then remove its write access: - - pkey_set(pkey, 0); // clear PKEY_DISABLE_WRITE - *ptr = foo; // assign something - pkey_set(pkey, PKEY_DISABLE_WRITE); // set PKEY_DISABLE_WRITE again - -Now when it frees the memory, it will also free the pkey since it -is no longer in use: - - munmap(ptr, PAGE_SIZE); - pkey_free(pkey); - -(Note: pkey_set() is a wrapper for the RDPKRU and WRPKRU instructions. - An example implementation can be found in - tools/testing/selftests/x86/protection_keys.c) - -=========================== Behavior =========================== - -The kernel attempts to make protection keys consistent with the -behavior of a plain mprotect(). For instance if you do this: - - mprotect(ptr, size, PROT_NONE); - something(ptr); - -you can expect the same effects with protection keys when doing this: - - pkey = pkey_alloc(0, PKEY_DISABLE_WRITE | PKEY_DISABLE_READ); - pkey_mprotect(ptr, size, PROT_READ|PROT_WRITE, pkey); - something(ptr); - -That should be true whether something() is a direct access to 'ptr' -like: - - *ptr = foo; - -or when the kernel does the access on the application's behalf like -with a read(): - - read(fd, ptr, 1); - -The kernel will send a SIGSEGV in both cases, but si_code will be set -to SEGV_PKERR when violating protection keys versus SEGV_ACCERR when -the plain mprotect() permissions are violated. -- 1.7.1 From 1583223879942244405@xxx Sun Nov 05 11:05:51 +0000 2017 X-GM-THRID: 1583223879942244405 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread